Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / src / dump.c
1 /*
2 ** dump.c - mruby binary dumper (mrbc binary format)
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #include <string.h>
8 #include <limits.h>
9 #include <math.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>
15
16 #define FLAG_BYTEORDER_NATIVE 2
17 #define FLAG_BYTEORDER_NONATIVE 0
18
19 #ifndef MRB_WITHOUT_FLOAT
20 #ifdef MRB_USE_FLOAT
21 #define MRB_FLOAT_FMT "%.9g"
22 #else
23 #define MRB_FLOAT_FMT "%.17g"
24 #endif
25 #endif
26
27 static size_t get_irep_record_size_1(mrb_state *mrb, mrb_irep *irep);
28
29 #if UINT32_MAX > SIZE_MAX
30 # error This code cannot be built on your environment.
31 #endif
32
33 static size_t
34 write_padding(uint8_t *buf)
35 {
36   const size_t align = MRB_DUMP_ALIGNMENT;
37   size_t pad_len = -(intptr_t)buf & (align-1);
38   if (pad_len > 0) {
39     memset(buf, 0, pad_len);
40   }
41   return pad_len;
42 }
43
44 static size_t
45 get_irep_header_size(mrb_state *mrb)
46 {
47   size_t size = 0;
48
49   size += sizeof(uint32_t) * 1;
50   size += sizeof(uint16_t) * 3;
51
52   return size;
53 }
54
55 static ptrdiff_t
56 write_irep_header(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
57 {
58   uint8_t *cur = buf;
59
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 */
64
65   return cur - buf;
66 }
67
68
69 static size_t
70 get_iseq_block_size(mrb_state *mrb, mrb_irep *irep)
71 {
72   size_t size = 0;
73
74   size += sizeof(uint32_t); /* ilen */
75   size += sizeof(uint32_t); /* max padding */
76   size += sizeof(uint32_t) * irep->ilen; /* iseq(n) */
77
78   return size;
79 }
80
81 static ptrdiff_t
82 write_iseq_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf, uint8_t flags)
83 {
84   uint8_t *cur = buf;
85
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);
90
91   return cur - buf;
92 }
93
94 #ifndef MRB_WITHOUT_FLOAT
95 static mrb_value
96 float_to_str(mrb_state *mrb, mrb_value flt)
97 {
98   mrb_float f = mrb_float(flt);
99
100   if (isinf(f)) {
101     return f < 0 ? mrb_str_new_lit(mrb, "I") : mrb_str_new_lit(mrb, "i");
102   }
103   return  mrb_float_to_str(mrb, flt, MRB_FLOAT_FMT);
104 }
105 #endif
106
107 static size_t
108 get_pool_block_size(mrb_state *mrb, mrb_irep *irep)
109 {
110   int pool_no;
111   size_t size = 0;
112   mrb_value str;
113
114   size += sizeof(uint32_t); /* plen */
115   size += irep->plen * (sizeof(uint8_t) + sizeof(uint16_t)); /* len(n) */
116
117   for (pool_no = 0; pool_no < irep->plen; pool_no++) {
118     int ai = mrb_gc_arena_save(mrb);
119
120     switch (mrb_type(irep->pool[pool_no])) {
121     case MRB_TT_FIXNUM:
122       str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10);
123       {
124         mrb_int len = RSTRING_LEN(str);
125         mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
126         size += (size_t)len;
127       }
128       break;
129
130 #ifndef MRB_WITHOUT_FLOAT
131     case MRB_TT_FLOAT:
132       str = float_to_str(mrb, irep->pool[pool_no]);
133       {
134         mrb_int len = RSTRING_LEN(str);
135         mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
136         size += (size_t)len;
137       }
138       break;
139 #endif
140
141     case MRB_TT_STRING:
142       {
143         mrb_int len = RSTRING_LEN(irep->pool[pool_no]);
144         mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
145         size += (size_t)len;
146       }
147       break;
148
149     default:
150       break;
151     }
152     mrb_gc_arena_restore(mrb, ai);
153   }
154
155   return size;
156 }
157
158 static ptrdiff_t
159 write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
160 {
161   int pool_no;
162   uint8_t *cur = buf;
163   uint16_t len;
164   mrb_value str;
165   const char *char_ptr;
166
167   cur += uint32_to_bin(irep->plen, cur); /* number of pool */
168
169   for (pool_no = 0; pool_no < irep->plen; pool_no++) {
170     int ai = mrb_gc_arena_save(mrb);
171
172     switch (mrb_type(irep->pool[pool_no])) {
173     case MRB_TT_FIXNUM:
174       cur += uint8_to_bin(IREP_TT_FIXNUM, cur); /* data type */
175       str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10);
176       break;
177
178 #ifndef MRB_WITHOUT_FLOAT
179     case MRB_TT_FLOAT:
180       cur += uint8_to_bin(IREP_TT_FLOAT, cur); /* data type */
181       str = float_to_str(mrb, irep->pool[pool_no]);
182       break;
183 #endif
184
185     case MRB_TT_STRING:
186       cur += uint8_to_bin(IREP_TT_STRING, cur); /* data type */
187       str = irep->pool[pool_no];
188       break;
189
190     default:
191       continue;
192     }
193
194     char_ptr = RSTRING_PTR(str);
195     {
196       mrb_int tlen = RSTRING_LEN(str);
197       mrb_assert_int_fit(mrb_int, tlen, uint16_t, UINT16_MAX);
198       len = (uint16_t)tlen;
199     }
200
201     cur += uint16_to_bin(len, cur); /* data length */
202     memcpy(cur, char_ptr, (size_t)len);
203     cur += len;
204
205     mrb_gc_arena_restore(mrb, ai);
206   }
207
208   return cur - buf;
209 }
210
211
212 static size_t
213 get_syms_block_size(mrb_state *mrb, mrb_irep *irep)
214 {
215   size_t size = 0;
216   int sym_no;
217   mrb_int len;
218
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 */
225     }
226   }
227
228   return size;
229 }
230
231 static ptrdiff_t
232 write_syms_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
233 {
234   int sym_no;
235   uint8_t *cur = buf;
236   const char *name;
237
238   cur += uint32_to_bin(irep->slen, cur); /* number of symbol */
239
240   for (sym_no = 0; sym_no < irep->slen; sym_no++) {
241     if (irep->syms[sym_no] != 0) {
242       mrb_int len;
243
244       name = mrb_sym2name_len(mrb, irep->syms[sym_no], &len);
245
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;
250       *cur++ = '\0';
251     }
252     else {
253       cur += uint16_to_bin(MRB_DUMP_NULL_SYM_LEN, cur); /* length of symbol name */
254     }
255   }
256
257   return cur - buf;
258 }
259
260 static size_t
261 get_irep_record_size_1(mrb_state *mrb, mrb_irep *irep)
262 {
263   size_t size = 0;
264
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);
269   return size;
270 }
271
272 static size_t
273 get_irep_record_size(mrb_state *mrb, mrb_irep *irep)
274 {
275   size_t size = 0;
276   int irep_no;
277
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]);
281   }
282   return size;
283 }
284
285 static int
286 write_irep_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, size_t *irep_record_size, uint8_t flags)
287 {
288   int i;
289   uint8_t *src = bin;
290
291   if (irep == NULL) {
292     return MRB_DUMP_INVALID_IREP;
293   }
294
295   *irep_record_size = get_irep_record_size_1(mrb, irep);
296   if (*irep_record_size == 0) {
297     return MRB_DUMP_GENERAL_FAILURE;
298   }
299
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);
304
305   for (i = 0; i < irep->rlen; i++) {
306     int result;
307     size_t rsize;
308
309     result = write_irep_record(mrb, irep->reps[i], bin, &rsize, flags);
310     if (result != MRB_DUMP_OK) {
311       return result;
312     }
313     bin += rsize;
314   }
315   *irep_record_size = bin - src;
316   return MRB_DUMP_OK;
317 }
318
319 static uint32_t
320 write_footer(mrb_state *mrb, uint8_t *bin)
321 {
322   struct rite_binary_footer footer;
323
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));
327
328   return sizeof(struct rite_binary_footer);
329 }
330
331
332 static int
333 write_section_irep_header(mrb_state *mrb, size_t section_size, uint8_t *bin)
334 {
335   struct rite_section_irep_header *header = (struct rite_section_irep_header*)bin;
336
337   memcpy(header->section_ident, RITE_SECTION_IREP_IDENT, sizeof(header->section_ident));
338
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));
342
343   return MRB_DUMP_OK;
344 }
345
346 static int
347 write_section_irep(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, size_t *len_p, uint8_t flags)
348 {
349   int result;
350   size_t rsize = 0;
351   uint8_t *cur = bin;
352
353   if (mrb == NULL || bin == NULL) {
354     return MRB_DUMP_INVALID_ARGUMENT;
355   }
356
357   cur += sizeof(struct rite_section_irep_header);
358
359   result = write_irep_record(mrb, irep, cur, &rsize, flags);
360   if (result != MRB_DUMP_OK) {
361     return result;
362   }
363   *len_p = cur - bin + rsize;
364   write_section_irep_header(mrb, *len_p, bin);
365
366   return MRB_DUMP_OK;
367 }
368
369 static size_t
370 get_debug_record_size(mrb_state *mrb, mrb_irep *irep)
371 {
372   size_t ret = 0;
373   uint16_t f_idx;
374   int i;
375
376   ret += sizeof(uint32_t); /* record size */
377   ret += sizeof(uint16_t); /* file count */
378
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];
381
382     ret += sizeof(uint32_t); /* position */
383     ret += sizeof(uint16_t); /* filename index */
384
385     /* lines */
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);
391         break;
392
393       case mrb_debug_line_flat_map:
394         ret += (sizeof(uint32_t) + sizeof(uint16_t)) * (size_t)(file->line_entry_count);
395         break;
396
397       default: mrb_assert(0); break;
398     }
399   }
400   for (i=0; i<irep->rlen; i++) {
401     ret += get_debug_record_size(mrb, irep->reps[i]);
402   }
403
404   return ret;
405 }
406
407 static int
408 find_filename_index(const mrb_sym *ary, int ary_len, mrb_sym s)
409 {
410   int i;
411
412   for (i = 0; i < ary_len; ++i) {
413     if (ary[i] == s) { return i; }
414   }
415   return -1;
416 }
417
418 static size_t
419 get_filename_table_size(mrb_state *mrb, mrb_irep *irep, mrb_sym **fp, uint16_t *lp)
420 {
421   mrb_sym *filenames = *fp;
422   size_t size = 0;
423   mrb_irep_debug_info *di = irep->debug_info;
424   int i;
425
426   mrb_assert(lp);
427   for (i = 0; i < di->flen; ++i) {
428     mrb_irep_debug_info_file *file;
429     mrb_int filename_len;
430
431     file = di->files[i];
432     if (find_filename_index(filenames, *lp, file->filename_sym) == -1) {
433       /* register filename */
434       *lp += 1;
435       *fp = filenames = (mrb_sym *)mrb_realloc(mrb, filenames, sizeof(mrb_sym) * (*lp));
436       filenames[*lp - 1] = file->filename_sym;
437
438       /* filename */
439       mrb_sym2name_len(mrb, file->filename_sym, &filename_len);
440       size += sizeof(uint16_t) + (size_t)filename_len;
441     }
442   }
443   for (i=0; i<irep->rlen; i++) {
444     size += get_filename_table_size(mrb, irep->reps[i], fp, lp);
445   }
446   return size;
447 }
448
449 static size_t
450 write_debug_record_1(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, uint16_t filenames_len)
451 {
452   uint8_t *cur;
453   uint16_t f_idx;
454   ptrdiff_t ret;
455
456   cur = bin + sizeof(uint32_t); /* skip record size */
457   cur += uint16_to_bin(irep->debug_info->flen, cur); /* file count */
458
459   for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) {
460     int filename_idx;
461     const mrb_irep_debug_info_file *file = irep->debug_info->files[f_idx];
462
463     /* position */
464     cur += uint32_to_bin(file->start_pos, cur);
465
466     /* filename index */
467     filename_idx = find_filename_index(filenames, filenames_len,
468                                                   file->filename_sym);
469     mrb_assert_int_fit(int, filename_idx, uint16_t, UINT16_MAX);
470     cur += uint16_to_bin((uint16_t)filename_idx, cur);
471
472     /* lines */
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: {
477         uint32_t l;
478         for (l = 0; l < file->line_entry_count; ++l) {
479           cur += uint16_to_bin(file->lines.ary[l], cur);
480         }
481       } break;
482
483       case mrb_debug_line_flat_map: {
484         uint32_t line;
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);
488         }
489       } break;
490
491       default: mrb_assert(0); break;
492     }
493   }
494
495   ret = cur - bin;
496   mrb_assert_int_fit(ptrdiff_t, ret, uint32_t, UINT32_MAX);
497   uint32_to_bin((uint32_t)ret, bin);
498
499   mrb_assert_int_fit(ptrdiff_t, ret, size_t, SIZE_MAX);
500   return (size_t)ret;
501 }
502
503 static size_t
504 write_debug_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, uint16_t filenames_len)
505 {
506   size_t size, len;
507   int irep_no;
508
509   size = len = write_debug_record_1(mrb, irep, bin, filenames, filenames_len);
510   bin += 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);
513     bin += len;
514     size += len;
515   }
516
517   mrb_assert(size == get_debug_record_size(mrb, irep));
518   return size;
519 }
520
521 static int
522 write_section_debug(mrb_state *mrb, mrb_irep *irep, uint8_t *cur, mrb_sym const *filenames, uint16_t filenames_len)
523 {
524   size_t section_size = 0;
525   const uint8_t *bin = cur;
526   struct rite_section_debug_header *header;
527   size_t dlen;
528   uint16_t i;
529   char const *sym; mrb_int sym_len;
530
531   if (mrb == NULL || cur == NULL) {
532     return MRB_DUMP_INVALID_ARGUMENT;
533   }
534
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);
538
539   /* filename table */
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);
544     mrb_assert(sym);
545     cur += uint16_to_bin((uint16_t)sym_len, cur);
546     memcpy(cur, sym, sym_len);
547     cur += sym_len;
548     section_size += sizeof(uint16_t) + sym_len;
549   }
550
551   /* debug records */
552   dlen = write_debug_record(mrb, irep, cur, filenames, filenames_len);
553   section_size += dlen;
554
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);
558
559   return MRB_DUMP_OK;
560 }
561
562 static void
563 create_lv_sym_table(mrb_state *mrb, const mrb_irep *irep, mrb_sym **syms, uint32_t *syms_len)
564 {
565   int i;
566
567   if (*syms == NULL) {
568     *syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) * 1);
569   }
570
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;
575
576     ++(*syms_len);
577     *syms = (mrb_sym*)mrb_realloc(mrb, *syms, sizeof(mrb_sym) * (*syms_len));
578     (*syms)[*syms_len - 1] = name;
579   }
580
581   for (i = 0; i < irep->rlen; ++i) {
582     create_lv_sym_table(mrb, irep->reps[i], syms, syms_len);
583   }
584 }
585
586 static int
587 write_lv_sym_table(mrb_state *mrb, uint8_t **start, mrb_sym const *syms, uint32_t syms_len)
588 {
589   uint8_t *cur = *start;
590   uint32_t i;
591   const char *str;
592   mrb_int str_len;
593
594   cur += uint32_to_bin(syms_len, cur);
595
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);
600     cur += str_len;
601   }
602
603   *start = cur;
604
605   return MRB_DUMP_OK;
606 }
607
608 static int
609 write_lv_record(mrb_state *mrb, const mrb_irep *irep, uint8_t **start, mrb_sym const *syms, uint32_t syms_len)
610 {
611   uint8_t *cur = *start;
612   int i;
613
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);
618     }
619     else {
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 */
622
623       cur += uint16_to_bin(sym_idx, cur);
624       cur += uint16_to_bin(irep->lv[i].r, cur);
625     }
626   }
627
628   for (i = 0; i < irep->rlen; ++i) {
629     write_lv_record(mrb, irep->reps[i], &cur, syms, syms_len);
630   }
631
632   *start = cur;
633
634   return MRB_DUMP_OK;
635 }
636
637 static size_t
638 get_lv_record_size(mrb_state *mrb, mrb_irep *irep)
639 {
640   size_t ret = 0;
641   int i;
642
643   ret += (sizeof(uint16_t) + sizeof(uint16_t)) * (irep->nlocals - 1);
644
645   for (i = 0; i < irep->rlen; ++i) {
646     ret += get_lv_record_size(mrb, irep->reps[i]);
647   }
648
649   return ret;
650 }
651
652 static size_t
653 get_lv_section_size(mrb_state *mrb, mrb_irep *irep, mrb_sym const *syms, uint32_t syms_len)
654 {
655   size_t ret = 0, i;
656
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) {
660     mrb_int str_len;
661     mrb_sym2name_len(mrb, syms[i], &str_len);
662     ret += str_len;
663   }
664
665   ret += get_lv_record_size(mrb, irep);
666
667   return ret;
668 }
669
670 static int
671 write_section_lv(mrb_state *mrb, mrb_irep *irep, uint8_t *start, mrb_sym const *syms, uint32_t const syms_len)
672 {
673   uint8_t *cur = start;
674   struct rite_section_lv_header *header;
675   ptrdiff_t diff;
676   int result = MRB_DUMP_OK;
677
678   if (mrb == NULL || cur == NULL) {
679     return MRB_DUMP_INVALID_ARGUMENT;
680   }
681
682   header = (struct rite_section_lv_header*)cur;
683   cur += sizeof(struct rite_section_lv_header);
684
685   result = write_lv_sym_table(mrb, &cur, syms, syms_len);
686   if (result != MRB_DUMP_OK) {
687     goto lv_section_exit;
688   }
689
690   result = write_lv_record(mrb, irep, &cur, syms, syms_len);
691   if (result != MRB_DUMP_OK) {
692     goto lv_section_exit;
693   }
694
695   memcpy(header->section_ident, RITE_SECTION_LV_IDENT, sizeof(header->section_ident));
696
697   diff = cur - start;
698   mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
699   uint32_to_bin((uint32_t)diff, header->section_size);
700
701 lv_section_exit:
702   return result;
703 }
704
705 static int
706 write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin, uint8_t flags)
707 {
708   struct rite_binary_header *header = (struct rite_binary_header *)bin;
709   uint16_t crc;
710   uint32_t offset;
711
712   switch (flags & DUMP_ENDIAN_NAT) {
713   endian_big:
714   case DUMP_ENDIAN_BIG:
715     memcpy(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident));
716     break;
717   endian_little:
718   case DUMP_ENDIAN_LIL:
719     memcpy(header->binary_ident, RITE_BINARY_IDENT_LIL, sizeof(header->binary_ident));
720     break;
721
722   case DUMP_ENDIAN_NAT:
723     if (bigendian_p()) goto endian_big;
724     goto endian_little;
725     break;
726   }
727
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);
733
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);
737
738   return MRB_DUMP_OK;
739 }
740
741 static mrb_bool
742 debug_info_defined_p(mrb_irep *irep)
743 {
744   int i;
745
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;
749   }
750   return TRUE;
751 }
752
753 static mrb_bool
754 lv_defined_p(mrb_irep *irep)
755 {
756   int i;
757
758   if (irep->lv) { return TRUE; }
759
760   for (i = 0; i < irep->rlen; ++i) {
761     if (lv_defined_p(irep->reps[i])) { return TRUE; }
762   }
763
764   return FALSE;
765 }
766
767 static uint8_t
768 dump_flags(uint8_t flags, uint8_t native)
769 {
770   if (native == FLAG_BYTEORDER_NATIVE) {
771     if ((flags & DUMP_ENDIAN_NAT) == 0) {
772       return (flags & DUMP_DEBUG_INFO) | DUMP_ENDIAN_NAT;
773     }
774     return flags;
775   }
776   if ((flags & DUMP_ENDIAN_NAT) == 0) {
777     return (flags & DUMP_DEBUG_INFO) | DUMP_ENDIAN_BIG;
778   }
779   return flags;
780 }
781
782 static int
783 dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
784 {
785   int result = MRB_DUMP_GENERAL_FAILURE;
786   size_t malloc_size;
787   size_t section_irep_size;
788   size_t section_lineno_size = 0, section_lv_size = 0;
789   uint8_t *cur = NULL;
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;
793
794   if (mrb == NULL) {
795     *bin = NULL;
796     return MRB_DUMP_GENERAL_FAILURE;
797   }
798
799   section_irep_size = sizeof(struct rite_section_irep_header);
800   section_irep_size += get_irep_record_size(mrb, irep);
801
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);
806       /* filename table */
807       filenames = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) + 1);
808
809       /* filename table size */
810       section_lineno_size += sizeof(uint16_t);
811       section_lineno_size += get_filename_table_size(mrb, irep, &filenames, &filenames_len);
812
813       section_lineno_size += get_debug_record_size(mrb, irep);
814     }
815   }
816
817   if (lv_defined) {
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);
821   }
822
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);
828
829   result = write_section_irep(mrb, irep, cur, &section_irep_size, flags);
830   if (result != MRB_DUMP_OK) {
831     goto error_exit;
832   }
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);
837
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) {
843         goto error_exit;
844       }
845     }
846     cur += section_lineno_size;
847   }
848
849   if (lv_defined) {
850     result = write_section_lv(mrb, irep, cur, lv_syms, lv_syms_len);
851     if (result != MRB_DUMP_OK) {
852       goto error_exit;
853     }
854     cur += section_lv_size;
855   }
856
857   write_footer(mrb, cur);
858   write_rite_binary_header(mrb, *bin_size, *bin, flags);
859
860 error_exit:
861   if (result != MRB_DUMP_OK) {
862     mrb_free(mrb, *bin);
863     *bin = NULL;
864   }
865   mrb_free(mrb, lv_syms);
866   mrb_free(mrb, filenames);
867   return result;
868 }
869
870 int
871 mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
872 {
873   return dump_irep(mrb, irep, dump_flags(flags, FLAG_BYTEORDER_NONATIVE), bin, bin_size);
874 }
875
876 #ifndef MRB_DISABLE_STDIO
877
878 int
879 mrb_dump_irep_binary(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE* fp)
880 {
881   uint8_t *bin = NULL;
882   size_t bin_size = 0;
883   int result;
884
885   if (fp == NULL) {
886     return MRB_DUMP_INVALID_ARGUMENT;
887   }
888
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;
893     }
894   }
895
896   mrb_free(mrb, bin);
897   return result;
898 }
899
900 static mrb_bool
901 dump_bigendian_p(uint8_t flags)
902 {
903   switch (flags & DUMP_ENDIAN_NAT) {
904   case DUMP_ENDIAN_BIG:
905     return TRUE;
906   case DUMP_ENDIAN_LIL:
907     return FALSE;
908   default:
909   case DUMP_ENDIAN_NAT:
910     return bigendian_p();
911   }
912 }
913
914 int
915 mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE *fp, const char *initname)
916 {
917   uint8_t *bin = NULL;
918   size_t bin_size = 0, bin_idx = 0;
919   int result;
920
921   if (fp == NULL || initname == NULL || initname[0] == '\0') {
922     return MRB_DUMP_INVALID_ARGUMENT;
923   }
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) {
930         mrb_free(mrb, bin);
931         return MRB_DUMP_WRITE_FAULT;
932       }
933     }
934     else {
935       if (fprintf(fp, "/* dumped in big endian order.\n"
936                   "   use `mrbc -e` option for better performance on little endian CPU. */\n") < 0) {
937         mrb_free(mrb, bin);
938         return MRB_DUMP_WRITE_FAULT;
939       }
940     }
941     if (fprintf(fp, "#include <stdint.h>\n") < 0) { /* for uint8_t under at least Darwin */
942       mrb_free(mrb, bin);
943       return MRB_DUMP_WRITE_FAULT;
944     }
945     if (fprintf(fp,
946           "extern const uint8_t %s[];\n"
947           "const uint8_t\n"
948           "#if defined __GNUC__\n"
949           "__attribute__((aligned(%u)))\n"
950           "#elif defined _MSC_VER\n"
951           "__declspec(align(%u))\n"
952           "#endif\n"
953           "%s[] = {",
954           initname,
955           (uint16_t)MRB_DUMP_ALIGNMENT, (uint16_t)MRB_DUMP_ALIGNMENT, initname) < 0) {
956       mrb_free(mrb, bin);
957       return MRB_DUMP_WRITE_FAULT;
958     }
959     while (bin_idx < bin_size) {
960       if (bin_idx % 16 == 0) {
961         if (fputs("\n", fp) == EOF) {
962           mrb_free(mrb, bin);
963           return MRB_DUMP_WRITE_FAULT;
964         }
965       }
966       if (fprintf(fp, "0x%02x,", bin[bin_idx++]) < 0) {
967         mrb_free(mrb, bin);
968         return MRB_DUMP_WRITE_FAULT;
969       }
970     }
971     if (fputs("\n};\n", fp) == EOF) {
972       mrb_free(mrb, bin);
973       return MRB_DUMP_WRITE_FAULT;
974     }
975   }
976
977   mrb_free(mrb, bin);
978   return result;
979 }
980
981 #endif /* MRB_DISABLE_STDIO */