180 likes | 372 Views
Compiler Term Project. MiniJava Compiler. 이한욱 2005220146 조재호 2000160239 권신일 99390274. Overview. Mips Assembly Code. MiniJava Source Code. Parser. Jlex Specification file (parser/ MiniJavaLexer ). CUP Specification file (parser/MiniJavaParser.cup). JLex. CUP. Generated.
E N D
Compiler Term Project MiniJavaCompiler 이한욱 2005220146 조재호 2000160239 권신일 99390274
Overview Mips Assembly Code MiniJava Source Code
Parser Jlex Specification file (parser/MiniJavaLexer) CUP Specification file (parser/MiniJavaParser.cup) JLex CUP Generated Scanner (Lexer) (parser/MiniJavaLexer.java) Symbols (parser/Sym.java) Parser (parser/MiniJavaParser.java) Parser.java (Module) MiniJavaParser Yylex TokenSymbol Yylex.next_token()
Local Error Recovery Source Code Grammar VarDecl := Type Identifier SEMICOLON | Type error SEMICOLON … int a; ? b; int c; … LookAhead Token List ? (error symbol) b (Identifier) ; (SEMICOLON) (skip) int (Type) a (Identifier) … ; (SEMICOLON) ; (SEMICOLON) … … (pop) ? (error symbol) a (Identifier) (pop) ? (error symbol) ? (error symbol) b (Identifier) int (Type) int (Type) int (Type) ; (SEMICOLON) Stack int (Type) int (Type) c (Identifier) c (Identifier) ; (SEMICOLON) … ; (SEMICOLON) … ? (error symbol) int (Type) … int c; … int (Type) VarDecl (error rule)
AST Phase Symbol Table Phase Symbol Table Builder Symbol Table Tree Abstract Syntax Tree • Symbol Table은 Class별로 생성 • AST는 Scope에 따라 특정 Symbol Table을 참조 • Symbol Table은 Tree형태로 구성되어 있기 때문에 상위 Scope의 Symbol Table을 참조할 수 있다.
Type Rules (1) • 변수 또는 메소드에 대한 접근 (사용) • [Statement] var = 1; • [Statement] foo(); • 변수 또는 메소드에 대한 접근 시 변수인지 메소드인지 판별한 후 해당 Scope의 심볼 테이블로부터 탐색하여 해당 변수 또는 메소드가 존재하는지 검사한다. • 객체를 통한 변수/메소드 접근 • [Statement] obj.var = 1; • [Statement] obj.foo(); • 심볼테이블에서obj를 찾은 후 Type과 Type Name을 얻는다. Type이 CLASS이면 Type Name에 해당하는 클래스의 심볼에서 탐색하여 변수 또는 메소드가 존재하는지 검사한다. • 변수 또는 메소드 선언 • [Statement] Type Identifier; • [Statement] Type Identifier(…) • Type이 올바른 타입인지 검사한다. 기본형이 아닌 경우 클래스 타입이므로 해당 클래스가 존재하는지 검사한다. • (변수 또는 메소드의 중복 선언 검사는 심볼 테이블 생성 시 이루어진다.)
Type Rules (2) • 연산자 Semantics • [Statement] Exp && Exp (Operand가 모두boolean타입이어야 한다.) • [Statement] Exp <|+|-|* Exp (Operand가 모두int타입이어야 한다.) • [Statement] !Exp (Operand가 모두boolean타입이어야 한다.) • 연산자가 받아들일 수 있는 피연산자(Exp)의 타입은 연산자에 의해 결정된다. 따라서 연산자에 따라 피연산자의 타입을 검사해야 한다. 만약 피연산자가 Identifier인 경우 변수/메소드 여부 판단 후 심볼테이블을 탐색하여 Type을 얻을 수 있다. • Type mismatch • 변수에 값을 할당할 때 변수의 타입과 할당된 값의 타입이 일치해야 한다. 즉, A 타입으로 선언된 변수에 B 타입의 값을 할당할 수 없다. • Method return type • 메소드는 반드시 메소드의 Signature에 명시된 리턴 타입의 값을 리턴해야 한다. 즉, A 타입을 리턴하도록 명시된 메소드는 B 타입의 값을 리턴할 수 없다.
Translate Program Mainclass classDecl MethodDeclaration MethodDeclaration MethodDeclaration
Implementation public Exp visit(Minus n, Types.Typearg) { return new Ex(BINOP (Tree.BINOP.MINUS, (n.e1.accept(this, arg)).unEx(), (n.e2.accept(this, arg)).unEx())); }
Canonical Form • Canon.java • IR Tree를 순회하면서 SEQ와 ESEQ를 제거 • CALL 수행 시 return value overwriting 문제 해결
Canonical Form Transformed Original staticTree.ESEQdo_exp(Tree.ESEQ e) { Tree.Stmstms = do_stm(e.stm); Tree.ESEQ b = do_exp(e.exp); returnnewTree.ESEQ(seq(stms,b.stm), b.exp); }
Basic Block • 연속적으로 수행되는statement • LABEL로 시작해서 JUMP 또는 CJUMP로 끝남 • 모든 LABEL, JUMP, CJUMP은 Basic Block 에서 속해야만한다.
Trace void trace(LinkedList<Tree.Stm> l) { for(;;) { 현재 block의 label을 hash table에서 삭제 if(현재 block의 마지막 label이 JUMP) { if(JUMP가 가리키는 곳이 block일 경우) { JUMP / label 삭제 Statement list l에 statement 추가 Hash table에서 label 삭제 } else { 재귀 호출을 통해 hash table의 다음 label로 } } else if (현재 block의 마지막 label이 CJUMP) { … } else { error } }
Liveness Analysis &Register Allocation (변경전 Temp 레지스터 t101, t102 사용) add t101 t101 t102 # t101 = t101 + t102 (변경후 새로운 Temp 레지스터 t201, t202 발생) lwt201mem(for t101) # t101에 해당하는 메모리 주소에서 값을 읽어 t201에 저장 lwt202mem(for t102) # t102에 해당하는 메모리 주소에서 값을 읽어 t202에 저장 add t201 t201 t202 # t201 = t201 + t202 swmem(for 101) t201 # t101에 해당하는 메모리 주소에 t201 값을 저장 • lw$t0mem(for t101) # t101에 해당하는 메모리 주소에서 값을 읽어 $t0에 저장 • lw$t1mem(for t102) # t102에 해당하는 메모리 주소에서 값을 읽어 $t1에 저장 • add $t0 $t0 $t1 # $t0 = $t0 + $t1 • swmem(for 101) $t0 # t101에 해당하는 메모리 주소에 $t0 값을 저장