llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
symbolic_object.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 <llvm/Support/Alignment.h>
8 
9 #include "core/type.h"
10 #include "core/func.h"
11 
12 #include "passes/pre_eval/symbolic_value.h"
13 
14 class Atom;
15 class Prog;
16 class Object;
17 class SymbolicFrame;
18 class SymbolicValue;
19 class SymbolicHeap;
20 
21 
22 
26 class SymbolicObject final {
27 public:
31  std::optional<size_t> size,
32  llvm::Align align,
33  bool rdonly,
34  bool zero
35  );
37  SymbolicObject(const SymbolicObject &that);
40 
42  ID<SymbolicObject> GetID() const { return id_; }
44  llvm::Align GetAlignment() const { return align_; }
45 
47  const SymbolicValue *begin() const;
48  const SymbolicValue *end() const;
49 
51  void Merge(const SymbolicObject &that);
52 
54  SymbolicValue Load(int64_t offset, Type type);
56  SymbolicValue LoadImprecise(Type type);
57 
59  bool Init(int64_t offset, const SymbolicValue &val, Type type);
61  bool Store(int64_t offset, const SymbolicValue &val, Type type);
63  bool StoreImprecise(int64_t offset, const SymbolicValue &val, Type type);
65  bool StoreImprecise(const SymbolicValue &val, Type type);
66 
67 private:
71  std::optional<size_t> size_;
73  llvm::Align align_;
75  bool rdonly_;
76 
78  class MergedStorage {
79  public:
81  MergedStorage(const SymbolicValue &value) : value_(value) {}
82 
84  SymbolicValue Load() const { return value_; }
85 
87  bool Store(const SymbolicValue &value);
88 
90  const SymbolicValue *begin() const { return &value_; }
91  const SymbolicValue *end() const { return &value_ + 1; }
92 
93  private:
95  bool Accurate = false;
97  SymbolicValue value_;
98  };
99 
101  class BucketStorage {
102  public:
104  BucketStorage(size_t size, const SymbolicValue &value);
105 
107  void Merge(const BucketStorage &that);
108 
109  SymbolicValue Load(int64_t offset, Type type) const;
110  SymbolicValue Load() const { return approx_; }
111 
112  bool StorePrecise(int64_t offset, const SymbolicValue &value, Type type);
113  bool StoreImprecise(int64_t offset, const SymbolicValue &value, Type type);
114 
116  const SymbolicValue *begin() const { return &*buckets_.begin(); }
117  const SymbolicValue *end() const { return &*buckets_.end(); }
118 
119  private:
121  SymbolicValue Read(int64_t offset, Type type) const;
123  bool Write(
124  int64_t offset,
125  const SymbolicValue &val,
126  Type type,
127  bool (BucketStorage::*mutate)(unsigned, const SymbolicValue &)
128  );
130  bool Set(unsigned bucket, const SymbolicValue &val);
132  bool Merge(unsigned bucket, const SymbolicValue &val);
133 
134  private:
135  bool Accurate = true;
137  std::vector<SymbolicValue> buckets_;
139  SymbolicValue approx_;
140  };
141 
143  union U {
144  bool Accurate;
145  MergedStorage M;
146  BucketStorage B;
147 
148  U() { }
149  ~U() { }
150  } v_;
151 };
SymbolicValue
Definition: symbolic_value.h:24
SymbolicObject::LoadImprecise
SymbolicValue LoadImprecise(Type type)
Reads a value from all possible locations in the object.
Definition: symbolic_object.cpp:132
Atom
Definition: atom.h:23
SymbolicObject::SymbolicObject
SymbolicObject(ID< SymbolicObject > id, std::optional< size_t > size, llvm::Align align, bool rdonly, bool zero)
Constructs a symbolic object.
Definition: symbolic_object.cpp:26
SymbolicObject::Init
bool Init(int64_t offset, const SymbolicValue &val, Type type)
Initialises a value inside the object.b.
Definition: symbolic_object.cpp:142
SymbolicObject
Definition: symbolic_object.h:26
ID< SymbolicObject >
Object
Definition: object.h:43
SymbolicObject::~SymbolicObject
~SymbolicObject()
Cleanup.
Definition: symbolic_object.cpp:67
SymbolicObject::Store
bool Store(int64_t offset, const SymbolicValue &val, Type type)
Performs a store to an atom inside the object.
Definition: symbolic_object.cpp:152
SymbolicObject::begin
const SymbolicValue * begin() const
Iterator over buckets.
Definition: symbolic_object.cpp:77
SymbolicObject::GetAlignment
llvm::Align GetAlignment() const
Return the alignment.
Definition: symbolic_object.h:44
Prog
Definition: prog.h:33
SymbolicFrame
Definition: symbolic_frame.h:29
SymbolicObject::GetID
ID< SymbolicObject > GetID() const
Return the ID of the object.
Definition: symbolic_object.h:42
SymbolicObject::Load
SymbolicValue Load(int64_t offset, Type type)
Performs a load from an atom inside the object.
Definition: symbolic_object.cpp:122
SymbolicObject::StoreImprecise
bool StoreImprecise(int64_t offset, const SymbolicValue &val, Type type)
Clobbers the value at an exact location.
Definition: symbolic_object.cpp:173
SymbolicObject::Merge
void Merge(const SymbolicObject &that)
Merges another object into this one.
Definition: symbolic_object.cpp:97
SymbolicHeap
Definition: symbolic_heap.h:21