diff options
Diffstat (limited to 'src/bytecode')
-rw-r--r-- | src/bytecode/bytecode.c | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/bytecode/bytecode.c b/src/bytecode/bytecode.c index ffcd2e0..d670407 100644 --- a/src/bytecode/bytecode.c +++ b/src/bytecode/bytecode.c @@ -59,6 +59,20 @@ static CHECK_RESULT usize ssa_extract_jump(u8 *instruction_data, SsaInsJump *res } static void add_intermediates(BytecodeCompilerContext *self) { + /*doc(Bytecode intermediates) + # Intermediates layout + |Type |Field |Description | + |------------|------------------|-------------------------------------------------------------------------------| + |u32 |Intermediates size|The size of the intermediates section, in bytes. | + |Intermediate|Intermediate data |Multiple intermediates, where the total size is defined by @Intermediates size.| + + # Intermediate + |Type|Field|Description | + |----|-----|----------------------------------------------------| + |u8 |Type |The type of the number. 0=integer, 1=float. | + |u64 |Value|The type of the value depends on the value of @Type.| + */ + Ssa *ssa; Buffer *instructions; SsaNumber *intermediate; @@ -81,6 +95,21 @@ static void add_intermediates(BytecodeCompilerContext *self) { } void add_strings(BytecodeCompilerContext *self) { + /*doc(Bytecode strings) + # Strings layout + |Type |Field |Description | + |-------|-----------------|------------------------------------------------------------------| + |u16 |Number of strings|The number of strings. | + |u32 |Strings size |The size of the strings section, in bytes. | + |String*|Strings data |Multiple strings, where the total size is defined by @Strings size| + + # String + |Type|Field|Description | + |----|----|----------------------------------------------------------------------------------------| + |u16 |Size|The size of the string, in bytes. | + |u8* |Data|The data of the string, where the size is defined by @Size. Strings are null-terminated.| + */ + Ssa *ssa; Buffer *instructions; BufferView *string; @@ -100,7 +129,7 @@ void add_strings(BytecodeCompilerContext *self) { strings_size = 0; for(; string != strings_end; ++string) { - strings_size += sizeof(u16) + string->size; + strings_size += sizeof(u16) + string->size + 1; /* +1 for null-termination of string */ } string = buffer_begin(&ssa->strings); @@ -108,8 +137,10 @@ void add_strings(BytecodeCompilerContext *self) { throw_if_error(buffer_append(instructions, &num_strings, sizeof(u16))); throw_if_error(buffer_append(instructions, &strings_size, sizeof(u32))); for(; string != strings_end; ++string) { + char null_s = '\0'; throw_if_error(buffer_append(instructions, &string->size, sizeof(u16))); throw_if_error(buffer_append(instructions, string->data, string->size)); + throw_if_error(buffer_append(instructions, &null_s, sizeof(char))); } } @@ -223,6 +254,14 @@ static const char* lhs_expr_get_c_name(BytecodeCompilerContext *self, LhsExpr *l #endif static void add_instructions(BytecodeCompilerContext *self) { + /*doc(Bytecode instructions) + # Instructions layout + |Type |Field |Description | + |------------|-----------------|---------------------------------------------------------------------------| + |u32 |Instructions size|The size of the instructions section, in bytes. | + |Instruction*|Instructions data|The instructions data. Each instructions begins with an opcode, see #Opcode| + */ + Ssa *ssa; u8 *instruction; u8 *instructions_end; @@ -486,7 +525,8 @@ static void add_instructions(BytecodeCompilerContext *self) { /* Prepend instructions with its size */ { u32 instructions_size; - instructions_size = self->bytecode.data.size - num_instructions_index - sizeof(instructions_size); /* Remove the count itself from the size of the instructions size */ + /* -sizeof to Remove the count itself from the size of the instructions size */ + instructions_size = self->bytecode.data.size - num_instructions_index - sizeof(instructions_size); am_memcpy(&self->bytecode.data.data[num_instructions_index], &instructions_size, sizeof(instructions_size)); } |