Add basic blocks and implementation of flattening thunks -> basic blocks
* Basic are a more linear way of representing code. Thunks beget basic blocks, which beget vectors of instructions. * Basic blocks are also being flattened into a vector of instructions (hopefully, no tests done yet) * OH yeah locals can be collected too (but currently are not being collected in the compiler, that should come soon) Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -1,17 +1,28 @@
|
||||
pub mod thunk;
|
||||
pub mod basic_block;
|
||||
pub mod error;
|
||||
mod locals;
|
||||
pub mod thunk;
|
||||
|
||||
use crate::{obj::prelude::*, vm::consts::*};
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
|
||||
pub struct Compile<'t> {
|
||||
text: &'t str,
|
||||
const_data: ConstData,
|
||||
globals: BTreeMap<Sym, Local>,
|
||||
locals: Vec<BTreeMap<Sym, Local>>,
|
||||
next_local: Local,
|
||||
}
|
||||
|
||||
impl<'t> Compile<'t> {
|
||||
pub fn new(text: &'t str) -> Self {
|
||||
Compile { text, const_data: Default::default() }
|
||||
Compile {
|
||||
text,
|
||||
const_data: Default::default(),
|
||||
globals: Default::default(),
|
||||
locals: Default::default(),
|
||||
next_local: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn text(&self) -> &'t str {
|
||||
@@ -28,14 +39,62 @@ impl<'t> Compile<'t> {
|
||||
|
||||
/// Gets or inserts a static int reference.
|
||||
pub fn const_int(&mut self, int: i64) -> (ConstHandle, IntRef) {
|
||||
self.const_data_mut()
|
||||
.const_int(int)
|
||||
self.const_data_mut().const_int(int)
|
||||
}
|
||||
|
||||
/// Gets or inserts a static string reference.
|
||||
pub fn const_str(&mut self, s: impl AsRef<str>) -> (ConstHandle, StrRef) {
|
||||
self.const_data_mut()
|
||||
.const_str(s)
|
||||
self.const_data_mut().const_str(s)
|
||||
}
|
||||
|
||||
/// Looks up a variable name.
|
||||
///
|
||||
/// This will search up the locals stack until the given name is found, ultimately ending with
|
||||
/// a global name lookup.
|
||||
pub fn lookup_scope(&mut self, sym: Sym) -> Option<Local> {
|
||||
self.locals
|
||||
.iter()
|
||||
.rev()
|
||||
.filter_map(|locals| locals.get(&sym))
|
||||
.next()
|
||||
.or_else(|| self.globals.get(&sym))
|
||||
.copied()
|
||||
}
|
||||
|
||||
/// Creates a new local variable if it does not exist in the current local scope.
|
||||
pub fn create_local(&mut self, sym: Sym) -> Local {
|
||||
let locals = self.locals.last_mut().expect("scope");
|
||||
if let Some(local) = locals.get(&sym) {
|
||||
*local
|
||||
} else {
|
||||
// wish I could use mem::replace here, oh well
|
||||
let local = self.next_local;
|
||||
self.next_local = self.next_local.next();
|
||||
locals.insert(sym, local);
|
||||
local
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new global variable if it does not exist in the current global scope.
|
||||
pub fn create_global(&mut self, sym: Sym) -> Local {
|
||||
if let Some(global) = self.globals.get(&sym) {
|
||||
*global
|
||||
} else {
|
||||
let global = self.next_local;
|
||||
self.next_local = self.next_local.next();
|
||||
self.globals.insert(sym, global);
|
||||
global
|
||||
}
|
||||
}
|
||||
|
||||
/// Pushes an empty scope layer of local variables.
|
||||
pub fn push_scope_layer(&mut self) {
|
||||
self.locals.push(Default::default());
|
||||
}
|
||||
|
||||
/// Pops a scope layer of local variables, if any are available.
|
||||
pub fn pop_scope_layer(&mut self) -> Option<BTreeMap<Sym, Local>> {
|
||||
self.locals.pop()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user