From 35200031e88c65da6a0bde563f20d95c1dd4f464 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 14 Sep 2019 01:45:31 +0200 Subject: Use struct for bytecode header instead of pointer arithmetic --- src/bytecode/bytecode.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'src/bytecode') diff --git a/src/bytecode/bytecode.c b/src/bytecode/bytecode.c index c968743..0a2b157 100644 --- a/src/bytecode/bytecode.c +++ b/src/bytecode/bytecode.c @@ -42,20 +42,24 @@ CHECK_RESULT int buffer_append_header(Buffer *program_data) { |u8 |Major version|The major version of the bytecode. Updates in this is a breaking change. | |u8 |Minor version|The minor version of the bytecode. Updates in this are backwards compatible.| |u8 |Patch version|The patch version of the bytecode. Updates in this are only minor bug fixes.| + |u8 |Endian |Endian of the program. 0 = little endian, 1 = big endian. | The versions in the header only changes for every release, not every change. */ - const u32 magic_number = AMAL_BYTECODE_MAGIC_NUMBER; - const u8 major_version = AMAL_BYTECODE_MAJOR_VERSION; - const u8 minor_version = AMAL_BYTECODE_MINOR_VERSION; - const u8 patch_version = AMAL_BYTECODE_PATCH_VERSION; - - return_if_error(buffer_append(program_data, &magic_number, 4)); - return_if_error(buffer_append(program_data, &major_version, 1)); - return_if_error(buffer_append(program_data, &minor_version, 1)); - return_if_error(buffer_append(program_data, &patch_version, 1)); - + BytecodeHeader header; + header.magic_number = AMAL_BYTECODE_MAGIC_NUMBER; + 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; +#endif + + return_if_error(buffer_append(program_data, &header, sizeof(header))); return 0; } -- cgit v1.2.3