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 | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 28 | void AsmWriterEmitter::run(std::ostream &O) { |
| 29 | EmitSourceFileHeader("Assembly Writer Source Fragment", O); |
| 30 | |
| 31 | CodeGenTarget Target; |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame^] | 32 | |
| 33 | Record *AsmWriter = Target.getAsmWriter(); |
| 34 | |
| 35 | std::string AsmWriterClassName = |
| 36 | AsmWriter->getValueAsString("AsmWriterClassName"); |
| 37 | |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 38 | O << |
| 39 | "/// printInstruction - This method is automatically generated by tablegen\n" |
| 40 | "/// from the instruction set description. This method returns true if the\n" |
| 41 | "/// machine instruction was sufficiently described to print it, otherwise\n" |
| 42 | "/// it returns false.\n" |
Chris Lattner | 175580c | 2004-08-14 22:50:53 +0000 | [diff] [blame^] | 43 | "bool " << Target.getName() << AsmWriterClassName |
| 44 | << "::printInstruction(const MachineInstr *MI) {\n"; |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 45 | O << " switch (MI->getOpcode()) {\n" |
| 46 | " default: return false;\n"; |
| 47 | |
| 48 | std::string Namespace = Target.inst_begin()->second.Namespace; |
| 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; |
Chris Lattner | 076efa7 | 2004-08-01 07:43:02 +0000 | [diff] [blame] | 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 = AsmString.find('$', LastEmitted); |
| 59 | if (DollarPos == std::string::npos) DollarPos = AsmString.size(); |
| 60 | |
| 61 | // Emit a constant string fragment. |
| 62 | if (DollarPos != LastEmitted) { |
| 63 | // TODO: this should eventually handle escaping. |
| 64 | O << " << \"" << std::string(AsmString.begin()+LastEmitted, |
| 65 | AsmString.begin()+DollarPos) << "\""; |
| 66 | LastEmitted = DollarPos; |
| 67 | } else if (DollarPos+1 != AsmString.size() && |
| 68 | AsmString[DollarPos+1] == '$') { |
| 69 | O << " << '$'"; // "$$" -> $ |
| 70 | } else { |
| 71 | // Get the name of the variable. |
| 72 | // TODO: should eventually handle ${foo}bar as $foo |
| 73 | std::string::size_type VarEnd = DollarPos+1; |
| 74 | while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd])) |
| 75 | ++VarEnd; |
| 76 | std::string VarName(AsmString.begin()+DollarPos+1, |
| 77 | AsmString.begin()+VarEnd); |
| 78 | if (VarName.empty()) |
| 79 | throw "Stray '$' in '" + I->first + |
| 80 | "' asm string, maybe you want $$?"; |
| 81 | unsigned OpNo = I->second.getOperandNamed(VarName); |
| 82 | |
| 83 | // If this is a two-address instruction and we are not accessing the |
| 84 | // 0th operand, remove an operand. |
Chris Lattner | 9302ba4 | 2004-08-11 04:08:36 +0000 | [diff] [blame] | 85 | unsigned MIOp = I->second.OperandList[OpNo].MIOperandNo; |
| 86 | if (I->second.isTwoAddress && MIOp != 0) { |
| 87 | if (MIOp == 1) |
Chris Lattner | c3d5f3e | 2004-08-01 08:55:34 +0000 | [diff] [blame] | 88 | throw "Should refer to operand #0 instead of #1 for two-address" |
| 89 | " instruction '" + I->first + "'!"; |
Chris Lattner | 9302ba4 | 2004-08-11 04:08:36 +0000 | [diff] [blame] | 90 | --MIOp; |
Chris Lattner | c3d5f3e | 2004-08-01 08:55:34 +0000 | [diff] [blame] | 91 | } |
Chris Lattner | 076efa7 | 2004-08-01 07:43:02 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 1caef2c | 2004-08-11 02:23:23 +0000 | [diff] [blame] | 93 | O << "; " << I->second.OperandList[OpNo].PrinterMethodName |
Chris Lattner | 9302ba4 | 2004-08-11 04:08:36 +0000 | [diff] [blame] | 94 | << "(MI, " << MIOp << ", MVT::" |
Chris Lattner | 076efa7 | 2004-08-01 07:43:02 +0000 | [diff] [blame] | 95 | << getName(I->second.OperandList[OpNo].Ty) << "); O "; |
| 96 | LastEmitted = VarEnd; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | O << " << '\\n'; break;\n"; |
Chris Lattner | 2e1f51b | 2004-08-01 05:59:33 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | O << " }\n" |
| 104 | " return true;\n" |
| 105 | "}\n"; |
| 106 | EmitSourceFileTail(O); |
| 107 | } |