-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.go
718 lines (624 loc) · 15.5 KB
/
ast.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package asm
import (
"fmt"
"math/big"
"regexp"
"strconv"
)
// document is the toplevel of the AST. It represents a list of abstract instructions and
// macro definitions.
type document struct {
file string
instructions []astInstruction
labels map[string]*labelDefInstruction
exprMacros map[string]*expressionMacroDef
instrMacros map[string]*instructionMacroDef
parent *document
// instruction macro parameters are passed through this map
instrMacroArgs map[string]instrMacroArg
// for compiler
includes map[*includeInstruction]*document // filled by compiler
creation astStatement
}
type instrMacroArg struct {
calldoc *document // document of callsite
expr astExpr // the argument
}
func (doc *document) lookupLabel(lref *labelRefExpr) (*labelDefInstruction, *document) {
for doc != nil {
li, ok := doc.labels[lref.ident]
if ok && li.dotted == lref.dotted {
return li, doc
}
doc = doc.parent
}
return nil, nil
}
func (doc *document) lookupInstrMacro(name string) (*instructionMacroDef, *document) {
for doc != nil {
if def, ok := doc.instrMacros[name]; ok {
return def, doc
}
doc = doc.parent
}
return nil, nil
}
type astStatement interface {
position() Position
description() string
}
// toplevel statement types
type (
opcodeInstruction struct {
tok token
src *document
arg astExpr // Immediate argument for PUSH* / JUMP*.
pushSize byte // For PUSH<n>, this is n+1.
}
labelDefInstruction struct {
tok token
src *document
dotted bool
global bool
}
macroCallInstruction struct {
ident token
src *document
args []astExpr
}
includeInstruction struct {
tok token
src *document
filename string
}
assembleInstruction struct {
tok token
src *document
filename string
}
)
// definitions
type (
expressionMacroDef struct {
name string
pos Position
params []string
body astExpr
}
instructionMacroDef struct {
name string
pos Position
params []string
body *document
}
)
// expression types
type (
astExpr interface {
eval(e *evaluator, env *evalEnvironment) (*big.Int, error)
}
literalExpr struct {
tok token
value *big.Int
}
labelRefExpr struct {
ident string
dotted bool
global bool
}
variableExpr struct {
ident string
}
macroCallExpr struct {
ident string
builtin bool
args []astExpr
}
arithExpr struct {
op token
left astExpr
right astExpr
}
)
func (inst *macroCallInstruction) position() Position {
return Position{File: inst.src.file, Line: inst.ident.line}
}
func (inst *macroCallInstruction) description() string {
return fmt.Sprintf("invocation of %%%s", inst.ident.text)
}
func (inst *includeInstruction) position() Position {
return Position{File: inst.src.file, Line: inst.tok.line}
}
func (inst *includeInstruction) description() string {
return fmt.Sprintf("#include %q", inst.filename)
}
func (inst *assembleInstruction) position() Position {
return Position{File: inst.src.file, Line: inst.tok.line}
}
func (inst *assembleInstruction) description() string {
return fmt.Sprintf("#assemble %q", inst.filename)
}
func (inst *opcodeInstruction) position() Position {
return Position{File: inst.src.file, Line: inst.tok.line}
}
func (inst *opcodeInstruction) description() string {
return fmt.Sprintf("opcode %s", inst.tok.text)
}
func (inst *labelDefInstruction) position() Position {
return Position{File: inst.src.file, Line: inst.tok.line}
}
func (inst *labelDefInstruction) description() string {
return fmt.Sprintf("definition of %s", inst.String())
}
func (def *instructionMacroDef) position() Position {
return def.pos
}
func (def *instructionMacroDef) description() string {
return fmt.Sprintf("definition of %%%s", def.name)
}
func (def *expressionMacroDef) position() Position {
return def.pos
}
func (def *expressionMacroDef) description() string {
return fmt.Sprintf("definition of %s", def.name)
}
func (l *labelRefExpr) String() string {
dot := ""
if l.dotted {
dot = "."
}
return "@" + dot + l.ident
}
func (l *labelDefInstruction) String() string {
r := labelRefExpr{dotted: l.dotted, ident: l.tok.text}
return r.String()
}
// parser performs parsing of the token stream.
type parser struct {
in <-chan token
buffer []token
doc *document
errors []*parseError
}
func newParser(file string, content []byte, debug bool) *parser {
return &parser{
in: runLexer(content, debug),
doc: newDocument(file, nil),
}
}
func newDocument(file string, parent *document) *document {
return &document{
file: file,
labels: make(map[string]*labelDefInstruction),
exprMacros: make(map[string]*expressionMacroDef),
instrMacros: make(map[string]*instructionMacroDef),
includes: make(map[*includeInstruction]*document),
parent: parent,
}
}
// next reads the next token from the lexer.
func (p *parser) next() token {
if len(p.buffer) > 0 {
t := p.buffer[len(p.buffer)-1]
p.buffer = p.buffer[:len(p.buffer)-1]
return t
}
t := <-p.in
return t
}
// unread puts a token back into the queue for reading.
func (p *parser) unread(t token) {
p.buffer = append(p.buffer, t)
}
// drainLexer runs the lexer to completion.
func (p *parser) drainLexer() {
for p.next().typ != eof {
}
}
// throwError adds a new error to the error list.
// The parser is returned to the toplevel and will continue parsing
// at the next line.
func (p *parser) throwError(tok token, format string, args ...any) {
err := &parseError{tok: tok, file: p.doc.file, err: fmt.Errorf(format, args...)}
p.errors = append(p.errors, err)
// resync to start of next line
for {
switch tok.typ {
case lineEnd, eof:
panic(err)
}
tok = p.next()
}
}
// unexpected signals that an unexpected token occurred in the input.
func (p *parser) unexpected(tok token) {
p.throwError(tok, "unexpected %s %s", tok.typ.String(), tok.text)
}
// parse runs the parser, outputting a document.
func (p *parser) parse() (*document, []*parseError) {
defer p.drainLexer()
for {
if p.parseOne() {
return p.doc, p.errors
}
}
}
func (p *parser) parseOne() bool {
defer func() {
err := recover()
if _, ok := err.(*parseError); !ok && err != nil {
panic(err)
}
}()
return parseStatement(p)
}
// parseExpression parses the input as a single expression.
// This is used in evaluator tests.
func (p *parser) parseExpression() (expr astExpr, err error) {
defer p.drainLexer()
defer func() {
e := recover()
if pe, ok := e.(*parseError); ok {
err = pe
} else if e != nil {
panic(e)
}
}()
// skip lineStart
switch tok := p.next(); tok.typ {
case lineStart:
expr = parseExpr(p, p.next())
return expr, nil
case lineEnd, eof:
p.unexpected(tok)
}
return nil, nil
}
// atDocumentTop reports whether the parser is at the toplevel.
// This returns false while parsing an instruction macro definition.
func (p *parser) atDocumentTop() bool {
return p.doc.parent == nil
}
// ------------- start parser functions -------------
func parseStatement(p *parser) (done bool) {
switch tok := p.next(); tok.typ {
case eof, closeBrace:
if p.atDocumentTop() != (tok.typ == eof) {
p.unexpected(tok)
}
return true
case label, dottedLabel:
parseLabelDef(p, tok)
case directive:
parseDirective(p, tok)
case identifier:
parseInstruction(p, tok)
case instMacroIdent:
parseInstructionMacroCall(p, tok)
case lineStart, lineEnd:
return false
default:
p.unexpected(tok)
}
return false
}
func parseLabelDef(p *parser, tok token) {
name := tok.text
li := &labelDefInstruction{
tok: tok,
src: p.doc,
dotted: tok.typ == dottedLabel,
global: isGlobal(name),
}
p.doc.instructions = append(p.doc.instructions, li)
if firstDef, ok := p.doc.labels[name]; ok {
p.throwError(tok, "%w", errLabelAlreadyDef(firstDef, li))
return
}
p.doc.labels[name] = li
}
func parseDirective(p *parser, tok token) {
switch tok.text {
case "#define":
if !p.atDocumentTop() {
p.throwError(tok, "nested macro definitions are not allowed")
}
parseMacroDef(p)
case "#include":
parseInclude(p, tok)
case "#assemble":
parseAssemble(p, tok)
default:
p.throwError(tok, "unknown compiler directive %q", tok.text)
}
}
func parseMacroDef(p *parser) {
name := p.next()
switch name.typ {
case identifier:
case instMacroIdent:
parseInstructionMacroDef(p, name)
return
default:
p.unexpected(name)
}
// Parse parameters and body.
pos := Position{File: p.doc.file, Line: name.line}
def := &expressionMacroDef{name: name.text, pos: pos}
var didParams bool
loop:
for {
tok := p.next()
switch tok.typ {
case lineEnd, eof:
p.throwError(tok, "incomplete macro definition")
case openBrace:
p.throwError(tok, "unexpected { in expression macro definition")
case openParen:
if !didParams {
def.params = parseParameterList(p)
didParams = true
continue
}
fallthrough
default:
def.body = parseExpr(p, tok)
break loop
}
}
// Register the macro.
checkDuplicateMacro(p, name)
p.doc.exprMacros[name.text] = def
}
func parseInstructionMacroDef(p *parser, nameTok token) {
var params []string
var didParams bool
paramLoop:
for {
switch tok := p.next(); tok.typ {
case lineEnd, eof:
p.throwError(tok, "incomplete macro definition")
case openBrace:
// start of body
break paramLoop
case openParen:
if !didParams {
params = parseParameterList(p)
didParams = true
continue paramLoop
}
}
}
// Set definition context in parser.
topdoc := p.doc
doc := newDocument(p.doc.file, p.doc)
p.doc = doc
defer func() { p.doc = topdoc }()
// Parse macro body.
for !parseStatement(p) {
}
// Register definition.
checkDuplicateMacro(p, nameTok)
pos := Position{File: p.doc.file, Line: nameTok.line}
def := &instructionMacroDef{name: nameTok.text, pos: pos, params: params, body: doc}
doc.creation = def
topdoc.instrMacros[nameTok.text] = def
}
func checkDuplicateMacro(p *parser, nameTok token) {
name := nameTok.text
if _, ok := builtinMacros[name]; ok {
p.throwError(nameTok, "attempt to redefine builtin macro %s", name)
}
if _, ok := p.doc.instrMacros[name]; ok {
p.throwError(nameTok, "instruction macro %s already defined", name)
}
if _, ok := p.doc.exprMacros[name]; ok {
p.throwError(nameTok, "expression macro %s already defined", name)
}
}
func parseInclude(p *parser, d token) {
instr := &includeInstruction{src: p.doc, tok: d}
switch tok := p.next(); tok.typ {
case stringLiteral:
instr.filename = tok.text
p.doc.instructions = append(p.doc.instructions, instr)
default:
p.throwError(tok, "expected filename following #include")
}
}
func parseAssemble(p *parser, d token) {
instr := &assembleInstruction{src: p.doc, tok: d}
switch tok := p.next(); tok.typ {
case stringLiteral:
instr.filename = tok.text
p.doc.instructions = append(p.doc.instructions, instr)
default:
p.throwError(tok, "expected filename following #assemble")
}
}
func parseInstruction(p *parser, tok token) {
opcode := &opcodeInstruction{src: p.doc, tok: tok}
size, isPush := parsePushSize(tok.text)
if isPush {
opcode.pushSize = byte(size + 1)
}
// Register in document.
p.doc.instructions = append(p.doc.instructions, opcode)
// Parse optional argument.
argToken := p.next()
switch argToken.typ {
case lineEnd, eof:
return
default:
opcode.arg = parseExpr(p, argToken)
}
}
var sizedPushRE = regexp.MustCompile("(?i)^PUSH([0-9]*)$")
func parsePushSize(name string) (int, bool) {
m := sizedPushRE.FindStringSubmatch(name)
if len(m) == 0 {
return 0, false
}
if len(m[1]) > 0 {
sz, _ := strconv.Atoi(m[1])
return sz, true
}
return -1, true
}
func parseInstructionMacroCall(p *parser, nameTok token) {
call := ¯oCallInstruction{src: p.doc, ident: nameTok}
p.doc.instructions = append(p.doc.instructions, call)
switch tok := p.next(); tok.typ {
case lineEnd, eof:
return
case openParen:
call.args = parseCallArguments(p)
default:
p.unexpected(tok)
}
}
func parseExpr(p *parser, tok token) astExpr {
switch tok.typ {
case identifier, dottedIdentifier:
arg := ¯oCallExpr{ident: tok.text, builtin: tok.typ == dottedIdentifier}
return parseExprTail(p, arg)
case variableIdentifier:
arg := &variableExpr{ident: tok.text}
return parseExprTail(p, arg)
case labelRef, dottedLabelRef:
arg := &labelRefExpr{
ident: tok.text,
dotted: tok.typ == dottedLabelRef,
global: isGlobal(tok.text),
}
return parseExprTail(p, arg)
case numberLiteral, stringLiteral:
arg := &literalExpr{tok: tok}
return parseExprTail(p, arg)
case openParen:
e := parseParenExpr(p)
return parseExprTail(p, e)
default:
p.unexpected(tok)
return nil
}
}
func parseParenExpr(p *parser) astExpr {
var expr astExpr
switch tok := p.next(); tok.typ {
case closeParen:
p.throwError(tok, "empty parenthesized expression")
return nil
default:
expr = parseExpr(p, tok)
}
// Ensure closing paren is there.
for {
switch tok := p.next(); tok.typ {
case closeParen:
return expr
case lineStart, lineEnd:
continue
default:
p.unexpected(tok)
}
}
}
// parseExprTail parses the end of an expression. Here we check whether the expression
// is a binary arithmetic operation.
func parseExprTail(p *parser, arg astExpr) astExpr {
for {
tok := p.next()
switch {
case tok.is(closeParen, lineEnd, comma, closeBrace, eof):
p.unread(tok)
return arg
case tok.is(openParen):
call, ok := arg.(*macroCallExpr)
if !ok {
p.unexpected(tok)
}
call.args = parseCallArguments(p)
arg = call // continue parsing for arith binop after call
case tok.isArith():
return parseArith(p, arg, tok)
default:
p.unexpected(tok)
}
}
}
// parseParameterList parses a comma-separated list of names.
func parseParameterList(p *parser) (names []string) {
for {
tok := p.next()
switch tok.typ {
case closeParen:
return names
case identifier:
names = append(names, tok.text)
default:
p.unexpected(tok)
}
if parseListEnd(p) {
return names
}
}
}
// parseCallArguments parses the argument list of a macro call.
func parseCallArguments(p *parser) (args []astExpr) {
for {
tok := p.next()
switch tok.typ {
case closeParen:
return args
default:
if arg := parseExpr(p, tok); arg != nil {
args = append(args, arg)
}
}
if parseListEnd(p) {
return args
}
}
}
func parseListEnd(p *parser) bool {
for {
tok := p.next()
switch tok.typ {
case comma:
return false
case lineStart, lineEnd:
continue
case closeParen:
return true
default:
p.unexpected(tok)
}
}
}
func parseArith(p *parser, arg astExpr, opToken token) astExpr {
expr := &arithExpr{op: opToken, left: arg}
tok := p.next()
switch tok.typ {
case lineEnd, eof, closeParen:
p.throwError(tok, "expected right operand in arithmetic expression")
default:
expr.right = parseExpr(p, tok)
}
return expr
}