mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Remove endian information/flags from compiled binary format.
Since `mruby 2.0`, compiled bytecode no longer depends on the endian of the machine.
This commit is contained in:
@@ -17,10 +17,6 @@
|
|||||||
MRB_BEGIN_DECL
|
MRB_BEGIN_DECL
|
||||||
|
|
||||||
#define DUMP_DEBUG_INFO 1
|
#define DUMP_DEBUG_INFO 1
|
||||||
#define DUMP_ENDIAN_BIG 2
|
|
||||||
#define DUMP_ENDIAN_LIL 4
|
|
||||||
#define DUMP_ENDIAN_NAT 6
|
|
||||||
#define DUMP_ENDIAN_MASK 6
|
|
||||||
|
|
||||||
int mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size);
|
int mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size);
|
||||||
#ifndef MRB_DISABLE_STDIO
|
#ifndef MRB_DISABLE_STDIO
|
||||||
@@ -52,7 +48,6 @@ MRB_API mrb_irep *mrb_read_irep_buf(mrb_state*, const void*, size_t);
|
|||||||
|
|
||||||
/* Rite Binary File header */
|
/* Rite Binary File header */
|
||||||
#define RITE_BINARY_IDENT "RITE"
|
#define RITE_BINARY_IDENT "RITE"
|
||||||
#define RITE_BINARY_IDENT_LIL "ETIR"
|
|
||||||
#define RITE_BINARY_FORMAT_VER "0007"
|
#define RITE_BINARY_FORMAT_VER "0007"
|
||||||
#define RITE_COMPILER_NAME "MATZ"
|
#define RITE_COMPILER_NAME "MATZ"
|
||||||
#define RITE_COMPILER_VERSION "0000"
|
#define RITE_COMPILER_VERSION "0000"
|
||||||
@@ -106,17 +101,6 @@ struct rite_binary_footer {
|
|||||||
RITE_SECTION_HEADER;
|
RITE_SECTION_HEADER;
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline int
|
|
||||||
bigendian_p()
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char *p;
|
|
||||||
|
|
||||||
i = 1;
|
|
||||||
p = (char*)&i;
|
|
||||||
return p[0]?0:1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline size_t
|
static inline size_t
|
||||||
uint8_to_bin(uint8_t s, uint8_t *bin)
|
uint8_to_bin(uint8_t s, uint8_t *bin)
|
||||||
{
|
{
|
||||||
|
|||||||
+3
-62
@@ -709,22 +709,7 @@ write_rite_binary_header(mrb_state *mrb, size_t binary_size, uint8_t *bin, uint8
|
|||||||
uint16_t crc;
|
uint16_t crc;
|
||||||
uint32_t offset;
|
uint32_t offset;
|
||||||
|
|
||||||
switch (flags & DUMP_ENDIAN_NAT) {
|
memcpy(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident));
|
||||||
endian_big:
|
|
||||||
case DUMP_ENDIAN_BIG:
|
|
||||||
memcpy(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident));
|
|
||||||
break;
|
|
||||||
endian_little:
|
|
||||||
case DUMP_ENDIAN_LIL:
|
|
||||||
memcpy(header->binary_ident, RITE_BINARY_IDENT_LIL, sizeof(header->binary_ident));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case DUMP_ENDIAN_NAT:
|
|
||||||
if (bigendian_p()) goto endian_big;
|
|
||||||
goto endian_little;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(header->binary_version, RITE_BINARY_FORMAT_VER, sizeof(header->binary_version));
|
memcpy(header->binary_version, RITE_BINARY_FORMAT_VER, sizeof(header->binary_version));
|
||||||
memcpy(header->compiler_name, RITE_COMPILER_NAME, sizeof(header->compiler_name));
|
memcpy(header->compiler_name, RITE_COMPILER_NAME, sizeof(header->compiler_name));
|
||||||
memcpy(header->compiler_version, RITE_COMPILER_VERSION, sizeof(header->compiler_version));
|
memcpy(header->compiler_version, RITE_COMPILER_VERSION, sizeof(header->compiler_version));
|
||||||
@@ -764,21 +749,6 @@ lv_defined_p(mrb_irep *irep)
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t
|
|
||||||
dump_flags(uint8_t flags, uint8_t native)
|
|
||||||
{
|
|
||||||
if (native == FLAG_BYTEORDER_NATIVE) {
|
|
||||||
if ((flags & DUMP_ENDIAN_NAT) == 0) {
|
|
||||||
return (flags & DUMP_DEBUG_INFO) | DUMP_ENDIAN_NAT;
|
|
||||||
}
|
|
||||||
return flags;
|
|
||||||
}
|
|
||||||
if ((flags & DUMP_ENDIAN_NAT) == 0) {
|
|
||||||
return (flags & DUMP_DEBUG_INFO) | DUMP_ENDIAN_BIG;
|
|
||||||
}
|
|
||||||
return flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
|
dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
|
||||||
{
|
{
|
||||||
@@ -870,7 +840,7 @@ error_exit:
|
|||||||
int
|
int
|
||||||
mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
|
mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *bin_size)
|
||||||
{
|
{
|
||||||
return dump_irep(mrb, irep, dump_flags(flags, FLAG_BYTEORDER_NONATIVE), bin, bin_size);
|
return dump_irep(mrb, irep, flags, bin, bin_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef MRB_DISABLE_STDIO
|
#ifndef MRB_DISABLE_STDIO
|
||||||
@@ -886,7 +856,7 @@ mrb_dump_irep_binary(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE* fp)
|
|||||||
return MRB_DUMP_INVALID_ARGUMENT;
|
return MRB_DUMP_INVALID_ARGUMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = dump_irep(mrb, irep, dump_flags(flags, FLAG_BYTEORDER_NONATIVE), &bin, &bin_size);
|
result = dump_irep(mrb, irep, flags, &bin, &bin_size);
|
||||||
if (result == MRB_DUMP_OK) {
|
if (result == MRB_DUMP_OK) {
|
||||||
if (fwrite(bin, sizeof(bin[0]), bin_size, fp) != bin_size) {
|
if (fwrite(bin, sizeof(bin[0]), bin_size, fp) != bin_size) {
|
||||||
result = MRB_DUMP_WRITE_FAULT;
|
result = MRB_DUMP_WRITE_FAULT;
|
||||||
@@ -897,20 +867,6 @@ mrb_dump_irep_binary(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE* fp)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static mrb_bool
|
|
||||||
dump_bigendian_p(uint8_t flags)
|
|
||||||
{
|
|
||||||
switch (flags & DUMP_ENDIAN_NAT) {
|
|
||||||
case DUMP_ENDIAN_BIG:
|
|
||||||
return TRUE;
|
|
||||||
case DUMP_ENDIAN_LIL:
|
|
||||||
return FALSE;
|
|
||||||
default:
|
|
||||||
case DUMP_ENDIAN_NAT:
|
|
||||||
return bigendian_p();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE *fp, const char *initname)
|
mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE *fp, const char *initname)
|
||||||
{
|
{
|
||||||
@@ -921,23 +877,8 @@ mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep *irep, uint8_t flags, FILE *fp, con
|
|||||||
if (fp == NULL || initname == NULL || initname[0] == '\0') {
|
if (fp == NULL || initname == NULL || initname[0] == '\0') {
|
||||||
return MRB_DUMP_INVALID_ARGUMENT;
|
return MRB_DUMP_INVALID_ARGUMENT;
|
||||||
}
|
}
|
||||||
flags = dump_flags(flags, FLAG_BYTEORDER_NATIVE);
|
|
||||||
result = dump_irep(mrb, irep, flags, &bin, &bin_size);
|
result = dump_irep(mrb, irep, flags, &bin, &bin_size);
|
||||||
if (result == MRB_DUMP_OK) {
|
if (result == MRB_DUMP_OK) {
|
||||||
if (!dump_bigendian_p(flags)) {
|
|
||||||
if (fprintf(fp, "/* dumped in little endian order.\n"
|
|
||||||
" use `mrbc -E` option for big endian CPU. */\n") < 0) {
|
|
||||||
mrb_free(mrb, bin);
|
|
||||||
return MRB_DUMP_WRITE_FAULT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (fprintf(fp, "/* dumped in big endian order.\n"
|
|
||||||
" use `mrbc -e` option for better performance on little endian CPU. */\n") < 0) {
|
|
||||||
mrb_free(mrb, bin);
|
|
||||||
return MRB_DUMP_WRITE_FAULT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (fprintf(fp, "#include <stdint.h>\n") < 0) { /* for uint8_t under at least Darwin */
|
if (fprintf(fp, "#include <stdint.h>\n") < 0) { /* for uint8_t under at least Darwin */
|
||||||
mrb_free(mrb, bin);
|
mrb_free(mrb, bin);
|
||||||
return MRB_DUMP_WRITE_FAULT;
|
return MRB_DUMP_WRITE_FAULT;
|
||||||
|
|||||||
+2
-18
@@ -19,9 +19,6 @@
|
|||||||
# error size_t must be at least 32 bits wide
|
# error size_t must be at least 32 bits wide
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define FLAG_BYTEORDER_BIG 2
|
|
||||||
#define FLAG_BYTEORDER_LIL 4
|
|
||||||
#define FLAG_BYTEORDER_NATIVE 8
|
|
||||||
#define FLAG_SRC_MALLOC 1
|
#define FLAG_SRC_MALLOC 1
|
||||||
#define FLAG_SRC_STATIC 0
|
#define FLAG_SRC_STATIC 0
|
||||||
|
|
||||||
@@ -94,8 +91,7 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag
|
|||||||
if (SIZE_ERROR_MUL(irep->ilen, sizeof(mrb_code))) {
|
if (SIZE_ERROR_MUL(irep->ilen, sizeof(mrb_code))) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if ((flags & FLAG_SRC_MALLOC) == 0 &&
|
if ((flags & FLAG_SRC_MALLOC) == 0) {
|
||||||
(flags & FLAG_BYTEORDER_NATIVE)) {
|
|
||||||
irep->iseq = (mrb_code*)src;
|
irep->iseq = (mrb_code*)src;
|
||||||
src += sizeof(mrb_code) * irep->ilen;
|
src += sizeof(mrb_code) * irep->ilen;
|
||||||
irep->flags |= MRB_ISEQ_NO_FREE;
|
irep->flags |= MRB_ISEQ_NO_FREE;
|
||||||
@@ -464,19 +460,7 @@ read_binary_header(const uint8_t *bin, size_t bufsize, size_t *bin_size, uint16_
|
|||||||
return MRB_DUMP_READ_FAULT;
|
return MRB_DUMP_READ_FAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (memcmp(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident)) == 0) {
|
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;
|
|
||||||
}
|
|
||||||
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 {
|
|
||||||
return MRB_DUMP_INVALID_FILE_HEADER;
|
return MRB_DUMP_INVALID_FILE_HEADER;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user