简体   繁体   中英

which is the order that a compiler compiles

Well, i want to know which is the order that a compiler "read" the code. For example:

Suppose I have the following code snippet:

int N, M;

N = M = 0;

In this case, the compiler would separete a part of memory (int, 4 bytes) for N and M, and then, at the second line (where comes my doubt), of two things, one:

The compiler "read" N equals to M and equals both to zero.

OR

The compiler "reads" the zero, put it in memory of M, then get the value of M, that is zero, and put it on memory of N.

In other words, it is from right to left, or from left to right ?

I don't know if became clear my doubt, but in a test that i made:

int i=0; /*I declared the variable i, and assign zero value to it*/

printf("%d", i++); /*Prints 0*/

printf("%d", i); /*Prints 1*/

I understand the above code, at the second line, the compiler seems(from what i undestood)"read" from left to right, assigning to the type %d the i value, and after print, the variable i is incremented, because at the third line it is printed as 1.

The code snippet below, reverses the position of the ++:

int i=0; /*I declared i variable to zero*/

printf("%d", ++i); /*Prints 1*/

printf("%d", i); /*Prints 1*/

In this case, at the second line, (from what i understood) the compiler "reads" from left to right,and when the compiler reads what will be printed (that stay after the comma, what is the name of this space?), first "reads" the ++ and increments the variable below that is i in this case, and then assign to %d to be printed.

In order, which is the order that a compiler "reads" ? I had some teachers that told me the compiler "read" from right to left, from the semicolon(;), but the compiler actually has an order? And if something that i told above are wrong, please, correct me.(I don't speak english very well)

Thanks!

Only the lexical anaylizer reads the source code left to right. The grammar parser builds AST's and reads them in a variety of ways, depending on the particular node it finds.

For expressions, the AST holding the expression may be read in post-order, to generate a postfixed expression, more suitable for a stack based evaluator (easiest to implement).

For assignments (which are indeed expressions), the AST reads and generates code first the RHS, then for the LHS, and then generates the write-to-memory instruction.

For a function call, the AST may be parsed from the last node containing an argument expression to the first one (if using the C calling convention to perform the call).

I understand the above code, at the second line, the compiler seems(from what i undestood)"read" from left to right, assigning to the type %d the i value, and after print, the variable i is incremented, because at the third line it is printed as 1.

That is not the order things are being done in.

When you call i++, i is incremented by 1. The value before the increment happened is then returned.

So your: printf("%d", i++)

is actually doing:

int i = somevalue;
int temp = i;
i = i + 1;
printf("%d", temp);

I is NOT incremented after the printing. It was actually before printing.

and when you do: printf("%d", ++i)

it does:

int i = somevalue;
i = i + 1;
printf("%d", i);

The difference is the temp variable. Your teacher is right in this case that it is going from right to left from the semicolon because:

It has to process the value of I first. Then it can print it. In reality, if you break it all up into a separate line per operation or instruction, it's actually going from top to bottom as shown above.

According to the C++ Standard (and the C Standard)

The assignment operator (=) and the compound assignment operators all group right-to-left.

So in this statement

N = M = 0;

the compiler assigna 0 to M and accotding to the Standard

return an lvalue referring to the left operand

That is in your example 0 that in tuen is assigned to N.

For empirical evidence, I compiled: int M,N; N = M = 0;

with no compiler options, on my mac and disassembled it:

0000000100000f10    pushq   %rbp
0000000100000f11    movq    %rsp,%rbp
0000000100000f14    movl    %edi,0xfc(%rbp)
0000000100000f17    movq    %rsi,0xf0(%rbp)
0000000100000f1b    movl    $0x00000000,0xe4(%rbp)
0000000100000f22    movl    0xe4(%rbp),%eax
0000000100000f25    movl    %eax,0xe0(%rbp)
0000000100000f28    movl    $0x00000000,0xe8(%rbp)
0000000100000f2f    movl    0xe8(%rbp),%eax
0000000100000f32    movl    %eax,0xec(%rbp)
0000000100000f35    movl    0xec(%rbp),%eax
0000000100000f38    popq    %rbp
0000000100000f39    ret

So it looks like the compiler has decided to compile it as: N = 0; M = 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM