llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
symbolic_eval.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include "core/inst_visitor.h"
8 
9 class SymbolicContext;
10 class SymbolicHeap;
11 class ReferenceGraph;
12 class SymbolicFrame;
13 
14 
15 
19 class SymbolicEval final : public InstVisitor<bool> {
20 public:
22  SymbolicHeap &heap,
23  SymbolicFrame &frame,
24  ReferenceGraph &refs,
25  SymbolicContext &ctx,
26  Inst &inst)
27  : heap_(heap)
28  , frame_(frame)
29  , refs_(refs)
30  , ctx_(ctx)
31  , inst_(inst)
32  {
33  }
34 
35  bool Evaluate();
36 
37 public:
39  SymbolicContext &GetContext() { return ctx_; }
40 
42  const SymbolicValue &Find(ConstRef<Inst> inst) { return frame_.Find(inst); }
43 
45  bool SetUndefined()
46  {
47  return frame_.Set(inst_, SymbolicValue::Undefined(GetOrigin()));
48  }
49 
51  bool SetScalar()
52  {
53  return frame_.Set(inst_, SymbolicValue::Scalar(GetOrigin()));
54  }
55 
57  bool SetInteger(const APInt &i)
58  {
59  return frame_.Set(inst_, SymbolicValue::Integer(i, GetOrigin()));
60  }
61 
63  bool SetFloat(const APFloat &i)
64  {
65  return frame_.Set(inst_, SymbolicValue::Float(i, GetOrigin()));
66  }
67 
69  bool SetLowerBounded(const APInt &i)
70  {
71  return frame_.Set(inst_, SymbolicValue::LowerBoundedInteger(i, GetOrigin()));
72  }
73 
75  bool SetMask(const APInt &k, const APInt &v)
76  {
77  return frame_.Set(inst_, SymbolicValue::Mask(k, v, GetOrigin()));
78  }
79 
81  bool SetValue(const SymbolicPointer::Ref &ptr)
82  {
83  return frame_.Set(inst_, SymbolicValue::Value(ptr, GetOrigin()));
84  }
85 
87  bool SetPointer(const SymbolicPointer::Ref &ptr)
88  {
89  return frame_.Set(inst_, SymbolicValue::Pointer(ptr, GetOrigin()));
90  }
91 
93  bool SetNullable(const SymbolicPointer::Ref &ptr)
94  {
95  return frame_.Set(inst_, SymbolicValue::Nullable(ptr, GetOrigin()));
96  }
97 
99  bool NOP(const SymbolicValue &value)
100  {
101  return frame_.Set(inst_, value);
102  }
103 
104 private:
105  bool VisitInst(Inst &i) override;
106  bool VisitBarrierInst(BarrierInst &i) override;
107  bool VisitMemoryLoadInst(MemoryLoadInst &i) override;
108  bool VisitMemoryStoreInst(MemoryStoreInst &i) override;
109  bool VisitMemoryExchangeInst(MemoryExchangeInst &i) override;
110  bool VisitMemoryCompareExchangeInst(MemoryCompareExchangeInst &i) override;
111  bool VisitVaStartInst(VaStartInst &i) override;
112  bool VisitArgInst(ArgInst &i) override;
113  bool VisitMovInst(MovInst &i) override;
114  bool VisitBitCastInst(BitCastInst &i) override;
115  bool VisitUndefInst(UndefInst &i) override;
116  bool VisitFrameInst(FrameInst &i) override;
117  bool VisitTruncInst(TruncInst &i) override;
118  bool VisitZExtInst(ZExtInst &i) override;
119  bool VisitSExtInst(SExtInst &i) override;
120 
121  bool VisitSllInst(SllInst &i) override;
122  bool VisitSrlInst(SrlInst &i) override;
123  bool VisitSraInst(SraInst &i) override;
124 
125  bool VisitAndInst(AndInst &i) override;
126  bool VisitOrInst(OrInst &i) override;
127  bool VisitXorInst(XorInst &i) override;
128 
129  bool VisitAddInst(AddInst &i) override;
130  bool VisitSubInst(SubInst &i) override;
131  bool VisitUDivInst(UDivInst &i) override;
132  bool VisitSDivInst(SDivInst &i) override;
133  bool VisitURemInst(URemInst &i) override;
134  bool VisitMulInst(MulInst &i) override;
135 
136  bool VisitCmpInst(CmpInst &i) override;
137 
138  bool VisitSelectInst(SelectInst &i) override;
139  bool VisitOUMulInst(OUMulInst &i) override;
140  bool VisitOUAddInst(OUAddInst &i) override;
141 
142  bool VisitGetInst(GetInst &i) override;
143 
144  bool VisitX86_OutInst(X86_OutInst &i) override;
145  bool VisitX86_LgdtInst(X86_LgdtInst &i) override;
146  bool VisitX86_LidtInst(X86_LidtInst &i) override;
147  bool VisitX86_LtrInst(X86_LtrInst &i) override;
148  bool VisitX86_WrMsrInst(X86_WrMsrInst &i) override;
149  bool VisitX86_RdTscInst(X86_RdTscInst &i) override;
150 
151 private:
153  ID<SymbolicFrame> GetFrame();
155  SymbolicValue::Origin GetOrigin();
156 
157 private:
159  SymbolicHeap &heap_;
161  SymbolicFrame &frame_;
163  ReferenceGraph &refs_;
165  SymbolicContext &ctx_;
167  Inst &inst_;
168 };
ReferenceGraph
Definition: reference_graph.h:32
Inst
Definition: inst.h:53
SymbolicContext
Definition: symbolic_context.h:28
SymbolicValue
Definition: symbolic_value.h:24
ConstRef< Inst >
SymbolicEval::SetUndefined
bool SetUndefined()
Helper to return a scalar.
Definition: symbolic_eval.h:45
SymbolicEval::SetScalar
bool SetScalar()
Helper to return a scalar.
Definition: symbolic_eval.h:51
SymbolicEval
Definition: symbolic_eval.h:19
SymbolicValue::Origin
std::pair< ID< SymbolicFrame >, Ref< Inst > > Origin
Instruction which originated the value.
Definition: symbolic_value.h:49
SymbolicEval::Find
const SymbolicValue & Find(ConstRef< Inst > inst)
Find a value.
Definition: symbolic_eval.h:42
SymbolicEval::SetInteger
bool SetInteger(const APInt &i)
Helper to return an integer.
Definition: symbolic_eval.h:57
ID< SymbolicFrame >
SymbolicEval::SetNullable
bool SetNullable(const SymbolicPointer::Ref &ptr)
Helper to forward a pointer (nullptr).
Definition: symbolic_eval.h:93
SymbolicEval::SetPointer
bool SetPointer(const SymbolicPointer::Ref &ptr)
Helper to forward a pointer (pointer).
Definition: symbolic_eval.h:87
MovInst
Definition: mov.h:17
SymbolicFrame::Find
const SymbolicValue & Find(ConstRef< Inst > inst)
Return the value an instruction was mapped to.
Definition: symbolic_frame.cpp:86
SymbolicValue::Value
APInt Value
Values of the known bits.
Definition: symbolic_value.h:209
SymbolicEval::SetMask
bool SetMask(const APInt &k, const APInt &v)
Forward to frameuator, return a pointer.
Definition: symbolic_eval.h:75
SymbolicFrame::Set
bool Set(Ref< Inst > i, const SymbolicValue &value)
Definition: symbolic_frame.cpp:68
InstVisitor
Definition: inst_visitor.h:15
SymbolicEval::SetLowerBounded
bool SetLowerBounded(const APInt &i)
Helper to return a lower bounded integer.
Definition: symbolic_eval.h:69
SymbolicFrame
Definition: symbolic_frame.h:29
SymbolicEval::NOP
bool NOP(const SymbolicValue &value)
Helper to set a value.
Definition: symbolic_eval.h:99
SymbolicEval::SetValue
bool SetValue(const SymbolicPointer::Ref &ptr)
Helper to forward a pointer (value).
Definition: symbolic_eval.h:81
SymbolicEval::SetFloat
bool SetFloat(const APFloat &i)
Helper to return a float.
Definition: symbolic_eval.h:63
SymbolicEval::GetContext
SymbolicContext & GetContext()
Return the context.
Definition: symbolic_eval.h:39
SymbolicHeap
Definition: symbolic_heap.h:21