Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 131B – Compiler Construction II Discussion 8: Complex Data Structures 3/7/2007 Reminder: No class Thursday – lab hours instead.

Similar presentations


Presentation on theme: "CSE 131B – Compiler Construction II Discussion 8: Complex Data Structures 3/7/2007 Reminder: No class Thursday – lab hours instead."— Presentation transcript:

1 CSE 131B – Compiler Construction II Discussion 8: Complex Data Structures 3/7/2007 Reminder: No class Thursday – lab hours instead

2 Example of Arrays in Details TYPE arr = ARRAY 10 OF INTEGER; TYPE dblArr = ARRAY 5 OF arr; VAR x : INTEGER; VAR myArr : dblArr; BEGIN x := 3; myArr[x][7] := 99; WRITE myArr[x][7]; myArr[1] := myArr[x]; WRITE myArr[1][7]; END.

3 Resulting Code.section ".rodata" intFmt:.asciz "%d".section ".bss".align 4.skip 204 ! 4 for int, 200 for array of array globals: ! x is at %l7-4 ! myArr is at %l7-204.section ".text".align 4.global main main: set SAVE.main, %g1 save %sp, %g1, %sp set globals, %l7 ! x := 3 set 3, %l0 st %l0, [%fp-4] ! tmp1 (value) ld [%fp-4], %l0 st %l0, [%l7-4] ! assign into x ! myArr[x][7] := 99 ld [%l7-4], %l0 ! get x st %l0, [%fp-8] ! tmp2 (value) ld [%fp-8], %o0 set 40, %o1 ! size of "arr" call.mul nop sub %l7, 204, %l0 ! base of myArr add %l0, %o0, %l0 ! add offset of "x“ st %l0, [%fp-12] ! tmp3 (address) set 7, %l0 ! set 7 st %l0, [%fp-16] ! tmp4 (value) ld [%fp-16], %o0 set 4, %o1 ! size of "INTEGER" call.mul nop ld [%fp-12], %l0 ! base of myArr[x]-- Note difference! add %l0, %o0, %l0 ! add offset of "7" st %l0, [%fp-20] ! tmp5 (address) set 99, %l0 ! set 99 st %l0, [%fp-24] ! tmp6 (value) ld [%fp-24], %l0 ! get rhs ld [%fp-20], %l1 ! get dest addr st %l0, [%l1] ! do assignment ! WRITE myArray[x][7] ld [%l7-4], %l0 ! get x st %l0, [%fp-28] ! tmp7 (value) ld [%fp-28], %o0 set 40, %o1 ! size of "arr" call.mul nop sub %l7, 204, %l0 ! base of myArr add %l0, %o0, %l0 ! add offset of "x" st %l0, [%fp-32] ! tmp8 (address) set 7, %l0 ! set 7 st %l0, [%fp-36] ! tmp9 (value) ld [%fp-36], %o0 set 4, %o1 ! size of "INTEGER" call.mul nop ld [%fp-32], %l0 ! base of myArr[x] – Note difference! add %l0, %o0, %l0 ! add offset of "7" st %l0, [%fp-40] ! tmp10 (address) set intFmt, %o0 ld [%fp-40], %l0 ! get addr of myArr[x][7] ld [%l0], %o1 ! load value call printf nop

4 Resulting Code ! myArr[1] := myArr[x] set 1, %l0 ! set 1 st %l0, [%fp-44] ! tmp11 (value) ld [%fp-44], %o0 set 40, %o1 ! size of "arr" call.mul nop sub %l7, 204, %l0 ! base of myArr add %l0, %o0, %l0 ! add offset of "1" st %l0, [%fp-48] ! tmp12 (address) ld [%l7-4], %l0 ! get x st %l0, [%fp-52] ! tmp13 (value) ld [%fp-52], %o0 set 40, %o1 ! size of "arr" call.mul nop sub %l7, 204, %l0 ! base of myArr add %l0, %o0, %l0 ! add offset of "x" st %l0, [%fp-56] ! tmp14 (address) ! do memcpy for array assignment ld [%fp-48], %o0 ! dest address ld [%fp-56], %o1 ! src address set 40, %o2 ! num bytes (size of "arr") call memcpy nop ! WRITE myArr[1][7] set 1, %l0 ! set 1 st %l0, [%fp-60] ! tmp15 (value) ld [%fp-60], %o0 set 40, %o1 ! size of "arr" call.mul nop sub %l7, 204, %l0 ! base of myArr add %l0, %o0, %l0 ! add offset of "1" st %l0, [%fp-64] ! tmp16 (address) set 7, %l0 ! set 7 st %l0, [%fp-68] ! tmp17 (value) ld [%fp-68], %o0 set 4, %o1 ! size of "INTEGER" call.mul nop ld [%fp-64], %l0 ! base of myArr[1] ! Note difference! add %l0, %o0, %l0 ! add offset of "7" st %l0, [%fp-72] ! tmp18 (address) set intFmt, %o0 ld [%fp-72], %l0 ! get addr of myArr[1][7] ld [%l0], %o1 ! load value call printf nop ret restore SAVE.main = -(92 + 72) & -8 ! 18 temp vars (4 bytes each)

