2 ** @file mruby/dump.h - mruby binary dumper (mrbc binary format)
4 ** See Copyright Notice in mruby.h
11 #include <mruby/irep.h>
15 * Dumping compiled mruby script.
19 #define DUMP_DEBUG_INFO 1
21 int mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size);
22 #ifndef MRB_DISABLE_STDIO
23 int mrb_dump_irep_binary(mrb_state*, mrb_irep*, uint8_t, FILE*);
24 int mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep*, uint8_t flags, FILE *f, const char *initname);
25 mrb_irep *mrb_read_irep_file(mrb_state*, FILE*);
26 MRB_API mrb_value mrb_load_irep_file(mrb_state*,FILE*);
27 MRB_API mrb_value mrb_load_irep_file_cxt(mrb_state*, FILE*, mrbc_context*);
29 MRB_API mrb_irep *mrb_read_irep(mrb_state*, const uint8_t*);
30 MRB_API mrb_irep *mrb_read_irep_buf(mrb_state*, const void*, size_t);
32 /* dump/load error code
34 * NOTE: MRB_DUMP_GENERAL_FAILURE is caused by
35 * unspecified issues like malloc failed.
38 #define MRB_DUMP_GENERAL_FAILURE (-1)
39 #define MRB_DUMP_WRITE_FAULT (-2)
40 #define MRB_DUMP_READ_FAULT (-3)
41 #define MRB_DUMP_CRC_ERROR (-4)
42 #define MRB_DUMP_INVALID_FILE_HEADER (-5)
43 #define MRB_DUMP_INVALID_IREP (-6)
44 #define MRB_DUMP_INVALID_ARGUMENT (-7)
46 /* null symbol length */
47 #define MRB_DUMP_NULL_SYM_LEN 0xFFFF
49 /* Rite Binary File header */
50 #define RITE_BINARY_IDENT "RITE"
51 #define RITE_BINARY_FORMAT_VER "0007"
52 #define RITE_COMPILER_NAME "MATZ"
53 #define RITE_COMPILER_VERSION "0000"
55 #define RITE_VM_VER "0002"
57 #define RITE_BINARY_EOF "END\0"
58 #define RITE_SECTION_IREP_IDENT "IREP"
59 #define RITE_SECTION_DEBUG_IDENT "DBG\0"
60 #define RITE_SECTION_LV_IDENT "LVAR"
62 #define MRB_DUMP_DEFAULT_STR_LEN 128
63 #define MRB_DUMP_ALIGNMENT sizeof(uint32_t)
66 struct rite_binary_header {
67 uint8_t binary_ident[4]; /* Binary Identifier */
68 uint8_t binary_version[4]; /* Binary Format Version */
69 uint8_t binary_crc[2]; /* Binary CRC */
70 uint8_t binary_size[4]; /* Binary Size */
71 uint8_t compiler_name[4]; /* Compiler name */
72 uint8_t compiler_version[4];
76 #define RITE_SECTION_HEADER \
77 uint8_t section_ident[4]; \
78 uint8_t section_size[4]
80 struct rite_section_header {
84 struct rite_section_irep_header {
87 uint8_t rite_version[4]; /* Rite Instruction Specification Version */
90 struct rite_section_debug_header {
94 struct rite_section_lv_header {
98 #define RITE_LV_NULL_MARK UINT16_MAX
100 struct rite_binary_footer {
105 uint8_to_bin(uint8_t s, uint8_t *bin)
108 return sizeof(uint8_t);
112 uint16_to_bin(uint16_t s, uint8_t *bin)
114 *bin++ = (s >> 8) & 0xff;
116 return sizeof(uint16_t);
120 uint32_to_bin(uint32_t l, uint8_t *bin)
122 *bin++ = (l >> 24) & 0xff;
123 *bin++ = (l >> 16) & 0xff;
124 *bin++ = (l >> 8) & 0xff;
126 return sizeof(uint32_t);
130 uint32l_to_bin(uint32_t l, uint8_t *bin)
132 bin[3] = (l >> 24) & 0xff;
133 bin[2] = (l >> 16) & 0xff;
134 bin[1] = (l >> 8) & 0xff;
136 return sizeof(uint32_t);
139 static inline uint32_t
140 bin_to_uint32(const uint8_t *bin)
142 return (uint32_t)bin[0] << 24 |
143 (uint32_t)bin[1] << 16 |
144 (uint32_t)bin[2] << 8 |
148 static inline uint32_t
149 bin_to_uint32l(const uint8_t *bin)
151 return (uint32_t)bin[3] << 24 |
152 (uint32_t)bin[2] << 16 |
153 (uint32_t)bin[1] << 8 |
157 static inline uint16_t
158 bin_to_uint16(const uint8_t *bin)
160 return (uint16_t)bin[0] << 8 |
164 static inline uint8_t
165 bin_to_uint8(const uint8_t *bin)
167 return (uint8_t)bin[0];
172 /** @internal crc.c */
174 calc_crc_16_ccitt(const uint8_t *src, size_t nbytes, uint16_t crc);
176 #endif /* MRUBY_DUMP_H */