Code Gen Flashcards

(20 cards)

1
Q

What is the WLP4 rule for dereferencing a pointer?

What is the code you need to generate?

A

factor1 => STAR factor2

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the requirement for generating code for NULL

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the WLP4 rule for NULL?

What is the code generated for NULL?

A

factor => NULL

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an lvalue?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Generate code for factor => AMP lvalue

if lvalue is an ID

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
fp = 0xFC
&y = 0xF0

generate code for
x = &y ;

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Generate code for:

x = &(*y)

A

For this case, the two operators cancel each other out

code(factor1) = code(factor2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

statement => lvalue BECOMES expr SEMI

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

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens when you add a pointer and an integer?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you change comparison for pointers?

A

Use the unsigned versions of mips comparisons since pointers are unsigned

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When a procedure is called, who saves what register?

A

Caller:

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

Callee:
- Saves any register it overwrites

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A
push($29)
push($31) 
lis $5
.word foo 
jalr $5
pop($31)
pop($29)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does the callee initialize its frame ptr?

A

sub $29, $30, $4

17
Q

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

A

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

18
Q

What is the WLP4 rule for calling a procedure?

Generate code for calling a procedure with 3 args

A

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
Q

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

A

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
Q

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

A
  • 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