1 // AsmJit - Machine code generation for C++
2 //
3 //  * Official AsmJit Home Page: https://asmjit.com
4 //  * Official Github Repository: https://github.com/asmjit/asmjit
5 //
6 // Copyright (c) 2008-2020 The AsmJit Authors
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 //    claim that you wrote the original software. If you use this software
18 //    in a product, an acknowledgment in the product documentation would be
19 //    appreciated but is not required.
20 // 2. Altered source versions must be plainly marked as such, and must not be
21 //    misrepresented as being the original software.
22 // 3. This notice may not be removed or altered from any source distribution.
23 
24 #ifndef ASMJIT_CORE_TARGET_H_INCLUDED
25 #define ASMJIT_CORE_TARGET_H_INCLUDED
26 
27 #include "../core/arch.h"
28 #include "../core/func.h"
29 
30 ASMJIT_BEGIN_NAMESPACE
31 
32 //! \addtogroup asmjit_core
33 //! \{
34 
35 // ============================================================================
36 // [asmjit::CodeInfo]
37 // ============================================================================
38 
39 #ifndef ASMJIT_NO_DEPRECATED
40 //! Basic information about a code (or target). It describes its architecture,
41 //! code generation mode (or optimization level), and base address.
42 class ASMJIT_DEPRECATED_STRUCT("Use Environment instead of CodeInfo") CodeInfo {
43 public:
44   //!< Environment information.
45   Environment _environment;
46   //! Base address.
47   uint64_t _baseAddress;
48 
49   //! \name Construction & Destruction
50   //! \{
51 
CodeInfo()52   inline CodeInfo() noexcept
53     : _environment(),
54       _baseAddress(Globals::kNoBaseAddress) {}
55 
56   inline explicit CodeInfo(uint32_t arch, uint32_t subArch = 0, uint64_t baseAddress = Globals::kNoBaseAddress) noexcept
_environment(arch,subArch)57     : _environment(arch, subArch),
58       _baseAddress(baseAddress) {}
59 
60   inline explicit CodeInfo(const Environment& environment, uint64_t baseAddress = Globals::kNoBaseAddress) noexcept
_environment(environment)61     : _environment(environment),
62       _baseAddress(baseAddress) {}
63 
64 
CodeInfo(const CodeInfo & other)65   inline CodeInfo(const CodeInfo& other) noexcept { init(other); }
66 
isInitialized()67   inline bool isInitialized() const noexcept {
68     return _environment.arch() != Environment::kArchUnknown;
69   }
70 
init(const CodeInfo & other)71   inline void init(const CodeInfo& other) noexcept {
72     *this = other;
73   }
74 
75   inline void init(uint32_t arch, uint32_t subArch = 0, uint64_t baseAddress = Globals::kNoBaseAddress) noexcept {
76     _environment.init(arch, subArch);
77     _baseAddress = baseAddress;
78   }
79 
reset()80   inline void reset() noexcept {
81     _environment.reset();
82     _baseAddress = Globals::kNoBaseAddress;
83   }
84 
85   //! \}
86 
87   //! \name Overloaded Operators
88   //! \{
89 
90   inline CodeInfo& operator=(const CodeInfo& other) noexcept = default;
91 
92   inline bool operator==(const CodeInfo& other) const noexcept { return ::memcmp(this, &other, sizeof(*this)) == 0; }
93   inline bool operator!=(const CodeInfo& other) const noexcept { return ::memcmp(this, &other, sizeof(*this)) != 0; }
94 
95   //! \}
96 
97   //! \name Accessors
98   //! \{
99 
100   //! Returns the target environment information, see \ref Environment.
environment()101   inline const Environment& environment() const noexcept { return _environment; }
102 
103   //! Returns the target architecture, see \ref Environment::Arch.
arch()104   inline uint32_t arch() const noexcept { return _environment.arch(); }
105   //! Returns the target sub-architecture, see \ref Environment::SubArch.
subArch()106   inline uint32_t subArch() const noexcept { return _environment.subArch(); }
107   //! Returns the native size of the target's architecture GP register.
gpSize()108   inline uint32_t gpSize() const noexcept { return _environment.registerSize(); }
109 
110   //! Tests whether this CodeInfo has a base address set.
hasBaseAddress()111   inline bool hasBaseAddress() const noexcept { return _baseAddress != Globals::kNoBaseAddress; }
112   //! Returns the base address or \ref Globals::kNoBaseAddress if it's not set.
baseAddress()113   inline uint64_t baseAddress() const noexcept { return _baseAddress; }
114   //! Sets base address to `p`.
setBaseAddress(uint64_t p)115   inline void setBaseAddress(uint64_t p) noexcept { _baseAddress = p; }
116   //! Resets base address (implicitly sets it to \ref Globals::kNoBaseAddress).
resetBaseAddress()117   inline void resetBaseAddress() noexcept { _baseAddress = Globals::kNoBaseAddress; }
118 
119   //! \}
120 };
121 #endif // !ASMJIT_NO_DEPRECATED
122 
123 // ============================================================================
124 // [asmjit::Target]
125 // ============================================================================
126 
127 //! Target is an abstract class that describes a machine code target.
128 class ASMJIT_VIRTAPI Target {
129 public:
130   ASMJIT_BASE_CLASS(Target)
131   ASMJIT_NONCOPYABLE(Target)
132 
133   //! Target environment information.
134   Environment _environment;
135 
136   //! \name Construction & Destruction
137   //! \{
138 
139   //! Creates a `Target` instance.
140   ASMJIT_API Target() noexcept;
141   //! Destroys the `Target` instance.
142   ASMJIT_API virtual ~Target() noexcept;
143 
144   //! \}
145 
146   //! \name Accessors
147   //! \{
148 
149   //! Returns CodeInfo of this target.
150   //!
151   //! CodeInfo can be used to setup a CodeHolder in case you plan to generate a
152   //! code compatible and executable by this Runtime.
environment()153   inline const Environment& environment() const noexcept { return _environment; }
154 
155   //! Returns the target architecture, see \ref Environment::Arch.
arch()156   inline uint32_t arch() const noexcept { return _environment.arch(); }
157   //! Returns the target sub-architecture, see \ref Environment::SubArch.
subArch()158   inline uint32_t subArch() const noexcept { return _environment.subArch(); }
159 
160 #ifndef ASMJIT_NO_DEPRECATED
161   ASMJIT_DEPRECATED("Use environment() instead")
codeInfo()162   inline CodeInfo codeInfo() const noexcept { return CodeInfo(_environment); }
163 
164   ASMJIT_DEPRECATED("Use environment().format() instead")
targetType()165   inline uint32_t targetType() const noexcept { return _environment.format(); }
166 #endif // !ASMJIT_NO_DEPRECATED
167 
168   //! \}
169 };
170 
171 //! \}
172 
173 ASMJIT_END_NAMESPACE
174 
175 #endif // ASMJIT_CORE_TARGET_H_INCLUDED
176