Question: myFunc: addi sp , sp , - 1 2 8 # assume 1 2 8 bytes is enough sw ra , 0 ( sp )
myFunc:
addi sp sp # assume bytes is enough
sw rasp
sw fpsp
mv fp sp
sw afp
sw afp
sw afp
la tSC
mv a t
jal printStr
la tSC
mv a t
jal printStr
#assignment
li t
sw t x
mv sp fp # for safety, restore sp from fp
lw rasp
lw fpsp
addi sp sp
ret
The above is my output, please can you fix the code to match the Description of Needed Functionality below
Short Description of Needed Functionality.
Parameters up to six: See the info page, but in short, to use parameters we first store them from the argument registers onto the stack and then access them using a constant offset from the frame pointer fp register. Our symbol table record and our ASTVARREF node for each parameter must remember the parameter position starting with perhaps in the ival field; then when we generate code we can use an offset based on ival that accesses the correct position on the stack. We should also set the varKind field to VPARAM. see the updated addSymbol function; it has a new last parameter
Local variables: Local variables are also stored on the stack, so we need to open up space for them, too. We should also use the ASTVARREF ival field to store a position number for them; it is most useful to just continue the parameter counter into the local variables, and give them position counts that continue from the parameters. This way they can be handled exactly the same in code generation. We should also set the varKind field to VLOCAL.
# Function prologue addi sp sp # Allocate bytes on the stack sw rasp # Save return address sw fpsp # Save old frame pointer mv fp sp # Set new frame pointer # Parameters are at offsets from fp # Local variables start at offset fp # Function body code here... # Function epilogue mv sp fp # Restore stack pointer from frame pointer lw rasp # Restore return address lw fpsp # Restore old frame pointer addi sp sp # Deallocate stack space ret # Return from function # AST Assignment Handling case ASTASSIGNMENT: fprintfoutt# assignment
; genCodeFromASTreenodechild out; Generate code for RHS if nodevarKind VGLOBAL fprintfouttswtts
nodestrval; Store in global variable else if nodevarKind VPARAM nodevarKind VLOCAL fprintfouttswttdfp
nodeival ; Store in localparam else if nodevarKind VGLARRAY Handle global array assignment fprintfouttadditsp sp
tswttsp
; Save RHS value onto stack genCodeFromASTreenodechild out; Generate index expression code fprintfouttlatts
Load base address of array into ttmultt t
Multiply index by size of int taddtt t t
Add offset to base address tlwttsp
Pop RHS value from stack into ttadditsp sp
Restore stack pointer tswttt
Store value into array element nodestrval; else fprintfout"Unknown variable kind assignment
; break; # Function Handling in AST case ASTFUNCTION: fprintfouts:
"additsp sp
# assume bytes is enough
swtrasp
swtfpsp
mvtfp sp
nodestrval; ptr nodechild; while ptr NULL fprintfoutswtaddfp
ptrival, ptrival ; ptr ptrnext; genCodeFromASTreenodechild hval, out; Generate function body fprintfoutmvtsp fp
# for safety restore sp from fp
lwtra sp
lwtfp sp
"additsp sp
"ret
; break; # Variable Reference Handling in AST case ASTVARREF: if nodevarKind VGLOBAL fprintfoutlwtts
nodestrval; else if nodevarKind VPARAM nodevarKind VLOCAL fprintfoutlwttdfp
nodeival ; else if nodevarKind VGLARRAY genCodeFromASTreenodechild hval, out; Generate index expression code fprintfoutlatts
"multtt
"addtttt
lwttt
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
