1 Code Generation Part I Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,

Similar documents
Microsoft PowerPoint - NxLecture ppt [互換モード]

compiler-text.dvi

Introduction Purpose This training course demonstrates the use of the High-performance Embedded Workshop (HEW), a key tool for developing software for

Introduction Purpose This training course describes the configuration and session features of the High-performance Embedded Workshop (HEW), a key tool

Table 1. Assumed performance of a water electrol ysis plant. Fig. 1. Structure of a proposed power generation system utilizing waste heat from factori


AtCoder Regular Contest 073 Editorial Kohei Morita(yosupo) A: Shiritori if python3 a, b, c = input().split() if a[len(a)-1] == b[0] and b[len(

Introduction Purpose This course explains how to use Mapview, a utility program for the Highperformance Embedded Workshop (HEW) development environmen

3 SIMPLE ver 3.2: SIMPLE (SIxteen-bit MicroProcessor for Laboratory Experiment) 1 16 SIMPLE SIMPLE 2 SIMPLE 2.1 SIMPLE (main memo

2

1 # include < stdio.h> 2 # include < string.h> 3 4 int main (){ 5 char str [222]; 6 scanf ("%s", str ); 7 int n= strlen ( str ); 8 for ( int i=n -2; i

JOURNAL OF THE JAPANESE ASSOCIATION FOR PETROLEUM TECHNOLOGY VOL. 66, NO. 6 (Nov., 2001) (Received August 10, 2001; accepted November 9, 2001) Alterna

Vol.54 No (July 2013) [9] [10] [11] [12], [13] 1 Fig. 1 Flowchart of the proposed system. c 2013 Information

はじめに


25 II :30 16:00 (1),. Do not open this problem booklet until the start of the examination is announced. (2) 3.. Answer the following 3 proble

I117 II I117 PROGRAMMING PRACTICE II SOFTWARE DEVELOPMENT ENV. 1 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara

137. Tenancy specific information (a) Amount of deposit paid. (insert amount of deposit paid; in the case of a joint tenancy it should be the total am

RX600 & RX200シリーズ アプリケーションノート RX用仮想EEPROM

Motivation and Purpose There is no definition about whether seatbelt anchorage should be fixed or not. We tested the same test conditions except for t

2 122

Microsoft Word - Win-Outlook.docx


elemmay09.pub

Q E Q T a k Q Q Q T Q =

& Vol.5 No (Oct. 2015) TV 1,2,a) , Augmented TV TV AR Augmented Reality 3DCG TV Estimation of TV Screen Position and Ro

ScanFront300/300P セットアップガイド

Vol. 42 No MUC-6 6) 90% 2) MUC-6 MET-1 7),8) 7 90% 1 MUC IREX-NE 9) 10),11) 1) MUCMET 12) IREX-NE 13) ARPA 1987 MUC 1992 TREC IREX-N

ストリーミング SIMD 拡張命令2 (SSE2) を使用した、倍精度浮動小数点ベクトルの最大/最小要素とそのインデックスの検出

2

How to read the marks and remarks used in this parts book. Section 1 : Explanation of Code Use In MRK Column OO : Interchangeable between the new part

Vol. 48 No. 4 Apr LAN TCP/IP LAN TCP/IP 1 PC TCP/IP 1 PC User-mode Linux 12 Development of a System to Visualize Computer Network Behavior for L

Microsoft PowerPoint - Lecture ppt [互換モード]

0

How to read the marks and remarks used in this parts book. Section 1 : Explanation of Code Use In MRK Column OO : Interchangeable between the new part

@08470030ヨコ/篠塚・窪田 221号


エレクトーンのお客様向けiPhone/iPad接続マニュアル


ScanFront 220/220P 取扱説明書

ScanFront 220/220P セットアップガイド

2

How to read the marks and remarks used in this parts book. Section 1 : Explanation of Code Use In MRK Column OO : Interchangeable between the new part

