|Writing a C Compiler: lvalues firstname.lastname@example.org (=?ISO-8859-1?Q?Andr=E9_Wagner?=) (2010-05-08)|
|Re: Writing a C Compiler: lvalues email@example.com (Ben Bacarisse) (2010-05-09)|
|Re: Writing a C Compiler: lvalues firstname.lastname@example.org (bart.c) (2010-05-09)|
|Re: Writing a C Compiler: lvalues email@example.com (Tom St Denis) (2010-05-09)|
|Re: Writing a C Compiler: lvalues firstname.lastname@example.org (Keith Thompson) (2010-05-09)|
|Re: Writing a C Compiler: lvalues email@example.com (Eric Sosman) (2010-05-09)|
|Re: Writing a C Compiler: lvalues firstname.lastname@example.org (Stargazer) (2010-05-10)|
|[10 later articles]|
|Date:||Sat, 8 May 2010 06:34:15 -0700 (PDT)|
|Keywords:||C, parse, question|
|Posted-Date:||09 May 2010 12:25:45 EDT|
I'm writing a C compiler. It's almost over, except that is not
handling lvalues correctly.
Let me show a example. The code "x = 5" (let's say 'x' was declared
before) yields this in pseudo-assembly:
mov $b, $fp+8 ; $fp+8 is 'x' addess, so I'm storing x's address in
mov $a, 5
mov [$b], $a ; here I'm putting what's in $a in the address
pointed to $b
Since 'x' is a lvalue in this case, I don't need its value, just the
address of the variable.
Now, if I want to access 'x' in the middle of a non-lvalue expressing,
I would do:
mov $a, $fp+8
mov $a, [$a]
Notice how I get the varible addres, and from it, the value.
What I'm trying to say is: the compiler yields different assembly code
for when 'x' is a lvalue and when 'x' is not a lvalue.
This gets more confusing when I have expressions such as 'x++'. This
is simple, since 'x' is obviously a lvalue in this case. In the case
of the compiler, I can parse 'x' and see that the lookahead points to
'++', so it's a lvalue.
But what about '(x)++'? In this case, the compiler evaluates the
subexpression '(x)', and this expression results the value of 'x', not
the address. Now I have a '++' ahead, so how can I know the address of
'x' since all that I have is a value?
All documentation that I found about lvalues were too vague, and
directed to the programmer, and not to the compiler writer. Are there
any specific rules for determining if the result of a expression is a
Thanks in advance,
[I believe the usual approach is to translate the expression into an
AST before doing much else, which has the useful effect of making the
parentheses go away. As you've found, in C you have to treat (x) and x
the same. It's not Fortran. -John]
Return to the
Search the comp.compilers archives again.