Question: funcName: addi sp , sp , - 1 2 8 # assume 1 2 8 bytes is enough sw ra , 0 ( sp )
funcName: addi sp sp # assume bytes is enough sw rasp sw fpsp mv fp sp # set frame pointer to this space # now params can be at fpfpfpfpfp and fp # and locals can be at fp and so on or right after the actual parameter count # # code for your function body here # mv sp fp # for safety, restore sp from fp lw rasp lw fpsp addi sp sp ret
please fix this code to generate the instructions in the above output
case ASTASSIGNMENT:
fprintfoutt#assignment
;
genCodeFromASTreenodechildout; child is right hand side
if nodevarKind VGLOBAL
fprintfouttswtts
nodestrval;
else if nodevarKind VPARAM nodevarKind VLOCAL
fprintfouttswttdfp
nodeival ;
else if nodevarKind VGLARRAYchild
fprintfouttadditsp sp
tswttsp
; save RHS value onto stack
genCodeFromASTreenodechildout; generate code for index expression
fprintfouttlatts
Load base address of the array into t
tmultt t
Multiply index t by size of int into t
taddtt t t
Add offset to base address t t t
tlwttsp
Pop RHS value from the stack into t
tadditsp sp
Restore stack pointer
tswttt
Store value into array element
nodestrval;
else
fprintfout"Unknown variable kind assignment
;
break;
case ASTFUNCTION:
fprintfouts:
tadditsp sp# assume bytes is enough
tswtrasp
tsw fpsp
tmvtfp sp nodestrval;
ptr nodechild;
while ptr NULL
fprintfout
tswtaddfp
ptrival, ptrival ;
ptr ptrnext;
genCodeFromASTreenodechild hval, out; function body
fprintfouttmv sp fp # for safety, restore sp from fp
tlwtrasp
tlwtfpsp
tadditp sp
tret
;
break;
case ASTVARREF:
if nodevarKind VGLOBAL
Load the global variable directly
fprintfouttlwtts
nodestrval;
else if nodevarKind VPARAM nodevarKind VLOCAL
Load the variable using its offset from the frame pointer
fprintfouttlwttdfp
nodeival ;
else if nodevarKind VGLARRAY child is the index
genCodeFromASTreenodechild hval, out; Generate index expression code
fprintfout
tlatts
Load base address of the array into t
tmultt t
Multiply index t by size of int into t
taddtt t t
Add offset to base address t t t
tlwttt
Load the array element into t
nodestrval
;
else
fprintfout "Unknown variable kind reference
;
break;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
