Add simple codegen from actual AST

This commit is contained in:
Sofia 2023-08-02 17:03:06 +03:00
parent a9d5302774
commit a196857dd6
4 changed files with 161 additions and 55 deletions

View File

@ -1,8 +1,14 @@
// Hello, comment here! // Hello, comment here!
import std::print;
fn somethingelse() {
return 12;
}
fn main() { fn main() {
let hello = 32; let hello = 32 + 2 + 4;
let beep = let beep =
hello ; hello + 100;
return beep; return beep;
} }

View File

@ -1,3 +1,4 @@
use std::ffi::CString;
use std::mem; use std::mem;
use llvm_sys::{core::*, prelude::*, LLVMBuilder, LLVMContext, LLVMModule}; use llvm_sys::{core::*, prelude::*, LLVMBuilder, LLVMContext, LLVMModule};
@ -10,37 +11,43 @@ macro_rules! cstr {
}; };
} }
#[derive(PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum ValueType { pub enum ValueType {
I32, I32,
} }
impl ValueType { impl ValueType {
unsafe fn get_llvm_type(&self, codegen: &mut CodeGenerator) -> LLVMTypeRef { unsafe fn get_llvm_type(&self, codegen: &mut IRModule) -> LLVMTypeRef {
match *self { match *self {
Self::I32 => LLVMInt32TypeInContext(codegen.context), Self::I32 => LLVMInt32TypeInContext(codegen.context),
} }
} }
} }
#[derive(Clone, Debug)]
#[must_use = "value contains raw pointer and must be inserted somewhere"] #[must_use = "value contains raw pointer and must be inserted somewhere"]
pub struct Value(ValueType, LLVMValueRef); pub struct Value(ValueType, LLVMValueRef);
pub struct CodeGenerator { fn into_cstring<T: Into<String>>(value: T) -> CString {
let string = value.into();
unsafe { CString::from_vec_with_nul_unchecked((string + "\0").into_bytes()) }
}
pub struct IRModule {
context: *mut LLVMContext, context: *mut LLVMContext,
module: *mut LLVMModule, module: *mut LLVMModule,
builder: *mut LLVMBuilder, builder: *mut LLVMBuilder,
} }
impl CodeGenerator { impl IRModule {
pub fn new() -> CodeGenerator { pub fn new() -> IRModule {
unsafe { unsafe {
// Set up a context, module and builder in that context. // Set up a context, module and builder in that context.
let context = LLVMContextCreate(); let context = LLVMContextCreate();
let module = LLVMModuleCreateWithNameInContext(cstr!("testmodule"), context); let module = LLVMModuleCreateWithNameInContext(cstr!("testmodule"), context);
let builder = LLVMCreateBuilderInContext(context); let builder = LLVMCreateBuilderInContext(context);
CodeGenerator { IRModule {
context, context,
module, module,
builder, builder,
@ -48,13 +55,83 @@ impl CodeGenerator {
} }
} }
pub fn create_block(&mut self) -> IRBlock {
IRBlock::create("entry", self)
}
pub fn create_func<T: Into<String>>(&mut self, name: T, return_type: ValueType) -> IRFunction {
unsafe {
let mut argts = [];
let func_type = LLVMFunctionType(
return_type.get_llvm_type(self),
argts.as_mut_ptr(),
argts.len() as u32,
0,
);
let anon_func = LLVMAddFunction(self.module, into_cstring(name).as_ptr(), func_type);
IRFunction {
value: Value(return_type, anon_func),
}
// // Create a basic block in the function and set our builder to generate
// // code in it.
// let bb = LLVMAppendBasicBlockInContext(self.context, anon_func, cstr!("entry"));
// LLVMPositionBuilderAtEnd(self.builder, bb);
// // Emit a `ret i64` into the function to return the computed sum.
// LLVMBuildRet(self.builder, ret.1);
}
}
}
impl Drop for IRModule {
fn drop(&mut self) {
// Clean up. Values created in the context mostly get cleaned up there.
unsafe {
LLVMDisposeBuilder(self.builder);
LLVMDumpModule(self.module);
LLVMDisposeModule(self.module);
LLVMContextDispose(self.context);
}
}
}
pub struct IRFunction {
value: Value,
}
impl IRFunction {
pub fn add_definition(self, ret: Value, block: IRBlock) {
unsafe {
LLVMAppendExistingBasicBlock(self.value.1, block.blockref);
LLVMBuildRet(block.codegen.builder, ret.1);
}
}
}
pub struct IRBlock<'a> {
codegen: &'a mut IRModule,
blockref: LLVMBasicBlockRef,
}
impl<'a> IRBlock<'a> {
fn create<T: Into<String>>(name: T, codegen: &'a mut IRModule) -> IRBlock<'a> {
unsafe {
let blockref =
LLVMCreateBasicBlockInContext(codegen.context, into_cstring(name).as_ptr());
LLVMPositionBuilderAtEnd(codegen.builder, blockref);
IRBlock { codegen, blockref }
}
}
pub fn get_const(&mut self, literal_type: &Literal) -> Value { pub fn get_const(&mut self, literal_type: &Literal) -> Value {
unsafe { unsafe {
match *literal_type { match *literal_type {
Literal::I32(v) => Value( Literal::I32(v) => Value(
ValueType::I32, ValueType::I32,
LLVMConstInt( LLVMConstInt(
LLVMInt32TypeInContext(self.context), LLVMInt32TypeInContext(self.codegen.context),
mem::transmute(v as i64), mem::transmute(v as i64),
1, 1,
), ),
@ -68,45 +145,11 @@ impl CodeGenerator {
if lhs.0 == rhs.0 { if lhs.0 == rhs.0 {
Ok(Value( Ok(Value(
lhs.0, lhs.0,
LLVMBuildAdd(self.builder, lhs.1, rhs.1, cstr!("tmpadd")), LLVMBuildAdd(self.codegen.builder, lhs.1, rhs.1, cstr!("tmpadd")),
)) ))
} else { } else {
Err(()) Err(())
} }
} }
} }
pub fn create_func(&mut self, ret: Value) {
unsafe {
let mut argts = [];
let func_type = LLVMFunctionType(
ret.0.get_llvm_type(self),
argts.as_mut_ptr(),
argts.len() as u32,
0,
);
let anon_func = LLVMAddFunction(self.module, cstr!("_anon_func"), func_type);
// Create a basic block in the function and set our builder to generate
// code in it.
let bb = LLVMAppendBasicBlockInContext(self.context, anon_func, cstr!("entry"));
LLVMPositionBuilderAtEnd(self.builder, bb);
// Emit a `ret i64` into the function to return the computed sum.
LLVMBuildRet(self.builder, ret.1);
}
}
}
impl Drop for CodeGenerator {
fn drop(&mut self) {
// Clean up. Values created in the context mostly get cleaned up there.
unsafe {
LLVMDisposeBuilder(self.builder);
LLVMDumpModule(self.module);
LLVMDisposeModule(self.module);
LLVMContextDispose(self.context);
}
}
} }

View File

@ -1,5 +1,13 @@
use std::collections::HashMap;
use codegen::IRBlock;
use parser::Expression;
use crate::{ use crate::{
codegen::CodeGenerator, lexer::Token, parser::TopLevelStatement, token_stream::TokenStream, codegen::{IRModule, Value},
lexer::Token,
parser::{FunctionDefinition, Literal, TopLevelStatement},
token_stream::TokenStream,
}; };
pub static EASIEST: &str = include_str!("../reid/easiest.reid"); pub static EASIEST: &str = include_str!("../reid/easiest.reid");
@ -25,16 +33,65 @@ fn main() {
let mut token_stream = TokenStream::from(&tokens); let mut token_stream = TokenStream::from(&tokens);
let mut statements = Vec::new();
while !matches!(token_stream.peek().unwrap_or(Token::Eof), Token::Eof) { while !matches!(token_stream.peek().unwrap_or(Token::Eof), Token::Eof) {
let statement = token_stream.parse::<TopLevelStatement>().unwrap(); let statement = token_stream.parse::<TopLevelStatement>().unwrap();
dbg!(&statement); dbg!(&statement);
statements.push(statement);
} }
let mut c = CodeGenerator::new(); let mut c = IRModule::new();
let x = c.get_const(&parser::Literal::I32(3)); for statement in statements {
let y = c.get_const(&parser::Literal::I32(4)); match statement {
let add = c.add(x, y).unwrap(); TopLevelStatement::FunctionDefinition(FunctionDefinition(sig, block)) => {
c.create_func(add); let mut named_vars: HashMap<String, Value> = HashMap::new();
// dbg!(token_stream.expect(Token::Eof).ok()); let func = c.create_func(sig.name, codegen::ValueType::I32);
let mut c_block = c.create_block();
for stmt in block.0 {
match stmt {
parser::BlockLevelStatement::Let(let_statement) => {
let value = codegen_exp(&mut c_block, &named_vars, let_statement.1);
named_vars.insert(let_statement.0, value);
}
parser::BlockLevelStatement::Return(_) => panic!("Should never exist!"),
parser::BlockLevelStatement::Import(_) => {}
parser::BlockLevelStatement::Expression(_) => {}
}
}
let value = if let Some(exp) = block.1 {
codegen_exp(&mut c_block, &named_vars, exp)
} else {
c_block.get_const(&Literal::I32(0))
};
func.add_definition(value, c_block);
}
TopLevelStatement::Import(_) => {}
}
}
}
fn codegen_exp(
c_block: &mut IRBlock,
named_vars: &HashMap<String, Value>,
expression: Expression,
) -> Value {
use parser::Expression::*;
match expression {
Binop(op, lhs, rhs) => match op {
parser::BinaryOperator::Add => {
let lhs = codegen_exp(c_block, named_vars, *lhs);
let rhs = codegen_exp(c_block, named_vars, *rhs);
c_block.add(lhs, rhs).unwrap()
}
parser::BinaryOperator::Mult => panic!("Not implemented!"),
},
BlockExpr(_) => panic!("Not implemented!"),
FunctionCall(_) => panic!("Not implemented!"),
VariableName(name) => named_vars.get(&name).cloned().unwrap(),
Literal(lit) => c_block.get_const(&lit),
}
} }

View File

@ -125,7 +125,7 @@ impl Parse for FunctionCallExpression {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct LetStatement(String, Expression); pub struct LetStatement(pub String, pub Expression);
impl Parse for LetStatement { impl Parse for LetStatement {
fn parse(mut stream: TokenStream) -> Result<LetStatement, ()> { fn parse(mut stream: TokenStream) -> Result<LetStatement, ()> {
@ -172,7 +172,7 @@ impl Parse for ImportStatement {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct FunctionDefinition(FunctionSignature, Block); pub struct FunctionDefinition(pub FunctionSignature, pub Block);
impl Parse for FunctionDefinition { impl Parse for FunctionDefinition {
fn parse(mut stream: TokenStream) -> Result<Self, ()> { fn parse(mut stream: TokenStream) -> Result<Self, ()> {
@ -183,7 +183,7 @@ impl Parse for FunctionDefinition {
#[derive(Debug)] #[derive(Debug)]
pub struct FunctionSignature { pub struct FunctionSignature {
name: String, pub name: String,
} }
impl Parse for FunctionSignature { impl Parse for FunctionSignature {
@ -199,7 +199,7 @@ impl Parse for FunctionSignature {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Block(Vec<BlockLevelStatement>, Option<Expression>); pub struct Block(pub Vec<BlockLevelStatement>, pub Option<Expression>);
impl Parse for Block { impl Parse for Block {
fn parse(mut stream: TokenStream) -> Result<Self, ()> { fn parse(mut stream: TokenStream) -> Result<Self, ()> {