mbed I/F binding for mruby
Dependents: mruby_mbed_web mirb_mbed
mbed-mruby
How to use
Class
include/mruby/dump.h@0:158c61bb030f, 2015-03-25 (annotated)
- Committer:
- mzta
- Date:
- Wed Mar 25 17:36:16 2015 +0000
- Revision:
- 0:158c61bb030f
mirb_mbed initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mzta | 0:158c61bb030f | 1 | /* |
mzta | 0:158c61bb030f | 2 | ** mruby/dump.h - mruby binary dumper (mrbc binary format) |
mzta | 0:158c61bb030f | 3 | ** |
mzta | 0:158c61bb030f | 4 | ** See Copyright Notice in mruby.h |
mzta | 0:158c61bb030f | 5 | */ |
mzta | 0:158c61bb030f | 6 | |
mzta | 0:158c61bb030f | 7 | #ifndef MRUBY_DUMP_H |
mzta | 0:158c61bb030f | 8 | #define MRUBY_DUMP_H |
mzta | 0:158c61bb030f | 9 | |
mzta | 0:158c61bb030f | 10 | #if defined(__cplusplus) |
mzta | 0:158c61bb030f | 11 | extern "C" { |
mzta | 0:158c61bb030f | 12 | #endif |
mzta | 0:158c61bb030f | 13 | |
mzta | 0:158c61bb030f | 14 | #include "mruby.h" |
mzta | 0:158c61bb030f | 15 | #include "mruby/irep.h" |
mzta | 0:158c61bb030f | 16 | |
mzta | 0:158c61bb030f | 17 | int mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t *bin_size); |
mzta | 0:158c61bb030f | 18 | #ifdef ENABLE_STDIO |
mzta | 0:158c61bb030f | 19 | int mrb_dump_irep_binary(mrb_state*, mrb_irep*, int, FILE*); |
mzta | 0:158c61bb030f | 20 | int mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep*, int, FILE *f, const char *initname); |
mzta | 0:158c61bb030f | 21 | mrb_irep *mrb_read_irep_file(mrb_state*, FILE*); |
mzta | 0:158c61bb030f | 22 | MRB_API mrb_value mrb_load_irep_file(mrb_state*,FILE*); |
mzta | 0:158c61bb030f | 23 | MRB_API mrb_value mrb_load_irep_file_cxt(mrb_state*, FILE*, mrbc_context*); |
mzta | 0:158c61bb030f | 24 | #endif |
mzta | 0:158c61bb030f | 25 | MRB_API mrb_irep *mrb_read_irep(mrb_state*, const uint8_t*); |
mzta | 0:158c61bb030f | 26 | |
mzta | 0:158c61bb030f | 27 | /* dump/load error code |
mzta | 0:158c61bb030f | 28 | * |
mzta | 0:158c61bb030f | 29 | * NOTE: MRB_DUMP_GENERAL_FAILURE is caused by |
mzta | 0:158c61bb030f | 30 | * unspecified issues like malloc failed. |
mzta | 0:158c61bb030f | 31 | */ |
mzta | 0:158c61bb030f | 32 | #define MRB_DUMP_OK 0 |
mzta | 0:158c61bb030f | 33 | #define MRB_DUMP_GENERAL_FAILURE (-1) |
mzta | 0:158c61bb030f | 34 | #define MRB_DUMP_WRITE_FAULT (-2) |
mzta | 0:158c61bb030f | 35 | #define MRB_DUMP_READ_FAULT (-3) |
mzta | 0:158c61bb030f | 36 | #define MRB_DUMP_CRC_ERROR (-4) |
mzta | 0:158c61bb030f | 37 | #define MRB_DUMP_INVALID_FILE_HEADER (-5) |
mzta | 0:158c61bb030f | 38 | #define MRB_DUMP_INVALID_IREP (-6) |
mzta | 0:158c61bb030f | 39 | #define MRB_DUMP_INVALID_ARGUMENT (-7) |
mzta | 0:158c61bb030f | 40 | |
mzta | 0:158c61bb030f | 41 | /* null symbol length */ |
mzta | 0:158c61bb030f | 42 | #define MRB_DUMP_NULL_SYM_LEN 0xFFFF |
mzta | 0:158c61bb030f | 43 | |
mzta | 0:158c61bb030f | 44 | /* Rite Binary File header */ |
mzta | 0:158c61bb030f | 45 | #define RITE_BINARY_IDENTIFIER "RITE" |
mzta | 0:158c61bb030f | 46 | #define RITE_BINARY_FORMAT_VER "0003" |
mzta | 0:158c61bb030f | 47 | #define RITE_COMPILER_NAME "MATZ" |
mzta | 0:158c61bb030f | 48 | #define RITE_COMPILER_VERSION "0000" |
mzta | 0:158c61bb030f | 49 | |
mzta | 0:158c61bb030f | 50 | #define RITE_VM_VER "0000" |
mzta | 0:158c61bb030f | 51 | |
mzta | 0:158c61bb030f | 52 | #define RITE_BINARY_EOF "END\0" |
mzta | 0:158c61bb030f | 53 | #define RITE_SECTION_IREP_IDENTIFIER "IREP" |
mzta | 0:158c61bb030f | 54 | #define RITE_SECTION_LINENO_IDENTIFIER "LINE" |
mzta | 0:158c61bb030f | 55 | #define RITE_SECTION_DEBUG_IDENTIFIER "DBG\0" |
mzta | 0:158c61bb030f | 56 | #define RITE_SECTION_LV_IDENTIFIER "LVAR" |
mzta | 0:158c61bb030f | 57 | |
mzta | 0:158c61bb030f | 58 | #define MRB_DUMP_DEFAULT_STR_LEN 128 |
mzta | 0:158c61bb030f | 59 | #define MRB_DUMP_ALIGNMENT sizeof(uint32_t) |
mzta | 0:158c61bb030f | 60 | |
mzta | 0:158c61bb030f | 61 | /* binary header */ |
mzta | 0:158c61bb030f | 62 | struct rite_binary_header { |
mzta | 0:158c61bb030f | 63 | uint8_t binary_identify[4]; /* Binary Identifier */ |
mzta | 0:158c61bb030f | 64 | uint8_t binary_version[4]; /* Binary Format Version */ |
mzta | 0:158c61bb030f | 65 | uint8_t binary_crc[2]; /* Binary CRC */ |
mzta | 0:158c61bb030f | 66 | uint8_t binary_size[4]; /* Binary Size */ |
mzta | 0:158c61bb030f | 67 | uint8_t compiler_name[4]; /* Compiler name */ |
mzta | 0:158c61bb030f | 68 | uint8_t compiler_version[4]; |
mzta | 0:158c61bb030f | 69 | }; |
mzta | 0:158c61bb030f | 70 | |
mzta | 0:158c61bb030f | 71 | /* section header */ |
mzta | 0:158c61bb030f | 72 | #define RITE_SECTION_HEADER \ |
mzta | 0:158c61bb030f | 73 | uint8_t section_identify[4]; \ |
mzta | 0:158c61bb030f | 74 | uint8_t section_size[4] |
mzta | 0:158c61bb030f | 75 | |
mzta | 0:158c61bb030f | 76 | struct rite_section_header { |
mzta | 0:158c61bb030f | 77 | RITE_SECTION_HEADER; |
mzta | 0:158c61bb030f | 78 | }; |
mzta | 0:158c61bb030f | 79 | |
mzta | 0:158c61bb030f | 80 | struct rite_section_irep_header { |
mzta | 0:158c61bb030f | 81 | RITE_SECTION_HEADER; |
mzta | 0:158c61bb030f | 82 | |
mzta | 0:158c61bb030f | 83 | uint8_t rite_version[4]; /* Rite Instruction Specification Version */ |
mzta | 0:158c61bb030f | 84 | }; |
mzta | 0:158c61bb030f | 85 | |
mzta | 0:158c61bb030f | 86 | struct rite_section_lineno_header { |
mzta | 0:158c61bb030f | 87 | RITE_SECTION_HEADER; |
mzta | 0:158c61bb030f | 88 | }; |
mzta | 0:158c61bb030f | 89 | |
mzta | 0:158c61bb030f | 90 | struct rite_section_debug_header { |
mzta | 0:158c61bb030f | 91 | RITE_SECTION_HEADER; |
mzta | 0:158c61bb030f | 92 | }; |
mzta | 0:158c61bb030f | 93 | |
mzta | 0:158c61bb030f | 94 | struct rite_section_lv_header { |
mzta | 0:158c61bb030f | 95 | RITE_SECTION_HEADER; |
mzta | 0:158c61bb030f | 96 | }; |
mzta | 0:158c61bb030f | 97 | |
mzta | 0:158c61bb030f | 98 | #define RITE_LV_NULL_MARK UINT16_MAX |
mzta | 0:158c61bb030f | 99 | |
mzta | 0:158c61bb030f | 100 | struct rite_binary_footer { |
mzta | 0:158c61bb030f | 101 | RITE_SECTION_HEADER; |
mzta | 0:158c61bb030f | 102 | }; |
mzta | 0:158c61bb030f | 103 | |
mzta | 0:158c61bb030f | 104 | static inline size_t |
mzta | 0:158c61bb030f | 105 | uint8_to_bin(uint8_t s, uint8_t *bin) |
mzta | 0:158c61bb030f | 106 | { |
mzta | 0:158c61bb030f | 107 | *bin = s; |
mzta | 0:158c61bb030f | 108 | return sizeof(uint8_t); |
mzta | 0:158c61bb030f | 109 | } |
mzta | 0:158c61bb030f | 110 | |
mzta | 0:158c61bb030f | 111 | static inline size_t |
mzta | 0:158c61bb030f | 112 | uint16_to_bin(uint16_t s, uint8_t *bin) |
mzta | 0:158c61bb030f | 113 | { |
mzta | 0:158c61bb030f | 114 | *bin++ = (s >> 8) & 0xff; |
mzta | 0:158c61bb030f | 115 | *bin = s & 0xff; |
mzta | 0:158c61bb030f | 116 | return sizeof(uint16_t); |
mzta | 0:158c61bb030f | 117 | } |
mzta | 0:158c61bb030f | 118 | |
mzta | 0:158c61bb030f | 119 | static inline size_t |
mzta | 0:158c61bb030f | 120 | uint32_to_bin(uint32_t l, uint8_t *bin) |
mzta | 0:158c61bb030f | 121 | { |
mzta | 0:158c61bb030f | 122 | *bin++ = (l >> 24) & 0xff; |
mzta | 0:158c61bb030f | 123 | *bin++ = (l >> 16) & 0xff; |
mzta | 0:158c61bb030f | 124 | *bin++ = (l >> 8) & 0xff; |
mzta | 0:158c61bb030f | 125 | *bin = l & 0xff; |
mzta | 0:158c61bb030f | 126 | return sizeof(uint32_t); |
mzta | 0:158c61bb030f | 127 | } |
mzta | 0:158c61bb030f | 128 | |
mzta | 0:158c61bb030f | 129 | static inline uint32_t |
mzta | 0:158c61bb030f | 130 | bin_to_uint32(const uint8_t *bin) |
mzta | 0:158c61bb030f | 131 | { |
mzta | 0:158c61bb030f | 132 | return (uint32_t)bin[0] << 24 | |
mzta | 0:158c61bb030f | 133 | (uint32_t)bin[1] << 16 | |
mzta | 0:158c61bb030f | 134 | (uint32_t)bin[2] << 8 | |
mzta | 0:158c61bb030f | 135 | (uint32_t)bin[3]; |
mzta | 0:158c61bb030f | 136 | } |
mzta | 0:158c61bb030f | 137 | |
mzta | 0:158c61bb030f | 138 | static inline uint16_t |
mzta | 0:158c61bb030f | 139 | bin_to_uint16(const uint8_t *bin) |
mzta | 0:158c61bb030f | 140 | { |
mzta | 0:158c61bb030f | 141 | return (uint16_t)bin[0] << 8 | |
mzta | 0:158c61bb030f | 142 | (uint16_t)bin[1]; |
mzta | 0:158c61bb030f | 143 | } |
mzta | 0:158c61bb030f | 144 | |
mzta | 0:158c61bb030f | 145 | static inline uint8_t |
mzta | 0:158c61bb030f | 146 | bin_to_uint8(const uint8_t *bin) |
mzta | 0:158c61bb030f | 147 | { |
mzta | 0:158c61bb030f | 148 | return (uint8_t)bin[0]; |
mzta | 0:158c61bb030f | 149 | } |
mzta | 0:158c61bb030f | 150 | |
mzta | 0:158c61bb030f | 151 | #if defined(__cplusplus) |
mzta | 0:158c61bb030f | 152 | } /* extern "C" { */ |
mzta | 0:158c61bb030f | 153 | #endif |
mzta | 0:158c61bb030f | 154 | |
mzta | 0:158c61bb030f | 155 | /* crc.c */ |
mzta | 0:158c61bb030f | 156 | uint16_t |
mzta | 0:158c61bb030f | 157 | calc_crc_16_ccitt(const uint8_t *src, size_t nbytes, uint16_t crc); |
mzta | 0:158c61bb030f | 158 | |
mzta | 0:158c61bb030f | 159 | #endif /* MRUBY_DUMP_H */ |
mzta | 0:158c61bb030f | 160 |