1 //===-- "main" function of libc-wrappergen --------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "utils/LibcTableGenUtil/APIIndexer.h" 10 11 #include "llvm/ADT/StringRef.h" 12 #include "llvm/Support/CommandLine.h" 13 #include "llvm/TableGen/Error.h" 14 #include "llvm/TableGen/Main.h" 15 16 #include <sstream> 17 #include <string> 18 19 llvm::cl::opt<std::string> 20 FunctionName("name", llvm::cl::desc("Name of the function to be wrapped."), 21 llvm::cl::value_desc("<function name>"), llvm::cl::Required); 22 23 static bool WrapperGenMain(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) { 24 llvm_libc::APIIndexer Indexer(Records); 25 auto Iter = Indexer.FunctionSpecMap.find(FunctionName); 26 if (Iter == Indexer.FunctionSpecMap.end()) { 27 llvm::PrintFatalError("Function '" + FunctionName + 28 "' not found in any standard spec."); 29 } 30 31 // To avoid all confusion, we include the implementation header using the 32 // full path (relative the libc directory.) 33 std::string Header = Indexer.FunctionToHeaderMap[FunctionName]; 34 auto RelPath = llvm::StringRef(Header).drop_back(2); // Drop the ".h" suffix. 35 OS << "#include \"src/" << RelPath << "/" << FunctionName << ".h\"\n"; 36 37 auto &NameSpecPair = *Iter; 38 llvm::Record *FunctionSpec = NameSpecPair.second; 39 llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return"); 40 llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType"); 41 OS << "extern \"C\" " << Indexer.getTypeAsString(ReturnType) << " " 42 << FunctionName << "("; 43 44 auto ArgsList = FunctionSpec->getValueAsListOfDefs("Args"); 45 std::stringstream CallArgs; 46 std::string ArgPrefix("__arg"); 47 for (size_t i = 0; i < ArgsList.size(); ++i) { 48 llvm::Record *ArgType = ArgsList[i]->getValueAsDef("ArgType"); 49 auto TypeName = Indexer.getTypeAsString(ArgType); 50 51 if (TypeName.compare("void") == 0) { 52 if (ArgsList.size() == 1) { 53 break; 54 } else { 55 // the reason this is a fatal error is that a void argument means this 56 // function has no arguments; multiple copies of no arguments is an 57 // error. 58 llvm::PrintFatalError( 59 "The specification for function " + FunctionName + 60 " lists other arguments along with a void argument."); 61 } 62 } 63 64 OS << TypeName << " " << ArgPrefix << i; 65 CallArgs << ArgPrefix << i; 66 if (i < ArgsList.size() - 1) { 67 OS << ", "; 68 CallArgs << ", "; 69 } 70 } 71 72 // TODO: Arg types of the C++ implementation functions need not 73 // match the standard types. Either handle such differences here, or 74 // avoid such a thing in the implementations. 75 OS << ") {\n" 76 << " return __llvm_libc::" << FunctionName << "(" << CallArgs.str() 77 << ");\n" 78 << "}\n"; 79 80 return false; 81 } 82 83 int main(int argc, char *argv[]) { 84 llvm::cl::ParseCommandLineOptions(argc, argv); 85 return TableGenMain(argv[0], WrapperGenMain); 86 } 87