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
use vex_sys::*;
use super::logger;

// Re-exports:
pub use vex_sys::{
    IRType as Type,
    _IRStmt__bindgen_ty_1__bindgen_ty_2 as StmtIMark,
    IREndness, // don't remove the IR, to differentiate from VexEndness.
    IRTemp as Temp,
    _IRExpr__bindgen_ty_1__bindgen_ty_2 as ExprGet,
    _IRExpr__bindgen_ty_1__bindgen_ty_4 as ExprRdTmp,
};

#[derive(Copy, Clone)]
pub enum ConstEnum {
    U1(bool),
    U8(u8),
    U16(u16),
    U32(u32),
    U64(u64),
    F32(f32),
    F32i(u32),
    F64(f64),
    F64i(u64),
    V128(vex_sys::V128),
    V256(vex_sys::V256),
}

pub struct Const<'a>(&'a mut IRConst);

impl Const<'_> {
    pub fn as_enum(self) -> ConstEnum {
        match self.0.tag {
            IRConstTag::Ico_U1 => ConstEnum::U1(unsafe { self.0.Ico.U1 != 0 }),
            IRConstTag::Ico_U8 => ConstEnum::U8(unsafe { self.0.Ico.U8 }),
            IRConstTag::Ico_U16 => ConstEnum::U16(unsafe { self.0.Ico.U16 }),
            IRConstTag::Ico_U32 => ConstEnum::U32(unsafe { self.0.Ico.U32 }),
            IRConstTag::Ico_U64 => ConstEnum::U64(unsafe { self.0.Ico.U64 }),
            IRConstTag::Ico_F32 => ConstEnum::F32(unsafe { self.0.Ico.F32 }),
            IRConstTag::Ico_F32i => ConstEnum::F32i(unsafe { self.0.Ico.F32i }),
            IRConstTag::Ico_F64 => ConstEnum::F64(unsafe { self.0.Ico.F64 }),
            IRConstTag::Ico_F64i => ConstEnum::F64i(unsafe { self.0.Ico.F64i }),
            IRConstTag::Ico_V128 => {
                let val = unsafe { self.0.Ico.V128 };
                let vec = vex_sys::V128 {
                    w16: [val, val, val, val, val, val, val, val]
                };
                ConstEnum::V128(vec)
            }
            IRConstTag::Ico_V256 => {
                let val = unsafe { self.0.Ico.V256 };
                let vec = vex_sys::V256 {
                    w32: [val, val, val, val, val, val, val, val]
                };
                ConstEnum::V256(vec)
            }
        }
    }
}

pub struct Callee<'a>(&'a mut IRCallee);

pub struct RegArray<'a>(&'a mut IRRegArray);

pub enum ExprEnum<'a> {
    Get(ExprGet),
    GetI(ExprGetI<'a>),
    RdTmp(ExprRdTmp),
    Qop(ExprQop<'a>),
    Triop(ExprTriop<'a>),
    Binop(ExprBinop<'a>),
    Unop(ExprUnop<'a>),
    Load(ExprLoad<'a>),
    Const(Const<'a>),
    CCall(ExprCCall<'a>),
    ITE(ExprITE<'a>),
}

pub struct ExprGetI<'a>(&'a mut _IRExpr__bindgen_ty_1__bindgen_ty_3);

pub struct ExprQop<'a>(&'a mut _IRExpr__bindgen_ty_1__bindgen_ty_5);

pub struct ExprTriop<'a>(&'a mut IRTriop);

pub struct ExprBinop<'a>(&'a mut _IRExpr__bindgen_ty_1__bindgen_ty_7);

pub struct ExprUnop<'a>(&'a mut _IRExpr__bindgen_ty_1__bindgen_ty_8);

pub struct ExprLoad<'a>(&'a mut _IRExpr__bindgen_ty_1__bindgen_ty_9);

pub struct ExprCCall<'a>(&'a mut _IRExpr__bindgen_ty_1__bindgen_ty_11);

pub struct ExprITE<'a>(&'a mut _IRExpr__bindgen_ty_1__bindgen_ty_12);

pub struct Expr<'a>(&'a mut IRExpr);

