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:
@@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
compile::{Compile, thunk::{Thunk, ThunkBranch}},
|
compile::{Compile, thunk::Thunk},
|
||||||
obj::{prelude::*, reserved::*},
|
obj::{prelude::*, reserved::*},
|
||||||
syn::{ast::*, visit::*},
|
syn::{ast::*, visit::*},
|
||||||
vm::inst::Inst,
|
vm::inst::Inst,
|
||||||
@@ -89,11 +89,11 @@ impl List {
|
|||||||
]);
|
]);
|
||||||
let thunk_true = body.thunkify(compile).into();
|
let thunk_true = body.thunkify(compile).into();
|
||||||
let thunk_false = el.thunkify(compile).into();
|
let thunk_false = el.thunkify(compile).into();
|
||||||
Thunk::Branch(ThunkBranch {
|
Thunk::Branch {
|
||||||
preamble: preamble.into(),
|
preamble: preamble.into(),
|
||||||
thunk_true,
|
thunk_true,
|
||||||
thunk_false,
|
thunk_false,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
List::Lambda { params, expr } => {
|
List::Lambda { params, expr } => {
|
||||||
// TODO(fun) : need captures for functions, built dynamically (or statically?)
|
// TODO(fun) : need captures for functions, built dynamically (or statically?)
|
||||||
|
|||||||
@@ -23,7 +23,11 @@ pub enum Thunk {
|
|||||||
///
|
///
|
||||||
/// Only one of these thunks will be executed. At the end of either thunk, the program will
|
/// Only one of these thunks will be executed. At the end of either thunk, the program will
|
||||||
/// continue at the address following this branch.
|
/// 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.
|
/// 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
|
Thunk::List(thunks) => thunks
|
||||||
.iter()
|
.iter()
|
||||||
.fold(0, |n, thunk| n + thunk.basic_block_count()),
|
.fold(0, |n, thunk| n + thunk.basic_block_count()),
|
||||||
Thunk::Branch(ThunkBranch {
|
Thunk::Branch {
|
||||||
preamble,
|
preamble,
|
||||||
thunk_true,
|
thunk_true,
|
||||||
thunk_false,
|
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
|
// length is thunk, + 1 for branch at the start of the loop
|
||||||
Thunk::Loop(thunk) => thunk.basic_block_count() + 1,
|
Thunk::Loop(thunk) => thunk.basic_block_count() + 1,
|
||||||
Thunk::Nop => 0,
|
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
|
// struct Flatten
|
||||||
//
|
//
|
||||||
@@ -187,11 +181,11 @@ impl Flatten {
|
|||||||
// don't assert_eq here because the "next_block" really should be interpreted as an
|
// don't assert_eq here because the "next_block" really should be interpreted as an
|
||||||
// "exit_block"
|
// "exit_block"
|
||||||
}
|
}
|
||||||
Thunk::Branch(ThunkBranch {
|
Thunk::Branch {
|
||||||
preamble,
|
preamble,
|
||||||
thunk_true,
|
thunk_true,
|
||||||
thunk_false,
|
thunk_false,
|
||||||
}) => {
|
} => {
|
||||||
let preamble_block = self.this_block();
|
let preamble_block = self.this_block();
|
||||||
let branch_block = preamble_block + preamble.basic_block_count();
|
let branch_block = preamble_block + preamble.basic_block_count();
|
||||||
let block_true = branch_block + 1;
|
let block_true = branch_block + 1;
|
||||||
@@ -238,11 +232,11 @@ fn test_flatten_thunk() {
|
|||||||
|
|
||||||
let thunk = Thunk::List(vec![
|
let thunk = Thunk::List(vec![
|
||||||
// branch
|
// branch
|
||||||
Thunk::Branch(ThunkBranch {
|
Thunk::Branch {
|
||||||
preamble: Thunk::Body(init_body.clone()).into(),
|
preamble: Thunk::Body(init_body.clone()).into(),
|
||||||
thunk_true: Thunk::Body(true_body.clone()).into(),
|
thunk_true: Thunk::Body(true_body.clone()).into(),
|
||||||
thunk_false: Thunk::Body(false_body.clone()).into(),
|
thunk_false: Thunk::Body(false_body.clone()).into(),
|
||||||
}),
|
},
|
||||||
// do something after
|
// do something after
|
||||||
Thunk::Body(end_body.clone()),
|
Thunk::Body(end_body.clone()),
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user