AsmWriterInst.h revision 218893
1203954Srdivacky//===- AsmWriterInst.h - Classes encapsulating a printable inst -*- C++ -*-===// 2203954Srdivacky// 3203954Srdivacky// The LLVM Compiler Infrastructure 4203954Srdivacky// 5203954Srdivacky// This file is distributed under the University of Illinois Open Source 6203954Srdivacky// License. See LICENSE.TXT for details. 7203954Srdivacky// 8203954Srdivacky//===----------------------------------------------------------------------===// 9203954Srdivacky// 10203954Srdivacky// These classes implement a parser for assembly strings. The parser splits 11203954Srdivacky// the string into operands, which can be literal strings (the constant bits of 12203954Srdivacky// the string), actual operands (i.e., operands from the MachineInstr), and 13203954Srdivacky// dynamically-generated text, specified by raw C++ code. 14203954Srdivacky// 15203954Srdivacky//===----------------------------------------------------------------------===// 16203954Srdivacky 17203954Srdivacky#ifndef ASMWRITER_INST_H 18203954Srdivacky#define ASMWRITER_INST_H 19203954Srdivacky 20203954Srdivacky#include <string> 21203954Srdivacky#include <vector> 22203954Srdivacky 23203954Srdivackynamespace llvm { 24203954Srdivacky class CodeGenInstruction; 25203954Srdivacky class Record; 26218893Sdim 27203954Srdivacky struct AsmWriterOperand { 28203954Srdivacky enum OpType { 29203954Srdivacky // Output this text surrounded by quotes to the asm. 30218893Sdim isLiteralTextOperand, 31203954Srdivacky // This is the name of a routine to call to print the operand. 32203954Srdivacky isMachineInstrOperand, 33203954Srdivacky // Output this text verbatim to the asm writer. It is code that 34203954Srdivacky // will output some text to the asm. 35203954Srdivacky isLiteralStatementOperand 36203954Srdivacky } OperandType; 37218893Sdim 38203954Srdivacky /// Str - For isLiteralTextOperand, this IS the literal text. For 39203954Srdivacky /// isMachineInstrOperand, this is the PrinterMethodName for the operand.. 40218893Sdim /// For isLiteralStatementOperand, this is the code to insert verbatim 41203954Srdivacky /// into the asm writer. 42203954Srdivacky std::string Str; 43218893Sdim 44203954Srdivacky /// CGIOpNo - For isMachineInstrOperand, this is the index of the operand in 45203954Srdivacky /// the CodeGenInstruction. 46203954Srdivacky unsigned CGIOpNo; 47218893Sdim 48203954Srdivacky /// MiOpNo - For isMachineInstrOperand, this is the operand number of the 49203954Srdivacky /// machine instruction. 50203954Srdivacky unsigned MIOpNo; 51218893Sdim 52203954Srdivacky /// MiModifier - For isMachineInstrOperand, this is the modifier string for 53203954Srdivacky /// an operand, specified with syntax like ${opname:modifier}. 54203954Srdivacky std::string MiModifier; 55218893Sdim 56203954Srdivacky // To make VS STL happy 57203954Srdivacky AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {} 58218893Sdim 59203954Srdivacky AsmWriterOperand(const std::string &LitStr, 60203954Srdivacky OpType op = isLiteralTextOperand) 61203954Srdivacky : OperandType(op), Str(LitStr) {} 62218893Sdim 63203954Srdivacky AsmWriterOperand(const std::string &Printer, 64203954Srdivacky unsigned _CGIOpNo, 65203954Srdivacky unsigned _MIOpNo, 66203954Srdivacky const std::string &Modifier, 67218893Sdim OpType op = isMachineInstrOperand) 68203954Srdivacky : OperandType(op), Str(Printer), CGIOpNo(_CGIOpNo), MIOpNo(_MIOpNo), 69203954Srdivacky MiModifier(Modifier) {} 70218893Sdim 71203954Srdivacky bool operator!=(const AsmWriterOperand &Other) const { 72203954Srdivacky if (OperandType != Other.OperandType || Str != Other.Str) return true; 73203954Srdivacky if (OperandType == isMachineInstrOperand) 74203954Srdivacky return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier; 75203954Srdivacky return false; 76203954Srdivacky } 77203954Srdivacky bool operator==(const AsmWriterOperand &Other) const { 78203954Srdivacky return !operator!=(Other); 79203954Srdivacky } 80218893Sdim 81203954Srdivacky /// getCode - Return the code that prints this operand. 82203954Srdivacky std::string getCode() const; 83203954Srdivacky }; 84218893Sdim 85203954Srdivacky class AsmWriterInst { 86203954Srdivacky public: 87203954Srdivacky std::vector<AsmWriterOperand> Operands; 88203954Srdivacky const CodeGenInstruction *CGI; 89218893Sdim 90218893Sdim AsmWriterInst(const CodeGenInstruction &CGI, 91203954Srdivacky unsigned Variant, 92203954Srdivacky int FirstOperandColumn, 93203954Srdivacky int OperandSpacing); 94218893Sdim 95203954Srdivacky /// MatchesAllButOneOp - If this instruction is exactly identical to the 96203954Srdivacky /// specified instruction except for one differing operand, return the 97203954Srdivacky /// differing operand number. Otherwise return ~0. 98203954Srdivacky unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const; 99218893Sdim 100203954Srdivacky private: 101203954Srdivacky void AddLiteralString(const std::string &Str) { 102203954Srdivacky // If the last operand was already a literal text string, append this to 103203954Srdivacky // it, otherwise add a new operand. 104203954Srdivacky if (!Operands.empty() && 105203954Srdivacky Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand) 106203954Srdivacky Operands.back().Str.append(Str); 107203954Srdivacky else 108203954Srdivacky Operands.push_back(AsmWriterOperand(Str)); 109203954Srdivacky } 110203954Srdivacky }; 111203954Srdivacky} 112203954Srdivacky 113203954Srdivacky#endif 114