-
Notifications
You must be signed in to change notification settings - Fork 11
/
sections.go
366 lines (286 loc) · 9.35 KB
/
sections.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package wasm
type section struct {
id sectionID
name string
size uint32
}
func (s *section) ID() uint8 { return uint8(s.id) }
func (s *section) Name() string { return s.name }
func (s *section) Size() uint32 { return s.size }
// A Section contains all the information for a single section in the WASM
// file. A file is built up of zero or more sections.
type Section interface {
// ID returns the WASM identifier of the section, for example 0x0A for the
// code section.
ID() uint8
// Name returns the name of the section.
Name() string
// Size returns the size of the section in bytes.
Size() uint32
}
// SectionCustom is a custom or name section added by the compiler that
// generated the WASM file.
type SectionCustom struct {
// SectionName is the name of the section as defined in the wasm file.
SectionName string
// Payload is the raw payload for the section.
Payload []byte
*section
}
// SectionType declares all function type definitions used in the module.
type SectionType struct {
// Entries are the entries in a Type section. Each entry declares one type.
Entries []FuncType
*section
}
// A FuncType is the description of a function signature.
type FuncType struct {
// Form is the value for a func type constructor (always 0x60, the op code
// for a function).
Form int8
// Params contains the parameter types of the function.
Params []int8
// ReturnCount returns the number of results from the function.
// The value will be 0 or 1.
//
// Future version may allow more: https://github.com/WebAssembly/design/issues/1146
ReturnCount uint8
// ReturnType is the result type if ReturnCount > 0.
ReturnTypes []int8
}
// SectionImport declares all imports defined by the module.
type SectionImport struct {
// Entries contains import entries to the module.
Entries []ImportEntry
*section
}
// ImportEntry describes an individual import to the module.
type ImportEntry struct {
// Module is the name of the module.
Module string
// Field is the field name being imported.
Field string
// Kind specified the type of import. The import type value will be set
// depending on the kind, the other ones will be nil.
Kind ExternalKind
// FunctionType describes a function import, if Kind == ExtKindFunction.
FunctionType *FunctionType
// TableType describes a table import, if Kind == ExtKindTable.
TableType *TableType
// MemoryType describes a memory import, if Kind == ExtKindMemory.
MemoryType *MemoryType
// GlobalType describes a global import, if Kind == ExtKindGlobal.
GlobalType *GlobalType
}
// FunctionType the type for a function import.
type FunctionType struct {
// Index is the index of the function signature.
Index uint32
}
// MemoryType is the type for a memory import.
type MemoryType struct {
// Limits contains memory limits defined by the import.
Limits ResizableLimits
}
// TableType is the type for a table import.
type TableType struct {
// ElemType specifies the type of the elements.
ElemType int8
// Limits specifies the resizable limits of the table.
Limits ResizableLimits
}
// GlobalType is the type for a global import.
type GlobalType struct {
// ContentType is the type of the value.
ContentType int8
// Mutable is true if the global value can be modified.
Mutable bool
}
// ResizableLimits describes the limits of a table or memory.
type ResizableLimits struct {
// Initial is the initial length of the memory.
Initial uint32
// Maximum is the maximum length of the memory. May not be set.
Maximum uint32
}
// SectionFunction declares the signatures of all functions in the modules.
// The definitions of the functions will be in the code section.
type SectionFunction struct {
// Types contains a sequence of indices into the type section.
Types []uint32
*section
}
// SectionTable declares a table section. A table is similar to linear memory,
// whose elements, instead of being bytes, are opaque values of a particular
// table element. This allows the table to contain values -- like GC
// references, raw OS handles, or native pointers -- that are accessed by
// WebAssembly code indirectly through an integer index.
//
// https://github.com/WebAssembly/design/blob/master/Semantics.md#table
type SectionTable struct {
Entries []MemoryType
*section
}
// SectionMemory declares a memory section. The section provides an internal
// definition of one linear memory.
//
// https://github.com/WebAssembly/design/blob/master/Modules.md#linear-memory-section
type SectionMemory struct {
Entries []MemoryType
*section
}
// SectionGlobal provides an internal definition of global variables.
//
// https://github.com/WebAssembly/design/blob/master/Modules.md#global-section
type SectionGlobal struct {
Globals []GlobalVariable
*section
}
// A GlobalVariable is a global variable defined by the module.
type GlobalVariable struct {
// Type is the type of the global variable.
Type GlobalType
// Init is an init expression (wasm bytecode) to set the initial value of
// the global variable.
Init []byte
}
// SectionExport declares exports from the WASM module.
//
// https://github.com/WebAssembly/design/blob/master/Modules.md#exports
type SectionExport struct {
Entries []ExportEntry
*section
}
// ExportEntry specifies an individual export from the module.
type ExportEntry struct {
// Field is the name of the field being exported.
Field string
// Kind is the kind of export.
Kind ExternalKind
// Index is the index into the corresponding index space.
//
// https://github.com/WebAssembly/design/blob/master/Modules.md#function-index-space
Index uint32
}
// SectionStart defines the start node, if the module has a start node defined.
//
// https://github.com/WebAssembly/design/blob/master/Modules.md#module-start-function
type SectionStart struct {
// Index is the index to the start function in the function index space.
//
// https://github.com/WebAssembly/design/blob/master/Modules.md#function-index-space
Index uint32
*section
}
// SectionElement defines element segments that initialize elements of imported
// or internally-defined tables with any other definition in the module.
//
// https://github.com/WebAssembly/design/blob/master/Modules.md#elements-section
type SectionElement struct {
// Entries contains the elements.
Entries []ElemSegment
*section
}
// An ElemSegment is an element segment. It initializes a table with initial
// values.
type ElemSegment struct {
// Index is the table index.
Index uint32
// Offset is an init expression (wasm bytecode) to compute the offset at
// which to place the elements.
Offset []byte
// Elems contains the sequence of function indicies.
Elems []uint32
}
// SectionCode contains a function body for every function in the module.
type SectionCode struct {
// Bodies contains all function bodies.
Bodies []FunctionBody
*section
}
// A FunctionBody is the body of a function.
type FunctionBody struct {
// Locals define the local variables of the function.
Locals []LocalEntry
// Code is the wasm bytecode of the function.
Code []byte
}
// LocalEntry is a local variable in a function.
type LocalEntry struct {
// Count specifies the number of the following type.
Count uint32
// Type is the type of the variable.
Type int8
}
// SectionData declares the initialized data that is loaded into the linear
// memory.
type SectionData struct {
// Entries contains the data segment entries.
Entries []DataSegment
*section
}
// A DataSegment is a segment of data in the Data section that is loaded into
// linear memory.
type DataSegment struct {
// Index is the linear memory index.
//
// https://github.com/WebAssembly/design/blob/master/Modules.md#linear-memory-index-space
Index uint32
// Offset is an init expression (wasm bytecode) that computes the offset to
// place the data.
Offset []byte
// Data is the raw data to be placed in memory.
Data []byte
}
// SectionName is a custom section that provides debugging information, by
// matching indices to human readable names.
type SectionName struct {
// SectionName is the name of the name section. The value is always "name".
SectionName string
// Module is the name of the WASM module.
Module string
// Functions contains function name mappings.
Functions *NameMap
// Locals contains local function name mappings.
Locals *Locals
*section
}
// A NameMap is a map that maps an index to a name.
type NameMap struct {
// Names contains a list of mappings in the NameMap.
Names []Naming
}
// Naming is a single function naming. It maps an index to a human readable
// function name.
type Naming struct {
// Index is the index that is being named.
Index uint32
// Name is a UTF-8 name.
Name string
}
// Locals assigns name maps to a subset of functions in the function index
// space (imports and module-defined).
type Locals struct {
// Funcs are the functions to be named.
Funcs []LocalName
}
// LocalName a name mapping for a local function name.
type LocalName struct {
// Index is the index of the function whose locals are being named.
Index uint32
// LocalMap is the name mapping for the function.
LocalMap NameMap
}
// ExternalKind is set as the Kind for an import entry. The value specifies
// what type of import it is.
type ExternalKind uint8
const (
// ExtKindFunction is an imported function.
ExtKindFunction ExternalKind = iota
// ExtKindTable is an imported table.
ExtKindTable
// ExtKindMemory is imported memory.
ExtKindMemory
// ExtKindGlobal is an imported global.
ExtKindGlobal
)