ECS 142 Project: Code generation hints (2)

Similar documents
ECS 142 Project: Code generation hints

Formal Languages and Compilers Lecture X Intermediate Code Generation

Module 25 Control Flow statements and Boolean Expressions

CMPSC 160 Translation of Programming Languages. Three-Address Code

Module 27 Switch-case statements and Run-time storage management

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

Syntax-Directed Translation

Course Administration

Midterm II CS164, Spring 2006

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

Lecture 4: MIPS Instruction Set

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

Code Genera*on for Control Flow Constructs

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

MIPS Functions and the Runtime Stack

The Activation Record (AR)

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Intermediate Representations

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

CSE Lecture In Class Example Handout

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Intermediate Code Generation

ECE/CS 314 Fall 2003 Homework 2 Solutions. Question 1

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

Code Generation. Lecture 19

CS61C : Machine Structures

CS2210: Compiler Construction. Code Generation

Lecture 7: Procedures

Intermediate Code Generation

Compiling Techniques

Compiling Code, Procedures and Stacks

CS61C : Machine Structures

We will study the MIPS assembly language as an exemplar of the concept.

ECE 30 Introduction to Computer Engineering

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Module 26 Backpatching and Procedures

Review of Activation Frames. FP of caller Y X Return value A B C

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

THEORY OF COMPILATION

SPIM Procedure Calls

Virtual Machine Tutorial

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

CS 61c: Great Ideas in Computer Architecture

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions

CS153: Compilers Lecture 8: Compiling Calls

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

Compilers and computer architecture: A realistic compiler to MIPS

ECE 15B Computer Organization Spring 2010

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

THEORY OF COMPILATION

CSE Lecture In Class Example Handout

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

UCB CS61C : Machine Structures

Reduced Instruction Set Computer (RISC)

Intermediate Representations

University of California at Santa Barbara. ECE 154A Introduction to Computer Architecture. Quiz #1. October 30 th, Name (Last, First)

ECE 30 Introduction to Computer Engineering

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

CSEE W3827 Fundamentals of Computer Systems Homework Assignment 3 Solutions

CS 316: Procedure Calls/Pipelining

Lecture 5: Procedure Calls

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

PART 6 - RUN-TIME ENVIRONMENT. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Readings and References. Procedure Detail. Leaf procedures. Non-leaf procedure. Calling tree. Layout of stack frame (little leaf)

Previously. CSE 2021: Computer Organization. Memory Organization. The Load/Store Family (1) 5/26/2011. Used to transfer data to/from DRAM.

CS61C : Machine Structures

EE 361 University of Hawaii Fall

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Code Generation. Lecture 12

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

Lecture 9: Procedures & Functions. CS 540 George Mason University

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

Lecture 7: Procedures and Program Execution Preview

Anne Bracy CS 3410 Computer Science Cornell University

Homework 1 Answers. CS 322 Compiler Construction Winter Quarter 2006

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

1/26/2014. Previously. CSE 2021: Computer Organization. The Load/Store Family (1) Memory Organization. The Load/Store Family (2)

Code Generation. Lecture 30

MIPS function continued

intermediate-code Generation

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Code Generation Super Lectures

CMSC 611: Advanced Computer Architecture

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

Transcription:

ECS 142 Project: Code generation hints (2) Winter 2011 This document describes the nature of final code generation for your project. I have made a number of assumptions here, mostly in terms of availability of information regarding various aspects of the parse tree and the symbol tables. They are specified in terms of method calls to various data structures. You will need to implement them so that they are specific to your data structures. First, assume that the following are defined (and implemented by you): Symbol table: A symbol table contains a set of entries (one for each entity such as method, variable, class, etc.). Each entity will be represented by a pointer to its entry in the table. We can use the pointer to access various information about the entity (for instance, its identifier, offset, size, type etc.). E.code: code sequence for evaluating nonterminal E. This can be stored as part of the data structure associated with nonterminal E. E.GenCode(): a method that generates code for E. E.GenCode(label1, label2): A method that generates code for expression E with labels label1 and label2. Labels can be simple strings that you pass to GenCode. This document will show how GenCodes for various language constructs are specified. GenLabel(): a method used for generating labels. You can define a global method that will return a unique label every time it is called. Generated labels will be used as targets of assembly jump instructions. GenTemp(): A method for creating temporary variables. In addition to creating the temporary, it will put the temporary in current symbol table. It returns a pointer to temporary variable s entry in the symbol table. id.symtab: Defines the pointer to id s entry into a symbol table. This pointer will be used to determine id s size, type, offset etc. E.symtab: Defines the pointer to the symbol table entry which will hold the value of E at runtime. This entry may be a constant, a variable or a temporary generated for E. For instance, if value of E will be be stored in a temporary variable, say T1, E.symtab will point to the symbol table entry for T1. CurrentSymbolTable: Pointer to the top symbol table on stack. 1 Expressions The primary goal in generating code for expression is to generate code for evaluating simple expressions (expressions involving one or two operands), and store them into temporaries. E id // find the symbol table entry for the identifier, and make // it symbtab of expression. E.symtab = CurrentSymbolTable->FindId(id); // assumption: FindId returns a pointer to symbol table entry for id. E.type = (E.symtab)->type(); // get type of identifier. -1-

