ilang  1.1.4
ILAng: A Modeling and Verification Platform for SoCs
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros
memory_model.h
Go to the documentation of this file.
1 
4 #ifndef ILANG_MCM_MEMORY_MODEL_H__
5 #define ILANG_MCM_MEMORY_MODEL_H__
6 
7 #include <ilang/mcm/ast_helper.h>
10 
12 namespace ilang {
13 
14 #define FINAL_STEP_SUFFIX "final"
15 
17 enum AccessType { READ, WRITE, EITHER };
18 class MemoryModel;
19 // used when query the type of accesses (in register trace step)
20 
24 // HZ note: accessors like w1.wfe.global should be interpreted as an accessor
25 // function e.g., _wfe_global(w1) This function may either convert to a derived
26 // class or access a fixed member to extract the attribute HZ note: In a memory
27 // model, usually a derived class of TraceStep is used.
28 class TraceStep {
30  friend class MemoryModel;
31 
32 public:
34  typedef z3::expr ZExpr;
36  typedef std::vector<ZExpr> ZExprVec;
38  typedef std::shared_ptr<TraceStep> TraceStepPtr;
40  typedef std::set<TraceStepPtr> TraceStepPtrSet;
42  typedef std::set<std::string> StateNameSet;
44  enum TraceStepType { INST_EVT, FACET_EVT, INIT_EVT, FINAL_EVT };
45  // ------------------------- MEMREAD BOOKKEEPING
46  // -------------------------------------- //
48  typedef std::vector<std::pair<ExprPtr, ExprPtr>> AddrDataVec;
50  typedef std::unordered_map<std::string, AddrDataVec> MRFVal;
51 
52 private:
53  // ------------------------- HELPERS -------------------------------------- //
55  std::string
56  GetName(); // This is the helper function to generate unique names,
57  // this is private and usshould not be used to return the name of
58  // the current trace step. Please use name()
59  static unsigned trace_step_seq_no;
61  void InitReadWriteSet(const InstrPtr& inst);
63  void InitReadSetForProperty(const ExprPtr& e);
64 
65  // ------------------------- MEMREAD BOOKKEEPING
66  // -------------------------------------- //
68  mutable MRFVal nad_map_;
71  mutable bool already_searched_for_memreadpair_;
73  void
74  FindAddrDataPairVecInExpr(const ExprPtr& node,
75  NestedMemAddrDataAvoider& nested_finder_) const;
78  void VisitNodeForMemoryRead(const ExprPtr& node,
79  NestedMemAddrDataAvoider& nested_finder_) const;
83  const AddrDataVec&
84  FindAddrDataPairVecInInst(const std::string& sname,
85  NestedMemAddrDataAvoider& nested_finder_) const;
86 
87 protected:
88  // ------------------------- MEMBERS -------------------------------------- //
91  // Type : INST_EVT
93  InstrPtr _inst; // I assume it is nullptr by default
94  // Type : FACET_EVT
103 
105  StateNameSet _inst_read_set; // this applies to both inst-evt and facet-evt
107  StateNameSet _inst_write_set; // this applies to both inst-evt and facet-evt
108 
113  std::string _name;
115  z3::expr timestamp;
117  size_t _pos_suffix;
121  z3::context& _ctx_;
126 
127 public:
128  // ------------------------- ACCESSORS/MUTATORS --------------------------- //
130  InstrLvlAbsPtr host() const;
132  std::string name() const { return _name; }
134  const StateNameSet& get_inst_read_set() const { return _inst_read_set; }
138  const TraceStepType& type() const { return _type; }
140  bool is_init_tracestep() const { return _type == TraceStepType::INIT_EVT; }
142  bool is_facet_tracestep() const { return _type == TraceStepType::FACET_EVT; }
144  bool is_final_tracestep() const { return _type == TraceStepType::FINAL_EVT; }
145 
147  InstrPtr inst() const;
150  size_t pos_suffix() const;
152  inline Z3ExprAdapter& z3adapter() const { return _expr2z3_; }
154  z3::context& ctx() { return _ctx_; }
157 
158  // ------------------------- CONSTRUCTOR/DESTRUCTOR ----------------------- //
161  TraceStep(const InstrPtr& inst, ZExprVec& cstr, z3::context& ctx, size_t pos);
164  TraceStep(const InstrPtr& inst, ZExprVec& cstr, z3::context& ctx,
165  ZExpr _ts_overwrite, size_t pos);
167  TraceStep(const InstrPtr& ref_inst, ZExprVec& cstr, z3::context& ctx,
168  const std::string& fevt_name, size_t pos);
170  TraceStep(const ExprPtr& property, ZExprVec& cstr, z3::context& ctx,
171  const InstrLvlAbsPtr& host);
173  virtual ~TraceStep() {}
174  // ------------------------- MEMBERS -------------------------------------- //
176  void AddStateAccess(const std::string& name, AccessType acc_type);
178  void AddStateAccess(const StateNameSet& s, AccessType acc_type);
181  ZExpr ConvertZ3OnThisStep(const ExprPtr& ast) const;
182 
183  // ------------------------- HELPERS -------------------------------------- //
185  bool Access(AccessType acc_type, const std::string& name) const;
187  bool Access(AccessType acc_type, const StateNameSet& stateset) const;
188 
189  // ------------------------- HELPERS -------------------------------------- //
191  std::string Print(z3::model& m) const;
192 
193 }; // class TraceStep
194 
196 class MemoryModel {
197 
198 public:
214  typedef std::vector<InstrPtr> InstrVec;
217  typedef std::vector<InstrVec> ProgramTemplate;
220  typedef std::vector<std::vector<TraceStepPtr>> PerILATraceStepPtrSet;
222  typedef std::map<std::string, StateNameSet> ILANameStateNameSetMap;
223 
224 protected:
225  // ------------------------- MEMBERS -------------------------------------- //
228  _init_trace_step; // will be assigned by function CreateGlobalInitStep
231  _final_trace_step; // will be assigned by function CreateGlobalFinalStep
233  TraceStepPtrSet _all_trace_steps; // will be assigned by specific memory model
236  _all_inst_trace_steps; // will be assigned by specific memory model
241  const StateNameSet&
242  m_shared_state_names; // will get a copy from the given set
245  m_ila_private_state_names; // will get a copy from the given set
248  // For passing argument, we need to keep track of the following info:
251  z3::context& _ctx_;
257 
258  // ------------------------- ACCESSORS/MUTATORS --------------------------- //
260  z3::context& ctx() const { return _ctx_; }
261 
262 public:
264  ZExpr ConvertZ3(const ExprPtr& ast, const std::string& suffix) const {
265  return _expr2z3_.GetExpr(ast, suffix);
266  }
267 
268 public:
270  void virtual InitSize(const ProgramTemplate& _tmpl);
273  void virtual RegisterSteps(size_t regIdx, const InstrVec& _inst_seq) = 0;
276  void virtual FinishRegisterSteps() = 0;
278  void virtual ApplyAxioms() = 0;
281  void virtual SetLocalState(const std::vector<bool>& ordered);
282  // HZ note: All the steps should be registered through the first function:
283  // RegisterSteps
285  void virtual AddSingleTraceStepProperty(
286  const ExprPtr& property, std::function<bool(const TraceStep&)> filter);
288  void virtual GetSingleTraceStepProperty(
289  const ExprPtr& property, std::function<bool(const TraceStep&)> filter,
290  std::function<void(const z3::expr&)> collector);
292  void virtual SetFinalProperty(const ExprPtr& property) = 0;
295  void virtual AddDoubleTraceStepProperty(
296  std::function<z3::expr(const TraceStep&, const TraceStep&)>,
297  std::function<bool(const TraceStep&, const TraceStep&)>);
298  // ------------------------- CONSTRUCTOR/DESTRUCTOR ----------------------- //
299  // we will need a wrapper on the outside
300  MemoryModel(z3::context& ctx, ZExprVec& _cstrlist,
301  const StateNameSet& shared_states,
302  const ILANameStateNameSetMap& private_states,
303  const InstrLvlAbsPtr& global_ila_ptr);
304  // make destructor virtual
305  virtual ~MemoryModel() {}
306  // ------------------------- HELPERS -------------------------------------- //
308  // bool AccessShared ( const InstrPtr & ip, AccessType acc_type ); //
309  // (deprecated)
310 
311  // ------------------------- DEBUG USE ONLY --------------------------------
312  // //
314 
315  // ------------------------- AXIOM HELPERS -------------------------------- //
316 protected:
317  // ------------------------- MEMBERS -------------------------------------- //
320  // ------------------------- HELPERS -------------------------------------- //
324  TraceStepPtr CreateGlobalFinalStep(const ExprPtr& property);
325  // ------------------------- AXIOM HELPERS
326  // -------------------------------------- // The implementation of the
327  // following functions can be found in axiom_helper.cpp The type of results
328  // that can statically determined
329  enum StaticResult { STATIC_TRUE = 1, STATIC_FALSE, STATIC_UNKNOWN };
330  // The type of hints given to the functions to tell what kind of mem ops to
331  // look for
332  enum AxiomFuncHint {
333  HINT_NONE = 0,
334  HINT_READ = 1,
335  HINT_WRITE
336  }; // if read disable write-set, if write disable read-set // should only used
337  // for SameAddr/SameData
338  // Happen-before
339  z3::expr HB(const TraceStep& l, const TraceStep& r) const;
340  // At the same time
341  z3::expr Sync(const TraceStep& l, const TraceStep& r) const;
342  // Program-order
343  z3::expr PO(const TraceStep& l, const TraceStep& r) const;
344  // Same address
345  z3::expr SameAddress(const TraceStep& l, const TraceStep& r,
346  const std::string& sname,
347  AxiomFuncHint lhint = HINT_NONE,
348  AxiomFuncHint rhint = HINT_NONE) const;
349  // Get decode (z3 not ast)
350  z3::expr Decode(const TraceStep& l) const;
351  // Enforce same data
352  z3::expr SameData(const TraceStep& l, const TraceStep& r,
353  const std::string& sname, AxiomFuncHint lhint = HINT_NONE,
354  AxiomFuncHint rhint = HINT_NONE) const;
355  // Enforcing same core constraint
356  bool SameCore(const TraceStep& l, const TraceStep& r) const;
357  // STATICALLY DETERMINED // not implemented
358  StaticResult SameAddressStatic(const TraceStep& l, const TraceStep& r) const;
359  // STATICALLY DETERMINED // not implemented
360  StaticResult DecodeStatic(const TraceStep& l) const;
361  // STATICALLY DETERMINED // not implemented
362  StaticResult SameCoreStatic(const TraceStep& l, const TraceStep& r) const;
363 
365  z3::expr Z3ForallList(const ZExprVec& l) const; // move into mcm class
367  z3::expr Z3ExistsList(const ZExprVec& l) const;
368 
369 private:
371  z3::expr MemVarSameAddress(const ExprPtr& leftWAddr,
372  const AddrDataVec& leftRAddrDataVec,
373  const ExprPtr& rightWAddr,
374  const AddrDataVec& rightRAddrDataVec,
375  const TraceStep& traceL,
376  const TraceStep& traceR) const;
378  z3::expr MemVarSameData(const std::pair<ExprPtr, ExprPtr>& leftAddrDataPair,
379  const AddrDataVec& leftRAddrDataVec,
380  const std::pair<ExprPtr, ExprPtr>& rightAddrDataPair,
381  const AddrDataVec& rightRAddrDataVec,
382  const TraceStep& traceL, const TraceStep& traceR,
383  const ExprPtr& memVar) const;
385  ExprPtr CheckAndPeel(const ExprPtr& e, const std::string& type,
386  size_t argn) const;
388  z3::expr NonMemVarSameData(const TraceStep& l, const TraceStep& r,
389  const std::string& sname, AxiomFuncHint lhint,
390  AxiomFuncHint rhint) const;
394  void SameAddrDataSanityCheck(const TraceStep& l, const TraceStep& r,
395  const std::string& sname) const;
396 
397 }; // class MemoryModel
398 
399 } // namespace ilang
400 
401 #endif // ILANG_MCM_MEMORY_MODEL_H__
std::vector< InstrVec > ProgramTemplate
Definition: memory_model.h:217
ZExprVec & _constr
A reference to the constraint list.
Definition: memory_model.h:253
std::string name() const
Return the name.
Definition: memory_model.h:132
ZExprVec & _cstr
Definition: memory_model.h:111
z3::context & _ctx_
Definition: memory_model.h:251
Z3ExprAdapter _expr2z3_
Definition: memory_model.h:256
std::vector< std::vector< TraceStepPtr > > PerILATraceStepPtrSet
Definition: memory_model.h:220
NestedMemAddrDataAvoider nested_finder_
An AST traversor, make sure there are no nested asts.
Definition: memory_model.h:319
void AddStateAccess(const std::string &name, AccessType acc_type)
To update the set for FACET_EVT.
std::string Print(z3::model &m) const
Print out a trace step for debug purpose.
TraceStep::TraceStepPtrSet TraceStepPtrSet
Type of trace steps, we need to collect the set of trace steps (WRITE)
Definition: memory_model.h:206
TraceStep(const InstrPtr &inst, ZExprVec &cstr, z3::context &ctx, size_t pos)
TraceStepPtrSet _all_inst_trace_steps
The set of all instruction trace steps.
Definition: memory_model.h:236
PerILATraceStepPtrSet _ila_trace_steps
Definition: memory_model.h:239
Expr::ExprPtr ExprPtr
Pointer type for normal use of Expr.
Definition: expr.h:138
std::vector< InstrPtr > InstrVec
Type for storing a set of Instr.
Definition: instr.h:136
InstrLvlAbsPtr _host_final_
Host for Final Event.
Definition: memory_model.h:123
size_t pos_suffix() const
std::map< std::string, StateNameSet > ILANameStateNameSetMap
Type of map ila-name to state-set.
Definition: memory_model.h:222
ZExpr ConvertZ3OnThisStep(const ExprPtr &ast) const
const ILANameStateNameSetMap & m_ila_private_state_names
The internal storage of all private states of all ilas.
Definition: memory_model.h:245
Z3ExprAdapter _expr2z3_
Keep an adapter that trace steps (won&#39;t shared w. others)
Definition: memory_model.h:119
z3::expr timestamp
The time stamp.
Definition: memory_model.h:115
The class for trace step (an instance of instruction) As in the unrolling, there may be multiple inst...
Definition: memory_model.h:28
InstrLvlAbsPtr host() const
Return the host ILA.
std::unordered_map< std::string, AddrDataVec > MRFVal
Map from state name to memory read pair.
Definition: memory_model.h:50
virtual void SetFinalProperty(const ExprPtr &property)=0
Set the assertion on the final state.
InstrPtr _inst
The pointer to the instruction executed in this step.
Definition: memory_model.h:93
TraceStepType _type
the type of this trace step (event)
Definition: memory_model.h:90
z3::expr Z3ForallList(const ZExprVec &l) const
This is to deal with forall (if does not exist, it should be true also)
StateNameSet _write_state_set
Definition: memory_model.h:102
TraceStepPtr _final_trace_step
The pointer to the final trace step.
Definition: memory_model.h:231
z3::context & ctx() const
Return the context (for variable creation)
Definition: memory_model.h:260
bool is_init_tracestep() const
Decide if the trace step is the initial trace step (a short-cut)
Definition: memory_model.h:140
TraceStep::ZExprVec ZExprVec
Type for containing a vector of z3::expr.
Definition: memory_model.h:202
const StateNameSet & get_inst_write_set() const
Return the set of states written by inst/parent-inst.
Definition: memory_model.h:136
Instr::InstrPtr InstrPtr
Pointer type for normal use of Instr.
Definition: instr.h:132
ExprPtr final_property()
Return final property.
Definition: memory_model.h:156
StateNameSet _read_state_set
Definition: memory_model.h:99
ExprPtr _final_property
The cache for the property to witness on the final step.
Definition: memory_model.h:125
InstrPtr inst() const
Return the associated instruction for facet / instruction event.
StateNameSet _inst_write_set
Set of states (that are written by inst or parent-inst)
Definition: memory_model.h:107
InstrPtr _parent_inst
The pointer to the referred instruction.
Definition: memory_model.h:96
std::string _name
The name of trace step.
Definition: memory_model.h:113
TraceStepPtrSet _all_trace_steps
The set of all trace steps.
Definition: memory_model.h:233
size_t _pos_suffix
The relative position.
Definition: memory_model.h:117
TraceStep::TraceStepPtr TraceStepPtr
Type of trace step shared pointers.
Definition: memory_model.h:204
std::vector< InstrPtr > InstrVec
Type of an instruction vector to represent a sequence.
Definition: memory_model.h:214
z3::expr GetExpr(const ExprPtr &expr, const std::string &suffix="")
Get the z3 expression of the AST node.
const StateNameSet & m_shared_state_names
The set of names of shared states.
Definition: memory_model.h:242
virtual void RegisterSteps(size_t regIdx, const InstrVec &_inst_seq)=0
virtual void GetSingleTraceStepProperty(const ExprPtr &property, std::function< bool(const TraceStep &)> filter, std::function< void(const z3::expr &)> collector)
Get the z3 expression of a property on a trace step.
InstrLvlAbs::InstrLvlAbsPtr InstrLvlAbsPtr
Pointer type for normal use of InstrLvlAbs.
Definition: instr_lvl_abs.h:326
std::set< std::string > StateNameSet
Type of state name set.
Definition: memory_model.h:42
Class of traversing to avoid nested memory access in address.
Definition: ast_helper.h:57
TraceStep::StateNameSet StateNameSet
Type of set of state names.
Definition: memory_model.h:210
virtual void AddSingleTraceStepProperty(const ExprPtr &property, std::function< bool(const TraceStep &)> filter)
Assert a property that some trace step (decided by filter) should follow.
Z3ExprAdapter & z3adapter() const
Return the adapter.
Definition: memory_model.h:152
bool is_final_tracestep() const
Decide if the trace step is the facet trace step (a short-cut)
Definition: memory_model.h:144
virtual void ApplyAxioms()=0
To apply the axioms, the complete program should be given.
TraceStepType
The type of trace step type.
Definition: memory_model.h:44
TraceStep::AddrDataVec AddrDataVec
Type of vector of (addr,data) pair.
Definition: memory_model.h:212
z3::context & ctx()
Return the context (for variable creation)
Definition: memory_model.h:154
virtual void InitSize(const ProgramTemplate &_tmpl)
To initialize the inner storage space.
z3::expr Z3ExistsList(const ZExprVec &l) const
This is to apply to exists, (if does not exist, it should be false)
bool is_facet_tracestep() const
Decide if the trace step is the facet trace step (a short-cut)
Definition: memory_model.h:142
virtual ~TraceStep()
Destructor, make it virtual.
Definition: memory_model.h:173
TraceStepPtr CreateGlobalInitStep()
A function for dervied classes to create init trace step.
const TraceStepPtrSet & GetAllTraceSteps() const
Determine if an instruction access a shared state.
Definition: memory_model.h:313
virtual void SetLocalState(const std::vector< bool > &ordered)
TraceStepPtr _init_trace_step
The pointer to the initial trace step.
Definition: memory_model.h:228
The class for generating z3 expression from an ILA.
Definition: z3_expr_adapter.h:20
AccessType
Type of state read or write.
Definition: memory_model.h:17
std::vector< std::pair< ExprPtr, ExprPtr > > AddrDataVec
Type of (addr,data) pair vector.
Definition: memory_model.h:48
InstrLvlAbsPtr m_p_global_ila
The pointer to the (dummy) global ILA.
Definition: memory_model.h:247
bool Access(AccessType acc_type, const std::string &name) const
To determine if the instruction READ/WRITE a certain state.
z3::expr ZExpr
Type alias for z3::expr.
Definition: memory_model.h:34
ZExpr ConvertZ3(const ExprPtr &ast, const std::string &suffix) const
Return the z3 converter used by this memory model.
Definition: memory_model.h:264
The base class for memory models.
Definition: memory_model.h:196
const TraceStepType & type() const
Return the type of the trace step.
Definition: memory_model.h:138
z3::context & _ctx_
Keep a context to allocate z3 vars.
Definition: memory_model.h:121
const StateNameSet & get_inst_read_set() const
Return the set of states read by inst/parent-inst.
Definition: memory_model.h:134
StateNameSet _inst_read_set
Set of states (that are read by inst or parent-inst)
Definition: memory_model.h:105
TraceStepPtr CreateGlobalFinalStep(const ExprPtr &property)
A function for dervied classes to create final trace step.
virtual void FinishRegisterSteps()=0
TraceStep::ZExpr ZExpr
Type alias for z3::expr.
Definition: memory_model.h:200
std::shared_ptr< TraceStep > TraceStepPtr
Type of trace step pointer.
Definition: memory_model.h:38
std::vector< ZExpr > ZExprVec
Type for containing a set of z3::expr.
Definition: memory_model.h:36
std::set< TraceStepPtr > TraceStepPtrSet
Type of set of trace steps.
Definition: memory_model.h:40
virtual void AddDoubleTraceStepProperty(std::function< z3::expr(const TraceStep &, const TraceStep &)>, std::function< bool(const TraceStep &, const TraceStep &)>)