190 likes | 295 Views
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.
E N D
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 ! e2 is TRUE, so result TRUE mov 1, %l5 ba endlabel nop flabel: mov 0, %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 be flabel nop ! e1 is TRUE, so check e2 ld [%fp-8], %l0 ! e2’s temp cmp %l0, %g0 be flabel 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” .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 record set 4, %l1 ! Field at offset of 4 add %l0, %l1, %l2 ! Locate field’s address 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) call calloc ! NEW nop st %o0, [%fp-4] ! myLocal.y := 420 set 420, %l0 ld [%fp-4], %l1 set 4, %l2 ! y’s offset add %l1, %l2, %l1 st %l0, [%l1] ! RETURN 7 set 7, %i0 ret restore foo.SAVE = -(92 + 4) & -8 .global main main: save %sp, -96, %sp set globals, %g7 ! NEW(myGlobal) (same code as other NEW) ! myGlobal.x := foo() call foo nop ld [%g7-4], %l0 set 0, %l1 ! x’s offset add %l0, %l1, %l0 st %o0, [%l0] ! OUTPUT myGlobal.x set ifmt, %o0 ld [%g7-4], %l0 set 0, %l1 ! x’s offset add %l0, %l1, %l0 ld [%l0], %o1 call printf nop .section ".bss" .align 4 .skip 4 globals: .section ".rodata" .align 4 ifmt: .asciz "%d" .section ".text" .align 4 .global foo foo: set foo.SAVE, %g1 save %sp, %g1, %sp ! NEW(myLocal) set 1, %o0 ! numelem set 8, %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?