Starting to output a binary for real.
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 14 Jun 2020 01:16:18 +0000 (20:16 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 14 Jun 2020 01:16:18 +0000 (20:16 -0500)
Added LEB128 encodings

.gitignore
include/bh.h
include/onyxwasm.h
onyx
src/onyx.c
src/onyxwasm.c

index f9efa12457d7dde5851ab02a687f83ceaca1bb93..8ca5336ca22e411d67de8e46a8ca5439f8eefc8c 100644 (file)
@@ -3,3 +3,4 @@
 tags
 test.c
 test
+*.wasm
index 9424690c27618da2ba4d02230a856a456f8d5a1f..a0a33c908978a028be6e60024e6edbbbc993fcb0 100644 (file)
@@ -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;
+}
 
 
 
index 7c0d6d90f01d5f1016997393324e1f3b2f9c9c0e..17f35599b359e0d5cbb2afa0b704a07c99799e19 100644 (file)
@@ -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 552631e52fb05489e67e55946bd09f677b6a05eb..ad0674531f06f9a329c80daa897e65112079d6a3 100755 (executable)
Binary files a/onyx and b/onyx differ
index 73eef378c977e16739ac7bc982d85d35699f358f..5e4ec5b79c3017a4853364210187b103247fe28e 100644 (file)
@@ -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
index a410d635f7b58826064f2508814757c8301e9a0e..ccf87dffb0ec594c1d48cba37adaea6fd37f9b0f 100644 (file)
@@ -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);
+}