Pristine Ack-5.5
[Ack-5.5.git] / doc / em / dspace.nr
1 .bp
2 .P1 "DATA ADDRESS SPACE"
3 .PP
4 The data address space is divided into three parts, called 'areas',
5 each with its own addressing method:
6 global data area,
7 local data area (including the stack),
8 and heap data area.
9 These data areas must be part of the same
10 address space because all data is accessed by
11 the same type of pointers.
12 .PP
13 Space for global data is reserved using several pseudoinstructions in the
14 assembly language, as described in
15 the next paragraph and chapter 11.
16 The size of the global data area is fixed per program.
17 .QQ
18 Global data is addressed absolutely in the machine language.
19 Many instructions are available to address global data.
20 They all have an absolute address as argument.
21 Examples are LOE, LAE and STE.
22 .PP
23 Part of the global data area is initialized by the
24 compiler, the
25 rest is not initialized at all or is initialized
26 with a value, typically \-32768 or 0.
27 Part of the initialized global data may be made read-only
28 if the implementation supports protection.
29 .PP
30 The local data area is used as a stack,
31 which grows from high to low addresses
32 and contains some data for each active procedure
33 invocation, called a 'frame'.
34 The size of the local data area varies dynamically during
35 execution.
36 Below the current procedure frame resides the operand stack.
37 The stack pointer SP always points to the bottom of
38 the local data area.
39 Local data is addressed by offsetting from the local base pointer LB.
40 LB always points to the frame of the current procedure.
41 Only the words of the current frame and the parameters
42 can be addressed directly.
43 Variables in other active procedures are addressed by following
44 the chain of statically enclosing procedures using the LXL or LXA instruction.
45 The variables in dynamically enclosing procedures can be
46 addressed with the use of the DCH instruction.
47 .QQ
48 Many instructions have offsets to LB as argument,
49 for instance LOL, LAL and STL.
50 The arguments of these instructions range from \-1 to some
51 (negative) minimum
52 for the access of local storage and from 0 to some (positive)
53 maximum for parameter access.
54 .PP
55 The procedure call instructions CAL and CAI each create a new frame
56 on the stack.
57 Each procedure has an assembly-time parameter specifying
58 the number of bytes needed for local storage.
59 This storage is allocated each time the procedure is called and
60 must be a multiple of the wordsize.
61 Each procedure, therefore, starts with a stack with the local variables
62 already allocated.
63 The return instructions RET and RTT remove a frame.
64 The actual parameters must be removed by the calling procedure.
65 .PP
66 RET may copy some words from the stack of
67 the returning procedure to an unnamed 'function return area'.
68 This area is available for 'READ-ONCE' access using the LFR instruction.
69 The result of a LFR is only defined if the size used to fetch
70 is identical to the size used in the last return.
71 The instruction ASP, used to remove the parameters from the
72 stack, the branch instruction BRA and the non-local goto
73 instruction GTO are the only ones that leave the contents of
74 the 'function return area' intact.
75 All other instructions are allowed to destroy the function
76 return area.
77 Thus parameters can be popped before fetching the function result.
78 The maximum size of all function return areas is
79 implementation dependent,
80 but should allow procedure instance identifiers and all
81 implemented objects of type integer, unsigned, float
82 and pointer to be returned.
83 In most implementations
84 the maximum size of the function return
85 area is twice the pointer size,
86 because we want to be able to handle 'procedure instance
87 identifiers' which consist of a procedure identifier and the LB
88 of a frame belonging to that procedure.
89 .PP
90 The heap data area grows upwards, to higher numbered
91 addresses.
92 It is initially empty.
93 The initial value of the heap pointer HP
94 marks the low end.
95 The heap pointer may be manipulated
96 by the LOR and STR instructions.
97 The heap can only be addressed indirectly,
98 by pointers derived from previous values of HP.
99 .P2 "Global data area"
100 .PP
101 The initial size of the global data area is determined at assembly time.
102 Global data is allocated by several
103 pseudoinstructions in the EM assembly
104 language.
105 Each pseudoinstruction allocates one or more bytes.
106 The bytes allocated for a single pseudo form
107 a 'block'.
108 A block differs from a fragment, because,
109 under certain conditions, several blocks are allocated
110 in a single fragment.
111 This guarantees that the bytes of these blocks
112 are consecutive.
113 .PP
114 Global data is addressed absolutely in binary
115 machine language.
116 Most compilers, however,
117 cannot assign absolute addresses to their global variables,
118 especially not if the language
119 allows programs to be composed of several separately compiled modules.
120 The assembly language therefore allows the compiler to name
121 the first address of a global data block with an alphanumeric label.
122 Moreover, the only way to address such a named global data block
123 in the assembly language is by using its name.
124 It is the task of the assembler/loader to
125 translate these labels into absolute addresses.
126 These labels may also be used
127 in CON and ROM pseudoinstructions to initialize pointers.
128 .PP
129 The pseudoinstruction CON allocates initialized data.
130 ROM acts like CON but indicates that the initialized data will
131 not change during execution of the program.
132 The pseudoinstruction BSS allocates a block of uninitialized
133 or identically initialized
134 data.
135 The pseudoinstruction HOL is similar to BSS,
136 but it alters the meaning of subsequent absolute addressing in
137 the assembly language.
138 .PP
139 Another type of global data is a small block,
140 called the ABS block, with an implementation defined size.
141 Storage in this type of block can only be addressed
142 absolutely in assembly language.
143 The first word has address 0 and is used to maintain the
144 source line number.
145 Special instructions LIN and LNI are provided to
146 update this counter.
147 A pointer at location 4 points to a string containing the
148 current source file name.
149 The instruction FIL can be used to update the pointer.
150 .PP
151 All numeric arguments of the instructions that address
152 the global data area refer to locations in the
153 ABS block unless
154 they are preceded by at least one HOL pseudo in the same
155 module,
156 in which case they refer to the storage area allocated by the
157 last HOL pseudoinstruction.
158 Thus LOE 0 loads the zeroth word of the most recent HOL, unless no HOL has
159 appeared in the current file so
160 far, in which case it loads the zeroth word of the
161 ABS fragment.
162 .PP
163 The global data area is highly fragmented.
164 The ABS block and each HOL and BSS block are separate fragments.
165 The way fragments are formed from CON and ROM blocks is more complex.
166 The assemblers group several blocks into a single fragment.
167 A fragment only contains blocks of the same type: CON or ROM.
168 It is guaranteed that the bytes allocated for two consecutive CON pseudos are
169 allocated consecutively in a single fragment, unless
170 these CON pseudos are separated in the assembly language program
171 by a data label definition or one or more of the following pseudos:
172 .DS
173 ROM, BSS, HOL and END
174 .DE
175 An analogous rule holds for ROM pseudos.
176 .P2 "Local data area"
177 .PP
178 The local data area consists of a sequence of frames, one for
179 each active procedure.
180 Below the frame of the current procedure resides the
181 expression stack.
182 Frames are generated by procedure calls and are
183 removed by procedure returns.
184 A procedure frame consists of six 'zones':
185 .DS
186 1.  The return status block
187 2.  The local variables and compiler temporaries
188 3.  The register save block
189 4.  The dynamic local generators
190 5.  The operand stack.
191 6.  The parameters of a procedure one level deeper
192 .DE
193 A sample frame is shown in Figure 1.
194 .PP
195 Before a procedure call is performed the actual
196 parameters are pushed onto the stack of the calling procedure.
197 The exact details are compiler dependent.
198 EM allows procedures to be called with a variable number of
199 parameters.
200 The implementation of the C-language almost forces its runtime
201 system to push the parameters in reverse order, that is,
202 the first positional parameter last.
203 Most compilers use the C calling convention to be compatible.
204 The parameters of a procedure belong to the frame of the
205 calling procedure.
206 Note that the evaluation of the actual parameters may imply
207 the calling of procedures.
208 The parameters can be accessed with certain instructions using
209 offsets of 0 and greater.
210 The first byte of the last parameter pushed has offset 0.
211 Note that the parameter at offset 0 has a special use in the
212 instructions following the static chain (LXL and LXA).
213 These instructions assume that this parameter contains the LB of
214 the statically enclosing procedure.
215 Procedures that do not have a dynamically enclosing procedure
216 do not need a static link at offset 0.
217 .PP
218 Two instructions are available to perform procedure calls, CAL
219 and CAI.
220 Several tasks are performed by these call instructions.
221 .QQ
222 First, a part of the status of the calling procedure is
223 saved on the stack in the return status block.
224 This block should contain the return address of the calling
225 procedure, its LB and other implementation dependent data.
226 The size of this block is fixed for any given implementation
227 because the lexical instructions LPB, LXL and LXA must be able to
228 obtain the base addresses of the procedure parameters \fBand\fP local
229 variables.
230 An alternative solution can be used on machines with a highly
231 segmented address space.
232 The stack frames need not be contiguous then and the first
233 status save area can contain the parameter base AB,
234 which has the value of SP just after the last parameter has
235 been pushed.
236 .QQ
237 Second, the LB is changed to point to the
238 first word above the local variables.
239 The new LB is a copy of the SP after the return status
240 block has been pushed.
241 .QQ
242 Third, the amount of local storage needed by the procedure is
243 reserved.
244 The parameters and local storage are accessed by the same instructions.
245 Negative offsets are used for access to local variables.
246 The highest byte, that is the byte nearest
247 to LB, has to be accessed with offset \-1.
248 The pseudoinstruction specifying the entry point of a
249 procedure, has an argument that specifies the amount of local
250 storage needed.
251 The local variables allocated by the CAI or CAL instructions
252 are the only ones that can be accessed with a fixed negative offset.
253 The initial value of the allocated words is
254 not defined, but implementations that check for undefined
255 values will probably initialize them with a
256 special 'undefined' pattern, typically \-32768.
257 .QQ
258 Fourth, any EM implementation is allowed to reserve a variable size
259 block beneath the local variables.
260 This block could, for example, be used to save a variable number
261 of registers.
262 .QQ
263 Finally, the address of the entry point of the called procedure
264 is loaded into the Program Counter.
265 .PP
266 The ASP instruction can be used to allocate further (dynamic)
267 local storage.
268 The base address of such storage must be obtained with a LOR~SP
269 instruction.
270 This same instruction ASP may also be used
271 to remove some words from the stack.
272 .PP
273 There is a version of ASP, called ASS, which fetches the number
274 of bytes to allocate from the stack.
275 It can be used to allocate space for local
276 objects whose size is unknown at compile time,
277 so called 'dynamic local generators'.
278 .PP
279 Control is returned to the calling procedure with a RET instruction.
280 Any return value is then copied to the 'function return area'.
281 The frame created by the call is deallocated and the status of
282 the calling procedure is restored.
283 The value of SP just after the return value has been popped must
284 be the same as the
285 value of SP just before executing the first instruction of this
286 invocation.
287 This means that when a RET is executed the operand stack can
288 only contain the return value and all dynamically generated locals must be
289 deallocated.
290 Violating this restriction might result in hard to detect
291 errors.
292 The calling procedure has to remove the parameters from the stack.
293 This can be done with the aforementioned ASP instruction.
294 .PP
295 Each procedure frame is a separate fragment.
296 Because any fragment may be placed anywhere in memory,
297 procedure frames need not be contiguous.
298 .Dr 47
299                 |===============================|
300                 |     actual parameter  n-1     |
301                 |-------------------------------|
302                 |              .                |
303                 |              .                |
304                 |              .                |
305                 |-------------------------------|
306                 |     actual parameter  0       | ( <\- AB )
307                 |===============================|
308
309
310                 |===============================|
311                 |///////////////////////////////|
312                 |///// return status block /////|
313                 |///////////////////////////////|   <\- LB
314                 |===============================|
315                 |                               |
316                 |       local variables         |
317                 |                               |
318                 |-------------------------------|
319                 |                               |
320                 |      compiler temporaries     |
321                 |                               |
322                 |===============================|
323                 |///////////////////////////////|
324                 |///// register save block /////|
325                 |///////////////////////////////|
326                 |===============================|
327                 |                               |
328                 |   dynamic local generators    |
329                 |                               |
330                 |===============================|
331                 |           operand             |
332                 |-------------------------------|
333                 |           operand             |
334                 |===============================|
335                 |         parameter  m-1        |
336                 |-------------------------------|
337                 |              .                |
338                 |              .                |
339                 |              .                |
340                 |-------------------------------|
341                 |         parameter  0          | <\- SP
342                 |===============================|
343 .Df
344 Figure 1. A sample procedure frame and parameters.
345 .De
346 .P2 "Heap data area"
347 .PP
348 The heap area starts empty, with HP
349 pointing to the low end of it.
350 HP always contains a word address.
351 A copy of HP can always be obtained with the LOR instruction.
352 A new value may be stored in the heap pointer using the STR instruction.
353 If the new value is greater than the old one,
354 then the heap grows.
355 If it is smaller, then the heap shrinks.
356 HP may never point below its original value.
357 All words between the current HP and the original HP
358 are allocated to the heap.
359 The heap may not grow into a part of memory that is already allocated.
360 When this is attempted, the STR instruction will cause a trap to occur.
361 In this case, HP retains its old value.
362 .PP
363 The only way to address the heap is indirectly.
364 Whenever an object is allocated by increasing HP,
365 then the old HP value must be saved and can be used later to address
366 the allocated object.
367 If, in the meantime, HP is decreased so that the object
368 is no longer part of the heap, then an attempt to access
369 the object is not allowed.
370 Furthermore, if the heap pointer is increased again to above
371 the object address, then access to the old object gives undefined results.
372 .PP
373 The heap is a single fragment.
374 All bytes have consecutive addresses.
375 No limits are imposed on the size of the heap as long as it fits
376 in the available data address space.