On the Wireless Beam of Short Electric Waves. (VII) (A New Electric Wave Projector.) By S. UDA, Member (Tohoku Imperial University.) Abstract. A new e

Tab 5, 11 Tab 4, 10, Tab 3, 9, 15Tab 2, 8, 14 Tab 1, 7, 13 2

インターネット接続ガイド v110

PBASIC 2.5 PBASIC 2.5 $PBASIC directive PIN type New DEBUG control characters DEBUGIN Line continuation for comma-delimited lists IF THEN ELSE * SELEC

23 Fig. 2: hwmodulev2 3. Reconfigurable HPC 3.1 hw/sw hw/sw hw/sw FPGA PC FPGA PC FPGA HPC FPGA FPGA hw/sw hw/sw hw- Module FPGA hwmodule hw/sw FPGA h


2

Sport and the Media: The Close Relationship between Sport and Broadcasting SUDO, Haruo1) Abstract This report tries to demonstrate the relationship be

WARNING To reduce the risk of fire or electric shock,do not expose this apparatus to rain or moisture. To avoid electrical shock, do not open the cabi

ID 3) 9 4) 5) ID 2 ID 2 ID 2 Bluetooth ID 2 SRCid1 DSTid2 2 id1 id2 ID SRC DST SRC 2 2 ID 2 2 QR 6) 8) 6) QR QR QR QR

1 2 3

00.\...ec5

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1


MIDI_IO.book

untitled

_07.indd

Chapter

2009 No

main.dvi

The 15th Game Programming Workshop 2010 Magic Bitboard Magic Bitboard Bitboard Magic Bitboard Bitboard Magic Bitboard Magic Bitboard Magic Bitbo

基本操作ガイド

VQT3B86-4 DMP-HV200 DMP-HV150 μ μ l μ

Huawei G6-L22 QSG-V100R001_02



7,, i

取説_VE-PV11L(応用編)

基本操作ガイド


Bull. of Nippon Sport Sci. Univ. 47 (1) Devising musical expression in teaching methods for elementary music An attempt at shared teaching


iPhone/iPad接続マニュアル

03-03 Bush Mentori.pdf

2

Webサービス本格活用のための設計ポイント

Fig. 1 Schematic construction of a PWS vehicle Fig. 2 Main power circuit of an inverter system for two motors drive

操作ガイド(本体操作編)


学部ゼミ新規申請方法 (Blackboard 9.1) Seminar Application Method for Undergraduate Seminar Courses ゼミ新規申請は Blackboard で受け付けます! 次セメスターにゼミ履修を希望する学生は 下記マニュアルに従ってゼミ

Transcription:

1 Code Generation Part I Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2013

2 Position of a Code Generator in the Compiler Model Source program Front-End Intermediate code Code Optimizer Intermediate code Code Generator Target program Lexical error Syntax error Semantic error Symbol Table

3 Code Generation Code produced by compiler must be correct Source-to-target program transformation should be semantics preserving Code produced by compiler should be of high quality Effective use of target machine resources Heuristic techniques should be used to generate good but suboptimal code, because generating optimal code is undecidable

4 Target Program Code The back-end code generator of a compiler may generate different forms of code, depending on the requirements: Absolute machine code (executable code) Relocatable machine code (object files for linker) Assembly language (facilitates debugging) Byte code forms for interpreters (e.g. JVM)

5 The Target Machine Implementing code generation requires thorough understanding of the target machine architecture and its instruction set Our (hypothetical) machine: Byte-addressable (word = 4 bytes) Has n general purpose registers R0, R1,, Rn-1 Two-address instructions of the form op source, destination

6 The Target Machine: Op-codes and Address Modes Op-codes (op), for example MOV (move content of source to destination) ADD (add content of source to destination) SUB (subtract content of source from dest.) Address modes Mode Form Address Added Cost Absolute M M 1 Register R R 0 Indexed c(r) c+contents(r) 1 Indirect register *R contents(r) 0 Indirect indexed *c(r) contents(c+contents(r)) 1 Literal #c N/A 1

