plugin: integrate evaluator for runtime comp
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I1e43447957eb0c09b3ee7c2c21abe6496a6a6964
This commit is contained in:
parent
c32328f14d
commit
22c92bfdd5
1 changed files with 23 additions and 48 deletions
|
|
@ -13,6 +13,7 @@
|
||||||
#include "irc/resolver.h"
|
#include "irc/resolver.h"
|
||||||
#include "irc/serializer.h"
|
#include "irc/serializer.h"
|
||||||
#include "irc/types.h"
|
#include "irc/types.h"
|
||||||
|
#include "irc/evaluator.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
@ -36,40 +37,31 @@ static void prim_loadIR(EvalState &state, const PosIdx pos, Value **args,
|
||||||
|
|
||||||
std::string pathStr(path);
|
std::string pathStr(path);
|
||||||
|
|
||||||
// Try to load the IR bundle
|
Deserializer deserializer;
|
||||||
std::ifstream file(pathStr, std::ios::binary | std::ios::ate);
|
IRModule module;
|
||||||
if (!file.is_open()) {
|
|
||||||
state.error<EvalError>("cannot open IR bundle: %s", pathStr)
|
try {
|
||||||
|
module = deserializer.deserialize(pathStr);
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
state.error<EvalError>("failed to deserialize IR bundle: %s", e.what())
|
||||||
.atPos(pos)
|
.atPos(pos)
|
||||||
.debugThrow();
|
.debugThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read magic number to verify it's an IR file
|
if (!module.entry) {
|
||||||
file.seekg(0);
|
state.error<EvalError>("IR bundle has no entry point")
|
||||||
uint32_t magic = 0;
|
|
||||||
file.read(reinterpret_cast<char *>(&magic), sizeof(magic));
|
|
||||||
|
|
||||||
if (magic != IR_MAGIC) {
|
|
||||||
state
|
|
||||||
.error<EvalError>("not a valid IR bundle: %s (bad magic number)",
|
|
||||||
pathStr)
|
|
||||||
.atPos(pos)
|
.atPos(pos)
|
||||||
.debugThrow();
|
.debugThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
// For now, just return a marker that this is an IR file
|
try {
|
||||||
// FIXME: complete full VM implementation
|
Evaluator evaluator(state);
|
||||||
auto bindings = state.buildBindings(2);
|
evaluator.eval_to_nix(module.entry, v);
|
||||||
|
} catch (const std::exception &e) {
|
||||||
Value *vType = state.allocValue();
|
state.error<EvalError>("failed to evaluate IR: %s", e.what())
|
||||||
vType->mkString("ir-bundle");
|
.atPos(pos)
|
||||||
bindings.insert(state.symbols.create("type"), vType);
|
.debugThrow();
|
||||||
|
}
|
||||||
Value *vPath = state.allocValue();
|
|
||||||
vPath->mkString(pathStr);
|
|
||||||
bindings.insert(state.symbols.create("path"), vPath);
|
|
||||||
|
|
||||||
v.mkAttrs(bindings.finish());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -85,7 +77,6 @@ static void prim_compileNix(EvalState &state, const PosIdx pos, Value **args,
|
||||||
std::string sourceStr(source);
|
std::string sourceStr(source);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Parse the Nix source
|
|
||||||
Parser parser;
|
Parser parser;
|
||||||
auto ast = parser.parse(sourceStr, "<inline>");
|
auto ast = parser.parse(sourceStr, "<inline>");
|
||||||
|
|
||||||
|
|
@ -95,23 +86,11 @@ static void prim_compileNix(EvalState &state, const PosIdx pos, Value **args,
|
||||||
.debugThrow();
|
.debugThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate IR
|
|
||||||
IRGenerator ir_gen;
|
IRGenerator ir_gen;
|
||||||
auto ir = ir_gen.generate(ast);
|
auto ir = ir_gen.generate(ast);
|
||||||
|
|
||||||
// For now, return a marker that compilation succeeded
|
Evaluator evaluator(state);
|
||||||
// FIXME: full evaluation
|
evaluator.eval_to_nix(ir, v);
|
||||||
auto bindings = state.buildBindings(2);
|
|
||||||
|
|
||||||
Value *vType = state.allocValue();
|
|
||||||
vType->mkString("ir-compiled");
|
|
||||||
bindings.insert(state.symbols.create("type"), vType);
|
|
||||||
|
|
||||||
Value *vSource = state.allocValue();
|
|
||||||
vSource->mkString(sourceStr.substr(0, 50));
|
|
||||||
bindings.insert(state.symbols.create("source"), vSource);
|
|
||||||
|
|
||||||
v.mkAttrs(bindings.finish());
|
|
||||||
|
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
state.error<EvalError>("IR compilation failed: %s", e.what())
|
state.error<EvalError>("IR compilation failed: %s", e.what())
|
||||||
|
|
@ -126,22 +105,18 @@ static void prim_compileNix(EvalState &state, const PosIdx pos, Value **args,
|
||||||
*/
|
*/
|
||||||
static void prim_info(EvalState &state, const PosIdx pos, Value **args,
|
static void prim_info(EvalState &state, const PosIdx pos, Value **args,
|
||||||
Value &v) {
|
Value &v) {
|
||||||
auto bindings = state.buildBindings(4);
|
auto bindings = state.buildBindings(3);
|
||||||
|
|
||||||
Value *vName = state.allocValue();
|
Value *vName = state.allocValue();
|
||||||
vName->mkString("nix-ir-plugin");
|
vName->mkString("nix-ir-plugin");
|
||||||
bindings.insert(state.symbols.create("name"), vName);
|
bindings.insert(state.symbols.create("name"), vName);
|
||||||
|
|
||||||
Value *vVersion = state.allocValue();
|
Value *vVersion = state.allocValue();
|
||||||
vVersion->mkString("0.1.0-alpha");
|
vVersion->mkString("0.1.0");
|
||||||
bindings.insert(state.symbols.create("version"), vVersion);
|
bindings.insert(state.symbols.create("version"), vVersion);
|
||||||
|
|
||||||
Value *vPhase = state.allocValue();
|
|
||||||
vPhase->mkString("phase-4");
|
|
||||||
bindings.insert(state.symbols.create("phase"), vPhase);
|
|
||||||
|
|
||||||
Value *vStatus = state.allocValue();
|
Value *vStatus = state.allocValue();
|
||||||
vStatus->mkString("compiler-complete");
|
vStatus->mkString("runtime-active");
|
||||||
bindings.insert(state.symbols.create("status"), vStatus);
|
bindings.insert(state.symbols.create("status"), vStatus);
|
||||||
|
|
||||||
v.mkAttrs(bindings.finish());
|
v.mkAttrs(bindings.finish());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue