Compare commits
No commits in common. "0392c293ba69edbc88fe9a3e77a9815c291ec7b0" and "8a32e66ba863abbe6c1b7cf8b0430022c60c8915" have entirely different histories.
0392c293ba
...
8a32e66ba8
@ -6,7 +6,7 @@
|
|||||||
# Do note this file is extremely simply for my own personal convenience
|
# Do note this file is extremely simply for my own personal convenience
|
||||||
|
|
||||||
export .env
|
export .env
|
||||||
cargo run --example $1 && \
|
cargo run --example testcodegen && \
|
||||||
# clang hello.o -o main && \
|
# clang hello.o -o main && \
|
||||||
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
|
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
|
||||||
-o main /usr/lib/crt1.o hello.o -lc && \
|
-o main /usr/lib/crt1.o hello.o -lc && \
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
use reid::compile;
|
|
||||||
|
|
||||||
pub static FIBONACCI: &str = include_str!("./reid/fibonacci.reid");
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let text = match compile(FIBONACCI) {
|
|
||||||
Ok(t) => t,
|
|
||||||
Err(e) => panic!("{}", e),
|
|
||||||
};
|
|
||||||
println!("{}", text);
|
|
||||||
}
|
|
@ -3,11 +3,12 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
let test = 9;
|
let test = 9;
|
||||||
let simpleAdd = 2 + 2;
|
let simpleAdd = 2 + 2;
|
||||||
let simpleSub = 7 - 2; // 14
|
let simpleMult = 7 * 2; // 14
|
||||||
|
let arithmetic = 3 + 2 * 5 + 1 * 2; // 15
|
||||||
|
|
||||||
if simpleAdd < test {
|
if simpleAdd < test {
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
return arithmetic + simpleSub + boop;
|
return arithmetic + simpleMult * arithmetic;
|
||||||
}
|
}
|
@ -11,114 +11,115 @@ fn main() {
|
|||||||
kind: FunctionDefinitionKind::Local(
|
kind: FunctionDefinitionKind::Local(
|
||||||
Block {
|
Block {
|
||||||
statements: vec![Statement(
|
statements: vec![Statement(
|
||||||
StmtKind::Expression(Expression(
|
StatementKind::If(IfExpression(
|
||||||
ExprKind::If(IfExpression(
|
// If N < 3
|
||||||
// If N < 3
|
Box::new(Expression(
|
||||||
Box::new(Expression(
|
ExpressionKind::BinOp(
|
||||||
ExprKind::BinOp(
|
BinaryOperator::Logic(LogicOperator::GreaterThan),
|
||||||
BinaryOperator::Logic(LogicOperator::GreaterThan),
|
Box::new(Expression(
|
||||||
Box::new(Expression(
|
ExpressionKind::Variable(VariableReference(
|
||||||
ExprKind::Variable(VariableReference(
|
TypeKind::I32,
|
||||||
TypeKind::I32,
|
"N".to_string(),
|
||||||
"N".to_string(),
|
Default::default(),
|
||||||
|
)),
|
||||||
|
Default::default(),
|
||||||
|
)),
|
||||||
|
Box::new(Expression(
|
||||||
|
ExpressionKind::Literal(Literal::I32(2)),
|
||||||
|
Default::default(),
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
Default::default(),
|
||||||
|
)),
|
||||||
|
// Then
|
||||||
|
Block {
|
||||||
|
statements: vec![],
|
||||||
|
return_expression: Some((
|
||||||
|
ReturnKind::HardReturn,
|
||||||
|
// return fibonacci(n-1) + fibonacci(n-2)
|
||||||
|
Box::new(Expression(
|
||||||
|
ExpressionKind::BinOp(
|
||||||
|
BinaryOperator::Add,
|
||||||
|
// fibonacci(n-1)
|
||||||
|
Box::new(Expression(
|
||||||
|
ExpressionKind::FunctionCall(FunctionCall {
|
||||||
|
name: fibonacci_name.clone(),
|
||||||
|
return_type: TypeKind::I32,
|
||||||
|
parameters: vec![Expression(
|
||||||
|
ExpressionKind::BinOp(
|
||||||
|
BinaryOperator::Minus,
|
||||||
|
Box::new(Expression(
|
||||||
|
ExpressionKind::Variable(
|
||||||
|
VariableReference(
|
||||||
|
TypeKind::I32,
|
||||||
|
fibonacci_n.clone(),
|
||||||
|
Default::default(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Default::default(),
|
||||||
|
)),
|
||||||
|
Box::new(Expression(
|
||||||
|
ExpressionKind::Literal(Literal::I32(
|
||||||
|
1,
|
||||||
|
)),
|
||||||
|
Default::default(),
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
Default::default(),
|
||||||
|
)],
|
||||||
|
}),
|
||||||
Default::default(),
|
Default::default(),
|
||||||
)),
|
)),
|
||||||
Default::default(),
|
// fibonacci(n-2)
|
||||||
)),
|
Box::new(Expression(
|
||||||
Box::new(Expression(
|
ExpressionKind::FunctionCall(FunctionCall {
|
||||||
ExprKind::Literal(Literal::I32(2)),
|
name: fibonacci_name.clone(),
|
||||||
Default::default(),
|
return_type: TypeKind::I32,
|
||||||
)),
|
parameters: vec![Expression(
|
||||||
),
|
ExpressionKind::BinOp(
|
||||||
Default::default(),
|
BinaryOperator::Minus,
|
||||||
)),
|
Box::new(Expression(
|
||||||
// Then
|
ExpressionKind::Variable(
|
||||||
Block {
|
VariableReference(
|
||||||
statements: vec![],
|
TypeKind::I32,
|
||||||
return_expression: Some((
|
fibonacci_n.clone(),
|
||||||
ReturnKind::Hard,
|
Default::default(),
|
||||||
// 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(),
|
),
|
||||||
|
Default::default(),
|
||||||
|
)),
|
||||||
|
Box::new(Expression(
|
||||||
|
ExpressionKind::Literal(Literal::I32(
|
||||||
|
2,
|
||||||
)),
|
)),
|
||||||
Box::new(Expression(
|
Default::default(),
|
||||||
ExprKind::Literal(Literal::I32(1)),
|
)),
|
||||||
Default::default(),
|
),
|
||||||
)),
|
Default::default(),
|
||||||
),
|
)],
|
||||||
Default::default(),
|
}),
|
||||||
)],
|
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(),
|
)),
|
||||||
},
|
range: Default::default(),
|
||||||
// No else-block
|
},
|
||||||
None,
|
// No else-block
|
||||||
)),
|
None,
|
||||||
Default::default(),
|
|
||||||
)),
|
)),
|
||||||
Default::default(),
|
Default::default(),
|
||||||
)],
|
)],
|
||||||
// return 1
|
// return 1
|
||||||
return_expression: Some((
|
return_expression: Some((
|
||||||
ReturnKind::Soft,
|
ReturnKind::SoftReturn,
|
||||||
Box::new(Expression(
|
Box::new(Expression(
|
||||||
ExprKind::Literal(Literal::I32(1)),
|
ExpressionKind::Literal(Literal::I32(1)),
|
||||||
Default::default(),
|
Default::default(),
|
||||||
)),
|
)),
|
||||||
)),
|
)),
|
||||||
meta: Default::default(),
|
range: Default::default(),
|
||||||
},
|
},
|
||||||
Default::default(),
|
Default::default(),
|
||||||
),
|
),
|
||||||
@ -131,20 +132,20 @@ fn main() {
|
|||||||
Block {
|
Block {
|
||||||
statements: vec![],
|
statements: vec![],
|
||||||
return_expression: Some((
|
return_expression: Some((
|
||||||
ReturnKind::Soft,
|
ReturnKind::SoftReturn,
|
||||||
Box::new(Expression(
|
Box::new(Expression(
|
||||||
ExprKind::FunctionCall(FunctionCall {
|
ExpressionKind::FunctionCall(FunctionCall {
|
||||||
name: fibonacci_name.clone(),
|
name: fibonacci_name.clone(),
|
||||||
return_type: TypeKind::I32,
|
return_type: TypeKind::I32,
|
||||||
parameters: vec![Expression(
|
parameters: vec![Expression(
|
||||||
ExprKind::Literal(Literal::I32(5)),
|
ExpressionKind::Literal(Literal::I32(5)),
|
||||||
Default::default(),
|
Default::default(),
|
||||||
)],
|
)],
|
||||||
}),
|
}),
|
||||||
Default::default(),
|
Default::default(),
|
||||||
)),
|
)),
|
||||||
)),
|
)),
|
||||||
meta: Default::default(),
|
range: Default::default(),
|
||||||
},
|
},
|
||||||
Default::default(),
|
Default::default(),
|
||||||
),
|
),
|
||||||
|
@ -1,109 +0,0 @@
|
|||||||
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,544 +0,0 @@
|
|||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -102,14 +102,17 @@ impl<'ctx> Scope<'ctx> {
|
|||||||
impl mir::Statement {
|
impl mir::Statement {
|
||||||
pub fn codegen<'ctx>(&self, scope: &mut Scope<'ctx>) -> Option<Value<'ctx>> {
|
pub fn codegen<'ctx>(&self, scope: &mut Scope<'ctx>) -> Option<Value<'ctx>> {
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
mir::StmtKind::Let(VariableReference(_, name, _), expression) => {
|
mir::StatementKind::Let(VariableReference(_, name, _), expression) => {
|
||||||
let value = expression.codegen(scope).unwrap();
|
let value = expression.codegen(scope).unwrap();
|
||||||
scope.stack_values.insert(name.clone(), value);
|
scope.stack_values.insert(name.clone(), value);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
// mir::StmtKind::If(if_expression) => if_expression.codegen(scope),
|
mir::StatementKind::If(if_expression) => if_expression.codegen(scope),
|
||||||
mir::StmtKind::Import(_) => todo!(),
|
mir::StatementKind::Import(_) => todo!(),
|
||||||
mir::StmtKind::Expression(expression) => expression.codegen(scope),
|
mir::StatementKind::Expression(expression) => {
|
||||||
|
let value = expression.codegen(scope).unwrap();
|
||||||
|
Some(value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -175,15 +178,15 @@ impl mir::IfExpression {
|
|||||||
impl mir::Expression {
|
impl mir::Expression {
|
||||||
pub fn codegen<'ctx>(&self, scope: &mut Scope<'ctx>) -> Option<Value<'ctx>> {
|
pub fn codegen<'ctx>(&self, scope: &mut Scope<'ctx>) -> Option<Value<'ctx>> {
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
mir::ExprKind::Variable(varref) => {
|
mir::ExpressionKind::Variable(varref) => {
|
||||||
let v = scope
|
let v = scope
|
||||||
.stack_values
|
.stack_values
|
||||||
.get(&varref.1)
|
.get(&varref.1)
|
||||||
.expect("Variable reference not found?!");
|
.expect("Variable reference not found?!");
|
||||||
Some(v.clone())
|
Some(v.clone())
|
||||||
}
|
}
|
||||||
mir::ExprKind::Literal(lit) => Some(lit.codegen(scope.context)),
|
mir::ExpressionKind::Literal(lit) => Some(lit.codegen(scope.context)),
|
||||||
mir::ExprKind::BinOp(binop, lhs_exp, rhs_exp) => {
|
mir::ExpressionKind::BinOp(binop, lhs_exp, rhs_exp) => {
|
||||||
let lhs = lhs_exp.codegen(scope).expect("lhs has no return value");
|
let lhs = lhs_exp.codegen(scope).expect("lhs has no return value");
|
||||||
let rhs = rhs_exp.codegen(scope).expect("rhs has no return value");
|
let rhs = rhs_exp.codegen(scope).expect("rhs has no return value");
|
||||||
Some(match binop {
|
Some(match binop {
|
||||||
@ -200,7 +203,7 @@ impl mir::Expression {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
mir::ExprKind::FunctionCall(call) => {
|
mir::ExpressionKind::FunctionCall(call) => {
|
||||||
let params = call
|
let params = call
|
||||||
.parameters
|
.parameters
|
||||||
.iter()
|
.iter()
|
||||||
@ -212,8 +215,8 @@ impl mir::Expression {
|
|||||||
.expect("function not found!");
|
.expect("function not found!");
|
||||||
Some(scope.block.call(callee, params, "call").unwrap())
|
Some(scope.block.call(callee, params, "call").unwrap())
|
||||||
}
|
}
|
||||||
mir::ExprKind::If(if_expression) => if_expression.codegen(scope),
|
mir::ExpressionKind::If(if_expression) => if_expression.codegen(scope),
|
||||||
mir::ExprKind::Block(block) => {
|
mir::ExpressionKind::Block(block) => {
|
||||||
let mut inner_scope = scope.with_block(scope.function.block("inner"));
|
let mut inner_scope = scope.with_block(scope.function.block("inner"));
|
||||||
if let Some(ret) = block.codegen(&mut inner_scope) {
|
if let Some(ret) = block.codegen(&mut inner_scope) {
|
||||||
inner_scope.block.br(&scope.block);
|
inner_scope.block.br(&scope.block);
|
||||||
@ -246,11 +249,11 @@ impl mir::Block {
|
|||||||
if let Some((kind, expr)) = &self.return_expression {
|
if let Some((kind, expr)) = &self.return_expression {
|
||||||
let ret = expr.codegen(&mut scope).unwrap();
|
let ret = expr.codegen(&mut scope).unwrap();
|
||||||
match kind {
|
match kind {
|
||||||
mir::ReturnKind::Hard => {
|
mir::ReturnKind::HardReturn => {
|
||||||
scope.block.ret(&ret).unwrap();
|
scope.block.ret(&ret).unwrap();
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
mir::ReturnKind::Soft => Some(ret),
|
mir::ReturnKind::SoftReturn => Some(ret),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@ -273,7 +276,6 @@ impl TypeKind {
|
|||||||
match &self {
|
match &self {
|
||||||
TypeKind::I32 => TypeEnum::Integer(context.type_i32()),
|
TypeKind::I32 => TypeEnum::Integer(context.type_i32()),
|
||||||
TypeKind::I16 => TypeEnum::Integer(context.type_i16()),
|
TypeKind::I16 => TypeEnum::Integer(context.type_i16()),
|
||||||
TypeKind::Void => panic!("Void not a supported type"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
use reid_lib::Context;
|
use old_codegen::{form_context, from_statements};
|
||||||
|
|
||||||
use crate::{ast::TopLevelStatement, lexer::Token, token_stream::TokenStream};
|
use crate::{lexer::Token, parser::TopLevelStatement, token_stream::TokenStream};
|
||||||
|
|
||||||
mod ast;
|
|
||||||
mod codegen;
|
|
||||||
mod lexer;
|
mod lexer;
|
||||||
pub mod mir;
|
pub mod mir;
|
||||||
|
mod old_codegen;
|
||||||
|
mod parser;
|
||||||
|
// mod llvm_ir;
|
||||||
|
pub mod codegen;
|
||||||
mod token_stream;
|
mod token_stream;
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
@ -39,21 +41,8 @@ pub fn compile(source: &str) -> Result<String, ReidError> {
|
|||||||
statements.push(statement);
|
statements.push(statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
let ast_module = ast::Module {
|
let mut context = form_context();
|
||||||
name: "test".to_owned(),
|
let mut module = from_statements(&mut context, statements).unwrap();
|
||||||
top_level_statements: statements,
|
let text = module.print_to_string().unwrap();
|
||||||
};
|
Ok(text.to_owned())
|
||||||
|
|
||||||
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),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
@ -1,34 +1,18 @@
|
|||||||
/// In this module are defined structs that are used for performing passes on
|
/// 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
|
/// Reid. It contains a simplified version of Reid which must already be
|
||||||
/// type-checked beforehand.
|
/// type-checked beforehand.
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use types::*;
|
||||||
|
|
||||||
use crate::token_stream::TokenRange;
|
use crate::token_stream::TokenRange;
|
||||||
|
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(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 {
|
pub enum TypeKind {
|
||||||
I32,
|
I32,
|
||||||
I16,
|
I16,
|
||||||
Void,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TypeKind {
|
impl TypeKind {
|
||||||
@ -39,14 +23,14 @@ impl TypeKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub enum Literal {
|
pub enum Literal {
|
||||||
I32(i32),
|
I32(i32),
|
||||||
I16(i16),
|
I16(i16),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Literal {
|
impl Literal {
|
||||||
pub fn as_type(self: &Literal) -> TypeKind {
|
fn as_type(self: &Literal) -> TypeKind {
|
||||||
match self {
|
match self {
|
||||||
Literal::I32(_) => TypeKind::I32,
|
Literal::I32(_) => TypeKind::I32,
|
||||||
Literal::I16(_) => TypeKind::I16,
|
Literal::I16(_) => TypeKind::I16,
|
||||||
@ -71,18 +55,15 @@ pub enum LogicOperator {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum ReturnKind {
|
pub enum ReturnKind {
|
||||||
Hard,
|
HardReturn,
|
||||||
Soft,
|
SoftReturn,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
pub struct VariableReference(pub TypeKind, pub String, pub TokenRange);
|
||||||
pub struct VariableReference(pub TypeKind, pub String, pub Metadata);
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
pub struct Import(pub String, pub TokenRange);
|
||||||
pub struct Import(pub String, pub Metadata);
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
pub enum ExpressionKind {
|
||||||
pub enum ExprKind {
|
|
||||||
Variable(VariableReference),
|
Variable(VariableReference),
|
||||||
Literal(Literal),
|
Literal(Literal),
|
||||||
BinOp(BinaryOperator, Box<Expression>, Box<Expression>),
|
BinOp(BinaryOperator, Box<Expression>, Box<Expression>),
|
||||||
@ -91,55 +72,47 @@ pub enum ExprKind {
|
|||||||
Block(Block),
|
Block(Block),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
pub struct Expression(pub ExpressionKind, pub TokenRange);
|
||||||
pub struct Expression(pub ExprKind, pub Metadata);
|
|
||||||
|
|
||||||
/// Condition, Then, Else
|
/// Condition, Then, Else
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct IfExpression(pub Box<Expression>, pub Block, pub Option<Block>);
|
pub struct IfExpression(pub Box<Expression>, pub Block, pub Option<Block>);
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct FunctionCall {
|
pub struct FunctionCall {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub return_type: TypeKind,
|
pub return_type: TypeKind,
|
||||||
pub parameters: Vec<Expression>,
|
pub parameters: Vec<Expression>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct FunctionDefinition {
|
pub struct FunctionDefinition {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub parameters: Vec<(String, TypeKind)>,
|
pub parameters: Vec<(String, TypeKind)>,
|
||||||
pub kind: FunctionDefinitionKind,
|
pub kind: FunctionDefinitionKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum FunctionDefinitionKind {
|
pub enum FunctionDefinitionKind {
|
||||||
/// Actual definition block and surrounding signature range
|
/// Actual definition block and surrounding signature range
|
||||||
Local(Block, Metadata),
|
Local(Block, TokenRange),
|
||||||
/// Return Type
|
/// Return Type
|
||||||
Extern(TypeKind),
|
Extern(TypeKind),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Block {
|
pub struct Block {
|
||||||
/// List of non-returning statements
|
/// List of non-returning statements
|
||||||
pub statements: Vec<Statement>,
|
pub statements: Vec<Statement>,
|
||||||
pub return_expression: Option<(ReturnKind, Box<Expression>)>,
|
pub return_expression: Option<(ReturnKind, Box<Expression>)>,
|
||||||
pub meta: Metadata,
|
pub range: TokenRange,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
pub struct Statement(pub StatementKind, pub TokenRange);
|
||||||
pub struct Statement(pub StmtKind, pub Metadata);
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
pub enum StatementKind {
|
||||||
pub enum StmtKind {
|
|
||||||
/// Variable name+type, evaluation
|
/// Variable name+type, evaluation
|
||||||
Let(VariableReference, Expression),
|
Let(VariableReference, Expression),
|
||||||
|
If(IfExpression),
|
||||||
Import(Import),
|
Import(Import),
|
||||||
Expression(Expression),
|
Expression(Expression),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Module {
|
pub struct Module {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub imports: Vec<Import>,
|
pub imports: Vec<Import>,
|
||||||
|
@ -16,25 +16,26 @@ impl ReturnType for Block {
|
|||||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||||
self.return_expression
|
self.return_expression
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or(ReturnTypeOther::NoBlockReturn(self.meta.range))
|
.ok_or(ReturnTypeOther::NoBlockReturn(self.range.clone()))
|
||||||
.and_then(|(_, stmt)| stmt.return_type())
|
.and_then(|(_, stmt)| stmt.return_type())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ReturnType for Statement {
|
impl ReturnType for Statement {
|
||||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||||
use StmtKind::*;
|
use StatementKind::*;
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
Expression(e) => e.return_type(),
|
Expression(e) => e.return_type(),
|
||||||
Import(_) => Err(ReturnTypeOther::Import(self.1.range)),
|
If(e) => e.return_type(),
|
||||||
Let(_, _) => Err(ReturnTypeOther::Let(self.1.range)),
|
Import(_) => Err(ReturnTypeOther::Import(self.1)),
|
||||||
|
Let(_, _) => Err(ReturnTypeOther::Let(self.1)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ReturnType for Expression {
|
impl ReturnType for Expression {
|
||||||
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
|
||||||
use ExprKind::*;
|
use ExpressionKind::*;
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
Literal(lit) => Ok(lit.as_type()),
|
Literal(lit) => Ok(lit.as_type()),
|
||||||
Variable(var) => var.return_type(),
|
Variable(var) => var.return_type(),
|
||||||
|
287
reid/src/old_codegen/llvm.rs
Normal file
287
reid/src/old_codegen/llvm.rs
Normal file
@ -0,0 +1,287 @@
|
|||||||
|
use std::borrow::BorrowMut;
|
||||||
|
use std::ffi::{CStr, CString};
|
||||||
|
use std::mem;
|
||||||
|
use std::ptr::null_mut;
|
||||||
|
|
||||||
|
use llvm_sys::analysis::LLVMVerifyModule;
|
||||||
|
use llvm_sys::transforms::pass_manager_builder::{
|
||||||
|
self, LLVMOpaquePassManagerBuilder, LLVMPassManagerBuilderCreate,
|
||||||
|
LLVMPassManagerBuilderSetOptLevel,
|
||||||
|
};
|
||||||
|
use llvm_sys::{
|
||||||
|
core::*, prelude::*, LLVMBasicBlock, LLVMBuilder, LLVMContext, LLVMModule, LLVMType, LLVMValue,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::parser;
|
||||||
|
|
||||||
|
fn into_cstring<T: Into<String>>(value: T) -> CString {
|
||||||
|
let string = value.into();
|
||||||
|
unsafe { CString::from_vec_with_nul_unchecked((string + "\0").into_bytes()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(thiserror::Error, Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("Type mismatch: {0:?} vs {1:?}")]
|
||||||
|
TypeMismatch(IRType, IRType),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
|
pub enum IRType {
|
||||||
|
I32,
|
||||||
|
Boolean,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IRType {
|
||||||
|
fn in_context(&self, context: &IRContext) -> *mut LLVMType {
|
||||||
|
use IRType::*;
|
||||||
|
unsafe {
|
||||||
|
return match self {
|
||||||
|
I32 => LLVMInt32TypeInContext(context.context),
|
||||||
|
Boolean => LLVMInt1TypeInContext(context.context),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct IRValue(pub IRType, *mut LLVMValue);
|
||||||
|
|
||||||
|
impl IRValue {
|
||||||
|
pub fn from_literal(literal: &parser::Literal, module: &IRModule) -> Self {
|
||||||
|
use parser::Literal;
|
||||||
|
match literal {
|
||||||
|
Literal::I32(v) => {
|
||||||
|
let ir_type = IRType::I32;
|
||||||
|
unsafe {
|
||||||
|
let ir_value = LLVMConstInt(
|
||||||
|
ir_type.in_context(module.context),
|
||||||
|
mem::transmute(*v as i64),
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
return IRValue(ir_type, ir_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct IRContext {
|
||||||
|
context: *mut LLVMContext,
|
||||||
|
builder: *mut LLVMBuilder,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IRContext {
|
||||||
|
pub fn new() -> IRContext {
|
||||||
|
unsafe {
|
||||||
|
// Set up a context, module and builder in that context.
|
||||||
|
let context = LLVMContextCreate();
|
||||||
|
let builder = LLVMCreateBuilderInContext(context);
|
||||||
|
IRContext { context, builder }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn module<'a>(&'a mut self, name: String) -> IRModule<'a> {
|
||||||
|
IRModule::new(self, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for IRContext {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
// Clean up. Values created in the context mostly get cleaned up there.
|
||||||
|
unsafe {
|
||||||
|
LLVMDisposeBuilder(self.builder);
|
||||||
|
LLVMContextDispose(self.context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct IRModule<'a> {
|
||||||
|
context: &'a mut IRContext,
|
||||||
|
module: *mut LLVMModule,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> IRModule<'a> {
|
||||||
|
fn new<'b: 'a>(context: &'b mut IRContext, name: String) -> IRModule<'a> {
|
||||||
|
unsafe {
|
||||||
|
let module =
|
||||||
|
LLVMModuleCreateWithNameInContext(into_cstring(name).as_ptr(), context.context);
|
||||||
|
|
||||||
|
IRModule { context, module }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_to_string(&mut self) -> Result<&str, std::str::Utf8Error> {
|
||||||
|
unsafe {
|
||||||
|
LLVMVerifyModule(
|
||||||
|
self.module,
|
||||||
|
llvm_sys::analysis::LLVMVerifierFailureAction::LLVMPrintMessageAction,
|
||||||
|
null_mut(),
|
||||||
|
);
|
||||||
|
CStr::from_ptr(LLVMPrintModuleToString(self.module)).to_str()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Drop for IRModule<'a> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
// Clean up. Values created in the context mostly get cleaned up there.
|
||||||
|
unsafe {
|
||||||
|
LLVMDisposeModule(self.module);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct IRFunction<'a, 'b> {
|
||||||
|
pub module: &'b IRModule<'a>,
|
||||||
|
/// The actual function
|
||||||
|
value: *mut LLVMValue,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> IRFunction<'a, 'b> {
|
||||||
|
pub fn new(name: &String, module: &'b IRModule<'a>) -> IRFunction<'a, 'b> {
|
||||||
|
unsafe {
|
||||||
|
// TODO, fix later!
|
||||||
|
|
||||||
|
let return_type = LLVMInt32TypeInContext(module.context.context);
|
||||||
|
let mut argts = [];
|
||||||
|
let func_type =
|
||||||
|
LLVMFunctionType(return_type, argts.as_mut_ptr(), argts.len() as u32, 0);
|
||||||
|
|
||||||
|
let function = LLVMAddFunction(module.module, into_cstring(name).as_ptr(), func_type);
|
||||||
|
|
||||||
|
IRFunction {
|
||||||
|
module,
|
||||||
|
value: function,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct IRBlock<'a, 'b, 'c> {
|
||||||
|
pub function: &'c IRFunction<'a, 'b>,
|
||||||
|
blockref: *mut LLVMBasicBlock,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b, 'c> IRBlock<'a, 'b, 'c> {
|
||||||
|
pub fn new(function: &'c IRFunction<'a, 'b>, name: &CStr) -> IRBlock<'a, 'b, 'c> {
|
||||||
|
unsafe {
|
||||||
|
let blockref =
|
||||||
|
LLVMCreateBasicBlockInContext(function.module.context.context, name.as_ptr());
|
||||||
|
|
||||||
|
IRBlock { function, blockref }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add(
|
||||||
|
&mut self,
|
||||||
|
IRValue(lhs_t, lhs_v): IRValue,
|
||||||
|
IRValue(rhs_t, rhs_v): IRValue,
|
||||||
|
) -> Result<IRValue, Error> {
|
||||||
|
unsafe {
|
||||||
|
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||||
|
if lhs_t == rhs_t {
|
||||||
|
Ok(IRValue(
|
||||||
|
lhs_t,
|
||||||
|
LLVMBuildAdd(
|
||||||
|
self.function.module.context.builder,
|
||||||
|
lhs_v,
|
||||||
|
rhs_v,
|
||||||
|
c"tmpadd".as_ptr(),
|
||||||
|
),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
Err(Error::TypeMismatch(lhs_t, rhs_t))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mult(
|
||||||
|
&mut self,
|
||||||
|
IRValue(lhs_t, lhs_v): IRValue,
|
||||||
|
IRValue(rhs_t, rhs_v): IRValue,
|
||||||
|
) -> Result<IRValue, Error> {
|
||||||
|
unsafe {
|
||||||
|
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||||
|
if lhs_t == rhs_t {
|
||||||
|
Ok(IRValue(
|
||||||
|
lhs_t,
|
||||||
|
LLVMBuildMul(
|
||||||
|
self.function.module.context.builder,
|
||||||
|
lhs_v,
|
||||||
|
rhs_v,
|
||||||
|
c"tmpadd".as_ptr(),
|
||||||
|
),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
Err(Error::TypeMismatch(lhs_t, rhs_t))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn less_than(
|
||||||
|
&mut self,
|
||||||
|
IRValue(lhs_t, lhs_v): IRValue,
|
||||||
|
IRValue(rhs_t, rhs_v): IRValue,
|
||||||
|
) -> Result<IRValue, Error> {
|
||||||
|
unsafe {
|
||||||
|
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||||
|
if lhs_t == rhs_t {
|
||||||
|
Ok(IRValue(
|
||||||
|
IRType::Boolean,
|
||||||
|
LLVMBuildICmp(
|
||||||
|
self.function.module.context.builder,
|
||||||
|
llvm_sys::LLVMIntPredicate::LLVMIntULT,
|
||||||
|
lhs_v,
|
||||||
|
rhs_v,
|
||||||
|
c"IntULT".as_ptr(),
|
||||||
|
),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
Err(Error::TypeMismatch(lhs_t, rhs_t))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_return(&mut self, value: Option<IRValue>) {
|
||||||
|
unsafe {
|
||||||
|
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||||
|
if let Some(IRValue(_, value)) = value {
|
||||||
|
LLVMBuildRet(self.function.module.context.builder, value);
|
||||||
|
} else {
|
||||||
|
LLVMBuildRetVoid(self.function.module.context.builder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn branch(
|
||||||
|
&mut self,
|
||||||
|
IRValue(_, condition): IRValue,
|
||||||
|
then_block: &mut IRBlock,
|
||||||
|
else_block: &mut IRBlock,
|
||||||
|
) {
|
||||||
|
unsafe {
|
||||||
|
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||||
|
LLVMBuildCondBr(
|
||||||
|
self.function.module.context.builder,
|
||||||
|
condition,
|
||||||
|
then_block.blockref,
|
||||||
|
else_block.blockref,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn move_into(&mut self, block: &mut IRBlock) {
|
||||||
|
unsafe {
|
||||||
|
LLVMPositionBuilderAtEnd(self.function.module.context.builder, self.blockref);
|
||||||
|
LLVMBuildBr(self.function.module.context.builder, block.blockref);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b, 'c> Drop for IRBlock<'a, 'b, 'c> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
unsafe {
|
||||||
|
LLVMAppendExistingBasicBlock(self.function.value, self.blockref);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
176
reid/src/old_codegen/mod.rs
Normal file
176
reid/src/old_codegen/mod.rs
Normal file
@ -0,0 +1,176 @@
|
|||||||
|
mod llvm;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use llvm::{Error, IRBlock, IRContext, IRFunction, IRModule, IRValue};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
parser::{
|
||||||
|
Block, BlockLevelStatement, Expression, ExpressionKind, FunctionDefinition, IfExpression,
|
||||||
|
LetStatement, ReturnType,
|
||||||
|
},
|
||||||
|
TopLevelStatement,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn form_context() -> IRContext {
|
||||||
|
IRContext::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_statements(
|
||||||
|
context: &mut IRContext,
|
||||||
|
statements: Vec<TopLevelStatement>,
|
||||||
|
) -> Result<IRModule, Error> {
|
||||||
|
let mut module = context.module("testmod".to_owned());
|
||||||
|
let mut scope = ScopeData::new();
|
||||||
|
for statement in statements {
|
||||||
|
statement.codegen(&mut scope, &mut module);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(module)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TopLevelStatement {
|
||||||
|
fn codegen(&self, scope: &mut ScopeData, module: &mut IRModule) {
|
||||||
|
match self {
|
||||||
|
Self::FunctionDefinition(func) => func.codegen(scope, module),
|
||||||
|
Self::Import(_) => panic!("not implemented"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FunctionDefinition {
|
||||||
|
fn codegen(&self, scope: &mut ScopeData, module: &mut IRModule) {
|
||||||
|
let FunctionDefinition(signature, block, _) = self;
|
||||||
|
let ir_function = IRFunction::new(&signature.name, module);
|
||||||
|
|
||||||
|
let ir_block = IRBlock::new(&ir_function, c"entry");
|
||||||
|
let mut scope = scope.inner(ir_block);
|
||||||
|
if let Some((_, val)) = block.codegen(&mut scope) {
|
||||||
|
scope.block.add_return(Some(val));
|
||||||
|
} else {
|
||||||
|
scope.block.add_return(None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Block {
|
||||||
|
#[must_use]
|
||||||
|
fn codegen(&self, scope: &mut Scope) -> Option<(ReturnType, IRValue)> {
|
||||||
|
for statement in &self.0 {
|
||||||
|
statement.codegen(scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some((ret_type, return_exp)) = &self.1 {
|
||||||
|
let value = return_exp.codegen(scope);
|
||||||
|
Some((*ret_type, value))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BlockLevelStatement {
|
||||||
|
fn codegen(&self, scope: &mut Scope) {
|
||||||
|
use BlockLevelStatement::*;
|
||||||
|
match self {
|
||||||
|
Expression(exp) | Return(ReturnType::Soft, exp) => {
|
||||||
|
exp.codegen(scope);
|
||||||
|
}
|
||||||
|
Let(LetStatement(name, exp, _)) => {
|
||||||
|
let val = exp.codegen(scope);
|
||||||
|
scope.data.insert(name, val);
|
||||||
|
}
|
||||||
|
Return(ReturnType::Hard, _) => panic!("hard returns here should not be possible.."),
|
||||||
|
Import(_) => panic!("block level import not supported"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Expression {
|
||||||
|
fn codegen(&self, scope: &mut Scope) -> IRValue {
|
||||||
|
let Expression(kind, _) = self;
|
||||||
|
|
||||||
|
use ExpressionKind::*;
|
||||||
|
match kind {
|
||||||
|
Literal(lit) => IRValue::from_literal(lit, &scope.block.function.module),
|
||||||
|
VariableName(v) => scope.data.fetch(v),
|
||||||
|
Binop(op, lhs, rhs) => {
|
||||||
|
let lhs = lhs.codegen(scope);
|
||||||
|
let rhs = rhs.codegen(scope);
|
||||||
|
use crate::parser::BinaryOperator::*;
|
||||||
|
match op {
|
||||||
|
Add => scope.block.add(lhs, rhs).unwrap(),
|
||||||
|
Mult => scope.block.mult(lhs, rhs).unwrap(),
|
||||||
|
LessThan => scope.block.less_than(lhs, rhs).unwrap(),
|
||||||
|
_ => panic!("operator not supported: {:?}", op),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IfExpr(ifx) => {
|
||||||
|
let IfExpression(expr, block, _) = ifx.as_ref();
|
||||||
|
let condition = expr.codegen(scope);
|
||||||
|
|
||||||
|
let mut thenb = IRBlock::new(scope.block.function, c"then");
|
||||||
|
let mut afterb = IRBlock::new(scope.block.function, c"merge");
|
||||||
|
|
||||||
|
scope.block.branch(condition, &mut thenb, &mut afterb);
|
||||||
|
scope.block = afterb;
|
||||||
|
|
||||||
|
let mut then = scope.inner(thenb);
|
||||||
|
match block.codegen(&mut then) {
|
||||||
|
Some((ReturnType::Hard, v)) => then.block.add_return(Some(v)),
|
||||||
|
_ => then.block.move_into(&mut scope.block),
|
||||||
|
}
|
||||||
|
|
||||||
|
IRValue::from_literal(&crate::parser::Literal::I32(1), scope.block.function.module)
|
||||||
|
}
|
||||||
|
BlockExpr(_) => panic!("block expr not supported"),
|
||||||
|
FunctionCall(_) => panic!("function call expr not supported"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct ScopeData {
|
||||||
|
vars: HashMap<String, IRValue>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ScopeData {
|
||||||
|
fn new() -> ScopeData {
|
||||||
|
ScopeData {
|
||||||
|
vars: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn with_block<'a, 'b, 'c>(self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> {
|
||||||
|
Scope { data: self, block }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inner<'a, 'b, 'c>(&self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> {
|
||||||
|
self.clone().with_block(block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fetch(&self, name: &String) -> IRValue {
|
||||||
|
match self.vars.get(name) {
|
||||||
|
Some(val) => val.clone(),
|
||||||
|
_ => panic!("No such variable in scope: {}", name),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert(&mut self, name: &String, value: IRValue) {
|
||||||
|
match self.vars.insert(name.clone(), value) {
|
||||||
|
Some(_) => panic!("{} was already defined in scope", name),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Scope<'a, 'b, 'c> {
|
||||||
|
data: ScopeData,
|
||||||
|
block: IRBlock<'a, 'b, 'c>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b, 'c> Scope<'a, 'b, 'c> {
|
||||||
|
fn inner(&self, block: IRBlock<'a, 'b, 'c>) -> Scope<'a, 'b, 'c> {
|
||||||
|
self.data.clone().with_block(block)
|
||||||
|
}
|
||||||
|
}
|
222
reid/src/old_llvm/codegen.rs
Normal file
222
reid/src/old_llvm/codegen.rs
Normal file
@ -0,0 +1,222 @@
|
|||||||
|
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
reid/src/old_llvm/llvm_ir.rs
Normal file
203
reid/src/old_llvm/llvm_ir.rs
Normal file
@ -0,0 +1,203 @@
|
|||||||
|
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,4 +1,3 @@
|
|||||||
use crate::ast::*;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
lexer::Token,
|
lexer::Token,
|
||||||
token_stream::{Error, TokenRange, TokenStream},
|
token_stream::{Error, TokenRange, TokenStream},
|
||||||
@ -11,6 +10,14 @@ where
|
|||||||
fn parse(stream: TokenStream) -> Result<Self, Error>;
|
fn parse(stream: TokenStream) -> Result<Self, Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Type(pub TypeKind, pub TokenRange);
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum TypeKind {
|
||||||
|
I32,
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse for Type {
|
impl Parse for Type {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
let kind = if let Some(Token::Identifier(ident)) = stream.next() {
|
let kind = if let Some(Token::Identifier(ident)) = stream.next() {
|
||||||
@ -26,6 +33,24 @@ impl Parse for Type {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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>),
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse for Expression {
|
impl Parse for Expression {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Expression, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Expression, Error> {
|
||||||
let lhs = parse_primary_expression(&mut stream)?;
|
let lhs = parse_primary_expression(&mut stream)?;
|
||||||
@ -117,6 +142,16 @@ fn parse_binop_rhs(
|
|||||||
Ok(lhs)
|
Ok(lhs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum BinaryOperator {
|
||||||
|
Add,
|
||||||
|
Minus,
|
||||||
|
Mult,
|
||||||
|
|
||||||
|
And,
|
||||||
|
LessThan,
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse for BinaryOperator {
|
impl Parse for BinaryOperator {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
Ok(match (stream.next(), stream.peek()) {
|
Ok(match (stream.next(), stream.peek()) {
|
||||||
@ -134,6 +169,22 @@ 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>, pub TokenRange);
|
||||||
|
|
||||||
impl Parse for FunctionCallExpression {
|
impl Parse for FunctionCallExpression {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
if let Some(Token::Identifier(name)) = stream.next() {
|
if let Some(Token::Identifier(name)) = stream.next() {
|
||||||
@ -162,18 +213,23 @@ impl Parse for FunctionCallExpression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct IfExpression(pub Expression, pub Block, pub TokenRange);
|
||||||
|
|
||||||
impl Parse for IfExpression {
|
impl Parse for IfExpression {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
stream.expect(Token::If)?;
|
stream.expect(Token::If)?;
|
||||||
Ok(IfExpression(
|
Ok(IfExpression(
|
||||||
stream.parse()?,
|
stream.parse()?,
|
||||||
stream.parse()?,
|
stream.parse()?,
|
||||||
None,
|
|
||||||
stream.get_range().unwrap(),
|
stream.get_range().unwrap(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct LetStatement(pub String, pub Expression, pub TokenRange);
|
||||||
|
|
||||||
impl Parse for LetStatement {
|
impl Parse for LetStatement {
|
||||||
fn parse(mut stream: TokenStream) -> Result<LetStatement, Error> {
|
fn parse(mut stream: TokenStream) -> Result<LetStatement, Error> {
|
||||||
stream.expect(Token::LetKeyword)?;
|
stream.expect(Token::LetKeyword)?;
|
||||||
@ -194,6 +250,9 @@ impl Parse for LetStatement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct ImportStatement(Vec<String>, pub TokenRange);
|
||||||
|
|
||||||
impl Parse for ImportStatement {
|
impl Parse for ImportStatement {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
stream.expect(Token::ImportKeyword)?;
|
stream.expect(Token::ImportKeyword)?;
|
||||||
@ -219,6 +278,9 @@ impl Parse for ImportStatement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct FunctionDefinition(pub FunctionSignature, pub Block, pub TokenRange);
|
||||||
|
|
||||||
impl Parse for FunctionDefinition {
|
impl Parse for FunctionDefinition {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
stream.expect(Token::FnKeyword)?;
|
stream.expect(Token::FnKeyword)?;
|
||||||
@ -230,6 +292,14 @@ impl Parse for FunctionDefinition {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct FunctionSignature {
|
||||||
|
pub name: String,
|
||||||
|
pub args: Vec<(String, Type)>,
|
||||||
|
pub return_type: Option<Type>,
|
||||||
|
pub range: TokenRange,
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse for FunctionSignature {
|
impl Parse for FunctionSignature {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
if let Some(Token::Identifier(name)) = stream.next() {
|
if let Some(Token::Identifier(name)) = stream.next() {
|
||||||
@ -261,6 +331,19 @@ 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)>,
|
||||||
|
pub TokenRange,
|
||||||
|
);
|
||||||
|
|
||||||
impl Parse for Block {
|
impl Parse for Block {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
let mut statements = Vec::new();
|
let mut statements = Vec::new();
|
||||||
@ -299,6 +382,14 @@ impl Parse for Block {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum BlockLevelStatement {
|
||||||
|
Let(LetStatement),
|
||||||
|
Import(ImportStatement),
|
||||||
|
Expression(Expression),
|
||||||
|
Return(ReturnType, Expression),
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse for BlockLevelStatement {
|
impl Parse for BlockLevelStatement {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
use BlockLevelStatement as Stmt;
|
use BlockLevelStatement as Stmt;
|
||||||
@ -326,6 +417,12 @@ impl Parse for BlockLevelStatement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum TopLevelStatement {
|
||||||
|
Import(ImportStatement),
|
||||||
|
FunctionDefinition(FunctionDefinition),
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse for TopLevelStatement {
|
impl Parse for TopLevelStatement {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
use TopLevelStatement as Stmt;
|
use TopLevelStatement as Stmt;
|
@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ast::parse::Parse,
|
|
||||||
lexer::{FullToken, Position, Token},
|
lexer::{FullToken, Position, Token},
|
||||||
|
parser::Parse,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct TokenStream<'a, 'b> {
|
pub struct TokenStream<'a, 'b> {
|
||||||
@ -177,27 +177,6 @@ impl Default for TokenRange {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("Expected {} at Ln {}, Col {}, got {:?}", .0, (.2).1, (.2).0, .1)]
|
#[error("Expected {} at Ln {}, Col {}, got {:?}", .0, (.2).1, (.2).0, .1)]
|
||||||
|
Loading…
Reference in New Issue
Block a user