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 #include "../core/api-build_p.h"
25 #if defined(ASMJIT_BUILD_X86) && !defined(ASMJIT_NO_BUILDER)
26 
27 #include "../x86/x86assembler.h"
28 #include "../x86/x86builder.h"
29 
ASMJIT_BEGIN_SUB_NAMESPACE(x86)30 ASMJIT_BEGIN_SUB_NAMESPACE(x86)
31 
32 // ============================================================================
33 // [asmjit::x86::Builder - Construction / Destruction]
34 // ============================================================================
35 
36 Builder::Builder(CodeHolder* code) noexcept : BaseBuilder() {
37   if (code)
38     code->attach(this);
39 }
~Builder()40 Builder::~Builder() noexcept {}
41 
42 // ============================================================================
43 // [asmjit::x86::Builder - Finalize]
44 // ============================================================================
45 
finalize()46 Error Builder::finalize() {
47   ASMJIT_PROPAGATE(runPasses());
48   Assembler a(_code);
49   a.addEncodingOptions(encodingOptions());
50   a.addValidationOptions(validationOptions());
51   return serialize(&a);
52 }
53 
54 // ============================================================================
55 // [asmjit::x86::Builder - Events]
56 // ============================================================================
57 
onAttach(CodeHolder * code)58 Error Builder::onAttach(CodeHolder* code) noexcept {
59   uint32_t arch = code->arch();
60   if (!Environment::isFamilyX86(arch))
61     return DebugUtils::errored(kErrorInvalidArch);
62 
63   ASMJIT_PROPAGATE(Base::onAttach(code));
64 
65   _gpRegInfo.setSignature(Environment::is32Bit(arch) ? uint32_t(Gpd::kSignature) : uint32_t(Gpq::kSignature));
66   return kErrorOk;
67 }
68 
69 ASMJIT_END_SUB_NAMESPACE
70 
71 #endif // ASMJIT_BUILD_X86 && !ASMJIT_NO_BUILDER
72