-
어휘 해석(Lexical Analysis)
스캔
어휘 해석기(LEXICAL ANALYZER) 또는 스캐너
토큰열을 생성
스캐너 제너레이터
구문 해석(Syntax Analysis)
parse추상 구문 트리파서 제너레이터
의미 해석(Semantic Analysis)
중간 코드
(operation ,left operand,right operand,result,<Cmp>,Lbl))
<Cmp>
1(==)
2(<)
3(>)
4(<=)
5(>=)
6(!=)
(MOV, A,,B)
(ADD, A,B,C)
(NEG, A,,B)
(JMP, ,,,,Lbl)
(TST, A,B,,<Cmp>,Lbl)
Label
run-time operations
Code Generation
(MOV, A,,B)
==>
LOD R1,A
STO R1,B
(ADD, A,B,T1)
==>
LOD R1,A
ADD R1,B
STO R1,T1
(JMP, ,,,,Lbl)
==>
CMP 0,0,0
JMP Lbl
(TST, A,B,,<Cmp>,Lbl)
==>
LOD R1,A
CMP R1,B,<Cmp>
JMP Lbl
Label
(LBL, ,,,,Lbl)
Unconditional Jump
(JMP, ,,,,Lbl)
Conditional Branch
(TST, E1,E2,,<Cmp>,Lbl)
A '+' B '*' C '+' D
==>
(MULT B,C,Temp1)
(MULT A,Temp1,Temp2)
(ADD Temp2,D,Temp3)
a '=' b '+' '(' c '=' 3 ')'
==>
(MOV, 3,,c)
(ADD, b,c,T1)
(MOV, T1,,a)
x '>' y
==>
(MOV, 1,,T1)
(TST, x,y,,3,L1)
(MOV, 0,,T1)
(LBL, L1)
if '(' Expr ')' Stmt1 (else Stmt2)
==>
Expr(TST, t0,0,,1,Lbl1)
Stmt1
(JMP, ,,,,Lbl2)
(Lbl, ,,,,Lbl1)
Stmt2
(Lbl, ,,,,Lbl2)while '(' Expr')' Stmt
==>
(Lbl, ,,,,Lbl1)
Expr
Stmt
(JMP, ,,,,Lbl1)
(Lbl, ,,,,Lbl2)
for '(' Expr1 ';' Expr2 ';' Expr3) Stmt
/*
Expr1;
while (Expr2)
{ Stmt
Expr3;
}
*/
==>
Expr1
(Lbl, ,,,,Lbl1)
Expr2
(TST, t1,0,,1,Lbl2)
(JMP, ,,,,Lbl3)
(Lbl, ,,,,Lbl4)
Expr3
(JMP, ,,,,Lbl1)
(Lbl, ,,,,Lbl4)
Stmt
(JMP, ,,,,Lbl4)
(Lbl, ,,,,Lbl3)
gcccpp전처리기(preprocessor)cc1어셈블러 코드가 생성된다collect2(as)object 파일이 생성된다
참조 사이트: