This repository was archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinst.go
257 lines (211 loc) · 5.39 KB
/
inst.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// SPDX-License-Identifier: Apache-2.0
package gowasm2cpp
import (
"fmt"
"os"
"path/filepath"
"sort"
"text/template"
"golang.org/x/sync/errgroup"
)
func min(a, b int) int {
if a < b {
return a
}
return b
}
func writeInst(dir string, incpath string, namespace string, importFuncs, funcs []*wasmFunc, exports []*wasmExport, globals []*wasmGlobal, types []*wasmType, tables [][]uint32) error {
const groupSize = 64
sort.Slice(funcs, func(a, b int) bool {
return funcs[a].Wasm.Name < funcs[b].Wasm.Name
})
sort.Slice(exports, func(a, b int) bool {
return exports[a].Name < exports[b].Name
})
var g errgroup.Group
g.Go(func() error {
f, err := os.Create(filepath.Join(dir, "inst.h"))
if err != nil {
return err
}
defer f.Close()
m := 0
for _, t := range tables {
if m < len(t) {
m = len(t)
}
}
if err := instHTmpl.Execute(f, struct {
IncludeGuard string
IncludePath string
Namespace string
ImportFuncs []*wasmFunc
Exports []*wasmExport
Funcs []*wasmFunc
Types []*wasmType
Globals []*wasmGlobal
NumFuncs int
NumTable int
NumMaxTableElements int
}{
IncludeGuard: includeGuard(namespace) + "_INST_H",
IncludePath: incpath,
Namespace: namespace,
ImportFuncs: importFuncs,
Exports: exports,
Funcs: funcs,
Types: types,
Globals: globals,
NumFuncs: len(importFuncs) + len(funcs),
NumTable: len(tables),
NumMaxTableElements: m,
}); err != nil {
return err
}
return nil
})
groups := map[byte][]*wasmFunc{}
for _, f := range funcs {
n := f.Wasm.Name
g := n[0]
groups[g] = append(groups[g], f)
}
for gp, fs := range groups {
gp := gp
fs := fs
g.Go(func() error {
f, err := os.Create(filepath.Join(dir, fmt.Sprintf("inst.funcs.%c.cpp", gp)))
if err != nil {
return err
}
defer f.Close()
if err := instFuncCppTmpl.Execute(f, struct {
IncludePath string
Namespace string
Funcs []*wasmFunc
}{
IncludePath: incpath,
Namespace: namespace,
Funcs: fs,
}); err != nil {
return err
}
return nil
})
}
// exports
g.Go(func() error {
f, err := os.Create(filepath.Join(dir, "inst.exports.cpp"))
if err != nil {
return err
}
defer f.Close()
if err := instExportsCppTmpl.Execute(f, struct {
IncludePath string
Namespace string
Exports []*wasmExport
}{
IncludePath: incpath,
Namespace: namespace,
Exports: exports,
}); err != nil {
return err
}
return nil
})
// init
g.Go(func() error {
f, err := os.Create(filepath.Join(dir, "inst.init.cpp"))
if err != nil {
return err
}
defer f.Close()
if err := instInitCppTmpl.Execute(f, struct {
IncludePath string
Namespace string
ImportFuncs []*wasmFunc
Funcs []*wasmFunc
Types []*wasmType
Tables [][]uint32
Globals []*wasmGlobal
}{
IncludePath: incpath,
Namespace: namespace,
ImportFuncs: importFuncs,
Funcs: funcs,
Types: types,
Tables: tables,
Globals: globals,
}); err != nil {
return err
}
return nil
})
if err := g.Wait(); err != nil {
return err
}
return nil
}
var instHTmpl = template.Must(template.New("inst.h").Parse(`// Code generated by go2cpp. DO NOT EDIT.
#ifndef {{.IncludeGuard}}
#define {{.IncludeGuard}}
#include <cstdint>
namespace {{.Namespace}} {
class Mem;
class Import {
public:
virtual ~Import();
{{range $value := .ImportFuncs}}{{$value.CppDecl " " true false}}
{{end -}} };
class Inst {
public:
Inst(Mem* mem, Import* import);
{{range $value := .Exports}}{{$value.CppDecl " "}}
{{end}}
private:
{{range $value := .Types}} using Type{{.Index}} = {{.Cpp}};
{{end}}
union Func {
{{range $value := .Types}} Type{{.Index}} type{{.Index}}_;
{{end}} };
{{range $value := .Funcs}}{{$value.CppDecl " " false false}}
{{end}} Mem* mem_;
Import* import_;
Func funcs_[{{.NumFuncs}}];
uint32_t table_[{{.NumTable}}][{{.NumMaxTableElements}}];
{{range $value := .Globals}} {{$value.Cpp}}
{{end}}};
}
#endif // {{.IncludeGuard}}
`))
var instFuncCppTmpl = template.Must(template.New("inst.funcs.cpp").Parse(`// Code generated by go2cpp. DO NOT EDIT.
#include "{{.IncludePath}}inst.h"
#include "{{.IncludePath}}bits.h"
#include "{{.IncludePath}}mem.h"
#include <cassert>
#include <cmath>
namespace {{.Namespace}} {
{{range $value := .Funcs}}{{$value.CppImpl "Inst" ""}}
{{end}}}
`))
var instExportsCppTmpl = template.Must(template.New("inst.exports.cpp").Parse(`// Code generated by go2cpp. DO NOT EDIT.
#include "{{.IncludePath}}inst.h"
namespace {{.Namespace}} {
{{range $value := .Exports}}{{$value.CppImpl ""}}
{{end}}}
`))
var instInitCppTmpl = template.Must(template.New("inst.init.cpp").Parse(`// Code generated by go2cpp. DO NOT EDIT.
#include "{{.IncludePath}}inst.h"
namespace {{.Namespace}} {
Import::~Import() = default;
Inst::Inst(Mem* mem, Import* import)
: mem_{mem},
import_{import},
table_{
{{range $value := .Tables}} { {{- range $value2 := $value}}{{$value2}}, {{end}} },
{{end}} } {
{{range $value := .ImportFuncs}} funcs_[{{.Index}}].type0_ = nullptr;
{{end}}{{range $value := .Funcs}} funcs_[{{.Index}}].type{{.Type.Index}}_ = &Inst::{{.Identifier}};
{{end}}}
}
`))