This is 2nd post in the series of assembly code generation.
Part - I
In this post, I am writing the possible assembly code generated from a typical if expression.
cgen(if e1 == e2 then e3 else e4)
#generate code for e1 that will put
#e1 evaluation in \$a0
cgen(e1)
sw \$a0 0(\$sp) #push the result to stack
addiu \$sp \$sp 4
#generate code for e2 evaluation, this will
#will put result of e2 evaluation in \$a0
cgen(e2)
#load the result of e1 evaluation
#from stack in temp register \$t1
lw \$t1 4(\$sp)
addiu \$sp \$sp 4
#check if \$a0 and \$t1 are equal, jump to
#label true_branch if they are equal or else
#continue
beq \$a0 \$t1 true_branch
#we come to this this point only if value of
#e1 and e2 evaluation were different, here
#we generate code for else case
cgen(e4)
#after code for else branch, unconditionally jump to label
#endif which is the label generated in the
#end
jumpto endif
#true_branch label, we come here if e1 and e2 evaluated
#to same value
true_branch:
cgen(e3)
#the label endif
endif:
#code beyond the if expression follows...
Saturday, July 7, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment