2021-08-11 14:37:26 +08:00
|
|
|
use crate::{
|
|
|
|
symbol_resolver::SymbolResolver,
|
2021-08-24 17:14:34 +08:00
|
|
|
toplevel::{TopLevelContext, TopLevelDef},
|
2021-08-11 14:37:26 +08:00
|
|
|
typecheck::{
|
2021-08-19 15:30:15 +08:00
|
|
|
type_inferencer::{CodeLocation, PrimitiveStore},
|
2021-08-25 15:29:58 +08:00
|
|
|
typedef::{CallId, FunSignature, SharedUnifier, Type, TypeEnum, Unifier},
|
2021-08-11 14:37:26 +08:00
|
|
|
},
|
|
|
|
};
|
2021-08-13 14:48:46 +08:00
|
|
|
use crossbeam::channel::{unbounded, Receiver, Sender};
|
2021-08-11 14:37:26 +08:00
|
|
|
use inkwell::{
|
|
|
|
basic_block::BasicBlock,
|
|
|
|
builder::Builder,
|
|
|
|
context::Context,
|
|
|
|
module::Module,
|
|
|
|
types::{BasicType, BasicTypeEnum},
|
|
|
|
values::PointerValue,
|
|
|
|
AddressSpace,
|
|
|
|
};
|
|
|
|
use itertools::Itertools;
|
2021-08-13 14:48:46 +08:00
|
|
|
use parking_lot::{Condvar, Mutex};
|
2021-08-12 13:55:15 +08:00
|
|
|
use rustpython_parser::ast::Stmt;
|
2021-08-11 14:37:26 +08:00
|
|
|
use std::collections::HashMap;
|
2021-08-13 16:20:14 +08:00
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
};
|
2021-08-13 14:48:46 +08:00
|
|
|
use std::thread;
|
2021-08-11 14:37:26 +08:00
|
|
|
|
2021-08-05 14:55:23 +08:00
|
|
|
mod expr;
|
2021-08-09 15:39:50 +08:00
|
|
|
mod stmt;
|
2021-08-11 14:37:26 +08:00
|
|
|
|
2021-08-12 13:55:15 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test;
|
|
|
|
|
|
|
|
pub struct CodeGenContext<'ctx, 'a> {
|
2021-08-11 14:37:26 +08:00
|
|
|
pub ctx: &'ctx Context,
|
|
|
|
pub builder: Builder<'ctx>,
|
|
|
|
pub module: Module<'ctx>,
|
2021-08-12 13:55:15 +08:00
|
|
|
pub top_level: &'a TopLevelContext,
|
2021-08-11 14:37:26 +08:00
|
|
|
pub unifier: Unifier,
|
2021-09-08 19:27:32 +08:00
|
|
|
pub resolver: Arc<Box<dyn SymbolResolver + Send + Sync>>,
|
2021-08-11 14:37:26 +08:00
|
|
|
pub var_assignment: HashMap<String, PointerValue<'ctx>>,
|
|
|
|
pub type_cache: HashMap<Type, BasicTypeEnum<'ctx>>,
|
|
|
|
pub primitives: PrimitiveStore,
|
2021-08-19 15:30:15 +08:00
|
|
|
pub calls: HashMap<CodeLocation, CallId>,
|
2021-08-25 15:29:58 +08:00
|
|
|
pub registry: &'a WorkerRegistry,
|
2021-08-11 14:37:26 +08:00
|
|
|
// stores the alloca for variables
|
|
|
|
pub init_bb: BasicBlock<'ctx>,
|
|
|
|
// where continue and break should go to respectively
|
|
|
|
// the first one is the test_bb, and the second one is bb after the loop
|
|
|
|
pub loop_bb: Option<(BasicBlock<'ctx>, BasicBlock<'ctx>)>,
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:48:46 +08:00
|
|
|
type Fp = Box<dyn Fn(&Module) + Send + Sync>;
|
|
|
|
|
|
|
|
pub struct WithCall {
|
|
|
|
fp: Fp,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WithCall {
|
|
|
|
pub fn new(fp: Fp) -> WithCall {
|
|
|
|
WithCall { fp }
|
|
|
|
}
|
|
|
|
|
2021-08-13 16:20:14 +08:00
|
|
|
pub fn run<'ctx>(&self, m: &Module<'ctx>) {
|
2021-08-13 14:48:46 +08:00
|
|
|
(self.fp)(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct WorkerRegistry {
|
|
|
|
sender: Arc<Sender<Option<CodeGenTask>>>,
|
|
|
|
receiver: Arc<Receiver<Option<CodeGenTask>>>,
|
2021-08-13 16:20:14 +08:00
|
|
|
panicked: AtomicBool,
|
2021-08-13 14:48:46 +08:00
|
|
|
task_count: Mutex<usize>,
|
|
|
|
thread_count: usize,
|
|
|
|
wait_condvar: Condvar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorkerRegistry {
|
|
|
|
pub fn create_workers(
|
|
|
|
names: &[&str],
|
|
|
|
top_level_ctx: Arc<TopLevelContext>,
|
|
|
|
f: Arc<WithCall>,
|
2021-08-13 16:20:14 +08:00
|
|
|
) -> (Arc<WorkerRegistry>, Vec<thread::JoinHandle<()>>) {
|
2021-08-13 14:48:46 +08:00
|
|
|
let (sender, receiver) = unbounded();
|
|
|
|
let task_count = Mutex::new(0);
|
|
|
|
let wait_condvar = Condvar::new();
|
|
|
|
|
|
|
|
let registry = Arc::new(WorkerRegistry {
|
|
|
|
sender: Arc::new(sender),
|
|
|
|
receiver: Arc::new(receiver),
|
|
|
|
thread_count: names.len(),
|
2021-08-13 16:20:14 +08:00
|
|
|
panicked: AtomicBool::new(false),
|
2021-08-13 14:48:46 +08:00
|
|
|
task_count,
|
|
|
|
wait_condvar,
|
|
|
|
});
|
|
|
|
|
2021-08-13 16:20:14 +08:00
|
|
|
let mut handles = Vec::new();
|
2021-08-13 14:48:46 +08:00
|
|
|
for name in names.iter() {
|
|
|
|
let top_level_ctx = top_level_ctx.clone();
|
|
|
|
let registry = registry.clone();
|
2021-08-13 16:20:14 +08:00
|
|
|
let registry2 = registry.clone();
|
2021-08-13 14:48:46 +08:00
|
|
|
let name = name.to_string();
|
|
|
|
let f = f.clone();
|
2021-08-13 16:20:14 +08:00
|
|
|
let handle = thread::spawn(move || {
|
2021-08-13 14:48:46 +08:00
|
|
|
registry.worker_thread(name, top_level_ctx, f);
|
|
|
|
});
|
2021-08-13 16:20:14 +08:00
|
|
|
let handle = thread::spawn(move || {
|
|
|
|
if let Err(e) = handle.join() {
|
|
|
|
if let Some(e) = e.downcast_ref::<&'static str>() {
|
|
|
|
eprintln!("Got an error: {}", e);
|
|
|
|
} else {
|
|
|
|
eprintln!("Got an unknown error: {:?}", e);
|
|
|
|
}
|
|
|
|
registry2.panicked.store(true, Ordering::SeqCst);
|
|
|
|
registry2.wait_condvar.notify_all();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
handles.push(handle);
|
2021-08-13 14:48:46 +08:00
|
|
|
}
|
2021-08-13 16:20:14 +08:00
|
|
|
(registry, handles)
|
2021-08-13 14:48:46 +08:00
|
|
|
}
|
|
|
|
|
2021-08-13 16:20:14 +08:00
|
|
|
pub fn wait_tasks_complete(&self, handles: Vec<thread::JoinHandle<()>>) {
|
2021-08-13 14:48:46 +08:00
|
|
|
{
|
|
|
|
let mut count = self.task_count.lock();
|
|
|
|
while *count != 0 {
|
2021-08-13 16:20:14 +08:00
|
|
|
if self.panicked.load(Ordering::SeqCst) {
|
|
|
|
break;
|
|
|
|
}
|
2021-08-13 14:48:46 +08:00
|
|
|
self.wait_condvar.wait(&mut count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _ in 0..self.thread_count {
|
|
|
|
self.sender.send(None).unwrap();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut count = self.task_count.lock();
|
|
|
|
while *count != self.thread_count {
|
2021-08-13 16:20:14 +08:00
|
|
|
if self.panicked.load(Ordering::SeqCst) {
|
|
|
|
break;
|
|
|
|
}
|
2021-08-13 14:48:46 +08:00
|
|
|
self.wait_condvar.wait(&mut count);
|
|
|
|
}
|
|
|
|
}
|
2021-08-13 16:20:14 +08:00
|
|
|
for handle in handles {
|
|
|
|
handle.join().unwrap();
|
|
|
|
}
|
|
|
|
if self.panicked.load(Ordering::SeqCst) {
|
|
|
|
panic!("tasks panicked");
|
|
|
|
}
|
2021-08-13 14:48:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_task(&self, task: CodeGenTask) {
|
|
|
|
*self.task_count.lock() += 1;
|
|
|
|
self.sender.send(Some(task)).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn worker_thread(
|
|
|
|
&self,
|
|
|
|
module_name: String,
|
|
|
|
top_level_ctx: Arc<TopLevelContext>,
|
|
|
|
f: Arc<WithCall>,
|
|
|
|
) {
|
|
|
|
let context = Context::create();
|
|
|
|
let mut builder = context.create_builder();
|
|
|
|
let mut module = context.create_module(&module_name);
|
|
|
|
|
|
|
|
while let Some(task) = self.receiver.recv().unwrap() {
|
2021-08-25 15:29:58 +08:00
|
|
|
let result = gen_func(&context, self, builder, module, task, top_level_ctx.clone());
|
2021-08-13 14:48:46 +08:00
|
|
|
builder = result.0;
|
|
|
|
module = result.1;
|
|
|
|
*self.task_count.lock() -= 1;
|
|
|
|
self.wait_condvar.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
// do whatever...
|
|
|
|
module.verify().unwrap();
|
|
|
|
f.run(&module);
|
2021-09-16 21:36:42 +08:00
|
|
|
let mut lock = self.task_count.lock();
|
2021-08-13 14:48:46 +08:00
|
|
|
*lock += 1;
|
|
|
|
self.wait_condvar.notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 14:37:26 +08:00
|
|
|
pub struct CodeGenTask {
|
|
|
|
pub subst: Vec<(Type, Type)>,
|
|
|
|
pub symbol_name: String,
|
|
|
|
pub signature: FunSignature,
|
2021-08-12 13:55:15 +08:00
|
|
|
pub body: Vec<Stmt<Option<Type>>>,
|
2021-08-19 15:30:15 +08:00
|
|
|
pub calls: HashMap<CodeLocation, CallId>,
|
2021-08-25 15:29:58 +08:00
|
|
|
pub unifier: (SharedUnifier, PrimitiveStore),
|
2021-09-08 19:27:32 +08:00
|
|
|
pub resolver: Arc<Box<dyn SymbolResolver + Send + Sync>>,
|
2021-08-11 14:37:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_llvm_type<'ctx>(
|
|
|
|
ctx: &'ctx Context,
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
top_level: &TopLevelContext,
|
|
|
|
type_cache: &mut HashMap<Type, BasicTypeEnum<'ctx>>,
|
|
|
|
ty: Type,
|
|
|
|
) -> BasicTypeEnum<'ctx> {
|
|
|
|
use TypeEnum::*;
|
|
|
|
// we assume the type cache should already contain primitive types,
|
|
|
|
// and they should be passed by value instead of passing as pointer.
|
2021-08-12 16:36:23 +08:00
|
|
|
type_cache.get(&unifier.get_representative(ty)).cloned().unwrap_or_else(|| {
|
|
|
|
match &*unifier.get_ty(ty) {
|
|
|
|
TObj { obj_id, fields, .. } => {
|
|
|
|
// a struct with fields in the order of declaration
|
2021-08-23 10:34:11 +08:00
|
|
|
let top_level_defs = top_level.definitions.read();
|
|
|
|
let definition = top_level_defs.get(obj_id.0).unwrap();
|
2021-08-12 16:36:23 +08:00
|
|
|
let ty = if let TopLevelDef::Class { fields: fields_list, .. } = &*definition.read()
|
|
|
|
{
|
|
|
|
let fields = fields.borrow();
|
|
|
|
let fields = fields_list
|
|
|
|
.iter()
|
|
|
|
.map(|f| get_llvm_type(ctx, unifier, top_level, type_cache, fields[&f.0]))
|
|
|
|
.collect_vec();
|
|
|
|
ctx.struct_type(&fields, false).ptr_type(AddressSpace::Generic).into()
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
TTuple { ty } => {
|
|
|
|
// a struct with fields in the order present in the tuple
|
|
|
|
let fields = ty
|
2021-08-11 14:37:26 +08:00
|
|
|
.iter()
|
2021-08-12 16:36:23 +08:00
|
|
|
.map(|ty| get_llvm_type(ctx, unifier, top_level, type_cache, *ty))
|
2021-08-11 14:37:26 +08:00
|
|
|
.collect_vec();
|
|
|
|
ctx.struct_type(&fields, false).ptr_type(AddressSpace::Generic).into()
|
2021-08-12 16:36:23 +08:00
|
|
|
}
|
|
|
|
TList { ty } => {
|
|
|
|
// a struct with an integer and a pointer to an array
|
|
|
|
let element_type = get_llvm_type(ctx, unifier, top_level, type_cache, *ty);
|
|
|
|
let fields =
|
|
|
|
[ctx.i32_type().into(), element_type.ptr_type(AddressSpace::Generic).into()];
|
|
|
|
ctx.struct_type(&fields, false).ptr_type(AddressSpace::Generic).into()
|
|
|
|
}
|
|
|
|
TVirtual { .. } => unimplemented!(),
|
|
|
|
_ => unreachable!(),
|
2021-08-11 14:37:26 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-12 16:36:23 +08:00
|
|
|
pub fn gen_func<'ctx>(
|
|
|
|
context: &'ctx Context,
|
2021-08-25 15:29:58 +08:00
|
|
|
registry: &WorkerRegistry,
|
2021-08-12 16:36:23 +08:00
|
|
|
builder: Builder<'ctx>,
|
|
|
|
module: Module<'ctx>,
|
|
|
|
task: CodeGenTask,
|
|
|
|
top_level_ctx: Arc<TopLevelContext>,
|
2021-08-13 13:33:59 +08:00
|
|
|
) -> (Builder<'ctx>, Module<'ctx>) {
|
2021-08-11 14:37:26 +08:00
|
|
|
// unwrap_or(0) is for unit tests without using rayon
|
|
|
|
let (mut unifier, primitives) = {
|
2021-08-25 15:29:58 +08:00
|
|
|
let (unifier, primitives) = task.unifier;
|
|
|
|
(Unifier::from_shared_unifier(&unifier), primitives)
|
2021-08-11 14:37:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
for (a, b) in task.subst.iter() {
|
|
|
|
// this should be unification between variables and concrete types
|
|
|
|
// and should not cause any problem...
|
|
|
|
unifier.unify(*a, *b).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// rebuild primitive store with unique representatives
|
|
|
|
let primitives = PrimitiveStore {
|
|
|
|
int32: unifier.get_representative(primitives.int32),
|
|
|
|
int64: unifier.get_representative(primitives.int64),
|
|
|
|
float: unifier.get_representative(primitives.float),
|
|
|
|
bool: unifier.get_representative(primitives.bool),
|
|
|
|
none: unifier.get_representative(primitives.none),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut type_cache: HashMap<_, _> = [
|
2021-08-12 13:55:15 +08:00
|
|
|
(unifier.get_representative(primitives.int32), context.i32_type().into()),
|
|
|
|
(unifier.get_representative(primitives.int64), context.i64_type().into()),
|
|
|
|
(unifier.get_representative(primitives.float), context.f64_type().into()),
|
|
|
|
(unifier.get_representative(primitives.bool), context.bool_type().into()),
|
2021-08-11 14:37:26 +08:00
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let params = task
|
|
|
|
.signature
|
|
|
|
.args
|
|
|
|
.iter()
|
|
|
|
.map(|arg| {
|
|
|
|
get_llvm_type(&context, &mut unifier, top_level_ctx.as_ref(), &mut type_cache, arg.ty)
|
|
|
|
})
|
|
|
|
.collect_vec();
|
|
|
|
|
|
|
|
let fn_type = if unifier.unioned(task.signature.ret, primitives.none) {
|
|
|
|
context.void_type().fn_type(¶ms, false)
|
|
|
|
} else {
|
|
|
|
get_llvm_type(
|
|
|
|
&context,
|
|
|
|
&mut unifier,
|
|
|
|
top_level_ctx.as_ref(),
|
|
|
|
&mut type_cache,
|
|
|
|
task.signature.ret,
|
|
|
|
)
|
|
|
|
.fn_type(¶ms, false)
|
|
|
|
};
|
|
|
|
|
2021-08-27 16:25:59 +08:00
|
|
|
let symbol = &task.symbol_name;
|
2021-09-08 19:45:36 +08:00
|
|
|
let fn_val =
|
|
|
|
module.get_function(symbol).unwrap_or_else(|| module.add_function(symbol, fn_type, None));
|
2021-08-27 16:25:59 +08:00
|
|
|
|
2021-08-11 14:37:26 +08:00
|
|
|
let init_bb = context.append_basic_block(fn_val, "init");
|
|
|
|
builder.position_at_end(init_bb);
|
|
|
|
let body_bb = context.append_basic_block(fn_val, "body");
|
|
|
|
|
|
|
|
let mut var_assignment = HashMap::new();
|
|
|
|
for (n, arg) in task.signature.args.iter().enumerate() {
|
|
|
|
let param = fn_val.get_nth_param(n as u32).unwrap();
|
|
|
|
let alloca = builder.build_alloca(
|
|
|
|
get_llvm_type(&context, &mut unifier, top_level_ctx.as_ref(), &mut type_cache, arg.ty),
|
|
|
|
&arg.name,
|
|
|
|
);
|
|
|
|
builder.build_store(alloca, param);
|
|
|
|
var_assignment.insert(arg.name.clone(), alloca);
|
|
|
|
}
|
|
|
|
builder.build_unconditional_branch(body_bb);
|
|
|
|
builder.position_at_end(body_bb);
|
|
|
|
|
|
|
|
let mut code_gen_context = CodeGenContext {
|
|
|
|
ctx: &context,
|
|
|
|
resolver: task.resolver,
|
|
|
|
top_level: top_level_ctx.as_ref(),
|
2021-08-19 15:30:15 +08:00
|
|
|
calls: task.calls,
|
2021-08-11 14:37:26 +08:00
|
|
|
loop_bb: None,
|
2021-08-25 15:29:58 +08:00
|
|
|
registry,
|
2021-08-11 14:37:26 +08:00
|
|
|
var_assignment,
|
|
|
|
type_cache,
|
|
|
|
primitives,
|
|
|
|
init_bb,
|
|
|
|
builder,
|
|
|
|
module,
|
|
|
|
unifier,
|
|
|
|
};
|
|
|
|
|
2021-08-12 13:55:15 +08:00
|
|
|
for stmt in task.body.iter() {
|
|
|
|
code_gen_context.gen_stmt(stmt);
|
2021-08-11 14:37:26 +08:00
|
|
|
}
|
2021-08-12 13:55:15 +08:00
|
|
|
|
2021-08-13 13:33:59 +08:00
|
|
|
let CodeGenContext { builder, module, .. } = code_gen_context;
|
|
|
|
|
|
|
|
(builder, module)
|
2021-08-11 14:37:26 +08:00
|
|
|
}
|