blob: ce3832f9d9026dc776d363fd0cd232fdea9f457b [file] [log] [blame]
Sean Callananb7e8f4a2010-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 Kramera7c40ef2014-08-13 16:26:38 +000017#ifndef LLVM_UTILS_TABLEGEN_ASMWRITERINST_H
18#define LLVM_UTILS_TABLEGEN_ASMWRITERINST_H
Sean Callananb7e8f4a2010-02-09 21:50:41 +000019
20#include <string>
21#include <vector>
22
23namespace llvm {
24 class CodeGenInstruction;
25 class Record;
Jim Grosbach806b1392010-10-11 19:38:01 +000026
Sean Callananb7e8f4a2010-02-09 21:50:41 +000027 struct AsmWriterOperand {
28 enum OpType {
29 // Output this text surrounded by quotes to the asm.
Jim Grosbach806b1392010-10-11 19:38:01 +000030 isLiteralTextOperand,
Sean Callananb7e8f4a2010-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 Grosbach806b1392010-10-11 19:38:01 +000037
Sean Callananb7e8f4a2010-02-09 21:50:41 +000038 /// Str - For isLiteralTextOperand, this IS the literal text. For
39 /// isMachineInstrOperand, this is the PrinterMethodName for the operand..
Jim Grosbach806b1392010-10-11 19:38:01 +000040 /// For isLiteralStatementOperand, this is the code to insert verbatim
Sean Callananb7e8f4a2010-02-09 21:50:41 +000041 /// into the asm writer.
42 std::string Str;
Jim Grosbach806b1392010-10-11 19:38:01 +000043
Sean Callananb7e8f4a2010-02-09 21:50:41 +000044 /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
45 /// machine instruction.
46 unsigned MIOpNo;
Jim Grosbach806b1392010-10-11 19:38:01 +000047
Sean Callananb7e8f4a2010-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 Grosbach806b1392010-10-11 19:38:01 +000051
Sean Callananb7e8f4a2010-02-09 21:50:41 +000052 // To make VS STL happy
53 AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {}
Jim Grosbach806b1392010-10-11 19:38:01 +000054
Sean Callananb7e8f4a2010-02-09 21:50:41 +000055 AsmWriterOperand(const std::string &LitStr,
56 OpType op = isLiteralTextOperand)
57 : OperandType(op), Str(LitStr) {}
Jim Grosbach806b1392010-10-11 19:38:01 +000058
Sean Callanan515937d2010-02-10 02:27:43 +000059 AsmWriterOperand(const std::string &Printer,
Sean Callanan515937d2010-02-10 02:27:43 +000060 unsigned _MIOpNo,
Sean Callananb7e8f4a2010-02-09 21:50:41 +000061 const std::string &Modifier,
Jim Grosbach806b1392010-10-11 19:38:01 +000062 OpType op = isMachineInstrOperand)
Craig Topperdb75cc12016-01-22 05:59:37 +000063 : OperandType(op), Str(Printer), MIOpNo(_MIOpNo),
Craig Topperc24a4012016-01-14 06:15:07 +000064 MiModifier(Modifier) {}
Jim Grosbach806b1392010-10-11 19:38:01 +000065
Sean Callananb7e8f4a2010-02-09 21:50:41 +000066 bool operator!=(const AsmWriterOperand &Other) const {
67 if (OperandType != Other.OperandType || Str != Other.Str) return true;
68 if (OperandType == isMachineInstrOperand)
69 return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier;
70 return false;
71 }
72 bool operator==(const AsmWriterOperand &Other) const {
73 return !operator!=(Other);
74 }
Jim Grosbach806b1392010-10-11 19:38:01 +000075
Sean Callananb7e8f4a2010-02-09 21:50:41 +000076 /// getCode - Return the code that prints this operand.
Craig Topperc24a4012016-01-14 06:15:07 +000077 std::string getCode(bool PassSubtarget) const;
Sean Callananb7e8f4a2010-02-09 21:50:41 +000078 };
Jim Grosbach806b1392010-10-11 19:38:01 +000079
Sean Callananb7e8f4a2010-02-09 21:50:41 +000080 class AsmWriterInst {
81 public:
82 std::vector<AsmWriterOperand> Operands;
83 const CodeGenInstruction *CGI;
Craig Topper9e9ae602016-01-17 08:05:33 +000084 unsigned CGIIndex;
Jim Grosbach806b1392010-10-11 19:38:01 +000085
Craig Topper9e9ae602016-01-17 08:05:33 +000086 AsmWriterInst(const CodeGenInstruction &CGI, unsigned CGIIndex,
87 unsigned Variant);
Jim Grosbach806b1392010-10-11 19:38:01 +000088
Sean Callananb7e8f4a2010-02-09 21:50:41 +000089 /// MatchesAllButOneOp - If this instruction is exactly identical to the
90 /// specified instruction except for one differing operand, return the
91 /// differing operand number. Otherwise return ~0.
92 unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
Jim Grosbach806b1392010-10-11 19:38:01 +000093
Sean Callananb7e8f4a2010-02-09 21:50:41 +000094 private:
95 void AddLiteralString(const std::string &Str) {
96 // If the last operand was already a literal text string, append this to
97 // it, otherwise add a new operand.
98 if (!Operands.empty() &&
99 Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
100 Operands.back().Str.append(Str);
101 else
102 Operands.push_back(AsmWriterOperand(Str));
103 }
104 };
105}
106
107#endif