Expression
Given an expression, how do we transform that into Assembly code.
Learned in CS241E.
Technique 1: Using stack
- GOOD: The technique is general in that it can generate correct code for expressions of arbitrary depth. The stack can hold an arbitrary number of results of subexpressions.
- BAD: The generated code contains many pushes and pops on the stack, and is thus not very efficient in terms of code size and execution speed.
- BAD: The use of the stack makes the generated code complicated and difficult to understand, not only for people, but especially for later passes in the compiler that might optimize or improve the code further.
Technique 2: Using variables (Virtual Registers)
- GOOD: The generated code is simple and flexible, and easy for a compiler to improve and optimize further.
- BAD: Machine language arithmetic instructions operate on registers, not directly on variables, so we cannot directly express expressions such as
a*b
orv1 + v2
as a single machine language instruction. - BAD: To further translate this code into efficient assembly language, we need a way to map variables to registers, rather than to stack locations, and to map a large number of variables to a limited number of registers.
A technique to alleviate these last two disadvantages is Register Allocation.
Technique 3: variables for temporary values, arithmetic operations on registers (Used in assignment)