Re: Writing a C Compiler: lvalues

Ben Bacarisse <ben.usenet@bsb.me.uk>
Sun, 09 May 2010 17:56:26 +0100

          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)
Re: Writing a C Compiler: lvalues marc@lithia.nl (Marc van Lieshout) (2010-05-16)
[9 later articles]
| List of all articles for this month |
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups: comp.lang.c,comp.compilers
Date: Sun, 09 May 2010 17:56:26 +0100
Organization: Zen Internet
References: 10-05-036
Keywords: C, design
Posted-Date: 09 May 2010 16:06:01 EDT

AndrC) Wagner <andre.nho@gmail.com> writes:


> I'm writing a C compiler. It's almost over, except that is not
> handling lvalues correctly.
<snip>
> 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.


Yes, that's normal -- at least as the level of the abstract machine
which seems to be roughly what yo pseudo-assembler is.


> 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?


The C standard (draft PDF available here[1]) tells you which expression
forms denote lvalues and which don't. As you traverse the parse tree,
the "lead operator" of the tree will tell you whether you need l- or
r-value evaluation. The result will be rather naive code, but it is a
start.


[1] http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf
<snip>
--
Ben.



Post a followup to this message

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