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;
57 void ParseAsmString(const CodeGenInstruction &CGI, unsigned Variant);
58 void EmitCode(std::ostream &OS) const {
59 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
60 Operands[i].EmitCode(OS);
63 void AddLiteralString(const std::string &Str) {
64 // If the last operand was already a literal text string, append this to
65 // it, otherwise add a new operand.
66 if (!Operands.empty() &&
67 Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
68 Operands.back().Str.append(Str);
70 Operands.push_back(AsmWriterOperand(Str));
76 void AsmWriterOperand::EmitCode(std::ostream &OS) const {
77 if (OperandType == isLiteralTextOperand)
78 OS << "O << \"" << Str << "\"; ";
80 OS << Str << "(MI, " << MIOpNo << ", MVT::" << getName(OpVT) << "); ";
84 /// ParseAsmString - Parse the specified Instruction's AsmString into this
87 void AsmWriterInst::ParseAsmString(const CodeGenInstruction &CGI,
89 bool inVariant = false; // True if we are inside a {.|.|.} region.
91 const std::string &AsmString = CGI.AsmString;
92 std::string::size_type LastEmitted = 0;
93 while (LastEmitted != AsmString.size()) {
94 std::string::size_type DollarPos =
95 AsmString.find_first_of("${|}", LastEmitted);
96 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
98 // Emit a constant string fragment.
99 if (DollarPos != LastEmitted) {
100 // TODO: this should eventually handle escaping.
101 AddLiteralString(std::string(AsmString.begin()+LastEmitted,
102 AsmString.begin()+DollarPos));
103 LastEmitted = DollarPos;
104 } else if (AsmString[DollarPos] == '{') {
106 throw "Nested variants found for instruction '" + CGI.Name + "'!";
107 LastEmitted = DollarPos+1;
108 inVariant = true; // We are now inside of the variant!
109 for (unsigned i = 0; i != Variant; ++i) {
110 // Skip over all of the text for an irrelevant variant here. The
111 // next variant starts at |, or there may not be text for this
112 // variant if we see a }.
113 std::string::size_type NP =
114 AsmString.find_first_of("|}", LastEmitted);
115 if (NP == std::string::npos)
116 throw "Incomplete variant for instruction '" + CGI.Name + "'!";
118 if (AsmString[NP] == '}') {
119 inVariant = false; // No text for this variant.
123 } else if (AsmString[DollarPos] == '|') {
125 throw "'|' character found outside of a variant in instruction '"
127 // Move to the end of variant list.
128 std::string::size_type NP = AsmString.find('}', LastEmitted);
129 if (NP == std::string::npos)
130 throw "Incomplete variant for instruction '" + CGI.Name + "'!";
133 } else if (AsmString[DollarPos] == '}') {
135 throw "'}' character found outside of a variant in instruction '"
137 LastEmitted = DollarPos+1;
139 } else if (DollarPos+1 != AsmString.size() &&
140 AsmString[DollarPos+1] == '$') {
141 AddLiteralString("$"); // "$$" -> $
142 LastEmitted = DollarPos+2;
144 // Get the name of the variable.
145 // TODO: should eventually handle ${foo}bar as $foo
146 std::string::size_type VarEnd = DollarPos+1;
147 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
149 std::string VarName(AsmString.begin()+DollarPos+1,
150 AsmString.begin()+VarEnd);
152 throw "Stray '$' in '" + CGI.Name + "' asm string, maybe you want $$?";
154 unsigned OpNo = CGI.getOperandNamed(VarName);
156 // If this is a two-address instruction and we are not accessing the
157 // 0th operand, remove an operand.
158 unsigned MIOp = CGI.OperandList[OpNo].MIOperandNo;
159 if (CGI.isTwoAddress && MIOp != 0) {
161 throw "Should refer to operand #0 instead of #1 for two-address"
162 " instruction '" + CGI.Name + "'!";
166 Operands.push_back(AsmWriterOperand(CGI.OperandList[OpNo].PrinterMethodName,
167 MIOp, CGI.OperandList[OpNo].Ty));
168 LastEmitted = VarEnd;
172 AddLiteralString("\\n");
176 void AsmWriterEmitter::run(std::ostream &O) {
177 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
179 CodeGenTarget Target;
180 Record *AsmWriter = Target.getAsmWriter();
181 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
182 unsigned Variant = AsmWriter->getValueAsInt("Variant");
185 "/// printInstruction - This method is automatically generated by tablegen\n"
186 "/// from the instruction set description. This method returns true if the\n"
187 "/// machine instruction was sufficiently described to print it, otherwise\n"
188 "/// it returns false.\n"
189 "bool " << Target.getName() << ClassName
190 << "::printInstruction(const MachineInstr *MI) {\n";
191 O << " switch (MI->getOpcode()) {\n"
192 " default: return false;\n";
194 std::string Namespace = Target.inst_begin()->second.Namespace;
196 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
197 E = Target.inst_end(); I != E; ++I)
198 if (!I->second.AsmString.empty()) {
199 O << " case " << Namespace << "::" << I->first << ": ";
202 AWI.ParseAsmString(I->second, Variant);