Skip to content
Snippets Groups Projects
Commit 3f9fab04 authored by Michal Štěpánek's avatar Michal Štěpánek
Browse files

Little bit of code upkeep

parent 90aa8331
No related branches found
No related tags found
Loading
Pipeline #262027 passed
......@@ -183,7 +183,6 @@ void print_call ( VM * vm, u16 index, u8 arguments ) {
assert ( arg == arguments );
}
 
static inline u8 read_byte ( const u8 * data, size_t * pos ) {
return data [ (*pos)++ ];
}
......@@ -221,29 +220,23 @@ void read_header ( Str input, size_t * pos ) {
}
 
void read_constant_pool ( Program * program, Str input, size_t * pos ) {
// printf ( "%u, %u\n", count_bytes [ 0 ], count_bytes [ 1 ] );
program -> constant_count = read_u16 ( input . str, pos );
// printf ( "%x, %x\n", count_bytes [ 0 ], count_bytes [ 1 ] );
u8 tag;
//printf ( "size is %u\n", program -> constant_count );
Constant * constant_pool = (Constant*) malloc ( program -> constant_count * sizeof (Constant) );
for ( size_t i = 0; i < program -> constant_count; ++i ) {
tag = read_byte ( input . str, pos );
switch ( tag ) {
case 0x00:
constant_pool [ i ] = (Constant) { .kind = CONSTANT_INTEGER, .integer = read_i32 ( input . str, pos ) };
// printf ( "%d\n", constant_pool [ i ] . integer );
break;
case 0x01:
constant_pool [ i ] = (Constant) { .kind = CONSTANT_NULL };
// printf ( "null\n" );
break;
case 0x02: {
u32 len = (u32) read_i32 ( input . str, pos );
const u8 * start = input . str + *pos;
*pos += len;
constant_pool [ i ] = (Constant) { .kind = CONSTANT_STRING, .string = (Str) { .len = len, .str = start } };
// printf ( "%.*s\n", (int) constant_pool [ i ] . string . len, constant_pool [ i ] . string . str );
break;
}
case 0x03: {
......@@ -253,28 +246,21 @@ void read_constant_pool ( Program * program, Str input, size_t * pos ) {
const u8 * start = input . str + *pos;
*pos += len;
constant_pool [ i ] = (Constant) { .kind = CONSTANT_FUNCTION, .function = (ConstantFunction) { .parameters = parameters, .locals = locals, .len = len, .start = start } };
// printf ( "func: p=%u, l=%u, len=%u, start=%lu\n", parameters, locals, len, *pos - len );
break;
}
case 0x04:
constant_pool [ i ] = (Constant) { .kind = CONSTANT_BOOLEAN, .boolean = read_byte ( input . str, pos ) };
// printf ( "%s\n", constant_pool [ i ] . boolean == true ? "true" : "false" );
break;
case 0x05: {
constant_pool [ i ] = (Constant) { .kind = CONSTANT_CLASS, .class = read_class ( input . str, pos ) };
}
default:
// printf ( "unsupported type\n" );
break;
}
}
program -> constant_pool = constant_pool;
}
 
// void read_globals ( Program * program, Str input, size_t * pos ) {
// program -> globals = read_class ( input . str, pos );
// }
void parse_program ( Program * program, Str input ) {
// read + check header
size_t pos = 0;
......@@ -463,12 +449,12 @@ void bc_function_call ( VM * vm, u8 arguments, bool is_function, Str * name ) {
Value * cond = ostack_pop ( & vm -> stack );
if ( value_to_bool ( cond ) )
vm -> instruction_pointer += offset;
// printf (" %s\n", value_to_bool ( cond ) ? "T" : "F" );
// printf (" %c\n", value_to_bool ( cond ) ? 'T' : 'F');
break;
}
case 0x0E: {
i16 offset = (i16) read_u16 ( function . start, & vm -> instruction_pointer );
// printf ("jump %d from %lu\n", offset, vm -> instruction_pointer );
// printf ("jump %d from %lu\n", offset, vm -> instruction_pointer);
assert ( (i32) vm -> instruction_pointer >= - offset && vm -> instruction_pointer + offset < function . len );
vm -> instruction_pointer += offset;
break;
......@@ -482,7 +468,6 @@ void bc_function_call ( VM * vm, u8 arguments, bool is_function, Str * name ) {
return;
}
default:
printf ( "%d\n", opcode );
assert ( false );
}
}
......@@ -496,11 +481,6 @@ int vm_interpret ( VM * vm, Str input ) {
obj -> members [ i ] . value = make_null ( & vm -> heap );
vm -> globals = (Value*) obj;
bc_function_call ( vm, 0, true, NULL );
//Constant start = vm -> program . constant_pool [ vm -> program . entry_point ];
//u8 opcode;
//size_t addr = f . start - input . str;
//ostack_push ( & vm -> stack, (Value*) vm -> heap . begin );
return 0;
}
......@@ -5,8 +5,6 @@
#define OPERAND_STACK_INIT_CAPACITY 512
#define FRAME_STACK_CAPACITY 512
 
//#define READ_BYTE(str,pos) str . str [ *pos++ ]
typedef enum {
CONSTANT_INTEGER,
CONSTANT_BOOLEAN,
......@@ -14,9 +12,6 @@ typedef enum {
CONSTANT_STRING,
CONSTANT_FUNCTION,
CONSTANT_CLASS
// VALUE_ARRAY,
// VALUE_OBJECT,
// VALUE_INVALID
} ConstantType;
 
typedef struct Class {
......@@ -39,7 +34,6 @@ typedef struct Program {
u16 constant_count;
Constant * constant_pool;
Class globals;
//size_t entry_point;
u16 entry_point;
} Program;
 
......@@ -73,16 +67,6 @@ typedef struct VM {
void vm_init ( VM * vm, size_t heap_size );
void vm_destroy ( VM * vm );
 
// void * heap_alloc_alligned ( Heap * heap, size_t len, size_t align );
// void * heap_alloc ( Heap * heap, size_t len );
// void heap_init ( Heap * heap, size_t heap_size );
// void heap_destroy ( Heap * heap );
// Value * make_null ( ASTInterpreterState * state );
// Value * make_int ( ASTInterpreterState * state, i32 val );
// Value * make_bool ( ASTInterpreterState * state, bool val );
//inline u8 read_byte ( Str str, size_t * pos );
Value * alloc_constant ( VM * vm, u16 index );
ObjectValue * create_object ( VM * vm, Class * class );
 
......@@ -91,11 +75,8 @@ void print_call ( VM * vm, u16 index, u8 arguments );
 
void read_header ( Str input, size_t * pos );
void read_constant_pool ( Program * program, Str input, size_t * pos );
//void read_globals ( Program * program, Str input, size_t * pos );
void parse_program ( Program * program, Str input );
 
void bc_function_call ( VM * vm, u8 arguments, bool is_function, Str * name );
//void bc_intepret ( VM * vm, Str input, size_t & offset );
int vm_interpret ( VM * vm, Str input );
 
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment