add support for a 1<<29 bit in the block flags field to mark blocks using alternate struct return ABI

llvm-svn: 97775
This commit is contained in:
Blaine Garst 2010-03-05 01:29:59 +00:00
parent 43d6ff7701
commit f27ab71d9f
3 changed files with 128 additions and 3 deletions

View File

@ -154,7 +154,7 @@ llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
{
// C = BuildBlockStructInitlist();
unsigned int flags = BLOCK_HAS_OBJC_TYPE;
unsigned int flags = BLOCK_HAS_SIGNATURE;
// We run this first so that we set BlockHasCopyDispose from the entire
// block literal.
@ -184,6 +184,18 @@ llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
Elts[0] = C;
// __flags
{
QualType BPT = BE->getType();
const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
QualType ResultType = ftype->getResultType();
CallArgList Args;
CodeGenTypes &Types = CGM.getTypes();
const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
CC_Default, false);
if (CGM.ReturnTypeUsesSret(FnInfo))
flags |= BLOCK_USE_STRET;
}
const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
CGM.getTypes().ConvertType(CGM.getContext().IntTy));
C = llvm::ConstantInt::get(IntTy, flags);
@ -200,6 +212,7 @@ llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
// Optimize to being a global block.
Elts[0] = CGM.getNSConcreteGlobalBlock();
Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
C = llvm::ConstantStruct::get(VMContext, Elts, false);
@ -604,7 +617,7 @@ BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
// Flags
LiteralFields[1] =
llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_OBJC_TYPE);
llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE);
// Reserved
LiteralFields[2] = llvm::Constant::getNullValue(IntTy);

View File

@ -54,10 +54,12 @@ public:
BLOCK_HAS_COPY_DISPOSE = (1 << 25),
BLOCK_HAS_CXX_OBJ = (1 << 26),
BLOCK_IS_GLOBAL = (1 << 28),
BLOCK_HAS_OBJC_TYPE = (1 << 30)
BLOCK_USE_STRET = (1 << 29),
BLOCK_HAS_SIGNATURE = (1 << 30)
};
};
class BlockModule : public BlockBase {
ASTContext &Context;
llvm::Module &TheModule;

View File

@ -0,0 +1,110 @@
// RUN: %clang_cc1 -fblocks -triple x86_64-apple-darwin9 %s -emit-llvm -o - | FileCheck %s -check-prefix=X64
// RUN: %clang_cc1 -fblocks -triple i686-apple-darwin9 %s -emit-llvm -o - | FileCheck %s -check-prefix=X32
// X64: internal constant %2 { i8** @_NSConcreteGlobalBlock, i32 1879048192
// X64: store i32 1610612736, i32* %want
// X32: @_NSConcreteGlobalBlock, i32 1879048192, i32 0,
// X32: store i32 1610612736, i32* %want
// rdar://7677537
#include <stdlib.h>
typedef struct bigbig {
int array[512];
char more[32];
} BigStruct_t;
BigStruct_t (^global)(void) = ^{ return *(BigStruct_t *)malloc(sizeof(struct bigbig)); };
const char * getBlockSignature(void *);
BigStruct_t foo(int param) {
BigStruct_t x;
BigStruct_t (^f)(int) = ^(int param) {
BigStruct_t *result = malloc(sizeof(BigStruct_t));
result->array[23] = param;
return *result;
};
getBlockSignature(f);
return x;
}
#if 1
#include <stdio.h>
#include <stdbool.h>
enum {
BLOCK_HAS_COPY_DISPOSE = (1 << 25),
BLOCK_HAS_CXX_OBJ = (1 << 26),
BLOCK_IS_GLOBAL = (1 << 28),
BLOCK_USE_STRET = (1 << 29),
BLOCK_HAS_OBJC_TYPE = (1 << 30)
};
struct block_descriptor_big {
unsigned long int reserved;
unsigned long int size;
void (*copy)(void *dst, void *src); // conditional on BLOCK_HAS_COPY_DISPOSE
void (*dispose)(void *); // conditional on BLOCK_HAS_COPY_DISPOSE
const char *signature; // conditional on BLOCK_HAS_OBJC
const char *layout; // conditional on BLOCK_HAS_OBJC
};
struct block_descriptor_small {
unsigned long int reserved;
unsigned long int size;
const char *signature; // conditional on BLOCK_HAS_OBJC
const char *layout; // conditional on BLOCK_HAS_OBJC
};
struct block_layout_abi { // can't change
void *isa;
int flags;
int reserved;
void (*invoke)(void *, ...);
struct block_descriptor_big *descriptor;
};
const char *getBlockSignature(void *block) {
struct block_layout_abi *layout = (struct block_layout_abi *)block;
if ((layout->flags & BLOCK_HAS_OBJC_TYPE) != BLOCK_HAS_OBJC_TYPE) return NULL;
if (layout->flags & BLOCK_HAS_COPY_DISPOSE)
return layout->descriptor->signature;
else
return ((struct block_descriptor_small *)layout->descriptor)->signature;
}
bool usesStruct(void *block) {
struct block_layout_abi *layout = (struct block_layout_abi *)block;
int want = BLOCK_HAS_OBJC_TYPE | BLOCK_USE_STRET;
return (layout->flags & want) == want;
}
int main(int argc, char *argv[]) {
printf("desired global flags: %d\n", BLOCK_USE_STRET | BLOCK_IS_GLOBAL | BLOCK_HAS_OBJC_TYPE);
printf("desired stack flags: %d\n", BLOCK_USE_STRET | BLOCK_HAS_OBJC_TYPE);
printf("should be non-zero: %d\n", usesStruct(global));
BigStruct_t x;
BigStruct_t (^local)(int) = ^(int param) {
BigStruct_t *result = (BigStruct_t *)malloc(sizeof(BigStruct_t));
result->array[23] = argc;
return *result;
};
printf("should be non-zero: %d\n", usesStruct(global));
printf("should be non-zero: %d\n", usesStruct(local));
printf("should be zero: %d\n", usesStruct(^void(int x){ }));
return 0;
}
/*
desired global flags: 1879048192
desired stack flags: 1610612736
should be non-zero: 0
should be non-zero: 0
should be non-zero: 1
should be zero: 0
*/
#endif