Writing a C Compiler: lvalues

=?ISO-8859-1?Q?Andr=E9_Wagner?= <andre.nho@gmail.com>
Sat, 8 May 2010 06:34:15 -0700 (PDT)

          From comp.compilers

Related articles
Writing a C Compiler: lvalues andre.nho@gmail.com (=?ISO-8859-1?Q?Andr=E9_Wagner?=) (2010-05-08)
Re: Writing a C Compiler: lvalues ben.usenet@bsb.me.uk (Ben Bacarisse) (2010-05-09)
Re: Writing a C Compiler: lvalues bartc@freeuk.com (bart.c) (2010-05-09)
Re: Writing a C Compiler: lvalues tom@iahu.ca (Tom St Denis) (2010-05-09)
Re: Writing a C Compiler: lvalues kst-u@mib.org (Keith Thompson) (2010-05-09)
Re: Writing a C Compiler: lvalues esosman@ieee.org (Eric Sosman) (2010-05-09)
Re: Writing a C Compiler: lvalues stargazer3p14@gmail.com (Stargazer) (2010-05-10)
[10 later articles]
| List of all articles for this month |
From: =?ISO-8859-1?Q?Andr=E9_Wagner?= <andre.nho@gmail.com>
Newsgroups: comp.lang.c,comp.compilers
Date: Sat, 8 May 2010 06:34:15 -0700 (PDT)
Organization: Compilers Central
Keywords: C, parse, question
Posted-Date: 09 May 2010 12:25:45 EDT

Hello,


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
$b
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
lvalue?


Thanks in advance,


Andri
[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]



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.