-
Notifications
You must be signed in to change notification settings - Fork 4
/
AsmParser.hs
192 lines (156 loc) · 6.2 KB
/
AsmParser.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
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
{-# LANGUAGE NamedFieldPuns #-}
module Ebpf.AsmParser where
import Ebpf.Asm
import qualified Data.Char as C
import qualified Data.Map.Strict as M
import Data.Map.Strict (Map)
import Data.Int (Int64, Int32)
import Data.Bifunctor (first)
import Text.Parsec
import qualified Text.Parsec as PS
type Parser u a = Parsec String u a
ignore :: Parser u a -> Parser u ()
ignore p = p >> return ()
comment = ignore $ char ';' >> manyTill anyChar (try endOfLine)
sc = skipMany (comment <|> ignore space)
lexeme :: Parser u a -> Parser u a
lexeme p = p <* sc
symbol s = lexeme $ string s
operator = lexeme $ many1 alphaNum
rawReg :: Parser u Reg
rawReg = Reg . read <$> lexeme (char 'r' >> many1 digit) <?> "register"
rawImm :: Parser u Int64
rawImm = lexeme number <?> "immediate constant"
where
number = sign <*> unsigned
unsigned = (char '0' >> (hex <|> decimal <|> return 0))
<|> decimal
hex = read . ("0x" ++) <$> (oneOf "Xx" >> many1 hexDigit)
decimal = read <$> many1 digit
sign = (char '-' >> return negate)
<|> (optional (char '+') >> return id)
memoryOffset = rawImm
codeOffset = rawImm
rawImm32 :: Parser u Int32
rawImm32 = lexeme number <?> "extern function"
where
number = sign <*> unsigned
unsigned = (char '0' >> (hex <|> decimal <|> return 0))
<|> decimal
hex = read . ("0x" ++) <$> (oneOf "Xx" >> many1 hexDigit)
decimal = read <$> many1 digit
sign = (char '-' >> return negate)
<|> (optional (char '+') >> return id)
-- splice = lexeme $ char '#' *> char '{' *> ident <* char '}'
splice = lexeme $ char '$' *> ident
where ident = (:) <$> letter <*> (many (alphaNum <|> oneOf "_'"))
reg = do
S {onlyReg, varReg} <- getState
(onlyReg <$> rawReg) <|> (varReg <$> splice)
imm = do
S {onlyImm, varImm} <- getState
(onlyImm <$> rawImm) <|> (varImm <$> splice)
regimm = do
S {vRegImm, rRegImm, iRegImm} <- getState
(rRegImm <$> rawReg) <|> (iRegImm <$> rawImm) <|> (vRegImm <$> splice)
extern = do
S {iHelperId, vHelperId} <- getState
(iHelperId <$> rawImm32) <|> (vHelperId <$> splice)
ocomma = lexeme . optional $ char ','
lowercase opr = map C.toLower $ show opr
binAlus = do
alu <- [Add .. Arsh]
(post, sz) <- [("32", B32), ("64", B64), ("", B64)]
let name = lowercase alu ++ post
return (name, Binary sz alu <$> reg <* ocomma <*> regimm)
unAlus = do
alu <- [Neg .. Be]
(post, sz) <- case alu of
Neg -> [("32", B32), ("64", B64), ("", B64)]
_ -> [("16", B16), ("32", B32), ("64", B64)]
let name = lowercase alu ++ post
return (name, Unary sz alu <$> reg)
memref = between (symbol "[") (symbol "]")
((,) <$> reg <*> optionMaybe (symbol "+" *> memoryOffset))
stores = do
(mem_sz, bsz) <- [("b", B8), ("h", B16), ("w", B32), ("dw", B64)]
(x, r_or_i) <- [("x", rSplice), ("", iSplice)]
let name = "st" ++ x ++ mem_sz
return (name, do (r, off) <- memref
ocomma
Store bsz r off <$> r_or_i)
where
rSplice = do S {vRegImm, rRegImm} <- getState
(rRegImm <$> rawReg) <|> (vRegImm <$> splice)
iSplice = do S {vRegImm, iRegImm} <- getState
(iRegImm <$> rawImm) <|> (vRegImm <$> splice)
loads = do
(mem_sz, bsz) <- [("b", B8), ("h", B16), ("w", B32), ("dw", B64)]
let name = "ldx" ++ mem_sz
return (name, do dst <- reg
ocomma
(src, off) <- memref
return $ Load bsz dst src off)
ldabs = do
(mem_sz, bsz) <- [("b", B8), ("h", B16), ("w", B32), ("dw", B64)]
let name = "ldabs" ++ mem_sz
return (name, do off <- imm
return $ LoadAbs bsz off)
ldind = do
(mem_sz, bsz) <- [("b", B8), ("h", B16), ("w", B32), ("dw", B64)]
let name = "ldind" ++ mem_sz
return (name, do src <- reg
ocomma
off <- imm
return $ LoadInd bsz src off)
conditionals = do
jmp <- [Jeq .. Jsle]
let name = lowercase jmp
return(name, JCond jmp <$> reg <* ocomma <*> regimm <* ocomma <*> (symbol "+" *> codeOffset)) -- TODO: Do we really want to require the '+' here?
instruction = do
opr <- operator
case M.lookup opr operators of
Just argP -> argP
_ -> fail $ "Unknown operator: " ++ opr
where
operators = M.fromList $ binAlus ++
unAlus ++
stores ++
loads ++ [ ("lddw", LoadImm <$> reg <* ocomma <*> imm)] ++
[ ("lmfd", LoadMapFd <$> reg <* ocomma <*> imm) ] ++
ldabs ++
ldind ++
conditionals ++
[ ("ja", Jmp <$> codeOffset),
("jmp", Jmp <$> codeOffset),
("call", Call <$> extern),
("exit", pure Exit)]
program = sc >> many1 instruction <* eof
-- TODO the naming of the splice stuff is clunky
data SpliceConstructors reg imm regimm extern = S { onlyReg :: Reg -> reg
, varReg :: String -> reg
, onlyImm :: Imm -> imm
, varImm :: String -> imm
, iHelperId :: HelperId -> extern
, vHelperId :: String -> extern
, rRegImm :: Reg -> regimm
, iRegImm :: Imm -> regimm
, vRegImm :: String -> regimm
}
defaultSpliceCons =
S { onlyReg = id
, varReg = don't
, onlyImm = id
, varImm = don't
, iHelperId = id
, vHelperId = don't
, rRegImm = R
, iRegImm = Imm
, vRegImm = don't }
where don't = error "Splices not allowed"
parse :: String -> Either String Program
parse str = first show $ runParser program defaultSpliceCons "<input>" str
parseFromFile :: FilePath -> IO (Either String Program)
parseFromFile filename = first show <$> do
input <- readFile filename
return $ runParser program defaultSpliceCons filename input