Chapter 6: Runtime System

Layout of local data

. Assume byte is the smallest unit

. Multi-byte objects are stored in consecutive bytes and given address of first byte

. The amount of storage needed is determined by its type

. Memory allocation is done as the declarations are processed

. Data may have to be aligned (in a word) padding is done to have alignment.

- Complier may pack the data so no padding is left

- Additional instructions may be required to execute packed data

We are assuming here that runtime storage is allocated in blocks of contiguous bytes. As mentioned, type determines the amount of space needed. Elementary types generally require an integral number of bytes. For aggregates like arrays or structures, enough memory is needed to store all their components. This memory is usually allocated contiguously for easy access. As declarations are examined, the space for local data is laid out. A count is kept of the number of allocated memory locations. From this count, a relative address for each local data object can be determined, with respect to some fixed starting point such as the beginning of the activation record. This relative address, or offset, represents the difference between the addresses of the starting point and the data object. The layout of data may be influenced by the machine's addressing system. For example, a machine may have a word of length 4 bytes, and may expect separate data objects to be stored in separate words (i.e., each object should have a starting byte address divisible by 4). In order to achieve this kind of alignment, padding has to be used, which means that blank spaces are left in between objects, with the number of blanks after an object depending on its size. Even if the machine can operate on non- aligned data, it may increase runtime delays, so padding is useful to speed up data access. For example, FORTRAN has a specification for a packed array, which can be declared as follows:

a: packed array [1.10] of boolean;

As per the language specification, this should take up only 10 bits of memory , but it was actually implemented in such a way as to take up 10 words (on a typical machine, each word has 32 bits), because it was too inefficient to store it without padding. Sometimes, due to space constraints, padding may not be possible, so that the data has to be packed together, with no gaps. Since the machine generally expects aligned data, special instructions may be required at runtime to position packed data so that it can be operated on as if aligned.