Code Gen Flashcards Preview

CS 241 > Code Gen > Flashcards

Flashcards in Code Gen Deck (20):
1

What is the WLP4 rule for dereferencing a pointer?

What is the code you need to generate?

factor1 => STAR factor2

code (factor1) =
code(factor2)
lw $3, 0($3)

2

What is the requirement for generating code for NULL

Dereferencing a NULL pointer should throw an error, so we need a value for NULL that will not correspond with an address in memory.

3

What is the WLP4 rule for NULL?

What is the code generated for NULL?

factor => NULL

code(factor) = add $3, $11, $0

4

What is an lvalue?

An lvalue is a value that has a location in RAM and a type associated with that location

5

Why is the rule for address of
factor => AMP lvalue
and not
factor => AMP factor

Prevents us taking the address of non-lvalues such as NULL or 5

6

How many cases are there for taking the address of an lvalue

3, one for every type of value an lvalue can take

7

Generate code for factor => AMP lvalue
if lvalue is an ID

We want the address associated with that lvalue, so we need to find the offset for the ID wrt the frame ptr

code(factor) =
[lookup ID in the symbol table]
lis $3
.word [ID offset from symbol table]
add $3, $3, $29 ;

That last line adds the offset to the frame pointer, giving us the address

8

fp = 0xFC
&y = 0xF0

generate code for
x = &y ;

code(factor) =
lis $3
.word -0xC
add $3, $3, $29

9

Generate code for:
x = &(*y)

For this case, the two operators cancel each other out

code(factor1) = code(factor2)

10

How does lvalue assignment to an int differ from an lvalue assignment to a pointer

statement => lvalue BECOMES expr SEMI

int:
code(statement)=
code(expr)
sw $3, [offset of lvalue]($29)

But in a pointer assignment, we want the pointer to POINT to the same value as expr, we dont want it to BE the same value as expr so:
code(statement) =
code(expr)
push($3)
code(lvalue)
pop($5)
sw $5, 0($3)

11

What happens when you add a pointer and an integer?

You have to multiply the integer by 4 before adding to the pointer value

12

How do you change comparison for pointers?

Use the unsigned versions of mips comparisons since pointers are unsigned

13

What does wain's prolog do vs. every procedures prolog and epilog

Wain's prolog:
- imports external directives (e.g. print)
- initializes our constants (e.g. $4 = 4, $11 = 1 etc...)

Every procedure prolog/epilog
- Initializes frame and its frame ptr
- Save and restore the registers used

14

When a procedure is called, who saves what register?

Caller:
- Saves $31 (overwritten when the called procedure returns)
- Saves $29 (needs to save its own frame ptr)

Callee:
- Saves any register it overwrites

15

What code is generated if I want to call a procedure foo()

push($29)
push($31)
lis $5
.word foo
jalr $5
pop($31)
pop($29)

16

How does the callee initialize its frame ptr?

sub $29, $30, $4

17

How are arguments passed from a procedure caller to the callee?

The caller stores the arguments onto the stack before calling the procedure

18

What is the WLP4 rule for calling a procedure?

Generate code for calling a procedure with 3 args

factor => ID (expr1, expr2, expr3)

push($29)
push($31)
code(expr1)
push($3)
code(expr2)
push($3)
code(expr3)
push($3)
lis $5
.word ID
jalr $5
pop arg
pop($31)
pop($29)

19

Once the parameters have been saved by the caller, generate code for code(procedure)

procedure => INT ID(params) { dcls stmts RETURN expr;}

sub $29, $30, $4
[push all registers that will be overwritten]
code(dcls)
code(stmts)
code(expr)
[pop registers that were saved]
add $30, $29, $4
jr $31

20

What are the order of all the info that is saved on the stack? Who saves what?

- Caller pushes $29
- Caller pushes $31
- Caller pushes arguments
- New frame ptr initialized to one address after $30
- Callee pushes local variables
- Callee saves register values

Note that after this process happens, arguments for the new procedure have positive offsets while local variables have negative offsets