Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 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" |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 17 | #include "Record.h" |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 18 | #include <ostream> |
| 19 | using namespace llvm; |
| 20 | |
Chris Lattner | 076efa7 | 2004-08-01 07:43:02 +0000 | [diff] [blame] | 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 | |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame^] | 28 | namespace { |
| 29 | struct AsmWriterOperand { |
| 30 | enum { isLiteralTextOperand, isMachineInstrOperand } OperandType; |
| 31 | |
| 32 | /// Str - For isLiteralTextOperand, this IS the literal text. For |
| 33 | /// isMachineInstrOperand, this is the PrinterMethodName for the operand. |
| 34 | std::string Str; |
| 35 | |
| 36 | /// MiOpNo - For isMachineInstrOperand, this is the operand number of the |
| 37 | /// machine instruction. |
| 38 | unsigned MIOpNo; |
| 39 | |
| 40 | /// OpVT - For isMachineInstrOperand, this is the value type for the |
| 41 | /// operand. |
| 42 | MVT::ValueType OpVT; |
| 43 | |
| 44 | AsmWriterOperand(const std::string &LitStr) |
| 45 | : OperandType(isLiteralTextOperand), Str(LitStr) {} |
| 46 | |
| 47 | AsmWriterOperand(const std::string &Printer, unsigned OpNo, |
| 48 | MVT::ValueType VT) : OperandType(isMachineInstrOperand), |
| 49 | Str(Printer), MIOpNo(OpNo), OpVT(VT){} |
| 50 | |
| 51 | void EmitCode(std::ostream &OS) const; |
| 52 | }; |
| 53 | |
| 54 | struct AsmWriterInst { |
| 55 | std::vector<AsmWriterOperand> Operands; |
| 56 | |
| 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); |
| 61 | } |
| 62 | private: |
| 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); |
| 69 | else |
| 70 | Operands.push_back(AsmWriterOperand(Str)); |
| 71 | } |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | void AsmWriterOperand::EmitCode(std::ostream &OS) const { |
| 77 | if (OperandType == isLiteralTextOperand) |
| 78 | OS << "O << \"" << Str << "\"; "; |
| 79 | else |
| 80 | OS << Str << "(MI, " << MIOpNo << ", MVT::" << getName(OpVT) << "); "; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /// ParseAsmString - Parse the specified Instruction's AsmString into this |
| 85 | /// AsmWriterInst. |
| 86 | /// |
| 87 | void AsmWriterInst::ParseAsmString(const CodeGenInstruction &CGI, |
| 88 | unsigned Variant) { |
| 89 | bool inVariant = false; // True if we are inside a {.|.|.} region. |
| 90 | |
| 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(); |
| 97 | |
| 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] == '{') { |
| 105 | if (inVariant) |
| 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 + "'!"; |
| 117 | LastEmitted = NP+1; |
| 118 | if (AsmString[NP] == '}') { |
| 119 | inVariant = false; // No text for this variant. |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | } else if (AsmString[DollarPos] == '|') { |
| 124 | if (!inVariant) |
| 125 | throw "'|' character found outside of a variant in instruction '" |
| 126 | + CGI.Name + "'!"; |
| 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 + "'!"; |
| 131 | LastEmitted = NP+1; |
| 132 | inVariant = false; |
| 133 | } else if (AsmString[DollarPos] == '}') { |
| 134 | if (!inVariant) |
| 135 | throw "'}' character found outside of a variant in instruction '" |
| 136 | + CGI.Name + "'!"; |
| 137 | LastEmitted = DollarPos+1; |
| 138 | inVariant = false; |
| 139 | } else if (DollarPos+1 != AsmString.size() && |
| 140 | AsmString[DollarPos+1] == '$') { |
| 141 | AddLiteralString("$"); // "$$" -> $ |
| 142 | LastEmitted = DollarPos+2; |
| 143 | } else { |
| 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])) |
| 148 | ++VarEnd; |
| 149 | std::string VarName(AsmString.begin()+DollarPos+1, |
| 150 | AsmString.begin()+VarEnd); |
| 151 | if (VarName.empty()) |
| 152 | throw "Stray '$' in '" + CGI.Name + "' asm string, maybe you want $$?"; |
| 153 | |
| 154 | unsigned OpNo = CGI.getOperandNamed(VarName); |
| 155 | |
| 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) { |
| 160 | if (MIOp == 1) |
| 161 | throw "Should refer to operand #0 instead of #1 for two-address" |
| 162 | " instruction '" + CGI.Name + "'!"; |
| 163 | --MIOp; |
| 164 | } |
| 165 | |
| 166 | Operands.push_back(AsmWriterOperand(CGI.OperandList[OpNo].PrinterMethodName, |
| 167 | MIOp, CGI.OperandList[OpNo].Ty)); |
| 168 | LastEmitted = VarEnd; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | AddLiteralString("\\n"); |
| 173 | } |
| 174 | |
| 175 | |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 176 | void AsmWriterEmitter::run(std::ostream &O) { |
| 177 | EmitSourceFileHeader("Assembly Writer Source Fragment", O); |
| 178 | |
| 179 | CodeGenTarget Target; |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 180 | Record *AsmWriter = Target.getAsmWriter(); |
Chris Lattner | 953c6fe | 2004-10-03 20:19:02 +0000 | [diff] [blame] | 181 | std::string ClassName = AsmWriter->getValueAsString("AsmWriterClassName"); |
| 182 | unsigned Variant = AsmWriter->getValueAsInt("Variant"); |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 184 | O << |
| 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" |
Chris Lattner | 953c6fe | 2004-10-03 20:19:02 +0000 | [diff] [blame] | 189 | "bool " << Target.getName() << ClassName |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame] | 190 | << "::printInstruction(const MachineInstr *MI) {\n"; |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 191 | O << " switch (MI->getOpcode()) {\n" |
| 192 | " default: return false;\n"; |
| 193 | |
| 194 | std::string Namespace = Target.inst_begin()->second.Namespace; |
| 195 | |
| 196 | for (CodeGenTarget::inst_iterator I = Target.inst_begin(), |
| 197 | E = Target.inst_end(); I != E; ++I) |
| 198 | if (!I->second.AsmString.empty()) { |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame^] | 199 | O << " case " << Namespace << "::" << I->first << ": "; |
Chris Lattner | 076efa7 | 2004-08-01 07:43:02 +0000 | [diff] [blame] | 200 | |
Chris Lattner | b0b55e7 | 2005-01-22 17:32:42 +0000 | [diff] [blame^] | 201 | AsmWriterInst AWI; |
| 202 | AWI.ParseAsmString(I->second, Variant); |
| 203 | AWI.EmitCode(O); |
| 204 | O << " break;\n"; |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | O << " }\n" |
| 208 | " return true;\n" |
| 209 | "}\n"; |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 210 | } |