Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Rename PerPageCopy to ChunkedData (and move in its own files)
[simgrid.git] / src / mc / DwarfExpression.hpp
1 #ifndef SIMGRID_MC_DWARF_EXPRESSION_HPP
2 #define SIMGRID_MC_DWARF_EXPRESSION_HPP
3
4 #include <cstdint>
5
6 #include <stdexcept>
7
8 #include <mc/AddressSpace.hpp>
9
10 namespace simgrid {
11 namespace dwarf {
12
13 class evaluation_error : std::runtime_error {
14 public:
15   evaluation_error(const char* what) : std::runtime_error(what) {}
16   ~evaluation_error();
17 };
18
19 struct ExpressionContext {
20   ExpressionContext() :
21     cursor(nullptr), frame_base(nullptr), address_space(nullptr),
22     object_info(nullptr), process_index(simgrid::mc::ProcessIndexMissing) {}
23     
24   unw_cursor_t* cursor;
25   void* frame_base;
26   simgrid::mc::AddressSpace* address_space;
27   simgrid::mc::ObjectInformation* object_info;
28   int process_index;
29 };
30
31 typedef std::vector<Dwarf_Op> DwarfExpression;
32
33 class ExpressionStack {
34 public:
35   typedef std::uintptr_t value_type;
36   static const std::size_t max_size = 64;
37 private:
38   uintptr_t stack_[max_size];
39   size_t size_;
40 public:
41   ExpressionStack() : size_(0) {}
42
43   // Access:
44   std::size_t size() const { return size_; }
45   bool empty()       const { return size_ == 0; }
46   void clear()             { size_ = 0; }
47   uintptr_t&       operator[](int i)       { return stack_[i]; }
48   uintptr_t const& operator[](int i) const { return stack_[i]; }
49   value_type& top()
50   {
51     if (size_ == 0)
52       throw evaluation_error("Empty stack");
53     return stack_[size_ - 1];
54   }
55   value_type& top(unsigned i)
56   {
57     if (size_ < i)
58       throw evaluation_error("Invalid element");
59     return stack_[size_ - 1 - i];
60   }
61
62   // Push/pop:
63   void push(value_type value)
64   {
65     if (size_ == max_size)
66       throw evaluation_error("Dwarf stack overflow");
67     stack_[size_++] = value;
68   }
69   value_type pop()
70   {
71     if (size_ == 0)
72       throw evaluation_error("Stack underflow");
73     return stack_[--size_];
74   }
75
76   // Other operations:
77   void dup() { push(top()); }
78 };
79
80 void execute(const Dwarf_Op* ops, std::size_t n,
81   ExpressionContext const& context, ExpressionStack& stack);
82
83 inline
84 void execute(simgrid::dwarf::DwarfExpression const& expression,
85   ExpressionContext const& context, ExpressionStack& stack)
86 {
87   execute(expression.data(), expression.size(), context, stack);
88 }
89
90 }
91 }
92
93 #endif