1 //===-- CodeTemplate.cpp ----------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "CodeTemplate.h"
14 CodeTemplate::CodeTemplate(CodeTemplate
&&) = default;
16 CodeTemplate
&CodeTemplate::operator=(CodeTemplate
&&) = default;
18 InstructionTemplate::InstructionTemplate(const Instruction
&Instr
)
19 : Instr(Instr
), VariableValues(Instr
.Variables
.size()) {}
21 InstructionTemplate::InstructionTemplate(InstructionTemplate
&&) = default;
23 InstructionTemplate
&InstructionTemplate::
24 operator=(InstructionTemplate
&&) = default;
26 InstructionTemplate::InstructionTemplate(const InstructionTemplate
&) = default;
28 InstructionTemplate
&InstructionTemplate::
29 operator=(const InstructionTemplate
&) = default;
31 unsigned InstructionTemplate::getOpcode() const {
32 return Instr
.Description
->getOpcode();
35 llvm::MCOperand
&InstructionTemplate::getValueFor(const Variable
&Var
) {
36 return VariableValues
[Var
.getIndex()];
39 const llvm::MCOperand
&
40 InstructionTemplate::getValueFor(const Variable
&Var
) const {
41 return VariableValues
[Var
.getIndex()];
44 llvm::MCOperand
&InstructionTemplate::getValueFor(const Operand
&Op
) {
45 return getValueFor(Instr
.Variables
[Op
.getVariableIndex()]);
48 const llvm::MCOperand
&
49 InstructionTemplate::getValueFor(const Operand
&Op
) const {
50 return getValueFor(Instr
.Variables
[Op
.getVariableIndex()]);
53 bool InstructionTemplate::hasImmediateVariables() const {
54 return llvm::any_of(Instr
.Variables
, [this](const Variable
&Var
) {
55 return Instr
.getPrimaryOperand(Var
).isImmediate();
59 llvm::MCInst
InstructionTemplate::build() const {
61 Result
.setOpcode(Instr
.Description
->Opcode
);
62 for (const auto &Op
: Instr
.Operands
)
64 Result
.addOperand(getValueFor(Op
));
68 bool isEnumValue(ExecutionMode Execution
) {
69 return llvm::isPowerOf2_32(static_cast<uint32_t>(Execution
));
72 llvm::StringRef
getName(ExecutionMode Bit
) {
73 assert(isEnumValue(Bit
) && "Bit must be a power of two");
75 case ExecutionMode::UNKNOWN
:
77 case ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS
:
78 return "ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS";
79 case ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS
:
80 return "ALWAYS_SERIAL_TIED_REGS_ALIAS";
81 case ExecutionMode::SERIAL_VIA_MEMORY_INSTR
:
82 return "SERIAL_VIA_MEMORY_INSTR";
83 case ExecutionMode::SERIAL_VIA_EXPLICIT_REGS
:
84 return "SERIAL_VIA_EXPLICIT_REGS";
85 case ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR
:
86 return "SERIAL_VIA_NON_MEMORY_INSTR";
87 case ExecutionMode::ALWAYS_PARALLEL_MISSING_USE_OR_DEF
:
88 return "ALWAYS_PARALLEL_MISSING_USE_OR_DEF";
89 case ExecutionMode::PARALLEL_VIA_EXPLICIT_REGS
:
90 return "PARALLEL_VIA_EXPLICIT_REGS";
92 llvm_unreachable("Missing enum case");
95 llvm::ArrayRef
<ExecutionMode
> getAllExecutionBits() {
96 static const ExecutionMode kAllExecutionModeBits
[] = {
97 ExecutionMode::ALWAYS_SERIAL_IMPLICIT_REGS_ALIAS
,
98 ExecutionMode::ALWAYS_SERIAL_TIED_REGS_ALIAS
,
99 ExecutionMode::SERIAL_VIA_MEMORY_INSTR
,
100 ExecutionMode::SERIAL_VIA_EXPLICIT_REGS
,
101 ExecutionMode::SERIAL_VIA_NON_MEMORY_INSTR
,
102 ExecutionMode::ALWAYS_PARALLEL_MISSING_USE_OR_DEF
,
103 ExecutionMode::PARALLEL_VIA_EXPLICIT_REGS
,
105 return llvm::makeArrayRef(kAllExecutionModeBits
);
108 llvm::SmallVector
<ExecutionMode
, 4>
109 getExecutionModeBits(ExecutionMode Execution
) {
110 llvm::SmallVector
<ExecutionMode
, 4> Result
;
111 for (const auto Bit
: getAllExecutionBits())
112 if ((Execution
& Bit
) == Bit
)
113 Result
.push_back(Bit
);
117 } // namespace exegesis