Joey Paquet, 2000-20141 Lecture 11 Code Generation.

Slides:



Advertisements
Similar presentations
Monday, January 13, Instructor Development Unit 1 Instructional Responsibilities Ed Humphrey.
Advertisements

1/15/ A car starts from rest and travel 10s with uniform acceleration 5m/s 2. Find out its final velocity. Here, u=0 m/s t=10s a= 5m/s 2 v=? We.
International Technology Alliance In Network & Information Sciences International Technology Alliance In Network & Information Sciences 1 Interference.
State the purpose of the presentation. State the outcome of the presentation. (expectation – go/no decision) 2/14/20142St. Louis Public Schools.
Modeling Software Systems Lecture 2 Book: Chapter 4.
Group 4 : Christopher Thorpe Jonghyun Kim ELEG-652 Principles of Parallel Computer Architectures Instructor : Dr. Gao Mentor : Joseph Data : 12/9/05.
The First Few Pages By: Jeton Mehmeti Joshi Dibyadhar
1 B. Bruidegom Computer Architecture Top down approach B. Bruidegom AMSTEL-instituut.
1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization.
Shortest Violation Traces in Model Checking Based on Petri Net Unfoldings and SAT Victor Khomenko University of Newcastle upon Tyne Supported by IST project.
EX 6B THE RELATIONSHIP BETWEEN ARITHMETIC SEQUENCES AND FIRST ORDER DIFFERENCE EQUATIONS.
10/22/20141 Consumers, Producers, and the Efficiency of Markets Chapter 7.
Application: The Costs of Taxation
Compiler Construction
Chapter 6 Intermediate Code Generation
Slides created by: Professor Ian G. Harris Efficient C Code  Your C program is not exactly what is executed  Machine code is specific to each ucontroller.
Compiler Construction Sohail Aslam Lecture Code Generation  The code generation problem is the task of mapping intermediate code to machine code.
©UCB CPSC 161 Lecture 3 Prof. L.N. Bhuyan
ARM versions ARM architecture has been extended over several versions.
CSC 3210 Computer Organization and Programming
Embedded Systems Programming
RAT R1 R2 R3 R4 R5 R6 R7 Fetch Q RS MOB ROB Execute Retire.
Cosc 2150: Computer Organization
Overheads for Computers as Components 2nd ed.
CS/COE0447 Computer Organization & Assembly Language
CPSC 330 Fall 1999 HW #1 Assigned September 1, 1999 Due September 8, 1999 Submit in class Use a word processor (although you may hand-draw answers to Problems.
Target Code Generation
CBP 2002Repository1 Overview PC Structure 1. CBP 2002Repository2.
CMPUT Compiler Design and Optimization
MIPS Assembly Tutorial
F28PL1 Programming Languages Lecture 3: Assembly Language 2.
ITEC 352 Lecture 13 ISA(4).
Target code Generation Made by – Siddharth Rakesh 11CS30036 Date – 12/11/2013.
Code Generation.
Lecture 9 – OOO execution © Avi Mendelson, 5/ MAMAS – Computer Architecture Lecture 9 – Out Of Order (OOO) Dr. Avi Mendelson Some of the slides.
Lecturer PSOE Dan Garcia
Lecture 13: 10/8/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Lecture 5: MIPS Instruction Set
MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
Assembly Code Example Selection Sort.
Fall EE 333 Lillevik 333f06-l4 University of Portland School of Engineering Computer Organization Lecture 4 Assembly language programming ALU and.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Computer Architecture Lecture 2 Instruction Set Principles.
Computer Architecture Lecture 3 C and Assembly. Intro to Assembly Language MIPS and Intel Variables and Constants int count = 10, I, j, k; count.word.
CS 300 – Lecture 6 Intro to Computer Architecture / Assembly Language Instructions.
MIPS Instruction Set Advantages
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Code generation Joey Paquet,
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Computer Organization Instructions Language of The Computer (MIPS) 2.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS Instruction Set Advantages
Compiler design Code generation COMP 442/6421 – Compiler Design
MIPS Assembly.
Data Hazard Example (stall).
3.
COMS 361 Computer Organization
COMS 361 Computer Organization
MIPS e pipelining Tecniche di base.
Appendix C Practice Problem Set 1
Data Hazard Example (no stalls).
MIPS assembly.
7/6/
MIPS instructions.
Conditional Branching (beq)
Presentation transcript:

