Skip to content

Commit

Permalink
Megarefactor- replace bytecode interpreter
Browse files Browse the repository at this point in the history
- New interpreter does proper register assignment
- Lots of other updates
  • Loading branch information
andyfischer committed Feb 21, 2015
1 parent 990f5da commit 3d9c1a2
Show file tree
Hide file tree
Showing 221 changed files with 8,077 additions and 3,600 deletions.
2 changes: 2 additions & 0 deletions BUGS
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

Miscellanous (most-recent first)

using @ on a function input is broken again (it creates an extra output)

make 'make' optional on a type. As in, "Type.something" is equivalent to "Type.make.something"

to_string should escape the stuff inside the string.. quotes and newlines and etc.
Expand Down
39 changes: 35 additions & 4 deletions include/circa/circa.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ namespace circa {
struct Type;
struct World;
struct Value;
struct VM;
}

typedef circa::Block caBlock;

// a Stack holds the interpreter's current state, including a list of frames (activation
// records).
// TODO: replace with VM
typedef circa::Stack caStack;

typedef circa::VM caVM;

typedef circa::Term caTerm;

// a Type holds data for a single Circa type, including a name, handlers for
Expand All @@ -51,6 +55,7 @@ typedef circa::Value caValue;
#else

typedef struct caBlock caBlock;
typedef struct caVM caVM;
typedef struct caStack caStack;
typedef struct caTerm caTerm;
typedef struct caType caType;
Expand Down Expand Up @@ -79,7 +84,6 @@ union caValueData {

#ifdef __cplusplus

// C++ wrapper on caValue. Provides C++-style initialization and destruction.
namespace circa {

struct Value
Expand All @@ -93,21 +97,32 @@ struct Value
Value& operator=(Value const&);

caTerm* asTerm();
caBlock* asBlock();
bool asBool();
caSymbol asSymbol();
int asInt();
float asFloat();
ListData* listData();
int as_i() { return asInt(); }
float as_f() { return asFloat(); }
float to_f();
caSymbol as_s() { return asSymbol(); }
const char* as_str();
caSymbol as_b() { return asBool(); }

Value* index(int i);
int length();

// Assignment
Value* set(caValue* v) { return set_value(v); }
Value* set_value(caValue* v);
Value* set_block(Block* b);
Value* set_bool(bool b);
Value* set_string(const char* s);
Value* set_int(int i);
Value* set_float(float f);
Value* set_symbol(caSymbol s);
Value* set_term(Term* t);

// List utils
Value* set_list(int size);
Expand All @@ -122,17 +137,30 @@ struct Value
Value* append_sym(caSymbol s);
Value* append_str(const char* s);
Value* extend(Value* rhsList);
void pop();
Value* last();

// Hashtable utils
Value* set_hashtable();
Value* field(caSymbol field);
Value* int_key(int key);
Value* val_key(Value* key);
Value* term_key(Term* key);
Value* block_key(Block* key);
Value* set_field_int(caSymbol field, int i);
Value* set_field_str(caSymbol field, const char*);
Value* set_field_sym(caSymbol field, caSymbol sym);
Value* insert(caSymbol field);
Value* set_field_term(caSymbol field, Term* term);
Value* set_field_bool(caSymbol field, bool b);
bool field_bool(caSymbol field, bool defaultValue);
Value* insert(caSymbol key);
Value* insert_int(int key);
Value* insert_val(Value* valueKey);
Value* insert_term(Term* key);

// For debugging:
void dump();
char* to_c_string(); // caller must free() this char*.
};

} // namespace circa
Expand All @@ -142,7 +170,7 @@ struct Value
// EvaluateFunc is the signature for a C evaluation function. The function will access the
// stack to read inputs (if any), possibly perform some action, and write output values
// (if any) back to the stack.
typedef void (*caEvaluateFunc)(caStack* stack);
typedef void (*caEvaluateFunc)(caVM* stack);

typedef void (*caNativePtrRelease)(void* ptr);

Expand Down Expand Up @@ -534,7 +562,10 @@ caValue* circa_declare_value(caBlock* block, const char* name);

// -- Native module support --
caNativePatch* circa_create_native_patch(caWorld* world, const char* name);
void circa_patch_function(caNativePatch* module, const char* name, caEvaluateFunc func);

