Strings, Records and Arrays
Strings, Records and Arrays
String
direct representation: Pascal string var s:string[11];
layout
Records
A record it typically represented as a contiguous area of memory with the addresses of the elds of the record going rising as one moves through the elds.
records continued
This would 3 2 1 0 +-------+ | s | +-------+ | s | +---+-+-+ | s ||b| +---+-+-+ | x | +-------+ be 16 bytes organised as offset in word Address 12 8 4 0 note indicates length byte of string s
APL Algol Fortran like like like A+ Delphi C J Algol60 Pascal Scheme S-algol Ada Java ML Hi Haskell Note that at level of abstraction I treat lists, arrays and vectors as syntactic sugar over the basic concept of a sequence of values.
lisp like
Dynamism
Dynamic rank Fixed rank mutable array size Fixed rank array size xed at point of declaration - may depend on function parameters Statically known array size
Bounds
Are they checked Do they start at zero
Lisp Like
A list is a sequence of values which may be either atoms or lists. This allows lists of dynamically dened rank, and even of variable rank. (( 2, 3, 7), 6) The rst element of the list is a list of 3 atoms and the second element is an atom. Thus the notion of rank is not dened in this case.
APL Like
All arrays have a well dened rank ( number of dimensions ), and a well dened shape ( number of elements in a dimension) , but these can vary for a given array variable in the course of computation. APL: x n x gets the sequence of integers from 1 to n S-Algol : let x:= vector 1::n of 1 x becomes the array of n storage locations, all initialised to 1 Java: x= new int[n]; x becomes the array of n storage locations initialised to 0
APL Like
Note the difference in degrees of initialisation supported.
Algol 60 like
On entry to a block the array size is specied by values that may be computed at run time but once the variable has been created its size is un-varying.
Fortran Like :
In this case the compiler knows as soon as it has parsed the declaration exactly how much store will be used. The size of the array is xed. Additional issue here is whether the bounds have to start at a xed number such as 0 or 1 or can be dened by the programmer: C: int x[6]; reserve 6 store locations numbered 0 to 5 Pascal : var sales: array[1996..2004] of integer; reserve 8 store location numbered 1996 to 2004 The latter form has some advantages from the standpoint of intelligibility of code.
Representations
Lisp Like
((2,3,5),7)
Tags
Words can be tagged using the top bit of each word to indicate a pointer. This allows arbitrary list structures to be disambiguaged but this has the effect of reducing arithmetic precision. Assume you have a 32 bit word. Then the normal sign bit ( bit 31) is reserved as a tag. You only have the normal range of +ve numbers available as integers 0..231 1 We have to map this into a range of -ve and +ve numbers. Do this with the operations: getint(t)=t + 230 putint(i,t) t i 230
Tags
Some Lisp Machines have provided 40 bit words with 8 bit tags. Tags distinguished different types.
a:= 6 a 0 1 2 3 4 5 a 6 b:= 2 3 a b 0 1 2 3 4 5 b
Note that each variable is stored as a data vector and a shape vector which says how to interpret the data vector.
Algol-Like arrays
Note that the array is created on the stack, the stack pointer being moved down when the array declaration is encountered.
Static Arrays
Denition: An array is static if its bounds are known at compile time. Examples
b[2]:=7 mem[@(b)+2] :=7 mem[008c+2]:=7 mem[008e]:=7 mov byte [008e],7 C606 8E00 07
a[b[2]]:=6 a[mem[008e]]:=6 mem[@(a)+(mem[008e]-lwb(a))*esize(a)]:=6 mem[0080 +( mem[008e]- 1)*4]:=6 edi:=mem[008e] movzx edi,byte[0x008e] ; mem[0080+edi*4]:=6mov dword[0x0080+edi*4],6 ;
movzx edi, byte [0x008e] 660FB63E 8E00 mov dword[0x0080+edi*4],66667C704BD 80000000 06000
General rule:
for a 1 dimensional static array translate according to x[i] mem[@x + (i lwb(x)) esize(x)] where i is an arbitrary expression
2 D array
var e:array[1..4,3..6] of integer; General rule f [i, j] mem[@f + ((i lwb1(f )) rs(f ) + j lwb2(f )) esize(f )] rs(f ) = upb(f )lwb(f )+1 row sizeAlgol-Like arrays assume @p = 0040, @q=0044, @e= 00c0 e[p,q]:=5 mem[@e+((p-1)*4+q-3)*4]:=5
; ; ; ;
2 D array
sub edi,3 ; -lwb mov dword [00c0+edi*4],5