MiniJava IL. Winter /17/ Hal Perkins & UW CSE I3-1

Seotud dokumendid
Funktsionaalne Programmeerimine

(loeng3-ohtlikud_koodiloigud)

SQL

Microsoft Word - EVS_ISO_IEC_27001;2014_et_esilehed.doc

HAJUSSÜSTEEMID HAJUSSÜSTEEMID Kaugprotseduurid IDL kompileerimine ONCIDLnäide CORBAIDLnäide MIDLnäide DCERPCmuidomadusi XML-RPC JSON-RPC REST document

SQL

Scala ülevaade 1 Meetodid, muutujad ja väärtused. Süntaks 2 Lihtsad tüübid ja väärtused. 3 OOP, case-klassid ja mustrisobitus. 4 Puhta Scala väärtusta

Pealkiri

I Generaatori mõiste (Java) 1. Variantide läbivaatamine Generaator (ehk generaator-klass) on klass, milles leidub (vähemalt) isendimeetod next(). Kons

(Tõrked ja töökindlus \(2\))

Slide 1

REQUEST FOR AN ASSIGNMENT OF LEI (fond) LEI KOODI MÄÄRAMISE TAOTLUS (fond) 1. FUND DATA / FONDI ANDMED: Legal Name / Ametlik nimi: Other Fund Names /

Ppt [Read-Only]

EESTI STANDARD EVS-EN :2000 This document is a preview generated by EVS Terastraat ja traattooted piirete valmistamiseks. Osa 4: Terastraadist

EESTI STANDARD EVS-EN 1790:1999 This document is a preview generated by EVS Teemärgistusmaterjalid. Kasutusvalmid teekattemärgised Road marking materi

SAF 7 demo paigaldus. 1.Eeldused SAF 7 demo vajab 32- või 64-bitist Windows 7, Window 8, Windows 10, Windows Server 2008 R2, Windows Server 2012, Wind

loeng2

E-õppe ajalugu

KUULA & KORDA INGLISE KEEL 1

Võrguinverterite valik ja kasutusala päikeseelektrijaamades Robert Mägi insener

Microsoft Word - Loppukilpailu2015_16_tehtavat_viro_1

Share wave 5: 50+ in Europe - Coverscreenwizard version IF SKIP_INTROPAGE = 0 CoverscreenWizardIntroPage Järgnev sissejuhatus sisaldab üldküsimu

PowerPoint Presentation

Funktsionaalne Programmeerimine

untitled

Sissejuhatus GRADE metoodikasse

Kuidas hoida tervist töökohal?

Microsoft Word - EVS_ISO_IEC_10646;2012_esilehed.doc

Algoritmid ja andmestruktuurid

Inglise keele ainekava 9.klassile Kuu Õpitulemused Õppesisu Kohustuslik hindamine September 1. Kasutab Present Simple, Present Mina ja teised. Inimese

Microsoft Word - 23jaan07.doc

rp_ IS_3

Avatud ja läbipaistev e-riik: Ees6 kui rajaleidja Andrus Kaarelson RIA peadirektori asetäitja riigi infosüsteemi alal 10. oktoober 2017

AWK Aho Weinberger Kernighan struktuurse teksti töötlemise keel rikkalikult tekstitöötlusvahendeid omal alal suhteliselt lihtne ja kiiresti realiseeri

Süsteemide modelleerimine: praktikum Klassiskeemid Oleg Mürk

Pealkiri

OPERATSIOONISÜSTEEMIDE EHITUS Ülevaade Protsessi mõiste Protsesside planeerimine Operatsioonid protsessidega Protsessihaldus Protsesside koostöö Prots

Rahvusvaheline motokross Baltimere Karikas 2015 Soome Eesti Läti Leedu Kooskõlastanud: EMF-i alajuht; Kinnitanud: EMF peasekretär 1. Aeg ja koht: 18.0

INVESTMENT FRIENDS CAPITAL SE MINUTES OF THE EXTRAORDINARY GENERAL MEETING OF SHAREHOLDERS Place of holding the meeting: Plock, ul. Padlewskiego 18C,

