QuISP
Loading...
Searching...
No Matches
opcode.h
Go to the documentation of this file.
1#pragma once
2#include <sstream>
3#include <string>
4#include <tuple>
5#include <variant>
6
7#include "macro_utils.h"
8#include "types.h"
9
10namespace quisp::runtime {
11
12// defines OpCode Enums
13enum OpType : int {
14#define OP(Opcode) Opcode,
15#include "def_opcodes.h"
16#undef OP
17};
18
19static const std::string OpTypeStr[]{
20#define OP(Opcode) #Opcode,
21#include "def_opcodes.h"
22#undef OP
23};
24
25// Opcode Literal types for providing static type check for the instructions
26// OP_ADD => Op<OpType::ADD>
27// see also https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Int-To-Type
28template <int I>
29struct Op {
30 enum { Value = I };
31};
32
33// this defines OP(DEBUG) as OP_DEBUG
34#define OP(Opcode) using OP_##Opcode = Op<OpType::Opcode>;
35#define OP_LAST(Opcode) using OP_##Opcode = Op<OpType::Opcode>;
36#include "def_opcodes.h"
37#undef OP
38
46template <class OpLit, class... Operands>
48 Instruction(std::tuple<Operands...> args, std::string label = "") : opcode(OpLit::Value), args(args), label(label) {}
49 Instruction(std::tuple<Operands...> args, Label label) : opcode(OpLit::Value), args(args), label(label.val) {}
50 int opcode;
51 std::tuple<Operands...> args;
52 std::string label;
53
55 std::string toString() const {
56 std::stringstream ss;
57 ss << OpTypeStr[opcode];
58 ss << " ";
59 toStringArgs(args, ss);
60 if (label.size() > 0) {
61 ss << " :" << label;
62 }
63 return ss.str();
64 }
65
66 template <size_t N = 0, typename T>
67 void toStringArgs(const T& t, std::stringstream& s) const {
68 if constexpr (N < std::tuple_size<T>::value) {
69 const auto& x = std::get<N>(t);
70 s << x << ", ";
71 // this recursion will not cause the stack overflow because the number of
72 // arguments in T are fixed.
74 }
75 }
76};
77
78// specialize template for each instruction
79#define INSTR(Opcode, ...) using INSTRUCTION_TYPE_ALIAS(Opcode, __VA_ARGS__) = Instruction<OP_##Opcode, __VA_ARGS__>;
80#include "def_instructions.h"
81#undef INSTR
82
84using InstructionTypes = std::variant<
85#define INSTR(Opcode, ...) INSTRUCTION_TYPE_ALIAS(Opcode, __VA_ARGS__),
86#define INSTR_LAST(Opcode, ...) INSTRUCTION_TYPE_ALIAS(Opcode, __VA_ARGS__)
87#include "def_instructions.h"
88#undef INSTR
89 >;
90
91} // namespace quisp::runtime
this file contains a list of macros defining the opcodes used in the IR. These opcodes are used in de...
Definition InstructionVisitor.cc:7
OpType
Definition opcode.h:13
std::variant< #define INSTR(Opcode,...) #define INSTR_LAST(Opcode,...) # 1 "/github/workspace/quisp/runtime/def_instructions.h" 1 # 87 "/github/workspace/quisp/runtime/opcode.h" 2 > InstructionTypes
a variant that is capable of storing all instructions
Definition opcode.h:84
static const std::string OpTypeStr[]
Definition opcode.h:19
this file contains the definitions of all the user-defined types widely used alongside the runtime::R...
This class represents IR (intermediate representation) instruction. Program consists of Instructions.
Definition opcode.h:47
Instruction(std::tuple< Operands... > args, Label label)
Definition opcode.h:49
std::string label
Definition opcode.h:52
int opcode
Definition opcode.h:50
void toStringArgs(const T &t, std::stringstream &s) const
Definition opcode.h:67
Instruction(std::tuple< Operands... > args, std::string label="")
Definition opcode.h:48
std::tuple< Operands... > args
Definition opcode.h:51
std::string toString() const
returns string representation for debugging
Definition opcode.h:55
label to annotate the instruction index in a Program.
Definition types.h:95
@ Value
Definition opcode.h:108