Programming Languages (CS 550) Mini Language Compiler

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
1 Chapter 8: Code Generation. 2 Generating Instructions from Three-address Code Example: D = (A*B)+C =* A B T1 =+ T1 C T2 = T2 D.
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
CS 536 Spring Code generation I Lecture 20.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Chapter 9: Subprogram Control
1 CS Programming Languages Random Access Machines Jeremy R. Johnson.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
CPSC 388 – Compiler Design and Construction Runtime Environments.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
Assembly Language Friday, Week 5 Monday, Week 6. Assembly Language  Set of mnemonic names for the instructions in a particular computer's machine language.
September 26, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 2: Implementation of a Simplified Computer Jeremy R. Johnson Wednesday,
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Jeremy R. Johnson William M. Mongan
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Science 210 Computer Organization Machine Language Instructions: Control.
CS 270: Mathematical Foundations of Computer Science
Overview of Instruction Set Architectures
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
Chapter 7 Subroutines Dr. A.P. Preethy
Basic Block Optimizations
Instructions - Type and Format
Mini Language Interpreter Programming Languages (CS 550)
Lecture 4: MIPS Instruction Set
Systems Architecture I (CS ) Lecture 1: Random Access Machines
Unit IV Code Generation
Computer Science 210 Computer Organization
Code Generation Part I Chapter 8 (1st ed. Ch.9)
MIPS Instructions.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Systems Architecture I (CS ) Lecture 2: A Simplified Computer
Understanding Program Address Space
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Program and memory layout
Systems Architecture I
Intermediate Code Generation
Code Generation Part II
Computer Architecture
10/6: Lecture Topics C Brainteaser More on Procedure Call
Program and memory layout
Systems Architecture I (CS ) Lecture 1: Random Access Machines
Programming Languages (CS 360) Mini Language Compiler
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic Block Optimizations
Presentation transcript:

Programming Languages (CS 550) Mini Language Compiler September 4, 1997 Programming Languages (CS 550) Mini Language Compiler Jeremy R. Johnson

September 4, 1997 Introduction Objective: To illustrate how to map Mini Language instructions to RAL instructions. To do this in a systematic way that illustrates how to write a compiler to translate Mini Language programs to RAL programs. Show simple optimizations that can be used to reduce the number of instructions. Algorithm Construct code for expressions, assignments, if, and while. Concatenate statements in stmt-list Allocate temporaries as needed Keep track of variables, constants, and temporaries in Symbol Table Use symbolic instructions and fill in absolute addresses (linking) when complete code has been constructed

A Random Access Machine September 4, 1997 A Random Access Machine AC Control Unit Memory 1 2 Program 3 ... 4 5 6 AC = accumulator register ...

September 4, 1997 Instruction Set LDA X; Load the AC with the contents of memory address X LDI X; Load the AC indirectly with the contents of address X STA X; Store the contents of the AC at memory address X STI X; Store the contents of the AC indirectly at address X ADD X; Add the contents of address X to the contents of the AC SUB X; Subtract the contents of address X from the AC MUL X; Multiply the contents of address X to the contents of the AC JMP X; Jump to the instruction labeled X JMZ X; Jump to the instruction labeled X if the AC contains 0 JMN X; Jump to the instruction labeled X if the contents of the AC is negative HLT ; Halt execution

Memory Organization Constants Num_Consts Num_Vars get_temp() September 4, 1997 Memory Organization Constants Prog. Variables Temp. Variables Num_Consts Num_Vars get_temp() Num_Temps

Symbolic Instructions September 4, 1997 Symbolic Instructions Addresses and labels can be symbolic names Symbolic names are mapped to actual addresses during linking Example: LD x ST z ADD y JMP L Linked code with (x=100, y =110, z = 105, L = 20) LD 100 ST 105 ADD 110 JMP 20

Symbol Table Map from identifiers → Symbol table entries September 4, 1997 Symbol Table Map from identifiers → Symbol table entries Symbol table entries contain: address [may be unknown] Indicate whether entry is an constant, variable, temporary or label

Expressions expr → expr1 op expr2 Code1 ; result stored in t1 September 4, 1997 Expressions expr → expr1 op expr2 Code1 ; result stored in t1 Code2 ; result stored in t2 LD t1 ; load result of exp1 OP t2 ; apply op to result of exp2 and result of exp1 ST t3 ; store result of exp1 op exp2

Expressions expr → NUMBER ; check to see if NUMBER in symbol table, September 4, 1997 Expressions expr → NUMBER ; check to see if NUMBER in symbol table, ; otherwise add to symbol table LD NUMBER ; load constant from constant table ST tn ; next available temporary

