-
Notifications
You must be signed in to change notification settings - Fork 11
/
expr.go
657 lines (565 loc) · 15.8 KB
/
expr.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
// Copyright 2014-2018 Brett Vickers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package asm
import (
"fmt"
"strconv"
)
//
// exprOp
//
type exprOp byte
const (
// operators in descending order of precedence
// unary operations (0..5)
// opUnaryMinus
// opUnaryPlus
// opUnaryLessThan
// opUnaryGreaterThan
// opUnarySlash
// opBitwiseNEG
// binary operations (6..15)
// opMultiply
// opDivide
// opModulo
// opAdd
// opSubstract
// opShiftLeft
// opShiftRight
// opBitwiseAND
// opBitwiseXOR
// opBitwiseOR
// value "operations" (16..19)
opNumber exprOp = iota + 16
opString
opIdentifier
opHere
// pseudo-ops (20..21) (used only during parsing but not stored in expr's)
opLeftParen
opRightParen
)
type opdata struct {
precedence byte
children int
leftAssociative bool
symbol string
eval func(a, b int) int
}
func (o *opdata) isBinary() bool {
return o.children == 2
}
func (o *opdata) isUnary() bool {
return o.children == 1
}
// One entry per exprOp value (order must match)
var ops = []opdata{
// unary operations
{7, 1, false, "-", func(a, b int) int { return -a }}, // uminus
{7, 1, false, "+", func(a, b int) int { return a }}, // uplus
{7, 1, false, "<", func(a, b int) int { return a & 0xff }}, // ulessthan
{7, 1, false, ">", func(a, b int) int { return (a >> 8) & 0xff }}, // ugreaterthan
{7, 1, false, "/", func(a, b int) int { return (a >> 8) & 0xff }}, // uslash
{7, 1, false, "~", func(a, b int) int { return 0xffffffff ^ a }}, // bitneg
// binary operations
{6, 2, true, "*", func(a, b int) int { return a * b }}, // multiply
{6, 2, true, "/", func(a, b int) int { return a / b }}, // divide
{6, 2, true, "%", func(a, b int) int { return a % b }}, // modulo
{5, 2, true, "+", func(a, b int) int { return a + b }}, // add
{5, 2, true, "-", func(a, b int) int { return a - b }}, // subtract
{4, 2, true, "<<", func(a, b int) int { return a << uint32(b) }}, // shift_left
{4, 2, true, ">>", func(a, b int) int { return a >> uint32(b) }}, // shift_right
{3, 2, true, "&", func(a, b int) int { return a & b }}, // and
{2, 2, true, "^", func(a, b int) int { return a ^ b }}, // xor
{1, 2, true, "|", func(a, b int) int { return a | b }}, // or
// value "operations"
{0, 0, false, "", nil}, // numeric literal
{0, 0, false, "", nil}, // string literal
{0, 0, false, "", nil}, // identifier
{0, 0, false, "", nil}, // here
// pseudo-operations
{0, 0, false, "", nil}, // lparen
{0, 0, false, "", nil}, // rparen
}
func (op exprOp) isBinary() bool {
return ops[op].isBinary()
}
func (op exprOp) eval(a, b int) int {
return ops[op].eval(a, b)
}
func (op exprOp) symbol() string {
return ops[op].symbol
}
func (op exprOp) isCollapsible() bool {
return ops[op].precedence > 0
}
// Compare the precendence and associativity of 'op' to 'other'.
// Return true if the shunting yard algorithm should cause an
// expression node collapse.
func (op exprOp) collapses(other exprOp) bool {
if ops[op].leftAssociative {
return ops[op].precedence <= ops[other].precedence
}
return ops[op].precedence < ops[other].precedence
}
//
// expr
//
type parseFlags uint32
const (
allowParentheses parseFlags = 1 << iota
allowStrings
)
// An expr represents a single node in a binary expression tree.
// The root node represents an entire expression.
type expr struct {
line fstring // start of expression line
op exprOp // type of expression
value int // resolved value
bytes int // number of bytes to hold the value
address bool // true if value is an address
evaluated bool // true if value has been evaluated
isString bool // true if expr is a string literal (not a value)
stringLiteral fstring // if op == opString
identifier fstring // if op == opIdentifier
scopeLabel fstring // active scope label when parsing began
child0 *expr // first child in expression tree
child1 *expr // second child in expression tree (parent must be binary op)
}
// Return the expression as a postfix notation string.
func (e *expr) String() string {
switch {
case e.op == opNumber:
return fmt.Sprintf("%d", e.value)
case e.op == opString:
return e.stringLiteral.str
case e.op == opIdentifier:
if e.address && (e.identifier.startsWithChar('.') || e.identifier.startsWithChar('@')) {
return "~" + e.scopeLabel.str + e.identifier.str
}
return e.identifier.str
case e.op == opHere:
return "$"
case e.op.isBinary():
return fmt.Sprintf("%s %s %s", e.child0.String(), e.child1.String(), e.op.symbol())
case !e.op.isBinary():
return fmt.Sprintf("%s [%s]", e.child0.String(), e.op.symbol())
default:
return ""
}
}
// Evaluate the expression tree.
func (e *expr) eval(addr int, constants map[string]*expr, labels map[string]int) bool {
if !e.evaluated {
switch {
case e.op == opNumber:
e.evaluated = true
case e.op == opString:
e.evaluated = true
case e.op == opIdentifier:
var ident string
switch {
case e.identifier.startsWithChar('.') || e.identifier.startsWithChar('@'):
ident = "~" + e.scopeLabel.str + e.identifier.str
default:
ident = e.identifier.str
}
if m, ok := constants[ident]; ok {
e.bytes = maxInt(e.bytes, m.bytes)
if m.address {
e.address = true
}
if m.evaluated {
e.value, e.bytes, e.evaluated = m.value, m.bytes, true
}
}
if _, ok := labels[ident]; ok {
e.address, e.bytes = true, 2
}
case e.op == opHere:
if addr != -1 {
e.value, e.bytes, e.address, e.evaluated = addr, 2, true, true
}
case e.op.isBinary():
e.child0.eval(addr, constants, labels)
e.child1.eval(addr, constants, labels)
if e.child0.evaluated && e.child1.evaluated {
e.value = e.op.eval(e.child0.value, e.child1.value)
e.bytes = maxInt(e.child0.bytes, e.child1.bytes)
e.evaluated = true
}
if e.child0.address || e.child1.address {
e.address, e.bytes = true, 2
}
default:
e.child0.eval(addr, constants, labels)
if e.child0.evaluated {
e.value = e.op.eval(e.child0.value, 0)
e.bytes = e.child0.bytes
e.evaluated = true
}
if e.child0.address {
e.address, e.bytes = true, 2
}
}
}
return e.evaluated
}
//
// token
//
type tokentype byte
const (
tokenNil tokentype = iota
tokenOp
tokenNumber
tokenString
tokenIdentifier
tokenHere
tokenLeftParen
tokenRightParen
)
func (tt tokentype) isValue() bool {
return tt == tokenNumber || tt == tokenIdentifier || tt == tokenHere
}
func (tt tokentype) canPrecedeUnaryOp() bool {
return tt == tokenOp || tt == tokenLeftParen || tt == tokenNil
}
type token struct {
typ tokentype
value int
bytes int
stringLiteral fstring
identifier fstring
op exprOp
}
//
// exprParser
//
type exprParser struct {
operandStack stack[*expr]
operatorStack stack[exprOp]
parenCounter int
flags parseFlags
prevTokenType tokentype
errors []asmerror
}
// Parse an expression from the line until it is exhausted.
func (p *exprParser) parse(line, scopeLabel fstring, flags parseFlags) (e *expr, remain fstring, err error) {
p.errors = nil
p.flags = flags
p.prevTokenType = tokenNil
orig := line
// Process expression using Dijkstra's shunting-yard algorithm
for err == nil {
// Parse the next expression token
var token token
token, remain, err = p.parseToken(line)
if err != nil {
break
}
// We're done when the token parser returns the nil token
if token.typ == tokenNil {
break
}
// Handle each possible token type
switch token.typ {
case tokenNumber:
e := &expr{
op: opNumber,
value: token.value,
bytes: token.bytes,
evaluated: true,
}
p.operandStack.push(e)
case tokenString:
e := &expr{
op: opString,
stringLiteral: token.stringLiteral,
isString: true,
bytes: token.bytes,
evaluated: true,
}
p.operandStack.push(e)
case tokenIdentifier:
e := &expr{
op: opIdentifier,
identifier: token.identifier,
scopeLabel: scopeLabel,
}
p.operandStack.push(e)
case tokenHere:
e := &expr{
op: opHere,
bytes: 2,
address: true,
}
p.operandStack.push(e)
case tokenOp:
for err == nil && !p.operatorStack.empty() && token.op.collapses(p.operatorStack.peek()) {
err = collapse(&p.operandStack, p.operatorStack.pop())
if err != nil {
p.addError(line, "invalid expression")
}
}
p.operatorStack.push(token.op)
case tokenLeftParen:
p.operatorStack.push(opLeftParen)
case tokenRightParen:
for err == nil {
if p.operatorStack.empty() {
p.addError(line, "mismatched parentheses")
err = errParse
break
}
op := p.operatorStack.pop()
if op == opLeftParen {
break
}
err = collapse(&p.operandStack, op)
if err != nil {
p.addError(line, "invalid expression")
}
}
}
line = remain
}
// Collapse any operators (and operands) remaining on the stack
for err == nil && !p.operatorStack.empty() {
err = collapse(&p.operandStack, p.operatorStack.pop())
if err != nil {
p.addError(line, "invalid expression")
err = errParse
}
}
if err == nil {
e = p.operandStack.peek()
e.line = orig
}
p.reset()
return e, remain, err
}
// Collapse one or more expression nodes on the top of the
// stack into a combined expression node, and push the combined
// node back onto the stack.
func collapse(s *stack[*expr], op exprOp) error {
switch {
case !op.isCollapsible():
return errParse
case op.isBinary():
if len(s.data) < 2 {
return errParse
}
e := &expr{
op: op,
child1: s.pop(),
child0: s.pop(),
}
s.push(e)
return nil
default:
if s.empty() {
return errParse
}
e := &expr{
op: op,
child0: s.pop(),
}
s.push(e)
return nil
}
}
// Attempt to parse the next token from the line.
func (p *exprParser) parseToken(line fstring) (t token, remain fstring, err error) {
if line.isEmpty() {
return token{typ: tokenNil}, line, nil
}
switch {
case line.startsWithChar('$') && (len(line.str) == 1 || !hexadecimal(line.str[1])):
remain = line.consume(1)
t.typ = tokenHere
t.bytes = 2
case line.startsWith(decimal) || line.startsWithChar('$') || line.startsWithChar('%'):
t.value, t.bytes, remain, err = p.parseNumber(line)
t.typ = tokenNumber
if p.prevTokenType.isValue() || p.prevTokenType == tokenRightParen {
p.addError(line, "invalid numeric literal")
err = errParse
}
case line.startsWithChar('\''):
t.value, remain, err = p.parseCharLiteral(line)
t.bytes = 1
t.typ = tokenNumber
if p.prevTokenType.isValue() || p.prevTokenType == tokenRightParen {
p.addError(line, "invalid character literal")
err = errParse
}
case line.startsWith(stringQuote) && (p.flags&allowStrings) != 0:
t.stringLiteral, remain, err = p.parseStringLiteral(line)
t.bytes = len(t.stringLiteral.str)
t.typ = tokenString
case line.startsWithChar('(') && (p.flags&allowParentheses) != 0:
p.parenCounter++
t.typ, t.op = tokenLeftParen, opLeftParen
remain = line.consume(1)
case line.startsWithChar(')') && (p.flags&allowParentheses) != 0:
if p.parenCounter == 0 {
p.addError(line, "mismatched parentheses")
err = errParse
remain = line.consume(1)
} else {
p.parenCounter--
t.typ, t.op, remain = tokenRightParen, opRightParen, line.consume(1)
}
case line.startsWith(identifierStartChar):
t.typ = tokenIdentifier
t.identifier, remain = line.consumeWhile(identifierChar)
if p.prevTokenType.isValue() || p.prevTokenType == tokenRightParen {
p.addError(line, "invalid identifier")
err = errParse
}
default:
for i, o := range ops {
if o.children > 0 && line.startsWithString(o.symbol) {
if o.isBinary() || (o.isUnary() && p.prevTokenType.canPrecedeUnaryOp()) {
t.typ, t.op, remain = tokenOp, exprOp(i), line.consume(len(o.symbol))
break
}
}
}
if t.typ != tokenOp {
p.addError(line, "invalid token")
err = errParse
}
}
p.prevTokenType = t.typ
remain = remain.consumeWhitespace()
return t, remain, err
}
// Parse a number from the line. The following numeric formats are allowed:
//
// [0-9]+ Decimal number
// $[0-9a-fA-F]+ Hexadecimal number
// 0x[0-9a-fA-F]+ Hexadecimal number
// 0b[01]+ Binary number
//
// The function returns the parsed value, the number of bytes used to
// hold the value, the remainder of the line, and any parsing error
// encountered. The number of bytes used to hold the value will be 1, 2
// or 4.
//
// If a hexadecimal or binary value is parsed, the length of the parsed
// string is used to determine how many bytes are required to hold the
// value. For example, if the parsed string is "0x0020", the number of bytes
// required to hold the value is 2, while if the parse string is "0x20", the
// number of bytes required is 1.
//
// If a decimal number if parsed, the length of the parsed string is ignored,
// and the minimum number of bytes required to hold the value is returned.
func (p *exprParser) parseNumber(line fstring) (value, bytes int, remain fstring, err error) {
// Select decimal, hexadecimal or binary depending on the prefix.
base, fn, bitsPerChar, negative := 10, decimal, 0, false
if line.startsWithChar('-') {
negative = true
line = line.consume(1)
}
switch {
case line.startsWithChar('$'):
line = line.consume(1)
base, fn, bitsPerChar = 16, hexadecimal, 4
case line.startsWithString("0x"):
line = line.consume(2)
base, fn, bitsPerChar = 16, hexadecimal, 4
case line.startsWithChar('%'):
line = line.consume(1)
base, fn, bitsPerChar = 2, binarynum, 1
case line.startsWithString("0b"):
line = line.consume(2)
base, fn, bitsPerChar = 2, binarynum, 1
}
numstr, remain := line.consumeWhile(fn)
num64, converr := strconv.ParseInt(numstr.str, base, 32)
if converr != nil {
p.addError(numstr, "invalid numeric literal")
err = errParse
}
value = int(num64)
if base == 10 {
switch negative {
case true:
switch {
case value <= 0x80:
return 0x100 - value, 1, remain, err
case value <= 0x8000:
return 0x10000 - value, 2, remain, err
default:
return 0x100000000 - value, 4, remain, err
}
case false:
switch {
case value <= 0xff:
return value, 1, remain, err
case value <= 0xffff:
return value, 2, remain, err
default:
return value, 4, remain, err
}
}
}
bytes = (len(numstr.str)*bitsPerChar + 7) / 8
if bytes > 2 {
bytes = 4
}
if negative {
value = -value
}
return value, bytes, remain, err
}
func (p *exprParser) parseStringLiteral(line fstring) (s, remain fstring, err error) {
quote := line.str[0]
remain = line.consume(1)
s, remain = remain.consumeUntilChar(quote)
if remain.isEmpty() {
p.addError(remain, "string literal missing closing quote")
return fstring{}, remain, errParse
}
remain = remain.consume(1)
return s, remain, nil
}
func (p *exprParser) parseCharLiteral(line fstring) (value int, remain fstring, err error) {
if len(line.str) < 3 || line.str[2] != '\'' {
p.addError(line, "invalid character literal")
return 0, fstring{}, errParse
}
value = int(line.str[1])
remain = line.consume(3)
return value, remain, nil
}
func (p *exprParser) addError(line fstring, msg string) {
p.errors = append(p.errors, asmerror{line, msg})
}
func (p *exprParser) reset() {
p.operandStack.data, p.operatorStack.data = nil, nil
p.parenCounter = 0
}
//
// stack
//
type stack[T any] struct {
data []T
}
func (s *stack[T]) push(value T) {
s.data = append(s.data, value)
}
func (s *stack[T]) pop() T {
i := len(s.data) - 1
value := s.data[i]
s.data = s.data[:i]
return value
}
func (s *stack[T]) empty() bool {
return len(s.data) == 0
}
func (s *stack[T]) peek() T {
return s.data[len(s.data)-1]
}