-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathint_help.go
592 lines (459 loc) · 13.4 KB
/
int_help.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
package frontend
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"math"
"math/big"
"strconv"
"strings"
)
func __ignore() { fmt.Sprintf("") }
/*********
* STACK PTR HELPERS
*/
func (self *IntGen) loadStackPtr() *IntInstr {
push := self.makePush("0")
mload := newIntInstr(IntMLoad, "")
concat(push, mload)
return push
}
func (self *IntGen) SetStackPtr(ptr int) *IntInstr {
self.currentStackSize = ptr
push := self.makePush(strconv.Itoa(ptr))
push2 := self.makePush("0")
mstore := newIntInstr(IntMStore, "")
concat(push, push2)
concat(push2, mstore)
return push
}
func (self *IntGen) addStackPtr(size int) *IntInstr {
push := self.makePush(strconv.Itoa(size))
load := self.loadStackPtr()
add := newIntInstr(IntAdd, "")
loc := self.makePush("0")
store := newIntInstr(IntMStore, "")
concat(push, load)
concat(load, add)
concat(add, loc)
concat(loc, store)
return push
}
func (self *IntGen) CheckGlobal() bool {
// Recheck frames for _global_
for e := self.scopes.Front(); e != nil; e = e.Next() {
if _, ok := e.Value.(*Function); ok {
return false
}
}
return true
}
/******************
* END
*/
// XXX This is actually a range function. FIXME
func (gen *IntGen) makeArg(t *SyntaxTree) (*IntInstr, error) {
if t.Type == NilTy {
return gen.pushNil(), nil
}
name := t.Constant
variable := gen.CurrentScope().GetVar(name)
if variable == nil {
return gen.Errorf(t, "undefine variable: '%s'", t.Constant)
}
pushOff, offCons := pushConstant(strconv.Itoa(variable.Size()))
pushLoc, locCons := pushConstant(strconv.Itoa(variable.Offset()))
locCons.ConstRef = t.Constant
concat(pushOff, offCons)
concat(offCons, pushLoc)
concat(pushLoc, locCons)
return pushOff, nil
}
func padr(slice []byte, size int) []byte {
padded := make([]byte, size)
copy(padded[0:len(slice)], slice)
return padded
}
func (gen *IntGen) makeString(tree *SyntaxTree) *IntInstr {
slice := []byte(tree.Constant)
padded := padr(slice, 32)
hexStr := "0x" + hex.EncodeToString(padded)
push := newIntInstr(Instr(int(IntPush1)-1+len(padded)), "")
cons := newIntInstr(IntConst, hexStr)
cons.size = len(padded)
concat(push, cons)
return push
}
func (gen *IntGen) makePush(num string, v ...int) *IntInstr {
var push, cons *IntInstr
if len(v) > 0 {
n, _ := strconv.Atoi(num)
push = newIntInstr(IntPush4, "")
cons = newIntInstr(IntConst, "0x"+hex.EncodeToString(numberToBytes(int32(n), v[0])))
cons.size = v[0]
} else {
push, cons = pushConstant(num)
}
concat(push, cons)
return push
}
func (gen *IntGen) pushNil() *IntInstr {
p1 := concat(gen.makePush("0"), gen.makePush("0"))
p2 := concat(gen.makePush("0"), gen.makePush("0"))
return concat(p1, p2)
}
func constToPush(constant string) (*IntInstr, int) {
num, ok := new(big.Int).SetString(constant, 0)
if !ok {
return newIntInstr(IntIgnore, ""), 0
}
numBytes := len(num.Bytes())
if numBytes == 0 {
numBytes = 1
}
return newIntInstr(Instr(int(IntPush1)-1+numBytes), ""), numBytes
}
func pushConstant(constant string) (*IntInstr, *IntInstr) {
blk1, numBytes := constToPush(constant)
blk2 := newIntInstr(IntConst, constant)
blk2.size = numBytes
return blk1, blk2
}
func numberToBytes(num interface{}, bits int) []byte {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, num)
if err != nil {
panic(err)
}
return buf.Bytes()[buf.Len()-(bits/8):]
}
func (gen *IntGen) compileLambda(memOffset int, tree *SyntaxTree) (*IntInstr, int) {
code, errors := Compiler.Compile(strings.NewReader(tree.Constant))
if len(errors) != 0 {
gen.Errors = append(gen.Errors, errors...)
return newIntInstr(IntIgnore, ""), 0
}
var (
inlineCode = InlineCode{code, nil, 0}
length = gen.makePush(strconv.Itoa(len(code)))
cOffset = newIntInstr(IntPush4, "")
cConst = newIntInstr(IntConst, "")
//mOffset = newIntInstr(IntMSize, "") // gen.makePush("0")
mOffset = gen.makePush("0")
codecopy = newIntInstr(IntCodeCopy, "")
)
cConst.Constant = "0x" + hex.EncodeToString(numberToBytes(int32(0), 32))
cConst.size = 4
inlineCode.OffsetInstr = cConst
cc(length, cOffset, cConst, mOffset, codecopy)
gen.InlineCode = append(gen.InlineCode, inlineCode)
return length, len(code)
}
func (gen *IntGen) bytesToHexInstr(memOffset int, b []byte) (*IntInstr, int) {
ignore := newIntInstr(IntIgnore, "")
var lastPush *IntInstr
i := 0
l := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
for ; i < len(b); i += 32 {
offset := int(math.Min(float64(i+32), float64(len(b))))
hex := hex.EncodeToString(append(b[i:offset], l[0:32-len(b[i:offset])]...))
mem := gen.makePush(strconv.Itoa(i + memOffset))
push := gen.makePush("0x" + hex)
store := newIntInstr(IntMStore, "")
concat(push, mem)
concat(mem, store)
if lastPush != nil {
concat(lastPush, push)
} else {
concat(ignore, push)
}
lastPush = push
}
return ignore, i
}
func (gen *IntGen) stringToInstr(variable Var, b []byte, t Instr) (*IntInstr, int) {
ignore := newIntInstr(IntIgnore, "")
var lastPush *IntInstr
i := 0
// TODO clean this up. Strings can no longer be longer than 32 bytes
for ; i < len(b); i += 32 {
offset := int(math.Min(float64(i+32), float64(len(b))))
hex := hex.EncodeToString(b[i:offset])
mem := gen.makePush(strconv.Itoa(i))
if t != IntSStore {
gen.stringTable[variable.Id()] = append(gen.stringTable[variable.Id()], mem.Next)
}
push := gen.makePush("0x" + hex)
store := newIntInstr(t, "")
concat(push, mem)
concat(mem, store)
if lastPush != nil {
concat(lastPush, push)
} else {
concat(ignore, push)
}
lastPush = push
}
return ignore, i
}
func (gen *IntGen) getMemoryAddress(tree *SyntaxTree) (*IntInstr, error) {
variable := gen.GetVar(tree.Constant)
if variable == nil {
return gen.Errorf(tree, "Undefined variable: %v", tree.Constant)
}
return gen.getAddress(variable), nil
}
// Generates asm for getting a memory address
func (gen *IntGen) getMemory(tree *SyntaxTree) (*IntInstr, error) {
variable := gen.GetVar(tree.Constant)
if variable == nil {
return gen.Errorf(tree, "Undefined variable: %v", tree.Constant)
}
offset := strconv.Itoa(variable.Offset())
rPos := gen.loadStackPtr()
push, cons := pushConstant(offset)
add := newIntInstr(IntAdd, "")
load := newIntInstr(IntMLoad, "")
concat(rPos, push)
concat(push, cons)
concat(cons, add)
concat(add, load)
return rPos, nil
}
func makeStore(offset int) *IntInstr {
push, cons := pushConstant(strconv.Itoa(offset))
store := newIntInstr(IntMStore, "")
concat(push, cons)
concat(cons, store)
return push
}
func (gen *IntGen) assignMemory(offset int) *IntInstr {
ptr := gen.loadStackPtr()
push, cons := pushConstant(strconv.Itoa(offset))
add := newIntInstr(IntAdd, "")
store := newIntInstr(IntMStore, "")
concat(ptr, push)
concat(push, cons)
concat(cons, add)
concat(add, store)
return ptr
}
// Generates asm for setting a memory address
func (gen *IntGen) setMemory(tree *SyntaxTree) (*IntInstr, error) {
variable := gen.GetVar(tree.Constant)
if variable == nil {
return gen.Errorf(tree, "Undefined variable '%s'", tree.Constant)
}
instr := gen.assignMemory(variable.Offset())
return instr, nil
}
func (gen *IntGen) initNewNumber(tree *SyntaxTree) (*IntInstr, error) {
name := tree.Constant
scope := gen.CurrentScope()
var err error
if tree.Ptr {
_, err = scope.NewVar(name, varPtrTy)
} else {
_, err = scope.NewVar(name, varUndefinedTy)
}
if err != nil {
return gen.Errorf(tree, "%v", err)
}
return nil, nil
}
func (gen *IntGen) sizeof(tree *SyntaxTree) (*IntInstr, error) {
name := tree.Constant
variable := gen.CurrentScope().GetVar(name)
if variable == nil {
return gen.Errorf(tree, "undefined variable: '%s'", name)
}
push, constant := pushConstant(strconv.Itoa(variable.Size()))
return concat(push, constant), nil
}
func (gen *IntGen) getAddress(v Var) *IntInstr {
// Get stack ptr
ptr := gen.loadStackPtr()
// Push the variable offset
push, cons := pushConstant(strconv.Itoa(v.Offset()))
add := newIntInstr(IntAdd, "")
cc(ptr, push, cons, add)
return ptr
}
func (gen *IntGen) pushDerefPtr(tree *SyntaxTree) (*IntInstr, error) {
name := tree.Constant
variable := gen.CurrentScope().GetVar(name)
if variable == nil {
return gen.Errorf(tree, "undefined array: %v", name)
}
ptr := gen.dereferencePtr(variable)
loadRef := newIntInstr(IntMLoad, "")
cc(ptr, loadRef)
return ptr, nil
}
func (gen *IntGen) dereferencePtr(v Var) *IntInstr {
ptr := gen.getAddress(v)
loadPtr := newIntInstr(IntMLoad, "")
cc(ptr, loadPtr)
return ptr
}
func (gen *IntGen) setPtr(tree *SyntaxTree) (*IntInstr, error) {
name := tree.Constant
variable := gen.CurrentScope().GetVar(name)
if variable == nil {
return gen.Errorf(tree, "undefined array: %v", name)
}
ptr := gen.dereferencePtr(variable)
store := newIntInstr(IntMStore, "")
cc(ptr, store)
return ptr, nil
}
func (gen *IntGen) getPtr(tree *SyntaxTree) (*IntInstr, error) {
name := tree.Constant
variable := gen.CurrentScope().GetVar(name)
if variable == nil {
return gen.Errorf(tree, "undefined array: %v", name)
}
// Get stack ptr
ptr := gen.loadStackPtr()
// Push the variable offset
push, cons := pushConstant(strconv.Itoa(variable.Offset()))
cc(ptr, push, cons)
return ptr, nil
return gen.getAddress(variable), nil
}
func (gen *IntGen) getArray(tree *SyntaxTree) (*IntInstr, error) {
name := tree.Constant
variable := gen.CurrentScope().GetVar(name)
if variable == nil {
return gen.Errorf(tree, "undefined array: %v", name)
}
// TODO optimize if the expression in offset. If regular const (i.e. 0-9)
// do an inline calculation instead.
// Dereference pointer
ptr := gen.getAddress(variable)
if IsPtr(variable) {
ptr = gen.dereferencePtr(variable)
}
// Get the offset (= expression between brackets [expression])
offset := gen.MakeIntCode(tree.Children[0])
// Get the size of the variable in bytes
size, sizeConst := pushConstant("32")
// Multiply offset with size
mul := newIntInstr(IntMul, "")
// Add the result to the memory location
iAdd := newIntInstr(IntAdd, "")
// b = a[0] // loc(a) + sizeOf(type(a)) * len(a)
load := newIntInstr(IntMLoad, "")
cc(ptr, offset, size, sizeConst, mul, iAdd, load)
return ptr, nil
}
func (gen *IntGen) setArray(tree *SyntaxTree) (*IntInstr, error) {
name := tree.Constant
variable := gen.CurrentScope().GetVar(name)
if variable == nil {
return gen.Errorf(tree, "undefined array: %v", name)
}
val := gen.MakeIntCode(tree.Children[1])
ptr := gen.getAddress(variable)
// Get the offset (= expression between brackets [expression])
expr := gen.MakeIntCode(tree.Children[0])
// The value which we want to assign
size, sizeConst := pushConstant("32")
// Multiply offset with size
mul := newIntInstr(IntMul, "")
// Add the result to the memory location (ptr + offset + expr)
iAdd := newIntInstr(IntAdd, "")
store := newIntInstr(IntMStore, "")
cc(val, ptr, expr, size, sizeConst, mul, iAdd, store)
return val, nil
}
func (gen *IntGen) setArrayWithIndex(variable Var, value *SyntaxTree, idx int) *IntInstr {
val := gen.MakeIntCode(value)
ptr := gen.getAddress(variable)
push, cons := pushConstant(strconv.Itoa(idx))
size, sizeConst := pushConstant("32")
mul := newIntInstr(IntMul, "")
iAdd := newIntInstr(IntAdd, "")
store := newIntInstr(IntMStore, "")
cc(val, ptr, push, cons, size, sizeConst, mul, iAdd, store)
return val
}
func (gen *IntGen) initNewArray(tree *SyntaxTree) (*IntInstr, error) {
name := tree.Constant
variable := gen.CurrentScope().GetVar(name)
variable, err := gen.CurrentScope().NewVar(name, varArrTy)
if err != nil {
return gen.Errorf(tree, "Redeclaration of variable '%s'", name)
}
length, _ := strconv.Atoi(tree.Size)
variable.SetSize(32 * length)
return newIntInstr(IntIgnore, ""), nil
}
func (gen *IntGen) err(err error) *IntInstr {
gen.addError(err)
return newIntInstr(IntIgnore, "")
}
func (gen *IntGen) setVariable(tree *SyntaxTree, identifier *SyntaxTree) *IntInstr {
variable := gen.GetVar(identifier.Constant)
var lhs, rhs *IntInstr
var err error
switch identifier.Type {
case DerefPtrTy:
lhs, err = gen.setPtr(identifier)
if err != nil {
return gen.err(err)
}
default:
lhs = gen.MakeIntCode(identifier)
}
if lhs.Code == IntErr {
return lhs
}
switch tree.Type {
case StringTy:
if len(tree.Constant) > 32 {
c, err := gen.Errorf(tree, "attempting to store string greater than 32 bytes")
gen.addError(err)
return c
}
//var t Instr
if identifier.Type == SetStoreTy {
padded := padr([]byte(tree.Constant), 32)
rhs = gen.makePush("0x" + hex.EncodeToString(padded))
break
} else {
//t = IntMStore
variable.SetType(varStrTy)
}
/*
var length int
rhs, length = gen.stringToInstr(variable, []byte(tree.Constant), t)
if identifier.Type != SetStoreTy {
variable.SetSize(int(math.Max(math.Max(float64(variable.Size()), float64(length)), 32.0)))
}
*/
rhs = gen.makeString(tree)
variable.SetSize(32)
case ConstantTy:
if identifier.Type != SetStoreTy {
variable.SetType(varNumTy)
}
rhs = gen.MakeIntCode(tree)
case DerefPtrTy:
rhs = cc(gen.MakeIntCode(tree), newIntInstr(IntMLoad, ""))
case InitListTy:
// Push all arguments on to the local VM stack
argsPush := newIntInstr(IntIgnore, "")
for i, arg := range tree.ArgList {
instr := gen.setArrayWithIndex(variable, arg, i)
concat(argsPush, instr)
}
// Return without concat
return argsPush
default:
rhs = gen.MakeIntCode(tree)
}
concat(rhs, lhs)
return rhs
}