[clang] [Clang] Add GCC's __builtin_stack_address() to Clang. (PR #121332)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 30 12:21:33 PST 2025


================
@@ -4782,6 +4857,34 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
     Function *F = CGM.getIntrinsic(Intrinsic::frameaddress, AllocaInt8PtrTy);
     return RValue::get(Builder.CreateCall(F, Depth));
   }
+  case Builtin::BI__builtin_stack_address: {
+    switch (getTarget().getTriple().getArch()) {
+    case Triple::x86:
+      return RValue::get(EmitSpecialRegisterBuiltin(
+          *this, E, Int32Ty, VoidPtrTy, NormalRead, "esp"));
+    case Triple::x86_64:
+      return RValue::get(EmitSpecialRegisterBuiltin(
+          *this, E, Int64Ty, VoidPtrTy, NormalRead, "rsp"));
+    case Triple::arm:
+    case Triple::armeb:
+    case Triple::thumb:
+    case Triple::thumbeb:
+    case Triple::aarch64:
+    case Triple::aarch64_be:
+    case Triple::aarch64_32:
+    case Triple::riscv32:
+    case Triple::riscv64: {
+      llvm::IntegerType *SPRegIntTy =
+          getTarget().getTriple().getArchPointerBitWidth() == 64 ? Int64Ty
+                                                                 : Int32Ty;
+      return RValue::get(EmitSpecialRegisterBuiltin(
+          *this, E, SPRegIntTy, VoidPtrTy, NormalRead, "sp"));
+    }
+    default:
+      ErrorUnsupported(E, "__builtin_stack_address");
----------------
AaronBallman wrote:

We're missing test coverage for unsupported architectures; this seems like something that should be rejected before CodeGen too (in SemaChecking.cpp)

https://github.com/llvm/llvm-project/pull/121332


More information about the cfe-commits mailing list