Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compilers Jakub Yaghob

Similar presentations


Presentation on theme: "Compilers Jakub Yaghob"— Presentation transcript:

1 Compilers Jakub Yaghob
Computer skills Compilers Jakub Yaghob

2 Compiler Simplified definition
Translates a source code in an input language to a target code of a given CPU program test; var s,i:integer; a:array[1..5] of integer; begin for i:=1 to 5 do a[i]:=I; s:=0; s:=s+a[i]; writeln(s); end. compiler .obj linker .exe

3 Control structures translation – if-then-else
if A<>B then S1 else S2 lw $t1,A($zero) lw $t2,B($zero) beq $t1,$t2,L1 S1 j L2 L1: S2 L2:

4 Control structures translation – while
while A<B do S1 L1: lw $t1,A($zero) lw $t2,B($zero) slt $t3,$t1,$t2 beq $t3,$zero,L2 S1 j L1 L2:

5 Control structures translation – repeat-until
repeat S1 until A<B L1: S1 lw $t1,A($zero) lw $t2,B($zero) slt $t3,$t1,$t2 beq $t3,$zero,L1

6 Control structures translation – for
sw $r0,S($r0) li $r1,1 sw $r1,I($r0) j L2 L1: lw $r1,I($r0) subi $r2,$r1,1 sll $r2,$r2,2 lw $r2,arr($r2) lw $r3,S($r0) add $r3,$r3,$r2 sw $r3,S($r0) addi $r2,$r1,1 sw $r2,I($r0) L2: li $r3,5 bne $r1,$r3,L1 type int=integer; var arr:array[1..5] of int; s,i:int; begin …fill the array… s:=0; for i:=1 to 5 do s:=s+arr[i] end

7 Function calls Calling convention in procedural languages
Parameter passing Returning a value from a function Order of parameters evaluation Stack cleanup Stack frame pointer saving Return address saving Local variables Register preservation

8 Function calls Call mechanism (C language)
Caller evaluates and stores actual parameters from right to left Caller passes control flow using a “CALL” instruction Callee saves return address on the stack Callee saves caller’s frame pointer on the stack Callee sets up its frame pointer Callee saves some registers Callee reserves space for local variables on the stack Callee executes its code Callee removes local variables from stack Callee restores preserved registers Callee restores caller’s frame pointer Callee jumps to the return address Caller removes parameters

9 Function calls – example
{ int a = f(1, 2); } int f(int i, int j) { int r = i + j; return r; } subi $sp,$sp,8 sw $ra,4($sp) sw $fp,0($sp) move $fp,$sp subi $sp,$sp,4 lw $t8,8($fp) lw $t9,12($fp) add $t1,$t8,$t9 sw $t1,-4($fp) lw $v0,-4($fp) lw $ra,4($fp) lw $fp,0($fp) addi $sp,$sp,12 jr $ra li $t8,2 subi $sp,$sp,4 sw $t8,0($sp) li $t8,1 jal f addi $sp,$sp,8 sw $v0,-4($fp) RA FP R30 A R29 J I RA FP R

10 Parameter passing Pass by value Pass by reference
The actual value of the parameter is copied as a local variable of the callee Input parameter Pass by reference An address of the parameter is passed as local variable of the callee Input/output parameters procedure P(i:integer; var v:integer); P(1, a); A I = 1 V

11 Activation record (frame)
Control link Activation record of the caller Access link Pointer to nonlocal data held in other activation records Saved machine status Return address to the code Registers Return value Actual parameters Control link Access link Saved machine status Local data Temporaries


Download ppt "Compilers Jakub Yaghob"

Similar presentations


Ads by Google