From bfc07ea4a0a676660d2328326a0311312a391f94 Mon Sep 17 00:00:00 2001 From: Edgar Date: Tue, 7 May 2024 12:35:35 +0200 Subject: [PATCH 1/4] Add more type and mutability checks --- crates/concrete_check/src/lib.rs | 46 ++++++++++++++++ crates/concrete_driver/tests/checks.rs | 55 +++++++++++++++++-- .../invalid_programs/immutable_mutation.con | 7 +++ .../tests/invalid_programs/invalid_assign.con | 8 +++ .../{refmut.con => invalid_borrow_mut.con} | 2 +- .../mutable_nonmut_borrow.con | 8 +++ crates/concrete_ir/src/lib.rs | 44 ++++++++++++++- crates/concrete_ir/src/lowering.rs | 50 ++++++++++++++--- crates/concrete_ir/src/lowering/errors.rs | 12 ++++ examples/for.con | 2 +- examples/for_while.con | 2 +- examples/if_if_false.con | 2 +- 12 files changed, 220 insertions(+), 18 deletions(-) create mode 100644 crates/concrete_driver/tests/invalid_programs/immutable_mutation.con create mode 100644 crates/concrete_driver/tests/invalid_programs/invalid_assign.con rename crates/concrete_driver/tests/invalid_programs/{refmut.con => invalid_borrow_mut.con} (82%) create mode 100644 crates/concrete_driver/tests/invalid_programs/mutable_nonmut_borrow.con diff --git a/crates/concrete_check/src/lib.rs b/crates/concrete_check/src/lib.rs index 608d53b..c10dc8b 100644 --- a/crates/concrete_check/src/lib.rs +++ b/crates/concrete_check/src/lib.rs @@ -222,5 +222,51 @@ pub fn lowering_error_to_report( .with_message(msg) .finish() } + LoweringError::NotMutable { + span, + declare_span, + program_id, + } => { + let path = file_paths[program_id].display().to_string(); + let mut report = Report::build(ReportKind::Error, path.clone(), span.from) + .with_code("NotMutable") + .with_label( + Label::new((path.clone(), span.into())) + .with_message("can't mutate this variable because it's not mutable") + .with_color(colors.next()), + ); + + if let Some(declare_span) = declare_span { + report = report.with_label( + Label::new((path, declare_span.into())) + .with_message("variable declared here") + .with_color(colors.next()), + ); + } + report.finish() + } + LoweringError::CantTakeMutableBorrow { + span, + declare_span, + program_id, + } => { + let path = file_paths[program_id].display().to_string(); + let mut report = Report::build(ReportKind::Error, path.clone(), span.from) + .with_code("CantTakeMutableBorrow") + .with_label( + Label::new((path.clone(), span.into())) + .with_message("can't take a mutate borrow to this variable because it's not declared mutable") + .with_color(colors.next()), + ); + + if let Some(declare_span) = declare_span { + report = report.with_label( + Label::new((path, declare_span.into())) + .with_message("variable declared here") + .with_color(colors.next()), + ); + } + report.finish() + } } } diff --git a/crates/concrete_driver/tests/checks.rs b/crates/concrete_driver/tests/checks.rs index 2a5e670..6673a6e 100644 --- a/crates/concrete_driver/tests/checks.rs +++ b/crates/concrete_driver/tests/checks.rs @@ -35,14 +35,14 @@ fn import_not_found() { #[test] fn invalid_borrow_mut() { - let (source, name) = (include_str!("invalid_programs/refmut.con"), "refmut"); + let (source, name) = ( + include_str!("invalid_programs/invalid_borrow_mut.con"), + "invalid_borrow_mut", + ); let error = check_invalid_program(source, name); assert!( - matches!( - &error, - LoweringError::BorrowNotMutable { name, .. } if name == "a" - ), + matches!(&error, LoweringError::NotMutable { .. }), "{:#?}", error ); @@ -102,3 +102,48 @@ fn undeclared_var() { error ); } + +#[test] +fn invalid_assign() { + let (source, name) = ( + include_str!("invalid_programs/invalid_assign.con"), + "invalid_assign", + ); + let error = check_invalid_program(source, name); + + assert!( + matches!(&error, LoweringError::UnexpectedType { .. }), + "{:#?}", + error + ); +} + +#[test] +fn immutable_mutation() { + let (source, name) = ( + include_str!("invalid_programs/immutable_mutation.con"), + "immutable_mutation", + ); + let error = check_invalid_program(source, name); + + assert!( + matches!(&error, LoweringError::NotMutable { .. }), + "{:#?}", + error + ); +} + +#[test] +fn mutable_nonmut_borrow() { + let (source, name) = ( + include_str!("invalid_programs/mutable_nonmut_borrow.con"), + "mutable_nonmut_borrow", + ); + let error = check_invalid_program(source, name); + + assert!( + matches!(&error, LoweringError::CantTakeMutableBorrow { .. }), + "{:#?}", + error + ); +} diff --git a/crates/concrete_driver/tests/invalid_programs/immutable_mutation.con b/crates/concrete_driver/tests/invalid_programs/immutable_mutation.con new file mode 100644 index 0000000..9d42bab --- /dev/null +++ b/crates/concrete_driver/tests/invalid_programs/immutable_mutation.con @@ -0,0 +1,7 @@ +mod Simple { + fn main() -> i32 { + let x: i32 = 2; + x = 4; + return x; + } +} diff --git a/crates/concrete_driver/tests/invalid_programs/invalid_assign.con b/crates/concrete_driver/tests/invalid_programs/invalid_assign.con new file mode 100644 index 0000000..69ace67 --- /dev/null +++ b/crates/concrete_driver/tests/invalid_programs/invalid_assign.con @@ -0,0 +1,8 @@ +mod Simple { + fn main() -> i32 { + let mut x: i32 = 2; + let y: i64 = 4; + x = y; + return x; + } +} diff --git a/crates/concrete_driver/tests/invalid_programs/refmut.con b/crates/concrete_driver/tests/invalid_programs/invalid_borrow_mut.con similarity index 82% rename from crates/concrete_driver/tests/invalid_programs/refmut.con rename to crates/concrete_driver/tests/invalid_programs/invalid_borrow_mut.con index 526ea46..7cbfb46 100644 --- a/crates/concrete_driver/tests/invalid_programs/refmut.con +++ b/crates/concrete_driver/tests/invalid_programs/invalid_borrow_mut.con @@ -1,7 +1,7 @@ mod Simple { fn main() -> i32 { - let x: i32 = 1; + let mut x: i32 = 1; hello(&x); return x; } diff --git a/crates/concrete_driver/tests/invalid_programs/mutable_nonmut_borrow.con b/crates/concrete_driver/tests/invalid_programs/mutable_nonmut_borrow.con new file mode 100644 index 0000000..e0bf1f5 --- /dev/null +++ b/crates/concrete_driver/tests/invalid_programs/mutable_nonmut_borrow.con @@ -0,0 +1,8 @@ +mod Simple { + fn main() -> i32 { + let x: i32 = 2; + let y: &mut i32 = &mut x; + *y = 4; + return *y; + } +} diff --git a/crates/concrete_ir/src/lib.rs b/crates/concrete_ir/src/lib.rs index 7be040f..1f425e4 100644 --- a/crates/concrete_ir/src/lib.rs +++ b/crates/concrete_ir/src/lib.rs @@ -178,6 +178,17 @@ pub enum Rvalue { Cast(Operand, Ty, Span), } +impl Rvalue { + pub fn get_local(&self) -> Option { + match self { + Rvalue::Use(op) => op.get_local(), + Rvalue::Ref(_, op) => Some(op.local), + Rvalue::Cast(op, _, _) => op.get_local(), + _ => None, + } + } +} + /// A operand is a value, either from a place in memory or constant data. #[derive(Debug, Clone)] pub enum Operand { @@ -185,6 +196,15 @@ pub enum Operand { Const(ConstData), } +impl Operand { + pub fn get_local(&self) -> Option { + match self { + Operand::Place(place) => Some(place.local), + Operand::Const(_) => None, + } + } +} + /// A place in memory, defined by the given local and it's projection (deref, field, index, etc). #[derive(Debug, Clone)] pub struct Place { @@ -216,15 +236,24 @@ pub struct Local { pub ty: Ty, /// The type of local. pub kind: LocalKind, + /// Whether this local is declared mutable. + pub mutable: bool, } impl Local { - pub fn new(span: Option, kind: LocalKind, ty: Ty, debug_name: Option) -> Self { + pub fn new( + span: Option, + kind: LocalKind, + ty: Ty, + debug_name: Option, + mutable: bool, + ) -> Self { Self { span, kind, ty, debug_name, + mutable, } } @@ -234,6 +263,19 @@ impl Local { ty, kind: LocalKind::Temp, debug_name: None, + mutable: false, + } + } + + pub fn is_mutable(&self) -> bool { + if self.mutable { + return true; + } + + match self.ty.kind { + TyKind::Ptr(_, is_mut) => matches!(is_mut, Mutability::Mut), + TyKind::Ref(_, is_mut) => matches!(is_mut, Mutability::Mut), + _ => false, } } } diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index 6cf620e..19189fb 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -258,10 +258,13 @@ fn lower_func( .clone(); builder.ret_local = builder.body.locals.len(); - builder - .body - .locals - .push(Local::new(None, LocalKind::ReturnPointer, ret_ty, None)); + builder.body.locals.push(Local::new( + None, + LocalKind::ReturnPointer, + ret_ty, + None, + false, + )); for (arg, ty) in func.decl.params.iter().zip(args_ty) { builder @@ -272,6 +275,7 @@ fn lower_func( LocalKind::Arg, ty, Some(arg.name.name.clone()), + false, )); } @@ -289,6 +293,7 @@ fn lower_func( LocalKind::Temp, ty, Some(name.name.clone()), + info.is_mutable, )); } LetStmtTarget::Destructure(_) => todo!(), @@ -306,6 +311,7 @@ fn lower_func( LocalKind::Temp, ty, Some(name.name.clone()), + info.is_mutable, )); } LetStmtTarget::Destructure(_) => todo!(), @@ -684,12 +690,12 @@ fn lower_let(builder: &mut FnBodyBuilder, info: &LetStmt) -> Result<(), Lowering match &info.target { LetStmtTarget::Simple { name, r#type } => { let ty = lower_type(&builder.ctx, r#type, builder.local_module)?; - let (rvalue, rvalue_ty, _exp_span) = + let (rvalue, rvalue_ty, rvalue_span) = lower_expression(builder, &info.value, Some(ty.clone()))?; if ty.kind != rvalue_ty.kind { return Err(LoweringError::UnexpectedType { - span: info.span, + span: rvalue_span, found: rvalue_ty, expected: ty.clone(), program_id: builder.local_module.program_id, @@ -720,6 +726,14 @@ fn lower_let(builder: &mut FnBodyBuilder, info: &LetStmt) -> Result<(), Lowering fn lower_assign(builder: &mut FnBodyBuilder, info: &AssignStmt) -> Result<(), LoweringError> { let (mut place, mut ty, _path_span) = lower_path(builder, &info.target)?; + if !builder.body.locals[place.local].is_mutable() { + return Err(LoweringError::NotMutable { + span: info.span, + declare_span: builder.body.locals[place.local].span, + program_id: builder.body.id.program_id, + }); + } + for _ in 0..info.derefs { match &ty.kind { TyKind::Ref(inner, is_mut) | TyKind::Ptr(inner, is_mut) => { @@ -738,7 +752,17 @@ fn lower_assign(builder: &mut FnBodyBuilder, info: &AssignStmt) -> Result<(), Lo place.projection.push(PlaceElem::Deref); } - let (rvalue, _rvalue_ty, _exp_span) = lower_expression(builder, &info.value, Some(ty.clone()))?; + let (rvalue, rvalue_ty, rvalue_span) = + lower_expression(builder, &info.value, Some(ty.clone()))?; + + if ty.kind != rvalue_ty.kind { + return Err(LoweringError::UnexpectedType { + span: rvalue_span, + found: rvalue_ty, + expected: ty.clone(), + program_id: builder.local_module.program_id, + }); + } builder.statements.push(Statement { span: Some(info.target.first.span), @@ -935,7 +959,17 @@ fn lower_expression( }, None => None, }; - let (value, ty, _span) = lower_expression(builder, inner, type_hint)?; + let (value, ty, _ref_target_span) = lower_expression(builder, inner, type_hint)?; + + if let Some(local) = value.get_local() { + if *mutable && !builder.body.locals[local].mutable { + return Err(LoweringError::CantTakeMutableBorrow { + span: *asref_span, + declare_span: builder.body.locals[local].span, + program_id: builder.body.id.program_id, + }); + } + } let mutability = match mutable { false => Mutability::Not, diff --git a/crates/concrete_ir/src/lowering/errors.rs b/crates/concrete_ir/src/lowering/errors.rs index 48d9a58..8d329c8 100644 --- a/crates/concrete_ir/src/lowering/errors.rs +++ b/crates/concrete_ir/src/lowering/errors.rs @@ -43,6 +43,18 @@ pub enum LoweringError { name: String, program_id: usize, }, + #[error("can't mutate this value because it's not declared mutable")] + NotMutable { + span: Span, + declare_span: Option, + program_id: usize, + }, + #[error("can't take a mutable borrow to this value because it's not declared mutable")] + CantTakeMutableBorrow { + span: Span, + declare_span: Option, + program_id: usize, + }, #[error("unrecognized type {name}")] UnrecognizedType { span: Span, diff --git a/examples/for.con b/examples/for.con index 1f2ddd0..154c7d5 100644 --- a/examples/for.con +++ b/examples/for.con @@ -6,7 +6,7 @@ mod Example { fn sum_to(limit: i64) -> i64 { let mut result: i64 = 0; - for (let n: i64 = 1; n <= limit; n = n + 1) { + for (let mut n: i64 = 1; n <= limit; n = n + 1) { result = result + n; } diff --git a/examples/for_while.con b/examples/for_while.con index d87f1d7..3da1adf 100644 --- a/examples/for_while.con +++ b/examples/for_while.con @@ -6,7 +6,7 @@ mod Example { fn sum_to(limit: i64) -> i64 { let mut result: i64 = 0; - let n: i64 = 1; + let mut n: i64 = 1; for (n <= limit) { result = result + n; n = n + 1; diff --git a/examples/if_if_false.con b/examples/if_if_false.con index a38c67d..456cd4c 100644 --- a/examples/if_if_false.con +++ b/examples/if_if_false.con @@ -1,6 +1,6 @@ mod Example { fn main() -> i32 { - let foo: i32 = 7; + let mut foo: i32 = 7; if true { if false { From aff819dfefd124caf6310d191a087826c8c3a95c Mon Sep 17 00:00:00 2001 From: Edgar Date: Tue, 7 May 2024 15:33:43 +0200 Subject: [PATCH 2/4] Add pointer addition (arithmetic) and intrinsic parsing --- crates/concrete_ast/src/common.rs | 7 ++++ crates/concrete_ast/src/functions.rs | 3 +- crates/concrete_codegen_mlir/src/codegen.rs | 39 ++++++++++++++++++++- crates/concrete_codegen_mlir/src/errors.rs | 2 ++ crates/concrete_driver/tests/common.rs | 18 +++++++++- crates/concrete_driver/tests/examples.rs | 22 +++++++++++- crates/concrete_ir/src/lib.rs | 16 ++++++++- crates/concrete_ir/src/lowering.rs | 35 ++++++++++++++---- crates/concrete_parser/src/grammar.lalrpop | 20 ++++++++++- crates/concrete_parser/src/lib.rs | 11 ++++++ crates/concrete_parser/src/tokens.rs | 2 ++ examples/hello_world_hacky.con | 36 +++++++++++++++++++ 12 files changed, 198 insertions(+), 13 deletions(-) create mode 100644 examples/hello_world_hacky.con diff --git a/crates/concrete_ast/src/common.rs b/crates/concrete_ast/src/common.rs index 3de3ee3..5b8d48f 100644 --- a/crates/concrete_ast/src/common.rs +++ b/crates/concrete_ast/src/common.rs @@ -37,3 +37,10 @@ pub struct GenericParam { pub params: Vec, pub span: Span, } + +#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)] +pub struct Attribute { + pub name: String, + pub value: Option, + pub span: Span, +} diff --git a/crates/concrete_ast/src/functions.rs b/crates/concrete_ast/src/functions.rs index 7ef8746..42c2e0d 100644 --- a/crates/concrete_ast/src/functions.rs +++ b/crates/concrete_ast/src/functions.rs @@ -1,5 +1,5 @@ use crate::{ - common::{DocString, GenericParam, Ident, Span}, + common::{Attribute, DocString, GenericParam, Ident, Span}, statements::Statement, types::TypeSpec, }; @@ -13,6 +13,7 @@ pub struct FunctionDecl { pub ret_type: Option, pub is_extern: bool, pub is_pub: bool, + pub attributes: Vec, pub span: Span, } diff --git a/crates/concrete_codegen_mlir/src/codegen.rs b/crates/concrete_codegen_mlir/src/codegen.rs index 1aa5e5d..2dd5fb8 100644 --- a/crates/concrete_codegen_mlir/src/codegen.rs +++ b/crates/concrete_codegen_mlir/src/codegen.rs @@ -613,10 +613,32 @@ fn compile_binop<'c: 'b, 'b>( let is_float = matches!(lhs_ty.kind, TyKind::Float(_)); let is_signed = matches!(lhs_ty.kind, TyKind::Int(_)); + let is_ptr = if let TyKind::Ptr(inner, _) = &lhs_ty.kind { + Some((*inner).clone()) + } else { + None + }; Ok(match op { BinOp::Add => { - let value = if is_float { + let value = if let Some(inner) = is_ptr { + let inner_ty = compile_type(ctx.module_ctx, &inner); + block + .append_operation( + ods::llvm::getelementptr( + ctx.context(), + pointer(ctx.context(), 0), + lhs, + &[rhs], + DenseI32ArrayAttribute::new(ctx.context(), &[i32::MIN]), + TypeAttribute::new(inner_ty), + location, + ) + .into(), + ) + .result(0)? + .into() + } else if is_float { block .append_operation(arith::addf(lhs, rhs, location)) .result(0)? @@ -630,6 +652,11 @@ fn compile_binop<'c: 'b, 'b>( (value, lhs_ty) } BinOp::Sub => { + if is_ptr.is_some() { + return Err(CodegenError::NotImplemented( + "substracting from a pointer is not yet implemented".to_string(), + )); + } let value = if is_float { block .append_operation(arith::subf(lhs, rhs, location)) @@ -644,6 +671,11 @@ fn compile_binop<'c: 'b, 'b>( (value, lhs_ty) } BinOp::Mul => { + if is_ptr.is_some() { + return Err(CodegenError::NotImplemented( + "multiplying a pointer is not yet implemented".to_string(), + )); + } let value = if is_float { block .append_operation(arith::mulf(lhs, rhs, location)) @@ -658,6 +690,11 @@ fn compile_binop<'c: 'b, 'b>( (value, lhs_ty) } BinOp::Div => { + if is_ptr.is_some() { + return Err(CodegenError::NotImplemented( + "dividing a pointer is not yet implemented".to_string(), + )); + } let value = if is_float { block .append_operation(arith::divf(lhs, rhs, location)) diff --git a/crates/concrete_codegen_mlir/src/errors.rs b/crates/concrete_codegen_mlir/src/errors.rs index 55d5092..1d90b88 100644 --- a/crates/concrete_codegen_mlir/src/errors.rs +++ b/crates/concrete_codegen_mlir/src/errors.rs @@ -8,4 +8,6 @@ pub enum CodegenError { LLVMCompileError(String), #[error("melior error: {0}")] MeliorError(#[from] melior::Error), + #[error("not yet implemented: {0}")] + NotImplemented(String), } diff --git a/crates/concrete_driver/tests/common.rs b/crates/concrete_driver/tests/common.rs index ba1e261..bdf2e5d 100644 --- a/crates/concrete_driver/tests/common.rs +++ b/crates/concrete_driver/tests/common.rs @@ -2,7 +2,7 @@ use std::{ borrow::Cow, fmt, path::{Path, PathBuf}, - process::Output, + process::{Output, Stdio}, }; use ariadne::Source; @@ -110,6 +110,7 @@ pub fn compile_program( pub fn run_program(program: &Path) -> Result { std::process::Command::new(program) + .stdout(Stdio::piped()) .spawn()? .wait_with_output() } @@ -122,3 +123,18 @@ pub fn compile_and_run(source: &str, name: &str, library: bool, optlevel: OptLev output.status.code().unwrap() } + +#[allow(unused)] // false positive +#[track_caller] +pub fn compile_and_run_output( + source: &str, + name: &str, + library: bool, + optlevel: OptLevel, +) -> String { + let result = compile_program(source, name, library, optlevel).expect("failed to compile"); + + let output = run_program(&result.binary_file).expect("failed to run"); + + std::str::from_utf8(&output.stdout).unwrap().to_string() +} diff --git a/crates/concrete_driver/tests/examples.rs b/crates/concrete_driver/tests/examples.rs index 455f58e..1377b7f 100644 --- a/crates/concrete_driver/tests/examples.rs +++ b/crates/concrete_driver/tests/examples.rs @@ -1,4 +1,4 @@ -use crate::common::compile_and_run; +use crate::common::{compile_and_run, compile_and_run_output}; use concrete_session::config::OptLevel; use test_case::test_case; @@ -39,3 +39,23 @@ fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32) { compile_and_run(source, name, is_library, OptLevel::Aggressive) ); } + +#[test_case(include_str!("../../../examples/hello_world_hacky.con"), "hello_world_hacky", false, "Hello World\n" ; "hello_world_hacky.con")] +fn example_tests_with_output(source: &str, name: &str, is_library: bool, result: &str) { + assert_eq!( + result, + compile_and_run_output(source, name, is_library, OptLevel::None) + ); + assert_eq!( + result, + compile_and_run_output(source, name, is_library, OptLevel::Less) + ); + assert_eq!( + result, + compile_and_run_output(source, name, is_library, OptLevel::Default) + ); + assert_eq!( + result, + compile_and_run_output(source, name, is_library, OptLevel::Aggressive) + ); +} diff --git a/crates/concrete_ir/src/lib.rs b/crates/concrete_ir/src/lib.rs index 7be040f..464a532 100644 --- a/crates/concrete_ir/src/lib.rs +++ b/crates/concrete_ir/src/lib.rs @@ -68,6 +68,7 @@ pub struct FnBody { pub id: DefId, pub name: String, pub is_extern: bool, + pub is_intrinsic: Option, pub basic_blocks: Vec, pub locals: Vec, } @@ -397,7 +398,15 @@ impl fmt::Display for TyKind { FloatTy::F64 => write!(f, "f32"), }, TyKind::String => write!(f, "string"), - TyKind::Array(_, _) => todo!(), + TyKind::Array(inner, size) => { + let value = + if let ConstKind::Value(ValueTree::Leaf(ConstValue::U64(x))) = &size.data { + *x + } else { + unreachable!("const data for array sizes should always be u64") + }; + write!(f, "[{}; {:?}]", inner.kind, value) + } TyKind::Ref(inner, is_mut) => { let word = if let Mutability::Mut = is_mut { "mut" @@ -571,3 +580,8 @@ pub enum ConstValue { F32(f32), F64(f64), } + +#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] +pub enum ConcreteIntrinsic { + // Todo: Add intrinsics here +} diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index 6cf620e..dd2104c 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -16,10 +16,10 @@ use concrete_ast::{ }; use crate::{ - AdtBody, BasicBlock, BinOp, ConstData, ConstKind, ConstValue, DefId, FloatTy, FnBody, IntTy, - Local, LocalKind, LogOp, Mutability, Operand, Place, PlaceElem, ProgramBody, Rvalue, Statement, - StatementKind, SwitchTargets, Terminator, TerminatorKind, Ty, TyKind, UintTy, ValueTree, - VariantDef, + AdtBody, BasicBlock, BinOp, ConcreteIntrinsic, ConstData, ConstKind, ConstValue, DefId, + FloatTy, FnBody, IntTy, Local, LocalKind, LogOp, Mutability, Operand, Place, PlaceElem, + ProgramBody, Rvalue, Statement, StatementKind, SwitchTargets, Terminator, TerminatorKind, Ty, + TyKind, UintTy, ValueTree, VariantDef, }; use self::errors::LoweringError; @@ -217,11 +217,16 @@ fn lower_func( func: &FunctionDef, module_id: DefId, ) -> Result { + let is_intrinsic: Option = None; + + // TODO: parse insintrics here. + let mut builder = FnBodyBuilder { body: FnBody { basic_blocks: Vec::new(), locals: Vec::new(), is_extern: func.decl.is_extern, + is_intrinsic, name: func.decl.name.name.clone(), id: { let body = ctx.body.modules.get(&module_id).unwrap(); @@ -350,11 +355,16 @@ fn lower_func_decl( func: &FunctionDecl, module_id: DefId, ) -> Result { + let is_intrinsic: Option = None; + + // TODO: parse insintrics here. + let builder = FnBodyBuilder { body: FnBody { basic_blocks: Vec::new(), locals: Vec::new(), is_extern: func.is_extern, + is_intrinsic, name: func.name.name.clone(), id: { let body = ctx.body.modules.get(&module_id).unwrap(); @@ -1236,14 +1246,22 @@ fn lower_binary_op( } else { lower_expression(builder, lhs, type_hint.clone())? }; + + // We must handle the special case where you can do ptr + offset. + let is_lhs_ptr = matches!(lhs_ty.kind, TyKind::Ptr(_, _)); + let (rhs, rhs_ty, rhs_span) = if type_hint.is_none() { let ty = find_expression_type(builder, rhs).unwrap_or(lhs_ty.clone()); - lower_expression(builder, rhs, Some(ty))? + lower_expression(builder, rhs, if is_lhs_ptr { None } else { Some(ty) })? } else { - lower_expression(builder, rhs, type_hint.clone())? + lower_expression( + builder, + rhs, + if is_lhs_ptr { None } else { type_hint.clone() }, + )? }; - if lhs_ty != rhs_ty { + if !is_lhs_ptr && lhs_ty != rhs_ty { return Err(LoweringError::UnexpectedType { span: rhs_span, found: rhs_ty, @@ -1409,6 +1427,9 @@ fn lower_value_expr( UintTy::U128 => ConstValue::U128(*value), }, TyKind::Bool => ConstValue::Bool(*value != 0), + TyKind::Ptr(ref _inner, _mutable) => { + ConstValue::I64((*value).try_into().expect("value out of range")) + } x => unreachable!("{:?}", x), })), }, diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index 3b40d96..7990ba8 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -53,6 +53,7 @@ extern { ":" => Token::Colon, "->" => Token::Arrow, "," => Token::Coma, + "#" => Token::Hashtag, "<" => Token::LessThanSign, ">" => Token::MoreThanSign, ">=" => Token::MoreThanEqSign, @@ -119,6 +120,14 @@ PlusSeparated: Vec = { } }; +List: Vec = { + => vec![<>], + > => { + s.push(n); + s + }, +} + // Requires the semicolon at end SemiColonSeparated: Vec = { ";" => vec![<>], @@ -291,13 +300,22 @@ pub(crate) Param: ast::functions::Param = { } } +pub(crate) Attribute: ast::common::Attribute = { + "#" "[" )?> "]" => ast::common::Attribute { + name, + value, + span: ast::common::Span::new(lo, hi), + } +} + pub(crate) FunctionDecl: ast::functions::FunctionDecl = { - + ?> "fn" "(" > ")" => ast::functions::FunctionDecl { doc_string: None, generic_params: generic_params.unwrap_or(vec![]), + attributes: attributes.unwrap_or(vec![]), name, params, ret_type, diff --git a/crates/concrete_parser/src/lib.rs b/crates/concrete_parser/src/lib.rs index 3108b09..5acfc43 100644 --- a/crates/concrete_parser/src/lib.rs +++ b/crates/concrete_parser/src/lib.rs @@ -285,6 +285,17 @@ mod ModuleName { return arr[1][0]; } +}"##; + let lexer = Lexer::new(source); + let parser = grammar::ProgramParser::new(); + parser.parse(lexer).unwrap(); + } + + #[test] + fn parse_intrinsic() { + let source = r##"mod MyMod { + #[intrinsic = "simdsomething"] + pub extern fn myintrinsic(); }"##; let lexer = Lexer::new(source); let parser = grammar::ProgramParser::new(); diff --git a/crates/concrete_parser/src/tokens.rs b/crates/concrete_parser/src/tokens.rs index 4b1326c..c9a884a 100644 --- a/crates/concrete_parser/src/tokens.rs +++ b/crates/concrete_parser/src/tokens.rs @@ -109,6 +109,8 @@ pub enum Token { Coma, #[token(".")] Dot, + #[token("#")] + Hashtag, #[token("<")] LessThanSign, #[token(">")] diff --git a/examples/hello_world_hacky.con b/examples/hello_world_hacky.con new file mode 100644 index 0000000..ed27057 --- /dev/null +++ b/examples/hello_world_hacky.con @@ -0,0 +1,36 @@ +mod HelloWorld { + pub extern fn malloc(size: u64) -> *mut u8; + pub extern fn puts(data: *mut u8) -> i32; + + fn main() -> i32 { + let origin: *mut u8 = malloc(12); + let mut p: *mut u8 = origin; + + *p = 'H'; + p = p + 1; + *p = 'e'; + p = p + 1; + *p = 'l'; + p = p + 1; + *p = 'l'; + p = p + 1; + *p = 'o'; + p = p + 1; + *p = ' '; + p = p + 1; + *p = 'W'; + p = p + 1; + *p = 'o'; + p = p + 1; + *p = 'r'; + p = p + 1; + *p = 'l'; + p = p + 1; + *p = 'd'; + p = p + 1; + *p = '\0'; + puts(origin); + + return 0; + } +} From 7d0e5a590caf209230695bf1c0d5fb5d51b8b1a2 Mon Sep 17 00:00:00 2001 From: Edgar Date: Thu, 9 May 2024 11:45:25 +0200 Subject: [PATCH 3/4] macos ci --- .github/workflows/ci.yml | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5468d33..57f8985 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -73,7 +73,26 @@ jobs: run: sudo apt-get install libc-dev build-essential - name: test run: make test - + test-macos: + name: test (macOS) + runs-on: macos-14 + env: + CARGO_TERM_COLOR: always + CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse + LIBRARY_PATH: /opt/homebrew/lib + MLIR_SYS_170_PREFIX: /opt/homebrew/opt/llvm@18 + LLVM_SYS_170_PREFIX: /opt/homebrew/opt/llvm@18 + TABLEGEN_170_PREFIX: /opt/homebrew/opt/llvm@18 + RUST_LOG: debug + steps: + - uses: actions/checkout@v4 + - name: Rustup toolchain install + uses: dtolnay/rust-toolchain@1.78.0 + - uses: homebrew/actions/setup-homebrew@master + - name: install llvm + run: brew install llvm@18 + - name: Run tests + run: make test coverage: name: coverage runs-on: ubuntu-latest From a984efc06b92bbc05aa722218d8e1f06b6eb138d Mon Sep 17 00:00:00 2001 From: Edgar Date: Thu, 9 May 2024 11:49:00 +0200 Subject: [PATCH 4/4] fix env --- .github/workflows/ci.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 57f8985..839997c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -78,11 +78,10 @@ jobs: runs-on: macos-14 env: CARGO_TERM_COLOR: always - CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse LIBRARY_PATH: /opt/homebrew/lib - MLIR_SYS_170_PREFIX: /opt/homebrew/opt/llvm@18 - LLVM_SYS_170_PREFIX: /opt/homebrew/opt/llvm@18 - TABLEGEN_170_PREFIX: /opt/homebrew/opt/llvm@18 + MLIR_SYS_180_PREFIX: /opt/homebrew/opt/llvm@18 + LLVM_SYS_180_PREFIX: /opt/homebrew/opt/llvm@18 + TABLEGEN_180_PREFIX: /opt/homebrew/opt/llvm@18 RUST_LOG: debug steps: - uses: actions/checkout@v4