Code generation

(コード生成)

13th lecture, July 1, 2016

Language Theory and Compilers

http://www.sw.it.aoyama.ac.jp/2016/Compiler/lecture13.html

Martin J. Dürst

AGU

© 2005-15 Martin J. Dürst 青山学院大学

Today's Schedule

 

Summary of Previous Lecture

 

Remaining Schedule

 

Final Exam

 

Compilation Stages

  1. Lexical analysis
  2. Parsing (syntax analysis)
  3. Semantic analysis
  4. Optimization (or 5)
  5. Code generation (or 4)

 

Relationship between Code Generation and Optimization

Many variations are possible:

 

Methods of Code Generation

 

Difficulty of Code Generation

 

Main Machine Types

 

Example of Assembly Langugage

Input (C):

sum += price * 25;

Output (assembly):

LOAD   R1, price    ; R1 (register 1) ← price
CONST R2, 25 ; R2 (register 2) ← constant 25
MUL R1, R1, R2 ; R1 ← R1*R2
LOAD R2, sum ; R2 ← sum
ADD R2, R1, R2 ; R2 ← R1+R2
STORE sum, R2 ; sum ← R2

 

Assembly Language Details

 

Very Simple Assembly Language

(extreme example of a RISC architecture)

instruction operands explanation
LOAD R1, a load value from memory location (variable) a into register R1
STORE a, R1 store the value in R1 to the memory location (variable) a
CONST R1, 5 set register R1 to the constant value 5
ADD R1, R2, R3 Add R2 and R3 and put the result into R1. The same register can be used two or three times. SUB, MUL, and DIV are also available.
JUMP target Unconditional jump to instruction with label target
JUMP< R1, label Jump to target if R1 is smaller than 0. Otherwise, continue to next instruction. JUMP>=, JUMP!=, and so on are also available.

 

Code Generation from (Abstract) Syntax Trees

 

Code Generation for if Statements

 

Example of Code Generation for if Statements

Original statement: if (a>10) b = 15;

Generated code:

       LOAD    R1, a
CONST R2, 10
SUB R3, R1, R2 ; R3 = a-10
JUMP<= R3, endif ; jump over 'if' part if a-10<=0
CONST R4, 15
STORE b, R4
endif:

 

Restricted C Language

An intermediate language for humans)

 

Example of Restricted C Language

Original statement:
if (a>10) b = 15;

Use comparision with 0 in condition:

if (a-10 > 0) b = 15;

Add a label:

if (a-10 > 0)
b = 15;
endif:

Invert the condition and use goto:

if (a-10<=0)
goto endif;
b = 15;
endif:

 

Example of Code Generation for ifelse

Original program:

if (a > b)
c = a;
else
c = b;

Rewriting to restricted C:

if (a-b <= 0)
goto else;
c = a;
goto end;
else:
c = b;
end:

 

Result of Code Generation for ifelse

       LOAD   R1, a
LOAD R2, b
SUB R1, R1, R2 ; a-b > 0
JUMP<= R1, else
LOAD R1, a
STORE c, R1
JUMP end
else: LOAD R1, b
STORE c, R1
end:

 

Code Generation for Logical Or

Original program:

if (a>10 || b < 3)
c = 5;

Rewriting to restricted C:

if (a>10)
c = 5;
else if (b < 3)
c = 5;

 

Result of Code Generation for Logical Or

       LOAD   R1, a
CONST R2, 10
SUB R1, R1, R2 ; a-10 > 0
JUMP<= R1, else
CONST R1, 5
STORE c, R1
JUMP end
else: LOAD R1, b
CONST R2, 3
SUB R1, R1, R2 ; b-3 < 0
JUMP>= R1, end
CONST R1, 5
STORE c, R1
end:

 

Code Generation for while Loop

Original program:

while (a < 20)
a += 3;

Rewriting to restricted C:

next:  if (a-20 >= 0) goto break;
a += 3;
goto next;
break:

 

Result of Code Generation for while Loop

next:  LOAD   R1, a
CONST R2, 20
SUB R1, R1, R2
JUMP>= R1, break
LOAD R1, a
CONST R2, 3
ADD R1, R1, R2
STORE a, R1
JUMP next
break:

 

Code Generation for Function Calls

Contents of function call stack frame:

 

Homework

Deadline: July 7, 2016 (Thursday), 19:00

Where to submit: Box in front of room O-529 (building O, 5th floor)

Format: A4 single page (using both sides is okay; NO cover page, staple in top left corner if more than one page is necessary), easily readable handwriting (NO printouts), name (kanji and kana) and student number at the top right

Problem 1: Code generation for logical AND: Convert the C fragment
if (c<4 && f>=12) a=d;
to "Restricted C Language" and "Very Simple Assembly Language".

Problem 2 (bonus problem): Code generation for for statement: Convert the C fragment
for (i=0; i<20; i++) x*=y;
to "Restricted C Language" and "Very Simple Assembly Language".

Hint (for both problems): Try to remove the construct in question (&& or for) by rewriting the C program (Example: convert for to while)

 

Glossary

Turing-complete
チューリング完全
stack machine
スタック・マシーン
conditional statement
条件文
assembly language
アセンブリ言語
assembler
アセンブラ
(machine) instruction
(機械の) 命令
very simple assembly language
超単純アセンブリ言語
conditional jump instruction
条件付きジャンプ命令
restricted C language
制限された C 言語
stack frame
関数フレーム