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

via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 31 01:10:02 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");
----------------
aalhwc wrote:

Noted! Thanks!

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


More information about the cfe-commits mailing list