2021-10-16 22:17:36 +08:00
|
|
|
use crate::{
|
2023-09-20 13:34:50 +08:00
|
|
|
codegen::{expr::*, stmt::*, bool_to_i1, bool_to_i8, CodeGenContext},
|
2021-11-20 19:50:25 +08:00
|
|
|
symbol_resolver::ValueEnum,
|
2021-10-16 22:17:36 +08:00
|
|
|
toplevel::{DefinitionId, TopLevelDef},
|
|
|
|
typecheck::typedef::{FunSignature, Type},
|
|
|
|
};
|
2021-12-27 22:55:51 +08:00
|
|
|
use inkwell::{
|
|
|
|
context::Context,
|
|
|
|
types::{BasicTypeEnum, IntType},
|
2023-09-20 13:34:50 +08:00
|
|
|
values::{BasicValueEnum, IntValue, PointerValue},
|
2021-12-27 22:55:51 +08:00
|
|
|
};
|
2021-11-03 17:11:00 +08:00
|
|
|
use nac3parser::ast::{Expr, Stmt, StrRef};
|
2021-10-16 22:17:36 +08:00
|
|
|
|
|
|
|
pub trait CodeGenerator {
|
|
|
|
/// Return the module name for the code generator.
|
|
|
|
fn get_name(&self) -> &str;
|
|
|
|
|
2021-12-27 22:55:51 +08:00
|
|
|
fn get_size_type<'ctx>(&self, ctx: &'ctx Context) -> IntType<'ctx>;
|
|
|
|
|
2021-10-16 22:17:36 +08:00
|
|
|
/// Generate function call and returns the function return value.
|
|
|
|
/// - obj: Optional object for method call.
|
|
|
|
/// - fun: Function signature and definition ID.
|
|
|
|
/// - params: Function parameters. Note that this does not include the object even if the
|
|
|
|
/// function is a class method.
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_call<'ctx>(
|
2021-10-16 22:17:36 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2021-11-20 19:50:25 +08:00
|
|
|
obj: Option<(Type, ValueEnum<'ctx>)>,
|
2021-10-16 22:17:36 +08:00
|
|
|
fun: (&FunSignature, DefinitionId),
|
2021-11-20 19:50:25 +08:00
|
|
|
params: Vec<(Option<StrRef>, ValueEnum<'ctx>)>,
|
2022-02-21 17:52:34 +08:00
|
|
|
) -> Result<Option<BasicValueEnum<'ctx>>, String>
|
2021-12-27 22:55:51 +08:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2021-10-16 22:17:36 +08:00
|
|
|
gen_call(self, ctx, obj, fun, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate object constructor and returns the constructed object.
|
2022-04-13 11:32:31 +08:00
|
|
|
/// - signature: Function signature of the constructor.
|
2021-10-16 22:17:36 +08:00
|
|
|
/// - def: Class definition for the constructor class.
|
|
|
|
/// - params: Function parameters.
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_constructor<'ctx>(
|
2021-10-16 22:17:36 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2021-10-16 22:17:36 +08:00
|
|
|
signature: &FunSignature,
|
|
|
|
def: &TopLevelDef,
|
2021-11-20 19:50:25 +08:00
|
|
|
params: Vec<(Option<StrRef>, ValueEnum<'ctx>)>,
|
2022-02-21 17:52:34 +08:00
|
|
|
) -> Result<BasicValueEnum<'ctx>, String>
|
2021-12-27 22:55:51 +08:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2021-10-16 22:17:36 +08:00
|
|
|
gen_constructor(self, ctx, signature, def, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate a function instance.
|
|
|
|
/// - obj: Optional object for method call.
|
|
|
|
/// - fun: Function signature, definition ID and the substitution key.
|
|
|
|
/// - params: Function parameters. Note that this does not include the object even if the
|
|
|
|
/// function is a class method.
|
|
|
|
/// Note that this function should check if the function is generated in another thread (due to
|
|
|
|
/// possible race condition), see the default implementation for an example.
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_func_instance<'ctx>(
|
2021-10-16 22:17:36 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2021-11-20 19:50:25 +08:00
|
|
|
obj: Option<(Type, ValueEnum<'ctx>)>,
|
2021-10-16 22:17:36 +08:00
|
|
|
fun: (&FunSignature, &mut TopLevelDef, String),
|
2021-11-20 19:50:25 +08:00
|
|
|
id: usize,
|
2022-02-21 17:52:34 +08:00
|
|
|
) -> Result<String, String> {
|
2021-11-20 19:50:25 +08:00
|
|
|
gen_func_instance(ctx, obj, fun, id)
|
2021-10-16 22:17:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate the code for an expression.
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_expr<'ctx>(
|
2021-10-16 22:17:36 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2021-10-16 22:17:36 +08:00
|
|
|
expr: &Expr<Option<Type>>,
|
2022-02-21 17:52:34 +08:00
|
|
|
) -> Result<Option<ValueEnum<'ctx>>, String>
|
2021-12-27 22:55:51 +08:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2021-10-16 22:17:36 +08:00
|
|
|
gen_expr(self, ctx, expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocate memory for a variable and return a pointer pointing to it.
|
|
|
|
/// The default implementation places the allocations at the start of the function.
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_var_alloc<'ctx>(
|
2021-10-16 22:17:36 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2021-12-27 22:55:51 +08:00
|
|
|
ty: BasicTypeEnum<'ctx>,
|
2023-09-06 11:00:02 +08:00
|
|
|
name: Option<&str>,
|
2022-02-21 17:52:34 +08:00
|
|
|
) -> Result<PointerValue<'ctx>, String> {
|
2023-09-06 11:00:02 +08:00
|
|
|
gen_var(ctx, ty, name)
|
2021-10-16 22:17:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a pointer pointing to the target of the expression.
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_store_target<'ctx>(
|
2021-10-16 22:17:36 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2021-10-16 22:17:36 +08:00
|
|
|
pattern: &Expr<Option<Type>>,
|
2023-09-06 11:00:02 +08:00
|
|
|
name: Option<&str>,
|
2023-12-06 15:26:37 +08:00
|
|
|
) -> Result<Option<PointerValue<'ctx>>, String>
|
2021-12-27 22:55:51 +08:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2023-09-06 11:00:02 +08:00
|
|
|
gen_store_target(self, ctx, pattern, name)
|
2021-10-16 22:17:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate code for an assignment expression.
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_assign<'ctx>(
|
2021-10-16 22:17:36 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2021-10-16 22:17:36 +08:00
|
|
|
target: &Expr<Option<Type>>,
|
2021-11-20 19:50:25 +08:00
|
|
|
value: ValueEnum<'ctx>,
|
2022-02-21 17:52:34 +08:00
|
|
|
) -> Result<(), String>
|
|
|
|
where
|
2021-12-27 22:55:51 +08:00
|
|
|
Self: Sized,
|
|
|
|
{
|
2021-10-16 22:17:36 +08:00
|
|
|
gen_assign(self, ctx, target, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate code for a while expression.
|
|
|
|
/// Return true if the while loop must early return
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_while(
|
2022-02-21 18:27:46 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'_, '_>,
|
2022-02-21 18:27:46 +08:00
|
|
|
stmt: &Stmt<Option<Type>>,
|
|
|
|
) -> Result<(), String>
|
2021-12-27 22:55:51 +08:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2022-02-21 17:52:34 +08:00
|
|
|
gen_while(self, ctx, stmt)
|
2021-10-16 22:17:36 +08:00
|
|
|
}
|
|
|
|
|
2021-10-23 23:53:36 +08:00
|
|
|
/// Generate code for a while expression.
|
|
|
|
/// Return true if the while loop must early return
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_for(
|
2022-02-21 18:27:46 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'_, '_>,
|
2022-02-21 18:27:46 +08:00
|
|
|
stmt: &Stmt<Option<Type>>,
|
|
|
|
) -> Result<(), String>
|
2021-12-27 22:55:51 +08:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2022-02-21 17:52:34 +08:00
|
|
|
gen_for(self, ctx, stmt)
|
2021-10-23 23:53:36 +08:00
|
|
|
}
|
|
|
|
|
2021-10-16 22:17:36 +08:00
|
|
|
/// Generate code for an if expression.
|
|
|
|
/// Return true if the statement must early return
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_if(
|
2022-02-21 18:27:46 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'_, '_>,
|
2022-02-21 18:27:46 +08:00
|
|
|
stmt: &Stmt<Option<Type>>,
|
|
|
|
) -> Result<(), String>
|
2021-12-27 22:55:51 +08:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2022-02-21 17:52:34 +08:00
|
|
|
gen_if(self, ctx, stmt)
|
2021-10-16 22:17:36 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_with(
|
2022-02-21 18:27:46 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'_, '_>,
|
2022-02-21 18:27:46 +08:00
|
|
|
stmt: &Stmt<Option<Type>>,
|
|
|
|
) -> Result<(), String>
|
2021-12-27 22:55:51 +08:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2022-02-21 17:52:34 +08:00
|
|
|
gen_with(self, ctx, stmt)
|
2021-10-31 17:16:21 +08:00
|
|
|
}
|
|
|
|
|
2021-10-16 22:17:36 +08:00
|
|
|
/// Generate code for a statement
|
2023-10-23 15:42:39 +08:00
|
|
|
///
|
2021-10-16 22:17:36 +08:00
|
|
|
/// Return true if the statement must early return
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_stmt(
|
2022-02-21 18:27:46 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'_, '_>,
|
2022-02-21 18:27:46 +08:00
|
|
|
stmt: &Stmt<Option<Type>>,
|
|
|
|
) -> Result<(), String>
|
2021-12-27 22:55:51 +08:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2022-02-21 17:52:34 +08:00
|
|
|
gen_stmt(self, ctx, stmt)
|
2021-10-16 22:17:36 +08:00
|
|
|
}
|
2023-09-20 13:34:50 +08:00
|
|
|
|
2023-10-23 15:42:39 +08:00
|
|
|
/// Generates code for a block statement.
|
2023-12-06 11:49:02 +08:00
|
|
|
fn gen_block<'a, I: Iterator<Item = &'a Stmt<Option<Type>>>>(
|
2023-10-23 15:42:39 +08:00
|
|
|
&mut self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &mut CodeGenContext<'_, '_>,
|
2023-10-23 15:42:39 +08:00
|
|
|
stmts: I,
|
|
|
|
) -> Result<(), String>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
gen_block(self, ctx, stmts)
|
|
|
|
}
|
|
|
|
|
2023-09-20 16:59:47 +08:00
|
|
|
/// See [bool_to_i1].
|
2023-12-06 11:49:02 +08:00
|
|
|
fn bool_to_i1<'ctx>(
|
2023-09-20 13:34:50 +08:00
|
|
|
&self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &CodeGenContext<'ctx, '_>,
|
2023-09-20 13:34:50 +08:00
|
|
|
bool_value: IntValue<'ctx>
|
|
|
|
) -> IntValue<'ctx> {
|
|
|
|
bool_to_i1(&ctx.builder, bool_value)
|
|
|
|
}
|
|
|
|
|
2023-09-20 16:59:47 +08:00
|
|
|
/// See [bool_to_i8].
|
2023-12-06 11:49:02 +08:00
|
|
|
fn bool_to_i8<'ctx>(
|
2023-09-20 13:34:50 +08:00
|
|
|
&self,
|
2023-12-06 11:49:02 +08:00
|
|
|
ctx: &CodeGenContext<'ctx, '_>,
|
2023-09-20 13:34:50 +08:00
|
|
|
bool_value: IntValue<'ctx>
|
|
|
|
) -> IntValue<'ctx> {
|
2023-12-06 11:49:02 +08:00
|
|
|
bool_to_i8(&ctx.builder, ctx.ctx, bool_value)
|
2023-09-20 13:34:50 +08:00
|
|
|
}
|
2021-10-16 22:17:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DefaultCodeGenerator {
|
|
|
|
name: String,
|
2021-12-27 22:55:51 +08:00
|
|
|
size_t: u32,
|
2021-10-16 22:17:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DefaultCodeGenerator {
|
2021-12-27 22:55:51 +08:00
|
|
|
pub fn new(name: String, size_t: u32) -> DefaultCodeGenerator {
|
2023-12-06 11:49:02 +08:00
|
|
|
assert!(matches!(size_t, 32 | 64));
|
2021-12-27 22:55:51 +08:00
|
|
|
DefaultCodeGenerator { name, size_t }
|
2021-10-16 22:17:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CodeGenerator for DefaultCodeGenerator {
|
|
|
|
fn get_name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
2021-12-27 22:55:51 +08:00
|
|
|
|
|
|
|
fn get_size_type<'ctx>(&self, ctx: &'ctx Context) -> IntType<'ctx> {
|
|
|
|
// it should be unsigned, but we don't really need unsigned and this could save us from
|
|
|
|
// having to do a bit cast...
|
|
|
|
if self.size_t == 32 {
|
|
|
|
ctx.i32_type()
|
|
|
|
} else {
|
|
|
|
ctx.i64_type()
|
|
|
|
}
|
|
|
|
}
|
2021-10-16 22:17:36 +08:00
|
|
|
}
|