Skip to content

Commit 4b5940a

Browse files
committed
Fix overflow operations
1 parent 0d77317 commit 4b5940a

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Diff for: src/int.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
404404

405405
let ret_indirect = matches!(fn_abi.ret.mode, PassMode::Indirect { .. });
406406

407-
let result = if ret_indirect {
407+
let call = if ret_indirect {
408408
let res_value = self.current_func().new_local(self.location, res_type, "result_value");
409409
let res_addr = res_value.get_address(self.location);
410410
let res_param_type = res_type.make_pointer();
@@ -432,8 +432,17 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
432432
);
433433
self.context.new_call(self.location, func, &[lhs, rhs, overflow_addr])
434434
};
435-
436-
(result, self.context.new_cast(self.location, overflow_value, self.bool_type).to_rvalue())
435+
// NOTE: we must assign the result of the operation to a variable at this point to make
436+
// sure it will be evaluated by libgccjit now.
437+
// Otherwise, it will only be evaluated when the rvalue for the call is used somewhere else
438+
// and overflow_value will not be initialized at the correct point in the program.
439+
let result = self.current_func().new_local(self.location, res_type, "result");
440+
self.block.add_assignment(self.location, result, call);
441+
442+
(
443+
result.to_rvalue(),
444+
self.context.new_cast(self.location, overflow_value, self.bool_type).to_rvalue(),
445+
)
437446
}
438447

439448
pub fn gcc_icmp(
@@ -865,6 +874,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
865874
let value_type = value.get_type();
866875
if self.is_native_int_type_or_bool(dest_typ) && self.is_native_int_type_or_bool(value_type)
867876
{
877+
// TODO: use self.location.
868878
self.context.new_cast(None, value, dest_typ)
869879
} else if self.is_native_int_type_or_bool(dest_typ) {
870880
self.context.new_cast(None, self.low(value), dest_typ)

0 commit comments

Comments
 (0)