From fed52c03275760a8b38dd732f90965a77669783d Mon Sep 17 00:00:00 2001 From: Edgar Date: Sat, 27 Jan 2024 08:55:54 -0300 Subject: [PATCH] a --- Cargo.lock | 27 +++- Cargo.toml | 2 +- lib/edlang_ast/Cargo.toml | 1 + lib/edlang_ast/src/lib.rs | 12 +- lib/edlang_ir/Cargo.toml | 10 ++ lib/edlang_ir/src/lib.rs | 248 +++++++++++++++++++++++++++++++++ lib/edlang_lowering/Cargo.toml | 10 ++ lib/edlang_lowering/src/lib.rs | 14 ++ lib/edlang_span/Cargo.toml | 8 ++ lib/edlang_span/src/lib.rs | 11 ++ 10 files changed, 329 insertions(+), 14 deletions(-) create mode 100644 lib/edlang_ir/Cargo.toml create mode 100644 lib/edlang_ir/src/lib.rs create mode 100644 lib/edlang_lowering/Cargo.toml create mode 100644 lib/edlang_lowering/src/lib.rs create mode 100644 lib/edlang_span/Cargo.toml create mode 100644 lib/edlang_span/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 4ac654de2..cfb4fbfe9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 256d30d58..d70f2dda0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/lib/edlang_ast/Cargo.toml b/lib/edlang_ast/Cargo.toml index 9c4f18795..8cdefde4c 100644 --- a/lib/edlang_ast/Cargo.toml +++ b/lib/edlang_ast/Cargo.toml @@ -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" } diff --git a/lib/edlang_ast/src/lib.rs b/lib/edlang_ast/src/lib.rs index 28a0ceffa..6d8efbb8e 100644 --- a/lib/edlang_ast/src/lib.rs +++ b/lib/edlang_ast/src/lib.rs @@ -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 { diff --git a/lib/edlang_ir/Cargo.toml b/lib/edlang_ir/Cargo.toml new file mode 100644 index 000000000..2d82ac18b --- /dev/null +++ b/lib/edlang_ir/Cargo.toml @@ -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" diff --git a/lib/edlang_ir/src/lib.rs b/lib/edlang_ir/src/lib.rs new file mode 100644 index 000000000..33434b065 --- /dev/null +++ b/lib/edlang_ir/src/lib.rs @@ -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, + 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, + 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, + dest: Place, + target: Option, // block + fn_span: Span, + }, + Unreachable, +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct TypeInfo { + pub id: usize, + pub debug_info: Option, + 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 }, +} + +#[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, + 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), +} + +#[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, +} diff --git a/lib/edlang_lowering/Cargo.toml b/lib/edlang_lowering/Cargo.toml new file mode 100644 index 000000000..c03eecb8f --- /dev/null +++ b/lib/edlang_lowering/Cargo.toml @@ -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" } diff --git a/lib/edlang_lowering/src/lib.rs b/lib/edlang_lowering/src/lib.rs new file mode 100644 index 000000000..7d12d9af8 --- /dev/null +++ b/lib/edlang_lowering/src/lib.rs @@ -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); + } +} diff --git a/lib/edlang_span/Cargo.toml b/lib/edlang_span/Cargo.toml new file mode 100644 index 000000000..783b7cb53 --- /dev/null +++ b/lib/edlang_span/Cargo.toml @@ -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] diff --git a/lib/edlang_span/src/lib.rs b/lib/edlang_span/src/lib.rs new file mode 100644 index 000000000..0571779fa --- /dev/null +++ b/lib/edlang_span/src/lib.rs @@ -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 } + } +}