@@ -42,6 +42,7 @@ const (
42
42
opcodeVector // 15
43
43
opcodeStruct
44
44
opcodeUndefGlobal
45
+ opcodePrimCall
45
46
)
46
47
47
48
var symOpClosure = intern ("closure" )
@@ -54,20 +55,21 @@ var symOpJump = intern("jump")
54
55
var symOpJumpFalse = intern ("jumpfalse" )
55
56
var symOpCall = intern ("call" )
56
57
var symOpTailCall = intern ("tailcall" )
57
- var symOpReturn = intern ("return" )
58
- var symOpPop = intern ("pop" )
58
+ var symOpPrimCall = intern ("primcall" )
59
+ var symOpReturn = intern ("return" )
60
+ var symOpPop = intern ("pop" )
59
61
var symOpDefGlobal = intern ("defglobal" )
60
62
var symOpUndefGlobal = intern ("undefglobal" )
61
63
var symOpDefMacro = intern ("defmacro" )
62
64
var symOpUse = intern ("use" )
63
65
64
66
// Code - compiled Ell bytecode
65
67
type Code struct {
66
- name string
67
- ops []int
68
- argc int
69
- defaults []LAny
70
- keys []LAny
68
+ name string
69
+ ops []int
70
+ argc int
71
+ defaults []LAny
72
+ keys []LAny
71
73
}
72
74
73
75
func newCode (argc int , defaults []LAny , keys []LAny , name string ) * Code {
@@ -148,7 +150,7 @@ func (code *Code) signature() string {
148
150
}
149
151
//the following has no type info
150
152
tmp := ""
151
- for i := 0 ; i < code .argc ; i ++ {
153
+ for i := 0 ; i < code .argc ; i ++ {
152
154
tmp += " <any>"
153
155
}
154
156
if code .defaults != nil {
@@ -197,31 +199,27 @@ func (code *Code) decompileInto(buf *bytes.Buffer, indent string, pretty bool) {
197
199
for offset < max {
198
200
switch code .ops [offset ] {
199
201
case opcodeLiteral :
200
- //fmt.Printf("%sL%03d:\t(literal %d) \t; %v\n", indent, offset, code.ops[offset+1], constants[code.ops[offset+1]])
201
202
buf .WriteString (prefix + "(literal " + write (constants [code .ops [offset + 1 ]]) + ")" )
202
203
offset += 2
203
204
case opcodeDefGlobal :
204
- //fmt.Printf("%sL%03d:\t(global %v)\n", indent, offset, constants[code.ops[offset+1]])
205
205
buf .WriteString (prefix + "(defglobal " + write (constants [code .ops [offset + 1 ]]) + ")" )
206
206
offset += 2
207
207
case opcodeCall :
208
- //fmt.Printf("%sL%03d:\t(call %d)\n", indent, offset, code.ops[offset+1])
209
208
buf .WriteString (prefix + "(call " + strconv .Itoa (code .ops [offset + 1 ]) + ")" )
210
209
offset += 2
211
210
case opcodeTailCall :
212
- //fmt.Printf("%s%03d:\t(tailcall %d)\n", indent, offset, code.ops[offset+1])
213
211
buf .WriteString (prefix + "(tailcall " + strconv .Itoa (code .ops [offset + 1 ]) + ")" )
214
212
offset += 2
213
+ case opcodePrimCall :
214
+ buf .WriteString (prefix + "(primcall " + strconv .Itoa (code .ops [offset + 1 ]) + " " + primitives [code .ops [offset + 2 ]].name + ")" )
215
+ offset += 3
215
216
case opcodePop :
216
- //fmt.Printf("%sL%03d:\t(pop)\n", indent, offset)
217
217
buf .WriteString (prefix + "(pop)" )
218
218
offset ++
219
219
case opcodeReturn :
220
- //fmt.Printf("%sL%03d:\t(return)\n", indent, offset)
221
220
buf .WriteString (prefix + "(return)" )
222
221
offset ++
223
222
case opcodeClosure :
224
- //fmt.Printf("%sL%03d:\t(closure %v)\n", indent, offset, code.ops[offset+1])
225
223
buf .WriteString (prefix + "(closure" )
226
224
if pretty {
227
225
buf .WriteString ("\n " )
@@ -236,31 +234,24 @@ func (code *Code) decompileInto(buf *bytes.Buffer, indent string, pretty bool) {
236
234
buf .WriteString (")" )
237
235
offset += 2
238
236
case opcodeLocal :
239
- //fmt.Printf("%sL%03d:\t(local %d %d)\n", indent, offset, code.ops[offset+1], code.ops[offset+2])
240
237
buf .WriteString (prefix + "(local " + strconv .Itoa (code .ops [offset + 1 ]) + " " + strconv .Itoa (code .ops [offset + 2 ]) + ")" )
241
238
offset += 3
242
239
case opcodeGlobal :
243
- //fmt.Printf("%sL%03d:\t(global %v)\n", indent, offset, constants[code.ops[offset+1]])
244
240
buf .WriteString (prefix + "(global " + write (constants [code .ops [offset + 1 ]]) + ")" )
245
241
offset += 2
246
242
case opcodeUndefGlobal :
247
- //fmt.Printf("%sL%03d:\t(unglobal %v)\n", indent, offset, constants[code.ops[offset+1]])
248
243
buf .WriteString (prefix + "(undefglobal " + write (constants [code .ops [offset + 1 ]]) + ")" )
249
244
offset += 2
250
245
case opcodeDefMacro :
251
- //fmt.Printf("%sL%03d:\t(defmacro%6d ; %v)\n", indent, offset, code.ops[offset+1], constants[code.ops[offset+1]])
252
246
buf .WriteString (prefix + "(defmacro " + write (constants [code .ops [offset + 1 ]]) + ")" )
253
247
offset += 2
254
248
case opcodeSetLocal :
255
- //Println("%sL%03d:\t(setlocal %d %d)\n", indent, offset, code.ops[offset+1], code.ops[offset+2])
256
249
buf .WriteString (prefix + "(setlocal " + strconv .Itoa (code .ops [offset + 1 ]) + " " + strconv .Itoa (code .ops [offset + 2 ]) + ")" )
257
250
offset += 3
258
251
case opcodeJumpFalse :
259
- //fmt.Printf("%sL%03d:\t(jumpfalse %d)\t; L%03d\n", indent, offset, code.ops[offset+1], code.ops[offset+1] + offset)
260
252
buf .WriteString (prefix + "(jumpfalse " + strconv .Itoa (code .ops [offset + 1 ]) + ")" )
261
253
offset += 2
262
254
case opcodeJump :
263
- //fmt.Printf("%sL%03d:\t(jump %d) \t; L%03d\n", indent, offset, code.ops[offset+1], code.ops[offset+1] + offset)
264
255
buf .WriteString (prefix + "(jump " + strconv .Itoa (code .ops [offset + 1 ]) + ")" )
265
256
offset += 2
266
257
case opcodeVector :
@@ -363,7 +354,11 @@ func (code *Code) loadOps(lst LAny) error {
363
354
}
364
355
code .emitSetLocal (i , j )
365
356
case symOpGlobal :
366
- code .emitGlobal (cadr (instr ))
357
+ if sym , ok := cadr (instr ).(* LSymbol ); ok {
358
+ code .emitGlobal (sym )
359
+ } else {
360
+ return Error (symOpGlobal , " argument 1 not a symbol: " , cadr (instr ))
361
+ }
367
362
case symOpUndefGlobal :
368
363
code .emitUndefGlobal (cadr (instr ))
369
364
case symOpJump :
@@ -413,9 +408,9 @@ func (code *Code) emitLiteral(val LAny) {
413
408
code .ops = append (code .ops , putConstant (val ))
414
409
}
415
410
416
- func (code * Code ) emitGlobal (sym LAny ) {
411
+ func (code * Code ) emitGlobal (sym * LSymbol ) {
417
412
code .ops = append (code .ops , opcodeGlobal )
418
- code .ops = append (code .ops , putConstant ( sym ) )
413
+ code .ops = append (code .ops , sym . tag )
419
414
}
420
415
func (code * Code ) emitCall (argc int ) {
421
416
code .ops = append (code .ops , opcodeCall )
@@ -428,6 +423,11 @@ func (code *Code) emitTailCall(argc int) {
428
423
code .ops = append (code .ops , opcodeTailCall )
429
424
code .ops = append (code .ops , argc )
430
425
}
426
+ func (code * Code ) emitPrimCall (prim * LPrimitive , argc int ) {
427
+ code .ops = append (code .ops , opcodePrimCall )
428
+ code .ops = append (code .ops , prim .idx )
429
+ code .ops = append (code .ops , argc )
430
+ }
431
431
func (code * Code ) emitPop () {
432
432
code .ops = append (code .ops , opcodePop )
433
433
}
0 commit comments