Line data Source code
1 : #ifdef __PLUMED_HAS_ASMJIT
2 : // This file is part of AsmJit project <https://asmjit.com>
3 : //
4 : // See asmjit.h or LICENSE.md for license and copyright information
5 : // SPDX-License-Identifier: Zlib
6 :
7 : #include "api-build_p.h"
8 : #include "assembler.h"
9 : #include "emitterutils_p.h"
10 : #include "formatter_p.h"
11 : #include "logger.h"
12 : #include "support.h"
13 :
14 : ASMJIT_BEGIN_NAMESPACE
15 :
16 : namespace EmitterUtils {
17 :
18 : #ifndef ASMJIT_NO_LOGGING
19 :
20 0 : Error finishFormattedLine(String& sb, const FormatOptions& formatOptions, const uint8_t* binData, size_t binSize, size_t offsetSize, size_t immSize, const char* comment) noexcept {
21 : ASMJIT_ASSERT(binSize >= offsetSize);
22 : const size_t kNoBinSize = SIZE_MAX;
23 :
24 0 : size_t commentSize = comment ? Support::strLen(comment, Globals::kMaxCommentSize) : 0;
25 :
26 0 : if ((binSize != 0 && binSize != kNoBinSize) || commentSize) {
27 : char sep = ';';
28 : size_t padding = Formatter::paddingFromOptions(formatOptions, FormatPaddingGroup::kRegularLine);
29 :
30 0 : for (size_t i = (binSize == kNoBinSize); i < 2; i++) {
31 0 : ASMJIT_PROPAGATE(sb.padEnd(padding));
32 :
33 : if (sep) {
34 0 : ASMJIT_PROPAGATE(sb.append(sep));
35 0 : ASMJIT_PROPAGATE(sb.append(' '));
36 : }
37 :
38 : // Append binary data or comment.
39 0 : if (i == 0) {
40 0 : ASMJIT_PROPAGATE(sb.appendHex(binData, binSize - offsetSize - immSize));
41 0 : ASMJIT_PROPAGATE(sb.appendChars('.', offsetSize * 2));
42 0 : ASMJIT_PROPAGATE(sb.appendHex(binData + binSize - immSize, immSize));
43 0 : if (commentSize == 0) break;
44 : }
45 : else {
46 0 : ASMJIT_PROPAGATE(sb.append(comment, commentSize));
47 : }
48 :
49 : sep = '|';
50 0 : padding += Formatter::paddingFromOptions(formatOptions, FormatPaddingGroup::kMachineCode);
51 : }
52 : }
53 :
54 0 : return sb.append('\n');
55 : }
56 :
57 0 : void logLabelBound(BaseAssembler* self, const Label& label) noexcept {
58 : Logger* logger = self->logger();
59 :
60 : StringTmp<512> sb;
61 0 : size_t binSize = logger->hasFlag(FormatFlags::kMachineCode) ? size_t(0) : SIZE_MAX;
62 :
63 0 : sb.appendChars(' ', logger->indentation(FormatIndentationGroup::kLabel));
64 0 : Formatter::formatLabel(sb, logger->flags(), self, label.id());
65 : sb.append(':');
66 0 : finishFormattedLine(sb, logger->options(), nullptr, binSize, 0, 0, self->_inlineComment);
67 : logger->log(sb.data(), sb.size());
68 0 : }
69 :
70 0 : void logInstructionEmitted(
71 : BaseAssembler* self,
72 : InstId instId,
73 : InstOptions options,
74 : const Operand_& o0, const Operand_& o1, const Operand_& o2, const Operand_* opExt,
75 : uint32_t relSize, uint32_t immSize, uint8_t* afterCursor) {
76 :
77 : Logger* logger = self->logger();
78 : ASMJIT_ASSERT(logger != nullptr);
79 :
80 : StringTmp<256> sb;
81 : FormatFlags formatFlags = logger->flags();
82 :
83 : uint8_t* beforeCursor = self->bufferPtr();
84 0 : intptr_t emittedSize = (intptr_t)(afterCursor - beforeCursor);
85 :
86 : Operand_ opArray[Globals::kMaxOpCount];
87 : opArrayFromEmitArgs(opArray, o0, o1, o2, opExt);
88 :
89 0 : sb.appendChars(' ', logger->indentation(FormatIndentationGroup::kCode));
90 0 : self->_funcs.formatInstruction(sb, formatFlags, self, self->arch(), BaseInst(instId, options, self->extraReg()), opArray, Globals::kMaxOpCount);
91 :
92 0 : if (Support::test(formatFlags, FormatFlags::kMachineCode))
93 0 : finishFormattedLine(sb, logger->options(), self->bufferPtr(), size_t(emittedSize), relSize, immSize, self->inlineComment());
94 : else
95 0 : finishFormattedLine(sb, logger->options(), nullptr, SIZE_MAX, 0, 0, self->inlineComment());
96 0 : logger->log(sb);
97 0 : }
98 :
99 0 : Error logInstructionFailed(
100 : BaseAssembler* self,
101 : Error err,
102 : InstId instId,
103 : InstOptions options,
104 : const Operand_& o0, const Operand_& o1, const Operand_& o2, const Operand_* opExt) {
105 :
106 : StringTmp<256> sb;
107 0 : sb.append(DebugUtils::errorAsString(err));
108 : sb.append(": ");
109 :
110 : Operand_ opArray[Globals::kMaxOpCount];
111 : opArrayFromEmitArgs(opArray, o0, o1, o2, opExt);
112 :
113 0 : self->_funcs.formatInstruction(sb, FormatFlags::kNone, self, self->arch(), BaseInst(instId, options, self->extraReg()), opArray, Globals::kMaxOpCount);
114 :
115 0 : if (self->inlineComment()) {
116 : sb.append(" ; ");
117 : sb.append(self->inlineComment());
118 : }
119 :
120 : self->resetInstOptions();
121 : self->resetExtraReg();
122 : self->resetInlineComment();
123 0 : return self->reportError(err, sb.data());
124 : }
125 :
126 : #endif
127 :
128 : } // {EmitterUtils}
129 :
130 : ASMJIT_END_NAMESPACE
131 :
132 : #endif // __PLUMED_HAS_ASMJIT
|