Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Use std::equal_range in get_search_range()
[simgrid.git] / src / mc / Type.hpp
1 /* Copyright (c) 2007-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_TYPE_HPP
8 #define SIMGRID_MC_TYPE_HPP
9
10 #include <cstddef>
11
12 #include <vector>
13 #include <string>
14
15 #include <xbt/base.h>
16 #include <xbt/asserts.h>
17
18 #include <dwarf.h>
19
20 #include "src/mc/mc_forward.hpp"
21 #include "src/mc/LocationList.hpp"
22
23 namespace simgrid {
24 namespace mc {
25
26 /** A member of a structure, union
27  *
28  *  Inheritance is seen as a special member as well.
29  */
30 class Member {
31 public:
32   Member() {}
33
34   /** Whether this member represent some inherited part of the object */
35   bool inheritance = false;
36
37   /** Name of the member (if any) */
38   std::string name;
39
40   /** DWARF location expression for locating the location of the member */
41   simgrid::dwarf::DwarfExpression location_expression;
42
43   std::size_t byte_size = 0; // Do we really need this?
44
45   unsigned type_id = 0;
46   simgrid::mc::Type* type = nullptr;
47
48   /** Whether the member is at a fixed offset from the base address */
49   bool has_offset_location() const
50   {
51     // Recognize the expression `DW_OP_plus_uconst(offset)`:
52     return location_expression.size() == 1 &&
53       location_expression[0].atom == DW_OP_plus_uconst;
54   }
55
56   /** Get the offset of the member
57   *
58   *  This is only valid is the member is at a fixed offset from the base.
59   *  This is often the case (for C types, C++ type without virtual
60   *  inheritance).
61   *
62   *  If the location is more complex, the location expression has
63   *  to be evaluated (which might need accessing the memory).
64   */
65   int offset() const
66   {
67     xbt_assert(this->has_offset_location());
68     return this->location_expression[0].number;
69   }
70
71   /** Set the location of the member as a fixed offset */
72   void offset(int new_offset)
73   {
74     // Set the expression to be `DW_OP_plus_uconst(offset)`:
75     Dwarf_Op op;
76     op.atom = DW_OP_plus_uconst;
77     op.number = new_offset;
78     this->location_expression = { op };
79   }
80
81 };
82
83 /** A type in the model-checked program */
84 class Type {
85 public:
86   Type() {}
87
88   /** The DWARF TAG of the type (e.g. DW_TAG_array_type) */
89   int type = 0;
90   unsigned id = 0; /* Offset in the section (in hexadecimal form) */
91   std::string name; /* Name of the type */
92   int byte_size = 0; /* Size in bytes */
93   int element_count = 0; /* Number of elements for array type */
94   unsigned type_id = 0; /* DW_AT_type id */
95   std::vector<Member> members; /* if DW_TAG_structure_type, DW_TAG_class_type, DW_TAG_union_type*/
96
97   simgrid::mc::Type* subtype = nullptr; // DW_AT_type
98   simgrid::mc::Type* full_type = nullptr; // The same (but more complete) type
99 };
100
101 }
102 }
103
104 #endif