assembler.h 5.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
// [AsmJit]
// Complete x86/x64 JIT and Remote Assembler for C++.
//
// [License]
// Zlib - See LICENSE.md file in the package.

// [Guard]
#ifndef _ASMJIT_BASE_ASSEMBLER_H
#define _ASMJIT_BASE_ASSEMBLER_H

11
12
13
// [Dependencies]
#include "../base/codeemitter.h"
#include "../base/codeholder.h"
14
#include "../base/operand.h"
15
#include "../base/simdtypes.h"
16
17

// [Api-Begin]
18
#include "../asmjit_apibegin.h"
19
20
21

namespace asmjit {

22
//! \addtogroup asmjit_base
23
24
25
26
27
28
29
30
//! \{

// ============================================================================
// [asmjit::Assembler]
// ============================================================================

//! Base assembler.
//!
31
32
//! This class implements a base interface that is used by architecture
//! specific assemblers.
33
//!
34
35
36
37
38
//! \sa CodeCompiler.
class ASMJIT_VIRTAPI Assembler : public CodeEmitter {
public:
  ASMJIT_NONCOPYABLE(Assembler)
  typedef CodeEmitter Base;
39
40
41
42
43
44

  // --------------------------------------------------------------------------
  // [Construction / Destruction]
  // --------------------------------------------------------------------------

  //! Create a new `Assembler` instance.
45
  ASMJIT_API Assembler() noexcept;
46
  //! Destroy the `Assembler` instance.
47
  ASMJIT_API virtual ~Assembler() noexcept;
48
49

  // --------------------------------------------------------------------------
50
  // [Events]
51
52
  // --------------------------------------------------------------------------

53
54
  ASMJIT_API Error onAttach(CodeHolder* code) noexcept override;
  ASMJIT_API Error onDetach(CodeHolder* code) noexcept override;
55
56

  // --------------------------------------------------------------------------
57
  // [Code-Generation]
58
59
  // --------------------------------------------------------------------------

60
  using CodeEmitter::_emit;
61

62
63
  ASMJIT_API Error _emit(uint32_t instId, const Operand_& o0, const Operand_& o1, const Operand_& o2, const Operand_& o3, const Operand_& o4, const Operand_& o5) override;
  ASMJIT_API Error _emitOpArray(uint32_t instId, const Operand_* opArray, size_t opCount) override;
64
65

  // --------------------------------------------------------------------------
66
  // [Code-Buffer]
67
68
  // --------------------------------------------------------------------------

69
70
  //! Called by \ref CodeHolder::sync().
  ASMJIT_API virtual void sync() noexcept;
71

72
73
74
75
  //! Get the capacity of the current CodeBuffer.
  ASMJIT_INLINE size_t getBufferCapacity() const noexcept { return (size_t)(_bufferEnd - _bufferData); }
  //! Get the number of remaining bytes in the current CodeBuffer.
  ASMJIT_INLINE size_t getRemainingSpace() const noexcept { return (size_t)(_bufferEnd - _bufferPtr); }
76

77
78
79
  //! Get the current position in the CodeBuffer.
  ASMJIT_INLINE size_t getOffset() const noexcept { return (size_t)(_bufferPtr - _bufferData); }
  //! Set the current position in the CodeBuffer to `offset`.
80
  //!
81
82
83
  //! NOTE: The `offset` cannot be outside of the buffer length (even if it's
  //! within buffer's capacity).
  ASMJIT_API Error setOffset(size_t offset);
84

85
86
87
88
89
90
  //! Get start of the CodeBuffer of the current section.
  ASMJIT_INLINE uint8_t* getBufferData() const noexcept { return _bufferData; }
  //! Get end (first invalid byte) of the current section.
  ASMJIT_INLINE uint8_t* getBufferEnd() const noexcept { return _bufferEnd; }
  //! Get pointer in the CodeBuffer of the current section.
  ASMJIT_INLINE uint8_t* getBufferPtr() const noexcept { return _bufferPtr; }
91
92

  // --------------------------------------------------------------------------
93
  // [Code-Generation]
94
95
  // --------------------------------------------------------------------------

96
97
98
99
100
101
102
103
104
105
106
  ASMJIT_API Label newLabel() override;
  ASMJIT_API Label newNamedLabel(
    const char* name,
    size_t nameLength = Globals::kInvalidIndex,
    uint32_t type = Label::kTypeGlobal,
    uint32_t parentId = 0) override;
  ASMJIT_API Error bind(const Label& label) override;
  ASMJIT_API Error embed(const void* data, uint32_t size) override;
  ASMJIT_API Error embedLabel(const Label& label) override;
  ASMJIT_API Error embedConstPool(const Label& label, const ConstPool& pool) override;
  ASMJIT_API Error comment(const char* s, size_t len = Globals::kInvalidIndex) override;
107
108

  // --------------------------------------------------------------------------
109
  // [Emit-Helpers]
110
111
  // --------------------------------------------------------------------------

112
113
114
115
116
protected:
#if !defined(ASMJIT_DISABLE_LOGGING)
  void _emitLog(
    uint32_t instId, uint32_t options, const Operand_& o0, const Operand_& o1, const Operand_& o2, const Operand_& o3,
    uint32_t relSize, uint32_t imLen, uint8_t* afterCursor);
117

118
119
120
121
122
123
124
  Error _emitFailed(
    Error err,
    uint32_t instId, uint32_t options, const Operand_& o0, const Operand_& o1, const Operand_& o2, const Operand_& o3);
#else
  ASMJIT_INLINE Error _emitFailed(
    uint32_t err,
    uint32_t instId, uint32_t options, const Operand_& o0, const Operand_& o1, const Operand_& o2, const Operand_& o3) {
125

126
127
128
    resetOptions();
    resetInlineComment();
    return setLastError(err);
129
  }
130
#endif
131
132
133
134
135

  // --------------------------------------------------------------------------
  // [Members]
  // --------------------------------------------------------------------------

136
137
138
139
140
141
142
143
public:
  SectionEntry* _section;                //!< Current section where the assembling happens.
  uint8_t* _bufferData;                  //!< Start of the CodeBuffer of the current section.
  uint8_t* _bufferEnd;                   //!< End (first invalid byte) of the current section.
  uint8_t* _bufferPtr;                   //!< Pointer in the CodeBuffer of the current section.

  Operand_ _op4;                         //!< 5th operand data, used only temporarily.
  Operand_ _op5;                         //!< 6th operand data, used only temporarily.
144
145
146
147
148
149
150
};

//! \}

} // asmjit namespace

// [Api-End]
151
#include "../asmjit_apiend.h"
152
153
154

// [Guard]
#endif // _ASMJIT_BASE_ASSEMBLER_H