Skip to content

Commit

Permalink
kinda-refactor llvm codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Jul 23, 2024
1 parent b2bb8a3 commit fe7ef24
Show file tree
Hide file tree
Showing 6 changed files with 735 additions and 734 deletions.
86 changes: 0 additions & 86 deletions src/compile_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use index_vec::IndexVec;
use crate::{
repr::{ast::typed::Function, identifier::FunctionIdx, ir, ty::Ty},
stage::{
codegen::llvm::{LLVMCodegenCtx, LLVMFunctionBuilder},
lower_ir::{FunctionBuilder as FunctionBuilderTrait, IRCtx},
parse::ParseCtx,
type_check::{FunctionSignature, TypeCheckCtx},
Expand All @@ -20,8 +19,6 @@ pub struct CompilePass {
function_symbols: HashMap<Symbol, FunctionIdx>,

ir_functions: HashMap<FunctionIdx, ir::Function>,

llvm_ctx: inkwell::context::Context,
}

impl CompilePass {
Expand All @@ -31,7 +28,6 @@ impl CompilePass {
function_signatures: IndexVec::new(),
function_symbols: HashMap::new(),
ir_functions: HashMap::new(),
llvm_ctx: inkwell::context::Context::create(),
}
}
}
Expand Down Expand Up @@ -163,85 +159,3 @@ impl FunctionBuilderTrait for FunctionBuilder {
)
}
}

impl<'ctx> LLVMCodegenCtx<'ctx> for CompilePass {
type FunctionBuilder = CompilePassFunctionBuilder;

fn new_builder(&self) -> inkwell::builder::Builder<'ctx> {
self.llvm_ctx.create_builder()
}

fn lookup_function_value(
&self,
function_idx: FunctionIdx,
) -> Option<inkwell::values::FunctionValue<'ctx>> {
todo!()
}

fn create_function_value(
&mut self,
function_idx: FunctionIdx,
) -> inkwell::values::FunctionValue<'ctx> {
todo!()
}

fn append_basic_block(
&mut self,
function_idx: FunctionIdx,
) -> inkwell::basic_block::BasicBlock<'ctx> {
todo!()
}

fn get_function(&self, function_idx: FunctionIdx) -> ir::Function {
todo!()
}

fn get_type(&self, ty: Ty) -> impl inkwell::types::BasicType<'ctx> {
todo!()
}

fn const_int(&self, value: i64) -> inkwell::values::IntValue<'ctx> {
todo!()
}

fn const_bool(&self, value: bool) -> inkwell::values::IntValue<'ctx> {
todo!()
}

fn create_function_builder(
&self,
symbol_locations: HashMap<Symbol, inkwell::values::PointerValue<'ctx>>,
) -> Self::FunctionBuilder {
todo!()
}
}

pub struct CompilePassFunctionBuilder {}

impl<'ctx> LLVMFunctionBuilder<'ctx> for CompilePassFunctionBuilder {
fn lookup_basic_block(
&self,
basic_block_idx: ir::BasicBlockIdx,
) -> Option<inkwell::basic_block::BasicBlock<'ctx>> {
todo!()
}

fn create_basic_block(
&mut self,
basic_block_idx: ir::BasicBlockIdx,
) -> inkwell::basic_block::BasicBlock<'ctx> {
todo!()
}

fn anonymous_basic_block(&self) -> inkwell::basic_block::BasicBlock<'ctx> {
todo!()
}

fn lookup_symbol(&self, symbol: Symbol) -> inkwell::values::PointerValue<'ctx> {
todo!()
}

fn get_triples(&self, basic_block_idx: ir::BasicBlockIdx) -> Vec<ir::Triple> {
todo!()
}
}
85 changes: 71 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
use std::collections::HashMap;

use inkwell::{
module::Module,
passes::PassBuilderOptions,
targets::{CodeModel, RelocMode, Target, TargetMachine},
values::FunctionValue,
OptimizationLevel,
};
use lumina::{
compile_pass::CompilePass,
stage::{
codegen::llvm::Pass,
codegen::llvm::FunctionGenerator,
lex::Lexer,
lower_ir::{self as ir, IRCtx},
parse::parse,
Expand Down Expand Up @@ -43,17 +52,31 @@ fn main() -> int {

ir::lower(&mut ctx, program);
let llvm_ctx = inkwell::context::Context::create();
let module = llvm_ctx.create_module("module");

let function_ids = ctx
let function_map = ctx
.all_functions()
.iter()
.map(|(idx, _)| *idx)
.collect::<Vec<_>>();
let mut llvm_pass = Pass::new(&llvm_ctx, ctx);
function_ids.into_iter().for_each(|function| {
llvm_pass.compile(function);
});
let main = *llvm_pass.function_values.get(&main).unwrap();
.map(|(idx, _)| {
(
*idx,
module.add_function("fn", llvm_ctx.i64_type().fn_type(&[], false), None),
)
})
.collect::<HashMap<_, _>>();

for (idx, function) in ctx.all_functions() {
FunctionGenerator::new(
&mut ctx,
&llvm_ctx,
function_map.clone(),
*function_map.get(&idx).unwrap(),
function,
)
.codegen();
}

let main = function_map.get(&main).unwrap();

// llvm_pass.run_passes(&[
// "instcombine",
Expand All @@ -63,12 +86,46 @@ fn main() -> int {
// "mem2reg",
// ]);

llvm_pass.debug_print();
module.print_to_stderr();

let result = llvm_pass.jit(main);
let result = jit(&module, *main);
println!("result: {result}");
}

fn _run_passes(module: &Module, passes: &[&str]) {
Target::initialize_all(&Default::default());

let target_triple = TargetMachine::get_default_triple();
let target = Target::from_triple(&target_triple).unwrap();
let target_machine = target
.create_target_machine(
&target_triple,
"generic",
"",
OptimizationLevel::None,
RelocMode::PIC,
CodeModel::Default,
)
.unwrap();

module
.run_passes(
passes.join(",").as_str(),
&target_machine,
PassBuilderOptions::create(),
)
.unwrap();
}

fn jit(module: &Module, entry: FunctionValue) -> i64 {
let engine = module
.create_jit_execution_engine(OptimizationLevel::None)
.unwrap();

// let compiler = Compiler::new();
// let module = compiler.compile(program);
// module.jit();
unsafe {
engine
.get_function::<unsafe extern "C" fn() -> i64>(entry.get_name().to_str().unwrap())
.unwrap()
.call()
}
}
2 changes: 1 addition & 1 deletion src/repr/ir/triple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ define_index_type! {
}

/// A reference to a specific triple.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct TripleRef {
pub basic_block: BasicBlockIdx,
pub triple: TripleIdx,
Expand Down
Loading

0 comments on commit fe7ef24

Please sign in to comment.