From 383d9185bfcfa0ece9e19cdd9a8df6c53826056d Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Sat, 20 Apr 2024 20:47:34 -0400 Subject: [PATCH] Clean up expression sizes, which seems to fix stack blowouts. --- src/backend/eval.rs | 19 ++++++++----------- src/backend/into_crane.rs | 2 +- src/ir/ast.rs | 19 +++++++++++++++++++ src/ir/eval.rs | 2 -- src/ir/fields.rs | 15 +++++---------- src/syntax.rs | 2 -- src/syntax/validate.rs | 1 - src/type_infer/convert.rs | 2 +- 8 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/backend/eval.rs b/src/backend/eval.rs index 227a48a..4aabe7e 100644 --- a/src/backend/eval.rs +++ b/src/backend/eval.rs @@ -120,20 +120,18 @@ proptest::proptest! { #[test] fn static_backend(program in Program::arbitrary()) { use crate::eval::PrimOpError; - use pretty::DocAllocator; - let allocator = pretty::Arena::new(); - let result = allocator.text("-------------") - .append(allocator.line()) - .append(program.pretty(&allocator)) - .append(allocator.line()); - result.render_raw(70, &mut pretty::IoWrite::new(std::io::stdout())) - .expect("rendering works"); + // use pretty::DocAllocator; + // let allocator = pretty::Arena::new(); + // let result = allocator.text("-------------") + // .append(allocator.line()) + // .append(program.pretty(&allocator)) + // .append(allocator.line()); + // result.render_raw(70, &mut pretty::IoWrite::new(std::io::stdout())) + // .expect("rendering works"); - eprintln!("BEFORE EVAL"); let ir_evaluator = crate::ir::Evaluator::default(); let basic_result = ir_evaluator.eval(program.clone()).map(|(_,o)| o); - eprintln!("AFTER EVAL"); // windows `printf` is going to terminate lines with "\r\n", so we need to adjust // our test result here. @@ -150,7 +148,6 @@ proptest::proptest! { //result.render_raw(70, &mut pretty::IoWrite::new(std::io::stdout())) // .expect("rendering works"); - eprintln!("BEFORE OM EVAL"); let compiled_result = Backend::::eval(program); proptest::prop_assert_eq!(basic_result, compiled_result); } diff --git a/src/backend/into_crane.rs b/src/backend/into_crane.rs index 68f98f7..68a7774 100644 --- a/src/backend/into_crane.rs +++ b/src/backend/into_crane.rs @@ -100,7 +100,7 @@ impl Backend { false, )?; tracing::info!(name = %top_level_name, "defining top-level data structure"); - self.module.define_data(data_id, fields.blank_data())?; + self.module.define_data(data_id, &fields.blank_data())?; let pointer = self.module.target_config().pointer_type(); self.defined_symbols .insert(top_level_name, (data_id, pointer)); diff --git a/src/ir/ast.rs b/src/ir/ast.rs index 9699fa3..24d7c41 100644 --- a/src/ir/ast.rs +++ b/src/ir/ast.rs @@ -482,3 +482,22 @@ impl TryFrom for Type { } } } + +#[test] +fn struct_sizes_are_rational() { + assert_eq!(8, std::mem::size_of::>()); + assert_eq!(24, std::mem::size_of::()); + assert_eq!(1, std::mem::size_of::()); + assert_eq!(1, std::mem::size_of::()); + assert_eq!(32, std::mem::size_of::>()); + assert_eq!(40, std::mem::size_of::()); + assert_eq!(40, std::mem::size_of::()); + assert_eq!(80, std::mem::size_of::>()); + assert_eq!(80, std::mem::size_of::>()); + assert_eq!(200, std::mem::size_of::>()); + assert_eq!(200, std::mem::size_of::>()); + assert_eq!(272, std::mem::size_of::>()); + assert_eq!(272, std::mem::size_of::>()); + assert_eq!(72, std::mem::size_of::>()); + assert_eq!(72, std::mem::size_of::>()); +} \ No newline at end of file diff --git a/src/ir/eval.rs b/src/ir/eval.rs index aa88e21..9a42560 100644 --- a/src/ir/eval.rs +++ b/src/ir/eval.rs @@ -64,7 +64,6 @@ where } fn eval_expr(&mut self, expr: Expression) -> Result, IREvalError> { - println!("evaluating {}", expr); match expr { Expression::Atomic(x) => self.eval_atomic(x), @@ -168,7 +167,6 @@ where .into_iter() .map(|x| self.eval_atomic(x)) .collect::>()?; - println!("primitive {}: args {:?}", name, values); Value::calculate(name.as_str(), values).map_err(Into::into) } diff --git a/src/ir/fields.rs b/src/ir/fields.rs index f59ae43..239f1cf 100644 --- a/src/ir/fields.rs +++ b/src/ir/fields.rs @@ -6,7 +6,6 @@ use std::fmt; pub struct Fields { ordering: FieldOrdering, total_size: usize, - cranelift_description: Option, fields: Vec<(ArcIntern, T)>, } @@ -41,7 +40,6 @@ impl Fields { Fields { ordering, total_size: 0, - cranelift_description: None, fields: vec![], } } @@ -69,7 +67,6 @@ impl Fields { Fields { ordering: self.ordering, total_size: self.total_size, - cranelift_description: self.cranelift_description, fields: self.fields.into_iter().map(|(n, t)| (n, f(t))).collect(), } } @@ -111,13 +108,11 @@ impl Fields { self.fields.iter_mut().map(|(_, x)| x) } - pub fn blank_data(&mut self) -> &DataDescription { - self.cranelift_description.get_or_insert_with(|| { - let mut cranelift_description = DataDescription::new(); - cranelift_description.set_align(8); - cranelift_description.define_zeroinit(self.total_size); - cranelift_description - }) + pub fn blank_data(&mut self) -> DataDescription { + let mut cranelift_description = DataDescription::new(); + cranelift_description.set_align(8); + cranelift_description.define_zeroinit(self.total_size); + cranelift_description } pub fn field_type_and_offset(&self, field: &ArcIntern) -> Option<(&T, i32)> { diff --git a/src/syntax.rs b/src/syntax.rs index 6dd75d2..309f7ec 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -331,8 +331,6 @@ proptest::proptest! { #[test] fn generated_run_or_overflow(program in Program::arbitrary_with(GenerationEnvironment::new(false))) { use crate::eval::{EvalError, PrimOpError}; - println!("-----------\nprogram:\n{}\n", program); - println!("-----------\nresult:\n{:?}\n", program.eval()); prop_assert!(matches!(program.eval(), Ok(_) | Err(EvalError::PrimOp(PrimOpError::MathFailure(_))))); } } diff --git a/src/syntax/validate.rs b/src/syntax/validate.rs index 8ee2792..c95b2bc 100644 --- a/src/syntax/validate.rs +++ b/src/syntax/validate.rs @@ -68,7 +68,6 @@ impl Program { /// actually a problem. pub fn validate(&self) -> (Vec, Vec) { let mut bound_variables = ScopedMap::new(); - println!("validate:\n{}", self); self.validate_with_bindings(&mut bound_variables) } diff --git a/src/type_infer/convert.rs b/src/type_infer/convert.rs index 02cb653..a8fe5c0 100644 --- a/src/type_infer/convert.rs +++ b/src/type_infer/convert.rs @@ -416,7 +416,7 @@ fn convert_expression( let mut ret_type = ir::TypeOrVar::Primitive(PrimitiveType::Void); let mut exprs = vec![]; - for xpr in stmts { + for xpr in stmts.into_iter() { let (expr, expr_type) = convert_expression(xpr, constraint_db, renames, bindings); ret_type = expr_type;