1 //===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend is emits an assembly printer for the current target.
11 // Note that this is currently fairly skeletal, but will grow over time.
13 //===----------------------------------------------------------------------===//
15 #include "AsmWriterEmitter.h"
16 #include "CodeGenTarget.h"
21 static bool isIdentChar(char C) {
22 return (C >= 'a' && C <= 'z') ||
23 (C >= 'A' && C <= 'Z') ||
24 (C >= '0' && C <= '9') ||
29 struct AsmWriterOperand {
30 enum { isLiteralTextOperand, isMachineInstrOperand } OperandType;
32 /// Str - For isLiteralTextOperand, this IS the literal text. For
33 /// isMachineInstrOperand, this is the PrinterMethodName for the operand.
36 /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
37 /// machine instruction.
40 /// OpVT - For isMachineInstrOperand, this is the value type for the
44 AsmWriterOperand(const std::string &LitStr)
45 : OperandType(isLiteralTextOperand), Str(LitStr) {}
47 AsmWriterOperand(const std::string &Printer, unsigned OpNo,
48 MVT::ValueType VT) : OperandType(isMachineInstrOperand),
49 Str(Printer), MIOpNo(OpNo), OpVT(VT){}
51 void EmitCode(std::ostream &OS) const;
54 struct AsmWriterInst {
55 std::vector<AsmWriterOperand> Operands;
56 const CodeGenInstruction *CGI;
58 AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant);
59 void EmitCode(std::ostream &OS) const {
60 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
61 Operands[i].EmitCode(OS);
64 void AddLiteralString(const std::string &Str) {
65 // If the last operand was already a literal text string, append this to
66 // it, otherwise add a new operand.
67 if (!Operands.empty() &&
68 Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
69 Operands.back().Str.append(Str);
71 Operands.push_back(AsmWriterOperand(Str));
77 void AsmWriterOperand::EmitCode(std::ostream &OS) const {
78 if (OperandType == isLiteralTextOperand)
79 OS << "O << \"" << Str << "\"; ";
81 OS << Str << "(MI, " << MIOpNo << ", MVT::" << getName(OpVT) << "); ";
85 /// ParseAsmString - Parse the specified Instruction's AsmString into this
88 AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
90 bool inVariant = false; // True if we are inside a {.|.|.} region.
92 const std::string &AsmString = CGI.AsmString;
93 std::string::size_type LastEmitted = 0;
94 while (LastEmitted != AsmString.size()) {
95 std::string::size_type DollarPos =
96 AsmString.find_first_of("${|}", LastEmitted);
97 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
99 // Emit a constant string fragment.
100 if (DollarPos != LastEmitted) {
101 // TODO: this should eventually handle escaping.
102 AddLiteralString(std::string(AsmString.begin()+LastEmitted,
103 AsmString.begin()+DollarPos));
104 LastEmitted = DollarPos;
105 } else if (AsmString[DollarPos] == '{') {
107 throw "Nested variants found for instruction '" + CGI.Name + "'!";
108 LastEmitted = DollarPos+1;
109 inVariant = true; // We are now inside of the variant!
110 for (unsigned i = 0; i != Variant; ++i) {
111 // Skip over all of the text for an irrelevant variant here. The
112 // next variant starts at |, or there may not be text for this
113 // variant if we see a }.
114 std::string::size_type NP =
115 AsmString.find_first_of("|}", LastEmitted);
116 if (NP == std::string::npos)
117 throw "Incomplete variant for instruction '" + CGI.Name + "'!";
119 if (AsmString[NP] == '}') {
120 inVariant = false; // No text for this variant.
124 } else if (AsmString[DollarPos] == '|') {
126 throw "'|' character found outside of a variant in instruction '"
128 // Move to the end of variant list.
129 std::string::size_type NP = AsmString.find('}', LastEmitted);
130 if (NP == std::string::npos)
131 throw "Incomplete variant for instruction '" + CGI.Name + "'!";
134 } else if (AsmString[DollarPos] == '}') {
136 throw "'}' character found outside of a variant in instruction '"
138 LastEmitted = DollarPos+1;
140 } else if (DollarPos+1 != AsmString.size() &&
141 AsmString[DollarPos+1] == '$') {
142 AddLiteralString("$"); // "$$" -> $
143 LastEmitted = DollarPos+2;
145 // Get the name of the variable.
146 // TODO: should eventually handle ${foo}bar as $foo
147 std::string::size_type VarEnd = DollarPos+1;
148 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
150 std::string VarName(AsmString.begin()+DollarPos+1,
151 AsmString.begin()+VarEnd);
153 throw "Stray '$' in '" + CGI.Name + "' asm string, maybe you want $$?";
155 unsigned OpNo = CGI.getOperandNamed(VarName);
156 CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
158 // If this is a two-address instruction and we are not accessing the
159 // 0th operand, remove an operand.
160 unsigned MIOp = OpInfo.MIOperandNo;
161 if (CGI.isTwoAddress && MIOp != 0) {
163 throw "Should refer to operand #0 instead of #1 for two-address"
164 " instruction '" + CGI.Name + "'!";
168 Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName,
170 LastEmitted = VarEnd;
174 AddLiteralString("\\n");
178 void AsmWriterEmitter::run(std::ostream &O) {
179 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
181 CodeGenTarget Target;
182 Record *AsmWriter = Target.getAsmWriter();
183 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
184 unsigned Variant = AsmWriter->getValueAsInt("Variant");
187 "/// printInstruction - This method is automatically generated by tablegen\n"
188 "/// from the instruction set description. This method returns true if the\n"
189 "/// machine instruction was sufficiently described to print it, otherwise\n"
190 "/// it returns false.\n"
191 "bool " << Target.getName() << ClassName
192 << "::printInstruction(const MachineInstr *MI) {\n";
193 O << " switch (MI->getOpcode()) {\n"
194 " default: return false;\n";
196 std::string Namespace = Target.inst_begin()->second.Namespace;
198 std::vector<AsmWriterInst> Instructions;
200 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
201 E = Target.inst_end(); I != E; ++I)
202 if (!I->second.AsmString.empty())
203 Instructions.push_back(AsmWriterInst(I->second, Variant));
205 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
206 O << " case " << Namespace << "::"
207 << Instructions[i].CGI->Name << ": ";
208 Instructions[i].EmitCode(O);