2021-08-19 17:14:38 +08:00
|
|
|
use super::{
|
|
|
|
expr::{assert_int_val, assert_pointer_val},
|
|
|
|
CodeGenContext,
|
|
|
|
};
|
2021-08-11 14:37:26 +08:00
|
|
|
use crate::typecheck::typedef::Type;
|
|
|
|
use inkwell::values::{BasicValue, BasicValueEnum, PointerValue};
|
2021-08-09 15:39:50 +08:00
|
|
|
use rustpython_parser::ast::{Expr, ExprKind, Stmt, StmtKind};
|
|
|
|
|
2021-08-12 13:55:15 +08:00
|
|
|
impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
2021-08-09 15:39:50 +08:00
|
|
|
fn gen_var(&mut self, ty: Type) -> PointerValue<'ctx> {
|
2021-08-09 16:37:28 +08:00
|
|
|
// put the alloca in init block
|
|
|
|
let current = self.builder.get_insert_block().unwrap();
|
2021-08-11 14:37:26 +08:00
|
|
|
// position before the last branching instruction...
|
|
|
|
self.builder.position_before(&self.init_bb.get_last_instruction().unwrap());
|
2021-08-09 15:39:50 +08:00
|
|
|
let ty = self.get_llvm_type(ty);
|
2021-08-09 16:37:28 +08:00
|
|
|
let ptr = self.builder.build_alloca(ty, "tmp");
|
|
|
|
self.builder.position_at_end(current);
|
|
|
|
ptr
|
2021-08-09 15:39:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_pattern(&mut self, pattern: &Expr<Option<Type>>) -> PointerValue<'ctx> {
|
|
|
|
// very similar to gen_expr, but we don't do an extra load at the end
|
|
|
|
// and we flatten nested tuples
|
|
|
|
match &pattern.node {
|
|
|
|
ExprKind::Name { id, .. } => {
|
|
|
|
self.var_assignment.get(id).cloned().unwrap_or_else(|| {
|
|
|
|
let ptr = self.gen_var(pattern.custom.unwrap());
|
|
|
|
self.var_assignment.insert(id.clone(), ptr);
|
|
|
|
ptr
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ExprKind::Attribute { value, attr, .. } => {
|
|
|
|
let index = self.get_attr_index(value.custom.unwrap(), attr);
|
2021-08-19 15:30:15 +08:00
|
|
|
let val = self.gen_expr(value).unwrap();
|
2021-08-09 15:39:50 +08:00
|
|
|
let ptr = if let BasicValueEnum::PointerValue(v) = val {
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
};
|
|
|
|
unsafe {
|
2021-08-19 16:54:15 +08:00
|
|
|
self.builder.build_in_bounds_gep(
|
|
|
|
ptr,
|
|
|
|
&[
|
|
|
|
self.ctx.i32_type().const_zero(),
|
|
|
|
self.ctx.i32_type().const_int(index as u64, false),
|
|
|
|
],
|
|
|
|
"attr",
|
|
|
|
)
|
2021-08-09 15:39:50 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-19 17:14:38 +08:00
|
|
|
ExprKind::Subscript { value, slice, .. } => {
|
|
|
|
let i32_type = self.ctx.i32_type();
|
|
|
|
let v = assert_pointer_val(self.gen_expr(value).unwrap());
|
|
|
|
let index = assert_int_val(self.gen_expr(slice).unwrap());
|
|
|
|
unsafe {
|
|
|
|
let ptr_to_arr = self.builder.build_in_bounds_gep(
|
|
|
|
v,
|
|
|
|
&[i32_type.const_zero(), i32_type.const_int(1, false)],
|
|
|
|
"ptr_to_arr",
|
|
|
|
);
|
|
|
|
let arr_ptr =
|
|
|
|
assert_pointer_val(self.builder.build_load(ptr_to_arr, "loadptr"));
|
|
|
|
self.builder.build_gep(arr_ptr, &[index], "loadarrgep")
|
|
|
|
}
|
|
|
|
}
|
2021-08-09 15:39:50 +08:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gen_assignment(&mut self, target: &Expr<Option<Type>>, value: BasicValueEnum<'ctx>) {
|
2021-08-19 16:54:15 +08:00
|
|
|
let i32_type = self.ctx.i32_type();
|
2021-08-09 15:39:50 +08:00
|
|
|
if let ExprKind::Tuple { elts, .. } = &target.node {
|
|
|
|
if let BasicValueEnum::PointerValue(ptr) = value {
|
|
|
|
for (i, elt) in elts.iter().enumerate() {
|
|
|
|
unsafe {
|
2021-08-19 16:54:15 +08:00
|
|
|
let t = self.builder.build_in_bounds_gep(
|
|
|
|
ptr,
|
|
|
|
&[i32_type.const_zero(), i32_type.const_int(i as u64, false)],
|
|
|
|
"elem",
|
|
|
|
);
|
2021-08-09 15:39:50 +08:00
|
|
|
let v = self.builder.build_load(t, "tmpload");
|
|
|
|
self.gen_assignment(elt, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let ptr = self.parse_pattern(target);
|
|
|
|
self.builder.build_store(ptr, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 15:30:15 +08:00
|
|
|
// return true if it contains terminator
|
|
|
|
pub fn gen_stmt(&mut self, stmt: &Stmt<Option<Type>>) -> bool {
|
2021-08-09 15:39:50 +08:00
|
|
|
match &stmt.node {
|
2021-09-22 15:15:55 +08:00
|
|
|
StmtKind::Pass => {},
|
2021-08-09 15:39:50 +08:00
|
|
|
StmtKind::Expr { value } => {
|
|
|
|
self.gen_expr(&value);
|
|
|
|
}
|
2021-08-11 14:37:26 +08:00
|
|
|
StmtKind::Return { value } => {
|
2021-08-19 15:30:15 +08:00
|
|
|
let value = value.as_ref().map(|v| self.gen_expr(&v).unwrap());
|
2021-08-11 14:37:26 +08:00
|
|
|
let value = value.as_ref().map(|v| v as &dyn BasicValue);
|
|
|
|
self.builder.build_return(value);
|
2021-08-19 15:30:15 +08:00
|
|
|
return true;
|
2021-08-11 14:37:26 +08:00
|
|
|
}
|
2021-08-09 16:19:20 +08:00
|
|
|
StmtKind::AnnAssign { target, value, .. } => {
|
|
|
|
if let Some(value) = value {
|
2021-08-19 15:30:15 +08:00
|
|
|
let value = self.gen_expr(&value).unwrap();
|
2021-08-09 16:19:20 +08:00
|
|
|
self.gen_assignment(target, value);
|
|
|
|
}
|
|
|
|
}
|
2021-08-09 15:39:50 +08:00
|
|
|
StmtKind::Assign { targets, value, .. } => {
|
2021-08-19 15:30:15 +08:00
|
|
|
let value = self.gen_expr(&value).unwrap();
|
2021-08-09 15:39:50 +08:00
|
|
|
for target in targets.iter() {
|
|
|
|
self.gen_assignment(target, value);
|
|
|
|
}
|
|
|
|
}
|
2021-08-09 16:10:17 +08:00
|
|
|
StmtKind::Continue => {
|
|
|
|
self.builder.build_unconditional_branch(self.loop_bb.unwrap().0);
|
2021-08-19 15:30:15 +08:00
|
|
|
return true;
|
2021-08-09 16:10:17 +08:00
|
|
|
}
|
|
|
|
StmtKind::Break => {
|
|
|
|
self.builder.build_unconditional_branch(self.loop_bb.unwrap().1);
|
2021-08-19 15:30:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
StmtKind::If { test, body, orelse } => {
|
|
|
|
let current = self.builder.get_insert_block().unwrap().get_parent().unwrap();
|
|
|
|
let test_bb = self.ctx.append_basic_block(current, "test");
|
|
|
|
let body_bb = self.ctx.append_basic_block(current, "body");
|
2021-08-27 11:46:12 +08:00
|
|
|
let mut cont_bb = None; // self.ctx.append_basic_block(current, "cont");
|
|
|
|
// if there is no orelse, we just go to cont_bb
|
2021-08-19 15:30:15 +08:00
|
|
|
let orelse_bb = if orelse.is_empty() {
|
2021-08-27 11:46:12 +08:00
|
|
|
cont_bb = Some(self.ctx.append_basic_block(current, "cont"));
|
|
|
|
cont_bb.unwrap()
|
2021-08-19 15:30:15 +08:00
|
|
|
} else {
|
|
|
|
self.ctx.append_basic_block(current, "orelse")
|
|
|
|
};
|
|
|
|
self.builder.build_unconditional_branch(test_bb);
|
|
|
|
self.builder.position_at_end(test_bb);
|
|
|
|
let test = self.gen_expr(test).unwrap();
|
|
|
|
if let BasicValueEnum::IntValue(test) = test {
|
|
|
|
self.builder.build_conditional_branch(test, body_bb, orelse_bb);
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
self.builder.position_at_end(body_bb);
|
|
|
|
let mut exited = false;
|
|
|
|
for stmt in body.iter() {
|
|
|
|
exited = self.gen_stmt(stmt);
|
|
|
|
if exited {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !exited {
|
2021-08-27 11:46:12 +08:00
|
|
|
if cont_bb.is_none() {
|
|
|
|
cont_bb = Some(self.ctx.append_basic_block(current, "cont"));
|
|
|
|
}
|
|
|
|
self.builder.build_unconditional_branch(cont_bb.unwrap());
|
2021-08-19 15:30:15 +08:00
|
|
|
}
|
|
|
|
if !orelse.is_empty() {
|
|
|
|
exited = false;
|
|
|
|
self.builder.position_at_end(orelse_bb);
|
|
|
|
for stmt in orelse.iter() {
|
|
|
|
exited = self.gen_stmt(stmt);
|
|
|
|
if exited {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !exited {
|
2021-08-27 11:46:12 +08:00
|
|
|
if cont_bb.is_none() {
|
|
|
|
cont_bb = Some(self.ctx.append_basic_block(current, "cont"));
|
|
|
|
}
|
|
|
|
self.builder.build_unconditional_branch(cont_bb.unwrap());
|
2021-08-19 15:30:15 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-27 11:46:12 +08:00
|
|
|
if let Some(cont_bb) = cont_bb {
|
|
|
|
self.builder.position_at_end(cont_bb);
|
|
|
|
}
|
2021-08-09 16:10:17 +08:00
|
|
|
}
|
|
|
|
StmtKind::While { test, body, orelse } => {
|
|
|
|
let current = self.builder.get_insert_block().unwrap().get_parent().unwrap();
|
|
|
|
let test_bb = self.ctx.append_basic_block(current, "test");
|
|
|
|
let body_bb = self.ctx.append_basic_block(current, "body");
|
|
|
|
let cont_bb = self.ctx.append_basic_block(current, "cont");
|
|
|
|
// if there is no orelse, we just go to cont_bb
|
|
|
|
let orelse_bb = if orelse.is_empty() {
|
|
|
|
cont_bb
|
|
|
|
} else {
|
|
|
|
self.ctx.append_basic_block(current, "orelse")
|
|
|
|
};
|
|
|
|
// store loop bb information and restore it later
|
|
|
|
let loop_bb = self.loop_bb.replace((test_bb, cont_bb));
|
|
|
|
self.builder.build_unconditional_branch(test_bb);
|
|
|
|
self.builder.position_at_end(test_bb);
|
2021-08-19 15:30:15 +08:00
|
|
|
let test = self.gen_expr(test).unwrap();
|
2021-08-09 16:10:17 +08:00
|
|
|
if let BasicValueEnum::IntValue(test) = test {
|
|
|
|
self.builder.build_conditional_branch(test, body_bb, orelse_bb);
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
self.builder.position_at_end(body_bb);
|
|
|
|
for stmt in body.iter() {
|
|
|
|
self.gen_stmt(stmt);
|
|
|
|
}
|
|
|
|
self.builder.build_unconditional_branch(test_bb);
|
|
|
|
if !orelse.is_empty() {
|
|
|
|
self.builder.position_at_end(orelse_bb);
|
|
|
|
for stmt in orelse.iter() {
|
|
|
|
self.gen_stmt(stmt);
|
|
|
|
}
|
|
|
|
self.builder.build_unconditional_branch(cont_bb);
|
|
|
|
}
|
|
|
|
self.builder.position_at_end(cont_bb);
|
|
|
|
self.loop_bb = loop_bb;
|
|
|
|
}
|
2021-08-19 15:30:15 +08:00
|
|
|
_ => unimplemented!("{:?}", stmt),
|
|
|
|
};
|
|
|
|
false
|
2021-08-09 15:39:50 +08:00
|
|
|
}
|
|
|
|
}
|