CA Compiler Construction

Similar documents
On my honor, as an Aggie, I have neither given nor received unauthorized aid on this academic work

ECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University

Processor Design & ALU Design

Lecture 3, Performance

Lecture 3, Performance

Four Important Number Systems

Register machines L2 18

Computer Engineering Department. CC 311- Computer Architecture. Chapter 4. The Processor: Datapath and Control. Single Cycle

CSE 200 Lecture Notes Turing machine vs. RAM machine vs. circuits

Building a Computer. Quiz #2 on 10/31, open book and notes. (This is the last lecture covered) I wonder where this goes? L16- Building a Computer 1

Compiling Techniques

Pipelining. Traditional Execution. CS 365 Lecture 12 Prof. Yih Huang. add ld beq CS CS 365 2

Project Two RISC Processor Implementation ECE 485

Arithmetic and Logic Unit First Part

Computer Architecture. ECE 361 Lecture 5: The Design Process & ALU Design. 361 design.1

EC 413 Computer Organization

Quantum Computing Virtual Machine. Author: Alexandru Gheorghiu Scientific advisor: PhD. Lorina Negreanu

CSCI-564 Advanced Computer Architecture

Computer Science Introductory Course MSc - Introduction to Java

A crash course in Digital Logic

Computer Architecture

TEST 1 REVIEW. Lectures 1-5

CPU DESIGN The Single-Cycle Implementation

mith College Computer Science CSC231 - Assembly Week #7 Dominique Thiébaut

ICS 233 Computer Architecture & Assembly Language

Automata Theory CS S-12 Turing Machine Modifications

Grade 10 Arithmetic Progressions

Operational Semantics

Review. Combined Datapath

COVER SHEET: Problem#: Points

Register Allocation. Maryam Siahbani CMPT 379 4/5/2016 1

EXAMPLES 4/12/2018. The MIPS Pipeline. Hazard Summary. Show the pipeline diagram. Show the pipeline diagram. Pipeline Datapath and Control

Sets. We discuss an informal (naive) set theory as needed in Computer Science. It was introduced by G. Cantor in the second half of the nineteenth

Lecture 13: Sequential Circuits, FSM

Logic and Computer Design Fundamentals. Chapter 8 Sequencing and Control

Grade 7 Integers. Answer t he quest ions. Choose correct answer(s) f rom given choice. Fill in the blanks

A Second Datapath Example YH16

Designing Single-Cycle MIPS Processor

Outline. policies for the first part. with some potential answers... MCS 260 Lecture 10.0 Introduction to Computer Science Jan Verschelde, 9 July 2014

Calculating Algebraic Signatures Thomas Schwarz, S.J.

Lecture 13: Sequential Circuits, FSM

Computer Architecture ELEC2401 & ELEC3441

1. (2 )Clock rates have grown by a factor of 1000 while power consumed has only grown by a factor of 30. How was this accomplished?

Grade 10 Arithmetic Progressions

Algorithms and Data S tructures Structures Complexity Complexit of Algorithms Ulf Leser

CSE Computer Architecture I

3. (2) What is the difference between fixed and hybrid instructions?

Special Nodes for Interface

Numerical Methods - Lecture 2. Numerical Methods. Lecture 2. Analysis of errors in numerical methods

4. (3) What do we mean when we say something is an N-operand machine?

Lecture 5: Arithmetic

Material Covered on the Final

ww.padasalai.net

System Data Bus (8-bit) Data Buffer. Internal Data Bus (8-bit) 8-bit register (R) 3-bit address 16-bit register pair (P) 2-bit address

Grade 9 Number System

Chapter 9 Intermediate code generation

Microprocessor Power Analysis by Labeled Simulation

Department of Electrical and Computer Engineering The University of Texas at Austin

Grade 8 Rational Numbers

Building Algorithmic Polytopes. David Bremner with D. Avis, H.R. Tiwary, and O. Watanabe December 16, 2014

hexadecimal-to-decimal conversion

APPENDIX 1 ERROR ESTIMATION

CSC : Homework #3

Universal Turing Machine. Lecture 20

1. Write a program to calculate distance traveled by light

CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

A Lower Bound of 2 n Conditional Jumps for Boolean Satisfiability on A Random Access Machine

EEE Lecture 1 -1-

LABORATORY MANUAL MICROPROCESSOR AND MICROCONTROLLER

[2] Predicting the direction of a branch is not enough. What else is necessary?

3 The fundamentals: Algorithms, the integers, and matrices

COSE312: Compilers. Lecture 17 Intermediate Representation (2)

ECE290 Fall 2012 Lecture 22. Dr. Zbigniew Kalbarczyk

Lecture 8: Number theory

Deterministic Finite Automaton (DFA)

CS20a: Turing Machines (Oct 29, 2002)