#define circa_patch_function(x,y,z) ;

void circa_patch_function2(caNativePatch* patch, const char* nameStr, caEvaluateFunc func);
// void circa_patch_type_release(caNativePatch* module, const char* typeName, caReleaseFunc func);
void circa_finish_native_patch(caNativePatch* module);

Expand Down
101 changes: 51 additions & 50 deletions src/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "kernel.h"
#include "tagged_value.h"
#include "type.h"
#include "vm.h"

namespace circa {

Expand Down Expand Up @@ -44,7 +45,7 @@ BlobSlice* blob_alloc_slice()
return slice;
}

BlobRaw* blob_alloc_raw(char* data, u32 numBytes)
BlobRaw* blob_alloc_raw(const char* data, u32 numBytes)
{
BlobRaw* blob = (BlobRaw*) malloc(sizeof(*blob) + numBytes);
blob->concreteType = BLOB_RAW;
Expand All @@ -57,7 +58,7 @@ BlobRaw* blob_alloc_raw(char* data, u32 numBytes)
return blob;
}

char* set_blob_alloc_raw(Value* value, char* data, u32 numBytes)
char* set_blob_alloc_raw(Value* value, const char* data, u32 numBytes)
{
BlobRaw* raw = blob_alloc_raw(data, numBytes);
make_no_initialize(TYPES.blob, value);
Expand Down Expand Up @@ -188,135 +189,135 @@ void blob_setup_type(Type* type)
type->hashFunc = blob_hash;
}

void make_blob(Stack* stack)
void make_blob(VM* vm)
{
u32 size = circa_int_input(stack, 0);
set_blob_alloc_raw(circa_output(stack, 0), NULL, size);
u32 size = vm->input(0)->as_i();
set_blob_alloc_raw(vm->output(), NULL, size);
}

void Blob__size(Stack* stack)
void Blob__size(VM* vm)
{
set_int(circa_output(stack, 0), blob_size(circa_input(stack, 0)));
set_int(vm->output(), blob_size(vm->input(0)));
}

void Blob__slice(Stack* stack)
void Blob__slice(VM* vm)
{
Value* existingBlob = circa_input(stack, 0);
int offset = circa_int_input(stack, 1);
int size = circa_int_input(stack, 2);
Value* existingBlob = vm->input(0);
int offset = vm->input(1)->as_i();
int size = vm->input(2)->as_i();

char* existingData;
u32 existingNumBytes;
blob_data(existingBlob, &existingData, &existingNumBytes);

if ((offset + size) >= existingNumBytes)
return circa_output_error(stack, "Offset+size is out of bounds");
if ((offset + size) > existingNumBytes)
return vm->throw_str("Offset+size is out of bounds");

circa_set_blob_from_backing_value(circa_output(stack, 0), existingBlob, existingData+offset, size);
circa_set_blob_from_backing_value(vm->output(), existingBlob, existingData+offset, size);
}

#define BLOB_SET(type) \
Value* blob = circa_input(stack, 0); \
int offset = circa_int_input(stack, 1); \
Value* blob = vm->input(0); \
int offset = vm->input(1)->as_i(); \
\
if ((offset + sizeof(type)) > blob_size(blob)) \
return circa_output_error(stack, "Offset is out of bounds"); \
return vm->throw_str("Offset is out of bounds"); \
\
char* data = blob_touch(blob); \
*(type*)(data + offset) = circa_int_input(stack, 2); \
move(blob, circa_output(stack, 0));
*(type*)(data + offset) = vm->input(2)->as_i(); \
move(blob, vm->output());

void Blob__set_u8(Stack* stack)
void Blob__set_u8(VM* vm)
{
BLOB_SET(u8);
}

void Blob__set_u16(Stack* stack)
void Blob__set_u16(VM* vm)
{
BLOB_SET(u16);
}

void Blob__set_u32(Stack* stack)
void Blob__set_u32(VM* vm)
{
BLOB_SET(u32);
}

void Blob__set_i8(Stack* stack)
void Blob__set_i8(VM* vm)
{
BLOB_SET(i8);
}