E integer constant: E.symtab = NULL; // This E is a constant. E.type = INTEGER; // type of expression E.val = integer_constant.val; // get the value of literal // assumption: integer_constant.val contains the value // associated with the token. Also, E.val stores the constant // value directly. Similar code will be generated for other kinds of constants. E E1 op E2: E1.GenCode(); // generate code for E1 E2.GenCode(); // generate code for E2 E.code = E1.code + E2.code; // add generated code of E1 and E2. // E1.symtab points to entry where value for E1 will be stored; // similarly for E2.symtab if ((E1.symtab!= NULL) && (E1.symtab->PrimitiveType()) { // assumption: E1.symtab->SimpleType() returns true if // E1.symtab is of primitive type. // E1.symtab is simple local variable or temporary, which means // it will be on stack at a fixed offset // generate code for loading E1.symtab in a register, say 19 sprintf(buffer, "lw $19, %d($fp)\n", (E1.symtab)->offset()); // assumption: (E1.symtab)->offset() returns offset of entry. } else if ((E1.symtab!= NULL) && ((E1.symtab)->ReferenceType()) { // E1.symtab is a reference; // generate code for accessing variable from heap. Look at // lecture 14 handout how that can be done. } else { // E1 is a constant. // use type of E1 to determine what constant to load sprintf(buffer, "li $19, %d\n", E1.val); } E.code = E.code + buffer; // add buffer to E.code // similar code for E2 // add code from E2 to E.code // generate a temporary variable that will store value computed // by E. E.symtab = GenTemp(); // this puts temporary in symbol table (E.symtab)->type = E.type; // after putting in symbol table, calculate offset for temporary. CurrentSymbolTable->EvalOffset(E.symtab); // assumption: CurrentSymbolTable->EvalOffset evaluates offset of // the passed entry pointer. // generate code for evaluating operation: switch (op) { case ADDITION: sprintf(buffer, "add $19, $19, $20\n"); break; -2-

case... : } E.code = E.code+buffer; // now generate code for storing value back sprintf(buffer, "sw $19, %d($fp)\n", E.symtab.offset); E.code = E.code+buffer; E.code will contain the assembly code for these set of instructions. For assignment expression E id = E1, GenCode: E1.GenCode(); // generate code for right hand side // note E.symtab will contain pointer to entry that will store // its value. // code for loading E s value. Same as above. // assume that register 19 contains E s value if ((id.symtab)->simpletype()) { // id is simple local variable or temporary, which means // it will be on stack at a fixed offset // generate code for storing value in register 19 at fixed offset sprintf(buffer, "sw $19, %d($fp)\n", (id.symtab)->offset()); } else { // variable is a reference; // generate code for storing information on heap. } E.code = E1.code + buffer; Other kinds of expressions can be implemented similarly. 2 Boolean expressions Boolean expressions are evaluated by generating jmps. E true sprintf(buffer, "%s:\n", E.true); E.code = buffer; E false sprintf(buffer, "%s:\n", E.false); E.code = buffer; E E1 or E2 E1.true = E.true; E1.false = GenLabel(); E2.true = E.true; E2.false = E.false; E1.GenCode(E1.true, E1.false); sprintf(buffer, "%s:\n", E1.false); E2.GenCode(E2.true, E2.false); E.code = E1.code + buffer + E2.code; -3-