What languages are Turing-decidable? What languages are not Turing-decidable? Is there a language that isn t even Turingrecognizable?

L07-L09 recap: Fundamental lesson(s)!

MACHINE COMPUTING. the limitations

Lecture 3: Finite Automata

Numbering Systems. Contents: Binary & Decimal. Converting From: B D, D B. Arithmetic operation on Binary.

MSE405 Microstructure Characterization XRD-1 Lab X-ray diffraction in crystallography

Chapter 1 Error Analysis

Control. Control. the ALU. ALU control signals 11/4/14. Next: control. We built the instrument. Now we read music and play it...

Tasks of lexer. CISC 5920: Compiler Construction Chapter 2 Lexical Analysis. Tokens and lexemes. Buffering

NCU EE -- DSP VLSI Design. Tsung-Han Tsai 1

Appendix: a brief history of numbers

[2] Predicting the direction of a branch is not enough. What else is necessary?

Grade 6 Integers. Answer t he quest ions. Choose correct answer(s) f rom given choice. Fill in the blanks

Compiling Techniques

Section Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence

SISD SIMD. Flynn s Classification 8/8/2016. CS528 Parallel Architecture Classification & Single Core Architecture C P M

cse 311: foundations of computing Fall 2015 Lecture 12: Primes, GCD, applications

Motivation. Dictionaries. Direct Addressing. CSE 680 Prof. Roger Crawfis

Discrete Mathematics. CS204: Spring, Jong C. Park Computer Science Department KAIST

Lecture 4: Finite Automata

MiniMat: Matrix Language in OCaml LLVM

Remainders. We learned how to multiply and divide in elementary

CA Compiler Construction

Lexical Analysis. Reinhard Wilhelm, Sebastian Hack, Mooly Sagiv Saarland University, Tel Aviv University.

Transcription:

CA4003 - Compiler Construction Code Generation to MIPS David Sinclair Code Generation The generation o machine code depends heavily on: the intermediate representation;and the target processor. We are going to ocus on the generation o symbolic machine code or the MIPS processor, but these concepts can be generalised to other processors. Symbolic machine code is a text-based representation that is translated into binary code by an assembler and linker. Symbolic machine code is: more readable than binary code; use label as destinations o jumps; and allows the use o constants as operands.

MIPS Revision The.data directive deines the ollowing section as data. The.text directive deines the ollowing section as instructions The.space n directive reserves n bytes o memory. The directive.word w1, w2,..., wn reserves and initialises n words. The.asciiz directives deines a null terminated string, e.g..asciiz "Hello". Similarly, the.ascii directive deines a string without the null terminator. The.align n directive causes the next data or code to be located at an address divisible by 2 n. MIPS Revision (2) i n t x ; char y, z ; oo ( ) {... } i n t a [ 1 0 ] ; bar ( ) {... }. data x :. space 4 y :. space 1 z :. space 1. a l i g n 2. t e x t oo : code or oo. data a :. space 40. t e x t bar : code or bar

MIPS Revision (3) A constant n can be loaded into a register r using the load immediate instruction, e.g. l i r, n A scalar global variable can be loaded directly by name, e.g. to load an int into register $5 lw $5, x An element o a global array can be accessed by calculating the oset rom the array s base address, e.g. to load A[i] lw r 0, i # i i was g l o b a l l a r 1, A s l l r 0, r 0, 2 # s c a l e by 4 add r 1, r 1, r 0 lw r 2, (r 1 ) MIPS Revision (4) The MIPS instructions to load an 8-bit character at address addr into a 32-bit register reg, use the lb reg, addr instruction. Similarly, storing a 32-bit register reg into an 8-bit char at address addr, use sb addr, reg. In MIPS, a constant s size is restricted to 16-bits. I a larger constant is needed, the lui instruction is used to load a 16-bit constant into the upper hal o a 32-bit register. Then another 16-bit constant can be ored with the register to orm a 32-bit constant.

Intermediate Code Patterns Each intermediate code instruction, in our case each 3-address code, is mapped to one or more MIPS instructions. To take advantage o complex machine instructions, we use patterns that map a sequence o intermediate code instructions to one or more machine instructions. We try to map the longer patterns irst. Variables marks the last in an intermediate code pattern must be the last use o that variable in the intermediate code. In the ollowing, t, r d, r s and r t can match arbitrary variables and k can match any constant. These are just a subset o possible patterns. It is important that at least one pattern can match any intermediate code instruction. Arithmetic Operations r d := r s + r t r d := r t r d := r s + k r d := k r d := r s r t r d := r s k r d := r s r t r d := r s k r d := r s /r t r d := r s /k add r d, r s, r t add r d, $R0, r t addi r d, r s, k addi r d, $R0, k sub r d, r s, r t addi r d, r s, k mul r s, r t mul r s, k div r s, r t div r s, k The results o mul and div are store in 2 special registers $hi and $lo. For mul $hi has the most signiicant word and $lo has the least signiicant word. For div, $lo has the quotient and $hi has the remainder.

