1 {-# LANGUAGE OverloadedStrings #-}
2 module Mate.X86CodeGen where
8 import qualified Data.Map as M
9 import qualified Data.ByteString.Lazy as B
13 import Foreign.C.Types
17 import qualified JVM.Assembler as J
18 import JVM.Assembler hiding (Instruction)
21 import Harpy.X86Disassembler
23 import Mate.BasicBlocks
25 test_01, test_02, test_03 :: IO ()
26 test_01 = testCase "./tests/Fib.class" "fib"
27 test_02 = testCase "./tests/While.class" "f"
28 test_03 = testCase "./tests/While.class" "g"
30 testCase :: String -> B.ByteString -> IO ()
31 testCase cf method = do
32 hmap <- parseMethod cf method
35 Nothing -> putStrLn "sorry, no code generation"
37 let ebb = emitFromBB hmap
38 (_, Right ((entry, bbstarts), disasm)) <- runCodeGen ebb () ()
39 let int_entry = ((fromIntegral $ ptrToIntPtr entry) :: Int)
41 mapM_ (putStrLn . showAtt) disasm
42 printf "basicblocks addresses:\n"
43 let b = map (\(x,y) -> (x,y + int_entry)) $ M.toList bbstarts
44 mapM_ (\(x,y) -> printf "\tBasicBlock %2d starts at 0x%08x\n" x y) b
46 type EntryPoint = Ptr Word8
47 type EntryPointOffset = Int
48 type PatchInfo = (BlockID, EntryPointOffset)
50 type BBStarts = M.Map BlockID Int
52 type CompileInfo = (EntryPoint, BBStarts)
54 emitFromBB :: MapBB -> CodeGen e s (CompileInfo, [Instruction])
56 llmap <- sequence [newNamedLabel ("bb_" ++ show x) | (x,_) <- M.toList hmap]
57 let lmap = zip (Prelude.fst $ unzip $ M.toList hmap) llmap
61 bbstarts <- efBB (0,(hmap M.! 0)) M.empty lmap
66 return ((ep, bbstarts), d)
68 getLabel :: BlockID -> [(BlockID, Label)] -> Label
69 getLabel _ [] = error "label not found!"
70 getLabel i ((x,l):xs) = if i==x then l else getLabel i xs
72 efBB :: (BlockID, BasicBlock) -> BBStarts -> [(BlockID, Label)] -> CodeGen e s (BBStarts)
73 efBB (bid, bb) bbstarts lmap =
74 if M.member bid bbstarts then
77 bb_offset <- getCodeOffset
78 let bbstarts' = M.insert bid bb_offset bbstarts
79 defineLabel $ getLabel bid lmap
82 Return -> return bbstarts'
84 efBB (t, hmap M.! t) bbstarts' lmap
86 bbstarts'' <- efBB (t1, hmap M.! t1) bbstarts' lmap
87 efBB (t2, hmap M.! t2) bbstarts'' lmap
88 -- TODO(bernhard): also use metainformation
89 -- TODO(bernhard): implement `emit' as function which accepts a list of
90 -- instructions, so we can use patterns for optimizations
92 emit :: J.Instruction -> CodeGen e s ()
93 emit (ICONST_1) = push (1 :: Word32)
94 emit (ICONST_2) = push (2 :: Word32)
96 push (Disp (cArgs_ x), ebp)
99 mov (Disp (cArgs_ x), ebp) eax
100 emit IADD = do pop ebx; pop eax; add eax ebx; push eax
101 emit ISUB = do pop ebx; pop eax; sub eax ebx; push eax
102 emit (IINC x imm) = do
103 add (Disp (cArgs x), ebp) (s8_w32 imm)
105 emit (IF_ICMP cond _) = do
108 cmp eax ebx -- intel syntax is swapped (TODO(bernhard): test that plz)
109 let sid = case successor bb of TwoTarget _ t -> t
110 let l = getLabel sid lmap
112 C_EQ -> je l; C_NE -> jne l
113 C_LT -> jl l; C_GT -> jg l
114 C_GE -> jge l; C_LE -> jle l
116 emit (IF cond _) = do
118 cmp eax (0 :: Word32) -- TODO(bernhard): test that plz
119 let sid = case successor bb of TwoTarget _ t -> t
120 let l = getLabel sid lmap
122 C_EQ -> je l; C_NE -> jne l
123 C_LT -> jl l; C_GT -> jg l
124 C_GE -> jge l; C_LE -> jle l
127 let sid = case successor bb of OneTarget t -> t
128 jmp $ getLabel sid lmap
130 emit IRETURN = do pop eax
131 emit _ = do cmovbe eax eax -- dummy
133 cArgs x = (8 + 4 * (fromIntegral x))
134 cArgs_ x = (8 + 4 * case x of I0 -> 0; I1 -> 1; I2 -> 2; I3 -> 3)
136 -- sign extension from w8 to w32 (over s8)
137 -- unfortunately, hs-java is using Word8 everywhere (while
138 -- it should be Int8 actually)
139 s8_w32 :: Word8 -> Word32
140 s8_w32 w8 = fromIntegral s8
141 where s8 = (fromIntegral w8) :: Int8