7 Instruction Costs Machine is a simple, non-super-scalar processor with fixed instruction costs Realistic machines have deep pipelines, I-cache, D-cache, etc. Define the cost of instruction = 1 + cost(source-mode) + cost(destination-mode)

8 Examples Instruction Operation Cost MOV R0,R1 Store content(r0) into register R1 1 MOV R0,M Store content(r0) into memory location M 2 MOV M,R0 Store content(m) into register R0 2 MOV 4(R0),M Store contents(4+contents(r0)) into M 3 MOV *4(R0),M Store contents(contents(4+contents(r0))) into M 3 MOV #1,R0 Store 1 into R0 2 ADD 4(R0),*12(R1) Add contents(4+contents(r0)) to value at location contents(12+contents(r1)) 3

9 Instruction Selection Instruction selection is important to obtain efficient code Suppose we translate three-address code x:=y+z to: MOV y,r0 ADD z,r0 MOV R0,x Better a:=a+1 Best MOV a,r0 ADD #1,R0 MOV R0,a Cost = 6 ADD #1,a INC a Cost = 3 Cost = 2

10 Instruction Selection: Utilizing Addressing Modes Suppose we translate a:=b+c into MOV b,r0 ADD c,r0 MOV R0,a Assuming addresses of a, b, and c are stored in R0, R1, and R2 MOV *R1,*R0 ADD *R2,*R0 Assuming R1 and R2 contain values of b and c ADD R2,R1 MOV R1,a

11 Need for Global Machine- Specific Code Optimizations Suppose we translate three-address code x:=y+z to: MOV y,r0 ADD z,r0 MOV R0,x Then, we translate a:=b+c d:=a+e to: MOV a,r0 ADD b,r0 MOV R0,a MOV a,r0 ADD e,r0 MOV R0,d Redundant

12 Register Allocation and Assignment Efficient utilization of the limited set of registers is important to generate good code Registers are assigned by Register allocation to select the set of variables that will reside in registers at a point in the code Register assignment to pick the specific register that a variable will reside in Finding an optimal register assignment in general is NP-complete

13 Example t:=a*b t:=t+a t:=t/d t:=a*b t:=t+a t:=t/d { R1=t } { R0=a, R1=t } MOV a,r1 MUL b,r1 ADD a,r1 DIV d,r1 MOV R1,t MOV a,r0 MOV R0,R1 MUL b,r1 ADD R0,R1 DIV d,r1 MOV R1,t

14 Choice of Evaluation Order When instructions are independent, their evaluation order can be changed a+b-(c+d)*e reorder t1:=a+b t2:=c+d t3:=e*t2 t4:=t1-t3 t2:=c+d t3:=e*t2 t1:=a+b t4:=t1-t3 MOV a,r0 ADD b,r0 MOV R0,t1 MOV c,r1 ADD d,r1 MOV e,r0 MUL R1,R0 MOV t1,r1 SUB R0,R1 MOV R1,t4 MOV c,r0 ADD d,r0 MOV e,r1 MUL R0,R1 MOV a,r0 ADD b,r0 SUB R1,R0 MOV R0,t4

15 Generating Code for Stack Allocation of Activation Records t1 := a + b param t1 param c t2 := call foo,2 func foo return t1 100: ADD #16,SP 108: MOV a,r0 116: ADD b,r0 124: MOV R0,4(SP) 132: MOV c,8(sp) 140: MOV #156,*SP 148: GOTO 500 156: MOV 12(SP),R0 164: SUB #16,SP 172: Push frame Store a+b Store c Store return address Jump to foo Get return value Remove frame 500: 564: MOV R0,12(SP) Store return value 572: GOTO *SP Return to caller Note: Language and machine dependent Here we assume C-like implementation with SP and no FP