/
bondgo.go
600 lines (499 loc) · 18.2 KB
/
bondgo.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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"strconv"
"github.com/BondMachineHQ/BondMachine/pkg/bondgo"
"github.com/BondMachineHQ/BondMachine/pkg/etherbond"
"github.com/BondMachineHQ/BondMachine/pkg/udpbond"
)
var verbose = flag.Bool("v", false, "Verbose")
var debug = flag.Bool("d", false, "Verbose")
var registerSize = flag.Int("register-size", 8, "Number of bits per register (n-bit)")
var mpm = flag.Bool("mpm", false, "Use the multi processor mode")
var cascading_io = flag.Bool("cascading-io", false, "Connect the processors in cascading io")
var o = flag.Int("O", 0, "Optimization level")
var showRequirements = flag.Bool("show-requirements", false, "Show bondmachine requirements")
var input_file = flag.String("input-file", "", "Go input file")
var multiAbstractAssemblyInput = flag.Bool("multi-abstract-assembly-input", false, "Input from a multi abstract assembly JSON file")
var abstractAssemblyInput = flag.Bool("abstract-assembly-input", false, "Input from abstract assembly")
var assemblyInput = flag.Bool("assembly-input", false, "Input from assembly")
var goInput = flag.Bool("go-input", true, "Input from Go file")
// Compiler modes:
//
// standard: produce assembly files
// checking: produce assembly files and check if the given machine/bondmachine is suitable for the code execution
// enforcing: the produced assembly has to run on the given machine/bondmachine
// optimizing: the optimized machine/bondmachine is created.
var compiler_mode = flag.String("compiler-mode", "standard", "Compiler mode: standard, checking, enforcing, optimizing")
// Loaded things
// For checking,enforcing
var loadBondmachine = flag.String("load-bondmachine", "", "Filename of the bondmachine to load")
var loadMachine = flag.String("load-machine", "", "Filename of the machine")
// Saved things
// For optimizing mandatory, optional for the others
var saveMachine = flag.String("save-machine", "", "Create a machine JSON file")
var saveBondmachine = flag.String("save-bondmachine", "", "Create a bondmachine JSON file")
// For standard, checking, enforcing
var saveAssembly = flag.String("save-assembly", "", "Machine or bondmachine (numbered per domain) assembly output file")
var useEtherbond = flag.Bool("use-etherbond", false, "Build including etherbond support")
var etherbond_external = flag.String("etherbond-external", "", "Etherbond external peers description file")
var save_etherbond_cluster = flag.String("save-etherbond-cluster", "ebcluster", "Create several BM files and the cluster file with the given prefix")
var use_udpbond = flag.Bool("use-udpbond", false, "Build including udpbond support")
var udpbond_external = flag.String("udpbond-external", "", "Udobond external peers description file")
var save_udpbond_cluster = flag.String("save-udpbond-cluster", "ebcluster", "Create several BM files and the cluster file with the given prefix")
var save_redeployer_file = flag.String("save-redeployer-file", "", "Create a redeployer file out of the cluster")
func check(e error) {
if e != nil {
panic(e)
}
}
func init() {
flag.Parse()
if *saveAssembly == "" {
if *mpm {
if *saveBondmachine == "" {
*saveAssembly = "a.out.asm"
}
} else {
if *saveMachine == "" {
*saveAssembly = "a.out.asm"
}
}
}
if *abstractAssemblyInput || *multiAbstractAssemblyInput {
*goInput = false
}
}
func main() {
fset := token.NewFileSet()
// Setting the global compiler option that will be exported everywhere
config := new(bondgo.BondgoConfig)
config.Debug = *debug
config.Verbose = *verbose
if *mpm {
config.Mpm = true
} else {
config.Mpm = false
}
if *cascading_io {
config.Cascading_io = true
} else {
config.Cascading_io = false
}
switch *registerSize {
case 8:
config.Rsize = uint8(8)
config.Basic_type = "uint8"
config.Basic_chantype = "chan uint8"
case 16:
config.Rsize = uint8(16)
config.Basic_type = "uint16"
config.Basic_chantype = "chan uint16"
case 32:
config.Rsize = uint8(32)
config.Basic_type = "uint32"
config.Basic_chantype = "chan uint32"
case 64:
config.Rsize = uint8(64)
config.Basic_type = "uint64"
config.Basic_chantype = "chan uint64"
default:
fmt.Println("The specified register_size is not usable, defaulting to 8")
config.Rsize = uint8(8)
config.Basic_type = "uint8"
config.Basic_chantype = "chan uint8"
}
if *input_file != "" {
if *goInput {
f, err := parser.ParseFile(fset, *input_file, nil, 0)
if err != nil {
fmt.Println(err)
return
}
usagedone := make(chan bool)
assignerdone := make(chan bool)
results := new(bondgo.BondgoResults) // Results go in here
results.Init_Results(config)
messages := new(bondgo.BondgoMessages) // Compiler logs and errors
messages.Init_Messages(config)
reqmnts := new(bondgo.BondgoRequirements) // The pointer to the requirements struct
reqmnts.Init_Requirements(config)
usagenotify := make(chan bondgo.UsageNotify) // Used to notify the used resource
go reqmnts.Usage_Monitor(usagenotify, usagedone) // Spawn the usage monitor
run := new(bondgo.BondgoRuninfo) // Running data
run.Init_Runinfo(config)
varreq := make(chan bondgo.VarReq) // Variable request
varans := make(chan bondgo.VarAns) // Variable response
go run.Var_assigner(varreq, varans, usagenotify, assignerdone) // Spawn the variable allocator
functs := new(bondgo.BondgoFunctions) // Functions
functs.Init_Functions(config, messages)
vars := make(map[string]bondgo.VarCell)
returns := make([]bondgo.VarCell, 0)
bgmain := &bondgo.BondgoCheck{results, config, reqmnts, run, messages, functs, usagenotify, varreq, varans, nil, nil, vars, returns, "", "", "device_0", 0}
bgmain.Used <- bondgo.UsageNotify{bondgo.TR_PROC, 0, bondgo.C_DEVICE, bgmain.CurrentDevice, bondgo.I_NIL}
if config.Debug {
ast.Print(fset, f)
}
// Load all the functions
ast.Walk(functs, f)
if !bgmain.Is_faulty() {
// Start the main function
executable := false
for ifuncname, ifunc := range functs.Functions {
if ifuncname == "main" {
start_ast := ifunc.Body
ast.Walk(bgmain, start_ast)
executable = true
break
}
}
if !executable {
bgmain.Set_faulty("main function not found.")
}
for procid, _ := range bgmain.Program {
// Add io connections
if bgmain.Cascading_io {
bgmain.WriteLine(procid, "i2r r0 i0")
bgmain.Used <- bondgo.UsageNotify{bondgo.TR_PROC, procid, bondgo.C_OPCODE, "i2r", bondgo.I_NIL}
bgmain.Used <- bondgo.UsageNotify{bondgo.TR_PROC, procid, bondgo.C_INPUT, bondgo.S_NIL, 1}
bgmain.WriteLine(procid, "r2o r0 o0")
bgmain.Used <- bondgo.UsageNotify{bondgo.TR_PROC, procid, bondgo.C_OPCODE, "r2o", bondgo.I_NIL}
bgmain.Used <- bondgo.UsageNotify{bondgo.TR_PROC, procid, bondgo.C_OUTPUT, bondgo.S_NIL, 1}
}
}
for procid, rout := range bgmain.Program {
// TODO Recheck
linesn := len(rout.Lines)
bgmain.Used <- bondgo.UsageNotify{bondgo.TR_PROC, procid, bondgo.C_ROMSIZE, bondgo.S_NIL, linesn}
}
bgmain.Used <- bondgo.UsageNotify{bondgo.TR_EXIT, 0, 0, bondgo.S_NIL, bondgo.I_NIL}
<-usagedone
gent, _ := bondgo.Type_from_string(bgmain.Basic_type)
bgmain.Reqs <- bondgo.VarReq{bondgo.REQ_EXIT, 0, bondgo.VarCell{gent, 0, 0, 0, 0, 0, 0, 0}}
<-assignerdone
}
fmt.Print(bgmain.Dump_log())
if !bgmain.Is_faulty() {
if *showRequirements {
fmt.Print(bgmain.Dump_Requirements())
}
switch *compiler_mode {
case "standard":
var machine_to_save string
savedsomething := false
if *mpm {
machine_to_save = *saveBondmachine
} else {
machine_to_save = *saveMachine
}
if *saveAssembly != "" {
savedsomething = true
if *mpm || *useEtherbond || *use_udpbond {
for i, _ := range bgmain.Program {
if _, err := os.Stat(*saveAssembly + "_" + strconv.Itoa(i)); os.IsNotExist(err) {
f, err := os.Create(*saveAssembly + "_" + strconv.Itoa(i))
check(err)
defer f.Close()
f.WriteString(bgmain.Write_assembly(i))
}
}
} else {
if _, err := os.Stat(*saveAssembly); os.IsNotExist(err) {
f, err := os.Create(*saveAssembly)
check(err)
defer f.Close()
f.WriteString(bgmain.Write_assembly(0))
}
}
}
if *useEtherbond {
if *save_etherbond_cluster != "" {
var external_cluster *etherbond.Cluster
if *etherbond_external != "" {
ebconfig := new(etherbond.Config)
ebconfig.Rsize = uint8(*registerSize)
if *debug {
ebconfig.Debug = true
}
if external_cluster_t, err := etherbond.UnmarshallCluster(ebconfig, *etherbond_external); err != nil {
panic(err)
} else {
external_cluster = external_cluster_t
}
}
mycluster, peerids, mymachines, myethio, myres, err := bgmain.Create_Etherbond_Cluster(*registerSize, external_cluster)
check(err)
//fmt.Println(mycluster, peerids, mymachines, myethio, myres)
if _, err := os.Stat(*save_etherbond_cluster + ".json"); os.IsNotExist(err) {
f, err := os.Create(*save_etherbond_cluster + ".json")
check(err)
b, errj := json.Marshal(mycluster)
check(errj)
_, err = f.WriteString(string(b))
check(err)
f.Close()
}
for i, peerid := range peerids {
machine_to_save := *save_etherbond_cluster + "_peer_" + strconv.Itoa(int(peerid)) + "_bm.json"
if _, err := os.Stat(machine_to_save); os.IsNotExist(err) {
f, err := os.Create(machine_to_save)
check(err)
b, errj := json.Marshal(mymachines[i].Jsoner())
check(errj)
_, err = f.WriteString(string(b))
check(err)
defer f.Close()
}
io_to_save := *save_etherbond_cluster + "_peer_" + strconv.Itoa(int(peerid)) + "_io.json"
if _, err := os.Stat(io_to_save); os.IsNotExist(err) {
f, err := os.Create(io_to_save)
check(err)
b, errj := json.Marshal(myethio[i])
check(errj)
_, err = f.WriteString(string(b))
check(err)
defer f.Close()
}
residual_to_save := *save_etherbond_cluster + "_peer_" + strconv.Itoa(int(peerid)) + "_residual.json"
if _, err := os.Stat(residual_to_save); os.IsNotExist(err) {
f, err := os.Create(residual_to_save)
check(err)
b, errj := json.Marshal(myres[i])
check(errj)
_, err = f.WriteString(string(b))
check(err)
defer f.Close()
}
}
} else {
fmt.Println("Missing output file name")
}
} else if *use_udpbond {
if *save_udpbond_cluster != "" {
var external_cluster *udpbond.Cluster
if *udpbond_external != "" {
ebconfig := new(udpbond.Config)
ebconfig.Rsize = uint8(*registerSize)
if *debug {
ebconfig.Debug = true
}
if external_cluster_t, err := udpbond.UnmarshallCluster(ebconfig, *udpbond_external); err != nil {
panic(err)
} else {
external_cluster = external_cluster_t
}
}
mycluster, peerids, mymachines, myethio, myres, err := bgmain.Create_Udpbond_Cluster(*registerSize, external_cluster)
check(err)
//fmt.Println(mycluster, peerids, mymachines, myethio, myres)
if _, err := os.Stat(*save_udpbond_cluster + ".json"); os.IsNotExist(err) {
f, err := os.Create(*save_udpbond_cluster + ".json")
check(err)
b, errj := json.Marshal(mycluster)
check(errj)
_, err = f.WriteString(string(b))
check(err)
f.Close()
}
for i, peerid := range peerids {
machine_to_save := *save_udpbond_cluster + "_peer_" + strconv.Itoa(int(peerid)) + "_bm.json"
if _, err := os.Stat(machine_to_save); os.IsNotExist(err) {
f, err := os.Create(machine_to_save)
check(err)
b, errj := json.Marshal(mymachines[i].Jsoner())
check(errj)
_, err = f.WriteString(string(b))
check(err)
defer f.Close()
}
io_to_save := *save_udpbond_cluster + "_peer_" + strconv.Itoa(int(peerid)) + "_io.json"
if _, err := os.Stat(io_to_save); os.IsNotExist(err) {
f, err := os.Create(io_to_save)
check(err)
b, errj := json.Marshal(myethio[i])
check(errj)
_, err = f.WriteString(string(b))
check(err)
defer f.Close()
}
residual_to_save := *save_udpbond_cluster + "_peer_" + strconv.Itoa(int(peerid)) + "_residual.json"
if _, err := os.Stat(residual_to_save); os.IsNotExist(err) {
f, err := os.Create(residual_to_save)
check(err)
b, errj := json.Marshal(myres[i])
check(errj)
_, err = f.WriteString(string(b))
check(err)
defer f.Close()
}
}
} else {
fmt.Println("Missing output file name")
}
} else {
if machine_to_save != "" {
savedsomething = true
if *mpm {
if mymachine, _, err := bgmain.Create_Bondmachine(*registerSize, "device_0"); err == nil {
if _, err := os.Stat(machine_to_save); os.IsNotExist(err) {
f, err := os.Create(machine_to_save)
check(err)
defer f.Close()
b, errj := json.Marshal(mymachine.Jsoner())
check(errj)
_, err = f.WriteString(string(b))
check(err)
}
} else {
fmt.Println("Creating bondmachine failed")
}
} else {
if mymachine, ok := bgmain.Create_Connecting_Processor(*registerSize, 0); ok {
if _, err := os.Stat(machine_to_save); os.IsNotExist(err) {
f, err := os.Create(machine_to_save)
check(err)
defer f.Close()
b, errj := json.Marshal(mymachine.Jsoner())
check(errj)
_, err = f.WriteString(string(b))
check(err)
}
} else {
fmt.Println("Creating processor failed")
}
}
}
if !savedsomething {
fmt.Println("Missing output file name")
}
}
case "checking":
fmt.Println("TODO Uninplemented")
case "enforcing":
fmt.Println("TODO Uninplemented")
case "optimizing":
fmt.Println("TODO Uninplemented")
default:
fmt.Println("Unknown operating mode")
}
}
} else if *assemblyInput {
// Placeholder for the new assembly engine
} else if *abstractAssemblyInput {
usagedone := make(chan bool)
results := new(bondgo.BondgoResults) // Results go in here
results.Init_Results(config)
messages := new(bondgo.BondgoMessages) // Compiler logs and errors
messages.Init_Messages(config)
reqmnts := new(bondgo.BondgoRequirements) // The pointer to the requirements struct
reqmnts.Init_Requirements(config)
usagenotify := make(chan bondgo.UsageNotify) // Used to notify the used resource
go reqmnts.Usage_Monitor(usagenotify, usagedone) // Spawn the usage monitor
run := new(bondgo.BondgoRuninfo) // Running data
run.Init_Runinfo(config)
varreq := make(chan bondgo.VarReq) // Variable request
varans := make(chan bondgo.VarAns) // Variable response
functs := new(bondgo.BondgoFunctions) // Functions
functs.Init_Functions(config, messages)
vars := make(map[string]bondgo.VarCell)
returns := make([]bondgo.VarCell, 0)
bgmain := &bondgo.BondgoCheck{results, config, reqmnts, run, messages, functs, usagenotify, varreq, varans, nil, nil, vars, returns, "", "", "device_0", 0}
// Establish the Device parameter for the next goroutine
bgmain.Used <- bondgo.UsageNotify{bondgo.TR_PROC, 0, bondgo.C_DEVICE, bgmain.CurrentDevice, bondgo.I_NIL}
f, err := os.Open(*input_file)
if err != nil {
log.Fatal(err)
}
defer f.Close()
source_asm := make([]string, 0)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
currline := scanner.Text()
source_asm = append(source_asm, currline)
bgmain.WriteLine(0, currline)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
bgmain.Abstract_assembler(*registerSize, source_asm, usagenotify)
for procid, rout := range bgmain.Program {
// TODO Recheck
linesn := len(rout.Lines)
bgmain.Used <- bondgo.UsageNotify{bondgo.TR_PROC, procid, bondgo.C_ROMSIZE, bondgo.S_NIL, linesn}
}
bgmain.Used <- bondgo.UsageNotify{bondgo.TR_EXIT, 0, 0, bondgo.S_NIL, bondgo.I_NIL}
<-usagedone
if *saveBondmachine != "" {
if mymachine, _, err := bgmain.Create_Bondmachine(*registerSize, "device_0"); err == nil {
if _, err := os.Stat(*saveBondmachine); os.IsNotExist(err) {
f, err := os.Create(*saveBondmachine)
check(err)
defer f.Close()
b, errj := json.Marshal(mymachine.Jsoner())
check(errj)
_, err = f.WriteString(string(b))
check(err)
}
} else {
fmt.Println("Creating processor failed")
}
}
if *saveMachine != "" {
if mymachine, ok := bgmain.Create_Connecting_Processor(*registerSize, 0); ok {
if _, err := os.Stat(*saveMachine); os.IsNotExist(err) {
f, err := os.Create(*saveMachine)
check(err)
defer f.Close()
b, errj := json.Marshal(mymachine.Jsoner())
check(errj)
_, err = f.WriteString(string(b))
check(err)
}
} else {
fmt.Println("Creating processor failed")
}
}
} else if *multiAbstractAssemblyInput {
aafile := new(bondgo.Abs_assembly)
if _, err := os.Stat(*input_file); err == nil {
if jsonfile, err := ioutil.ReadFile(*input_file); err == nil {
if err := json.Unmarshal([]byte(jsonfile), aafile); err != nil {
panic(err)
}
} else {
panic(err)
}
} else {
panic(err)
}
if *saveBondmachine != "" {
if mymachine, err := bondgo.MultiAsm2BondMachine(*registerSize, aafile); err == nil {
if _, err := os.Stat(*saveBondmachine); os.IsNotExist(err) {
f, err := os.Create(*saveBondmachine)
check(err)
defer f.Close()
b, errj := json.Marshal(mymachine.Jsoner())
check(errj)
_, err = f.WriteString(string(b))
check(err)
}
} else {
fmt.Println("Creating processor failed")
}
}
} else {
fmt.Println("Missing input file type")
}
} else {
fmt.Println("Missing input file")
}
}