Week 5 - Lecture 5 - MIPS ISA
Week 5 - Lecture 5 - MIPS ISA
❑ Procedure conventions:
➢ Caller:
▪ passes arguments to callee
▪ jumps to callee Factorial C Code
❑ Register usage:
➢ Caller puts arguments in $ a0– $ a3, jumps to callee & simultaneously
saves $PC + 4 in register $ra via the instruction jal &(callee)
➢ Some temporary registers (e.g. $s0 – $s7) must be restored to the values
they hold before the procedure was invoked → spill registers to stack.
➢ When the callee finishes calculations, it places the results in $v0 & $v1, and
returns control to the caller using jr $ra.
MIPS call convention: registers
❑ MIPS register file partition:
➢ $a0–$a3: arguments (reg’s 4 – 7)
➢ $v0, $v1: result values (reg’s 2 and 3)
➢ $t0–$t9: temporaries
➢ $s0–$s7: variables
➢ $gp: global pointer for static data (reg 28)
➢ $sp: stack pointer (reg 29)
➢ $fp: frame pointer (reg 30)
➢ $ra: return address (reg 31)
➢ No need for $fp in the example as the stack is adjusted only on entry and exit
of the procedure → can be managed with only $sp.
➢ Stack frame (activation record) appears on the stack whether or not an explicit
frame pointer is used.
Array and Loop
❑ Access large amounts of similar data (in memory)
➢ Index vs Pointer
Element size
Accessing array elements via pointers in a loop
Initialization for result
0x12340010 array[4] variables, loop counter,
and array pointers.
0x1234800C array[3]
0x12348008 array[2]
0x12348004 array[1]
0x12348000 array[0] Label: Work by:
1. Calculating address
2. Load data
3. Perform task
Array indexing involves
▪ Multiplying index by element size Update loop counter and
▪ Adding to array base address array pointers.
Compiling Interpreting
Machine 10010100 Virtual Machine
code 11001101
Machine
Running code
Program
Hardware Outputs
outputs Hardware
❑ Characteristics
Compiler Interpreter
How it converts the input? Entire program at once Read one instruction at a time
When is it needed? Once, before the 1st run Every time the program is run
Compilation
Interpretation
Anatomy of a compiler
❑ Frontend (analysis phase)
➢ Read source code text & break it up into
meaningful elements (lexeme) & gen. tokens
▪ token = {type, location} of a lexeme
➢ Check correctness, report errors
▪ e.g. “3x” is an illegal token in C
➢ Translate to intermediate representation
▪ IR = machine-independent language
▪ e.g. three-address code (3AC)
❑ Backend (synthesis)
➢ Optimize IR
▪ reduces #operations to be executed
➢ Translate IR to assembly & further optimize
▪ take advantage of particular features of
the ISA
▪ e.g. mult ← sll
Front end stages: Lexical Analysis
❑ Lexical Analysis (scanning)
➢ Source → list of tokens.
Front end stages: Syntax Analysis
❑ Syntax Analysis (parsing)
➢ Tokens → syntax tree = syntactic structure of the original source code (text)
Front end stages: Semantic Analysis
❑ Semantic Analysis
➢ Mainly type checking, i.e. if types of the operands are compatible with the
requested operation.
Symbol table
Front end stages: Intermediate
representation (IR)
❑ Internal compiler language that is
➢ Language-independent
➢ Machine-independent
➢ Easy to optimize
❑ Typical optimizations:
➢ Dead code elimination: eliminate assignments to variables that are never
used and basic blocks that are never reached.
➢ Constant propagation: identify variables that have a constant value &
substitute that constant in place of references to the variable.
➢ Constant folding: compute expressions with all constant operands.
➢ Example: optimize
IR optimization example: 1st batch of
passes
❑ First 3 passes:
1. Dead code elimination: remove the assignment to z in the first basic block.
2. Constant propagation: replace all references to x with the constant 3.
3. Constant folding: compute constant expressions: y=3+7=10 & _t1=3/2=1
IR optimization example: subsequent
batches of passes
Repetition of simple
optimizations on CFG
=
Very powerful optimizations
No further
optimization found
Backend stages: Code generation
❑ Translate IR to assembly
➢ Map variables to registers (register allocation)
▪ Code generator assigns each variable a dedicated register.
▪ If #variables > #registers, map some less frequently used variables to
Mem and load/store them when needed.
➢ Translate each assignment to instructions
▪ Some assignments requires > 1 instructions.
➢ Emit each basic block
▪ Codes + appropriate labels and branches.
➢ Reorder basic block code wherever possible
▪ to eliminate superfluous jumps.
➢ Perform ISA- and CPU-specific optimizations
▪ e.g. reorder instructions to improve performance.
Compiler output: an object file example
Object file Compiled (assembly) code
Executable
Object file header Text Size Data Size .data
f:
0x34 (52 bytes) 0xC (12 bytes) g:
y:
Text segment Address Instruction .text
0x00400000 0x23BDFFFC main:
addi $sp,addi
$sp, -4$sp, $sp, -4 #init stack
0x00400004 0xAFBF0000 sw $ra,sw 0 ($sp)$ra, 0($sp) # push $ra
0x00400008 0x20040002 addi $a0,addi
$0, 2 $a0, $0, 2 # $a0 = 2
0x0040000C 0xAF848000 sw $a0,sw 0x8000 $a0,
($gp) f # f = 2
0x00400010 0x20050003 addi $a1,addi
$0, 3 $a1, $0, 3 # $a1 = 3
0x00400014 0xAF858004 sw $a1,sw 0x8004 $a1,
($gp) g # g = 3
0x00400018 0x0C10000B jal sum
jal 0x0040002C # call sum
0x0040001C 0xAF828008 sw $v0,sw 0x8008 $v0,
($gp) y # y = sum()
0x00400020 0x8FBF0000 lw $ra,lw 0 ($sp)$ra, 0($sp) # old $ra
0x00400024 0x23BD0004 addi $sp,addi
$sp, -4$sp, $sp, 4 # pop stack
0x00400028 0x03E00008 jr $ra jr $ra # return
0x0040002C 0x00851020 sum:$v0,add
add $v0, $a0, $a1
$a0, $a1
0x00400030 0x03E00008 jr $ra jr $ra
❑ Dynamic Linking
➢ Static linking: library routines is part of the executable code → will not be
updated with new versions. Furthermore, it loads all routines in the library
even if those are not executed (image bloat ).
➢ Dynamically linked libraries (DLLs): only link/load library procedure when it
is called → keep location of nonlocal procedures and their names.
Linking object files example
+
+
Loader
❑ Load from image file on disk into memory for execution
1. Read header to determine segment sizes
2. Create virtual address space
3. Copy text and initialized data into memory
▪ Or set page table entries so they can be faulted in
4. Set up arguments on stack
5. Initialize registers (including $sp, $fp, $gp)
6. Jump to startup routine
▪ Copies arguments to $a0, … and calls main
▪ When main returns, do exit syscall