-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.go
404 lines (388 loc) · 10.2 KB
/
code.go
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
package vile
import (
"bytes"
"fmt"
"strconv"
"strings"
)
const ( /* Vile operation code or opcode */
opcodeLiteral = iota // 0
opcodeLocal // 1
opcodeJumpFalse // 2
opcodeJump // 3
opcodeTailCall // 4
opcodeCall // 5
opcodeReturn // 6
opcodeClosure // 7
opcodePop // 8
opcodeGlobal // 9
opcodeDefGlobal // 10
opcodeSetLocal // 11
opcodeImport // 12
opcodeDefMacro // 13
opcodeVector // 14
opcodeStruct // 15
opcodeUndefGlobal // 16
opcodeCount // 17
) /* Vile have 18 opcodes */
var LiteralSymbol = Intern("literal")
var LocalSymbol = Intern("local")
var JumpfalseSymbol = Intern("jumpfalse")
var JumpSymbol = Intern("jump")
var TailcallSymbol = Intern("tailcall")
var CallSymbol = Intern("call")
var ReturnSymbol = Intern("return")
var ClosureSymbol = Intern("closure")
var PopSymbol = Intern("pop")
var GlobalSymbol = Intern("global")
var DefglobalSymbol = Intern("defglobal")
var SetlocalSymbol = Intern("setlocal")
var ImportSymbol = Intern("import")
var DefmacroSymbol = Intern("macro")
var VectorSymbol = Intern("vector")
var StructSymbol = Intern("struct")
var UndefineSymbol = Intern("undefine")
var FuncSymbol = Intern("func")
var opsyms = initOpsyms()
func initOpsyms() []*Object {
syms := make([]*Object, opcodeCount)
syms[opcodeLiteral] = LiteralSymbol
syms[opcodeLocal] = LocalSymbol
syms[opcodeJumpFalse] = JumpfalseSymbol
syms[opcodeJump] = JumpSymbol
syms[opcodeTailCall] = TailcallSymbol
syms[opcodeCall] = CallSymbol
syms[opcodeReturn] = ReturnSymbol
syms[opcodeClosure] = ClosureSymbol
syms[opcodePop] = PopSymbol
syms[opcodeGlobal] = GlobalSymbol
syms[opcodeDefGlobal] = DefglobalSymbol
syms[opcodeSetLocal] = SetlocalSymbol
syms[opcodeImport] = ImportSymbol
syms[opcodeDefMacro] = DefmacroSymbol
syms[opcodeVector] = VectorSymbol
syms[opcodeStruct] = StructSymbol
syms[opcodeUndefGlobal] = UndefineSymbol
return syms
}
// Code - compiled Vile bytecode
type Code struct {
name string
ops []int
argc int
defaults []*Object // defaults => []*Object
keys []*Object // keys => []*Object
}
// MakeCode - create a new code object
func MakeCode(argc int, defaults []*Object, keys []*Object, name string) *Object {
var ops []int
code := &Code{
name,
ops,
argc,
defaults, // nil for normal procs, empty for rest, and non-empty for optional/keyword
keys,
}
result := new(Object)
result.Type = CodeType // CodeType is the type of compiled code
result.code = code
return result
}
func (code *Code) signature() string {
if code.name != "" {
val := GetGlobal(Intern("*declarations*"))
if val != nil && IsStruct(val) {
sig, _ := Get(val, Intern(code.name))
if sig != Null {
return sig.String()
}
}
}
tmp := ""
for i := 0; i < code.argc; i++ {
tmp += " <any>"
}
if code.defaults != nil {
tmp += " <any>*"
}
if tmp != "" {
tmp = "(" + tmp[1:] + ")"
} else {
tmp = "()"
}
return tmp
}
func (code *Code) decompile(pretty bool) string {
var buf bytes.Buffer
code.decompileInto(&buf, "", pretty)
s := buf.String()
return strings.Replace(s, "("+FuncSymbol.text+" (\"\" 0 [] [])", "(code", 1)
}
func (code *Code) decompileInto(buf *bytes.Buffer, indent string, pretty bool) {
indentAmount := " "
offset := 0
max := len(code.ops)
prefix := " "
buf.WriteString(indent + "(" + FuncSymbol.text + " (")
buf.WriteString(fmt.Sprintf("%q ", code.name))
buf.WriteString(strconv.Itoa(code.argc))
if code.defaults != nil {
buf.WriteString(" ")
buf.WriteString(fmt.Sprintf("%v", code.defaults))
} else {
buf.WriteString(" []")
}
if code.keys != nil {
buf.WriteString(" ")
buf.WriteString(fmt.Sprintf("%v", code.keys))
} else {
buf.WriteString(" []")
}
buf.WriteString(")")
if pretty {
indent = indent + indentAmount
prefix = "\n" + indent
}
for offset < max {
op := code.ops[offset]
s := prefix + "(" + opsyms[op].text
switch op {
case opcodePop, opcodeReturn:
buf.WriteString(s + ")")
offset++
case opcodeLiteral, opcodeDefGlobal, opcodeImport, opcodeGlobal, opcodeUndefGlobal, opcodeDefMacro:
buf.WriteString(s + " " + Write(constants[code.ops[offset+1]]) + ")")
offset += 2
case opcodeCall, opcodeTailCall, opcodeJumpFalse, opcodeJump, opcodeVector, opcodeStruct:
buf.WriteString(s + " " + strconv.Itoa(code.ops[offset+1]) + ")")
offset += 2
case opcodeLocal, opcodeSetLocal:
buf.WriteString(s + " " + strconv.Itoa(code.ops[offset+1]) + " " + strconv.Itoa(code.ops[offset+2]) + ")")
offset += 3
case opcodeClosure:
buf.WriteString(s)
if pretty {
buf.WriteString("\n")
} else {
buf.WriteString(" ")
}
indent2 := ""
if pretty {
indent2 = indent + indentAmount
}
constants[code.ops[offset+1]].code.decompileInto(buf, indent2, pretty)
buf.WriteString(")")
offset += 2
default:
panic(fmt.Sprintf("Bad instruction: %d", code.ops[offset]))
}
}
buf.WriteString(")")
}
func (code *Code) String() string {
return code.decompile(true)
// return fmt.Sprintf("(function (%d %v %s) %v)", code.argc, code.defaults, code.keys, code.ops)
}
func (code *Code) loadOps(lst *Object) error {
for lst != EmptyList {
instr := Car(lst)
op := Car(instr)
switch op {
case ClosureSymbol:
lstFunc := Cadr(instr)
if Car(lstFunc) != FuncSymbol {
return Error(SyntaxErrorKey, instr)
}
lstFunc = Cdr(lstFunc)
funcParams := Car(lstFunc)
var argc int
var name string
var defaults []*Object
var keys []*Object
var err error
if IsSymbol(funcParams) {
// legacy form, just the argc
argc, err = AsIntValue(funcParams)
if err != nil {
return err
}
if argc < 0 {
argc = -argc - 1
defaults = make([]*Object, 0)
}
} else if IsList(funcParams) && ListLength(funcParams) == 4 {
tmp := funcParams
a := Car(tmp)
tmp = Cdr(tmp)
name, err = AsStringValue(a)
if err != nil {
return Error(SyntaxErrorKey, funcParams)
}
a = Car(tmp)
tmp = Cdr(tmp)
argc, err = AsIntValue(a)
if err != nil {
return Error(SyntaxErrorKey, funcParams)
}
a = Car(tmp)
tmp = Cdr(tmp)
if IsVector(a) {
defaults = a.elements
}
a = Car(tmp)
if IsVector(a) {
keys = a.elements
}
} else {
return Error(SyntaxErrorKey, funcParams)
}
fun := MakeCode(argc, defaults, keys, name)
fun.code.loadOps(Cdr(lstFunc))
code.emitClosure(fun)
case LiteralSymbol:
code.emitLiteral(Cadr(instr))
case LocalSymbol:
i, err := AsIntValue(Cadr(instr))
if err != nil {
return err
}
j, err := AsIntValue(Caddr(instr))
if err != nil {
return err
}
code.emitLocal(i, j)
case SetlocalSymbol:
i, err := AsIntValue(Cadr(instr))
if err != nil {
return err
}
j, err := AsIntValue(Caddr(instr))
if err != nil {
return err
}
code.emitSetLocal(i, j)
case GlobalSymbol:
sym := Cadr(instr)
if IsSymbol(sym) {
code.emitGlobal(sym)
} else {
return Error(GlobalSymbol, " argument 1 not a symbol: ", sym)
}
case UndefineSymbol:
code.emitUndefGlobal(Cadr(instr))
case JumpSymbol:
loc, err := AsIntValue(Cadr(instr))
if err != nil {
return err
}
code.emitJump(loc)
case JumpfalseSymbol:
loc, err := AsIntValue(Cadr(instr))
if err != nil {
return err
}
code.emitJumpFalse(loc)
case CallSymbol:
argc, err := AsIntValue(Cadr(instr))
if err != nil {
return err
}
code.emitCall(argc)
case TailcallSymbol:
argc, err := AsIntValue(Cadr(instr))
if err != nil {
return err
}
code.emitTailCall(argc)
case ReturnSymbol:
code.emitReturn()
case PopSymbol:
code.emitPop()
case DefglobalSymbol:
code.emitDefGlobal(Cadr(instr))
case DefmacroSymbol:
code.emitDefMacro(Cadr(instr))
case ImportSymbol:
code.emitImport(Cadr(instr))
default:
panic(fmt.Sprintf("Bad instruction: %v", op))
}
lst = Cdr(lst)
}
return nil
}
func (code *Code) emitLiteral(val *Object) {
code.ops = append(code.ops, opcodeLiteral)
code.ops = append(code.ops, putConstant(val))
}
func (code *Code) emitGlobal(sym *Object) {
code.ops = append(code.ops, opcodeGlobal)
code.ops = append(code.ops, putConstant(sym))
}
func (code *Code) emitCall(argc int) {
code.ops = append(code.ops, opcodeCall)
code.ops = append(code.ops, argc)
}
func (code *Code) emitReturn() {
code.ops = append(code.ops, opcodeReturn)
}
func (code *Code) emitTailCall(argc int) {
code.ops = append(code.ops, opcodeTailCall)
code.ops = append(code.ops, argc)
}
func (code *Code) emitPop() {
code.ops = append(code.ops, opcodePop)
}
func (code *Code) emitLocal(i int, j int) {
code.ops = append(code.ops, opcodeLocal)
code.ops = append(code.ops, i)
code.ops = append(code.ops, j)
}
func (code *Code) emitSetLocal(i int, j int) {
code.ops = append(code.ops, opcodeSetLocal)
code.ops = append(code.ops, i)
code.ops = append(code.ops, j)
}
func (code *Code) emitDefGlobal(sym *Object) {
code.ops = append(code.ops, opcodeDefGlobal)
code.ops = append(code.ops, putConstant(sym))
}
func (code *Code) emitUndefGlobal(sym *Object) {
code.ops = append(code.ops, opcodeUndefGlobal)
code.ops = append(code.ops, putConstant(sym))
}
func (code *Code) emitDefMacro(sym *Object) {
code.ops = append(code.ops, opcodeDefMacro)
code.ops = append(code.ops, putConstant(sym))
}
func (code *Code) emitClosure(newCode *Object) {
code.ops = append(code.ops, opcodeClosure)
code.ops = append(code.ops, putConstant(newCode))
}
func (code *Code) emitJumpFalse(offset int) int {
code.ops = append(code.ops, opcodeJumpFalse)
loc := len(code.ops)
code.ops = append(code.ops, offset)
return loc
}
func (code *Code) emitJump(offset int) int {
code.ops = append(code.ops, opcodeJump)
loc := len(code.ops)
code.ops = append(code.ops, offset)
return loc
}
func (code *Code) setJumpLocation(loc int) {
code.ops[loc] = len(code.ops) - loc + 1
}
func (code *Code) emitVector(alen int) {
code.ops = append(code.ops, opcodeVector)
code.ops = append(code.ops, alen)
}
func (code *Code) emitStruct(slen int) {
code.ops = append(code.ops, opcodeStruct)
code.ops = append(code.ops, slen)
}
func (code *Code) emitImport(sym *Object) {
code.ops = append(code.ops, opcodeImport)
code.ops = append(code.ops, putConstant(sym))
}