Malloc prototyping now works even if the original file had its own prototype for malloc

llvm-svn: 4271
This commit is contained in:
Nick Hildenbrandt 2002-10-23 18:59:40 +00:00
parent e97144120d
commit 181dbf0851
1 changed files with 13 additions and 3 deletions

View File

@ -3,7 +3,6 @@
// This library converts LLVM code to C code, compilable by GCC. // This library converts LLVM code to C code, compilable by GCC.
// //
//===-----------------------------------------------------------------------==// //===-----------------------------------------------------------------------==//
#include "llvm/Assembly/CWriter.h" #include "llvm/Assembly/CWriter.h"
#include "llvm/Constants.h" #include "llvm/Constants.h"
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
@ -28,6 +27,7 @@ using std::string;
using std::map; using std::map;
using std::ostream; using std::ostream;
namespace { namespace {
class CWriter : public Pass, public InstVisitor<CWriter> { class CWriter : public Pass, public InstVisitor<CWriter> {
ostream &Out; ostream &Out;
@ -35,6 +35,7 @@ namespace {
const Module *TheModule; const Module *TheModule;
map<const Type *, string> TypeNames; map<const Type *, string> TypeNames;
std::set<const Value*> MangledGlobals; std::set<const Value*> MangledGlobals;
bool needsMalloc;
map<const ConstantFP *, unsigned> FPConstantMap; map<const ConstantFP *, unsigned> FPConstantMap;
public: public:
@ -549,8 +550,6 @@ void CWriter::printModule(Module *M) {
// Global variable declarations... // Global variable declarations...
if (!M->gempty()) { if (!M->gempty()) {
Out << "\n/* External Global Variable Declarations */\n"; Out << "\n/* External Global Variable Declarations */\n";
// Needed for malloc to work on sun.
Out << "extern void * malloc(size_t);\n";
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) { for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
if (I->hasExternalLinkage()) { if (I->hasExternalLinkage()) {
Out << "extern "; Out << "extern ";
@ -563,12 +562,19 @@ void CWriter::printModule(Module *M) {
// Function declarations // Function declarations
if (!M->empty()) { if (!M->empty()) {
Out << "\n/* Function Declarations */\n"; Out << "\n/* Function Declarations */\n";
needsMalloc = true;
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
printFunctionSignature(I, true); printFunctionSignature(I, true);
Out << ";\n"; Out << ";\n";
} }
} }
// Print Malloc prototype if needed
if (needsMalloc){
Out << "\n/* Malloc to make sun happy */\n";
Out << "extern void * malloc(size_t);\n\n";
}
// Output the global variable definitions and contents... // Output the global variable definitions and contents...
if (!M->gempty()) { if (!M->gempty()) {
Out << "\n\n/* Global Variable Definitions and Initialization */\n"; Out << "\n\n/* Global Variable Definitions and Initialization */\n";
@ -673,6 +679,10 @@ void CWriter::printContainedStructs(const Type *Ty,
void CWriter::printFunctionSignature(const Function *F, bool Prototype) { void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
// If the program provides it's own malloc prototype we don't need
// to include the general one.
if (getValueName(F) == "malloc")
needsMalloc = false;
if (F->hasInternalLinkage()) Out << "static "; if (F->hasInternalLinkage()) Out << "static ";
// Loop over the arguments, printing them... // Loop over the arguments, printing them...