-
Notifications
You must be signed in to change notification settings - Fork 10
/
compexp.go
473 lines (426 loc) · 10.9 KB
/
compexp.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
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
package astcomp
import (
"github.com/arnodel/golua/ast"
"github.com/arnodel/golua/ir"
"github.com/arnodel/golua/ops"
)
//
// Expression compilation
//
type expCompiler struct {
*compiler
dst ir.Register
}
var _ ast.ExpProcessor = (*expCompiler)(nil)
// ProcessBFunctionCallExp compiles a BFunctionCall
func (c *expCompiler) ProcessBFunctionCallExp(f ast.BFunctionCall) {
c.compileCall(f, false)
c.emitInstr(f, ir.Receive{Dst: []ir.Register{c.dst}})
}
// ProcessBinOpExp compiles a BinOpExp.
func (c *expCompiler) ProcessBinOpExp(b ast.BinOp) {
if b.OpType == ops.OpAnd {
c.compileLogicalOp(b, true)
return
}
if b.OpType == ops.OpOr {
c.compileLogicalOp(b, false)
return
}
lsrc := c.compileExpNoDestHint(b.Left)
for _, r := range b.Right {
c.TakeRegister(lsrc)
rsrc := c.compileExpNoDestHint(r.Operand)
switch r.Op {
case ops.OpNeq:
// x ~= y ==> ~(x = y)
c.emitInstr(b, ir.Combine{
Op: ops.OpEq,
Dst: c.dst,
Lsrc: lsrc,
Rsrc: rsrc,
})
c.emitInstr(b, ir.Transform{
Op: ops.OpNot,
Dst: c.dst,
Src: c.dst,
})
case ops.OpGt:
// x > y ==> y < x
c.emitInstr(b, ir.Combine{
Op: ops.OpLt,
Dst: c.dst,
Lsrc: rsrc,
Rsrc: lsrc,
})
case ops.OpGeq:
// x >= y ==> y <= x
c.emitInstr(b, ir.Combine{
Op: ops.OpLeq,
Dst: c.dst,
Lsrc: rsrc,
Rsrc: lsrc,
})
default:
c.emitInstr(b, ir.Combine{
Op: r.Op,
Dst: c.dst,
Lsrc: lsrc,
Rsrc: rsrc,
})
}
c.ReleaseRegister(lsrc)
lsrc = c.dst
}
}
// This implements short-circuiting in logical expressions.
func (c *expCompiler) compileLogicalOp(b ast.BinOp, not bool) {
doneLbl := c.GetNewLabel()
c.compileExpInto(b.Left, c.dst)
c.emitInstr(b.Left, ir.JumpIf{Cond: c.dst, Label: doneLbl, Not: not})
for i, r := range b.Right {
c.compileExpInto(r.Operand, c.dst)
if i < len(b.Right) {
c.emitInstr(r.Operand, ir.JumpIf{Cond: c.dst, Label: doneLbl, Not: not})
}
}
must(c.EmitLabelNoLine(doneLbl))
}
// ProcesBoolExp compiles a oolExp.
func (c *expCompiler) ProcesBoolExp(b ast.Bool) {
c.emitLoadConst(b, ir.Bool(b.Val), c.dst)
}
// ProcessEtcExp compiles a EtcExp.
func (c *expCompiler) ProcessEtcExp(e ast.Etc) {
reg := c.getEllipsisReg()
c.emitInstr(e, ir.EtcLookup{Dst: c.dst, Etc: reg})
}
// ProcessFunctionExp compiles a Function.
func (c *expCompiler) ProcessFunctionExp(f ast.Function) {
fc := c.NewChild(f.Name)
fc.compileFunctionBody(f)
kidx, upvalues := fc.Close()
c.emitInstr(f, ir.MkClosure{
Dst: c.dst,
Code: kidx,
Upvalues: upvalues,
})
}
// ProcessFunctionCallExp compiles a FunctionCall.
func (c *expCompiler) ProcessFunctionCallExp(f ast.FunctionCall) {
c.ProcessBFunctionCallExp(*f.BFunctionCall)
}
// ProcessIndexExp compiles a IndexExp.
func (c *expCompiler) ProcessIndexExp(e ast.IndexExp) {
tReg := c.compileExpNoDestHint(e.Coll)
c.TakeRegister(tReg)
iReg := c.compileExpNoDestHint(e.Idx)
c.emitInstr(e, ir.Lookup{
Dst: c.dst,
Table: tReg,
Index: iReg,
})
c.ReleaseRegister(tReg)
}
// ProcessNameExp compiles a NameExp.
func (c *expCompiler) ProcessNameExp(n ast.Name) {
// Is it bound to a local name?
reg, ok := c.GetRegister(ir.Name(n.Val))
if ok {
c.dst = reg
return
}
// If not, try _ENV.
c.CompileExp(globalVar(n))
}
// ProcessNilExp compiles a NilExp.
func (c *expCompiler) ProcessNilExp(n ast.Nil) {
c.emitLoadConst(n, ir.NilType{}, c.dst)
}
// ProcessIntExp compiles a IntExp.
func (c *expCompiler) ProcessIntExp(n ast.Int) {
c.emitLoadConst(n, ir.Int(n.Val), c.dst)
}
// ProcessFloatExp compiles a FloatExp.
func (c *expCompiler) ProcessFloatExp(f ast.Float) {
c.emitLoadConst(f, ir.Float(f.Val), c.dst)
}
// ProcessStringExp compiles a StringExp.
func (c *expCompiler) ProcessStringExp(s ast.String) {
c.emitLoadConst(s, ir.String(s.Val), c.dst)
}
// ProcessTableConstructorExp compiles a TableConstructorExp.
func (c *expCompiler) ProcessTableConstructorExp(t ast.TableConstructor) {
c.emitInstr(t, ir.MkTable{Dst: c.dst})
c.TakeRegister(c.dst)
currImplicitKey := 1
for i, field := range t.Fields {
keyExp := field.Key
_, noKey := keyExp.(ast.NoTableKey)
if i == len(t.Fields)-1 && noKey {
tailExp, ok := field.Value.(ast.TailExpNode)
if ok {
etc := c.compileEtcExp(tailExp, c.GetFreeRegister())
c.emitInstr(field.Value, ir.FillTable{
Dst: c.dst,
Idx: currImplicitKey,
Etc: etc,
})
break
}
}
valReg := c.compileExpNoDestHint(field.Value)
c.TakeRegister(valReg)
if noKey {
keyExp = ast.Int{Val: uint64(currImplicitKey)}
currImplicitKey++
}
keyReg := c.compileExpNoDestHint(keyExp)
c.emitInstr(field.Value, ir.SetIndex{
Table: c.dst,
Index: keyReg,
Src: valReg,
})
c.ReleaseRegister(valReg)
}
c.ReleaseRegister(c.dst)
}
// ProcessUnOpExp compiles a UnOpExp.
func (c *expCompiler) ProcessUnOpExp(u ast.UnOp) {
c.emitInstr(u, ir.Transform{
Op: u.Op,
Dst: c.dst,
Src: c.compileExpNoDestHint(u.Operand),
})
}
func (c *expCompiler) CompileExp(e ast.ExpNode) {
e.ProcessExp(c)
}
//
// Tail Expression compilation
//
type tailExpCompiler struct {
*compiler
dsts []ir.Register
}
var _ ast.TailExpProcessor = tailExpCompiler{}
// ProcessEtcTailExp compiles an Etc tail expression.
func (c tailExpCompiler) ProcessEtcTailExp(e ast.Etc) {
reg := c.getEllipsisReg()
for i, dst := range c.dsts {
c.emitInstr(e, ir.EtcLookup{
Dst: dst,
Etc: reg,
Idx: i,
})
}
}
// ProcessFunctionCallTailExp compiles a FunctionCall tail expression.
func (c tailExpCompiler) ProcessFunctionCallTailExp(f ast.FunctionCall) {
c.compileCall(*f.BFunctionCall, false)
c.emitInstr(f, ir.Receive{Dst: c.dsts})
}
func (c tailExpCompiler) CompileTailExp(e ast.TailExpNode) {
e.ProcessTailExp(c)
}
//
// Etc Expression compilation
//
type etcExpCompiler struct {
*compiler
dst ir.Register
}
var _ ast.TailExpProcessor = (*etcExpCompiler)(nil)
func (c *etcExpCompiler) ProcessEtcTailExp(e ast.Etc) {
c.dst = c.getEllipsisReg()
}
func (c *etcExpCompiler) ProcessFunctionCallTailExp(f ast.FunctionCall) {
c.compileCall(*f.BFunctionCall, false)
c.emitInstr(f, ir.ReceiveEtc{Etc: c.dst})
}
func (c *etcExpCompiler) CompileTailExp(e ast.TailExpNode) {
e.ProcessTailExp(c)
}
//
// Helper functions
//
// compileExp compiles the given expression into a register and returns it.
func (c *compiler) compileExp(e ast.ExpNode, dst ir.Register) ir.Register {
ec := expCompiler{
compiler: c,
dst: dst,
}
ec.CompileExp(e)
return ec.dst
}
// compileExpNoDestHint compiles the given expression into any register (perhaps a
// new free one) and returns it.
func (c *compiler) compileExpNoDestHint(e ast.ExpNode) ir.Register {
return c.compileExp(e, c.GetFreeRegister())
}
// compileExpInto compiles the given expression into the given register.
func (c *compiler) compileExpInto(e ast.ExpNode, dst ir.Register) {
c.emitMove(e, dst, c.compileExp(e, dst))
}
// compileTailExp compiles the given tail expression into the register slice.
func (c *compiler) compileTailExp(e ast.TailExpNode, dsts []ir.Register) {
tailExpCompiler{
compiler: c,
dsts: dsts,
}.CompileTailExp(e)
}
// compileEtcExp compiles the given tail expression as an etc and returns the
// register the result lives in.
func (c *compiler) compileEtcExp(e ast.TailExpNode, dst ir.Register) ir.Register {
ec := etcExpCompiler{
compiler: c,
dst: dst,
}
ec.CompileTailExp(e)
return ec.dst
}
// compileExpList compiles the given expressions into free registers, which are
// recorded into dstRegs (hence exps and dstRegs must have the same length).
// Those registers are taken so need to be released by the caller when no longer
// needed.
func (c *compiler) compileExpList(exps []ast.ExpNode, dstRegs []ir.Register) {
commonCount := len(exps)
if commonCount > len(dstRegs) {
commonCount = len(dstRegs)
}
var tailExp ast.TailExpNode
doTailExp := false
if len(dstRegs) > len(exps) && len(exps) > 0 {
tailExp, doTailExp = exps[len(exps)-1].(ast.TailExpNode)
if doTailExp {
commonCount--
}
}
for i, exp := range exps[:commonCount] {
dst := c.GetFreeRegister()
c.compileExpInto(exp, dst)
c.TakeRegister(dst)
dstRegs[i] = dst
}
for i := commonCount; i < len(dstRegs); i++ {
dst := c.GetFreeRegister()
c.TakeRegister(dst)
dstRegs[i] = dst
}
if doTailExp {
c.compileTailExp(tailExp, dstRegs[commonCount:])
} else if len(dstRegs) > len(exps) {
nilK := ir.NilType{}
for _, dst := range dstRegs[len(exps):] {
c.emitLoadConst(nil, nilK, dst)
}
}
}
func (c *compiler) compileFunctionBody(f ast.Function) {
recvRegs := make([]ir.Register, len(f.Params))
callerReg := c.GetFreeRegister()
c.DeclareLocal(callerRegName, callerReg)
for i, p := range f.Params {
reg := c.GetFreeRegister()
c.DeclareLocal(ir.Name(p.Val), reg)
recvRegs[i] = reg
}
if !f.HasDots {
c.emitInstr(f, ir.Receive{Dst: recvRegs})
} else {
reg := c.GetFreeRegister()
c.DeclareLocal(ellipsisRegName, reg)
c.emitInstr(f, ir.ReceiveEtc{Dst: recvRegs, Etc: reg})
}
// Need to make sure there is a return instruction emitted at the
// end.
body := f.Body
if body.Return == nil {
body.Return = []ast.ExpNode{}
}
c.compileBlock(body)
}
func (c *compiler) getEllipsisReg() ir.Register {
reg, ok := c.GetRegister(ellipsisRegName)
if !ok {
panic("... not defined")
}
return reg
}
func (c *compiler) getCallerReg() ir.Register {
reg, ok := c.GetRegister(callerRegName)
if !ok {
panic("no caller register")
}
return reg
}
func (c *compiler) compileCall(f ast.BFunctionCall, tail bool) {
fReg := c.compileExpNoDestHint(f.Target)
var contReg ir.Register
if f.Method.Val != "" {
self := fReg
c.TakeRegister(self)
fReg = c.GetFreeRegister()
mReg := c.GetFreeRegister()
c.emitLoadConst(f.Method, ir.String(f.Method.Val), mReg)
c.emitInstr(f.Target, ir.Lookup{
Dst: fReg,
Table: self,
Index: mReg,
})
contReg = c.GetFreeRegister()
c.emitInstr(f, ir.MkCont{
Dst: contReg,
Closure: fReg,
Tail: tail,
})
c.emitInstr(f, ir.Push{
Cont: fReg,
Item: self,
})
c.ReleaseRegister(self)
} else {
contReg = c.GetFreeRegister()
c.emitInstr(f, ir.MkCont{
Dst: contReg,
Closure: fReg,
Tail: tail,
})
}
c.compilePushArgs(f.Args, contReg)
c.emitInstr(f, ir.Call{
Cont: contReg,
Tail: tail,
})
}
// TODO: move this to somewhere better
func (c *compiler) compilePushArgs(args []ast.ExpNode, contReg ir.Register) {
c.TakeRegister(contReg)
for i, arg := range args {
var argReg ir.Register
var isTailArg bool
if i == len(args)-1 {
var tailArg ast.TailExpNode
tailArg, isTailArg = arg.(ast.TailExpNode)
if isTailArg {
argReg = c.compileEtcExp(tailArg, c.GetFreeRegister())
}
}
if !isTailArg {
argReg = c.compileExpNoDestHint(arg)
}
c.emitInstr(arg, ir.Push{
Cont: contReg,
Item: argReg,
Etc: isTailArg,
})
}
c.ReleaseRegister(contReg)
}
func globalVar(n ast.Name) ast.IndexExp {
return ast.IndexExp{
Location: n.Location,
Coll: ast.Name{Location: n.Location, Val: "_ENV"},
Idx: n.AstString(),
}
}