From 2928e5f74983f5dd33bc65f192298af87996a037 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 14 Sep 2019 22:45:46 +0200 Subject: Less endian code --- include/bytecode/bytecode.h | 6 +++--- src/bytecode/bytecode.c | 9 +++------ src/program.c | 4 ++-- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/include/bytecode/bytecode.h b/include/bytecode/bytecode.h index 83d62b9..346455c 100644 --- a/include/bytecode/bytecode.h +++ b/include/bytecode/bytecode.h @@ -64,7 +64,8 @@ typedef enum { AMAL_OP_LABEL /* label - Label. This is the target of a jump instruction. Jump instructions only jump to labels in the same function scope */ } AmalOpcode; -#define AMAL_BYTECODE_MAGIC_NUMBER (u32)0xdec05eba +#define AMAL_BYTECODE_MAGIC_NUMBER "\xde\xc0\x5e\xba" +#define AMAL_BYTECODE_MAGIC_NUMBER_SIZE 4 #define AMAL_BYTECODE_MAJOR_VERSION 1 #define AMAL_BYTECODE_MINOR_VERSION 0 #define AMAL_BYTECODE_PATCH_VERSION 0 @@ -83,11 +84,10 @@ typedef u8 AmalOpcodeType; /* TODO: Make sure this pragma pack works on all platforms */ #pragma pack(push, 1) typedef struct { - u32 magic_number; /* AMAL_BYTECODE_MAGIC_NUMBER */ + u8 magic_number[AMAL_BYTECODE_MAGIC_NUMBER_SIZE]; /* @AMAL_BYTECODE_MAGIC_NUMBER */ u8 major_version; u8 minor_version; u8 patch_version; - u8 endian; /* 0 = little endian, 1 = big endian */ } BytecodeHeader; #pragma pack(pop) diff --git a/src/bytecode/bytecode.c b/src/bytecode/bytecode.c index 0a2b157..3b6d980 100644 --- a/src/bytecode/bytecode.c +++ b/src/bytecode/bytecode.c @@ -48,15 +48,12 @@ CHECK_RESULT int buffer_append_header(Buffer *program_data) { */ BytecodeHeader header; - header.magic_number = AMAL_BYTECODE_MAGIC_NUMBER; + am_memcpy(header.magic_number, AMAL_BYTECODE_MAGIC_NUMBER, AMAL_BYTECODE_MAGIC_NUMBER_SIZE); header.major_version = AMAL_BYTECODE_MAJOR_VERSION; header.minor_version = AMAL_BYTECODE_MINOR_VERSION; header.patch_version = AMAL_BYTECODE_PATCH_VERSION; -#if defined(AMAL_LITTLE_ENDIAN) - header.endian = 0; -#elif defined(AMAL_BIG_ENDIAN) - header.magic_number = byteswap32(header.magic_number); - header.endian = 1; +#if defined(AMAL_BIG_ENDIAN) + #error TODO: convert bytecode to little endian #endif return_if_error(buffer_append(program_data, &header, sizeof(header))); diff --git a/src/program.c b/src/program.c index 1a53b53..63d2b6e 100644 --- a/src/program.c +++ b/src/program.c @@ -8,6 +8,7 @@ #include #include #include +#include /* One gigabyte */ #define PROGRAM_MAX_SIZE 1024*1024*1024 @@ -194,11 +195,10 @@ static CHECK_RESULT int amal_program_read_header(amal_program *self) { am_memcpy(&header, self->data.data + self->read_index, sizeof(header)); self->read_index += sizeof(header); #ifdef AMAL_BIG_ENDIAN - header.magic_number = byteswap32(header.magic_number); #error TODO: Support big endian for program decoding #endif - if(header.magic_number != AMAL_BYTECODE_MAGIC_NUMBER) + if(memcmp(header.magic_number, AMAL_BYTECODE_MAGIC_NUMBER, AMAL_BYTECODE_MAGIC_NUMBER_SIZE) != 0) return AMAL_PROGRAM_INVALID_MAGIC_NUMBER; /* -- cgit v1.2.3