[270] | 1 | /*
|
---|
| 2 | ** 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 "mruby/common.h"
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * Dumping compiled mruby script.
|
---|
| 16 | */
|
---|
| 17 | MRB_BEGIN_DECL
|
---|
| 18 |
|
---|
| 19 | #define DUMP_DEBUG_INFO 1
|
---|
| 20 | #define DUMP_ENDIAN_BIG 2
|
---|
| 21 | #define DUMP_ENDIAN_LIL 4
|
---|
| 22 | #define DUMP_ENDIAN_NAT 6
|
---|
| 23 | #define DUMP_ENDIAN_MASK 6
|
---|
| 24 |
|
---|
| 25 | int mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size);
|
---|
| 26 | #ifndef MRB_DISABLE_STDIO
|
---|
| 27 | int mrb_dump_irep_binary(mrb_state*, mrb_irep*, uint8_t, FILE*);
|
---|
| 28 | int mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep*, uint8_t flags, FILE *f, const char *initname);
|
---|
| 29 | mrb_irep *mrb_read_irep_file(mrb_state*, FILE*);
|
---|
| 30 | MRB_API mrb_value mrb_load_irep_file(mrb_state*,FILE*);
|
---|
| 31 | MRB_API mrb_value mrb_load_irep_file_cxt(mrb_state*, FILE*, mrbc_context*);
|
---|
| 32 | #endif
|
---|
| 33 | MRB_API mrb_irep *mrb_read_irep(mrb_state*, const uint8_t*);
|
---|
| 34 |
|
---|
| 35 | /* dump/load error code
|
---|
| 36 | *
|
---|
| 37 | * NOTE: MRB_DUMP_GENERAL_FAILURE is caused by
|
---|
| 38 | * unspecified issues like malloc failed.
|
---|
| 39 | */
|
---|
| 40 | #define MRB_DUMP_OK 0
|
---|
| 41 | #define MRB_DUMP_GENERAL_FAILURE (-1)
|
---|
| 42 | #define MRB_DUMP_WRITE_FAULT (-2)
|
---|
| 43 | #define MRB_DUMP_READ_FAULT (-3)
|
---|
| 44 | #define MRB_DUMP_CRC_ERROR (-4)
|
---|
| 45 | #define MRB_DUMP_INVALID_FILE_HEADER (-5)
|
---|
| 46 | #define MRB_DUMP_INVALID_IREP (-6)
|
---|
| 47 | #define MRB_DUMP_INVALID_ARGUMENT (-7)
|
---|
| 48 |
|
---|
| 49 | /* null symbol length */
|
---|
| 50 | #define MRB_DUMP_NULL_SYM_LEN 0xFFFF
|
---|
| 51 |
|
---|
| 52 | /* Rite Binary File header */
|
---|
| 53 | #define RITE_BINARY_IDENT "RITE"
|
---|
| 54 | #define RITE_BINARY_IDENT_LIL "ETIR"
|
---|
| 55 | #define RITE_BINARY_FORMAT_VER "0003"
|
---|
| 56 | #define RITE_COMPILER_NAME "MATZ"
|
---|
| 57 | #define RITE_COMPILER_VERSION "0000"
|
---|
| 58 |
|
---|
| 59 | #define RITE_VM_VER "0000"
|
---|
| 60 |
|
---|
| 61 | #define RITE_BINARY_EOF "END\0"
|
---|
| 62 | #define RITE_SECTION_IREP_IDENT "IREP"
|
---|
| 63 | #define RITE_SECTION_LINENO_IDENT "LINE"
|
---|
| 64 | #define RITE_SECTION_DEBUG_IDENT "DBG\0"
|
---|
| 65 | #define RITE_SECTION_LV_IDENT "LVAR"
|
---|
| 66 |
|
---|
| 67 | #define MRB_DUMP_DEFAULT_STR_LEN 128
|
---|
| 68 | #define MRB_DUMP_ALIGNMENT sizeof(uint32_t)
|
---|
| 69 |
|
---|
| 70 | #ifdef __CA850__
|
---|
| 71 | #pragma pack(1)
|
---|
| 72 | #elif __RX
|
---|
| 73 | #pragma pack
|
---|
| 74 | #elif _MSC_VER
|
---|
| 75 | #pragma pack(push, 1)
|
---|
| 76 | #endif
|
---|
| 77 | #ifndef __GNUC__
|
---|
| 78 | #define __attribute__(x)
|
---|
| 79 | #endif // !__GNUC__
|
---|
| 80 |
|
---|
| 81 | /* binary header */
|
---|
| 82 | struct rite_binary_header {
|
---|
| 83 | uint8_t binary_ident[4]; /* Binary Identifier */
|
---|
| 84 | uint8_t binary_version[4]; /* Binary Format Version */
|
---|
| 85 | uint8_t binary_crc[2]; /* Binary CRC */
|
---|
| 86 | uint8_t binary_size[4]; /* Binary Size */
|
---|
| 87 | uint8_t compiler_name[4]; /* Compiler name */
|
---|
| 88 | uint8_t compiler_version[4];
|
---|
| 89 | } __attribute__((packed));
|
---|
| 90 |
|
---|
| 91 | /* section header */
|
---|
| 92 | #define RITE_SECTION_HEADER \
|
---|
| 93 | uint8_t section_ident[4]; \
|
---|
| 94 | uint8_t section_size[4]
|
---|
| 95 |
|
---|
| 96 | struct rite_section_header {
|
---|
| 97 | RITE_SECTION_HEADER;
|
---|
| 98 | } __attribute__((packed));
|
---|
| 99 |
|
---|
| 100 | struct rite_section_irep_header {
|
---|
| 101 | RITE_SECTION_HEADER;
|
---|
| 102 |
|
---|
| 103 | uint8_t rite_version[4]; /* Rite Instruction Specification Version */
|
---|
| 104 | } __attribute__((packed));
|
---|
| 105 |
|
---|
| 106 | struct rite_section_lineno_header {
|
---|
| 107 | RITE_SECTION_HEADER;
|
---|
| 108 | } __attribute__((packed));
|
---|
| 109 |
|
---|
| 110 | struct rite_section_debug_header {
|
---|
| 111 | RITE_SECTION_HEADER;
|
---|
| 112 | } __attribute__((packed));
|
---|
| 113 |
|
---|
| 114 | struct rite_section_lv_header {
|
---|
| 115 | RITE_SECTION_HEADER;
|
---|
| 116 | } __attribute__((packed));
|
---|
| 117 |
|
---|
| 118 | #define RITE_LV_NULL_MARK UINT16_MAX
|
---|
| 119 |
|
---|
| 120 | struct rite_binary_footer {
|
---|
| 121 | RITE_SECTION_HEADER;
|
---|
| 122 | } __attribute__((packed));
|
---|
| 123 |
|
---|
| 124 | #ifdef __CA850__
|
---|
| 125 | #pragma pack(8)
|
---|
| 126 | #elif __RX
|
---|
| 127 | #pragma unpack
|
---|
| 128 | #elif _MSC_VER
|
---|
| 129 | #pragma pack(pop)
|
---|
| 130 | #endif
|
---|
| 131 |
|
---|
| 132 | static inline int
|
---|
| 133 | bigendian_p()
|
---|
| 134 | {
|
---|
| 135 | int i;
|
---|
| 136 | char *p;
|
---|
| 137 |
|
---|
| 138 | i = 1;
|
---|
| 139 | p = (char*)&i;
|
---|
| 140 | return p[0]?0:1;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | static inline size_t
|
---|
| 144 | uint8_to_bin(uint8_t s, uint8_t *bin)
|
---|
| 145 | {
|
---|
| 146 | *bin = s;
|
---|
| 147 | return sizeof(uint8_t);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | static inline size_t
|
---|
| 151 | uint16_to_bin(uint16_t s, uint8_t *bin)
|
---|
| 152 | {
|
---|
| 153 | *bin++ = (s >> 8) & 0xff;
|
---|
| 154 | *bin = s & 0xff;
|
---|
| 155 | return sizeof(uint16_t);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | static inline size_t
|
---|
| 159 | uint32_to_bin(uint32_t l, uint8_t *bin)
|
---|
| 160 | {
|
---|
| 161 | *bin++ = (l >> 24) & 0xff;
|
---|
| 162 | *bin++ = (l >> 16) & 0xff;
|
---|
| 163 | *bin++ = (l >> 8) & 0xff;
|
---|
| 164 | *bin = l & 0xff;
|
---|
| 165 | return sizeof(uint32_t);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | static inline size_t
|
---|
| 169 | uint32l_to_bin(uint32_t l, uint8_t *bin)
|
---|
| 170 | {
|
---|
| 171 | bin[3] = (l >> 24) & 0xff;
|
---|
| 172 | bin[2] = (l >> 16) & 0xff;
|
---|
| 173 | bin[1] = (l >> 8) & 0xff;
|
---|
| 174 | bin[0] = l & 0xff;
|
---|
| 175 | return sizeof(uint32_t);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | static inline uint32_t
|
---|
| 179 | bin_to_uint32(const uint8_t *bin)
|
---|
| 180 | {
|
---|
| 181 | return (uint32_t)bin[0] << 24 |
|
---|
| 182 | (uint32_t)bin[1] << 16 |
|
---|
| 183 | (uint32_t)bin[2] << 8 |
|
---|
| 184 | (uint32_t)bin[3];
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | static inline uint32_t
|
---|
| 188 | bin_to_uint32l(const uint8_t *bin)
|
---|
| 189 | {
|
---|
| 190 | return (uint32_t)bin[3] << 24 |
|
---|
| 191 | (uint32_t)bin[2] << 16 |
|
---|
| 192 | (uint32_t)bin[1] << 8 |
|
---|
| 193 | (uint32_t)bin[0];
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | static inline uint16_t
|
---|
| 197 | bin_to_uint16(const uint8_t *bin)
|
---|
| 198 | {
|
---|
| 199 | return (uint16_t)bin[0] << 8 |
|
---|
| 200 | (uint16_t)bin[1];
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | static inline uint8_t
|
---|
| 204 | bin_to_uint8(const uint8_t *bin)
|
---|
| 205 | {
|
---|
| 206 | return (uint8_t)bin[0];
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | MRB_END_DECL
|
---|
| 210 |
|
---|
| 211 | /** @internal crc.c */
|
---|
| 212 | uint16_t
|
---|
| 213 | calc_crc_16_ccitt(const uint8_t *src, size_t nbytes, uint16_t crc);
|
---|
| 214 |
|
---|
| 215 | #endif /* MRUBY_DUMP_H */
|
---|