Compare commits
33 Commits
47b9d7e044
...
0392c293ba
Author | SHA1 | Date | |
---|---|---|---|
0392c293ba | |||
61ba00ea58 | |||
a66fc61c07 | |||
8a32e66ba8 | |||
05c585d47c | |||
7b93ab5d2e | |||
814b816450 | |||
5b23d7d4d5 | |||
740aee1382 | |||
02d8b37424 | |||
85564c74c9 | |||
54f25481f4 | |||
922afaa672 | |||
d0aa3e1410 | |||
5f93b7c9c2 | |||
6d3d0fd03e | |||
a3642f127c | |||
7f3a3ac1f8 | |||
e21f47e34b | |||
8defa39b31 | |||
6a4c30e49e | |||
8b8cd2c464 | |||
6448b0c438 | |||
e00d9afc7b | |||
7208fe962e | |||
525dab2147 | |||
9b5d8acdb4 | |||
22ee941ad6 | |||
9a74158ae7 | |||
dd3e0618ae | |||
8176dc98a3 | |||
c5c9cd3458 | |||
c6e6e1dbee |
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,3 +1,7 @@
|
||||
|
||||
src/old_llvm
|
||||
/target
|
||||
/.vscode
|
||||
.env
|
||||
.env
|
||||
hello.*
|
||||
main
|
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -1,6 +1,6 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
@ -98,6 +98,15 @@ checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2"
|
||||
[[package]]
|
||||
name = "reid"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"llvm-sys",
|
||||
"reid-lib",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "reid-lib"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"llvm-sys",
|
||||
"thiserror",
|
||||
|
17
Cargo.toml
17
Cargo.toml
@ -1,12 +1,5 @@
|
||||
[package]
|
||||
name = "reid"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
## LLVM Bindings
|
||||
llvm-sys = "160"
|
||||
## Make it easier to generate errors
|
||||
thiserror = "1.0.44"
|
||||
[workspace]
|
||||
members = [
|
||||
"reid",
|
||||
"reid-llvm-lib"
|
||||
]
|
@ -1,12 +0,0 @@
|
||||
// Arithmetic, function calls and imports!
|
||||
|
||||
import std::print;
|
||||
|
||||
fn main() {
|
||||
let test = 5;
|
||||
let simpleAdd = 2 + 2;
|
||||
let arithmetic = 3 + 2 * 5 + 1 * 2;
|
||||
let multiplier = 5 * 2;
|
||||
|
||||
return arithmetic + multiplier * arithmetic;
|
||||
}
|
46
libtest.sh
Executable file
46
libtest.sh
Executable file
@ -0,0 +1,46 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Compiles example libtest, which produces hello.o and hello.asm, which is then
|
||||
# compiled with main.cpp and executed for final result
|
||||
#
|
||||
# Do note this file is extremely simply for my own personal convenience
|
||||
|
||||
export .env
|
||||
cargo run --example $1 && \
|
||||
# clang hello.o -o main && \
|
||||
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
|
||||
-o main /usr/lib/crt1.o hello.o -lc && \
|
||||
./main ; echo "Return value: ""$?"
|
||||
|
||||
|
||||
## Command from: clang -v hello.o -o test
|
||||
## Original command:
|
||||
# ld --hash-style=gnu \
|
||||
# --build-id \
|
||||
# --eh-frame-hdr \
|
||||
# -m elf_x86_64 \
|
||||
# -pie \
|
||||
# -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
|
||||
# -o test \
|
||||
# /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/15.1.1/../../../../lib64/Scrt1.o \
|
||||
# /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/15.1.1/../../../../lib64/crti.o \
|
||||
# /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/15.1.1/crtbeginS.o \
|
||||
# -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/15.1.1 \
|
||||
# -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/15.1.1/../../../../lib64 \
|
||||
# -L/lib/../lib64 \
|
||||
# -L/usr/lib/../lib64 \
|
||||
# -L/lib \
|
||||
# -L/usr/lib \
|
||||
# hello.o \
|
||||
# -lgcc \
|
||||
# --as-needed \
|
||||
# -lgcc_s \
|
||||
# --no-as-needed \
|
||||
# -lc \
|
||||
# -lgcc \
|
||||
# --as-needed \
|
||||
# -lgcc_s \
|
||||
# --no-as-needed \
|
||||
# /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/15.1.1/crtendS.o \
|
||||
# /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/15.1.1/../../../../lib64/crtn.o \
|
||||
# && \
|
7
main.cpp
Normal file
7
main.cpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include <iostream>
|
||||
|
||||
extern "C" {
|
||||
int mainfunc();
|
||||
}
|
||||
|
||||
int main() { std::cout << "Return value of test: " << mainfunc() << std::endl; }
|
12
reid-llvm-lib/Cargo.toml
Normal file
12
reid-llvm-lib/Cargo.toml
Normal file
@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "reid-lib"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
## LLVM Bindings
|
||||
llvm-sys = "160"
|
||||
## Make it easier to generate errors
|
||||
thiserror = "1.0.44"
|
105
reid-llvm-lib/examples/libtest.rs
Normal file
105
reid-llvm-lib/examples/libtest.rs
Normal file
@ -0,0 +1,105 @@
|
||||
use reid_lib::{
|
||||
Context, IntPredicate,
|
||||
types::{BasicType, IntegerValue, Value},
|
||||
};
|
||||
|
||||
pub fn main() {
|
||||
// Notes from inkwell:
|
||||
// - Creating new values should probably just be functions in the context
|
||||
// - Creating functions should probably be functions from module
|
||||
// - Builder could well be it's own struct
|
||||
// - Although, I do like the fact where blocks move the builder by itself..
|
||||
|
||||
let context = Context::new();
|
||||
|
||||
let module = context.module("testmodule");
|
||||
|
||||
let int_32 = context.type_i32();
|
||||
|
||||
let fibonacci = module.add_function(int_32.function_type(vec![int_32.into()]), "fibonacci");
|
||||
let mut f_main = fibonacci.block("main");
|
||||
|
||||
let param = fibonacci
|
||||
.get_param::<IntegerValue>(0, int_32.into())
|
||||
.unwrap();
|
||||
let mut cmp = f_main
|
||||
.integer_compare(¶m, &int_32.from_unsigned(3), &IntPredicate::ULT, "cmp")
|
||||
.unwrap();
|
||||
|
||||
let mut done = fibonacci.block("done");
|
||||
let mut recurse = fibonacci.block("recurse");
|
||||
f_main.conditional_br(&cmp, &done, &recurse).unwrap();
|
||||
|
||||
done.ret(&int_32.from_unsigned(1)).unwrap();
|
||||
|
||||
let minus_one = recurse
|
||||
.sub(¶m, &int_32.from_unsigned(1), "minus_one")
|
||||
.unwrap();
|
||||
let minus_two = recurse
|
||||
.sub(¶m, &int_32.from_unsigned(2), "minus_two")
|
||||
.unwrap();
|
||||
let one: IntegerValue = recurse
|
||||
.call(&fibonacci, vec![Value::Integer(minus_one)], "call_one")
|
||||
.unwrap();
|
||||
let two = recurse
|
||||
.call(&fibonacci, vec![Value::Integer(minus_two)], "call_two")
|
||||
.unwrap();
|
||||
|
||||
let add = recurse.add(&one, &two, "add").unwrap();
|
||||
|
||||
recurse.ret(&add).unwrap();
|
||||
|
||||
let main_f = module.add_function(int_32.function_type(Vec::new()), "main");
|
||||
|
||||
let mut main_b = main_f.block("main");
|
||||
let call: IntegerValue = main_b
|
||||
.call(
|
||||
&fibonacci,
|
||||
vec![Value::Integer(int_32.from_unsigned(8))],
|
||||
"fib_call",
|
||||
)
|
||||
.unwrap();
|
||||
main_b.ret(&call).unwrap();
|
||||
|
||||
// let secondary = module.add_function(int_32.function_type(&[]), "secondary");
|
||||
// let s_entry = secondary.block("entry");
|
||||
// s_entry.ret(&int_32.from_signed(54)).unwrap();
|
||||
|
||||
// let function = module.add_function(int_32.function_type(&[]), "main");
|
||||
|
||||
// let entry = function.block("entry");
|
||||
|
||||
// let call = entry.call(&secondary, vec![], "call").unwrap();
|
||||
// let add = entry.add(&int_32.from_signed(100), &call, "add").unwrap();
|
||||
// let rhs_cmp = int_32.from_signed(200);
|
||||
|
||||
// let cond_res = entry
|
||||
// .integer_compare(&add, &rhs_cmp, &IntPredicate::SLT, "cmp")
|
||||
// .unwrap();
|
||||
|
||||
// let (lhs, rhs) = entry.conditional_br(&cond_res, "lhs", "rhs").unwrap();
|
||||
|
||||
// let left = lhs.add(&call, &int_32.from_signed(20), "add").unwrap();
|
||||
// let right = rhs.add(&call, &int_32.from_signed(30), "add").unwrap();
|
||||
|
||||
// let final_block = function.block("final");
|
||||
// let phi = final_block
|
||||
// .phi::<IntegerValue>(&int_32, "phi")
|
||||
// .unwrap()
|
||||
// .add_incoming(&left, &lhs)
|
||||
// .add_incoming(&right, &rhs)
|
||||
// .build();
|
||||
|
||||
// lhs.br(&final_block).unwrap();
|
||||
// rhs.br(&final_block).unwrap();
|
||||
|
||||
// let val = final_block
|
||||
// .add(&phi, &int_32.from_signed(11), "add")
|
||||
// .unwrap();
|
||||
// final_block.ret(&val).unwrap();
|
||||
|
||||
match module.print_to_string() {
|
||||
Ok(v) => println!("{}", v),
|
||||
Err(e) => println!("Err: {:?}", e),
|
||||
}
|
||||
}
|
454
reid-llvm-lib/src/lib.rs
Normal file
454
reid-llvm-lib/src/lib.rs
Normal file
@ -0,0 +1,454 @@
|
||||
use std::ffi::CString;
|
||||
use std::marker::PhantomData;
|
||||
use std::net::Incoming;
|
||||
use std::ptr::null_mut;
|
||||
|
||||
use llvm_sys::analysis::LLVMVerifyModule;
|
||||
use llvm_sys::target::{
|
||||
LLVM_InitializeAllAsmParsers, LLVM_InitializeAllAsmPrinters, LLVM_InitializeAllTargetInfos,
|
||||
LLVM_InitializeAllTargetMCs, LLVM_InitializeAllTargets, LLVMSetModuleDataLayout,
|
||||
};
|
||||
use llvm_sys::target_machine::{
|
||||
LLVMCodeGenFileType, LLVMCreateTargetDataLayout, LLVMCreateTargetMachine,
|
||||
LLVMGetDefaultTargetTriple, LLVMGetTargetFromTriple, LLVMTargetMachineEmitToFile,
|
||||
};
|
||||
use llvm_sys::{LLVMBuilder, LLVMContext, LLVMIntPredicate, core::*, prelude::*};
|
||||
use types::{BasicType, BasicValue, FunctionType, IntegerType, Value};
|
||||
use util::{ErrorMessageHolder, from_cstring, into_cstring};
|
||||
|
||||
pub mod types;
|
||||
mod util;
|
||||
|
||||
pub enum IntPredicate {
|
||||
SLT,
|
||||
SGT,
|
||||
|
||||
ULT,
|
||||
UGT,
|
||||
}
|
||||
|
||||
impl IntPredicate {
|
||||
pub fn as_llvm(&self) -> LLVMIntPredicate {
|
||||
match *self {
|
||||
Self::SLT => LLVMIntPredicate::LLVMIntSLT,
|
||||
Self::SGT => LLVMIntPredicate::LLVMIntSGT,
|
||||
Self::ULT => LLVMIntPredicate::LLVMIntULT,
|
||||
Self::UGT => LLVMIntPredicate::LLVMIntUGT,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Context {
|
||||
pub(crate) context_ref: *mut LLVMContext,
|
||||
pub(crate) builder_ref: *mut LLVMBuilder,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn new() -> Context {
|
||||
unsafe {
|
||||
// Set up a context, module and builder in that context.
|
||||
let context = LLVMContextCreate();
|
||||
let builder = LLVMCreateBuilderInContext(context);
|
||||
|
||||
Context {
|
||||
context_ref: context,
|
||||
builder_ref: builder,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn type_i1<'a>(&'a self) -> IntegerType<'a> {
|
||||
IntegerType::in_context(&self, 1)
|
||||
}
|
||||
|
||||
pub fn type_i8<'a>(&'a self) -> IntegerType<'a> {
|
||||
IntegerType::in_context(&self, 8)
|
||||
}
|
||||
|
||||
pub fn type_i16<'a>(&'a self) -> IntegerType<'a> {
|
||||
IntegerType::in_context(&self, 16)
|
||||
}
|
||||
|
||||
pub fn type_i32<'a>(&'a self) -> IntegerType<'a> {
|
||||
IntegerType::in_context(&self, 32)
|
||||
}
|
||||
|
||||
pub fn module(&self, name: &str) -> Module {
|
||||
Module::with_name(self, name)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Context {
|
||||
fn drop(&mut self) {
|
||||
// Clean up. Values created in the context mostly get cleaned up there.
|
||||
unsafe {
|
||||
LLVMDisposeBuilder(self.builder_ref);
|
||||
LLVMContextDispose(self.context_ref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Module<'ctx> {
|
||||
context: &'ctx Context,
|
||||
module_ref: LLVMModuleRef,
|
||||
name: CString,
|
||||
}
|
||||
|
||||
impl<'ctx> Module<'ctx> {
|
||||
fn with_name(context: &'ctx Context, name: &str) -> Module<'ctx> {
|
||||
unsafe {
|
||||
let cstring_name = into_cstring(name);
|
||||
let module_ref =
|
||||
LLVMModuleCreateWithNameInContext(cstring_name.as_ptr(), context.context_ref);
|
||||
Module {
|
||||
context,
|
||||
module_ref,
|
||||
name: cstring_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_function(&'ctx self, fn_type: FunctionType<'ctx>, name: &str) -> Function<'ctx> {
|
||||
unsafe {
|
||||
let name_cstring = into_cstring(name);
|
||||
let function_ref =
|
||||
LLVMAddFunction(self.module_ref, name_cstring.as_ptr(), fn_type.llvm_type());
|
||||
Function {
|
||||
module: self,
|
||||
fn_type,
|
||||
name: name_cstring,
|
||||
fn_ref: function_ref,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_to_string(&self) -> Result<String, String> {
|
||||
unsafe {
|
||||
LLVM_InitializeAllTargets();
|
||||
LLVM_InitializeAllTargetInfos();
|
||||
LLVM_InitializeAllTargetMCs();
|
||||
LLVM_InitializeAllAsmParsers();
|
||||
LLVM_InitializeAllAsmPrinters();
|
||||
|
||||
let triple = LLVMGetDefaultTargetTriple();
|
||||
|
||||
let mut target: _ = null_mut();
|
||||
let mut err = ErrorMessageHolder::null();
|
||||
LLVMGetTargetFromTriple(triple, &mut target, err.borrow_mut());
|
||||
println!("{:?}, {:?}", from_cstring(triple), target);
|
||||
err.into_result().unwrap();
|
||||
|
||||
let target_machine = LLVMCreateTargetMachine(
|
||||
target,
|
||||
triple,
|
||||
c"generic".as_ptr(),
|
||||
c"".as_ptr(),
|
||||
llvm_sys::target_machine::LLVMCodeGenOptLevel::LLVMCodeGenLevelNone,
|
||||
llvm_sys::target_machine::LLVMRelocMode::LLVMRelocDefault,
|
||||
llvm_sys::target_machine::LLVMCodeModel::LLVMCodeModelDefault,
|
||||
);
|
||||
|
||||
let data_layout = LLVMCreateTargetDataLayout(target_machine);
|
||||
LLVMSetTarget(self.module_ref, triple);
|
||||
LLVMSetModuleDataLayout(self.module_ref, data_layout);
|
||||
|
||||
let mut err = ErrorMessageHolder::null();
|
||||
LLVMVerifyModule(
|
||||
self.module_ref,
|
||||
llvm_sys::analysis::LLVMVerifierFailureAction::LLVMPrintMessageAction,
|
||||
err.borrow_mut(),
|
||||
);
|
||||
err.into_result().unwrap();
|
||||
|
||||
let mut err = ErrorMessageHolder::null();
|
||||
LLVMTargetMachineEmitToFile(
|
||||
target_machine,
|
||||
self.module_ref,
|
||||
CString::new("hello.asm").unwrap().into_raw(),
|
||||
LLVMCodeGenFileType::LLVMAssemblyFile,
|
||||
err.borrow_mut(),
|
||||
);
|
||||
err.into_result().unwrap();
|
||||
|
||||
let mut err = ErrorMessageHolder::null();
|
||||
LLVMTargetMachineEmitToFile(
|
||||
target_machine,
|
||||
self.module_ref,
|
||||
CString::new("hello.o").unwrap().into_raw(),
|
||||
LLVMCodeGenFileType::LLVMObjectFile,
|
||||
err.borrow_mut(),
|
||||
);
|
||||
err.into_result().unwrap();
|
||||
|
||||
from_cstring(LLVMPrintModuleToString(self.module_ref)).ok_or("UTF-8 error".to_owned())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for Module<'a> {
|
||||
fn drop(&mut self) {
|
||||
// Clean up. Values created in the context mostly get cleaned up there.
|
||||
unsafe {
|
||||
LLVMDisposeModule(self.module_ref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Function<'ctx> {
|
||||
module: &'ctx Module<'ctx>,
|
||||
name: CString,
|
||||
fn_type: FunctionType<'ctx>,
|
||||
fn_ref: LLVMValueRef,
|
||||
}
|
||||
|
||||
impl<'ctx> Function<'ctx> {
|
||||
pub fn block<T: Into<String>>(&'ctx self, name: T) -> BasicBlock<'ctx> {
|
||||
BasicBlock::in_function(&self, name.into())
|
||||
}
|
||||
|
||||
pub fn get_param<T: BasicValue<'ctx>>(
|
||||
&'ctx self,
|
||||
nth: usize,
|
||||
param_type: T::BaseType,
|
||||
) -> Result<T, String> {
|
||||
if let Some(actual_type) = self.fn_type.param_types.iter().nth(nth) {
|
||||
if param_type.llvm_type() != *actual_type {
|
||||
return Err(String::from("Wrong type"));
|
||||
}
|
||||
} else {
|
||||
return Err(String::from("nth too large"));
|
||||
}
|
||||
unsafe { Ok(T::from_llvm(LLVMGetParam(self.fn_ref, nth as u32))) }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BasicBlock<'ctx> {
|
||||
function: &'ctx Function<'ctx>,
|
||||
builder_ref: LLVMBuilderRef,
|
||||
name: String,
|
||||
blockref: LLVMBasicBlockRef,
|
||||
inserted: bool,
|
||||
}
|
||||
|
||||
impl<'ctx> BasicBlock<'ctx> {
|
||||
fn in_function(function: &'ctx Function<'ctx>, name: String) -> BasicBlock<'ctx> {
|
||||
unsafe {
|
||||
let block_name = into_cstring(name.clone());
|
||||
let block_ref = LLVMCreateBasicBlockInContext(
|
||||
function.module.context.context_ref,
|
||||
block_name.as_ptr(),
|
||||
);
|
||||
LLVMAppendExistingBasicBlock(function.fn_ref, block_ref);
|
||||
BasicBlock {
|
||||
function: function,
|
||||
builder_ref: function.module.context.builder_ref,
|
||||
name,
|
||||
blockref: block_ref,
|
||||
inserted: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn integer_compare<T: BasicValue<'ctx>>(
|
||||
&self,
|
||||
lhs: &T,
|
||||
rhs: &T,
|
||||
comparison: &IntPredicate,
|
||||
name: &str,
|
||||
) -> Result<T, ()> {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.builder_ref, self.blockref);
|
||||
let value = LLVMBuildICmp(
|
||||
self.builder_ref,
|
||||
comparison.as_llvm(),
|
||||
lhs.llvm_value(),
|
||||
rhs.llvm_value(),
|
||||
into_cstring(name).as_ptr(),
|
||||
);
|
||||
|
||||
Ok(T::from_llvm(value))
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn call<T: BasicValue<'ctx>>(
|
||||
&self,
|
||||
callee: &Function<'ctx>,
|
||||
params: Vec<Value<'ctx>>,
|
||||
name: &str,
|
||||
) -> Result<T, ()> {
|
||||
if params.len() != callee.fn_type.param_types.len() {
|
||||
return Err(()); // TODO invalid amount of parameters
|
||||
}
|
||||
for (t1, t2) in callee.fn_type.param_types.iter().zip(¶ms) {
|
||||
if t1 != &t2.llvm_type() {
|
||||
return Err(()); // TODO wrong types in parameters
|
||||
}
|
||||
}
|
||||
if !T::BaseType::is_type(callee.fn_type.return_type) {
|
||||
return Err(()); // TODO wrong return type
|
||||
}
|
||||
unsafe {
|
||||
let mut param_list: Vec<LLVMValueRef> = params.iter().map(|p| p.llvm_value()).collect();
|
||||
LLVMPositionBuilderAtEnd(self.builder_ref, self.blockref);
|
||||
let ret_val = LLVMBuildCall2(
|
||||
self.builder_ref,
|
||||
callee.fn_type.llvm_type(),
|
||||
callee.fn_ref,
|
||||
param_list.as_mut_ptr(),
|
||||
param_list.len() as u32,
|
||||
into_cstring(name).as_ptr(),
|
||||
);
|
||||
Ok(T::from_llvm(ret_val))
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn add<T: BasicValue<'ctx>>(&self, lhs: &T, rhs: &T, name: &str) -> Result<T, ()> {
|
||||
if lhs.llvm_type() != rhs.llvm_type() {
|
||||
return Err(()); // TODO error
|
||||
}
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.builder_ref, self.blockref);
|
||||
let add_value_ref = LLVMBuildAdd(
|
||||
self.builder_ref,
|
||||
lhs.llvm_value(),
|
||||
rhs.llvm_value(),
|
||||
into_cstring(name).as_ptr(),
|
||||
);
|
||||
Ok(T::from_llvm(add_value_ref))
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn sub<T: BasicValue<'ctx>>(&self, lhs: &T, rhs: &T, name: &str) -> Result<T, ()> {
|
||||
dbg!(lhs, rhs);
|
||||
dbg!(lhs.llvm_type(), rhs.llvm_type());
|
||||
if lhs.llvm_type() != rhs.llvm_type() {
|
||||
return Err(()); // TODO error
|
||||
}
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.builder_ref, self.blockref);
|
||||
let add_value_ref = LLVMBuildSub(
|
||||
self.builder_ref,
|
||||
lhs.llvm_value(),
|
||||
rhs.llvm_value(),
|
||||
into_cstring(name).as_ptr(),
|
||||
);
|
||||
Ok(T::from_llvm(add_value_ref))
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn phi<PhiValue: BasicValue<'ctx>>(
|
||||
&self,
|
||||
phi_type: &PhiValue::BaseType,
|
||||
name: &str,
|
||||
) -> Result<PhiBuilder<'ctx, PhiValue>, ()> {
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.builder_ref, self.blockref);
|
||||
let phi_node = LLVMBuildPhi(
|
||||
self.builder_ref,
|
||||
phi_type.llvm_type(),
|
||||
into_cstring(name).as_ptr(),
|
||||
);
|
||||
Ok(PhiBuilder::new(phi_node))
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn br(&mut self, into: &BasicBlock<'ctx>) -> Result<(), ()> {
|
||||
self.try_insert()?;
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.builder_ref, self.blockref);
|
||||
LLVMBuildBr(self.builder_ref, into.blockref);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn conditional_br<T: BasicValue<'ctx>>(
|
||||
&mut self,
|
||||
condition: &T,
|
||||
lhs: &BasicBlock<'ctx>,
|
||||
rhs: &BasicBlock<'ctx>,
|
||||
) -> Result<(), ()> {
|
||||
self.try_insert()?;
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.builder_ref, self.blockref);
|
||||
LLVMBuildCondBr(
|
||||
self.builder_ref,
|
||||
condition.llvm_value(),
|
||||
lhs.blockref,
|
||||
rhs.blockref,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn ret<T: BasicValue<'ctx>>(&mut self, return_value: &T) -> Result<(), ()> {
|
||||
if self.function.fn_type.return_type != return_value.llvm_type() {
|
||||
return Err(());
|
||||
}
|
||||
self.try_insert()?;
|
||||
|
||||
unsafe {
|
||||
LLVMPositionBuilderAtEnd(self.builder_ref, self.blockref);
|
||||
LLVMBuildRet(self.builder_ref, return_value.llvm_value());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn try_insert(&mut self) -> Result<(), ()> {
|
||||
if self.inserted {
|
||||
return Err(());
|
||||
}
|
||||
self.inserted = true;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> Drop for BasicBlock<'ctx> {
|
||||
fn drop(&mut self) {
|
||||
if !self.inserted {
|
||||
unsafe {
|
||||
LLVMDeleteBasicBlock(self.blockref);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PhiBuilder<'ctx, PhiValue: BasicValue<'ctx>> {
|
||||
phi_node: LLVMValueRef,
|
||||
phantom: PhantomData<&'ctx PhiValue>,
|
||||
}
|
||||
|
||||
impl<'ctx, PhiValue: BasicValue<'ctx>> PhiBuilder<'ctx, PhiValue> {
|
||||
fn new(phi_node: LLVMValueRef) -> PhiBuilder<'ctx, PhiValue> {
|
||||
PhiBuilder {
|
||||
phi_node,
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_incoming(&self, value: &PhiValue, block: &BasicBlock<'ctx>) -> &Self {
|
||||
let mut values = vec![value.llvm_value()];
|
||||
let mut blocks = vec![block.blockref];
|
||||
unsafe {
|
||||
LLVMAddIncoming(
|
||||
self.phi_node,
|
||||
values.as_mut_ptr(),
|
||||
blocks.as_mut_ptr(),
|
||||
values.len() as u32,
|
||||
);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(&self) -> PhiValue {
|
||||
unsafe { PhiValue::from_llvm(self.phi_node) }
|
||||
}
|
||||
}
|
336
reid-llvm-lib/src/types.rs
Normal file
336
reid-llvm-lib/src/types.rs
Normal file
@ -0,0 +1,336 @@
|
||||
use std::{any::Any, marker::PhantomData, ptr::null_mut};
|
||||
|
||||
use llvm_sys::{
|
||||
LLVMTypeKind,
|
||||
core::*,
|
||||
prelude::{LLVMTypeRef, LLVMValueRef},
|
||||
};
|
||||
|
||||
use crate::{BasicBlock, Context, PhiBuilder};
|
||||
|
||||
pub trait BasicType<'ctx> {
|
||||
fn llvm_type(&self) -> LLVMTypeRef;
|
||||
|
||||
fn is_type(llvm_type: LLVMTypeRef) -> bool
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
unsafe fn from_llvm(context: &'ctx Context, llvm_type: LLVMTypeRef) -> Self
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
fn function_type(&self, params: Vec<TypeEnum>) -> FunctionType<'ctx> {
|
||||
unsafe {
|
||||
let mut typerefs: Vec<LLVMTypeRef> = params.iter().map(|b| b.llvm_type()).collect();
|
||||
let param_ptr = typerefs.as_mut_ptr();
|
||||
let param_len = typerefs.len();
|
||||
FunctionType {
|
||||
phantom: PhantomData,
|
||||
return_type: self.llvm_type(),
|
||||
param_types: typerefs,
|
||||
type_ref: LLVMFunctionType(self.llvm_type(), param_ptr, param_len as u32, 0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn array_type(&'ctx self, length: u32) -> ArrayType<'ctx>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
ArrayType {
|
||||
phantom: PhantomData,
|
||||
element_type: self.llvm_type(),
|
||||
length,
|
||||
type_ref: unsafe { LLVMArrayType(self.llvm_type(), length) },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> PartialEq for &dyn BasicType<'ctx> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.llvm_type() == other.llvm_type()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> PartialEq<LLVMTypeRef> for &dyn BasicType<'ctx> {
|
||||
fn eq(&self, other: &LLVMTypeRef) -> bool {
|
||||
self.llvm_type() == *other
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct IntegerType<'ctx> {
|
||||
context: &'ctx Context,
|
||||
type_ref: LLVMTypeRef,
|
||||
}
|
||||
|
||||
impl<'ctx> BasicType<'ctx> for IntegerType<'ctx> {
|
||||
fn llvm_type(&self) -> LLVMTypeRef {
|
||||
self.type_ref
|
||||
}
|
||||
|
||||
unsafe fn from_llvm(context: &'ctx Context, llvm_type: LLVMTypeRef) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
IntegerType {
|
||||
context,
|
||||
type_ref: llvm_type,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_type(llvm_type: LLVMTypeRef) -> bool {
|
||||
unsafe { LLVMGetTypeKind(llvm_type) == LLVMTypeKind::LLVMIntegerTypeKind }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> IntegerType<'ctx> {
|
||||
pub(crate) fn in_context(context: &Context, width: u32) -> IntegerType {
|
||||
let type_ref = unsafe {
|
||||
match width {
|
||||
128 => LLVMInt128TypeInContext(context.context_ref),
|
||||
64 => LLVMInt64TypeInContext(context.context_ref),
|
||||
32 => LLVMInt32TypeInContext(context.context_ref),
|
||||
16 => LLVMInt16TypeInContext(context.context_ref),
|
||||
8 => LLVMInt8TypeInContext(context.context_ref),
|
||||
1 => LLVMInt1TypeInContext(context.context_ref),
|
||||
_ => LLVMIntTypeInContext(context.context_ref, width),
|
||||
}
|
||||
};
|
||||
IntegerType { context, type_ref }
|
||||
}
|
||||
|
||||
pub fn from_signed(&self, value: i64) -> IntegerValue<'ctx> {
|
||||
self.from_const(value as u64, true)
|
||||
}
|
||||
|
||||
pub fn from_unsigned(&self, value: i64) -> IntegerValue<'ctx> {
|
||||
self.from_const(value as u64, false)
|
||||
}
|
||||
|
||||
fn from_const(&self, value: u64, sign: bool) -> IntegerValue<'ctx> {
|
||||
unsafe {
|
||||
IntegerValue::from_llvm(LLVMConstInt(
|
||||
self.type_ref,
|
||||
value,
|
||||
match sign {
|
||||
true => 1,
|
||||
false => 0,
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FunctionType<'ctx> {
|
||||
phantom: PhantomData<&'ctx ()>,
|
||||
pub(crate) return_type: LLVMTypeRef,
|
||||
pub(crate) param_types: Vec<LLVMTypeRef>,
|
||||
type_ref: LLVMTypeRef,
|
||||
}
|
||||
|
||||
impl<'ctx> BasicType<'ctx> for FunctionType<'ctx> {
|
||||
fn llvm_type(&self) -> LLVMTypeRef {
|
||||
self.type_ref
|
||||
}
|
||||
|
||||
unsafe fn from_llvm(_context: &'ctx Context, fn_type: LLVMTypeRef) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
unsafe {
|
||||
let param_count = LLVMCountParamTypes(fn_type);
|
||||
let param_types_ptr: *mut LLVMTypeRef = null_mut();
|
||||
LLVMGetParamTypes(fn_type, param_types_ptr);
|
||||
let param_types: Vec<LLVMTypeRef> =
|
||||
std::slice::from_raw_parts(param_types_ptr, param_count as usize)
|
||||
.iter()
|
||||
.map(|t| *t)
|
||||
.collect();
|
||||
FunctionType {
|
||||
phantom: PhantomData,
|
||||
return_type: LLVMGetReturnType(fn_type),
|
||||
param_types,
|
||||
type_ref: fn_type,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_type(llvm_type: LLVMTypeRef) -> bool {
|
||||
unsafe { LLVMGetTypeKind(llvm_type) == LLVMTypeKind::LLVMFunctionTypeKind }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct ArrayType<'ctx> {
|
||||
phantom: PhantomData<&'ctx ()>,
|
||||
element_type: LLVMTypeRef,
|
||||
length: u32,
|
||||
type_ref: LLVMTypeRef,
|
||||
}
|
||||
|
||||
impl<'ctx> BasicType<'ctx> for ArrayType<'ctx> {
|
||||
fn llvm_type(&self) -> LLVMTypeRef {
|
||||
self.type_ref
|
||||
}
|
||||
|
||||
unsafe fn from_llvm(context: &'ctx Context, llvm_type: LLVMTypeRef) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
unsafe {
|
||||
let length = LLVMGetArrayLength(llvm_type);
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
fn is_type(llvm_type: LLVMTypeRef) -> bool {
|
||||
unsafe { LLVMGetTypeKind(llvm_type) == LLVMTypeKind::LLVMArrayTypeKind }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum TypeEnum<'ctx> {
|
||||
Integer(IntegerType<'ctx>),
|
||||
Array(ArrayType<'ctx>),
|
||||
Function(FunctionType<'ctx>),
|
||||
}
|
||||
|
||||
impl<'ctx> From<IntegerType<'ctx>> for TypeEnum<'ctx> {
|
||||
fn from(int: IntegerType<'ctx>) -> Self {
|
||||
TypeEnum::Integer(int)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> From<ArrayType<'ctx>> for TypeEnum<'ctx> {
|
||||
fn from(arr: ArrayType<'ctx>) -> Self {
|
||||
TypeEnum::Array(arr)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> From<FunctionType<'ctx>> for TypeEnum<'ctx> {
|
||||
fn from(func: FunctionType<'ctx>) -> Self {
|
||||
TypeEnum::Function(func)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> TypeEnum<'ctx> {
|
||||
fn inner_basic(&'ctx self) -> &'ctx dyn BasicType<'ctx> {
|
||||
match self {
|
||||
TypeEnum::Integer(integer_type) => integer_type,
|
||||
TypeEnum::Array(array_type) => array_type,
|
||||
TypeEnum::Function(function_type) => function_type,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> BasicType<'ctx> for TypeEnum<'ctx> {
|
||||
fn llvm_type(&self) -> LLVMTypeRef {
|
||||
self.inner_basic().llvm_type()
|
||||
}
|
||||
|
||||
fn is_type(llvm_type: LLVMTypeRef) -> bool
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
true
|
||||
}
|
||||
|
||||
unsafe fn from_llvm(context: &'ctx Context, llvm_type: LLVMTypeRef) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
unsafe {
|
||||
match LLVMGetTypeKind(llvm_type) {
|
||||
LLVMTypeKind::LLVMIntegerTypeKind => {
|
||||
TypeEnum::Integer(IntegerType::from_llvm(context, llvm_type))
|
||||
}
|
||||
LLVMTypeKind::LLVMArrayTypeKind => {
|
||||
TypeEnum::Array(ArrayType::from_llvm(context, llvm_type))
|
||||
}
|
||||
LLVMTypeKind::LLVMFunctionTypeKind => {
|
||||
TypeEnum::Function(FunctionType::from_llvm(context, llvm_type))
|
||||
}
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait BasicValue<'ctx>: std::fmt::Debug {
|
||||
type BaseType: BasicType<'ctx>;
|
||||
unsafe fn from_llvm(value: LLVMValueRef) -> Self
|
||||
where
|
||||
Self: Sized;
|
||||
fn llvm_value(&self) -> LLVMValueRef;
|
||||
fn llvm_type(&self) -> LLVMTypeRef;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct IntegerValue<'ctx> {
|
||||
phantom: PhantomData<&'ctx ()>,
|
||||
pub(crate) value_ref: LLVMValueRef,
|
||||
}
|
||||
|
||||
impl<'ctx> BasicValue<'ctx> for IntegerValue<'ctx> {
|
||||
type BaseType = IntegerType<'ctx>;
|
||||
|
||||
unsafe fn from_llvm(value: LLVMValueRef) -> Self {
|
||||
IntegerValue {
|
||||
phantom: PhantomData,
|
||||
value_ref: value,
|
||||
}
|
||||
}
|
||||
|
||||
fn llvm_value(&self) -> LLVMValueRef {
|
||||
self.value_ref
|
||||
}
|
||||
|
||||
fn llvm_type(&self) -> LLVMTypeRef {
|
||||
unsafe { LLVMTypeOf(self.value_ref) }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Value<'ctx> {
|
||||
Integer(IntegerValue<'ctx>),
|
||||
}
|
||||
|
||||
impl<'ctx> BasicValue<'ctx> for Value<'ctx> {
|
||||
type BaseType = TypeEnum<'ctx>;
|
||||
|
||||
unsafe fn from_llvm(value: LLVMValueRef) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
unsafe {
|
||||
use LLVMTypeKind::*;
|
||||
|
||||
let llvm_type = LLVMTypeOf(value);
|
||||
let type_kind = LLVMGetTypeKind(llvm_type);
|
||||
match type_kind {
|
||||
LLVMIntegerTypeKind => Value::Integer(IntegerValue::from_llvm(value)),
|
||||
_ => panic!("asd"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn llvm_value(&self) -> LLVMValueRef {
|
||||
match self {
|
||||
Self::Integer(i) => i.llvm_value(),
|
||||
}
|
||||
}
|
||||
|
||||
fn llvm_type(&self) -> LLVMTypeRef {
|
||||
match self {
|
||||
Self::Integer(i) => i.llvm_type(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> From<IntegerValue<'ctx>> for Value<'ctx> {
|
||||
fn from(value: IntegerValue<'ctx>) -> Self {
|
||||
Value::Integer(value)
|
||||
}
|
||||
}
|
51
reid-llvm-lib/src/util.rs
Normal file
51
reid-llvm-lib/src/util.rs
Normal file
@ -0,0 +1,51 @@
|
||||
use std::{
|
||||
ffi::{CStr, CString, c_char},
|
||||
ptr::null_mut,
|
||||
};
|
||||
|
||||
use llvm_sys::error::LLVMDisposeErrorMessage;
|
||||
|
||||
pub 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 fn from_cstring(pointer: *mut c_char) -> Option<String> {
|
||||
if pointer.is_null() {
|
||||
None
|
||||
} else {
|
||||
unsafe { CStr::from_ptr(pointer).to_str().ok().map(|s| s.to_owned()) }
|
||||
}
|
||||
}
|
||||
|
||||
fn cstring_to_err(value: *mut c_char) -> Result<(), String> {
|
||||
from_cstring(value)
|
||||
.filter(|s| !s.is_empty())
|
||||
.map_or(Ok(()), |s| Err(s))
|
||||
}
|
||||
|
||||
pub struct ErrorMessageHolder(*mut c_char);
|
||||
|
||||
impl ErrorMessageHolder {
|
||||
pub fn null() -> Self {
|
||||
ErrorMessageHolder(null_mut())
|
||||
}
|
||||
|
||||
pub fn borrow_mut(&mut self) -> *mut *mut c_char {
|
||||
&mut self.0
|
||||
}
|
||||
|
||||
pub fn into_result(&self) -> Result<(), String> {
|
||||
cstring_to_err(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ErrorMessageHolder {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
if !self.0.is_null() {
|
||||
LLVMDisposeErrorMessage(self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
13
reid/Cargo.toml
Normal file
13
reid/Cargo.toml
Normal file
@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "reid"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
## LLVM Bindings
|
||||
llvm-sys = "160"
|
||||
## Make it easier to generate errors
|
||||
thiserror = "1.0.44"
|
||||
reid-lib = { path = "../reid-llvm-lib" }
|
11
reid/examples/arithmetic.rs
Normal file
11
reid/examples/arithmetic.rs
Normal file
@ -0,0 +1,11 @@
|
||||
use reid::compile;
|
||||
|
||||
pub static ARITHMETIC: &str = include_str!("./reid/arithmetic.reid");
|
||||
|
||||
fn main() {
|
||||
let text = match compile(ARITHMETIC) {
|
||||
Ok(t) => t,
|
||||
Err(e) => panic!("{}", e),
|
||||
};
|
||||
println!("{}", text);
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
use reid::compile;
|
||||
|
||||
pub static EASIEST: &str = include_str!("./reid/easiest.reid");
|
||||
pub static FIBONACCI: &str = include_str!("./reid/fibonacci.reid");
|
||||
|
||||
fn main() {
|
||||
let text = match compile(EASIEST) {
|
||||
let text = match compile(FIBONACCI) {
|
||||
Ok(t) => t,
|
||||
Err(e) => panic!("{}", e),
|
||||
};
|
13
reid/examples/reid/arithmetic.reid
Normal file
13
reid/examples/reid/arithmetic.reid
Normal file
@ -0,0 +1,13 @@
|
||||
// Arithmetic, function calls and imports!
|
||||
|
||||
fn main() {
|
||||
let test = 9;
|
||||
let simpleAdd = 2 + 2;
|
||||
let simpleSub = 7 - 2; // 14
|
||||
|
||||
if simpleAdd < test {
|
||||
return 3;
|
||||
}
|
||||
|
||||
return arithmetic + simpleSub + boop;
|
||||
}
|
@ -1,17 +1,9 @@
|
||||
// Hello, comment here!
|
||||
|
||||
import std::print;
|
||||
|
||||
// Main
|
||||
fn main() {
|
||||
let hello = 32 + {
|
||||
2 + 3
|
||||
};
|
||||
let beep = hello + fibonacci();
|
||||
return beep;
|
||||
return fibonacci(10);
|
||||
}
|
||||
|
||||
// Fibonacci
|
||||
|
||||
fn fibonacci(value: i32) -> i32 {
|
||||
if value < 3 {
|
||||
return 1;
|
171
reid/examples/testcodegen.rs
Normal file
171
reid/examples/testcodegen.rs
Normal file
@ -0,0 +1,171 @@
|
||||
use reid::mir::*;
|
||||
use reid_lib::Context;
|
||||
|
||||
fn main() {
|
||||
let fibonacci_name = "fibonacci".to_owned();
|
||||
let fibonacci_n = "N".to_owned();
|
||||
|
||||
let fibonacci = FunctionDefinition {
|
||||
name: fibonacci_name.clone(),
|
||||
parameters: vec![(fibonacci_n.clone(), TypeKind::I32)],
|
||||
kind: FunctionDefinitionKind::Local(
|
||||
Block {
|
||||
statements: vec![Statement(
|
||||
StmtKind::Expression(Expression(
|
||||
ExprKind::If(IfExpression(
|
||||
// If N < 3
|
||||
Box::new(Expression(
|
||||
ExprKind::BinOp(
|
||||
BinaryOperator::Logic(LogicOperator::GreaterThan),
|
||||
Box::new(Expression(
|
||||
ExprKind::Variable(VariableReference(
|
||||
TypeKind::I32,
|
||||
"N".to_string(),
|
||||
Default::default(),
|
||||
)),
|
||||
Default::default(),
|
||||
)),
|
||||
Box::new(Expression(
|
||||
ExprKind::Literal(Literal::I32(2)),
|
||||
Default::default(),
|
||||
)),
|
||||
),
|
||||
Default::default(),
|
||||
)),
|
||||
// Then
|
||||
Block {
|
||||
statements: vec![],
|
||||
return_expression: Some((
|
||||
ReturnKind::Hard,
|
||||
// return fibonacci(n-1) + fibonacci(n-2)
|
||||
Box::new(Expression(
|
||||
ExprKind::BinOp(
|
||||
BinaryOperator::Add,
|
||||
// fibonacci(n-1)
|
||||
Box::new(Expression(
|
||||
ExprKind::FunctionCall(FunctionCall {
|
||||
name: fibonacci_name.clone(),
|
||||
return_type: TypeKind::I32,
|
||||
parameters: vec![Expression(
|
||||
ExprKind::BinOp(
|
||||
BinaryOperator::Minus,
|
||||
Box::new(Expression(
|
||||
ExprKind::Variable(
|
||||
VariableReference(
|
||||
TypeKind::I32,
|
||||
fibonacci_n.clone(),
|
||||
Default::default(),
|
||||
),
|
||||
),
|
||||
Default::default(),
|
||||
)),
|
||||
Box::new(Expression(
|
||||
ExprKind::Literal(Literal::I32(1)),
|
||||
Default::default(),
|
||||
)),
|
||||
),
|
||||
Default::default(),
|
||||
)],
|
||||
}),
|
||||
Default::default(),
|
||||
)),
|
||||
// fibonacci(n-2)
|
||||
Box::new(Expression(
|
||||
ExprKind::FunctionCall(FunctionCall {
|
||||
name: fibonacci_name.clone(),
|
||||
return_type: TypeKind::I32,
|
||||
parameters: vec![Expression(
|
||||
ExprKind::BinOp(
|
||||
BinaryOperator::Minus,
|
||||
Box::new(Expression(
|
||||
ExprKind::Variable(
|
||||
VariableReference(
|
||||
TypeKind::I32,
|
||||
fibonacci_n.clone(),
|
||||
Default::default(),
|
||||
),
|
||||
),
|
||||
Default::default(),
|
||||
)),
|
||||
Box::new(Expression(
|
||||
ExprKind::Literal(Literal::I32(2)),
|
||||
Default::default(),
|
||||
)),
|
||||
),
|
||||
Default::default(),
|
||||
)],
|
||||
}),
|
||||
Default::default(),
|
||||
)),
|
||||
),
|
||||
Default::default(),
|
||||
)),
|
||||
)),
|
||||
meta: Default::default(),
|
||||
},
|
||||
// No else-block
|
||||
None,
|
||||
)),
|
||||
Default::default(),
|
||||
)),
|
||||
Default::default(),
|
||||
)],
|
||||
// return 1
|
||||
return_expression: Some((
|
||||
ReturnKind::Soft,
|
||||
Box::new(Expression(
|
||||
ExprKind::Literal(Literal::I32(1)),
|
||||
Default::default(),
|
||||
)),
|
||||
)),
|
||||
meta: Default::default(),
|
||||
},
|
||||
Default::default(),
|
||||
),
|
||||
};
|
||||
|
||||
let main = FunctionDefinition {
|
||||
name: "main".to_owned(),
|
||||
parameters: vec![],
|
||||
kind: FunctionDefinitionKind::Local(
|
||||
Block {
|
||||
statements: vec![],
|
||||
return_expression: Some((
|
||||
ReturnKind::Soft,
|
||||
Box::new(Expression(
|
||||
ExprKind::FunctionCall(FunctionCall {
|
||||
name: fibonacci_name.clone(),
|
||||
return_type: TypeKind::I32,
|
||||
parameters: vec![Expression(
|
||||
ExprKind::Literal(Literal::I32(5)),
|
||||
Default::default(),
|
||||
)],
|
||||
}),
|
||||
Default::default(),
|
||||
)),
|
||||
)),
|
||||
meta: Default::default(),
|
||||
},
|
||||
Default::default(),
|
||||
),
|
||||
};
|
||||
|
||||
println!("test1");
|
||||
|
||||
let module = Module {
|
||||
name: "test module".to_owned(),
|
||||
imports: vec![],
|
||||
functions: vec![fibonacci, main],
|
||||
};
|
||||
|
||||
println!("test2");
|
||||
let context = Context::new();
|
||||
let codegen_module = module.codegen(&context);
|
||||
|
||||
println!("test3");
|
||||
|
||||
match codegen_module.module.print_to_string() {
|
||||
Ok(v) => println!("{}", v),
|
||||
Err(e) => println!("Err: {:?}", e),
|
||||
}
|
||||
}
|
109
reid/src/ast/mod.rs
Normal file
109
reid/src/ast/mod.rs
Normal file
@ -0,0 +1,109 @@
|
||||
use crate::token_stream::TokenRange;
|
||||
|
||||
pub mod parse;
|
||||
pub mod process;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Type(pub TypeKind, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum TypeKind {
|
||||
I32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Literal {
|
||||
I32(i32),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Expression(pub ExpressionKind, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ExpressionKind {
|
||||
VariableName(String),
|
||||
Literal(Literal),
|
||||
Binop(BinaryOperator, Box<Expression>, Box<Expression>),
|
||||
FunctionCall(Box<FunctionCallExpression>),
|
||||
BlockExpr(Box<Block>),
|
||||
IfExpr(Box<IfExpression>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum BinaryOperator {
|
||||
Add,
|
||||
Minus,
|
||||
Mult,
|
||||
|
||||
And,
|
||||
LessThan,
|
||||
}
|
||||
|
||||
impl BinaryOperator {
|
||||
pub fn get_precedence(&self) -> i8 {
|
||||
use BinaryOperator::*;
|
||||
match &self {
|
||||
Add => 10,
|
||||
Minus => 10,
|
||||
Mult => 20,
|
||||
And => 100,
|
||||
LessThan => 100,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FunctionCallExpression(pub String, pub Vec<Expression>, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IfExpression(pub Expression, pub Block, pub Option<Block>, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LetStatement(pub String, pub Expression, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ImportStatement(Vec<String>, pub TokenRange);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FunctionDefinition(pub FunctionSignature, pub Block, pub TokenRange);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FunctionSignature {
|
||||
pub name: String,
|
||||
pub args: Vec<(String, Type)>,
|
||||
pub return_type: Option<Type>,
|
||||
pub range: TokenRange,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ReturnType {
|
||||
Soft,
|
||||
Hard,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Block(
|
||||
pub Vec<BlockLevelStatement>,
|
||||
pub Option<(ReturnType, Expression)>,
|
||||
pub TokenRange,
|
||||
);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BlockLevelStatement {
|
||||
Let(LetStatement),
|
||||
Import(ImportStatement),
|
||||
Expression(Expression),
|
||||
Return(ReturnType, Expression),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TopLevelStatement {
|
||||
Import(ImportStatement),
|
||||
FunctionDefinition(FunctionDefinition),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Module {
|
||||
pub name: String,
|
||||
pub top_level_statements: Vec<TopLevelStatement>,
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
use crate::ast::*;
|
||||
use crate::{
|
||||
lexer::Token,
|
||||
token_stream::{Error, TokenStream},
|
||||
token_stream::{Error, TokenRange, TokenStream},
|
||||
};
|
||||
|
||||
pub trait Parse
|
||||
@ -10,39 +11,21 @@ where
|
||||
fn parse(stream: TokenStream) -> Result<Self, Error>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Type {
|
||||
I32,
|
||||
}
|
||||
|
||||
impl Parse for Type {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
if let Some(Token::Identifier(ident)) = stream.next() {
|
||||
let kind = if let Some(Token::Identifier(ident)) = stream.next() {
|
||||
Ok(match &*ident {
|
||||
"i32" => Type::I32,
|
||||
"i32" => TypeKind::I32,
|
||||
_ => panic!("asd"),
|
||||
})
|
||||
} else {
|
||||
Err(stream.expected_err("type identifier")?)
|
||||
}
|
||||
}?;
|
||||
|
||||
Ok(Type(kind, stream.get_range().unwrap()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Literal {
|
||||
I32(i32),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Expression {
|
||||
VariableName(String),
|
||||
Literal(Literal),
|
||||
Binop(BinaryOperator, Box<Expression>, Box<Expression>),
|
||||
FunctionCall(Box<FunctionCallExpression>),
|
||||
BlockExpr(Box<Block>),
|
||||
IfExpr(Box<IfExpression>),
|
||||
}
|
||||
|
||||
impl Parse for Expression {
|
||||
fn parse(mut stream: TokenStream) -> Result<Expression, Error> {
|
||||
let lhs = parse_primary_expression(&mut stream)?;
|
||||
@ -51,16 +34,32 @@ impl Parse for Expression {
|
||||
}
|
||||
|
||||
fn parse_primary_expression(stream: &mut TokenStream) -> Result<Expression, Error> {
|
||||
use ExpressionKind as Kind;
|
||||
|
||||
if let Ok(exp) = stream.parse() {
|
||||
Ok(Expression::FunctionCall(Box::new(exp)))
|
||||
Ok(Expression(
|
||||
Kind::FunctionCall(Box::new(exp)),
|
||||
stream.get_range().unwrap(),
|
||||
))
|
||||
} else if let Ok(block) = stream.parse() {
|
||||
Ok(Expression::BlockExpr(Box::new(block)))
|
||||
Ok(Expression(
|
||||
Kind::BlockExpr(Box::new(block)),
|
||||
stream.get_range().unwrap(),
|
||||
))
|
||||
} else if let Ok(ifexpr) = stream.parse() {
|
||||
Ok(Expression::IfExpr(Box::new(ifexpr)))
|
||||
Ok(Expression(
|
||||
Kind::IfExpr(Box::new(ifexpr)),
|
||||
stream.get_range().unwrap(),
|
||||
))
|
||||
} else if let Some(token) = stream.next() {
|
||||
Ok(match &token {
|
||||
Token::Identifier(v) => Expression::VariableName(v.clone()),
|
||||
Token::DecimalValue(v) => Expression::Literal(Literal::I32(v.parse().unwrap())),
|
||||
Token::Identifier(v) => {
|
||||
Expression(Kind::VariableName(v.clone()), stream.get_range().unwrap())
|
||||
}
|
||||
Token::DecimalValue(v) => Expression(
|
||||
Kind::Literal(Literal::I32(v.parse().unwrap())),
|
||||
stream.get_range().unwrap(),
|
||||
),
|
||||
Token::ParenOpen => {
|
||||
let exp = stream.parse()?;
|
||||
stream.expect(Token::ParenClose)?;
|
||||
@ -81,49 +80,43 @@ fn parse_primary_expression(stream: &mut TokenStream) -> Result<Expression, Erro
|
||||
fn parse_binop_rhs(
|
||||
stream: &mut TokenStream,
|
||||
mut lhs: Expression,
|
||||
mut operator: Option<BinaryOperator>,
|
||||
mut prev_operator: Option<BinaryOperator>,
|
||||
) -> Result<Expression, Error> {
|
||||
let expr_prec = if let Some(op) = operator {
|
||||
op.get_precedence() + 1
|
||||
// Expression precedence = LHS precedence so far.
|
||||
let expr_precedence = if let Some(op) = prev_operator.take() {
|
||||
op.get_precedence()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
while let Some(op) = operator.take().as_ref().or(stream.parse().as_ref().ok()) {
|
||||
while let Ok(op) =
|
||||
// If next operator precedence is lower than expression precedence, we
|
||||
// need to climb back up the recursion.
|
||||
stream.parse_if::<BinaryOperator, _>(|b| b.get_precedence() >= expr_precedence)
|
||||
{
|
||||
let curr_token_prec = op.get_precedence();
|
||||
let mut rhs = parse_primary_expression(stream)?;
|
||||
|
||||
if curr_token_prec < expr_prec {
|
||||
break; // Just return lhs
|
||||
} else {
|
||||
let mut rhs = parse_primary_expression(stream)?;
|
||||
if let Ok(next_op) = stream.parse::<BinaryOperator>() {
|
||||
let next_prec = next_op.get_precedence();
|
||||
if curr_token_prec < next_prec {
|
||||
// Operator on the right of rhs has more precedence, turn
|
||||
// rhs into lhs for new binop
|
||||
rhs = parse_binop_rhs(stream, rhs, Some(next_op))?;
|
||||
} else {
|
||||
let _ = operator.insert(next_op);
|
||||
}
|
||||
if let Ok(next_op) = stream.parse_peek::<BinaryOperator>() {
|
||||
let next_prec = next_op.get_precedence();
|
||||
if curr_token_prec < next_prec {
|
||||
// Operator on the right of rhs has more precedence, turn
|
||||
// rhs into lhs for new binop
|
||||
rhs = parse_binop_rhs(stream, rhs, Some(op))?;
|
||||
} else {
|
||||
let _ = prev_operator.insert(next_op);
|
||||
}
|
||||
|
||||
lhs = Expression::Binop(*op, Box::new(lhs), Box::new(rhs));
|
||||
}
|
||||
|
||||
lhs = Expression(
|
||||
ExpressionKind::Binop(op, Box::new(lhs), Box::new(rhs)),
|
||||
stream.get_range().unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(lhs)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum BinaryOperator {
|
||||
Add,
|
||||
Minus,
|
||||
Mult,
|
||||
|
||||
And,
|
||||
LessThan,
|
||||
}
|
||||
|
||||
impl Parse for BinaryOperator {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
Ok(match (stream.next(), stream.peek()) {
|
||||
@ -141,22 +134,6 @@ impl Parse for BinaryOperator {
|
||||
}
|
||||
}
|
||||
|
||||
impl BinaryOperator {
|
||||
pub fn get_precedence(&self) -> i8 {
|
||||
use BinaryOperator::*;
|
||||
match &self {
|
||||
Add => 10,
|
||||
Minus => 10,
|
||||
Mult => 20,
|
||||
And => 100,
|
||||
LessThan => 100,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FunctionCallExpression(pub String, pub Vec<Expression>);
|
||||
|
||||
impl Parse for FunctionCallExpression {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
if let Some(Token::Identifier(name)) = stream.next() {
|
||||
@ -174,26 +151,29 @@ impl Parse for FunctionCallExpression {
|
||||
|
||||
stream.expect(Token::ParenClose)?;
|
||||
|
||||
Ok(FunctionCallExpression(name, args))
|
||||
Ok(FunctionCallExpression(
|
||||
name,
|
||||
args,
|
||||
stream.get_range().unwrap(),
|
||||
))
|
||||
} else {
|
||||
Err(stream.expected_err("identifier")?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IfExpression(Expression, pub Block);
|
||||
|
||||
impl Parse for IfExpression {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
stream.expect(Token::If)?;
|
||||
Ok(IfExpression(stream.parse()?, stream.parse()?))
|
||||
Ok(IfExpression(
|
||||
stream.parse()?,
|
||||
stream.parse()?,
|
||||
None,
|
||||
stream.get_range().unwrap(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LetStatement(pub String, pub Expression);
|
||||
|
||||
impl Parse for LetStatement {
|
||||
fn parse(mut stream: TokenStream) -> Result<LetStatement, Error> {
|
||||
stream.expect(Token::LetKeyword)?;
|
||||
@ -203,16 +183,17 @@ impl Parse for LetStatement {
|
||||
|
||||
let expression = stream.parse()?;
|
||||
stream.expect(Token::Semi)?;
|
||||
Ok(LetStatement(variable, expression))
|
||||
Ok(LetStatement(
|
||||
variable,
|
||||
expression,
|
||||
stream.get_range().unwrap(),
|
||||
))
|
||||
} else {
|
||||
Err(stream.expected_err("identifier")?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ImportStatement(Vec<String>);
|
||||
|
||||
impl Parse for ImportStatement {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
stream.expect(Token::ImportKeyword)?;
|
||||
@ -234,27 +215,21 @@ impl Parse for ImportStatement {
|
||||
|
||||
stream.expect(Token::Semi)?;
|
||||
|
||||
Ok(ImportStatement(import_list))
|
||||
Ok(ImportStatement(import_list, stream.get_range().unwrap()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FunctionDefinition(pub FunctionSignature, pub Block);
|
||||
|
||||
impl Parse for FunctionDefinition {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
stream.expect(Token::FnKeyword)?;
|
||||
Ok(FunctionDefinition(stream.parse()?, stream.parse()?))
|
||||
Ok(FunctionDefinition(
|
||||
stream.parse()?,
|
||||
stream.parse()?,
|
||||
stream.get_range().unwrap(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FunctionSignature {
|
||||
pub name: String,
|
||||
pub args: Vec<(String, Type)>,
|
||||
pub return_type: Option<Type>,
|
||||
}
|
||||
|
||||
impl Parse for FunctionSignature {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
if let Some(Token::Identifier(name)) = stream.next() {
|
||||
@ -278,6 +253,7 @@ impl Parse for FunctionSignature {
|
||||
name,
|
||||
args,
|
||||
return_type,
|
||||
range: stream.get_range().unwrap(),
|
||||
})
|
||||
} else {
|
||||
Err(stream.expected_err("identifier")?)?
|
||||
@ -285,18 +261,6 @@ impl Parse for FunctionSignature {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ReturnType {
|
||||
Soft,
|
||||
Hard,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Block(
|
||||
pub Vec<BlockLevelStatement>,
|
||||
pub Option<(ReturnType, Expression)>,
|
||||
);
|
||||
|
||||
impl Parse for Block {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
let mut statements = Vec::new();
|
||||
@ -307,7 +271,7 @@ impl Parse for Block {
|
||||
if let Some((r_type, e)) = return_stmt.take() {
|
||||
// Special list of expressions that are simply not warned about,
|
||||
// if semicolon is missing.
|
||||
if !matches!(&e, &Expression::IfExpr(_)) {
|
||||
if !matches!(e, Expression(ExpressionKind::IfExpr(_), _)) {
|
||||
dbg!(r_type, &e);
|
||||
println!("Oh no, does this statement lack ;");
|
||||
}
|
||||
@ -315,7 +279,7 @@ impl Parse for Block {
|
||||
statements.push(BlockLevelStatement::Expression(e));
|
||||
}
|
||||
let statement = stream.parse()?;
|
||||
if let BlockLevelStatement::Return((r_type, e)) = &statement {
|
||||
if let BlockLevelStatement::Return(r_type, e) = &statement {
|
||||
match r_type {
|
||||
ReturnType::Hard => {
|
||||
return_stmt = Some((*r_type, e.clone()));
|
||||
@ -331,18 +295,10 @@ impl Parse for Block {
|
||||
statements.push(statement);
|
||||
}
|
||||
stream.expect(Token::BraceClose)?;
|
||||
Ok(Block(statements, return_stmt))
|
||||
Ok(Block(statements, return_stmt, stream.get_range().unwrap()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BlockLevelStatement {
|
||||
Let(LetStatement),
|
||||
Import(ImportStatement),
|
||||
Expression(Expression),
|
||||
Return((ReturnType, Expression)),
|
||||
}
|
||||
|
||||
impl Parse for BlockLevelStatement {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
use BlockLevelStatement as Stmt;
|
||||
@ -353,14 +309,14 @@ impl Parse for BlockLevelStatement {
|
||||
stream.next();
|
||||
let exp = stream.parse()?;
|
||||
stream.expect(Token::Semi)?;
|
||||
Stmt::Return((ReturnType::Hard, exp))
|
||||
Stmt::Return(ReturnType::Hard, exp)
|
||||
}
|
||||
_ => {
|
||||
if let Ok(e) = stream.parse() {
|
||||
if stream.expect(Token::Semi).is_ok() {
|
||||
Stmt::Expression(e)
|
||||
} else {
|
||||
Stmt::Return((ReturnType::Soft, e))
|
||||
Stmt::Return(ReturnType::Soft, e)
|
||||
}
|
||||
} else {
|
||||
Err(stream.expected_err("expression")?)?
|
||||
@ -370,12 +326,6 @@ impl Parse for BlockLevelStatement {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TopLevelStatement {
|
||||
Import(ImportStatement),
|
||||
FunctionDefinition(FunctionDefinition),
|
||||
}
|
||||
|
||||
impl Parse for TopLevelStatement {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
use TopLevelStatement as Stmt;
|
544
reid/src/ast/process.rs
Normal file
544
reid/src/ast/process.rs
Normal file
@ -0,0 +1,544 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
ast,
|
||||
mir::{self, StmtKind, VariableReference},
|
||||
token_stream::TokenRange,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum InferredType {
|
||||
FromVariable(String, TokenRange),
|
||||
FunctionReturn(String, TokenRange),
|
||||
Static(mir::TypeKind, TokenRange),
|
||||
OneOf(Vec<InferredType>),
|
||||
Void(TokenRange),
|
||||
DownstreamError(IntoMIRError, TokenRange),
|
||||
}
|
||||
|
||||
fn all_ok<T, E>(result: Vec<Result<T, E>>) -> Option<Vec<T>> {
|
||||
let mut res = Vec::with_capacity(result.len());
|
||||
for item in result {
|
||||
if let Ok(item) = item {
|
||||
res.push(item);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(res)
|
||||
}
|
||||
|
||||
impl InferredType {
|
||||
fn collapse(
|
||||
&self,
|
||||
state: &mut State,
|
||||
scope: &VirtualScope,
|
||||
) -> Result<mir::TypeKind, IntoMIRError> {
|
||||
match self {
|
||||
InferredType::FromVariable(name, token_range) => {
|
||||
if let Some(inferred) = scope.get_var(name) {
|
||||
let temp = inferred.collapse(state, scope);
|
||||
state.note(temp)
|
||||
} else {
|
||||
state.err(IntoMIRError::VariableNotDefined(name.clone(), *token_range))
|
||||
}
|
||||
}
|
||||
InferredType::FunctionReturn(name, token_range) => {
|
||||
if let Some(type_kind) = scope.get_return_type(name) {
|
||||
Ok(*type_kind)
|
||||
} else {
|
||||
state.err(IntoMIRError::VariableNotDefined(name.clone(), *token_range))
|
||||
}
|
||||
}
|
||||
InferredType::Static(type_kind, _) => Ok(*type_kind),
|
||||
InferredType::OneOf(inferred_types) => {
|
||||
let collapsed = all_ok(
|
||||
inferred_types
|
||||
.iter()
|
||||
.map(|t| {
|
||||
let temp = t.collapse(state, scope);
|
||||
state.note(temp)
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
if let Some(list) = collapsed {
|
||||
if let Some(first) = list.first() {
|
||||
if list.iter().all(|i| i == first) {
|
||||
Ok((*first).into())
|
||||
} else {
|
||||
state.err(IntoMIRError::ConflictingType(self.get_range()))
|
||||
}
|
||||
} else {
|
||||
state.err(IntoMIRError::VoidType(self.get_range()))
|
||||
}
|
||||
} else {
|
||||
state.err(IntoMIRError::DownstreamError(self.get_range()))
|
||||
}
|
||||
}
|
||||
InferredType::Void(token_range) => state.err(IntoMIRError::VoidType(*token_range)),
|
||||
InferredType::DownstreamError(e, _) => state.err(e.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_range(&self) -> TokenRange {
|
||||
match &self {
|
||||
InferredType::FromVariable(_, token_range) => *token_range,
|
||||
InferredType::FunctionReturn(_, token_range) => *token_range,
|
||||
InferredType::Static(_, token_range) => *token_range,
|
||||
InferredType::OneOf(inferred_types) => {
|
||||
inferred_types.iter().map(|i| i.get_range()).sum()
|
||||
}
|
||||
InferredType::Void(token_range) => *token_range,
|
||||
InferredType::DownstreamError(_, range) => *range,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VirtualVariable {
|
||||
name: String,
|
||||
inferred: InferredType,
|
||||
meta: mir::Metadata,
|
||||
}
|
||||
|
||||
pub struct VirtualFunctionSignature {
|
||||
name: String,
|
||||
return_type: mir::TypeKind,
|
||||
parameter_types: Vec<mir::TypeKind>,
|
||||
metadata: mir::Metadata,
|
||||
}
|
||||
|
||||
pub enum VirtualStorageError {
|
||||
KeyAlreadyExists(String),
|
||||
}
|
||||
|
||||
pub struct VirtualStorage<T> {
|
||||
storage: HashMap<String, Vec<T>>,
|
||||
}
|
||||
|
||||
impl<T> VirtualStorage<T> {
|
||||
fn set(&mut self, name: String, value: T) -> Result<(), VirtualStorageError> {
|
||||
let result = if let Some(list) = self.storage.get_mut(&name) {
|
||||
list.push(value);
|
||||
Err(VirtualStorageError::KeyAlreadyExists(name.clone()))
|
||||
} else {
|
||||
self.storage.insert(name, vec![value]);
|
||||
Ok(())
|
||||
};
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn get(&self, name: &String) -> Option<&T> {
|
||||
if let Some(list) = self.storage.get(name) {
|
||||
list.first()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for VirtualStorage<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
storage: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum IntoMIRError {
|
||||
DuplicateVariable(String, TokenRange),
|
||||
DuplicateFunction(String, TokenRange),
|
||||
VariableNotDefined(String, TokenRange),
|
||||
FunctionNotDefined(String, TokenRange),
|
||||
DownstreamError(TokenRange),
|
||||
ConflictingType(TokenRange),
|
||||
VoidType(TokenRange),
|
||||
}
|
||||
|
||||
pub struct VirtualScope {
|
||||
variables: VirtualStorage<VirtualVariable>,
|
||||
functions: VirtualStorage<VirtualFunctionSignature>,
|
||||
}
|
||||
|
||||
impl VirtualScope {
|
||||
pub fn set_var(&mut self, variable: VirtualVariable) -> Result<(), IntoMIRError> {
|
||||
let range = variable.meta.range;
|
||||
match self.variables.set(variable.name.clone(), variable) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(VirtualStorageError::KeyAlreadyExists(n)) => {
|
||||
Err(IntoMIRError::DuplicateVariable(n, range))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_fun(&mut self, function: VirtualFunctionSignature) -> Result<(), IntoMIRError> {
|
||||
let range = function.metadata.range;
|
||||
match self.functions.set(function.name.clone(), function) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(VirtualStorageError::KeyAlreadyExists(n)) => {
|
||||
Err(IntoMIRError::DuplicateVariable(n, range))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_var(&self, name: &String) -> Option<&InferredType> {
|
||||
self.variables.get(name).map(|v| &v.inferred)
|
||||
}
|
||||
|
||||
pub fn get_return_type(&self, name: &String) -> Option<&mir::TypeKind> {
|
||||
self.functions.get(name).map(|v| &v.return_type)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for VirtualScope {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
variables: Default::default(),
|
||||
functions: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct State {
|
||||
errors: Vec<IntoMIRError>,
|
||||
fatal: bool,
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn note<T: std::fmt::Debug>(
|
||||
&mut self,
|
||||
value: Result<T, IntoMIRError>,
|
||||
) -> Result<T, IntoMIRError> {
|
||||
dbg!(&value);
|
||||
if let Err(e) = &value {
|
||||
self.errors.push(e.clone());
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
fn err<T>(&mut self, error: IntoMIRError) -> Result<T, IntoMIRError> {
|
||||
self.errors.push(error.clone());
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for State {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
errors: Default::default(),
|
||||
fatal: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Module {
|
||||
pub fn process(&self) -> mir::Module {
|
||||
let mut state = State::default();
|
||||
let mut scope = VirtualScope::default();
|
||||
|
||||
for stmt in &self.top_level_statements {
|
||||
match stmt {
|
||||
FunctionDefinition(ast::FunctionDefinition(signature, _, range)) => {
|
||||
state.note(scope.set_fun(VirtualFunctionSignature {
|
||||
name: signature.name.clone(),
|
||||
return_type: signature.return_type.into(),
|
||||
parameter_types: signature.args.iter().map(|p| p.1.into()).collect(),
|
||||
metadata: (*range).into(),
|
||||
}));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let mut imports = Vec::new();
|
||||
let mut functions = Vec::new();
|
||||
|
||||
use ast::TopLevelStatement::*;
|
||||
for stmt in &self.top_level_statements {
|
||||
match stmt {
|
||||
Import(import) => {
|
||||
for name in &import.0 {
|
||||
imports.push(mir::Import(name.clone(), import.1.into()));
|
||||
}
|
||||
}
|
||||
FunctionDefinition(ast::FunctionDefinition(signature, block, range)) => {
|
||||
for (name, ptype) in &signature.args {
|
||||
state.note(scope.set_var(VirtualVariable {
|
||||
name: name.clone(),
|
||||
inferred: InferredType::Static((*ptype).into(), *range),
|
||||
meta: ptype.1.into(),
|
||||
}));
|
||||
}
|
||||
|
||||
dbg!(&signature);
|
||||
|
||||
if let Some(mir_block) = block.process(&mut state, &mut scope) {
|
||||
let def = mir::FunctionDefinition {
|
||||
name: signature.name.clone(),
|
||||
parameters: signature
|
||||
.args
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|p| (p.0, p.1.into()))
|
||||
.collect(),
|
||||
kind: mir::FunctionDefinitionKind::Local(mir_block, (*range).into()),
|
||||
};
|
||||
functions.push(def);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dbg!(&state);
|
||||
|
||||
// TODO do something with state here
|
||||
|
||||
mir::Module {
|
||||
name: self.name.clone(),
|
||||
imports,
|
||||
functions,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Block {
|
||||
pub fn process(&self, state: &mut State, scope: &mut VirtualScope) -> Option<mir::Block> {
|
||||
let mut mir_statements = Vec::new();
|
||||
|
||||
for statement in &self.0 {
|
||||
let (kind, range): (Option<mir::StmtKind>, TokenRange) = match statement {
|
||||
ast::BlockLevelStatement::Let(s_let) => {
|
||||
let res = s_let.1.infer_return_type().collapse(state, scope);
|
||||
let collapsed = state.note(res);
|
||||
let inferred = match &collapsed {
|
||||
Ok(t) => InferredType::Static(*t, s_let.2),
|
||||
Err(e) => InferredType::DownstreamError(e.clone(), s_let.2),
|
||||
};
|
||||
state
|
||||
.note(scope.set_var(VirtualVariable {
|
||||
name: s_let.0.clone(),
|
||||
inferred,
|
||||
meta: s_let.2.into(),
|
||||
}))
|
||||
.ok();
|
||||
|
||||
(
|
||||
collapsed.ok().and_then(|t| {
|
||||
s_let.1.process(state, scope).map(|e| {
|
||||
mir::StmtKind::Let(
|
||||
mir::VariableReference(t, s_let.0.clone(), s_let.2.into()),
|
||||
e,
|
||||
)
|
||||
})
|
||||
}),
|
||||
s_let.2,
|
||||
)
|
||||
}
|
||||
ast::BlockLevelStatement::Import(_) => todo!(),
|
||||
ast::BlockLevelStatement::Expression(e) => (
|
||||
e.process(state, scope).map(|e| StmtKind::Expression(e)),
|
||||
e.1,
|
||||
),
|
||||
ast::BlockLevelStatement::Return(_, e) => (
|
||||
e.process(state, scope).map(|e| StmtKind::Expression(e)),
|
||||
e.1,
|
||||
),
|
||||
};
|
||||
|
||||
if let Some(kind) = kind {
|
||||
mir_statements.push(mir::Statement(kind, range.into()));
|
||||
} else {
|
||||
state.fatal = true;
|
||||
}
|
||||
}
|
||||
|
||||
let return_expression = if let Some(r) = &self.1 {
|
||||
if let Some(expr) = r.1.process(state, scope) {
|
||||
Some((r.0.into(), Box::new(expr)))
|
||||
} else {
|
||||
state.fatal = true;
|
||||
None?
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Some(mir::Block {
|
||||
statements: mir_statements,
|
||||
return_expression,
|
||||
meta: self.2.into(),
|
||||
})
|
||||
}
|
||||
|
||||
fn infer_return_type(&self) -> InferredType {
|
||||
self.1
|
||||
.as_ref()
|
||||
.map(|(_, expr)| expr.infer_return_type())
|
||||
.unwrap_or(InferredType::Void(self.2))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ast::ReturnType> for mir::ReturnKind {
|
||||
fn from(value: ast::ReturnType) -> Self {
|
||||
match value {
|
||||
ast::ReturnType::Soft => mir::ReturnKind::Soft,
|
||||
ast::ReturnType::Hard => mir::ReturnKind::Hard,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Expression {
|
||||
fn process(&self, state: &mut State, scope: &mut VirtualScope) -> Option<mir::Expression> {
|
||||
let kind = match &self.0 {
|
||||
ast::ExpressionKind::VariableName(name) => {
|
||||
let ty = scope.get_var(name);
|
||||
if let Some(ty) = ty {
|
||||
let res = ty.collapse(state, scope);
|
||||
state
|
||||
.note(res)
|
||||
.map(|result| {
|
||||
mir::ExprKind::Variable(VariableReference(
|
||||
result,
|
||||
name.clone(),
|
||||
self.1.into(),
|
||||
))
|
||||
})
|
||||
.ok()
|
||||
} else {
|
||||
state
|
||||
.err(IntoMIRError::VariableNotDefined(
|
||||
name.clone(),
|
||||
self.1.into(),
|
||||
))
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
ast::ExpressionKind::Literal(literal) => Some(mir::ExprKind::Literal(literal.mir())),
|
||||
ast::ExpressionKind::Binop(binary_operator, lhs, rhs) => {
|
||||
let mir_lhs = lhs.process(state, scope);
|
||||
let mir_rhs = rhs.process(state, scope);
|
||||
Some(mir::ExprKind::BinOp(
|
||||
binary_operator.mir(),
|
||||
Box::new(mir_lhs?),
|
||||
Box::new(mir_rhs?),
|
||||
))
|
||||
}
|
||||
ast::ExpressionKind::FunctionCall(fn_call_expr) => {
|
||||
if let Some(fn_type) = scope.get_return_type(&fn_call_expr.0).cloned() {
|
||||
let parameters = all_ok(
|
||||
fn_call_expr
|
||||
.1
|
||||
.iter()
|
||||
.map(|e| {
|
||||
e.process(state, scope)
|
||||
.ok_or(IntoMIRError::DownstreamError(self.1.into()))
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
if let Some(parameters) = parameters {
|
||||
Some(mir::ExprKind::FunctionCall(mir::FunctionCall {
|
||||
name: fn_call_expr.0.clone(),
|
||||
return_type: fn_type,
|
||||
parameters,
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
state
|
||||
.err(IntoMIRError::FunctionNotDefined(
|
||||
fn_call_expr.0.clone(),
|
||||
self.1,
|
||||
))
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
ast::ExpressionKind::BlockExpr(block) => {
|
||||
block.process(state, scope).map(|b| mir::ExprKind::Block(b))
|
||||
}
|
||||
ast::ExpressionKind::IfExpr(if_expression) => {
|
||||
let cond = if_expression.0.process(state, scope);
|
||||
let then_block = if_expression.1.process(state, scope);
|
||||
let else_block = if let Some(el) = &if_expression.2 {
|
||||
Some(el.process(state, scope)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Some(mir::ExprKind::If(mir::IfExpression(
|
||||
Box::new(cond?),
|
||||
then_block?,
|
||||
else_block,
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
kind.map(|k| mir::Expression(k, self.1.into()))
|
||||
}
|
||||
|
||||
fn infer_return_type(&self) -> InferredType {
|
||||
use ast::ExpressionKind::*;
|
||||
match &self.0 {
|
||||
VariableName(name) => InferredType::FromVariable(name.clone(), self.1),
|
||||
Literal(lit) => InferredType::Static(lit.mir().as_type(), self.1),
|
||||
Binop(_, lhs, rhs) => {
|
||||
InferredType::OneOf(vec![lhs.infer_return_type(), rhs.infer_return_type()])
|
||||
}
|
||||
FunctionCall(fncall) => InferredType::FunctionReturn(fncall.0.clone(), self.1),
|
||||
BlockExpr(block) => block.infer_return_type(),
|
||||
IfExpr(exp) => {
|
||||
let mut types = vec![exp.1.infer_return_type()];
|
||||
if let Some(e) = &exp.2 {
|
||||
types.push(e.infer_return_type())
|
||||
}
|
||||
InferredType::OneOf(types)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::BinaryOperator {
|
||||
fn mir(&self) -> mir::BinaryOperator {
|
||||
match self {
|
||||
ast::BinaryOperator::Add => mir::BinaryOperator::Add,
|
||||
ast::BinaryOperator::Minus => mir::BinaryOperator::Minus,
|
||||
ast::BinaryOperator::Mult => mir::BinaryOperator::Mult,
|
||||
ast::BinaryOperator::And => mir::BinaryOperator::And,
|
||||
ast::BinaryOperator::LessThan => {
|
||||
mir::BinaryOperator::Logic(mir::LogicOperator::LessThan)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Literal {
|
||||
fn mir(&self) -> mir::Literal {
|
||||
match *self {
|
||||
ast::Literal::I32(v) => mir::Literal::I32(v),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ast::TypeKind> for mir::TypeKind {
|
||||
fn from(value: ast::TypeKind) -> Self {
|
||||
match value {
|
||||
ast::TypeKind::I32 => mir::TypeKind::I32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ast::Type> for mir::TypeKind {
|
||||
fn from(value: ast::Type) -> Self {
|
||||
value.0.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Option<ast::Type>> for mir::TypeKind {
|
||||
fn from(value: Option<ast::Type>) -> Self {
|
||||
match value {
|
||||
Some(v) => v.into(),
|
||||
None => mir::TypeKind::Void,
|
||||
}
|
||||
}
|
||||
}
|
279
reid/src/codegen.rs
Normal file
279
reid/src/codegen.rs
Normal file
@ -0,0 +1,279 @@
|
||||
use std::{collections::HashMap, mem, ops::Deref};
|
||||
|
||||
use crate::mir::{self, types::ReturnType, TypeKind, VariableReference};
|
||||
use reid_lib::{
|
||||
types::{BasicType, BasicValue, IntegerValue, TypeEnum, Value},
|
||||
BasicBlock, Context, Function, IntPredicate, Module,
|
||||
};
|
||||
|
||||
pub struct ModuleCodegen<'ctx> {
|
||||
context: &'ctx Context,
|
||||
pub module: Module<'ctx>,
|
||||
}
|
||||
|
||||
impl mir::Module {
|
||||
pub fn codegen<'ctx>(&self, context: &'ctx Context) -> ModuleCodegen<'ctx> {
|
||||
let module = context.module(&self.name);
|
||||
|
||||
let mut functions = HashMap::new();
|
||||
|
||||
for function in &self.functions {
|
||||
let ret_type = function.return_type().unwrap().get_type(&context);
|
||||
let fn_type = ret_type.function_type(
|
||||
function
|
||||
.parameters
|
||||
.iter()
|
||||
.map(|(_, p)| p.get_type(&context))
|
||||
.collect(),
|
||||
);
|
||||
|
||||
let func = match &function.kind {
|
||||
mir::FunctionDefinitionKind::Local(_, _) => {
|
||||
module.add_function(fn_type, &function.name)
|
||||
}
|
||||
mir::FunctionDefinitionKind::Extern(_) => todo!(),
|
||||
};
|
||||
functions.insert(function.name.clone(), func);
|
||||
}
|
||||
|
||||
for mir_function in &self.functions {
|
||||
let function = functions.get(&mir_function.name).unwrap();
|
||||
|
||||
let mut stack_values = HashMap::new();
|
||||
for (i, (p_name, p_type)) in mir_function.parameters.iter().enumerate() {
|
||||
stack_values.insert(
|
||||
p_name.clone(),
|
||||
function.get_param(i, p_type.get_type(&context)).unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
let mut scope = Scope {
|
||||
context,
|
||||
module: &module,
|
||||
function,
|
||||
block: function.block("entry"),
|
||||
functions: functions.clone(),
|
||||
stack_values,
|
||||
};
|
||||
match &mir_function.kind {
|
||||
mir::FunctionDefinitionKind::Local(block, _) => {
|
||||
if let Some(ret) = block.codegen(&mut scope) {
|
||||
scope.block.ret(&ret).unwrap();
|
||||
}
|
||||
}
|
||||
mir::FunctionDefinitionKind::Extern(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
ModuleCodegen { context, module }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Scope<'ctx> {
|
||||
context: &'ctx Context,
|
||||
module: &'ctx Module<'ctx>,
|
||||
function: &'ctx Function<'ctx>,
|
||||
block: BasicBlock<'ctx>,
|
||||
functions: HashMap<String, Function<'ctx>>,
|
||||
stack_values: HashMap<String, Value<'ctx>>,
|
||||
}
|
||||
|
||||
impl<'ctx> Scope<'ctx> {
|
||||
pub fn with_block(&self, block: BasicBlock<'ctx>) -> Scope<'ctx> {
|
||||
Scope {
|
||||
block,
|
||||
context: self.context,
|
||||
function: self.function,
|
||||
module: self.module,
|
||||
functions: self.functions.clone(),
|
||||
stack_values: self.stack_values.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes the block out from this scope, swaps the given block in it's place
|
||||
/// and returns the old block.
|
||||
pub fn swap_block(&mut self, block: BasicBlock<'ctx>) -> BasicBlock<'ctx> {
|
||||
let mut old_block = block;
|
||||
mem::swap(&mut self.block, &mut old_block);
|
||||
old_block
|
||||
}
|
||||
}
|
||||
|
||||
impl mir::Statement {
|
||||
pub fn codegen<'ctx>(&self, scope: &mut Scope<'ctx>) -> Option<Value<'ctx>> {
|
||||
match &self.0 {
|
||||
mir::StmtKind::Let(VariableReference(_, name, _), expression) => {
|
||||
let value = expression.codegen(scope).unwrap();
|
||||
scope.stack_values.insert(name.clone(), value);
|
||||
None
|
||||
}
|
||||
// mir::StmtKind::If(if_expression) => if_expression.codegen(scope),
|
||||
mir::StmtKind::Import(_) => todo!(),
|
||||
mir::StmtKind::Expression(expression) => expression.codegen(scope),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl mir::IfExpression {
|
||||
pub fn codegen<'ctx>(&self, scope: &mut Scope<'ctx>) -> Option<Value<'ctx>> {
|
||||
let condition = self.0.codegen(scope).unwrap();
|
||||
|
||||
// Create blocks
|
||||
let then_bb = scope.function.block("then");
|
||||
let after_bb = scope.function.block("after");
|
||||
let mut before_bb = scope.swap_block(after_bb);
|
||||
|
||||
let mut then_scope = scope.with_block(then_bb);
|
||||
let then_res = self.1.codegen(&mut then_scope);
|
||||
then_scope.block.br(&scope.block).ok();
|
||||
|
||||
let else_bb = scope.function.block("else");
|
||||
let mut else_scope = scope.with_block(else_bb);
|
||||
|
||||
let else_opt = if let Some(else_block) = &self.2 {
|
||||
before_bb
|
||||
.conditional_br(&condition, &then_scope.block, &else_scope.block)
|
||||
.unwrap();
|
||||
|
||||
let opt = else_block.codegen(&mut else_scope);
|
||||
|
||||
if let Some(ret) = opt {
|
||||
else_scope.block.br(&scope.block).ok();
|
||||
Some((else_scope.block, ret))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
else_scope.block.br(&scope.block).unwrap();
|
||||
before_bb
|
||||
.conditional_br(&condition, &then_scope.block, &scope.block)
|
||||
.unwrap();
|
||||
None
|
||||
};
|
||||
|
||||
if then_res.is_none() && else_opt.is_none() {
|
||||
None
|
||||
} else if let Ok(ret_type) = self.1.return_type() {
|
||||
let phi = scope
|
||||
.block
|
||||
.phi(&ret_type.get_type(scope.context), "phi")
|
||||
.unwrap();
|
||||
if let Some(then_ret) = then_res {
|
||||
phi.add_incoming(&then_ret, &then_scope.block);
|
||||
}
|
||||
if let Some((else_bb, else_ret)) = else_opt {
|
||||
phi.add_incoming(&else_ret, &else_bb);
|
||||
}
|
||||
|
||||
Some(phi.build())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl mir::Expression {
|
||||
pub fn codegen<'ctx>(&self, scope: &mut Scope<'ctx>) -> Option<Value<'ctx>> {
|
||||
match &self.0 {
|
||||
mir::ExprKind::Variable(varref) => {
|
||||
let v = scope
|
||||
.stack_values
|
||||
.get(&varref.1)
|
||||
.expect("Variable reference not found?!");
|
||||
Some(v.clone())
|
||||
}
|
||||
mir::ExprKind::Literal(lit) => Some(lit.codegen(scope.context)),
|
||||
mir::ExprKind::BinOp(binop, lhs_exp, rhs_exp) => {
|
||||
let lhs = lhs_exp.codegen(scope).expect("lhs has no return value");
|
||||
let rhs = rhs_exp.codegen(scope).expect("rhs has no return value");
|
||||
Some(match binop {
|
||||
mir::BinaryOperator::Add => scope.block.add(&lhs, &rhs, "add").unwrap(),
|
||||
mir::BinaryOperator::Minus => scope.block.sub(&lhs, &rhs, "sub").unwrap(),
|
||||
mir::BinaryOperator::Mult => todo!(),
|
||||
mir::BinaryOperator::And => todo!(),
|
||||
mir::BinaryOperator::Logic(l) => {
|
||||
let ret_type = lhs_exp.return_type().expect("No ret type in lhs?");
|
||||
scope
|
||||
.block
|
||||
.integer_compare(&lhs, &rhs, &l.int_predicate(ret_type.signed()), "cmp")
|
||||
.unwrap()
|
||||
}
|
||||
})
|
||||
}
|
||||
mir::ExprKind::FunctionCall(call) => {
|
||||
let params = call
|
||||
.parameters
|
||||
.iter()
|
||||
.map(|e| e.codegen(scope).unwrap())
|
||||
.collect();
|
||||
let callee = scope
|
||||
.functions
|
||||
.get(&call.name)
|
||||
.expect("function not found!");
|
||||
Some(scope.block.call(callee, params, "call").unwrap())
|
||||
}
|
||||
mir::ExprKind::If(if_expression) => if_expression.codegen(scope),
|
||||
mir::ExprKind::Block(block) => {
|
||||
let mut inner_scope = scope.with_block(scope.function.block("inner"));
|
||||
if let Some(ret) = block.codegen(&mut inner_scope) {
|
||||
inner_scope.block.br(&scope.block);
|
||||
Some(ret)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl mir::LogicOperator {
|
||||
fn int_predicate(&self, signed: bool) -> IntPredicate {
|
||||
match (self, signed) {
|
||||
(mir::LogicOperator::LessThan, true) => IntPredicate::SLT,
|
||||
(mir::LogicOperator::GreaterThan, true) => IntPredicate::SGT,
|
||||
(mir::LogicOperator::LessThan, false) => IntPredicate::ULT,
|
||||
(mir::LogicOperator::GreaterThan, false) => IntPredicate::UGT,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl mir::Block {
|
||||
pub fn codegen<'ctx>(&self, mut scope: &mut Scope<'ctx>) -> Option<Value<'ctx>> {
|
||||
for stmt in &self.statements {
|
||||
stmt.codegen(&mut scope);
|
||||
}
|
||||
|
||||
if let Some((kind, expr)) = &self.return_expression {
|
||||
let ret = expr.codegen(&mut scope).unwrap();
|
||||
match kind {
|
||||
mir::ReturnKind::Hard => {
|
||||
scope.block.ret(&ret).unwrap();
|
||||
None
|
||||
}
|
||||
mir::ReturnKind::Soft => Some(ret),
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl mir::Literal {
|
||||
pub fn codegen<'ctx>(&self, context: &'ctx Context) -> Value<'ctx> {
|
||||
let val: IntegerValue<'ctx> = match *self {
|
||||
mir::Literal::I32(val) => context.type_i32().from_signed(val as i64),
|
||||
mir::Literal::I16(val) => context.type_i16().from_signed(val as i64),
|
||||
};
|
||||
Value::Integer(val)
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeKind {
|
||||
fn get_type<'ctx>(&self, context: &'ctx Context) -> TypeEnum<'ctx> {
|
||||
match &self {
|
||||
TypeKind::I32 => TypeEnum::Integer(context.type_i32()),
|
||||
TypeKind::I16 => TypeEnum::Integer(context.type_i16()),
|
||||
TypeKind::Void => panic!("Void not a supported type"),
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +1,11 @@
|
||||
use crate::{
|
||||
ast::TopLevelStatement, codegen::codegen_from_statements, lexer::Token,
|
||||
token_stream::TokenStream,
|
||||
};
|
||||
use reid_lib::Context;
|
||||
|
||||
use crate::{ast::TopLevelStatement, lexer::Token, token_stream::TokenStream};
|
||||
|
||||
mod ast;
|
||||
mod codegen;
|
||||
mod lexer;
|
||||
mod llvm_ir;
|
||||
pub mod mir;
|
||||
mod token_stream;
|
||||
|
||||
// TODO:
|
||||
@ -21,8 +20,8 @@ pub enum ReidError {
|
||||
LexerError(#[from] lexer::Error),
|
||||
#[error(transparent)]
|
||||
ParserError(#[from] token_stream::Error),
|
||||
#[error(transparent)]
|
||||
CodegenError(#[from] codegen::Error),
|
||||
// #[error(transparent)]
|
||||
// CodegenError(#[from] codegen::Error),
|
||||
}
|
||||
|
||||
pub fn compile(source: &str) -> Result<String, ReidError> {
|
||||
@ -40,7 +39,21 @@ pub fn compile(source: &str) -> Result<String, ReidError> {
|
||||
statements.push(statement);
|
||||
}
|
||||
|
||||
let mut module = codegen_from_statements(statements)?;
|
||||
let text = module.print_to_string().unwrap();
|
||||
Ok(text.to_owned())
|
||||
let ast_module = ast::Module {
|
||||
name: "test".to_owned(),
|
||||
top_level_statements: statements,
|
||||
};
|
||||
|
||||
dbg!(&ast_module);
|
||||
let mir_module = ast_module.process();
|
||||
|
||||
dbg!(&mir_module);
|
||||
|
||||
let mut context = Context::new();
|
||||
let cogegen_module = mir_module.codegen(&mut context);
|
||||
|
||||
Ok(match cogegen_module.module.print_to_string() {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("Err: {:?}", e),
|
||||
})
|
||||
}
|
147
reid/src/mir/mod.rs
Normal file
147
reid/src/mir/mod.rs
Normal file
@ -0,0 +1,147 @@
|
||||
/// In this module are defined structs that are used for performing passes on
|
||||
/// Reid. It contains a simplified version of Reid which must already be
|
||||
/// type-checked beforehand.
|
||||
use crate::token_stream::TokenRange;
|
||||
|
||||
pub mod types;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Metadata {
|
||||
pub range: TokenRange,
|
||||
}
|
||||
|
||||
impl From<TokenRange> for Metadata {
|
||||
fn from(value: TokenRange) -> Self {
|
||||
Metadata { range: value }
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Metadata {
|
||||
fn default() -> Self {
|
||||
Metadata {
|
||||
range: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TypeKind {
|
||||
I32,
|
||||
I16,
|
||||
Void,
|
||||
}
|
||||
|
||||
impl TypeKind {
|
||||
pub fn signed(&self) -> bool {
|
||||
match self {
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Literal {
|
||||
I32(i32),
|
||||
I16(i16),
|
||||
}
|
||||
|
||||
impl Literal {
|
||||
pub fn as_type(self: &Literal) -> TypeKind {
|
||||
match self {
|
||||
Literal::I32(_) => TypeKind::I32,
|
||||
Literal::I16(_) => TypeKind::I16,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum BinaryOperator {
|
||||
Add,
|
||||
Minus,
|
||||
Mult,
|
||||
And,
|
||||
Logic(LogicOperator),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum LogicOperator {
|
||||
LessThan,
|
||||
GreaterThan,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ReturnKind {
|
||||
Hard,
|
||||
Soft,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct VariableReference(pub TypeKind, pub String, pub Metadata);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Import(pub String, pub Metadata);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ExprKind {
|
||||
Variable(VariableReference),
|
||||
Literal(Literal),
|
||||
BinOp(BinaryOperator, Box<Expression>, Box<Expression>),
|
||||
FunctionCall(FunctionCall),
|
||||
If(IfExpression),
|
||||
Block(Block),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Expression(pub ExprKind, pub Metadata);
|
||||
|
||||
/// Condition, Then, Else
|
||||
#[derive(Debug)]
|
||||
pub struct IfExpression(pub Box<Expression>, pub Block, pub Option<Block>);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FunctionCall {
|
||||
pub name: String,
|
||||
pub return_type: TypeKind,
|
||||
pub parameters: Vec<Expression>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FunctionDefinition {
|
||||
pub name: String,
|
||||
pub parameters: Vec<(String, TypeKind)>,
|
||||
pub kind: FunctionDefinitionKind,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FunctionDefinitionKind {
|
||||
/// Actual definition block and surrounding signature range
|
||||
Local(Block, Metadata),
|
||||
/// Return Type
|
||||
Extern(TypeKind),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Block {
|
||||
/// List of non-returning statements
|
||||
pub statements: Vec<Statement>,
|
||||
pub return_expression: Option<(ReturnKind, Box<Expression>)>,
|
||||
pub meta: Metadata,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Statement(pub StmtKind, pub Metadata);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum StmtKind {
|
||||
/// Variable name+type, evaluation
|
||||
Let(VariableReference, Expression),
|
||||
Import(Import),
|
||||
Expression(Expression),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Module {
|
||||
pub name: String,
|
||||
pub imports: Vec<Import>,
|
||||
pub functions: Vec<FunctionDefinition>,
|
||||
}
|
74
reid/src/mir/types.rs
Normal file
74
reid/src/mir/types.rs
Normal file
@ -0,0 +1,74 @@
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ReturnTypeOther {
|
||||
Import(TokenRange),
|
||||
Let(TokenRange),
|
||||
EmptyBlock(TokenRange),
|
||||
NoBlockReturn(TokenRange),
|
||||
}
|
||||
|
||||
pub trait ReturnType {
|
||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther>;
|
||||
}
|
||||
|
||||
impl ReturnType for Block {
|
||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||
self.return_expression
|
||||
.as_ref()
|
||||
.ok_or(ReturnTypeOther::NoBlockReturn(self.meta.range))
|
||||
.and_then(|(_, stmt)| stmt.return_type())
|
||||
}
|
||||
}
|
||||
|
||||
impl ReturnType for Statement {
|
||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||
use StmtKind::*;
|
||||
match &self.0 {
|
||||
Expression(e) => e.return_type(),
|
||||
Import(_) => Err(ReturnTypeOther::Import(self.1.range)),
|
||||
Let(_, _) => Err(ReturnTypeOther::Let(self.1.range)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ReturnType for Expression {
|
||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||
use ExprKind::*;
|
||||
match &self.0 {
|
||||
Literal(lit) => Ok(lit.as_type()),
|
||||
Variable(var) => var.return_type(),
|
||||
BinOp(_, expr, _) => expr.return_type(),
|
||||
Block(block) => block.return_type(),
|
||||
FunctionCall(fcall) => fcall.return_type(),
|
||||
If(expr) => expr.return_type(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ReturnType for IfExpression {
|
||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||
self.1.return_type()
|
||||
}
|
||||
}
|
||||
|
||||
impl ReturnType for VariableReference {
|
||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||
Ok(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl ReturnType for FunctionCall {
|
||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||
Ok(self.return_type)
|
||||
}
|
||||
}
|
||||
|
||||
impl ReturnType for FunctionDefinition {
|
||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||
match &self.kind {
|
||||
FunctionDefinitionKind::Local(block, _) => block.return_type(),
|
||||
FunctionDefinitionKind::Extern(type_kind) => Ok(*type_kind),
|
||||
}
|
||||
}
|
||||
}
|
213
reid/src/token_stream.rs
Normal file
213
reid/src/token_stream.rs
Normal file
@ -0,0 +1,213 @@
|
||||
use crate::{
|
||||
ast::parse::Parse,
|
||||
lexer::{FullToken, Position, Token},
|
||||
};
|
||||
|
||||
pub struct TokenStream<'a, 'b> {
|
||||
ref_position: Option<&'b mut usize>,
|
||||
tokens: &'a [FullToken],
|
||||
pub position: usize,
|
||||
}
|
||||
|
||||
impl<'a, 'b> TokenStream<'a, 'b> {
|
||||
pub fn from(tokens: &'a [FullToken]) -> Self {
|
||||
TokenStream {
|
||||
ref_position: None,
|
||||
tokens,
|
||||
position: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expected_err<T: Into<String>>(&mut self, expected: T) -> Result<Error, Error> {
|
||||
Ok(Error::Expected(
|
||||
expected.into(),
|
||||
self.peek().unwrap_or(Token::Eof),
|
||||
self.get_next_position()?,
|
||||
))
|
||||
}
|
||||
|
||||
pub fn expect(&mut self, token: Token) -> Result<(), Error> {
|
||||
if let Some(peeked) = self.peek() {
|
||||
if token == peeked {
|
||||
self.position += 1;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(self.expected_err(token)?)
|
||||
}
|
||||
} else {
|
||||
Err(self.expected_err(token)?)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next(&mut self) -> Option<Token> {
|
||||
let value = if self.tokens.len() < self.position {
|
||||
None
|
||||
} else {
|
||||
Some(self.tokens[self.position].token.clone())
|
||||
};
|
||||
self.position += 1;
|
||||
value
|
||||
}
|
||||
|
||||
pub fn peek(&mut self) -> Option<Token> {
|
||||
if self.tokens.len() < self.position {
|
||||
None
|
||||
} else {
|
||||
Some(self.tokens[self.position].token.clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the next value of trait Parse. If the parse succeeded, the related
|
||||
/// tokens are consumed, otherwise token stream does not advance.
|
||||
///
|
||||
/// Parsetime-error is returned on failure.
|
||||
pub fn parse<T: Parse + std::fmt::Debug>(&mut self) -> Result<T, Error> {
|
||||
let (res, pos) = self.parse_meta()?;
|
||||
self.position = pos;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Parse the next item with Parse-trait (Same as [`TokenStream::parse`])
|
||||
/// without consuming the related tokens, essentially only peeking.
|
||||
pub fn parse_peek<T: Parse + std::fmt::Debug>(&mut self) -> Result<T, Error> {
|
||||
self.parse_meta().map(|(res, _)| res)
|
||||
}
|
||||
|
||||
/// Parse the next item with Parse-trait, also mapping it with the given
|
||||
/// function. The token-stream is only consumed, if the inner function
|
||||
/// retuns an Ok.
|
||||
pub fn parse_map<T: Parse + std::fmt::Debug, F, O>(&mut self, inner: F) -> Result<O, Error>
|
||||
where
|
||||
F: Fn(T) -> Result<O, Error>,
|
||||
{
|
||||
let (res, pos) = self.parse_meta::<T>()?;
|
||||
match inner(res) {
|
||||
Ok(mapped) => {
|
||||
self.position = pos;
|
||||
Ok(mapped)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses the item with Parse if the condition specified by the
|
||||
/// lambda-function is passed. Errors returned from this should not be
|
||||
/// passed to the end-user.
|
||||
pub fn parse_if<T: Parse + std::fmt::Debug, F>(&mut self, inner: F) -> Result<T, Error>
|
||||
where
|
||||
F: Fn(&T) -> bool,
|
||||
{
|
||||
let (res, pos) = self.parse_meta::<T>()?;
|
||||
if inner(&res) {
|
||||
self.position = pos;
|
||||
Ok(res)
|
||||
} else {
|
||||
Err(Error::IfFailed)
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the next item with Parse-trait. If successful, returning the
|
||||
/// parsed item and the new position of the TokenStream. Failing, returning
|
||||
/// parse-error.
|
||||
///
|
||||
/// Used for [`TokenStream::parse`] and [`TokenStream::parse_peek`]
|
||||
fn parse_meta<T: Parse + std::fmt::Debug>(&mut self) -> Result<(T, usize), Error> {
|
||||
let mut ref_pos = self.position;
|
||||
|
||||
let position = self.position;
|
||||
let clone = TokenStream {
|
||||
ref_position: Some(&mut ref_pos),
|
||||
tokens: self.tokens,
|
||||
position,
|
||||
};
|
||||
|
||||
match T::parse(clone) {
|
||||
Ok(res) => {
|
||||
dbg!(&res);
|
||||
let new_pos = ref_pos.max(self.position);
|
||||
Ok((res, new_pos))
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_next_position(&self) -> Result<Position, Error> {
|
||||
if self.tokens.is_empty() {
|
||||
Err(Error::FileEmpty)
|
||||
} else {
|
||||
let token_idx = self.position.min(self.tokens.len() - 1);
|
||||
Ok(self.tokens[token_idx].position)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_range(&self) -> Option<TokenRange> {
|
||||
self.ref_position.as_ref().map(|ref_pos| TokenRange {
|
||||
start: **ref_pos,
|
||||
end: self.position,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TokenStream<'_, '_> {
|
||||
fn drop(&mut self) {
|
||||
if let Some(ref_pos) = &mut self.ref_position {
|
||||
**ref_pos = self.position;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct TokenRange {
|
||||
pub start: usize,
|
||||
pub end: usize,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for TokenRange {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Tokens[{} - {}]", self.start, self.end)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TokenRange {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
start: Default::default(),
|
||||
end: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Add for TokenRange {
|
||||
type Output = TokenRange;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
TokenRange {
|
||||
start: self.start.min(rhs.start),
|
||||
end: self.end.min(rhs.end),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::iter::Sum for TokenRange {
|
||||
fn sum<I: Iterator<Item = Self>>(mut iter: I) -> Self {
|
||||
let mut start = iter.next().unwrap_or(Default::default());
|
||||
for item in iter {
|
||||
start = start + item;
|
||||
}
|
||||
start
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Expected {} at Ln {}, Col {}, got {:?}", .0, (.2).1, (.2).0, .1)]
|
||||
Expected(String, Token, Position),
|
||||
#[error("Source file contains no tokens")]
|
||||
FileEmpty,
|
||||
/// Only use this error in situations where the error never ends up for the end-user!
|
||||
#[error("Undefined error, should only be used in situations where the error is not emitted!")]
|
||||
Undefined,
|
||||
/// Condition failed for the parse-if
|
||||
#[error("Condition failed for parse-if. Should never be returned to end-user.")]
|
||||
IfFailed,
|
||||
}
|
222
src/codegen.rs
222
src/codegen.rs
@ -1,222 +0,0 @@
|
||||
use std::collections::{hash_map, HashMap};
|
||||
|
||||
use crate::{
|
||||
ast::{
|
||||
BinaryOperator, Block, BlockLevelStatement, Expression, FunctionCallExpression,
|
||||
FunctionDefinition, FunctionSignature, ReturnType, TopLevelStatement,
|
||||
},
|
||||
llvm_ir::{self, IRBlock, IRFunction, IRModule, IRValue, IRValueType},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ScopeData {
|
||||
named_vars: HashMap<String, IRValue>,
|
||||
defined_functions: HashMap<String, (FunctionSignature, Option<IRFunction>)>,
|
||||
}
|
||||
|
||||
impl ScopeData {
|
||||
pub fn inner<'a, 'b>(&self, block: &'b mut IRBlock<'a>) -> Scope<'a, 'b> {
|
||||
Scope {
|
||||
block,
|
||||
data: self.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn var(&self, name: &String) -> Option<&IRValue> {
|
||||
self.named_vars.get(name)
|
||||
}
|
||||
|
||||
pub fn set_var(&mut self, name: &str, val: IRValue) -> Result<(), Error> {
|
||||
if let hash_map::Entry::Vacant(e) = self.named_vars.entry(name.to_owned()) {
|
||||
e.insert(val);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::VariableAlreadyDefined(name.to_owned()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn function(
|
||||
&mut self,
|
||||
name: &String,
|
||||
) -> Option<&mut (FunctionSignature, Option<IRFunction>)> {
|
||||
self.defined_functions.get_mut(name)
|
||||
}
|
||||
|
||||
pub fn set_function_signature(
|
||||
&mut self,
|
||||
name: &str,
|
||||
sig: FunctionSignature,
|
||||
ir: IRFunction,
|
||||
) -> Result<(), Error> {
|
||||
if let hash_map::Entry::Vacant(e) = self.defined_functions.entry(name.to_owned()) {
|
||||
e.insert((sig, Some(ir)));
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::VariableAlreadyDefined(name.to_owned()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Scope<'a, 'b> {
|
||||
pub block: &'b mut IRBlock<'a>,
|
||||
pub data: ScopeData,
|
||||
}
|
||||
|
||||
impl<'a, 'b> Scope<'a, 'b> {
|
||||
pub fn inner<'c>(&'c mut self) -> Scope<'a, 'c> {
|
||||
Scope {
|
||||
block: self.block,
|
||||
data: self.data.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn codegen_from_statements(statements: Vec<TopLevelStatement>) -> Result<IRModule, Error> {
|
||||
let mut module = IRModule::new("testmod");
|
||||
|
||||
let mut scope = ScopeData {
|
||||
defined_functions: HashMap::new(),
|
||||
named_vars: HashMap::new(),
|
||||
};
|
||||
|
||||
for statement in &statements {
|
||||
match statement {
|
||||
TopLevelStatement::FunctionDefinition(FunctionDefinition(sig, _)) => {
|
||||
let function = module.create_func(&sig.name, IRValueType::I32);
|
||||
scope.set_function_signature(&sig.name.clone(), sig.clone(), function)?;
|
||||
}
|
||||
TopLevelStatement::Import(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
for statement in &statements {
|
||||
statement.codegen(&mut module, &mut scope)?;
|
||||
}
|
||||
|
||||
Ok(module)
|
||||
}
|
||||
|
||||
impl TopLevelStatement {
|
||||
pub fn codegen(&self, module: &mut IRModule, root_data: &mut ScopeData) -> Result<(), Error> {
|
||||
match self {
|
||||
TopLevelStatement::FunctionDefinition(FunctionDefinition(sig, block)) => {
|
||||
if let Some((_, ir)) = root_data.function(&sig.name) {
|
||||
if let Some(ir_function) = ir.take() {
|
||||
let mut ir_block = module.create_block();
|
||||
let mut scope = root_data.inner(&mut ir_block);
|
||||
|
||||
let (_, value) = match block.codegen(&mut scope)? {
|
||||
Some(v) => v,
|
||||
None => panic!("Void-return type function not yet implemented!"),
|
||||
};
|
||||
|
||||
ir_function.add_definition(value, ir_block);
|
||||
} else {
|
||||
Err(Error::FunctionAlreadyDefined(sig.name.clone()))?
|
||||
}
|
||||
} else {
|
||||
panic!("Function was not declared before it's definition")
|
||||
}
|
||||
}
|
||||
TopLevelStatement::Import(_) => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Block {
|
||||
pub fn codegen(&self, scope: &mut Scope) -> Result<Option<(ReturnType, IRValue)>, Error> {
|
||||
for statement in &self.0 {
|
||||
statement.codegen(scope)?;
|
||||
}
|
||||
|
||||
let value = if let Some((rt, exp)) = &self.1 {
|
||||
Some((*rt, exp.codegen(scope)?))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockLevelStatement {
|
||||
pub fn codegen(&self, scope: &mut Scope) -> Result<(), Error> {
|
||||
match self {
|
||||
BlockLevelStatement::Let(let_statement) => {
|
||||
let val = let_statement.1.codegen(scope)?;
|
||||
scope.data.set_var(&let_statement.0, val)?;
|
||||
Ok(())
|
||||
}
|
||||
BlockLevelStatement::Return(_) => panic!("Should never happen"),
|
||||
BlockLevelStatement::Import(_) => Ok(()), // TODO: To implement
|
||||
BlockLevelStatement::Expression(e) => {
|
||||
let _value = e.codegen(scope)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
pub fn codegen(&self, scope: &mut Scope) -> Result<IRValue, Error> {
|
||||
use Expression::*;
|
||||
match self {
|
||||
Binop(op, lhs, rhs) => match op {
|
||||
BinaryOperator::Add => {
|
||||
let lhs = lhs.codegen(scope)?;
|
||||
let rhs = rhs.codegen(scope)?;
|
||||
Ok(scope.block.add(lhs, rhs)?)
|
||||
}
|
||||
BinaryOperator::Mult => {
|
||||
let lhs = lhs.codegen(scope)?;
|
||||
let rhs = rhs.codegen(scope)?;
|
||||
Ok(scope.block.mul(lhs, rhs)?)
|
||||
}
|
||||
_ => panic!("Other binary operators not supported yet!"),
|
||||
},
|
||||
BlockExpr(block) => {
|
||||
let mut inner = scope.inner();
|
||||
|
||||
Ok(match block.codegen(&mut inner)? {
|
||||
Some((r_type, value)) => match r_type {
|
||||
ReturnType::Soft => value,
|
||||
ReturnType::Hard => {
|
||||
panic!("Hard returns in inner blocks not supported yet")
|
||||
}
|
||||
},
|
||||
None => panic!("Void-return type block not yet implemented!"),
|
||||
})
|
||||
}
|
||||
FunctionCall(fc) => {
|
||||
let FunctionCallExpression(name, _) = &**fc;
|
||||
if let Some((sig, _)) = scope.data.function(name) {
|
||||
Ok(scope.block.function_call(sig)?)
|
||||
} else {
|
||||
Err(Error::UndefinedFunction(name.clone()))?
|
||||
}
|
||||
}
|
||||
VariableName(name) => scope
|
||||
.data
|
||||
.var(name)
|
||||
.cloned()
|
||||
.ok_or(Error::UndefinedVariable(name.clone())),
|
||||
Literal(lit) => Ok(scope.block.get_const(lit)),
|
||||
IfExpr(_) => panic!("if expressions not yet supported"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Variable '{0}' already defined")]
|
||||
VariableAlreadyDefined(String),
|
||||
#[error("Variable '{0}' not yet defined")]
|
||||
UndefinedVariable(String),
|
||||
#[error("Function '{0}' not defined")]
|
||||
UndefinedFunction(String),
|
||||
#[error("Function '{0}' already defined")]
|
||||
FunctionAlreadyDefined(String),
|
||||
#[error(transparent)]
|
||||
Deeper(#[from] llvm_ir::Error),
|
||||
}
|
203
src/llvm_ir.rs
203
src/llvm_ir.rs
@ -1,203 +0,0 @@
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::mem;
|
||||
|
||||
use llvm_sys::{core::*, prelude::*, LLVMBuilder, LLVMContext, LLVMModule};
|
||||
|
||||
use crate::ast::{FunctionSignature, Literal};
|
||||
|
||||
macro_rules! cstr {
|
||||
($string:expr) => {
|
||||
core::ffi::CStr::from_bytes_with_nul_unchecked(concat!($string, "\0").as_bytes()).as_ptr()
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[must_use = "value contains raw pointer and must be inserted somewhere"]
|
||||
pub struct IRValue(IRValueType, LLVMValueRef);
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum IRValueType {
|
||||
I32,
|
||||
}
|
||||
|
||||
impl IRValueType {
|
||||
unsafe fn get_llvm_type(&self, module: &mut IRModule) -> LLVMTypeRef {
|
||||
match *self {
|
||||
Self::I32 => LLVMInt32TypeInContext(module.context),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
module: *mut LLVMModule,
|
||||
builder: *mut LLVMBuilder,
|
||||
}
|
||||
|
||||
impl IRModule {
|
||||
pub fn new<T: Into<String>>(name: T) -> IRModule {
|
||||
unsafe {
|
||||
// Set up a context, module and builder in that context.
|
||||
let context = LLVMContextCreate();
|
||||
let module = LLVMModuleCreateWithNameInContext(into_cstring(name).as_ptr(), context);
|
||||
let builder = LLVMCreateBuilderInContext(context);
|
||||
|
||||
IRModule {
|
||||
context,
|
||||
module,
|
||||
builder,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_block(&mut self) -> IRBlock {
|
||||
IRBlock::create("entry", self)
|
||||
}
|
||||
|
||||
pub fn create_func<T: Into<String>>(
|
||||
&mut self,
|
||||
name: T,
|
||||
return_type: IRValueType,
|
||||
) -> 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: IRValue(return_type, anon_func),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_to_string(&mut self) -> Result<&str, std::str::Utf8Error> {
|
||||
unsafe { CStr::from_ptr(LLVMPrintModuleToString(self.module)).to_str() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for IRModule {
|
||||
fn drop(&mut self) {
|
||||
// Clean up. Values created in the context mostly get cleaned up there.
|
||||
unsafe {
|
||||
LLVMDisposeBuilder(self.builder);
|
||||
LLVMDisposeModule(self.module);
|
||||
LLVMContextDispose(self.context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct IRFunction {
|
||||
value: IRValue,
|
||||
}
|
||||
|
||||
impl IRFunction {
|
||||
pub fn add_definition(self, ret: IRValue, block: IRBlock) {
|
||||
unsafe {
|
||||
LLVMAppendExistingBasicBlock(self.value.1, block.blockref);
|
||||
LLVMBuildRet(block.module.builder, ret.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IRBlock<'a> {
|
||||
module: &'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 {
|
||||
module: codegen,
|
||||
blockref,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_const(&mut self, literal_type: &Literal) -> IRValue {
|
||||
unsafe {
|
||||
match *literal_type {
|
||||
Literal::I32(v) => IRValue(
|
||||
IRValueType::I32,
|
||||
LLVMConstInt(
|
||||
LLVMInt32TypeInContext(self.module.context),
|
||||
mem::transmute(v as i64),
|
||||
1,
|
||||
),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(&mut self, lhs: IRValue, rhs: IRValue) -> Result<IRValue, Error> {
|
||||
unsafe {
|
||||
if lhs.0 == rhs.0 {
|
||||
Ok(IRValue(
|
||||
lhs.0,
|
||||
LLVMBuildAdd(self.module.builder, lhs.1, rhs.1, cstr!("tmpadd")),
|
||||
))
|
||||
} else {
|
||||
Err(Error::TypeMismatch(lhs.0, rhs.0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mul(&mut self, lhs: IRValue, rhs: IRValue) -> Result<IRValue, Error> {
|
||||
unsafe {
|
||||
if lhs.0 == rhs.0 {
|
||||
Ok(IRValue(
|
||||
lhs.0,
|
||||
LLVMBuildMul(self.module.builder, lhs.1, rhs.1, cstr!("tmpadd")),
|
||||
))
|
||||
} else {
|
||||
Err(Error::TypeMismatch(lhs.0, rhs.0))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn function_call(&mut self, callee: &FunctionSignature) -> Result<IRValue, Error> {
|
||||
unsafe {
|
||||
let function = LLVMGetNamedFunction(
|
||||
self.module.module,
|
||||
into_cstring(callee.name.clone()).as_ptr(),
|
||||
);
|
||||
|
||||
let ret_t = LLVMInt32TypeInContext(self.module.context);
|
||||
let mut argts = [];
|
||||
let mut args = [];
|
||||
|
||||
let fun_t = LLVMFunctionType(ret_t, argts.as_mut_ptr(), argts.len() as u32, 0);
|
||||
|
||||
let call = LLVMBuildCall2(
|
||||
self.module.builder,
|
||||
fun_t,
|
||||
function,
|
||||
args.as_mut_ptr(),
|
||||
args.len() as u32,
|
||||
into_cstring(&callee.name).as_ptr(),
|
||||
);
|
||||
|
||||
Ok(IRValue(IRValueType::I32, call))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("Type Mismatch: {0:?} {1:?}")]
|
||||
TypeMismatch(IRValueType, IRValueType),
|
||||
}
|
@ -1,103 +0,0 @@
|
||||
use crate::{
|
||||
ast::Parse,
|
||||
lexer::{FullToken, Position, Token},
|
||||
};
|
||||
|
||||
pub struct TokenStream<'a, 'b> {
|
||||
ref_position: Option<&'b mut usize>,
|
||||
tokens: &'a [FullToken],
|
||||
pub position: usize,
|
||||
}
|
||||
|
||||
impl<'a, 'b> TokenStream<'a, 'b> {
|
||||
pub fn from(tokens: &'a [FullToken]) -> Self {
|
||||
TokenStream {
|
||||
ref_position: None,
|
||||
tokens,
|
||||
position: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expected_err<T: Into<String>>(&mut self, expected: T) -> Result<Error, Error> {
|
||||
Ok(Error::Expected(
|
||||
expected.into(),
|
||||
self.peek().unwrap_or(Token::Eof),
|
||||
self.get_next_position()?,
|
||||
))
|
||||
}
|
||||
|
||||
pub fn expect(&mut self, token: Token) -> Result<(), Error> {
|
||||
if let Some(peeked) = self.peek() {
|
||||
if token == peeked {
|
||||
self.position += 1;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(self.expected_err(token)?)
|
||||
}
|
||||
} else {
|
||||
Err(self.expected_err(token)?)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next(&mut self) -> Option<Token> {
|
||||
let value = if self.tokens.len() < self.position {
|
||||
None
|
||||
} else {
|
||||
Some(self.tokens[self.position].token.clone())
|
||||
};
|
||||
self.position += 1;
|
||||
value
|
||||
}
|
||||
|
||||
pub fn peek(&mut self) -> Option<Token> {
|
||||
if self.tokens.len() < self.position {
|
||||
None
|
||||
} else {
|
||||
Some(self.tokens[self.position].token.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse<T: Parse>(&mut self) -> Result<T, Error> {
|
||||
let mut ref_pos = self.position;
|
||||
|
||||
let position = self.position;
|
||||
let clone = TokenStream {
|
||||
ref_position: Some(&mut ref_pos),
|
||||
tokens: self.tokens,
|
||||
position,
|
||||
};
|
||||
|
||||
match T::parse(clone) {
|
||||
Ok(res) => {
|
||||
self.position = ref_pos.max(self.position);
|
||||
Ok(res)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_next_position(&self) -> Result<Position, Error> {
|
||||
if self.tokens.is_empty() {
|
||||
Err(Error::FileEmpty)
|
||||
} else {
|
||||
let token_idx = self.position.min(self.tokens.len() - 1);
|
||||
Ok(self.tokens[token_idx].position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TokenStream<'_, '_> {
|
||||
fn drop(&mut self) {
|
||||
if let Some(ref_pos) = &mut self.ref_position {
|
||||
**ref_pos = self.position;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Expected {} at Ln {}, Col {}, got {:?}", .0, (.2).1, (.2).0, .1)]
|
||||
Expected(String, Token, Position),
|
||||
#[error("Source file contains no tokens")]
|
||||
FileEmpty,
|
||||
}
|
Loading…
Reference in New Issue
Block a user