forked from jnz/q3vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuake 3 Virtual Machine (Q3VM) Specifications.html
473 lines (426 loc) · 26.3 KB
/
Quake 3 Virtual Machine (Q3VM) Specifications.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- saved from url=(0053)http://www.icculus.org/~phaethon/q3mc/q3vm_specs.html -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>
Quake 3 Virtual Machine (Q3VM) Specifications
</title>
</head>
<body>
<h1>Quake 3 Virtual Machine (Q3VM) Specifications</h1>
<h2>Synopsis</h2><p>A description of the Q3VM architecture and operations.</p>
<a name="intro"></a><h3>Introduction</h3>
<p>
The Q3VM is a virtual machine used by Quake III to run the game module.
The VM is a sort of sandbox to limit the damage a rogue or malicious QVM program can wreak.
Though not perfect, it certainly is much safer than full access to native machine language, which could otherwise more easily allow the spread of viruses or the corruption of system resources.
In addition, Q3VM programs, similar to JVM's, are write-once run-on-many-platforms (at least for which a Q3VM interpreter or compiler exists).
</p><p>
The instruction set for the Q3VM is derived from the bytecode interpreter target of LCC, with minor differences.
The VM heavily relies on stack operations (stack-based machine).
<a name="memory"></a></p><h3>Memory</h3>
<p>
Memory bus of little-endian, unsigned 32 bits, flat memory space, zero-based addressing, octet-accessible.
</p><p>
Memory is accessible in words of 4 octets (32 bits), 2 octets (16 bits), and single octect (8 bits).
</p><p>
Code and data occupy distinct address spaces.
Thus code address 0 is not the same as data address 0.
This separation also precludes self-modifying code.
</p><h4>Code segment</h4>
<p>
Q3VM is CISC-like in the sense that opcodes do not occupy equal amounts of space.
Some opcodes take a parameter embedded in the code segment.
As a result, the length of the code segment has no bearing on the number of instructions in the code segment.
The QVM file format header manifests this distinction.
</p><p>
Jump addresses are specified in terms of instructions, rather than bytes.
Thus, the VM code memory requires special processing when loading the code segment to identify instruction offsets.
<a name="processor"></a></p><h3>Processor/CPU</h3>
<p>
While the Q3VM is currently implemented only in emulation, the concept of a VM's CPU is a convenient notion.
</p><h4>Registers</h4>
<p>
Theses are guesses derived from modern machines, other fictional machines, and Forth.
</p><ul>
<li>Program Counter (PC)</li>
<li>Stack Pointer (SP) ("the stack")</li>
<li>Local Pointer (LP) ("start of local variable space")</li>
<li>Frame Pointer (FP) ("start of subroutine stack space")</li>
</ul>
<h4>Opcodes</h4>
<p>
List of opcodes recognized by the Q3VM.
All opcodes occupy one octet, and take 1 or 4 additional octets as parameters.
All multi-octet values are treated as little-endian (a legacy of Id's MS-DOS days).
</p><p>
Opcode mnemonics ("names") follow a pattern (intriguingly, the opcode values don't, as they do on other architectures).
Up to four letters for the operation name, then a letter indicating a data type, then a digit indicating data size (usually the number of required octets).
</p><p>
Data type and size codes:
</p><dl>
<dt><a name="processor.B">B</a></dt><dd>block (sequence of octets, of arbitrary length (e.g. character strings or structs))</dd>
<dt><a name="processor.F4">F4</a></dt><dd>little-endian IEEE-754 32-bit single-precision floating point value.</dd>
<dt><a name="processor.P4">P4</a></dt><dd>Four-octet pointer (memory address) value.</dd>
<dt><a name="processor.I4">I4</a></dt><dd>Four-octet signed integer. Corresponds to Q3VM's C data type "signed int".</dd>
<dt><a name="processor.I2">I2</a></dt><dd>Two-octet signed integer. Corresponds to Q3VM's C data type "signed short".</dd>
<dt><a name="processor.I1">I1</a></dt><dd>One-octet signed integer. Corresponds to Q3VM's C data type "signed char".</dd>
<dt><a name="processor.U4">U4</a></dt><dd>Four-octet unsigned integer. Corresponds to Q3VM's C data type "unsigned int".</dd>
<dt><a name="processor.U2">U2</a></dt><dd>Two-octet unsigned integer. Corresponds to Q3VM's C data type "unsigned short".</dd>
<dt><a name="processor.U1">U1</a></dt><dd>One-octet unsigned integer. Corresponds to Q3VM's C data type "unsigned char".</dd>
</dl>
<p>
Generalized opcodes list (lcc bytecode):
</p><dl>
<dt><a name="processor.CNST<em>t</em> <em>v</em>">CNST<em>t</em> <em>v</em></a></dt><dd>
CONSTANT.
Reads <em>v</em> as a value of type <em>t</em>.
For floats, <em>v</em> is the bit pattern of the float value.
</dd>
<dt><a name="processor.ASGN<em>t</em>">ASGN<em>t</em></a></dt><dd>
ASSIGN.
Pops a value from stack, interpreted according to <em>t</em>, to use as the assignment value.
Pops another value from stack, interpreted as a pointer value (memory address) to use as the assignment destination.
</dd>
<dt><a name="processor.INDIR<em>t</em>">INDIR<em>t</em></a></dt><dd>
INDIRECTION.
Pops a value from stack, interpreted as a pointer value (memory address).
Retrieves the 32-bit value from the indicated memory location, and interprets the 32-bit value according to <em>t</em>, then pushes to stack.
(think of it as pointer dereferencing)
</dd>
<dt><a name="processor.CVF<em>t</em>">CVF<em>t</em></a></dt><dd>
CONVERT TO FLOAT.
Pops a value from stack, interpreted according to <em>t</em>, converted to its equivalent float (F4) form, then pushed to stack.
</dd>
<dt><a name="processor.CVI<em>t</em>">CVI<em>t</em></a></dt><dd>
CONVERT TO (signed) INTEGER.
Pops a value from stack, interpreted according to <em>t</em>, converted to its equivalent signed integer (I4) form, then pushed to stack.
</dd>
<dt><a name="processor.CVU<em>t</em>">CVU<em>t</em></a></dt><dd>
CONVERT TO UNSIGNED INTEGER.
Pops a value from stack, interpreted according to <em>t</em>, converted to its equivalent unsigned integer (U4) form, then pushed to stack.
</dd>
<dt><a name="processor.CVP<em>t</em>">CVP<em>t</em></a></dt><dd>
CONVERT TO UNSIGNED INTEGER.
Pops a value from stack, interpreted according to <em>t</em>, converted to its equivalent pointer (P4) form, then pushed to stack.
</dd>
<dt><a name="processor.NEG<em>t</em>">NEG<em>t</em></a></dt><dd>
ARITHMETIC NEGATE.
Pops a value from stack, interpreted according to <em>t</em>, negated arithmeticaly (multiplied by -1 or -1.0), then pushed to stack.
</dd>
<dt><a name="processor.ADDRGP4 <em>v</em>">ADDRGP4 <em>v</em></a></dt><dd>
ADDRESS OF GLOBAL (pointer).
Takes <em>v</em> as a memory address, takes the 32-bit value at the address, pushes the 32-bit value to stack (i.e. get value of a global variable/function/symbol).
</dd>
<dt><a name="processor.ADDRLP4 <em>v</em>">ADDRLP4 <em>v</em></a></dt><dd>
ADDRESS OF (from?) LOCAL (pointer).
Address of a local variable.
Local Pointer(?) plus <em>v</em>.
First local variable is at 0.
</dd>
<dt><a name="processor.ADDRFP4 <em>v</em>">ADDRFP4 <em>v</em></a></dt><dd>
ADDRESS (from?) FRAME (pointer).
Address of an argument (with repect to the frame pointer).
Frame Pointer plus (minus?) <em>v</em>.
First argument is at 0, second argument at 4, etc (XXX: verify).
</dd>
<dt><a name="processor.ADD<em>t</em>">ADD<em>t</em></a></dt><dd>
ADD.
Pops a value from stack, interpreted according to <em>t</em>, as second operand.
Pops another value from stack, interpreted <em>t</em>, as first operand.
The two values are arithmeticaly added, pushes sum to stack.
</dd>
<dt><a name="processor.SUB<em>t</em>">SUB<em>t</em></a></dt><dd>
SUBTRACT.
Pops a value from stack, interpreted <em>t</em>, as second operand.
Pops another value from stack, interpreted <em>t</em>, as first operand.
Subtracts second operand from first operand, pushes difference to stack.
</dd>
<dt><a name="processor.MUL<em>t</em>">MUL<em>t</em></a></dt><dd>
MULTIPLY.
Pops a value from stack, interpreted <em>t</em>, as second operand.
Pops another value from stack, interpreted <em>t</em>, as first operand.
Multiplies the values together, pushes product to stack.
</dd>
<dt><a name="processor.DIV<em>t</em>">DIV<em>t</em></a></dt><dd>
DIVIDE.
Pops a value from stack, interpreted <em>t</em>, as second operand.
Pops another value from stack, interpreted <em>t</em>, as first operand.
Second operand divides into first operand, pushes quotient to stack
(XXX: integer division C style?).
</dd>
<dt><a name="processor.MOD<em>t</em>">MOD<em>t</em></a></dt><dd>
MODULUS.
Pops a value from stack, as <em>t</em>, as second operand.
Pops another value from stack, as <em>t</em>, as first operand.
The second operand divides into the first operand, pushes remainder to stack.
</dd>
<dt><a name="processor.LSH<em>t</em>">LSH<em>t</em></a></dt><dd>
LEFT SHIFT.
Pops a value from stack, as <em>t</em>, as second operand.
Pops another value from stack, as <em>t</em>, as first operand.
First operand bit-wise shifts left by number of bits specified by second operand, pushes result to stack.
</dd>
<dt><a name="processor.RSH<em>t</em>">RSH<em>t</em></a></dt><dd>
RIGHT SHIFT.
Pops a value from stack, as <em>t</em>, as second operand.
Pops another value from stack, as <em>t</em>, as first operand.
First operand bit-wise shifts right by number of bits specified by second operand, pushes result to stack.
Sign-extension depends on <em>t</em>.
</dd>
<dt><a name="processor.BAND<em>t</em>">BAND<em>t</em></a></dt><dd>
BINARY/BITWISE AND.
Pops a value from stack (<em>t</em>) as second operand.
Pops another value from stack (<em>t</em>) as first operand.
The two operands combine by bitwise AND, pushes result to stack.
</dd>
<dt><a name="processor.BCOM<em>t</em>">BCOM<em>t</em></a></dt><dd>
BINARY/BITWISE COMPLEMENT.
Pops a value from stack (<em>t</em>).
Flips (almost-)every bit to its opposite value.
Changing the high bit (bit 31) depends on <em>t</em>.
</dd>
<dt><a name="processor.BOR<em>t</em>">BOR<em>t</em></a></dt><dd>
BINARY/BITWISE OR.
Pops a value from stack (<em>t</em>) as second operand.
Pops another value from stack (<em>t</em>) as first operand.
Combines the two operands by bitwise OR, pushes result to stack.
</dd>
<dt><a name="processor.BXOR<em>t</em>">BXOR<em>t</em></a></dt><dd>
BINARY/BITWISE XOR.
Pops two values, XORs, pushes result to stack.
</dd>
<dt><a name="processor.EQ<em>t</em> <em>v</em>">EQ<em>t</em> <em>v</em></a></dt><dd>
EQUAL-TO.
Pops two values from stack (as type <em>t</em>), compares the two values.
Jumps to address <em>v</em> if true.
</dd>
<dt><a name="processor.GE<em>t</em> <em>v</em>">GE<em>t</em> <em>v</em></a></dt><dd>
GREATER-THAN OR EQUAL-TO.
Pops a value from stack (type <em>t</em>) as second operand.
Pops another value from stack (type <em>t</em>) as first operand.
Compares if first operand is equal to the second operand.
Jumps to address <em>v</em> if true.
</dd>
<dt><a name="processor.GT<em>t</em> <em>v</em>">GT<em>t</em> <em>v</em></a></dt><dd>
GREATER-THAN.
Pops a second operand (type <em>t</em>) then a first operand (type <em>t</em>).
Compares if first operand is greater than the second operand.
Jumps to address <em>v</em> if true.
</dd>
<dt><a name="processor.LE<em>t</em> <em>v</em>">LE<em>t</em> <em>v</em></a></dt><dd>
LESS-THAN OR EQUAL-TO.
Pops a second operand (type <em>t</em>) then a first operand (type <em>t</em>).
Compares if first operand is less than or equal to the second operand.
Jumps to address <em>v</em> if true.
</dd>
<dt><a name="processor.LT<em>t</em> <em>v</em>">LT<em>t</em> <em>v</em></a></dt><dd>
LESS-THAN.
Pops a second operand (<em>t</em>) then a first operand (<em>t</em>).
Compares if first operand is less than the second operand.
Jumps to address <em>v</em> if true.
</dd>
<dt><a name="processor.NE<em>t</em> <em>v</em>">NE<em>t</em> <em>v</em></a></dt><dd>
NOT-EQUAL.
Pops two values (<em>t</em>), compares the two values.
Compares if first operand is not equal to the second operand.
Jumps to address <em>v</em> if true.
</dd>
<dt><a name="processor.JUMPV">JUMPV</a></dt><dd>
Pops a pointer value from stack, sets PC to the value (jump).
</dd>
<dt><a name="processor.RET<em>t</em>">RET<em>t</em></a></dt><dd>
Pop value from stack,
shrink stack to eliminate local frame,
push value back to stack.
Or copy top of stack to bottom of frame and shrink stack size/pointer?
</dd>
<dt><a name="processor.ARG<em>t</em>">ARG<em>t</em></a></dt><dd>
Pop value from stack as type <em>t</em>,
store into arguments-marshalling space.
</dd>
<dt><a name="processor.CALL<em>t</em>">CALL<em>t</em></a></dt><dd>
Pops value from stack as address of a function.
Makes a procedure call to the function, which is expected to return a value of type <em>t</em>.
</dd>
<dt><a name="processor.pop">pop</a></dt><dd>
Pop stack (discard top of stack).
(functions of void type still return a (nonsense) 32-bit value)
</dd>
</dl>
<p>
List of assembly directives:
</p><dl>
<dt><a name="processor.equ <em>s</em> <em>v</em>">equ <em>s</em> <em>v</em></a></dt><dd>
Assign integer value <em>v</em> to symbol <em>s</em>.
</dd>
<dt><a name="processor.data">data</a></dt><dd>
Assemble to DATA segment (initialized 32-bit variables).
</dd>
<dt><a name="processor.code">code</a></dt><dd>
Assemble to CODE segment (instructions).
</dd>
<dt><a name="processor.lit">lit</a></dt><dd>
Assemble to LIT segment (initialized 8-bit variables).
</dd>
<dt><a name="processor.bss">bss</a></dt><dd>
Assemble to BSS segment (uninitialized block storage segment).
</dd>
<dt><a name="processor.LABELV <em>symbol</em>">LABELV <em>symbol</em></a></dt><dd>
Assigns the value of the currently computed Program Counter to the symbol named <em>symbol</em>.
(i.e. the current assembled address value is assigned to <em>symbol</em>).
</dd>
<dt><a name="processor.byte <em>l</em> <em>v</em>">byte <em>l</em> <em>v</em></a></dt><dd>
Write initialized value <em>v</em> of <em>l</em> octets.
1-octet values go into LIT segment.
2-octet values are invalid (fatal error).
4-octet values go into DATA segment.
</dd>
<dt><a name="processor.skip <em>v</em>">skip <em>v</em></a></dt><dd>
Skip <em>v</em> octets in the segment, leaving the octets uninitialized.
</dd>
<dt><a name="processor.export <em>s</em>">export <em>s</em></a></dt><dd>
Export symbol <em>s</em> for external linkage.
</dd>
<dt><a name="processor.import <em>s</em>">import <em>s</em></a></dt><dd>
Import symbol <em>s</em>.
</dd>
<dt><a name="processor.align <em>v</em>">align <em>v</em></a></dt><dd>
Ensure memory alignment at a multiple of <em>v</em>, skipping octets if needed.
</dd>
<dt><a name="processor.address <em>x</em>">address <em>x</em></a></dt><dd>
???
(evaluates expression <em>x</em> and append result to DATA segment)
</dd>
<dt><a name="processor.file <em>filename</em>">file <em>filename</em></a></dt><dd>
Set filename to <em>filename</em> for status and error reporting.
</dd>
<dt><a name="processor.line <em>lineno</em>">line <em>lineno</em></a></dt><dd>
Indicates current source line of <em>lineno</em>.
</dd>
<dt><a name="processor.proc <em>locals</em> <em>args</em>">proc <em>locals</em> <em>args</em></a></dt><dd>
Start of procedure (function) body.
Set aside <em>locals</em> octets for local variables, and <em>args</em> octets for arguments marshalling (for all possible subcalls within this procedure).
</dd>
<dt><a name="processor.endproc <em>locals</em> <em>args</em>">endproc <em>locals</em> <em>args</em></a></dt><dd>
End of procedure body.
Clean up <em>args</em> bytes (for arguments marshalling) and <em>locals</em> bytes (for local variables).
</dd>
</dl>
<h4>Q3VM Bytecodes</h4>
<p>
Specific opcodes (Q3VM bytecode):
(TOS = Top Of Stack; NIS = Next In Stack (next value after TOS))
(Hack syntax: $PARM = code parameter)
</p><table border="1">
<tbody><tr>
<th>Byte</th><th>Opcode</th><th>Parameter size</th><th>Description</th>
</tr>
<tr><td>0 </td><td> UNDEF </td><td align="CENTER"> - </td><td> undefined opcode.</td></tr>
<tr><td>1 </td><td> IGNORE </td><td align="CENTER"> - </td><td> no-operation (nop) instruction.</td></tr>
<tr><td>2 </td><td> BREAK </td><td align="CENTER"> - </td><td> ??</td></tr>
<tr><td>3 </td><td> ENTER </td><td align="CENTER"> 4 </td><td> Begin procedure body, adjust stack $PARM octets for frame (always at least 8 (i.e. 2 words)). Frame contains all local storage/variables and arguments space for any calls within this procedure.</td></tr>
<tr><td>4 </td><td> LEAVE </td><td align="CENTER"> 4 </td><td> End procedure body, $PARM is same as that of the matching ENTER.</td></tr>
<tr><td>5 </td><td> CALL </td><td align="CENTER"> - </td><td> make call to procedure (code address <- TOS).</td></tr>
<tr><td>6 </td><td> PUSH </td><td align="CENTER"> - </td><td> push nonsense (void) value to opstack (TOS <- 0).</td></tr>
<tr><td>7 </td><td> POP </td><td align="CENTER"> - </td><td> pop a value from stack (remove TOS, decrease stack by 1).</td></tr>
<tr><td>8 </td><td> CONST </td><td align="CENTER"> 4 </td><td> push literal value onto stack (TOS <- $PARM)</td></tr>
<tr><td>9 </td><td> LOCAL </td><td align="CENTER"> 4 </td><td> get address of local storage (local variable or argument) (TOS <- (frame + $PARM)).</td></tr>
<tr><td>10 </td><td> JUMP </td><td align="CENTER"> - </td><td> branch (code address <- TOS)</td></tr>
<tr><td>11 </td><td> EQ </td><td align="CENTER"> 4 </td><td> check equality (signed integer) (compares NIS vs TOS, jump to $PARM if true).</td></tr>
<tr><td>12 </td><td> NE </td><td align="CENTER"> 4 </td><td> check inequality (signed integer) (NIS vs TOS, jump to $PARM if true).</td></tr>
<tr><td>13 </td><td> LTI </td><td align="CENTER"> 4 </td><td> check less-than (signed integer) (NIS vs TOS, jump to $PARM if true).</td></tr>
<tr><td>14 </td><td> LEI </td><td align="CENTER"> 4 </td><td> check less-than or equal-to (signed integer) (NIS vs TOS, jump to $PARM if true).</td></tr>
<tr><td>15 </td><td> GTI </td><td align="CENTER"> 4 </td><td> check greater-than (signed integer) (NIS vs TOS), jump to $PARM if true.</td></tr>
<tr><td>16 </td><td> GEI </td><td align="CENTER"> 4 </td><td> check greater-than or equal-to (signed integer) (NIS vs TOS), jump to $PARM if true.</td></tr>
<tr><td>17 </td><td> LTU </td><td align="CENTER"> 4 </td><td> check less-than (unsigned integer) (NIS vs TOS), jump to $PARM if true.</td></tr>
<tr><td>18 </td><td> LEU </td><td align="CENTER"> 4 </td><td> check less-than or equal-to (unsigned integer) (NIS vs TOS), jump to $PARM if true.</td></tr>
<tr><td>19 </td><td> GTU </td><td align="CENTER"> 4 </td><td> check greater-than (unsigned integer) (NIS vs TOS), jump to $PARM if true.</td></tr>
<tr><td>20 </td><td> GEU </td><td align="CENTER"> 4 </td><td> check greater-than or equal-to (unsigned integer) (NIS vs TOS), jump to $PARM if true.</td></tr>
<tr><td>21 </td><td> EQF </td><td align="CENTER"> 4 </td><td> check equality (float) (NIS vs TOS, jump to $PARM if true).</td></tr>
<tr><td>22 </td><td> NEF </td><td align="CENTER"> 4 </td><td> check inequality (float) (NIS vs TOS, jump to $PARM if true).</td></tr>
<tr><td>23 </td><td> LTF </td><td align="CENTER"> 4 </td><td> check less-than (float) (NIS vs TOS, jump to $PARM if true).</td></tr>
<tr><td>24 </td><td> LEF </td><td align="CENTER"> 4 </td><td> check less-than or equal-to (float) (NIS vs TOS, jump to $PARM if true).</td></tr>
<tr><td>25 </td><td> GTF </td><td align="CENTER"> 4 </td><td> check greater-than (float) (NIS vs TOS, jump to $PARM if true).</td></tr>
<tr><td>26 </td><td> GEF </td><td align="CENTER"> 4 </td><td> check greater-than or equal-to (float) (NIS vs TOS, jump to $PARM if true).</td></tr>
<tr><td>27 </td><td> LOAD1 </td><td align="CENTER"> - </td><td> Load 1-octet value from address in TOS (TOS <- [TOS])</td></tr>
<tr><td>28 </td><td> LOAD2 </td><td align="CENTER"> - </td><td> Load 2-octet value from address in TOS (TOS <- [TOS])</td></tr>
<tr><td>29 </td><td> LOAD4 </td><td align="CENTER"> - </td><td> Load 4-octet value from address in TOS (TOS <- [TOS])</td></tr>
<tr><td>30 </td><td> STORE1 </td><td align="CENTER"> - </td><td> lowest octet of TOS is 1-octet value to store, destination address in next-in-stack ([NIS] <- TOS).</td></tr>
<tr><td>31 </td><td> STORE2 </td><td align="CENTER"> - </td><td> lowest two octets of TOS is 2-octet value to store, destination address in next-in-stack ([NIS] <- TOS).</td></tr>
<tr><td>32 </td><td> STORE4 </td><td align="CENTER"> - </td><td> TOS is 4-octet value to store, destination address in next-in-stack ([NIS] <- TOS).</td></tr>
<tr><td>33 </td><td> ARG </td><td align="CENTER"> 1 </td><td> TOS is 4-octet value to store into arguments-marshalling space of the indicated octet offset (ARGS[offset] <- TOS).</td></tr>
<tr><td>34 </td><td> BLOCK_COPY </td><td align="CENTER"> - </td><td> ???</td></tr>
<tr><td>35 </td><td> SEX8 </td><td align="CENTER"> - </td><td> Sign-extend 8-bit (TOS <- TOS).</td></tr>
<tr><td>36 </td><td> SEX16 </td><td align="CENTER"> - </td><td> Sign-extend 16-bit (TOS <- TOS).</td></tr>
<tr><td>37 </td><td> NEGI </td><td align="CENTER"> - </td><td> Negate signed integer (TOS <- -TOS).</td></tr>
<tr><td>38 </td><td> ADD </td><td align="CENTER"> - </td><td> Add integer-wise (TOS <- NIS + TOS).</td></tr>
<tr><td>39 </td><td> SUB </td><td align="CENTER"> - </td><td> Subtract integer-wise (TOS <- NIS - TOS).</td></tr>
<tr><td>40 </td><td> DIVI </td><td align="CENTER"> - </td><td> Divide integer-wise (TOS <- NIS / TOS).</td></tr>
<tr><td>41 </td><td> DIVU </td><td align="CENTER"> - </td><td> Divide unsigned integer (TOS <- NIS / TOS).</td></tr>
<tr><td>42 </td><td> MODI </td><td align="CENTER"> - </td><td> Modulo (signed integer) (TOS <- NIS mod TOS).</td></tr>
<tr><td>43 </td><td> MODU </td><td align="CENTER"> - </td><td> Modulo (unsigned integer) (TOS <- NIS mod TOS).</td></tr>
<tr><td>44 </td><td> MULI </td><td align="CENTER"> - </td><td> Multiply (signed integer) (TOS <- NIS * TOS).</td></tr>
<tr><td>45 </td><td> MULU </td><td align="CENTER"> - </td><td> Multiply (unsigned integer) (TOS <- NIS * TOS).</td></tr>
<tr><td>46 </td><td> BAND </td><td align="CENTER"> - </td><td> Bitwise AND (TOS <- NIS & TOS).</td></tr>
<tr><td>47 </td><td> BOR </td><td align="CENTER"> - </td><td> Bitwise OR (TOS <- NIS | TOS).</td></tr>
<tr><td>48 </td><td> BXOR </td><td align="CENTER"> - </td><td> Bitwise XOR (TOS <- NIS ^ TOS).</td></tr>
<tr><td>49 </td><td> BCOM </td><td align="CENTER"> - </td><td> Bitwise complement (TOS <- ~TOS).</td></tr>
<tr><td>50 </td><td> LSH </td><td align="CENTER"> - </td><td> Bitwise left-shift (TOS <- NIS << TOS).</td></tr>
<tr><td>51 </td><td> RSHI </td><td align="CENTER"> - </td><td> Algebraic (signed) right-shift (TOS <- NIS >> TOS).</td></tr>
<tr><td>52 </td><td> RSHU </td><td align="CENTER"> - </td><td> Bitwise (unsigned) right-shift (TOS <- NIS >> TOS).</td></tr>
<tr><td>53 </td><td> NEGF </td><td align="CENTER"> - </td><td> Negate float value (TOS <- -TOS).</td></tr>
<tr><td>54 </td><td> ADDF </td><td align="CENTER"> - </td><td> Add floats (TOS <- NIS + TOS).</td></tr>
<tr><td>55 </td><td> SUBF </td><td align="CENTER"> - </td><td> Subtract floats (TOS <- NIS - TOS).</td></tr>
<tr><td>56 </td><td> DIVF </td><td align="CENTER"> - </td><td> Divide floats (TOS <- NIS / TOS).</td></tr>
<tr><td>57 </td><td> MULF </td><td align="CENTER"> - </td><td> Multiply floats (TOS <- NIS x TOS).</td></tr>
<tr><td>58 </td><td> CVIF </td><td align="CENTER"> - </td><td> Convert signed integer to float (TOS <- TOS).</td></tr>
<tr><td>59 </td><td> CVFI </td><td align="CENTER"> - </td><td> Convert float to signed integer (TOS <- TOS).</td></tr>
</tbody></table>
<a name="qvmfile"></a><h3>QVM File Format</h3>
<p>
QVM file format.
Multi-octet words are little-endian (LE).
</p><table border="1">
<tbody><tr><td colspan="3">QVM header</td></tr>
<tr><td align="RIGHT">offset</td><td align="CENTER">size</td><td>description</td></tr>
<tr><td align="RIGHT">0</td><td align="CENTER">4</td><td>magic (0x12721444 LE, 0x44147212 BE)</td></tr>
<tr><td align="RIGHT">4</td><td align="CENTER">4</td><td>instruction count</td></tr>
<tr><td align="RIGHT">8</td><td align="CENTER">4</td><td>length of CODE segment</td></tr>
<tr><td align="RIGHT">12</td><td align="CENTER">4</td><td>offset of CODE segment</td></tr>
<tr><td align="RIGHT">16</td><td align="CENTER">4</td><td>lenth of DATA segment</td></tr>
<tr><td align="RIGHT">20</td><td align="CENTER">4</td><td>offset of DATA segment</td></tr>
<tr><td align="RIGHT">24</td><td align="CENTER">4</td><td>length of LIT segment</td></tr>
<tr><td align="RIGHT">28</td><td align="CENTER">4</td><td>length of BSS segment</td></tr>
</tbody></table>
<p>
Following the header is the code segment, aligned on a 4-byte boundary.
The code segment can be processed octet-by-octet to identify instructions.
For bytecodes that take a multi-octet parameter, the parameter is stored in LE order.
Big-endian machines may want to byte-swap the parameter for native-arithmetic purposes.
The instruction count field in the header identifies when the sequence of instructions end.
Comparisons of code length, instruction counts, and file positions can be used to verify proper processing.
</p><p>
The data segment follows the code segment.
The assembler (qvm generator) needs to ensure the data segment begins on a 4-octet boundary (by padding with zeroes).
The data segment consists of a series of 4-octet LE values that are copied into VM memory.
For big-endian machines, each word may be optionally byte-swapped (for native arithmetic) on copy.
</p><p>
</p><p>
The lit segment immediately follows the data segment.
This segment should already be aligned on a 4-octet boundary.
The lit segment consists of a series of 1-octet values, copied verbatim to VM memory.
No byte-swapping is needed.
</p><p>
The bss segment is not stored in the qvm file, since this segment consists of unitialized values (i.e. values that don't need to be stored).
The VM must ensure there is still sufficient memory to accomodate the bss segment.
The QVM header specifies the size of the bss segment.
The VM may optionally initialize the bss portion of memory with zeroes.
</p><hr id="footer" width="42%" align="left">
<p>
Updated 2003.02.23 <br>
Updated 2011.07.11 - change of contact e-mail <br>
</p><address>
PhaethonH
< [email protected] >
</address>
</body></html>