From e031c8702a426e7dd0a92c3d3178e4bd8e1201c1 Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Sat, 27 Apr 2024 13:28:20 -0700 Subject: [PATCH] [ast][ir] Introduce i31 type (#1195) --- crates/samlang-ast/src/lir.rs | 9 +- crates/samlang-ast/src/lir_tests.rs | 79 ++-- crates/samlang-ast/src/mir.rs | 6 +- crates/samlang-ast/src/mir_tests.rs | 127 +++--- crates/samlang-compiler/src/lir_lowering.rs | 155 +++---- .../src/lir_unused_name_elimination.rs | 50 +-- .../src/mir_constant_param_elimination.rs | 100 +++-- .../src/mir_generics_specialization.rs | 24 +- .../src/mir_tail_recursion_rewrite.rs | 134 +++--- .../src/mir_type_deduplication.rs | 46 +- crates/samlang-compiler/src/wasm_lowering.rs | 40 +- .../src/common_subexpression_elimination.rs | 29 +- .../src/conditional_constant_propagation.rs | 14 +- .../conditional_constant_propagation_tests.rs | 412 +++++++++--------- .../src/dead_code_elimination_tests.rs | 205 ++++----- crates/samlang-optimization/src/inlining.rs | 26 +- .../src/inlining_tests.rs | 246 ++++++----- crates/samlang-optimization/src/lib.rs | 4 +- .../src/local_value_numbering_tests.rs | 141 +++--- .../src/loop_algebraic_optimization.rs | 30 +- .../src/loop_induction_analysis.rs | 224 +++++----- .../loop_induction_variable_elimination.rs | 72 +-- .../src/loop_invariant_code_motion.rs | 94 ++-- .../src/loop_optimizations.rs | 192 ++++---- .../src/loop_strength_reduction.rs | 18 +- .../src/optimization_common.rs | 2 +- .../src/unused_name_elimination.rs | 60 +-- 27 files changed, 1324 insertions(+), 1215 deletions(-) diff --git a/crates/samlang-ast/src/lir.rs b/crates/samlang-ast/src/lir.rs index f801642f..3b4ce1b2 100644 --- a/crates/samlang-ast/src/lir.rs +++ b/crates/samlang-ast/src/lir.rs @@ -9,14 +9,16 @@ use samlang_heap::{Heap, PStr}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum PrimitiveType { - Int, + Int32, + Int31, Any, } impl PrimitiveType { fn as_str(&self) -> &'static str { match self { - PrimitiveType::Int => "number", + PrimitiveType::Int32 => "number", + PrimitiveType::Int31 => "i31", PrimitiveType::Any => "any", } } @@ -89,7 +91,8 @@ impl Type { } } -pub const INT_TYPE: Type = Type::Primitive(PrimitiveType::Int); +pub const INT_32_TYPE: Type = Type::Primitive(PrimitiveType::Int32); +pub const INT_31_TYPE: Type = Type::Primitive(PrimitiveType::Int31); pub const ANY_TYPE: Type = Type::Primitive(PrimitiveType::Any); #[derive(Debug, Clone, EnumAsInner)] diff --git a/crates/samlang-ast/src/lir_tests.rs b/crates/samlang-ast/src/lir_tests.rs index 07609847..cf73ffa2 100644 --- a/crates/samlang-ast/src/lir_tests.rs +++ b/crates/samlang-ast/src/lir_tests.rs @@ -10,8 +10,9 @@ mod tests { #[test] fn boilterplate() { - assert!(PrimitiveType::Int.eq(&PrimitiveType::Int)); - assert!(INT_TYPE.as_fn().is_none()); + assert!(PrimitiveType::Int32.eq(&PrimitiveType::Int32)); + assert!(PrimitiveType::Int31.eq(&PrimitiveType::Int31)); + assert!(INT_32_TYPE.as_fn().is_none()); assert!(ZERO.as_fn_name().is_none()); let table = &mut SymbolTable::new(); @@ -21,7 +22,7 @@ mod tests { .clone() ); format!("{:?}", Expression::StringName(PStr::LOWER_A).clone()); - format!("{:?}", Type::new_fn(vec![(INT_TYPE)], INT_TYPE).clone()); + format!("{:?}", Type::new_fn(vec![INT_32_TYPE, INT_31_TYPE], INT_32_TYPE).clone()); } #[test] @@ -35,12 +36,12 @@ mod tests { ); assert_eq!( false, - Type::Id(table.create_type_name_for_test(PStr::LOWER_A)).is_the_same_type(&INT_TYPE) + Type::Id(table.create_type_name_for_test(PStr::LOWER_A)).is_the_same_type(&INT_32_TYPE) ); assert_eq!( true, - Type::new_fn(vec![(INT_TYPE)], INT_TYPE) - .is_the_same_type(&Type::new_fn(vec![(INT_TYPE)], INT_TYPE)) + Type::new_fn(vec![(INT_32_TYPE)], INT_32_TYPE) + .is_the_same_type(&Type::new_fn(vec![(INT_32_TYPE)], INT_32_TYPE)) ); } @@ -63,7 +64,7 @@ mod tests { type_definitions: vec![ TypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("Foo")), - mappings: vec![INT_TYPE, INT_TYPE], + mappings: vec![INT_32_TYPE, INT_31_TYPE], }, TypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("Foo")), @@ -75,17 +76,17 @@ mod tests { Function { name: FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![], return_value: ZERO, }, Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("Bar")), parameters: vec![PStr::LOWER_F, PStr::LOWER_G], - type_: Type::new_fn_unwrapped(vec![INT_TYPE, INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE, INT_32_TYPE], INT_32_TYPE), body: vec![Statement::IndexedAccess { name: PStr::LOWER_F, - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: Expression::Variable( heap.alloc_str_for_test("big"), Type::Id(table.create_type_name_for_test(heap.alloc_str_for_test("FooBar"))), @@ -99,10 +100,10 @@ mod tests { parameters: vec![PStr::LOWER_F, PStr::LOWER_G], type_: Type::new_fn_unwrapped( vec![ - Type::Fn(Type::new_fn_unwrapped(vec![INT_TYPE, INT_TYPE], INT_TYPE)), - Type::Fn(Type::new_fn_unwrapped(vec![], INT_TYPE)), + Type::Fn(Type::new_fn_unwrapped(vec![INT_32_TYPE, INT_32_TYPE], INT_32_TYPE)), + Type::Fn(Type::new_fn_unwrapped(vec![], INT_32_TYPE)), ], - INT_TYPE, + INT_32_TYPE, ), body: vec![], return_value: ZERO, @@ -111,8 +112,8 @@ mod tests { name: FunctionName::new_for_test(PStr::LOWER_F), parameters: vec![heap.alloc_str_for_test("v1")], type_: Type::new_fn_unwrapped( - vec![Type::Fn(Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE))], - INT_TYPE, + vec![Type::Fn(Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE))], + INT_32_TYPE, ), body: vec![Statement::IfElse { condition: ZERO, @@ -146,7 +147,7 @@ mod tests { Statement::While { loop_variables: vec![GenenalLoopVariable { name: PStr::UNDERSCORE, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO, }], @@ -155,7 +156,7 @@ mod tests { invert_condition: true, statements: vec![Statement::Break(ZERO)], }], - break_collector: Some((PStr::UNDERSCORE, INT_TYPE)), + break_collector: Some((PStr::UNDERSCORE, INT_32_TYPE)), }, ], s2: vec![ @@ -183,45 +184,45 @@ mod tests { Statement::Call { callee: Expression::FnName( FunctionName::new_for_test(heap.alloc_str_for_test("h")), - Type::new_fn(vec![], INT_TYPE), + Type::new_fn(vec![], INT_32_TYPE), ), arguments: vec![Expression::Variable( heap.alloc_str_for_test("big"), Type::Id(table.create_type_name_for_test(heap.alloc_str_for_test("FooBar"))), )], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("vibez")), }, Statement::Call { callee: Expression::FnName( FunctionName::new_for_test(heap.alloc_str_for_test("stresso")), - Type::new_fn(vec![], INT_TYPE), + Type::new_fn(vec![], INT_32_TYPE), ), - arguments: vec![Expression::Variable(PStr::LOWER_D, INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::Variable(PStr::LOWER_D, INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { - callee: Expression::Variable(PStr::LOWER_D, INT_TYPE), - arguments: vec![Expression::Variable(PStr::LOWER_D, INT_TYPE)], - return_type: INT_TYPE, + callee: Expression::Variable(PStr::LOWER_D, INT_32_TYPE), + arguments: vec![Expression::Variable(PStr::LOWER_D, INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { - callee: Expression::Variable(PStr::LOWER_D, INT_TYPE), - arguments: vec![Expression::Variable(PStr::LOWER_D, INT_TYPE), ZERO], - return_type: INT_TYPE, + callee: Expression::Variable(PStr::LOWER_D, INT_32_TYPE), + arguments: vec![Expression::Variable(PStr::LOWER_D, INT_32_TYPE), ZERO], + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { - callee: Expression::Variable(PStr::LOWER_D, INT_TYPE), + callee: Expression::Variable(PStr::LOWER_D, INT_32_TYPE), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::IndexedAccess { name: PStr::LOWER_F, - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: Expression::Variable( heap.alloc_str_for_test("big"), Type::Id(table.create_type_name_for_test(heap.alloc_str_for_test("FooBar"))), @@ -233,10 +234,14 @@ mod tests { pointer_expression: ZERO, index: 0, }, - Statement::Cast { name: PStr::LOWER_C, type_: INT_TYPE, assigned_expression: ZERO }, + Statement::Cast { + name: PStr::LOWER_C, + type_: INT_32_TYPE, + assigned_expression: ZERO, + }, Statement::LateInitDeclaration { name: heap.alloc_str_for_test("c"), - type_: INT_TYPE, + type_: INT_32_TYPE, }, Statement::LateInitAssignment { name: heap.alloc_str_for_test("c"), @@ -246,9 +251,9 @@ mod tests { ], final_assignments: vec![( heap.alloc_str_for_test("bar"), - INT_TYPE, - Expression::Variable(heap.alloc_str_for_test("b1"), INT_TYPE), - Expression::Variable(heap.alloc_str_for_test("b2"), INT_TYPE), + INT_32_TYPE, + Expression::Variable(heap.alloc_str_for_test("b1"), INT_32_TYPE), + Expression::Variable(heap.alloc_str_for_test("b2"), INT_32_TYPE), )], }], return_value: ZERO, @@ -259,7 +264,7 @@ mod tests { let expected = format!( r#"{}const dev_meggo: _Str = [0, `vibez` as unknown as number]; const esc: _Str = [0, `f"\"` as unknown as number]; -type _Foo = [number, number]; +type _Foo = [number, i31]; type _Foo = []; function __$main(): number {{ return 0; diff --git a/crates/samlang-ast/src/mir.rs b/crates/samlang-ast/src/mir.rs index 86eae034..03c0bd71 100644 --- a/crates/samlang-ast/src/mir.rs +++ b/crates/samlang-ast/src/mir.rs @@ -61,6 +61,7 @@ impl TypeName { collector.push('_'); match t { Type::Int32 => collector.push_str("int"), + Type::Int31 => collector.push_str("i31"), Type::Id(id) => id.write_encoded(collector, heap, table), } } @@ -194,6 +195,7 @@ impl SymbolTable { #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, EnumAsInner)] pub enum Type { Int32, + Int31, Id(TypeNameId), } @@ -205,12 +207,14 @@ impl Type { pub fn pretty_print(&self, heap: &Heap, table: &SymbolTable) -> String { match self { Type::Int32 => "int".to_string(), + Type::Int31 => "i31".to_string(), Type::Id(id) => id.encoded_for_test(heap, table), } } } -pub const INT_TYPE: Type = Type::Int32; +pub const INT_32_TYPE: Type = Type::Int32; +pub const INT_31_TYPE: Type = Type::Int31; #[derive(Debug, Clone)] pub struct ClosureTypeDefinition { diff --git a/crates/samlang-ast/src/mir_tests.rs b/crates/samlang-ast/src/mir_tests.rs index e60318a6..76ebb6a3 100644 --- a/crates/samlang-ast/src/mir_tests.rs +++ b/crates/samlang-ast/src/mir_tests.rs @@ -11,13 +11,17 @@ mod tests { let heap = &mut Heap::new(); let table = &mut SymbolTable::new(); - assert!(INT_TYPE <= INT_TYPE); - format!("{:?}", INT_TYPE.cmp(&INT_TYPE)); + assert!(INT_32_TYPE <= INT_32_TYPE); + format!("{:?}", INT_32_TYPE.cmp(&INT_32_TYPE)); + assert!(INT_31_TYPE <= INT_31_TYPE); + format!("{:?}", INT_31_TYPE.cmp(&INT_31_TYPE)); assert!(ZERO.as_int_literal().is_some()); Type::Id(table.create_type_name_for_test(PStr::UPPER_A)).as_id(); Type::Id(table.create_type_name_for_test(PStr::UPPER_A)).is_int32(); - assert!(INT_TYPE.is_int32()); - assert!(INT_TYPE.as_id().is_none()); + assert!(INT_32_TYPE.is_int32()); + assert!(INT_32_TYPE.as_id().is_none()); + assert!(INT_31_TYPE.is_int31()); + assert!(INT_31_TYPE.as_id().is_none()); format!( "{:?}", Expression::var_name(PStr::LOWER_A, Type::Id(table.create_type_name_for_test(PStr::UPPER_A))) @@ -27,20 +31,24 @@ mod tests { Expression::var_name(PStr::LOWER_A, Type::Id(table.create_type_name_for_test(PStr::UPPER_A))) ); format!("{:?}", Expression::StringName(PStr::LOWER_A)); - format!("{:?}", INT_TYPE); + format!("{:?}", INT_32_TYPE); assert_eq!( "(s: int)", - VariableName::new(heap.alloc_str_for_test("s"), INT_TYPE).debug_print(heap, table) + VariableName::new(heap.alloc_str_for_test("s"), INT_32_TYPE).debug_print(heap, table) + ); + assert_eq!( + "(s: i31)", + VariableName::new(heap.alloc_str_for_test("s"), INT_31_TYPE).debug_print(heap, table) ); GenenalLoopVariable { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO, } .pretty_print(heap, table); - format!("{:?}", Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE)); - Expression::var_name(PStr::LOWER_A, INT_TYPE).convert_to_callee(); + format!("{:?}", Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE)); + Expression::var_name(PStr::LOWER_A, INT_32_TYPE).convert_to_callee(); Expression::StringName(PStr::LOWER_A).convert_to_callee(); ZERO.convert_to_callee(); Statement::Break(ZERO).as_binary(); @@ -51,10 +59,10 @@ mod tests { let call = Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_A), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }; format!("{:?}", call); @@ -69,19 +77,19 @@ mod tests { ) ); assert!( - FunctionType { argument_types: vec![], return_type: Box::new(INT_TYPE) } - == FunctionType { argument_types: vec![], return_type: Box::new(INT_TYPE) } + FunctionType { argument_types: vec![], return_type: Box::new(INT_32_TYPE) } + == FunctionType { argument_types: vec![], return_type: Box::new(INT_32_TYPE) } ); let mut hasher = DefaultHasher::new(); ZERO.hash(&mut hasher); Expression::StringName(PStr::LOWER_A).hash(&mut hasher); - Expression::var_name(PStr::LOWER_A, INT_TYPE).hash(&mut hasher); + Expression::var_name(PStr::LOWER_A, INT_32_TYPE).hash(&mut hasher); Expression::var_name(PStr::LOWER_A, Type::Id(table.create_type_name_for_test(PStr::UPPER_A))) .hash(&mut hasher); Statement::binary_flexible_unwrapped(PStr::LOWER_A, BinaryOperator::DIV, ZERO, ZERO); Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("s")), - type_: FunctionType { argument_types: vec![], return_type: Box::new(INT_TYPE) }, + type_: FunctionType { argument_types: vec![], return_type: Box::new(INT_32_TYPE) }, }) .as_function_name(); assert!(TypeDefinitionMappings::Struct(vec![]).as_struct().is_some()); @@ -90,6 +98,12 @@ mod tests { let mut type_name_id = table.create_type_name_for_test(PStr::UPPER_A); format!("{:?}", type_name_id); assert_eq!(false, type_name_id.encoded_for_test(heap, &table).is_empty()); + type_name_id = table.create_type_name_with_suffix( + ModuleReference::ROOT, + PStr::UPPER_A, + vec![INT_31_TYPE, INT_32_TYPE], + ); + assert_eq!(false, type_name_id.encoded_for_test(heap, &table).is_empty()); type_name_id = table.create_simple_type_name(ModuleReference::ROOT, PStr::UPPER_A); type_name_id = table.derived_type_name_with_subtype_tag(type_name_id, 1); assert_eq!(true, type_name_id <= type_name_id); @@ -101,9 +115,13 @@ mod tests { let heap = &mut Heap::new(); let table = &mut SymbolTable::new(); - assert_eq!("int", INT_TYPE.pretty_print(heap, table)); + assert_eq!("int", INT_32_TYPE.pretty_print(heap, table)); + assert_eq!("i31", INT_31_TYPE.pretty_print(heap, table)); assert_eq!("0", ZERO.clone().debug_print(heap, table)); - assert_eq!("(a: int)", Expression::var_name(PStr::LOWER_A, INT_TYPE).debug_print(heap, table)); + assert_eq!( + "(a: int)", + Expression::var_name(PStr::LOWER_A, INT_32_TYPE).debug_print(heap, table) + ); assert_eq!( "(a: _A)", Expression::var_name(PStr::LOWER_A, Type::Id(table.create_type_name_for_test(PStr::UPPER_A))) @@ -119,10 +137,10 @@ mod tests { let d1 = TypeDefinition { name: table.create_type_name_for_test(PStr::UPPER_A), - mappings: TypeDefinitionMappings::Struct(vec![INT_TYPE, INT_TYPE]), + mappings: TypeDefinitionMappings::Struct(vec![INT_32_TYPE, INT_32_TYPE]), }; let ed1 = EnumTypeDefinition::Unboxed(Type::Id(table.create_type_name_for_test(PStr::UPPER_D))); - let ed2 = EnumTypeDefinition::Boxed(vec![INT_TYPE, INT_TYPE]); + let ed2 = EnumTypeDefinition::Boxed(vec![INT_32_TYPE, INT_32_TYPE]); let ed3 = EnumTypeDefinition::Int; assert!(ed1.eq(&ed1)); assert!(ed2.eq(&ed2)); @@ -157,7 +175,7 @@ mod tests { closure_type_name: table.create_type_name_for_test(heap.alloc_str_for_test("CCC")), function_name: FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("foo")), - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), }, context: ZERO, }, @@ -174,10 +192,13 @@ mod tests { Statement::binary(heap.alloc_str_for_test("dd"), BinaryOperator::XOR.clone(), ZERO, ZERO), Statement::Cast { name: heap.alloc_str_for_test("cast"), - type_: INT_TYPE, + type_: INT_32_TYPE, assigned_expression: ZERO, }, - Statement::LateInitDeclaration { name: heap.alloc_str_for_test("cast"), type_: INT_TYPE }, + Statement::LateInitDeclaration { + name: heap.alloc_str_for_test("cast"), + type_: INT_32_TYPE, + }, Statement::LateInitAssignment { name: heap.alloc_str_for_test("cast"), assigned_expression: ZERO, @@ -194,7 +215,7 @@ mod tests { Statement::While { loop_variables: vec![GenenalLoopVariable { name: PStr::UNDERSCORE, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO, }], @@ -203,7 +224,7 @@ mod tests { invert_condition: true, statements: vec![Statement::Break(ZERO)], }], - break_collector: Some(VariableName { name: PStr::UNDERSCORE, type_: INT_TYPE }), + break_collector: Some(VariableName { name: PStr::UNDERSCORE, type_: INT_32_TYPE }), }, ], s2: vec![ @@ -231,33 +252,33 @@ mod tests { Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("h")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![Expression::var_name( heap.alloc_str_for_test("big"), Type::Id(table.create_type_name_for_test(heap.alloc_str_for_test("FooBar"))), )], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("vibez")), }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("stresso")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), - arguments: vec![Expression::var_name(PStr::LOWER_D, INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(PStr::LOWER_D, INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { - callee: Callee::Variable(VariableName { name: PStr::LOWER_D, type_: INT_TYPE }), - arguments: vec![Expression::var_name(PStr::LOWER_D, INT_TYPE)], - return_type: INT_TYPE, + callee: Callee::Variable(VariableName { name: PStr::LOWER_D, type_: INT_32_TYPE }), + arguments: vec![Expression::var_name(PStr::LOWER_D, INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }, Statement::IndexedAccess { name: PStr::LOWER_F, - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: Expression::var_name( heap.alloc_str_for_test("big"), Type::Id(table.create_type_name_for_test(heap.alloc_str_for_test("FooBar"))), @@ -268,9 +289,9 @@ mod tests { ], final_assignments: vec![( heap.alloc_str_for_test("bar"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("b1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b2"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("b1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b2"), INT_32_TYPE), )], }; format!("{:?}", stmt.clone()); @@ -339,22 +360,22 @@ if 0 { .clone()], closure_types: vec![ClosureTypeDefinition { name: table.create_type_name_for_test(PStr::UPPER_A), - function_type: Type::new_fn_unwrapped(vec![], INT_TYPE), + function_type: Type::new_fn_unwrapped(vec![], INT_32_TYPE), } .clone()], type_definitions: vec![TypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("Foo")), - mappings: TypeDefinitionMappings::Struct(vec![INT_TYPE, INT_TYPE]), + mappings: TypeDefinitionMappings::Struct(vec![INT_32_TYPE, INT_32_TYPE]), } .clone()], main_function_names: vec![FunctionName::new_for_test(heap.alloc_str_for_test("ddd"))], functions: vec![Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("Bar")), parameters: vec![PStr::LOWER_F], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![Statement::IndexedAccess { name: PStr::LOWER_F, - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: Expression::var_name( heap.alloc_str_for_test("big"), Type::Id(table.create_type_name_for_test(heap.alloc_str_for_test("FooBar"))), @@ -388,7 +409,7 @@ sources.mains = [__$ddd]"#; functions: vec![Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("Bar")), parameters: vec![PStr::LOWER_F], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![], return_value: ZERO, }], @@ -424,11 +445,11 @@ sources.mains = [__$ddd]"#; ), ); assert_eq!( - (BinaryOperator::PLUS, Expression::var_name(PStr::LOWER_A, INT_TYPE), ZERO), + (BinaryOperator::PLUS, Expression::var_name(PStr::LOWER_A, INT_32_TYPE), ZERO), Statement::flexible_order_binary( BinaryOperator::PLUS, ZERO, - Expression::var_name(PStr::LOWER_A, INT_TYPE) + Expression::var_name(PStr::LOWER_A, INT_32_TYPE) ), ); @@ -453,46 +474,46 @@ sources.mains = [__$ddd]"#; assert_eq!( ( BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_A, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), Expression::StringName(PStr::LOWER_A), ), Statement::flexible_order_binary( BinaryOperator::PLUS, Expression::StringName(PStr::LOWER_A), - Expression::var_name(PStr::LOWER_A, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), ), ); assert_eq!( - (BinaryOperator::PLUS, Expression::var_name(PStr::LOWER_A, INT_TYPE), ZERO), + (BinaryOperator::PLUS, Expression::var_name(PStr::LOWER_A, INT_32_TYPE), ZERO), Statement::flexible_order_binary( BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_A, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), ZERO ), ); assert_eq!( ( BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_A, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), Expression::StringName(PStr::LOWER_B), ), Statement::flexible_order_binary( BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_A, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), Expression::StringName(PStr::LOWER_B), ), ); assert_eq!( ( BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_B, INT_TYPE), - Expression::var_name(PStr::LOWER_A, INT_TYPE), + Expression::var_name(PStr::LOWER_B, INT_32_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), ), Statement::flexible_order_binary( BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_A, INT_TYPE), - Expression::var_name(PStr::LOWER_B, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), + Expression::var_name(PStr::LOWER_B, INT_32_TYPE), ), ); diff --git a/crates/samlang-compiler/src/lir_lowering.rs b/crates/samlang-compiler/src/lir_lowering.rs index 257a65c2..118b5fd6 100644 --- a/crates/samlang-compiler/src/lir_lowering.rs +++ b/crates/samlang-compiler/src/lir_lowering.rs @@ -6,7 +6,8 @@ use std::collections::{BTreeMap, HashSet}; fn lower_type(type_: mir::Type) -> lir::Type { match type_ { - mir::Type::Int32 => lir::Type::Primitive(lir::PrimitiveType::Int), + mir::Type::Int32 => lir::Type::Primitive(lir::PrimitiveType::Int32), + mir::Type::Int31 => lir::Type::Primitive(lir::PrimitiveType::Int31), mir::Type::Id(name) => lir::Type::Id(name), } } @@ -21,7 +22,7 @@ fn lower_fn_type( } fn unknown_member_destructor_type() -> lir::FunctionType { - lir::Type::new_fn_unwrapped(vec![lir::ANY_TYPE], lir::INT_TYPE) + lir::Type::new_fn_unwrapped(vec![lir::ANY_TYPE], lir::INT_32_TYPE) } fn lower_expression(expr: mir::Expression) -> lir::Expression { @@ -117,7 +118,7 @@ impl<'a> LoweringManager<'a> { lir::Type::Fn(unknown_member_destructor_type()), ), arguments: vec![lir::Expression::Variable(variable_name, var_type)], - return_type: lir::INT_TYPE, + return_type: lir::INT_32_TYPE, return_collector: None, }); } @@ -366,7 +367,7 @@ impl<'a> LoweringManager<'a> { lir::Type::Fn(unknown_member_destructor_type()), ), arguments: vec![lir::Expression::Variable(casted, lir::ANY_TYPE)], - return_type: lir::INT_TYPE, + return_type: lir::INT_32_TYPE, return_collector: None, }); true @@ -406,61 +407,61 @@ fn generate_inc_ref_fn() -> lir::Function { lir::Statement::binary( not_ptr, hir::BinaryOperator::LOR, - lir::Expression::Variable(tiny_int, lir::INT_TYPE), - lir::Expression::Variable(is_odd, lir::INT_TYPE), + lir::Expression::Variable(tiny_int, lir::INT_32_TYPE), + lir::Expression::Variable(is_odd, lir::INT_32_TYPE), ), lir::Statement::SingleIf { - condition: lir::Expression::Variable(not_ptr, lir::INT_TYPE), + condition: lir::Expression::Variable(not_ptr, lir::INT_32_TYPE), invert_condition: true, statements: vec![ lir::Statement::IndexedAccess { name: header, - type_: lir::INT_TYPE, + type_: lir::INT_32_TYPE, pointer_expression: lir::Expression::Variable(ptr, lir::ANY_TYPE), index: 0, }, lir::Statement::binary( old_ref_count, hir::BinaryOperator::LAND, - lir::Expression::Variable(header, lir::INT_TYPE), + lir::Expression::Variable(header, lir::INT_32_TYPE), lir::Expression::int(65535), ), lir::Statement::binary( is_zero, hir::BinaryOperator::EQ, - lir::Expression::Variable(old_ref_count, lir::INT_TYPE), + lir::Expression::Variable(old_ref_count, lir::INT_32_TYPE), lir::ZERO, ), lir::Statement::SingleIf { - condition: lir::Expression::Variable(is_zero, lir::INT_TYPE), + condition: lir::Expression::Variable(is_zero, lir::INT_32_TYPE), invert_condition: true, statements: vec![ lir::Statement::binary( ref_count, hir::BinaryOperator::PLUS, - lir::Expression::Variable(old_ref_count, lir::INT_TYPE), + lir::Expression::Variable(old_ref_count, lir::INT_32_TYPE), lir::ONE, ), lir::Statement::binary( lower, hir::BinaryOperator::LAND, - lir::Expression::Variable(ref_count, lir::INT_TYPE), + lir::Expression::Variable(ref_count, lir::INT_32_TYPE), lir::Expression::int(65535), ), lir::Statement::binary( upper, hir::BinaryOperator::LAND, - lir::Expression::Variable(header, lir::INT_TYPE), + lir::Expression::Variable(header, lir::INT_32_TYPE), lir::Expression::int(!65535), ), lir::Statement::binary( new_header, hir::BinaryOperator::LOR, - lir::Expression::Variable(upper, lir::INT_TYPE), - lir::Expression::Variable(lower, lir::INT_TYPE), + lir::Expression::Variable(upper, lir::INT_32_TYPE), + lir::Expression::Variable(lower, lir::INT_32_TYPE), ), lir::Statement::IndexedAssign { - assigned_expression: lir::Expression::Variable(new_header, lir::INT_TYPE), + assigned_expression: lir::Expression::Variable(new_header, lir::INT_32_TYPE), pointer_expression: lir::Expression::Variable(ptr, lir::ANY_TYPE), index: 0, }, @@ -512,52 +513,52 @@ fn generate_dec_ref_fn() -> lir::Function { lir::Statement::binary( not_ptr, hir::BinaryOperator::LOR, - lir::Expression::Variable(tiny_int, lir::INT_TYPE), - lir::Expression::Variable(is_odd, lir::INT_TYPE), + lir::Expression::Variable(tiny_int, lir::INT_32_TYPE), + lir::Expression::Variable(is_odd, lir::INT_32_TYPE), ), lir::Statement::SingleIf { - condition: lir::Expression::Variable(not_ptr, lir::INT_TYPE), + condition: lir::Expression::Variable(not_ptr, lir::INT_32_TYPE), invert_condition: true, statements: vec![ lir::Statement::IndexedAccess { name: header, - type_: lir::INT_TYPE, + type_: lir::INT_32_TYPE, pointer_expression: lir::Expression::Variable(ptr, lir::ANY_TYPE), index: 0, }, lir::Statement::binary( ref_count, hir::BinaryOperator::LAND, - lir::Expression::Variable(header, lir::INT_TYPE), + lir::Expression::Variable(header, lir::INT_32_TYPE), lir::Expression::int(65535), ), lir::Statement::binary( is_zero, hir::BinaryOperator::EQ, - lir::Expression::Variable(ref_count, lir::INT_TYPE), + lir::Expression::Variable(ref_count, lir::INT_32_TYPE), lir::ZERO, ), lir::Statement::SingleIf { - condition: lir::Expression::Variable(is_zero, lir::INT_TYPE), + condition: lir::Expression::Variable(is_zero, lir::INT_32_TYPE), invert_condition: true, statements: vec![ lir::Statement::binary( gt_one, hir::BinaryOperator::GT, - lir::Expression::Variable(ref_count, lir::INT_TYPE), + lir::Expression::Variable(ref_count, lir::INT_32_TYPE), lir::ONE, ), lir::Statement::IfElse { - condition: lir::Expression::Variable(gt_one, lir::INT_TYPE), + condition: lir::Expression::Variable(gt_one, lir::INT_32_TYPE), s1: vec![ lir::Statement::binary( new_header, hir::BinaryOperator::MINUS, - lir::Expression::Variable(header, lir::INT_TYPE), + lir::Expression::Variable(header, lir::INT_32_TYPE), lir::Expression::int(1), ), lir::Statement::IndexedAssign { - assigned_expression: lir::Expression::Variable(new_header, lir::INT_TYPE), + assigned_expression: lir::Expression::Variable(new_header, lir::INT_32_TYPE), pointer_expression: lir::Expression::Variable(ptr, lir::ANY_TYPE), index: 0, }, @@ -566,57 +567,57 @@ fn generate_dec_ref_fn() -> lir::Function { lir::Statement::binary( is_ref_bit_set, hir::BinaryOperator::SHR, - lir::Expression::Variable(header, lir::INT_TYPE), + lir::Expression::Variable(header, lir::INT_32_TYPE), lir::Expression::int(16), ), lir::Statement::While { loop_variables: vec![ lir::GenenalLoopVariable { name: bitset, - type_: lir::INT_TYPE, - initial_value: lir::Expression::Variable(is_ref_bit_set, lir::INT_TYPE), - loop_value: lir::Expression::Variable(new_is_ref_bit_set, lir::INT_TYPE), + type_: lir::INT_32_TYPE, + initial_value: lir::Expression::Variable(is_ref_bit_set, lir::INT_32_TYPE), + loop_value: lir::Expression::Variable(new_is_ref_bit_set, lir::INT_32_TYPE), }, lir::GenenalLoopVariable { name: PStr::LOWER_I, - type_: lir::INT_TYPE, + type_: lir::INT_32_TYPE, initial_value: lir::ONE, - loop_value: lir::Expression::Variable(new_i, lir::INT_TYPE), + loop_value: lir::Expression::Variable(new_i, lir::INT_32_TYPE), }, ], statements: vec![ lir::Statement::binary( should_stop, hir::BinaryOperator::GT, - lir::Expression::Variable(PStr::LOWER_I, lir::INT_TYPE), + lir::Expression::Variable(PStr::LOWER_I, lir::INT_32_TYPE), lir::Expression::int(16), ), lir::Statement::SingleIf { - condition: lir::Expression::Variable(should_stop, lir::INT_TYPE), + condition: lir::Expression::Variable(should_stop, lir::INT_32_TYPE), invert_condition: false, statements: vec![lir::Statement::Break(lir::ZERO)], }, lir::Statement::binary( is_ref, hir::BinaryOperator::LAND, - lir::Expression::Variable(is_ref_bit_set, lir::INT_TYPE), + lir::Expression::Variable(is_ref_bit_set, lir::INT_32_TYPE), lir::ONE, ), lir::Statement::SingleIf { - condition: lir::Expression::Variable(is_ref, lir::INT_TYPE), + condition: lir::Expression::Variable(is_ref, lir::INT_32_TYPE), invert_condition: false, statements: vec![ lir::Statement::binary( byte_offset, hir::BinaryOperator::SHL, - lir::Expression::Variable(PStr::LOWER_I, lir::INT_TYPE), + lir::Expression::Variable(PStr::LOWER_I, lir::INT_32_TYPE), lir::Expression::int(2), ), lir::Statement::binary( field_ptr, hir::BinaryOperator::PLUS, - lir::Expression::Variable(ptr, lir::INT_TYPE), - lir::Expression::Variable(byte_offset, lir::INT_TYPE), + lir::Expression::Variable(ptr, lir::INT_32_TYPE), + lir::Expression::Variable(byte_offset, lir::INT_32_TYPE), ), lir::Statement::Call { callee: lir::Expression::FnName( @@ -624,7 +625,7 @@ fn generate_dec_ref_fn() -> lir::Function { lir::Type::Fn(unknown_member_destructor_type()), ), arguments: vec![lir::Expression::Variable(field_ptr, lir::ANY_TYPE)], - return_type: lir::INT_TYPE, + return_type: lir::INT_32_TYPE, return_collector: None, }, ], @@ -632,13 +633,13 @@ fn generate_dec_ref_fn() -> lir::Function { lir::Statement::binary( new_is_ref_bit_set, hir::BinaryOperator::SHR, - lir::Expression::Variable(bitset, lir::INT_TYPE), + lir::Expression::Variable(bitset, lir::INT_32_TYPE), lir::ONE, ), lir::Statement::binary( new_i, hir::BinaryOperator::PLUS, - lir::Expression::Variable(PStr::LOWER_I, lir::INT_TYPE), + lir::Expression::Variable(PStr::LOWER_I, lir::INT_32_TYPE), lir::ONE, ), ], @@ -650,7 +651,7 @@ fn generate_dec_ref_fn() -> lir::Function { lir::Type::Fn(unknown_member_destructor_type()), ), arguments: vec![lir::Expression::Variable(ptr, lir::ANY_TYPE)], - return_type: lir::INT_TYPE, + return_type: lir::INT_32_TYPE, return_collector: None, }, ], @@ -685,14 +686,14 @@ pub fn compile_mir_to_lir(heap: &mut Heap, sources: mir::Sources) -> lir::Source }; type_defs.push(lir::TypeDefinition { name, - mappings: vec![lir::INT_TYPE, lir::Type::Fn(fn_type.clone()), lir::ANY_TYPE], + mappings: vec![lir::INT_32_TYPE, lir::Type::Fn(fn_type.clone()), lir::ANY_TYPE], }); closure_def_map.insert(name, fn_type); } for type_def in type_definitions { match &type_def.mappings { mir::TypeDefinitionMappings::Struct(types) => { - let mir_mappings = vec![lir::INT_TYPE] + let mir_mappings = vec![lir::INT_32_TYPE] .into_iter() .chain(types.iter().cloned().map(lower_type)) .collect_vec(); @@ -706,7 +707,7 @@ pub fn compile_mir_to_lir(heap: &mut Heap, sources: mir::Sources) -> lir::Source mir::EnumTypeDefinition::Boxed(types) => { let name = symbol_table.derived_type_name_with_subtype_tag(type_def.name, i as u32); let mut mappings = Vec::with_capacity(types.len() + 1); - mappings.push(lir::INT_TYPE); + mappings.push(lir::INT_32_TYPE); for t in types { mappings.push(lower_type(*t)); } @@ -716,7 +717,7 @@ pub fn compile_mir_to_lir(heap: &mut Heap, sources: mir::Sources) -> lir::Source } type_defs.push(lir::TypeDefinition { name: type_def.name, - mappings: vec![lir::INT_TYPE, lir::INT_TYPE], + mappings: vec![lir::INT_32_TYPE, lir::INT_32_TYPE], }); type_def_map.insert(type_def.name, type_def); } @@ -746,7 +747,8 @@ mod tests { mir::{ Callee, ClosureTypeDefinition, EnumTypeDefinition, Expression, Function, FunctionName, FunctionNameExpression, GenenalLoopVariable, Sources, Statement, SymbolTable, Type, - TypeDefinition, TypeDefinitionMappings, TypeNameId, VariableName, INT_TYPE, ONE, ZERO, + TypeDefinition, TypeDefinitionMappings, TypeNameId, VariableName, INT_31_TYPE, INT_32_TYPE, + ONE, ZERO, }, }; use samlang_heap::{Heap, PStr}; @@ -792,19 +794,19 @@ mod tests { global_variables: vec![], closure_types: vec![ClosureTypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("CC")), - function_type: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + function_type: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), }], type_definitions: vec![ TypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("Object")), - mappings: TypeDefinitionMappings::Struct(vec![INT_TYPE, INT_TYPE]), + mappings: TypeDefinitionMappings::Struct(vec![INT_32_TYPE, INT_32_TYPE]), }, TypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("Variant")), mappings: TypeDefinitionMappings::Enum(vec![ EnumTypeDefinition::Int, - EnumTypeDefinition::Unboxed(INT_TYPE), - EnumTypeDefinition::Boxed(vec![INT_TYPE, INT_TYPE]), + EnumTypeDefinition::Unboxed(INT_32_TYPE), + EnumTypeDefinition::Boxed(vec![INT_32_TYPE, INT_31_TYPE]), ]), }, TypeDefinition { @@ -830,7 +832,7 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("cc")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_31_TYPE), body: vec![ Statement::Call { callee: Callee::Variable(VariableName::new( @@ -838,24 +840,24 @@ mod tests { closure_type.clone(), )), arguments: vec![ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::IndexedAccess { name: heap.alloc_str_for_test("v1"), - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: Expression::var_name(PStr::LOWER_A, obj_type.clone()), index: 0, }, Statement::IndexedAccess { name: heap.alloc_str_for_test("v2"), - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: Expression::var_name(PStr::LOWER_B, variant_type.clone()), index: 0, }, Statement::IndexedAccess { name: heap.alloc_str_for_test("v3"), - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: Expression::var_name(PStr::LOWER_B, variant_type.clone()), index: 1, }, @@ -877,7 +879,7 @@ mod tests { Statement::While { loop_variables: vec![GenenalLoopVariable { name: PStr::UNDERSCORE, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO, }], @@ -897,7 +899,7 @@ mod tests { Function { name: FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![ Statement::Unary { name: heap.alloc_str_for_test("v1"), @@ -933,7 +935,7 @@ mod tests { name: FunctionName::new_for_test(heap.alloc_str_for_test("aaa")), type_: Type::new_fn_unwrapped( vec![Type::Id(table.create_type_name_for_test(PStr::STR_TYPE))], - INT_TYPE, + INT_32_TYPE, ), }, context: Expression::StringName(heap.alloc_str_for_test("G1")), @@ -943,7 +945,7 @@ mod tests { closure_type_name: *closure_type.as_id().unwrap(), function_name: FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("bbb")), - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), }, context: ZERO, }, @@ -953,7 +955,7 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("compiled_program_main")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![ Statement::IfElse { condition: ONE, @@ -961,19 +963,19 @@ mod tests { Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(PStr::MAIN_FN), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("cc")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("ccc")), }, ], @@ -996,7 +998,7 @@ mod tests { name: FunctionName::new_for_test(heap.alloc_str_for_test("aaa")), type_: Type::new_fn_unwrapped( vec![Type::Id(table.create_type_name_for_test(PStr::STR_TYPE))], - INT_TYPE, + INT_32_TYPE, ), }, context: Expression::var_name( @@ -1016,27 +1018,32 @@ mod tests { condition: ONE, s1: vec![Statement::Cast { name: heap.alloc_str_for_test("cast"), - type_: INT_TYPE, + type_: INT_32_TYPE, assigned_expression: ZERO, }], s2: vec![ Statement::LateInitDeclaration { name: heap.alloc_str_for_test("cast"), - type_: INT_TYPE, + type_: INT_32_TYPE, }, Statement::LateInitAssignment { name: heap.alloc_str_for_test("cast"), assigned_expression: ZERO, }, ], - final_assignments: vec![(heap.alloc_str_for_test("finalV2"), INT_TYPE, ZERO, ZERO)], + final_assignments: vec![( + heap.alloc_str_for_test("finalV2"), + INT_32_TYPE, + ZERO, + ZERO, + )], }, Statement::While { loop_variables: vec![], statements: vec![], break_collector: Some(VariableName::new( heap.alloc_str_for_test("finalV3"), - INT_TYPE, + INT_32_TYPE, )), }, ], @@ -1049,7 +1056,7 @@ mod tests { r#"{}type _CC = [number, (t0: any, t1: number) => number, any]; type _Object = [number, number, number]; type _Variant = [number, number]; -function __$cc(): number {{ +function __$cc(): i31 {{ let _t1: (t0: any, t1: number) => number = cc[1]; let _t2: any = cc[2]; _t1(_t2, 0); diff --git a/crates/samlang-compiler/src/lir_unused_name_elimination.rs b/crates/samlang-compiler/src/lir_unused_name_elimination.rs index 954960b2..d3d433d2 100644 --- a/crates/samlang-compiler/src/lir_unused_name_elimination.rs +++ b/crates/samlang-compiler/src/lir_unused_name_elimination.rs @@ -215,7 +215,7 @@ mod tests { hir, lir::{ Expression, Function, GenenalLoopVariable, Sources, Statement, Type, TypeDefinition, - INT_TYPE, ZERO, + INT_32_TYPE, ZERO, }, mir::{FunctionName, SymbolTable}, }; @@ -240,11 +240,11 @@ mod tests { type_definitions: vec![ TypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("Foo")), - mappings: vec![INT_TYPE], + mappings: vec![INT_32_TYPE], }, TypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("Baz")), - mappings: vec![INT_TYPE], + mappings: vec![INT_32_TYPE], }, ], main_function_names: vec![FunctionName::new_for_test(PStr::MAIN_FN)], @@ -252,14 +252,14 @@ mod tests { Function { name: FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { callee: Expression::FnName( FunctionName::new_for_test(heap.alloc_str_for_test("foo")), Type::Id(table.create_type_name_for_test(heap.alloc_str_for_test("Foo"))), ), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], return_value: ZERO, @@ -267,27 +267,27 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("foo")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![ Statement::Cast { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, assigned_expression: Expression::StringName(heap.alloc_str_for_test("bar")), }, Statement::StructInit { struct_variable_name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, expression_list: vec![Expression::FnName( FunctionName::new_for_test(heap.alloc_str_for_test("bar")), - INT_TYPE, + INT_32_TYPE, )], }, Statement::IndexedAccess { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: Expression::FnName( FunctionName::new_for_test(heap.alloc_str_for_test("bar")), - INT_TYPE, + INT_32_TYPE, ), index: 0, }, @@ -295,20 +295,20 @@ mod tests { assigned_expression: ZERO, pointer_expression: Expression::FnName( FunctionName::new_for_test(heap.alloc_str_for_test("bar")), - INT_TYPE, + INT_32_TYPE, ), index: 0, }, Statement::Call { callee: Expression::FnName( FunctionName::new_for_test(heap.alloc_str_for_test("baz")), - INT_TYPE, + INT_32_TYPE, ), arguments: vec![Expression::FnName( FunctionName::new_for_test(heap.alloc_str_for_test("haha")), - INT_TYPE, + INT_32_TYPE, )], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Unary { @@ -323,19 +323,19 @@ mod tests { hir::BinaryOperator::GE, Expression::FnName( FunctionName::new_for_test(heap.alloc_str_for_test("foo")), - INT_TYPE, + INT_32_TYPE, ), Expression::FnName( FunctionName::new_for_test(heap.alloc_str_for_test("bar")), - INT_TYPE, + INT_32_TYPE, ), )], s2: vec![Statement::Cast { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, assigned_expression: ZERO, }], - final_assignments: vec![(heap.alloc_str_for_test("fff"), INT_TYPE, ZERO, ZERO)], + final_assignments: vec![(heap.alloc_str_for_test("fff"), INT_32_TYPE, ZERO, ZERO)], }, Statement::SingleIf { condition: ZERO, @@ -345,27 +345,27 @@ mod tests { Statement::While { loop_variables: vec![GenenalLoopVariable { name: PStr::LOWER_F, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO, }], statements: vec![], - break_collector: Some((PStr::LOWER_D, INT_TYPE)), + break_collector: Some((PStr::LOWER_D, INT_32_TYPE)), }, ], return_value: Expression::FnName( FunctionName::new_for_test(heap.alloc_str_for_test("bar")), - INT_TYPE, + INT_32_TYPE, ), }, Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("bar")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { callee: Expression::StringName(heap.alloc_str_for_test("foo")), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], return_value: ZERO, @@ -373,7 +373,7 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("baz")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![], return_value: ZERO, }, diff --git a/crates/samlang-compiler/src/mir_constant_param_elimination.rs b/crates/samlang-compiler/src/mir_constant_param_elimination.rs index bb002128..91ce85f8 100644 --- a/crates/samlang-compiler/src/mir_constant_param_elimination.rs +++ b/crates/samlang-compiler/src/mir_constant_param_elimination.rs @@ -406,10 +406,11 @@ mod tests { hir::{BinaryOperator, UnaryOperator}, mir::{ Callee, Expression, Function, FunctionName, FunctionNameExpression, FunctionType, - GenenalLoopVariable, Sources, Statement, SymbolTable, Type, VariableName, INT_TYPE, ZERO, + GenenalLoopVariable, Sources, Statement, SymbolTable, Type, VariableName, INT_31_TYPE, + INT_32_TYPE, ZERO, }, }; - use samlang_heap::{Heap, PStr}; + use samlang_heap::{Heap, ModuleReference, PStr}; #[test] fn boilerplate() { @@ -439,8 +440,8 @@ mod tests { name: FunctionName::new_for_test(heap.alloc_str_for_test("otherwise_optimizable")), parameters: vec![PStr::LOWER_A, PStr::LOWER_B], type_: FunctionType { - argument_types: vec![INT_TYPE, INT_TYPE], - return_type: Box::new(INT_TYPE), + argument_types: vec![INT_32_TYPE, INT_32_TYPE], + return_type: Box::new(INT_32_TYPE), }, body: vec![], return_value: ZERO, @@ -448,8 +449,11 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("str_const")), parameters: vec![PStr::LOWER_A], - type_: FunctionType { argument_types: vec![INT_TYPE], return_type: Box::new(INT_TYPE) }, - body: vec![Statement::Break(Expression::var_name(PStr::LOWER_A, INT_TYPE))], + type_: FunctionType { + argument_types: vec![INT_32_TYPE], + return_type: Box::new(INT_32_TYPE), + }, + body: vec![Statement::Break(Expression::var_name(PStr::LOWER_A, INT_32_TYPE))], return_value: ZERO, }, Function { @@ -469,24 +473,24 @@ mod tests { Type::Id(table.create_type_name_for_test(PStr::UPPER_D)), Type::Id(table.create_type_name_for_test(PStr::UPPER_E)), ], - return_type: Box::new(INT_TYPE), + return_type: Box::new(INT_32_TYPE), }, body: vec![ Statement::Unary { name: dummy_name, operator: UnaryOperator::Not, - operand: Expression::var_name(PStr::LOWER_A, INT_TYPE), + operand: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), }, Statement::binary( dummy_name, BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_A, INT_TYPE), - Expression::var_name(PStr::LOWER_B, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), + Expression::var_name(PStr::LOWER_B, INT_32_TYPE), ), Statement::IndexedAccess { name: dummy_name, - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 0, }, Statement::ClosureInit { @@ -494,22 +498,28 @@ mod tests { closure_type_name: table.create_type_name_for_test(dummy_name), function_name: FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("otherwise_optimizable")), - type_: FunctionType { argument_types: vec![], return_type: Box::new(INT_TYPE) }, + type_: FunctionType { argument_types: vec![], return_type: Box::new(INT_32_TYPE) }, }, context: ZERO, }, Statement::Call { - callee: Callee::Variable(VariableName { name: dummy_name, type_: INT_TYPE }), + callee: Callee::Variable(VariableName { name: dummy_name, type_: INT_32_TYPE }), arguments: vec![ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("func_with_consts")), type_: FunctionType { - argument_types: vec![INT_TYPE, INT_TYPE, INT_TYPE, INT_TYPE, INT_TYPE], - return_type: Box::new(INT_TYPE), + argument_types: vec![ + INT_32_TYPE, + INT_32_TYPE, + INT_32_TYPE, + INT_32_TYPE, + INT_32_TYPE, + ], + return_type: Box::new(INT_32_TYPE), }, }), // a: matching constant @@ -521,59 +531,65 @@ mod tests { ZERO, Expression::int(1), Expression::int(2), - Expression::var_name(PStr::LOWER_D, INT_TYPE), - Expression::var_name(PStr::LOWER_E, INT_TYPE), + Expression::var_name(PStr::LOWER_D, INT_32_TYPE), + Expression::var_name(PStr::LOWER_E, INT_32_TYPE), ], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("func_with_consts")), type_: FunctionType { - argument_types: vec![INT_TYPE, INT_TYPE, INT_TYPE, INT_TYPE, INT_TYPE], - return_type: Box::new(INT_TYPE), + argument_types: vec![ + INT_32_TYPE, + INT_32_TYPE, + INT_32_TYPE, + INT_32_TYPE, + INT_32_TYPE, + ], + return_type: Box::new(INT_32_TYPE), }, }), arguments: vec![ ZERO, Expression::int(3), Expression::int(3), - Expression::var_name(PStr::LOWER_D, INT_TYPE), - Expression::var_name(PStr::LOWER_E, INT_TYPE), + Expression::var_name(PStr::LOWER_D, INT_32_TYPE), + Expression::var_name(PStr::LOWER_E, INT_32_TYPE), ], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("str_const")), type_: FunctionType { - argument_types: vec![INT_TYPE], - return_type: Box::new(INT_TYPE), + argument_types: vec![INT_32_TYPE], + return_type: Box::new(INT_32_TYPE), }, }), arguments: vec![Expression::StringName(heap.alloc_str_for_test("STR"))], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("otherwise_optimizable")), type_: FunctionType { - argument_types: vec![INT_TYPE, INT_TYPE], - return_type: Box::new(INT_TYPE), + argument_types: vec![INT_32_TYPE, INT_32_TYPE], + return_type: Box::new(INT_32_TYPE), }, }), arguments: vec![ZERO, ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::IfElse { - condition: Expression::var_name(PStr::LOWER_E, INT_TYPE), + condition: Expression::var_name(PStr::LOWER_E, INT_32_TYPE), s1: vec![Statement::Break(ZERO)], s2: vec![Statement::Break(ZERO)], - final_assignments: vec![(dummy_name, INT_TYPE, ZERO, ZERO)], + final_assignments: vec![(dummy_name, INT_32_TYPE, ZERO, ZERO)], }, Statement::SingleIf { condition: ZERO, @@ -583,19 +599,23 @@ mod tests { Statement::While { loop_variables: vec![GenenalLoopVariable { name: dummy_name, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO, }], statements: vec![Statement::Break(ZERO)], break_collector: None, }, - Statement::Cast { name: dummy_name, type_: INT_TYPE, assigned_expression: ZERO }, - Statement::LateInitDeclaration { name: dummy_name, type_: INT_TYPE }, + Statement::Cast { name: dummy_name, type_: INT_31_TYPE, assigned_expression: ZERO }, + Statement::LateInitDeclaration { name: dummy_name, type_: INT_31_TYPE }, Statement::LateInitAssignment { name: dummy_name, assigned_expression: ZERO }, Statement::StructInit { struct_variable_name: dummy_name, - type_name: table.create_type_name_for_test(dummy_name), + type_name: table.create_type_name_with_suffix( + ModuleReference::DUMMY, + dummy_name, + vec![INT_31_TYPE], + ), expression_list: vec![ZERO, ZERO], }, ], @@ -646,10 +666,10 @@ function __$func_with_consts(b: _B, e: _E): int { break; _ = 0; } - let _ = 0 as int; - let _: int; + let _ = 0 as i31; + let _: i31; _ = 0; - let _: __ = [0, 0]; + let _: DUMMY____i31 = [0, 0]; return 0; } "# diff --git a/crates/samlang-compiler/src/mir_generics_specialization.rs b/crates/samlang-compiler/src/mir_generics_specialization.rs index 3305014a..3e3ad126 100644 --- a/crates/samlang-compiler/src/mir_generics_specialization.rs +++ b/crates/samlang-compiler/src/mir_generics_specialization.rs @@ -116,14 +116,14 @@ impl Rewriter { let casted_collector = heap.alloc_temp_str(); collector.push(mir::Statement::IndexedAccess { name: variable_for_tag, - type_: mir::INT_TYPE, + type_: mir::INT_32_TYPE, pointer_expression: test_expr, index: 0, }); collector.push(mir::Statement::binary( comparison_temp, hir::BinaryOperator::EQ, - mir::Expression::var_name(variable_for_tag, mir::INT_TYPE), + mir::Expression::var_name(variable_for_tag, mir::INT_32_TYPE), mir::Expression::int(i32::try_from(*tag * 2 + 1).unwrap()), )); let mut nested_stmts = vec![]; @@ -146,7 +146,7 @@ impl Rewriter { } nested_stmts.append(&mut self.rewrite_stmts(heap, s1, generics_replacement_map)); collector.push(mir::Statement::IfElse { - condition: mir::Expression::var_name(comparison_temp, mir::INT_TYPE), + condition: mir::Expression::var_name(comparison_temp, mir::INT_32_TYPE), s1: nested_stmts, s2: self.rewrite_stmts(heap, s2, generics_replacement_map), final_assignments: self.rewrite_final_assignments( @@ -167,14 +167,14 @@ impl Rewriter { // do low-level bitwise is-pointer check. collector.push(mir::Statement::Cast { name: casted_int_collector, - type_: mir::INT_TYPE, + type_: mir::INT_32_TYPE, assigned_expression: test_expr, }); // Here we test whether this is a pointer collector.push(mir::Statement::Unary { name: comparison_temp, operator: hir::UnaryOperator::IsPointer, - operand: mir::Expression::var_name(casted_int_collector, mir::INT_TYPE), + operand: mir::Expression::var_name(casted_int_collector, mir::INT_32_TYPE), }); let mut nested_stmts = vec![]; // Once we pass the is-pointer check, we can cast the test expression to the underlying @@ -186,7 +186,7 @@ impl Rewriter { }); nested_stmts.append(&mut self.rewrite_stmts(heap, s1, generics_replacement_map)); collector.push(mir::Statement::IfElse { - condition: mir::Expression::var_name(comparison_temp, mir::INT_TYPE), + condition: mir::Expression::var_name(comparison_temp, mir::INT_32_TYPE), s1: nested_stmts, s2: self.rewrite_stmts(heap, s2, generics_replacement_map), final_assignments: self.rewrite_final_assignments( @@ -203,17 +203,17 @@ impl Rewriter { // Once we have i31 type, the test should be performed on i31. collector.push(mir::Statement::Cast { name: casted_collector, - type_: mir::INT_TYPE, + type_: mir::INT_32_TYPE, assigned_expression: test_expr, }); collector.push(mir::Statement::binary( comparison_temp, hir::BinaryOperator::EQ, - mir::Expression::var_name(casted_collector, mir::INT_TYPE), + mir::Expression::var_name(casted_collector, mir::INT_32_TYPE), mir::Expression::int(i32::try_from(*tag * 2 + 1).unwrap()), )); collector.push(mir::Statement::IfElse { - condition: mir::Expression::var_name(comparison_temp, mir::INT_TYPE), + condition: mir::Expression::var_name(comparison_temp, mir::INT_32_TYPE), s1: self.rewrite_stmts(heap, s1, generics_replacement_map), s2: self.rewrite_stmts(heap, s2, generics_replacement_map), final_assignments: self.rewrite_final_assignments( @@ -536,11 +536,11 @@ impl Rewriter { mir_variants.push(mir::EnumTypeDefinition::Int); } else { if let Some((i, t)) = already_unused_boxed_optimization { - mir_variants[i] = mir::EnumTypeDefinition::Boxed(vec![mir::INT_TYPE, t]); + mir_variants[i] = mir::EnumTypeDefinition::Boxed(vec![mir::INT_32_TYPE, t]); already_unused_boxed_optimization = None; } let mut mapping_types = Vec::with_capacity(types.len() + 1); - mapping_types.push(mir::INT_TYPE); + mapping_types.push(mir::INT_32_TYPE); for t in types { mapping_types.push(self.rewrite_type(heap, t, &solved_targs_replacement_map)); } @@ -584,7 +584,7 @@ impl Rewriter { fn type_permit_enum_boxed_optimization(&self, type_: &mir::Type) -> bool { match type_ { // We cannot distinguish unboxed int from tags - mir::Type::Int32 => false, + mir::Type::Int32 | mir::Type::Int31 => false, mir::Type::Id(type_id) => { match &self.specialized_type_definitions.get(type_id).unwrap().mappings { // Structs are always pointers. diff --git a/crates/samlang-compiler/src/mir_tail_recursion_rewrite.rs b/crates/samlang-compiler/src/mir_tail_recursion_rewrite.rs index 05f92351..8ff90689 100644 --- a/crates/samlang-compiler/src/mir_tail_recursion_rewrite.rs +++ b/crates/samlang-compiler/src/mir_tail_recursion_rewrite.rs @@ -213,7 +213,7 @@ pub(super) fn optimize_function_by_tailrec_rewrite( mod tests { use super::*; use pretty_assertions::assert_eq; - use samlang_ast::mir::{FunctionNameExpression, SymbolTable, INT_TYPE}; + use samlang_ast::mir::{FunctionNameExpression, SymbolTable, INT_32_TYPE}; fn assert_optimization_failed(f: Function, heap: &mut Heap) { assert!(optimize_function_by_tailrec_rewrite_aux(heap, f).is_err()) @@ -236,7 +236,7 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("ff")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![], return_value: Expression::StringName(PStr::LOWER_A), }, @@ -247,9 +247,9 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("ff")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![], - return_value: Expression::var_name(PStr::LOWER_A, INT_TYPE), + return_value: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), }, heap, ); @@ -258,9 +258,9 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("ff")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::binary(PStr::LOWER_A, BinaryOperator::PLUS, ZERO, ZERO)], - return_value: Expression::var_name(PStr::LOWER_A, INT_TYPE), + return_value: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), }, heap, ); @@ -269,14 +269,14 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("ff")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { - callee: Callee::Variable(VariableName::new(PStr::LOWER_A, INT_TYPE)), + callee: Callee::Variable(VariableName::new(PStr::LOWER_A, INT_32_TYPE)), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], - return_value: Expression::var_name(PStr::LOWER_A, INT_TYPE), + return_value: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), }, heap, ); @@ -285,17 +285,17 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("ff")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_A), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], - return_value: Expression::var_name(PStr::LOWER_A, INT_TYPE), + return_value: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), }, heap, ); @@ -304,14 +304,14 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("ff")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::IfElse { condition: ZERO, s1: vec![], s2: vec![], final_assignments: vec![], }], - return_value: Expression::var_name(PStr::LOWER_A, INT_TYPE), + return_value: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), }, heap, ); @@ -320,7 +320,7 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("ff")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::IfElse { condition: ZERO, s1: vec![], @@ -336,17 +336,17 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("ff")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("ff")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], - return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), }, heap, ); @@ -360,25 +360,25 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), parameters: vec![heap.alloc_str_for_test("n")], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![ Statement::binary( PStr::LOWER_A, BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), }), - arguments: vec![Expression::var_name(PStr::LOWER_A, INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(PStr::LOWER_A, INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("r")), }, ], - return_value: Expression::var_name(heap.alloc_str_for_test("r"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("r"), INT_32_TYPE), }, heap, table, @@ -400,21 +400,21 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), parameters: vec![heap.alloc_str_for_test("n")], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![ Statement::binary( PStr::LOWER_A, BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), }), - arguments: vec![Expression::var_name(PStr::LOWER_A, INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(PStr::LOWER_A, INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }, ], @@ -443,12 +443,12 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), parameters: vec![heap.alloc_str_for_test("n")], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![ Statement::binary( PStr::LOWER_A, BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::IfElse { @@ -456,30 +456,30 @@ mod tests { s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), }), - arguments: vec![Expression::var_name(PStr::LOWER_A, INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(PStr::LOWER_A, INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("r1")), }], s2: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), }), - arguments: vec![Expression::var_name(PStr::LOWER_A, INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(PStr::LOWER_A, INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("r2")), }], final_assignments: vec![( heap.alloc_str_for_test("r"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("r1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("r2"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("r1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("r2"), INT_32_TYPE), )], }, ], - return_value: Expression::var_name(heap.alloc_str_for_test("r"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("r"), INT_32_TYPE), }, heap, table, @@ -509,12 +509,12 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), parameters: vec![heap.alloc_str_for_test("n")], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![ Statement::binary( PStr::LOWER_A, BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::IfElse { @@ -522,22 +522,22 @@ mod tests { s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), }), - arguments: vec![Expression::var_name(PStr::LOWER_A, INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(PStr::LOWER_A, INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("r1")), }], s2: vec![], final_assignments: vec![( heap.alloc_str_for_test("r"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("r1"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("r1"), INT_32_TYPE), ZERO, )], }, ], - return_value: Expression::var_name(heap.alloc_str_for_test("r"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("r"), INT_32_TYPE), }, heap, table, @@ -564,12 +564,12 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), parameters: vec![heap.alloc_str_for_test("n")], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![ Statement::binary( PStr::LOWER_A, BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::IfElse { @@ -577,10 +577,10 @@ mod tests { s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), }), - arguments: vec![Expression::var_name(PStr::LOWER_A, INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(PStr::LOWER_A, INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }], s2: vec![], @@ -616,7 +616,7 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), parameters: vec![heap.alloc_str_for_test("n")], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![Statement::IfElse { condition: ZERO, s1: vec![], @@ -627,34 +627,34 @@ mod tests { Statement::binary( heap.alloc_str_for_test("nn"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), Expression::int(1), ), Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("loopy")), - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), }), - arguments: vec![Expression::var_name(heap.alloc_str_for_test("nn"), INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(heap.alloc_str_for_test("nn"), INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("r")), }, ], final_assignments: vec![( heap.alloc_str_for_test("nested_return"), - INT_TYPE, + INT_32_TYPE, Expression::int(1), - Expression::var_name(heap.alloc_str_for_test("r"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("r"), INT_32_TYPE), )], }], final_assignments: vec![( heap.alloc_str_for_test("v"), - INT_TYPE, + INT_32_TYPE, ZERO, - Expression::var_name(heap.alloc_str_for_test("nested_return"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("nested_return"), INT_32_TYPE), )], }], - return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), }, heap, table, diff --git a/crates/samlang-compiler/src/mir_type_deduplication.rs b/crates/samlang-compiler/src/mir_type_deduplication.rs index f4bcd8ac..a9788ab5 100644 --- a/crates/samlang-compiler/src/mir_type_deduplication.rs +++ b/crates/samlang-compiler/src/mir_type_deduplication.rs @@ -19,6 +19,7 @@ fn rewrite_id_type_name(state: &State, id: TypeNameId) -> TypeNameId { fn rewrite_type(state: &State, type_: Type) -> Type { match type_ { Type::Int32 => Type::Int32, + Type::Int31 => Type::Int31, Type::Id(id) => Type::Id(rewrite_id_type_name(state, id)), } } @@ -241,7 +242,7 @@ pub(super) fn deduplicate( mod tests { use super::*; use pretty_assertions::assert_eq; - use samlang_ast::mir::{FunctionName, SymbolTable, INT_TYPE, ONE, ZERO}; + use samlang_ast::mir::{FunctionName, SymbolTable, INT_31_TYPE, INT_32_TYPE, ONE, ZERO}; use samlang_heap::{Heap, PStr}; #[should_panic] @@ -275,7 +276,12 @@ mod tests { assert_eq!( "() -> int", - rewrite_fn_type(&HashMap::new(), Type::new_fn_unwrapped(vec![], INT_TYPE)) + rewrite_fn_type(&HashMap::new(), Type::new_fn_unwrapped(vec![], INT_32_TYPE)) + .pretty_print(heap, table) + ); + assert_eq!( + "() -> i31", + rewrite_fn_type(&HashMap::new(), Type::new_fn_unwrapped(vec![], INT_31_TYPE)) .pretty_print(heap, table) ); } @@ -290,11 +296,11 @@ mod tests { closure_types: vec![ ClosureTypeDefinition { name: table.create_type_name_for_test(PStr::UPPER_A), - function_type: Type::new_fn_unwrapped(vec![], INT_TYPE), + function_type: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, ClosureTypeDefinition { name: table.create_type_name_for_test(PStr::UPPER_B), - function_type: Type::new_fn_unwrapped(vec![], INT_TYPE), + function_type: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, ClosureTypeDefinition { name: table.create_type_name_for_test(PStr::UPPER_C), @@ -308,22 +314,22 @@ mod tests { TypeDefinition { name: table.create_type_name_for_test(PStr::UPPER_C), mappings: TypeDefinitionMappings::Struct(vec![ - INT_TYPE, + INT_32_TYPE, Type::Id(table.create_type_name_for_test(PStr::STR_TYPE)), ]), }, TypeDefinition { name: table.create_type_name_for_test(PStr::UPPER_D), mappings: TypeDefinitionMappings::Struct(vec![ - INT_TYPE, + INT_32_TYPE, Type::Id(table.create_type_name_for_test(PStr::STR_TYPE)), ]), }, TypeDefinition { name: table.create_type_name_for_test(PStr::UPPER_E), mappings: TypeDefinitionMappings::Enum(vec![ - EnumTypeDefinition::Boxed(vec![INT_TYPE]), - EnumTypeDefinition::Unboxed(INT_TYPE), + EnumTypeDefinition::Boxed(vec![INT_32_TYPE]), + EnumTypeDefinition::Unboxed(INT_32_TYPE), EnumTypeDefinition::Int, ]), }, @@ -332,7 +338,7 @@ mod tests { functions: vec![Function { name: FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![Statement::IfElse { condition: ONE, s1: vec![ @@ -345,16 +351,16 @@ mod tests { Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_F), - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), }), arguments: vec![ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { - callee: Callee::Variable(VariableName { name: PStr::LOWER_F, type_: INT_TYPE }), + callee: Callee::Variable(VariableName { name: PStr::LOWER_F, type_: INT_32_TYPE }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::IndexedAccess { @@ -365,8 +371,12 @@ mod tests { }, ], s2: vec![ - Statement::Cast { name: PStr::UNDERSCORE, type_: INT_TYPE, assigned_expression: ZERO }, - Statement::LateInitDeclaration { name: PStr::UNDERSCORE, type_: INT_TYPE }, + Statement::Cast { + name: PStr::UNDERSCORE, + type_: INT_32_TYPE, + assigned_expression: ZERO, + }, + Statement::LateInitDeclaration { name: PStr::UNDERSCORE, type_: INT_32_TYPE }, Statement::LateInitAssignment { name: PStr::UNDERSCORE, assigned_expression: ZERO }, Statement::StructInit { struct_variable_name: PStr::UNDERSCORE, @@ -380,13 +390,13 @@ mod tests { name: FunctionName::new_for_test(PStr::LOWER_F), type_: Type::new_fn_unwrapped( vec![Type::Id(table.create_type_name_for_test(PStr::UPPER_E))], - INT_TYPE, + INT_32_TYPE, ), }, - context: Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + context: Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), }, ], - final_assignments: vec![(PStr::UNDERSCORE, INT_TYPE, ZERO, ZERO)], + final_assignments: vec![(PStr::UNDERSCORE, INT_32_TYPE, ZERO, ZERO)], }], return_value: ZERO, }], diff --git a/crates/samlang-compiler/src/wasm_lowering.rs b/crates/samlang-compiler/src/wasm_lowering.rs index 8f85c616..c44ef053 100644 --- a/crates/samlang-compiler/src/wasm_lowering.rs +++ b/crates/samlang-compiler/src/wasm_lowering.rs @@ -320,7 +320,7 @@ mod tests { use pretty_assertions::assert_eq; use samlang_ast::{ hir::{BinaryOperator, GlobalVariable, UnaryOperator}, - lir::{Expression, Function, GenenalLoopVariable, Sources, Statement, Type, INT_TYPE, ZERO}, + lir::{Expression, Function, GenenalLoopVariable, Sources, Statement, Type, INT_32_TYPE, ZERO}, mir, wasm, }; use samlang_heap::{Heap, PStr}; @@ -354,7 +354,7 @@ mod tests { functions: vec![Function { name: mir::FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![heap.alloc_str_for_test("bar")], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![ Statement::IfElse { condition: ZERO, s1: vec![], s2: vec![], final_assignments: vec![] }, Statement::IfElse { @@ -362,7 +362,7 @@ mod tests { s1: vec![], s2: vec![Statement::Cast { name: PStr::LOWER_C, - type_: INT_TYPE, + type_: INT_32_TYPE, assigned_expression: ZERO, }], final_assignments: vec![], @@ -372,13 +372,17 @@ mod tests { s1: vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO, }], statements: vec![ - Statement::Cast { name: PStr::LOWER_C, type_: INT_TYPE, assigned_expression: ZERO }, - Statement::LateInitDeclaration { name: PStr::LOWER_C, type_: INT_TYPE }, + Statement::Cast { + name: PStr::LOWER_C, + type_: INT_32_TYPE, + assigned_expression: ZERO, + }, + Statement::LateInitDeclaration { name: PStr::LOWER_C, type_: INT_32_TYPE }, Statement::LateInitAssignment { name: PStr::LOWER_C, assigned_expression: ZERO }, ], break_collector: None, @@ -391,7 +395,7 @@ mod tests { invert_condition: false, statements: vec![Statement::Break(ZERO)], }], - break_collector: Some((PStr::LOWER_B, INT_TYPE)), + break_collector: Some((PStr::LOWER_B, INT_32_TYPE)), }, Statement::While { loop_variables: vec![], @@ -405,11 +409,11 @@ mod tests { ], final_assignments: vec![( PStr::LOWER_F, - INT_TYPE, + INT_32_TYPE, Expression::StringName(heap.alloc_str_for_test("FOO")), Expression::FnName( mir::FunctionName::new_for_test(PStr::MAIN_FN), - Type::new_fn(vec![], INT_TYPE), + Type::new_fn(vec![], INT_32_TYPE), ), )], }, @@ -426,41 +430,41 @@ mod tests { Statement::binary( heap.alloc_str_for_test("bin"), BinaryOperator::PLUS, - Expression::Variable(PStr::LOWER_F, INT_TYPE), + Expression::Variable(PStr::LOWER_F, INT_32_TYPE), ZERO, ), Statement::Call { callee: Expression::FnName( mir::FunctionName::new_for_test(PStr::MAIN_FN), - Type::new_fn(vec![], INT_TYPE), + Type::new_fn(vec![], INT_32_TYPE), ), arguments: vec![ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { - callee: Expression::Variable(PStr::LOWER_F, INT_TYPE), + callee: Expression::Variable(PStr::LOWER_F, INT_32_TYPE), arguments: vec![ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("rc")), }, Statement::IndexedAccess { name: heap.alloc_str_for_test("v"), - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: ZERO, index: 3, }, Statement::IndexedAssign { - assigned_expression: Expression::Variable(heap.alloc_str_for_test("v"), INT_TYPE), + assigned_expression: Expression::Variable(heap.alloc_str_for_test("v"), INT_32_TYPE), pointer_expression: ZERO, index: 3, }, Statement::StructInit { struct_variable_name: heap.alloc_str_for_test("s"), - type_: INT_TYPE, + type_: INT_32_TYPE, expression_list: vec![ ZERO, - Expression::Variable(heap.alloc_str_for_test("v"), INT_TYPE), + Expression::Variable(heap.alloc_str_for_test("v"), INT_32_TYPE), ], }, ], diff --git a/crates/samlang-optimization/src/common_subexpression_elimination.rs b/crates/samlang-optimization/src/common_subexpression_elimination.rs index b0a2bad3..33a26559 100644 --- a/crates/samlang-optimization/src/common_subexpression_elimination.rs +++ b/crates/samlang-optimization/src/common_subexpression_elimination.rs @@ -93,7 +93,7 @@ mod tests { hir::{BinaryOperator, UnaryOperator}, mir::{ Callee, Expression, Function, FunctionName, FunctionNameExpression, Statement, SymbolTable, - Type, VariableName, INT_TYPE, ONE, ZERO, + Type, VariableName, INT_32_TYPE, ONE, ZERO, }, }; use samlang_heap::{Heap, PStr}; @@ -102,7 +102,7 @@ mod tests { let mut f = Function { name: FunctionName::new_for_test(PStr::LOWER_A), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: stmts, return_value: ZERO, }; @@ -121,7 +121,7 @@ mod tests { assert_correctly_optimized( vec![Statement::IfElse { - condition: Expression::var_name(PStr::LOWER_B, INT_TYPE), + condition: Expression::var_name(PStr::LOWER_B, INT_32_TYPE), s1: vec![ Statement::binary(heap.alloc_str_for_test("ddddd"), BinaryOperator::PLUS, ONE, ONE), Statement::Unary { @@ -132,20 +132,20 @@ mod tests { Statement::binary(PStr::LOWER_A, BinaryOperator::PLUS, ONE, ZERO), Statement::IndexedAccess { name: heap.alloc_str_for_test("ddd"), - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: ZERO, index: 3, }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fff")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![ - Expression::var_name(PStr::LOWER_A, INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("ddd"), INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("ddd"), INT_32_TYPE), ], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, ], @@ -158,17 +158,20 @@ mod tests { Statement::binary(heap.alloc_str_for_test("fd"), BinaryOperator::PLUS, ONE, ZERO), Statement::IndexedAccess { name: heap.alloc_str_for_test("eee"), - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: ZERO, index: 3, }, Statement::Call { - callee: Callee::Variable(VariableName::new(heap.alloc_str_for_test("eeee"), INT_TYPE)), + callee: Callee::Variable(VariableName::new( + heap.alloc_str_for_test("eeee"), + INT_32_TYPE, + )), arguments: vec![ - Expression::var_name(heap.alloc_str_for_test("fd"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("eee"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("fd"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("eee"), INT_32_TYPE), ], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, ], diff --git a/crates/samlang-optimization/src/conditional_constant_propagation.rs b/crates/samlang-optimization/src/conditional_constant_propagation.rs index ebd743ed..3549df8f 100644 --- a/crates/samlang-optimization/src/conditional_constant_propagation.rs +++ b/crates/samlang-optimization/src/conditional_constant_propagation.rs @@ -255,7 +255,7 @@ fn optimize_stmt( Statement::IndexedAccess { name, type_, pointer_expression, index } => { let pointer_expression = optimize_expr(value_cx, pointer_expression); if let Some(computed) = index_access_cx.get(&IndexAccessBindedValue { - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression, index: *index, }) { @@ -463,7 +463,7 @@ fn optimize_stmt( for (i, e) in expression_list.iter().enumerate() { let optimized = optimize_expr(value_cx, e); let key = IndexAccessBindedValue { - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: Expression::Variable(VariableName { name: *struct_variable_name, type_: Type::Id(*type_name), @@ -609,7 +609,7 @@ mod boilterplate_tests { 1, BinaryExpression { operator: BinaryOperator::PLUS, - e1: VariableName { name: PStr::INVALID_PSTR, type_: INT_TYPE }, + e1: VariableName { name: PStr::INVALID_PSTR, type_: INT_32_TYPE }, e2: 1, } .clone() @@ -622,20 +622,20 @@ mod boilterplate_tests { fn panic_test() { let mut value_cx = LocalValueContextForOptimization::new(); let heap = &mut Heap::new(); - value_cx.checked_bind(PStr::LOWER_A, Expression::var_name(PStr::LOWER_A, INT_TYPE)); + value_cx.checked_bind(PStr::LOWER_A, Expression::var_name(PStr::LOWER_A, INT_32_TYPE)); value_cx.checked_bind(PStr::LOWER_B, Expression::StringName(heap.alloc_str_for_test("1"))); value_cx.checked_bind(PStr::LOWER_C, Expression::StringName(PStr::UPPER_A)); optimize_callee( &mut value_cx, - &Callee::Variable(VariableName { name: PStr::LOWER_A, type_: INT_TYPE }), + &Callee::Variable(VariableName { name: PStr::LOWER_A, type_: INT_32_TYPE }), ); optimize_callee( &mut value_cx, - &Callee::Variable(VariableName { name: PStr::LOWER_B, type_: INT_TYPE }), + &Callee::Variable(VariableName { name: PStr::LOWER_B, type_: INT_32_TYPE }), ); optimize_callee( &mut value_cx, - &Callee::Variable(VariableName { name: PStr::LOWER_C, type_: INT_TYPE }), + &Callee::Variable(VariableName { name: PStr::LOWER_C, type_: INT_32_TYPE }), ); } } diff --git a/crates/samlang-optimization/src/conditional_constant_propagation_tests.rs b/crates/samlang-optimization/src/conditional_constant_propagation_tests.rs index cd5a2a0f..805e75a4 100644 --- a/crates/samlang-optimization/src/conditional_constant_propagation_tests.rs +++ b/crates/samlang-optimization/src/conditional_constant_propagation_tests.rs @@ -6,7 +6,7 @@ mod tests { hir::{BinaryOperator, UnaryOperator}, mir::{ Callee, Expression, Function, FunctionName, FunctionNameExpression, GenenalLoopVariable, - Statement, SymbolTable, Type, VariableName, INT_TYPE, ONE, ZERO, + Statement, SymbolTable, Type, VariableName, INT_32_TYPE, ONE, ZERO, }, }; use samlang_heap::{Heap, PStr}; @@ -21,7 +21,7 @@ mod tests { let mut f = Function { name: FunctionName::new_for_test(PStr::LOWER_A), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: stmts, return_value, }; @@ -50,7 +50,7 @@ mod tests { heap.alloc_str_for_test("c0"), BinaryOperator::SHL, Expression::int(3), - Expression::var_name(heap.alloc_str_for_test("c00"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("c00"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("c1"), @@ -80,11 +80,11 @@ mod tests { struct_variable_name: heap.alloc_str_for_test("c_o"), type_name: table.create_type_name_for_test(heap.alloc_str_for_test("Id")), expression_list: vec![ - Expression::var_name(heap.alloc_str_for_test("c0"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("c1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("c2"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("c3"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("c4"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("c0"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("c1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("c2"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("c3"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("c4"), INT_32_TYPE), ], }, Statement::binary( @@ -96,112 +96,112 @@ mod tests { Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), ), Statement::IndexedAccess { name: heap.alloc_str_for_test("i0"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), index: 2, }, Statement::binary( heap.alloc_str_for_test("a3"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("a2"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a2"), INT_32_TYPE), ONE, ), Statement::binary( heap.alloc_str_for_test("b1"), BinaryOperator::DIV, - Expression::var_name(heap.alloc_str_for_test("a2"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a2"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a2"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a2"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("b2"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("a2"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a2"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a2"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a2"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("b3"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("b1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b2"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("b1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b2"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("b4"), BinaryOperator::MOD, - Expression::var_name(heap.alloc_str_for_test("b1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("b1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b1"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("b5"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("b6"), BinaryOperator::MOD, - Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("b7"), BinaryOperator::DIV, - Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("b8"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("a4"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a3"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a3"), INT_32_TYPE), ZERO, ), Statement::binary( heap.alloc_str_for_test("a5"), BinaryOperator::DIV, - Expression::var_name(heap.alloc_str_for_test("a4"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a4"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b1"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("a6"), BinaryOperator::DIV, - Expression::var_name(heap.alloc_str_for_test("i1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a5"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a5"), INT_32_TYPE), ), Statement::Unary { name: heap.alloc_str_for_test("a7"), operator: UnaryOperator::Not, - operand: Expression::var_name(heap.alloc_str_for_test("a6"), INT_TYPE), + operand: Expression::var_name(heap.alloc_str_for_test("a6"), INT_32_TYPE), }, Statement::Unary { name: heap.alloc_str_for_test("a8"), operator: UnaryOperator::IsPointer, - operand: Expression::var_name(heap.alloc_str_for_test("a6"), INT_TYPE), + operand: Expression::var_name(heap.alloc_str_for_test("a6"), INT_32_TYPE), }, Statement::StructInit { struct_variable_name: heap.alloc_str_for_test("s"), type_name: table.create_type_name_for_test(heap.alloc_str_for_test("Id")), expression_list: vec![ - Expression::var_name(heap.alloc_str_for_test("b2"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a6"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a5"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a7"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a8"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("b2"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a6"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a5"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a7"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a8"), INT_32_TYPE), ], }, Statement::ClosureInit { @@ -209,49 +209,49 @@ mod tests { closure_type_name: table.create_type_name_for_test(heap.alloc_str_for_test("Id")), function_name: FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("closure")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, - context: Expression::var_name(heap.alloc_str_for_test("b2"), INT_TYPE), + context: Expression::var_name(heap.alloc_str_for_test("b2"), INT_32_TYPE), }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fff")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![ - Expression::var_name(heap.alloc_str_for_test("b1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b2"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b3"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b4"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b5"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b6"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b7"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("b1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b2"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b3"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b4"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b5"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b6"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b7"), INT_32_TYPE), ], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::binary( heap.alloc_str_for_test("a7"), BinaryOperator::MOD, - Expression::var_name(heap.alloc_str_for_test("a5"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a5"), INT_32_TYPE), Expression::int(12), ), Statement::binary( heap.alloc_str_for_test("a8"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("a7"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a7"), INT_32_TYPE), Expression::int(7), ), Statement::binary( heap.alloc_str_for_test("a9"), BinaryOperator::DIV, - Expression::var_name(heap.alloc_str_for_test("a7"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a7"), INT_32_TYPE), ZERO, ), Statement::binary( heap.alloc_str_for_test("a10"), BinaryOperator::MOD, - Expression::var_name(heap.alloc_str_for_test("a7"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a7"), INT_32_TYPE), ZERO, ), Statement::binary( @@ -269,51 +269,51 @@ mod tests { Statement::binary( heap.alloc_str_for_test("a13"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a11"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a8"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a11"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a8"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("a14"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a13"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a12"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a13"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a12"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("a15"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), Expression::int(5), ), Statement::binary( heap.alloc_str_for_test("a16"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a15"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a15"), INT_32_TYPE), Expression::int(5), ), Statement::binary( heap.alloc_str_for_test("a17"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a14"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a16"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a14"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a16"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("a18"), BinaryOperator::DIV, - Expression::var_name(heap.alloc_str_for_test("a15"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a15"), INT_32_TYPE), Expression::int(5), ), Statement::Cast { name: heap.alloc_str_for_test("a19"), - type_: INT_TYPE, - assigned_expression: Expression::var_name(heap.alloc_str_for_test("a18"), INT_TYPE), + type_: INT_32_TYPE, + assigned_expression: Expression::var_name(heap.alloc_str_for_test("a18"), INT_32_TYPE), }, - Statement::LateInitDeclaration { name: heap.alloc_str_for_test("a20"), type_: INT_TYPE }, + Statement::LateInitDeclaration { name: heap.alloc_str_for_test("a20"), type_: INT_32_TYPE }, Statement::LateInitAssignment { name: heap.alloc_str_for_test("a20"), - assigned_expression: Expression::var_name(heap.alloc_str_for_test("a18"), INT_TYPE), + assigned_expression: Expression::var_name(heap.alloc_str_for_test("a18"), INT_32_TYPE), }, ], - Expression::var_name(heap.alloc_str_for_test("a17"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a17"), INT_32_TYPE), heap, table, r#"let c_o: _Id = [6, 1, 2147483646, 0, 3]; @@ -352,7 +352,7 @@ return (a17: int);"#, }, Statement::IndexedAccess { name: heap.alloc_str_for_test("v1"), - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: Expression::var_name( PStr::LOWER_A, Type::Id(table.create_type_name_for_test(heap.alloc_str_for_test("Id"))), @@ -361,7 +361,7 @@ return (a17: int);"#, }, Statement::IndexedAccess { name: heap.alloc_str_for_test("v2"), - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: Expression::var_name( PStr::LOWER_A, Type::Id(table.create_type_name_for_test(heap.alloc_str_for_test("Id"))), @@ -371,11 +371,11 @@ return (a17: int);"#, Statement::binary( heap.alloc_str_for_test("result"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("v1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("v2"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("v1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("v2"), INT_32_TYPE), ), ], - Expression::var_name(heap.alloc_str_for_test("result"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("result"), INT_32_TYPE), heap, table, r#"let a: _Id = [0, 1]; @@ -393,13 +393,13 @@ return 1;"#, Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), Expression::int(2), ), ], @@ -416,13 +416,13 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), Expression::int(3), ), ], @@ -439,13 +439,13 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), Expression::int(3), ), ], @@ -462,13 +462,13 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), Expression::int(3), ), ], @@ -485,25 +485,25 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), Expression::int(3), ), Statement::binary( heap.alloc_str_for_test("a3"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a4"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("a3"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a3"), INT_32_TYPE), Expression::int(3), ), ], @@ -522,13 +522,13 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::LT, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), Expression::int(3), ), ], @@ -545,13 +545,13 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::LE, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), Expression::int(3), ), ], @@ -568,13 +568,13 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::GT, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), Expression::int(3), ), ], @@ -591,13 +591,13 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::GE, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), Expression::int(3), ), ], @@ -614,13 +614,13 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), Expression::int(3), ), ], @@ -637,13 +637,13 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::NE, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), Expression::int(3), ), ], @@ -660,13 +660,13 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("a1"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), Expression::int(2), ), Statement::binary( heap.alloc_str_for_test("a2"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), Expression::int(3), ), ], @@ -688,129 +688,129 @@ return 0;"#, vec![ Statement::binary(heap.alloc_str_for_test("b1"), BinaryOperator::LT, ZERO, ONE), Statement::IfElse { - condition: Expression::var_name(heap.alloc_str_for_test("b1"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("b1"), INT_32_TYPE), s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("foo")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], s2: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("bar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], final_assignments: vec![], }, Statement::binary(heap.alloc_str_for_test("b2"), BinaryOperator::GT, ZERO, ONE), Statement::IfElse { - condition: Expression::var_name(heap.alloc_str_for_test("b2"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("b2"), INT_32_TYPE), s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("foo")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], s2: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("bar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], final_assignments: vec![], }, Statement::binary(heap.alloc_str_for_test("b3"), BinaryOperator::LE, ZERO, ONE), Statement::IfElse { - condition: Expression::var_name(heap.alloc_str_for_test("b3"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("b3"), INT_32_TYPE), s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("foo")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("a1")), }], s2: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("bar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("a2")), }], final_assignments: vec![( heap.alloc_str_for_test("ma1"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a2"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a2"), INT_32_TYPE), )], }, Statement::binary(heap.alloc_str_for_test("b4"), BinaryOperator::GE, ZERO, ONE), Statement::IfElse { - condition: Expression::var_name(heap.alloc_str_for_test("b4"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("b4"), INT_32_TYPE), s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("foo")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("a11")), }], s2: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("bar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("a22")), }], final_assignments: vec![( heap.alloc_str_for_test("ma2"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("a11"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a22"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("a11"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a22"), INT_32_TYPE), )], }, Statement::IfElse { - condition: Expression::var_name(heap.alloc_str_for_test("ma2"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("ma2"), INT_32_TYPE), s1: vec![], s2: vec![], - final_assignments: vec![(heap.alloc_str_for_test("ma3"), INT_TYPE, ONE, ZERO)], + final_assignments: vec![(heap.alloc_str_for_test("ma3"), INT_32_TYPE, ONE, ZERO)], }, Statement::IfElse { - condition: Expression::var_name(heap.alloc_str_for_test("ma2"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("ma2"), INT_32_TYPE), s1: vec![], s2: vec![], - final_assignments: vec![(heap.alloc_str_for_test("ma4"), INT_TYPE, ZERO, ONE)], + final_assignments: vec![(heap.alloc_str_for_test("ma4"), INT_32_TYPE, ZERO, ONE)], }, Statement::binary( heap.alloc_str_for_test("r1"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("ma1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("ma2"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("ma1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("ma2"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("r2"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("ma3"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("ma4"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("ma3"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("ma4"), INT_32_TYPE), ), Statement::binary(heap.alloc_str_for_test("r3"), BinaryOperator::NE, ONE, ZERO), Statement::binary(heap.alloc_str_for_test("r4"), BinaryOperator::XOR, ONE, ZERO), @@ -818,11 +818,11 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("r6"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("r5"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("r3"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("r5"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("r3"), INT_32_TYPE), ), ], - Expression::var_name(heap.alloc_str_for_test("r6"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("r6"), INT_32_TYPE), heap, table, r#"__$foo(); @@ -851,67 +851,67 @@ return 1;"#, Expression::int(3), ), Statement::IfElse { - condition: Expression::var_name(PStr::LOWER_B, INT_TYPE), + condition: Expression::var_name(PStr::LOWER_B, INT_32_TYPE), s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("foo")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), - arguments: vec![Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }], s2: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("bar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), - arguments: vec![Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }], final_assignments: vec![], }, Statement::IfElse { - condition: Expression::var_name(PStr::LOWER_B, INT_TYPE), + condition: Expression::var_name(PStr::LOWER_B, INT_32_TYPE), s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("foo")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), - arguments: vec![Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("a1")), }], s2: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("bar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), - arguments: vec![Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("a2")), }], final_assignments: vec![( heap.alloc_str_for_test("ma1"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a2"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a2"), INT_32_TYPE), )], }, Statement::IfElse { - condition: Expression::var_name(PStr::LOWER_B, INT_TYPE), + condition: Expression::var_name(PStr::LOWER_B, INT_32_TYPE), s1: vec![], s2: vec![], final_assignments: vec![( heap.alloc_str_for_test("ma2"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a0"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a0"), INT_32_TYPE), )], }, ], - Expression::var_name(heap.alloc_str_for_test("ma2"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("ma2"), INT_32_TYPE), heap, table, r#"if (b: int) { @@ -951,7 +951,7 @@ return 6;"#, invert_condition: false, statements: vec![Statement::Break(Expression::var_name( heap.alloc_str_for_test("n"), - INT_TYPE, + INT_32_TYPE, ))], }], ZERO, @@ -966,7 +966,7 @@ return 6;"#, invert_condition: true, statements: vec![Statement::Break(Expression::var_name( heap.alloc_str_for_test("n"), - INT_TYPE, + INT_32_TYPE, ))], }], ZERO, @@ -977,7 +977,7 @@ return 6;"#, let heap = &mut Heap::new(); assert_correctly_optimized( vec![Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), invert_condition: false, statements: vec![], }], @@ -997,29 +997,29 @@ return 6;"#, vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(4), - loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_32_TYPE), }], statements: vec![ Statement::binary( heap.alloc_str_for_test("is_zero"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_32_TYPE), invert_condition: false, statements: vec![Statement::Break(Expression::var_name( heap.alloc_str_for_test("n"), - INT_TYPE, + INT_32_TYPE, ))], }, Statement::binary( heap.alloc_str_for_test("_tmp_n"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ONE, ), ], @@ -1035,35 +1035,35 @@ return 6;"#, vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(4), - loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_32_TYPE), }], statements: vec![ Statement::binary( heap.alloc_str_for_test("is_zero"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_32_TYPE), invert_condition: false, statements: vec![Statement::Break(Expression::var_name( heap.alloc_str_for_test("n"), - INT_TYPE, + INT_32_TYPE, ))], }, Statement::binary( heap.alloc_str_for_test("_tmp_n"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ONE, ), ], - break_collector: Some(VariableName { name: PStr::LOWER_B, type_: INT_TYPE }), + break_collector: Some(VariableName { name: PStr::LOWER_B, type_: INT_32_TYPE }), }], - Expression::var_name(PStr::LOWER_B, INT_TYPE), + Expression::var_name(PStr::LOWER_B, INT_32_TYPE), heap, table, "\nreturn 0;", @@ -1073,29 +1073,29 @@ return 6;"#, vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), - loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_32_TYPE), }], statements: vec![ Statement::binary( heap.alloc_str_for_test("is_zero"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_32_TYPE), invert_condition: false, statements: vec![Statement::Break(Expression::var_name( heap.alloc_str_for_test("n"), - INT_TYPE, + INT_32_TYPE, ))], }, Statement::binary( heap.alloc_str_for_test("_tmp_n"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ONE, ), ], @@ -1121,7 +1121,7 @@ return 0;"#, vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), loop_value: Expression::int(10), }], @@ -1129,21 +1129,21 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("is_zero"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_32_TYPE), invert_condition: true, statements: vec![Statement::Break(Expression::var_name( heap.alloc_str_for_test("n"), - INT_TYPE, + INT_32_TYPE, ))], }, Statement::binary( heap.alloc_str_for_test("_tmp_n"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ONE, ), ], @@ -1159,13 +1159,13 @@ return 0;"#, vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), - loop_value: Expression::var_name(heap.alloc_str_for_test("t"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("t"), INT_32_TYPE), }], statements: vec![Statement::Break(Expression::var_name( heap.alloc_str_for_test("n"), - INT_TYPE, + INT_32_TYPE, ))], break_collector: None, }], @@ -1179,17 +1179,20 @@ return 0;"#, vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), - loop_value: Expression::var_name(heap.alloc_str_for_test("t"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("t"), INT_32_TYPE), }], statements: vec![Statement::Break(Expression::var_name( heap.alloc_str_for_test("n"), - INT_TYPE, + INT_32_TYPE, ))], - break_collector: Some(VariableName { name: heap.alloc_str_for_test("v"), type_: INT_TYPE }), + break_collector: Some(VariableName { + name: heap.alloc_str_for_test("v"), + type_: INT_32_TYPE, + }), }], - Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), heap, table, "\nreturn 10;", @@ -1199,14 +1202,17 @@ return 0;"#, vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), loop_value: Expression::int(11), }], statements: vec![], - break_collector: Some(VariableName { name: heap.alloc_str_for_test("v"), type_: INT_TYPE }), + break_collector: Some(VariableName { + name: heap.alloc_str_for_test("v"), + type_: INT_32_TYPE, + }), }], - Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), heap, table, r#"let n: int = 11; @@ -1223,8 +1229,8 @@ return (v: int);"#, statements: vec![Statement::binary( PStr::LOWER_A, BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("v1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("v2"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("v1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("v2"), INT_32_TYPE), )], break_collector: None, }], diff --git a/crates/samlang-optimization/src/dead_code_elimination_tests.rs b/crates/samlang-optimization/src/dead_code_elimination_tests.rs index a49cd51a..7690fcc1 100644 --- a/crates/samlang-optimization/src/dead_code_elimination_tests.rs +++ b/crates/samlang-optimization/src/dead_code_elimination_tests.rs @@ -7,7 +7,7 @@ mod tests { hir::{BinaryOperator, UnaryOperator}, mir::{ Callee, Expression, Function, FunctionName, FunctionNameExpression, GenenalLoopVariable, - Statement, SymbolTable, Type, VariableName, INT_TYPE, ONE, ZERO, + Statement, SymbolTable, Type, VariableName, INT_32_TYPE, ONE, ZERO, }, }; use samlang_heap::{Heap, PStr}; @@ -20,7 +20,7 @@ mod tests { &[Statement::Unary { name: heap.alloc_str_for_test("uu"), operator: UnaryOperator::Not, - operand: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + operand: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), }], &mut HashSet::new(), ); @@ -36,7 +36,7 @@ mod tests { let mut f = Function { name: FunctionName::new_for_test(PStr::LOWER_A), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: stmts, return_value, }; @@ -61,23 +61,23 @@ mod tests { Statement::binary(heap.alloc_str_for_test("p"), BinaryOperator::PLUS, ZERO, ONE), Statement::IndexedAccess { name: PStr::LOWER_I, - type_: INT_TYPE, - pointer_expression: Expression::var_name(heap.alloc_str_for_test("p"), INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(heap.alloc_str_for_test("p"), INT_32_TYPE), index: 3, }, Statement::StructInit { struct_variable_name: heap.alloc_str_for_test("s"), type_name: table.create_type_name_for_test(heap.alloc_str_for_test("S")), - expression_list: vec![Expression::var_name(heap.alloc_str_for_test("p"), INT_TYPE)], + expression_list: vec![Expression::var_name(heap.alloc_str_for_test("p"), INT_32_TYPE)], }, Statement::Cast { name: heap.alloc_str_for_test("i_am_definitely_unused"), - type_: INT_TYPE, - assigned_expression: Expression::var_name(heap.alloc_str_for_test("s"), INT_TYPE), + type_: INT_32_TYPE, + assigned_expression: Expression::var_name(heap.alloc_str_for_test("s"), INT_32_TYPE), }, Statement::LateInitDeclaration { name: heap.alloc_str_for_test("i_am_definitely_unused_2"), - type_: INT_TYPE, + type_: INT_32_TYPE, }, Statement::LateInitAssignment { name: heap.alloc_str_for_test("i_am_definitely_unused_2"), @@ -86,10 +86,10 @@ mod tests { Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("ff")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), - arguments: vec![Expression::var_name(heap.alloc_str_for_test("s"), INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(heap.alloc_str_for_test("s"), INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }, ]; @@ -104,7 +104,7 @@ mod tests { assert_correctly_optimized( stmts, - Expression::var_name(heap.alloc_str_for_test("ii"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("ii"), INT_32_TYPE), heap, table, r#"let u1 = 0 / 1; @@ -136,7 +136,7 @@ return (ii: int);"#, Statement::binary( heap.alloc_str_for_test("u1"), BinaryOperator::DIV, - Expression::var_name(heap.alloc_str_for_test("u0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("u0"), INT_32_TYPE), ONE, ), Statement::binary(heap.alloc_str_for_test("u2"), BinaryOperator::MOD, ZERO, ONE), @@ -144,66 +144,66 @@ return (ii: int);"#, Statement::binary(heap.alloc_str_for_test("p"), BinaryOperator::PLUS, ZERO, ONE), Statement::IndexedAccess { name: PStr::LOWER_I, - type_: INT_TYPE, - pointer_expression: Expression::var_name(heap.alloc_str_for_test("p"), INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(heap.alloc_str_for_test("p"), INT_32_TYPE), index: 3, }, Statement::IndexedAccess { name: heap.alloc_str_for_test("i1"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(heap.alloc_str_for_test("p"), INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(heap.alloc_str_for_test("p"), INT_32_TYPE), index: 3, }, Statement::StructInit { struct_variable_name: heap.alloc_str_for_test("s"), type_name: table.create_type_name_for_test(heap.alloc_str_for_test("S")), - expression_list: vec![Expression::var_name(heap.alloc_str_for_test("p"), INT_TYPE)], + expression_list: vec![Expression::var_name(heap.alloc_str_for_test("p"), INT_32_TYPE)], }, Statement::ClosureInit { closure_variable_name: heap.alloc_str_for_test("s"), closure_type_name: table.create_type_name_for_test(heap.alloc_str_for_test("Id")), function_name: FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("closure")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, - context: Expression::var_name(heap.alloc_str_for_test("b2"), INT_TYPE), + context: Expression::var_name(heap.alloc_str_for_test("b2"), INT_32_TYPE), }, Statement::ClosureInit { closure_variable_name: heap.alloc_str_for_test("s1"), closure_type_name: table.create_type_name_for_test(heap.alloc_str_for_test("Id")), function_name: FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("closure")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, - context: Expression::var_name(heap.alloc_str_for_test("b2"), INT_TYPE), + context: Expression::var_name(heap.alloc_str_for_test("b2"), INT_32_TYPE), }, Statement::Cast { name: heap.alloc_str_for_test("s2"), - type_: INT_TYPE, - assigned_expression: Expression::var_name(heap.alloc_str_for_test("s1"), INT_TYPE), + type_: INT_32_TYPE, + assigned_expression: Expression::var_name(heap.alloc_str_for_test("s1"), INT_32_TYPE), }, - Statement::LateInitDeclaration { name: heap.alloc_str_for_test("s3"), type_: INT_TYPE }, + Statement::LateInitDeclaration { name: heap.alloc_str_for_test("s3"), type_: INT_32_TYPE }, Statement::LateInitAssignment { name: heap.alloc_str_for_test("s3"), - assigned_expression: Expression::var_name(heap.alloc_str_for_test("s1"), INT_TYPE), + assigned_expression: Expression::var_name(heap.alloc_str_for_test("s1"), INT_32_TYPE), }, - Statement::LateInitDeclaration { name: heap.alloc_str_for_test("s4"), type_: INT_TYPE }, + Statement::LateInitDeclaration { name: heap.alloc_str_for_test("s4"), type_: INT_32_TYPE }, Statement::LateInitAssignment { name: heap.alloc_str_for_test("s4"), - assigned_expression: Expression::var_name(heap.alloc_str_for_test("s1"), INT_TYPE), + assigned_expression: Expression::var_name(heap.alloc_str_for_test("s1"), INT_32_TYPE), }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("ff")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![ - Expression::var_name(heap.alloc_str_for_test("i1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("s1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("s2"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("s3"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("s1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("s2"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("s3"), INT_32_TYPE), ], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, ], @@ -233,23 +233,23 @@ return 0;"#, vec![ Statement::binary(PStr::LOWER_B, BinaryOperator::EQ, ZERO, ONE), Statement::IfElse { - condition: Expression::var_name(PStr::LOWER_B, INT_TYPE), + condition: Expression::var_name(PStr::LOWER_B, INT_32_TYPE), s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("s1")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], s2: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("s1")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], final_assignments: vec![], @@ -277,34 +277,34 @@ return 0;"#, vec![ Statement::binary(PStr::LOWER_B, BinaryOperator::EQ, ZERO, ONE), Statement::IfElse { - condition: Expression::var_name(PStr::LOWER_B, INT_TYPE), + condition: Expression::var_name(PStr::LOWER_B, INT_32_TYPE), s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("s1")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("a1")), }], s2: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("s1")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("a2")), }], final_assignments: vec![( heap.alloc_str_for_test("ma"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a2"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a2"), INT_32_TYPE), )], }, ], - Expression::var_name(heap.alloc_str_for_test("ma"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("ma"), INT_32_TYPE), heap, table, r#"let b = 0 == 1; @@ -329,27 +329,27 @@ return (ma: int);"#, vec![ Statement::binary(PStr::LOWER_B, BinaryOperator::EQ, ZERO, ONE), Statement::IfElse { - condition: Expression::var_name(PStr::LOWER_B, INT_TYPE), + condition: Expression::var_name(PStr::LOWER_B, INT_32_TYPE), s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("s1")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], s2: vec![Statement::Call { - callee: Callee::Variable(VariableName::new(heap.alloc_str_for_test("s1"), INT_TYPE)), + callee: Callee::Variable(VariableName::new(heap.alloc_str_for_test("s1"), INT_32_TYPE)), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], final_assignments: vec![( heap.alloc_str_for_test("ma"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a2"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a2"), INT_32_TYPE), )], }, ], @@ -375,30 +375,30 @@ return 0;"#, vec![ Statement::binary(PStr::LOWER_B, BinaryOperator::EQ, ZERO, ONE), Statement::IfElse { - condition: Expression::var_name(PStr::LOWER_B, INT_TYPE), + condition: Expression::var_name(PStr::LOWER_B, INT_32_TYPE), s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("s1")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("a1")), }], s2: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("s1")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("a2")), }], final_assignments: vec![( heap.alloc_str_for_test("ma"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("a1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("a2"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("a1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("a2"), INT_32_TYPE), )], }, ], @@ -424,7 +424,7 @@ return 0;"#, vec![ Statement::binary(PStr::LOWER_B, BinaryOperator::EQ, ZERO, ONE), Statement::IfElse { - condition: Expression::var_name(PStr::LOWER_B, INT_TYPE), + condition: Expression::var_name(PStr::LOWER_B, INT_32_TYPE), s1: vec![], s2: vec![], final_assignments: vec![], @@ -446,7 +446,7 @@ return 0;"#, vec![ Statement::binary(PStr::LOWER_B, BinaryOperator::EQ, ZERO, ONE), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_32_TYPE), invert_condition: false, statements: vec![], }, @@ -468,13 +468,13 @@ return 0;"#, loop_variables: vec![ GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), - loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_32_TYPE), }, GenenalLoopVariable { name: heap.alloc_str_for_test("unused"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), loop_value: Expression::int(20), }, @@ -483,60 +483,63 @@ return 0;"#, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("s1")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("a2")), }, Statement::Call { - callee: Callee::Variable(VariableName::new(heap.alloc_str_for_test("s1"), INT_TYPE)), + callee: Callee::Variable(VariableName::new(heap.alloc_str_for_test("s1"), INT_32_TYPE)), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::While { loop_variables: vec![], statements: vec![], break_collector: None }, Statement::binary( heap.alloc_str_for_test("is_zero"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::IfElse { - condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_32_TYPE), s1: vec![ Statement::IndexedAccess { name: heap.alloc_str_for_test("s"), - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: ZERO, index: 0, }, Statement::StructInit { struct_variable_name: heap.alloc_str_for_test("s"), type_name: table.create_type_name_for_test(heap.alloc_str_for_test("S")), - expression_list: vec![Expression::var_name(heap.alloc_str_for_test("p"), INT_TYPE)], + expression_list: vec![Expression::var_name( + heap.alloc_str_for_test("p"), + INT_32_TYPE, + )], }, Statement::ClosureInit { closure_variable_name: heap.alloc_str_for_test("s"), closure_type_name: table.create_type_name_for_test(heap.alloc_str_for_test("Id")), function_name: FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("closure")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, - context: Expression::var_name(heap.alloc_str_for_test("b2"), INT_TYPE), + context: Expression::var_name(heap.alloc_str_for_test("b2"), INT_32_TYPE), }, ], s2: vec![Statement::binary( heap.alloc_str_for_test("s2_n"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ONE, )], final_assignments: vec![( heap.alloc_str_for_test("_tmp_n"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("s2_n"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("s2_n"), INT_32_TYPE), )], }, ], @@ -575,13 +578,13 @@ return 0;"#, loop_variables: vec![ GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), - loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_32_TYPE), }, GenenalLoopVariable { name: heap.alloc_str_for_test("n1"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), loop_value: Expression::int(20), }, @@ -590,16 +593,19 @@ return 0;"#, Statement::binary( heap.alloc_str_for_test("is_zero"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_32_TYPE), invert_condition: false, statements: vec![Statement::Break(ZERO)], }, ], - break_collector: Some(VariableName { name: heap.alloc_str_for_test("v"), type_: INT_TYPE }), + break_collector: Some(VariableName { + name: heap.alloc_str_for_test("v"), + type_: INT_32_TYPE, + }), }], ZERO, heap, @@ -626,19 +632,22 @@ return 0;"#, vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), - loop_value: Expression::var_name(heap.alloc_str_for_test("n1"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("n1"), INT_32_TYPE), }], statements: vec![Statement::binary( heap.alloc_str_for_test("n1"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, )], - break_collector: Some(VariableName { name: heap.alloc_str_for_test("v"), type_: INT_TYPE }), + break_collector: Some(VariableName { + name: heap.alloc_str_for_test("v"), + type_: INT_32_TYPE, + }), }], - Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), heap, table, r#"let n: int = 10; @@ -660,19 +669,19 @@ return (v: int);"#, vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), loop_value: Expression::int(11), }], statements: vec![Statement::binary( heap.alloc_str_for_test("n1"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, )], break_collector: None, }], - Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), heap, table, r#"while (true) { diff --git a/crates/samlang-optimization/src/inlining.rs b/crates/samlang-optimization/src/inlining.rs index 7c91f180..5b2c9a38 100644 --- a/crates/samlang-optimization/src/inlining.rs +++ b/crates/samlang-optimization/src/inlining.rs @@ -4,7 +4,7 @@ use samlang_ast::{ hir::BinaryOperator, mir::{ Binary, Callee, Expression, Function, FunctionName, FunctionNameExpression, - GenenalLoopVariable, Statement, Type, VariableName, INT_TYPE, ZERO, + GenenalLoopVariable, Statement, Type, VariableName, INT_32_TYPE, ZERO, }, }; use samlang_heap::{Heap, PStr}; @@ -86,7 +86,7 @@ mod estimator { hir::BinaryOperator, mir::{ Callee, Function, FunctionName, FunctionNameExpression, GenenalLoopVariable, Statement, - SymbolTable, Type, INT_TYPE, ZERO, + SymbolTable, Type, INT_32_TYPE, ZERO, }, }; use samlang_heap::PStr; @@ -98,11 +98,11 @@ mod estimator { let actual = super::estimate_fn_inline_cost(&Function { name: FunctionName::new_for_test(PStr::EMPTY), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![ Statement::IndexedAccess { name: PStr::EMPTY, - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: ZERO, index: 2, }, @@ -117,24 +117,24 @@ mod estimator { closure_type_name: table.create_type_name_for_test(PStr::EMPTY), function_name: FunctionNameExpression { name: FunctionName::new_for_test(PStr::EMPTY), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, context: ZERO, }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(PStr::EMPTY), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![ZERO, ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::IfElse { condition: ZERO, s1: vec![], s2: vec![], - final_assignments: vec![(PStr::EMPTY, INT_TYPE, ZERO, ZERO)], + final_assignments: vec![(PStr::EMPTY, INT_32_TYPE, ZERO, ZERO)], }, Statement::IfElse { condition: ZERO, @@ -150,15 +150,15 @@ mod estimator { Statement::While { loop_variables: vec![GenenalLoopVariable { name: PStr::EMPTY, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO, }], statements: vec![Statement::binary(PStr::EMPTY, BinaryOperator::PLUS, ZERO, ZERO)], break_collector: None, }, - Statement::Cast { name: PStr::EMPTY, type_: INT_TYPE, assigned_expression: ZERO }, - Statement::LateInitDeclaration { name: PStr::EMPTY, type_: INT_TYPE }, + Statement::Cast { name: PStr::EMPTY, type_: INT_32_TYPE, assigned_expression: ZERO }, + Statement::LateInitDeclaration { name: PStr::EMPTY, type_: INT_32_TYPE }, Statement::LateInitAssignment { name: PStr::EMPTY, assigned_expression: ZERO }, ], return_value: ZERO, @@ -221,12 +221,12 @@ fn inline_rewrite_stmt( ) -> Statement { match stmt { Statement::Unary { name, operator, operand } => Statement::Unary { - name: bind_with_mangled_name(cx, heap, prefix, name, &INT_TYPE), + name: bind_with_mangled_name(cx, heap, prefix, name, &INT_32_TYPE), operator: *operator, operand: inline_rewrite_expr(operand, cx), }, Statement::Binary(Binary { name, operator, e1, e2 }) => Statement::Binary(Binary { - name: bind_with_mangled_name(cx, heap, prefix, name, &INT_TYPE), + name: bind_with_mangled_name(cx, heap, prefix, name, &INT_32_TYPE), operator: *operator, e1: inline_rewrite_expr(e1, cx), e2: inline_rewrite_expr(e2, cx), diff --git a/crates/samlang-optimization/src/inlining_tests.rs b/crates/samlang-optimization/src/inlining_tests.rs index 851df166..39b14635 100644 --- a/crates/samlang-optimization/src/inlining_tests.rs +++ b/crates/samlang-optimization/src/inlining_tests.rs @@ -6,7 +6,7 @@ mod tests { hir::{BinaryOperator, UnaryOperator}, mir::{ Callee, Expression, Function, FunctionName, FunctionNameExpression, GenenalLoopVariable, - Statement, SymbolTable, Type, VariableName, INT_TYPE, ONE, ZERO, + Statement, SymbolTable, Type, VariableName, INT_32_TYPE, ONE, ZERO, }, }; use samlang_heap::{Heap, PStr}; @@ -38,43 +38,43 @@ mod tests { stmts.push(Statement::While { loop_variables: vec![GenenalLoopVariable { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO, }], statements: vec![ Statement::IndexedAccess { name: heap.alloc_str_for_test("i0"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 2, }, Statement::binary( heap.alloc_str_for_test("b0"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), Expression::int(3), ), Statement::StructInit { struct_variable_name: heap.alloc_str_for_test("s"), type_name: table.create_type_name_for_test(heap.alloc_str_for_test("SS")), expression_list: vec![ - Expression::var_name(heap.alloc_str_for_test("i1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b3"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b3"), INT_32_TYPE), ], }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fff")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![ - Expression::var_name(heap.alloc_str_for_test("i1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b3"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b3"), INT_32_TYPE), ], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::IfElse { @@ -82,13 +82,13 @@ mod tests { s1: vec![Statement::binary( PStr::LOWER_A, BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_A, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), Expression::int(3), )], s2: vec![Statement::binary( PStr::LOWER_A, BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_A, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), Expression::int(3), )], final_assignments: vec![], @@ -97,7 +97,7 @@ mod tests { condition: ZERO, s1: vec![], s2: vec![], - final_assignments: vec![(PStr::LOWER_A, INT_TYPE, ZERO, ZERO)], + final_assignments: vec![(PStr::LOWER_A, INT_32_TYPE, ZERO, ZERO)], }, Statement::SingleIf { condition: ZERO, @@ -105,14 +105,14 @@ mod tests { statements: vec![Statement::binary( PStr::LOWER_A, BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_A, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), Expression::int(3), )], }, Statement::binary( PStr::LOWER_A, BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_A, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), Expression::int(3), ), ], @@ -131,7 +131,7 @@ mod tests { vec![Function { name: FunctionName::new_for_test(PStr::LOWER_A), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: big_stmts(heap, table), return_value: ZERO, }], @@ -143,14 +143,14 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("loop")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("loop")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], return_value: ZERO, @@ -158,7 +158,7 @@ mod tests { Function { name: FunctionName::new_for_test(PStr::LOWER_A), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: big_stmts(heap, table), return_value: ZERO, }, @@ -177,64 +177,64 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("factorial")), parameters: vec![heap.alloc_str_for_test("n"), heap.alloc_str_for_test("acc")], - type_: Type::new_fn_unwrapped(vec![INT_TYPE, INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE, INT_32_TYPE], INT_32_TYPE), body: vec![ Statement::binary( PStr::LOWER_C, BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::IfElse { - condition: Expression::var_name(PStr::LOWER_C, INT_TYPE), + condition: Expression::var_name(PStr::LOWER_C, INT_32_TYPE), s1: vec![], s2: vec![ Statement::binary( heap.alloc_str_for_test("n1"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ONE, ), Statement::binary( heap.alloc_str_for_test("acc1"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("acc"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("acc"), INT_32_TYPE), ), Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("factorial")), - type_: Type::new_fn_unwrapped(vec![INT_TYPE, INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE, INT_32_TYPE], INT_32_TYPE), }), arguments: vec![ - Expression::var_name(heap.alloc_str_for_test("n1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("acc1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("acc1"), INT_32_TYPE), ], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("v")), }, ], final_assignments: vec![( heap.alloc_str_for_test("fa"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("acc"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("acc"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), )], }, ], - return_value: Expression::var_name(heap.alloc_str_for_test("fa"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("fa"), INT_32_TYPE), }, Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("loop")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("loop")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], return_value: ZERO, @@ -242,39 +242,39 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("insanelyBigFunction")), parameters: vec![PStr::LOWER_A], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![ Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("bb")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("cc")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("moveMove")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { - callee: Callee::Variable(VariableName::new(PStr::LOWER_A, INT_TYPE)), + callee: Callee::Variable(VariableName::new(PStr::LOWER_A, INT_32_TYPE)), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, ] @@ -282,10 +282,10 @@ mod tests { .chain((0..10).map(|_| Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("non-existing-function")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, })) .collect(), @@ -294,20 +294,24 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("moveMove")), parameters: vec![PStr::LOWER_A], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![ Statement::Unary { name: heap.alloc_str_for_test("u0"), operator: UnaryOperator::Not, - operand: Expression::var_name(PStr::LOWER_A, INT_TYPE), + operand: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), }, - Statement::Cast { name: PStr::UNDERSCORE, type_: INT_TYPE, assigned_expression: ZERO }, - Statement::LateInitDeclaration { name: PStr::LOWER_B, type_: INT_TYPE }, + Statement::Cast { + name: PStr::UNDERSCORE, + type_: INT_32_TYPE, + assigned_expression: ZERO, + }, + Statement::LateInitDeclaration { name: PStr::LOWER_B, type_: INT_32_TYPE }, Statement::LateInitAssignment { name: PStr::LOWER_B, assigned_expression: ZERO }, Statement::IndexedAccess { name: PStr::LOWER_C, - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 0, }, ], @@ -316,19 +320,19 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("bb")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::IfElse { condition: ZERO, s1: vec![Statement::IndexedAccess { name: PStr::LOWER_C, - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 0, }], s2: vec![Statement::IndexedAccess { name: PStr::LOWER_C, - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 0, }], final_assignments: vec![], @@ -338,11 +342,11 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("cc")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { - callee: Callee::Variable(VariableName::new(PStr::LOWER_A, INT_TYPE)), + callee: Callee::Variable(VariableName::new(PStr::LOWER_A, INT_32_TYPE)), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], return_value: ZERO, @@ -423,19 +427,19 @@ function __$insanelyBigFunction(a: int): int { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::IfElse { - condition: Expression::var_name(heap.alloc_str_for_test("bar"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("bar"), INT_32_TYPE), s1: vec![], s2: vec![ Statement::binary(heap.alloc_str_for_test("vvv"), BinaryOperator::PLUS, ZERO, ZERO), Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, ], @@ -446,17 +450,17 @@ function __$insanelyBigFunction(a: int): int { Function { name: FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("v")), }], - return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), }, ], heap, @@ -502,19 +506,19 @@ function __$main(): int { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("bar"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("bar"), INT_32_TYPE), invert_condition: false, statements: vec![ Statement::binary(heap.alloc_str_for_test("vvv"), BinaryOperator::PLUS, ZERO, ZERO), Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, ], @@ -524,17 +528,17 @@ function __$main(): int { Function { name: FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("v")), }], - return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), }, ], heap, @@ -574,24 +578,24 @@ function __$main(): int { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::IfElse { - condition: Expression::var_name(heap.alloc_str_for_test("bar"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("bar"), INT_32_TYPE), s1: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], s2: vec![], final_assignments: vec![( PStr::LOWER_B, - INT_TYPE, + INT_32_TYPE, ZERO, - Expression::var_name(PStr::LOWER_A, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), )], }], return_value: ZERO, @@ -599,17 +603,17 @@ function __$main(): int { Function { name: FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("v")), }], - return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), }, ], heap, @@ -673,14 +677,14 @@ function __$main(): int { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), parameters: vec![heap.alloc_str_for_test("bar"), heap.alloc_str_for_test("baz")], - type_: Type::new_fn_unwrapped(vec![INT_TYPE, INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE, INT_32_TYPE], INT_32_TYPE), body: vec![ Statement::StructInit { struct_variable_name: heap.alloc_str_for_test("ff"), type_name: table.create_type_name_for_test(heap.alloc_str_for_test("FF")), expression_list: vec![ - Expression::var_name(heap.alloc_str_for_test("bar"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("baz"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("bar"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("baz"), INT_32_TYPE), ], }, Statement::ClosureInit { @@ -688,7 +692,7 @@ function __$main(): int { closure_type_name: table.create_type_name_for_test(heap.alloc_str_for_test("SS")), function_name: FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("aaa")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, context: ZERO, }, @@ -699,14 +703,14 @@ function __$main(): int { Function { name: FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![ONE, ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("v")), }], return_value: ZERO, @@ -742,25 +746,25 @@ function __$main(): int { vec![Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), - loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_32_TYPE), }], statements: vec![Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), invert_condition: false, statements: vec![Statement::Break(ZERO)], }], break_collector: Some(VariableName { name: heap.alloc_str_for_test("v"), - type_: INT_TYPE, + type_: INT_32_TYPE, }), }], - return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), }], heap, table, @@ -781,41 +785,41 @@ function __$main(): int { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), - loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_32_TYPE), }], statements: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("_tmp_n")), }], break_collector: None, }], - return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), }, Function { name: FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("v")), }], - return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), }, ], heap, @@ -867,31 +871,31 @@ function __$main(): int { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::While { loop_variables: vec![], statements: vec![Statement::Break(ZERO)], break_collector: Some(VariableName { name: heap.alloc_str_for_test("v"), - type_: INT_TYPE, + type_: INT_32_TYPE, }), }], - return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), }, Function { name: FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fooBar")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("v")), }], - return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + return_value: Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), }, ], heap, diff --git a/crates/samlang-optimization/src/lib.rs b/crates/samlang-optimization/src/lib.rs index a4fe67ef..c519883c 100644 --- a/crates/samlang-optimization/src/lib.rs +++ b/crates/samlang-optimization/src/lib.rs @@ -115,7 +115,7 @@ pub fn optimize_sources( #[cfg(test)] mod tests { use pretty_assertions::assert_eq; - use samlang_ast::mir::{Function, FunctionName, Sources, SymbolTable, Type, INT_TYPE, ZERO}; + use samlang_ast::mir::{Function, FunctionName, Sources, SymbolTable, Type, INT_32_TYPE, ZERO}; use samlang_heap::{Heap, PStr}; fn sources() -> Sources { @@ -128,7 +128,7 @@ mod tests { functions: vec![Function { name: FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![], return_value: ZERO, }], diff --git a/crates/samlang-optimization/src/local_value_numbering_tests.rs b/crates/samlang-optimization/src/local_value_numbering_tests.rs index 51342dff..acb9db34 100644 --- a/crates/samlang-optimization/src/local_value_numbering_tests.rs +++ b/crates/samlang-optimization/src/local_value_numbering_tests.rs @@ -7,7 +7,7 @@ mod tests { hir::BinaryOperator, mir::{ Callee, Expression, Function, FunctionName, FunctionNameExpression, GenenalLoopVariable, - Statement, SymbolTable, Type, VariableName, INT_TYPE, ONE, ZERO, + Statement, SymbolTable, Type, VariableName, INT_32_TYPE, ONE, ZERO, }, }; use samlang_heap::{Heap, PStr}; @@ -22,7 +22,7 @@ mod tests { let mut f = Function { name: FunctionName::new_for_test(PStr::LOWER_A), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: stmts, return_value, }; @@ -44,40 +44,40 @@ mod tests { vec![ Statement::IndexedAccess { name: heap.alloc_str_for_test("i0"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 2, }, Statement::IndexedAccess { name: heap.alloc_str_for_test("i1"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 2, }, Statement::binary( heap.alloc_str_for_test("b0"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), Expression::int(3), ), Statement::binary( heap.alloc_str_for_test("b1"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), Expression::int(3), ), Statement::binary( heap.alloc_str_for_test("b3"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("i1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b1"), INT_32_TYPE), ), Statement::Cast { name: heap.alloc_str_for_test("c1"), - type_: INT_TYPE, + type_: INT_32_TYPE, assigned_expression: ZERO, }, - Statement::LateInitDeclaration { name: heap.alloc_str_for_test("c2"), type_: INT_TYPE }, + Statement::LateInitDeclaration { name: heap.alloc_str_for_test("c2"), type_: INT_32_TYPE }, Statement::LateInitAssignment { name: heap.alloc_str_for_test("c2"), assigned_expression: ZERO, @@ -86,9 +86,9 @@ mod tests { struct_variable_name: heap.alloc_str_for_test("s"), type_name: table.create_type_name_for_test(heap.alloc_str_for_test("S")), expression_list: vec![ - Expression::var_name(heap.alloc_str_for_test("i1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b3"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b3"), INT_32_TYPE), ], }, Statement::ClosureInit { @@ -96,31 +96,31 @@ mod tests { closure_type_name: table.create_type_name_for_test(heap.alloc_str_for_test("S")), function_name: FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_A), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, context: ZERO, }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("fff")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![ - Expression::var_name(heap.alloc_str_for_test("i1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("b3"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("i1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("b3"), INT_32_TYPE), ], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { - callee: Callee::Variable(VariableName::new(heap.alloc_str_for_test("fff"), INT_TYPE)), + callee: Callee::Variable(VariableName::new(heap.alloc_str_for_test("fff"), INT_32_TYPE)), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, ], - Expression::var_name(heap.alloc_str_for_test("ss"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("ss"), INT_32_TYPE), heap, table, r#"let i0: int = (a: int)[2]; @@ -146,8 +146,8 @@ return (ss: int);"#, vec![ Statement::IndexedAccess { name: heap.alloc_str_for_test("i0"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 2, }, Statement::IfElse { @@ -155,28 +155,28 @@ return (ss: int);"#, s1: vec![ Statement::IndexedAccess { name: heap.alloc_str_for_test("i1"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 2, }, Statement::IndexedAccess { name: heap.alloc_str_for_test("i3"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(heap.alloc_str_for_test("i1"), INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(heap.alloc_str_for_test("i1"), INT_32_TYPE), index: 1, }, ], s2: vec![ Statement::IndexedAccess { name: heap.alloc_str_for_test("i2"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 2, }, Statement::IndexedAccess { name: heap.alloc_str_for_test("i4"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(heap.alloc_str_for_test("i2"), INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(heap.alloc_str_for_test("i2"), INT_32_TYPE), index: 1, }, ], @@ -184,8 +184,8 @@ return (ss: int);"#, }, Statement::IndexedAccess { name: heap.alloc_str_for_test("i5"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(heap.alloc_str_for_test("i0"), INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(heap.alloc_str_for_test("i0"), INT_32_TYPE), index: 1, }, ], @@ -206,8 +206,8 @@ return 0;"#, vec![ Statement::IndexedAccess { name: heap.alloc_str_for_test("i0"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 2, }, Statement::IfElse { @@ -215,36 +215,36 @@ return 0;"#, s1: vec![ Statement::IndexedAccess { name: heap.alloc_str_for_test("i1"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 2, }, Statement::IndexedAccess { name: heap.alloc_str_for_test("i3"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(heap.alloc_str_for_test("i1"), INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(heap.alloc_str_for_test("i1"), INT_32_TYPE), index: 1, }, ], s2: vec![ Statement::IndexedAccess { name: heap.alloc_str_for_test("i2"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_A, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), index: 2, }, Statement::IndexedAccess { name: heap.alloc_str_for_test("i4"), - type_: INT_TYPE, - pointer_expression: Expression::var_name(heap.alloc_str_for_test("i2"), INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(heap.alloc_str_for_test("i2"), INT_32_TYPE), index: 1, }, ], final_assignments: vec![( heap.alloc_str_for_test("bar"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("i1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("i2"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("i1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("i2"), INT_32_TYPE), )], }, ], @@ -273,33 +273,33 @@ return 0;"#, vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), - loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_32_TYPE), }], statements: vec![ Statement::binary( heap.alloc_str_for_test("is_zero"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::IfElse { - condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_32_TYPE), s1: vec![], s2: vec![Statement::binary( heap.alloc_str_for_test("s2_n"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ONE, )], final_assignments: vec![ - (PStr::LOWER_C, INT_TYPE, ZERO, ONE), + (PStr::LOWER_C, INT_32_TYPE, ZERO, ONE), ( heap.alloc_str_for_test("_tmp_n"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("s2_n"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("s2_n"), INT_32_TYPE), ), ], }, @@ -340,40 +340,43 @@ return 0;"#, vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: heap.alloc_str_for_test("n"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(10), - loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("_tmp_n"), INT_32_TYPE), }], statements: vec![ Statement::binary( heap.alloc_str_for_test("is_zero"), BinaryOperator::EQ, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ZERO, ), Statement::IfElse { - condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("is_zero"), INT_32_TYPE), s1: vec![], s2: vec![Statement::binary( heap.alloc_str_for_test("s2_n"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), ONE, )], final_assignments: vec![ - (PStr::LOWER_C, INT_TYPE, ZERO, ONE), + (PStr::LOWER_C, INT_32_TYPE, ZERO, ONE), ( heap.alloc_str_for_test("_tmp_n"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("n"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("s2_n"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("n"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("s2_n"), INT_32_TYPE), ), ], }, ], - break_collector: Some(VariableName { name: heap.alloc_str_for_test("v"), type_: INT_TYPE }), + break_collector: Some(VariableName { + name: heap.alloc_str_for_test("v"), + type_: INT_32_TYPE, + }), }], - Expression::var_name(heap.alloc_str_for_test("v"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("v"), INT_32_TYPE), heap, table, r#"let n: int = 10; diff --git a/crates/samlang-optimization/src/loop_algebraic_optimization.rs b/crates/samlang-optimization/src/loop_algebraic_optimization.rs index 418f95d3..b7c31bb0 100644 --- a/crates/samlang-optimization/src/loop_algebraic_optimization.rs +++ b/crates/samlang-optimization/src/loop_algebraic_optimization.rs @@ -4,7 +4,7 @@ use super::loop_induction_analysis::{ }; use samlang_ast::{ hir::BinaryOperator, - mir::{Binary, Expression, Statement, INT_TYPE, ZERO}, + mir::{Binary, Expression, Statement, INT_32_TYPE, ZERO}, }; fn analyze_number_of_iterations_to_break_less_than_guard( @@ -129,7 +129,7 @@ pub(super) fn optimize( *break_collector.0, BinaryOperator::PLUS, relevant_general_induction_variable.initial_value, - Expression::var_name(increment_temporary, INT_TYPE), + Expression::var_name(increment_temporary, INT_32_TYPE), )), ]) } else { @@ -152,7 +152,7 @@ mod tests { }; use itertools::Itertools; use pretty_assertions::assert_eq; - use samlang_ast::mir::{Expression, Statement, SymbolTable, VariableName, INT_TYPE, ZERO}; + use samlang_ast::mir::{Expression, Statement, SymbolTable, VariableName, INT_32_TYPE, ZERO}; use samlang_heap::{Heap, PStr}; #[test] @@ -237,7 +237,7 @@ mod tests { OptimizableWhileLoop { basic_induction_variable_with_loop_guard: BasicInductionVariableWithLoopGuard { name: PStr::LOWER_I, - initial_value: Expression::var_name(PStr::LOWER_A, INT_TYPE), + initial_value: Expression::var_name(PStr::LOWER_A, INT_32_TYPE), increment_amount: PotentialLoopInvariantExpression::Int(0), guard_operator: GuardOperator::LT, guard_expression: PotentialLoopInvariantExpression::Int(0), @@ -343,7 +343,7 @@ mod tests { loop_variables_that_are_not_basic_induction_variables: vec![], derived_induction_variables: vec![], statements: vec![], - break_collector: Some((heap.alloc_str_for_test("bc"), INT_TYPE, Expression::int(3))), + break_collector: Some((heap.alloc_str_for_test("bc"), INT_32_TYPE, Expression::int(3))), }, heap, "let bc = 3 + 0;", @@ -364,8 +364,8 @@ mod tests { statements: vec![], break_collector: Some(( heap.alloc_str_for_test("bc"), - INT_TYPE, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + INT_32_TYPE, + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), )), }, heap, @@ -383,10 +383,10 @@ mod tests { }, general_induction_variables: vec![GeneralBasicInductionVariable { name: PStr::LOWER_J, - initial_value: Expression::var_name(heap.alloc_str_for_test("j_init"), INT_TYPE), + initial_value: Expression::var_name(heap.alloc_str_for_test("j_init"), INT_32_TYPE), increment_amount: PotentialLoopInvariantExpression::Var(VariableName::new( heap.alloc_str_for_test("outside"), - INT_TYPE, + INT_32_TYPE, )), }], loop_variables_that_are_not_basic_induction_variables: vec![], @@ -394,8 +394,8 @@ mod tests { statements: vec![], break_collector: Some(( heap.alloc_str_for_test("bc"), - INT_TYPE, - Expression::var_name(PStr::LOWER_J, INT_TYPE), + INT_32_TYPE, + Expression::var_name(PStr::LOWER_J, INT_32_TYPE), )), }, heap, @@ -413,10 +413,10 @@ mod tests { }, general_induction_variables: vec![GeneralBasicInductionVariable { name: PStr::LOWER_J, - initial_value: Expression::var_name(heap.alloc_str_for_test("j_init"), INT_TYPE), + initial_value: Expression::var_name(heap.alloc_str_for_test("j_init"), INT_32_TYPE), increment_amount: PotentialLoopInvariantExpression::Var(VariableName::new( heap.alloc_str_for_test("outside"), - INT_TYPE, + INT_32_TYPE, )), }], loop_variables_that_are_not_basic_induction_variables: vec![], @@ -424,8 +424,8 @@ mod tests { statements: vec![], break_collector: Some(( heap.alloc_str_for_test("bc"), - INT_TYPE, - Expression::var_name(heap.alloc_str_for_test("aa"), INT_TYPE), + INT_32_TYPE, + Expression::var_name(heap.alloc_str_for_test("aa"), INT_32_TYPE), )), }, heap, diff --git a/crates/samlang-optimization/src/loop_induction_analysis.rs b/crates/samlang-optimization/src/loop_induction_analysis.rs index b50ccae7..027ff413 100644 --- a/crates/samlang-optimization/src/loop_induction_analysis.rs +++ b/crates/samlang-optimization/src/loop_induction_analysis.rs @@ -671,7 +671,7 @@ mod tests { use super::*; use pretty_assertions::assert_eq; use samlang_ast::mir::{ - Callee, FunctionName, FunctionNameExpression, SymbolTable, INT_TYPE, ONE, ZERO, + Callee, FunctionName, FunctionNameExpression, SymbolTable, INT_32_TYPE, ONE, ZERO, }; #[test] @@ -731,7 +731,7 @@ mod tests { &PotentialLoopInvariantExpression::Int(1), &PotentialLoopInvariantExpression::Var(VariableName::new( heap.alloc_str_for_test("v"), - INT_TYPE + INT_32_TYPE )) ) .unwrap() @@ -743,7 +743,7 @@ mod tests { merge_invariant_multiplication_for_loop_optimization( &PotentialLoopInvariantExpression::Var(VariableName::new( heap.alloc_str_for_test("v"), - INT_TYPE + INT_32_TYPE )), &PotentialLoopInvariantExpression::Int(1), ) @@ -754,11 +754,11 @@ mod tests { assert!(merge_invariant_multiplication_for_loop_optimization( &PotentialLoopInvariantExpression::Var(VariableName::new( heap.alloc_str_for_test("v"), - INT_TYPE + INT_32_TYPE )), &PotentialLoopInvariantExpression::Var(VariableName::new( heap.alloc_str_for_test("v"), - INT_TYPE + INT_32_TYPE )), ) .is_none()); @@ -778,7 +778,7 @@ mod tests { base_name: PStr::LOWER_A, multiplier: PotentialLoopInvariantExpression::Var(VariableName::new( heap.alloc_str_for_test("vv"), - INT_TYPE + INT_32_TYPE )), immediate: PotentialLoopInvariantExpression::Int(1), } @@ -812,13 +812,13 @@ mod tests { &vec![ GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO }, GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO }, @@ -833,28 +833,28 @@ mod tests { &vec![ GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), }, GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }, ], &[ Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::StringName(PStr::LOWER_A) ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_J, INT_TYPE), + Expression::var_name(PStr::LOWER_J, INT_32_TYPE), Expression::StringName(PStr::LOWER_A), ) ], @@ -877,28 +877,28 @@ mod tests { &vec![ GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), }, GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }, ], &[ Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE, ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_J, INT_TYPE), + Expression::var_name(PStr::LOWER_J, INT_32_TYPE), Expression::int(3), ), ], @@ -955,80 +955,80 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_J, INT_TYPE), + Expression::var_name(PStr::LOWER_J, INT_32_TYPE), Expression::int(3), ), Statement::binary( heap.alloc_str_for_test("tmp_x"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), Expression::int(5), ), Statement::binary( heap.alloc_str_for_test("tmp_y"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE), Expression::int(6), ), Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_A), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), - arguments: vec![Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_A), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), - arguments: vec![Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }, Statement::binary( heap.alloc_str_for_test("tmp_z"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("tmp_y"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_y"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("tmp_useless_1"), BinaryOperator::MINUS, - Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("tmp_y"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_y"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("tmp_useless_2"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("tmp_useless_1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_useless_1"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("tmp_useless_3"), BinaryOperator::PLUS, ZERO, - Expression::var_name(heap.alloc_str_for_test("tmp_useless_1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_useless_1"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("tmp_useless_4"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_useless_1"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("tmp_useless_1"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_useless_1"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_useless_1"), INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("tmp_useless_6"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), - Expression::var_name(PStr::LOWER_J, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), + Expression::var_name(PStr::LOWER_J, INT_32_TYPE), ), ], &HashSet::from([ @@ -1058,14 +1058,14 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_J, INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("outside"), INT_TYPE), + Expression::var_name(PStr::LOWER_J, INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("outside"), INT_32_TYPE), ) ], &HashSet::from([ @@ -1089,13 +1089,13 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_J, INT_TYPE), + Expression::var_name(PStr::LOWER_J, INT_32_TYPE), Expression::StringName(heap.alloc_str_for_test("outside")), ) ], @@ -1122,13 +1122,13 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("outside"), INT_TYPE) + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("outside"), INT_32_TYPE) ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), ZERO, ) ], @@ -1158,14 +1158,14 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO, ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("outside"), INT_TYPE) + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("outside"), INT_32_TYPE) ) ], &HashSet::from([ @@ -1189,7 +1189,7 @@ mod tests { initial_value: ZERO, increment_amount: PotentialLoopInvariantExpression::Var(VariableName::new( heap.alloc_str_for_test("outside"), - INT_TYPE + INT_32_TYPE )), loop_value_collector: heap.alloc_str_for_test("tmp_i"), }], @@ -1197,13 +1197,13 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("outside"), INT_TYPE) + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("outside"), INT_32_TYPE) ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), ONE ) ], @@ -1226,7 +1226,7 @@ mod tests { initial_value: ZERO, increment_amount: PotentialLoopInvariantExpression::Var(VariableName::new( heap.alloc_str_for_test("outside"), - INT_TYPE + INT_32_TYPE )), loop_value_collector: heap.alloc_str_for_test("tmp_i"), }], @@ -1234,13 +1234,13 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("outside"), INT_TYPE) + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("outside"), INT_32_TYPE) ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), Expression::int(2) ) ], @@ -1262,7 +1262,7 @@ mod tests { initial_value: ZERO, increment_amount: PotentialLoopInvariantExpression::Var(VariableName::new( heap.alloc_str_for_test("outside"), - INT_TYPE + INT_32_TYPE )), loop_value_collector: heap.alloc_str_for_test("tmp_i"), }], @@ -1270,14 +1270,14 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("outside"), INT_TYPE) + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("outside"), INT_32_TYPE) ) ], &HashSet::from([ @@ -1304,14 +1304,14 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::int(2) ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("outside"), INT_TYPE) + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("outside"), INT_32_TYPE) ) ], &HashSet::from([ @@ -1337,13 +1337,13 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE ), Statement::binary( heap.alloc_str_for_test("t1"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), ONE ) ], @@ -1372,13 +1372,13 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE ), Statement::binary( heap.alloc_str_for_test("t1"), BinaryOperator::DIV, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), ONE ) ], @@ -1402,15 +1402,15 @@ mod tests { &vec![ GenenalLoopVariable { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO, }, GenenalLoopVariable { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("name"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("name"), INT_32_TYPE), }, ], &mut vec![], @@ -1452,7 +1452,7 @@ mod tests { &vec![Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ),], &None @@ -1503,7 +1503,7 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::StructInit { @@ -1524,7 +1524,7 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::StructInit { @@ -1546,7 +1546,7 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::SingleIf { condition: ZERO, invert_condition: false, statements: vec![] } @@ -1563,7 +1563,7 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::SingleIf { @@ -1584,7 +1584,7 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::SingleIf { @@ -1605,11 +1605,11 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: false, statements: vec![Statement::Break(ZERO)] }, @@ -1639,23 +1639,23 @@ mod tests { }, Statement::IndexedAccess { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: ZERO, index: 0 }, Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_A), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE) + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE) }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None }, Statement::Break(ZERO) @@ -1672,11 +1672,11 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::EQ, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: false, statements: vec![Statement::Break(ZERO)] }, @@ -1693,11 +1693,11 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: false, statements: vec![Statement::StructInit { struct_variable_name: PStr::LOWER_A, @@ -1718,7 +1718,7 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::SingleIf { @@ -1749,7 +1749,7 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::SingleIf { @@ -1771,7 +1771,7 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::EQ, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::SingleIf { @@ -1782,7 +1782,7 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ) ], @@ -1799,7 +1799,7 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::SingleIf { @@ -1810,7 +1810,7 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ) ], @@ -1827,18 +1827,18 @@ mod tests { Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::GE, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: false, statements: vec![Statement::Break(ZERO)], }, Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE ), ], @@ -1866,61 +1866,61 @@ mod tests { vec![ GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), }, GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }, GenenalLoopVariable { name: heap.alloc_str_for_test("x"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE), }, ], vec![ Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::GE, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO, ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: false, statements: vec![Statement::Break(ZERO)], }, Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE, ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_J, INT_TYPE), + Expression::var_name(PStr::LOWER_J, INT_32_TYPE), Expression::int(3), ), Statement::binary( heap.alloc_str_for_test("tmp_x"), BinaryOperator::MUL, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), Expression::int(5), ), Statement::binary( heap.alloc_str_for_test("tmp_y"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE), Expression::int(6), ), ], - Some(VariableName { name: heap.alloc_str_for_test("bc"), type_: INT_TYPE }), + Some(VariableName { name: heap.alloc_str_for_test("bc"), type_: INT_32_TYPE }), ), &HashSet::new(), ) diff --git a/crates/samlang-optimization/src/loop_induction_variable_elimination.rs b/crates/samlang-optimization/src/loop_induction_variable_elimination.rs index ad86b950..ee50a1d4 100644 --- a/crates/samlang-optimization/src/loop_induction_variable_elimination.rs +++ b/crates/samlang-optimization/src/loop_induction_variable_elimination.rs @@ -5,7 +5,7 @@ use super::loop_induction_analysis::{ use itertools::Itertools; use samlang_ast::{ hir::BinaryOperator, - mir::{Callee, Expression, Statement, VariableName, INT_TYPE}, + mir::{Callee, Expression, Statement, VariableName, INT_32_TYPE}, }; pub(super) struct LoopInductionVariableEliminationResult { @@ -132,7 +132,7 @@ pub(super) fn optimize( new_initial_value_name, BinaryOperator::PLUS, only_relevant_induction_loop_variables.immediate.to_expression(), - Expression::var_name(new_initial_value_temp_temporary, INT_TYPE), + Expression::var_name(new_initial_value_temp_temporary, INT_32_TYPE), )), Statement::Binary(Statement::binary_flexible_unwrapped( new_guard_value_temp_temporary, @@ -147,18 +147,18 @@ pub(super) fn optimize( new_guard_value_name, BinaryOperator::PLUS, only_relevant_induction_loop_variables.immediate.to_expression(), - Expression::var_name(new_guard_value_temp_temporary, INT_TYPE), + Expression::var_name(new_guard_value_temp_temporary, INT_32_TYPE), )), ]; let basic_induction_variable_with_loop_guard = BasicInductionVariableWithLoopGuard { name: only_relevant_induction_loop_variables.name, - initial_value: Expression::var_name(new_initial_value_name, INT_TYPE), + initial_value: Expression::var_name(new_initial_value_name, INT_32_TYPE), increment_amount: added_invariant_expression_in_loop, guard_operator: GuardOperator::LT, guard_expression: PotentialLoopInvariantExpression::Var(VariableName { name: new_guard_value_name, - type_: INT_TYPE, + type_: INT_32_TYPE, }), }; let derived_induction_variables = optimizable_while_loop @@ -202,7 +202,7 @@ mod tests { hir::{BinaryOperator, UnaryOperator}, mir::{ Callee, Expression, FunctionName, FunctionNameExpression, GenenalLoopVariable, Statement, - SymbolTable, Type, VariableName, INT_TYPE, ONE, ZERO, + SymbolTable, Type, VariableName, INT_32_TYPE, ONE, ZERO, }, }; use samlang_heap::{Heap, PStr}; @@ -224,9 +224,9 @@ mod tests { general_induction_variables: vec![], loop_variables_that_are_not_basic_induction_variables: vec![GenenalLoopVariable { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(PStr::LOWER_I, INT_TYPE) + loop_value: Expression::var_name(PStr::LOWER_I, INT_32_TYPE) }], derived_induction_variables: vec![], statements: vec![], @@ -251,8 +251,8 @@ mod tests { statements: vec![], break_collector: Some(( PStr::LOWER_A, - INT_TYPE, - Expression::var_name(PStr::LOWER_I, INT_TYPE) + INT_32_TYPE, + Expression::var_name(PStr::LOWER_I, INT_32_TYPE) )) }, heap, @@ -274,7 +274,7 @@ mod tests { statements: vec![ Statement::IndexedAccess { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: ZERO, index: 3 }, @@ -292,22 +292,26 @@ mod tests { closure_type_name: table.create_type_name_for_test(heap.alloc_str_for_test("I")), function_name: FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_A), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE) + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE) }, context: ZERO }], - final_assignments: vec![(PStr::LOWER_A, INT_TYPE, ZERO, ZERO)] + final_assignments: vec![(PStr::LOWER_A, INT_32_TYPE, ZERO, ZERO)] }, Statement::While { loop_variables: vec![GenenalLoopVariable { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO }], statements: vec![ - Statement::Cast { name: PStr::LOWER_A, type_: INT_TYPE, assigned_expression: ZERO }, - Statement::LateInitDeclaration { name: PStr::LOWER_A, type_: INT_TYPE }, + Statement::Cast { + name: PStr::LOWER_A, + type_: INT_32_TYPE, + assigned_expression: ZERO + }, + Statement::LateInitDeclaration { name: PStr::LOWER_A, type_: INT_32_TYPE }, Statement::LateInitAssignment { name: PStr::LOWER_A, assigned_expression: ZERO }, Statement::StructInit { struct_variable_name: PStr::LOWER_A, @@ -320,16 +324,16 @@ mod tests { Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_A), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE) + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE) }), arguments: vec![ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None }, Statement::Call { - callee: Callee::Variable(VariableName::new(PStr::LOWER_A, INT_TYPE)), + callee: Callee::Variable(VariableName::new(PStr::LOWER_A, INT_32_TYPE)), arguments: vec![ZERO], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None }, ], @@ -351,9 +355,9 @@ mod tests { general_induction_variables: vec![], loop_variables_that_are_not_basic_induction_variables: vec![GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE) + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE) }], derived_induction_variables: vec![ DerivedInductionVariableWithName { @@ -383,7 +387,7 @@ mod tests { initial_value: ONE, increment_amount: PotentialLoopInvariantExpression::Var(VariableName::new( PStr::LOWER_A, - INT_TYPE + INT_32_TYPE )), guard_operator: GuardOperator::LT, guard_expression: PotentialLoopInvariantExpression::Int(10), @@ -391,16 +395,16 @@ mod tests { general_induction_variables: vec![], loop_variables_that_are_not_basic_induction_variables: vec![GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }], derived_induction_variables: vec![DerivedInductionVariableWithName { name: heap.alloc_str_for_test("tmp_j"), base_name: PStr::LOWER_I, multiplier: PotentialLoopInvariantExpression::Var(VariableName::new( PStr::LOWER_A, - INT_TYPE + INT_32_TYPE )), immediate: PotentialLoopInvariantExpression::Int(5), }], @@ -429,9 +433,9 @@ mod tests { general_induction_variables: vec![], loop_variables_that_are_not_basic_induction_variables: vec![GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }], derived_induction_variables: vec![DerivedInductionVariableWithName { name: heap.alloc_str_for_test("tmp_j"), @@ -485,16 +489,16 @@ mod tests { general_induction_variables: vec![], loop_variables_that_are_not_basic_induction_variables: vec![GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }], derived_induction_variables: vec![DerivedInductionVariableWithName { name: heap.alloc_str_for_test("tmp_j"), base_name: PStr::LOWER_I, multiplier: PotentialLoopInvariantExpression::Var(VariableName::new( PStr::LOWER_A, - INT_TYPE, + INT_32_TYPE, )), immediate: PotentialLoopInvariantExpression::Int(5), }], @@ -537,7 +541,7 @@ mod tests { initial_value: ONE, increment_amount: PotentialLoopInvariantExpression::Var(VariableName::new( PStr::LOWER_A, - INT_TYPE, + INT_32_TYPE, )), guard_operator: GuardOperator::LT, guard_expression: PotentialLoopInvariantExpression::Int(10), @@ -545,9 +549,9 @@ mod tests { general_induction_variables: vec![], loop_variables_that_are_not_basic_induction_variables: vec![GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }], derived_induction_variables: vec![DerivedInductionVariableWithName { name: heap.alloc_str_for_test("tmp_j"), diff --git a/crates/samlang-optimization/src/loop_invariant_code_motion.rs b/crates/samlang-optimization/src/loop_invariant_code_motion.rs index ce1eea43..a342b96d 100644 --- a/crates/samlang-optimization/src/loop_invariant_code_motion.rs +++ b/crates/samlang-optimization/src/loop_invariant_code_motion.rs @@ -132,7 +132,7 @@ mod tests { hir::{BinaryOperator, UnaryOperator}, mir::{ Callee, Expression, FunctionName, FunctionNameExpression, GenenalLoopVariable, Statement, - SymbolTable, Type, VariableName, INT_TYPE, ONE, ZERO, + SymbolTable, Type, VariableName, INT_32_TYPE, ONE, ZERO, }, }; use samlang_heap::{Heap, PStr}; @@ -150,44 +150,44 @@ mod tests { vec![ GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), }, GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }, GenenalLoopVariable { name: heap.alloc_str_for_test("x"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE), }, GenenalLoopVariable { name: heap.alloc_str_for_test("y"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_y"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_y"), INT_32_TYPE), }, GenenalLoopVariable { name: heap.alloc_str_for_test("z"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_z"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_z"), INT_32_TYPE), }, ], vec![ Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ZERO, ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: false, statements: vec![Statement::Break(ZERO)], }, @@ -195,97 +195,97 @@ mod tests { Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE, ), Statement::Unary { name: heap.alloc_str_for_test("non_lv_unary"), operator: UnaryOperator::Not, - operand: Expression::var_name(PStr::LOWER_I, INT_TYPE), + operand: Expression::var_name(PStr::LOWER_I, INT_32_TYPE), }, Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_J, INT_TYPE), + Expression::var_name(PStr::LOWER_J, INT_32_TYPE), Expression::int(3), ), Statement::binary( heap.alloc_str_for_test("tmp_x"), BinaryOperator::MUL, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::int(5), ), Statement::binary( heap.alloc_str_for_test("tmp_y"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE), Expression::int(6), ), Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_F), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), - arguments: vec![Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_F), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), - arguments: vec![Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: Some(heap.alloc_str_for_test("fc")), }, Statement::binary( heap.alloc_str_for_test("tmp_z"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("tmp_y"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_x"), INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_y"), INT_32_TYPE), ), Statement::binary( PStr::LOWER_C, BinaryOperator::MINUS, - Expression::var_name(PStr::LOWER_A, INT_TYPE), - Expression::var_name(PStr::LOWER_B, INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), + Expression::var_name(PStr::LOWER_B, INT_32_TYPE), ), Statement::IndexedAccess { name: PStr::LOWER_D, - type_: INT_TYPE, - pointer_expression: Expression::var_name(PStr::LOWER_C, INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(PStr::LOWER_C, INT_32_TYPE), index: 0, }, Statement::IndexedAccess { name: PStr::LOWER_E, - type_: INT_TYPE, - pointer_expression: Expression::var_name(heap.alloc_str_for_test("x"), INT_TYPE), + type_: INT_32_TYPE, + pointer_expression: Expression::var_name(heap.alloc_str_for_test("x"), INT_32_TYPE), index: 0, }, Statement::binary( PStr::LOWER_F, BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_B, INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("x"), INT_TYPE), + Expression::var_name(PStr::LOWER_B, INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("x"), INT_32_TYPE), ), Statement::ClosureInit { closure_variable_name: PStr::LOWER_G, closure_type_name: table.create_type_name_for_test(heap.alloc_str_for_test("I")), function_name: FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_F), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, - context: Expression::var_name(heap.alloc_str_for_test("x"), INT_TYPE), + context: Expression::var_name(heap.alloc_str_for_test("x"), INT_32_TYPE), }, Statement::ClosureInit { closure_variable_name: heap.alloc_str_for_test("h"), closure_type_name: table.create_type_name_for_test(heap.alloc_str_for_test("I")), function_name: FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_F), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, - context: Expression::var_name(PStr::LOWER_D, INT_TYPE), + context: Expression::var_name(PStr::LOWER_D, INT_32_TYPE), }, Statement::StructInit { struct_variable_name: heap.alloc_str_for_test("kk"), @@ -295,37 +295,37 @@ mod tests { Statement::StructInit { struct_variable_name: heap.alloc_str_for_test("kk2"), type_name: table.create_type_name_for_test(heap.alloc_str_for_test("I")), - expression_list: vec![Expression::var_name(PStr::LOWER_G, INT_TYPE)], + expression_list: vec![Expression::var_name(PStr::LOWER_G, INT_32_TYPE)], }, Statement::Cast { name: heap.alloc_str_for_test("l1"), - type_: INT_TYPE, + type_: INT_32_TYPE, assigned_expression: ZERO, }, Statement::Cast { name: heap.alloc_str_for_test("l2"), - type_: INT_TYPE, - assigned_expression: Expression::var_name(PStr::LOWER_I, INT_TYPE), + type_: INT_32_TYPE, + assigned_expression: Expression::var_name(PStr::LOWER_I, INT_32_TYPE), }, - Statement::LateInitDeclaration { name: heap.alloc_str_for_test("l3"), type_: INT_TYPE }, + Statement::LateInitDeclaration { name: heap.alloc_str_for_test("l3"), type_: INT_32_TYPE }, Statement::LateInitAssignment { name: heap.alloc_str_for_test("l3"), - assigned_expression: Expression::var_name(PStr::LOWER_I, INT_TYPE), + assigned_expression: Expression::var_name(PStr::LOWER_I, INT_32_TYPE), }, Statement::IfElse { condition: ZERO, s1: vec![], s2: vec![], - final_assignments: vec![(heap.alloc_str_for_test("bad"), INT_TYPE, ZERO, ZERO)], + final_assignments: vec![(heap.alloc_str_for_test("bad"), INT_32_TYPE, ZERO, ZERO)], }, Statement::While { loop_variables: vec![], statements: vec![], break_collector: None }, Statement::While { loop_variables: vec![], statements: vec![], - break_collector: Some(VariableName::new(heap.alloc_str_for_test("zzzz"), INT_TYPE)), + break_collector: Some(VariableName::new(heap.alloc_str_for_test("zzzz"), INT_32_TYPE)), }, ], - Some(VariableName::new(heap.alloc_str_for_test("bc"), INT_TYPE)), + Some(VariableName::new(heap.alloc_str_for_test("bc"), INT_32_TYPE)), )); let optimized_stmts = hoisted_statements_before_while diff --git a/crates/samlang-optimization/src/loop_optimizations.rs b/crates/samlang-optimization/src/loop_optimizations.rs index fb139986..b48b5685 100644 --- a/crates/samlang-optimization/src/loop_optimizations.rs +++ b/crates/samlang-optimization/src/loop_optimizations.rs @@ -7,7 +7,7 @@ use super::{ use itertools::Itertools; use samlang_ast::hir::BinaryOperator; use samlang_ast::mir::{ - Expression, Function, GenenalLoopVariable, Statement, VariableName, INT_TYPE, ZERO, + Expression, Function, GenenalLoopVariable, Statement, VariableName, INT_32_TYPE, ZERO, }; use samlang_heap::Heap; use std::collections::HashSet; @@ -42,19 +42,19 @@ fn expand_optimizable_while_loop( .filter(|v| useful_used_set.contains(&v.name)) .chain(vec![GenenalLoopVariable { name: basic_induction_variable_with_loop_guard.name, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: basic_induction_variable_with_loop_guard.initial_value, loop_value: Expression::var_name( basic_induction_variable_with_loop_guard_value_collector, - INT_TYPE, + INT_32_TYPE, ), }]) .chain(general_basic_induction_variables_with_loop_value_collectors.iter().map(|(v, n)| { GenenalLoopVariable { name: v.name, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: v.initial_value, - loop_value: Expression::var_name(*n, INT_TYPE), + loop_value: Expression::var_name(*n, INT_32_TYPE), } })) .collect_vec(); @@ -65,11 +65,11 @@ fn expand_optimizable_while_loop( Statement::Binary(Statement::binary_unwrapped( loop_condition_variable, basic_induction_variable_with_loop_guard.guard_operator.invert().to_op(), - Expression::var_name(basic_induction_variable_with_loop_guard.name, INT_TYPE), + Expression::var_name(basic_induction_variable_with_loop_guard.name, INT_32_TYPE), basic_induction_variable_with_loop_guard.guard_expression.to_expression(), )), Statement::SingleIf { - condition: Expression::var_name(loop_condition_variable, INT_TYPE), + condition: Expression::var_name(loop_condition_variable, INT_32_TYPE), invert_condition: false, statements: vec![Statement::Break(*break_value)], }, @@ -79,7 +79,7 @@ fn expand_optimizable_while_loop( .chain(vec![Statement::Binary(Statement::binary_unwrapped( basic_induction_variable_with_loop_guard_value_collector, BinaryOperator::PLUS, - Expression::var_name(basic_induction_variable_with_loop_guard.name, INT_TYPE), + Expression::var_name(basic_induction_variable_with_loop_guard.name, INT_32_TYPE), basic_induction_variable_with_loop_guard.increment_amount.to_expression(), ))]) .chain(general_basic_induction_variables_with_loop_value_collectors.into_iter().map( @@ -87,7 +87,7 @@ fn expand_optimizable_while_loop( Statement::Binary(Statement::binary_unwrapped( collector, BinaryOperator::PLUS, - Expression::var_name(v.name, INT_TYPE), + Expression::var_name(v.name, INT_32_TYPE), v.increment_amount.to_expression(), )) }, @@ -98,13 +98,13 @@ fn expand_optimizable_while_loop( Statement::Binary(Statement::binary_flexible_unwrapped( step_1_temp, BinaryOperator::MUL, - Expression::var_name(v.base_name, INT_TYPE), + Expression::var_name(v.base_name, INT_32_TYPE), v.multiplier.to_expression(), )), Statement::Binary(Statement::binary_flexible_unwrapped( v.name, BinaryOperator::PLUS, - Expression::var_name(step_1_temp, INT_TYPE), + Expression::var_name(step_1_temp, INT_32_TYPE), v.immediate.to_expression(), )), ] @@ -233,7 +233,7 @@ mod tests { hir::BinaryOperator, mir::{ Callee, Expression, Function, FunctionName, FunctionNameExpression, GenenalLoopVariable, - Statement, SymbolTable, Type, VariableName, INT_TYPE, ONE, ZERO, + Statement, SymbolTable, Type, VariableName, INT_32_TYPE, ONE, ZERO, }, }; use samlang_heap::{Heap, PStr}; @@ -259,7 +259,7 @@ mod tests { let mut f = Function { name: FunctionName::new_for_test(PStr::INVALID_PSTR), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: stmts, return_value, }; @@ -280,48 +280,48 @@ mod tests { vec![ GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), }, GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }, ], vec![ Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::GE, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::int(10), ), Statement::Cast { name: heap.alloc_str_for_test("cast"), - type_: INT_TYPE, + type_: INT_32_TYPE, assigned_expression: ZERO, }, Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: false, - statements: vec![Statement::Break(Expression::var_name(PStr::LOWER_J, INT_TYPE))], + statements: vec![Statement::Break(Expression::var_name(PStr::LOWER_J, INT_32_TYPE))], }, Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE, ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_J, INT_TYPE), + Expression::var_name(PStr::LOWER_J, INT_32_TYPE), Expression::int(10), ), ], - Some(VariableName::new(heap.alloc_str_for_test("bc"), INT_TYPE)), + Some(VariableName::new(heap.alloc_str_for_test("bc"), INT_32_TYPE)), ) } @@ -332,43 +332,43 @@ mod tests { vec![ GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), }, GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }, ], vec![ Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::GE, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::int(10), ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: false, - statements: vec![Statement::Break(Expression::var_name(PStr::LOWER_J, INT_TYPE))], + statements: vec![Statement::Break(Expression::var_name(PStr::LOWER_J, INT_32_TYPE))], }, Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE, ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), Expression::int(10), ), ], - Some(VariableName::new(heap.alloc_str_for_test("bc"), INT_TYPE)), + Some(VariableName::new(heap.alloc_str_for_test("bc"), INT_32_TYPE)), ) } @@ -379,55 +379,55 @@ mod tests { vec![ GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), }, GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }, GenenalLoopVariable { name: PStr::LOWER_K, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_k"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_k"), INT_32_TYPE), }, ], vec![ Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::GE, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::int(10), ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: false, - statements: vec![Statement::Break(Expression::var_name(PStr::LOWER_J, INT_TYPE))], + statements: vec![Statement::Break(Expression::var_name(PStr::LOWER_J, INT_32_TYPE))], }, Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE, ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), Expression::int(9), ), Statement::binary( heap.alloc_str_for_test("tmp_k"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), Expression::int(9), ), ], - Some(VariableName::new(heap.alloc_str_for_test("bc"), INT_TYPE)), + Some(VariableName::new(heap.alloc_str_for_test("bc"), INT_32_TYPE)), ) } @@ -505,43 +505,43 @@ while (true) { vec![ GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), }, GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }, ], vec![ Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::int(10), ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: false, - statements: vec![Statement::Break(Expression::var_name(PStr::LOWER_J, INT_TYPE))], + statements: vec![Statement::Break(Expression::var_name(PStr::LOWER_J, INT_32_TYPE))], }, Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), - Expression::var_name(PStr::LOWER_A, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::MUL, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::int(2), ), ], - Some(VariableName::new(heap.alloc_str_for_test("bc"), INT_TYPE)), + Some(VariableName::new(heap.alloc_str_for_test("bc"), INT_32_TYPE)), ), heap, r#"let j: int = 0; @@ -567,39 +567,39 @@ while (true) { vec![ GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), }, GenenalLoopVariable { name: PStr::LOWER_J, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }, ], vec![ Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::int(10), ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: false, - statements: vec![Statement::Break(Expression::var_name(PStr::LOWER_J, INT_TYPE))], + statements: vec![Statement::Break(Expression::var_name(PStr::LOWER_J, INT_32_TYPE))], }, Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE, ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::PLUS, - Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), Expression::int(10), ), ], @@ -637,7 +637,7 @@ while (true) { s2: vec![Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::MUL, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::int(2), )], final_assignments: vec![], @@ -651,7 +651,7 @@ while (true) { let (loop_variables, statements, break_collector) = optimizable_loop_1(heap); assert_stmts_optimized( vec![Statement::While { loop_variables, statements, break_collector }], - Expression::var_name(heap.alloc_str_for_test("bc"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("bc"), INT_32_TYPE), heap, "let cast = 0 as int;\nreturn 100;", ); @@ -660,7 +660,7 @@ while (true) { let (loop_variables, statements, break_collector) = optimizable_loop_2(heap); assert_stmts_optimized( vec![Statement::While { loop_variables, statements, break_collector }], - Expression::var_name(heap.alloc_str_for_test("bc"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("bc"), INT_32_TYPE), heap, r#"let j: int = 16; let tmp_j: int = 17; @@ -682,7 +682,7 @@ return (bc: int);"#, let (loop_variables, statements, break_collector) = optimizable_loop_3(heap); assert_stmts_optimized( vec![Statement::While { loop_variables, statements, break_collector }], - Expression::var_name(heap.alloc_str_for_test("bc"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("bc"), INT_32_TYPE), heap, r#"let j: int = 15; let i: int = 6; @@ -712,48 +712,48 @@ return (bc: int);"#, loop_variables: vec![ GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: Expression::int(4), - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), }, GenenalLoopVariable { name: heap.alloc_str_for_test("acc"), - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ONE, - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_j"), INT_32_TYPE), }, ], statements: vec![ Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), ONE, ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: false, statements: vec![Statement::Break(Expression::var_name( heap.alloc_str_for_test("acc"), - INT_TYPE, + INT_32_TYPE, ))], }, Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::int(-1), ), Statement::binary( heap.alloc_str_for_test("tmp_j"), BinaryOperator::MUL, - Expression::var_name(PStr::LOWER_I, INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("acc"), INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("acc"), INT_32_TYPE), ), ], - break_collector: Some(VariableName::new(heap.alloc_str_for_test("bc"), INT_TYPE)), + break_collector: Some(VariableName::new(heap.alloc_str_for_test("bc"), INT_32_TYPE)), }], - Expression::var_name(heap.alloc_str_for_test("bc"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("bc"), INT_32_TYPE), heap, "\nreturn 24;", ); @@ -767,47 +767,47 @@ return (bc: int);"#, vec![Statement::While { loop_variables: vec![GenenalLoopVariable { name: PStr::LOWER_I, - type_: INT_TYPE, - initial_value: Expression::var_name(heap.alloc_str_for_test("init_i"), INT_TYPE), - loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_TYPE), + type_: INT_32_TYPE, + initial_value: Expression::var_name(heap.alloc_str_for_test("init_i"), INT_32_TYPE), + loop_value: Expression::var_name(heap.alloc_str_for_test("tmp_i"), INT_32_TYPE), }], statements: vec![ Statement::binary( heap.alloc_str_for_test("cc"), BinaryOperator::LT, - Expression::var_name(PStr::LOWER_I, INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("L"), INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("L"), INT_32_TYPE), ), Statement::SingleIf { - condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_TYPE), + condition: Expression::var_name(heap.alloc_str_for_test("cc"), INT_32_TYPE), invert_condition: true, statements: vec![Statement::Break(ZERO)], }, Statement::binary( heap.alloc_str_for_test("t"), BinaryOperator::MUL, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::int(3), ), Statement::binary( PStr::LOWER_J, BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_A, INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("t"), INT_TYPE), + Expression::var_name(PStr::LOWER_A, INT_32_TYPE), + Expression::var_name(heap.alloc_str_for_test("t"), INT_32_TYPE), ), Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(PStr::LOWER_F), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), - arguments: vec![Expression::var_name(PStr::LOWER_J, INT_TYPE)], - return_type: INT_TYPE, + arguments: vec![Expression::var_name(PStr::LOWER_J, INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }, Statement::binary( heap.alloc_str_for_test("tmp_i"), BinaryOperator::PLUS, - Expression::var_name(PStr::LOWER_I, INT_TYPE), + Expression::var_name(PStr::LOWER_I, INT_32_TYPE), Expression::int(2), ), ], diff --git a/crates/samlang-optimization/src/loop_strength_reduction.rs b/crates/samlang-optimization/src/loop_strength_reduction.rs index bad70696..212d0166 100644 --- a/crates/samlang-optimization/src/loop_strength_reduction.rs +++ b/crates/samlang-optimization/src/loop_strength_reduction.rs @@ -4,7 +4,7 @@ use super::loop_induction_analysis::{ }; use samlang_ast::{ hir::BinaryOperator, - mir::{Expression, Statement, INT_TYPE}, + mir::{Expression, Statement, INT_32_TYPE}, }; use samlang_heap::Heap; use std::collections::HashMap; @@ -56,11 +56,11 @@ pub(super) fn optimize( new_initial_value_name, BinaryOperator::PLUS, derived_induction_variable.immediate.to_expression(), - Expression::var_name(new_initial_value_temp_temporary, INT_TYPE), + Expression::var_name(new_initial_value_temp_temporary, INT_32_TYPE), ))); new_general_induction_variables.push(GeneralBasicInductionVariable { name: derived_induction_variable.name, - initial_value: Expression::var_name(new_initial_value_name, INT_TYPE), + initial_value: Expression::var_name(new_initial_value_name, INT_32_TYPE), increment_amount: added_invariant_expression_in_loop, }); } else { @@ -93,7 +93,7 @@ mod tests { }; use itertools::Itertools; use pretty_assertions::assert_eq; - use samlang_ast::mir::{SymbolTable, VariableName, INT_TYPE, ONE}; + use samlang_ast::mir::{SymbolTable, VariableName, INT_32_TYPE, ONE}; use samlang_heap::{Heap, PStr}; #[test] @@ -118,7 +118,7 @@ mod tests { initial_value: ONE, increment_amount: PotentialLoopInvariantExpression::Var(VariableName::new( PStr::LOWER_C, - INT_TYPE, + INT_32_TYPE, )), }], loop_variables_that_are_not_basic_induction_variables: vec![], @@ -128,11 +128,11 @@ mod tests { base_name: PStr::LOWER_I, multiplier: PotentialLoopInvariantExpression::Var(VariableName::new( PStr::LOWER_A, - INT_TYPE, + INT_32_TYPE, )), immediate: PotentialLoopInvariantExpression::Var(VariableName::new( PStr::LOWER_B, - INT_TYPE, + INT_32_TYPE, )), }, DerivedInductionVariableWithName { @@ -140,11 +140,11 @@ mod tests { base_name: PStr::LOWER_J, multiplier: PotentialLoopInvariantExpression::Var(VariableName::new( PStr::LOWER_A, - INT_TYPE, + INT_32_TYPE, )), immediate: PotentialLoopInvariantExpression::Var(VariableName::new( PStr::LOWER_B, - INT_TYPE, + INT_32_TYPE, )), }, ], diff --git a/crates/samlang-optimization/src/optimization_common.rs b/crates/samlang-optimization/src/optimization_common.rs index d6ab172a..b37f322c 100644 --- a/crates/samlang-optimization/src/optimization_common.rs +++ b/crates/samlang-optimization/src/optimization_common.rs @@ -135,7 +135,7 @@ mod tests { assert_eq!(false, single_if_or_null(ZERO, false, vec![Statement::Break(ZERO)]).is_empty()); let bv1 = BindedValue::IndexedAccess( - IndexAccessBindedValue { type_: INT_TYPE, pointer_expression: ZERO, index: 0 }.clone(), + IndexAccessBindedValue { type_: INT_32_TYPE, pointer_expression: ZERO, index: 0 }.clone(), ); let bv2 = BindedValue::Binary( BinaryBindedValue { operator: BinaryOperator::PLUS, e1: ZERO, e2: ZERO }.clone(), diff --git a/crates/samlang-optimization/src/unused_name_elimination.rs b/crates/samlang-optimization/src/unused_name_elimination.rs index dc8f78f6..27d8d2f6 100644 --- a/crates/samlang-optimization/src/unused_name_elimination.rs +++ b/crates/samlang-optimization/src/unused_name_elimination.rs @@ -251,7 +251,7 @@ mod tests { mir::{ Callee, ClosureTypeDefinition, EnumTypeDefinition, Expression, Function, FunctionName, FunctionNameExpression, GenenalLoopVariable, Sources, Statement, SymbolTable, Type, - TypeDefinition, TypeDefinitionMappings, VariableName, INT_TYPE, ZERO, + TypeDefinition, TypeDefinitionMappings, VariableName, INT_32_TYPE, ZERO, }, }; use samlang_heap::{Heap, PStr}; @@ -275,22 +275,22 @@ mod tests { closure_types: vec![ ClosureTypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("Foo")), - function_type: Type::new_fn_unwrapped(vec![], INT_TYPE), + function_type: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, ClosureTypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("Baz")), - function_type: Type::new_fn_unwrapped(vec![], INT_TYPE), + function_type: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, ClosureTypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("RefByType")), - function_type: Type::new_fn_unwrapped(vec![], INT_TYPE), + function_type: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }, ], type_definitions: vec![ TypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("Foo")), mappings: TypeDefinitionMappings::Struct(vec![ - INT_TYPE, + INT_32_TYPE, Type::Id(table.create_type_name_for_test(heap.alloc_str_for_test("Foo"))), Type::Id(table.create_type_name_for_test(heap.alloc_str_for_test("Bar"))), ]), @@ -303,14 +303,14 @@ mod tests { }, TypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("Baz")), - mappings: TypeDefinitionMappings::Struct(vec![INT_TYPE]), + mappings: TypeDefinitionMappings::Struct(vec![INT_32_TYPE]), }, TypeDefinition { name: table.create_type_name_for_test(heap.alloc_str_for_test("Baz")), mappings: TypeDefinitionMappings::Enum(vec![ EnumTypeDefinition::Int, - EnumTypeDefinition::Unboxed(INT_TYPE), - EnumTypeDefinition::Boxed(vec![INT_TYPE]), + EnumTypeDefinition::Unboxed(INT_32_TYPE), + EnumTypeDefinition::Boxed(vec![INT_32_TYPE]), ]), }, TypeDefinition { @@ -338,14 +338,14 @@ mod tests { Function { name: FunctionName::new_for_test(PStr::MAIN_FN), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("foo")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }], return_value: ZERO, @@ -353,7 +353,7 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("foo")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![INT_TYPE], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![INT_32_TYPE], INT_32_TYPE), body: vec![ Statement::StructInit { struct_variable_name: PStr::LOWER_A, @@ -365,22 +365,22 @@ mod tests { closure_type_name: table.create_type_name_for_test(heap.alloc_str_for_test("Foo")), function_name: (FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("foo")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), context: ZERO, }, Statement::IndexedAccess { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, pointer_expression: Expression::StringName(heap.alloc_str_for_test("bar")), index: 0, }, Statement::Cast { name: PStr::LOWER_A, - type_: INT_TYPE, + type_: INT_32_TYPE, assigned_expression: Expression::StringName(heap.alloc_str_for_test("bar")), }, - Statement::LateInitDeclaration { name: PStr::LOWER_A, type_: INT_TYPE }, + Statement::LateInitDeclaration { name: PStr::LOWER_A, type_: INT_32_TYPE }, Statement::LateInitAssignment { name: PStr::LOWER_A, assigned_expression: Expression::StringName(heap.alloc_str_for_test("bar")), @@ -388,16 +388,19 @@ mod tests { Statement::Call { callee: Callee::FunctionName(FunctionNameExpression { name: FunctionName::new_for_test(heap.alloc_str_for_test("baz")), - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), }), arguments: vec![Expression::StringName(heap.alloc_str_for_test("haha"))], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Call { - callee: Callee::Variable(VariableName::new(heap.alloc_str_for_test("baz"), INT_TYPE)), + callee: Callee::Variable(VariableName::new( + heap.alloc_str_for_test("baz"), + INT_32_TYPE, + )), arguments: vec![Expression::StringName(heap.alloc_str_for_test("haha"))], - return_type: INT_TYPE, + return_type: INT_32_TYPE, return_collector: None, }, Statement::Unary { @@ -419,7 +422,7 @@ mod tests { Expression::StringName(heap.alloc_str_for_test("foo")), Expression::StringName(heap.alloc_str_for_test("bar")), )], - final_assignments: vec![(heap.alloc_str_for_test("fff"), INT_TYPE, ZERO, ZERO)], + final_assignments: vec![(heap.alloc_str_for_test("fff"), INT_32_TYPE, ZERO, ZERO)], }, Statement::SingleIf { condition: ZERO, @@ -429,7 +432,7 @@ mod tests { Statement::While { loop_variables: vec![GenenalLoopVariable { name: PStr::LOWER_F, - type_: INT_TYPE, + type_: INT_32_TYPE, initial_value: ZERO, loop_value: ZERO, }], @@ -446,11 +449,14 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("bar")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![Statement::Call { - callee: Callee::Variable(VariableName::new(heap.alloc_str_for_test("baz"), INT_TYPE)), - arguments: vec![Expression::var_name(heap.alloc_str_for_test("baz"), INT_TYPE)], - return_type: INT_TYPE, + callee: Callee::Variable(VariableName::new( + heap.alloc_str_for_test("baz"), + INT_32_TYPE, + )), + arguments: vec![Expression::var_name(heap.alloc_str_for_test("baz"), INT_32_TYPE)], + return_type: INT_32_TYPE, return_collector: None, }], return_value: ZERO, @@ -458,7 +464,7 @@ mod tests { Function { name: FunctionName::new_for_test(heap.alloc_str_for_test("baz")), parameters: vec![], - type_: Type::new_fn_unwrapped(vec![], INT_TYPE), + type_: Type::new_fn_unwrapped(vec![], INT_32_TYPE), body: vec![], return_value: ZERO, },