Don't emit the method into the llvm namespace, let the #includer decide where it...
[oota-llvm.git] / utils / TableGen / AsmWriterEmitter.cpp
1 //===- AsmWriterEmitter.cpp - Generate an assembly writer -----------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "AsmWriterEmitter.h"
16 #include "CodeGenTarget.h"
17 #include "Record.h"
18 #include <ostream>
19 using namespace llvm;
20
21 static bool isIdentChar(char C) {
22   return (C >= 'a' && C <= 'z') ||
23          (C >= 'A' && C <= 'Z') ||
24          (C >= '0' && C <= '9') ||
25          C == '_';
26 }
27
28 void AsmWriterEmitter::run(std::ostream &O) {
29   EmitSourceFileHeader("Assembly Writer Source Fragment", O);
30
31   CodeGenTarget Target;
32   Record *AsmWriter = Target.getAsmWriter();
33   std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
34   unsigned Variant = AsmWriter->getValueAsInt("Variant");
35
36   O <<
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";
45
46   std::string Namespace = Target.inst_begin()->second.Namespace;
47
48   bool inVariant = false;  // True if we are inside a {.|.|.} region.
49
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 ";
55
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();
61
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] == '{') {
69           if (inVariant)
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 + "'!";
81             LastEmitted = NP+1;
82             if (AsmString[NP] == '}') {
83               inVariant = false;        // No text for this variant.
84               break;
85             }
86           }
87         } else if (AsmString[DollarPos] == '|') {
88           if (!inVariant)
89             throw "'|' character found outside of a variant in instruction '"
90                   + I->first + "'!";
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 + "'!";
95           LastEmitted = NP+1;
96           inVariant = false;
97         } else if (AsmString[DollarPos] == '}') {
98           if (!inVariant)
99             throw "'}' character found outside of a variant in instruction '"
100                   + I->first + "'!";
101           LastEmitted = DollarPos+1;
102           inVariant = false;
103         } else if (DollarPos+1 != AsmString.size() &&
104                    AsmString[DollarPos+1] == '$') {
105           O << " << '$'";         // "$$" -> $
106         } else {
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]))
111             ++VarEnd;
112           std::string VarName(AsmString.begin()+DollarPos+1,
113                               AsmString.begin()+VarEnd);
114           if (VarName.empty())
115             throw "Stray '$' in '" + I->first +
116                   "' asm string, maybe you want $$?";
117           unsigned OpNo = I->second.getOperandNamed(VarName);
118
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) {
123             if (MIOp == 1)
124               throw "Should refer to operand #0 instead of #1 for two-address"
125                     " instruction '" + I->first + "'!";
126             --MIOp;
127           }
128
129           O << ";  " << I->second.OperandList[OpNo].PrinterMethodName 
130             << "(MI, " << MIOp << ", MVT::"
131             << getName(I->second.OperandList[OpNo].Ty) << "); O ";
132           LastEmitted = VarEnd;
133         }
134       }
135
136       O << " << '\\n'; break;\n";
137     }
138
139   O << "  }\n"
140        "  return true;\n"
141        "}\n";
142 }