Joey Paquet, Lecture 11 Code Generation

Joey Paquet, Code Generation Examples Variable declaration: where x and y are the addresses of x and y, which are labels generated during the parse and stored in the symbol table To load or change the content of a variable: where x is the label of variable x, r1 is the register containing the value of variable x and r0 is assumed to contain 0 (offset) x dw 0 y dw 0 lw r1,x(r0) sw x(r0),r1

Joey Paquet, Arithmetic Operations a+b lw r1,a(r0) lw r2,b(r0) add r3,r1,r2 t1 dw 0 sw t1(r0),r3 a+8 lw r1,a(r0) addi r2,r1,8 t2 dw 0 sw t2(r0),r2 a*b lw r1,a(r0) lw r2,b(r0) mul r3,r1,r2 t3 dw 0 sw t3(r0),r3 a*8 lw r1,a(r0) muli r2,r1,8 t4 dw 0 sw t4(r0),r2

Joey Paquet, Relational Operations a==b lw r1,a(r0) lw r2,b(r0) ceq r3,r1,r2 t5 dw 0 sw t5(r0),r3 a==8 lw r1,a(r0) ceqi r2,r1,8 t6 dw 0 sw t6(r0),r2

Joey Paquet, Logical Operations a and b lw r1,a(r0) lw r2,b(r0) and r3,r1,r2 t7 dw 0 bz r3,zero1 addi r1,r0,1 sw t7(r0),r1 j endand1 zero1sw t7(r0),r0 endand1 not a lw r1,a(r0) not r2,r1 t8 dw 0 bz r2,zero2 addi r1,r0,1 sw t8(r0),r1 j endnot1 zero2 sw t8(r0),r0 endnot1

Joey Paquet, Assignment Operation a := b+c {code for b+c. yields tn as a result} lw r1,tn(r0) sw a(r0),r1 a := 8 sub r1,r1,r1 addi r1,r1,8 sw a(r0),r1 a := b lw r1,b(r0) sw a(r0),r1

Joey Paquet, Conditional Statements if a>b then a:=b else a:=0; [1] [2] [3] [4] [5] [6] {code for "a>b". yeilds tn as a result}[1] lw r1,tn(r0)[2] bz r1,else1[2] {code for "a:=b”}[3] j endif1[4] else1 [4] {code for "a:=0”}[5] endif1[6] {code continuation}

Joey Paquet, Loop Statements while a<b do a:=a+1; [1] [2] [3] [4] [5] gowhile1 [1]{code for "a<b". yeilds tn as a result}[2] lw r1,tn(r0)[3] bz r1,endwhile1[3] {code for statblock (a:=a+1)}[4] j gowhile1[5] endwhile1[5] {code continuation}

Joey Paquet, Translating Functions There are two essential parts in translating functions: –translating function declarations –translating function calls First, the compiler encounters a function header. It can either be a function prototype or the header of a full function declaration In both cases, a record can be created in the global symbol table, and a local symbol table can be created for new functions In the case of a full definition, the code is generated for the variable declarations and statements inside the body of the function

Joey Paquet, Translating Functions The address field in the symbol table entry of the function contains the address (or an ASM label) of the first memory cell assigned to the function. This address will be used to jump to the function when a function call is encountered and translated Function calls raise the need for semantic checking. The number and type of actual parameters must match with the information stored in the symbol table for that function Once the semantic check is successful, semantic translation can occur for the function call

Joey Paquet, Function Declarations int fn ( int a, int b ){ statlist }; [1] [2] [3] [4] [5] fndw 0[1] fnp1 dw 0[2] sw fnp1(r0),r2[2] fnp2 dw 0[3] sw fnp2(r0),r3[3] {code for local var. decl. & statement list}[4] lw r2,fnp1(r0)[5] lw r3,fnp2(r0)[5] jr r15[5]

