summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Gamari <ben@well-typed.com>2022-06-23 12:41:12 -0400
committerMarge Bot <ben+marge-bot@smart-cactus.org>2022-06-28 03:24:24 -0400
commit696d64c347364ce75e23d37884ec0bd2543b0a6a (patch)
treed5c7686431a4a4e751cd462ecd16359ec78336d8
parent70f0c1f84213f7a09bc31e0eeefb5b089708f04b (diff)
downloadhaskell-696d64c347364ce75e23d37884ec0bd2543b0a6a.tar.gz
CmmToAsm/AArch64: Sign-extend narrow C arguments
The AArch64/Darwin ABI requires that function arguments narrower than 32-bits must be sign-extended by the caller. We neglected to do this, resulting in #20735. Fixes #20735.
-rw-r--r--compiler/GHC/CmmToAsm/AArch64/CodeGen.hs16
1 files changed, 14 insertions, 2 deletions
diff --git a/compiler/GHC/CmmToAsm/AArch64/CodeGen.hs b/compiler/GHC/CmmToAsm/AArch64/CodeGen.hs
index 9509ffb467..0071f291a1 100644
--- a/compiler/GHC/CmmToAsm/AArch64/CodeGen.hs
+++ b/compiler/GHC/CmmToAsm/AArch64/CodeGen.hs
@@ -1620,9 +1620,21 @@ genCCall target dest_regs arg_regs bid = do
-- size we want to pack. Failure to get this right can result in pretty
-- subtle bugs, e.g. #20137.
- passArguments pack (gpReg:gpRegs) fpRegs ((r, format, _hint, code_r):args) stackSpace accumRegs accumCode | isIntFormat format = do
+ passArguments pack (gpReg:gpRegs) fpRegs ((r, format, hint, code_r):args) stackSpace accumRegs accumCode | isIntFormat format = do
+ platform <- getPlatform
let w = formatToWidth format
- mov = MOV (OpReg w gpReg) (OpReg w r)
+ mov
+ -- Specifically, Darwin/AArch64's ABI requires that the caller
+ -- sign-extend arguments which are smaller than 32-bits.
+ | w < W32
+ , platformCConvNeedsExtension platform
+ , SignedHint <- hint
+ = case w of
+ W8 -> SXTB (OpReg W64 gpReg) (OpReg w r)
+ W16 -> SXTH (OpReg W64 gpReg) (OpReg w r)
+ _ -> panic "impossible"
+ | otherwise
+ = MOV (OpReg w gpReg) (OpReg w r)
accumCode' = accumCode `appOL`
code_r `snocOL`
ann (text "Pass gp argument: " <> ppr r) mov