summaryrefslogtreecommitdiffhomepage
path: root/src/load.c
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2015-02-02 09:34:24 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2015-02-02 09:34:24 +0900
commitbc9c47d5180ab28e1a3db4dca80da7512aa6839a (patch)
tree7225a711463345e443d994833c13de817184f10c /src/load.c
parent089f1f6c75d1642f96c84d12c3c2f4cb567343b9 (diff)
downloadmruby-bc9c47d5180ab28e1a3db4dca80da7512aa6839a.tar.gz
mruby-bc9c47d5180ab28e1a3db4dca80da7512aa6839a.zip
allow endian specification of mrb files by `mrbc -e/-E`
`mruby -b` now accepts both big/little endian mrb (compiled binary) files. `mrbc` generates mrb files in big endian for .mrb files and in native endian for C files (with -B option specified) by default. If you are cross compiling, you need to specify target endian by -e/-E options if it is different from host endian.
Diffstat (limited to 'src/load.c')
-rw-r--r--src/load.c49
1 files changed, 27 insertions, 22 deletions
diff --git a/src/load.c b/src/load.c
index 7640d8fda..889420ae9 100644
--- a/src/load.c
+++ b/src/load.c
@@ -14,8 +14,9 @@
#include "mruby/debug.h"
#include "mruby/error.h"
-#define FLAG_BYTEORDER_NATIVE 2
-#define FLAG_BYTEORDER_NONATIVE 0
+#define FLAG_BYTEORDER_BIG 2
+#define FLAG_BYTEORDER_LIL 4
+#define FLAG_BYTEORDER_NATIVE 8
#define FLAG_SRC_MALLOC 1
#define FLAG_SRC_STATIC 0
@@ -93,12 +94,18 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag
memcpy(irep->iseq, src, sizeof(uint32_t) * irep->ilen);
src += sizeof(uint32_t) * irep->ilen;
}
- else {
+ else if (flags & FLAG_BYTEORDER_BIG) {
for (i = 0; i < irep->ilen; i++) {
irep->iseq[i] = (mrb_code)bin_to_uint32(src); /* iseq */
src += sizeof(uint32_t);
}
}
+ else {
+ for (i = 0; i < irep->ilen; i++) {
+ irep->iseq[i] = (mrb_code)bin_to_uint32l(src); /* iseq */
+ src += sizeof(uint32_t);
+ }
+ }
}
}
@@ -521,22 +528,20 @@ static int
read_binary_header(const uint8_t *bin, size_t *bin_size, uint16_t *crc, uint8_t *flags)
{
const struct rite_binary_header *header = (const struct rite_binary_header *)bin;
- uint32_t ident = 0;
- size_t i;
- /* create native byteorder version of RITE_BINARY_IDENTIFIER */
- for(i=0; i<sizeof(ident); i++) {
- ident<<=8;
- ident|=RITE_BINARY_IDENTIFIER[i];
+ if (memcmp(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident)) == 0) {
+ if (bigendian_p())
+ *flags |= FLAG_BYTEORDER_NATIVE;
+ else
+ *flags |= FLAG_BYTEORDER_BIG;
}
- if (memcmp(header->binary_identify, &ident, sizeof(header->binary_identify)) == 0) {
- *flags |= FLAG_BYTEORDER_NATIVE;
+ else if (memcmp(header->binary_ident, RITE_BINARY_IDENT_LIL, sizeof(header->binary_ident)) == 0) {
+ if (bigendian_p())
+ *flags |= FLAG_BYTEORDER_LIL;
+ else
+ *flags |= FLAG_BYTEORDER_NATIVE;
}
- else if (memcmp(header->binary_identify, RITE_BINARY_IDENTIFIER, sizeof(header->binary_identify)) != 0) {
- return MRB_DUMP_INVALID_FILE_HEADER;
- }
-
- if (memcmp(header->binary_version, RITE_BINARY_FORMAT_VER, sizeof(header->binary_version)) != 0) {
+ else {
return MRB_DUMP_INVALID_FILE_HEADER;
}
@@ -575,25 +580,25 @@ read_irep(mrb_state *mrb, const uint8_t *bin, uint8_t flags)
bin += sizeof(struct rite_binary_header);
do {
section_header = (const struct rite_section_header *)bin;
- if (memcmp(section_header->section_identify, RITE_SECTION_IREP_IDENTIFIER, sizeof(section_header->section_identify)) == 0) {
+ if (memcmp(section_header->section_ident, RITE_SECTION_IREP_IDENT, sizeof(section_header->section_ident)) == 0) {
irep = read_section_irep(mrb, bin, flags);
if (!irep) return NULL;
}
- else if (memcmp(section_header->section_identify, RITE_SECTION_LINENO_IDENTIFIER, sizeof(section_header->section_identify)) == 0) {
+ else if (memcmp(section_header->section_ident, RITE_SECTION_LINENO_IDENT, sizeof(section_header->section_ident)) == 0) {
if (!irep) return NULL; /* corrupted data */
result = read_section_lineno(mrb, bin, irep);
if (result < MRB_DUMP_OK) {
return NULL;
}
}
- else if (memcmp(section_header->section_identify, RITE_SECTION_DEBUG_IDENTIFIER, sizeof(section_header->section_identify)) == 0) {
+ else if (memcmp(section_header->section_ident, RITE_SECTION_DEBUG_IDENT, sizeof(section_header->section_ident)) == 0) {
if (!irep) return NULL; /* corrupted data */
result = read_section_debug(mrb, bin, irep, flags);
if (result < MRB_DUMP_OK) {
return NULL;
}
}
- else if (memcmp(section_header->section_identify, RITE_SECTION_LV_IDENTIFIER, sizeof(section_header->section_identify)) == 0) {
+ else if (memcmp(section_header->section_ident, RITE_SECTION_LV_IDENT, sizeof(section_header->section_ident)) == 0) {
if (!irep) return NULL;
result = read_section_lv(mrb, bin, irep, flags);
if (result < MRB_DUMP_OK) {
@@ -601,7 +606,7 @@ read_irep(mrb_state *mrb, const uint8_t *bin, uint8_t flags)
}
}
bin += bin_to_uint32(section_header->section_size);
- } while (memcmp(section_header->section_identify, RITE_BINARY_EOF, sizeof(section_header->section_identify)) != 0);
+ } while (memcmp(section_header->section_ident, RITE_BINARY_EOF, sizeof(section_header->section_ident)) != 0);
return irep;
}
@@ -655,7 +660,7 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp)
uint8_t *buf;
const size_t header_size = sizeof(struct rite_binary_header);
size_t buf_size = 0;
- uint8_t flags;
+ uint8_t flags = 0;
int result;
if ((mrb == NULL) || (fp == NULL)) {