Skip to content

Commit

Permalink
Added ir as a compiler mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rj45 committed Dec 18, 2021
1 parent e547791 commit de34bf5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
20 changes: 14 additions & 6 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ func SetArch(a Arch) {

var arch Arch

type Mode int

const (
Asm Mode = iota << 1
Assemble
Run
IR
)

type dumper interface {
WritePhase(string, string)
WriteAsmBuf(string, *bytes.Buffer)
Expand Down Expand Up @@ -66,10 +75,9 @@ var _ io.WriteCloser = nopWriteCloser{}

var dump = flag.String("dump", "", "Dump a function to ssa.html")
var trace = flag.Bool("trace", false, "debug program with tracing info")
var ffir2 = flag.Bool("ir2", false, "test ir2 on program (WIP)")
var ngir = flag.String("ngir", "", "Read ngir file")

func Compile(outname, dir string, patterns []string, assemble, run bool) int {
func Compile(outname, dir string, patterns []string, mode Mode) int {
log.SetFlags(log.Lshortfile)

var finalout io.WriteCloser
Expand Down Expand Up @@ -109,7 +117,7 @@ func Compile(outname, dir string, patterns []string, assemble, run bool) int {
finalout = f
}

if *ffir2 {
if mode&IR != 0 {
fe := frontend.NewFrontEnd(dir, patterns...)
fe.Scan()
for fn := fe.NextUnparsedFunc(); fn != nil; fn = fe.NextUnparsedFunc() {
Expand All @@ -125,7 +133,7 @@ func Compile(outname, dir string, patterns []string, assemble, run bool) int {

var binfile string
var asmcmd *exec.Cmd
if assemble {
if mode&Assemble != 0 {
// todo: if specified, allow this to not be a temp file
asmtemp, err := os.CreateTemp("", "nanogo_*.asm")
if err != nil {
Expand Down Expand Up @@ -157,7 +165,7 @@ func Compile(outname, dir string, patterns []string, assemble, run bool) int {
}

var runcmd *exec.Cmd
if run {
if mode&Run != 0 {
args := arch.EmulatorArgs()
args = append(args, binfile)
if *trace {
Expand Down Expand Up @@ -230,7 +238,7 @@ func Compile(outname, dir string, patterns []string, assemble, run bool) int {
if err := asmcmd.Run(); err != nil {
os.Exit(1)
}
if !run {
if mode&Run == 0 {
// todo: read file and emit to finalout
f, err := os.Open(binfile)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestCompilerForRj32(t *testing.T) {
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
arch.SetArch("rj32")
result := compiler.Compile("-", "../testdata/", []string{tC.filename}, true, true)
result := compiler.Compile("-", "../testdata/", []string{tC.filename}, compiler.Assemble|compiler.Run)
if result != 0 {
t.Errorf("test %s failed with code %d", tC.filename, result)
}
Expand All @@ -69,7 +69,7 @@ func TestCompilerForA32(t *testing.T) {
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
arch.SetArch("a32")
result := compiler.Compile("-", "../testdata/", []string{tC.filename}, true, true)
result := compiler.Compile("-", "../testdata/", []string{tC.filename}, compiler.Assemble|compiler.Run)
if result != 0 {
t.Errorf("test %s failed with code %d", tC.filename, result)
}
Expand Down
13 changes: 6 additions & 7 deletions nanogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ func main() {
command := flag.Arg(0)
printUsage := flag.NArg() < 2

assemble := false
run := false
_ = run
mode := compiler.Asm

switch command {
case "i", "ir":
mode = compiler.IR
case "b", "build":
assemble = true
mode = compiler.Assemble
case "r", "run":
assemble = true
run = true
mode = compiler.Assemble | compiler.Run
case "s", "asm":
default:
printUsage = true
Expand Down Expand Up @@ -83,7 +82,7 @@ func main() {
arch.SetArch(*theArch)
}

result := compiler.Compile(outname, *dir, flag.Args()[1:], assemble, run)
result := compiler.Compile(outname, *dir, flag.Args()[1:], mode)

os.Exit(result)
}

0 comments on commit de34bf5

Please sign in to comment.