Commit c546ea44 authored by Julian Kranz's avatar Julian Kranz
Browse files

RREIL Grammar

parent da829302
......@@ -47,6 +47,12 @@ val lv-gen gens stmt =
| SEM_LIN_SCALE x: visit-lin gens sz x.opnd
end
val visit-sexpr gens sexpr =
case sexpr of
SEM_SEXPR_LIN l: visit-lin gens 1 l
| SEM_SEXPR_CMP c: visit-op-cmp gens c
end
val visit-arity1 gens x = visit-lin gens x.size x.opnd1
val visit-arity2 gens x =
......@@ -54,6 +60,16 @@ val lv-gen gens stmt =
(visit-lin gens x.size x.opnd1)
(visit-lin gens x.size x.opnd2)
val visit-op-cmp gens cmp =
case cmp of
SEM_CMPEQ x: visit-arity2 gens x
| SEM_CMPNEQ x: visit-arity2 gens x
| SEM_CMPLES x: visit-arity2 gens x
| SEM_CMPLEU x: visit-arity2 gens x
| SEM_CMPLTS x: visit-arity2 gens x
| SEM_CMPLTU x: visit-arity2 gens x
end
val visit-op gens op =
case op of
SEM_LIN x: visit-arity1 gens x
......@@ -70,12 +86,7 @@ val lv-gen gens stmt =
| SEM_XOR x: visit-arity2 gens x
| SEM_SX x: visit-lin gens x.fromsize x.opnd1
| SEM_ZX x: visit-lin gens x.fromsize x.opnd1
| SEM_CMPEQ x: visit-arity2 gens x
| SEM_CMPNEQ x: visit-arity2 gens x
| SEM_CMPLES x: visit-arity2 gens x
| SEM_CMPLEU x: visit-arity2 gens x
| SEM_CMPLTS x: visit-arity2 gens x
| SEM_CMPLTU x: visit-arity2 gens x
| SEM_CMP c: visit-op-cmp gens c
| SEM_ARB x: gens
end
......@@ -83,7 +94,7 @@ val lv-gen gens stmt =
val visit-flow gens x =
lv-union
(visit-lin gens 1 x.cond)
(visit-sexpr gens x.cond)
(lv-union
(visit-address gens x.target-true)
(visit-address gens x.target-false))
......@@ -93,8 +104,8 @@ val lv-gen gens stmt =
SEM_ASSIGN x: visit-op gens x.rhs
| SEM_LOAD x: visit-address gens x.address
| SEM_STORE x: visit-address gens x.address
| SEM_WHILE x: visit-lin gens 1 x.cond
| SEM_ITE x: visit-lin gens 1 x.cond
| SEM_WHILE x: visit-sexpr gens x.cond
| SEM_ITE x: visit-sexpr gens x.cond
| SEM_BRANCH x: visit-address gens x.target
| SEM_CBRANCH x: visit-flow gens x
# | SEM_LABEL x: gens
......
......@@ -20,9 +20,9 @@ val rreil-show-stmt s =
SEM_ASSIGN x: rreil-show-var x.lhs +++ " = " +++ rreil-show-op x.rhs
| SEM_LOAD x: rreil-show-var x.lhs +++ " = " +++ rreil-show-ptrderef x.size x.address
| SEM_STORE x: "*" +++ rreil-show-address x.address +++ " = " +++ rreil-show-op x.rhs
| SEM_ITE x: "if (" +++ rreil-show-linear x.cond +++ ") {\n" +++ rreil-show-stmts x.then_branch +++ "} else {\n" +++ rreil-show-stmts x.else_branch +++ "}"
| SEM_WHILE x: "while (" +++ rreil-show-linear x.cond +++ ") {\n" +++ rreil-show-stmts x.body +++ "}"
| SEM_CBRANCH x: "if (" +++ rreil-show-linear x.cond +++ ") goto " +++ rreil-show-address x.target-true +++ " else goto " +++ rreil-show-address x.target-false
| SEM_ITE x: "if (" +++ rreil-show-sexpr x.cond +++ ") {\n" +++ rreil-show-stmts x.then_branch +++ "} else {\n" +++ rreil-show-stmts x.else_branch +++ "}"
| SEM_WHILE x: "while (" +++ rreil-show-sexpr x.cond +++ ") {\n" +++ rreil-show-stmts x.body +++ "}"
| SEM_CBRANCH x: "if (" +++ rreil-show-sexpr x.cond +++ ") goto " +++ rreil-show-address x.target-true +++ " else goto " +++ rreil-show-address x.target-false
| SEM_BRANCH x: "goto [" +++ rreil-show-hint x.hint +++ "] " +++ rreil-show-address x.target
end
......@@ -35,6 +35,16 @@ val rreil-show-hint x =
val rreil-show-label l = "l" +++ showint l +++ ":"
val rreil-show-op-cmp cmp =
case cmp of
SEM_CMPEQ x: "==" +++ rreil-show-cmp x
| SEM_CMPNEQ x: "/=" +++ rreil-show-cmp x
| SEM_CMPLES x: "<=s" +++ rreil-show-cmp x
| SEM_CMPLEU x: "<=u" +++ rreil-show-cmp x
| SEM_CMPLTS x: "<s" +++ rreil-show-cmp x
| SEM_CMPLTU x: "<u" +++ rreil-show-cmp x
end
val rreil-show-op op =
case op of
SEM_LIN x: rreil-show-arity1 x
......@@ -50,12 +60,7 @@ val rreil-show-op op =
| SEM_XOR x: "xor" +++ rreil-show-arity2 x
| SEM_SX x: "sx[" +++ showint x.fromsize +++ "->" +++ showint x.size +++ "](" +++ rreil-show-linear x.opnd1 +++ ")"
| SEM_ZX x: "zx[" +++ showint x.fromsize +++ "->" +++ showint x.size +++ "](" +++ rreil-show-linear x.opnd1 +++ ")"
| SEM_CMPEQ x: "==" +++ rreil-show-cmp x
| SEM_CMPNEQ x: "/=" +++ rreil-show-cmp x
| SEM_CMPLES x: "<=s" +++ rreil-show-cmp x
| SEM_CMPLEU x: "<=u" +++ rreil-show-cmp x
| SEM_CMPLTS x: "<s" +++ rreil-show-cmp x
| SEM_CMPLTU x: "<u" +++ rreil-show-cmp x
| SEM_CMP c: rreil-show-op-cmp c
| SEM_ARB x: "arbitrary[" +++ showint x.size +++ "]"
end
......@@ -84,6 +89,12 @@ val rreil-show-linear lin =
end
end
val rreil-show-sexpr sexpr =
case sexpr of
SEM_SEXPR_LIN l: rreil-show-linear l
| SEM_SEXPR_CMP c: rreil-show-op-cmp c
end
val rreil-show-id id =
case id of
VIRT_EQ: "EQ"
......
......@@ -23,6 +23,18 @@ type sem_linear =
| SEM_LIN_SUB of {opnd1:sem_linear, opnd2:sem_linear}
| SEM_LIN_SCALE of {imm:int, opnd:sem_linear}
type sem_sexpr =
SEM_SEXPR_LIN of sem_linear
| SEM_SEXPR_CMP of sem_op_cmp
type sem_op_cmp =
SEM_CMPEQ of sem_cmp
| SEM_CMPNEQ of sem_cmp
| SEM_CMPLES of sem_cmp
| SEM_CMPLEU of sem_cmp
| SEM_CMPLTS of sem_cmp
| SEM_CMPLTU of sem_cmp
type sem_op =
SEM_LIN of sem_arity1
| SEM_MUL of sem_arity2
......@@ -37,21 +49,16 @@ type sem_op =
| SEM_XOR of sem_arity2
| SEM_SX of {size:int, fromsize:int, opnd1:sem_linear}
| SEM_ZX of {size:int, fromsize:int, opnd1:sem_linear}
| SEM_CMPEQ of sem_cmp
| SEM_CMPNEQ of sem_cmp
| SEM_CMPLES of sem_cmp
| SEM_CMPLEU of sem_cmp
| SEM_CMPLTS of sem_cmp
| SEM_CMPLTU of sem_cmp
| SEM_ARB of {size:int}
| SEM_CMP of sem_op_cmp
type sem_stmt =
SEM_ASSIGN of {lhs:sem_var, rhs:sem_op}
| SEM_LOAD of {lhs:sem_var, size:int, address:sem_address}
| SEM_STORE of {address:sem_address, rhs:sem_op}
| SEM_ITE of {cond:sem_linear, then_branch:sem_stmts, else_branch:sem_stmts}
| SEM_WHILE of {cond:sem_linear, body:sem_stmts}
| SEM_CBRANCH of {cond:sem_linear, target-true:sem_address, target-false:sem_address}
| SEM_ITE of {cond:sem_sexpr, then_branch:sem_stmts, else_branch:sem_stmts}
| SEM_WHILE of {cond:sem_sexpr, body:sem_stmts}
| SEM_CBRANCH of {cond:sem_sexpr, target-true:sem_address, target-false:sem_address}
| SEM_BRANCH of {hint:branch_hint, target:sem_address}
type branch_hint =
......@@ -78,12 +85,7 @@ val rreil-sizeOf op =
| SEM_XOR x: x.size
| SEM_SX x: x.size
| SEM_ZX x: x.size
| SEM_CMPEQ x: 1
| SEM_CMPNEQ x: 1
| SEM_CMPLES x: 1
| SEM_CMPLEU x: 1
| SEM_CMPLTS x: 1
| SEM_CMPLTU x: 1
| SEM_CMP x: 1
| SEM_ARB x: x.size
end
......@@ -200,14 +202,14 @@ val modulo sz a b c = push (/ASSIGN a (SEM_MOD{size=sz,opnd1=b,opnd2=c}))
val movsx szA a szB b = push (/ASSIGN a (SEM_SX{size=szA,fromsize=szB,opnd1=b}))
val movzx szA a szB b = push (/ASSIGN a (SEM_ZX{size=szA,fromsize=szB,opnd1=b}))
val convert szA a szB b = push (/ASSIGN a (SEM_ZX{size=szA,fromsize=szB,opnd1=b}))
val cmpeq sz f a b = push (/ASSIGN f (SEM_CMPEQ{size=sz,opnd1=a,opnd2=b}))
val cmpneq sz f a b = push (/ASSIGN f (SEM_CMPNEQ{size=sz,opnd1=a,opnd2=b}))
val cmples sz f a b = push (/ASSIGN f (SEM_CMPLES{size=sz,opnd1=a,opnd2=b}))
val cmpleu sz f a b = push (/ASSIGN f (SEM_CMPLEU{size=sz,opnd1=a,opnd2=b}))
val cmplts sz f a b = push (/ASSIGN f (SEM_CMPLTS{size=sz,opnd1=a,opnd2=b}))
val cmpltu sz f a b = push (/ASSIGN f (SEM_CMPLTU{size=sz,opnd1=a,opnd2=b}))
val ite c t e = push (/ITE c t e)
val while c b = push (/WHILE c b)
val cmpeq sz f a b = push (/ASSIGN f (SEM_CMP (SEM_CMPEQ{size=sz,opnd1=a,opnd2=b})))
val cmpneq sz f a b = push (/ASSIGN f (SEM_CMP (SEM_CMPNEQ{size=sz,opnd1=a,opnd2=b})))
val cmples sz f a b = push (/ASSIGN f (SEM_CMP (SEM_CMPLES{size=sz,opnd1=a,opnd2=b})))
val cmpleu sz f a b = push (/ASSIGN f (SEM_CMP (SEM_CMPLEU{size=sz,opnd1=a,opnd2=b})))
val cmplts sz f a b = push (/ASSIGN f (SEM_CMP (SEM_CMPLTS{size=sz,opnd1=a,opnd2=b})))
val cmpltu sz f a b = push (/ASSIGN f (SEM_CMP (SEM_CMPLTU{size=sz,opnd1=a,opnd2=b})))
val ite c t e = push (/ITE (SEM_SEXPR_LIN c) t e)
val while c b = push (/WHILE (SEM_SEXPR_LIN c) b)
val jump address = do
update @{foundJump = '1'};
push (/BRANCH HINT_JUMP address)
......@@ -222,7 +224,7 @@ val ret address = do
end
val cbranch cond target-true target-false = do
update @{foundJump = '1'};
push (/CBRANCH cond target-true target-false)
push (/CBRANCH (SEM_SEXPR_LIN cond) target-true target-false)
end
val _if c _then a _else b = do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment