Talen en Compilers. Johan Jeuring , period 2. December 15, Department of Information and Computing Sciences Utrecht University

Similar documents
INFOB3TC Exam 2. Sean Leather. Monday, 30 January 2012, 17:00 20:00

INFOB3TC Solutions for Exam 2

INFOB3TC Exam. Johan Jeuring. Monday, 31 January 2011, 09:00 11:30, EDUC-GAMMA

Virtual Machine Tutorial

SOURCE LANGUAGE DESCRIPTION

Intermediate Representations

S3.0 : A multicore 32-bit Processor

CMa simple C Abstract Machine

Project 3 Due October 21, 2015, 11:59:59pm

Code generation scheme for RCMA

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

2/6/2018. Let s Act Like Compilers! ECE 220: Computer Systems & Programming. Decompose Finding Absolute Value

Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

Grundlagen Microcontroller Processor Core. Günther Gridling Bettina Weiss

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.

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

Xuan Guo. Lecture XIX: Subroutines (2) CSC 3210 Computer Organization and Programming Georgia State University. March 31, 2015.

Assembly Language Programming of 8085

Compiler Construction I

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

Assembly Language Programming of 8085

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

CSCE 5610: Computer Architecture

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

Branch Instructions. R type: Cond

ARM Architecture and Instruction Set

EE 361 University of Hawaii Fall

G Programming Languages - Fall 2012

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality

Raspberry Pi / ARM Assembly. OrgArch / Fall 2018

Compiling Code, Procedures and Stacks

3.1 DATA MOVEMENT INSTRUCTIONS 45

Chapter 10 Memory Model for Program Execution. Problem

The PAW Architecture Reference Manual

Computer Systems Lecture 9

Compiler Construction

Implementing Functions at the Machine Level

The TinyJ Compiler's Static and Stack-Dynamic Memory Allocation Rules Static Memory Allocation for Static Variables in TinyJ:

Oct 13, 2015 Robert Heckendorn University of Idaho ======================================================================

ARM Shift Operations. Shift Type 00 - logical left 01 - logical right 10 - arithmetic right 11 - rotate right. Shift Amount 0-31 bits

Subroutine. Chapter 8

Course Administration

High Performance Computing Lecture 7. Matthew Jacob Indian Institute of Science

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

System Software Assignment 1 Runtime Support for Procedures

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18

3 Assembly Programming (Part 2) Date: 07/09/2016 Name: ID:

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

IR Generation. May 13, Monday, May 13, 13

Compiler Construction

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

Chapter 3. Z80 Instructions & Assembly Language. Von Neumann Architecture. Memory. instructions. program. data

Architecture. Digital Computer Design

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones

CS 320: Concepts of Programming Languages

Comparison InstruCtions

ECE232: Hardware Organization and Design

Compilation a primer

Summary: Direct Code Generation

Three-Address Code IR

Code Generation. Dragon: Ch (Just part of it) Holub: Ch 6.

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

The Assembly Language of the Boz 5

Vint Cerf - UCLA. Eric Harslem - Rand. John Heafner - Rand NIC Updates: None THE DATA RECONFIGURATION SERVICE --

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College

CS64 Week 5 Lecture 1. Kyle Dewey

Midterm 2 Review Chapters 4-16 LC-3


Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Wednesday, October 15, 14. Functions

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

Summer 2003 Lecture 4 06/14/03

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Practical Malware Analysis

1/30/2018. Conventions Provide Implicit Information. ECE 220: Computer Systems & Programming. Arithmetic with Trees is Unambiguous

6.001 Recitation 23: Register Machines and Stack Frames

bash Execution Control COMP2101 Winter 2019

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

CODE GENERATION Monday, May 31, 2010

Generation System Advanced Seminar on Compilation and Run-time Systems for Object-Oriented Languages. David Bélanger

Instruction Set Architecture (Contd)

LEO-1 Homebrew Computer

ISaGRAF complies with the requirements set forth in IEC , for the following language features:

Intel 8086: Instruction Set

ARM Cortex-M4 Programming Model Flow Control Instructions

Computer Architecture /

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

Chapter 2A Instructions: Language of the Computer

ARM Cortex-M4 Architecture and Instruction Set 3: Branching; Data definition and memory access instructions

Parsing Scheme (+ (* 2 3) 1) * 1

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Wednesday, April 16, 2014

SISTEMI EMBEDDED. Stack, Subroutine, Parameter Passing C Storage Classes and Scope. Federico Baronti Last version:

Intermediate Code Generation

Lecture 5: Procedure Calls

Transcription:

Talen en Compilers 2016-2017, period 2 Johan Jeuring Department of Information and Computing Sciences Utrecht University December 15, 2016

9 Simple stack machine 9-1

Recap: Semantic functions In the previous lectures, we have seen how to evaluate (interpret) expressions We have added variables and talked about environments We have added local definitions and talked about nesting and blocks We have added (mutually) recursive definitions and talked about scoping Now we are going to generate code in a low-level language instead of interpreting the expression directly 9-2

This lecture Simple stack machine Architecture of the simple stack machine Instructions Translating programs Functions / methods 9-3

91 Architecture of the simple stack machine 9-4

Simple stack machine A virtual machine that executes programs consisting of assembly language instructions The program is a list of instructions with arguments, stored in a continuous block of memory A stack is used to store the current state of execution There are eight registers, four with a special name: Question the program counter (PC) the stack pointer (SP) the mark pointer (MP) the return register (RR) 9-5 Why a stack?

Execution A step in the execution interprets the instruction pointed to by the program counter Depending on the instruction, the contents of the stack and registers are modified Example: LDC (load constant) SP post = SP pre + 1 (increment stack pointer) M post [SP post ] = M pre [PC pre + 1] (place argument on stack) PC post = PC pre + 2 (adjust program counter) 9-6

Visualizing the execution SP post = SP pre + 1 (increment stack pointer) M post [SP post ] = M pre [PC pre + 1] (place argument on stack) PC post = PC pre + 2 (adjust program counter) LDC 42 PC 23 42 SP MP 9-7 RR The instruction LDC 42 takes up two words in memory, but we write it in one cell

92 Instructions 9-8

Instructions Most instructions can be classified into the following groups: load instructions store instructions jump instructions arithmetic and logical operations 9-9

Load and store instructions LDC load constant LDR load from register LDL load local LDS load from stack LDLA load local address LDA load via address STR store to register STL store local STS store to stack SDA store via address 9-10

Load instructions LDC load constantldr load from registerldl load locallds load from stackldla load local addresslda load via addressnop noophalt halt program 9-11 LDC 17 LDR RR LDL 2 LDS -3 LDLA 1 LDA 2 NOP HALT PC SP MP 31 RR 12 17 31 75 12 81 59 63 75 81

Branch instructions BRA branch always (unconditional) BRT branch on true ( 1) BRF branch on false (0) BSR branch to subroutine (push return address on stack) RET return (from subroutine) 9-12

Register and jump instructions LDRR load register from registerajs adjust stack pointerbra unconditional branchbsr branch to subroutineldc load constantstr store to registerret returnldr load from register 9-13 LDRR R5 RR AJS -2 BRA +5 LDC 91 STR RR RET BSR -7 LDR RR PC SP MP 3191 RR 31 R5 12 17 31 75 12 91 81 91

Operators Operators remove stack arguments and put the result back on the stack Binary operators Unary operators ADD AND EQ NOT SUB OR NE NEG MUL XOR LT DIV MOD GT LE GE 9-14

93 Translating programs 9-15

Arithmetic expressions Expression 3+4*7+2 Code LDC 3 LDC 4 LDC 7 MUL ADD LDC 2 ADD 9-16

Arithmetic expression example LDC 3 LDC 4 LDC 7 MUL ADD LDC 2 ADD PC SP MP RR 31 33 4 28 7 9-17

Representing code in Haskell type Code = [Instr] data Instr = LDC Int LDL Int ADD NEG EQ codesize :: Code Int codesize = sum map instrsize instrsize :: Instr Int instrsize (LDC n) = 2 instrsize ADD = 1 9-18

Translating expressions data Expr = Num Int Add Expr Expr Mul Expr Expr Neg Expr code :: Expr Code code (Num n) = [LDC n] code (Add e 1 e 2 ) = code e 1 + code e 2 + [ADD] code (Mul e 1 e 2 ) = code e 1 + code e 2 + [MUL] code (Neg e) = code e + [NEG] We can also write this as a fold 9-19

Translating conditional expressions data Expr = If Expr Expr Expr code :: Expr Code code (If c t f) = cc + [BRF (st + 2)] + ct + [BRA sf] + cf where cc = code c ct = code t cf = code f st = codesize ct sf = codesize cf 9-20