E E1 and E2 E1.true = GenLabel(); E1.false = E.false; E2.true = E.true; E2.false = E.false; E1.GenCode(E1.true, E1.false); sprintf(buffer, "%s:\n", E1.true); E2.GenCode( E2.true, E2.false); E.code = E1.code + buffer + E2.code; E id1 relop id2 sprintf(buffer, "lw $19, %d($fp)\n", (id1.symtab)->offset()); sprintf(buffer1, "lw $20, %d($fp)\n", *id2.symtab)->offset()); sprintf(buffer2, "ble $19, $20 %s\n", E.true ); sprintf(buffer3, "jmp %s\n", E.false); E.code = buffer+buffer1+buffer2+buffer3; 3 Statements Statements can be evaluated by generating code for sub-statements, and proper jumps. S if E then S1 else S2 E.true = GenLabel(); E.false = GenLabel(); S.next = GenLabel(); E.GenCode( E.true, E.false); sprintf(buffer, "%s:\n", E1.true); S1.GenCode(); sprintf(buffer2, "jmp %s:\n", S.next); sprintf(buffer3, "%s:\n", E1.false); S2.GenCode(); sprintf(buffer4, "%s:\n", S.next); s.code = E.code + buffer + S1.code + buffer2 + buffer3 + S2.code + buffer4; S while E do S1 S.begin = GenLabel(); E.true = GenLabel(); E.false = GenLabel(); sprintf(buffer, "%s:\n", S.begin); E.GenCode(E.true, E.false); sprintf(buffer1, "%s:\n", E1.true); S1.GenCode(); sprintf(buffer2, "jmp %s:\n", S.begin); sprintf(buffer3, "%s:\n", E1.false); S.code = buffer + E.code + buffer1 + S1.code + buffer2 + buffer3; MethodDecl ResultType MethodName ( ArgList ) Block -4-

// generate code for block first. This will allow us to // generate all temporaries. Block.GenCode(); ArSize = Block.FindARsize(); // find activation record size //assumption: FindArSize() returns size of AR associated with block VarSize = Block.FindVarSize(); // assumption: FindVarSize() returns total space occupied by variables on AR ArgList.EvalParameterOffsets(); // evaluate offsets for params // generate prologue sprintf(beg, ".text\n"); sprintf(beg1, ".ent %s\n", MethodName.AssemblyName()); sprintf(beg2, "%s:\n", MethodName.AssemblyName()); sprintf(beg3, "subu $sp, %d:\n", ArSize); sprintf(beg4, "sw $31, %d($sp):\n", ArSize-VarSize); sprintf(beg5, "sw $fp, %d($sp):\n", ArSize-VarSize-4); sprintf(beg6,"addu $fp, $sp, %d\n", ArSize); // now generate epilogue sprintf(end, "lw $31, %d($sp)\n", ArSize-VarSize); sprintf(end1, "lw $fp, %d($sp):\n",arsize-varsize-4); sprintf(end2, "addu \$sp, %d\n", ArSize); sprintf(end3, "j $31 \n"); sprintf(end4, ".end %s\n", MethodName.AssemblyName()); //combine epilogue, body code, and prologue MetodDecl.code = sum of all beg buffers + Block.code + sum of all end buffers. Method invocations: MethodInvocation MethodName(ParameterList) ParameterList Param ParameterList1 ɛ We look at GenCode for ParameterList first: Param.GenCode(); // generate code for evaluating parameters // generate code for rest of parameters ParamList1.GenCode(); // generate code for rest of list // generate code for pushing this parameter on stack sprintf(buf, "subu $sp, %d\n", (Param.symtab)->size()); // assumption: (Param.symtab)->size() returns size of parameter sprintf(buf1, "lw $19, %d($fp)\n", (Param.symtab)->offset()); sprintf(buf2, "sw $19, %d($sp)\n",(param.symtab)->size()); // order of addition of buffers is important to push parameters // in right order on stack. ParamList.code = Param.Code + ParamList1.code + buf + buf1 + buf2; Now for method invocation expression is: ParameterList.GenCode(); // generate code for jumping to function sprintf(buf, "jal %s\n", MethodName.AssemblyName()); // generate code for fixing stack -5-

int paramsize = MethodName.GetParamSize(); // assumption: GetParamSize returns size of parameters of method sprintf(buf1, "%s:\n", MethodName.AssemblyName()); sprintf(buf2, "addu $sp, %d\n", paramsize); MethodInvocation.code = ParameterList.code + buf + buf1 + buf2; -6-