Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

Translate int remainder operator

author
Danielle Maywood
committer
nandi
date (Jul 26, 2026, 12:11 PM -0700) commit dca34eee parent aa7ffca1 change-id vrtttoou
+96 -15
+24 -4
compiler-core/src/cranelift/mir.rs
··· 121 121 rhs: Box<Expression>, 122 122 }, 123 123 124 + IntRem { 125 + lhs: Box<Expression>, 126 + rhs: Box<Expression>, 127 + }, 128 + 124 129 FloatGt { 125 130 lhs: Box<Expression>, 126 131 rhs: Box<Expression>, ··· 289 294 | Expression::IntSub { .. } 290 295 | Expression::IntMul { .. } 291 296 | Expression::IntDiv { .. } 297 + | Expression::IntRem { .. } 292 298 | Expression::FloatGt { .. } 293 299 | Expression::FloatGtEq { .. } 294 300 | Expression::FloatLt { .. } ··· 355 361 lhs: Box::new(Self::flatten_expression(*lhs)), 356 362 rhs: Box::new(Self::flatten_expression(*rhs)), 357 363 }, 364 + Expression::IntRem { lhs, rhs } => Expression::IntRem { 365 + lhs: Box::new(Self::flatten_expression(*lhs)), 366 + rhs: Box::new(Self::flatten_expression(*rhs)), 367 + }, 358 368 Expression::FloatGt { lhs, rhs } => Expression::FloatGt { 359 369 lhs: Box::new(Self::flatten_expression(*lhs)), 360 370 rhs: Box::new(Self::flatten_expression(*rhs)), ··· 443 453 | Expression::IntSub { .. } 444 454 | Expression::IntMul { .. } 445 455 | Expression::IntDiv { .. } 456 + | Expression::IntRem { .. } 446 457 | Expression::FloatGt { .. } 447 458 | Expression::FloatGtEq { .. } 448 459 | Expression::FloatLt { .. } ··· 509 520 lhs: Box::new(Self::remove_unused_code(*lhs)), 510 521 rhs: Box::new(Self::remove_unused_code(*rhs)), 511 522 }, 523 + Expression::IntRem { lhs, rhs } => Expression::IntRem { 524 + lhs: Box::new(Self::flatten_expression(*lhs)), 525 + rhs: Box::new(Self::flatten_expression(*rhs)), 526 + }, 512 527 Expression::FloatGt { lhs, rhs } => Expression::FloatGt { 513 528 lhs: Box::new(Self::remove_unused_code(*lhs)), 514 529 rhs: Box::new(Self::remove_unused_code(*rhs)), ··· 780 795 ast::BinOp::MultFloat => Expression::FloatMul { lhs, rhs }, 781 796 ast::BinOp::DivInt => Expression::IntDiv { lhs, rhs }, 782 797 ast::BinOp::DivFloat => Expression::FloatDiv { lhs, rhs }, 783 - ast::BinOp::RemainderInt => todo!(), 798 + ast::BinOp::RemainderInt => Expression::IntRem { lhs, rhs }, 784 799 ast::BinOp::Concatenate => Expression::StringConcat { lhs, rhs }, 785 800 } 786 801 } ··· 1161 1176 }, 1162 1177 ast::ClauseGuard::RemainderInt { 1163 1178 location: _, 1164 - left: _, 1165 - right: _, 1166 - } => todo!(), 1179 + left, 1180 + right, 1181 + } => Expression::IntRem { 1182 + lhs: Box::new(self.translate_guard(left)), 1183 + rhs: Box::new(self.translate_guard(right)), 1184 + }, 1167 1185 ast::ClauseGuard::Or { 1168 1186 location: _, 1169 1187 left, ··· 1430 1448 let _ = 2 - 1 1431 1449 let _ = 3 * 2 1432 1450 let _ = 4 / 3 1451 + let _ = 5 % 4 1433 1452 1434 1453 let _ = 1.0 +. 0.0 1435 1454 let _ = 2.0 -. 1.0 ··· 1485 1504 x, y if y -. 1.0 <. 0.0 -> 19 1486 1505 x, y if y *. 2.0 == 4.0 -> 20 1487 1506 x, y if y /. 2.0 >=. 1.0 -> 21 1507 + x, y if x % 2 == 0 -> 22 1488 1508 _, _ -> 0 1489 1509 } 1490 1510 }"#
+20 -7
compiler-core/src/cranelift/snapshots/gleam_core__cranelift__mir__tests__translates_samples-5.snap
··· 1 1 --- 2 2 source: compiler-core/src/cranelift/mir.rs 3 - expression: "translate(r#\"pub fn maths() {\n let _ = 1 + 0\n let _ = 2 - 1\n let _ = 3 * 2\n let _ = 4 / 3\n\n let _ = 1.0 +. 0.0\n let _ = 2.0 -. 1.0\n let _ = 3.0 *. 2.0\n let _ = 4.0 /. 3.0\n\n let _ = 1 == 2 && 2 == 4\n let _ = 1 == 2 || 2 == 4\n }\"#)" 3 + expression: "translate(r#\"pub fn maths() {\n let _ = 1 + 0\n let _ = 2 - 1\n let _ = 3 * 2\n let _ = 4 / 3\n let _ = 5 % 4\n\n let _ = 1.0 +. 0.0\n let _ = 2.0 -. 1.0\n let _ = 3.0 *. 2.0\n let _ = 4.0 /. 3.0\n\n let _ = 1 == 2 && 2 == 4\n let _ = 1 == 2 || 2 == 4\n }\"#)" 4 4 --- 5 5 Module { 6 6 functions: [ ··· 66 66 name: Var { 67 67 name: "_tmp$5", 68 68 }, 69 + value: IntRem { 70 + lhs: Int { 71 + value: 5, 72 + }, 73 + rhs: Int { 74 + value: 4, 75 + }, 76 + }, 77 + }, 78 + Set { 79 + name: Var { 80 + name: "_tmp$6", 81 + }, 69 82 value: FloatAdd { 70 83 lhs: Float { 71 84 value: "1.0", ··· 77 90 }, 78 91 Set { 79 92 name: Var { 80 - name: "_tmp$6", 93 + name: "_tmp$7", 81 94 }, 82 95 value: FloatSub { 83 96 lhs: Float { ··· 90 103 }, 91 104 Set { 92 105 name: Var { 93 - name: "_tmp$7", 106 + name: "_tmp$8", 94 107 }, 95 108 value: FloatMul { 96 109 lhs: Float { ··· 103 116 }, 104 117 Set { 105 118 name: Var { 106 - name: "_tmp$8", 119 + name: "_tmp$9", 107 120 }, 108 121 value: FloatDiv { 109 122 lhs: Float { ··· 116 129 }, 117 130 Set { 118 131 name: Var { 119 - name: "_tmp$9", 132 + name: "_tmp$10", 120 133 }, 121 134 value: If { 122 135 cond: Equals { ··· 142 155 }, 143 156 Set { 144 157 name: Var { 145 - name: "_tmp$10", 158 + name: "_tmp$11", 146 159 }, 147 160 value: If { 148 161 cond: Equals { ··· 168 181 }, 169 182 Var( 170 183 Var { 171 - name: "_tmp$10", 184 + name: "_tmp$11", 172 185 }, 173 186 ), 174 187 ],
+52 -4
compiler-core/src/cranelift/snapshots/gleam_core__cranelift__mir__tests__translates_samples-7.snap
··· 1 1 --- 2 2 source: compiler-core/src/cranelift/mir.rs 3 - expression: "translate(r#\"pub fn guard_operations(n: Int, f: Float) -> Int {\n case n, f {\n x, y if x == 0 -> 1\n x, y if x != 1 -> 2\n x, y if x > 2 -> 3\n x, y if x >= 3 -> 4\n x, y if x < 4 -> 5\n x, y if x <= 5 -> 6\n x, y if y >. 1.0 -> 7\n x, y if y >=. 2.0 -> 8\n x, y if y <. 3.0 -> 9\n x, y if y <=. 4.0 -> 10\n x, y if x > 0 && x < 10 -> 11\n x, y if x < 0 || x > 100 -> 12\n x, y if !{ x == 0 } -> 13\n x, y if x + 1 > 5 -> 14\n x, y if x - 1 < 0 -> 15\n x, y if x * 2 == 10 -> 16\n x, y if x / 2 == 1 -> 17\n x, y if y +. 1.0 >. 5.0 -> 18\n x, y if y -. 1.0 <. 0.0 -> 19\n x, y if y *. 2.0 == 4.0 -> 20\n x, y if y /. 2.0 >=. 1.0 -> 21\n _, _ -> 0\n }\n }\"#)" 3 + expression: "translate(r#\"pub fn guard_operations(n: Int, f: Float) -> Int {\n case n, f {\n x, y if x == 0 -> 1\n x, y if x != 1 -> 2\n x, y if x > 2 -> 3\n x, y if x >= 3 -> 4\n x, y if x < 4 -> 5\n x, y if x <= 5 -> 6\n x, y if y >. 1.0 -> 7\n x, y if y >=. 2.0 -> 8\n x, y if y <. 3.0 -> 9\n x, y if y <=. 4.0 -> 10\n x, y if x > 0 && x < 10 -> 11\n x, y if x < 0 || x > 100 -> 12\n x, y if !{ x == 0 } -> 13\n x, y if x + 1 > 5 -> 14\n x, y if x - 1 < 0 -> 15\n x, y if x * 2 == 10 -> 16\n x, y if x / 2 == 1 -> 17\n x, y if y +. 1.0 >. 5.0 -> 18\n x, y if y -. 1.0 <. 0.0 -> 19\n x, y if y *. 2.0 == 4.0 -> 20\n x, y if y /. 2.0 >=. 1.0 -> 21\n x, y if x % 2 == 0 -> 22\n _, _ -> 0\n }\n }\"#)" 4 4 --- 5 5 Module { 6 6 functions: [ ··· 956 956 }, 957 957 ], 958 958 ), 959 - else_: Int { 960 - value: 0, 961 - }, 959 + else_: Block( 960 + [ 961 + Set { 962 + name: Var { 963 + name: "x$47", 964 + }, 965 + value: Var( 966 + Var { 967 + name: "_tmp$3", 968 + }, 969 + ), 970 + }, 971 + If { 972 + cond: Equals { 973 + lhs: IntRem { 974 + lhs: Var( 975 + Var { 976 + name: "x$47", 977 + }, 978 + ), 979 + rhs: Int { 980 + value: 2, 981 + }, 982 + }, 983 + rhs: Int { 984 + value: 0, 985 + }, 986 + }, 987 + then: Block( 988 + [ 989 + Set { 990 + name: Var { 991 + name: "y$48", 992 + }, 993 + value: Var( 994 + Var { 995 + name: "_tmp$4", 996 + }, 997 + ), 998 + }, 999 + Int { 1000 + value: 22, 1001 + }, 1002 + ], 1003 + ), 1004 + else_: Int { 1005 + value: 0, 1006 + }, 1007 + }, 1008 + ], 1009 + ), 962 1010 }, 963 1011 ], 964 1012 ),