Pristine Ack-5.5
[Ack-5.5.git] / doc / em / mach.nr
1 .bp
2 .P1 "EM MACHINE LANGUAGE"
3 .PP
4 The EM machine language is designed to make program text compact
5 and to make decoding easy.
6 Compact program text has many advantages: programs execute faster,
7 programs occupy less primary and secondary storage and loading
8 programs into satellite processors is faster.
9 The decoding of EM machine language is so simple,
10 that it is feasible to use interpreters as long as EM hardware
11 machines are not available.
12 This chapter is irrelevant when back ends are used to
13 produce executable target machine code.
14 .P2 "Instruction encoding"
15 .PP
16 A design goal of EM is to make the
17 program text as compact as possible.
18 Decoding must be easy, however.
19 The encoding is fully byte oriented, without any small bit fields.
20 There are 256 primary opcodes, two of which are an escape to
21 two groups of 256 secondary opcodes each.
22 .QQ
23 EM instructions without arguments have a single opcode assigned,
24 possibly escaped:
25 .ta 12n 24n
26 .Dr 6
27          |--------------|
28          |    opcode    |
29          |--------------|
30 .De
31                 or
32 .Dr 6
33          |--------------|--------------|
34          |    escape    |     opcode   |
35          |--------------|--------------|
36 .De
37 The encoding for instructions with an argument is more complex.
38 Several instructions have an address from the global data area
39 as argument.
40 Other instructions have different opcodes for positive
41 and negative arguments.
42 .LP
43 There is always an opcode that takes the next two bytes as argument,
44 high byte first:
45 .Dr 6
46          |--------------|--------------|--------------|
47          |    opcode    |    hibyte    |    lobyte    |
48          |--------------|--------------|--------------|
49 .De
50                 or
51 .Dr 6
52          |--------------|--------------|--------------|--------------|
53          |    escape    |    opcode    |    hibyte    |    lobyte    |
54          |--------------|--------------|--------------|--------------|
55 .De
56 An extra escape is provided for instructions with four or eight byte arguments.
57 .Dr 6
58          |--------------|--------------|--------------|   |--------------|
59          |    ESCAPE    |    opcode    |    hibyte    |...|    lobyte    |
60          |--------------|--------------|--------------|   |--------------|
61 .De
62 For most instructions some argument values predominate.
63 The most frequent combinations of instruction and argument
64 will be encoded in a single byte, called a mini:
65 .Dr 6
66          |---------------|
67          |opcode+argument|  (mini)
68          |---------------|
69 .De
70 The number of minis is restricted, because only
71 254 primary opcodes are available.
72 Many instructions have the bulk of their arguments
73 fall in the range 0 to 255.
74 Instructions that address global data have their arguments
75 distributed over a wider range,
76 but small values of the high byte are common.
77 For all these cases there is another encoding
78 that combines the instruction and the high byte of the argument
79 into a single opcode.
80 These opcodes are called shorties.
81 Shorties may be escaped.
82 .Dr 6
83          |--------------|--------------|
84          | opcode+high  |    lobyte    |  (shortie)
85          |--------------|--------------|
86 .De
87                 or
88 .Dr 6
89          |--------------|--------------|--------------|
90          |    escape    | opcode+high  |    lobyte    |
91          |--------------|--------------|--------------|
92 .De
93 Escaped shorties are useless if the normal encoding has a primary opcode.
94 Note that for some instruction-argument combinations
95 several different encodings are available.
96 It is the task of the assembler to select the shortest of these.
97 The savings by these mini and shortie
98 opcodes are considerable, about 55%.
99 .PP
100 Further improvements are possible:
101 the arguments of
102 many instructions are a multiple of the wordsize.
103 Some do also not allow zero as an argument.
104 If these arguments are divided by the wordsize and,
105 when zero is not allowed, then decremented by 1, more of them can
106 be encoded as shortie or mini.
107 The arguments of some other instructions
108 rarely or never assume the value 0, but start at 1.
109 The value 1 is then encoded as 0,
110 2 as 1 and so on.
111 .PP
112 Assigning opcodes to instructions by the assembler is completely
113 table driven.
114 For details see appendix B.
115 .P2 "Procedure descriptors"
116 .PP
117 The procedure identifiers used in the interpreter are indices
118 into a table of procedure descriptors.
119 Each descriptor contains:
120 .IP 1.
121 the number of bytes to be reserved for locals at each
122 invocation.
123 .br
124 This is a pointer-sized integer.
125 .IP 2.
126 the start address of the procedure
127 .P2 "Load format"
128 .PP
129 The EM machine language load format defines the interface between
130 the EM assembler/loader and the EM machine itself.
131 A load file consists of a header, the program text to be executed,
132 a description of the global data area and the procedure descriptor table,
133 in this order.
134 All integers in the load file are presented with the
135 least significant byte first.
136 .PP
137 The header has two parts: the first half (eight 16-bit integers)
138 aids in selecting
139 the correct EM machine or interpreter.
140 Some EM machines, for instance, may have hardware floating point
141 instructions.
142 .N
143 The header entries are as follows (bit 0 is rightmost):
144 .IP 1:
145 magic number (07255)
146 .IP 2:
147 flag bits with the following meaning:
148 .RS
149 .IP "bit 0"
150 TEST; test for integer overflow etc.
151 .IP "bit 1"
152 PROFILE; for each source line: count the number of memory
153 cycles executed.
154 .IP "bit 2"
155 FLOW; for each source line: set a bit in a bit map table if
156 instructions on that line are executed.
157 .IP "bit 3"
158 COUNT; for each source line: increment a counter if that line
159 is entered.
160 .IP "bit 4"
161 REALS; set if a program uses floating point instructions.
162 .IP "bit 5"
163 EXTRA; more tests during compiler debugging.
164 .RE
165 .IP 3:
166 number of unresolved references.
167 .IP 4:
168 version number; used to detect obsolete EM load files.
169 .IP 5:
170 wordsize ; the number of bytes in each machine word.
171 .IP 6:
172 pointer size ; the number of bytes available for addressing.
173 .IP 7:
174 unused
175 .IP 8:
176 unused
177 .LP
178 The second part of the header (eight entries, of pointer size bytes each)
179 describes the load file itself:
180 .IP 1:
181 NTEXT; the program text size in bytes.
182 .IP 2:
183 NDATA; the number of load-file descriptors (see below).
184 .IP 3:
185 NPROC; the number of entries in the procedure descriptor table.
186 .IP 4:
187 ENTRY; procedure number of the procedure to start with.
188 .IP 5:
189 NLINE; the maximum source line number.
190 .IP 6:
191 SZDATA; the address of the lowest uninitialized data byte.
192 .IP 7:
193 unused
194 .IP 8:
195 unused
196 .PP
197 The program text consists of NTEXT bytes.
198 NTEXT is always a multiple of the wordsize.
199 The first byte of the program text is the
200 first byte of the instruction address
201 space, i.e. it has address 0.
202 Pointers into the program text are found in the procedure descriptor
203 table where relocation is simple and in the global data area.
204 The initialization of the global data area allows easy
205 relocation of pointers into both address spaces.
206 .PP
207 The global data area is described by the NDATA descriptors.
208 Each descriptor describes a number of consecutive words (of~wordsize)
209 and consists of a sequence of bytes.
210 While reading the descriptors from the load file, one can
211 initialize the global data area from low to high addresses.
212 The size of the initialized data area is given by SZDATA,
213 this number can be used to check the initialization.
214 .br
215 The header of each descriptor consists of a byte, describing the type,
216 and a count.
217 The number of bytes used for this (unsigned) count depends on the
218 type of the descriptor and
219 is either a pointer-sized integer
220 or one byte.
221 The meaning of the count depends on the descriptor type.
222 At load time an interpreter can
223 perform any conversion deemed necessary, such as
224 reordering bytes in integers
225 and pointers and adding base addresses to pointers.
226 .QQ
227 In the following pictures we show a graphical notation of the
228 initializers.
229 The leftmost rectangle represents the leading byte.
230 .LP
231 Fields marked with
232 .TS
233 tab(:);
234 l l.
235 n:contain a pointer-sized integer used as a count
236 m:contain a one-byte integer used as a count
237 b:contain a one-byte integer
238 w:contain a wordsized integer
239 p:contain a data or instruction pointer
240 s:contain a null terminated ASCII string
241 .TE
242 .Dr 6
243     -------------------
244     | 0 |      n      |           repeat last initialization n times
245     -------------------
246 .De
247 .Dr 4
248     ---------
249     | 1 | m |                     m uninitialized words
250     ---------
251 .De
252 .Dr 6
253                ____________
254               /    bytes   \e
255     -----------------   -----
256     | 2 | m | b | b |...| b |     m initialized bytes
257     -----------------   -----
258 .De
259 .Dr 6
260                _________
261               /  word   \e
262     -----------------------
263     | 3 | m |      w      |...    m initialized wordsized integers
264     -----------------------
265 .De
266 .Dr 6
267                _________
268               / pointer \e
269     -----------------------
270     | 4 | m |      p      |...    m initialized data pointers
271     -----------------------
272 .De
273 .Dr 6
274                _________
275               / pointer \e
276     -----------------------
277     | 5 | m |      p      |...    m initialized instruction pointers
278     -----------------------
279 .De
280 .Dr 6
281                ____________
282               /    bytes   \e
283     -------------------------
284     | 6 | m | b | b |...| b |     initialized integer of size m
285     -------------------------
286 .De
287 .Dr 6
288                ____________
289               /    bytes   \e
290     -------------------------
291     | 7 | m | b | b |...| b |     initialized unsigned of size m
292     -------------------------
293 .De
294 .Dr 6
295                ____________
296               /   string   \e
297     -------------------------
298     | 8 | m |        s      |     initialized float of size m
299     -------------------------
300 .De
301 .IP type~0: 10
302 If the last initialization initialized k bytes starting
303 at address \fIa\fP, do the same initialization again n times,
304 starting at \fIa\fP+k, \fIa\fP+2*k, .... \fIa\fP+n*k.
305 This is the only descriptor whose starting byte
306 is followed by an integer with the
307 size of a
308 pointer,
309 in all other descriptors the first byte is followed by a one-byte count.
310 This descriptor must be preceded by a descriptor of
311 another type.
312 .IP type~1: 10
313 Reserve m words, not explicitly initialized (BSS and HOL).
314 .IP type~2: 10
315 The m bytes following the descriptor header are
316 initializers for the next m bytes of the
317 global data area.
318 m is divisible by the wordsize.
319 .IP type~3: 10
320 The m words following the header are initializers for the next m words of the
321 global data area.
322 .IP type~4: 10
323 The m data address space pointers following the header are
324 initializers for the next
325 m data pointers in the global data area.
326 Interpreters that represent EM pointers by
327 target machine addresses must relocate all data pointers.
328 .IP type~5: 10
329 The m instruction address space pointers following the header are
330 initializers for the next
331 m instruction pointers in the global data area.
332 Interpreters that represent EM instruction pointers by
333 target machine addresses must relocate these pointers.
334 .IP type~6: 10
335 The m bytes following the header form
336 a signed integer number with a size of m bytes,
337 which is an initializer for the next m bytes
338 of the global data area.
339 m is governed by the same restrictions as for
340 transfer of objects to/from memory.
341 .IP type~7: 10
342 The m bytes following the header form
343 an unsigned integer number with a size of m bytes,
344 which is an initializer for the next m bytes
345 of the global data area.
346 m is governed by the same restrictions as for
347 transfer of objects to/from memory.
348 .IP type~8: 10
349 The header is followed by an ASCII string, null terminated, to
350 initialize, in global data,
351 a floating point number with a size of m bytes.
352 m is governed by the same restrictions as for
353 transfer of objects to/from memory.
354 The ASCII string contains the notation of a real as used in the
355 Pascal language.
356 .PP
357 The NPROC procedure descriptors on the load file consist of
358 an instruction space address (of~pointer~size) and
359 an integer (of~pointer~size) specifying the number of bytes for
360 locals.