Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 1 | //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend emits information about intrinsic functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "IntrinsicEmitter.h" |
| 15 | #include "Record.h" |
| 16 | using namespace llvm; |
| 17 | |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | // CodeGenIntrinsic Implementation |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC) { |
| 23 | std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic"); |
| 24 | return std::vector<CodeGenIntrinsic>(I.begin(), I.end()); |
| 25 | } |
| 26 | |
| 27 | CodeGenIntrinsic::CodeGenIntrinsic(Record *R) { |
| 28 | std::string DefName = R->getName(); |
| 29 | |
| 30 | if (DefName.size() <= 4 || |
| 31 | std::string(DefName.begin(), DefName.begin()+4) != "int_") |
| 32 | throw "Intrinsic '" + DefName + "' does not start with 'int_'!"; |
| 33 | EnumName = std::string(DefName.begin()+4, DefName.end()); |
| 34 | |
| 35 | Name = R->getValueAsString("LLVMName"); |
| 36 | if (Name == "") { |
| 37 | // If an explicit name isn't specified, derive one from the DefName. |
| 38 | Name = "llvm."; |
| 39 | for (unsigned i = 0, e = EnumName.size(); i != e; ++i) |
| 40 | if (EnumName[i] == '_') |
| 41 | Name += '.'; |
| 42 | else |
| 43 | Name += EnumName[i]; |
| 44 | } |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame^] | 45 | |
| 46 | // Parse the list of argument types. |
| 47 | ListInit *TypeList = R->getValueAsListInit("Types"); |
| 48 | for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) { |
| 49 | DefInit *DI = dynamic_cast<DefInit*>(TypeList->getElement(i)); |
| 50 | assert(DI && "Invalid list type!"); |
| 51 | Record *TyEl = DI->getDef(); |
| 52 | assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!"); |
| 53 | ArgTypes.push_back(TyEl->getValueAsString("TypeVal")); |
| 54 | } |
| 55 | if (ArgTypes.size() == 0) |
| 56 | throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!"; |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | // IntrinsicEmitter Implementation |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
| 63 | void IntrinsicEmitter::run(std::ostream &OS) { |
| 64 | EmitSourceFileHeader("Intrinsic Function Source Fragment", OS); |
| 65 | |
| 66 | std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records); |
| 67 | |
| 68 | // Emit the enum information. |
| 69 | EmitEnumInfo(Ints, OS); |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 70 | |
| 71 | // Emit the function name recognizer. |
| 72 | EmitFnNameRecognizer(Ints, OS); |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame^] | 73 | |
| 74 | // Emit the intrinsic verifier. |
| 75 | EmitVerifier(Ints, OS); |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints, |
| 79 | std::ostream &OS) { |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 80 | OS << "// Enum values for Intrinsics.h\n"; |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 81 | OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n"; |
| 82 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 83 | OS << " " << Ints[i].EnumName; |
| 84 | OS << ((i != e-1) ? ", " : " "); |
| 85 | OS << std::string(40-Ints[i].EnumName.size(), ' ') |
| 86 | << "// " << Ints[i].Name << "\n"; |
| 87 | } |
| 88 | OS << "#endif\n\n"; |
| 89 | } |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 90 | |
| 91 | void IntrinsicEmitter:: |
| 92 | EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints, |
| 93 | std::ostream &OS) { |
| 94 | // Build a function name -> intrinsic name mapping. |
| 95 | std::map<std::string, std::string> IntMapping; |
| 96 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
| 97 | IntMapping[Ints[i].Name] = Ints[i].EnumName; |
| 98 | |
| 99 | OS << "// Function name -> enum value recognizer code.\n"; |
| 100 | OS << "#ifdef GET_FUNCTION_RECOGNIZER\n"; |
| 101 | OS << " switch (Name[5]) {\n"; |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame^] | 102 | OS << " default: break;\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 103 | // Emit the intrinsics in sorted order. |
| 104 | char LastChar = 0; |
| 105 | for (std::map<std::string, std::string>::iterator I = IntMapping.begin(), |
| 106 | E = IntMapping.end(); I != E; ++I) { |
| 107 | assert(I->first.size() > 5 && std::string(I->first.begin(), |
| 108 | I->first.begin()+5) == "llvm." && |
| 109 | "Invalid intrinsic name!"); |
| 110 | if (I->first[5] != LastChar) { |
| 111 | LastChar = I->first[5]; |
| 112 | OS << " case '" << LastChar << "':\n"; |
| 113 | } |
| 114 | |
| 115 | OS << " if (Name == \"" << I->first << "\") return Intrinsic::" |
| 116 | << I->second << ";\n"; |
| 117 | } |
| 118 | OS << " }\n"; |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame^] | 119 | OS << " // The 'llvm.' namespace is reserved!\n"; |
| 120 | OS << " assert(0 && \"Unknown LLVM intrinsic function!\");\n"; |
| 121 | OS << "#endif\n\n"; |
| 122 | } |
| 123 | |
| 124 | void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, |
| 125 | std::ostream &OS) { |
| 126 | OS << "// Verifier::visitIntrinsicFunctionCall code.\n"; |
| 127 | OS << "#ifdef GET_INTRINSIC_VERIFIER\n"; |
| 128 | OS << " switch (ID) {\n"; |
| 129 | OS << " default: assert(0 && \"Invalid intrinsic!\");\n"; |
| 130 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 131 | OS << " case Intrinsic::" << Ints[i].EnumName << ":\t\t// " |
| 132 | << Ints[i].Name << "\n"; |
| 133 | OS << " Assert1(FTy->getNumParams() == " << Ints[i].ArgTypes.size()-1 |
| 134 | << ",\n" |
| 135 | << " \"Illegal # arguments for intrinsic function!\", IF);\n"; |
| 136 | OS << " Assert1(FTy->getReturnType()->getTypeID() == " |
| 137 | << Ints[i].ArgTypes[0] << ",\n" |
| 138 | << " \"Illegal result type!\", IF);\n"; |
| 139 | for (unsigned j = 1; j != Ints[i].ArgTypes.size(); ++j) |
| 140 | OS << " Assert1(FTy->getParamType(" << j-1 << ")->getTypeID() == " |
| 141 | << Ints[i].ArgTypes[j] << ",\n" |
| 142 | << " \"Illegal result type!\", IF);\n"; |
| 143 | OS << " break;\n"; |
| 144 | } |
| 145 | OS << " }\n"; |
| 146 | OS << "#endif\n\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 147 | } |
| 148 | |