impl std::fmt::Display for Expr<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        logger::with(|| unsafe { ppIRExpr(self.0) }).1.unwrap().fmt(f)
    }
}

impl Expr<'_> {
    pub fn as_enum(&mut self) -> ExprEnum {
        match self.0.tag {
            IRExprTag::Iex_Binder => unreachable!("Binder should never be returned by VEX"),
            IRExprTag::Iex_Get => ExprEnum::Get(unsafe { self.0.Iex.Get }),
            IRExprTag::Iex_GetI => ExprEnum::GetI(ExprGetI(unsafe { &mut self.0.Iex.GetI })),
            IRExprTag::Iex_RdTmp => ExprEnum::RdTmp(unsafe { self.0.Iex.RdTmp }),
            IRExprTag::Iex_Qop => ExprEnum::Qop(ExprQop(unsafe { &mut self.0.Iex.Qop })),
            IRExprTag::Iex_Triop => ExprEnum::Triop(ExprTriop(unsafe { &mut *self.0.Iex.Triop.details })),
            IRExprTag::Iex_Binop => ExprEnum::Binop(ExprBinop(unsafe { &mut self.0.Iex.Binop })),
            IRExprTag::Iex_Unop => ExprEnum::Unop(ExprUnop(unsafe { &mut self.0.Iex.Unop })),
            IRExprTag::Iex_Load => ExprEnum::Load(ExprLoad(unsafe { &mut self.0.Iex.Load })),
            IRExprTag::Iex_Const => ExprEnum::Const(Const(unsafe { &mut *self.0.Iex.Const.con })),
            IRExprTag::Iex_ITE => ExprEnum::ITE(ExprITE(unsafe { &mut self.0.Iex.ITE })),
            IRExprTag::Iex_CCall => ExprEnum::CCall(ExprCCall(unsafe { &mut self.0.Iex.CCall })),
            IRExprTag::Iex_VECRET => unreachable!("VECRET should never be returned by VEX"),
            IRExprTag::Iex_GSPTR => unreachable!("GSPTR should never be returned by VEX"),
        }
    }
}

pub struct Stmt<'a>(pub(crate) &'a mut IRStmt);

