/
main.go
308 lines (256 loc) · 7.61 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/BondMachineHQ/BondMachine/pkg/basm"
"github.com/BondMachineHQ/BondMachine/pkg/bmconfig"
"github.com/BondMachineHQ/BondMachine/pkg/bminfo"
"github.com/BondMachineHQ/BondMachine/pkg/bmnumbers"
"github.com/BondMachineHQ/BondMachine/pkg/bondmachine"
"github.com/BondMachineHQ/BondMachine/pkg/procbuilder"
"google.golang.org/protobuf/proto"
)
var verbose = flag.Bool("v", false, "Verbose")
var debug = flag.Bool("d", false, "Verbose")
// BondMachine targets
var bondmachineFile = flag.String("bondmachine", "", "Load a bondmachine JSON file")
var symInFile = flag.String("si", "", "Load a symbols JSON file")
var bmOutFile = flag.String("o", "", "BondMachine Output file")
var bcofOutFile = flag.String("bo", "", "BCOF Output file")
var symOutFile = flag.String("so", "", "Symbols Output file")
// Utils
var getMeta = flag.String("getmeta", "", "Get the metadata of an internal parameter of the BondMachine")
// Optionals
var bmInfoFile = flag.String("bminfo-file", "", "Load additional information about the BondMachine")
var dumpRequirements = flag.String("dump-requirements", "", "Dump the requirements of the BondMachine in a JSON file")
var linearDataRange = flag.String("linear-data-range", "", "Load a linear data range file (with the syntax index,filename)")
// Phases
var listPasses = flag.Bool("list-passes", false, "List the available passes")
var actPasses = flag.String("activate-passes", "", "List of comma separated optional passes to activate (default: none)")
var deactPasses = flag.String("deactivate-passes", "", "List of comma separated optional passes to deactivate (default: none)")
// Optimizations
var listOptimizations = flag.Bool("list-optimizations", false, "List the available optimizations")
var actOptimizations = flag.String("activate-optimizations", "", "List of comma separated optional optimizations to activate (default: none, everything: all)")
// Config
var disableDynamicalMatching = flag.Bool("disable-dynamical-matching", false, "Disable the dynamical matching")
var chooserMinWordSize = flag.Bool("chooser-min-word-size", false, "Choose the minimum word size for the chooser")
var chooserForceSameName = flag.Bool("chooser-force-same-name", false, "Force the chooser to use the same name for the ROM and the RAM")
func init() {
flag.Parse()
// if *debug {
// fmt.Println("basm init")
// }
if *linearDataRange != "" {
if err := bmnumbers.LoadLinearDataRangesFromFile(*linearDataRange); err != nil {
log.Fatal(err)
}
var lqRanges *map[int]bmnumbers.LinearDataRange
for _, t := range bmnumbers.AllDynamicalTypes {
if t.GetName() == "dyn_linear_quantizer" {
lqRanges = t.(bmnumbers.DynLinearQuantizer).Ranges
}
}
for i, t := range procbuilder.AllDynamicalInstructions {
if t.GetName() == "dyn_linear_quantizer" {
dynIst := t.(procbuilder.DynLinearQuantizer)
dynIst.Ranges = lqRanges
procbuilder.AllDynamicalInstructions[i] = dynIst
}
}
}
}
func main() {
var bm *bondmachine.Bondmachine
if *bondmachineFile != "" {
if _, err := os.Stat(*bondmachineFile); err == nil {
// Open the bondmachine file is exists
if bondmachineJSON, err := os.ReadFile(*bondmachineFile); err == nil {
var bmj bondmachine.Bondmachine_json
if err := json.Unmarshal([]byte(bondmachineJSON), &bmj); err == nil {
bm = (&bmj).Dejsoner()
} else {
panic(err)
}
} else {
panic(err)
}
bm.Init()
} else {
bm = nil
}
}
bi := new(basm.BasmInstance)
if *debug {
bi.SetDebug()
}
if *verbose {
bi.SetVerbose()
}
// The BMinfo is always initialized and used internally. If the user wants to use it, it must be loaded from a file. Specifying the file is optional and it will be save only if specified.
bi.BMinfo = new(bminfo.BMinfo)
if *bmInfoFile != "" {
bi.BMinfo = new(bminfo.BMinfo)
if bmInfoJSON, err := os.ReadFile(*bmInfoFile); err == nil {
if err := json.Unmarshal(bmInfoJSON, bi.BMinfo); err != nil {
panic(err)
}
} else {
panic(err)
}
}
bi.BasmInstanceInit(bm)
// Config options
if *disableDynamicalMatching {
bi.Activate(bmconfig.DisableDynamicalMatching)
}
if *chooserMinWordSize {
bi.Activate(bmconfig.ChooserMinWordSize)
}
if *chooserForceSameName {
bi.Activate(bmconfig.ChooserForceSameName)
}
mne := basm.GetPassMnemonic()
if *actPasses != "" {
passA := strings.Split(*actPasses, ",")
for _, p := range passA {
if err := bi.SetActive(p); err != nil {
bi.Alert("Error while activating pass:", err)
return
}
}
}
if *deactPasses != "" {
passD := strings.Split(*deactPasses, ",")
for _, p := range passD {
if err := bi.UnsetActive(p); err != nil {
bi.Alert("Error while deactivating pass:", err)
return
}
}
}
if *listPasses || *debug || *verbose {
fmt.Println("Passes:")
pass := uint64(1)
for i := 1; pass != basm.LAST_PASS; i++ {
opt := basm.IsOptionalPass()[pass]
if bi.ActivePass(pass) {
if opt {
fmt.Printf(" %02d: %s (optional)\n", i, mne[pass])
} else {
fmt.Printf(" %02d: %s\n", i, mne[pass])
}
} else {
fmt.Printf(" %02d: %s (optional, disabled)\n", i, mne[pass])
}
pass = pass << 1
}
}
if *actOptimizations != "" {
optA := strings.Split(*actOptimizations, ",")
for _, o := range optA {
if err := bi.ActivateOptimization(o); err != nil {
bi.Alert("Error while activating optimization:", err)
return
}
}
}
if *listOptimizations || *debug || *verbose {
// TODO: Finish this
}
startAssembling := false
for _, asmFile := range flag.Args() {
startAssembling = true
// Get the file extension
ext := filepath.Ext(asmFile)
switch ext {
case ".basm":
err := bi.ParseAssemblyDefault(asmFile)
if err != nil {
bi.Alert("Error while parsing file:", err)
return
}
case ".ll":
err := bi.ParseAssemblyLLVM(asmFile)
if err != nil {
bi.Alert("Error while parsing file:", err)
return
}
default:
// Default is .basm
err := bi.ParseAssemblyDefault(asmFile)
if err != nil {
bi.Alert("Error while parsing file:", err)
return
}
}
}
if !startAssembling {
return
}
if err := bi.RunAssembler(); err != nil {
bi.Alert(err)
return
}
// All the utils
if *getMeta != "" {
if meta, err := bi.GetMeta(*getMeta); err == nil {
fmt.Println(meta)
} else {
bi.Alert(err)
}
return
}
// Targets
if *bmOutFile != "" {
if err := bi.Assembler2BondMachine(); err != nil {
bi.Alert("Error in creating a BondMachine", err)
return
}
var outF string
if *bmOutFile != "" {
outF = *bmOutFile
} else {
outF = "bondmachine.json"
}
bMach := bi.GetBondMachine()
// Write the bondmachine file (TODO rewrite)
f, _ := os.Create(outF)
defer f.Close()
b, _ := json.Marshal(bMach.Jsoner())
f.WriteString(string(b))
if *bmInfoFile != "" {
// Write the config file
if bmInfoFileJSON, err := json.MarshalIndent(bi.BMinfo, "", " "); err == nil {
os.WriteFile(*bmInfoFile, bmInfoFileJSON, 0644)
} else {
panic(err)
}
}
if *dumpRequirements != "" {
// Write the requirements file
if requirementsJSON, err := json.MarshalIndent(bi.DumpRequirements(), "", " "); err == nil {
os.WriteFile(*dumpRequirements, requirementsJSON, 0644)
} else {
panic(err)
}
}
}
if *bcofOutFile != "" {
if err := bi.Assembler2BCOF(); err != nil {
panic("Error in creating a BCOF file")
} else {
bcofBytes, err := proto.Marshal(bi.GetBCOF())
if err != nil {
panic("failed to marshal BCOF")
}
if err := os.WriteFile(*bcofOutFile, bcofBytes, 0644); err != nil {
panic("failed to write BCOF file")
}
}
}
}