feat: less clone on symbol

This commit is contained in:
2025-05-15 18:19:16 +08:00
parent 3e7a8a1c05
commit 864be73e77
12 changed files with 195 additions and 183 deletions

View File

@@ -22,6 +22,8 @@ pub fn compile(downgraded: ir::Downgraded) -> Program {
opcodes: Compiler::new().compile(*func.body),
})
.collect(),
symbols: downgraded.symbols,
symmap: downgraded.symmap
}
}
@@ -132,14 +134,8 @@ impl Compile for ir::UnOp {
use ir::UnOpKind::*;
match self.kind {
Neg => {
comp.push(OpCode::LookUp {
sym: "__sub".into(),
});
comp.push(OpCode::Const {
value: Const::Int(0),
});
self.rhs.compile(comp);
comp.push(OpCode::Call { arity: 2 });
comp.push(OpCode::UnOp { op: UnOp::Neg });
}
Not => {
self.rhs.compile(comp);
@@ -159,20 +155,14 @@ impl Compile for ir::BinOp {
comp.push(OpCode::BinOp { op: BinOp::Add });
}
Mul => {
comp.push(OpCode::LookUp {
sym: "__mul".into(),
});
self.lhs.compile(comp);
self.rhs.compile(comp);
comp.push(OpCode::Call { arity: 2 });
comp.push(OpCode::BinOp { op: BinOp::Mul });
}
Div => {
comp.push(OpCode::LookUp {
sym: "__div".into(),
});
self.lhs.compile(comp);
self.rhs.compile(comp);
comp.push(OpCode::Call { arity: 2 });
comp.push(OpCode::BinOp { op: BinOp::Div });
}
And => {
self.lhs.compile(comp);
@@ -190,12 +180,9 @@ impl Compile for ir::BinOp {
comp.push(OpCode::BinOp { op: BinOp::Eq });
}
Lt => {
comp.push(OpCode::LookUp {
sym: "__lessThan".into(),
});
self.lhs.compile(comp);
self.rhs.compile(comp);
comp.push(OpCode::Call { arity: 2 });
comp.push(OpCode::BinOp { op: BinOp::Lt });
}
Con => {
self.lhs.compile(comp);
@@ -209,12 +196,9 @@ impl Compile for ir::BinOp {
}
Sub => {
comp.push(OpCode::LookUp {
sym: "__sub".into(),
});
self.lhs.compile(comp);
self.rhs.compile(comp);
comp.push(OpCode::Call { arity: 2 });
comp.push(OpCode::BinOp { op: BinOp::Sub });
}
Impl => {
self.lhs.compile(comp);
@@ -229,29 +213,20 @@ impl Compile for ir::BinOp {
comp.push(OpCode::UnOp { op: UnOp::Not });
}
Gt => {
comp.push(OpCode::LookUp {
sym: "__lessThan".into(),
});
self.rhs.compile(comp);
self.lhs.compile(comp);
comp.push(OpCode::Call { arity: 2 });
comp.push(OpCode::BinOp { op: BinOp::Lt });
}
Leq => {
comp.push(OpCode::LookUp {
sym: "__lessThan".into(),
});
self.rhs.compile(comp);
self.lhs.compile(comp);
comp.push(OpCode::Call { arity: 2 });
comp.push(OpCode::BinOp { op: BinOp::Lt });
comp.push(OpCode::UnOp { op: UnOp::Not });
}
Geq => {
comp.push(OpCode::LookUp {
sym: "__lessThan".into(),
});
self.lhs.compile(comp);
self.rhs.compile(comp);
comp.push(OpCode::Call { arity: 2 });
comp.push(OpCode::BinOp { op: BinOp::Lt });
comp.push(OpCode::UnOp { op: UnOp::Not });
}