PHP

PowerPoint Presentation

Andmed arvuti mälus Bitid ja baidid

Language technology resources for Estonian text summarization Kaili Müürisep University of Tartu Institute of Computer Science

Microsoft PowerPoint _04_20_Teadusest_ATI_tudengitele.pptx

Microsoft Word - essee_CVE ___KASVANDIK_MARKKO.docx

Inglise keele ainekava 5.klassile Kuu Õpitulemused Õppesisu Kohustuslik hindamine September 1. Räägib loomadest. Vaba aeg. Animals (Wild life 2. Kuula

Pöördumine Eesti Valitususe ja Europa Nõukogu Inimõiguste Komissari hr. Hammarbergi poole Appeal to the Government of the Republic of Estonia and the

Tallinna Ülikool Digitehnoloogiate instituut JavaFX rakenduste kujundamine CSS abil Seminaritöö Autor: Hendrik Spiegelberg Juhendaja: Jaagup Kippar Au

Microsoft PowerPoint - TÜ TVT - Kavandamine ja arhitektuur 2.ppt

Operatsioonisüsteemi ülesanded

Microsoft Word - Referaat.docx

Tartu Ülikool Loodus- ja täppisteaduste valdkond Arvutiteaduse instituut Sandra Puusepp CVE Referaat Tartu 2018

PowerPointi esitlus

Infix Operaatorid I Infix operaatorid (näiteks +) ja tüübid (näiteks ->) kirjutatakse argumentide vahele, mitte argumentide ette. Näiteks: 5 + 2, 2*pi

Scanned Image

Andmeturve

Kommunikatsiooniteenuste arendus (IRT0080) loeng 8/2014 Võrkude haldus Avo 1

Failiotsing: find paljude võimalustega otsingukäsk find kataloog tingimused kataloog - otsitakse sellest kataloogist ja tema alamkataloogidest tingimu

Tiitelleht

Järelevalvetasutegurite kogumise eesmärgil kasutatavate varade koguväärtuse ja koguriskipositsiooni vormide täitmise juhised

Programmeerimiskeel APL Raivo Laanemets 17. mai a.

Microsoft PowerPoint IntroRiskAnal.ppt

Piima ja tooraine pakkumise tulevik kogu maailmas Erilise fookusega rasvadel ja proteiinidel Christophe Lafougere, CEO Gira Rakvere, 3rd of October 20

Programmi AnimatorDV Simple+ lühike kasutajajuhend

Tiia Salm 2011 Online kirjastus CALAMÉO Calameo kujutab endast on-line kirjastust, mis võimaldab oma dokumente avaldada e-raamatuna tasuta. Failid (Pd

Kerala State Haj Committee HAJ 2014 Flight Manifest FLIGHT NO. SV-5731 DATE: 14/09/2014 TIME: 16:35 RETURN FLIGHT NO: SV-5764 DATE: Cov No

Microsoft Word - polkaudio 2010 hinnakiri

EESTI STANDARD EVS-ISO/IEC :2005 This document is a preview generated by EVS INFOTEHNOLOOGIA Avatud süsteemide vastastikune ühendamine Tehingut

Baltic Retail Forum 2019 Baltic Retail Forum on konverents jaekaubanduse juhtidele. Arutleme uueneva tehnoloogia arengusuundade üle, analüüsime

EESTI STANDARD EVS-ISO/IEC/IEEE 15289:2013 This document is a preview generated by EVS SÜSTEEMI- JA TARKVARATEHNIKA Elutsükli infosaaduste (dokumentat

Väljavõte:

CSE 401 Compilers MiniJava IL Hal Perkins Winter 2009 2/17/2009 2002-09 Hal Perkins & UW CSE I3-1

Compile-time Processing Decide representation of run-time data values Decide where data will be stored registers s format of stack frames global memory format of in-memory data structures (e.g. records, arrays) Generate machine code to do basic operations just like interpreting expression, except generate code that will evaluate it later Do optimizations across instructions if desired CSE401 Wi09 2

Compilation Plan Translate ASTs into linear sequence of simple statements called intermediate code (IL or IR) Source-language, target-language independent Intermediate code generation focuses on simple representations of source constructs Target code generation focuses on constraints of particular target machines Different front ends and back ends can share IL Translate IL into target code IL can be optimized CSE401 Wi09 independently of each 3

Our Plan Look first at MiniJava s IL and code gen Next phase of project Then look at runtime representation of code/data on target machine(s) (x86 mainly) Survey of MiniJava s backend More general view of instruction selection/ register allocation/etc. Then code optimization and other topics as we have time 2/17/2009 2002-09 Hal Perkins & UW CSE I3-4

MiniJava s Intermediate Language Want intermediate language to have simple, explicit operations (humans don t write IL programs) Use simple declaration primitives global functions, global variables no classes, no implicit method lookup, no nesting Use simple data types ints, doubles, explicit i pointers, records, arrays no booleans no class types, no implicit class fields arrays are naked sequences; no implicit it length or bounds checks Use explicit gotos instead of control structures Make all implicit checks explicit (e.g. array bounds checks) Implement method lookup via explicit data structures and code CSE401 Wi09 5

MiniJava s IL (1) Program ::= {GlobalVarDecl {FunDecl GlobalVarDecl ::= Type ID [= Value] ; Type ::= int double *Type Type [] { {Type ID/, fun Value ::= Int Double &ID [ {Value/, ] { {ID = Value/, FunDecl ::= Type ID ( {Type ID/,) ) { {VarDecl {Stmt VarDecl ::= Type ID ; Stmt ::= Expr ; LHSExpr = Expr ; iffalse Expr goto Label ; iftrue Expr goto Label ; goto Label ; label Label ; throw new Exception( String ) ; return Expr ; CSE401 Wi09 6

MiniJava s IL (2) Expr ::= LHSExpr Unop Expr Expr Binop Expr Callee ( {Expr/,, ) new Type [ [ Expr ] ] Int Double & ID LHSExpr ::= ID * Expr Expr -> ID [ [ Expr ] ] Unop ::= -.int -.double not int2double Binop ::= (+ - * /).(int double) /).(int double) (< <= >= > ==!=).(int double) <.unsigned Callee ::= ID ( * Expr ) String CSE401 Wi09 7

MiniJava s IL Classes (1 of 6) ILProgram: {ILGlobalVarDecl {ILFunDecl ILGlobalVarDecl: ILType String IlInitializedGlobalVarDecl: ILValue ILType ILIntType ILDoubleType ILPtrType: ILType ILSequenceType: ILType ILRecordType: {ILType String ILCodeType CSE401 Wi09 8

MiniJava s IL Classes (2 of 6) ILValue ILIntValue: int ILDoubleValue: ue: double ILGlobalAddressValue: ILGlobalVar ILLabelAddressValue: ILLabel ILSequenceValue: {ILValue ILRecordValue: {ILValue String ILFunDecl: ILType String {ILFormalVarDecl {ILVarDecl {ILStmt ILVarDecl: ILType String ILFormalVarDecl CSE401 Wi09 9

MiniJava s IL Classes (3 of 6) ILStmt ILExprStmt: ILExpr ILAssignStmt: ILAssignableExpr ILConditionalBranchStmt: ILExpr ILLabel ILConditionalBranchFalseStmt ILConditionalBranchTrueStmt ILGotoStmt: ILLabel ILLabelStmt: ILLabel ILThrowExceptionStmt: String ILReturnStmt: ILExpr ILLabel: String ILGlobalVar: String CSE401 Wi09 10

MiniJava s IL Classes (4 of 6) ILVar: ILVarDecl ILExpr ILAssignableExpr ILVarExpr: ILVar ILPtrAccessExpr: ILExpr ILFieldAccessExpr: ILExpr ILType String ILSequenceFieldAccessExpr: ILExpr ILUnopExpr: ILExpr IL{Int,DoubleNegativeExpr, ILLOgicalNegateExpr, ILIntToDoubleExpr CSE401 Wi09 11

MiniJava s IL Classes (5 of 6) ILBinopExpr: ILExpr ILExpr IL{Int,Double{Add,Sub,Mul,Div, Equal,NotEqual, q, LessThan,LessThanOrEqual GreaterThanOrEqual, GreaterThanExpr, ILUnsignedLessThanExpr ILAllocateExpr: ILType ILAllocateSequenceExpr: ILExpr ILIntConstantExpr: int ILDoubleConstantExpr: double ILGlobalAddressExpr: ILGlobalVar CSE401 Wi09 12

MiniJava s IL Classes (6 of 6) ILGlobalAddressExpr: ILGlobalVar ILFunCallExpr: ILType {ILExpr ILDirectFunCallExpr: String ILIndirectFunCallExpr: ILExpr ILRuntimeCallExpr: String CSE401 Wi09 13

Intermediate Code Generation Choose representations for source-level data types translate each ResolvedType into ILType(s) Recursively traverse ASTs (lower operation) creating corresponding IL pgm parallels typechecking and evaluation traversals Expr ASTs create ILExpr ASTs Stmt ASTs create ILStmt ASTs MethodDecl ASTs create ILFunDecl ASTs ClassDecl ASTs create ILGlobalVarDecl ASTs CSE401 Wi09 14

Data Type Representation (1) What IL type to use for each source type? what operations are we going to need on them? int, boolean, double? CSE401 Au08 15

Data Type Representations (2) What IL type to use for each source type? what operations are we going to need on them? class B { int i; D j; Instance of Class B? CSE401 Au08 16

Inheritance How to lay out subclasses Subclass inherits from superclass Subclass can be assigned to a variable of superclass type implying subclass layout must match superclass layout class B { int i; D j; class C extends B { int x; F y; instance of class C CSE401 Au08 17

Methods How to translate a method? Use a function name is "mangled": nameofclass+nameofmethod method make this an explicit argument Example class B {... int m(int i, double d) {... body... B s method m translates to int B_m(*{...B... this, int i, double d) {... translation of body... CSE401 Au08 18

Implementing Method Invocation class B {... int m(...) {... E n(...) {... class C extends B {... int m(...) {... // override F p(...) {... B b1=new(b) C c2=new(c) B b2=c2 b1.m(...) b1.n(...) c2.m(...) c2.n(...) c2.p(...) b2.m(...) b2.n(...) CSE401 Au08 19

Methods via Function Pointers in Instances Store code pointer for each new method in each instance Initialize with right method for that name for that object Do instance var lookup to get code pointer to invoke class B { int i; int m(...) {... E n(...) {... class C extends B { int j; int m(...) {... // override F p(...) {... Instance of class B: *(int i, *code m, *code n Instance of class C: *(int i, *code m, *code n, int j, *code p CSE401 Au08 20

Manipulating Method Function Ptrs Example B b1 = new B(); C c2 = new C(); B b2 = c2; b1.m(3, 4.5); b2.m(3, 4.5); Translation: *.. b1 = alloc {...B... b1->i = 0; b1->m = &B_m; b1->n = &B_n; *.. c2 = alloc {...C...; c2->i = 0; c2->m = &C_ m; c2->n = &B_ n; c2->j = 0; c2->p = &C_p; *.. b2 = c2 (*(b1->m)) (b1, 3, 4.5); (*(b2->m)) (b2, 3, 4.5); CSE401 Au08 21

Shared Method Function Pointers All direct instances of a class store the same method function pointers So, can factor out common values into a single record shared by all -- often called a virtual function table, or vtbl smaller objects, faster object creation slower method invocations B s virtual function table (a global initialized variable): {*code m, *code n B_vtbl = {m=&b_m, m n=&b_n; n; Example: B b1 = new B(); b1.m(3, 4.5); Translation *.. b1 = alloc{int i, *{...B_vtbl... vtbl; b1->i=0; b1->vtbl = &B_vtbl; (*((b1->vtbl)->m))(b1, 3, 4.5); CSE401 Au08 22

Method Inheritance A subclass inherits all the methods of its superclasses: its method record includes all fields of its superclass Virtual function tables of subclass extends that of superclass with new methods, replacing overridden methods class B {int i; int m(...) {... E n(...) {... class C extends B { int j; int m(...) {... // override F p(...) {... {*code m, *code n B_vtbl = {m=&b_m, n=&b_n; {*code m, *code n, *code p C_ vtbl= {m=&c_ m, n=&b_ n, p=&c_p; CSE401 Au08 23

Example Example B b1 = new B(); C c2 = new C(); B b2 = c2; b1.m(3, 4.5); b2.m(3, 4.5); Translation *.. b1 = alloc {int i, *{...B_vtbl... vtbl; b1->i = 0; b1->vtbl = &B_vtbl; *.. c2 = alloc {int i, *{...C_vtbl... vtbl,int j; c2->i = 0; c2->vtbl = &C_vtbl; c2->j = 0; *.. b2 = c2 (*((b1->vtbl)->m)) (b1, 3, 4.5); (*((b2->vtbl)->m)) >m)) (b2, 3, 4.5); CSE401 Au08 24

Main ICG Operations ILProgram Program.lower(); translate the whole program into an ILProgram void ClassDecl.lower(ILProgram); translate method decls declare the class s method record (vtbl) void MethodDecl.lower(ILProgram, ClassSymbolTable); translate into IL fun decl, add to IL program void Stmt.lower(ILFunDecl); translate into IL statement(s), add to IL fun decl ILExpr Expr.evaluate(ILFunDecl); l) translate into IL expr, return it ILType Type.lower(); ILType ResolvedType.lower(); lower(); return corresponding IL type CSE401 Au08 25

An Example ICG Operation class IntLiteralExpr extends Expr { int value; ILExpr lower(ilfundecl fun) { return new ILIntConstantExpr(value); CSE401 Au08 26

An Example ICG Operation class AddExpr extends Expr { Expr arg1; Expr arg2; ILExpr lower(ilfundecl fun) { ILExpr arg1 _ expr = arg1.lower(fun); ILExpr arg2_expr = arg2.lower(fun); return new ILIntAddExpr(arg1_expr, arg2_expr); CSE401 Au08 27

Example Overloaded ICG Operation class EqualExpr extends Expr { Expr arg1; Expr arg2; ILExpr lower(ilfundecl fun) { ILExpr arg1_expr = arg1.lower(fun); ILExpr arg2_expr = arg2.lower(fun); if (arg1.getresulttype().isinttype() && arg2.getresulttype().isinttype()) { return new ILIntEqualExpr(arg1_expr, arg2_expr); else if (arg1.getresulttype().isbooltype() && arg2.getresulttype().isbooltype()) { return new ILBoolEqualExpr(arg1_expr, arg2_expr); else { throw new InternalCompilerError(...); CSE401 Au08 28

An Example ICG Operation class VarDeclStmt extends Stmt { String name; Type type; void lower(ilfundecl fun) { fun.declarelocal(type.lower(), name); declarelocal declares a new local variable in the IL function CSE401 Au08 29

ICG of Variable References class VarExpr extends Expr { String name; VarInterface var_iface; //set during typechecking ILExpr lower(ilfundecl fun) { return var_iface.generateread(fun); class AssignStmt extends Stmt { String lhs; Expr rhs; VarInterface lhs_iface; //set during typechecking void lower(ilfundecl fun) { ILExpr rhs_expr = rhs.lower(fun); lhs_iface.generateassignment(rhs_expr, fun); generateread/generateassignment gen IL code to read/assign the variable code depends on the kind of variable (local vs. instance) CSE401 Au08 30

ICG of Instance Variable Reference class InstanceVarInterface extends VarInterface { ClassSymbolTable class_st; ILExpr generateread(ilfundecl fun) { ILExpr rcvr_expr = new ILVarExpr(fun.lookupVar("this")); ILType class_type = ILType.classILType(class_st); ILRecordMember var_member = class_type.getrecordmember(name); return new ILFieldAccessExpr(rcvr_expr, class_type, var_member); CSE401 Au08 31

ICG of Instance Variable Reference void generateassignment(ilexpr rhs_expr, ILFunDecl fun) { ILExpr rcvr_expr = new ILVarExpr(fun.lookupVar("this")); ILType class_type = ILType.classILType(class_st); ILRecordMember var_member = class_type.getrecordmember(name); ILAssignableExpr lhs = new ILFieldAccessExpr(rcvr_expr, class_type, var_member); fun.addstmt(new ILAssignStmt(lhs, rhs_expr)); CSE401 Au08 32

ICG of if Statements What IL code to generate for an if statement? if (testexpr) thenstmt else elsestmt CSE401 Au08 33

if class IfStmt extends Stmt { Expr test; Stmt then_stmt; Stmt else_stmt; stmt; void lower(ilfundecl fun) { ILExpr test_expr = test.lower(fun); ILLabel false_label = fun.newlabel(); fun.addstmt( new ILCondBranchFalseStmt(test_expr, false_label)); then_stmt.lower(fun); ILLabel done_ label = fun.newlabel(); fun.addstmt(new ILGotoStmt(done_label)); fun.addstmt(new ILLabelStmt(false_label)); else_stmt.lower(fun); fun.addstmt(new ILLabelStmt(done_label)); label)); CSE401 Au08 34

ICG of Print Statements What IL code to generate for a print statement? System.out.println(expr); No IL operations exist that do printing (or any kind of I/O) oops CSE401 Au08 35

Runtime Libraries Can provide some functionality of compiled program in external runtime libraries libraries written in any language, compiled separately libraries can contain functions, data declarations Compiled code includes calls to functions & references to data declared libraries Final application links together compiled code and runtime libraries Often can implement functionality either through h compiled code or through calls to library functions tradeoffs? CSE401 Au08 36

ICG of Print Statements class PrintlnStmt extends Stmt { Expr arg; void lower(ilfundecl fun) { ILExpr arg_expr = arg.lower(fun); ILExpr call_expr = new ILRuntimeCallExpr("println println_int int", arg_expr); fun.addstmt(new ILExprStmt(call_expr)); // print out an integer value void println_int(int value) { What about printing printf("%d\n", value); doubles? CSE401 Au08 37

ICG of new Expressions What IL code to generate for a new expression? class C extends B { inst var decls method decls... new C()... CSE401 Au08 38

ICG of new Expressions class NewExpr extends Expr { String class_name; ILExpr lower(ilfundecl fun) { generate code to: allocate instance record initialize iti vtbl field with class s method record initialize inst vars to default values return reference to allocated record CSE401 Au08 39

An Example ICG Operation class MethodCallExpr extends Expr { String class_name; ILExpr lower(ilfundecl fun) { generate code to: evaluate receiver and arg exprs test whether receiver is null load vtbl member of receiver load called method member of vtbl call fun ptr, passing receiver and args return call expr CSE401 Au08 40

allocate instance record // allocate bytes of memory, and return a pointer to it void* allocate(int bytes) { return malloc(bytes); // allocate base_bytes + num_elems*elems_bytes bytes of cleared // memory, and return a pointer to it void* allocate_sequence(int t base_bytes, b int num_elems, int elems_bytes) { return calloc(base_bytes + num_elems * elems_bytes, 1); // report an exception and die void throw_exception(char* message) { fprintf(stderr, "Unhandled exception: %s\n", message); exit(-1); CSE401 Au08 41

Where We Are So far we have a pretty low-level, but still abstract machine Next: x86 overview; mapping languages like MiniJava etc. to real processors Then: MiniJava s ij code generator More elaborate backend algorithms Fundamentals of compiler optimizations CSE401 Au08 42