Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// |
| 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 coordinates the per-module state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenModule.h" |
| 15 | #include "CodeGenFunction.h" |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/GlobalVariable.h" |
| 23 | #include "llvm/Intrinsics.h" |
| 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
| 27 | |
| 28 | CodeGenModule::CodeGenModule(ASTContext &C, llvm::Module &M) |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 29 | : Context(C), TheModule(M), Types(C) {} |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | |
| 31 | llvm::Constant *CodeGenModule::GetAddrOfGlobalDecl(const Decl *D) { |
| 32 | // See if it is already in the map. |
| 33 | llvm::Constant *&Entry = GlobalDeclMap[D]; |
| 34 | if (Entry) return Entry; |
| 35 | |
| 36 | QualType ASTTy = cast<ValueDecl>(D)->getType(); |
| 37 | const llvm::Type *Ty = getTypes().ConvertType(ASTTy); |
| 38 | if (isa<FunctionDecl>(D)) { |
| 39 | const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty); |
| 40 | // FIXME: param attributes for sext/zext etc. |
| 41 | return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage, |
| 42 | D->getName(), &getModule()); |
| 43 | } |
| 44 | |
| 45 | assert(isa<FileVarDecl>(D) && "Unknown global decl!"); |
| 46 | |
| 47 | return Entry = new llvm::GlobalVariable(Ty, false, |
| 48 | llvm::GlobalValue::ExternalLinkage, |
| 49 | 0, D->getName(), &getModule()); |
| 50 | } |
| 51 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 52 | void CodeGenModule::EmitFunction(const FunctionDecl *FD) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | // If this is not a prototype, emit the body. |
| 54 | if (FD->getBody()) |
| 55 | CodeGenFunction(*this).GenerateCode(FD); |
| 56 | } |
| 57 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 58 | void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) { |
| 59 | llvm::GlobalVariable *GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalDecl(D)); |
| 60 | |
| 61 | // If the storage class is external and there is no initializer, just leave it |
| 62 | // as a declaration. |
| 63 | if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0) |
| 64 | return; |
| 65 | |
| 66 | // Otherwise, convert the initializer, or use zero if appropriate. |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 67 | llvm::Constant *Init = 0; |
| 68 | if (D->getInit() == 0) { |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 69 | Init = llvm::Constant::getNullValue(GV->getType()->getElementType()); |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 70 | } else if (D->getType()->isIntegerType()) { |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 71 | llvm::APSInt Value(getContext().getTypeSize(D->getInit()->getType(), |
| 72 | SourceLocation())); |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 73 | if (D->getInit()->isIntegerConstantExpr(Value, Context)) |
Chris Lattner | 8f32f71 | 2007-07-14 00:23:28 +0000 | [diff] [blame] | 74 | Init = llvm::ConstantInt::get(Value); |
| 75 | } |
| 76 | assert(Init && "FIXME: Global variable initializers unimp!"); |
| 77 | |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 78 | GV->setInitializer(Init); |
| 79 | |
| 80 | // Set the llvm linkage type as appropriate. |
| 81 | // FIXME: This isn't right. This should handle common linkage and other |
| 82 | // stuff. |
| 83 | switch (D->getStorageClass()) { |
| 84 | case VarDecl::Auto: |
| 85 | case VarDecl::Register: |
| 86 | assert(0 && "Can't have auto or register globals"); |
| 87 | case VarDecl::None: |
| 88 | case VarDecl::Extern: |
| 89 | // todo: common |
| 90 | break; |
| 91 | case VarDecl::Static: |
| 92 | GV->setLinkage(llvm::GlobalVariable::InternalLinkage); |
| 93 | break; |
| 94 | } |
| 95 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 32b266c | 2007-07-14 00:16:50 +0000 | [diff] [blame] | 97 | /// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified |
| 98 | /// declarator chain. |
| 99 | void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) { |
| 100 | for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator())) |
| 101 | EmitGlobalVar(D); |
| 102 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | |
| 104 | llvm::Function *CodeGenModule::getMemCpyFn() { |
| 105 | if (MemCpyFn) return MemCpyFn; |
| 106 | llvm::Intrinsic::ID IID; |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 107 | uint64_t Size; unsigned Align; |
| 108 | Context.Target.getPointerInfo(Size, Align, SourceLocation()); |
| 109 | switch (Size) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | default: assert(0 && "Unknown ptr width"); |
| 111 | case 32: IID = llvm::Intrinsic::memcpy_i32; break; |
| 112 | case 64: IID = llvm::Intrinsic::memcpy_i64; break; |
| 113 | } |
| 114 | return MemCpyFn = llvm::Intrinsic::getDeclaration(&TheModule, IID); |
| 115 | } |