2 ** dump.c - mruby binary dumper (mrbc binary format)
4 ** See Copyright Notice in mruby.h
10 #include <mruby/dump.h>
11 #include <mruby/string.h>
12 #include <mruby/irep.h>
13 #include <mruby/numeric.h>
14 #include <mruby/debug.h>
16 #define FLAG_BYTEORDER_NATIVE 2
17 #define FLAG_BYTEORDER_NONATIVE 0
19 #ifndef MRB_WITHOUT_FLOAT
21 #define MRB_FLOAT_FMT "%.9g"
23 #define MRB_FLOAT_FMT "%.17g"
27 static size_t get_irep_record_size_1(mrb_state *mrb, mrb_irep *irep);
29 #if UINT32_MAX > SIZE_MAX
30 # error This code cannot be built on your environment.
34 write_padding(uint8_t *buf)
36 const size_t align = MRB_DUMP_ALIGNMENT;
37 size_t pad_len = -(intptr_t)buf & (align-1);
39 memset(buf, 0, pad_len);
45 get_irep_header_size(mrb_state *mrb)
49 size += sizeof(uint32_t) * 1;
50 size += sizeof(uint16_t) * 3;
56 write_irep_header(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
60 cur += uint32_to_bin((uint32_t)get_irep_record_size_1(mrb, irep), cur); /* record size */
61 cur += uint16_to_bin((uint16_t)irep->nlocals, cur); /* number of local variable */
62 cur += uint16_to_bin((uint16_t)irep->nregs, cur); /* number of register variable */
63 cur += uint16_to_bin((uint16_t)irep->rlen, cur); /* number of child irep */
70 get_iseq_block_size(mrb_state *mrb, mrb_irep *irep)
74 size += sizeof(uint32_t); /* ilen */
75 size += sizeof(uint32_t); /* max padding */
76 size += sizeof(uint32_t) * irep->ilen; /* iseq(n) */
82 write_iseq_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf, uint8_t flags)
86 cur += uint32_to_bin(irep->ilen, cur); /* number of opcode */
87 cur += write_padding(cur);
88 memcpy(cur, irep->iseq, irep->ilen * sizeof(mrb_code));
89 cur += irep->ilen * sizeof(mrb_code);
94 #ifndef MRB_WITHOUT_FLOAT
96 float_to_str(mrb_state *mrb, mrb_value flt)
98 mrb_float f = mrb_float(flt);
101 return f < 0 ? mrb_str_new_lit(mrb, "I") : mrb_str_new_lit(mrb, "i");
103 return mrb_float_to_str(mrb, flt, MRB_FLOAT_FMT);
108 get_pool_block_size(mrb_state *mrb, mrb_irep *irep)
114 size += sizeof(uint32_t); /* plen */
115 size += irep->plen * (sizeof(uint8_t) + sizeof(uint16_t)); /* len(n) */
117 for (pool_no = 0; pool_no < irep->plen; pool_no++) {
118 int ai = mrb_gc_arena_save(mrb);
120 switch (mrb_type(irep->pool[pool_no])) {
122 str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10);
124 mrb_int len = RSTRING_LEN(str);
125 mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
130 #ifndef MRB_WITHOUT_FLOAT
132 str = float_to_str(mrb, irep->pool[pool_no]);
134 mrb_int len = RSTRING_LEN(str);
135 mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
143 mrb_int len = RSTRING_LEN(irep->pool[pool_no]);
144 mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
152 mrb_gc_arena_restore(mrb, ai);
159 write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
165 const char *char_ptr;
167 cur += uint32_to_bin(irep->plen, cur); /* number of pool */
169 for (pool_no = 0; pool_no < irep->plen; pool_no++) {
170 int ai = mrb_gc_arena_save(mrb);
172 switch (mrb_type(irep->pool[pool_no])) {
174 cur += uint8_to_bin(IREP_TT_FIXNUM, cur); /* data type */
175 str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10);
178 #ifndef MRB_WITHOUT_FLOAT
180 cur += uint8_to_bin(IREP_TT_FLOAT, cur); /* data type */
181 str = float_to_str(mrb, irep->pool[pool_no]);
186 cur += uint8_to_bin(IREP_TT_STRING, cur); /* data type */
187 str = irep->pool[pool_no];
194 char_ptr = RSTRING_PTR(str);
196 mrb_int tlen = RSTRING_LEN(str);
197 mrb_assert_int_fit(mrb_int, tlen, uint16_t, UINT16_MAX);
198 len = (uint16_t)tlen;
201 cur += uint16_to_bin(len, cur); /* data length */
202 memcpy(cur, char_ptr, (size_t)len);
205 mrb_gc_arena_restore(mrb, ai);
213 get_syms_block_size(mrb_state *mrb, mrb_irep *irep)
219 size += sizeof(uint32_t); /* slen */
220 for (sym_no = 0; sym_no < irep->slen; sym_no++) {
221 size += sizeof(uint16_t); /* snl(n) */
222 if (irep->syms[sym_no] != 0) {
223 mrb_sym2name_len(mrb, irep->syms[sym_no], &len);
224 size += len + 1; /* sn(n) + null char */
232 write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
238 cur += uint32_to_bin(irep->slen, cur); /* number of symbol */
240 for (sym_no = 0; sym_no < irep->slen; sym_no++) {
241 if (irep->syms[sym_no] != 0) {
244 name = mrb_sym2name_len(mrb, irep->syms[sym_no], &len);
246 mrb_assert_int_fit(mrb_int, len, uint16_t, UINT16_MAX);
247 cur += uint16_to_bin((uint16_t)len, cur); /* length of symbol name */
248 memcpy(cur, name, len); /* symbol name */
249 cur += (uint16_t)len;
253 cur += uint16_to_bin(MRB_DUMP_NULL_SYM_LEN, cur); /* length of symbol name */
261 get_irep_record_size_1(mrb_state *mrb, mrb_irep *irep)
265 size += get_irep_header_size(mrb);
266 size += get_iseq_block_size(mrb, irep);
267 size += get_pool_block_size(mrb, irep);
268 size += get_syms_block_size(mrb, irep);
273 get_irep_record_size(mrb_state *mrb, mrb_irep *irep)
278 size = get_irep_record_size_1(mrb, irep);
279 for (irep_no = 0; irep_no < irep->rlen; irep_no++) {
280 size += get_irep_record_size(mrb, irep->reps[irep_no]);
286 write_irep_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, size_t *irep_record_size, uint8_t flags)
292 return MRB_DUMP_INVALID_IREP;
295 *irep_record_size = get_irep_record_size_1(mrb, irep);
296 if (*irep_record_size == 0) {
297 return MRB_DUMP_GENERAL_FAILURE;
300 bin += write_irep_header(mrb, irep, bin);
301 bin += write_iseq_block(mrb, irep, bin, flags);
302 bin += write_pool_block(mrb, irep, bin);
303 bin += write_syms_block(mrb, irep, bin);
305 for (i = 0; i < irep->rlen; i++) {
309 result = write_irep_record(mrb, irep->reps[i], bin, &rsize, flags);
310 if (result != MRB_DUMP_OK) {
315 *irep_record_size = bin - src;
320 write_footer(mrb_state *mrb, uint8_t *bin)
322 struct rite_binary_footer footer;
324 memcpy(footer.section_ident, RITE_BINARY_EOF, sizeof(footer.section_ident));
325 uint32_to_bin(sizeof(struct rite_binary_footer), footer.section_size);
326 memcpy(bin, &footer, sizeof(struct rite_binary_footer));
328 return sizeof(struct rite_binary_footer);
333 write_section_irep_header(mrb_state *mrb, size_t section_size, uint8_t *bin)
335 struct rite_section_irep_header *header = (struct rite_section_irep_header*)bin;
337 memcpy(header->section_ident, RITE_SECTION_IREP_IDENT, sizeof(header->section_ident));
339 mrb_assert_int_fit(size_t, section_size, uint32_t, UINT32_MAX);
340 uint32_to_bin((uint32_t)section_size, header->section_size);
341 memcpy(header->rite_version, RITE_VM_VER, sizeof(header->rite_version));
347 write_section_irep(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, size_t *len_p, uint8_t flags)
353 if (mrb == NULL || bin == NULL) {
354 return MRB_DUMP_INVALID_ARGUMENT;
357 cur += sizeof(struct rite_section_irep_header);
359 result = write_irep_record(mrb, irep, cur, &rsize, flags);
360 if (result != MRB_DUMP_OK) {
363 *len_p = cur - bin + rsize;
364 write_section_irep_header(mrb, *len_p, bin);
370 get_debug_record_size(mrb_state *mrb, mrb_irep *irep)
376 ret += sizeof(uint32_t); /* record size */
377 ret += sizeof(uint16_t); /* file count */
379 for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) {
380 mrb_irep_debug_info_file const* file = irep->debug_info->files[f_idx];
382 ret += sizeof(uint32_t); /* position */
383 ret += sizeof(uint16_t); /* filename index */
386 ret += sizeof(uint32_t); /* entry count */
387 ret += sizeof(uint8_t); /* line type */
388 switch (file->line_type) {
389 case mrb_debug_line_ary:
390 ret += sizeof(uint16_t) * (size_t)(file->line_entry_count);
393 case mrb_debug_line_flat_map:
394 ret += (sizeof(uint32_t) + sizeof(uint16_t)) * (size_t)(file->line_entry_count);
397 default: mrb_assert(0); break;
400 for (i=0; i<irep->rlen; i++) {
401 ret += get_debug_record_size(mrb, irep->reps[i]);
408 find_filename_index(const mrb_sym *ary, int ary_len, mrb_sym s)
412 for (i = 0; i < ary_len; ++i) {
413 if (ary[i] == s) { return i; }
419 get_filename_table_size(mrb_state *mrb, mrb_irep *irep, mrb_sym **fp, uint16_t *lp)
421 mrb_sym *filenames = *fp;
423 mrb_irep_debug_info *di = irep->debug_info;
427 for (i = 0; i < di->flen; ++i) {
428 mrb_irep_debug_info_file *file;
429 mrb_int filename_len;
432 if (find_filename_index(filenames, *lp, file->filename_sym) == -1) {
433 /* register filename */
435 *fp = filenames = (mrb_sym *)mrb_realloc(mrb, filenames, sizeof(mrb_sym) * (*lp));
436 filenames[*lp - 1] = file->filename_sym;
439 mrb_sym2name_len(mrb, file->filename_sym, &filename_len);
440 size += sizeof(uint16_t) + (size_t)filename_len;
443 for (i=0; i<irep->rlen; i++) {
444 size += get_filename_table_size(mrb, irep->reps[i], fp, lp);
450 write_debug_record_1(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, uint16_t filenames_len)
456 cur = bin + sizeof(uint32_t); /* skip record size */
457 cur += uint16_to_bin(irep->debug_info->flen, cur); /* file count */
459 for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) {
461 const mrb_irep_debug_info_file *file = irep->debug_info->files[f_idx];
464 cur += uint32_to_bin(file->start_pos, cur);
467 filename_idx = find_filename_index(filenames, filenames_len,
469 mrb_assert_int_fit(int, filename_idx, uint16_t, UINT16_MAX);
470 cur += uint16_to_bin((uint16_t)filename_idx, cur);
473 cur += uint32_to_bin(file->line_entry_count, cur);
474 cur += uint8_to_bin(file->line_type, cur);
475 switch (file->line_type) {
476 case mrb_debug_line_ary: {
478 for (l = 0; l < file->line_entry_count; ++l) {
479 cur += uint16_to_bin(file->lines.ary[l], cur);
483 case mrb_debug_line_flat_map: {
485 for (line = 0; line < file->line_entry_count; ++line) {
486 cur += uint32_to_bin(file->lines.flat_map[line].start_pos, cur);
487 cur += uint16_to_bin(file->lines.flat_map[line].line, cur);
491 default: mrb_assert(0); break;
496 mrb_assert_int_fit(ptrdiff_t, ret, uint32_t, UINT32_MAX);
497 uint32_to_bin((uint32_t)ret, bin);
499 mrb_assert_int_fit(ptrdiff_t, ret, size_t, SIZE_MAX);
504 write_debug_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, uint16_t filenames_len)
509 size = len = write_debug_record_1(mrb, irep, bin, filenames, filenames_len);
511 for (irep_no = 0; irep_no < irep->rlen; irep_no++) {
512 len = write_debug_record(mrb, irep->reps[irep_no], bin, filenames, filenames_len);
517 mrb_assert(size == get_debug_record_size(mrb, irep));
522 write_section_debug(mrb_state *mrb, mrb_irep *irep, uint8_t *cur, mrb_sym const *filenames, uint16_t filenames_len)
524 size_t section_size = 0;
525 const uint8_t *bin = cur;
526 struct rite_section_debug_header *header;
529 char const *sym; mrb_int sym_len;
531 if (mrb == NULL || cur == NULL) {
532 return MRB_DUMP_INVALID_ARGUMENT;
535 header = (struct rite_section_debug_header *)bin;
536 cur += sizeof(struct rite_section_debug_header);
537 section_size += sizeof(struct rite_section_debug_header);
540 cur += uint16_to_bin(filenames_len, cur);
541 section_size += sizeof(uint16_t);
542 for (i = 0; i < filenames_len; ++i) {
543 sym = mrb_sym2name_len(mrb, filenames[i], &sym_len);
545 cur += uint16_to_bin((uint16_t)sym_len, cur);
546 memcpy(cur, sym, sym_len);
548 section_size += sizeof(uint16_t) + sym_len;
552 dlen = write_debug_record(mrb, irep, cur, filenames, filenames_len);
553 section_size += dlen;
555 memcpy(header->section_ident, RITE_SECTION_DEBUG_IDENT, sizeof(header->section_ident));
556 mrb_assert(section_size <= INT32_MAX);
557 uint32_to_bin((uint32_t)section_size, header->section_size);
563 create_lv_sym_table(mrb_state *mrb, const mrb_irep *irep, mrb_sym **syms, uint32_t *syms_len)
568 *syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) * 1);
571 for (i = 0; i + 1 < irep->nlocals; ++i) {
572 mrb_sym const name = irep->lv[i].name;
573 if (name == 0) continue;
574 if (find_filename_index(*syms, *syms_len, name) != -1) continue;
577 *syms = (mrb_sym*)mrb_realloc(mrb, *syms, sizeof(mrb_sym) * (*syms_len));
578 (*syms)[*syms_len - 1] = name;
581 for (i = 0; i < irep->rlen; ++i) {
582 create_lv_sym_table(mrb, irep->reps[i], syms, syms_len);
587 write_lv_sym_table(mrb_state *mrb, uint8_t **start, mrb_sym const *syms, uint32_t syms_len)
589 uint8_t *cur = *start;
594 cur += uint32_to_bin(syms_len, cur);
596 for (i = 0; i < syms_len; ++i) {
597 str = mrb_sym2name_len(mrb, syms[i], &str_len);
598 cur += uint16_to_bin((uint16_t)str_len, cur);
599 memcpy(cur, str, str_len);
609 write_lv_record(mrb_state *mrb, const mrb_irep *irep, uint8_t **start, mrb_sym const *syms, uint32_t syms_len)
611 uint8_t *cur = *start;
614 for (i = 0; i + 1 < irep->nlocals; ++i) {
615 if (irep->lv[i].name == 0) {
616 cur += uint16_to_bin(RITE_LV_NULL_MARK, cur);
617 cur += uint16_to_bin(0, cur);
620 int const sym_idx = find_filename_index(syms, syms_len, irep->lv[i].name);
621 mrb_assert(sym_idx != -1); /* local variable name must be in syms */
623 cur += uint16_to_bin(sym_idx, cur);
624 cur += uint16_to_bin(irep->lv[i].r, cur);
628 for (i = 0; i < irep->rlen; ++i) {
629 write_lv_record(mrb, irep->reps[i], &cur, syms, syms_len);
638 get_lv_record_size(mrb_state *mrb, mrb_irep *irep)
643 ret += (sizeof(uint16_t) + sizeof(uint16_t)) * (irep->nlocals - 1);
645 for (i = 0; i < irep->rlen; ++i) {
646 ret += get_lv_record_size(mrb, irep->reps[i]);
653 get_lv_section_size(mrb_state *mrb, mrb_irep *irep, mrb_sym const *syms, uint32_t syms_len)
657 ret += sizeof(uint32_t); /* syms_len */
658 ret += sizeof(uint16_t) * syms_len; /* symbol name lengths */
659 for (i = 0; i < syms_len; ++i) {
661 mrb_sym2name_len(mrb, syms[i], &str_len);
665 ret += get_lv_record_size(mrb, irep);
671 write_section_lv(mrb_state *mrb, mrb_irep *irep, uint8_t *start, mrb_sym const *syms, uint32_t const syms_len)
673 uint8_t *cur = start;
674 struct rite_section_lv_header *header;
676 int result = MRB_DUMP_OK;
678 if (mrb == NULL || cur == NULL) {
679 return MRB_DUMP_INVALID_ARGUMENT;
682 header = (struct rite_section_lv_header*)cur;
683 cur += sizeof(struct rite_section_lv_header);
685 result = write_lv_sym_table(mrb, &cur, syms, syms_len);
686 if (result != MRB_DUMP_OK) {
687 goto lv_section_exit;
690 result = write_lv_record(mrb, irep, &cur, syms, syms_len);
691 if (result != MRB_DUMP_OK) {
692 goto lv_section_exit;
695 memcpy(header->section_ident, RITE_SECTION_LV_IDENT, sizeof(header->section_ident));
698 mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
699 uint32_to_bin((uint32_t)diff, header->section_size);
706 write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin, uint8_t flags)
708 struct rite_binary_header *header = (struct rite_binary_header *)bin;
712 switch (flags & DUMP_ENDIAN_NAT) {
714 case DUMP_ENDIAN_BIG:
715 memcpy(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident));
718 case DUMP_ENDIAN_LIL:
719 memcpy(header->binary_ident, RITE_BINARY_IDENT_LIL, sizeof(header->binary_ident));
722 case DUMP_ENDIAN_NAT:
723 if (bigendian_p()) goto endian_big;
728 memcpy(header->binary_version, RITE_BINARY_FORMAT_VER, sizeof(header->binary_version));
729 memcpy(header->compiler_name, RITE_COMPILER_NAME, sizeof(header->compiler_name));
730 memcpy(header->compiler_version, RITE_COMPILER_VERSION, sizeof(header->compiler_version));
731 mrb_assert(binary_size <= UINT32_MAX);
732 uint32_to_bin((uint32_t)binary_size, header->binary_size);
734 offset = (uint32_t)((&(header->binary_crc[0]) - bin) + sizeof(uint16_t));
735 crc = calc_crc_16_ccitt(bin + offset, binary_size - offset, 0);
736 uint16_to_bin(crc, header->binary_crc);
742 debug_info_defined_p(mrb_irep *irep)
746 if (!irep->debug_info) return FALSE;
747 for (i=0; i<irep->rlen; i++) {
748 if (!debug_info_defined_p(irep->reps[i])) return FALSE;
754 lv_defined_p(mrb_irep *irep)
758 if (irep->lv) { return TRUE; }
760 for (i = 0; i < irep->rlen; ++i) {
761 if (lv_defined_p(irep->reps[i])) { return TRUE; }
768 dump_flags(uint8_t flags, uint8_t native)
770 if (native == FLAG_BYTEORDER_NATIVE) {
771 if ((flags & DUMP_ENDIAN_NAT) == 0) {
772 return (flags & DUMP_DEBUG_INFO) | DUMP_ENDIAN_NAT;
776 if ((flags & DUMP_ENDIAN_NAT) == 0) {
777 return (flags & DUMP_DEBUG_INFO) | DUMP_ENDIAN_BIG;
783 dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
785 int result = MRB_DUMP_GENERAL_FAILURE;
787 size_t section_irep_size;
788 size_t section_lineno_size = 0, section_lv_size = 0;
790 mrb_bool const debug_info_defined = debug_info_defined_p(irep), lv_defined = lv_defined_p(irep);
791 mrb_sym *lv_syms = NULL; uint32_t lv_syms_len = 0;
792 mrb_sym *filenames = NULL; uint16_t filenames_len = 0;
796 return MRB_DUMP_GENERAL_FAILURE;
799 section_irep_size = sizeof(struct rite_section_irep_header);
800 section_irep_size += get_irep_record_size(mrb, irep);
802 /* DEBUG section size */
803 if (flags & DUMP_DEBUG_INFO) {
804 if (debug_info_defined) {
805 section_lineno_size += sizeof(struct rite_section_debug_header);
807 filenames = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) + 1);
809 /* filename table size */
810 section_lineno_size += sizeof(uint16_t);
811 section_lineno_size += get_filename_table_size(mrb, irep, &filenames, &filenames_len);
813 section_lineno_size += get_debug_record_size(mrb, irep);
818 section_lv_size += sizeof(struct rite_section_lv_header);
819 create_lv_sym_table(mrb, irep, &lv_syms, &lv_syms_len);
820 section_lv_size += get_lv_section_size(mrb, irep, lv_syms, lv_syms_len);
823 malloc_size = sizeof(struct rite_binary_header) +
824 section_irep_size + section_lineno_size + section_lv_size +
825 sizeof(struct rite_binary_footer);
826 cur = *bin = (uint8_t*)mrb_malloc(mrb, malloc_size);
827 cur += sizeof(struct rite_binary_header);
829 result = write_section_irep(mrb, irep, cur, §ion_irep_size, flags);
830 if (result != MRB_DUMP_OK) {
833 cur += section_irep_size;
834 *bin_size = sizeof(struct rite_binary_header) +
835 section_irep_size + section_lineno_size + section_lv_size +
836 sizeof(struct rite_binary_footer);
838 /* write DEBUG section */
839 if (flags & DUMP_DEBUG_INFO) {
840 if (debug_info_defined) {
841 result = write_section_debug(mrb, irep, cur, filenames, filenames_len);
842 if (result != MRB_DUMP_OK) {
846 cur += section_lineno_size;
850 result = write_section_lv(mrb, irep, cur, lv_syms, lv_syms_len);
851 if (result != MRB_DUMP_OK) {
854 cur += section_lv_size;
857 write_footer(mrb, cur);
858 write_rite_binary_header(mrb, *bin_size, *bin, flags);
861 if (result != MRB_DUMP_OK) {
865 mrb_free(mrb, lv_syms);
866 mrb_free(mrb, filenames);
871 mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
873 return dump_irep(mrb, irep, dump_flags(flags, FLAG_BYTEORDER_NONATIVE), bin, bin_size);
876 #ifndef MRB_DISABLE_STDIO
879 mrb_dump_irep_binary(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE* fp)
886 return MRB_DUMP_INVALID_ARGUMENT;
889 result = dump_irep(mrb, irep, dump_flags(flags, FLAG_BYTEORDER_NONATIVE), &bin, &bin_size);
890 if (result == MRB_DUMP_OK) {
891 if (fwrite(bin, sizeof(bin[0]), bin_size, fp) != bin_size) {
892 result = MRB_DUMP_WRITE_FAULT;
901 dump_bigendian_p(uint8_t flags)
903 switch (flags & DUMP_ENDIAN_NAT) {
904 case DUMP_ENDIAN_BIG:
906 case DUMP_ENDIAN_LIL:
909 case DUMP_ENDIAN_NAT:
910 return bigendian_p();
915 mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE *fp, const char *initname)
918 size_t bin_size = 0, bin_idx = 0;
921 if (fp == NULL || initname == NULL || initname[0] == '\0') {
922 return MRB_DUMP_INVALID_ARGUMENT;
924 flags = dump_flags(flags, FLAG_BYTEORDER_NATIVE);
925 result = dump_irep(mrb, irep, flags, &bin, &bin_size);
926 if (result == MRB_DUMP_OK) {
927 if (!dump_bigendian_p(flags)) {
928 if (fprintf(fp, "/* dumped in little endian order.\n"
929 " use `mrbc -E` option for big endian CPU. */\n") < 0) {
931 return MRB_DUMP_WRITE_FAULT;
935 if (fprintf(fp, "/* dumped in big endian order.\n"
936 " use `mrbc -e` option for better performance on little endian CPU. */\n") < 0) {
938 return MRB_DUMP_WRITE_FAULT;
941 if (fprintf(fp, "#include <stdint.h>\n") < 0) { /* for uint8_t under at least Darwin */
943 return MRB_DUMP_WRITE_FAULT;
946 "extern const uint8_t %s[];\n"
948 "#if defined __GNUC__\n"
949 "__attribute__((aligned(%u)))\n"
950 "#elif defined _MSC_VER\n"
951 "__declspec(align(%u))\n"
955 (uint16_t)MRB_DUMP_ALIGNMENT, (uint16_t)MRB_DUMP_ALIGNMENT, initname) < 0) {
957 return MRB_DUMP_WRITE_FAULT;
959 while (bin_idx < bin_size) {
960 if (bin_idx % 16 == 0) {
961 if (fputs("\n", fp) == EOF) {
963 return MRB_DUMP_WRITE_FAULT;
966 if (fprintf(fp, "0x%02x,", bin[bin_idx++]) < 0) {
968 return MRB_DUMP_WRITE_FAULT;
971 if (fputs("\n};\n", fp) == EOF) {
973 return MRB_DUMP_WRITE_FAULT;
981 #endif /* MRB_DISABLE_STDIO */