-
Notifications
You must be signed in to change notification settings - Fork 0
/
opcode_gen.py
46 lines (38 loc) · 1.15 KB
/
opcode_gen.py
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
#!/usr/bin/env python3
#
# generates opcodes.h
#
preamble = """
#ifndef __OPCODES_H__
#define __OPCODES_H__
/*
* opcodes.h - this file is autogenerated from opcode_gen.py
*
* WASM opcodes are 1 byte wide. We generate a table(array) of opcodes that is indexed by the opcode.
*/
struct _instr opcodes[] = {
"""
end = """
};
#endif /* __OPCODES_H__ */
"""
opcodes = []
for idx in range(0xff + 1):
opcodes.append((hex(idx), "0x0", "unused"))
#
# fill in the opcodes we know about
#
opcodes[0x42] = ("0x42", "NUMERIC", "i32.const", ".num={I32_CONST}")
opcodes[0x43] = ("0x43", "NUMERIC", "i64.const", ".num={I64_CONST}")
opcodes[0x44] = ("0x44", "NUMERIC", "f32.const", ".num={F32_CONST}")
opcodes[0x45] = ("0x45", "NUMERIC", "f64.const", ".num={F64_CONST}")
with open('opcodes.h', 'w') as out:
out.write(preamble)
for idx in range(0xff + 1):
if len(opcodes[idx]) == 3:
(code, subtype, desc) = opcodes[idx]
out.write("{%s, %s, \"%s\"}," % (code, subtype, desc))
else:
(code, subtype, desc, sp) = opcodes[idx]
out.write("{%s, %s, \"%s\", %s}," % (code, subtype, desc, sp))
out.write(end)