Otawa  0.10
base.h
Go to the documentation of this file.
1 /*
2  * Base declaration interface.
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2003, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef OTAWA_BASE_H
22 #define OTAWA_BASE_H
23 
24 #include <elm/io.h>
25 #include <elm/utility.h>
26 #include <elm/string.h>
27 #include <elm/util/VarArg.h>
28 #include <elm/util/strong_type.h>
29 #include <elm/util/AutoComparator.h>
30 #include <elm/util/HashKey.h>
31 #include <elm/types.h>
32 #include <elm/type_info.h>
33 
34 // ELM definitions
35 namespace elm { namespace serial2 {
36  class Serializer;
37  class Unserializer;
38 } } // elm::serial2
39 
40 namespace otawa {
41 using namespace elm;
42 
43 namespace ot {
44  typedef t::uint8 byte;
45  typedef t::uint32 mask;
46  typedef t::uint32 size;
47  typedef t::int64 time;
48 } // ot
49 
50 // MemArea
51 class MemArea;
52 
53 // Address class
54 class Address {
55 public:
56  typedef t::uint32 page_t;
58  static Address null;
59  static const page_t null_page = elm::type_info<page_t>::max;
60 
61  // Constructors
62  inline Address(void): pg(null_page), off(0) { }
63  inline Address(offset_t offset): pg(0), off(offset) { }
64  inline Address(page_t page, offset_t offset): pg(page), off(offset) { }
65  inline Address(const Address& address): pg(address.pg), off(address.off) { }
66  inline Address(const MemArea& mem_area);
67 
68  // Accessors
69  inline page_t page(void) const { return pg; }
70  inline offset_t offset(void) const { return off; }
71  inline offset_t operator*(void) const { return offset(); }
72  inline bool isNull(void) const { return pg == null_page; }
73  inline operator offset_t(void) const { return offset(); }
74 
75  // Assignment
76  inline Address& operator=(const Address& address) { pg = address.pg; off = address.off; return *this; }
77  inline Address& operator=(offset_t offset) { pg = 0; off = offset; return *this; }
78  inline Address& operator+=(int offset) { ASSERT(!offset || !isNull()); off += offset; return *this; }
79  inline Address& operator+=(t::uint32 offset) { ASSERT(!offset || !isNull()); off += offset; return *this; }
80  inline Address& operator-=(int offset) { ASSERT(!offset || !isNull()); off -= offset; return *this; }
81  inline Address& operator-=(t::uint32 offset) { ASSERT(!offset || !isNull()); off -= offset; return *this; }
82 
83  // Operations
84  inline Address operator+(t::int32 offset) const { ASSERT(!offset || !isNull()); return Address(pg, off + offset); }
85  inline Address operator+(t::uint32 offset) const { ASSERT(!offset || !isNull()); return Address(pg, off + offset); }
86  inline Address operator-(t::int32 offset) const { ASSERT(!offset || !isNull()); return Address(pg, off - offset); }
87  inline Address operator-(t::uint32 offset) const { ASSERT(!offset || !isNull()); return Address(pg, off - offset); }
88  inline offset_t operator-(const Address& address) const
89  { ASSERT(!isNull() && !address.isNull()); ASSERT(pg == address.pg); return off - address.off; }
90 
91  // Comparisons
92  inline bool equals(const Address& address) const { return pg == address.pg && off == address.off; }
93  inline int compare(const Address& address) const
94  { ASSERT(pg == address.pg); if(off < address.off) return -1; else if(off > address.off) return 1; else return 0; }
95  inline bool operator==(const Address& addr) const { return equals(addr); }
96  inline bool operator!=(const Address& addr) const { return !equals(addr); }
97  inline bool operator<(const Address& addr) const { ASSERT(pg == addr.pg); return off < addr.off; }
98  inline bool operator<=(const Address& addr) const { ASSERT(pg == addr.pg); return off <= addr.off; }
99  inline bool operator>(const Address& addr) const { ASSERT(pg == addr.pg); return off > addr.off; }
100  inline bool operator>=(const Address& addr) const { ASSERT(pg == addr.pg); return off >= addr.off; }
101 
102 private:
105 };
106 
108 namespace ot { elm::io::IntFormat address(Address addr); }
110 
111 
112 // MemArea class
113 class MemArea {
114 public:
115  static MemArea null;
116 
117  inline MemArea(void): _size(0) { }
118  inline MemArea(const MemArea& a): _base(a._base), _size(a._size) { }
119  inline MemArea(const Address& base, ot::size size): _base(base), _size(size) { }
120  inline MemArea(const Address& base, const Address& top): _base(base), _size(top - base) { }
121 
122  inline Address address(void) const { return _base; }
123  inline ot::size size(void) const { return _size; }
124  inline Address topAddress(void) const { return _base + _size; }
125  inline Address lastAddress(void) const { return _base + (_size - 1); }
126 
127  inline bool isNull(void) const { return _base.isNull(); }
128  inline bool isEmpty(void) const { return !_size; }
129  inline bool contains(const Address& addr) const { return _base <= addr && addr <= lastAddress(); }
130 
131  inline bool equals(const MemArea& a) const { return _base == a._base && _size == a._size; }
132  bool includes(const MemArea& a) const;
133  bool meets(const MemArea& a) const;
134  MemArea meet(const MemArea& a) const;
135  MemArea join(const MemArea& a) const;
136 
137  inline void set(const MemArea& a) { _base = a._base; _size = a._size; }
138  inline void move(const Address& base) { _base = base; }
139  inline void moveTop(const Address& top) { _base = top - _size; }
140  inline void resize(const ot::size size) { _size = size; }
141 
142  inline operator bool(void) const { return isEmpty(); }
143  inline bool operator==(const MemArea& a) const { return equals(a); }
144  inline bool operator!=(const MemArea& a) const { return !equals(a); }
145  inline bool operator>=(const MemArea& a) const { return includes(a); }
146  inline bool operator>(const MemArea& a) const { return includes(a) && !equals(a); }
147  inline bool operator<(const MemArea& a) const { return a.includes(*this); }
148  inline bool operator<=(const MemArea& a) const { return a.includes(*this) && !equals(a); }
149 
150  inline MemArea operator&(const MemArea& a) const { return meet(a); }
151  inline MemArea operator+(const MemArea& a) const { return join(a); }
152  inline MemArea operator=(const MemArea& a) { set(a); return *this; }
153  inline void operator&=(const MemArea& a) { set(meet(a)); }
154  inline void operator+=(const MemArea& a) { set(join(a)); }
155 
156 private:
159 };
161 
162 
163 // Address inlines
164 Address::Address(const MemArea& mem_area): pg(mem_area.address().pg), off(mem_area.address().off) { }
165 
166 
167 // Exception class
169 public:
170  Exception(void);
171  Exception(string message);
173  Exception(string message, elm::Exception& exn);
174 };
175 
176 } // otawa
177 
178 // Useful ELM predefinitions
179 namespace elm {
180  template <>
181  class HashKey<otawa::Address> {
182  public:
183  static inline unsigned long hash (const otawa::Address &key) { return key.page() + key.offset(); }
184  static inline bool equals (const otawa::Address &key1, const otawa::Address &key2) { return key1 == key2; }
185  };
186 
187 } // elm
188 
189 #endif // OTAWA_BASE_H
bool operator==(const Address &addr) const
Definition: base.h:95
Address & operator=(offset_t offset)
Definition: base.h:77
MemArea(const Address &base, ot::size size)
Definition: base.h:119
bool operator>(const MemArea &a) const
Definition: base.h:146
t::uint8 byte
Definition: base.h:44
bool contains(const Address &addr) const
Test whether the area contains the given address.
Definition: base.h:129
MemArea(const Address &base, const Address &top)
MemArea::MemArea(const MemArea& a); Copy an existing mem area.
Definition: base.h:120
bool isEmpty(void) const
Test whether the area is empty, that is, whether the size is null.
Definition: base.h:128
t::uint32 mask
Definition: base.h:45
void resize(const ot::size size)
Definition: base.h:140
static unsigned long hash(const otawa::Address &key)
Definition: base.h:183
page_t pg
Definition: base.h:103
Address lastAddress(void) const
Get the last address of the area, that is, the address of the last byte in the area.
Definition: base.h:125
int32_t int32
offset_t off
Definition: base.h:104
Address operator+(t::uint32 offset) const
Definition: base.h:85
bool operator==(const MemArea &a) const
Definition: base.h:143
Address operator-(t::uint32 offset) const
Definition: base.h:87
bool equals(const Address &address) const
Test if two address are equals.
Definition: base.h:92
bool operator<(const MemArea &a) const
Definition: base.h:147
Address operator-(t::int32 offset) const
Definition: base.h:86
void set(const MemArea &a)
Definition: base.h:137
int compare(const Address &address) const
Compare two addresses and returns:
Definition: base.h:93
elm::io::Output & operator<<(elm::io::Output &out, Address addr)
Definition: base.cpp:188
Address & operator=(const Address &address)
Definition: base.h:76
bool operator!=(const MemArea &a) const
Definition: base.h:144
MemArea operator&(const MemArea &a) const
Definition: base.h:150
uint32 size
bool isNull(void) const
Test if the area is null.
Definition: base.h:127
elm::io::IntFormat address(Address addr)
Build a format to display addresses.
Definition: base.cpp:213
bool isNull(void) const
Test if the address is null.
Definition: base.h:72
ot::size size(void) const
Get the size of the area.
Definition: base.h:123
bool operator>=(const MemArea &a) const
Definition: base.h:145
t::uint32 offset_t
Definition: base.h:57
uint32 offset
bool operator!=(const Address &addr) const
Definition: base.h:96
void operator+=(const MemArea &a)
Definition: base.h:154
MemArea operator=(const MemArea &a)
Definition: base.h:152
t::uint32 size
Definition: base.h:46
void moveTop(const Address &top)
Definition: base.h:139
Address & operator-=(int offset)
Definition: base.h:80
void operator&=(const MemArea &a)
Definition: base.h:153
Address operator+(t::int32 offset) const
Definition: base.h:84
offset_t operator*(void) const
Short cut to offset().
Definition: base.h:71
offset_t offset(void) const
Get the offset value.
Definition: base.h:70
Utility class representing an area in the memory defined by a base address and a size.
Definition: base.h:113
bool equals(const MemArea &a) const
Test wether the current area and the given one are equal.
Definition: base.h:131
The representation of an address in OTAWA.
Definition: base.h:54
MemArea(void)
Build a null mem area.
Definition: base.h:117
bool operator<=(const MemArea &a) const
Definition: base.h:148
uint8_t uint8
bool operator>(const Address &addr) const
Definition: base.h:99
int64_t int64
t::uint32 page_t
Definition: base.h:56
static MemArea null
Null memory area.
Definition: base.h:115
sys::SystemOutStream & out
void move(const Address &base)
Definition: base.h:138
offset_t operator-(const Address &address) const
Definition: base.h:88
bool includes(const MemArea &a) const
Test if the current mem area includes (not strictly) the given one.
Definition: base.cpp:320
static Address null
Null address.
Definition: base.h:58
static bool equals(const otawa::Address &key1, const otawa::Address &key2)
Definition: base.h:184
MemArea operator+(const MemArea &a) const
Definition: base.h:151
MemArea(const MemArea &a)
Definition: base.h:118
bool operator<=(const Address &addr) const
Definition: base.h:98
Address(void)
Build a null address.
Definition: base.h:62
Address address_t
Definition: base.h:107
t::int64 time
Definition: base.h:47
Address & operator-=(t::uint32 offset)
Definition: base.h:81
Address & operator+=(t::uint32 offset)
Definition: base.h:79
Address _base
Definition: base.h:157
Address(page_t page, offset_t offset)
Build a full address.
Definition: base.h:64
bool operator<(const Address &addr) const
Definition: base.h:97
Address topAddress(void) const
Get the top address of the area, that is, the address of the first byte after the area...
Definition: base.h:124
bool operator>=(const Address &addr) const
Definition: base.h:100
Address(offset_t offset)
Build a simple absolute address.
Definition: base.h:63
ot::size _size
Definition: base.h:158
Address address(void) const
Get the base address of the area.
Definition: base.h:122
page_t page(void) const
Get the page number.
Definition: base.h:69
uint32_t uint32
Address(const Address &address)
Build an address by cloning.
Definition: base.h:65
virtual String message(void)
Address & operator+=(int offset)
Definition: base.h:78