From f31182a531770ed1d647c822de68b953663102ff Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 13 Feb 2004 23:18:48 +0000 Subject: [PATCH] Convert the C backend into a target, for use with LLC. This allows us to use the lowerallocations pass to eliminate malloc/free warnings and hackish code git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11409 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/CBackend/CBackend.cpp | 42 ++++++++-------------------- lib/Target/CBackend/CTargetMachine.h | 39 ++++++++++++++++++++++++++ lib/Target/CBackend/Writer.cpp | 42 ++++++++-------------------- 3 files changed, 63 insertions(+), 60 deletions(-) create mode 100644 lib/Target/CBackend/CTargetMachine.h diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index cd24e2765d7..25c940a6e1b 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -12,7 +12,8 @@ // //===----------------------------------------------------------------------===// -#include "llvm/Assembly/CWriter.h" +#include "CTargetMachine.h" +#include "llvm/Target/TargetMachineImpls.h" #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" #include "llvm/Module.h" @@ -42,7 +43,6 @@ namespace { std::map TypeNames; std::set MangledGlobals; - bool needsMalloc; std::map FPConstantMap; public: @@ -669,7 +669,6 @@ bool CWriter::doInitialization(Module &M) { // Function declarations if (!M.empty()) { Out << "\n/* Function Declarations */\n"; - needsMalloc = true; for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { // If the function is external and the name collides don't print it. // Sometimes the bytecode likes to have multiple "declarations" for @@ -684,12 +683,6 @@ bool CWriter::doInitialization(Module &M) { } } - // Print Malloc prototype if needed - if (needsMalloc) { - Out << "\n/* Malloc to make sun happy */\n"; - Out << "extern void * malloc();\n\n"; - } - // Output the global variable declarations if (!M.gempty()) { Out << "\n\n/* Global Variable Declarations */\n"; @@ -873,11 +866,6 @@ void CWriter::printContainedStructs(const Type *Ty, void CWriter::printFunctionSignature(const Function *F, bool Prototype) { - // If the program provides its own malloc prototype we don't need - // to include the general one. - if (Mang->getValueName(F) == "malloc") - needsMalloc = false; - if (F->hasInternalLinkage()) Out << "static "; if (F->hasLinkOnceLinkage()) Out << "inline "; @@ -1264,17 +1252,7 @@ void CWriter::visitCallSite(CallSite CS) { } void CWriter::visitMallocInst(MallocInst &I) { - Out << "("; - printType(Out, I.getType()); - Out << ")malloc(sizeof("; - printType(Out, I.getType()->getElementType()); - Out << ")"; - - if (I.isArrayAllocation()) { - Out << " * " ; - writeOperand(I.getOperand(0)); - } - Out << ")"; + assert(0 && "lowerallocations pass didn't work!"); } void CWriter::visitAllocaInst(AllocaInst &I) { @@ -1291,9 +1269,7 @@ void CWriter::visitAllocaInst(AllocaInst &I) { } void CWriter::visitFreeInst(FreeInst &I) { - Out << "free((char*)"; - writeOperand(I.getOperand(0)); - Out << ")"; + assert(0 && "lowerallocations pass didn't work!"); } void CWriter::printIndexingExpression(Value *Ptr, gep_type_iterator I, @@ -1394,8 +1370,14 @@ void CWriter::visitVAArgInst(VAArgInst &I) { // External Interface declaration //===----------------------------------------------------------------------===// -void llvm::AddPassesToWriteC(PassManager &PM, std::ostream &o) { +bool CTargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &o) { + PM.add(createLowerAllocationsPass()); PM.add(createLowerInvokePass()); - //PM.add(createLowerAllocationsPass()); PM.add(new CWriter(o)); + return false; +} + +TargetMachine *llvm::allocateCTargetMachine(const Module &M, + IntrinsicLowering *IL) { + return new CTargetMachine(M, IL); } diff --git a/lib/Target/CBackend/CTargetMachine.h b/lib/Target/CBackend/CTargetMachine.h new file mode 100644 index 00000000000..53e01471ea3 --- /dev/null +++ b/lib/Target/CBackend/CTargetMachine.h @@ -0,0 +1,39 @@ +//===-- CTargetMachine.h - TargetMachine for the C backend ------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by the LLVM research group and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file declares the TargetMachine that is used by the C backend. +// +//===----------------------------------------------------------------------===// + +#ifndef CTARGETMACHINE_H +#define CTARGETMACHINE_H + +#include "llvm/Target/TargetMachine.h" + +namespace llvm { +class IntrinsicLowering; + +struct CTargetMachine : public TargetMachine { + CTargetMachine(const Module &M, IntrinsicLowering *IL) : + TargetMachine("CBackend", IL) {} + + virtual const TargetInstrInfo &getInstrInfo() const { abort(); } + virtual const TargetFrameInfo &getFrameInfo() const { abort(); } + virtual const TargetSchedInfo &getSchedInfo() const { abort(); } + virtual const TargetRegInfo &getRegInfo() const { abort(); } + virtual const TargetCacheInfo &getCacheInfo() const { abort(); } + + // This is the only thing that actually does anything here. + virtual bool addPassesToEmitAssembly(PassManager &PM, std::ostream &Out); +}; + +} // End llvm namespace + + +#endif diff --git a/lib/Target/CBackend/Writer.cpp b/lib/Target/CBackend/Writer.cpp index cd24e2765d7..25c940a6e1b 100644 --- a/lib/Target/CBackend/Writer.cpp +++ b/lib/Target/CBackend/Writer.cpp @@ -12,7 +12,8 @@ // //===----------------------------------------------------------------------===// -#include "llvm/Assembly/CWriter.h" +#include "CTargetMachine.h" +#include "llvm/Target/TargetMachineImpls.h" #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" #include "llvm/Module.h" @@ -42,7 +43,6 @@ namespace { std::map TypeNames; std::set MangledGlobals; - bool needsMalloc; std::map FPConstantMap; public: @@ -669,7 +669,6 @@ bool CWriter::doInitialization(Module &M) { // Function declarations if (!M.empty()) { Out << "\n/* Function Declarations */\n"; - needsMalloc = true; for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { // If the function is external and the name collides don't print it. // Sometimes the bytecode likes to have multiple "declarations" for @@ -684,12 +683,6 @@ bool CWriter::doInitialization(Module &M) { } } - // Print Malloc prototype if needed - if (needsMalloc) { - Out << "\n/* Malloc to make sun happy */\n"; - Out << "extern void * malloc();\n\n"; - } - // Output the global variable declarations if (!M.gempty()) { Out << "\n\n/* Global Variable Declarations */\n"; @@ -873,11 +866,6 @@ void CWriter::printContainedStructs(const Type *Ty, void CWriter::printFunctionSignature(const Function *F, bool Prototype) { - // If the program provides its own malloc prototype we don't need - // to include the general one. - if (Mang->getValueName(F) == "malloc") - needsMalloc = false; - if (F->hasInternalLinkage()) Out << "static "; if (F->hasLinkOnceLinkage()) Out << "inline "; @@ -1264,17 +1252,7 @@ void CWriter::visitCallSite(CallSite CS) { } void CWriter::visitMallocInst(MallocInst &I) { - Out << "("; - printType(Out, I.getType()); - Out << ")malloc(sizeof("; - printType(Out, I.getType()->getElementType()); - Out << ")"; - - if (I.isArrayAllocation()) { - Out << " * " ; - writeOperand(I.getOperand(0)); - } - Out << ")"; + assert(0 && "lowerallocations pass didn't work!"); } void CWriter::visitAllocaInst(AllocaInst &I) { @@ -1291,9 +1269,7 @@ void CWriter::visitAllocaInst(AllocaInst &I) { } void CWriter::visitFreeInst(FreeInst &I) { - Out << "free((char*)"; - writeOperand(I.getOperand(0)); - Out << ")"; + assert(0 && "lowerallocations pass didn't work!"); } void CWriter::printIndexingExpression(Value *Ptr, gep_type_iterator I, @@ -1394,8 +1370,14 @@ void CWriter::visitVAArgInst(VAArgInst &I) { // External Interface declaration //===----------------------------------------------------------------------===// -void llvm::AddPassesToWriteC(PassManager &PM, std::ostream &o) { +bool CTargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &o) { + PM.add(createLowerAllocationsPass()); PM.add(createLowerInvokePass()); - //PM.add(createLowerAllocationsPass()); PM.add(new CWriter(o)); + return false; +} + +TargetMachine *llvm::allocateCTargetMachine(const Module &M, + IntrinsicLowering *IL) { + return new CTargetMachine(M, IL); } -- 2.34.1