This commit is contained in:
Edgar 2024-01-27 08:55:54 -03:00
parent 9333638933
commit fed52c0327
10 changed files with 329 additions and 14 deletions

27
Cargo.lock generated
View file

@ -525,6 +525,9 @@ dependencies = [
[[package]]
name = "edlang_ast"
version = "0.1.0"
dependencies = [
"edlang_span",
]
[[package]]
name = "edlang_check"
@ -566,6 +569,22 @@ dependencies = [
"tracing-subscriber",
]
[[package]]
name = "edlang_ir"
version = "0.1.0"
dependencies = [
"edlang_span",
"smallvec",
]
[[package]]
name = "edlang_lowering"
version = "0.1.0"
dependencies = [
"edlang_ast",
"edlang_ir",
]
[[package]]
name = "edlang_parser"
version = "0.1.0"
@ -586,6 +605,10 @@ dependencies = [
"ariadne",
]
[[package]]
name = "edlang_span"
version = "0.1.0"
[[package]]
name = "either"
version = "1.9.0"
@ -1414,9 +1437,9 @@ dependencies = [
[[package]]
name = "smallvec"
version = "1.12.0"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2593d31f82ead8df961d8bd23a64c2ccf2eb5dd34b0a34bfb4dd54011c72009e"
checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7"
[[package]]
name = "string_cache"

View file

@ -1,7 +1,7 @@
[workspace]
resolver = "2"
members = [ "bin/edlang", "lib/edlang_ast", "lib/edlang_check", "lib/edlang_codegen_mlir", "lib/edlang_driver","lib/edlang_parser", "lib/edlang_session"]
members = [ "bin/edlang", "lib/edlang_ast", "lib/edlang_check", "lib/edlang_codegen_mlir", "lib/edlang_driver", "lib/edlang_ir", "lib/edlang_lowering","lib/edlang_parser", "lib/edlang_session", "lib/edlang_span"]
[profile.release]
lto = true

View file

@ -11,3 +11,4 @@ categories = ["compilers"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
edlang_span = { version = "0.1.0", path = "../edlang_span" }

View file

@ -1,14 +1,4 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
pub struct Span {
pub lo: usize,
pub hi: usize,
}
impl Span {
pub fn new(lo: usize, hi: usize) -> Self {
Self { lo, hi }
}
}
pub use edlang_span::Span;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Module {

10
lib/edlang_ir/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "edlang_ir"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
edlang_span = { version = "0.1.0", path = "../edlang_span" }
smallvec = "1.13.1"

248
lib/edlang_ir/src/lib.rs Normal file
View file

@ -0,0 +1,248 @@
// Based on a cfg
use edlang_span::Span;
use smallvec::SmallVec;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Body {
pub locals: SmallVec<[Local; 4]>,
pub blocks: SmallVec<[BasicBlock; 8]>,
pub debug_info: SmallVec<[DebugInfo; 4]>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DebugInfo {
pub id: usize,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BasicBlock {
pub id: usize,
pub statements: SmallVec<[Statement; 8]>,
pub terminator: Terminator,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Local {
pub id: usize,
pub mutable: bool,
pub debug_info: Option<usize>,
pub ty: usize,
pub kind: LocalKind,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
pub enum LocalKind {
Temp,
Arg,
ReturnPointer,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Statement {
pub id: usize,
pub debug_info: Option<usize>,
pub kind: StatementKind,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum StatementKind {
Assign(Place, RValue),
StorageLive(usize),
StorageDead(usize),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Terminator {
Target(usize),
Return,
Switch,
Call {
func: Operand,
args: Vec<Operand>,
dest: Place,
target: Option<usize>, // block
fn_span: Span,
},
Unreachable,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TypeInfo {
pub id: usize,
pub debug_info: Option<usize>,
pub kind: TypeKind,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TypeKind {
Bool,
Char,
Int(IntTy),
Uint(UintTy),
Float(FloatTY),
FuncDef { name: String, args: Vec<Self> },
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum IntTy {
I128,
I64,
I32,
I16,
I8,
Isize,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum UintTy {
U128,
U64,
U32,
U16,
U8,
Usize,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FloatTY {
F32,
F64,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ConstData {
pub debug_info: Option<usize>,
pub type_info: usize,
pub kind: ConstKind,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ConstKind {
Value(ValueTree),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
pub struct ScalarInt {
pub data: u128,
pub size: u8,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ValueTree {
Leaf(ScalarInt),
Branch(Vec<Self>),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RValue {
Use(Operand),
BinOp(BinOp, Operand, Operand),
UnOp(UnOp, Operand),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Operand {
Copy(Place),
Move(Place),
Constant(ConstData),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Place {
pub local: usize,
pub projection: SmallVec<[PlaceElem; 1]>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
pub enum PlaceElem {
Deref,
Field { field_idx: usize, type_info: usize },
Index { local: usize },
}
impl TypeKind {
pub fn get_i128() -> Self {
Self::Int(IntTy::I128)
}
pub fn get_i64() -> Self {
Self::Int(IntTy::I64)
}
pub fn get_i32() -> Self {
Self::Int(IntTy::I32)
}
pub fn get_i16() -> Self {
Self::Int(IntTy::I16)
}
pub fn get_i8() -> Self {
Self::Int(IntTy::I8)
}
pub fn get_u128() -> Self {
Self::Uint(UintTy::U128)
}
pub fn get_u64() -> Self {
Self::Uint(UintTy::U64)
}
pub fn get_u32() -> Self {
Self::Uint(UintTy::U32)
}
pub fn get_u16() -> Self {
Self::Uint(UintTy::U16)
}
pub fn get_u8() -> Self {
Self::Uint(UintTy::U8)
}
pub fn get_f32() -> Self {
Self::Float(FloatTY::F32)
}
pub fn get_f64() -> Self {
Self::Float(FloatTY::F64)
}
pub fn get_bool() -> Self {
Self::Bool
}
pub fn get_char() -> Self {
Self::Char
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
Rem,
BitXor,
BitAnd,
BitOr,
Shl,
Shr,
Eq,
Lt,
Le,
Ne,
Ge,
Gt,
Offset,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
pub enum UnOp {
Not,
Neg,
}

View file

@ -0,0 +1,10 @@
[package]
name = "edlang_lowering"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
edlang_ast = { version = "0.1.0", path = "../edlang_ast" }
edlang_ir = { version = "0.1.0", path = "../edlang_ir" }

View file

@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}

View file

@ -0,0 +1,8 @@
[package]
name = "edlang_span"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,11 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
pub struct Span {
pub lo: usize,
pub hi: usize,
}
impl Span {
pub fn new(lo: usize, hi: usize) -> Self {
Self { lo, hi }
}
}