@@ -19,6 +19,7 @@ pub const Error = error{
1919 CantCompile ,
2020 UnwrappedNull ,
2121 OutOfBound ,
22+ DivisionByZero ,
2223 ReachedMaximumMemoryUsage ,
2324 WriteFailed ,
2425} || std .mem .Allocator .Error || std .fmt .BufPrintError ;
@@ -2731,7 +2732,7 @@ fn buildBinary(
27312732 },
27322733 else = > unreachable ,
27332734 }
2734- },
2735+ },
27352736 .Star , .StarEqual = > {
27362737 switch (def_type ) {
27372738 .Integer = > {
@@ -2746,12 +2747,44 @@ fn buildBinary(
27462747 }
27472748 },
27482749 .Slash , .SlashEqual = > {
2750+ const zero_label = m .MIR_new_label (self .ctx );
2751+
27492752 switch (def_type ) {
27502753 .Integer = > {
2754+ self .BNE (
2755+ zero_label ,
2756+ right ,
2757+ m .MIR_new_uint_op (self .ctx , Value .fromInteger (0 )),
2758+ );
2759+
2760+ // Exit if division by zero
2761+ // FIXME: would be great to be able to report cleanly the error but how to have string constant in MIR?
2762+ try self .buildExternApiCall (
2763+ .exit ,
2764+ null ,
2765+ &[_ ]m.MIR_op_t {m .MIR_new_uint_op (self .ctx , 1 )},
2766+ );
2767+
2768+ self .append (zero_label );
27512769 self .DIV (dest , left , right );
27522770 try self .wrap (.Integer , dest , dest );
27532771 },
27542772 .Double = > {
2773+ self .BNE (
2774+ zero_label ,
2775+ right ,
2776+ m .MIR_new_uint_op (self .ctx , Value .fromDouble (0 )),
2777+ );
2778+
2779+ // Exit if division by zero
2780+ // FIXME: would be great to be able to report cleanly the error but how to have string constant in MIR?
2781+ try self .buildExternApiCall (
2782+ .exit ,
2783+ null ,
2784+ &[_ ]m.MIR_op_t {m .MIR_new_uint_op (self .ctx , 1 )},
2785+ );
2786+
2787+ self .append (zero_label );
27552788 self .DDIV (left , left , right );
27562789 try self .wrap (.Double , left , dest );
27572790 },
0 commit comments