1
play

1 Specifying Grammar with JavaCUP Abstract Syntax Tree for Memory - PowerPoint PPT Presentation

CS 453: Compiler Construction Review Structure of the MiniJava Compiler Analysis Synthesis Phases of the compiler lexicographical analysis, or scanning (regular expressions) character stream syntactic analysis, or parsing (context


  1. CS 453: Compiler Construction Review Structure of the MiniJava Compiler Analysis Synthesis Phases of the compiler – lexicographical analysis, or scanning (regular expressions) character stream – syntactic analysis, or parsing (context free grammars) PA3 lexical analysis IR code generation PA6 – building the abstract syntax tree (syntax-directed translation) – building the symbol table (visitor design pattern) tokens “words” Assem (MIPS) – semantic analysis, or type checking (visitor design pattern) PA4 syntactic analysis – code generation (visitor design pattern) 553 optimization – 3-address code AST “sentences” Assem (MIPS) – Assem(MIPS) PA5 semantic analysis How would adding floats to the MiniJava compiler affect each phase? code gen AST and symbol table MIPS CS453 Lecture Final Review 1 CS453 Lecture Final Review 2 Specifying Tokens with JFlex Interaction Between Scanning and Parsing JFlex example input file: LETTER=[A-Za-z] DIGIT=[0-9] package mjparser; import java_cup.runtime.Symbol; UNDERSCORE="_" LETT_DIG_UND={LETTER}|{DIGIT}|{UNDERSCORE} lexer.next_token() ID={LETTER}({LETT_DIG_UND})* parse tree %% %line or AST character stream %char %% Lexical %cup "&&" { return new Symbol(sym.AND, new Parser %public TokenValue(yytext(), yyline, yychar)); } analyzer %eofval{ token return new Symbol(sym.EOF, new "boolean" {return new TokenValue("EOF", yyline, yychar)); Symbol(sym.BOOLEAN,... %eofval} {ID} { return new Symbol(sym.ID, new ... CS453 Lecture Final Review 3 CS453 Lecture Final Review 4 1

  2. Specifying Grammar with JavaCUP Abstract Syntax Tree for Memory Layout Example JavaCUP example input file: start with program; package mjparser; import java_cup.runtime.*; program ::= import ast.node.*; main_class:m class_decl_list:l ... {: RESULT = new Program(m,l); :} terminal AND, ASSIGN, INT; ; terminal mjparser.TokenValue NUMBER; ... exp ::= non terminal Program program; | NUMBER:n non terminal List<IClassDecl> {: Token token = new Token(n.text, class_decl_list; n.line, n.pos); RESULT = new IntegerExp( token ); non terminal MainClass :} main_class; ... CS453 Lecture Final Review 5 CS453 Lecture Final Review 6 Semantic Analysis Compiler Data Structures Determine whether source is meaningful Symbol Tables – Compile-time data structure – Check for semantic errors – Holds names, type information, and scope information for variables – Check for type errors Scopes – Gather type information for subsequent stages – A name space – Relate variable uses to their declarations e.g., In Pascal, each procedure creates a new scope e.g., In C, each set of curly braces defines a new scope – Can create a separate symbol table for each scope Example errors (from C) – What are the scopes in MiniJava? function1 = 3.14159; Using Symbol Tables x = 570 + “hello, world!” – For each variable declaration: scalar[i] – Check for symbol table entry – Add new entry; add type info – For each variable use: – Check symbol table entry CS453 Lecture Final Review 7 CS453 Lecture Final Review 8 2

  3. Example Symbol Table Compiling Procedures Properties of procedures higher addresses – Procedures/methods/functions define scopes AR: zoo – Procedure lifetimes are nested – Can store information related to dynamic invocation of a procedure on a call stack ( activation record or AR or AR: goo stack frame): – Space for saving registers – Space for passing parameters and returning values class And { AR: foo public static void main(String[] a){ – Space for local variables System.out.println(new Foo().testing(42)); }} – Return address of calling instruction class Foo { AR: foo public int testing(int p) { Stack management int x; – Push an AR on procedure entry (caller or callee) if (p < 10 && 2 < p) { lower addresses stack x = 7; – Pop an AR on procedure exit (caller or callee) } else { – Why do we need a stack? x = 22; } return x;}} CS453 Lecture Final Review 9 CS453 Lecture Final Review 10 Stack Frame for MiniJava Compiler Assem intermediate representation int foo(int x,int y,int *z) { .text Assem.Instr int a; .globl main main: – “assembly language instruction without register assignments” a = x * y - *z; sw $ra, 0($sp) #PUSH return a; subu $sp, $sp, 4 } sw $fp, 0($sp) #PUSH OPER(String assem, List<Temp> dst, List<Temp> src, List<Label> jumps) void main() { subu $sp, $sp, 4 – contains a string with holes for registers indicated by `d# and `s# and holes for labels addu $fp, $sp, 8 int x; indicated by `j# subu $sp, $fp, 12 x = 2; li $t0, 2 – dst and src are lists of Temps whose register assignment should fill holes cout << foo(4,5,&x); sw $t0, -8($fp) cout << "\n"; – first entry in src is associated with `s0, second with `s1, etc. li $t0, 4 } sw $t0, 0($sp) #PUSH – first entry in dst is associated with `d0, etc. subu $sp, $sp, 4 – jumps is a list of labels for filling in label holes .text li $t0, 5 _foo: sw $t0, 0($sp) #PUSH sw $ra, 0($sp) #PUSH subu $sp, $sp, 4 subu $sp, $sp, 4 subu $t0, $fp, 8 sw $fp, 0($sp) #PUSH sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 subu $sp, $sp, 4 addu $fp, $sp, 20 jal _foo subu $sp, $fp, 24 move $a0, $v0 ... ... lw $t0, -20($fp) lw $ra, 0($fp) move $v0, $t0 move $t0, $fp lw $ra, -12($fp) lw $fp, -4($fp) move $t0, $fp move $sp, $t0 lw $fp, -16($fp) jr $ra move $sp, $t0 jr $ra CS453 Lecture Final Review 11 CS453 Lecture Final Review 12 3

  4. Assem intermediate representation cont ... Instruction selection for x86-64 Registers LABEL(String assem, Label label) – 16 64-bit registers RSP, the stack pointer register – a label statement in the target code – RSP, the stack pointer register • RBP, the frame pointer register – RBP, the frame pointer register MOVE(String assem, Temp dst, Temp src) – 32-bit register names used to access lower 32-bits of corresponding 64-bit register: – similar to OPER in that assem string contains holes, but .. eax, edx, exc, ebx, esi, esi, edi, esp and ebp – no jumps Representations – only one src and dst Temp – Constants prefixed with ‘$’, for example $3, $4, $-5, etc – Registers prefixed with ‘%’, for example %rsp, %rbp, etc. CJUMP(String a, Temp.Temp src1, RELOP op, Temp.Temp src2, Some Instructions Temp.Label t, Temp.Label f) – movl %eax, -12(%rbp) // M[%rbp-12] = %eax – similar to OPER in that assem string contains holes, but .. – addl -20(%rbp), %eax // %eax = M[%rbp-20] * %eax – only jumps to true and false target – only two source Temps for comparison – cmpl -4(%rbp), %eax – explicit conditional operation, which enables later changes in code layout – jge .L2 // if ( M[%rbp-4] >= %eax ) goto .L2 CS453 Lecture Final Review 13 CS453 Lecture Final Review 14 x86-64 example Example continued... .file "funcCall2.c” .globl main .text .type main, @function .globl foo main: .type foo, @function pushq %rbp # %rsp = %rsp-8; M[%esp ] = %rbp; foo: pushes onto stack pushq %rbp # %rsp = %rsp-8; M[%esp ] = %rbp movq %rsp, %rbp # %rbp = %rsp movq %rsp, %rbp # %rbp = %rsp subq $16, %rsp # %rsp = %rsp - 16 movl %edi, -20(%rbp) # storing parameters to stack movl $5, %esi # %esi = 5 movl %esi, -24(%rbp) movl $4, %edi # %edi = 4 movl -24(%rbp), %eax # accessing x addl -20(%rbp), %eax # adding y to x and storing in %eax call foo movl %eax, -4(%rbp) movl %eax, -4(%rbp) # M[%rbp-4] = %eax movl -4(%rbp), %eax movl -4(%rbp), %eax # %eax = M[%rbp-4] leave leave ret ret .size foo, .-foo CS453 Lecture Final Review 15 CS453 Lecture Final Review 16 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend