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') ||
28 void AsmWriterEmitter::run(std::ostream &O) {
29 EmitSourceFileHeader("Assembly Writer Source Fragment", O);
32 Record *AsmWriter = Target.getAsmWriter();
33 std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
34 unsigned Variant = AsmWriter->getValueAsInt("Variant");
37 "/// printInstruction - This method is automatically generated by tablegen\n"
38 "/// from the instruction set description. This method returns true if the\n"
39 "/// machine instruction was sufficiently described to print it, otherwise\n"
40 "/// it returns false.\n"
41 "bool " << Target.getName() << ClassName
42 << "::printInstruction(const MachineInstr *MI) {\n";
43 O << " switch (MI->getOpcode()) {\n"
44 " default: return false;\n";
46 std::string Namespace = Target.inst_begin()->second.Namespace;
48 bool inVariant = false; // True if we are inside a {.|.|.} region.
50 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
51 E = Target.inst_end(); I != E; ++I)
52 if (!I->second.AsmString.empty()) {
53 const std::string &AsmString = I->second.AsmString;
54 O << " case " << Namespace << "::" << I->first << ": O ";
56 std::string::size_type LastEmitted = 0;
57 while (LastEmitted != AsmString.size()) {
58 std::string::size_type DollarPos =
59 AsmString.find_first_of("${|}", LastEmitted);
60 if (DollarPos == std::string::npos) DollarPos = AsmString.size();
62 // Emit a constant string fragment.
63 if (DollarPos != LastEmitted) {
64 // TODO: this should eventually handle escaping.
65 O << " << \"" << std::string(AsmString.begin()+LastEmitted,
66 AsmString.begin()+DollarPos) << "\"";
67 LastEmitted = DollarPos;
68 } else if (AsmString[DollarPos] == '{') {
70 throw "Nested variants found for instruction '" + I->first + "'!";
71 LastEmitted = DollarPos+1;
72 inVariant = true; // We are now inside of the variant!
73 for (unsigned i = 0; i != Variant; ++i) {
74 // Skip over all of the text for an irrelevant variant here. The
75 // next variant starts at |, or there may not be text for this
76 // variant if we see a }.
77 std::string::size_type NP =
78 AsmString.find_first_of("|}", LastEmitted);
79 if (NP == std::string::npos)
80 throw "Incomplete variant for instruction '" + I->first + "'!";
82 if (AsmString[NP] == '}') {
83 inVariant = false; // No text for this variant.
87 } else if (AsmString[DollarPos] == '|') {
89 throw "'|' character found outside of a variant in instruction '"
91 // Move to the end of variant list.
92 std::string::size_type NP = AsmString.find('}', LastEmitted);
93 if (NP == std::string::npos)
94 throw "Incomplete variant for instruction '" + I->first + "'!";
97 } else if (AsmString[DollarPos] == '}') {
99 throw "'}' character found outside of a variant in instruction '"
101 LastEmitted = DollarPos+1;
103 } else if (DollarPos+1 != AsmString.size() &&
104 AsmString[DollarPos+1] == '$') {
105 O << " << '$'"; // "$$" -> $
107 // Get the name of the variable.
108 // TODO: should eventually handle ${foo}bar as $foo
109 std::string::size_type VarEnd = DollarPos+1;
110 while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
112 std::string VarName(AsmString.begin()+DollarPos+1,
113 AsmString.begin()+VarEnd);
115 throw "Stray '$' in '" + I->first +
116 "' asm string, maybe you want $$?";
117 unsigned OpNo = I->second.getOperandNamed(VarName);
119 // If this is a two-address instruction and we are not accessing the
120 // 0th operand, remove an operand.
121 unsigned MIOp = I->second.OperandList[OpNo].MIOperandNo;
122 if (I->second.isTwoAddress && MIOp != 0) {
124 throw "Should refer to operand #0 instead of #1 for two-address"
125 " instruction '" + I->first + "'!";
129 O << "; " << I->second.OperandList[OpNo].PrinterMethodName
130 << "(MI, " << MIOp << ", MVT::"
131 << getName(I->second.OperandList[OpNo].Ty) << "); O ";
132 LastEmitted = VarEnd;
136 O << " << '\\n'; break;\n";