y=x++;
In how many steps does it break at compiler level without optimization not at CPU level or instructions ? Does any temporary is created to assign x to y or, it happens directly ?
y=x++;
In how many steps does it break at compiler level without optimization not at CPU level or instructions ? Does any temporary is created to assign x to y or, it happens directly ?
C++ follows the as-if principle, so the answer really is "n" steps.
x
to y
. 2) increment x
.y
is never used y
is declared as int& y = x;
x
and y
are used in the code. –
Diphenyl == x
x
is incrementedx
(kept in that temp variable) is assigned to the y
These will be the basic steps.
Depends on what you mean by "primitive" - if you mean CPU instructions, then the answer will be different.
You may want to read more about postfix and prefix increment/decrement operators.
It depends on the architecture for which you compile this code.
C++ doesn't specify things like these, or even what the possible "primitive steps" are.
One possible answer, for the venerable M68000 CPU, might be:
move.l x, d0
addq.l #1, x
move.l d0, y
So, this uses three instructions (primitive steps), and one temporary in the form of the register d0
.
Note: it's been ... a while since I wrote M68k assembly, I do hope the above code is valid but seem to be having issues reaching relevant reference pages at the moment. As an illustration, I'm pretty sure it's valid.
Depends on what do you mean primitive step, if primitive step is CPU instructions, I did a quick test like below:
(linux environment, g++ 4.6.3) use g++ -S main.cpp to show what CPU instructions are generated by g++, I got below output:
movl -8(%ebp), %eax<br>
movl %eax, -4(%ebp)<br>
addl $1, -8(%ebp)
So that means g++ will copy y to x, then increase x. In this case, 3 CPU instructions (if you consider them as primitive steps) are generated.
Please be noted that with different optimization level of compiler and different CPU architecture, the result will be different.
See Operators Precedence Table
Steps happen as follows:
A temporary variable is assigned with x
's value.
x
is incremented by 1.
y
is assigned with temporary variable value.
So, 3 primitive steps at abstract level.
x++
must be completely evaluated befor the assignment, this order is wrong. The temporary is created with the old value of x
, not the incremented one. –
Creolacreole Then after lot's of optimization stages it's handled with perhaps two instructions:
add r0, r1, #0
add r1, r1, #1
Thanks for the comments -- my point anyway was that a compiler might need to do some steps just to read a variable. (is it global or in a local stack frame or does it have an absolute address given by linker file...)
EDIT: of course this is not the end of the story: what kind of variables x & y are? Are they longer than the machine register width? Are they shorter and requiring zero-, or sign extension? Maybe they are floats or doubles and require call to external library...
x
and y
. –
Diphenyl © 2022 - 2024 — McMap. All rights reserved.
x=y++;
and assembler. Perhaps the compiler will translate the object code tox=y; y=y+1;
in between, but that's really just the same thing minus some syntactic sugar. – Funds