glasgow_exts.rst: fix code block
[ghc.git] / compiler / llvmGen / LlvmMangler.hs
1 -- -----------------------------------------------------------------------------
2 -- | GHC LLVM Mangler
3 --
4 -- This script processes the assembly produced by LLVM, rewriting all symbols
5 -- of type @function to @object. This keeps them from going through the PLT,
6 -- which would be bad due to tables-next-to-code. On x86_64,
7 -- it also rewrites AVX instructions that require alignment to their
8 -- unaligned counterparts, since the stack is only 16-byte aligned but these
9 -- instructions require 32-byte alignment.
10 --
11
12 module LlvmMangler ( llvmFixupAsm ) where
13
14 import DynFlags ( DynFlags, targetPlatform )
15 import Platform ( platformArch, Arch(..) )
16 import ErrUtils ( showPass )
17
18 import Control.Exception
19 import qualified Data.ByteString.Char8 as B
20 import System.IO
21
22 -- | Read in assembly file and process
23 llvmFixupAsm :: DynFlags -> FilePath -> FilePath -> IO ()
24 llvmFixupAsm dflags f1 f2 = {-# SCC "llvm_mangler" #-} do
25 showPass dflags "LLVM Mangler"
26 withBinaryFile f1 ReadMode $ \r -> withBinaryFile f2 WriteMode $ \w -> do
27 go r w
28 hClose r
29 hClose w
30 return ()
31 where
32 go :: Handle -> Handle -> IO ()
33 go r w = do
34 e_l <- try $ B.hGetLine r ::IO (Either IOError B.ByteString)
35 let writeline a = B.hPutStrLn w (rewriteLine dflags rewrites a) >> go r w
36 case e_l of
37 Right l -> writeline l
38 Left _ -> return ()
39
40 -- | These are the rewrites that the mangler will perform
41 rewrites :: [Rewrite]
42 rewrites = [rewriteSymType, rewriteAVX]
43
44 type Rewrite = DynFlags -> B.ByteString -> Maybe B.ByteString
45
46 -- | Rewrite a line of assembly source with the given rewrites,
47 -- taking the first rewrite that applies.
48 rewriteLine :: DynFlags -> [Rewrite] -> B.ByteString -> B.ByteString
49 rewriteLine dflags rewrites l =
50 case firstJust $ map (\rewrite -> rewrite dflags rest) rewrites of
51 Nothing -> l
52 Just rewritten -> B.concat $ [symbol, B.pack "\t", rewritten]
53 where
54 (symbol, rest) = splitLine l
55
56 firstJust :: [Maybe a] -> Maybe a
57 firstJust (Just x:_) = Just x
58 firstJust [] = Nothing
59 firstJust (_:rest) = firstJust rest
60
61 -- | This rewrites @.type@ annotations of function symbols to @%object@.
62 -- This is done as the linker can relocate @%functions@ through the
63 -- Procedure Linking Table (PLT). This is bad since we expect that the
64 -- info table will appear directly before the symbol's location. In the
65 -- case that the PLT is used, this will be not an info table but instead
66 -- some random PLT garbage.
67 rewriteSymType :: Rewrite
68 rewriteSymType _ l
69 | isType l = Just $ rewrite '@' $ rewrite '%' l
70 | otherwise = Nothing
71 where
72 isType = B.isPrefixOf (B.pack ".type")
73
74 rewrite :: Char -> B.ByteString -> B.ByteString
75 rewrite prefix = replaceOnce funcType objType
76 where
77 funcType = prefix `B.cons` B.pack "function"
78 objType = prefix `B.cons` B.pack "object"
79
80 -- | This rewrites aligned AVX instructions to their unaligned counterparts on
81 -- x86-64. This is necessary because the stack is not adequately aligned for
82 -- aligned AVX spills, so LLVM would emit code that adjusts the stack pointer
83 -- and disable tail call optimization. Both would be catastrophic here so GHC
84 -- tells LLVM that the stack is 32-byte aligned (even though it isn't) and then
85 -- rewrites the instructions in the mangler.
86 rewriteAVX :: Rewrite
87 rewriteAVX dflags s
88 | not isX86_64 = Nothing
89 | isVmovdqa s = Just $ replaceOnce (B.pack "vmovdqa") (B.pack "vmovdqu") s
90 | isVmovap s = Just $ replaceOnce (B.pack "vmovap") (B.pack "vmovup") s
91 | otherwise = Nothing
92 where
93 isX86_64 = platformArch (targetPlatform dflags) == ArchX86_64
94 isVmovdqa = B.isPrefixOf (B.pack "vmovdqa")
95 isVmovap = B.isPrefixOf (B.pack "vmovap")
96
97 -- | @replaceOnce match replace bs@ replaces the first occurrence of the
98 -- substring @match@ in @bs@ with @replace@.
99 replaceOnce :: B.ByteString -> B.ByteString -> B.ByteString -> B.ByteString
100 replaceOnce matchBS replaceOnceBS = loop
101 where
102 loop :: B.ByteString -> B.ByteString
103 loop cts =
104 case B.breakSubstring matchBS cts of
105 (hd,tl) | B.null tl -> hd
106 | otherwise -> hd `B.append` replaceOnceBS `B.append`
107 B.drop (B.length matchBS) tl
108
109 -- | This function splits a line of assembly code into the label and the
110 -- rest of the code.
111 splitLine :: B.ByteString -> (B.ByteString, B.ByteString)
112 splitLine l = (symbol, B.dropWhile isSpace rest)
113 where
114 isSpace ' ' = True
115 isSpace '\t' = True
116 isSpace _ = False
117 (symbol, rest) = B.span (not . isSpace) l