/src/mruby/include/mruby/dump.h
Line | Count | Source |
1 | | /** |
2 | | ** @file mruby/dump.h - mruby binary dumper (mrbc binary format) |
3 | | ** |
4 | | ** See Copyright Notice in mruby.h |
5 | | */ |
6 | | |
7 | | #ifndef MRUBY_DUMP_H |
8 | | #define MRUBY_DUMP_H |
9 | | |
10 | | #include <mruby.h> |
11 | | #include <mruby/irep.h> |
12 | | #include "common.h" |
13 | | |
14 | | /** |
15 | | * Dumping compiled mruby script. |
16 | | */ |
17 | | MRB_BEGIN_DECL |
18 | | |
19 | | /* flags for mrb_dump_irep{,_binary,_cfunc,_cstruct} */ |
20 | | #define MRB_DUMP_DEBUG_INFO 1 |
21 | | #define MRB_DUMP_STATIC 2 |
22 | | #define MRB_DUMP_NO_LVAR 4 |
23 | | |
24 | | #ifndef MRB_NO_STDIO |
25 | | MRB_API mrb_value mrb_load_irep_file(mrb_state*,FILE*); |
26 | | MRB_API mrb_value mrb_load_irep_file_cxt(mrb_state*, FILE*, mrb_ccontext*); |
27 | | mrb_irep *mrb_read_irep_file(mrb_state*, FILE*); |
28 | | int mrb_dump_irep_binary(mrb_state*, const mrb_irep*, uint8_t, FILE*); |
29 | | #endif |
30 | | /* avoid mrb_read_irep(); use mrb_read_irep_buf() instead (may cause buffer overflow) */ |
31 | | MRB_API mrb_irep *mrb_read_irep(mrb_state*, const uint8_t*); |
32 | | MRB_API mrb_irep *mrb_read_irep_buf(mrb_state*, const void*, size_t); |
33 | | |
34 | | /* dump/load error code |
35 | | * |
36 | | * NOTE: MRB_DUMP_GENERAL_FAILURE is caused by |
37 | | * unspecified issues like malloc failed. |
38 | | */ |
39 | 0 | #define MRB_DUMP_OK 0 |
40 | 0 | #define MRB_DUMP_GENERAL_FAILURE (-1) |
41 | | #define MRB_DUMP_WRITE_FAULT (-2) |
42 | 0 | #define MRB_DUMP_READ_FAULT (-3) |
43 | 0 | #define MRB_DUMP_INVALID_FILE_HEADER (-4) |
44 | 0 | #define MRB_DUMP_INVALID_IREP (-5) |
45 | | #define MRB_DUMP_INVALID_ARGUMENT (-6) |
46 | | |
47 | | /* null symbol length */ |
48 | 0 | #define MRB_DUMP_NULL_SYM_LEN 0xFFFF |
49 | | |
50 | | /* Rite Binary File header */ |
51 | 0 | #define RITE_BINARY_IDENT "RITE" |
52 | | /* Binary Format Version Major:Minor */ |
53 | | /* Major: Incompatible to prior versions */ |
54 | | /* Minor: Upper-compatible to prior versions */ |
55 | 0 | #define RITE_BINARY_MAJOR_VER "04" |
56 | 0 | #define RITE_BINARY_MINOR_VER "00" |
57 | | #define RITE_BINARY_FORMAT_VER RITE_BINARY_MAJOR_VER RITE_BINARY_MINOR_VER |
58 | | #define RITE_COMPILER_NAME "MATZ" |
59 | | #define RITE_COMPILER_VERSION "0000" |
60 | | |
61 | | #define RITE_VM_VER "0400" |
62 | | |
63 | 0 | #define RITE_BINARY_EOF "END\0" |
64 | 0 | #define RITE_SECTION_IREP_IDENT "IREP" |
65 | 0 | #define RITE_SECTION_DEBUG_IDENT "DBG\0" |
66 | 0 | #define RITE_SECTION_LV_IDENT "LVAR" |
67 | | |
68 | | #define MRB_DUMP_DEFAULT_STR_LEN 128 |
69 | | #define MRB_DUMP_ALIGNMENT sizeof(uint32_t) |
70 | | |
71 | | /* binary header */ |
72 | | struct rite_binary_header { |
73 | | uint8_t binary_ident[4]; /* Binary Identifier */ |
74 | | uint8_t major_version[2]; /* Binary Format Major Version */ |
75 | | uint8_t minor_version[2]; /* Binary Format Minor Version */ |
76 | | uint8_t binary_size[4]; /* Binary Size */ |
77 | | uint8_t compiler_name[4]; /* Compiler name */ |
78 | | uint8_t compiler_version[4]; |
79 | | }; |
80 | | |
81 | | /* section header */ |
82 | | #define RITE_SECTION_HEADER \ |
83 | | uint8_t section_ident[4]; \ |
84 | | uint8_t section_size[4] |
85 | | |
86 | | struct rite_section_header { |
87 | | RITE_SECTION_HEADER; |
88 | | }; |
89 | | |
90 | | struct rite_section_irep_header { |
91 | | RITE_SECTION_HEADER; |
92 | | |
93 | | uint8_t rite_version[4]; /* Rite Instruction Specification Version */ |
94 | | }; |
95 | | |
96 | | struct rite_section_debug_header { |
97 | | RITE_SECTION_HEADER; |
98 | | }; |
99 | | |
100 | | struct rite_section_lv_header { |
101 | | RITE_SECTION_HEADER; |
102 | | }; |
103 | | |
104 | 0 | #define RITE_LV_NULL_MARK UINT16_MAX |
105 | | |
106 | | struct rite_binary_footer { |
107 | | RITE_SECTION_HEADER; |
108 | | }; |
109 | | |
110 | | static inline size_t |
111 | | uint8_to_bin(uint8_t s, uint8_t *bin) |
112 | 2.65k | { |
113 | 2.65k | *bin = s; |
114 | 2.65k | return sizeof(uint8_t); |
115 | 2.65k | } Unexecuted instantiation: y.tab.c:uint8_to_bin Unexecuted instantiation: symbol.c:uint8_to_bin Unexecuted instantiation: load.c:uint8_to_bin Unexecuted instantiation: vm.c:uint8_to_bin Unexecuted instantiation: codedump.c:uint8_to_bin Line | Count | Source | 112 | 2.65k | { | 113 | 2.65k | *bin = s; | 114 | 2.65k | return sizeof(uint8_t); | 115 | 2.65k | } |
|
116 | | |
117 | | static inline size_t |
118 | | uint16_to_bin(uint16_t s, uint8_t *bin) |
119 | 0 | { |
120 | 0 | *bin++ = (s >> 8) & 0xff; |
121 | 0 | *bin = s & 0xff; |
122 | 0 | return sizeof(uint16_t); |
123 | 0 | } Unexecuted instantiation: y.tab.c:uint16_to_bin Unexecuted instantiation: symbol.c:uint16_to_bin Unexecuted instantiation: load.c:uint16_to_bin Unexecuted instantiation: vm.c:uint16_to_bin Unexecuted instantiation: codedump.c:uint16_to_bin Unexecuted instantiation: codegen.c:uint16_to_bin |
124 | | |
125 | | static inline size_t |
126 | | uint32_to_bin(uint32_t l, uint8_t *bin) |
127 | 7.97k | { |
128 | 7.97k | *bin++ = (l >> 24) & 0xff; |
129 | 7.97k | *bin++ = (l >> 16) & 0xff; |
130 | 7.97k | *bin++ = (l >> 8) & 0xff; |
131 | 7.97k | *bin = l & 0xff; |
132 | 7.97k | return sizeof(uint32_t); |
133 | 7.97k | } Unexecuted instantiation: y.tab.c:uint32_to_bin Unexecuted instantiation: symbol.c:uint32_to_bin Unexecuted instantiation: load.c:uint32_to_bin Unexecuted instantiation: vm.c:uint32_to_bin Unexecuted instantiation: codedump.c:uint32_to_bin Line | Count | Source | 127 | 7.97k | { | 128 | 7.97k | *bin++ = (l >> 24) & 0xff; | 129 | 7.97k | *bin++ = (l >> 16) & 0xff; | 130 | 7.97k | *bin++ = (l >> 8) & 0xff; | 131 | 7.97k | *bin = l & 0xff; | 132 | 7.97k | return sizeof(uint32_t); | 133 | 7.97k | } |
|
134 | | |
135 | | static inline uint32_t |
136 | | bin_to_uint32(const uint8_t *bin) |
137 | 193k | { |
138 | 193k | return (uint32_t)bin[0] << 24 | |
139 | 193k | (uint32_t)bin[1] << 16 | |
140 | 193k | (uint32_t)bin[2] << 8 | |
141 | 193k | (uint32_t)bin[3]; |
142 | 193k | } Unexecuted instantiation: y.tab.c:bin_to_uint32 Unexecuted instantiation: symbol.c:bin_to_uint32 Unexecuted instantiation: load.c:bin_to_uint32 Line | Count | Source | 137 | 193k | { | 138 | 193k | return (uint32_t)bin[0] << 24 | | 139 | 193k | (uint32_t)bin[1] << 16 | | 140 | 193k | (uint32_t)bin[2] << 8 | | 141 | 193k | (uint32_t)bin[3]; | 142 | 193k | } |
Unexecuted instantiation: codedump.c:bin_to_uint32 Unexecuted instantiation: codegen.c:bin_to_uint32 |
143 | | |
144 | | static inline uint16_t |
145 | | bin_to_uint16(const uint8_t *bin) |
146 | 0 | { |
147 | 0 | return (uint16_t)bin[0] << 8 | |
148 | 0 | (uint16_t)bin[1]; |
149 | 0 | } Unexecuted instantiation: y.tab.c:bin_to_uint16 Unexecuted instantiation: symbol.c:bin_to_uint16 Unexecuted instantiation: load.c:bin_to_uint16 Unexecuted instantiation: vm.c:bin_to_uint16 Unexecuted instantiation: codedump.c:bin_to_uint16 Unexecuted instantiation: codegen.c:bin_to_uint16 |
150 | | |
151 | | static inline uint8_t |
152 | | bin_to_uint8(const uint8_t *bin) |
153 | 0 | { |
154 | 0 | return (uint8_t)bin[0]; |
155 | 0 | } Unexecuted instantiation: y.tab.c:bin_to_uint8 Unexecuted instantiation: symbol.c:bin_to_uint8 Unexecuted instantiation: load.c:bin_to_uint8 Unexecuted instantiation: vm.c:bin_to_uint8 Unexecuted instantiation: codedump.c:bin_to_uint8 Unexecuted instantiation: codegen.c:bin_to_uint8 |
156 | | |
157 | | MRB_END_DECL |
158 | | |
159 | | #endif /* MRUBY_DUMP_H */ |