Re: Writing a C Compiler: lvalues

Keith Thompson <kst-u@mib.org>
Mon, 17 May 2010 09:39:05 -0700

          From comp.compilers

Related articles
[3 earlier articles]
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)
Re: Writing a C Compiler: lvalues esosman@ieee.org (Eric Sosman) (2010-05-17)
Re: Writing a C Compiler: lvalues kst-u@mib.org (Keith Thompson) (2010-05-17)
Re: Writing a C Compiler: lvalues kst-u@mib.org (Keith Thompson) (2010-05-19)
Re: Writing a C Compiler: lvalues bartc@freeuk.com (bart.c) (2010-05-19)
Re: Writing a C Compiler: lvalues lawrence.jones@siemens.com (2010-05-19)
Re: Writing a C Compiler: lvalues kst-u@mib.org (Keith Thompson) (2010-05-19)
Re: Writing a C Compiler: lvalues DrDiettrich1@aol.com (Hans-Peter Diettrich) (2010-05-20)
Re: Writing a C Compiler: lvalues s_dubrovich@yahoo.com (2010-05-22)
[1 later articles]
| List of all articles for this month |
From: Keith Thompson <kst-u@mib.org>
Newsgroups: comp.lang.c,comp.compilers
Date: Mon, 17 May 2010 09:39:05 -0700
Organization: None to speak of
References: 10-05-036 10-05-095
Keywords: C
Posted-Date: 19 May 2010 00:37:43 EDT

Marc van Lieshout <marc@lithia.nl> writes:
[...]
> An lvalue is an expression that evaluates to an address, so it *can* be
> used on the left hand side of an assignment.


Note that this is cross-posted to comp.lang.c and comp.compilers.
I'm posting this from comp.lang.c, and I'm using the C standard's
definitions of terms. C's definition of "lvalue" isn't necessarily
consistent with wider usage. For details, see
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>,
section 6.3.2.1.


No an lvalue is (roughly) an expression that *designates an object*.
(I say "roughly" because ``*ptr'' is an lvalue even if ptr==NULL.)


An lvalue can designate an object that has no address, such as
a bit field or a register variable. Conversely, an expression
that evaluates to an address, such as ``&obj'' is not necessarily
an lvalue.


The distinction, in C, between computing the address of an object
and "designating" an object is subtle but important.


It's very likely that the code generated for evaluating an lvalue will
compute the address of the designated object, which is relevant if
you're writing a compiler, but as far as C is concerned that's an
implementation detail that's not covered by the standard.


> But this is not necessarily
> the case. In an expression like (x + 5) x *is* an lvalue, but it isn't
> used as such, so it should be compiled as an ordinary rvalue.


C99 6.3.2.1p2:


        Except when it is the operand of [list of operators deleted],
        an lvalue that does not have array type is converted to the value
        stored in the designated object (and is no longer an lvalue).


So in (x + 5), x *isn't* an lvalue, even though it started out as one.


[...]


--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>


Post a followup to this message

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