aboutsummaryrefslogtreecommitdiff
path: root/X64.hs
blob: 42a88577185d16ae2f480a67a8995bc12a01e4ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
module X64 where

import Control.Monad
import Data.Char
import Data.Int
import Data.List
import Data.Maybe


type Offset = Int64

class Stringifiable a where
    stringify :: a -> String

data Register = RAX | RBX | RCX | RDX | RSI | RDI | RSP | RBP | R8 | R9 | R10 | R11 | R12 | R13 | R14 | R15
  deriving (Show, Eq, Ord, Enum, Bounded)

data XRef = XReg Int Register | XMem Int (Maybe Register) (Int, Register) (Maybe String) Offset | XImm Offset
  deriving (Show, Eq)

newtype Reg = Reg XRef deriving (Show, Eq)
newtype Mem = Mem XRef deriving (Show, Eq)
newtype Imm = Imm XRef deriving (Show, Eq)
newtype RegMem = RegMem XRef deriving (Show, Eq)
newtype RegMemImm = RegMemImm XRef deriving (Show, Eq)

data CondCode = CCA | CCAE | CCB | CCBE | CCC | CCE | CCG | CCGE | CCL | CCLE | CCNA | CCNAE | CCNB | CCNBE | CCNC | CCNE | CCNG | CCNGE | CCNL | CCNLE
  deriving (Show, Eq)

data Ins
    = MOV RegMem RegMem | MOVi RegMem Imm | MOVi64 Reg Imm
    | MOVSX Reg RegMem
    | ADD RegMem RegMemImm
    | SUB RegMem RegMemImm
    | AND RegMem RegMemImm
    | OR RegMem RegMemImm
    | XOR RegMem RegMemImm
    | IMULDA RegMem | IMUL Reg RegMem | IMUL3 Reg RegMem Imm
    | MULDA RegMem
    | IDIVDA RegMem
    | DIVDA RegMem
    | CMP RegMem RegMem | CMPi RegMem Imm
    | SETCC CondCode RegMem
    | CALL String
    | PUSH RegMemImm
    | POP RegMem
    | JMP String
    | JCC CondCode String
    | RET
  deriving (Show, Eq)

type Func = (String, [Ins])

data Asm = Asm [Func]
  deriving (Show, Eq)


class XRefSub a where
    xref :: XRef -> a

instance XRefSub Reg where
    xref x@(XReg _ _) = Reg x
    xref _ = undefined
instance XRefSub Mem where
    xref x@(XMem _ _ _ _ _) = Mem x
    xref _ = undefined
instance XRefSub Imm where
    xref x@(XImm _) = Imm x
    xref _ = undefined
instance XRefSub RegMem where
    xref x@(XReg _ _) = RegMem x
    xref x@(XMem _ _ _ _ _) = RegMem x
    xref x = RegMem x
    -- xref _ = undefined
instance XRefSub RegMemImm where
    xref x = RegMemImm x