pub enum StmtEnum<'a> {
    NoOp,
    IMark(StmtIMark),
    AbiHint(StmtAbiHint<'a>),
    Put(StmtPut<'a>),
    PutI(StmtPutI<'a>),
    WrTmp(StmtWrTmp<'a>),
    Store(StmtStore<'a>),
    LoadG(StmtLoadG<'a>),
    StoreG(StmtStoreG<'a>),
    CAS(StmtCAS<'a>),
    LLSC(StmtLLSC<'a>),
    Dirty(StmtDirty<'a>),
    MBE(StmtMBE<'a>),
    Exit(StmtExit<'a>),
}

impl std::fmt::Display for Stmt<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        logger::with(|| unsafe { ppIRStmt(self.0) }).1.unwrap().fmt(f)
    }
}

impl Stmt<'_> {
    pub fn kind(&self) -> IRStmtTag {
        self.0.tag
    }

    pub fn as_enum(&mut self) -> StmtEnum {
        match self.0.tag {
            IRStmtTag::Ist_NoOp => StmtEnum::NoOp,
            IRStmtTag::Ist_IMark => StmtEnum::IMark(*unsafe { self.0.Ist.IMark.as_ref() }),
            IRStmtTag::Ist_AbiHint => StmtEnum::AbiHint(StmtAbiHint(unsafe { self.0.Ist.AbiHint.as_mut() })),
            IRStmtTag::Ist_Put => StmtEnum::Put(StmtPut(unsafe { self.0.Ist.Put.as_mut() })),
            IRStmtTag::Ist_PutI => StmtEnum::PutI(StmtPutI(unsafe { self.0.Ist.PutI.as_mut() })),
            IRStmtTag::Ist_WrTmp => StmtEnum::WrTmp(StmtWrTmp(unsafe { self.0.Ist.WrTmp.as_mut() })),
            IRStmtTag::Ist_Store => StmtEnum::Store(StmtStore(unsafe { self.0.Ist.Store.as_mut() })),
            IRStmtTag::Ist_LoadG => StmtEnum::LoadG(StmtLoadG(unsafe { self.0.Ist.LoadG.as_mut() })),
            IRStmtTag::Ist_StoreG => StmtEnum::StoreG(StmtStoreG(unsafe { self.0.Ist.StoreG.as_mut() })),
            IRStmtTag::Ist_CAS => StmtEnum::CAS(StmtCAS(unsafe { self.0.Ist.CAS.as_mut() })),
            IRStmtTag::Ist_LLSC => StmtEnum::LLSC(StmtLLSC(unsafe { self.0.Ist.LLSC.as_mut() })),
            IRStmtTag::Ist_Dirty => StmtEnum::Dirty(StmtDirty(unsafe { self.0.Ist.Dirty.as_mut() })),
            IRStmtTag::Ist_MBE => StmtEnum::MBE(StmtMBE(unsafe { self.0.Ist.MBE.as_mut() })),
            IRStmtTag::Ist_Exit => StmtEnum::Exit(StmtExit(unsafe { self.0.Ist.Exit.as_mut() })),
        }
    }
}

pub struct StmtAbiHint<'a>(pub(crate) &'a mut _IRStmt__bindgen_ty_1__bindgen_ty_3);

pub struct StmtPut<'a>(pub(crate) &'a mut _IRStmt__bindgen_ty_1__bindgen_ty_4);

impl StmtPut<'_> {
    pub fn offset(&self) -> i32 {
        self.0.offset
    }

    pub fn data(&self) -> Expr {
        Expr(unsafe { &mut *self.0.data })
    }
}

pub struct StmtPutI<'a>(pub(crate) &'a mut _IRStmt__bindgen_ty_1__bindgen_ty_5);

pub struct StmtWrTmp<'a>(pub(crate) &'a mut _IRStmt__bindgen_ty_1__bindgen_ty_6);

pub struct StmtStore<'a>(pub(crate) &'a mut _IRStmt__bindgen_ty_1__bindgen_ty_7);

pub struct StmtStoreG<'a>(pub(crate) &'a mut _IRStmt__bindgen_ty_1__bindgen_ty_8);

pub struct StmtLoadG<'a>(pub(crate) &'a mut _IRStmt__bindgen_ty_1__bindgen_ty_9);

pub struct StmtCAS<'a>(pub(crate) &'a mut _IRStmt__bindgen_ty_1__bindgen_ty_10);

pub struct StmtLLSC<'a>(pub(crate) &'a mut _IRStmt__bindgen_ty_1__bindgen_ty_11);

pub struct StmtDirty<'a>(pub(crate) &'a mut _IRStmt__bindgen_ty_1__bindgen_ty_12);

pub struct StmtMBE<'a>(pub(crate) &'a mut _IRStmt__bindgen_ty_1__bindgen_ty_13);

pub struct StmtExit<'a>(pub(crate) &'a mut _IRStmt__bindgen_ty_1__bindgen_ty_14);

pub struct TypeEnv<'a>(&'a mut IRTypeEnv);

impl std::fmt::Display for TypeEnv<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        logger::with(|| unsafe { ppIRTypeEnv(self.0) }).1.unwrap().fmt(f)
    }
}

pub struct IRSB<'a> {
    pub(crate) inner: &'a vex_sys::IRSB,
    pub(crate) _lock: super::LiftGuard<'a>,
}

impl std::fmt::Display for IRSB<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        logger::with(|| unsafe { ppIRSB(self.inner) }).1.unwrap().fmt(f)
    }
}

impl<'a> IRSB<'a> {
    pub fn type_env(&self) -> TypeEnv {
        TypeEnv(unsafe { &mut *self.inner.tyenv })
    }

    pub fn stmts(&'a self) -> impl Iterator<Item=Stmt<'a>> + 'a {
        unsafe {
            std::slice::from_raw_parts(
                (*self.inner).stmts,
                (*self.inner).stmts_used as usize,
            )
                .iter()
            .map(|s| Stmt(&mut **s))
        }
    }

    pub fn next(&self) -> Expr {
        Expr(unsafe { &mut *self.inner.next })
    }
}