2020-09-16 17:18:31 -07:00
|
|
|
pub mod basic_block;
|
2020-09-14 14:09:29 -07:00
|
|
|
pub mod error;
|
2020-09-16 17:18:31 -07:00
|
|
|
mod locals;
|
2020-09-27 19:33:18 -07:00
|
|
|
mod scope;
|
2020-09-16 17:18:31 -07:00
|
|
|
pub mod thunk;
|
2020-09-14 14:09:29 -07:00
|
|
|
|
2020-09-27 19:33:18 -07:00
|
|
|
use crate::{syn::ast::Body, obj::prelude::*, vm::consts::*};
|
|
|
|
|
use scope::*;
|
2020-09-26 18:31:23 -07:00
|
|
|
use std::collections::HashMap;
|
2020-09-14 14:09:29 -07:00
|
|
|
|
2020-09-16 18:01:40 -07:00
|
|
|
#[derive(Default)]
|
2020-09-16 17:36:40 -07:00
|
|
|
pub struct Compile {
|
2020-09-14 14:09:29 -07:00
|
|
|
const_data: ConstData,
|
2020-09-27 19:33:18 -07:00
|
|
|
scope: Scope,
|
2020-09-14 14:09:29 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-16 17:36:40 -07:00
|
|
|
impl Compile {
|
|
|
|
|
/// Creates a new compiler using the given text.
|
|
|
|
|
pub fn new() -> Self {
|
2020-09-16 18:01:40 -07:00
|
|
|
Default::default()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Compiles the given AST body.
|
2020-09-27 19:33:18 -07:00
|
|
|
pub fn compile(mut self, body: &Body) -> error::Result<(ConstPool, UserFunRef)> {
|
|
|
|
|
self.push_scope_layer();
|
2020-10-07 15:48:24 -07:00
|
|
|
let mut main = thunk::CompileBody::new(&mut self)
|
2020-09-16 18:01:40 -07:00
|
|
|
.compile(body)?
|
|
|
|
|
.flatten()
|
|
|
|
|
.to_vec();
|
2020-10-07 15:48:24 -07:00
|
|
|
// XXX TODO(compile)
|
|
|
|
|
// remove this when we get returns implemented
|
|
|
|
|
main.push(crate::vm::inst::Inst::PushSym(crate::obj::reserved::NIL_NAME.sym));
|
|
|
|
|
main.push(crate::vm::inst::Inst::Return);
|
2020-09-27 19:33:18 -07:00
|
|
|
let globals_syms: std::collections::BTreeMap<_, _> = self.pop_scope_layer().unwrap()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(sym, name)| (name, sym))
|
|
|
|
|
.collect();
|
|
|
|
|
let globals = globals_syms.into_iter()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.map(|(index, (name, sym))| {
|
|
|
|
|
assert_eq!(index, name.index());
|
|
|
|
|
sym
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
Ok((self.const_data.const_pool, UserFun::new_obj(main, globals)))
|
2020-09-14 14:09:29 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-16 17:36:40 -07:00
|
|
|
/// Gets the constant data that is interned in this compile session.
|
2020-09-14 14:09:29 -07:00
|
|
|
pub fn const_data(&self) -> &ConstData {
|
|
|
|
|
&self.const_data
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-16 17:36:40 -07:00
|
|
|
/// Mutably gets the constant data that is interned in this compile session.
|
2020-09-14 14:09:29 -07:00
|
|
|
pub fn const_data_mut(&mut self) -> &mut ConstData {
|
|
|
|
|
&mut self.const_data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets or inserts a static int reference.
|
|
|
|
|
pub fn const_int(&mut self, int: i64) -> (ConstHandle, IntRef) {
|
2020-09-16 17:18:31 -07:00
|
|
|
self.const_data_mut().const_int(int)
|
2020-09-14 14:09:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets or inserts a static string reference.
|
|
|
|
|
pub fn const_str(&mut self, s: impl AsRef<str>) -> (ConstHandle, StrRef) {
|
2020-09-16 17:18:31 -07:00
|
|
|
self.const_data_mut().const_str(s)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-26 18:31:23 -07:00
|
|
|
/// Inserts a constant object without deduplication.
|
|
|
|
|
pub fn push_const(&mut self, obj: ObjRef) -> (ConstHandle, ObjRef) {
|
|
|
|
|
self.const_data_mut().push(obj)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 19:33:18 -07:00
|
|
|
/// Looks up a local variable name.
|
|
|
|
|
pub fn lookup_local(&mut self, sym: Sym) -> Option<Name> {
|
|
|
|
|
self.scope.lookup_local(sym)
|
2020-09-16 17:18:31 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-27 19:33:18 -07:00
|
|
|
/// Looks up a global variable name.
|
|
|
|
|
pub fn lookup_global(&mut self, sym: Sym) -> Option<Name> {
|
|
|
|
|
self.scope.lookup_global(sym)
|
2020-09-17 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-16 17:18:31 -07:00
|
|
|
/// Creates a new local variable if it does not exist in the current local scope.
|
2020-09-18 13:57:51 -07:00
|
|
|
pub(crate) fn create_local(&mut self, sym: Sym) -> Name {
|
2020-09-27 19:33:18 -07:00
|
|
|
self.scope.create_local(sym)
|
2020-09-16 17:18:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new global variable if it does not exist in the current global scope.
|
2020-09-18 13:57:51 -07:00
|
|
|
pub fn create_global(&mut self, sym: Sym) -> Name {
|
2020-09-27 19:33:18 -07:00
|
|
|
self.scope.create_global(sym)
|
2020-09-16 17:18:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Pushes an empty scope layer of local variables.
|
2020-09-27 19:33:18 -07:00
|
|
|
pub fn push_scope_layer(&mut self) {
|
|
|
|
|
self.scope.push_layer()
|
2020-09-16 17:18:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Pops a scope layer of local variables, if any are available.
|
2020-09-27 19:33:18 -07:00
|
|
|
pub fn pop_scope_layer(&mut self) -> Option<ScopeLocalSyms> {
|
|
|
|
|
self.scope.pop_layer()
|
2020-09-14 14:09:29 -07:00
|
|
|
}
|
2020-09-16 17:27:33 -07:00
|
|
|
|
|
|
|
|
/// Collects local variables for the given AST body non-recursively.
|
2020-09-16 17:36:40 -07:00
|
|
|
pub(crate) fn collect_locals(&mut self, body: &Body) {
|
2020-09-16 17:27:33 -07:00
|
|
|
locals::CollectLocals::new(self).collect(body);
|
|
|
|
|
}
|
2020-09-14 14:09:29 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-18 15:45:54 -07:00
|
|
|
/// Constant data pool used for building the const pool.
|
|
|
|
|
///
|
|
|
|
|
/// This distinguishes between the different types while building a const pool to avoid duplicates.
|
2020-09-14 14:09:29 -07:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
pub struct ConstData {
|
|
|
|
|
ints: HashMap<i64, (ConstHandle, IntRef)>,
|
|
|
|
|
strs: HashMap<String, (ConstHandle, StrRef)>,
|
|
|
|
|
const_pool: ConstPool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ConstData {
|
|
|
|
|
/// Gets or inserts a static int reference.
|
|
|
|
|
pub fn const_int(&mut self, int: i64) -> (ConstHandle, IntRef) {
|
|
|
|
|
if let Some(pair) = self.ints.get(&int) {
|
|
|
|
|
pair.clone()
|
|
|
|
|
} else {
|
|
|
|
|
let obj = Int::new_obj(int);
|
2020-09-26 18:31:23 -07:00
|
|
|
let (hdl, _) = self.push(obj.clone());
|
2020-09-14 14:09:29 -07:00
|
|
|
self.ints.insert(int, (hdl, obj.clone()));
|
|
|
|
|
(hdl, obj)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets or inserts a static string reference.
|
|
|
|
|
pub fn const_str(&mut self, s: impl AsRef<str>) -> (ConstHandle, StrRef) {
|
|
|
|
|
let s = s.as_ref();
|
|
|
|
|
if let Some(pair) = self.strs.get(s) {
|
|
|
|
|
pair.clone()
|
|
|
|
|
} else {
|
|
|
|
|
let obj = Str::new_obj(s.to_string());
|
2020-09-26 18:31:23 -07:00
|
|
|
let (hdl, _) = self.push(obj.clone());
|
2020-09-14 14:09:29 -07:00
|
|
|
self.strs.insert(s.to_string(), (hdl, obj.clone()));
|
|
|
|
|
(hdl, obj)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-26 18:31:23 -07:00
|
|
|
|
|
|
|
|
/// Inserts a user-defined function constant object.
|
|
|
|
|
///
|
|
|
|
|
/// This function provides no de-duplication. Use `const_str` or `const_int` for objects that
|
|
|
|
|
/// may be repeated.
|
|
|
|
|
pub fn push(&mut self, obj: ObjRef) -> (ConstHandle, ObjRef) {
|
|
|
|
|
let hdl = self.const_pool.push(obj.clone());
|
|
|
|
|
(hdl, obj)
|
|
|
|
|
}
|
2020-09-14 14:09:29 -07:00
|
|
|
}
|