Skip to content

Commit

Permalink
Removed ScopeBuffer as that was deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
TrolledWoods committed Sep 10, 2020
1 parent b40444b commit 43ca5ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 38 deletions.
27 changes: 13 additions & 14 deletions src/code_gen.rs
Expand Up @@ -65,15 +65,14 @@ pub type LocalId = usize;

pub fn compile_expression(
ast: &Ast,
scopes: &Scopes,
scopes: &mut Scopes,
resources: &Resources,
) -> (Locals, Vec<Instruction>, Value) {
let mut locals = Locals::new();
let mut node_values: Vec<Value> = Vec::with_capacity(ast.nodes.len());
let mut instructions = Vec::new();

let mut temporary_labels: Vec<(_, _, Option<Value>)> = Vec::new();
let mut storage_locations = scopes.create_buffer(|| None);

let mut function_arg_locations = Vec::new();

Expand All @@ -85,37 +84,37 @@ pub fn compile_expression(

match node.kind {
NodeKind::Identifier(member_id) => {
let member = match storage_locations.member(member_id) {
Some(value) => *value,
let member = match scopes.member(member_id).storage_loc {
Some(value) => value,
None => panic!("Invalid thing, \nLocals: {:?}, \nScopes: {:?}, \nInstructions: {:?}", locals, scopes, instructions),
};

node_values.push(member);
node_values.push(Value::Local(member));
}
NodeKind::DeclareFunctionArgument { variable_name, .. } => {
// Declaring a function argument is like moving the responsibility of setting
// the locals to the caller. This should be done by the 'call' instruction,
// which will set all the affected locals to the appropriate values.
let location = Value::Local(locals.alloc_custom(Local {
let location = locals.alloc_custom(Local {
n_uses: 0,
scope_member: Some(variable_name),
}));
});

*storage_locations.member_mut(variable_name) = Some(location);
function_arg_locations.push(location);
scopes.member_mut(variable_name).storage_loc = Some(location);
function_arg_locations.push(Value::Local(location));
node_values.push(Value::Poison);
}
NodeKind::Declaration { variable_name, value } => {
let location = Value::Local(locals.alloc_custom(Local {
let location = locals.alloc_custom(Local {
n_uses: 0,
scope_member: Some(variable_name),
}));
*storage_locations.member_mut(variable_name) = Some(location);
});
scopes.member_mut(variable_name).storage_loc = Some(location);

let input = node_values[value as usize];
locals.note_usage(&location);
locals.note_usage(&Value::Local(location));
locals.note_usage(&input);
push_instr!(instructions, Instruction::MoveU64(location, input));
push_instr!(instructions, Instruction::MoveU64(Value::Local(location), input));
node_values.push(Value::Poison);
}
NodeKind::Skip { label, value } => {
Expand Down
26 changes: 2 additions & 24 deletions src/parser.rs
Expand Up @@ -622,22 +622,6 @@ impl ScopeMemberId {
}
}

/// A buffer that stores some data 'T' for every member in the
/// scope.
pub struct ScopeBuffer<T> {
data: Vec<T>,
}

impl<T> ScopeBuffer<T> {
pub fn member(&self, member_id: ScopeMemberId) -> &T {
&self.data[member_id.get()]
}

pub fn member_mut(&mut self, member_id: ScopeMemberId) -> &mut T {
&mut self.data[member_id.get()]
}
}

/// Scopes contains all the scopes for a routine. A single routine has its own local scope,
/// because that makes it easy to duplicate the scope data for polymorphism and such.
///
Expand All @@ -653,14 +637,6 @@ pub struct Scopes {
impl Scopes {
pub fn new() -> Self { Default::default() }

pub fn create_buffer<T>(&self, mut default: impl FnMut() -> T) -> ScopeBuffer<T> {
ScopeBuffer {
data: (0..self.members.len())
.map(|_| default())
.collect(),
}
}

pub fn create_scope(&mut self, parent: Option<ScopeId>) -> ScopeId {
let id = self.scopes.len();
self.scopes.push(Scope { parent, .. Default::default() });
Expand Down Expand Up @@ -714,6 +690,7 @@ impl Scopes {
name,
kind,
type_: None,
storage_loc: None,
};

let scope_instance = &mut self.scopes[scope.get()];
Expand Down Expand Up @@ -746,4 +723,5 @@ pub struct ScopeMember {
pub kind: ScopeMemberKind,
pub declaration_location: CodeLoc,
pub type_: Option<TypeId>,
pub storage_loc: Option<crate::code_gen::LocalId>,
}

0 comments on commit 43ca5ec

Please sign in to comment.