blob: 708f23cb5b0ef7b453be07ab053183071d46a550 [file] [log] [blame]
Sean Callanand32c02f2010-02-09 21:50:41 +00001//===- AsmWriterInst.h - Classes encapsulating a printable inst -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes implement a parser for assembly strings. The parser splits
11// the string into operands, which can be literal strings (the constant bits of
12// the string), actual operands (i.e., operands from the MachineInstr), and
13// dynamically-generated text, specified by raw C++ code.
14//
15//===----------------------------------------------------------------------===//
16
Benjamin Kramer00e08fc2014-08-13 16:26:38 +000017#ifndef LLVM_UTILS_TABLEGEN_ASMWRITERINST_H
18#define LLVM_UTILS_TABLEGEN_ASMWRITERINST_H
Sean Callanand32c02f2010-02-09 21:50:41 +000019
20#include <string>
21#include <vector>
22
23namespace llvm {
24 class CodeGenInstruction;
25 class Record;
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000026
Sean Callanand32c02f2010-02-09 21:50:41 +000027 struct AsmWriterOperand {
28 enum OpType {
29 // Output this text surrounded by quotes to the asm.
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000030 isLiteralTextOperand,
Sean Callanand32c02f2010-02-09 21:50:41 +000031 // This is the name of a routine to call to print the operand.
32 isMachineInstrOperand,
33 // Output this text verbatim to the asm writer. It is code that
34 // will output some text to the asm.
35 isLiteralStatementOperand
36 } OperandType;
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000037
Craig Topper43b79902016-01-22 05:59:40 +000038 /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
39 /// machine instruction.
40 unsigned MIOpNo;
41
Sean Callanand32c02f2010-02-09 21:50:41 +000042 /// Str - For isLiteralTextOperand, this IS the literal text. For
43 /// isMachineInstrOperand, this is the PrinterMethodName for the operand..
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000044 /// For isLiteralStatementOperand, this is the code to insert verbatim
Sean Callanand32c02f2010-02-09 21:50:41 +000045 /// into the asm writer.
46 std::string Str;
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000047
Sean Callanand32c02f2010-02-09 21:50:41 +000048 /// MiModifier - For isMachineInstrOperand, this is the modifier string for
49 /// an operand, specified with syntax like ${opname:modifier}.
50 std::string MiModifier;
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000051
Sean Callanand32c02f2010-02-09 21:50:41 +000052 // To make VS STL happy
53 AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {}
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000054
Sean Callanand32c02f2010-02-09 21:50:41 +000055 AsmWriterOperand(const std::string &LitStr,
56 OpType op = isLiteralTextOperand)
57 : OperandType(op), Str(LitStr) {}
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000058
Sean Callanan397170b2010-02-10 02:27:43 +000059 AsmWriterOperand(const std::string &Printer,
Sean Callanan397170b2010-02-10 02:27:43 +000060 unsigned _MIOpNo,
Sean Callanand32c02f2010-02-09 21:50:41 +000061 const std::string &Modifier,
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000062 OpType op = isMachineInstrOperand)
Craig Topper43b79902016-01-22 05:59:40 +000063 : OperandType(op), MIOpNo(_MIOpNo), Str(Printer), MiModifier(Modifier) {}
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000064
Sean Callanand32c02f2010-02-09 21:50:41 +000065 bool operator!=(const AsmWriterOperand &Other) const {
66 if (OperandType != Other.OperandType || Str != Other.Str) return true;
67 if (OperandType == isMachineInstrOperand)
68 return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier;
69 return false;
70 }
71 bool operator==(const AsmWriterOperand &Other) const {
72 return !operator!=(Other);
73 }
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000074
Sean Callanand32c02f2010-02-09 21:50:41 +000075 /// getCode - Return the code that prints this operand.
Craig Topperefefcdd2016-01-14 06:15:07 +000076 std::string getCode(bool PassSubtarget) const;
Sean Callanand32c02f2010-02-09 21:50:41 +000077 };
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000078
Sean Callanand32c02f2010-02-09 21:50:41 +000079 class AsmWriterInst {
80 public:
81 std::vector<AsmWriterOperand> Operands;
82 const CodeGenInstruction *CGI;
Craig Topper73251b72016-01-17 08:05:33 +000083 unsigned CGIIndex;
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000084
Craig Topper73251b72016-01-17 08:05:33 +000085 AsmWriterInst(const CodeGenInstruction &CGI, unsigned CGIIndex,
86 unsigned Variant);
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000087
Sean Callanand32c02f2010-02-09 21:50:41 +000088 /// MatchesAllButOneOp - If this instruction is exactly identical to the
89 /// specified instruction except for one differing operand, return the
90 /// differing operand number. Otherwise return ~0.
91 unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
Jim Grosbachc4bd6fb2010-10-11 19:38:01 +000092
Sean Callanand32c02f2010-02-09 21:50:41 +000093 private:
94 void AddLiteralString(const std::string &Str) {
95 // If the last operand was already a literal text string, append this to
96 // it, otherwise add a new operand.
97 if (!Operands.empty() &&
98 Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
99 Operands.back().Str.append(Str);
100 else
101 Operands.push_back(AsmWriterOperand(Str));
102 }
103 };
104}
105
106#endif