Skip to content

Commit

Permalink
Reuse constants when compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkaplan committed May 20, 2024
1 parent 9d3db38 commit 8be2526
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 314 deletions.
15 changes: 14 additions & 1 deletion src/bytecode/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,25 @@ impl Chunk {
}

pub fn add_constant(&mut self, constant: Constant) -> u8 {
// Reuse existing constants when possible.
if let Some(idx) = self.find_constant(&constant) {
return idx;
}

let idx = self.constants.len();
assert!(idx < u8::MAX.into());

// TODO: Reuse existing constant if already registered
self.constants.push(constant);
idx as u8
}

fn find_constant(&self, constant: &Constant) -> Option<u8> {
// TODO: HashMap/BTreeMap?
self.constants
.iter()
.position(|c| c == constant)
.map(|idx| idx.try_into().expect("add_constant enforces count"))
}
}

// Globals
Expand All @@ -59,6 +71,7 @@ impl Chunk {
}

fn find_global(&self, name: &str) -> Option<u8> {
// TODO: HashMap/BTreeMap?
self.globals
.iter()
.position(|g| g == name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ DisassembledChunk(
String("dos"),
String("id"),
String("name"),
String("id"),
String("name"),
String("two"),
String("one"),
String("two"),
String("one"),
],
Expand Down Expand Up @@ -50,27 +46,27 @@ DisassembledChunk(
Pop,
GetGlobal(1),
GetLocal(2),
GetField(4),
GetField(2),
GetLocal(2),
GetField(5),
GetField(3),
Call(2),
Pop,
GetLocal(1),
GetLocal(2),
SetField(6),
SetField(4),
Pop,
GetLocal(2),
GetLocal(1),
SetField(7),
SetField(5),
Pop,
GetGlobal(1),
GetLocal(1),
GetField(8),
GetField(4),
Call(1),
Pop,
GetGlobal(1),
GetLocal(2),
GetField(9),
GetField(5),
Call(1),
Pop,
Nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ DisassembledChunk(
constants: [
String("OpenStruct"),
String("a"),
String("a"),
String("b"),
String("b"),
String("a"),
String("b"),
],
globals: [
Expand All @@ -23,25 +19,25 @@ DisassembledChunk(
DefineGlobal(0),
GetGlobal(0),
Object,
Constant(2),
Constant(1),
SetField(1),
DefineGlobal(1),
GetGlobal(1),
Constant(4),
SetField(3),
Constant(2),
SetField(2),
Pop,
GetGlobal(2),
GetGlobal(1),
Call(1),
Pop,
GetGlobal(2),
GetGlobal(1),
GetField(5),
GetField(1),
Call(1),
Pop,
GetGlobal(2),
GetGlobal(1),
GetField(6),
GetField(2),
Call(1),
Pop,
Nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ DisassembledChunk(
upvalues: 0,
chunk: DisassembledChunk(
constants: [
Rational(((1, [
1,
]), (1, [
1,
]))),
Rational(((1, [
1,
]), (1, [
Expand Down Expand Up @@ -43,12 +38,12 @@ DisassembledChunk(
Pop,
GetGlobal(0),
GetLocal(1),
Constant(1),
Constant(0),
Sub,
Call(1),
GetGlobal(0),
GetLocal(1),
Constant(2),
Constant(1),
Sub,
Call(1),
Add,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ DisassembledChunk(
constants: [
Float(1.0),
Float(0.0),
Float(1.0),
Float(0.0),
Float(0.0),
Float(0.0),
Float(0.1),
Float(0.2),
],
Expand All @@ -25,13 +21,13 @@ DisassembledChunk(
Constant(1),
Div,
DefineGlobal(0),
Constant(2),
Constant(0),
Neg,
Constant(3),
Constant(1),
Div,
DefineGlobal(1),
Constant(4),
Constant(5),
Constant(1),
Constant(1),
Div,
DefineGlobal(2),
GetGlobal(3),
Expand All @@ -56,8 +52,8 @@ DisassembledChunk(
Call(2),
Pop,
GetGlobal(3),
Constant(6),
Constant(7),
Constant(2),
Constant(3),
Add,
Call(1),
Pop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,11 @@ DisassembledChunk(
String("🈔"),
String("tres"),
String("append"),
Rational(((1, [
1,
]), (1, [
1,
]))),
String("append"),
String("🈔"),
String("append"),
String("append"),
Rational(((1, [
2,
]), (1, [
1,
]))),
String("tres"),
],
globals: [
"println",
Expand All @@ -47,21 +37,21 @@ DisassembledChunk(
List(0),
GetLocal(2),
GetField(3),
Constant(4),
Constant(0),
Call(1),
Pop,
GetLocal(2),
GetField(5),
Constant(6),
GetField(3),
Constant(1),
Call(1),
Pop,
GetLocal(2),
GetField(7),
GetField(3),
GetLocal(1),
Call(1),
Pop,
GetLocal(2),
GetField(8),
GetField(3),
Nil,
Call(1),
Pop,
Expand All @@ -80,8 +70,8 @@ DisassembledChunk(
Call(1),
Pop,
GetLocal(2),
Constant(9),
Constant(10),
Constant(4),
Constant(2),
SetIndex,
GetGlobal(0),
GetLocal(1),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: src/compile.rs
description: "func main() {\n let x = 0;\n loop twice {\n if x >= 2 {\n break;\n }\n\n println(\"twice before\", x);\n x = x + 1;\n println(\"twice after \", x);\n }\n println(\"out of twice\", x);\n\n loop outer {\n if x < 0 {\n println(\"break outer\", x);\n break;\n }\n\n loop inner {\n loop {\n break;\n }\n\n if x > 2 {\n println(\"break inner\", x);\n break;\n }\n\n println(\"inner before\", x);\n x = x + 1;\n println(\"inner after \", x);\n }\n println(\"out of inner\", x);\n // x = 3\n\n if x > 2 {\n x = -1;\n continue;\n }\n\n println(\"never happens\");\n x = x - 1;\n }\n println(\"out of outer\", x);\n}\n\nmain();\n"
description: "func main() {\n let x = 0;\n loop twice {\n if x >= 2 {\n break twice;\n }\n\n println(\"twice before\", x);\n x = x + 1;\n println(\"twice after \", x);\n }\n println(\"out of twice\", x);\n\n loop outer {\n if x < 0 {\n println(\"break outer\", x);\n break;\n }\n\n loop inner {\n loop {\n break;\n }\n\n if x > 2 {\n println(\"break inner\", x);\n break;\n }\n\n println(\"inner before\", x);\n x = x + 1;\n println(\"inner after \", x);\n }\n println(\"out of inner\", x);\n // x = 3\n\n if x > 2 {\n x = -1;\n continue;\n }\n\n println(\"never happens\");\n x = x - 1;\n }\n println(\"out of outer\", x);\n}\n\nmain();\n"
input_file: test_programs/loops.blis
---
DisassembledChunk(
Expand All @@ -27,40 +27,12 @@ DisassembledChunk(
]))),
String("twice after "),
String("out of twice"),
Rational(((0, []), (1, [
1,
]))),
String("break outer"),
Rational(((1, [
2,
]), (1, [
1,
]))),
String("break inner"),
String("inner before"),
Rational(((1, [
1,
]), (1, [
1,
]))),
String("inner after "),
String("out of inner"),
Rational(((1, [
2,
]), (1, [
1,
]))),
Rational(((1, [
1,
]), (1, [
1,
]))),
String("never happens"),
Rational(((1, [
1,
]), (1, [
1,
]))),
String("out of outer"),
],
globals: [
Expand Down Expand Up @@ -101,11 +73,11 @@ DisassembledChunk(
Call(2),
Pop,
GetLocal(1),
Constant(6),
Constant(0),
Lt,
JumpFalsePop(20),
GetGlobal(0),
Constant(7),
Constant(6),
GetLocal(1),
Call(2),
Pop,
Expand All @@ -121,11 +93,11 @@ DisassembledChunk(
PopUnderN(0),
Loop(8),
GetLocal(1),
Constant(8),
Constant(1),
Gt,
JumpFalsePop(20),
GetGlobal(0),
Constant(9),
Constant(7),
GetLocal(1),
Call(2),
Pop,
Expand All @@ -136,32 +108,32 @@ DisassembledChunk(
Nil,
Pop,
GetGlobal(0),
Constant(10),
Constant(8),
GetLocal(1),
Call(2),
Pop,
GetLocal(1),
Constant(11),
Constant(3),
Add,
SetLocal(1),
GetGlobal(0),
Constant(12),
Constant(9),
GetLocal(1),
Call(2),
Pop,
Nil,
PopUnderN(0),
Loop(66),
GetGlobal(0),
Constant(13),
Constant(10),
GetLocal(1),
Call(2),
Pop,
GetLocal(1),
Constant(14),
Constant(1),
Gt,
JumpFalsePop(16),
Constant(15),
Constant(3),
Neg,
SetLocal(1),
PopN(0),
Expand All @@ -171,18 +143,18 @@ DisassembledChunk(
Nil,
Pop,
GetGlobal(0),
Constant(16),
Constant(11),
Call(1),
Pop,
GetLocal(1),
Constant(17),
Constant(3),
Sub,
SetLocal(1),
Nil,
PopUnderN(0),
Loop(145),
GetGlobal(0),
Constant(18),
Constant(12),
GetLocal(1),
Call(2),
Pop,
Expand Down
Loading

0 comments on commit 8be2526

Please sign in to comment.