CSC448: Code Generation: Code Generation for Assignment Statements I [72/133] |
Corin did not show
Global variables, parameter variables, this
variable, and local variables each store a pointer to an
object, not the object itself.
When we assign from one variable to another, we need only copy the pointer.
For example, from:
method sort () : LinkedList { var tmpvar449 : Tree; var tmpvar485 : Tree; var tmpvar486 : LinkedList; (tmpvar485 := (this.toTree ())); (tmpvar449 := tmpvar485); (tmpvar486 := (tmpvar449.toLinkedList ())); return tmpvar486; }
The code generator produces:
/* method sort of clazz LinkedList */ .align 4 .global LinkedList$sort LinkedList$sort: pushl %ebp movl %esp, %ebp subl $12, %esp /* var tmpvar449 : Tree; */ /* stored in -4(%ebp) */ /* var tmpvar485 : Tree; */ /* stored in -8(%ebp) */ /* var tmpvar486 : LinkedList; */ /* stored in -12(%ebp) */ ... some assembly language removed here ... /* (tmpvar449 := tmpvar485); */ movl -8(%ebp), %eax movl %eax, -4(%ebp) ... some assembly language removed here ...
We call getLocation ("tmpvar485")
for the first
assembly language line, and getLocation
("tmpvar449")
for the second line.
The translation of each statement assumes that
eax
and ecx
are scratch registers,
which means that their values do not need to be preserved.
The line of COOL code in a comment above each group of assembly language lines is very helpful when debugging the code generator.