1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE ForeignFunctionInterface #-}
3 module Mate.X86CodeGen where
7 import qualified Data.Map as M
8 import qualified Data.ByteString.Lazy as B
11 import Foreign.C.Types
15 import qualified JVM.Assembler as J
16 import JVM.Assembler hiding (Instruction)
20 import Harpy.X86Disassembler
22 import Mate.BasicBlocks
24 foreign import ccall "dynamic"
25 code_int :: FunPtr (CInt -> CInt -> IO CInt) -> (CInt -> CInt -> IO CInt)
27 foreign import ccall "getaddr"
30 foreign import ccall "callertrap"
33 foreign import ccall "register_signal"
34 register_signal :: IO ()
36 test_01, test_02, test_03 :: IO ()
39 (entry, end) <- testCase "./tests/Fib.class" "fib"
40 let entryFuncPtr = ((castPtrToFunPtr entry) :: FunPtr (CInt -> CInt -> IO CInt))
43 result <- code_int entryFuncPtr x 0
44 let iresult :: Int; iresult = fromIntegral result
45 let kk :: String; kk = if iresult == (fib x) then "OK" else "FAIL (" ++ (show (fib x)) ++ ")"
46 printf "result of fib(%2d): %3d\t\t%s\n" (fromIntegral x :: Int) iresult kk
47 ) $ ([0..10] :: [CInt])
48 printf "patched disasm:\n"
49 Right newdisasm <- disassembleBlock entry end
50 mapM_ (putStrLn . showAtt) newdisasm
55 | otherwise = (fib (n - 1)) + (fib (n - 2))
59 (entry,_) <- testCase "./tests/While.class" "f"
60 let entryFuncPtr = ((castPtrToFunPtr entry) :: FunPtr (CInt -> CInt -> IO CInt))
61 result <- code_int entryFuncPtr 5 4
62 let iresult :: Int; iresult = fromIntegral result
63 let kk :: String; kk = if iresult == 15 then "OK" else "FAIL"
64 printf "result of f(5,4): %3d\t\t%s\n" iresult kk
66 result2 <- code_int entryFuncPtr 4 3
67 let iresult2 :: Int; iresult2 = fromIntegral result2
68 let kk2 :: String; kk2 = if iresult2 == 10 then "OK" else "FAIL"
69 printf "result of f(4,3): %3d\t\t%s\n" iresult2 kk2
73 (entry,_) <- testCase "./tests/While.class" "g"
74 let entryFuncPtr = ((castPtrToFunPtr entry) :: FunPtr (CInt -> CInt -> IO CInt))
75 result <- code_int entryFuncPtr 5 4
76 let iresult :: Int; iresult = fromIntegral result
77 let kk :: String; kk = if iresult == 15 then "OK" else "FAIL"
78 printf "result of g(5,4): %3d\t\t%s\n" iresult kk
80 result2 <- code_int entryFuncPtr 4 3
81 let iresult2 :: Int; iresult2 = fromIntegral result2
82 let kk2 :: String; kk2 = if iresult2 == 10 then "OK" else "FAIL"
83 printf "result of g(4,3): %3d\t\t%s\n" iresult2 kk2
86 testCase :: String -> B.ByteString -> IO (Ptr Word8, Int)
87 testCase cf method = do
88 cls <- parseClassFile cf
89 hmap <- parseMethod cls method
92 Nothing -> error "sorry, no code generation"
94 let ebb = emitFromBB hmap'
95 (_, Right ((entry, bbstarts, end), disasm)) <- runCodeGen ebb () ()
96 let int_entry = ((fromIntegral $ ptrToIntPtr entry) :: Int)
98 mapM_ (putStrLn . showAtt) disasm
99 printf "basicblocks addresses:\n"
100 let b = map (\(x,y) -> (x,y + int_entry)) $ M.toList bbstarts
101 mapM_ (\(x,y) -> printf "\tBasicBlock %2d starts at 0x%08x\n" x y) b
104 type EntryPoint = Ptr Word8
105 type EntryPointOffset = Int
106 type PatchInfo = (BlockID, EntryPointOffset)
108 type BBStarts = M.Map BlockID Int
110 type CompileInfo = (EntryPoint, BBStarts, Int)
112 emitFromBB :: MapBB -> CodeGen e s (CompileInfo, [Instruction])
114 llmap <- sequence [newNamedLabel ("bb_" ++ show x) | (x,_) <- M.toList hmap]
115 let lmap = zip (Prelude.fst $ unzip $ M.toList hmap) llmap
120 bbstarts <- efBB (0,(hmap M.! 0)) M.empty lmap
123 return ((ep, bbstarts, end), d)
125 getLabel :: BlockID -> [(BlockID, Label)] -> Label
126 getLabel _ [] = error "label not found!"
127 getLabel i ((x,l):xs) = if i==x then l else getLabel i xs
129 efBB :: (BlockID, BasicBlock) -> BBStarts -> [(BlockID, Label)] -> CodeGen e s (BBStarts)
130 efBB (bid, bb) bbstarts lmap =
131 if M.member bid bbstarts then
134 bb_offset <- getCodeOffset
135 let bbstarts' = M.insert bid bb_offset bbstarts
136 defineLabel $ getLabel bid lmap
139 Return -> return bbstarts'
141 efBB (t, hmap M.! t) bbstarts' lmap
142 TwoTarget t1 t2 -> do
143 bbstarts'' <- efBB (t1, hmap M.! t1) bbstarts' lmap
144 efBB (t2, hmap M.! t2) bbstarts'' lmap
145 -- TODO(bernhard): also use metainformation
146 -- TODO(bernhard): implement `emit' as function which accepts a list of
147 -- instructions, so we can use patterns for optimizations
149 emit :: J.Instruction -> CodeGen e s ()
150 emit POP = do -- print dropped value
152 let w32_ep = (fromIntegral $ ptrToIntPtr ep) :: Word32
153 -- '5' is the size of the `call' instruction ( + immediate)
154 calladdr <- getCodeOffset
155 let w32_calladdr = 5 + w32_ep + (fromIntegral calladdr) :: Word32
156 let trapaddr = (fromIntegral getaddr :: Word32)
157 call (trapaddr - w32_calladdr)
158 emit (BIPUSH val) = push ((fromIntegral val) :: Word32)
159 emit (ICONST_1) = push (1 :: Word32)
160 emit (ICONST_2) = push (2 :: Word32)
161 emit (ICONST_5) = push (5 :: Word32)
163 push (Disp (cArgs_ x), ebp)
164 emit (ISTORE_ x) = do
166 mov (Disp (cArgs_ x), ebp) eax
167 emit IADD = do pop ebx; pop eax; add eax ebx; push eax
168 emit ISUB = do pop ebx; pop eax; sub eax ebx; push eax
169 emit (IINC x imm) = do
170 add (Disp (cArgs x), ebp) (s8_w32 imm)
172 emit (IF_ICMP cond _) = do
175 cmp ebx eax -- intel syntax is swapped (TODO(bernhard): test that plz)
176 let sid = case successor bb of TwoTarget _ t -> t; _ -> error "bad"
177 let l = getLabel sid lmap
179 C_EQ -> je l; C_NE -> jne l
180 C_LT -> jl l; C_GT -> jg l
181 C_GE -> jge l; C_LE -> jle l
183 emit (IF cond _) = do
185 cmp eax (0 :: Word32) -- TODO(bernhard): test that plz
186 let sid = case successor bb of TwoTarget _ t -> t; _ -> error "bad"
187 let l = getLabel sid lmap
189 C_EQ -> je l; C_NE -> jne l
190 C_LT -> jl l; C_GT -> jg l
191 C_GE -> jge l; C_LE -> jle l
194 let sid = case successor bb of OneTarget t -> t; _ -> error "bad"
195 jmp $ getLabel sid lmap
196 emit (INVOKESTATIC _) = do
197 -- TODO(bernhard): get and save information about this call
198 -- TODO(bernhard): better try SIGILL instead of SIGSEGV?
200 -- discard arguments (TODO(bernhard): don't hardcode it)
201 add esp (4 :: Word32)
202 -- push result on stack (TODO(bernhard): if any)
205 emit RETURN = do mov esp ebp; pop ebp; ret
211 emit _ = do cmovbe eax eax -- dummy
213 cArgs x = (8 + 4 * (fromIntegral x))
214 cArgs_ x = (8 + 4 * case x of I0 -> 0; I1 -> 1; I2 -> 2; I3 -> 3)
216 -- sign extension from w8 to w32 (over s8)
217 -- unfortunately, hs-java is using Word8 everywhere (while
218 -- it should be Int8 actually)
219 s8_w32 :: Word8 -> Word32
220 s8_w32 w8 = fromIntegral s8
221 where s8 = (fromIntegral w8) :: Int8