Created
March 9, 2026 15:57
-
-
Save alt-romes/12cfb751f586b0f78dbf6420ec598fef to your computer and use it in GitHub Desktop.
Allocating Maybe in Cmm foreign import Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE GHCForeignImportPrim, MagicHash, UnliftedFFITypes, UnboxedTuples #-} | |
| import GHC.Exts | |
| foreign import prim "test" tt :: Word# -> Addr# | |
| main = do | |
| case addrToAny# (tt 0##) of | |
| (# mb #) -> print (mb :: Maybe (Maybe ())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ghc Main.hs X.cmm | |
| ./Main # expected result: Just Nothing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "Cmm.h" | |
| test (W_ r1) | |
| { | |
| I64[Hp - 16] = ghczminternal_GHCziInternalziMaybe_Just_con_info; | |
| I64[Hp - 8] = ghczminternal_GHCziInternalziMaybe_Nothing_closure; | |
| // Tag 0: Unevaluated | |
| // Tag 1: Nothing | |
| // Tag 2: Just <-- use this one | |
| return (Hp - 14); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment