change how numbers are stored so they are in an efficent binary format, able to be quickly read by the runtime
This commit is contained in:
@@ -8,14 +8,13 @@ SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
all opcodes are uint8_t, and all operands are uint64_t unless marked with an asterisk (*), where it is marked as uint8_t
|
||||
|
||||
## OP_LOAD_CONST
|
||||
## OP_LOAD_STRING
|
||||
|
||||
loads and initialises a value from the constant buffer into the provided register.
|
||||
loads and initialises a string from the constant buffer into the provided register.
|
||||
|
||||
this operation 4 operands.
|
||||
|
||||
1. the register to write to. (*)
|
||||
2. the type of data from the constant buffer. (*)
|
||||
3. the length of the data in the constant buffer.
|
||||
4. the offset in the constant buffer.
|
||||
|
||||
@@ -119,6 +118,16 @@ loads a boolean into register 1
|
||||
|
||||
1. byte representing true or false (1 or 0) *
|
||||
|
||||
## OP_LOAD_NUMBER
|
||||
|
||||
loads a mpq_t number into memory
|
||||
|
||||
1. the register to write to. (*)
|
||||
3. the size of the numerator in the constant buffer.
|
||||
4. the offset in the constant buffer of the numerator.
|
||||
3. the size of the denominator in the constant buffer.
|
||||
4. the offset in the constant buffer of the denominator.
|
||||
|
||||
## OP_SWAP_REGISTERS
|
||||
|
||||
swap the contents in two registers
|
||||
|
||||
@@ -4,23 +4,39 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#include "../translator.h"
|
||||
#include "number.h"
|
||||
#include "../translator.h"
|
||||
#include <gmp.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
size_t translate_parsed_number(Translated *translated, char *number_str, size_t to_register) {
|
||||
size_t length = strlen(number_str)+1;
|
||||
size_t number_pos = arena_push(&translated->constants, number_str, length);
|
||||
set_registers(translated, to_register+1);
|
||||
|
||||
size_t start = push_instruction_byte(translated, OP_LOAD_CONST);
|
||||
push_instruction_byte(translated, to_register);
|
||||
size_t translate_parsed_number(Translated *translated, mpq_t *number,
|
||||
size_t to_register) {
|
||||
mpz_t num, den;
|
||||
mpz_init(num);
|
||||
mpz_init(den);
|
||||
mpz_set(num, mpq_numref(*number));
|
||||
mpz_set(den, mpq_denref(*number));
|
||||
size_t num_size;
|
||||
void *num_data = mpz_export(NULL, &num_size, 1, 1, 0, 0, num);
|
||||
size_t numerator_pos = arena_push(&translated->constants, num_data, num_size);
|
||||
free(num_data);
|
||||
mpz_clear(num);
|
||||
// Export denominator
|
||||
size_t den_size;
|
||||
void *den_data = mpz_export(NULL, &den_size, 1, 1, 0, 0, den);
|
||||
size_t denominator_pos =
|
||||
arena_push(&translated->constants, den_data, den_size);
|
||||
free(den_data);
|
||||
mpz_clear(den);
|
||||
set_registers(translated, to_register + 1);
|
||||
|
||||
push_instruction_byte(translated, TYPE_OP_NUMBER);
|
||||
push_instruction_code(translated,length);
|
||||
push_instruction_code(translated, number_pos);
|
||||
size_t start = push_instruction_byte(translated, OP_LOAD_NUMBER);
|
||||
push_instruction_byte(translated, to_register);
|
||||
push_instruction_code(translated, num_size);
|
||||
push_instruction_code(translated, numerator_pos);
|
||||
push_instruction_code(translated, den_size);
|
||||
push_instruction_code(translated, denominator_pos);
|
||||
return start;
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
#define BYTECODE_NUMBER_H
|
||||
#include "../translator.h"
|
||||
|
||||
size_t translate_parsed_number(Translated *translated, char *number_str, size_t to_register);
|
||||
size_t translate_parsed_number(Translated *translated, mpq_t *number,
|
||||
size_t to_register);
|
||||
|
||||
#endif
|
||||
@@ -13,9 +13,8 @@
|
||||
size_t translate_parsed_string(Translated *translated, ParsedString parsedString) {
|
||||
size_t string_pos = arena_push(&translated->constants, parsedString.string, parsedString.length);
|
||||
set_registers(translated, 1);
|
||||
size_t start = push_instruction_byte(translated, OP_LOAD_CONST);
|
||||
size_t start = push_instruction_byte(translated, OP_LOAD_STRING);
|
||||
push_instruction_byte(translated, 0);
|
||||
push_instruction_byte(translated, TYPE_OP_STRING);
|
||||
push_instruction_code(translated,parsedString.length);
|
||||
push_instruction_code(translated, string_pos);
|
||||
return start;
|
||||
|
||||
@@ -127,7 +127,7 @@ size_t translate_parsed(Translated *translated, ParsedValue *parsedValue, ArErr*
|
||||
return translate_parsed_declaration(translated,
|
||||
*((DArray *)parsedValue->data), err);
|
||||
case AST_NUMBER:
|
||||
return translate_parsed_number(translated, (char *)parsedValue->data, 0);
|
||||
return translate_parsed_number(translated, (mpq_t*)parsedValue->data, 0);
|
||||
case AST_NULL:
|
||||
set_registers(translated, 1);
|
||||
size_t output = push_instruction_byte(translated, OP_LOAD_NULL);
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum {
|
||||
OP_LOAD_CONST,
|
||||
OP_LOAD_STRING,
|
||||
OP_DECLARE,
|
||||
OP_LOAD_NULL,
|
||||
OP_LOAD_FUNCTION,
|
||||
@@ -30,9 +30,9 @@ typedef enum {
|
||||
OP_CALL,
|
||||
OP_SOURCE_LOCATION,
|
||||
OP_LOAD_ACCESS_FUNCTION,
|
||||
OP_LOAD_BOOL
|
||||
OP_LOAD_BOOL,
|
||||
OP_LOAD_NUMBER
|
||||
} OperationType;
|
||||
typedef enum { TYPE_OP_STRING, TYPE_OP_NUMBER } types;
|
||||
|
||||
void arena_resize(ConstantArena *arena, size_t new_size);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user