Compare commits
No commits in common. "a3642f127c859ad6de3e1386c59a6e20279ceca6" and "6448b0c438643d5c1a6abe7bb3a0dbac663786a6" have entirely different histories.
a3642f127c
...
6448b0c438
@ -1,9 +1,9 @@
|
||||
use reid::compile;
|
||||
|
||||
pub static ARITHMETIC: &str = include_str!("./reid/arithmetic.reid");
|
||||
pub static EASY: &str = include_str!("./reid/easy.reid");
|
||||
|
||||
fn main() {
|
||||
let text = match compile(ARITHMETIC) {
|
||||
let text = match compile(EASY) {
|
||||
Ok(t) => t,
|
||||
Err(e) => panic!("{}", e),
|
||||
};
|
@ -3,12 +3,8 @@
|
||||
fn main() {
|
||||
let test = 9;
|
||||
let simpleAdd = 2 + 2;
|
||||
let simpleMult = 7 * 2; // 14
|
||||
let arithmetic = 3 + 2 * 5 + 1 * 2; // 15
|
||||
|
||||
if simpleAdd < test {
|
||||
return 3;
|
||||
}
|
||||
let simpleMult = 7 * 2;
|
||||
let arithmetic = 3 + 2 * 5 + 1 * 2;
|
||||
|
||||
return arithmetic + simpleMult * arithmetic;
|
||||
}
|
@ -1,9 +1,17 @@
|
||||
// Main
|
||||
// Hello, comment here!
|
||||
|
||||
import std::print;
|
||||
|
||||
fn main() {
|
||||
return fibonacci(10);
|
||||
let hello = 32 + {
|
||||
2 + 3
|
||||
};
|
||||
let beep = hello + fibonacci();
|
||||
return beep;
|
||||
}
|
||||
|
||||
// Fibonacci
|
||||
|
||||
fn fibonacci(value: i32) -> i32 {
|
||||
if value < 3 {
|
||||
return 1;
|
||||
|
@ -214,7 +214,7 @@ impl Parse for FunctionCallExpression {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IfExpression(pub Expression, pub Block, pub TokenRange);
|
||||
pub struct IfExpression(Expression, pub Block, pub TokenRange);
|
||||
|
||||
impl Parse for IfExpression {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
|
@ -1,9 +1,6 @@
|
||||
use std::borrow::BorrowMut;
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::mem;
|
||||
use std::ptr::null_mut;
|
||||
|
||||
use llvm_sys::analysis::LLVMVerifyModule;
|
||||
use llvm_sys::{
|
||||
core::*, prelude::*, LLVMBasicBlock, LLVMBuilder, LLVMContext, LLVMModule, LLVMType, LLVMValue,
|
||||
};
|
||||
@ -24,16 +21,14 @@ pub enum Error {
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum IRType {
|
||||
I32,
|
||||
Boolean,
|
||||
}
|
||||
|
||||
impl IRType {
|
||||
fn in_context(&self, context: &IRContext) -> *mut LLVMType {
|
||||
fn in_context(&self, context: &mut IRContext) -> *mut LLVMType {
|
||||
use IRType::*;
|
||||
unsafe {
|
||||
return match self {
|
||||
I32 => LLVMInt32TypeInContext(context.context),
|
||||
Boolean => LLVMInt1TypeInContext(context.context),
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -43,14 +38,14 @@ impl IRType {
|
||||
pub struct IRValue(pub IRType, *mut LLVMValue);
|
||||
|
||||
impl IRValue {
|
||||
pub fn from_literal(literal: &ast::Literal, module: &IRModule) -> Self {
|
||||
pub fn from_literal(literal: &ast::Literal, block: &mut IRBlock) -> Self {
|
||||
use ast::Literal;
|
||||
match literal {
|
||||
Literal::I32(v) => {
|
||||
let ir_type = IRType::I32;
|
||||
unsafe {
|
||||
let ir_value = LLVMConstInt(
|
||||
ir_type.in_context(module.context),
|
||||
ir_type.in_context(block.function.module.context),
|
||||
mem::transmute(*v as i64),
|
||||
1,
|
||||
);
|
||||
@ -107,14 +102,7 @@ impl<'a> IRModule<'a> {
|
||||
}
|
||||
|
||||
pub fn print_to_string(&mut self) -> Result<&str, std::str::Utf8Error> {
|
||||
unsafe {
|
||||
LLVMVerifyModule(
|
||||
self.module,
|
||||
llvm_sys::analysis::LLVMVerifierFailureAction::LLVMPrintMessageAction,
|
||||
null_mut(),
|
||||
);
|
||||
CStr::from_ptr(LLVMPrintModuleToString(self.module)).to_str()
|
||||
}
|
||||
unsafe { CStr::from_ptr(LLVMPrintModuleToString(self.module)).to_str() }
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,17 +116,18 @@ impl<'a> Drop for IRModule<'a> {
|
||||
}
|
||||
|
||||
pub struct IRFunction<'a, 'b> {
|
||||
pub module: &'b IRModule<'a>,
|
||||
module: &'b mut IRModule<'a>,
|
||||
/// The actual function
|
||||
value: *mut LLVMValue,
|
||||
}
|
||||
|
||||
impl<'a, 'b> IRFunction<'a, 'b> {
|
||||
pub fn new(name: &String, module: &'b IRModule<'a>) -> IRFunction<'a, 'b> {
|
||||
pub fn new(name: &String, module: &'b mut IRModule<'a>) -> IRFunction<'a, 'b> {
|
||||
unsafe {
|
||||
// TODO, fix later!
|
||||
|
||||
let return_type = LLVMInt32TypeInContext(module.context.context);
|
||||
|
||||
let mut argts = [];
|
||||
let func_type =
|
||||
LLVMFunctionType(return_type, argts.as_mut_ptr(), argts.len() as u32, 0);
|
||||
@ -154,15 +143,18 @@ impl<'a, 'b> IRFunction<'a, 'b> {
|
||||
}
|
||||
|
||||
pub struct IRBlock<'a, 'b, 'c> {
|
||||
pub function: &'c IRFunction<'a, 'b>,
|
||||
function: &'a mut IRFunction<'b, 'c>,
|
||||
blockref: *mut LLVMBasicBlock,
|
||||
}
|
||||
|
||||
impl<'a, 'b, 'c> IRBlock<'a, 'b, 'c> {
|
||||
pub fn new(function: &'c IRFunction<'a, 'b>, name: &CStr) -> IRBlock<'a, 'b, 'c> {
|
||||
pub fn new(function: &'a mut IRFunction<'b, 'c>) -> IRBlock<'a, 'b, 'c> {
|
||||
unsafe {
|
||||
let blockref =
|
||||
LLVMCreateBasicBlockInContext(function.module.context.context, name.as_ptr());
|
||||
let blockref = LLVMCreateBasicBlockInContext(
|
||||
function.module.context.context,
|
||||
into_cstring("entryblock").as_ptr(),
|
||||
);
|
||||
LLVMPositionBuilderAtEnd(function.module.context.builder, blockref);
|
||||
|
||||
IRBlock { function, blockref }
|
||||
}
|
||||
@ -174,7 +166,6 @@ impl<'a, 'b, 'c> IRBlock<'a, 'b, 'c> {
|
||||
IRValue(rhs_t, rhs_v): IRValue,
|
||||
) -> Result<IRValue, Error> {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||
if lhs_t == rhs_t {
|
||||
Ok(IRValue(
|
||||
lhs_t,
|
||||
@ -197,7 +188,6 @@ impl<'a, 'b, 'c> IRBlock<'a, 'b, 'c> {
|
||||
IRValue(rhs_t, rhs_v): IRValue,
|
||||
) -> Result<IRValue, Error> {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||
if lhs_t == rhs_t {
|
||||
Ok(IRValue(
|
||||
lhs_t,
|
||||
@ -214,33 +204,8 @@ impl<'a, 'b, 'c> IRBlock<'a, 'b, 'c> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn less_than(
|
||||
&mut self,
|
||||
IRValue(lhs_t, lhs_v): IRValue,
|
||||
IRValue(rhs_t, rhs_v): IRValue,
|
||||
) -> Result<IRValue, Error> {
|
||||
pub fn add_return(self, value: Option<IRValue>) {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||
if lhs_t == rhs_t {
|
||||
Ok(IRValue(
|
||||
IRType::Boolean,
|
||||
LLVMBuildICmp(
|
||||
self.function.module.context.builder,
|
||||
llvm_sys::LLVMIntPredicate::LLVMIntULT,
|
||||
lhs_v,
|
||||
rhs_v,
|
||||
c"IntULT".as_ptr(),
|
||||
),
|
||||
))
|
||||
} else {
|
||||
Err(Error::TypeMismatch(lhs_t, rhs_t))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_return(&mut self, value: Option<IRValue>) {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||
if let Some(IRValue(_, value)) = value {
|
||||
LLVMBuildRet(self.function.module.context.builder, value);
|
||||
} else {
|
||||
@ -248,30 +213,6 @@ impl<'a, 'b, 'c> IRBlock<'a, 'b, 'c> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn branch(
|
||||
&mut self,
|
||||
IRValue(_, condition): IRValue,
|
||||
then_block: &mut IRBlock,
|
||||
else_block: &mut IRBlock,
|
||||
) {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||
LLVMBuildCondBr(
|
||||
self.function.module.context.builder,
|
||||
condition,
|
||||
then_block.blockref,
|
||||
else_block.blockref,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_into(&mut self, block: &mut IRBlock) {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||
LLVMBuildBr(self.function.module.context.builder, block.blockref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, 'c> Drop for IRBlock<'a, 'b, 'c> {
|
||||
|
@ -6,7 +6,7 @@ use llvm::{Error, IRBlock, IRContext, IRFunction, IRModule, IRValue};
|
||||
|
||||
use crate::{
|
||||
ast::{
|
||||
Block, BlockLevelStatement, Expression, ExpressionKind, FunctionDefinition, IfExpression,
|
||||
BinaryOperator, Block, BlockLevelStatement, Expression, ExpressionKind, FunctionDefinition,
|
||||
LetStatement, ReturnType,
|
||||
},
|
||||
TopLevelStatement,
|
||||
@ -41,30 +41,21 @@ impl TopLevelStatement {
|
||||
impl FunctionDefinition {
|
||||
fn codegen(&self, scope: &mut ScopeData, module: &mut IRModule) {
|
||||
let FunctionDefinition(signature, block, _) = self;
|
||||
let ir_function = IRFunction::new(&signature.name, module);
|
||||
|
||||
let ir_block = IRBlock::new(&ir_function, c"entry");
|
||||
let mut scope = scope.inner(ir_block);
|
||||
if let Some((_, val)) = block.codegen(&mut scope) {
|
||||
scope.block.add_return(Some(val));
|
||||
} else {
|
||||
scope.block.add_return(None);
|
||||
}
|
||||
let mut ir_function = IRFunction::new(&signature.name, module);
|
||||
let ir_block = IRBlock::new(&mut ir_function);
|
||||
block.codegen(scope.with_block(ir_block));
|
||||
}
|
||||
}
|
||||
|
||||
impl Block {
|
||||
#[must_use]
|
||||
fn codegen(&self, scope: &mut Scope) -> Option<(ReturnType, IRValue)> {
|
||||
fn codegen(&self, mut scope: Scope) {
|
||||
for statement in &self.0 {
|
||||
statement.codegen(scope);
|
||||
statement.codegen(&mut scope);
|
||||
}
|
||||
|
||||
if let Some((ret_type, return_exp)) = &self.1 {
|
||||
let value = return_exp.codegen(scope);
|
||||
Some((*ret_type, value))
|
||||
} else {
|
||||
None
|
||||
if let Some((_, return_exp)) = &self.1 {
|
||||
let value = return_exp.codegen(&mut scope);
|
||||
scope.block.add_return(Some(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -92,7 +83,7 @@ impl Expression {
|
||||
|
||||
use ExpressionKind::*;
|
||||
match kind {
|
||||
Literal(lit) => IRValue::from_literal(lit, &scope.block.function.module),
|
||||
Literal(lit) => IRValue::from_literal(lit, &mut scope.block),
|
||||
VariableName(v) => scope.data.fetch(v),
|
||||
Binop(op, lhs, rhs) => {
|
||||
let lhs = lhs.codegen(scope);
|
||||
@ -101,30 +92,10 @@ impl Expression {
|
||||
match op {
|
||||
Add => scope.block.add(lhs, rhs).unwrap(),
|
||||
Mult => scope.block.mult(lhs, rhs).unwrap(),
|
||||
LessThan => scope.block.less_than(lhs, rhs).unwrap(),
|
||||
_ => panic!("operator not supported: {:?}", op),
|
||||
}
|
||||
}
|
||||
IfExpr(ifx) => {
|
||||
let IfExpression(expr, block, _) = ifx.as_ref();
|
||||
let condition = expr.codegen(scope);
|
||||
|
||||
let mut thenb = IRBlock::new(scope.block.function, c"then");
|
||||
let mut afterb = IRBlock::new(scope.block.function, c"merge");
|
||||
|
||||
scope.block.branch(condition, &mut thenb, &mut afterb);
|
||||
scope.block = afterb;
|
||||
|
||||
let mut then = scope.inner(thenb);
|
||||
match block.codegen(&mut then) {
|
||||
Some((ReturnType::Hard, v)) => then.block.add_return(Some(v)),
|
||||
_ => then.block.move_into(&mut scope.block),
|
||||
}
|
||||
|
||||
IRValue::from_literal(&crate::ast::Literal::I32(1), scope.block.function.module)
|
||||
}
|
||||
BlockExpr(_) => panic!("block expr not supported"),
|
||||
FunctionCall(_) => panic!("function call expr not supported"),
|
||||
_ => panic!("expression type not supported"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -141,12 +112,11 @@ impl ScopeData {
|
||||
}
|
||||
}
|
||||
|
||||
fn with_block<'a, 'b, 'c>(self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> {
|
||||
Scope { data: self, block }
|
||||
}
|
||||
|
||||
fn inner<'a, 'b, 'c>(&self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> {
|
||||
self.clone().with_block(block)
|
||||
fn with_block<'a, 'b, 'c>(&self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> {
|
||||
Scope {
|
||||
data: self.clone(),
|
||||
block,
|
||||
}
|
||||
}
|
||||
|
||||
fn fetch(&self, name: &String) -> IRValue {
|
||||
@ -168,9 +138,3 @@ struct Scope<'a, 'b, 'c> {
|
||||
data: ScopeData,
|
||||
block: IRBlock<'a, 'b, 'c>,
|
||||
}
|
||||
|
||||
impl<'a, 'b, 'c> Scope<'a, 'b, 'c> {
|
||||
fn inner(&self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> {
|
||||
self.data.clone().with_block(block)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user