Jumps goto label j label label lbl lbl: i r s = r t then label t else label beq r s, r t, label t label label label : i r s = r t then label t else label bne r s, r t, label label label t label t : beq r i r s = r t then label t else label s, r t, label t j label slt r i r s < r t then label t else label d, r s, r t bne r label label d, $R0, label t label : slt r i r s < r t then label t else label d, r s, r t beq r label label d, $R0, label t label t : slt r d, r s, r t i r s < r t then label t else label bne r d, $R0, label t j label Jumps (2) i r s relop r t then lbl bcc r s, r t, lbl where the condition code cc is given by: op cc op cc op cc <= le < lt!= ne == eq > gt >= ge Additional specialist patterns can be developed or other operators than = and <.

Arrays To read the i th element o a global array x into reg 3, where each element is 2 n bytes wide, we can use: load the value o i into reg 1 load the starting address o x into reg 2 s l l reg 1, reg 2, n #s c a l e o r width u n l e s s n = 0 add reg 2, reg 1, 0(reg 2 ) lw reg 3, 0(reg 2 ) I the element is only 1 byte wide, use lb instead o lw. To store a value at index i o global array x, use the sw, or sb, instruction instead o lw, or lb. Arrays - Example Let x be a global array o integers, y an array o characters at oset 40 rom $sp and i and j be global integers. Then the source language statement y[i] = x[j], could translate to the 3-address code sequence tmp= x[j]; y[i] = tmp, where tmp is a compiler generated temporary that has spilled onto the stack at oset 24. This then generates the ollowing machine code lw $t0, j l a $t1, x # l o a d s t a r t i n g a d d r e s s o x s l l $t0, $t0, 2 add $t1, $t0, $t1 # $t1 i s addr o x [ j ] lw $t2, ( $t1 ) sw $t2, 24( $sp ) # tmp = x [ j ] lw 24( $sp ), $t0 lw $t1, i l a $t2, 40( $sp ) add $t2, $t1, $t2 # $t2 i s addr o y [ i ] sb $t0, ( $t2 ) # y [ i ] = tmp

Procedure Calls We discuss earlier how the callee sets up the stack rame on entry to a procedure. Here we give the code generated or the caller. param x (x is an int/char) load x into reg 1 sw reg 1,-4($sp) la $sp,-4($sp) param x call p, n (x is an array) load address o x into reg 1 sw reg 1,-4($sp) la $sp,-4($sp) jal p la $sp,k($sp) where k = 4 n Building Code Sequences There can be more than one matching pattern. There are 2 algorithmic approaches: Greedy Find the irst longest pattern at the start o the code, translate it and continue with the remaining code. Dynamic Assign a cost to each machine instruction and ind the match that minimises the total cost using dynamic programming. Also the order in which the patterns appear is important! I we are adopting the greedy algorithm, irst longest matching pattern, then we need to arrange the patterns so that when two patterns overlap, the longest pattern is listed irst.

Example Generate symbolic machine code or the ollowing 3-address code sequence a = a + b last d = c + 8 M[d last ] = a i a = c then lbl 1 else lbl 2 label lbl 2 using the ollowing subset o patterns and the Greedy Matching Algorithm. t = r s + k r t = M[t last ] lw r t, k(r s ) r t = M[r s ] lw r t, 0(r s ) r t = M[k] lw r t, 0(r s ) t = r s + k M[t last ] = r r sw r t, k(r s ) Example (2) M[r s ] = r t sw r t, 0(r s ) M[k] = r t sw r t, k($0) r d = r s + r t add r d, r s, r t r d = r s add r d, $0, r t r d = r s + k addi r d, r s, k r d = k addi r d, $0, k goto label j label i r s = r t then lbl t else lbl beq r s, r t, lbl t label lbl lbl : i r s = r t then lbl t else lbl bne r s, r t, lbl label lbl t lbl t : i r s = r t then lbl t else lbl beq r s, r t, lbl t j lbl slt r i r s < r t then lbl t else lbl d, r s, r t bne r label lbl d, $0, lbl t lbl :

Example (3) slt r i r s < r t then lbl t else lbl d, r s, r t beq r label lbl d, $0, lbl t lbl t : i r s < r t then lbl t else lbl bne r d, $0, lbl t slt r d, r s, r t j lbl label label label : Only one pattern matches the start (preix) o our example and generates an add instruction. The preix o the remaining 3-address code lines is matched by 2 patterns. We choose the irst longest pattern. The remainder o the code is matched by another pattern. l b l 2 : add a, a, b sw a, 8( c ) beq a, c, lbl 1