rui314 / 8cc

A Small C Compiler
MIT License
6.12k stars 742 forks source link

Consider new PointerArith Node type #61

Closed andrewchambers closed 9 years ago

andrewchambers commented 9 years ago

I think reordering the Left and right children on a '+' node when the left side is a pointer is bad. This adds implicit assumptions about the structure of nodes.

Another way to solve this is to introduce a pointer arith node type. Have you considered that? Just close this issue if you disagree.

rui314 commented 9 years ago

I think enforcing layout is not actually bad. The point is that the + operator in C is not associative -- you cannot swap rhs and lhs in general. It's doable only when both types are arithmetic (non-pointer) types. Naturally, pointer minus something needs to be represented as a node with the pointer on lhs and the arithmetic value on rhs. Making pointer addition look similar to that feels practical.