-
Notifications
You must be signed in to change notification settings - Fork 4
/
Display.hs
73 lines (59 loc) · 2.47 KB
/
Display.hs
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
{-# LANGUAGE FlexibleInstances, LambdaCase, OverloadedStrings #-}
module Ebpf.Display where
import Data.Text.Display
import qualified Data.Text.Lazy.Builder as TB
import qualified Data.Char as C
import qualified Data.Text as T
import Ebpf.Asm
instance Display Reg where
displayBuilder (Reg i) = "r" <> displayBuilder i
lowercaseShow :: Show a => a -> TB.Builder
lowercaseShow x = displayBuilder $ map C.toLower $ show x
instance Display BinAlu where
displayBuilder = lowercaseShow
instance Display UnAlu where
displayBuilder = lowercaseShow
instance Display BSize where
displayBuilder = TB.fromText . \case B8 -> "8" ; B16 -> "16" ; B32 -> "32" ; B64 -> "64"
instance Display Jcmp where
displayBuilder = lowercaseShow
instance Display RegImm where
displayBuilder (R r) = displayBuilder r
displayBuilder (Imm n) = displayBuilder n
displayMemLocBuilder r moff = mconcat ["[", displayBuilder r, off, "]"]
where off = case moff of
Just i | i /= 0 -> " +" <> displayBuilder i
_ -> ""
memSz = \case B8 -> "b" ; B16 -> "h" ; B32 -> "w" ; B64 -> "dw"
instance Display (Inst Reg Imm RegImm HelperId) where
displayBuilder instr =
mconcat $
case instr of
Binary bsz alu r ir ->
[displayBuilder alu, displayBuilder bsz, " ", displayBuilder r, ", ", displayBuilder ir]
Unary bsz alu r ->
[displayBuilder alu, displayBuilder bsz, " ", displayBuilder r]
Store bsz r moff ir ->
["st", x, memSz bsz, " ", displayMemLocBuilder r moff, ", ", src]
where (x, src) = case ir of
R r -> ("x", displayBuilder r)
Imm i -> ("", displayBuilder i)
Load bsz dst src moff ->
["ldx", memSz bsz, " ", displayBuilder dst, ", ", displayMemLocBuilder src moff]
LoadImm r c ->
["lddw ", displayBuilder r, ", ", displayBuilder c]
LoadMapFd r c ->
["load_map_fd ", displayBuilder r, ", ", displayBuilder c]
LoadAbs bsz i ->
["ldabs", memSz bsz, " ", displayBuilder i]
LoadInd bsz src i ->
["ldind", memSz bsz, " ", displayBuilder src, " ", displayBuilder i]
JCond cmp r ir off ->
[displayBuilder cmp, " ", displayBuilder r, ", ", displayBuilder ir,
", +", displayBuilder off]
Jmp off -> ["ja +", displayBuilder off]
Call f -> ["call ", displayBuilder f]
Exit -> ["exit"]
displayProgram :: Program -> T.Text
displayProgram prog =
T.concat $ map (\i -> display i <> "\n") prog