Skip to content

Commit

Permalink
Introduce more special "immediate" value opcodes for ints, errors, ob…
Browse files Browse the repository at this point in the history
…jids, none, and empty list, rather than having them sit in the literals table.
  • Loading branch information
rdaum committed Jan 14, 2024
1 parent 68a1015 commit e522cc2
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 165 deletions.
29 changes: 26 additions & 3 deletions crates/compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::collections::HashMap;
use itertools::Itertools;
use tracing::error;

use moor_values::var::variant::Variant;
use moor_values::var::{v_int, Var};

use crate::ast::{
Expand Down Expand Up @@ -323,8 +324,28 @@ impl CodegenState {
fn generate_expr(&mut self, expr: &Expr) -> Result<(), CompileError> {
match expr {
Expr::VarExpr(v) => {
let literal = self.add_literal(v);
self.emit(Op::Imm(literal));
match v.variant() {
Variant::None => {
self.emit(Op::ImmNone);
}
Variant::Obj(oid) => {
self.emit(Op::ImmObjid(*oid));
}
Variant::Int(ref i) => {
if i <= &(i32::MAX as i64) {
self.emit(Op::ImmInt(*i as i32));
} else {
self.emit(Op::ImmBigInt(*i));
}
}
Variant::Err(e) => {
self.emit(Op::ImmErr(*e));
}
_ => {
let literal = self.add_literal(v);
self.emit(Op::Imm(literal));
}
};
self.push_stack(1);
}
Expr::Id(ident) => {
Expand Down Expand Up @@ -715,8 +736,10 @@ impl CodegenState {
}

fn generate_arg_list(&mut self, args: &Vec<Arg>) -> Result<(), CompileError> {
// TODO: Check recursion down to see if all literal values, and if so reduce to a Imm value with the full list,
// instead of concatenation with MkSingletonList.
if args.is_empty() {
self.emit(Op::MkEmptyList);
self.emit(Op::ImmEmptyList);
self.push_stack(1);
return Ok(());
}
Expand Down
Loading

0 comments on commit e522cc2

Please sign in to comment.