Expressions expr → IDENT ; check to see if IDENT in symbol table September 4, 1997 Expressions expr → IDENT ; check to see if IDENT in symbol table ; otherwise add to symbol table LD IDENT ; load constant from constant table ST tn ; next available temporary

Assignment assign_stmt → IDENT = expr September 4, 1997 Assignment assign_stmt → IDENT = expr ; check to see if IDENT in symbol table ; otherwise add to symbol table Code LD t ST IDENT

Conditional Statements September 4, 1997 Conditional Statements if_stmt → if expr then S1 else S2 fi  if expr > 0 then S1 else S2 fi Codee ; result stored in t LD t ; JMN L1 ; Jump if t  0 then JMZ L1 Code1 JMP L2 L1: Code2 L2:

While Statements while_stmt → while expr do S od September 4, 1997 While Statements while_stmt → while expr do S od  while expr > 0 then S od L1: Codee ; result stored in t LD t ; JMN L2 ; jump if t  0 JMZ L2 CodeS JMP L1 L2:

Statement List stmt-list → stmt; stmt-list | stmt code1 code2 … coden September 4, 1997 Statement List stmt-list → stmt; stmt-list | stmt code1 code2 … coden

Example n := 0 - 5; if n then i := n else i := 0 - n fi; fact := 1; September 4, 1997 Example n := 0 - 5; if n then i := n else i := 0 - n fi; fact := 1; while i do fact := fact * i; i := i - 1 od

Example n := 0 - 5 LD ZERO ST T1 LD FIVE ST T2 LD T1 SUB T2 ST T3 September 4, 1997 Example LD ZERO ST T1 LD FIVE ST T2 LD T1 SUB T2 ST T3 LD T3 ST n n := 0 - 5

Example if n then i := n else i := 0 - n fi; LD n ST T4 LD T4 September 4, 1997 Example if n then i := n else i := 0 - n fi; LD n ST T4 LD T4 JMN L1 JMZ L1 ST T5 LD T5 ST i JMP L2 L1: LD ZERO ST T6 LD n ST T7 LD T6 SUB T7 ST T8 LD T8 ST i L2:

September 4, 1997 Example fact := 1; LD ONE ST T9 LD T9 ST fact

Example while i do fact := fact * i; i := i - 1 od L3: LD i LD T13 September 4, 1997 Example while i do fact := fact * i; i := i - 1 od L3: LD i ST T10 LD T10 JMN L4 JMZ L4 LD fact ST T11 LD i ST T12 LD T11 MUL T12 ST T13 LD T13 ST fact LD i ST T14 LD ONE ST T15 LD T14 SUB T15 ST T16 LD T16 ST i JMP L3 L4:

Complete Example LD ZERO ST T1 LD FIVE ST T2 LD T1 SUB T2 ST T3 LD T3 September 4, 1997 Complete Example LD ZERO ST T1 LD FIVE ST T2 LD T1 SUB T2 ST T3 LD T3 ST n LD n ST T4 LD T4 JMN L1 JMZ L1 ST T5 LD T5 ST i JMP L2 L1: LD ZERO ST T6 LD n ST T7 LD T6 SUB T7 ST T8 LD T8 ST i L2: LD ONE ST T9 LD T9 ST fact L3: LD i ST T10 JMN L4 JMZ L4 LD fact ST T11 LD i ST T12 LD T11 MUL T12 ST T13 LD T13 ST fact LD i ST T14 LD ONE ST T15 LD T14 SUB T15 ST T16 LD T16 ST i JMP L3 L4: HLT

Symbol Table Name Value Type addr ZERO 0 const ? FIVE 5 const ? September 4, 1997 Symbol Table Name Value Type addr ZERO 0 const ? FIVE 5 const ? n u var ? T1 u temp ? T2 u temp ? T3 u temp ? T4 u temp ? T5 u temp ? i u var ? T6 u temp ? T7 u temp ? T8 u temp ? ONE 1 const ? T9 u temp ? fact u var ? T10 u temp ? T11 u temp ? T12 u temp ? T13 u temp ? T14 u temp ? T15 u temp ? T16 u temp ?

Symbol Table and Label Summary September 4, 1997 Symbol Table and Label Summary Num_Vars = 3 Num_Consts = 3 Num_Temps = 16 Constants ZERO -> addr 1 FIVE -> addr 2 One -> addr 3 Variables n -> addr 4 i -> addr 5 fact -> addr 6 Temporaries T1 -> addr 7 T2 -> addr 8 … T16 -> addr 22 L1 = 20 L2 = 29 L3 = 33 L4 = 55

