-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstructions.txt
193 lines (153 loc) · 8.73 KB
/
instructions.txt
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
One-byte opcodes, followed by whatever arguments the system expects
- STR String arguments are encoded in the obvious way, concluded by a null terminator
- INT Integer arguments are encoded using four bytes, in little endian byte order
(begins with a single "sign byte")
- REG Registers have their own indices and occupy one byte for storage
- SPC "Special" instruction arguments
* (1 byte n) (n bytes L) (L bytes content)
== ObjectPtr ==
%ptr (01) - Working register, holds an ObjectPtr
%slf (02) - The self ObjectPtr, used for method calls and lookups
%ret (03) - The return value register, holds an ObjectPtr
== Stack of ObjectPtr ==
%lex (04) - A stack of lexical scopes
%dyn (05) - A stack of dynamic scopes
%arg (06) - The current argument list (the bottom of the stack is $1)
%sto (07) - General-purpose storage stack for intermediate computations
== Continuation ==
%cont (08) - The current continuation (a sequence of instructions)
%stack (09) - The continuation stack
== Etc ==
%err0 (0A) - A boolean value that gets set by some computations if they err
%err1 (0B) - Can be swapped with %err0 for storage of error status
%sym (0C) - A symbol
%num0 (0D) - A number
%num1 (0E) - Another number
%str0 (0F) - A string
%str1 (10) - Another string
%mthd (11) - A method (sequence of instructions)
%cpp (12) - A table of C++ function calls, indexed by integers
%strm (13) - A stream pointer
%prcs (14) - A process pointer
%mthdz (15) - An extra method
%flag (16) - An arbitrary boolean value
%wind (17) - A stack of "wind frames" (thunk pairs)
== Stack of ObjectPtr ==
%hand (18) - A stack of exception handlers
== Position ==
%line (19) - The current line number, as a long (NOT an instance of the Number class)
%file (1A) - The current file name, as a string
%trace (1B) - A stack of number-name pairs
== Etc ==
%trns (1C) - A stack of translation units
%lit (1D) - A table of literal value objects, indexed by integers
%gtu (1E) - A single designated global translation unit object
mov SRC DEST (01 REG REG) - For ObjectPtr registers only, move from src to dest
push PTR STACK (02 REG REG) - Push ObjectPtr onto ObjectPtr stack
pop DEST STACK (03 REG REG) - Pop from stack into register, sets %err0 if empty
getl DEST (04 REG) - Get the top of %lex without popping, sets %err0 if empty
getd DEST (05 REG) - Get the top of %dyn without popping, sets %err0 if empty
eswap (06) - Swap %err0 and %err1
eclr (07) - Clear %err0
eset (08) - Set %err0
sym STRING (09 STR) - Intern the string in the global symbol table and put its symbolic index in %sym
num STRING (0A STR)- Read the string as a big integer into %num0
int INT (0B INT) - Put the small integer into %num0
float STRING (0C STR) - Read the string into a double into %num0
nswap (0D) - Swap %num0 and %num1
(All of the call-ish instructions call method %ptr on %slf, putting end result in %ret)
call INT (0E INT) - (See below)
xcall (0F) - (See below)
xcall0 INT (10 INT) - (See below)
ret (11) - Pop %lex and %dyn, discarding both values, sets %err0 if either is empty, also pops %trace
clone (12) - Perform the primitive clone operation on %slf, put result in %ret
rtrv (13) - Lookup the name %sym on %slf and put the value at %ret (may incur a call if `missing` is used)
rtrvd (14) - Retrieve directly; do not look into the parents or `missing` (sets %err0 if not found)
str STRING (15 STR) - Put the string into %str0
sswap (16) - Swap %str0 and %str1
expd (17 REG) - Try to put %ptr's prim() into the register given, %err0 if not possible
mthd (18 SPC) - Put the argument into %mthd
load (19 REG) - Inverse of expd, try to put register into %ptr's prim(), %err0 if invalid register
setf (1A) - Put %ptr into %slf at the %sym slot, sets %err0 if %slf is null
peek DEST STACK (1B REG REG) - Like pop but don't remove the element; sets %err0 if empty
symn INT (1C INT) - Like sym except grab the symbol with the given index
cpp INT (1D INT) - Call the system function at the specified index in %cpp, set %err0 if invalid
bol (1E) - Put `meta True` or `meta False` into %ret, depending on %flag (Boolean instruction)
test (1F) - Compare %slf and %ptr for equality, store result in %flag
branch (20) - Push %cont on %stack, then move %mthd (if true) or %mthdz (if false) to %cont based on %flag
(Note that the continuation jump instructions, cgoto and cret, resolve the %wind stack as well)
ccall (21) - Store the current continuation in %slf, then push %slf onto %arg and do "call 1L" (uses %ptr)
cgoto (22) - Restore the continuation from %ptr, resolving %wind in the process
cret (23) - Restore continuation from %ptr, store %ret on %sto, resolve %wind, then restore %ret (%slf / %ptr are undefined)
wnd (24) - Push %slf and %ptr (as before and after) onto %wind, sets %err0 if either is non-method
unwnd (25) - Pop %wind and discard the resulting frame
throw (26) - Using %slf as the exception, call all of %hand in sequence (ignoring non-methods), then terminate
throq (27) - Perform the throw instruction if %err0 is true, otherwise do nothing
adds (28) - Take %str0 and append %str1 to it
arith OP (29 INT) - Perform an operation on %num0 and %num1, storing result in %num0, sets %err0 if invalid arg
throa MSG (2A STR) - Throw on attribute, construct a generic `meta TypeError` and throw it if %err0 is true
locfn STR (2B STR) - Set the filename
locln INT (2C INT) - Set the line number
locrt (2D) - Retrieve the stack trace, storing a StackFrame object in %ret
nret (2E) - Identical to `ret` but does not touch %trace
untr (2F) - Pop %trns once, discarding the result
cmplx RL IM (30 STR STR) - Construct a complex number into %num0
yld INT REG (31 INT REG) - Get the nth value from %lit and put it into the register, set %err0 if invalid
yldc INT REG (32 INT REG) - Get the nth value from %lit and put a clone into the register, set %err0 if invalid
del (33) - Delete the %sym slot of %slf, sets %err0 if %slf is null
arr INT (34 INT) - Construct an array by drawing the top INT values off %arg and placing them in the array so that the topmost value in %arg is at the end of the array. The resulting array is placed in %ret.
dict INT (35 INT) - Construct a dictionary by drawing the top INT * 2 values off %arg. Each time, the first argument popped is the value and the second (a symbol) is the key. The resulting dictionary is placed in %ret.
xxx INT (36 INT) - This instruction is used for several purposes, depending on the argument. See below.
goto (37) - Pushes %cont onto %stack then %mthd onto %cont, unconditionally.
mswap (38) - Swaps %mthd and %mthdz.
((( The call instructions )))
1. Check for `closure`; if no `closure`, simply move %ptr to %ret and do not perform any other actions
2. Clone the top of %dyn and put it on %dyn (%err0 and continue with no stack if empty)
3. Push a clone of `closure` onto %lex
4. Bind all of the local lexical variables
5. Push %line and %file together onto %trace
6. Pop N args from %arg into %dyn
7. Push %cont onto %stack
8. Make a new %cont based on what needs to be done from %ptr
((( xcall and xcall0 )))
(xcall0) 1. Check for `closure`; if no `closure`, stop
(xcall0) 2. Clone the top of %dyn and put it on %dyn (%err0 and continue with no stack if empty)
(xcall0) 3. Push a clone of `closure` onto %lex
(xcall0) 4. Bind all of the local lexical variables
(xcall0) 5. Push %line and %file together onto %trace
(xcall0) 6. Pop N args from %arg into %dyn
(xcall ) 7. Push %cont onto %stack
(xcall ) 8. Make a new %cont based on what needs to be done
(Note that xcall is a no-op if %ptr is not a method)
((( arith )))
- 1L - Addition
- 2L - Subtraction
- 3L - Multiplication
- 4L - Division
- 5L - Modulo
- 6L - Power
- 7L - And
- 8L - Or
- 9L - Xor
((( expd / load )))
It can be passed
- %sym - To convert to sym
- %str0, %str1 - To convert to string
- %num0, %num1 - To convert to num
- %mthd - To convert to method
- %strm - To convert to stream ptr
- %prcs - To convert to process ptr
- %mthdz - To convert to method (as with %mthd)
((( notes on rtrv / rtrvd )))
- rtrvd DOES check the current object
- rtrvd DOES NOT check parent objects
- rtrvd DOES NOT look for `missing` or `missed` declarations
- rtrvd DOES set the %err0 flag on failure
1. rtrv first checks the current object
2. rtrv then checks any parent objects recursively until it encounters a cycle
3. rtrv then looks for a `missing` method in the current and parent objects
4. rtrv then looks for a `missed` method in the lexical meta object
5. If all of the above fail, rtrv triggers a kernel panic and terminates the virtual machine
((( xxx )))
- 0L - Store trace assertion information
- 1L - Compare trace assertion information; on unequal values, immediately abort