Joey Paquet, Function Declarations fn corresponds both to the first instruction in the function and the address of the return value of fn Parameters are copied to registers at function call and copied in the local variables when the function execution begins Before resolution, the parameters are copied to registers and copied back to the actual parameters in the calling function upon returning from the function This limits the possible number of parameters to the number of registers available r15 is reserved for link

Joey Paquet, Function Calls For languages not allowing recursive function calls, only one occurrence of any function can be running at any given time In this case, all variables local to the function are statically allocated at compile time. The only things there are to manage are: –the jump to the function code –the passing of parameters upon calling and return value upon completion –the jump back to the instruction following the function call

Joey Paquet, Function Call (no parameter) fn()... {code for calling function} jl R15,fn {code continuation in the calling function}... fn {code for called function}... lw r1,fn(r0) jr R15...

Joey Paquet, Passing Parameters Parameters are sometimes passed using registers. In this case, the number of parameters passed cannot exceed the total number of registers The return value can also be passed using a register, typically r1

Joey Paquet, Passing Parameters (Registers) x = fn(p1,p2);... {code for the calling function} lw r2,p1(r0) lw r3,p2(r0) jl r15,fn sw x(r0),r1 {code continuation in the calling function}... fn {refer to param[i] as ri+1 in fn code}... lw r1,fn(r0) jr r15

Joey Paquet, Passing Parameters To avoid the limitation of the allowed number of parameters to the number of registers, parameters can be stored following the function call jump These methods are only usable for languages where recursive function calls are not allowed With recursive function calls, the problem is that several occurences of the same function can be running at the same time In this case, all the variables and parameters of a running function are stored in an activation record dynamically allocated on the process stack This involves the elaboration of a run-time system as part of the compiled code

Joey Paquet, Passing Parameters (by value) fn dw 0 fnp1 dw 0 fnp2 dw 0 {code for called function} {refer to param[i] as fnpi} {put return value in r1} jr r15 x = fn(a,b) {code before} lw r1,a(r0) sw fnp1(r0),r1 lw r1,b(r0) sw fnp2(r0),r1 jl r15,fn sw x(r0),r1 {code after}

Joey Paquet, Calling the Operating System Some function calls interact with the operating system, e.g. when a program does input/output In these cases, there are several possibilities depending on the resources offered by the operating system, e.g.: –treatment via special predefined ASM operations –access to the OS via calls or traps In the MOON processor, we have two special operators: PUTC and GETC They respectively output some data to the screen and input data from the keyboard They are used to directly translate read() and write() function calls (see the MOON manual)

Joey Paquet, Part II Hints for Assignment IV

Joey Paquet, Hints for Assignment IV Keep in mind that constructing the symbol tables is part of semantic checking. It should not be taken as a separate topic. Your semantic actions should include symbol table construction function calls. For assignment IV, the most important thing is to proceed in stages. Do not try to resolve all semantic checking and translation in your first try.

Joey Paquet, Hints for Assignment IV First resolve all semantic checking, but again by proceeding in stages. Implement semantic checking for a small part of the grammar, use a complete test case to make sure this part works properly, and then resolve another part of semantic checking. After all semantic checking is done, you can begin code generation, which should be relatively easy if semantic checking is done properly (semantic checking is a condition to semantic translation). Again, proceed in stages for semantic translation.

Joey Paquet, Hints for Assignment IV Suggested sequence: –variable declarations –expressions (one operator at a time) –assignment statement –conditional statement –loop statement Tricky parts: –expressions involving arrays (offset calculation) –function calls –floating point numbers –recursive function calls –user-defined data types (offset calculations)

Joey Paquet, Hints for Assignment IV You will not fail the project if you did not implement code generation for all aspects of the language. But, you might fail if your compiler is not working. This is why you should proceed in stages and make sure each stage is correct before going further. Be careful to not break what was previously working. Make sure you have a compiler that works properly for a subset of the problem. For the part that you did not implement, think of a solution.You may get some marks if you are able to clearly explain how to do what is missing during your project demonstration.