Skip to content

Commit 3980439

Browse files
committed
feat: Implement core virtual machine, object system, and LLVM compiler backend.
1 parent f5baa71 commit 3980439

5 files changed

Lines changed: 24 additions & 11 deletions

File tree

include/object.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,12 @@ struct ObjDictionary {
150150

151151
typedef struct ObjTask ObjTask;
152152

153+
typedef void (*ResumeFn)(void*);
154+
153155
struct ObjTask {
154156
Obj obj;
155157
void* coroHandle; // LLVM Coroutine Handle
158+
ResumeFn resume; // Helper to resume coroutine
156159
bool completed;
157160
Value result;
158161
struct ObjTask* next; // For scheduler queue
@@ -175,7 +178,7 @@ struct ObjInstance *newInstance(struct ObjClass *klass);
175178
struct ObjBoundMethod *newBoundMethod(Value receiver, ObjClosure *method);
176179
struct ObjList *newList();
177180
struct ObjDictionary *newDictionary();
178-
struct ObjTask *newTask(void* hdl);
181+
struct ObjTask *newTask(void* hdl, ResumeFn resume);
179182
void printObject(Value value);
180183

181184
static inline bool isObjType(Value value, ObjType type) {

src/compiler/backend_llvm.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class LLVMEmitter {
8181
// Runtime helper: ObjTask* prox_rt_new_task(void* hdl)
8282
llvm::FunctionType *NewTaskType = llvm::FunctionType::get(
8383
Builder->getInt64Ty(), // Value (ObjTask* encoded)
84-
{Builder->getPtrTy()},
84+
{Builder->getPtrTy(), Builder->getPtrTy()},
8585
false
8686
);
8787
llvm::Function::Create(NewTaskType, llvm::Function::ExternalLinkage, "prox_rt_new_task", ModuleOb.get());
@@ -176,7 +176,9 @@ class LLVMEmitter {
176176

177177
// Create a Task object to return initially
178178
llvm::Function* NewTask = ModuleOb->getFunction("prox_rt_new_task");
179-
llvm::Value* TaskObj = Builder->CreateCall(NewTask, {CoroHdl}, "taskObj");
179+
llvm::Function* ResumeFn = ModuleOb->getFunction("prox_rt_resume");
180+
// ResumeFn should exist since setupSchedulerHelpers called before emitModule
181+
llvm::Value* TaskObj = Builder->CreateCall(NewTask, {CoroHdl, ResumeFn}, "taskObj");
180182

181183
// We need to return this TaskObj properly when function 'starts' (suspends at init).
182184
// But LLVM coroutines split functions. We need to handle `IR_OP_RETURN` specially too.

src/runtime/object.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@ void printObject(Value value) {
145145
}
146146
}
147147

148-
struct ObjTask *newTask(void* hdl) {
149-
struct ObjTask *task = ALLOCATE_OBJ(struct ObjTask, OBJ_TASK);
148+
struct ObjTask *newTask(void* hdl, ResumeFn resume) {
149+
ObjTask *task = (ObjTask *)allocateObject(sizeof(ObjTask), OBJ_TASK);
150150
task->coroHandle = hdl;
151+
task->resume = resume;
151152
task->completed = false;
152-
task->result = NIL_VAL;
153+
task->result = NULL_VAL;
153154
task->next = NULL;
154155
return task;
155156
}

src/runtime/scheduler.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#include <stdlib.h>
1111

1212
// Extern declaration of generated LLVM wrapper
13-
extern void prox_rt_resume(void* hdl);
13+
// prox_rt_resume removed, using function pointer in ObjTask
14+
1415

1516
typedef struct TaskQueue {
1617
ObjTask* head;
@@ -46,8 +47,8 @@ void scheduler_run() {
4647
ObjTask* task = scheduler_dequeue();
4748
currentTask = task;
4849
// Resume
49-
if (task->coroHandle) {
50-
prox_rt_resume(task->coroHandle);
50+
if (task->coroHandle && task->resume) {
51+
task->resume(task->coroHandle);
5152
}
5253
currentTask = NULL;
5354
}
@@ -76,8 +77,8 @@ void prox_rt_await(Value taskVal) {
7677
}
7778
}
7879

79-
Value prox_rt_new_task(void* hdl) {
80-
ObjTask* task = newTask(hdl);
80+
Value prox_rt_new_task(void* hdl, ResumeFn resume) {
81+
ObjTask* task = newTask(hdl, resume);
8182
// Auto-schedule new tasks?
8283
// Yes, usually.
8384
scheduler_enqueue(task);

src/runtime/vm.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
#include "../include/vm.h"
1919
#include "../include/error_report.h"
2020

21+
static bool bindMethod(struct ObjClass *klass, ObjString *name, VM *vm);
22+
static void defineMethod(ObjString *name, VM *vm);
23+
static void closeUpvalues(VM *vm, Value *last);
24+
static ObjUpvalue *captureUpvalue(Value *local, VM *vm);
25+
static bool invokeFromClass(struct ObjClass *klass, ObjString *name, int argCount, VM *vm);
26+
2127
VM vm;
2228

2329
static void resetStack(VM *pvm) {

0 commit comments

Comments
 (0)