diff options
author | terminaldweller <thabogre@gmail.com> | 2022-02-14 19:22:31 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-02-14 19:22:31 +0000 |
commit | bddd10b1dab1cb88905489f93a77b5a8c465650b (patch) | |
tree | d33e31e4da2b4da846969c4ab21fe45712cdf853 /dwasm-cpp | |
parent | listened to lgtm... (diff) | |
download | dwasm-master.tar.gz dwasm-master.zip |
Diffstat (limited to 'dwasm-cpp')
-rw-r--r-- | dwasm-cpp/dwasm.hpp | 475 | ||||
-rw-r--r-- | dwasm-cpp/misc.hpp | 11 | ||||
-rw-r--r-- | dwasm-cpp/wasm.h | 337 |
3 files changed, 460 insertions, 363 deletions
diff --git a/dwasm-cpp/dwasm.hpp b/dwasm-cpp/dwasm.hpp index 3c9c4a5..3b41e81 100644 --- a/dwasm-cpp/dwasm.hpp +++ b/dwasm-cpp/dwasm.hpp @@ -16,7 +16,8 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/ +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +USA.*/ /**********************************************************************************************************************/ #include <algorithm> #include <map> @@ -26,21 +27,68 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* #define _WASM_HPP #define MAGIC_NUMBER 0x6d736100 #define PAGE_SIZE 65536 -enum class section_id_t {custom, type, import, func, table, memory, global, exp, start, element, code, data, unknown}; -enum class relocation_type_t {R_WEBASSEMBLY_FUNCTION_INDEX_LEB, R_WEBASSEMBLY_TABLE_INDEX_SLEB, - R_WEBASSEMBLY_TABLE_INDEX_I32, R_WEBASSEMBLY_MEMORY_ADDR_LEB, R_WEBASSEMBLY_MEMORY_ADDR_SLEB, - R_WEBASSEMBLY_MEMORY_ADDR_I32, R_WEBASSEMBLY_TYPE_INDEX_LEB, R_WEBASSEMBLY_GLOBAL_INDEX_LEB, - R_WEBASSEMPLY_FUNCTION_OFFSET_I32, R_WEBASSEMBLY_SECTION_OFFSET_I32}; -enum class linking_subsection_t {WASM_SEGMENT_INFO, WASM_INIT_FUNCS, WASM_COMDAT_INFO, WASM_SYMBOL_TABLE}; -enum class syminfo_kind_t {SYMTAB_FUNCTION, SYMTAB_DATA, SYMTAB_GLOBAL, SYMTAB_SECTION}; -enum class w_type_t {uint8, uint16, uint32, uint64, - varuint1, varuint7, varuint32, varuint64, varint1, - varint7, varint32, varint64, floatt, doublet}; +enum class section_id_t { + custom, + type, + import, + func, + table, + memory, + global, + exp, + start, + element, + code, + data, + unknown +}; +enum class relocation_type_t { + R_WEBASSEMBLY_FUNCTION_INDEX_LEB, + R_WEBASSEMBLY_TABLE_INDEX_SLEB, + R_WEBASSEMBLY_TABLE_INDEX_I32, + R_WEBASSEMBLY_MEMORY_ADDR_LEB, + R_WEBASSEMBLY_MEMORY_ADDR_SLEB, + R_WEBASSEMBLY_MEMORY_ADDR_I32, + R_WEBASSEMBLY_TYPE_INDEX_LEB, + R_WEBASSEMBLY_GLOBAL_INDEX_LEB, + R_WEBASSEMPLY_FUNCTION_OFFSET_I32, + R_WEBASSEMBLY_SECTION_OFFSET_I32 +}; +enum class linking_subsection_t { + WASM_SEGMENT_INFO, + WASM_INIT_FUNCS, + WASM_COMDAT_INFO, + WASM_SYMBOL_TABLE +}; +enum class syminfo_kind_t { + SYMTAB_FUNCTION, + SYMTAB_DATA, + SYMTAB_GLOBAL, + SYMTAB_SECTION +}; +enum class w_type_t { + uint8, + uint16, + uint32, + uint64, + varuint1, + varuint7, + varuint32, + varuint64, + varint1, + varint7, + varint32, + varint64, + floatt, + doublet +}; struct wasm_op_t { - wasm_op_t(std::string _mnemonic, bool _has_opnd, std::vector<w_type_t> _opnd_types) { + wasm_op_t(std::string _mnemonic, bool _has_opnd, + std::vector<w_type_t> _opnd_types) { mnemonic = _mnemonic; has_opnd = _has_opnd; - std::for_each(_opnd_types.begin(), _opnd_types.end(), [this](w_type_t __t){this->opnd_types.push_back(__t);}); + std::for_each(_opnd_types.begin(), _opnd_types.end(), + [this](w_type_t __t) { this->opnd_types.push_back(__t); }); } std::string mnemonic; bool has_opnd; @@ -48,187 +96,226 @@ struct wasm_op_t { }; std::map<uint8_t, wasm_op_t> wasm_opcodes = { - //{0x7f, wasm_op_t{"i32", false, {}}}, - //{0x7e, wasm_op_t{"i64", false, {}}}, - //{0x7d, wasm_op_t{"f32", false, {}}}, - //{0x7c, wasm_op_t{"f64", false, {}}}, - //{0x7b, wasm_op_t{"anyfunc", false, {}}}, - //{0x60, wasm_op_t{"func", false, {}}}, - //{0x40, wasm_op_t{"empty_block_type", false, {}}}, - {0x00, wasm_op_t{"unreachable", false, {}}}, - {0x01, wasm_op_t{"nop", false, {}}}, - {0x02, wasm_op_t{"block", true, {w_type_t::varuint7}}}, - {0x03, wasm_op_t{"loop", true, {w_type_t::varuint7}}}, - {0x04, wasm_op_t{"if", true, {w_type_t::varuint7}}}, - {0x05, wasm_op_t{"else", false, {}}}, - {0x0b, wasm_op_t{"end", false, {}}}, - {0x0c, wasm_op_t{"br", true, {w_type_t::varuint32}}}, - {0x0d, wasm_op_t{"br_if", true, {w_type_t::varuint32}}}, - {0x0e, wasm_op_t{"br_table", true, {w_type_t::varuint32, w_type_t::varuint32, w_type_t::varuint32}}}, - {0x0f, wasm_op_t{"return", false, {}}}, - {0x10, wasm_op_t{"call", true, {w_type_t::varuint32}}}, - {0x11, wasm_op_t{"call_indirect", true, {w_type_t::varuint32, w_type_t::varuint1}}}, - {0x1a, wasm_op_t{"drop", false, {}}}, - {0x1b, wasm_op_t{"select", false, {}}}, - {0x20, wasm_op_t{"get_local", true, {w_type_t::varuint32}}}, - {0x21, wasm_op_t{"set_local", true, {w_type_t::varuint32}}}, - {0x22, wasm_op_t{"tee_local", true, {w_type_t::varuint32}}}, - {0x23, wasm_op_t{"get_global", true, {w_type_t::varuint32}}}, - {0x24, wasm_op_t{"set_global", true, {w_type_t::varuint32}}}, - {0x28, wasm_op_t{"i32.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x29, wasm_op_t{"i64.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x2a, wasm_op_t{"f32.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x2b, wasm_op_t{"f64.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x2c, wasm_op_t{"i32.load8_s", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x2d, wasm_op_t{"i32.load8_u", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x2e, wasm_op_t{"i32.load16_s", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x2f, wasm_op_t{"i32.load16_u", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x30, wasm_op_t{"i64.load8_s", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x31, wasm_op_t{"i64.load8_u", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x32, wasm_op_t{"i64.load16_s", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x33, wasm_op_t{"i64.load16_u", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x34, wasm_op_t{"i64.load32_s", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x35, wasm_op_t{"i64.load32_u", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x36, wasm_op_t{"i32.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x37, wasm_op_t{"i64.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x38, wasm_op_t{"f32.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x39, wasm_op_t{"f64.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x3a, wasm_op_t{"i32.store8", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x3b, wasm_op_t{"i32.store16", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x3c, wasm_op_t{"i64.store8", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x3d, wasm_op_t{"i64.store16", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x3e, wasm_op_t{"i64.store32", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x3f, wasm_op_t{"current_memory", true, {w_type_t::varuint1}}}, - {0x40, wasm_op_t{"grow_memory", true, {w_type_t::varuint1}}}, - {0x41, wasm_op_t{"i32.const", true, {w_type_t::varint32}}}, - {0x42, wasm_op_t{"i64.const", true, {w_type_t::varint64}}}, - {0x43, wasm_op_t{"f32.const", true, {w_type_t::uint32}}}, - {0x44, wasm_op_t{"f64.const", true, {w_type_t::uint64}}}, - {0x45, wasm_op_t{"i32.eqz", false, {}}}, - {0x46, wasm_op_t{"i32.eq", false, {}}}, - {0x47, wasm_op_t{"i32.ne", false, {}}}, - {0x48, wasm_op_t{"i32.lt_s", false, {}}}, - {0x49, wasm_op_t{"i32.lt_u", false, {}}}, - {0x4a, wasm_op_t{"i32.gt_s", false, {}}}, - {0x4b, wasm_op_t{"i32.gt_u", false, {}}}, - {0x4c, wasm_op_t{"i32.le_s", false, {}}}, - {0x4d, wasm_op_t{"i32.le_u", false, {}}}, - {0x4e, wasm_op_t{"i32.ge_s", false, {}}}, - {0x4f, wasm_op_t{"i32.ge_u", false, {}}}, - {0x50, wasm_op_t{"i64.eqz", false, {}}}, - {0x51, wasm_op_t{"i64.eq", false, {}}}, - {0x52, wasm_op_t{"i64.ne", false, {}}}, - {0x53, wasm_op_t{"i64.lt_s", false, {}}}, - {0x54, wasm_op_t{"i64.lt_u", false, {}}}, - {0x55, wasm_op_t{"i64.gt_s", false, {}}}, - {0x56, wasm_op_t{"i64.gt_u", false, {}}}, - {0x57, wasm_op_t{"i64.le_s", false, {}}}, - {0x58, wasm_op_t{"i64.le_u", false, {}}}, - {0x59, wasm_op_t{"i64.ge_s", false, {}}}, - {0x5a, wasm_op_t{"i64.ge_u", false, {}}}, - {0x5b, wasm_op_t{"f32.eq", false, {}}}, - {0x5c, wasm_op_t{"f32.ne", false, {}}}, - {0x5d, wasm_op_t{"f32.lt", false, {}}}, - {0x5e, wasm_op_t{"f32.gt", false, {}}}, - {0x5f, wasm_op_t{"f32.le", false, {}}}, - {0x60, wasm_op_t{"f32.ge", false, {}}}, - {0x61, wasm_op_t{"f64.eq", false, {}}}, - {0x62, wasm_op_t{"f64.ne", false, {}}}, - {0x63, wasm_op_t{"f64.lt", false, {}}}, - {0x64, wasm_op_t{"f64.gt", false, {}}}, - {0x65, wasm_op_t{"f64.le", false, {}}}, - {0x66, wasm_op_t{"f64.ge", false, {}}}, - {0x67, wasm_op_t{"i32.clz", false, {}}}, - {0x68, wasm_op_t{"i32.ctz", false, {}}}, - {0x69, wasm_op_t{"i32.popcnt", false, {}}}, - {0x6a, wasm_op_t{"i32.add", false, {}}}, - {0x6b, wasm_op_t{"i32.sub", false, {}}}, - {0x6c, wasm_op_t{"i32.mul", false, {}}}, - {0x6d, wasm_op_t{"i32.div_s", false, {}}}, - {0x6e, wasm_op_t{"i32.div_u", false, {}}}, - {0x6f, wasm_op_t{"i32.rem_s", false, {}}}, - {0x70, wasm_op_t{"i32.rem_u", false, {}}}, - {0x71, wasm_op_t{"i32.and", false, {}}}, - {0x72, wasm_op_t{"i32.or", false, {}}}, - {0x73, wasm_op_t{"i32.xor", false, {}}}, - {0x74, wasm_op_t{"i32.shl", false, {}}}, - {0x75, wasm_op_t{"i32.shr_s", false, {}}}, - {0x76, wasm_op_t{"i32.shr_u", false, {}}}, - {0x77, wasm_op_t{"i32.rotl", false, {}}}, - {0x78, wasm_op_t{"i32.rotr", false, {}}}, - {0x79, wasm_op_t{"i64.clz", false, {}}}, - {0x7a, wasm_op_t{"i64.ctz", false, {}}}, - {0x7b, wasm_op_t{"i64.popcnt", false, {}}}, - {0x7c, wasm_op_t{"i64.add", false, {}}}, - {0x7d, wasm_op_t{"i64.sub", false, {}}}, - {0x7e, wasm_op_t{"i64.mul", false, {}}}, - {0x7f, wasm_op_t{"i64.div_s", false, {}}}, - {0x80, wasm_op_t{"i64.div_u", false, {}}}, - {0x81, wasm_op_t{"i64.rem_s", false, {}}}, - {0x82, wasm_op_t{"i64.rem_u", false, {}}}, - {0x83, wasm_op_t{"i64.and", false, {}}}, - {0x84, wasm_op_t{"i64.or", false, {}}}, - {0x85, wasm_op_t{"i64.xor", false, {}}}, - {0x86, wasm_op_t{"i64.shl", false, {}}}, - {0x87, wasm_op_t{"i64.shr_s", false, {}}}, - {0x88, wasm_op_t{"i64.shr_u", false, {}}}, - {0x89, wasm_op_t{"i64.rotl", false, {}}}, - {0x8a, wasm_op_t{"i63.rotr", false, {}}}, - {0x8b, wasm_op_t{"f32.abs", false, {}}}, - {0x8c, wasm_op_t{"f32.neg", false, {}}}, - {0x8d, wasm_op_t{"f32.ceil", false, {}}}, - {0x8e, wasm_op_t{"f32.floor", false, {}}}, - {0x8f, wasm_op_t{"f32.trunc", false, {}}}, - {0x90, wasm_op_t{"f32.nearest", false, {}}}, - {0x91, wasm_op_t{"f32.sqrt", false, {}}}, - {0x92, wasm_op_t{"f32.add", false, {}}}, - {0x93, wasm_op_t{"f32.sub", false, {}}}, - {0x94, wasm_op_t{"f32.mul", false, {}}}, - {0x95, wasm_op_t{"f32.div", false, {}}}, - {0x96, wasm_op_t{"f32.min", false, {}}}, - {0x97, wasm_op_t{"f32.max", false, {}}}, - {0x98, wasm_op_t{"f32.copysign", false, {}}}, - {0x99, wasm_op_t{"f64.abs", false, {}}}, - {0x9a, wasm_op_t{"f64.neg", false, {}}}, - {0x9b, wasm_op_t{"f64.ceil", false, {}}}, - {0x9c, wasm_op_t{"f64.floor", false, {}}}, - {0x9d, wasm_op_t{"f64.trunc", false, {}}}, - {0x9e, wasm_op_t{"f64.nearest", false, {}}}, - {0x9f, wasm_op_t{"f64.sqrt", false, {}}}, - {0xa0, wasm_op_t{"f64.add", false, {}}}, - {0xa1, wasm_op_t{"f64.sub", false, {}}}, - {0xa2, wasm_op_t{"f64.mul", false, {}}}, - {0xa3, wasm_op_t{"f64.div", false, {}}}, - {0xa4, wasm_op_t{"f64.min", false, {}}}, - {0xa5, wasm_op_t{"f64.max", false, {}}}, - {0xa6, wasm_op_t{"f64.copysign", false, {}}}, - {0xa7, wasm_op_t{"i32.wrap/i64", false, {}}}, - {0xa8, wasm_op_t{"i32.trunc_s/f32", false, {}}}, - {0xa9, wasm_op_t{"i32.trunc_u/f32", false, {}}}, - {0xaa, wasm_op_t{"i32.trunc_s/f64", false, {}}}, - {0xab, wasm_op_t{"i32.trunc_u/f64", false, {}}}, - {0xac, wasm_op_t{"i64.extend_s/i32", false, {}}}, - {0xad, wasm_op_t{"i64.extend_u/i32", false, {}}}, - {0xae, wasm_op_t{"i64.trunc_s/f32", false, {}}}, - {0xaf, wasm_op_t{"i64.trunc_u/f32", false, {}}}, - {0xb0, wasm_op_t{"i64.trunc_s/f64", false, {}}}, - {0xb1, wasm_op_t{"i64.trunc_u/f64", false, {}}}, - {0xb2, wasm_op_t{"f32.convert_s/i32", false, {}}}, - {0xb3, wasm_op_t{"f32.convert_u/i32", false, {}}}, - {0xb4, wasm_op_t{"f32.convert_s/i64", false, {}}}, - {0xb5, wasm_op_t{"f32.convert_u/i64", false, {}}}, - {0xb6, wasm_op_t{"f32.demote/f64", false, {}}}, - {0xb7, wasm_op_t{"f64.convert_s/i32", false, {}}}, - {0xb8, wasm_op_t{"f64.convert_u/i32", false, {}}}, - {0xb9, wasm_op_t{"f64.convert_s/i64", false, {}}}, - {0xba, wasm_op_t{"f64.convert_u/i64", false, {}}}, - {0xbb, wasm_op_t{"f64.promote/f32", false, {}}}, - {0xbc, wasm_op_t{"i32.reinterpret/f32", false, {}}}, - {0xbd, wasm_op_t{"i64.reinterpret/f64", false, {}}}, - {0xbe, wasm_op_t{"f32.reinterpret/i32", false, {}}}, - {0xbf, wasm_op_t{"f64.reinterpret/i64", false, {}}} -}; + //{0x7f, wasm_op_t{"i32", false, {}}}, + //{0x7e, wasm_op_t{"i64", false, {}}}, + //{0x7d, wasm_op_t{"f32", false, {}}}, + //{0x7c, wasm_op_t{"f64", false, {}}}, + //{0x7b, wasm_op_t{"anyfunc", false, {}}}, + //{0x60, wasm_op_t{"func", false, {}}}, + //{0x40, wasm_op_t{"empty_block_type", false, {}}}, + {0x00, wasm_op_t{"unreachable", false, {}}}, + {0x01, wasm_op_t{"nop", false, {}}}, + {0x02, wasm_op_t{"block", true, {w_type_t::varuint7}}}, + {0x03, wasm_op_t{"loop", true, {w_type_t::varuint7}}}, + {0x04, wasm_op_t{"if", true, {w_type_t::varuint7}}}, + {0x05, wasm_op_t{"else", false, {}}}, + {0x0b, wasm_op_t{"end", false, {}}}, + {0x0c, wasm_op_t{"br", true, {w_type_t::varuint32}}}, + {0x0d, wasm_op_t{"br_if", true, {w_type_t::varuint32}}}, + {0x0e, wasm_op_t{"br_table", + true, + {w_type_t::varuint32, w_type_t::varuint32, + w_type_t::varuint32}}}, + {0x0f, wasm_op_t{"return", false, {}}}, + {0x10, wasm_op_t{"call", true, {w_type_t::varuint32}}}, + {0x11, wasm_op_t{"call_indirect", + true, + {w_type_t::varuint32, w_type_t::varuint1}}}, + {0x1a, wasm_op_t{"drop", false, {}}}, + {0x1b, wasm_op_t{"select", false, {}}}, + {0x20, wasm_op_t{"get_local", true, {w_type_t::varuint32}}}, + {0x21, wasm_op_t{"set_local", true, {w_type_t::varuint32}}}, + {0x22, wasm_op_t{"tee_local", true, {w_type_t::varuint32}}}, + {0x23, wasm_op_t{"get_global", true, {w_type_t::varuint32}}}, + {0x24, wasm_op_t{"set_global", true, {w_type_t::varuint32}}}, + {0x28, + wasm_op_t{"i32.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x29, + wasm_op_t{"i64.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x2a, + wasm_op_t{"f32.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x2b, + wasm_op_t{"f64.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x2c, wasm_op_t{"i32.load8_s", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x2d, wasm_op_t{"i32.load8_u", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x2e, wasm_op_t{"i32.load16_s", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x2f, wasm_op_t{"i32.load16_u", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x30, wasm_op_t{"i64.load8_s", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x31, wasm_op_t{"i64.load8_u", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x32, wasm_op_t{"i64.load16_s", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x33, wasm_op_t{"i64.load16_u", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x34, wasm_op_t{"i64.load32_s", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x35, wasm_op_t{"i64.load32_u", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x36, + wasm_op_t{"i32.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x37, + wasm_op_t{"i64.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x38, + wasm_op_t{"f32.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x39, + wasm_op_t{"f64.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x3a, + wasm_op_t{"i32.store8", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x3b, wasm_op_t{"i32.store16", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x3c, + wasm_op_t{"i64.store8", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x3d, wasm_op_t{"i64.store16", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x3e, wasm_op_t{"i64.store32", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x3f, wasm_op_t{"current_memory", true, {w_type_t::varuint1}}}, + {0x40, wasm_op_t{"grow_memory", true, {w_type_t::varuint1}}}, + {0x41, wasm_op_t{"i32.const", true, {w_type_t::varint32}}}, + {0x42, wasm_op_t{"i64.const", true, {w_type_t::varint64}}}, + {0x43, wasm_op_t{"f32.const", true, {w_type_t::uint32}}}, + {0x44, wasm_op_t{"f64.const", true, {w_type_t::uint64}}}, + {0x45, wasm_op_t{"i32.eqz", false, {}}}, + {0x46, wasm_op_t{"i32.eq", false, {}}}, + {0x47, wasm_op_t{"i32.ne", false, {}}}, + {0x48, wasm_op_t{"i32.lt_s", false, {}}}, + {0x49, wasm_op_t{"i32.lt_u", false, {}}}, + {0x4a, wasm_op_t{"i32.gt_s", false, {}}}, + {0x4b, wasm_op_t{"i32.gt_u", false, {}}}, + {0x4c, wasm_op_t{"i32.le_s", false, {}}}, + {0x4d, wasm_op_t{"i32.le_u", false, {}}}, + {0x4e, wasm_op_t{"i32.ge_s", false, {}}}, + {0x4f, wasm_op_t{"i32.ge_u", false, {}}}, + {0x50, wasm_op_t{"i64.eqz", false, {}}}, + {0x51, wasm_op_t{"i64.eq", false, {}}}, + {0x52, wasm_op_t{"i64.ne", false, {}}}, + {0x53, wasm_op_t{"i64.lt_s", false, {}}}, + {0x54, wasm_op_t{"i64.lt_u", false, {}}}, + {0x55, wasm_op_t{"i64.gt_s", false, {}}}, + {0x56, wasm_op_t{"i64.gt_u", false, {}}}, + {0x57, wasm_op_t{"i64.le_s", false, {}}}, + {0x58, wasm_op_t{"i64.le_u", false, {}}}, + {0x59, wasm_op_t{"i64.ge_s", false, {}}}, + {0x5a, wasm_op_t{"i64.ge_u", false, {}}}, + {0x5b, wasm_op_t{"f32.eq", false, {}}}, + {0x5c, wasm_op_t{"f32.ne", false, {}}}, + {0x5d, wasm_op_t{"f32.lt", false, {}}}, + {0x5e, wasm_op_t{"f32.gt", false, {}}}, + {0x5f, wasm_op_t{"f32.le", false, {}}}, + {0x60, wasm_op_t{"f32.ge", false, {}}}, + {0x61, wasm_op_t{"f64.eq", false, {}}}, + {0x62, wasm_op_t{"f64.ne", false, {}}}, + {0x63, wasm_op_t{"f64.lt", false, {}}}, + {0x64, wasm_op_t{"f64.gt", false, {}}}, + {0x65, wasm_op_t{"f64.le", false, {}}}, + {0x66, wasm_op_t{"f64.ge", false, {}}}, + {0x67, wasm_op_t{"i32.clz", false, {}}}, + {0x68, wasm_op_t{"i32.ctz", false, {}}}, + {0x69, wasm_op_t{"i32.popcnt", false, {}}}, + {0x6a, wasm_op_t{"i32.add", false, {}}}, + {0x6b, wasm_op_t{"i32.sub", false, {}}}, + {0x6c, wasm_op_t{"i32.mul", false, {}}}, + {0x6d, wasm_op_t{"i32.div_s", false, {}}}, + {0x6e, wasm_op_t{"i32.div_u", false, {}}}, + {0x6f, wasm_op_t{"i32.rem_s", false, {}}}, + {0x70, wasm_op_t{"i32.rem_u", false, {}}}, + {0x71, wasm_op_t{"i32.and", false, {}}}, + {0x72, wasm_op_t{"i32.or", false, {}}}, + {0x73, wasm_op_t{"i32.xor", false, {}}}, + {0x74, wasm_op_t{"i32.shl", false, {}}}, + {0x75, wasm_op_t{"i32.shr_s", false, {}}}, + {0x76, wasm_op_t{"i32.shr_u", false, {}}}, + {0x77, wasm_op_t{"i32.rotl", false, {}}}, + {0x78, wasm_op_t{"i32.rotr", false, {}}}, + {0x79, wasm_op_t{"i64.clz", false, {}}}, + {0x7a, wasm_op_t{"i64.ctz", false, {}}}, + {0x7b, wasm_op_t{"i64.popcnt", false, {}}}, + {0x7c, wasm_op_t{"i64.add", false, {}}}, + {0x7d, wasm_op_t{"i64.sub", false, {}}}, + {0x7e, wasm_op_t{"i64.mul", false, {}}}, + {0x7f, wasm_op_t{"i64.div_s", false, {}}}, + {0x80, wasm_op_t{"i64.div_u", false, {}}}, + {0x81, wasm_op_t{"i64.rem_s", false, {}}}, + {0x82, wasm_op_t{"i64.rem_u", false, {}}}, + {0x83, wasm_op_t{"i64.and", false, {}}}, + {0x84, wasm_op_t{"i64.or", false, {}}}, + {0x85, wasm_op_t{"i64.xor", false, {}}}, + {0x86, wasm_op_t{"i64.shl", false, {}}}, + {0x87, wasm_op_t{"i64.shr_s", false, {}}}, + {0x88, wasm_op_t{"i64.shr_u", false, {}}}, + {0x89, wasm_op_t{"i64.rotl", false, {}}}, + {0x8a, wasm_op_t{"i63.rotr", false, {}}}, + {0x8b, wasm_op_t{"f32.abs", false, {}}}, + {0x8c, wasm_op_t{"f32.neg", false, {}}}, + {0x8d, wasm_op_t{"f32.ceil", false, {}}}, + {0x8e, wasm_op_t{"f32.floor", false, {}}}, + {0x8f, wasm_op_t{"f32.trunc", false, {}}}, + {0x90, wasm_op_t{"f32.nearest", false, {}}}, + {0x91, wasm_op_t{"f32.sqrt", false, {}}}, + {0x92, wasm_op_t{"f32.add", false, {}}}, + {0x93, wasm_op_t{"f32.sub", false, {}}}, + {0x94, wasm_op_t{"f32.mul", false, {}}}, + {0x95, wasm_op_t{"f32.div", false, {}}}, + {0x96, wasm_op_t{"f32.min", false, {}}}, + {0x97, wasm_op_t{"f32.max", false, {}}}, + {0x98, wasm_op_t{"f32.copysign", false, {}}}, + {0x99, wasm_op_t{"f64.abs", false, {}}}, + {0x9a, wasm_op_t{"f64.neg", false, {}}}, + {0x9b, wasm_op_t{"f64.ceil", false, {}}}, + {0x9c, wasm_op_t{"f64.floor", false, {}}}, + {0x9d, wasm_op_t{"f64.trunc", false, {}}}, + {0x9e, wasm_op_t{"f64.nearest", false, {}}}, + {0x9f, wasm_op_t{"f64.sqrt", false, {}}}, + {0xa0, wasm_op_t{"f64.add", false, {}}}, + {0xa1, wasm_op_t{"f64.sub", false, {}}}, + {0xa2, wasm_op_t{"f64.mul", false, {}}}, + {0xa3, wasm_op_t{"f64.div", false, {}}}, + {0xa4, wasm_op_t{"f64.min", false, {}}}, + {0xa5, wasm_op_t{"f64.max", false, {}}}, + {0xa6, wasm_op_t{"f64.copysign", false, {}}}, + {0xa7, wasm_op_t{"i32.wrap/i64", false, {}}}, + {0xa8, wasm_op_t{"i32.trunc_s/f32", false, {}}}, + {0xa9, wasm_op_t{"i32.trunc_u/f32", false, {}}}, + {0xaa, wasm_op_t{"i32.trunc_s/f64", false, {}}}, + {0xab, wasm_op_t{"i32.trunc_u/f64", false, {}}}, + {0xac, wasm_op_t{"i64.extend_s/i32", false, {}}}, + {0xad, wasm_op_t{"i64.extend_u/i32", false, {}}}, + {0xae, wasm_op_t{"i64.trunc_s/f32", false, {}}}, + {0xaf, wasm_op_t{"i64.trunc_u/f32", false, {}}}, + {0xb0, wasm_op_t{"i64.trunc_s/f64", false, {}}}, + {0xb1, wasm_op_t{"i64.trunc_u/f64", false, {}}}, + {0xb2, wasm_op_t{"f32.convert_s/i32", false, {}}}, + {0xb3, wasm_op_t{"f32.convert_u/i32", false, {}}}, + {0xb4, wasm_op_t{"f32.convert_s/i64", false, {}}}, + {0xb5, wasm_op_t{"f32.convert_u/i64", false, {}}}, + {0xb6, wasm_op_t{"f32.demote/f64", false, {}}}, + {0xb7, wasm_op_t{"f64.convert_s/i32", false, {}}}, + {0xb8, wasm_op_t{"f64.convert_u/i32", false, {}}}, + {0xb9, wasm_op_t{"f64.convert_s/i64", false, {}}}, + {0xba, wasm_op_t{"f64.convert_u/i64", false, {}}}, + {0xbb, wasm_op_t{"f64.promote/f32", false, {}}}, + {0xbc, wasm_op_t{"i32.reinterpret/f32", false, {}}}, + {0xbd, wasm_op_t{"i64.reinterpret/f64", false, {}}}, + {0xbe, wasm_op_t{"f32.reinterpret/i32", false, {}}}, + {0xbf, wasm_op_t{"f64.reinterpret/i64", false, {}}}}; #endif // header guard end /**********************************************************************************************************************/ /*last line intentionally left blank.*/ - diff --git a/dwasm-cpp/misc.hpp b/dwasm-cpp/misc.hpp index 74f61ad..964c380 100644 --- a/dwasm-cpp/misc.hpp +++ b/dwasm-cpp/misc.hpp @@ -16,21 +16,23 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/ +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +USA.*/ /**********************************************************************************************************************/ #include <cstddef> #include <vector> /**********************************************************************************************************************/ -int read_header(std::ifstream& wasm) { +int read_header(std::ifstream &wasm) { char head[4]; wasm.read(head, sizeof(uint32_t)); std::cout << head[0] << head[1] << head[2] << head[3] << "\n"; wasm.read(head, sizeof(uint32_t)); - std::cout << (int)(head[0]) << (int)(head[1]) << (int)(head[2]) << (int)(head[3]) << "\n"; + std::cout << (int)(head[0]) << (int)(head[1]) << (int)(head[2]) + << (int)(head[3]) << "\n"; return 0; } -int dump_file(std::ifstream& wasm) { +int dump_file(std::ifstream &wasm) { while (wasm) { std::ios::pos_type before = wasm.tellg(); uint8_t x; @@ -47,4 +49,3 @@ std::vector<unsigned char> leb128_u_e() {} std::vector<unsigned char> leb128_s_e() {} /**********************************************************************************************************************/ /*last line intentionally left blank.*/ - diff --git a/dwasm-cpp/wasm.h b/dwasm-cpp/wasm.h index a7b916f..6687c03 100644 --- a/dwasm-cpp/wasm.h +++ b/dwasm-cpp/wasm.h @@ -16,7 +16,8 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/ +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +USA.*/ /**********************************************************************************************************************/ #ifndef WASM_H #define WASM_H @@ -34,147 +35,156 @@ typedef int8_t varuint1; typedef int8_t varuint7; typedef int32_t varuint32; - enum value_type_t {f64_vt = -4, f32_vt, i64_vt, i32_vt}; - enum external_kind_t {Function, Table, Memory, Global}; - enum type_ctor_t {i32_ctor = -1, i64_ctor = -2, f32_ctor = -3, f64_ctor = -4, anyfunc_ctor = -16, func_ctor = -32, block_type_ctor = -64}; - - typedef struct { - varuint32 size; - char* code; - }init_expr_t; - - typedef struct { - varuint1 flags; - varuint32 initial; - varuint32 maximum; - } resizable_limit_t; - - typedef struct { - enum value_type_t value_type; - varuint1 mutability; - }global_type_t; - - typedef struct { - varint7 element_type; - resizable_limit_t* resizable_limit; - }table_type_t; - - typedef struct { - resizable_limit_t* resizable_limit; - }memory_type_t; - - // func_type - typedef struct { - varint7 form; - varuint32 param_count; - varint7* param_types; - varuint1 return_count; - varint7 * return_types; - }W_Type_Section_Entry; - - typedef struct { - varuint32 count; - W_Type_Section_Entry** entries; - }W_Type_Section; - - typedef struct { - varuint32 module_length; - char* module_str; - varuint32 field_len; - char* field_str; - enum external_kind_t kind; - // based on external_kind it can be 4 different types. thats why im casting to void*. - void* type; - }W_Import_Section_Entry; - - typedef struct { - varuint32 count; - W_Import_Section_Entry** entries; - }W_Import_Section; - - typedef struct { - varuint32 count; - // indexes into the type section - varuint32* types; - }W_Function_Section; - - typedef struct W_Table_Section { - varuint32 count; - table_type_t** entries; - }W_Table_Section; - - typedef struct { - varuint32 count; - memory_type_t** entries; - }W_Memory_Section; - - typedef struct { - global_type_t* type; - init_expr_t* init; - }W_Global_Entry; - - typedef struct { - varuint32 count; - W_Global_Entry** globals; - }W_Global_Section; - - typedef struct { - varuint32 field_len; - char* field_str; - enum external_kind_t kind; - varuint32 index; - }W_Export_Entry; - - typedef struct { - int count; - W_Export_Entry** entries; - }W_Export_Section; - - typedef struct { - varuint32 index; - }W_Start_Section; - - typedef struct { - varuint32 index; - init_expr_t* offset; - varuint32 num_length; - varuint32* elems; - }W_Elem_Segment; - - typedef struct { - varuint32 count; - W_Elem_Segment** entries; - }W_Element_Section; - - typedef struct { - varuint32 count; - enum value_type_t type; - }W_Local_Entry; - - typedef struct W_Function_Body { - varuint32 body_size; - varuint32 local_count; - W_Local_Entry** locals; - char* code; - //char end = 0x0b; - }W_Function_Body; - - typedef struct { - varuint32 count; - W_Function_Body** bodies; - }W_Code_Section; - - typedef struct { - varuint32 index; - init_expr_t* offset; - varuint32 size; - char* data; - }W_Data_Segment; - - typedef struct { - varuint32 count; - W_Data_Segment** entries; - }W_Data_Section; +enum value_type_t { f64_vt = -4, f32_vt, i64_vt, i32_vt }; +enum external_kind_t { Function, Table, Memory, Global }; +enum type_ctor_t { + i32_ctor = -1, + i64_ctor = -2, + f32_ctor = -3, + f64_ctor = -4, + anyfunc_ctor = -16, + func_ctor = -32, + block_type_ctor = -64 +}; + +typedef struct { + varuint32 size; + char *code; +} init_expr_t; + +typedef struct { + varuint1 flags; + varuint32 initial; + varuint32 maximum; +} resizable_limit_t; + +typedef struct { + enum value_type_t value_type; + varuint1 mutability; +} global_type_t; + +typedef struct { + varint7 element_type; + resizable_limit_t *resizable_limit; +} table_type_t; + +typedef struct { + resizable_limit_t *resizable_limit; +} memory_type_t; + +// func_type +typedef struct { + varint7 form; + varuint32 param_count; + varint7 *param_types; + varuint1 return_count; + varint7 *return_types; +} W_Type_Section_Entry; + +typedef struct { + varuint32 count; + W_Type_Section_Entry **entries; +} W_Type_Section; + +typedef struct { + varuint32 module_length; + char *module_str; + varuint32 field_len; + char *field_str; + enum external_kind_t kind; + // based on external_kind it can be 4 different types. thats why im casting to + // void*. + void *type; +} W_Import_Section_Entry; + +typedef struct { + varuint32 count; + W_Import_Section_Entry **entries; +} W_Import_Section; + +typedef struct { + varuint32 count; + // indexes into the type section + varuint32 *types; +} W_Function_Section; + +typedef struct W_Table_Section { + varuint32 count; + table_type_t **entries; +} W_Table_Section; + +typedef struct { + varuint32 count; + memory_type_t **entries; +} W_Memory_Section; + +typedef struct { + global_type_t *type; + init_expr_t *init; +} W_Global_Entry; + +typedef struct { + varuint32 count; + W_Global_Entry **globals; +} W_Global_Section; + +typedef struct { + varuint32 field_len; + char *field_str; + enum external_kind_t kind; + varuint32 index; +} W_Export_Entry; + +typedef struct { + int count; + W_Export_Entry **entries; +} W_Export_Section; + +typedef struct { + varuint32 index; +} W_Start_Section; + +typedef struct { + varuint32 index; + init_expr_t *offset; + varuint32 num_length; + varuint32 *elems; +} W_Elem_Segment; + +typedef struct { + varuint32 count; + W_Elem_Segment **entries; +} W_Element_Section; + +typedef struct { + varuint32 count; + enum value_type_t type; +} W_Local_Entry; + +typedef struct W_Function_Body { + varuint32 body_size; + varuint32 local_count; + W_Local_Entry **locals; + char *code; + // char end = 0x0b; +} W_Function_Body; + +typedef struct { + varuint32 count; + W_Function_Body **bodies; +} W_Code_Section; + +typedef struct { + varuint32 index; + init_expr_t *offset; + varuint32 size; + char *data; +} W_Data_Segment; + +typedef struct { + varuint32 count; + W_Data_Segment **entries; +} W_Data_Section; #if 0 typedef struct W_Custom_Section {}; @@ -182,27 +192,27 @@ typedef int32_t varuint32; typedef struct W_Relocation_Section {}; #endif - typedef struct Wasm_Module { - W_Type_Section* type_section; - W_Import_Section* import_section; - W_Function_Section* function_section; - W_Table_Section* table_section; - W_Memory_Section* memory_section; - W_Global_Section* global_section; - W_Export_Section* export_section; - W_Start_Section* start_section; - W_Element_Section* element_section; - W_Code_Section* code_section; - W_Data_Section* data_section; - void** W_Custom_Sections; - char* name; - }Wasm_Module; - - // get the raw binary of the wasm module - // char* getWRaw(); - - // get wasm section raw binary by name - // char* get_W_Section_Raw(const char* section_name); +typedef struct Wasm_Module { + W_Type_Section *type_section; + W_Import_Section *import_section; + W_Function_Section *function_section; + W_Table_Section *table_section; + W_Memory_Section *memory_section; + W_Global_Section *global_section; + W_Export_Section *export_section; + W_Start_Section *start_section; + W_Element_Section *element_section; + W_Code_Section *code_section; + W_Data_Section *data_section; + void **W_Custom_Sections; + char *name; +} Wasm_Module; + +// get the raw binary of the wasm module +// char* getWRaw(); + +// get wasm section raw binary by name +// char* get_W_Section_Raw(const char* section_name); #ifdef __cplusplus } @@ -210,4 +220,3 @@ typedef int32_t varuint32; #endif // end of header guard /**********************************************************************************************************************/ /*last line intentionally left blank.*/ - |