1 // AsmJit - Complete JIT Assembler for C++ Language.
3 // Copyright (c) 2008-2010, Petr Kobalicek <kobalicek.petr@gmail.com>
5 // Permission is hereby granted, free of charge, to any person
6 // obtaining a copy of this software and associated documentation
7 // files (the "Software"), to deal in the Software without
8 // restriction, including without limitation the rights to use,
9 // copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 // OTHER DEALINGS IN THE SOFTWARE.
26 // We are using sprintf() here.
27 #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
28 #define _CRT_SECURE_NO_WARNINGS
32 #include "Assembler.h"
47 // ============================================================================
48 // [AsmJit::Emittable]
49 // ============================================================================
51 Emittable::Emittable(Compiler* c, uint32_t type) ASMJIT_NOTHROW :
60 _offset(INVALID_VALUE)
64 Emittable::~Emittable() ASMJIT_NOTHROW
68 void Emittable::prepare(CompilerContext& cc) ASMJIT_NOTHROW
70 _offset = cc._currentOffset;
73 Emittable* Emittable::translate(CompilerContext& cc) ASMJIT_NOTHROW
78 void Emittable::emit(Assembler& a) ASMJIT_NOTHROW
82 void Emittable::post(Assembler& a) ASMJIT_NOTHROW
86 int Emittable::getMaxSize() const ASMJIT_NOTHROW
88 // Default maximum size is -1 which means that it's not known.
92 bool Emittable::_tryUnuseVar(VarData* v) ASMJIT_NOTHROW
97 void Emittable::setComment(const char* str) ASMJIT_NOTHROW
99 _comment = _compiler->getZone().zstrdup(str);
102 void Emittable::setCommentF(const char* fmt, ...) ASMJIT_NOTHROW
104 // I'm really not expecting larger inline comments:)
109 vsnprintf(buf, 255, fmt, ap);
112 // I don't know if vsnprintf can produce non-null terminated string, in case
113 // it can, we terminate it here.
119 // ============================================================================
121 // ============================================================================
123 EDummy::EDummy(Compiler* c) ASMJIT_NOTHROW :
124 Emittable(c, EMITTABLE_DUMMY)
128 EDummy::~EDummy() ASMJIT_NOTHROW
132 int EDummy::getMaxSize() const ASMJIT_NOTHROW
137 // ============================================================================
138 // [AsmJit::EFunctionEnd]
139 // ============================================================================
141 EFunctionEnd::EFunctionEnd(Compiler* c) ASMJIT_NOTHROW :
144 _type = EMITTABLE_FUNCTION_END;
147 EFunctionEnd::~EFunctionEnd() ASMJIT_NOTHROW
151 Emittable* EFunctionEnd::translate(CompilerContext& cc) ASMJIT_NOTHROW
157 // ============================================================================
158 // [AsmJit::EComment]
159 // ============================================================================
161 EComment::EComment(Compiler* c, const char* str) ASMJIT_NOTHROW :
162 Emittable(c, EMITTABLE_COMMENT)
167 EComment::~EComment() ASMJIT_NOTHROW
171 void EComment::emit(Assembler& a) ASMJIT_NOTHROW
175 a.getLogger()->logString(getComment());
179 int EComment::getMaxSize() const ASMJIT_NOTHROW
184 // ============================================================================
186 // ============================================================================
188 EData::EData(Compiler* c, const void* data, sysuint_t length) ASMJIT_NOTHROW :
189 Emittable(c, EMITTABLE_EMBEDDED_DATA)
192 memcpy(_data, data, length);
195 EData::~EData() ASMJIT_NOTHROW
199 void EData::emit(Assembler& a) ASMJIT_NOTHROW
201 a.embed(_data, _length);
204 int EData::getMaxSize() const ASMJIT_NOTHROW
206 return (int)_length;;
209 // ============================================================================
211 // ============================================================================
213 EAlign::EAlign(Compiler* c, uint32_t size) ASMJIT_NOTHROW :
214 Emittable(c, EMITTABLE_ALIGN), _size(size)
218 EAlign::~EAlign() ASMJIT_NOTHROW
222 void EAlign::emit(Assembler& a) ASMJIT_NOTHROW
227 int EAlign::getMaxSize() const ASMJIT_NOTHROW
229 return (_size > 0) ? (int)_size - 1 : 0;
232 // ============================================================================
234 // ============================================================================
236 ETarget::ETarget(Compiler* c, const Label& label) ASMJIT_NOTHROW :
237 Emittable(c, EMITTABLE_TARGET),
245 ETarget::~ETarget() ASMJIT_NOTHROW
249 void ETarget::prepare(CompilerContext& cc) ASMJIT_NOTHROW
251 _offset = cc._currentOffset++;
254 Emittable* ETarget::translate(CompilerContext& cc) ASMJIT_NOTHROW
256 // If this ETarget was already translated, it's needed to change the current
257 // state and return NULL to tell CompilerContext to process next untranslated
261 cc._restoreState(_state);
269 // Assign state to the compiler context.
270 ASMJIT_ASSERT(_state != NULL);
271 cc._assignState(_state);
275 _state = cc._saveState();
281 void ETarget::emit(Assembler& a) ASMJIT_NOTHROW
286 int ETarget::getMaxSize() const ASMJIT_NOTHROW
291 } // AsmJit namespace