void Blob__set_i16(Stack* stack)
void Blob__set_i16(VM* vm)
{
BLOB_SET(i16);
}

void Blob__set_i32(Stack* stack)
void Blob__set_i32(VM* vm)
{
BLOB_SET(i32);
}

#define BLOB_GET(type) \
Value* blob = circa_input(stack, 0); \
int offset = circa_int_input(stack, 1); \
Value* blob = vm->input(0); \
int offset = vm->input(1)->as_i(); \
\
char* data; \
u32 numBytes; \
blob_data(blob, &data, &numBytes); \
\
if ((offset + sizeof(type)) > numBytes) \
return circa_output_error(stack, "Offset is out of bounds"); \
return vm->throw_str("Offset is out of bounds"); \
\
type result = *(type*) (data + offset); \
set_int(circa_output(stack, 0), result);
set_int(vm->output(), result);

void Blob__u8(Stack* stack)
void Blob__u8(VM* vm)
{
BLOB_GET(u8);
}

void Blob__u16(Stack* stack)
void Blob__u16(VM* vm)
{
BLOB_GET(u16);
}

void Blob__u32(Stack* stack)
void Blob__u32(VM* vm)
{
BLOB_GET(u32);
}

void Blob__i8(Stack* stack)
void Blob__i8(VM* vm)
{
BLOB_GET(i8);
}

void Blob__i16(Stack* stack)
void Blob__i16(VM* vm)
{
BLOB_GET(i16);
}

void Blob__i32(Stack* stack)
void Blob__i32(VM* vm)
{
BLOB_GET(i32);
}

void blob_install_functions(NativePatch* patch)
{
circa_patch_function(patch, "make_blob", make_blob);
circa_patch_function(patch, "Blob.size", Blob__size);
circa_patch_function(patch, "Blob.slice", Blob__slice);
circa_patch_function(patch, "Blob.set_u8", Blob__set_u8);
circa_patch_function(patch, "Blob.set_u16", Blob__set_u16);
circa_patch_function(patch, "Blob.set_u32", Blob__set_u32);
circa_patch_function(patch, "Blob.set_i8", Blob__set_i8);
circa_patch_function(patch, "Blob.set_i16", Blob__set_i16);
circa_patch_function(patch, "Blob.set_i32", Blob__set_i32);
circa_patch_function(patch, "Blob.i8", Blob__i8);
circa_patch_function(patch, "Blob.i16", Blob__i16);
circa_patch_function(patch, "Blob.i32", Blob__i32);
circa_patch_function(patch, "Blob.u8", Blob__u8);
circa_patch_function(patch, "Blob.u16", Blob__u16);
circa_patch_function(patch, "Blob.u32", Blob__u32);
circa_patch_function2(patch, "make_blob", make_blob);
circa_patch_function2(patch, "Blob.size", Blob__size);
circa_patch_function2(patch, "Blob.slice", Blob__slice);
circa_patch_function2(patch, "Blob.set_u8", Blob__set_u8);
circa_patch_function2(patch, "Blob.set_u16", Blob__set_u16);
circa_patch_function2(patch, "Blob.set_u32", Blob__set_u32);
circa_patch_function2(patch, "Blob.set_i8", Blob__set_i8);
circa_patch_function2(patch, "Blob.set_i16", Blob__set_i16);
circa_patch_function2(patch, "Blob.set_i32", Blob__set_i32);
circa_patch_function2(patch, "Blob.i8", Blob__i8);
circa_patch_function2(patch, "Blob.i16", Blob__i16);
circa_patch_function2(patch, "Blob.i32", Blob__i32);
circa_patch_function2(patch, "Blob.u8", Blob__u8);
circa_patch_function2(patch, "Blob.u16", Blob__u16);
circa_patch_function2(patch, "Blob.u32", Blob__u32);
}

} // namespace circa
2 changes: 2 additions & 0 deletions src/blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace circa {
#define blob_size circa_blob_size
#define set_blob_from_backing_value circa_set_blob_from_backing_value

char* set_blob_alloc_raw(Value* value, const char* data, u32 numBytes);

void blob_setup_type(Type* type);
void blob_install_functions(NativePatch* patch);

Expand Down
Loading

0 comments on commit 3d9c1a2

Please sign in to comment.