0

When it sees the parentheses, does it have the machine do the operations in the parentheses first, or does it distribute out the parentheses?

Like, if I have the following line of code:

int i = 2 * (a + b)

does it compile like this code:

int c = a + b
int i = 2 * c
moonman239
  • 2,023
  • 4
  • 18
  • 23

2 Answers2

3

It works "as-if" the addition is done first, followed by the multiplication. Under the as-if rule, any number of machine instructions that produce the correct result can be used, and in fact it's almost certain that a multiplication instruction will not be used by any optimizing compiler.

Ben Voigt
  • 3,227
  • 21
  • 24
  • "It works 'as-if' the addition is done first," So does it do the addition first, or doesn't it? Does the resulting assembly code look like the assembly code for this C code: "int c = a + b; int i = 2 * c; "? – moonman239 Sep 15 '15 at 20:07
  • @moonman239: `int c = a + b; int i = 2 * c;` is also not guaranteed to do the addition first. – Ben Voigt Sep 15 '15 at 20:16
  • Huh? If you have two assignment calculations, and the second one depends on the first, I am pretty sure the compiler cannot change their order. – Doc Brown Sep 15 '15 at 20:29
  • @DocBrown: And you would be wrong. These variables are not `volatile`, nor is there any synchronization operation. There's a good example at http://preshing.com/20120625/memory-ordering-at-compile-time/ – Ben Voigt Sep 15 '15 at 20:50
  • @BenVoigt: Assuming `a` and `b` are integers, I think a conforming compiler could issue code which computed `2*(a+b)` as `(2*a)+(2*b)`, which would be particularly useful if some previous code had already computed, for example, `2*a`. But the linked article isn't above algebraic manipulation; it's about storage order. In `int c = a + b; int i = 2 * c;`, the *stores* into `c` and `i` can be reordered, but barring the aforementioned algebraic manipulation, the *computation* of `a + b` ("the sum") certainly has to happen before the product or shift. – rici Sep 16 '15 at 03:53
  • @rici: All of the above are allowed under the as-if rule. Also, people think of multiply by two as a shift, but it's perfectly reasonable to use addition instead. Then you have `a + b + a + b`, and for integers, commutative property holds... – Ben Voigt Sep 16 '15 at 03:55
1

One likely sequence of machine instructions for a regular, orthogonal, 3-address RISC CPU would go something like this:

LOAD_MEMORY &a -> register01
LOAD_MEMORY &b -> register02
INT_ADD register01, register02 -> register01
LOAD_CONSTANT 1 -> register02
BIT_SHIFT register01, register02 -> register01
STORE_MEMORY &i, register01

Also, if a is a local variable and the compiler can prove that it won't be accessed after this, it might put i and a at the same memory address.

For a CISC CPU with a more complex irregular instruction set, there might be shift instructions that work on memory, and take constants, and add instructions that work on memory, then it would look something like this:

INT_ADD_MEM &a, &b -> &i
BIT_SHIFT_MEM_CONSTANT &i, 1 -> &i

Again, a and i may have the same address, if a is local and unused afterward.

Jörg W Mittag
  • 101,921
  • 24
  • 218
  • 318