Linked Example LD 1 ST 7 LD 2 ST 8 LD 7 SUB 8 ST 9 LD 9 ST 4 LD 4 September 4, 1997 Linked Example LD 1 ST 7 LD 2 ST 8 LD 7 SUB 8 ST 9 LD 9 ST 4 LD 4 ST 10 LD 10 JMN 20 JMZ 20 ST 11 LD 11 ST 5 JMP 29 L1: LD 1 ST 12 LD 4 ST 13 LD 12 SUB 13 ST 14 LD 14 ST 5 L2: LD 3 ST 15 LD 15 ST 6 L3: LD 5 ST 16 JMN 55 JMZ 55 LD 6 ST 17 LD 5 ST 18 LD 17 MUL 18 ST 19 LD 19 ST 6 LD 5 ST 20 LD 3 ST 21 LD 20 SUB 21 ST 22 LD 22 ST 5 JMP 33 L4: HLT

Optimizations Peephole optimization September 4, 1997 Optimizations Peephole optimization Remove LD immediately following by ST Commute (expr1,expr2) in expr → expr1 op expr2 to allow additional peephole optimizations Constant folding Common subexpression elimination

Complete Example (after peephole optimization) September 4, 1997 Complete Example (after peephole optimization) LD FIVE ST T2 LD ZERO ST T1 SUB T2 ST T3 ST n ST T4 JMN L1 JMZ L1 LD n ST T5 ST i JMP L2 L1: LD ZERO ST T6 LD n ST T7 LD T6 SUB T7 ST T8 ST i L2: LD ONE ST T9 ST fact L3: LD i ST T10 JMN L4 JMZ L4 LD i ST T12 LD fact ST T11 MUL T12 ST T13 ST fact LD i ST T14 LD ONE ST T15 LD T14 SUB T15 ST T16 ST i JMP L3 L4: HLT 46 vs. 56 instructions

Supporting Procedures September 4, 1997 Supporting Procedures Fully static environment No recursion Activation record Parameters Local variables (keep count) Return address (indirect jump needed) Can be statically allocated Dynamic environment Allow recursion Call stack (dynamic allocation) Use stack pointer (sp) and frame pointer (fp) access stack Indirect load and store needed

Memory Organization FP, SP & scratch Constants Constants Global September 4, 1997 Memory Organization FP, SP & scratch Constants Global Prog. Variables Temp. Variables Activation Records Constants Global Prog. Variables Global Temp. Variables Call Stack Static Dynamic

Program Memory Organization September 4, 1997 Program Memory Organization Procedure Entry in Function Table Number of parameters Number of local/temp variables Starting address Number of instructions Need to know starting address of main program Procedures P1 P2 P3 Main Program

Activation Record Frame Pointer Parameters Local Variables September 4, 1997 Activation Record Frame Pointer Parameters Local Variables Temp. Variables Return Address For call stack Stack Pointer

Example: fact(n) define fact proc(n) i := n; f := 1; while i do September 4, 1997 Example: fact(n) define fact proc(n) i := n; f := 1; while i do f := f * i; i := i - 1 od; return := f end

fact(n) Note that addressing (LD/ST) different than main program. September 4, 1997 fact(n) LD n ST T1 LD T1 ST i LD ONE ST T2 LD T2 ST fact L1: LD i ST T3 JMN L2 JMZ L2 LD fact ST T4 LD i ST T5 LD T4 MUL T5 ST T6 LD T6 ST fact LD i ST T7 LD ONE ST T8 LD T7 SUB T8 ST T9 LD T9 ST i JMP L1 L2: LD fact ST T10 LD T10 ST return Note that addressing (LD/ST) different than main program. If main were a function the code would be uniform.

Activation Record Accessing AR n FP i fact T1 T2 T3 T4 T5 T6 T7 T8 T9 September 4, 1997 Activation Record n FP i Accessing AR LD n  LDI FP ST n  STI FP LD i  LD FP ADD ONE ST FPB LDI FPB  LDO FP[1] ST i  STO FP[1] LD Tj  LDO FP[j+Num_Param+Num_Vars] fact T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 return SP prev fp ret. addr.

Calling Sequence Initiate call Create activation record September 4, 1997 Calling Sequence Initiate call Create activation record Update FP and SP Store parameters in activation record Store return address (RA) Jump to starting address of procedure code Introduce call instruction (can place RA relative to SP) Can compute RA from PC Return from call Store return value in activation record (when return is assigned) Jump to RA Introduce ret instruction (jmp indirect) Retrieve return value from activation record