From d3471ed1af1e9c617dca7848d8107ad44860305e Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Sun, 11 Aug 2024 19:25:37 +0100 Subject: [PATCH] Make primitive arithmetic operations compile to bytecode --- src/compiler.cpp | 33 ++++++++++++++++++++++++++++----- src/vli.cpp | 6 ++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/compiler.cpp b/src/compiler.cpp index 863358c..3274d83 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -98,6 +98,19 @@ Result Compiler::compile_primop(Context& context, Symbol& op, Value cur = TRY(expr.rest()); Expression ex = TRY(Expression::create()); + Oc opcode = Oc::Unknown; + const char* name = ""; + + if (TRY(op.cmp("+")) == 0) { + opcode = Oc::Add; + name = "add"; + } else if (TRY(op.cmp("*")) == 0) { + opcode = Oc::Mul; + name = "mul"; + } else { + return ERROR(NotImplemented); + } + if (cur.is()) { uint64_t reg = context.alloc_reg(); int64_t zero = TRY(context.add_const(TRY(Value::create((int64_t)0)))); @@ -113,7 +126,8 @@ Result Compiler::compile_primop(Context& context, Symbol& op, auto comp = TRY(compile_expr(context, subexpr)); ex.add_code(comp.code); - uint64_t reg = comp.reg; + uint64_t firstreg = comp.reg; + uint64_t reg = firstreg; cur = TRY(pair.rest()); @@ -125,15 +139,24 @@ Result Compiler::compile_primop(Context& context, Symbol& op, 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}, + auto rest = TRY(pair.rest()); + + uint64_t res = 0; + if (rest.is()) + res = firstreg; + else + res = context.alloc_reg(); + + std::cout << name << " r" << res << ", r" << reg << ", r" << comp.reg + << "\n"; + TRY(ex.add_opcode(opcode, {0, (int64_t)res}, {0, (int64_t)reg}, {0, (int64_t)comp.reg})); reg = res; - cur = TRY(pair.rest()); + cur = std::move(rest); } + context.maxreg = firstreg + 1; ex.reg = reg; return std::move(ex); } diff --git a/src/vli.cpp b/src/vli.cpp index 706828c..d30e9fd 100644 --- a/src/vli.cpp +++ b/src/vli.cpp @@ -9,13 +9,11 @@ StaticArena<64 * 1024 * 1024> arena; int main() { - auto s = DIEX(String::create("(+ (+ 1 2 3) 4)")); + auto s = DIEX(String::create("(* (+ 1 2 3) 4)")); auto reader = Reader(s); auto r = DIEX(reader.read_one()); - - auto writer = Writer(); - auto s2 = DIEX(writer.write_one(r)); + auto s2 = DIEX(write_one(r)); DIEX(arena_gc());