-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
198 lines (190 loc) · 4.95 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
package main
import (
"./jamulator"
"flag"
"fmt"
"os"
"path"
"strings"
)
var (
astFlag bool
assembleFlag bool
disassembleFlag bool
unRomFlag bool
compileFlag bool
romFlag bool
disableOptFlag bool
dumpFlag bool
dumpPreFlag bool
debugFlag bool
recompileFlag bool
)
// TODO: change this to use commands
func init() {
flag.BoolVar(&astFlag, "ast", false, "Print the abstract syntax tree and quit")
flag.BoolVar(&assembleFlag, "asm", false, "Assemble into 6502 machine code")
flag.BoolVar(&disassembleFlag, "dis", false, "Disassemble 6502 machine code")
flag.BoolVar(&romFlag, "rom", false, "Assemble a jam package into an NES ROM")
flag.BoolVar(&unRomFlag, "unrom", false, "Disassemble an NES ROM into a jam package")
flag.BoolVar(&compileFlag, "c", false, "Compile into a native executable")
flag.BoolVar(&disableOptFlag, "O0", false, "Disable optimizations")
flag.BoolVar(&dumpFlag, "d", false, "Dump LLVM IR code for generated code")
flag.BoolVar(&dumpPreFlag, "dd", false, "Dump LLVM IR code for generated code before verifying module")
flag.BoolVar(&debugFlag, "g", false, "Include debug print statements in generated code")
flag.BoolVar(&recompileFlag, "recompile", false, "Recompile an NES ROM into a native binary")
}
func usageAndQuit() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] inputfile [outputfile]\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
func removeExtension(filename string) string {
return filename[0 : len(filename)-len(path.Ext(filename))]
}
func compileFlags() (flags jamulator.CompileFlags) {
if disableOptFlag {
flags |= jamulator.DisableOptFlag
}
if dumpFlag {
flags |= jamulator.DumpModuleFlag
}
if dumpPreFlag {
flags |= jamulator.DumpModulePreFlag
}
if debugFlag {
flags |= jamulator.IncludeDebugFlag
}
return
}
func compile(filename string, program *jamulator.Program) {
outfile := removeExtension(filename) + ".bc"
if flag.NArg() == 2 {
outfile = flag.Arg(1)
}
fmt.Fprintf(os.Stderr, "Compiling to %s\n", outfile)
c, err := program.CompileToFilename(outfile, compileFlags())
if err != nil {
panic(err)
}
if len(c.Errors) != 0 {
fmt.Fprintf(os.Stderr, "Errors:\n%s\n", strings.Join(c.Errors, "\n"))
os.Exit(1)
}
if len(c.Warnings) != 0 {
fmt.Fprintf(os.Stderr, "Warnings:\n%s\n", strings.Join(c.Warnings, "\n"))
}
}
func main() {
flag.Parse()
if flag.NArg() != 1 && flag.NArg() != 2 {
usageAndQuit()
}
filename := flag.Arg(0)
if astFlag || assembleFlag {
fmt.Fprintf(os.Stderr, "Parsing %s\n", filename)
programAst, err := jamulator.ParseFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
if astFlag {
programAst.Print()
}
if !assembleFlag && !compileFlag {
return
}
fmt.Fprintf(os.Stderr, "Assembling %s\n", filename)
program := programAst.ToProgram()
if len(program.Errors) > 0 {
for _, err := range program.Errors {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(1)
}
if compileFlag {
compile(filename, program)
return
}
if assembleFlag {
outfile := removeExtension(filename) + ".bin"
if flag.NArg() == 2 {
outfile = flag.Arg(1)
}
fmt.Fprintf(os.Stderr, "Writing to %s\n", outfile)
err = program.AssembleToFile(outfile)
if err != nil {
panic(err)
}
}
return
} else if unRomFlag || recompileFlag {
fmt.Fprintf(os.Stderr, "loading %s\n", filename)
rom, err := jamulator.LoadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
if unRomFlag {
outdir := removeExtension(filename)
if flag.NArg() == 2 {
outdir = flag.Arg(1)
}
fmt.Fprintf(os.Stderr, "disassembling to %s\n", outdir)
err = rom.DisassembleToDir(outdir)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
return
}
// recompile to native binary
outfile := removeExtension(filename)
if flag.NArg() == 2 {
outfile = flag.Arg(1)
}
err = rom.RecompileToBinary(outfile, compileFlags())
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
return
} else if disassembleFlag {
fmt.Fprintf(os.Stderr, "disassembling %s\n", filename)
p, err := jamulator.DisassembleFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
if compileFlag {
compile(filename, p)
return
}
if disassembleFlag {
outfile := removeExtension(filename) + ".asm"
if flag.NArg() == 2 {
outfile = flag.Arg(1)
}
fmt.Fprintf(os.Stderr, "writing source %s\n", outfile)
err = p.WriteSourceFile(outfile)
if err != nil {
panic(err)
}
}
return
} else if romFlag {
fmt.Fprintf(os.Stderr, "building rom from %s\n", filename)
r, err := jamulator.AssembleRomFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "saving rom %s\n", r.Filename)
err = r.SaveFile(path.Dir(filename))
if err != nil {
panic(err)
}
return
}
usageAndQuit()
}