24
Cse322, Programming Languages and Compilers 1 03/27/22 Lecture #5, April 17, 2007 Array Access Case stmt Jump tables Procedure call Machine dependent strategy OO-langauages

Cse322, Programming Languages and Compilers 1 6/21/2015 Lecture #5, April 17, 2007 Array Access Case stmt Jump tables Procedure call Machine dependent

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Cse322, Programming Languages and Compilers

104/18/23

Lecture #5, April 17, 2007•Array Access•Case stmt•Jump tables•Procedure call•Machine dependent strategy•OO-langauages

Cse322, Programming Languages and Compilers

204/18/23

Assignments

• Reading– Read chapter 8 sections 8.1 – 8.3

– Possible Quiz Wednesday on the reading.

• Programming assignment #3 is now available on the class website under the assignments link.

Cse322, Programming Languages and Compilers

304/18/23

Array Access

• Array access is a lot like variable access in a block structured or OO-language

• We need to compute an address and an offset

Y[0] = 6

Y[1] = 8

Y[2] = 12

X = 23

rA

x= 1

y= 3

loadI @x => r1

loadAO rA,r1 => r2

@x = x = 1

Cse322, Programming Languages and Compilers

404/18/23

Array Access #2

• The instruction LoadAO takes a base address and an offset.

• In an array the offset includes the index of the array expression, as well as the delta from the rA

Y[0] = 6

Y[1] = 8

Y[2] = 12

X = 23

rA

x= 1

y= 3loadI @y => r1

... => rexp

Add r1,rexp => roff

loadAO rA,roff => r2

y[exp]

Cse322, Programming Languages and Compilers

504/18/23

Alternate approach

• Another way is to approach this is to include both the rA and the y into the base and just use the index as the offset.

• Compare

loadI @y => r1

... => rexp

Add r1,rexp => roff

loadAO rA,roff => r2

loadI @y => r1

Add rA,r1 => rbase

... => rexp

loadAO rbase,rexp => r2

Cse322, Programming Languages and Compilers

604/18/23

ML program

• To add this to our ML program we can proceed either way.

• We need to handle element size and 1 based indexing as well.

• Consider y[2*x]

loadI 2 => r1 loadI @x => r2loadAO rA,r2 => r3Mul r1,r3 => r4 ! 2*xloadI @y => r5loadI 1 => r7Sub r4,r7 => r4 ! 2*x – 1 handle 1-basedloadI 4 => r6Mul r4,r6 => r4 ! (2*x – 1) * 4 handle sizeAdd r5,r4 => r4 ! Add the offset of yloadAO rA,r4 => r8 ! Access the correct word

Cse322, Programming Languages and Compilers

704/18/23

fun expr dict node =case node ofArrayElm(array as(Var(NONE,y)),index,SOME Type) => let val size = (case Type of Bool => 1 | Int => 4 | Real => 8) val nbased = 1 val rindex = expr dict index val t1 = base array val t2 = offset array val rsize = NextRegister() val rbase = NextRegister() val result = NextRegister() in emit (LoadI(Int.toString nbased,rbase)); emit (Arith(SUB,rindex,rbase,rindex)); emit (LoadI(Int.toString size,rsize)); emit (Arith(MUL,rindex,rsize,rindex)); emit (Arith(ADD,t2,rindex,rindex)); emit (LoadAO(t1,rindex,result)); result end

Cse322, Programming Languages and Compilers

804/18/23

Case stmt• Consider a case statement over integers

case exp of 0 => exp0| 1 => exp1| 2 => exp2| 3 => exp3

• How can we efficiently translate this?– Nested if-then-else– Binary search– Jump table

Compact set of contiguous integers. Like

Tags in a datatype

Cse322, Programming Languages and Compilers

904/18/23

Jump Table

JumpI Top

Table: JumpI L0

JumpI L1

JumpI L2

JumpI L3

Top: . . . => rexp

LoadI Table => r0

Add r0,rexp => rtarget

JumpR rtarget

LO:

L1:

Cse322, Programming Languages and Compilers

1004/18/23

Add two new IR instructions

datatype IR

= LoadI of (string * Reg)

| LoadAO of (Reg * Reg * Reg)

| Arith of (Op * Reg * Reg * Reg)

| JumpR of Reg

| Move of Reg *Reg

Cse322, Programming Languages and Compilers

1104/18/23

fun caseExp dict exp arms = let val ns = map fst arms val [Table,Top,Bottom] = NextLabel 3 fun target (n,expr) = (n,hd (NextLabel 1),expr) val labels = map target arms fun emitJump (0,lab,exp) = emitAt Table (JumpI lab) | emitJump (_,lab,exp) = emit (JumpI lab) val result = NextRegister() val index = NextRegister () fun emitArm (_,lab,exp) = let val _ = emitAt lab Nop val r = expr dict exp in emit (Move(r,result)); emit (JumpI Bottom) end val _ = emit (JumpI Top) val _ = map emitJump labels val _ = emitAt Top Nop val rexp = expr dict exp in emit (LoadI(“L”^Int.toString Table,index)); emit (Arith(ADD,index,rexp,index)); emit (JumpR index); map emitArm labels; emitAt Bottom Nop; result end

