Move ThunkBranch struct impl into Thunk::Branch

ThunkBranch standalone struct was not as useful as I was expecting it
would be. Its logic is now directly stored in Thunk::Branch variant

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-11-10 18:01:55 -08:00
parent 004f2b91f8
commit 2f99742e85
2 changed files with 14 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
use crate::{
compile::{Compile, thunk::{Thunk, ThunkBranch}},
compile::{Compile, thunk::Thunk},
obj::{prelude::*, reserved::*},
syn::{ast::*, visit::*},
vm::inst::Inst,
@@ -89,11 +89,11 @@ impl List {
]);
let thunk_true = body.thunkify(compile).into();
let thunk_false = el.thunkify(compile).into();
Thunk::Branch(ThunkBranch {
Thunk::Branch {
preamble: preamble.into(),
thunk_true,
thunk_false,
})
}
}
List::Lambda { params, expr } => {
// TODO(fun) : need captures for functions, built dynamically (or statically?)

View File

@@ -23,7 +23,11 @@ pub enum Thunk {
///
/// Only one of these thunks will be executed. At the end of either thunk, the program will
/// continue at the address following this branch.
Branch(ThunkBranch),
Branch {
preamble: Box<Thunk>,
thunk_true: Box<Thunk>,
thunk_false: Box<Thunk>,
},
/// Based on the conditional flag in the VM, code for this loop will continue to execute.
///
@@ -89,11 +93,11 @@ impl Thunk {
Thunk::List(thunks) => thunks
.iter()
.fold(0, |n, thunk| n + thunk.basic_block_count()),
Thunk::Branch(ThunkBranch {
Thunk::Branch {
preamble,
thunk_true,
thunk_false,
}) => preamble.basic_block_count() + thunk_true.basic_block_count() + thunk_false.basic_block_count() + 1,
} => preamble.basic_block_count() + thunk_true.basic_block_count() + thunk_false.basic_block_count() + 1,
// length is thunk, + 1 for branch at the start of the loop
Thunk::Loop(thunk) => thunk.basic_block_count() + 1,
Thunk::Nop => 0,
@@ -123,16 +127,6 @@ impl From<Vec<Thunk>> for Thunk {
}
}
//
// struct ThunkBranch
//
#[derive(Debug, Clone, PartialEq)]
pub struct ThunkBranch {
pub(crate) preamble: Box<Thunk>,
pub(crate) thunk_true: Box<Thunk>,
pub(crate) thunk_false: Box<Thunk>,
}
//
// struct Flatten
//
@@ -187,11 +181,11 @@ impl Flatten {
// don't assert_eq here because the "next_block" really should be interpreted as an
// "exit_block"
}
Thunk::Branch(ThunkBranch {
Thunk::Branch {
preamble,
thunk_true,
thunk_false,
}) => {
} => {
let preamble_block = self.this_block();
let branch_block = preamble_block + preamble.basic_block_count();
let block_true = branch_block + 1;
@@ -238,11 +232,11 @@ fn test_flatten_thunk() {
let thunk = Thunk::List(vec![
// branch
Thunk::Branch(ThunkBranch {
Thunk::Branch {
preamble: Thunk::Body(init_body.clone()).into(),
thunk_true: Thunk::Body(true_body.clone()).into(),
thunk_false: Thunk::Body(false_body.clone()).into(),
}),
},
// do something after
Thunk::Body(end_body.clone()),
]);