sointu/vm/generate/generate.go
vsariola 6d2b63a5e9 feat(sointu, vm): implement pure-Go interpreter for bytecode
The old "native" compiler bridged version is now started with cmd/sointu-nativetrack,
while the new pure-Go bytecode implemented bytecode interpreter is started with
cmd/sointu-track

Thus, you do not need any of the CMake / cgo stuff to run cmd/sointu-track
2021-03-03 23:55:58 +02:00

48 lines
1.0 KiB
Go

// +build ignore
package main
import (
"fmt"
"os"
"strings"
"github.com/vsariola/sointu/vm"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
outputFile, err := os.Create("opcodes.go")
check(err)
defer outputFile.Close()
fmt.Fprintln(outputFile, "// Code generated by go generate; DO NOT EDIT.")
fmt.Fprintln(outputFile, "package vm")
fmt.Fprintln(outputFile, "")
fmt.Fprintln(outputFile, "const (")
features := vm.AllFeatures{}
max := 0
for _, instr := range features.Instructions() {
if l := len(instr); max < l {
max = l
}
}
for i, instr := range features.Instructions() {
format := fmt.Sprintf("\top%%-%vv = %%v\n", max)
fmt.Fprintf(outputFile, format, strings.Title(instr), i+1)
}
fmt.Fprintln(outputFile, ")")
fmt.Fprintln(outputFile, "")
fmt.Fprintf(outputFile, "var transformCounts = [...]int{")
for i, instr := range features.Instructions() {
if i > 0 {
fmt.Fprintf(outputFile, ", ")
}
fmt.Fprintf(outputFile, "%v", features.TransformCount(instr))
}
fmt.Fprintln(outputFile, "}")
}