-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
178 lines (156 loc) · 4.35 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"strings"
x64e "github.com/vishen/go-brainfunk/x64_encoding"
)
type Compiler struct {
x64 *x64e.Builder
program []byte
nextLoopNumber int
loopStack []int
loopNumberToOffset map[int]int32
loopNumberToAddrID map[int]int
memoryIndexMax int32
outputOffset int32 // Offset in program where sys_write fuction is.
}
func NewCompiler(program []byte) *Compiler {
c := &Compiler{
program: program,
loopNumberToOffset: make(map[int]int32),
loopNumberToAddrID: make(map[int]int),
x64: x64e.NewBuilder(),
}
// Some initialisation.
// Set up the .bss segment to contain the cells.
cells := c.x64.BssAdd(1024 * 64) // [1000]int64
c.x64.EmitJmpForwardRelative(23) // Length of stdout function below
c.outputOffset = c.x64.CurrentOffset()
// Add jump to exit above the write, once the write
// has been made into a function that returns
c.x64.EmitMovRegReg(x64e.RCX, x64e.RAX)
c.x64.EmitMovRegImm(x64e.RAX, 4) // sys_write
c.x64.EmitMovRegImm(x64e.RBX, 1) // fd 1: stdout
c.x64.EmitMovRegImm(x64e.RDX, 1)
c.x64.EmitInt(0x80)
c.x64.EmitRet()
c.x64.EmitMovRegImm(x64e.RAX, cells) // mov rax, cells ; current position in cells.
c.x64.EmitMovRegImm(x64e.R15, 0) // mov r15, 0 ; this is where the character to be outputted will be.
return c
}
func (c *Compiler) Build() []byte {
// Add the exit after the generated code.
c.x64.EmitMovRegImm(x64e.RAX, 1) // sys_exit
c.x64.EmitMovRegImm(x64e.RBX, 0) // return code
c.x64.EmitInt(0x80)
return c.x64.Build()
}
func (c *Compiler) EmitInc() {
c.x64.EmitIncMem(x64e.RAX, 0)
}
func (c *Compiler) EmitDec() {
c.x64.EmitDecMem(x64e.RAX, 0)
}
func (c *Compiler) EmitNext() {
c.x64.EmitAddRegImm(x64e.RAX, 64)
c.memoryIndexMax += 1
}
func (c *Compiler) EmitPrev() {
c.x64.EmitSubRegImm(x64e.RAX, 64)
c.memoryIndexMax -= 1
}
func (c *Compiler) EmitLoop() {
c.nextLoopNumber += 1
c.loopStack = append(c.loopStack, c.nextLoopNumber)
c.loopNumberToOffset[c.nextLoopNumber] = c.x64.CurrentOffset()
c.x64.EmitCmpMemImm(x64e.RAX, 0)
addrID := c.x64.EmitJeqNotYetDefined()
c.loopNumberToAddrID[c.nextLoopNumber] = addrID
}
func (c *Compiler) EmitLoopJump() {
loopNumber := c.nextLoopNumber
for i := len(c.loopStack) - 1; i >= 0; i-- {
if c.loopStack[i] == -1 {
continue
}
loopNumber = c.loopStack[i]
c.loopStack[i] = -1
break
}
offset := c.loopNumberToOffset[loopNumber]
c.x64.EmitCmpMemImm(x64e.RAX, 0)
length := c.x64.EmitJneBack(offset) // TODO: Why is it the length of the jne..?
c.x64.CompleteJeq(c.loopNumberToAddrID[loopNumber], c.x64.CurrentOffset(), length)
}
func (c *Compiler) EmitOutputChar() {
c.x64.EmitMovRegReg(x64e.R14, x64e.RAX)
c.x64.EmitCall(c.outputOffset)
c.x64.EmitMovRegReg(x64e.RAX, x64e.R14)
}
func (c *Compiler) ParseAndEmit() error {
loopsCounter := 0
loopsFinished := 0
for _, ch := range c.program {
switch ch {
case '+':
c.EmitInc()
case '-':
c.EmitDec()
case '>':
c.EmitNext()
case '<':
c.EmitPrev()
case '.':
c.EmitOutputChar()
case '[':
loopsCounter += 1
c.EmitLoop()
case ']':
loopsFinished += 1
c.EmitLoopJump()
}
}
if loopsCounter != loopsFinished {
return fmt.Errorf("unbalanced []: %d opened, %d closed\n", loopsCounter, loopsFinished)
}
return nil
}
var (
inputFilename = flag.String("f", "", "path to bainfuck program to compile")
outputBinaryName = flag.String("o", "", "binary executable output name. Defaults to the passed in filename")
)
func usage() {
fmt.Printf("usage: go-brainfunk -f /path/to/brainfuck-file -o <output-binary>\n")
}
func main() {
flag.Parse()
fileToCompile := *inputFilename
if fileToCompile == "" {
fmt.Printf("missing required flag -f <path/to/brainfuck program>\n")
usage()
return
}
program, err := ioutil.ReadFile(fileToCompile)
if err != nil {
log.Fatalf("unable to open file %q: %v", fileToCompile, err)
}
var outputFilename string
if *outputBinaryName != "" {
outputFilename = *outputBinaryName
} else {
fileBase := filepath.Base(fileToCompile)
outputFilename = strings.Replace(fileBase, filepath.Ext(fileBase), "", -1)
}
comp := NewCompiler(program)
if err := comp.ParseAndEmit(); err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(outputFilename, comp.Build(), 0755); err != nil {
log.Fatal(err)
}
fmt.Printf("wrote executable to %s\n", outputFilename)
}