forked from cosmos72/gomacro
/
code.go
398 lines (366 loc) · 9.77 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
/*
* gomacro - A Go interpreter with Lisp-like macros
*
* Copyright (C) 2017-2019 Massimiliano Ghilardi
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*
* exec.go
*
* Created on Apr 09, 2017
* Author Massimiliano Ghilardi
*/
package fast
import (
"go/token"
"github.com/lifepod-solutions/gomacro/base"
)
func (code *Code) Clear() {
code.List = nil
code.DebugPos = nil
code.WithDefers = false
}
func (code *Code) Len() int {
return len(code.List)
}
func (code *Code) Truncate(n int) {
if len(code.List) > n {
code.List = code.List[0:n]
}
if len(code.DebugPos) > n {
code.DebugPos = code.DebugPos[0:n]
}
}
func (code *Code) Append(stmt Stmt, pos token.Pos) {
if stmt != nil {
code.List = append(code.List, stmt)
code.DebugPos = append(code.DebugPos, pos)
}
}
func (code *Code) AsExpr() *Expr {
fun := code.Exec()
if fun == nil {
return nil
}
return expr0(fun)
}
// spinInterrupt is the statement executed while waiting for an interrupt to be serviced.
// To signal an interrupt, a statement must set env.ThreadGlobals.Signal to the desired signal,
// then return env.ThreadGlobals.Interrupt, env
func spinInterrupt(env *Env) (Stmt, *Env) {
run := env.Run
if run.Signals.IsEmpty() {
run.Signals.Sync = base.SigReturn
} else if sig := run.Signals.Async; sig != base.SigNone {
run.applyAsyncSignal(sig)
}
return run.Interrupt, env
}
func (run *Run) applyAsyncSignal(sig base.Signal) {
run.Signals.Async = base.SigNone
switch sig {
case base.SigNone:
break
case base.SigDebug:
run.applyDebugOp(DebugOpStep)
default:
panic(base.SigInterrupt)
}
}
func pushDefer(g *Run, deferOf *Env, panicking bool) (retg *Run, deferOf_ *Env, isDefer bool) {
deferOf_ = g.DeferOfFun
if panicking {
g.PanicFun = deferOf
}
g.DeferOfFun = deferOf
g.ExecFlags.SetStartDefer(true)
return g, deferOf_, g.ExecFlags.IsDefer()
}
func popDefer(run *Run, deferOf *Env, isDefer bool) {
run.DeferOfFun = deferOf
run.ExecFlags.SetStartDefer(false)
run.ExecFlags.SetDefer(isDefer)
}
func restore(run *Run, isDefer bool, interrupt Stmt, caller *Env) {
run.ExecFlags.SetDefer(isDefer)
run.Interrupt = interrupt
run.CurrEnv = caller
run.Signals.Sync = base.SigNone
if sig := run.Signals.Async; sig == base.SigInterrupt {
// do NOT handle async SigDebug here
run.applyAsyncSignal(sig)
}
}
func maybeRepanic(run *Run) bool {
if run.PanicFun != nil {
panic(run.Panic)
}
// either not panicking or recover() invoked, no longer panicking
return false
}
func (run *Run) interrupt() {
const CtrlCDebug = base.OptDebugger | base.OptCtrlCEnterDebugger
var sig base.Signal
if run.Options&CtrlCDebug == CtrlCDebug {
sig = base.SigDebug
} else {
sig = base.SigInterrupt
}
run.Signals.Async = sig
}
// Exec returns a func(*Env) that will execute the compiled code
func (code *Code) Exec() func(*Env) {
all := code.List
pos := code.DebugPos
defers := code.WithDefers
code.Clear()
if len(all) == 0 {
return nil
}
all = append(all, spinInterrupt)
if defers {
// code to support defer is slower... isolate it in a separate function
return execWithFlags(all, pos)
}
return exec(all, pos)
}
func exec(all []Stmt, pos []token.Pos) func(*Env) {
return func(env *Env) {
run := env.Run
run.Signals.Sync = base.SigNone
if run.ExecFlags != 0 {
// code to support defer and debugger is slower... isolate it in a separate function
reExecWithFlags(env, all, pos, all[0], 0)
return
}
if sig := run.Signals.Async; sig != base.SigNone {
run.applyAsyncSignal(sig)
}
saveInterrupt := run.Interrupt
run.Interrupt = nil
stmt := all[0]
env.IP = 0
env.Code = all
env.DebugPos = pos
for j := 0; j < 5; j++ {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if run.Signals.IsEmpty() {
continue
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
goto finish
}
run.Interrupt = spinInterrupt
for {
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
if !run.Signals.IsEmpty() {
break
}
}
finish:
// restore env.ThreadGlobals.Interrupt and Signal before returning
run.Interrupt = saveInterrupt
if sig := run.Signals.Async; sig != base.SigNone {
run.applyAsyncSignal(sig) // may set run.Signals.Debug if OptCtrlCEnterDebugger is set
}
if run.Signals.Debug == base.SigNone {
run.Signals.Sync = base.SigNone
} else {
reExecWithFlags(env, all, pos, stmt, env.IP)
}
}
}
// execWithFlags returns a function that will execute the given compiled code, including support for defer() and debugger
func execWithFlags(all []Stmt, pos []token.Pos) func(*Env) {
return func(env *Env) {
env.Run.Signals.Sync = base.SigNone
reExecWithFlags(env, all, pos, all[0], 0)
}
}
func reExecWithFlags(env *Env, all []Stmt, pos []token.Pos, stmt Stmt, ip int) {
run := env.Run
ef := &run.ExecFlags
trace := run.Options&base.OptDebugDebugger != 0
if trace {
run.Debugf("reExecWithFlags: executing function stmt = %p, env = %p, IP = %v, execFlags = %v, signals = %#v", stmt, env, ip, *ef, run.Signals)
}
if sig := run.Signals.Async; sig != base.SigNone {
run.applyAsyncSignal(sig)
}
caller := run.CurrEnv
// restore g.IsDefer, g.Signal, g.DebugCallDepth, g.Interrupt and g.Caller on return
defer restore(run, run.ExecFlags.IsDefer(), run.Interrupt, caller)
ef.SetDefer(ef.StartDefer())
ef.SetStartDefer(false)
ef.SetDebug(run.Signals.Debug != base.SigNone)
funenv := env
env.IP = ip
env.Code = all
env.DebugPos = pos
panicking, panicking2 := true, false
rundefer := func(fun func()) {
if panicking || panicking2 {
panicking = true
panicking2 = false
run.Panic = recover()
}
defer popDefer(pushDefer(run, funenv, panicking))
panicking2 = true // detect panics inside defer
fun()
panicking2 = false
if panicking {
panicking = maybeRepanic(run)
}
}
if stmt == nil || !run.Signals.IsEmpty() {
goto signal
}
again:
run.Interrupt = nil
for j := 0; j < 5; j++ {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if stmt, env = stmt(env); stmt != nil {
if run.Signals.IsEmpty() {
continue
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
for run.Signals.Sync == base.SigDefer {
run.Signals.Sync = base.SigNone
fun := run.InstallDefer
run.InstallDefer = nil
defer rundefer(fun)
stmt = env.Code[env.IP]
if stmt == nil {
goto signal
}
}
if !run.Signals.IsEmpty() {
goto signal
}
continue
}
run.Interrupt = spinInterrupt
for {
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
stmt, env = stmt(env)
for run.Signals.Sync == base.SigDefer {
run.Signals.Sync = base.SigNone
fun := run.InstallDefer
run.InstallDefer = nil
defer rundefer(fun)
// single step
stmt = env.Code[env.IP]
stmt, env = stmt(env)
}
if !run.Signals.IsEmpty() {
goto signal
}
}
signal:
if sig := run.Signals.Async; sig != base.SigNone {
// if OptCtrlCEnterDebugger is set, convert early
// Signals.Async = SigDebug to Signals.Debug = SigDebug
run.applyAsyncSignal(sig)
}
for run.Signals.Debug != base.SigNone {
run.Interrupt = spinInterrupt
stmt, env = singleStep(env)
if trace {
run.Debugf("singleStep returned stmt = %p, env = %p, IP = %v, execFlags = %v, signals = %#v", stmt, env, env.IP, run.ExecFlags, run.Signals)
}
// a Sync or Async signal may be pending.
sig := run.Signals.Sync
if run.Signals.IsEmpty() || sig == base.SigDefer {
goto again
} else if sig == base.SigReturn {
break
} else if sig = run.Signals.Async; sig != base.SigNone {
run.applyAsyncSignal(sig)
}
}
panicking = false
// no need to restore g.IsDefer, g.Signal, g.Interrupt:
// done by defer restore(g, g.IsDefer, interrupt) above
return
}