verify :: Asm -> Either String ()
verify (Asm funcs) = mapM_ (\(_, inss) -> mapM_ goI inss) funcs
  where
    goI :: Ins -> Either String ()
    goI (MOV (RegMem a) (RegMem b)) = ckRegMem a >> ckRegMem b >> ck2mem a b >> ckSizes a b
    goI (MOVi (RegMem a) (Imm b)) = ckRegMem a >> ckImm b >> ckSizes a b
    goI (MOVi64 (Reg a) (Imm b)) = ckReg a >> ckImm b >> ckSizes64 a b
    goI (MOVSX (Reg a) (RegMem b)) = ckReg a >> ckRegMem b >> ckMovsx a b
    goI (ADD (RegMem a) (RegMemImm b)) = ckRegMem a >> ckRegMemImm b >> ck2mem a b >> ckSizes a b
    goI (SUB (RegMem a) (RegMemImm b)) = ckRegMem a >> ckRegMemImm b >> ck2mem a b >> ckSizes a b
    goI (AND (RegMem a) (RegMemImm b)) = ckRegMem a >> ckRegMemImm b >> ck2mem a b >> ckSizes a b
    goI (OR (RegMem a) (RegMemImm b)) = ckRegMem a >> ckRegMemImm b >> ck2mem a b >> ckSizes a b
    goI (XOR (RegMem a) (RegMemImm b)) = ckRegMem a >> ckRegMemImm b >> ck2mem a b >> ckSizes a b
    goI (IMULDA (RegMem a)) = ckRegMem a
    goI (IMUL (Reg a) (RegMem b)) = ckReg a >> ckRegMem b >> ck2mem a b >> ckSizes a b
    goI (IMUL3 (Reg a) (RegMem b) (Imm c)) = ckReg a >> ckRegMem b >> ckImm c >> ckSizes a b >> ckSizes a c
    goI (MULDA (RegMem a)) = ckRegMem a
    goI (IDIVDA (RegMem a)) = ckRegMem a
    goI (DIVDA (RegMem a)) = ckRegMem a
    goI (CMP (RegMem a) (RegMem b)) = ckRegMem a >> ckRegMem b >> ck2mem a b >> ckSizes a b
    goI (CMPi (RegMem a) (Imm b)) = ckRegMem a >> ckImm b >> ckSizes a b
    goI (SETCC _ (RegMem a)) = ckRegMem a >> ckSizeEq 1 a
    goI (CALL s) = when (null s) $ Left "Empty call target"
    goI (PUSH (RegMemImm a)) = ckRegMemImm a
    goI (POP (RegMem a)) = ckRegMem a
    goI (JMP s) = when (null s) $ Left "Empty jump target"
    goI (JCC _ s) = when (null s) $ Left "Empty jcc target"
    goI RET = return ()

    ckReg (XReg _ _) = return ()
    ckReg _ = Left "Argument is not a Reg"

    ckImm (XImm _) = return ()
    ckImm _ = Left "Argument is not an Imm"

    ckRegMem (XReg _ _) = return ()
    ckRegMem (XMem _ _ _ _ _) = return ()
    ckRegMem _ = Left "Argument is not a Reg or a Mem"

    ckRegMemImm _ = return ()

    ck2mem x@(XMem _ _ _ _ _) y@(XMem _ _ _ _ _) =
        Left $ "Invalid double-memory operands: " ++ show x ++ "; " ++ show y
    ck2mem _ _ = return ()

    ckSizes64 x@(XReg a _) y@(XReg b _) =
        when (a /= b) $ Left $ "Inconsistent operand sizes: " ++ show x ++ "; " ++ show y
    ckSizes64 x@(XReg a _) y@(XMem b _ _ _ _) =
        when (a /= b) $ Left $ "Inconsistent operand sizes: " ++ show x ++ "; " ++ show y
    ckSizes64 x@(XMem a _ _ _ _) y@(XReg b _) =
        when (a /= b) $ Left $ "Inconsistent operand sizes: " ++ show x ++ "; " ++ show y
    ckSizes64 x@(XMem a _ _ _ _) y@(XMem b _ _ _ _) =
        when (a /= b) $ Left $ "Inconsistent operand sizes: " ++ show x ++ "; " ++ show y
    ckSizes64 x@(XReg a _) y@(XImm v) =
        when (not $ fitsIn v a) $ Left $ "Immediate too large: " ++ show x ++ "; " ++ show y
    ckSizes64 x@(XMem a _ _ _ _) y@(XImm v) =
        when (not $ fitsIn v a) $ Left $ "Immediate too large: " ++ show x ++ "; " ++ show y
    ckSizes64 _ _ = undefined

    ckSizes a b@(XImm v) = when (v >= 2 ^ (32::Int)) (Left "Immediate too large") >> ckSizes64 a b
    ckSizes a b = ckSizes64 a b

    ckMovsx (XReg a _) (XReg b _) = when (a <= b) $ Left "MOVSX with shrinking operands"
    ckMovsx (XReg a _) (XMem b _ _ _ _) = when (a <= b) $ Left "MOVSX with shrinking operands"
    ckMovsx (XMem a _ _ _ _) (XReg b _) = when (a <= b) $ Left "MOVSX with shrinking operands"
    ckMovsx (XMem a _ _ _ _) (XMem b _ _ _ _) = when (a <= b) $ Left "MOVSX with shrinking operands"
    ckMovsx _ _ = undefined

    ckSizeEq sz (XReg a _) = when (a /= sz) $ Left "Invalid operand size"
    ckSizeEq sz (XMem a _ _ _ _) = when (a /= sz) $ Left "Invalid operand size"
    ckSizeEq sz (XImm v) = when (v >= 2 ^ (8 * sz)) $ Left "Invalid operand size (immediate)"

    fitsIn v sz = (fromIntegral v :: Integer) < ((2 :: Integer) ^ (8 * sz))


instance Stringifiable XRef where
    stringify (XReg sz reg) =
        let n = fromEnum reg
        in case n of
               _ | n < 4 -> erPrefix $ lxSuffix $ [chr (n + ord 'a')]
                 | n < 8 -> erPrefix $ lSuffix $ ["si", "di", "sp", "bp"] !! (n - 4)
                 | otherwise -> bwdSuffix $ 'r' : show n
      where
        erPrefix s = case sz of
            4 -> 'e' : s
            8 -> 'r' : s
            _ -> s
        lxSuffix s = case sz of
            1 -> s ++ "l"
            _ -> s ++ "x"
        lSuffix s = case sz of
            1 -> s ++ "l"
            _ -> s
        bwdSuffix s = case sz of
            1 -> s ++ "b"
            2 -> s ++ "w"
            4 -> s ++ "d"
            _ -> s

    stringify (XMem _ _ (mult, _) _ _) | not (mult `elem` [0,1,2,4,8]) =
        error $ "Register multiplier has invalid value " ++ show mult ++ " in XMem"
    stringify (XMem sz mr pair lab off) =
        let res = intercalate "+" $ catMaybes [goR1 mr, goPair pair, goLab lab, goOff off]
        in szword sz ++ " " ++ if null res then "[0]" else "[" ++ res ++ "]"
      where
        szword 1 = "byte"
        szword 2 = "word"
        szword 4 = "dword"
        szword 8 = "qword"
        szword _ = undefined
        goR1 Nothing = Nothing
        goR1 (Just r) = Just $ stringify (XReg 8 r)
        goPair (0, _) = Nothing
        goPair (mult, r) = Just $ show mult ++ "*" ++ stringify (XReg 8 r)
        goLab = id
        goOff 0 = Nothing
        goOff o = Just $ show o

    stringify (XImm imm) = show imm

