From: Maarten B. <sou...@ds...> - 2014-01-04 09:45:24
|
Erik, > Both C99 and C11 (and probably the earlier specs as well, but I don't have > them handy to check) specifically say the "expression ++E is equivalent > to (E+=1)". Later when discussing the semantics of assignment operators, > it says an "assignment expression has the value of the left operand after > the assignment, but is not an lvalue". So I think SDCC should really be > rejecting "++a = b" rather than accepting it as it currently does. Thank you. I had read this part but still overlooked it :( So, ++a is not an lvalue and should generate an error. >> The order is not guaranteed? Are you saying C can interpret ++a=b as >> a=b;++a ? Or even c=a+1;a=b;a=c ? >> >> Maarten > > The ordering has some flexibility, but there still some constraints. The C > standard defines certain points in time in the execution of the program > called "sequence points". Between consecutive sequence points, when there > are multiple side effects (such as modifying a variable's value), these > side effects are allowed to occur in any order. > > Without getting into the all of details of how sequence points are > defined, a basic expression statement that does not call a function or use > the logical AND, logical OR, conditional ( ? : ), or comma operators will > be bounded by the same pair of sequence points and so the order in which > any variables are modified is left to the compiler implementation. Some > example expressions with ambiguous results: > > x = (x = 1) + 1; /* final value for x may be either 1 or 2 */ > > x = x++; /* final value for x may or may not reflect the increment */ > > The comma operator (as well as the other operators I noted above) does > introduce a new sequence point, so any side effects in the left expression > will complete (in some order) before any side effects in the right > expression. > > a = a + 1, a = b; /* final value for a is always b */ > > but as I mentioned, ++a is expanded as (a+=1) not (a=a+1,a) > > Erik So if ++a was an lvalue, it still contains no sequence point. And thus ++a=b could be interpreted as (?) a=a+1, a=b c=a+1, a=b, a=c a=b, a=a+1 Maarten |