-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.go
655 lines (593 loc) · 13.4 KB
/
parser.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
package main
import (
"fmt"
"os"
"strconv"
)
type symbolCollection struct {
symbolsInLexicalOrder []string
symbolsAppeared map[string]bool
}
func (p *parser) collectAppearedSymbols(symbol string) {
if !p.sc.symbolsAppeared[symbol] {
p.sc.symbolsInLexicalOrder = append(p.sc.symbolsInLexicalOrder, symbol)
p.sc.symbolsAppeared[symbol] = true
}
}
// https://sourceware.org/binutils/docs-2.37/as.html#Symbol-Names
// Symbol names begin with a letter or with one of ‘._’.
// Symbol names do not start with a digit.
//
// An exception to this rule is made for Local Labels.
func isSymbolBeginning(ch byte) bool {
return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ch == '.' || ch == '_'
}
// On most machines, you can also use $ in symbol names.
func isSymbolLetter(ch byte) bool {
return isSymbolBeginning(ch) || '0' <= ch && ch <= '9' || ch == '$'
}
func (p *parser) atEOF() bool {
return p.idx >= len(p.source)
}
func (p *parser) peekChorEOF() (byte, bool) {
if p.idx == len(p.source) {
return 0, true
}
return p.source[p.idx], false
}
func (p *parser) readCh() byte {
if p.idx == len(p.source) {
panic("Sudden EOF")
}
ch := p.source[p.idx]
p.idx++
return ch
}
func (p *parser) peekCh() byte {
if p.idx == len(p.source) {
panic(fmt.Sprintf("Sudden EOF at idx=%d", p.idx))
}
return p.source[p.idx]
}
// https://sourceware.org/binutils/docs-2.37/as.html#Whitespace
// Whitespace is one or more blanks or tabs, in any order.
// Whitespace is used to separate symbols, and to make programs neater for people to read.
// Unless within character constants (see Character Constants), any whitespace means the same as exactly one space.
func (p *parser) skipWhitespaces() {
for p.idx < len(p.source) && !isStatementTerminator(p.source[p.idx]) {
ch := p.peekCh()
if ch == ' ' || ch == '\t' {
p.idx++
continue
}
return
}
}
func (p *parser) readParenthRegister() *register {
p.expect('(')
if p.peekCh() == '%' {
regi := p.readRegi()
p.expect(')')
return ®ister{name: regi}
} else {
p.fail("TBI")
return nil
}
}
func (p *parser) expect(ch byte) {
if p.source[p.idx] != ch {
panic(fmt.Sprintf("[parser] %c is expected, but got %c at line %d", ch, p.source[p.idx], p.lineno))
}
p.idx++
}
func isAlphaNumeric(ch byte) bool {
return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <= ch && ch <= '9')
}
// e.g. '%rax', '%r10'
func (p *parser) readRegi() string {
p.expect('%')
var buf []byte
for {
ch := p.peekCh()
if isAlphaNumeric(ch) {
p.idx++
buf = append(buf, ch)
} else {
return string(buf)
}
}
}
func (p *parser) readSymbol(first byte) string {
p.expect(first)
buf := []byte{first}
for {
ch := p.peekCh()
if isSymbolLetter(ch) {
buf = append(buf, ch)
p.idx++
} else {
return string(buf)
}
}
}
func (p *parser) readCharLiteral() *charLit {
p.expect('\'')
b := p.readCh()
p.expect('\'')
return &charLit{
val: b,
}
}
func (p *parser) readStringLiteral() *strLit {
p.expect('"')
var buf []byte
for {
ch := p.peekCh()
switch ch {
case '"':
p.idx++
return &strLit{val: string(buf)}
case '\\':
p.expect('\\')
ch := p.peekCh()
var out byte
switch ch {
case 'n':
out = '\n'
case 'r':
out = '\n'
case 't':
out = '\t'
default:
out = ch
}
p.idx++
buf = append(buf, out)
default:
p.idx++
buf = append(buf, ch)
}
}
}
func evalNumExpr(expr expr) int {
switch e := expr.(type) {
case *numberLit:
num, err := strconv.ParseInt(e.val, 0, 32)
if err != nil {
panic(err)
}
return int(num)
case *charLit:
return int(e.val)
case *binaryExpr:
switch e.op {
case "*":
return evalNumExpr(e.left) * evalNumExpr(e.right)
case "+":
return evalNumExpr(e.left) + evalNumExpr(e.right)
case "-":
return evalNumExpr(e.left) - evalNumExpr(e.right)
default:
panic("Unsupported binary operation: " + e.op)
}
}
panic(fmt.Sprintf("unkonwn type %T", expr))
}
// binary or unary or primary expr
func (p *parser) parseArithExpr() expr {
n := p.readNumberLiteral()
p.skipWhitespaces()
ch := p.peekCh()
switch ch {
case '+', '-', '*', '/':
p.idx++
n2 := p.readNumberLiteral()
return &binaryExpr{
op: string(ch),
left: n,
right: n2,
}
default:
// number literal
return n
}
}
type charLit struct {
val uint8
}
type numberLit struct {
val string
}
type strLit struct {
val string
}
func (p *parser) readNumberLiteral() *numberLit {
p.skipWhitespaces()
first := p.readCh()
var buf []byte = []byte{first}
for {
ch := p.peekCh()
if ('0' <= ch && ch <= '9') || ch == 'x' || ('a' <= ch && ch <= 'f') {
buf = append(buf, ch)
p.idx++
} else {
return &numberLit{val: string(buf)}
}
}
}
func (p *parser) parseOperand() Operand {
p.skipWhitespaces()
ch := p.peekCh()
p.assert(!isStatementTerminator(ch), "")
switch {
case isSymbolBeginning(ch):
symbol := p.readSymbol(ch)
p.collectAppearedSymbols(symbol)
switch p.peekCh() {
case '(':
// indirection e.g. 24(%rbp)
regi := p.readParenthRegister()
return &indirection{
expr: &symbolExpr{
name: symbol,
},
regi: regi,
}
case '+': // e.g. foo+8(%rip)
p.expect('+')
e := p.parseArithExpr()
switch p.peekCh() {
case '(':
regi := p.readParenthRegister()
return &indirection{
expr: &binaryExpr{
op: "+",
left: &symbolExpr{name: symbol},
right: e,
},
regi: regi,
}
default:
panic("Unexpected operand format")
}
default:
// just a symbol
return &symbolExpr{name: symbol}
}
case ch == '\'':
return p.readCharLiteral()
case ch == '"':
return p.readStringLiteral()
case '0' <= ch && ch <= '9' || ch == '-': // "24", "-24(%rbp)"
e := p.parseArithExpr()
if p.peekCh() == '(' {
// indirection e.g. 24(%rbp)
regi := p.readParenthRegister()
return &indirection{
expr: e,
regi: regi,
}
} else {
// just a number
numExpr := e
return numExpr
}
case ch == '(':
regi := p.readParenthRegister()
return &indirection{
regi: regi,
}
case ch == '$':
// AT&T immediate operands are preceded by ‘$’;
p.expect('$')
// "$123" "$-7", "$ 2 * 3"
e := p.parseArithExpr()
return &immediate{
expr: e,
}
case ch == '%':
regName := p.readRegi()
return ®ister{
name: regName,
}
case ch == '*': // callq *%rax
p.expect('*')
op := p.parseOperand()
regi, isRegi := op.(*register)
if !isRegi {
panic("Expect register")
}
return &indirectCallTarget{
regi: regi,
}
default:
p.fail("Unknown char '" + string(ch) + "' buf=" + string(p.source[p.idx:p.idx+6]))
}
return nil
}
func (p *parser) parseOperands(keySymbol string) []Operand {
var operands []Operand
for !p.atEOL() {
op := p.parseOperand()
operands = append(operands, op)
p.skipWhitespaces()
if p.peekCh() == ',' {
p.idx++
continue
} else {
break
}
}
return operands
}
func (p *parser) trySymbol() string {
first := p.peekCh()
if isSymbolBeginning(first) {
return p.readSymbol(first)
} else {
p.idx++
return ""
}
}
type parser struct {
path string
lineno int
source []byte
idx int
sc *symbolCollection
}
// indirection | symbolExpr | immediate | register | charLit | strLit
type Operand interface{}
type register struct {
name string // e.g. "rax"
}
func (reg *register) isExt() bool {
switch reg.name {
case "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15":
return true
default:
return false
}
}
func (reg *register) is64() bool {
return reg.name[0] == 'r'
}
func (reg *register) is32() bool {
return reg.name[0] == 'e'
}
func (reg *register) isStackPointer() bool {
return reg.name == "rsp" || reg.name == "sp"
}
func (reg *register) toBits() uint8 {
if len(reg.name) <= 1 {
panic("Something wrong hpapended. reg.name is too short:" + reg.name)
}
var regShortName string
if reg.is64() || reg.is32() {
regShortName = reg.name[1:]
} else {
regShortName = reg.name
}
// https://wiki.osdev.org/X86-64_Instruction_Encoding
// The registers are encoded using the 4-bit values in the X.Reg column of the following table.
// X.Reg is in binary.
var r uint8
switch regShortName {
case "ax", "al":
r = 0b_000
case "cx", "cl":
r = 0b_001
case "dx", "dl":
r = 0b_010
case "bx", "bl":
r = 0b_011
case "sp", "ah":
r = 0b_100
case "bp", "ch":
r = 0b_101
case "si", "dh":
r = 0b_110
case "di", "bh":
r = 0b_111
case "8":
r = 0b_000
case "9":
r = 0b_001
case "10":
r = 0b_010
case "11":
r = 0b_011
case "12":
r = 0b_100
case "13":
r = 0b_101
case "14":
r = 0b_110
case "15":
r = 0b_111
default:
panic(fmt.Sprintf("TBI: unexpected register \"%s\"", regShortName))
}
return r
}
// e.g. *%rax
type indirectCallTarget struct {
expr expr
regi *register
}
// e.g. (%reg), 24(%reg), -24(%reg), foo(%rip), foo+8(%rip)
type indirection struct {
expr expr
regi *register
}
func (op *indirection) isRipRelative() bool {
return op.regi.name == "rip"
}
// $numberExpr
type immediate struct {
expr expr // "7", "-7", "2+3"
}
// "foo" in ".quad foo" or "foo(%rip)"
type symbolExpr struct {
name string
}
type expr interface{}
type binaryExpr struct {
op string /// "+" or "-"
left expr
right expr
}
type Stmt struct {
filePath *string
lineno int
source string // for debug output
labelSymbol string
keySymbol string
operands []Operand
}
func isStatementTerminator(char byte) bool {
return char == '\n' || char == ';'
}
func (p *parser) atEOL() bool {
char := p.source[p.idx]
return isStatementTerminator(char) || char == '#' || char == '/'
}
func (p *parser) fail(msg string) {
panic(msg + " at line " + strconv.Itoa(p.lineno))
}
func (p *parser) assert(bol bool, errorMsg string) {
if !bol {
p.fail("assert failed: " + errorMsg)
}
}
func (p *parser) consumeEOL() {
if p.source[p.idx] == '#' {
p.expect('#')
// In a comment, ';' has no role. Just skip till the first '\n'
for ; p.source[p.idx] != '\n'; p.idx++ {
}
} else if p.source[p.idx] == '/' {
// expect "//" comment
p.expect('/')
p.expect('/')
// In a comment, ';' has no role. Just skip till the first '\n'
for ; p.source[p.idx] != '\n'; p.idx++ {
}
}
for ; !isStatementTerminator(p.source[p.idx]); p.idx++ {
}
p.idx++
p.lineno++
}
// https://sourceware.org/binutils/docs-2.37/as.html#Statements
//
// A statement ends at a newline character (‘\n’) or a line separator character.
// The newline or line separator character is considered to be part of the preceding statement.
// Newlines and separators within character constants are an exception: they do not end statements.
//
// It is an error to end any statement with end-of-file: the last character of any input file should be a newline.
//
// An empty statement is allowed, and may include whitespace. It is ignored.
//
// A statement begins with zero or more labels, optionally followed by a key symbol
// which determines what kind of statement it is.
// The key symbol determines the syntax of the rest of the statement.
// If the symbol begins with a dot ‘.’ then the statement is an assembler directive: typically valid for any computer. If the symbol begins with a letter the statement is an assembly language instruction: it assembles into a machine language instruction. Different versions of as for different computers recognize different instructions. In fact, the same symbol may represent a different instruction in a different computer’s assembly language.
//
// A label is a symbol immediately followed by a colon (:).
// Whitespace before a label or after a colon is permitted, but you may not have whitespace between a label’s symbol and its colon. See Labels.
func (p *parser) parseStmt() *Stmt {
var stmt = &Stmt{
filePath: &p.path,
lineno: p.lineno,
}
p.skipWhitespaces()
if p.atEOL() {
p.consumeEOL()
return stmt
}
symbol := p.trySymbol()
if symbol == "" {
p.consumeEOL()
return stmt
}
var keySymbol string
if p.peekCh() == ':' {
// this symbol is a label
stmt.labelSymbol = symbol
p.collectAppearedSymbols(symbol)
p.skipWhitespaces()
if p.atEOL() {
p.consumeEOL()
return stmt
}
keySymbol = p.trySymbol()
if len(keySymbol) == 0 {
p.skipWhitespaces()
p.consumeEOL()
return stmt
}
} else {
// this symbol is the key symbol in this statement
keySymbol = symbol
}
stmt.keySymbol = keySymbol
p.skipWhitespaces()
if p.atEOL() {
p.consumeEOL()
return stmt
}
operands := p.parseOperands(keySymbol)
stmt.operands = operands
p.consumeEOL()
return stmt
}
func (p *parser) parse() []*Stmt {
var stmts []*Stmt
for !p.atEOF() {
idxBegin := p.idx
s := p.parseStmt()
idxEnd := p.idx - 1
s.source = string(p.source[idxBegin:idxEnd])
stmts = append(stmts, s)
}
return stmts
}
func ParseFiles(files []string) ([]*Stmt, []string) {
var stmts []*Stmt
sc := &symbolCollection{
symbolsAppeared: make(map[string]bool),
}
for _, f := range files {
ss := ParseFile(f, sc)
stmts = append(stmts, ss...)
}
return stmts, sc.symbolsInLexicalOrder
}
func ParseFile(path string, sc *symbolCollection) []*Stmt {
src, err := os.ReadFile(path)
if err != nil {
panic(err)
}
p := &parser{
path: path,
lineno: 1,
source: src,
idx: 0,
sc: sc,
}
stmts := p.parse()
return stmts
}
// For unit tests
func ParseString(src string, sc *symbolCollection) ([]*Stmt, []string) {
p := &parser{
path: "string",
lineno: 1,
source: []byte(src + "\n"),
idx: 0,
sc: sc,
}
stmts := p.parse()
return stmts, sc.symbolsInLexicalOrder
}