instance Stringifiable Reg where stringify (Reg x) = stringify x
instance Stringifiable Mem where stringify (Mem x) = stringify x
instance Stringifiable Imm where stringify (Imm x) = stringify x
instance Stringifiable RegMem where stringify (RegMem x) = stringify x
instance Stringifiable RegMemImm where stringify (RegMemImm x) = stringify x

instance Stringifiable CondCode where
    stringify CCA = "a"
    stringify CCAE = "ae"
    stringify CCB = "b"
    stringify CCBE = "be"
    stringify CCC = "c"
    stringify CCE = "e"
    stringify CCG = "g"
    stringify CCGE = "ge"
    stringify CCL = "l"
    stringify CCLE = "le"
    stringify CCNA = "na"
    stringify CCNAE = "nae"
    stringify CCNB = "nb"
    stringify CCNBE = "nbe"
    stringify CCNC = "nc"
    stringify CCNE = "ne"
    stringify CCNG = "ng"
    stringify CCNGE = "nge"
    stringify CCNL = "nl"
    stringify CCNLE = "nle"

instance Stringifiable Ins where
    stringify (MOV a b) = "mov " ++ stringify a ++ ", " ++ stringify b
    stringify (MOVi a b) = "mov " ++ stringify a ++ ", " ++ stringify b
    stringify (MOVi64 a b) = "mov " ++ stringify a ++ ", " ++ stringify b
    stringify (MOVSX a b@(RegMem bx)) = case compare (xrefGetSize bx) 4 of
        EQ -> "movsxd " ++ stringify a ++ ", " ++ stringify b
        LT -> "movsx " ++ stringify a ++ ", " ++ stringify b
        GT -> undefined
    stringify (ADD a b) = "add " ++ stringify a ++ ", " ++ stringify b
    stringify (SUB a b) = "sub " ++ stringify a ++ ", " ++ stringify b
    stringify (AND a b) = "and " ++ stringify a ++ ", " ++ stringify b
    stringify (OR a b) = "or " ++ stringify a ++ ", " ++ stringify b
    stringify (XOR a b) = "xor " ++ stringify a ++ ", " ++ stringify b
    stringify (IMULDA a) = "imul " ++ stringify a
    stringify (IMUL a b) = "imul " ++ stringify a ++ ", " ++ stringify b
    stringify (IMUL3 a b c) = "imul " ++ stringify a ++ ", " ++ stringify b ++ ", " ++ stringify c
    stringify (MULDA a) = "mul " ++ stringify a
    stringify (IDIVDA a) = "idiv " ++ stringify a
    stringify (DIVDA a) = "div " ++ stringify a
    stringify (CMP a b) = "cmp " ++ stringify a ++ ", " ++ stringify b
    stringify (CMPi a b) = "cmp " ++ stringify a ++ ", " ++ stringify b
    stringify (SETCC cc a) = "set" ++ stringify cc ++ " " ++ stringify a
    stringify (CALL a) = "call " ++ a
    stringify (PUSH a) = "push " ++ stringify a
    stringify (POP a) = "pop " ++ stringify a
    stringify (JMP s) = "jmp " ++ s
    stringify (JCC cc s) = "j" ++ stringify cc ++ " " ++ s
    stringify RET = "ret"

instance Stringifiable Asm where
    stringify (Asm funcs) = intercalate "\n" $ map goF funcs
      where
        goF :: (String, [Ins]) -> String
        goF (name, inss) = name ++ ":\n" ++ unlines (map (('\t' :) . stringify) inss)

xrefGetSize :: XRef -> Int
xrefGetSize (XReg s _) = s
xrefGetSize (XMem s _ _ _ _) = s
xrefGetSize (XImm _) = undefined

xrefSetSize :: Int -> XRef -> XRef
xrefSetSize sz (XReg _ r) = XReg sz r
xrefSetSize sz (XMem _ a b c d) = XMem sz a b c d
xrefSetSize _ x@(XImm _) = x

isXReg :: XRef -> Bool
isXReg (XReg _ _) = True
isXReg _ = False

isXMem :: XRef -> Bool
isXMem (XMem _ _ _ _ _) = True
isXMem _ = False

isXImm :: XRef -> Bool
isXImm (XImm _) = True
isXImm _ = False