ada declarations declarations id offsets i, 0 j; 2 type r is record x, 0 y: integer; 2 end; arecord:...

23
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

Post on 21-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Ada DeclarationsDeclarations ID Offsetsi, 0j; 2type r is

recordx, 0y: integer; 2

end;arecord: r; 4a1: array (1..10) of r 8a2: array (4..6) of array (8..12) of integer; 48

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

Code Generation Examples (2)a2(i,j) = a1(i).y;

subi addr(a2),40,t4 – – lower(a2)*element_size(a2)rangetest 4,6,imulti i,10,t5 – – element size is 10addi t4,t5,t6 – – t6 contains address of a2(i)subi t6,16,t7 – – subtract 8*2rangetest 8,12,jmulti j,2,t8 – – element size is 2addi t7,t8,t9 – – t9 contains address of a2(i,j)subi addr(a1),4,t10 – – 4 = lower(a1)*element_size(a1)rangetest 1,10,imulti i,4,t11 – – element size is 4addi t10,t11,t12 – – t12 contains address of a1(i)addi t12,2,t13 – – t12 has address of a1(i),yassign @t13,2,@t9

Stringsif (s1 = s2) seq s1,s2,t1

s2 := s3; jump0 t1,l1

else scopy s3,s2

s1 := s2&substr(s3,4,3); jump l2

end label l1

substr s3,4,3,t2

concatenate s2,t2,t3

assign t3,s1

– – scopy unnecessary

– – because source is temp

label l2

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

Multidimensional ArraysElements of multidimensional arrays arestored:• 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

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

Declaration

Assume the following declaration:

a: array(L1..U1, …, Ln..Un)

Elements Per Dimension

The number of elements in the jth dimension

of the array are:

Dj = Uj – Lj

Array Element PositionThe following is the position of a(i1, …in)

relative to a(L1, …Ln):

(in–Ln)+(in–1–Ln–1)Dn+(in–2–Ln–2) Dn Dn–1+(i1–L1)Dn …D2

It can be rewritten as:

varpart – conpart =

i1D2…Dn+ i2D3 …Dn +…+ in–1 Dn+in – (L1D2…Dn+ L2D3 …Dn +…+ Ln–1 Dn+Ln )

And in turn can be rewritten as:

varpart – conpart = (((i1D2+ i2) D3+ i2 ) D4 + i4…c Dn + in – conpart

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

Con_part

con_part = 1*10*20+1*20 + 1 = 221

Runtime Coderangetest 1,10,iassign i 2 t1rangetest 1,10,jmulti t1,10,t2addi, t2,j,t3rangetest 1,20,kmulti t3,20,t4addi t4,k,t5subi t5,221,t6multi t6,2,t7addi t7, addr(a),t8

Arrays of Arrays

a: array(1..10) of array(0..5) of array(3..4) of integer;

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

Referencing Slices

Offset Size

a SA type&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

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

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;

Storage LayoutElements of array E

Elements of array D

Field F

Offset for E

Offset for D

Field C

Field B

.

.

.

a’s descriptor

.

.

.

Descriptors for Recordstype 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;

Record Descriptors

L = 1 U = ? Size = ? descriptor for t1

L = 1 U = ? 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 = 1 U = 10 Size = ? descriptor for t4

address of x

After Elaboration

L = 1 U = 5 Size = 10 descriptor for t1

L = 1 U = 6 Size = 24 descriptor 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 = 1 U = 10 Size = 410

descriptor for t4

Stack Top address of X

Top of StackRecord 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