How many primitive steps in y=x++? [closed]
Asked Answered
K

6

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

Kickback answered 30/10, 2012 at 10:39 Comment(5)
What do you mean by 'primitive'?Hansom
Compile it and look at the generated machine code.Inhere
There is no middle step as such between x=y++; and assembler. Perhaps the compiler will translate the object code to x=y; y=y+1; in between, but that's really just the same thing minus some syntactic sugar.Funds
@Funds : I didn't get you .. what does middle step mean ?..My intend to ask this question is to know about,In how many steps compiler breaks this one line statementKickback
@Kickback It sounds like you are asking for the completely compiler-dependent part, ie how it does pre-processing, pasing and so on. The only thing that should matter is how the actual binary ends up. What a particular compiler does in between isn't standardized and mildly useful to know. If you are for some reason interested in such, GCC is open source, just go and check for yourself.Funds
F
15

C++ follows the as-if principle, so the answer really is "n" steps.

  • it could be 2: 1) assign x to y. 2) increment x.
  • it could be 1 if y is never used
  • it could be undefined behavior, is y is declared as int& y = x;
  • it could be 100 assignments and reassignments that in the end yield the same result.
Frydman answered 30/10, 2012 at 10:44 Comment(3)
It could even be 0 if this is the last time both x and y are used in the code.Diphenyl
Nice answer, +1 (I totally assumed that the variables are valid)Woothen
@Diphenyl yes, it can be 0 as well :)Frydman
W
8
  1. a temp variable is created with value == x
  2. x is incremented
  3. the old value of x (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.

Woothen answered 30/10, 2012 at 10:41 Comment(2)
Yeah you are right Kiril .. I know primitive depends on the CPU instructions but I wanted to know the flow of operation ...and as you said temporary is created , I was guessing the same ...Kickback
This will be the standard flow, without any optimizations. Please note, that if there are any optimizations, made by your compiler, this could be wrong. I mean - the temp could be avoided, for example.Woothen
T
2

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.

Toxic answered 30/10, 2012 at 10:40 Comment(1)
Thanks , but I just wanted actual flow of this operationKickback
V
2

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.

Veiling answered 30/10, 2012 at 11:9 Comment(0)
E
1

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.

Election answered 30/10, 2012 at 10:43 Comment(4)
Since 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
As far as I got the answers I think this should be temp=x, x=x+1 and last y=temp ...am I right?Kickback
Oh .. yes, missed it. corrected. thanks.Election
@Omkant: a reasonably smart compiler may dispense with the temporary in this case (assign x to y, then increment x), although in practice you may see all the work done in registers, which act as temporaries.Adore
M
0
  • allocate register for address of x
  • load address of x
  • allocate temporary register for x
  • dereference x
  • allocate register for y
  • assign y<=x
  • increment x
  • write back x
  • allocate temporary register for address of y
  • load address of y
  • store y

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

Mockheroic answered 30/10, 2012 at 10:44 Comment(3)
Might want to re-think that, I'm pretty sure that ARM instruction does not alter two variables... The statement changes both x and y.Diphenyl
Apparently I though is as ++x (with the premise that x is not accessed anymore -- although in that case an optimizer could just reassign the register that contained x to contain y from that on -- and that wouldn't actually need any instructions.Mockheroic
Well, that also changes two variables :)Diphenyl

© 2022 - 2024 — McMap. All rights reserved.