Upload
ashley-york
View
287
Download
12
Embed Size (px)
Citation preview
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
컴파일러 입문
제 10 장
중간 코드 생성
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
Contents
• Introduction
• Syntax-Directed
Translation
• Code Generation
• U-Code Translator
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
• Formal Specification– lexical structure : regular expression– syntactic structure : context-free grammar– the remaining phases of compilation : no such notations
but, we use a syntax-directed translation scheme which is a method associating semantic rules(or actions) with production.
• SDTS ::= cfg + semantic actions– cfg 의 production rule 에 있는 grammar symbol 을 이용하여
직접 semantic action 을 기술하는 방법 .– AST generation – Attribute grammar
Introduction
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
• Intermediate code generation– the phase that generates an explicit intermediate
code of the source program.– after syntax and semantic analysis. A Model for Intermediate code generation
• Our implementations:– Source program : Mini Pascal– Intermediate Representation : Abstract
Syntax Tree (AST)– Intermediate code : U-Code– Execution : U-Code Interpreter
Source Program
Scanner
Parser IntermediateRepresentation
Intermediate
Code Generation
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
• Implementation Model
– scanner : shift action of parser– parser : main program (LR parser)– SDT : reduce action of parser (AST generation)– ICG : Intermediate code generation by traversing
AST.※ Semantic Analysis 와 Intermediate Code Generation 을
효율적으로 처리하기 위해서 AST 의 design 은 매우 중요 .
Source Program Scanner
Parser
SDT AST ICG Ucode UcodeInterpreter
data
result
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
▶ Syntax-Directed Translation Scheme(SDTS)::= a production rule + semantic action(no widely-accepted
formalism)
▶ A Model of SDTS
whenever a reduction takes place, the semantic rule corresponding to the applied syntactic rule is activated.
Syntax-Directed Translation
SourceProgram
ScannerParser(Main)
get_token
token
Parsingtable
call semantic
: Semantic Actions
Result
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
▶ Advantages of SDT Providing a method describing semantic rules and that
description is independent of any particular implementation.
Easy to modify - new productions and semantic actions can be added without disturbing the existing ones.
▶ Disadvantages of SDT 파싱중에 error 가 일어난 경우 이제까지 행한 semantic action 이 모두
무의미해 진다 . input 에 대해 one pass 이면서 syntax-directed 하게 처리하기 때문에 어떤
경우에는 정보가 부족하여 후에 필요한 정보가 나타났을 때 backpatchingbackpatching 등 복잡하게 처리해야 한다 .
Solution Syntax-directed 한 방법으로는 의미 분석과 코드 생성시에 필요한 정보만을 구성하고 다음 단계에서 그것을 이용하여 의미 분석과 코드 생성을 한다 .
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
Description of Semantic Actions
▶ SDTS(Syntax-Directed Translation Scheme)::= production rules + semantic actions
▶ Description of Semantic Actions(1) Conventional PL.(2) Meta Language - Formal Semantic Language(FSL)
SemanticDescription
Preprocessor table
SDTInput Output
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
▶ Semantic Description using the Attributes of Grammar Symbol
::= We associate information with a programming language construct by attaching attributes to the grammar symbols representing the construct. Values for attributes are computed by "semantic rules" associated with the grammar productions.
▶ An attribute of symbol::= A value associated with a grammar symbol. Each
grammar symbol has an associated set of attributes. An attribute can represent anything we choose: a string, a number, a type, a memory location, or whatever.
ex)
Production Semantic Rules
L -> E \n print(E.val)E -> E1 + T E.val := E1.val + T.val
E -> T E.val := T.valT -> T1 * F T.val := T1.val * F.val
T -> F T.val := F.valF -> ( E ) F.val := E.valF -> digit F.val := digit.lexval
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
▶ Synthesized attribute::= the value of the attribute of the nonterminal on the left
side of the production is defined as a function of the grammar symbols on the right side.
ex) A XYZ A := F(X,Y,Z)
▶ Inherited attribute::= the value of the attribute of a nonterminal on the
right side of the production is defined in terms of an attribute of the nonterminal on the left.
ex) A XYZ Y.val := 2 * X.val
※ Synthesized attribute is more natural than inherited attribute for
mapping most programming language constructs into intermediate code.
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
Implementation of SDT
▶ Designing steps Input design - language construct 에 대한 grammar
를 cfg 를 이용하여 design. Scanner, Parser 의 작성 . Semantic Specification - conventional PL.
---> SDT Translator 의 완성 - interconnection.
▶ Examples : 1. Desk Calculator 2. Conversion infix into postfix
3. Construction of AST
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
1. Desk Calculator(1) Input design
0. S -> E $1. E -> E + E2. E -> E * E3. E -> ( E )4. E -> num
(2) Parsing table
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
(3) Semantic Specification
(4) Implementation of Desk Calculator
– Parsing stack : Symbol stack + State stack + Value stack
– Value stack : holding the values of the corresponding attribute.
Production Semantic Rules
L -> E $ print E.valE -> E1 + E2 E.val := E1.val + E2.valE -> E1 * E2 E.val := E1.val * E2.val
E -> ( E1) E.val := E1.valE -> num E.val := num.lexval
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
– the code fragments do not show how variable top is managed.– lexval : token value – the code fragments are executed before a reduction takes pl
ace.
Production Code Fragment
S -> E $ print (val[top])E -> E + E val[top-2] := val[top-2] + val[top]E -> E * EE -> (E) val[top-2] := val[top-1]
E -> num val[top] := num.lexval
val[top-2] := val[top-2] * val[top]
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
ex) 23 * 5 + 4 $
state input symbol value parse (0 23 * 5 + 4$, , , )s3 ----> (0 3 , * 5 + 4$, num , , )
r4 ----> (0 1 , * 5 + 4$, E , 23 , 4 )s5 ----> (0 1 5 , 5 + 4$, E * , 23_ , 4 )s3 ----> (0 1 5 3 , + 4$, E * num , 23__ , 4 )r4 ----> (0 1 5 8 , + 4$, E * E , 23_5 , 4 4 )r2 ----> (0 1 , + 4$, E , 115 , 4 4 2 )s4 ----> (0 1 4 , 4$, E + , 115_ , 4 4 2 )s3 ----> (0 1 4 3 , $, E + num , 115__ , 4 4 2 )r4 ----> (0 1 4 7 , $, E + E , 115_4 , 4 4 2 4 )r1 ----> (0 1 , $, E , 119 , 4 4 2 4 1 ) ----> accept
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
2. Conversion infix into postfix
Production Code Fragment
E -> E + E print ‘+’
E -> E * E print ‘*’
E -> E / E print ‘/’
E -> (E)
E -> a print ‘a’
a + (a + a) * a a a a + a * +
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
3. Construction of AST
▶AST is a condensed form of parse tree useful for representing language constructs.
ex) a := b + 1;
ex) if a > b then x := a else x := b;
:=
a +
b 1
if
>
a b
:= :=
x a x b
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
▶ Functions to create the nodes of AST for expressions with binary operators. Each function returns a pointer to a newly created node.1. mktree(op,left,right) creates an operator node with label op and two
fields containing pointers to left and right.2. mknode(a) creates a terminal node for a and returns the node pointer.
▶ Semantic Specification
Production Semantic Rules
E -> E1 + T E.nptr := mktree(‘+’, E1.nptr, T.nptr)
E -> E1 - T E.nptr := mktree(‘-’, E1.nptr, T.nptr)
E -> T E.nptr := T.nptr
T -> (E) T.nptr := E.nptr
T -> a T.nptr := mknode(a)
– The synthesized attribute nptr for E and T keeps track of the pointers returned by the function calls.
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
▶ AST for a - 4 + c
+
- id
id num 4
to entry for a
to entry for c
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
Programming Assignment #4
• Implement a syntax-directed translator producing an AST
for MiniPascal program.
– MiniPascal Program : Perfect.pas(Text pp.427-428)
– The Output form of AST using printtree() : Text pp.428-430
scannerMini-Pascal Programs
parser
SDT AST
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
• AST design– Grammar form : production rule [ => node_name ] ;
Note : => node name 의 생략 시에는 부 트리를 구성하지 않음 .
A -> => node_name ;
• MiniPascal GrammarMINI_PASCAL -> 'program' IDENT ';' BLOCK '. ' =>
PROGRAM; BLOCK -> DCL COMPOUND_ST => BLOCK;DCL -> CONST_DCL VAR_DCL PROC_DCL => DCL;
Text pp. 419-421
node_name
1 2 n...
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
• Data Structures– A node form of AST
– NODE structure struct tokentype {
int tokennumber; /* 토큰 번호 */char * tokenvalue; /* 토큰의 값 */
}; typedef struct nodetype {
struct tokentype token; /* 토큰 종류 */enum {terminal, nonterm} noderep;/* 노드의 종류 */struct nodetype *son; /* 왼쪽 링크 */struct nodetype *brother; /* 오른쪽
링크 */} NODE;
– Nonterminal node names :enum nodename { NOTREE, PROGRAM, BLOCK, DCL, ..., ARRAY_VAL};
sontoken
tokennumber tokenvaluenoderep brother
son node
next brother node
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
• Shift action of parsing :– if the token is meaningful, then call buildnode. NODE *buildnode(struct tokentype token) { NODE *ptr; ptr = (NODE *) malloc (sizeof(NODE)); if (!ptr) { printf(“ build node malloc error\n”); exit(1); } ptr -> token = token; ptr -> noderep = terminal; ptr -> son = NULL; ptr -> brother = NULL; return ptr; }
• Reduce action of parsing :– if the production rule is meaningful 1. build subtree
linking brothers making a subtree
else 2. only linking brothers
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
• Printing the information about AST– int printnode(NODE *t, int indent) : print a node
information– int printtree(NODE *t, int indent) : print an AST
void printnode(NODE *t, int indent){ extern FILE *ast; int i;
for (i=1; i<=indent; i++) fprintf(ast," ");if (t->noderep == terminal) {
fprintf(ast," Terminal: ");fprintf(ast,"%s",t->token.value);
}if (t->noderep == nonterm) {
i = (int) (t->token.number);fprintf(ast," Nonterminal: %s", ASTname[i]);
}}
void printtree(NODE *t, int indent){ NODE * p = t;
while (p != NULL) {printnode(p, indent);if (p->noderep == nonterm) printtree(p->son,indent+5);p = p->brother;
}}
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
▶ A Model for ICG
Source language : Mini Pascal Intermediate Representation : Abstract Syntax
Tree(AST)Intermediate code : UcodeExecution : Ucode Interpreter
Mini-Pascal Programs scanner
parser
SDT AST ICG Ucode UcodeInterpreter
data
executionresult
Code Generation
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
▶ Three Parts of MiniPascal
• Heading part : program name• Declaration part : description of data structures• Statement part : specifying the actions
• Production rules :(MiniPascal)
MINI_PASCAL -> 'program' IDENT ';' BLOCK '.’ BLOCK -> DCL COMPOUND_ST
DCL -> CONST_DCL VAR_DCL PROC_DCL;
STATEMENT -> ASSIGNMENT_ST; -> CALL_ST;-> COMPOUND_ST; -> IF_ST; -> WHILE_ST; -> ;
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
▶ Code Generating Routines
The basic structure of ICG void codegen(NODE *pt)
{switch (pt->token.tokennumber) {
case PROGRAM : { … } case BLOCK : { … }
…case ARRAY_VAL : ... default: printf(" Code
generation Error");}
} /* main program */…root = parser(); /* AST generated by SDT */codegen(root);codegen(root);...
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
Heading part
① Grammar
MINI_PASCAL -> 'program' IDENT ';' BLOCK '.’ => PROGRAM;
BLOCK -> DCL COMPOUND_ST => BLOCK;
② AST
PROGRAM
IDENT BLOCK
DCL COMPOUND_STMT
root
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
③ Code Segmentcase PROGRAM :
{/* 코드 생성에 필요한 초기화 작업 */codegen(pt->son->brother); /* BLOCK *//* 코드 생성한 후 마지막 처리 작업 */
}
• An Ucode program form :
sym(s) for global variables<name> proc
sym(s)… codes for procedureendopbgn... codes for main programendop
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
Declaration Part
① Grammar
DCL -> CONST_DCL VAR_DCL PROC_DCL => DCL;
VAR_DCL -> 'var' VAR_DEFS ';’ => VAR_DCL; -> ;
VAR_DEFS -> VAR_DEF; -> VAR_DEFS ';' VAR_DEF;VAR_DEF -> VAR_LIST ':' TYPE => VAR_DEF;VAR_LIST -> IDENT_LIST => VAR_LIST;TYPE -> 'integer’ => INT_TYPE; -> 'array' '[' CONSTANT '..' CONSTANT ']' 'of’ 'integer’ =>
ARRAY_TYPE;
※ Mini-Pascal 에서 선언은 상수선언 , 변수선언 , 프로시저선언으로 구성 .※ 자료형은 정수형과 정수형 배열만 있음 .
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
② AST
※ 변수선언은 여러 개의 변수정의로 이루어져 있으며 , 하나의 변수정의는 명칭들의 리스트와 그 명칭들이 갖는 형으로 되어 있다 .
DCL
CONST_DCL VAR_DCL
VAR_DEF
PROC_DCL
VAR_DEF VAR_DEF...
VAR_LIST TYPE_DENOTER
IDENT IDENT...
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
③ Code Segment
case DCL: { offset = 0; } break;case VAR_DCL : {
NODE * q = pt->son;while (q != NULL) { codegen(q);
q = q->brother; } }
break;case VAR_DEF : {
NODE *id=pt->son->son, *tp=pt->son->brother;while (id != NULL){
enter(id->token.tokenvalue, tp->token.tokennumber, offset);
offset += typesize(tp->token.tokennumber);id = id->brother;
}}
※ 함수 enter(name, type, offset) 는 symbol table 에 명칭을 삽입한다 .
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
Assignment Statement
① GrammarASSIGNMENT_ST -> LHS ':=' EXP =>
ASSIGN_STMT;LHS -> VARIABLE;VARIABLE -> IDENT;
-> IDENT '[' EXP ']' => INDEX;
② ASTASSIGN_STMT
IDENT <<expression>>
ASSIGN_STMT
INDEX <<expression>>
IDENT <<expression>>
Assignment Statement
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
③ Code Segment
case ASSIGN_STMT: {NODE *lhs = pt->son, *rhs = pt->son->brother;if (lhs->noderep == nonterm) codegen(lhs); /* array variable */if (rhs->noderep == nonterm) codegen(rhs); /* 식에 대한 코드 생성*/
else rv_emit(rhs);if ((p = lookup(lhs->token.tokenvalue)) == NULL) error();
emit1("str", p);}
– lookup() : 심 볼 테 이 블 에 서 명 칭 을 찾 아 해 당 엔 트 리 를 가 리 키 는 포인터를 반환하는 함수 . 실패할 경우 NULL 을 복귀한다 .
– rv_emit() : 매개변수가 변수인가 상수인가에 따라 lod 와 ldc 로 구분하여 출력하는 함수이다 .
– emit1() : mnemonic 이름과 하나의 operand 를 출력한다 .
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
Expression
① GrammarEXP -> EXP '+' TERM
=> ADD;-> EXP '-' TERM
=> SUB;-> TERM;
TERM -> TERM '*' FACTOR => MUL;
-> TERM 'div' FACTOR => DIV;
-> TERM 'mod' FACTOR=> MOD;
-> FACTOR; FACTOR-> '-' FACTOR => NEG;
-> VARIABLE; -> NUM;-> '(' EXP ')';
② AST
ASSIGN_STMT
IDENT EXP
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
③ Code Segment
case ADD: case SUB: case MUL: case DIV: case MOD:{
NODE *lhs = pt->son, *rhs = pt->son->brother; if (lhs->noderep == nonterm) codegen(lhs); /* 좌 피 연 산 자 의 코드생성 */
else rv_emit(lhs);if (rhs->noderep == nonterm) codegen(rhs); /* 우 피 연 산 자 의 코드생성 */
else rv_emit(rhs); switch (pt->token.tokennumber) {
case ADD: emit0("add"); break;case SUB: emit0("sub"); break;case MUL: emit0("mult"); break;case DIV: emit0("divop"); break;case MOD: emit0("modop"); break;
}}
– emit0() : mnemonic 이름만 출력한다 .
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
Example)init := 10;value := init + 20 * 2;
※ 여기서 , 일련 번호는 코드 생성 순서 .※ Ucode 의 계산순서는 postorder 와 일치하므로 postorder 로 운행한다 .
ASSIGN_STMT
init 10
ASSIGN_STMT
value ADD
init MUL
20 2
②
①
③
④ ⑤
⑥
⑦
⑧
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
– 생성된 Ucode :
ldc 10 ①str 1 1 ; init ②lod 1 1 ; init ③ldc 20 ④ldc 2 ⑤mult ⑥add ⑦str 1 2 ; value ⑧
※ Ucode 에서 , 변수는 (base, offset) 형태로 addressing 한다 .
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
▶ Array variable
• In one-dimensional array,
location of i's element = Base + (i - Low) * Wwhere, Low : lower bound of array Base : start address of array
• Address of A[i] = i*W+(Base - Low * W),– 상수식 c = Base - Low * W
∴ A[i] = c + i*W
• Assume that the size of integer is 1. W = 1
Location of list[10] = ( 배열의 시작주소 ) + (10 - Low) * 1
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
【예】다음은 미니 파스칼에서 배열의 선언과 배열의 참조를 나타낸다 .
program bubble;const
subscript = 20;var
list : array[5..subscript] of integer;…
begin…list[10] := 3; …end.
위 프로그램에서 list[10] := 3 에 해당하는 Ucode 는 다음과 같다 .
ldc 10ldc 5subldr 1 1 /* load the address of list */addldc 3sta
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
• Control Statement1. conditional statement --- if, case, switch2. iteration statement --- for, while, do-while, repeat-
until3. branch statement --- goto
▶ 논리식 (Logical expression)① Grammar
CONDITION -> EXP '=' EXP => EQ;-> EXP '<>' EXP => NE;-> EXP '>' EXP => GT;-> EXP '>=' EXP => GE;-> EXP '<' EXP => LT;-> EXP '<=' EXP => LE;
Control Statement
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
② 관계식 a >= b + 1 에 대한 AST 와 U- 코드는 다음과 같다 .
– AST 형태 :
– Ucode :
lod a /* 1 1 */lod b /* 1 2 */loc 1addge
GE
a ADD
b 1
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
▶ 제어문의 코드 생성
- if-then
CONDITION 코드STATEMENT 코드
tag :
if false thengoto tag
- if-then
CONDITION 코드STATEMENT 코드
if false thengoto tag1
goto tag2
STATEMENT 코드tag1 :
tag2 :
- while-do
CONDITION 코드STATEMENT 코드 if false then
goto tag1goto tag1
tag1 :
tag2 :
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
① Grammar
IF_ST -> 'if' CONDITION 'then' STATEMENT => IF_STMT;
-> 'if' CONDITION 'then' STATEMENT'else' STATEMENT
=> IF_ELSE_ST;WHILE_ST -> 'while' CONDITION 'do' STATEMENT =>
WHILE_STMT;
② AST & Code SegmentIF_STMT
CONDITION STATEMENT
case IF_STMT : { char tag[5]; taggen(tag); /* label generation */ codegen(pt->son); /* CONDITION part */ emit2(“fjp”, tag); codegen(pt->son->brother); /* statement */ emit_label(tag);}
- if-then 구조
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
IF_ELSE_STMT
CONDITION ST1
case IF_ELSE_STMT : { char tag1[5], tag2[5]; taggen(tag1); taggen(tag2); codegen(pt->son); /* CONDITION */ emit2(“fjp”, tag1); codegen(pt->son->brother); /* true part */ emit2(“ujp”, tag2); emit_label(tag1); /* false part*/ codegen(pt->son->brother->brother); emit_label(tag2);}
- if-then-else 구조
ST2
case WHILE_STMT : { char tag1[5], tag2[5]; taggen(tag1); taggen(tag2); emit_label(tag1); codegen(pt->son); /* CONDITION 코드 생성 */ emit2(“fjp”, tag2); codegen(pt->son->brother); /* loop body */ emit2(“ujp”, tag1); emit_label(tag2);}
WHILE_STMT
CONDITION
- while-do 구조
STATEMENT
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
– 예제 : if a > great then great := a;
• AST 형태 :
• 생성된 Ucode :
IF_STMT
GT ASSIGN_STMT
a great great a
lod 1 1lod 1 2gt
lod 1 1str 1 2
fjp $$1
nop$$1
; a; great; a > great
; great := a
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
– 예제 : while i <= 100 do begin sum := sum + i; i := i + 1
end;• AST 형태 :
WHILE_STMT
LE COMPOUND_STMT
i 100 ASSIGN_STMT ASSIGN_STMT
SUM
sum i
ADD i
i 1
ADD
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
• 생성된 Ucode :
$$1
lod ildc 100le
lod sumlod iaddstr sum
fjp $$2
nop
i < = 100
lod iloc 1addstr i
sum := sum + i;
i := i + 1
ujp $$1nop$$2
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
procedure call statement
① Grammar
CALL_ST -> IDENT OPT_ACT_PARMS => CALL_STMT;OPT_ACT_PARMS -> '(' ACTUAL_PARAMS ')’ =>
ACTUAL_PARAM; -> ;
ACTUAL_PARAMS -> EXP ;-> ACTUAL_PARAMS ',' EXP ;
② AST CALL_STMT
IDENT ACTUAL_PARAM
EXP EXP ...
Procedure
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
③ Code Segmentcase CALL_STMT : {
emit0("ldp");codegen(pt->son->brother); /* 실 매개 변수를 위한 코드 생성 */emit1("cal", pt->son->token.tokenvalue);
};
– 예제 : procedure swap (var x, y : integer);var …begin …end;begin /* main */ … swap( a, b ); …end.
swap proc …. … ret endop bgn … … ldp ldr a ldr b cal swap … endop
함수 호출을위한 Ucodeswap(a,b);
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
procedure definition
① GrammarPROC_DEF -> PROC_HEAD BLOCK ';' =>
PROC_DEF; PROC_HEAD -> 'procedure' IDENT
OPT_FORM_PARM ;OPT_FORM_PARM -> '(' FORMAL_PARAMS ')' ';’
=> FORM_PARM;
-> ';' ;
② AST
PROC_DEF
IDENT FORM_PARAM
VAR_PARAM VALUE_PARAM
BLOCK
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
③ Code Segment
case PROC_DEF : {block += 1;offset = 1;enter(pt->son->token.tokenvalue, PROC);emit3("proc", block, offset);codegen(pt->son->brother); /* 형식 매개 변수에 대한 코드 생성 */codegen(pt->son->brother->brother); /* Block 에 대 한 코드 생성 */emit0("ret");emit0("endop");
};
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
• Design and Implementation of Ucode Translator– scanner, parser, SDT, ICG
Ucode Translator
Mini-Pascal Programs scanner
parser
SDT AST ICG Ucode UcodeInterpreter
data
executionresult
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
• Execution sequence of perfect.pas
① MiniPascal program : Text pp.427-428
② The Output form of AST using printtree() : Text pp.428-430
③ Ucode that generated by code generator : Text pp.454-456
④ The execution of Ucode using Ucode Interpreter
ucodei perfect.uco
Result filename is perfect.lst
-- Assembling... -- Executing... -- Result Data
6 28 496
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
Programming Assignment #5
• MiniPascal 언어에 대한 Ucode Translator 를 작성하시오 . 생성된 Ucode 는 Interpreter 를 사용하여 실행하시오 .
AST ICG UcodeUcode
Interpreter
data
executionresult
Compiler Lecture Note, Intermediate Code Generation
PL Lab, DongGuk University
• 예제 프로그램 :program perfect;const max = 500;var i, j, k, r, sum: integer;begin
i := 2;while i <= max do
begin sum := 0; k := i div 2;
j := 1; while j <= k do begin
r := i mod j;if r = 0 then sum := sum + j;j := j + 1
end; if i = sum then write(i);
i := i + 1end
end.