This presentation is the property of its rightful owner.
1 / 19

# CSE 131B – Compiler Construction II PowerPoint PPT Presentation

CSE 131B – Compiler Construction II. Discussion 7: Short-Circuiting, Pointers/Records, and Arrays 2/28/2007. Overview. Phase 2 Phase 3. Short-Circuiting. & and OR are "short-circuiting" operators. In A & B, if A evaluates to FALSE, B is not evaluated.

CSE 131B – Compiler Construction II

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

## CSE 131B – Compiler Construction II

Discussion 7: Short-Circuiting, Pointers/Records, and Arrays

2/28/2007

• Phase 2

• Phase 3

### Short-Circuiting

• & and OR are "short-circuiting" operators.

• In A & B, if A evaluates to FALSE, B is not evaluated.

• In A OR B, if A evaluates to TRUE, B is not evaluated.

### Short-Circuiting

• Think of how you handle an if-else statement.

• Short-circuiting follows the same principle:

• In the A & B case, branch to the false case if A is FALSE. Else, check B.

• In the A OR B case, branch to the true case if A is TRUE. Else, check B.

### Short-Circuiting

! e2 is TRUE, so result TRUE

mov1, %l5

baendlabel

nop

flabel:

mov0, %l5

endlabel:

! Result is now in %l5

• Oberon: e1 & e2:

! Load e1 and check if FALSE

ld[%fp-4], %l0 ! e1’s temp

cmp%l0, %g0

beflabel

nop

! e1 is TRUE, so check e2

ld[%fp-8], %l0 ! e2’s temp

cmp%l0, %g0

beflabel

nop

### Array Allocation Method 1

• When you declare an array, one possible way to allocate space for it is to allocate an entire chunk in BSS and have the variable label at the end of it:

VAR X : ARRAY 7 OF INTEGER;

.section“.bss”

.align4

.skip28! 7 * sizeof(int)

X:

Now X[0] is at X-4, X[1] is at X-8, and so on.

### Array Allocation Method 2

• Similarly, if you are taking the single “globals” label approach, you just need to .skip/.space the total size of the array structure.

• A useful attribute to have for Arrays and Records is “size”, so you know how much space to allocation.

• Offsets would also be useful. For arrays, the offsets are simply multiples of the element’s size. For records, the offsets are the collective sizes of the preceding fields.

### Array Usage (Simplified)

• a := X[b] + 7;! X is array of INTEGER

setX, %l1! X  base address

setb, %l0! b

sll%l0, 2, %l0! (b++) * 4  offset

add%l1, %l0, %l0! Base + offset

ld[%l0], %l0! X[b]’s value

add%l0, 7, %l0! X[b] + 7

seta, %l1

st%l0, [%l1]! a := X[b] + 7

### Record Usage

• Very similar to array usage.

• You need to start at the base address of where the record is located.

• Then, you have to move some offset to get to a specific field you are interested in.

• Once at that location, you either load or store, depending on what you wanted to do.

### Passing Arrays

• Arrays must be passed by reference (VAR), where the address to the address is sent to the procedure.

PROCEDURE baz (VAR a : arrType)

baz(myArr);

• In the above case, you would store the base address + offset of the first element in %o0. Once in baz, %i0 will have the address of the first element. All other elements will be accessed by some offset from that first element address.

### Passing Records

• Since Records always have a pointer to them, Records can be passed by reference (VAR) or value.

FUNCTION bar1 (VAR r : recPtrType)

FUNCTION bar2 (r : recPtrType)

bar1(myRecPtr);

bar2(myRecPtr);

• The behavior is essentially the same, but when using VAR, there is a double dereference (one for the VAR, one for the record’s . operator)

### Value versus VAR

• If you have any doubts about value vs. VAR parameters or parameter passing, please look at the following URL:

• http://www.cse.ucsd.edu/classes/wi07/cse131b/VARvsValue.pdf

### Pointers

• Quick note: Pointers always have a size of 4 bytes. Pointers will point to another address, which can be of a different size (the size of the record object once dynamically created). So, the value of a pointer variable is an address, and that address is the base of the object being pointed to.

### Pointers (to Records)

• Consider p := q;

• This is just copying the address that is in q into p.

ld[%fp-4], %l0! Assuming q is in fp-4

st%l0, [%fp-8]

### Pointers (to Records)

• Consider x := myRPtr.field;

ld[%fp-4], %l0! Load myRPtr

! %l0 now contains the base address of the record

set4, %l1! Field at offset of 4

ld[%l2], %l0! Load field’s value

st%l0, [%fp-8]! Store value in x

### An Example

TYPE ptrType = POINTER TO RECORD x,y : INTEGER; END;

VAR myGlobal : ptrType;

FUNCTION foo() : INTEGER;

VAR myLocal : ptrType;

BEGIN

NEW(myLocal);

myLocal.y := 420;

RETURN 7;

END foo;

BEGIN

NEW(myGlobal);

myGlobal.x := foo();

WRITE myGlobal.x;

END.

### The Result (simplified)

callcalloc! NEW

nop

st%o0, [%fp-4]

! myLocal.y := 420

set420, %l0

ld[%fp-4], %l1

set4, %l2 ! y’s offset

st%l0, [%l1]

! RETURN 7

set7, %i0

ret

restore

foo.SAVE = -(92 + 4) & -8

.globalmain

main:

save%sp, -96, %sp

setglobals, %g7

! NEW(myGlobal)

(same code as other NEW)

! myGlobal.x := foo()

callfoo

nop

ld[%g7-4], %l0

set0, %l1 ! x’s offset

st%o0, [%l0]

! OUTPUT myGlobal.x

setifmt, %o0

ld[%g7-4], %l0

set0, %l1 ! x’s offset

ld[%l0], %o1

callprintf

nop

.section".bss"

.align4

.skip4

globals:

.section".rodata"

.align4

ifmt: .asciz"%d"

.section".text"

.align4

.globalfoo

foo:

setfoo.SAVE, %g1

save%sp, %g1, %sp

! NEW(myLocal)

set1, %o0! numelem

set8, %o1! sizeof(rec)

### What to do Next!

• Finish Phase 2.

• Start of Phase 3.

• Thoroughly test and re-test Phase 1, 2, and 3.

• Come to lab hours and ask questions.

### Topics/Questions you may have

• Anything else you would like me to go over now?

• Anything in particular you would like to see next week?