Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Add missing copyright notices
[simgrid.git] / src / mc / DwarfExpression.hpp
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SIMGRID_MC_DWARF_EXPRESSION_HPP
8 #define SIMGRID_MC_DWARF_EXPRESSION_HPP
9
10 #include <cstdint>
11
12 #include <stdexcept>
13
14 #include "src/mc/AddressSpace.hpp"
15
16 namespace simgrid {
17 namespace dwarf {
18
19 class evaluation_error : std::runtime_error {
20 public:
21   evaluation_error(const char* what): std::runtime_error(what) {}
22   ~evaluation_error() noexcept(true);
23 };
24
25 struct ExpressionContext {
26   ExpressionContext() :
27     cursor(nullptr), frame_base(nullptr), address_space(nullptr),
28     object_info(nullptr), process_index(simgrid::mc::ProcessIndexMissing) {}
29     
30   unw_cursor_t* cursor;
31   void* frame_base;
32   simgrid::mc::AddressSpace* address_space;
33   simgrid::mc::ObjectInformation* object_info;
34   int process_index;
35 };
36
37 typedef std::vector<Dwarf_Op> DwarfExpression;
38
39 class ExpressionStack {
40 public:
41   typedef std::uintptr_t value_type;
42   static const std::size_t max_size = 64;
43 private:
44   uintptr_t stack_[max_size];
45   size_t size_;
46 public:
47   ExpressionStack() : size_(0) {}
48
49   // Access:
50   std::size_t size() const { return size_; }
51   bool empty()       const { return size_ == 0; }
52   void clear()             { size_ = 0; }
53   uintptr_t&       operator[](int i)       { return stack_[i]; }
54   uintptr_t const& operator[](int i) const { return stack_[i]; }
55   value_type& top()
56   {
57     if (size_ == 0)
58       throw evaluation_error("Empty stack");
59     return stack_[size_ - 1];
60   }
61   value_type& top(unsigned i)
62   {
63     if (size_ < i)
64       throw evaluation_error("Invalid element");
65     return stack_[size_ - 1 - i];
66   }
67
68   // Push/pop:
69   void push(value_type value)
70   {
71     if (size_ == max_size)
72       throw evaluation_error("Dwarf stack overflow");
73     stack_[size_++] = value;
74   }
75   value_type pop()
76   {
77     if (size_ == 0)
78       throw evaluation_error("Stack underflow");
79     return stack_[--size_];
80   }
81
82   // Other operations:
83   void dup() { push(top()); }
84 };
85
86 void execute(const Dwarf_Op* ops, std::size_t n,
87   ExpressionContext const& context, ExpressionStack& stack);
88
89 inline
90 void execute(simgrid::dwarf::DwarfExpression const& expression,
91   ExpressionContext const& context, ExpressionStack& stack)
92 {
93   execute(expression.data(), expression.size(), context, stack);
94 }
95
96 }
97 }
98
99 #endif