-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcompiler.go
148 lines (139 loc) · 5.08 KB
/
compiler.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
package compiler
import (
"bytes"
"embed"
"fmt"
"path/filepath"
"text/template"
"github.com/Masterminds/sprig"
"github.com/vsariola/sointu"
"github.com/vsariola/sointu/vm"
)
type Compiler struct {
Template *template.Template
OS string
Arch string
Output16Bit bool
RowSync bool
}
//go:embed templates/amd64-386/* templates/wasm/*
var templateFS embed.FS
// New returns a new compiler using the default .asm templates
func New(os string, arch string, output16Bit bool, rowsync bool) (*Compiler, error) {
var subdir string
if arch == "386" || arch == "amd64" {
subdir = "amd64-386"
} else if arch == "wasm" {
subdir = "wasm"
} else {
return nil, fmt.Errorf("compiler.New failed, because only amd64, 386 and wasm archs are supported (targeted architecture was %v)", arch)
}
tmpl, err := template.New("base").Funcs(sprig.TxtFuncMap()).ParseFS(templateFS, "templates/"+subdir+"/*.*")
if err != nil {
return nil, fmt.Errorf(`could not create templates: %v`, err)
}
return &Compiler{Template: tmpl, OS: os, Arch: arch, RowSync: rowsync, Output16Bit: output16Bit}, nil
}
func NewFromTemplates(os string, arch string, output16Bit bool, rowsync bool, templateDirectory string) (*Compiler, error) {
globPtrn := filepath.Join(templateDirectory, "*.*")
tmpl, err := template.New("base").Funcs(sprig.TxtFuncMap()).ParseGlob(globPtrn)
if err != nil {
return nil, fmt.Errorf(`could not create template based on directory "%v": %v`, templateDirectory, err)
}
return &Compiler{Template: tmpl, OS: os, Arch: arch, RowSync: rowsync, Output16Bit: output16Bit}, nil
}
func (com *Compiler) Library() (map[string]string, error) {
if com.Arch != "386" && com.Arch != "amd64" {
return nil, fmt.Errorf(`compiling as a library is supported only on 386 and amd64 architectures (targeted architecture was %v)`, com.Arch)
}
templates := []string{"library.asm", "library.h"}
features := vm.AllFeatures{}
retmap := map[string]string{}
for _, templateName := range templates {
compilerMacros := *NewCompilerMacros(*com)
compilerMacros.Library = true
featureSetMacros := FeatureSetMacros{features}
x86Macros := *NewX86Macros(com.OS, com.Arch == "amd64", features, false)
data := struct {
CompilerMacros
FeatureSetMacros
X86Macros
}{compilerMacros, featureSetMacros, x86Macros}
populatedTemplate, extension, err := com.compile(templateName, &data)
if err != nil {
return nil, fmt.Errorf(`could not execute template "%v": %v`, templateName, err)
}
retmap[extension] = populatedTemplate
}
return retmap, nil
}
func (com *Compiler) Song(song *sointu.Song) (map[string]string, error) {
if com.Arch != "386" && com.Arch != "amd64" && com.Arch != "wasm" {
return nil, fmt.Errorf(`compiling a song player is supported only on 386, amd64 and wasm architectures (targeted architecture was %v)`, com.Arch)
}
var templates []string
if com.Arch == "386" || com.Arch == "amd64" {
templates = []string{"player.asm", "player.h", "player.inc"}
} else if com.Arch == "wasm" {
templates = []string{"player.wat"}
}
features := vm.NecessaryFeaturesFor(song.Patch)
retmap := map[string]string{}
encodedPatch, err := vm.NewBytecode(song.Patch, features, song.BPM)
if err != nil {
return nil, fmt.Errorf(`could not encode patch: %v`, err)
}
patterns, sequences, err := ConstructPatterns(song)
if err != nil {
return nil, fmt.Errorf(`could not encode song: %v`, err)
}
for _, templateName := range templates {
compilerMacros := *NewCompilerMacros(*com)
featureSetMacros := FeatureSetMacros{features}
songMacros := *NewSongMacros(song)
var populatedTemplate, extension string
var err error
if com.Arch == "386" || com.Arch == "amd64" {
x86Macros := *NewX86Macros(com.OS, com.Arch == "amd64", features, false)
data := struct {
CompilerMacros
FeatureSetMacros
X86Macros
SongMacros
*vm.Bytecode
Patterns [][]byte
Sequences [][]byte
PatternLength int
SequenceLength int
Hold int
}{compilerMacros, featureSetMacros, x86Macros, songMacros, encodedPatch, patterns, sequences, len(patterns[0]), len(sequences[0]), 1}
populatedTemplate, extension, err = com.compile(templateName, &data)
} else if com.Arch == "wasm" {
wasmMacros := *NewWasmMacros()
data := struct {
CompilerMacros
FeatureSetMacros
WasmMacros
SongMacros
*vm.Bytecode
Patterns [][]byte
Sequences [][]byte
PatternLength int
SequenceLength int
Hold int
}{compilerMacros, featureSetMacros, wasmMacros, songMacros, encodedPatch, patterns, sequences, len(patterns[0]), len(sequences[0]), 1}
populatedTemplate, extension, err = com.compile(templateName, &data)
}
if err != nil {
return nil, fmt.Errorf(`could not execute template "%v": %v`, templateName, err)
}
retmap[extension] = populatedTemplate
}
return retmap, nil
}
func (com *Compiler) compile(templateName string, data interface{}) (string, string, error) {
result := bytes.NewBufferString("")
err := com.Template.ExecuteTemplate(result, templateName, data)
extension := filepath.Ext(templateName)
return result.String(), extension, err
}