Cse322, Programming Languages and Compilers

1204/18/23

jumpI -> L2L1: jumpI -> L4 jumpI -> L5 jumpI -> L6L2: nop loadI @y => r3 loadAO rA,r3 => r4 loadI L1 => r2 Add r2,r4 => r2 jumpR r2L4: nop loadI @x => r5 loadAO rA,r5 => r6 Move r6 => r1 jumpI -> L3L5: nop loadI 5 => r7 Move r7 => r1 jumpI -> L3L6: nop loadI 2 => r8 Mul r8,r4 => r9 Move r9 => r1 jumpI -> L3L3: nop

case y of

0 => x

| 1 => 5

| 2 => 2 * y

Cse322, Programming Languages and Compilers

1304/18/23

Procedure Call

• AR creation is a cooperative effort

• Shared by the caller and the callee

• Has 4 parts– Precall

– Postreturn

– Prolog

– Epilog

• Precall & Postcall can be split

prolog

precall

postcall

epilog

prolog

epilog

Call

Return

Cse322, Programming Languages and Compilers

1404/18/23

• Setting up parameters– On stack

– In memory

– In registers

• Setting up the call– The address to point to

– The return address

– Special registers like the activation record or object pointer

• Handling returned values

Cse322, Programming Languages and Compilers

1504/18/23

Machine dependencies

• Many of these set up operations will be machine dependent

• To avoid machine dependencies at the IR level we define some abstract IR instructions

• Precall - param• Call - call• PostCall - retval

• For each machine these will be implemented in a different way

Cse322, Programming Languages and Compilers

1604/18/23

Example f(3,z,2*x)loadI 3 => r1loadI @z => r2loadAO rA,r2 => r3loadI 2 => r4loadI @x => r5loadAO rA,r5 => r6Mul r4,r6 => r7param r1param r3param r7loadI @f => r8call r8retval r9

Cse322, Programming Languages and Compilers

1704/18/23

fun expr dict node =case node of Call(_,f,_,args) => let val rargs = map (expr dict) args fun emitParam r = emit (Param r) val rf = NextRegister() val result = NextRegister() in map emitParam rargs; emit (LoadI(f,rf)); emit (Callx rf); emit (Retval result); result end

Cse322, Programming Languages and Compilers

1804/18/23

OO languages

• The code shape for OO languages is different from other languages in several places– Instance variables

– Method calls

• These must use the same code no matter where they are used, since we generate code from its abstract syntax.

• In different instances, objects may different numbers of instance variables and methods.– Uniform way of laying out these things is necessary

• Consider two different ways of laying out objects

Cse322, Programming Languages and Compilers

1904/18/23

class three

fee

fum

class two

fee

foe

class one

fee

fie

Object

Obj a

X=2.0

Y=0.1

Z =

N =1

Obj c

X=5.0

Y=3.1

N =1

Obj b

X=5.0

Y=3.0

Z =

N =1

#1

Cse322, Programming Languages and Compilers

2004/18/23

class three

fee

fum

Object

class two

fee

fum

foe class one

fee

fum

foe

fie

fum

fee

fee

foe

fee

fie

Obj c

X=5.0

Y=3.1

N =1

Obj a

X=2.0

Y=0.1

Z =

N =1

Obj b

X=5.0

Y=3.0

Z =

N =1

#2

Cse322, Programming Languages and Compilers

2104/18/23

Project #1

• Project #1 will be assigned Thursday– It will be due in two weeks time, May 3rd

– There will be no daily assignments next week, so get started as soon as you get the project #1 description

• Project 1 will translate MiniJava through a sequence of IR languages.

• These languages will be different (but also similar) to the IR we have seen in class.

• The first IR is located on the notes web page.

• Also see the Slides from Jenke Li about templates for MiniJava Translation (available on the notes web page).

Cse322, Programming Languages and Compilers

2204/18/23

IR #1datatype BINOP = ADD | SUB | MUL | DIV | AND | ORdatatype RELOP = EQ | NE | LT | LE | GT | GE

datatype EXP= BINOP of BINOP * EXP * EXP| CALL of EXP * EXP list| MEM of EXP| NAME of string| TEMP of int| PARAM of int| VAR of int| CONST of string| STRING of string| ESEQ of STMT * EXP| EXPLIST of EXP list

Cse322, Programming Languages and Compilers

2304/18/23

IR #1 continued

and STMT

= MOVE of EXP * EXP

| JUMP of EXP

| CJUMP of RELOP * EXP * EXP * EXP

| LABEL of EXP

| CALLST of EXP * EXP list

| RETURN of EXP

| STMTlist of STMT list;

datatype FUNC

= FUNC of string * int * int * STMT list

Cse322, Programming Languages and Compilers

2404/18/23

Assignment #3CS322 Prog Lang & Compilers Prog Assignment #3Assigned Wednesday April 12, 2006. Due Monday, April 17, 2006

This assignment is to extend the stmt program discussed in class(and available for download) so that it can translate while loops

fun stmt dict x = case x of (While(tst,body)) =>

• Base your solution on the if-then-else statement and the discussion in the text book about while-loops.

• You don’t need to add any new IR instructions.• Be sure and test your code, print out the tests, and hand them in.