Java Reference
In-Depth Information
(d) We then iterate through the LIR instructions for the block, invoking a method
toSpim() , which is defined for each LIR instruction; there is a one-to-one transla-
tion from each LIR instruction to its SPIM equivalent. For example, the (labeled)
code for block B2 is
Factorial.computeIter.2:
li$t3,0
ble$t1,$t3,Factorial.computeIter.4
jFactorial.computeIter.3
(e) Any string literals that are encountered in the instructions are put into a list,
together with appropriate labels. These will be emitted into a data segment at
the end of the method (see step 2 below).
(f) At the end of the method, we generate code to restore those registers that had
been saved at the start. This code also does a jump to that instruction following
the call in the calling code, which had been stored in the $ra register (ra is a
mnemonic for return address) at the call. This code is labeled so that, once a
return value has been placed in $v0, execution may branch to it to affect the
return. For example, the register-restoring code for computeIter() is
Factorial.computeIter.restore:
lw $ra,32($sp) #Restorereturnaddress
lw $fp,28($sp) #Restoreframepointer
lw $t0,24($sp) #Restoreregister$t0
lw $t1,20($sp) #Restoreregister$t1
lw $t2,16($sp) #Restoreregister$t2
lw $t3,12($sp) #Restoreregister$t3
lw $t4,8($sp) #Restoreregister$t4
lw $t5,4($sp) #Restoreregister$t5
lw $t6,0($sp) #Restoreregister$t6
addiu $sp,$sp,36 #Popstack
jr $ra #Returntocaller
2. After we have generated the text portion (the program instructions) for the method
(notice the .text directive at the start of code for each method), we then populate
a data area, beginning with the .data directive, from the list of string literals con-
structed in step 1(e). Any other literals that you may wish to implement would be
handled in the same way. Notice that computerIter() has no string literals; its data
segment is empty.
3. Once all of the program code has been generated, we then copy out the SPIM code
for implementing the SPIM class. This is where any further system code, which you
may wish to implement, should go.
For example, the code for Factorial.computeIter() is as follows:
.text
Factorial.computeIter:
subu $sp,$sp,36 #Stackframeis36byteslong
sw $ra,32($sp) #Savereturnaddress
sw $fp,28($sp) #Saveframepointer
sw $t0,24($sp) #Saveregister$t0
sw $t1,20($sp) #Saveregister$t1
sw $t2,16($sp) #Saveregister$t2
sw $t3,12($sp) #Saveregister$t3
sw $t4,8($sp) #Saveregister$t4
sw $t5,4($sp) #Saveregister$t5
 
Search WWH ::




Custom Search