First proper code generation for addition
This commit is contained in:
parent
a195b8d0e3
commit
3cb4869ed5
4 changed files with 49 additions and 18 deletions
|
@ -156,10 +156,10 @@ class Array : public Object {
|
|||
auto pod = TRY(arena_alloc<PodArray>(res_size * sizeof(PodObject*)));
|
||||
pod->size = res_size;
|
||||
for (uint64_t i = 0; i < size(); i++) {
|
||||
pod->data[i] = _value->data[i];
|
||||
pod->data[i] = _value->data[i].get();
|
||||
}
|
||||
for (uint64_t i = size(); i < rhs.size(); i++) {
|
||||
pod->data[i] = rhs._value->data[i];
|
||||
for (uint64_t i = 0; i < rhs.size(); i++) {
|
||||
pod->data[lhs_size + i] = rhs._value->data[i].get();
|
||||
}
|
||||
|
||||
return Array(TRY(MkGcRoot(pod)));
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include "compiler.hpp"
|
||||
|
||||
#include "common.hpp"
|
||||
|
||||
struct Context {
|
||||
Context() {}
|
||||
Context(Value&& env, Array&& constants, Dict&& constants_dict)
|
||||
|
@ -45,11 +47,11 @@ struct Context {
|
|||
uint64_t maxreg;
|
||||
};
|
||||
|
||||
Result<Array> add_opcode(Array& opcodes, Oc opcode, OpArg arg1 = {0, 0},
|
||||
OpArg arg2 = {0, 0}, OpArg arg3 = {0, 0},
|
||||
OpArg arg4 = {0, 0}) {
|
||||
Result<void> Expression::add_opcode(Oc opcode, OpArg arg1, OpArg arg2,
|
||||
OpArg arg3, OpArg arg4) {
|
||||
Value oc = Value(TRY(Opcode::create(opcode, arg1, arg2, arg3, arg4)));
|
||||
return opcodes.append(oc);
|
||||
code = TRY(code.append(oc));
|
||||
return Result<void>();
|
||||
}
|
||||
|
||||
Result<bool> is_primitive_op(Symbol& sym) {
|
||||
|
@ -61,7 +63,12 @@ Result<bool> is_primitive_op(Symbol& sym) {
|
|||
Result<Value> Compiler::compile(Value& expr) {
|
||||
auto context = TRY(Context::create());
|
||||
|
||||
TRY(compile_expr(context, expr));
|
||||
auto ex = TRY(compile_expr(context, expr));
|
||||
|
||||
Value constants = TRY(context.constants.copy());
|
||||
Value code = TRY(ex.code.copy());
|
||||
TRY(debug_print(constants));
|
||||
TRY(debug_print(code));
|
||||
return ERROR(NotImplemented);
|
||||
}
|
||||
|
||||
|
@ -89,12 +96,12 @@ Result<Expression> Compiler::compile_expr(Context& context, Value& expr) {
|
|||
Result<Expression> Compiler::compile_primop(Context& context, Symbol& op,
|
||||
Pair& expr) {
|
||||
Value cur = TRY(expr.rest());
|
||||
Array code = TRY(Array::create());
|
||||
Expression ex = TRY(Expression::create());
|
||||
|
||||
uint64_t reg = context.alloc_reg();
|
||||
int64_t zero = TRY(context.add_const(TRY(Value::create((int64_t)0))));
|
||||
std::cout << "load-immediate r" << reg << ", " << 0 << "\n";
|
||||
code = TRY(add_opcode(code, Oc::LoadImmediate, {0, (int64_t)reg}, {0, 0}));
|
||||
std::cout << "mov r" << reg << ", c" << zero << "\n";
|
||||
TRY(ex.add_opcode(Oc::Mov, {0, (int64_t)reg}, {1, zero}));
|
||||
|
||||
while (!cur.is<Nil>()) {
|
||||
Pair& pair = *cur.to<Pair>();
|
||||
|
@ -102,15 +109,19 @@ Result<Expression> Compiler::compile_primop(Context& context, Symbol& op,
|
|||
|
||||
auto comp = TRY(compile_expr(context, subexpr));
|
||||
|
||||
ex.add_code(comp.code);
|
||||
|
||||
uint64_t res = context.alloc_reg();
|
||||
std::cout << "add r" << res << ", r" << reg << ", r" << comp.reg << "\n";
|
||||
TRY(ex.add_opcode(Oc::Add, {0, (int64_t)res}, {0, (int64_t)reg},
|
||||
{0, (int64_t)comp.reg}));
|
||||
|
||||
reg = res;
|
||||
cur = TRY(pair.rest());
|
||||
}
|
||||
|
||||
Expression ex = Expression(reg, TRY(Array::create()));
|
||||
return ex;
|
||||
ex.reg = reg;
|
||||
return std::move(ex);
|
||||
}
|
||||
|
||||
Result<Expression> Compiler::compile_list(Context& context, Pair& expr) {
|
||||
|
@ -126,9 +137,14 @@ Result<Expression> Compiler::compile_list(Context& context, Pair& expr) {
|
|||
}
|
||||
|
||||
Result<Expression> Compiler::compile_int64(Context& context, Int64& value) {
|
||||
Expression ex = TRY(Expression::create());
|
||||
uint64_t reg = context.alloc_reg();
|
||||
std::cout << "loadc r" << reg << ", " << value.value() << "\n";
|
||||
|
||||
Expression expr = Expression(reg, TRY(Array::create()));
|
||||
return expr;
|
||||
int64_t c = TRY(context.add_const(TRY(value.copy())));
|
||||
|
||||
std::cout << "loadc r" << reg << ", " << value.value() << "\n";
|
||||
TRY(ex.add_opcode(Oc::Mov, {0, (int64_t)reg}, {1, (int64_t)c}));
|
||||
|
||||
ex.reg = reg;
|
||||
return std::move(ex);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,20 @@ struct Context;
|
|||
|
||||
struct Expression {
|
||||
Expression() {}
|
||||
Expression(uint64_t reg, Array&& code) : reg(reg), code(std::move(code)) {}
|
||||
Expression(Array&& code) : reg(0), code(std::move(code)) {}
|
||||
|
||||
static Result<Expression> create() {
|
||||
Array c = TRY(Array::create());
|
||||
|
||||
return Expression(std::move(c));
|
||||
}
|
||||
Result<void> add_opcode(Oc opcode, OpArg arg1 = {0, 0}, OpArg arg2 = {0, 0},
|
||||
OpArg arg3 = {0, 0}, OpArg arg4 = {0, 0});
|
||||
|
||||
Result<void> add_code(Array& c) {
|
||||
code = TRY(code.concat(c));
|
||||
return Result<void>();
|
||||
}
|
||||
uint64_t reg;
|
||||
Array code;
|
||||
};
|
||||
|
|
|
@ -215,7 +215,9 @@ Result<String> Writer::write_dict(Dict& val) {
|
|||
}
|
||||
|
||||
Result<String> Writer::write_opcode(Opcode& val) {
|
||||
return ERROR(NotImplemented);
|
||||
String res = TRY(String::create("#<opcode>"));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Result<String> write_one(Value& value) {
|
||||
|
|
Loading…
Reference in a new issue