mjacob created this revision. mjacob added a reviewer: rsmith. mjacob added subscribers: cfe-commits, alex.
The constant expression evaluator for pointers returns a base and an offset if successful. In this case, CodeGen generates a constant pointer cast, casting the base (with offset applied if necessary) to the destination type. However, that is different from casting the base to an integer and then to the destination type if the destination points to another address space as the base. Supporting this would require a lot of work because it can't be expressed as base + offset. Changing the whole constant expression evaluator is not worthline for supporting this special case. http://reviews.llvm.org/D15319 Files: lib/AST/ExprConstant.cpp test/Sema/const-eval.c Index: test/Sema/const-eval.c =================================================================== --- test/Sema/const-eval.c +++ test/Sema/const-eval.c @@ -137,3 +137,6 @@ void PR24622(); struct PR24622 {} pr24622; EVAL_EXPR(52, &pr24622 == (void *)&PR24622); // expected-error {{must have a constant size}} + +int __attribute__((address_space(1))) addrspace1; +int *addrspace0_ptr = (int *)(long)&addrspace1; // expected-error {{not a compile-time constant}} Index: lib/AST/ExprConstant.cpp =================================================================== --- lib/AST/ExprConstant.cpp +++ lib/AST/ExprConstant.cpp @@ -4995,7 +4995,11 @@ Result.Designator.setInvalid(); return true; } else { - // Cast is of an lvalue, no need to change value. + // Cast is of an lvalue, no need to change value, unless they have + // different address spaces. + if (Info.Ctx.getTargetAddressSpace(getType(Value.getLValueBase())) != + Info.Ctx.getTargetAddressSpace(E->getType()->getPointeeType())) + return false; Result.setFrom(Info.Ctx, Value); return true; }
Index: test/Sema/const-eval.c =================================================================== --- test/Sema/const-eval.c +++ test/Sema/const-eval.c @@ -137,3 +137,6 @@ void PR24622(); struct PR24622 {} pr24622; EVAL_EXPR(52, &pr24622 == (void *)&PR24622); // expected-error {{must have a constant size}} + +int __attribute__((address_space(1))) addrspace1; +int *addrspace0_ptr = (int *)(long)&addrspace1; // expected-error {{not a compile-time constant}} Index: lib/AST/ExprConstant.cpp =================================================================== --- lib/AST/ExprConstant.cpp +++ lib/AST/ExprConstant.cpp @@ -4995,7 +4995,11 @@ Result.Designator.setInvalid(); return true; } else { - // Cast is of an lvalue, no need to change value. + // Cast is of an lvalue, no need to change value, unless they have + // different address spaces. + if (Info.Ctx.getTargetAddressSpace(getType(Value.getLValueBase())) != + Info.Ctx.getTargetAddressSpace(E->getType()->getPointeeType())) + return false; Result.setFrom(Info.Ctx, Value); return true; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits