Download presentation
Presentation is loading. Please wait.
1
Ada Declarations Declarations ID Offsets i, 0 j; 2 type r is record x, 0 y: integer; 2 end; arecord: r; 4 a1: array (1..10) of r 8 a2: array (4..6) of array (8..12) of integer;48
2
Code Generation Examples (1) a1(i) = arecord; subi addr(a1),4,t1– – = lower(a1)*element size rangetest 1,10,i multi i,4,t2– – element size is 4 addi t1,t2,t3– – t3 contains address of a1(i) assign arecord,4,@t3– – @ indicates indirection arecord.y = i; assign i, 2, @(addr(arecord)+2))– – offset of y = 2
3
Code Generation Examples (2) a2(i,j) = a1(i).y; subi addr(a2),40,t4– – lower(a2)*element_size(a2) rangetest 4,6,i multi i,10,t5 – – element size is 10 addi t4,t5,t6– – t6 contains address of a2(i) subi t6,16,t7– – subtract 8*2 rangetest 8,12,j multi j,2,t8– – element size is 2 addi t7,t8,t9– – t9 contains address of a2(i,j) subi addr(a1),4,t10– – 4 = lower(a1)*element_size(a1) rangetest 1,10,i multi i,4,t11– – element size is 4 addi t10,t11,t12– – t12 contains address of a1(i) addi t12,2,t13– – t12 has address of a1(i),y assign @t13,2,@t9
4
Strings if (s1 = s2)seq s1,s2,t1 s2 := s3;jump0 t1,l1 elsescopy s3,s2 s1 := s2&substr(s3,4,3);jump l2 endlabel l1 substr s3,4,3,t2 concatenate s2,t2,t3 assign t3,s1 – – scopy unnecessary – – because source is temp label l2
5
Array Storage Allocation If all array bounds are constant, the array can be allocated in static, stack or heap storage. No runtime descriptor is needed If array bounds are evaluated at scope entry, stack or heap storage may be used. A descriptor is needed If array bounds are flexible (changeable at any time), heap storage must be used and a dope vector or some time of descriptor is needed. These arrays act like strings
6
Multidimensional Arrays Elements of multidimensional arrays are stored: In contiguous memory –Row Major – rightmost subscript increments most rapidly [a(1,1), a(1,2), …, a(2,1), a(2,2), …]. Used in PL/1, Algol, Pascal, C, Ada, etc. –Column Major – leftmost subscript increments most rapidly [a(1,1), a(2,1), …, a(1,2), a(2,2), …]. Used in FORTRAN By vectors –All elements are adjacent in a vector –An array is a vector of pointers to subarrays or vectors
7
Multidimensional Arrays Consider a row-major ordering arrays of the following forms are considered equivalent: –array(1..n, 1..m) of t –array(1..n) of array(1..m) of t
8
Declaration Assume the following declaration: a: array(L 1..U 1, …, L n..U n )
9
Elements Per Dimension The number of elements in the jth dimension of the array are: D j = U j – L j
10
Array Element Position The following is the position of a(i 1, …i n ) relative to a(L 1, …L n ) : (i n –L n )+(i n–1 –L n–1 )D n +(i n–2 –L n–2 ) D n D n–1 +(i 1 –L 1 )D n …D 2 It can be rewritten as: varpart – conpart = i 1 D 2 …D n + i 2 D 3 …D n +…+ i n–1 D n +i n – (L 1 D 2 …D n + L 2 D 3 …D n +…+ L n–1 D n +L n ) And in turn can be rewritten as: varpart – conpart = (((i 1 D 2 + i 2 ) D 3 + i 2 ) D 4 + i 4 …c D n + i n – conpart
11
Array Reference a: array(1..10, 1..10, 1..20) a(1, 2, 3); Generates no runtime code a(i, j, k); Does generate runtime code
12
Con_part con_part = 1*10*20+1*20 + 1 = 221
13
Runtime Code rangetest 1,10,i assign i 2 t1 rangetest 1,10,j multi t1,10,t2 addi, t2,j,t3 rangetest 1,20,k multi t3,20,t4 addi t4,k,t5 subi t5,221,t6 multi t6,2,t7 addi t7, addr(a),t8
14
Arrays of Arrays a: array(1..10) of array(0..5) of array(3..4) of integer;
15
Symbol Table Representation a SA type&3 U=1 L=10 S=240 type&2 U=5 L=0 S=24 type&1 U=4 L=3 S=4 int
16
Referencing Slices OffsetSize aSAtype&3.S a(i)SA + type&2.S*(i–type&3.L)type&2.S a(i)(j)a(i) offset+type&1.S*(j–type&2.L) SA+type&2.S*(i–type&3.L)+type&1.S*(j–type&2.L) type&1.S a(i)(j)(k)a(i,j) offset+int*(k–type&1.L) SA+type&2.S*(i–type&3.L)+type&1.S*(j–type&2.L)+ int*(k–type&1.L) int
17
Instructions for a(i)(j)(k); rangetest (type&3.L,type&3.U,i); subi(i,type&3.L,t1); multi(t1,type&2.S,t2); addi(SA,t2,t3);t3 = offset a(i); size = type&2.S rangetest(type&2.L,type&2.U,j); subi(j,type&2.L,t4); multi(t4,type&1.S,t5); addi(t3,t5,t6);t6 = offset a(i)(j); size = type&1.S rangetest(type&1.L,type&1.U,k); subi(k,type&1.L,t7); multi(t7,sizeof(int),t8) addi(t6,t8,t9)t9 = offset a(i)(j)(k); size = int
18
Dynamic Records type a1 is array(1..i) of integer; type a2 is array(1..j) of float; type r is record b: integer; c: float; d: a1; e: a2; f: boolean; end record; a: r;
19
Storage Layout Elements of array E Elements of array D Field F Offset for E Offset for D Field C Field B...... a’s descriptor......
20
Descriptors for Records type t1 is array(1..i) of integer; type t2 is array(1..j) of float; type t3 is record b: integer; c: float; d: t1; e: t2; f: boolean; end record; type t4 is array(1..10) of t3; x: t4;
21
Record Descriptors L = 1U = ?Size = ?descriptor for t1 L = 1U = ?Size = ?descriptor for t2 0 (b’s offset) 2 (c’s offset) 6 (d’s offset) ? (e’s offset) ? (f’s offset) ? (Size) descriptor for t3 L = 1U = 10Size = ?descriptor for t4 address of x
22
After Elaboration L = 1U = 5Size = 10descriptor for t1 L = 1U = 6Size = 24descriptor for t2 0 (b’s offset) 2 (c’s offset) 6 (d’s offset) 16 (e’s offset) 40 (f’s offset) 41 (Size) Size = 2 Size = 4 Size = 10 Size = 24 Size = 1 descriptor for t3 L = 1U = 10Size = 410descriptor for t4 Stack Topaddress of X
23
Top of Stack Record type t3 f (24) …e (7) …(6) (10) …d (6) …(5) c b (10) (9) (8) (7) x(6) (5) (4) (3) (2) (1) Stack Top
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.