comparison ir/irclass.cpp @ 1317:4099548c80e0

Allocate objects on the stack if they (a) don't have a destructor, and (b) don't override the delete operator (on top of the regular conditions for stack allocation that also apply to arrays, structs, etc.). The "no destructor" clause is not strictly necessary, but calling them at the right time would be tricky to say the least; it would involve, among other things, "manually" inserting a try-finally block around anything that might throw exceptions not caught in the current function. Note: objects with custom new operators are automatically ignored because they don't use the regular allocation runtime call, so there's no need to pay special attention to them.
author Frits van Bommel <fvbommel wxs.nl>
date Sat, 09 May 2009 00:50:15 +0200
parents 8fb39f7f1a7c
children c21a6654cce2
comparison
equal deleted inserted replaced
1316:8c65217be813 1317:4099548c80e0
9 #include "gen/logger.h" 9 #include "gen/logger.h"
10 #include "gen/tollvm.h" 10 #include "gen/tollvm.h"
11 #include "gen/llvmhelpers.h" 11 #include "gen/llvmhelpers.h"
12 #include "gen/utils.h" 12 #include "gen/utils.h"
13 #include "gen/arrays.h" 13 #include "gen/arrays.h"
14 #include "gen/metadata.h"
14 15
15 #include "ir/irstruct.h" 16 #include "ir/irstruct.h"
16 #include "ir/irtypeclass.h" 17 #include "ir/irtypeclass.h"
17 18
18 ////////////////////////////////////////////////////////////////////////////// 19 //////////////////////////////////////////////////////////////////////////////
67 assert(tc && "invalid ClassInfo type"); 68 assert(tc && "invalid ClassInfo type");
68 69
69 // classinfos cannot be constants since they're used a locks for synchronized 70 // classinfos cannot be constants since they're used a locks for synchronized
70 classInfo = new llvm::GlobalVariable( 71 classInfo = new llvm::GlobalVariable(
71 tc->getPA().get(), false, _linkage, NULL, initname, gIR->module); 72 tc->getPA().get(), false, _linkage, NULL, initname, gIR->module);
73
74 #ifdef USE_METADATA
75 // Generate some metadata on this ClassInfo if it's for a class.
76
77 ClassDeclaration* classdecl = aggrdecl->isClassDeclaration();
78 if (classdecl && !aggrdecl->isInterfaceDeclaration()) {
79 // Gather information
80 const LLType* type = DtoType(aggrdecl->type);
81 const LLType* bodyType = llvm::cast<LLPointerType>(type)->getElementType();
82 bool hasDestructor = (classdecl->dtor != NULL);
83 bool hasCustomDelete = (classdecl->aggDelete != NULL);
84 // Construct the fields
85 LLConstant* mdVals[CD_NumFields];
86 mdVals[CD_BodyType] = llvm::UndefValue::get(bodyType);
87 mdVals[CD_Finalize] = LLConstantInt::get(LLType::Int1Ty, hasDestructor);
88 mdVals[CD_CustomDelete] = LLConstantInt::get(LLType::Int1Ty, hasCustomDelete);
89 // Construct the metadata
90 llvm::MDNode* metadata = llvm::MDNode::get(mdVals, CD_NumFields);
91 // Insert it into the module
92 new llvm::GlobalVariable(metadata->getType(), true,
93 METADATA_LINKAGE_TYPE, metadata, CD_PREFIX + initname, gIR->module);
94 }
95 #endif
72 96
73 return classInfo; 97 return classInfo;
74 } 98 }
75 99
76 ////////////////////////////////////////////////////////////////////////////// 100 //////////////////////////////////////////////////////////////////////////////