5 Is it an address or a value?  Similar to VAR parameters, your objects (temp mem or STOs) may be holding either a value or an address. In the prior example, some of the temps were holding addresses. In that example, the resulting VarSTO from the Designator2 rule has a temp %fp-XX address, which itself also hold an address.  It may be useful to have some sort of indicator in your STOs, similar to how you did VAR parameters.

6 Example of Pointers TYPE rec = RECORD x, y : INTEGER; VAR myPtr1, myPtr2 : POINTER to rec; BEGIN myPtr1 := NIL; myPtr2 := NIL; NEW myPtr1; IF (myPtr1 # NIL) THEN myPtr1.y := 77; myPtr2 := myPtr1; myPtr1 := NIL; END; WRITE myPtr2.y; END.

7 Resulting Code.section ".rodata" intFmt:.asciz "%d".section ".bss".align 4.skip 8 ! 4 for myPtr1, 4 for myPtr2 globals: ! myPtr1 is at %l7-4 ! myPtr2 is at %l7-8.section ".text".align 4.global main main: set SAVE.main, %g1 save %sp, %g1, %sp set globals, %l7 ! myPtr1 := NIL mov %g0, %l0 ! NIL is address "0" st %l0, [%fp-4] ! tmp1 (address) ld [%fp-4], %l0 st %l0, [%l7-4] ! assign address into myPtr1 ! myPtr2 := NIL mov %g0, %l0 ! NIL is address "0" st %l0, [%fp-8] ! tmp2 (address) ld [%fp-8], %l0 st %l0, [%l7-8] ! assign address into myPtr2 ! NEW myPtr1 set 1, %o0 ! 1 element set 8, %o1 ! 8 bytes wide call calloc ! dynamically allocate nop st %o0, [%l7-4] ! make myPtr1 point to new mem ! (myPtr1 # NIL) ld [%l7-4], %l0 ! get myPtr1 mov %g0, %l1 ! set NIL cmp %l0, %l1 mov 0, %l2 ! result initially FALSE be L1 ! opposite logic nop mov 1, %l2 ! change result to TRUE L1: st %l2, [%fp-12] ! tmp3 (value) ! IF (myPtr1 # NIL) ld [%fp-12], %l0 ! get IF condition cmp %l0, %g0 ! compare with FALSE be L2 ! opposite logic nop ! myPtr1.y := 77 ld [%l7-4], %l0 ! get myPtr1 base address set 4, %l1 ! offset of "y" add %l0, %l1, %l0 ! myPtr1.y's address st %l0, [%fp-16] ! tmp4 (address) set 77, %l0 ! set 77 st %l0, [%fp-20] ! tmp5 (value) ld [%fp-20], %l0 ! get 77 ld [%fp-16], %l1 ! get myPtr1.y's address st %l0, [%l1] ! do assign ! myPtr2 := myPtr1 ld [%l7-4], %l0 ! get myPtr1 base address st %l0, [%fp-28] ! tmp7 (address) ld [%fp-28], %l0 ! get myPtr1's address st %l0, [%l7-8] ! assign into myPtr2 ! myPtr1 := NIL mov %g0, %l0 ! NIL is address "0" st %l0, [%fp-32] ! tmp8 (address) ld [%fp-32], %l0 st %l0, [%l7-4] ! assign address into myPtr1 L2: ! End of IF Body

8 Resulting Code ! WRITE myPtr2.y ld [%l7-8], %l0 ! myPtr2 base address set 4, %l1 ! y's offset add %l0, %l1, %l0 ! myPtr2.y's address st %l0, [%fp-36] ! tmp9 (address) set intFmt, %o0 ld [%fp-36], %l0 ! get myPtr2.y's address ld [%l0], %o1 ! get myPtr2.y's value call printf nop ret restore SAVE.main = -(92 + 36) & -8

9 Value vs VAR Thoughts TYPE ptr = POINTER TO RECORD x,y,z : INTEGER; END; VAR globalPtr : ptr; PROCEDURE foo(VAR xxx : ptr); BEGIN xxx := NIL;(* works *) NEW xxx;(* works *) xxx.x := 99;(* works *) END foo; PROCEDURE baz(xxx : ptr); BEGIN xxx := NIL;(* no side effect *) NEW xxx;(* no side effect *) xxx.x := 99;(* works *) END baz;

10 What to do Next! 1.Finish Phase 3. 2.Thoroughly test and re-test Phase 1, 2, and 3. 3.Come to lab hours and ask questions.

11 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?


Download ppt "CSE 131B – Compiler Construction II Discussion 8: Complex Data Structures 3/7/2007 Reminder: No class Thursday – lab hours instead."

Similar presentations


Ads by Google