Java Reference
In-Depth Information
sw $t6,0($sp) #Saveregister$t6
addiu $fp,$sp,32 #Saveframepointer
Factorial.computeIter.0:
Factorial.computeIter.1:
li$t0,1
move$t1,$a0
move$t2,$t0
Factorial.computeIter.2:
li$t3,0
ble$t1,$t3,Factorial.computeIter.4
jFactorial.computeIter.3
Factorial.computeIter.3:
li$t4,-1
add$t5,$t1,$t4
mul$t6,$t2,$t1
move$t2,$t6
move$t1,$t5
jFactorial.computeIter.2
Factorial.computeIter.4:
move$v0,$t2
jFactorial.computeIter.restore
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
This code is emitted, together with the code for other Factorial methods and the SPIM
class to a file ending in .s ; in this case, the file would be named Factorial.s . This file can
be loaded into any SPIM interpreter and executed.
You will notice, there are lots of moving values around among registers. In Chapter 7,
we address how that might be minimized.
6.3.7 Peephole Optimization of the SPIM Code
You might have noticed in the SPIM code generated in the previous section, that some
jumps were superfluous. For example, at the end of the SPIM code above, the jump at the
end of block B4 simply jumps to the very next instruction! There might also be jumps to
jumps. Such code can often be simplified. Jumps to the next instruction can be removed,
and jumps to jumps can be simplified, (sometimes) removing the intervening jump.
We call such simplification peephole optimization because we need to consider just a few
instructions at a time as we pass over the program; we need not do any global analysis.
To do peephole optimization on the SPIM code, it would be easier if we were to keep a
list (perhaps an array list) of SPIM labels and instructions that we emit, representing the
instructions in some easily analyzable way. Once the simplifications are done, we can spit
out the SPIM instructions to a file.
 
Search WWH ::




Custom Search