From 8538bf5fc3529fd7165c42d9cab522d2f678c7de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Thu, 18 Sep 2025 12:18:15 +0000 Subject: [PATCH] Use initializerCStub to create C stubs with constructor functions --- src/GhcPlugins/Extras.hs | 2 ++ src/Language/Java/Inline/Plugin.hs | 29 +++++++++++++++-------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/GhcPlugins/Extras.hs b/src/GhcPlugins/Extras.hs index 3c82db05..7c85fbfb 100644 --- a/src/GhcPlugins/Extras.hs +++ b/src/GhcPlugins/Extras.hs @@ -7,11 +7,13 @@ module GhcPlugins.Extras , module GhcPlugins.Extras , module GHC.Core.TyCo.Rep , module GHC.Types.ForeignStubs + , module GHC.Cmm.CLabel ) where import Control.Monad.Writer import Data.Data (Data) import Data.Maybe (mapMaybe) +import GHC.Cmm.CLabel (mkInitializerStubLabel) import GHC.Core.FamInstEnv import GHC.Core.Reduction (Reduction(..)) import GHC.Core.TyCo.Rep diff --git a/src/Language/Java/Inline/Plugin.hs b/src/Language/Java/Inline/Plugin.hs index 86cfd113..9f4f5210 100644 --- a/src/Language/Java/Inline/Plugin.hs +++ b/src/Language/Java/Inline/Plugin.hs @@ -49,7 +49,7 @@ import Prelude hiding ((<>)) -- -- Finally, it calls the java compiler to produce the bytecode and -- arranges to have it inserted in the bytecode table in constructor functions --- (cConstructors). +-- (cConstructor). plugin :: Plugin plugin = defaultPlugin @@ -82,15 +82,15 @@ plugin = defaultPlugin dcs <- buildJava guts qqOccs jimports >>= maybeDumpJava args >>= buildBytecode args + dflags <- getDynFlags + let cstub = + cConstructor + dflags + (mg_module guts) + (text bctable_header $$ dotClasses dcs) return guts { mg_binds = binds - , mg_foreign = appendStubC (mg_foreign guts) $ - CStub - ( text bctable_header - $$ dotClasses dcs - $$ cConstructors - ) - [] [] + , mg_foreign = appendStubC (mg_foreign guts) cstub } -- The contents of bctable.h @@ -460,9 +460,10 @@ dotClasses dcs = vcat $ -- | Produces the constructor function which inserts the static structures -- generated by 'dotClasses' into the bytecode table. -cConstructors :: SDoc -cConstructors = vcat - [ text "static void hs_inline_java_init(void) __attribute__((constructor));" - , text "static void hs_inline_java_init(void)" - , text "{ inline_java_bctable = inline_java_new_pack(inline_java_bctable, dcs, dc_count); }" - ] +cConstructor :: DynFlags -> Module -> SDoc -> CStub +cConstructor dflags this_mod decls = + initializerCStub + (targetPlatform dflags) + (mkInitializerStubLabel this_mod "inline_java_bctable") + decls + (text "{ inline_java_bctable = inline_java_new_pack(inline_java_bctable, dcs, dc_count); }")