-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
345 lines (302 loc) · 9.15 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
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
package main
import (
"fmt"
"io"
"os"
"path"
"strings"
"github.com/linux4life798/gobf/gobflib/il"
"github.com/linux4life798/gobf/gobflib/lang"
. "github.com/linux4life798/gobf/gobflib"
"github.com/spf13/cobra"
)
const (
defaultDataSize = 100000
)
var debugEnabled *bool
func dprintf(format string, a ...interface{}) {
if *debugEnabled {
fmt.Printf(format+"\n", a...)
}
}
func prepareIL(cmd *cobra.Command, bfinput io.Reader, bfinputsize int64) (*il.ILBlock, error) {
flagCompress, _ := cmd.Flags().GetBool("compress")
flagPrune, _ := cmd.Flags().GetBool("prune")
flagVectorize, _ := cmd.Flags().GetBool("vectorize")
flagFullVectorize, _ := cmd.Flags().GetBool("full-vectorize")
if flagFullVectorize {
flagVectorize = true
}
flagOpts, _ := cmd.Flags().GetStringSlice("optimize")
var optimization = make(map[string]bool)
for _, opt := range flagOpts {
optimization[opt] = true
}
dprintf("Reading BF Program")
prgm := NewBFProgram(uint64(bfinputsize), defaultDataSize)
prgm.ReadCommands(bfinput)
var compressCount int
var pruneCount int
var vectorizeCount int
var vectorBalanceCount int
var optimizationCount int
dprintf("Generating IL Representation")
iltree := prgm.CreateILTree()
if flagCompress {
dprintf("Compressing IL")
compressCount += iltree.Compress()
}
if flagPrune {
dprintf("Pruning IL")
pruneCount += iltree.Prune()
}
if flagVectorize {
dprintf("Vectoring IL")
vectorizeCount = iltree.Vectorize()
if !flagFullVectorize {
dprintf("Rebalancing Vectorized IL")
vectorBalanceCount = iltree.VectorBalance()
}
// ILDataAdd -1
// ILDataPtrAdd 0
// ILDataAdd 1
// prune possible datapadd(0) after vector replace
dprintf("Pruning IL")
pruneCount += iltree.Prune()
dprintf("Compressing IL")
compressCount += iltree.Compress()
dprintf("Pruning IL")
pruneCount += iltree.Prune()
if count := iltree.Compress(); count > 0 {
fmt.Println("# Error", count, "Additional Compresses Were Necessary!")
}
if count := iltree.Prune(); count > 0 {
fmt.Println("# Error", count, "Additional Prune Were Necessary!")
}
}
if optimization["lvec"] {
dprintf("Vectorizing IL")
iltree.Vectorize()
pruneCount += iltree.Prune()
compressCount += iltree.Compress()
pruneCount += iltree.Prune()
dprintf("Pattern Linear Vectorizing IL")
optimizationCount = iltree.PatternReplace(il.PatternReplaceLinearVector)
optimizationCount += iltree.Compress()
optimizationCount += iltree.Prune()
if !flagFullVectorize {
dprintf("Rebalancing Vectorized IL")
vectorBalanceCount = iltree.VectorBalance()
dprintf("Pruning IL")
pruneCount += iltree.Prune()
dprintf("Compressing IL")
compressCount += iltree.Compress()
dprintf("Pruning IL")
pruneCount += iltree.Prune()
}
}
if optimization["zero"] {
// TODO: Implement dataset vectoring for situations where lots
// of consecutive cells are set to 0
dprintf("Pattern Zero Replacing IL")
optimizationCount = iltree.PatternReplace(il.PatternReplaceZero)
dprintf("Compressing IL")
optimizationCount += iltree.Compress()
dprintf("Pruning IL")
optimizationCount += iltree.Prune()
}
if *debugEnabled {
if flagCompress {
fmt.Println("Compress Count: ", compressCount)
}
if flagPrune {
fmt.Println("Prune Count: ", pruneCount)
}
if flagVectorize {
fmt.Println("Vectorized Count: ", vectorizeCount)
if !flagFullVectorize {
fmt.Println("Vectors Balance Count: ", vectorBalanceCount,
"(", vectorizeCount-vectorBalanceCount, "remain )")
}
}
if len(flagOpts) > 0 {
fmt.Println("Optimization Count: ", optimizationCount)
}
fmt.Println("Final Block Count: ", iltree.BlockCount())
}
return iltree, nil
}
func BFRun(cmd *cobra.Command, args []string) {
filename := args[0]
f, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open file \"%s\": %v\n", filename, err)
os.Exit(1)
}
defer f.Close()
finfo, err := f.Stat()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to stat file \"%s\": %v\n", filename, err)
os.Exit(1)
}
fsize := finfo.Size()
prgm := NewBFProgram(uint64(fsize), defaultDataSize)
prgm.ReadCommands(f)
if err := prgm.Run(); err != nil {
fmt.Println(err)
}
if *debugEnabled {
fmt.Fprintln(os.Stderr, "Program terminated")
}
}
func BFGenGo(cmd *cobra.Command, args []string) {
flagProfile, _ := cmd.Flags().GetBool("profile")
filename := args[0]
f, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open file \"%s\": %v\n", filename, err)
os.Exit(1)
}
defer f.Close()
finfo, err := f.Stat()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to stat file \"%s\": %v\n", filename, err)
os.Exit(1)
}
il, err := prepareIL(cmd, f, finfo.Size())
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read BF and/or optimize: %v\n", err)
os.Exit(1)
}
output := os.Stdout
if len(args) > 1 {
outputfilename := args[1]
output, err = os.Create(outputfilename)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open file \"%s\": %v\n", outputfilename, err)
os.Exit(1)
}
}
err = lang.ILBlockToGo(il, output, flagProfile)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to generate Go: %v\n", err)
os.Exit(1)
}
}
func BFDumpIL(cmd *cobra.Command, args []string) {
filename := args[0]
f, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open file \"%s\": %v\n", filename, err)
os.Exit(1)
}
defer f.Close()
finfo, err := f.Stat()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to stat file \"%s\": %v\n", filename, err)
os.Exit(1)
}
il, err := prepareIL(cmd, f, finfo.Size())
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read BF and/or optimize: %v\n", err)
os.Exit(1)
}
output := os.Stdout
if len(args) > 1 {
outputfilename := args[1]
output, err = os.Create(outputfilename)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open file \"%s\": %v\n", outputfilename, err)
os.Exit(1)
}
}
il.Dump(output, 0)
}
func BFCompile(cmd *cobra.Command, args []string) {
flagProfile, _ := cmd.Flags().GetBool("profile")
filename := args[0]
f, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open file \"%s\": %v\n", filename, err)
os.Exit(1)
}
defer f.Close()
finfo, err := f.Stat()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to stat file \"%s\": %v\n", filename, err)
os.Exit(1)
}
il, err := prepareIL(cmd, f, finfo.Size())
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read BF and/or optimize: %v\n", err)
os.Exit(1)
}
var outputfilename string
// If no output file specified, use basename (without extension)
// as the program binary.
if len(args) > 1 {
outputfilename = args[1]
} else {
basename := path.Base(filename)
basenameparts := strings.Split(basename, ".")
basename = basenameparts[0]
// Sanity check
if basename == filename {
fmt.Fprintf(os.Stderr, "Error - Asked to overwrite original input file\n")
os.Exit(1)
}
outputfilename = basename
}
dprintf("Compiling IL")
err, tempdir := lang.CompileIL(il, outputfilename, *debugEnabled, flagProfile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error - %v", err)
os.Exit(2)
}
if *debugEnabled {
fmt.Println("TempDir:", tempdir)
}
}
func main() {
var cmdRun = &cobra.Command{
Use: "run <bf file>",
Short: "Run the given bf file",
Long: `This will evoke the interpreter for a specified bf text file`,
Args: cobra.MinimumNArgs(1),
Run: BFRun,
}
var cmdGenGo = &cobra.Command{
Use: "gengo <bf file> [output go file]",
Short: "Generate a Go representation of the given bf file",
Long: `This will parse a given bf text file and generate equivalent Go code`,
Args: cobra.MinimumNArgs(1),
Run: BFGenGo,
}
var cmdDumpIL = &cobra.Command{
Use: "dumpil <bf file> [output go file]",
Short: "Dumps a text representation of the Intermediate Language Tree",
Long: `This will parse the bf file, generate the intermediate tree, run the specified optimizations, and print the tree.`,
Args: cobra.MinimumNArgs(1),
Run: BFDumpIL,
}
var cmdCompile = &cobra.Command{
Use: "compile <bf file> [output go file]",
Short: "Compile the given bf file to a binary",
Long: `This will parse a given bf text file and generate equivalent binary program`,
Args: cobra.MinimumNArgs(1),
Run: BFCompile,
}
var rootCmd = &cobra.Command{Use: "gobf"}
debugEnabled = rootCmd.PersistentFlags().BoolP("debug", "d", false, "Enable debug mode")
rootCmd.PersistentFlags().BoolP("profile", "p", false, "Enable output program self profiling. This will slow down runtime.")
rootCmd.PersistentFlags().BoolP("compress", "C", true, "Enable collapsing of repeat commands")
rootCmd.PersistentFlags().BoolP("prune", "P", true, "Enable pruning of dead commands")
rootCmd.PersistentFlags().BoolP("vectorize", "V", false, "Enable vectorizing of commands in a block")
rootCmd.PersistentFlags().BoolP("full-vectorize", "F", false, "Force full vectorization without deciding cost tradeoff")
rootCmd.PersistentFlags().StringSliceP("optimize", "O", []string{}, "Enables particular optimizations")
rootCmd.AddCommand(cmdRun)
rootCmd.AddCommand(cmdGenGo)
rootCmd.AddCommand(cmdDumpIL)
rootCmd.AddCommand(cmdCompile)
rootCmd.Execute()
}