From: Brendan Hansen Date: Sun, 14 Jun 2020 01:16:18 +0000 (-0500) Subject: Starting to output a binary for real. X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=277a7ae7cb9fcdbd0583cc0a3742028a8a047ba1;p=onyx.git Starting to output a binary for real. Added LEB128 encodings --- diff --git a/.gitignore b/.gitignore index f9efa124..8ca5336c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ tags test.c test +*.wasm diff --git a/include/bh.h b/include/bh.h index 9424690c..a0a33c90 100644 --- a/include/bh.h +++ b/include/bh.h @@ -101,6 +101,20 @@ inline i64 chars_match(char* ptr1, char* ptr2) { + + +//------------------------------------------------------------------------------------- +// Conversion functions +//------------------------------------------------------------------------------------- + +// Converts an unsigned integer to the unsigned LEB128 format +u8* uint_to_uleb128(u64 n, i32* output_length); +u8* int_to_leb128(i64 n, i32* output_length); + + + + + //------------------------------------------------------------------------------------- // Helpful macros //------------------------------------------------------------------------------------- @@ -414,7 +428,7 @@ void* bh__debug_realloc(void* ptr, size_t size, const char* file, u64 line) { typedef struct bh_buffer { bh_allocator allocator; i32 length, capacity; - ptr data; + u8* data; } bh_buffer; #ifndef BH_BUFFER_GROW_FORMULA @@ -423,8 +437,9 @@ typedef struct bh_buffer { void bh_buffer_init(bh_buffer* buffer, bh_allocator alloc, i32 length); void bh_buffer_grow(bh_buffer* buffer, i32 length); -void bh_buffer_append(bh_buffer* buffer, ptr data, i32 length); +void bh_buffer_append(bh_buffer* buffer, const void * data, i32 length); void bh_buffer_concat(bh_buffer* buffer, bh_buffer other); +void bh_buffer_write_byte(bh_buffer* buffer, u8 byte); @@ -863,6 +878,51 @@ BH_ALLOCATOR_PROC(bh_scratch_allocator_proc) { +//------------------------------------------------------------------------------------- +// CONVERSION FUNCTIONS IMPLEMENTATION +//------------------------------------------------------------------------------------- +u8* uint_to_uleb128(u64 n, i32* output_length) { + static u8 buffer[16]; + + *output_length = 0; + u8* output = buffer; + u8 byte; + do { + byte = n & 0x7f; + n >>= 7; + if (n != 0) byte |= (1 << 7); + *output++ = byte; + (*output_length)++; + } while (n != 0); + + return buffer; +} + + +// Converts a signed integer to the signed LEB128 format +u8* int_to_leb128(i64 n, i32* output_length) { + static u8 buffer[16]; + + *output_length = 0; + u8* output = buffer; + b32 more = 1; + + i32 size = 64; + + u8 byte; + do { + byte = n & 0x7f; + n >>= 7; + + more = !(((n == 0) && (byte & 0x40) == 0) || ((n == -1) && (byte & 0x40) != 0)); + if (more) + byte |= 0x80; + *output++ = byte; + (*output_length)++; + } while (more); + + return buffer; +} @@ -1353,14 +1413,14 @@ void bh_buffer_grow(bh_buffer* buffer, i32 length) { buffer->data = new_data; } -void bh_buffer_append(bh_buffer* buffer, ptr data, i32 length) { +void bh_buffer_append(bh_buffer* buffer, const void * data, i32 length) { if (buffer == NULL) return; if (buffer->length + length > buffer->capacity) { bh_buffer_grow(buffer, buffer->length + length); } - memcpy(bh_pointer_add(buffer->data, buffer->length), data, length); + memcpy(buffer->data + buffer->length, data, length); buffer->length += length; } @@ -1368,6 +1428,10 @@ void bh_buffer_concat(bh_buffer* buffer, bh_buffer other) { bh_buffer_append(buffer, other.data, other.length); } +void bh_buffer_write_byte(bh_buffer* buffer, u8 byte) { + bh_buffer_grow(buffer, buffer->length + 1); + buffer->data[buffer->length++] = byte; +} diff --git a/include/onyxwasm.h b/include/onyxwasm.h index 7c0d6d90..17f35599 100644 --- a/include/onyxwasm.h +++ b/include/onyxwasm.h @@ -277,5 +277,6 @@ typedef struct OnyxWasmModule { OnyxWasmModule onyx_wasm_generate_module(bh_allocator alloc, OnyxAstNode* program); void onyx_wasm_module_free(OnyxWasmModule* module); +void onyx_wasm_module_write_to_file(OnyxWasmModule* module, bh_file file); #endif diff --git a/onyx b/onyx index 552631e5..ad067453 100755 Binary files a/onyx and b/onyx differ diff --git a/src/onyx.c b/src/onyx.c index 73eef378..5e4ec5b7 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -97,6 +97,10 @@ int main(int argc, char *argv[]) { bh_hash_each_end; #endif + bh_file out_file; + bh_file_create(&out_file, "out.wasm"); + onyx_wasm_module_write_to_file(&wasm_mod, out_file); + bh_file_close(&out_file); onyx_wasm_module_free(&wasm_mod); main_exit: // NOTE: Cleanup, since C doesn't have defer diff --git a/src/onyxwasm.c b/src/onyxwasm.c index a410d635..ccf87dff 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -544,3 +544,21 @@ void onyx_wasm_module_free(OnyxWasmModule* module) { bh_hash_free(module->type_map); bh_hash_free(module->exports); } + +static const u8 WASM_MAGIC_STRING[] = { 0x00, 0x61, 0x73, 0x6D }; +static const u8 WASM_VERSION[] = { 0x01, 0x00, 0x00, 0x00 }; + +void onyx_wasm_module_write_to_file(OnyxWasmModule* module, bh_file file) { + bh_buffer master_buffer; + bh_buffer_init(&master_buffer, bh_heap_allocator(), 128); + bh_buffer_append(&master_buffer, WASM_MAGIC_STRING, 4); + bh_buffer_append(&master_buffer, WASM_VERSION, 4); + + + + + + + + bh_file_write(&file, master_buffer.data, master_buffer.length); +}