Translating conditional expressions contd cc BRF (st + 2) ct st BRA sf 2 cf sf 9-21

Algebra for code generation data Expr = Num Int Add Expr Expr Neg Expr Eq Expr Expr If Expr Expr Expr 9-22 Var String Let String Expr Expr code x = foldexpr codealg x empty where codealg :: ExprAlg (Env Code) codealg = (λn λe [LDC n], λl r λe l e + r e + [ADD], λl λe l e + [NEG], λl r λe l e + r e + [EQ], λc t f λe let st = codesize (t e) sf = codesize (f e) in c e + [BRF (st + 2)] + t e + [BRA sf] + f e, λs λe [LDL??(e! s)], λs d b λe d e + [STL (size e)] + b (insert s (size e) e) )

Expressions vs statements We extend our language with statements: data Stmt = Assign String Expr If Expr Stmt Stmt While Expr Stmt Call String [Expr] For many languages, the following invariants hold: 9-23 Expressions always leave a single result on the stack after evaluation Statements do not leave a result on the stack after evaluation

Translating while loops data Stmt = While Expr Stmt code :: Stmt Code code (While c b) = cc + [BRF (sb + 2)] + cb + [BRA ( (sb + sc + 4))] where cc = code c cb = code b sc = codesize cc sb = codesize cb 9-24

Translating while loops contd BRA sb 2 cc sc cb sb BRF (sb + 2) 2 cb sb cc sc BRA ( (sb + sc + 4)) 2 BRT ( (sb + sc + 2)) 2 9-25

Translating while loops contd data Stmt = While Expr Stmt code :: Stmt Code code (While c b) = [BRA sb] + cb + cc + [BRT ( (sb + sc + 2))] where cc = code c cb = code b sc = codesize cc sb = codesize cb 9-26

Algebra for code generation data Stmt = Assign String Expr If Expr Stmt Stmt 9-27 While Expr Stmt Call String [Expr] code x = foldse codealg x empty where codealg :: SEAlg (Env Code) (Env Code) codealg = (λs d e d e + [STL (e! s)], λc t f e let st = codesize (t e) sf = codesize (f e) in c e + [BRF (st + 2)] + t e + [BRA sf] + f e, λc b e let sc = codesize (c e) sb = codesize (b e) in [BRA sb] + b e + c e + [BRT ( (sb + sc + 2))], λm ps e concat [p e p ps] + [BSR m], -- components for Expr )

94 Functions / methods 9-28

Methods with parameters m LDC 7 LDC 12 BSR m LDS -2 LDC 37 ADD BSR p LDS -2 LDS -2 MUL BSR q STS -2 AJS -1 RET PC SP MP RR m(7, 12); void m (int x, int y) { p (x + 37); q (x * y); } 7 12 744 84 37 12 9-29

Method translation Method call Put parameters on the stack Call BSR with the method label Method definition 9-30 Use parameters: from LDS (n + d) to LDS (1 + d), where n is the number of parameters and d is your current offset (this becomes easier with the mark pointer) Clean up: STS n followed by AJS (n 1) Return: RET It is also possible, but less common, to let the caller clean up after a method call

Methods with local variables 9-31 m LDC 7 LDC 12 BSR m LDR MP LDRR MP SP AJS +2 LDL -3 NEG STL +1 LDRR SP MP STR MP STS -2 AJS -1 RET PC SP MP RR m(7, 12); void m (int x, int y) { int a, b; a = -x; } 7 12-7 -7 a b

Method translation with local variables Method call as before Method definition (n parameters, k local variables) 9-32 Create room for local variables: LDR MP to save the mark pointer, LDRR MP SP to reset the mark pointer, AJS +k to adjust the stack pointer (Also available as a single instruction LINK k) Use parameters: from LDL (n + 1) to LDL 2 Use local variables: from LDL +1 to LDL +k Clean up local variables: LDRR SP MP to reset the stack pointer, and STR MP to restore the mark pointer (Also available as a single instruction UNLINK) Clean up: STS n followed by AJS (n 1) Return: RET

Methods with return values Two options Result on stack Leave the result as the final value on the stack Adapt the cleanup code so that this works Result in register Place the result of a method call in a fixed free register (RR for example) Use the value from there at the call site 9-33