Upload
dylan
View
33
Download
0
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
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.
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
Oberon: e1 & e2:! Load e1 and check if FALSE
ld [%fp-4], %l0 ! e1’s temp
cmp %l0, %g0
be flabel
nop
! e1 is TRUE, so check e2
ld [%fp-8], %l0 ! e2’s temp
cmp %l0, %g0
be flabel
nop
! e2 is TRUE, so result TRUE
mov 1, %l5
ba endlabel
nop
flabel:
mov 0, %l5
endlabel:
! Result is now in %l5
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”.align 4.skip 28 ! 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
set X, %l1 ! X base address
set b, %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
set a, %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 recordset 4, %l1 ! Field at offset of 4add %l0, %l1, %l2 ! Locate field’s addressld [%l2], %l0 ! Load field’s valuest %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).section ".bss".align 4.skip 4
globals:
.section ".rodata"
.align 4ifmt: .asciz "%d"
.section ".text"
.align 4
.global foofoo:
set foo.SAVE, %g1save %sp, %g1, %sp
! NEW(myLocal)set 1, %o0 ! numelemset 8, %o1 ! sizeof(rec)
call calloc ! NEWnopst %o0, [%fp-4]! myLocal.y := 420set 420, %l0ld [%fp-4], %l1set 4, %l2 ! y’s offsetadd %l1, %l2, %l1st %l0, [%l1]! RETURN 7set 7, %i0retrestore
foo.SAVE = -(92 + 4) & -8
.global mainmain:
save %sp, -96, %sp
set globals, %g7! NEW(myGlobal)(same code as other NEW)! myGlobal.x := foo()call foonopld [%g7-4], %l0set 0, %l1 ! x’s offsetadd %l0, %l1, %l0st %o0, [%l0]! OUTPUT myGlobal.xset ifmt, %o0ld [%g7-4], %l0set 0, %l1 ! x’s offsetadd %l0, %l1, %l0ld [%l0], %o1call printfnop
What to do Next!
1. Finish Phase 2.
2. Start of Phase 3.
3. Thoroughly test and re-test Phase 1, 2, and 3.
4. 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?