Code Generation

(コード生成)

13th lecture, July 13, 2018

Language Theory and Compilers

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

Martin J. Dürst

AGU

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

Today's Schedule

 

Summary of Previous Lecture

 

Example Solution for bison Homework

(paper only)

 

Remaining Schedule

 

Compilation Stages

  1. Lexical analysis
  2. Parsing (syntax analysis)
  3. Semantic analysis
  4. Code generation (or 5)
  5. Optimization (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; specific for this lecture)

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 at label target
JUMP< R1, target Jump to target if R1 is <0. Otherwise, continue to next instruction. JUMP>=, JUMP!=, ... 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, endif1 ; jump over 'if' part if a-10<=0
CONST R4, 15
STORE b, R4
endif1:

 

Restricted C Language

(intermediate language for humans; specific for this lecture)

 

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;
endif1:

Invert the condition and use goto:

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

 

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 else1;
c = a;
goto end1;
else1:
c = b;
end1:

 

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
else1: LOAD R1, b
STORE c, R1
end1:

 

Code Generation for Logical Or

Original program:

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

Rewriting to separate conditions:

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

Rewriting to restricted C:

  if (a-10 <= 0)
    goto else1;
c = 5; goto end1; else1:
if (b-3 >= 0) goto end1;
c = 5; end1:

 

Result of Code Generation for Logical Or

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

 

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:

For an example of call stack structure (for Ruby), see RubyKaigi2018 talk

Also see example images

 

Homework

Deadline: July 19, 2017 (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)

Additional homework: Bring your notebook computer with you next time

 

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
関数フレーム