From 298fa787da01ded60f2b9d02c9529760aabd2921 Mon Sep 17 00:00:00 2001 From: Tristan Gingold Date: Sun, 2 Oct 2022 13:57:49 +0200 Subject: translate, grt: add lib function for div and rem. Do not rely on hardware exceptions to catch division by 0, they are caught in windows by the c handler and not propagated --- src/grt/grt-lib.adb | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/grt/grt-lib.ads | 12 ++++++++++++ 2 files changed, 56 insertions(+) (limited to 'src/grt') diff --git a/src/grt/grt-lib.adb b/src/grt/grt-lib.adb index f69da860f..ec64f810f 100644 --- a/src/grt/grt-lib.adb +++ b/src/grt/grt-lib.adb @@ -271,6 +271,50 @@ package body Grt.Lib is return Res; end Ghdl_I64_Exp; + function Ghdl_I32_Div (L, R : Ghdl_I32) return Ghdl_I32 + is + pragma Suppress (Overflow_Check); + begin + if R = 0 then + Error ("division by 0"); + elsif R = -1 and L = Ghdl_I32'First then + Error ("overflow in division"); + end if; + return L / R; + end Ghdl_I32_Div; + + function Ghdl_I64_Div (L, R : Ghdl_I64) return Ghdl_I64 + is + pragma Suppress (Overflow_Check); + begin + if R = 0 then + Error ("division by 0"); + elsif R = -1 and L = Ghdl_I64'First then + Error ("overflow in division"); + end if; + return L / R; + end Ghdl_I64_Div; + + function Ghdl_I32_Mod (L, R : Ghdl_I32) return Ghdl_I32 + is + pragma Suppress (Overflow_Check); + begin + if R = 0 then + Error ("division by 0"); + end if; + return L mod R; + end Ghdl_I32_Mod; + + function Ghdl_I64_Mod (L, R : Ghdl_I64) return Ghdl_I64 + is + pragma Suppress (Overflow_Check); + begin + if R = 0 then + Error ("division by 0"); + end if; + return L mod R; + end Ghdl_I64_Mod; + procedure Ghdl_Check_Stack_Allocation (Size : Ghdl_Index_Type) is Bt : Backtrace_Addrs; diff --git a/src/grt/grt-lib.ads b/src/grt/grt-lib.ads index f8e7f0a7a..0210057fa 100644 --- a/src/grt/grt-lib.ads +++ b/src/grt/grt-lib.ads @@ -69,6 +69,12 @@ package Grt.Lib is function Ghdl_I32_Exp (V : Ghdl_I32; E : Std_Integer) return Ghdl_I32; function Ghdl_I64_Exp (V : Ghdl_I64; E : Std_Integer) return Ghdl_I64; + function Ghdl_I32_Div (L, R : Ghdl_I32) return Ghdl_I32; + function Ghdl_I64_Div (L, R : Ghdl_I64) return Ghdl_I64; + + function Ghdl_I32_Mod (L, R : Ghdl_I32) return Ghdl_I32; + function Ghdl_I64_Mod (L, R : Ghdl_I64) return Ghdl_I64; + -- Called before allocation of large (complex) objects. procedure Ghdl_Check_Stack_Allocation (Size : Ghdl_Index_Type); @@ -141,6 +147,12 @@ private pragma Export (C, Ghdl_I64_Exp, "__ghdl_i64_exp"); pragma Export (C, Ghdl_Real_Exp, "__ghdl_real_exp"); + pragma Export (C, Ghdl_I32_Div, "__ghdl_i32_div"); + pragma Export (C, Ghdl_I64_Div, "__ghdl_i64_div"); + + pragma Export (C, Ghdl_I32_Mod, "__ghdl_i32_mod"); + pragma Export (C, Ghdl_I64_Mod, "__ghdl_i64_mod"); + pragma Export (C, Ghdl_Std_Ulogic_To_Boolean_Array, "__ghdl_std_ulogic_to_boolean_array"); -- cgit v1.2.3