From 2f99742e854a2196519ed827706aee8e957b58bc Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Tue, 10 Nov 2020 18:01:55 -0800 Subject: [PATCH] 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 --- src/compile/list.rs | 6 +++--- src/compile/thunk.rs | 28 +++++++++++----------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/compile/list.rs b/src/compile/list.rs index b74a9ff..6273388 100644 --- a/src/compile/list.rs +++ b/src/compile/list.rs @@ -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?) diff --git a/src/compile/thunk.rs b/src/compile/thunk.rs index 2784cdb..325b4cc 100644 --- a/src/compile/thunk.rs +++ b/src/compile/thunk.rs @@ -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_true: Box, + thunk_false: Box, + }, /// 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> for Thunk { } } -// -// struct ThunkBranch -// -#[derive(Debug, Clone, PartialEq)] -pub struct ThunkBranch { - pub(crate) preamble: Box, - pub(crate) thunk_true: Box, - pub(crate) thunk_false: Box, -} - // // 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()), ]);