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 "mruby/dump.h"
|
---|
10 | #include "mruby/string.h"
|
---|
11 | #include "mruby/irep.h"
|
---|
12 | #include "mruby/numeric.h"
|
---|
13 | #include "mruby/debug.h"
|
---|
14 |
|
---|
15 | #define FLAG_BYTEORDER_NATIVE 2
|
---|
16 | #define FLAG_BYTEORDER_NONATIVE 0
|
---|
17 |
|
---|
18 | #ifdef MRB_USE_FLOAT
|
---|
19 | #define MRB_FLOAT_FMT "%.8e"
|
---|
20 | #else
|
---|
21 | #define MRB_FLOAT_FMT "%.16e"
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | static size_t get_irep_record_size_1(mrb_state *mrb, mrb_irep *irep);
|
---|
25 |
|
---|
26 | #if UINT32_MAX > SIZE_MAX
|
---|
27 | # error This code cannot be built on your environment.
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | static size_t
|
---|
31 | write_padding(uint8_t *buf)
|
---|
32 | {
|
---|
33 | const size_t align = MRB_DUMP_ALIGNMENT;
|
---|
34 | size_t pad_len = -(intptr_t)buf & (align-1);
|
---|
35 | if (pad_len > 0) {
|
---|
36 | memset(buf, 0, pad_len);
|
---|
37 | }
|
---|
38 | return pad_len;
|
---|
39 | }
|
---|
40 |
|
---|
41 | static size_t
|
---|
42 | get_irep_header_size(mrb_state *mrb)
|
---|
43 | {
|
---|
44 | size_t size = 0;
|
---|
45 |
|
---|
46 | size += sizeof(uint32_t) * 1;
|
---|
47 | size += sizeof(uint16_t) * 3;
|
---|
48 |
|
---|
49 | return size;
|
---|
50 | }
|
---|
51 |
|
---|
52 | static ptrdiff_t
|
---|
53 | write_irep_header(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
|
---|
54 | {
|
---|
55 | uint8_t *cur = buf;
|
---|
56 |
|
---|
57 | cur += uint32_to_bin(get_irep_record_size_1(mrb, irep), cur); /* record size */
|
---|
58 | cur += uint16_to_bin((uint16_t)irep->nlocals, cur); /* number of local variable */
|
---|
59 | cur += uint16_to_bin((uint16_t)irep->nregs, cur); /* number of register variable */
|
---|
60 | cur += uint16_to_bin((uint16_t)irep->rlen, cur); /* number of child irep */
|
---|
61 |
|
---|
62 | return cur - buf;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | static size_t
|
---|
67 | get_iseq_block_size(mrb_state *mrb, mrb_irep *irep)
|
---|
68 | {
|
---|
69 | size_t size = 0;
|
---|
70 |
|
---|
71 | size += sizeof(uint32_t); /* ilen */
|
---|
72 | size += sizeof(uint32_t); /* max padding */
|
---|
73 | size += sizeof(uint32_t) * irep->ilen; /* iseq(n) */
|
---|
74 |
|
---|
75 | return size;
|
---|
76 | }
|
---|
77 |
|
---|
78 | static ptrdiff_t
|
---|
79 | write_iseq_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf, uint8_t flags)
|
---|
80 | {
|
---|
81 | uint8_t *cur = buf;
|
---|
82 | uint32_t iseq_no;
|
---|
83 |
|
---|
84 | cur += uint32_to_bin(irep->ilen, cur); /* number of opcode */
|
---|
85 | cur += write_padding(cur);
|
---|
86 | switch (flags & DUMP_ENDIAN_NAT) {
|
---|
87 | case DUMP_ENDIAN_BIG:
|
---|
88 | if (bigendian_p()) goto native;
|
---|
89 | for (iseq_no = 0; iseq_no < irep->ilen; iseq_no++) {
|
---|
90 | cur += uint32_to_bin(irep->iseq[iseq_no], cur); /* opcode */
|
---|
91 | }
|
---|
92 | break;
|
---|
93 | case DUMP_ENDIAN_LIL:
|
---|
94 | if (!bigendian_p()) goto native;
|
---|
95 | for (iseq_no = 0; iseq_no < irep->ilen; iseq_no++) {
|
---|
96 | cur += uint32l_to_bin(irep->iseq[iseq_no], cur); /* opcode */
|
---|
97 | }
|
---|
98 | break;
|
---|
99 |
|
---|
100 | native:
|
---|
101 | case DUMP_ENDIAN_NAT:
|
---|
102 | memcpy(cur, irep->iseq, irep->ilen * sizeof(mrb_code));
|
---|
103 | cur += irep->ilen * sizeof(mrb_code);
|
---|
104 | break;
|
---|
105 | }
|
---|
106 |
|
---|
107 | return cur - buf;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | static size_t
|
---|
112 | get_pool_block_size(mrb_state *mrb, mrb_irep *irep)
|
---|
113 | {
|
---|
114 | size_t size = 0;
|
---|
115 | size_t pool_no;
|
---|
116 | mrb_value str;
|
---|
117 |
|
---|
118 | size += sizeof(uint32_t); /* plen */
|
---|
119 | size += irep->plen * (sizeof(uint8_t) + sizeof(uint16_t)); /* len(n) */
|
---|
120 |
|
---|
121 | for (pool_no = 0; pool_no < irep->plen; pool_no++) {
|
---|
122 | int ai = mrb_gc_arena_save(mrb);
|
---|
123 |
|
---|
124 | switch (mrb_type(irep->pool[pool_no])) {
|
---|
125 | case MRB_TT_FIXNUM:
|
---|
126 | str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10);
|
---|
127 | {
|
---|
128 | mrb_int len = RSTRING_LEN(str);
|
---|
129 | mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
|
---|
130 | size += (size_t)len;
|
---|
131 | }
|
---|
132 | break;
|
---|
133 |
|
---|
134 | case MRB_TT_FLOAT:
|
---|
135 | str = mrb_float_to_str(mrb, irep->pool[pool_no], MRB_FLOAT_FMT);
|
---|
136 | {
|
---|
137 | mrb_int len = RSTRING_LEN(str);
|
---|
138 | mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
|
---|
139 | size += (size_t)len;
|
---|
140 | }
|
---|
141 | break;
|
---|
142 |
|
---|
143 | case MRB_TT_STRING:
|
---|
144 | {
|
---|
145 | mrb_int len = RSTRING_LEN(irep->pool[pool_no]);
|
---|
146 | mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
|
---|
147 | size += (size_t)len;
|
---|
148 | }
|
---|
149 | break;
|
---|
150 |
|
---|
151 | default:
|
---|
152 | break;
|
---|
153 | }
|
---|
154 | mrb_gc_arena_restore(mrb, ai);
|
---|
155 | }
|
---|
156 |
|
---|
157 | return size;
|
---|
158 | }
|
---|
159 |
|
---|
160 | static ptrdiff_t
|
---|
161 | write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf)
|
---|
162 | {
|
---|
163 | size_t pool_no;
|
---|
164 | uint8_t *cur = buf;
|
---|
165 | uint16_t len;
|
---|
166 | mrb_value str;
|
---|
167 | const char *char_ptr;
|
---|
168 |
|
---|
169 | cur += uint32_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 (mrb_type(irep->pool[pool_no])) {
|
---|
175 | case MRB_TT_FIXNUM:
|
---|
176 | cur += uint8_to_bin(IREP_TT_FIXNUM, cur); /* data type */
|
---|
177 | str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10);
|
---|
178 | break;
|
---|
179 |
|
---|
180 | case MRB_TT_FLOAT:
|
---|
181 | cur += uint8_to_bin(IREP_TT_FLOAT, cur); /* data type */
|
---|
182 | str = mrb_float_to_str(mrb, irep->pool[pool_no], MRB_FLOAT_FMT);
|
---|
183 | break;
|
---|
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 | uint32_t 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 | uint32_t 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 | size_t 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 | uint32_t 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 int
|
---|
370 | write_section_lineno_header(mrb_state *mrb, size_t section_size, uint8_t *bin)
|
---|
371 | {
|
---|
372 | struct rite_section_lineno_header *header = (struct rite_section_lineno_header*)bin;
|
---|
373 |
|
---|
374 | memcpy(header->section_ident, RITE_SECTION_LINENO_IDENT, sizeof(header->section_ident));
|
---|
375 | uint32_to_bin((uint32_t)section_size, header->section_size);
|
---|
376 |
|
---|
377 | return MRB_DUMP_OK;
|
---|
378 | }
|
---|
379 |
|
---|
380 | static size_t
|
---|
381 | get_lineno_record_size(mrb_state *mrb, mrb_irep *irep)
|
---|
382 | {
|
---|
383 | size_t size = 0;
|
---|
384 |
|
---|
385 | size += sizeof(uint32_t); /* record size */
|
---|
386 | size += sizeof(uint16_t); /* filename size */
|
---|
387 | if (irep->filename) {
|
---|
388 | size += strlen(irep->filename); /* filename */
|
---|
389 | }
|
---|
390 | size += sizeof(uint32_t); /* niseq */
|
---|
391 | if (irep->lines) {
|
---|
392 | size += sizeof(uint16_t) * irep->ilen; /* lineno */
|
---|
393 | }
|
---|
394 |
|
---|
395 | return size;
|
---|
396 | }
|
---|
397 |
|
---|
398 | static size_t
|
---|
399 | write_lineno_record_1(mrb_state *mrb, mrb_irep *irep, uint8_t* bin)
|
---|
400 | {
|
---|
401 | uint8_t *cur = bin;
|
---|
402 | size_t iseq_no;
|
---|
403 | size_t filename_len;
|
---|
404 | ptrdiff_t diff;
|
---|
405 |
|
---|
406 | cur += sizeof(uint32_t); /* record size */
|
---|
407 |
|
---|
408 | if (irep->filename) {
|
---|
409 | filename_len = strlen(irep->filename);
|
---|
410 | } else {
|
---|
411 | filename_len = 0;
|
---|
412 | }
|
---|
413 | mrb_assert_int_fit(size_t, filename_len, uint16_t, UINT16_MAX);
|
---|
414 | cur += uint16_to_bin((uint16_t)filename_len, cur); /* filename size */
|
---|
415 |
|
---|
416 | if (filename_len) {
|
---|
417 | memcpy(cur, irep->filename, filename_len);
|
---|
418 | cur += filename_len; /* filename */
|
---|
419 | }
|
---|
420 |
|
---|
421 | if (irep->lines) {
|
---|
422 | mrb_assert_int_fit(size_t, irep->ilen, uint32_t, UINT32_MAX);
|
---|
423 | cur += uint32_to_bin((uint32_t)(irep->ilen), cur); /* niseq */
|
---|
424 | for (iseq_no = 0; iseq_no < irep->ilen; iseq_no++) {
|
---|
425 | cur += uint16_to_bin(irep->lines[iseq_no], cur); /* opcode */
|
---|
426 | }
|
---|
427 | }
|
---|
428 | else {
|
---|
429 | cur += uint32_to_bin(0, cur); /* niseq */
|
---|
430 | }
|
---|
431 |
|
---|
432 | diff = cur - bin;
|
---|
433 | mrb_assert_int_fit(ptrdiff_t, diff, uint32_t, UINT32_MAX);
|
---|
434 |
|
---|
435 | uint32_to_bin((uint32_t)diff, bin); /* record size */
|
---|
436 |
|
---|
437 | mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
|
---|
438 | return (size_t)diff;
|
---|
439 | }
|
---|
440 |
|
---|
441 | static size_t
|
---|
442 | write_lineno_record(mrb_state *mrb, mrb_irep *irep, uint8_t* bin)
|
---|
443 | {
|
---|
444 | size_t i;
|
---|
445 | size_t rlen, size = 0;
|
---|
446 |
|
---|
447 | rlen = write_lineno_record_1(mrb, irep, bin);
|
---|
448 | bin += rlen;
|
---|
449 | size += rlen;
|
---|
450 | for (i=0; i<irep->rlen; i++) {
|
---|
451 | rlen = write_lineno_record(mrb, irep, bin);
|
---|
452 | bin += rlen;
|
---|
453 | size += rlen;
|
---|
454 | }
|
---|
455 | return size;
|
---|
456 | }
|
---|
457 |
|
---|
458 | static int
|
---|
459 | write_section_lineno(mrb_state *mrb, mrb_irep *irep, uint8_t *bin)
|
---|
460 | {
|
---|
461 | size_t section_size = 0;
|
---|
462 | size_t rlen = 0; /* size of irep record */
|
---|
463 | uint8_t *cur = bin;
|
---|
464 |
|
---|
465 | if (mrb == NULL || bin == NULL) {
|
---|
466 | return MRB_DUMP_INVALID_ARGUMENT;
|
---|
467 | }
|
---|
468 |
|
---|
469 | cur += sizeof(struct rite_section_lineno_header);
|
---|
470 | section_size += sizeof(struct rite_section_lineno_header);
|
---|
471 |
|
---|
472 | rlen = write_lineno_record(mrb, irep, cur);
|
---|
473 | section_size += rlen;
|
---|
474 |
|
---|
475 | write_section_lineno_header(mrb, section_size, bin);
|
---|
476 |
|
---|
477 | return MRB_DUMP_OK;
|
---|
478 | }
|
---|
479 |
|
---|
480 | static size_t
|
---|
481 | get_debug_record_size(mrb_state *mrb, mrb_irep *irep)
|
---|
482 | {
|
---|
483 | size_t ret = 0;
|
---|
484 | uint16_t f_idx;
|
---|
485 | size_t i;
|
---|
486 |
|
---|
487 | ret += sizeof(uint32_t); /* record size */
|
---|
488 | ret += sizeof(uint16_t); /* file count */
|
---|
489 |
|
---|
490 | for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) {
|
---|
491 | mrb_irep_debug_info_file const* file = irep->debug_info->files[f_idx];
|
---|
492 |
|
---|
493 | ret += sizeof(uint32_t); /* position */
|
---|
494 | ret += sizeof(uint16_t); /* filename index */
|
---|
495 |
|
---|
496 | /* lines */
|
---|
497 | ret += sizeof(uint32_t); /* entry count */
|
---|
498 | ret += sizeof(uint8_t); /* line type */
|
---|
499 | switch (file->line_type) {
|
---|
500 | case mrb_debug_line_ary:
|
---|
501 | ret += sizeof(uint16_t) * (size_t)(file->line_entry_count);
|
---|
502 | break;
|
---|
503 |
|
---|
504 | case mrb_debug_line_flat_map:
|
---|
505 | ret += (sizeof(uint32_t) + sizeof(uint16_t)) * (size_t)(file->line_entry_count);
|
---|
506 | break;
|
---|
507 |
|
---|
508 | default: mrb_assert(0); break;
|
---|
509 | }
|
---|
510 | }
|
---|
511 | for (i=0; i<irep->rlen; i++) {
|
---|
512 | ret += get_debug_record_size(mrb, irep->reps[i]);
|
---|
513 | }
|
---|
514 |
|
---|
515 | return ret;
|
---|
516 | }
|
---|
517 |
|
---|
518 | static int
|
---|
519 | find_filename_index(const mrb_sym *ary, int ary_len, mrb_sym s)
|
---|
520 | {
|
---|
521 | int i;
|
---|
522 |
|
---|
523 | for (i = 0; i < ary_len; ++i) {
|
---|
524 | if (ary[i] == s) { return i; }
|
---|
525 | }
|
---|
526 | return -1;
|
---|
527 | }
|
---|
528 |
|
---|
529 | static size_t
|
---|
530 | get_filename_table_size(mrb_state *mrb, mrb_irep *irep, mrb_sym **fp, uint16_t *lp)
|
---|
531 | {
|
---|
532 | mrb_sym *filenames = *fp;
|
---|
533 | size_t i, size = 0;
|
---|
534 | mrb_irep_debug_info *di = irep->debug_info;
|
---|
535 |
|
---|
536 | mrb_assert(lp);
|
---|
537 | for (i = 0; i < di->flen; ++i) {
|
---|
538 | mrb_irep_debug_info_file *file;
|
---|
539 | mrb_int filename_len;
|
---|
540 |
|
---|
541 | file = di->files[i];
|
---|
542 | if (find_filename_index(filenames, *lp, file->filename_sym) == -1) {
|
---|
543 | /* register filename */
|
---|
544 | *lp += 1;
|
---|
545 | *fp = filenames = (mrb_sym *)mrb_realloc(mrb, filenames, sizeof(mrb_sym) * (*lp));
|
---|
546 | filenames[*lp - 1] = file->filename_sym;
|
---|
547 |
|
---|
548 | /* filename */
|
---|
549 | mrb_sym2name_len(mrb, file->filename_sym, &filename_len);
|
---|
550 | size += sizeof(uint16_t) + (size_t)filename_len;
|
---|
551 | }
|
---|
552 | }
|
---|
553 | for (i=0; i<irep->rlen; i++) {
|
---|
554 | size += get_filename_table_size(mrb, irep->reps[i], fp, lp);
|
---|
555 | }
|
---|
556 | return size;
|
---|
557 | }
|
---|
558 |
|
---|
559 | static size_t
|
---|
560 | write_debug_record_1(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, uint16_t filenames_len)
|
---|
561 | {
|
---|
562 | uint8_t *cur;
|
---|
563 | uint16_t f_idx;
|
---|
564 | ptrdiff_t ret;
|
---|
565 |
|
---|
566 | cur = bin + sizeof(uint32_t); /* skip record size */
|
---|
567 | cur += uint16_to_bin(irep->debug_info->flen, cur); /* file count */
|
---|
568 |
|
---|
569 | for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) {
|
---|
570 | int filename_idx;
|
---|
571 | const mrb_irep_debug_info_file *file = irep->debug_info->files[f_idx];
|
---|
572 |
|
---|
573 | /* position */
|
---|
574 | cur += uint32_to_bin(file->start_pos, cur);
|
---|
575 |
|
---|
576 | /* filename index */
|
---|
577 | filename_idx = find_filename_index(filenames, filenames_len,
|
---|
578 | file->filename_sym);
|
---|
579 | mrb_assert_int_fit(int, filename_idx, uint16_t, UINT16_MAX);
|
---|
580 | cur += uint16_to_bin((uint16_t)filename_idx, cur);
|
---|
581 |
|
---|
582 | /* lines */
|
---|
583 | cur += uint32_to_bin(file->line_entry_count, cur);
|
---|
584 | cur += uint8_to_bin(file->line_type, cur);
|
---|
585 | switch (file->line_type) {
|
---|
586 | case mrb_debug_line_ary: {
|
---|
587 | uint32_t l;
|
---|
588 | for (l = 0; l < file->line_entry_count; ++l) {
|
---|
589 | cur += uint16_to_bin(file->lines.ary[l], cur);
|
---|
590 | }
|
---|
591 | } break;
|
---|
592 |
|
---|
593 | case mrb_debug_line_flat_map: {
|
---|
594 | uint32_t line;
|
---|
595 | for (line = 0; line < file->line_entry_count; ++line) {
|
---|
596 | cur += uint32_to_bin(file->lines.flat_map[line].start_pos, cur);
|
---|
597 | cur += uint16_to_bin(file->lines.flat_map[line].line, cur);
|
---|
598 | }
|
---|
599 | } break;
|
---|
600 |
|
---|
601 | default: mrb_assert(0); break;
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | ret = cur - bin;
|
---|
606 | mrb_assert_int_fit(ptrdiff_t, ret, uint32_t, UINT32_MAX);
|
---|
607 | uint32_to_bin(ret, bin);
|
---|
608 |
|
---|
609 | mrb_assert_int_fit(ptrdiff_t, ret, size_t, SIZE_MAX);
|
---|
610 | return (size_t)ret;
|
---|
611 | }
|
---|
612 |
|
---|
613 | static size_t
|
---|
614 | write_debug_record(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, mrb_sym const* filenames, uint16_t filenames_len)
|
---|
615 | {
|
---|
616 | size_t size, len;
|
---|
617 | size_t irep_no;
|
---|
618 |
|
---|
619 | size = len = write_debug_record_1(mrb, irep, bin, filenames, filenames_len);
|
---|
620 | bin += len;
|
---|
621 | for (irep_no = 0; irep_no < irep->rlen; irep_no++) {
|
---|
622 | len = write_debug_record(mrb, irep->reps[irep_no], bin, filenames, filenames_len);
|
---|
623 | bin += len;
|
---|
624 | size += len;
|
---|
625 | }
|
---|
626 |
|
---|
627 | mrb_assert(size == get_debug_record_size(mrb, irep));
|
---|
628 | return size;
|
---|
629 | }
|
---|
630 |
|
---|
631 | static int
|
---|
632 | write_section_debug(mrb_state *mrb, mrb_irep *irep, uint8_t *cur, mrb_sym const *filenames, uint16_t filenames_len)
|
---|
633 | {
|
---|
634 | size_t section_size = 0;
|
---|
635 | const uint8_t *bin = cur;
|
---|
636 | struct rite_section_debug_header *header;
|
---|
637 | size_t dlen;
|
---|
638 | uint16_t i;
|
---|
639 | char const *sym; mrb_int sym_len;
|
---|
640 |
|
---|
641 | if (mrb == NULL || cur == NULL) {
|
---|
642 | return MRB_DUMP_INVALID_ARGUMENT;
|
---|
643 | }
|
---|
644 |
|
---|
645 | header = (struct rite_section_debug_header *)bin;
|
---|
646 | cur += sizeof(struct rite_section_debug_header);
|
---|
647 | section_size += sizeof(struct rite_section_debug_header);
|
---|
648 |
|
---|
649 | /* filename table */
|
---|
650 | cur += uint16_to_bin(filenames_len, cur);
|
---|
651 | section_size += sizeof(uint16_t);
|
---|
652 | for (i = 0; i < filenames_len; ++i) {
|
---|
653 | sym = mrb_sym2name_len(mrb, filenames[i], &sym_len);
|
---|
654 | mrb_assert(sym);
|
---|
655 | cur += uint16_to_bin(sym_len, cur);
|
---|
656 | memcpy(cur, sym, sym_len);
|
---|
657 | cur += sym_len;
|
---|
658 | section_size += sizeof(uint16_t) + sym_len;
|
---|
659 | }
|
---|
660 |
|
---|
661 | /* debug records */
|
---|
662 | dlen = write_debug_record(mrb, irep, cur, filenames, filenames_len);
|
---|
663 | section_size += dlen;
|
---|
664 |
|
---|
665 | memcpy(header->section_ident, RITE_SECTION_DEBUG_IDENT, sizeof(header->section_ident));
|
---|
666 | mrb_assert(section_size <= INT32_MAX);
|
---|
667 | uint32_to_bin(section_size, header->section_size);
|
---|
668 |
|
---|
669 | return MRB_DUMP_OK;
|
---|
670 | }
|
---|
671 |
|
---|
672 | static void
|
---|
673 | create_lv_sym_table(mrb_state *mrb, const mrb_irep *irep, mrb_sym **syms, uint32_t *syms_len)
|
---|
674 | {
|
---|
675 | size_t i;
|
---|
676 |
|
---|
677 | if (*syms == NULL) {
|
---|
678 | *syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) * 1);
|
---|
679 | }
|
---|
680 |
|
---|
681 | for (i = 0; i + 1 < irep->nlocals; ++i) {
|
---|
682 | mrb_sym const name = irep->lv[i].name;
|
---|
683 | if (name == 0) continue;
|
---|
684 | if (find_filename_index(*syms, *syms_len, name) != -1) continue;
|
---|
685 |
|
---|
686 | ++(*syms_len);
|
---|
687 | *syms = (mrb_sym*)mrb_realloc(mrb, *syms, sizeof(mrb_sym) * (*syms_len));
|
---|
688 | (*syms)[*syms_len - 1] = name;
|
---|
689 | }
|
---|
690 |
|
---|
691 | for (i = 0; i < irep->rlen; ++i) {
|
---|
692 | create_lv_sym_table(mrb, irep->reps[i], syms, syms_len);
|
---|
693 | }
|
---|
694 | }
|
---|
695 |
|
---|
696 | static int
|
---|
697 | write_lv_sym_table(mrb_state *mrb, uint8_t **start, mrb_sym const *syms, uint32_t syms_len)
|
---|
698 | {
|
---|
699 | uint8_t *cur = *start;
|
---|
700 | uint32_t i;
|
---|
701 | const char *str;
|
---|
702 | mrb_int str_len;
|
---|
703 |
|
---|
704 | cur += uint32_to_bin(syms_len, cur);
|
---|
705 |
|
---|
706 | for (i = 0; i < syms_len; ++i) {
|
---|
707 | str = mrb_sym2name_len(mrb, syms[i], &str_len);
|
---|
708 | cur += uint16_to_bin(str_len, cur);
|
---|
709 | memcpy(cur, str, str_len);
|
---|
710 | cur += str_len;
|
---|
711 | }
|
---|
712 |
|
---|
713 | *start = cur;
|
---|
714 |
|
---|
715 | return MRB_DUMP_OK;
|
---|
716 | }
|
---|
717 |
|
---|
718 | static int
|
---|
719 | write_lv_record(mrb_state *mrb, const mrb_irep *irep, uint8_t **start, mrb_sym const *syms, uint32_t syms_len)
|
---|
720 | {
|
---|
721 | uint8_t *cur = *start;
|
---|
722 | size_t i;
|
---|
723 |
|
---|
724 | for (i = 0; i + 1 < irep->nlocals; ++i) {
|
---|
725 | if (irep->lv[i].name == 0) {
|
---|
726 | cur += uint16_to_bin(RITE_LV_NULL_MARK, cur);
|
---|
727 | cur += uint16_to_bin(0, cur);
|
---|
728 | }
|
---|
729 | else {
|
---|
730 | int const sym_idx = find_filename_index(syms, syms_len, irep->lv[i].name);
|
---|
731 | mrb_assert(sym_idx != -1); /* local variable name must be in syms */
|
---|
732 |
|
---|
733 | cur += uint16_to_bin(sym_idx, cur);
|
---|
734 | cur += uint16_to_bin(irep->lv[i].r, cur);
|
---|
735 | }
|
---|
736 | }
|
---|
737 |
|
---|
738 | for (i = 0; i < irep->rlen; ++i) {
|
---|
739 | write_lv_record(mrb, irep->reps[i], &cur, syms, syms_len);
|
---|
740 | }
|
---|
741 |
|
---|
742 | *start = cur;
|
---|
743 |
|
---|
744 | return MRB_DUMP_OK;
|
---|
745 | }
|
---|
746 |
|
---|
747 | static size_t
|
---|
748 | get_lv_record_size(mrb_state *mrb, mrb_irep *irep)
|
---|
749 | {
|
---|
750 | size_t ret = 0, i;
|
---|
751 |
|
---|
752 | ret += (sizeof(uint16_t) + sizeof(uint16_t)) * (irep->nlocals - 1);
|
---|
753 |
|
---|
754 | for (i = 0; i < irep->rlen; ++i) {
|
---|
755 | ret += get_lv_record_size(mrb, irep->reps[i]);
|
---|
756 | }
|
---|
757 |
|
---|
758 | return ret;
|
---|
759 | }
|
---|
760 |
|
---|
761 | static size_t
|
---|
762 | get_lv_section_size(mrb_state *mrb, mrb_irep *irep, mrb_sym const *syms, uint32_t syms_len)
|
---|
763 | {
|
---|
764 | size_t ret = 0, i;
|
---|
765 |
|
---|
766 | ret += sizeof(uint32_t); /* syms_len */
|
---|
767 | ret += sizeof(uint16_t) * syms_len; /* symbol name lengths */
|
---|
768 | for (i = 0; i < syms_len; ++i) {
|
---|
769 | mrb_int str_len;
|
---|
770 | mrb_sym2name_len(mrb, syms[i], &str_len);
|
---|
771 | ret += str_len;
|
---|
772 | }
|
---|
773 |
|
---|
774 | ret += get_lv_record_size(mrb, irep);
|
---|
775 |
|
---|
776 | return ret;
|
---|
777 | }
|
---|
778 |
|
---|
779 | static int
|
---|
780 | write_section_lv(mrb_state *mrb, mrb_irep *irep, uint8_t *start, mrb_sym const *syms, uint32_t const syms_len)
|
---|
781 | {
|
---|
782 | uint8_t *cur = start;
|
---|
783 | struct rite_section_lv_header *header;
|
---|
784 | ptrdiff_t diff;
|
---|
785 | int result = MRB_DUMP_OK;
|
---|
786 |
|
---|
787 | if (mrb == NULL || cur == NULL) {
|
---|
788 | return MRB_DUMP_INVALID_ARGUMENT;
|
---|
789 | }
|
---|
790 |
|
---|
791 | header = (struct rite_section_lv_header*)cur;
|
---|
792 | cur += sizeof(struct rite_section_lv_header);
|
---|
793 |
|
---|
794 | result = write_lv_sym_table(mrb, &cur, syms, syms_len);
|
---|
795 | if (result != MRB_DUMP_OK) {
|
---|
796 | goto lv_section_exit;
|
---|
797 | }
|
---|
798 |
|
---|
799 | result = write_lv_record(mrb, irep, &cur, syms, syms_len);
|
---|
800 | if (result != MRB_DUMP_OK) {
|
---|
801 | goto lv_section_exit;
|
---|
802 | }
|
---|
803 |
|
---|
804 | memcpy(header->section_ident, RITE_SECTION_LV_IDENT, sizeof(header->section_ident));
|
---|
805 |
|
---|
806 | diff = cur - start;
|
---|
807 | mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
|
---|
808 | uint32_to_bin(diff, header->section_size);
|
---|
809 |
|
---|
810 | lv_section_exit:
|
---|
811 | return result;
|
---|
812 | }
|
---|
813 |
|
---|
814 | static int
|
---|
815 | write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin, uint8_t flags)
|
---|
816 | {
|
---|
817 | struct rite_binary_header *header = (struct rite_binary_header *)bin;
|
---|
818 | uint16_t crc;
|
---|
819 | uint32_t offset;
|
---|
820 |
|
---|
821 | switch (flags & DUMP_ENDIAN_NAT) {
|
---|
822 | endian_big:
|
---|
823 | case DUMP_ENDIAN_BIG:
|
---|
824 | memcpy(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident));
|
---|
825 | break;
|
---|
826 | endian_little:
|
---|
827 | case DUMP_ENDIAN_LIL:
|
---|
828 | memcpy(header->binary_ident, RITE_BINARY_IDENT_LIL, sizeof(header->binary_ident));
|
---|
829 | break;
|
---|
830 |
|
---|
831 | case DUMP_ENDIAN_NAT:
|
---|
832 | if (bigendian_p()) goto endian_big;
|
---|
833 | goto endian_little;
|
---|
834 | break;
|
---|
835 | }
|
---|
836 |
|
---|
837 | memcpy(header->binary_version, RITE_BINARY_FORMAT_VER, sizeof(header->binary_version));
|
---|
838 | memcpy(header->compiler_name, RITE_COMPILER_NAME, sizeof(header->compiler_name));
|
---|
839 | memcpy(header->compiler_version, RITE_COMPILER_VERSION, sizeof(header->compiler_version));
|
---|
840 | mrb_assert(binary_size <= UINT32_MAX);
|
---|
841 | uint32_to_bin((uint32_t)binary_size, header->binary_size);
|
---|
842 |
|
---|
843 | offset = (&(header->binary_crc[0]) - bin) + sizeof(uint16_t);
|
---|
844 | crc = calc_crc_16_ccitt(bin + offset, binary_size - offset, 0);
|
---|
845 | uint16_to_bin(crc, header->binary_crc);
|
---|
846 |
|
---|
847 | return MRB_DUMP_OK;
|
---|
848 | }
|
---|
849 |
|
---|
850 | static mrb_bool
|
---|
851 | is_debug_info_defined(mrb_irep *irep)
|
---|
852 | {
|
---|
853 | size_t i;
|
---|
854 |
|
---|
855 | if (!irep->debug_info) return FALSE;
|
---|
856 | for (i=0; i<irep->rlen; i++) {
|
---|
857 | if (!is_debug_info_defined(irep->reps[i])) return FALSE;
|
---|
858 | }
|
---|
859 | return TRUE;
|
---|
860 | }
|
---|
861 |
|
---|
862 | static mrb_bool
|
---|
863 | is_lv_defined(mrb_irep *irep)
|
---|
864 | {
|
---|
865 | size_t i;
|
---|
866 |
|
---|
867 | if (irep->lv) { return TRUE; }
|
---|
868 |
|
---|
869 | for (i = 0; i < irep->rlen; ++i) {
|
---|
870 | if (is_lv_defined(irep->reps[i])) { return TRUE; }
|
---|
871 | }
|
---|
872 |
|
---|
873 | return FALSE;
|
---|
874 | }
|
---|
875 |
|
---|
876 | static uint8_t
|
---|
877 | dump_flags(uint8_t flags, uint8_t native)
|
---|
878 | {
|
---|
879 | if (native == FLAG_BYTEORDER_NATIVE) {
|
---|
880 | if ((flags & DUMP_ENDIAN_NAT) == 0) {
|
---|
881 | return (flags & DUMP_DEBUG_INFO) | DUMP_ENDIAN_NAT;
|
---|
882 | }
|
---|
883 | return flags;
|
---|
884 | }
|
---|
885 | if ((flags & DUMP_ENDIAN_NAT) == 0) {
|
---|
886 | return (flags & DUMP_DEBUG_INFO) | DUMP_ENDIAN_BIG;
|
---|
887 | }
|
---|
888 | return flags;
|
---|
889 | }
|
---|
890 |
|
---|
891 | static int
|
---|
892 | dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
|
---|
893 | {
|
---|
894 | int result = MRB_DUMP_GENERAL_FAILURE;
|
---|
895 | size_t malloc_size;
|
---|
896 | size_t section_irep_size;
|
---|
897 | size_t section_lineno_size = 0, section_lv_size = 0;
|
---|
898 | uint8_t *cur = NULL;
|
---|
899 | mrb_bool const debug_info_defined = is_debug_info_defined(irep), lv_defined = is_lv_defined(irep);
|
---|
900 | mrb_sym *lv_syms = NULL; uint32_t lv_syms_len = 0;
|
---|
901 | mrb_sym *filenames = NULL; uint16_t filenames_len = 0;
|
---|
902 |
|
---|
903 | if (mrb == NULL) {
|
---|
904 | *bin = NULL;
|
---|
905 | return MRB_DUMP_GENERAL_FAILURE;
|
---|
906 | }
|
---|
907 |
|
---|
908 | section_irep_size = sizeof(struct rite_section_irep_header);
|
---|
909 | section_irep_size += get_irep_record_size(mrb, irep);
|
---|
910 |
|
---|
911 | /* DEBUG section size */
|
---|
912 | if (flags & DUMP_DEBUG_INFO) {
|
---|
913 | if (debug_info_defined) {
|
---|
914 | section_lineno_size += sizeof(struct rite_section_debug_header);
|
---|
915 | /* filename table */
|
---|
916 | filenames = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) + 1);
|
---|
917 |
|
---|
918 | /* filename table size */
|
---|
919 | section_lineno_size += sizeof(uint16_t);
|
---|
920 | section_lineno_size += get_filename_table_size(mrb, irep, &filenames, &filenames_len);
|
---|
921 |
|
---|
922 | section_lineno_size += get_debug_record_size(mrb, irep);
|
---|
923 | }
|
---|
924 | else {
|
---|
925 | section_lineno_size += sizeof(struct rite_section_lineno_header);
|
---|
926 | section_lineno_size += get_lineno_record_size(mrb, irep);
|
---|
927 | }
|
---|
928 | }
|
---|
929 |
|
---|
930 | if (lv_defined) {
|
---|
931 | section_lv_size += sizeof(struct rite_section_lv_header);
|
---|
932 | create_lv_sym_table(mrb, irep, &lv_syms, &lv_syms_len);
|
---|
933 | section_lv_size += get_lv_section_size(mrb, irep, lv_syms, lv_syms_len);
|
---|
934 | }
|
---|
935 |
|
---|
936 | malloc_size = sizeof(struct rite_binary_header) +
|
---|
937 | section_irep_size + section_lineno_size + section_lv_size +
|
---|
938 | sizeof(struct rite_binary_footer);
|
---|
939 | cur = *bin = (uint8_t*)mrb_malloc(mrb, malloc_size);
|
---|
940 | cur += sizeof(struct rite_binary_header);
|
---|
941 |
|
---|
942 | result = write_section_irep(mrb, irep, cur, §ion_irep_size, flags);
|
---|
943 | if (result != MRB_DUMP_OK) {
|
---|
944 | goto error_exit;
|
---|
945 | }
|
---|
946 | cur += section_irep_size;
|
---|
947 | *bin_size = sizeof(struct rite_binary_header) +
|
---|
948 | section_irep_size + section_lineno_size + section_lv_size +
|
---|
949 | sizeof(struct rite_binary_footer);
|
---|
950 |
|
---|
951 | /* write DEBUG section */
|
---|
952 | if (flags & DUMP_DEBUG_INFO) {
|
---|
953 | if (debug_info_defined) {
|
---|
954 | result = write_section_debug(mrb, irep, cur, filenames, filenames_len);
|
---|
955 | }
|
---|
956 | else {
|
---|
957 | result = write_section_lineno(mrb, irep, cur);
|
---|
958 | }
|
---|
959 | if (result != MRB_DUMP_OK) {
|
---|
960 | goto error_exit;
|
---|
961 | }
|
---|
962 | cur += section_lineno_size;
|
---|
963 | }
|
---|
964 |
|
---|
965 | if (lv_defined) {
|
---|
966 | result = write_section_lv(mrb, irep, cur, lv_syms, lv_syms_len);
|
---|
967 | if (result != MRB_DUMP_OK) {
|
---|
968 | goto error_exit;
|
---|
969 | }
|
---|
970 | cur += section_lv_size;
|
---|
971 | }
|
---|
972 |
|
---|
973 | write_footer(mrb, cur);
|
---|
974 | write_rite_binary_header(mrb, *bin_size, *bin, flags);
|
---|
975 |
|
---|
976 | error_exit:
|
---|
977 | if (result != MRB_DUMP_OK) {
|
---|
978 | mrb_free(mrb, *bin);
|
---|
979 | *bin = NULL;
|
---|
980 | }
|
---|
981 | mrb_free(mrb, lv_syms);
|
---|
982 | mrb_free(mrb, filenames);
|
---|
983 | return result;
|
---|
984 | }
|
---|
985 |
|
---|
986 | int
|
---|
987 | mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
|
---|
988 | {
|
---|
989 | return dump_irep(mrb, irep, dump_flags(flags, FLAG_BYTEORDER_NONATIVE), bin, bin_size);
|
---|
990 | }
|
---|
991 |
|
---|
992 | #ifndef MRB_DISABLE_STDIO
|
---|
993 |
|
---|
994 | int
|
---|
995 | mrb_dump_irep_binary(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE* fp)
|
---|
996 | {
|
---|
997 | uint8_t *bin = NULL;
|
---|
998 | size_t bin_size = 0;
|
---|
999 | int result;
|
---|
1000 |
|
---|
1001 | if (fp == NULL) {
|
---|
1002 | return MRB_DUMP_INVALID_ARGUMENT;
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | result = dump_irep(mrb, irep, dump_flags(flags, FLAG_BYTEORDER_NONATIVE), &bin, &bin_size);
|
---|
1006 | if (result == MRB_DUMP_OK) {
|
---|
1007 | if (fwrite(bin, sizeof(bin[0]), bin_size, fp) != bin_size) {
|
---|
1008 | result = MRB_DUMP_WRITE_FAULT;
|
---|
1009 | }
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | mrb_free(mrb, bin);
|
---|
1013 | return result;
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | static mrb_bool
|
---|
1017 | dump_bigendian_p(uint8_t flags)
|
---|
1018 | {
|
---|
1019 | switch (flags & DUMP_ENDIAN_NAT) {
|
---|
1020 | case DUMP_ENDIAN_BIG:
|
---|
1021 | return TRUE;
|
---|
1022 | case DUMP_ENDIAN_LIL:
|
---|
1023 | return FALSE;
|
---|
1024 | default:
|
---|
1025 | case DUMP_ENDIAN_NAT:
|
---|
1026 | return bigendian_p();
|
---|
1027 | }
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | int
|
---|
1031 | mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE *fp, const char *initname)
|
---|
1032 | {
|
---|
1033 | uint8_t *bin = NULL;
|
---|
1034 | size_t bin_size = 0, bin_idx = 0;
|
---|
1035 | int result;
|
---|
1036 |
|
---|
1037 | if (fp == NULL || initname == NULL || initname[0] == '\0') {
|
---|
1038 | return MRB_DUMP_INVALID_ARGUMENT;
|
---|
1039 | }
|
---|
1040 | flags = dump_flags(flags, FLAG_BYTEORDER_NATIVE);
|
---|
1041 | result = dump_irep(mrb, irep, flags, &bin, &bin_size);
|
---|
1042 | if (result == MRB_DUMP_OK) {
|
---|
1043 | if (!dump_bigendian_p(flags)) {
|
---|
1044 | if (fprintf(fp, "/* dumped in little endian order.\n"
|
---|
1045 | " use `mrbc -E` option for big endian CPU. */\n") < 0) {
|
---|
1046 | mrb_free(mrb, bin);
|
---|
1047 | return MRB_DUMP_WRITE_FAULT;
|
---|
1048 | }
|
---|
1049 | }
|
---|
1050 | else {
|
---|
1051 | if (fprintf(fp, "/* dumped in big endian order.\n"
|
---|
1052 | " use `mrbc -e` option for better performance on little endian CPU. */\n") < 0) {
|
---|
1053 | mrb_free(mrb, bin);
|
---|
1054 | return MRB_DUMP_WRITE_FAULT;
|
---|
1055 | }
|
---|
1056 | }
|
---|
1057 | if (fprintf(fp, "#include <stdint.h>\n") < 0) { /* for uint8_t under at least Darwin */
|
---|
1058 | mrb_free(mrb, bin);
|
---|
1059 | return MRB_DUMP_WRITE_FAULT;
|
---|
1060 | }
|
---|
1061 | if (fprintf(fp,
|
---|
1062 | "const uint8_t\n"
|
---|
1063 | "#if defined __GNUC__\n"
|
---|
1064 | "__attribute__((aligned(%u)))\n"
|
---|
1065 | "#elif defined _MSC_VER\n"
|
---|
1066 | "__declspec(align(%u))\n"
|
---|
1067 | "#endif\n"
|
---|
1068 | "%s[] = {",
|
---|
1069 | (uint16_t)MRB_DUMP_ALIGNMENT, (uint16_t)MRB_DUMP_ALIGNMENT, initname) < 0) {
|
---|
1070 | mrb_free(mrb, bin);
|
---|
1071 | return MRB_DUMP_WRITE_FAULT;
|
---|
1072 | }
|
---|
1073 | while (bin_idx < bin_size) {
|
---|
1074 | if (bin_idx % 16 == 0) {
|
---|
1075 | if (fputs("\n", fp) == EOF) {
|
---|
1076 | mrb_free(mrb, bin);
|
---|
1077 | return MRB_DUMP_WRITE_FAULT;
|
---|
1078 | }
|
---|
1079 | }
|
---|
1080 | if (fprintf(fp, "0x%02x,", bin[bin_idx++]) < 0) {
|
---|
1081 | mrb_free(mrb, bin);
|
---|
1082 | return MRB_DUMP_WRITE_FAULT;
|
---|
1083 | }
|
---|
1084 | }
|
---|
1085 | if (fputs("\n};\n", fp) == EOF) {
|
---|
1086 | mrb_free(mrb, bin);
|
---|
1087 | return MRB_DUMP_WRITE_FAULT;
|
---|
1088 | }
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | mrb_free(mrb, bin);
|
---|
1092 | return result;
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 | #endif /* MRB_DISABLE_STDIO */
|
---|