Debian Codegraph @

vendor/mruby-source/src/dump.c

raw
1 /*
2 ** dump.c - mruby binary dumper (mrbc binary format)
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #include <mruby.h>
8 #include <mruby/dump.h>
9 #include <mruby/string.h>
10 #include <mruby/irep.h>
11 #include <mruby/debug.h>
12 #include <string.h>
13
14 #ifndef MRB_NO_FLOAT
15 #include <mruby/endian.h>
16 #endif
17
18 static size_t get_irep_record_size_1(mrb_state *mrb, const mrb_irep *irep);
19
20 #if UINT32_MAX > SIZE_MAX
21 # error This code cannot be built on your environment.
22 #endif
23
24 static size_t
25 get_irep_header_size(mrb_state *mrb)
26 {
27 size_t size = 0;
28
29 size += sizeof(uint32_t) * 1;
30 size += sizeof(uint16_t) * 3;
31
32 return size;
33 }
34
35 static ptrdiff_t
36 write_irep_header(mrb_state *mrb, const mrb_irep *irep, uint8_t *buf)
37 {
38 uint8_t *cur = buf;
39
40 cur += uint32_to_bin((uint32_t)get_irep_record_size_1(mrb, irep), cur); /* record size */
41 cur += uint16_to_bin((uint16_t)irep->nlocals, cur); /* number of local variable */
42 cur += uint16_to_bin((uint16_t)irep->nregs, cur); /* number of register variable */
43 cur += uint16_to_bin((uint16_t)irep->rlen, cur); /* number of child irep */
44
45 return cur - buf;
46 }
47
48 static size_t
49 get_iseq_block_size(mrb_state *mrb, const mrb_irep *irep)
50 {
51 size_t size = 0;
52
53 size += sizeof(uint16_t); /* clen */
54 size += sizeof(uint32_t); /* ilen */
55 size += irep->ilen * sizeof(mrb_code); /* iseq(n) */
56 size += irep->clen * sizeof(struct mrb_irep_catch_handler);
57
58 return size;
59 }
60
61 static ptrdiff_t
62 write_iseq_block(mrb_state *mrb, const mrb_irep *irep, uint8_t *buf, uint8_t flags)
63 {
64 uint8_t *cur = buf;
65 size_t seqlen = irep->ilen * sizeof(mrb_code) +
66 irep->clen * sizeof(struct mrb_irep_catch_handler);
67
68 cur += uint16_to_bin(irep->clen, cur); /* number of catch handlers */
69 cur += uint32_to_bin(irep->ilen, cur); /* number of opcode */
70 memcpy(cur, irep->iseq, seqlen);
71 cur += seqlen;
72
73 return cur - buf;
74 }
75
76 #ifndef MRB_NO_FLOAT
77 static void
78 dump_float(mrb_state *mrb, uint8_t *buf, mrb_float f)
79 {
80 /* dump IEEE754 binary in little endian */
81 union {
82 double f;
83 char s[sizeof(double)];
84 } u = {.f = (double)f};
85
86 if (littleendian) {
87 memcpy(buf, u.s, sizeof(double));
88 }
89 else {
90 size_t i;
91
92 for (i=0; i<sizeof(double); i++) {
93 buf[i] = u.s[sizeof(double)-i-1];
94 }
95 }
96 }
97 #endif
98
99 static size_t
100 get_pool_block_size(mrb_state *mrb, const mrb_irep *irep)
101 {
102 int pool_no;
103 size_t size = 0;
104
105 size += sizeof(uint16_t); /* plen */
106 size += irep->plen * sizeof(uint8_t); /* len(n) */
107
108 for (pool_no = 0; pool_no < irep->plen; pool_no++) {
109 int ai = mrb_gc_arena_save(mrb);
110
111 switch (irep->pool[pool_no].tt) {
112 case IREP_TT_INT64:
113 #if defined(MRB_64BIT) || defined(MRB_INT64)
114 {
115 int64_t i = irep->pool[pool_no].u.i64;
116
117 if (i < INT32_MIN || INT32_MAX < i)
118 size += 8;
119 else
120 size += 4;
121 }
122 break;
123 #else
124 /* fall through */
125 #endif
126 case IREP_TT_INT32:
127 size += 4; /* 32bits = 4bytes */
128 break;
129
130 case IREP_TT_BIGINT:
131 {
132 mrb_int len = irep->pool[pool_no].u.str[0];
133 mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
134 size += (size_t)len+2;
135 }
136 break;
137
138 case IREP_TT_FLOAT:
139 #ifndef MRB_NO_FLOAT
140 {
141 size += sizeof(double);
142 }
143 #endif
144 break;
145
146 default: /* packed IREP_TT_STRING */
147 {
148 mrb_int len = irep->pool[pool_no].tt >> 2; /* unpack length */
149 mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
150 size += sizeof(uint16_t);
151 size += (size_t)len+1;
152 }
153 break;
154 }
155 mrb_gc_arena_restore(mrb, ai);
156 }
157
158 return size;
159 }
160
161 static ptrdiff_t
162 write_pool_block(mrb_state *mrb, const mrb_irep *irep, uint8_t *buf)
163 {
164 int pool_no;
165 uint8_t *cur = buf;
166 mrb_int len;
167 const char *ptr;
168
169 cur += uint16_to_bin(irep->plen, cur); /* number of pool */
170
171 for (pool_no = 0; pool_no < irep->plen; pool_no++) {
172 int ai = mrb_gc_arena_save(mrb);
173
174 switch (irep->pool[pool_no].tt) {
175 case IREP_TT_INT64:
176 #if defined(MRB_64BIT) || defined(MRB_INT64)
177 {
178 int64_t i = irep->pool[pool_no].u.i64;
179 if (i < INT32_MIN || INT32_MAX < i) {
180 cur += uint8_to_bin(IREP_TT_INT64, cur); /* data type */
181 cur += uint32_to_bin((uint32_t)((i>>32) & 0xffffffff), cur); /* i64 hi */
182 cur += uint32_to_bin((uint32_t)((i ) & 0xffffffff), cur); /* i64 lo */
183 }
184 else {
185 cur += uint8_to_bin(IREP_TT_INT32, cur); /* data type */
186 cur += uint32_to_bin(irep->pool[pool_no].u.i32, cur); /* i32 */
187 }
188 }
189 break;
190 #endif
191 case IREP_TT_INT32:
192 cur += uint8_to_bin(IREP_TT_INT32, cur); /* data type */
193 cur += uint32_to_bin(irep->pool[pool_no].u.i32, cur); /* i32 */
194 break;
195
196 case IREP_TT_BIGINT:
197 cur += uint8_to_bin(IREP_TT_BIGINT, cur); /* data type */
198 len = irep->pool[pool_no].u.str[0];
199 memcpy(cur, irep->pool[pool_no].u.str, (size_t)len+2);
200 cur += len+2;
201 break;
202
203 case IREP_TT_FLOAT:
204 cur += uint8_to_bin(IREP_TT_FLOAT, cur); /* data type */
205 #ifndef MRB_NO_FLOAT
206 {
207 dump_float(mrb, cur,irep->pool[pool_no].u.f);
208 cur += sizeof(double);
209 }
210 #else
211 cur += uint16_to_bin(0, cur); /* zero length */
212 #endif
213 break;
214
215 default: /* string */
216 cur += uint8_to_bin(IREP_TT_STR, cur); /* data type */
217 ptr = irep->pool[pool_no].u.str;
218 len = irep->pool[pool_no].tt>>2;
219 mrb_assert_int_fit(mrb_int, len, uint16_t, UINT16_MAX);
220 cur += uint16_to_bin((uint16_t)len, cur); /* data length */
221 memcpy(cur, ptr, (size_t)len);
222 cur += len;
223 *cur++ = '\0';
224 break;
225 }
226 mrb_gc_arena_restore(mrb, ai);
227 }
228
229 return cur - buf;
230 }
231
232 static size_t
233 get_syms_block_size(mrb_state *mrb, const mrb_irep *irep)
234 {
235 size_t size = 0;
236 int sym_no;
237 mrb_int len;
238
239 size += sizeof(uint16_t); /* slen */
240 for (sym_no = 0; sym_no < irep->slen; sym_no++) {
241 size += sizeof(uint16_t); /* snl(n) */
242 if (irep->syms[sym_no] != 0) {
243 mrb_sym_name_len(mrb, irep->syms[sym_no], &len);
244 size += len + 1; /* sn(n) + null char */
245 }
246 }
247
248 return size;
249 }
250
251 static ptrdiff_t
252 write_syms_block(mrb_state *mrb, const mrb_irep *irep, uint8_t *buf)
253 {
254 int sym_no;
255 uint8_t *cur = buf;
256 const char *name;
257
258 cur += uint16_to_bin(irep->slen, cur); /* number of symbol */
259
260 for (sym_no = 0; sym_no < irep->slen; sym_no++) {
261 if (irep->syms[sym_no] != 0) {
262 mrb_int len;
263
264 name = mrb_sym_name_len(mrb, irep->syms[sym_no], &len);
265
266 mrb_assert_int_fit(mrb_int, len, uint16_t, UINT16_MAX);
267 cur += uint16_to_bin((uint16_t)len, cur); /* length of symbol name */
268 memcpy(cur, name, len); /* symbol name */
269 cur += (uint16_t)len;
270 *cur++ = '\0';
271 }
272 else {
273 cur += uint16_to_bin(MRB_DUMP_NULL_SYM_LEN, cur); /* length of symbol name */
274 }
275 }
276
277 return cur - buf;
278 }
279
280 static size_t
281 get_irep_record_size_1(mrb_state *mrb, const mrb_irep *irep)
282 {
283 size_t size = 0;
284
285 size += get_irep_header_size(mrb);
286 size += get_iseq_block_size(mrb, irep);
287 size += get_pool_block_size(mrb, irep);
288 size += get_syms_block_size(mrb, irep);
289 return size;
290 }
291
292 static size_t
293 get_irep_record_size(mrb_state *mrb, const mrb_irep *irep)
294 {
295 size_t size = 0;
296 int irep_no;
297
298 size = get_irep_record_size_1(mrb, irep);
299 for (irep_no = 0; irep_no < irep->rlen; irep_no++) {
300 size += get_irep_record_size(mrb, irep->reps[irep_no]);
301 }
302 return size;
303 }
304
305 static int
306 write_irep_record(mrb_state *mrb, const mrb_irep *irep, uint8_t *bin, size_t *irep_record_size, uint8_t flags)
307 {
308 int i;
309 uint8_t *src = bin;
310
311 if (irep == NULL) {
312 return MRB_DUMP_INVALID_IREP;
313 }
314
315 bin += write_irep_header(mrb, irep, bin);
316 bin += write_iseq_block(mrb, irep, bin, flags);
317 bin += write_pool_block(mrb, irep, bin);
318 bin += write_syms_block(mrb, irep, bin);
319
320 for (i = 0; i < irep->rlen; i++) {
321 int result;
322 size_t rsize;
323
324 result = write_irep_record(mrb, irep->reps[i], bin, &rsize, flags);
325 if (result != MRB_DUMP_OK) {
326 return result;
327 }
328 bin += rsize;
329 }
330 *irep_record_size = bin - src;
331 return MRB_DUMP_OK;
332 }
333
334 static uint32_t
335 write_footer(mrb_state *mrb, uint8_t *bin)
336 {
337 struct rite_binary_footer footer;
338
339 memcpy(footer.section_ident, RITE_BINARY_EOF, sizeof(footer.section_ident));
340 uint32_to_bin(sizeof(struct rite_binary_footer), footer.section_size);
341 memcpy(bin, &footer, sizeof(struct rite_binary_footer));
342
343 return sizeof(struct rite_binary_footer);
344 }
345
346
347 static int
348 write_section_irep_header(mrb_state *mrb, size_t section_size, uint8_t *bin)
349 {
350 struct rite_section_irep_header *header = (struct rite_section_irep_header*)bin;
351
352 memcpy(header->section_ident, RITE_SECTION_IREP_IDENT, sizeof(header->section_ident));
353
354 mrb_assert_int_fit(size_t, section_size, uint32_t, UINT32_MAX);
355 uint32_to_bin((uint32_t)section_size, header->section_size);
356 memcpy(header->rite_version, RITE_VM_VER, sizeof(header->rite_version));
357
358 return MRB_DUMP_OK;
359 }
360
361 static int
362 write_section_irep(mrb_state *mrb, const mrb_irep *irep, uint8_t *bin, size_t *len_p, uint8_t flags)
363 {
364 int result;
365 size_t rsize = 0;
366 uint8_t *cur = bin;
367
368 if (mrb == NULL || bin == NULL) {
369 return MRB_DUMP_INVALID_ARGUMENT;
370 }
371
372 cur += sizeof(struct rite_section_irep_header);
373
374 result = write_irep_record(mrb, irep, cur, &rsize, flags);
375 if (result != MRB_DUMP_OK) {
376 return result;
377 }
378 mrb_assert(rsize == get_irep_record_size(mrb, irep));
379 *len_p = cur - bin + rsize;
380 write_section_irep_header(mrb, *len_p, bin);
381
382 return MRB_DUMP_OK;
383 }
384
385 static size_t
386 get_debug_record_size(mrb_state *mrb, const mrb_irep *irep)
387 {
388 size_t ret = 0;
389 uint16_t f_idx;
390 int i;
391
392 ret += sizeof(uint32_t); /* record size */
393 ret += sizeof(uint16_t); /* file count */
394
395 for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) {
396 mrb_irep_debug_info_file const* file = irep->debug_info->files[f_idx];
397
398 ret += sizeof(uint32_t); /* position */
399 ret += sizeof(uint16_t); /* filename index */
400
401 /* lines */
402 ret += sizeof(uint32_t); /* entry count */
403 ret += sizeof(uint8_t); /* line type */
404 switch (file->line_type) {
405 case mrb_debug_line_ary:
406 ret += sizeof(uint16_t) * (size_t)(file->line_entry_count);
407 break;
408
409 case mrb_debug_line_flat_map:
410 ret += (sizeof(uint32_t) + sizeof(uint16_t)) * (size_t)(file->line_entry_count);
411 break;
412
413 case mrb_debug_line_packed_map:
414 ret += (size_t)(file->line_entry_count);
415 break;
416
417 default: mrb_assert(0); break;
418 }
419 }
420 for (i=0; i<irep->rlen; i++) {
421 ret += get_debug_record_size(mrb, irep->reps[i]);
422 }
423
424 return ret;
425 }
426
427 static int
428 find_filename_index(const mrb_sym *ary, int ary_len, mrb_sym s)
429 {
430 int i;
431
432 for (i = 0; i < ary_len; ++i) {
433 if (ary[i] == s) { return i; }
434 }
435 return -1;
436 }
437
438 static size_t
439 get_filename_table_size(mrb_state *mrb, const mrb_irep *irep, mrb_sym **fp, uint16_t *lp)
440 {
441 mrb_sym *filenames = *fp;
442 size_t size = 0;
443 const mrb_irep_debug_info *di = irep->debug_info;
444 int i;
445
446 mrb_assert(lp);
447 for (i = 0; i < di->flen; ++i) {
448 mrb_irep_debug_info_file *file;
449 mrb_int filename_len;
450
451 file = di->files[i];
452 if (find_filename_index(filenames, *lp, file->filename_sym) == -1) {
453 /* register filename */
454 *lp += 1;
455 *fp = filenames = (mrb_sym*)mrb_realloc(mrb, filenames, sizeof(mrb_sym) * (*lp));
456 filenames[*lp - 1] = file->filename_sym;
457
458 /* filename */
459 mrb_sym_name_len(mrb, file->filename_sym, &filename_len);
460 size += sizeof(uint16_t) + (size_t)filename_len;
461 }
462 }
463 for (i=0; i<irep->rlen; i++) {
464 size += get_filename_table_size(mrb, irep->reps[i], fp, lp);
465 }
466 return size;
467 }
468
469 static size_t
470 write_debug_record_1(mrb_state *mrb, const mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, uint16_t filenames_len)
471 {
472 uint8_t *cur;
473 uint16_t f_idx;
474 ptrdiff_t ret;
475
476 cur = bin + sizeof(uint32_t); /* skip record size */
477 cur += uint16_to_bin(irep->debug_info->flen, cur); /* file count */
478
479 for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) {
480 int filename_idx;
481 const mrb_irep_debug_info_file *file = irep->debug_info->files[f_idx];
482
483 /* position */
484 cur += uint32_to_bin(file->start_pos, cur);
485
486 /* filename index */
487 filename_idx = find_filename_index(filenames, filenames_len,
488 file->filename_sym);
489 mrb_assert_int_fit(int, filename_idx, uint16_t, UINT16_MAX);
490 cur += uint16_to_bin((uint16_t)filename_idx, cur);
491
492 /* lines */
493 cur += uint32_to_bin(file->line_entry_count, cur);
494 cur += uint8_to_bin(file->line_type, cur);
495 switch (file->line_type) {
496 case mrb_debug_line_ary: {
497 uint32_t l;
498 for (l = 0; l < file->line_entry_count; ++l) {
499 cur += uint16_to_bin(file->lines.ary[l], cur);
500 }
501 } break;
502
503 case mrb_debug_line_flat_map: {
504 uint32_t line;
505 for (line = 0; line < file->line_entry_count; ++line) {
506 cur += uint32_to_bin(file->lines.flat_map[line].start_pos, cur);
507 cur += uint16_to_bin(file->lines.flat_map[line].line, cur);
508 }
509 } break;
510
511 case mrb_debug_line_packed_map: {
512 memcpy(cur, file->lines.packed_map, file->line_entry_count);
513 cur += file->line_entry_count;
514 } break;
515
516 default: mrb_assert(0); break;
517 }
518 }
519
520 ret = cur - bin;
521 mrb_assert_int_fit(ptrdiff_t, ret, uint32_t, UINT32_MAX);
522 uint32_to_bin((uint32_t)ret, bin);
523
524 mrb_assert_int_fit(ptrdiff_t, ret, size_t, SIZE_MAX);
525 return (size_t)ret;
526 }
527
528 static size_t
529 write_debug_record(mrb_state *mrb, const mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, uint16_t filenames_len)
530 {
531 size_t size, len;
532 int irep_no;
533
534 size = len = write_debug_record_1(mrb, irep, bin, filenames, filenames_len);
535 bin += len;
536 for (irep_no = 0; irep_no < irep->rlen; irep_no++) {
537 len = write_debug_record(mrb, irep->reps[irep_no], bin, filenames, filenames_len);
538 bin += len;
539 size += len;
540 }
541
542 mrb_assert(size == get_debug_record_size(mrb, irep));
543 return size;
544 }
545
546 static int
547 write_section_debug(mrb_state *mrb, const mrb_irep *irep, uint8_t *cur, mrb_sym const *filenames, uint16_t filenames_len)
548 {
549 size_t section_size = 0;
550 const uint8_t *bin = cur;
551 struct rite_section_debug_header *header;
552 size_t dlen;
553 uint16_t i;
554 char const *sym; mrb_int sym_len;
555
556 if (mrb == NULL || cur == NULL) {
557 return MRB_DUMP_INVALID_ARGUMENT;
558 }
559
560 header = (struct rite_section_debug_header*)bin;
561 cur += sizeof(struct rite_section_debug_header);
562 section_size += sizeof(struct rite_section_debug_header);
563
564 /* filename table */
565 cur += uint16_to_bin(filenames_len, cur);
566 section_size += sizeof(uint16_t);
567 for (i = 0; i < filenames_len; ++i) {
568 sym = mrb_sym_name_len(mrb, filenames[i], &sym_len);
569 mrb_assert(sym);
570 cur += uint16_to_bin((uint16_t)sym_len, cur);
571 memcpy(cur, sym, sym_len);
572 cur += sym_len;
573 section_size += sizeof(uint16_t) + sym_len;
574 }
575
576 /* debug records */
577 dlen = write_debug_record(mrb, irep, cur, filenames, filenames_len);
578 section_size += dlen;
579
580 memcpy(header->section_ident, RITE_SECTION_DEBUG_IDENT, sizeof(header->section_ident));
581 mrb_assert(section_size <= INT32_MAX);
582 uint32_to_bin((uint32_t)section_size, header->section_size);
583
584 return MRB_DUMP_OK;
585 }
586
587 static void
588 create_lv_sym_table(mrb_state *mrb, const mrb_irep *irep, mrb_sym **syms, uint32_t *syms_len)
589 {
590 int i;
591
592 if (*syms == NULL) {
593 *syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) * 1);
594 }
595
596 for (i = 0; i + 1 < irep->nlocals; ++i) {
597 mrb_sym const name = irep->lv[i];
598 if (name == 0) continue;
599 if (find_filename_index(*syms, *syms_len, name) != -1) continue;
600
601 ++(*syms_len);
602 *syms = (mrb_sym*)mrb_realloc(mrb, *syms, sizeof(mrb_sym) * (*syms_len));
603 (*syms)[*syms_len - 1] = name;
604 }
605
606 for (i = 0; i < irep->rlen; ++i) {
607 create_lv_sym_table(mrb, irep->reps[i], syms, syms_len);
608 }
609 }
610
611 static int
612 write_lv_sym_table(mrb_state *mrb, uint8_t **start, mrb_sym const *syms, uint32_t syms_len)
613 {
614 uint8_t *cur = *start;
615 uint32_t i;
616 const char *str;
617 mrb_int str_len;
618
619 cur += uint32_to_bin(syms_len, cur);
620
621 for (i = 0; i < syms_len; ++i) {
622 str = mrb_sym_name_len(mrb, syms[i], &str_len);
623 cur += uint16_to_bin((uint16_t)str_len, cur);
624 memcpy(cur, str, str_len);
625 cur += str_len;
626 }
627
628 *start = cur;
629
630 return MRB_DUMP_OK;
631 }
632
633 static int
634 write_lv_record(mrb_state *mrb, const mrb_irep *irep, uint8_t **start, mrb_sym const *syms, uint32_t syms_len)
635 {
636 uint8_t *cur = *start;
637 int i;
638
639 for (i = 0; i + 1 < irep->nlocals; ++i) {
640 if (irep->lv[i] == 0) {
641 cur += uint16_to_bin(RITE_LV_NULL_MARK, cur);
642 }
643 else {
644 int const sym_idx = find_filename_index(syms, syms_len, irep->lv[i]);
645 mrb_assert(sym_idx != -1); /* local variable name must be in syms */
646
647 cur += uint16_to_bin(sym_idx, cur);
648 }
649 }
650
651 for (i = 0; i < irep->rlen; ++i) {
652 write_lv_record(mrb, irep->reps[i], &cur, syms, syms_len);
653 }
654
655 *start = cur;
656
657 return MRB_DUMP_OK;
658 }
659
660 static size_t
661 get_lv_record_size(mrb_state *mrb, const mrb_irep *irep)
662 {
663 size_t ret = 0;
664 int i;
665
666 ret += sizeof(uint16_t) * (irep->nlocals - 1);
667
668 for (i = 0; i < irep->rlen; ++i) {
669 ret += get_lv_record_size(mrb, irep->reps[i]);
670 }
671
672 return ret;
673 }
674
675 static size_t
676 get_lv_section_size(mrb_state *mrb, const mrb_irep *irep, mrb_sym const *syms, uint32_t syms_len)
677 {
678 size_t ret = 0, i;
679
680 ret += sizeof(uint32_t); /* syms_len */
681 ret += sizeof(uint16_t) * syms_len; /* symbol name lengths */
682 for (i = 0; i < syms_len; ++i) {
683 mrb_int str_len;
684 mrb_sym_name_len(mrb, syms[i], &str_len);
685 ret += str_len;
686 }
687
688 ret += get_lv_record_size(mrb, irep);
689
690 return ret;
691 }
692
693 static int
694 write_section_lv(mrb_state *mrb, const mrb_irep *irep, uint8_t *start, mrb_sym const *syms, uint32_t const syms_len)
695 {
696 uint8_t *cur = start;
697 struct rite_section_lv_header *header;
698 ptrdiff_t diff;
699 int result = MRB_DUMP_OK;
700
701 if (mrb == NULL || cur == NULL) {
702 return MRB_DUMP_INVALID_ARGUMENT;
703 }
704
705 header = (struct rite_section_lv_header*)cur;
706 cur += sizeof(struct rite_section_lv_header);
707
708 result = write_lv_sym_table(mrb, &cur, syms, syms_len);
709 if (result != MRB_DUMP_OK) {
710 goto lv_section_exit;
711 }
712
713 result = write_lv_record(mrb, irep, &cur, syms, syms_len);
714 if (result != MRB_DUMP_OK) {
715 goto lv_section_exit;
716 }
717
718 memcpy(header->section_ident, RITE_SECTION_LV_IDENT, sizeof(header->section_ident));
719
720 diff = cur - start;
721 mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
722 uint32_to_bin((uint32_t)diff, header->section_size);
723
724 lv_section_exit:
725 return result;
726 }
727
728 static int
729 write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin, uint8_t flags)
730 {
731 struct rite_binary_header *header = (struct rite_binary_header*)bin;
732
733 memcpy(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident));
734 memcpy(header->major_version, RITE_BINARY_MAJOR_VER, sizeof(header->major_version));
735 memcpy(header->minor_version, RITE_BINARY_MINOR_VER, sizeof(header->minor_version));
736 memcpy(header->compiler_name, RITE_COMPILER_NAME, sizeof(header->compiler_name));
737 memcpy(header->compiler_version, RITE_COMPILER_VERSION, sizeof(header->compiler_version));
738 mrb_assert(binary_size <= UINT32_MAX);
739 uint32_to_bin((uint32_t)binary_size, header->binary_size);
740
741 return MRB_DUMP_OK;
742 }
743
744 static mrb_bool
745 debug_info_defined_p(const mrb_irep *irep)
746 {
747 int i;
748
749 if (!irep->debug_info) return FALSE;
750 for (i=0; i<irep->rlen; i++) {
751 if (!debug_info_defined_p(irep->reps[i])) return FALSE;
752 }
753 return TRUE;
754 }
755
756 static mrb_bool
757 lv_defined_p(const mrb_irep *irep)
758 {
759 int i;
760
761 if (irep->lv) { return TRUE; }
762
763 for (i = 0; i < irep->rlen; ++i) {
764 if (lv_defined_p(irep->reps[i])) { return TRUE; }
765 }
766
767 return FALSE;
768 }
769
770 int
771 mrb_dump_irep(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
772 {
773 int result = MRB_DUMP_GENERAL_FAILURE;
774 size_t malloc_size;
775 size_t section_irep_size;
776 size_t section_lineno_size = 0, section_lv_size = 0;
777 uint8_t *cur = NULL;
778 mrb_bool const debug_info_defined = debug_info_defined_p(irep), lv_defined = lv_defined_p(irep);
779 mrb_sym *lv_syms = NULL; uint32_t lv_syms_len = 0;
780 mrb_sym *filenames = NULL; uint16_t filenames_len = 0;
781
782 if (mrb == NULL) {
783 *bin = NULL;
784 return MRB_DUMP_GENERAL_FAILURE;
785 }
786
787 section_irep_size = sizeof(struct rite_section_irep_header);
788 section_irep_size += get_irep_record_size(mrb, irep);
789
790 /* DEBUG section size */
791 if (flags & MRB_DUMP_DEBUG_INFO) {
792 if (debug_info_defined) {
793 section_lineno_size += sizeof(struct rite_section_debug_header);
794 /* filename table */
795 filenames = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) + 1);
796
797 /* filename table size */
798 section_lineno_size += sizeof(uint16_t);
799 section_lineno_size += get_filename_table_size(mrb, irep, &filenames, &filenames_len);
800
801 section_lineno_size += get_debug_record_size(mrb, irep);
802 }
803 }
804
805 if (lv_defined) {
806 section_lv_size += sizeof(struct rite_section_lv_header);
807 create_lv_sym_table(mrb, irep, &lv_syms, &lv_syms_len);
808 section_lv_size += get_lv_section_size(mrb, irep, lv_syms, lv_syms_len);
809 }
810
811 malloc_size = sizeof(struct rite_binary_header) +
812 section_irep_size + section_lineno_size + section_lv_size +
813 sizeof(struct rite_binary_footer);
814 cur = *bin = (uint8_t*)mrb_malloc(mrb, malloc_size);
815 cur += sizeof(struct rite_binary_header);
816
817 result = write_section_irep(mrb, irep, cur, &section_irep_size, flags);
818 if (result != MRB_DUMP_OK) {
819 goto error_exit;
820 }
821 cur += section_irep_size;
822 *bin_size = sizeof(struct rite_binary_header) +
823 section_irep_size + section_lineno_size + section_lv_size +
824 sizeof(struct rite_binary_footer);
825
826 /* write DEBUG section */
827 if (flags & MRB_DUMP_DEBUG_INFO) {
828 if (debug_info_defined) {
829 result = write_section_debug(mrb, irep, cur, filenames, filenames_len);
830 if (result != MRB_DUMP_OK) {
831 goto error_exit;
832 }
833 }
834 cur += section_lineno_size;
835 }
836
837 if (lv_defined) {
838 result = write_section_lv(mrb, irep, cur, lv_syms, lv_syms_len);
839 if (result != MRB_DUMP_OK) {
840 goto error_exit;
841 }
842 cur += section_lv_size;
843 }
844
845 write_footer(mrb, cur);
846 write_rite_binary_header(mrb, *bin_size, *bin, flags);
847
848 error_exit:
849 if (result != MRB_DUMP_OK) {
850 mrb_free(mrb, *bin);
851 *bin = NULL;
852 }
853 mrb_free(mrb, lv_syms);
854 mrb_free(mrb, filenames);
855 return result;
856 }
857
858 #ifndef MRB_NO_STDIO
859
860 int
861 mrb_dump_irep_binary(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, FILE* fp)
862 {
863 uint8_t *bin = NULL;
864 size_t bin_size = 0;
865 int result;
866
867 if (fp == NULL) {
868 return MRB_DUMP_INVALID_ARGUMENT;
869 }
870
871 result = mrb_dump_irep(mrb, irep, flags, &bin, &bin_size);
872 if (result == MRB_DUMP_OK) {
873 if (fwrite(bin, sizeof(bin[0]), bin_size, fp) != bin_size) {
874 result = MRB_DUMP_WRITE_FAULT;
875 }
876 }
877
878 mrb_free(mrb, bin);
879 return result;
880 }
881
882 int
883 mrb_dump_irep_cfunc(mrb_state *mrb, const mrb_irep *irep, uint8_t flags, FILE *fp, const char *initname)
884 {
885 uint8_t *bin = NULL;
886 size_t bin_size = 0, bin_idx = 0;
887 int result;
888
889 if (fp == NULL || initname == NULL || initname[0] == '\0') {
890 return MRB_DUMP_INVALID_ARGUMENT;
891 }
892 result = mrb_dump_irep(mrb, irep, flags, &bin, &bin_size);
893 if (result == MRB_DUMP_OK) {
894 if (fprintf(fp, "#include <stdint.h>\n") < 0) { /* for uint8_t under at least Darwin */
895 mrb_free(mrb, bin);
896 return MRB_DUMP_WRITE_FAULT;
897 }
898 if (fprintf(fp,
899 "%s\n"
900 "const uint8_t %s[] = {",
901 (flags & MRB_DUMP_STATIC) ? "static"
902 : "#ifdef __cplusplus\n"
903 "extern\n"
904 "#endif",
905 initname) < 0) {
906 mrb_free(mrb, bin);
907 return MRB_DUMP_WRITE_FAULT;
908 }
909 while (bin_idx < bin_size) {
910 if (bin_idx % 16 == 0) {
911 if (fputs("\n", fp) == EOF) {
912 mrb_free(mrb, bin);
913 return MRB_DUMP_WRITE_FAULT;
914 }
915 }
916 if (fprintf(fp, "0x%02x,", bin[bin_idx++]) < 0) {
917 mrb_free(mrb, bin);
918 return MRB_DUMP_WRITE_FAULT;
919 }
920 }
921 if (fputs("\n};\n", fp) == EOF) {
922 mrb_free(mrb, bin);
923 return MRB_DUMP_WRITE_FAULT;
924 }
925 }
926
927 mrb_free(mrb, bin);
928 return result;
929 }
930
931 #endif /* MRB_NO_STDIO */