C backend: implement ret_addr

This commit is contained in:
Andrew Kelley 2021-12-21 14:32:02 -07:00
parent 4cb2f11693
commit 8b6ea9ffe7
2 changed files with 17 additions and 1 deletions

View File

@ -2214,7 +2214,9 @@ fn airBreakpoint(f: *Function) !CValue {
}
fn airRetAddr(f: *Function) !CValue {
return f.fail("TODO implement codegen for airRetAddr", .{});
const local = try f.allocLocal(Type.usize, .Const);
try f.object.writer().writeAll(" = zig_return_address();\n");
return local;
}
fn airFence(f: *Function, inst: Air.Inst.Index) !CValue {

View File

@ -60,6 +60,20 @@
#define zig_breakpoint() raise(SIGTRAP)
#endif
#if defined(_MSC_VER)
#define zig_return_address() _ReturnAddress()
#elif defined(__GNUC__)
#define zig_return_address() __builtin_extract_return_addr(__builtin_return_address(0))
#else
#define zig_return_address() 0
#endif
#if defined(__GNUC__)
#define zig_frame_address() __builtin_frame_address(0)
#else
#define zig_frame_address() 0
#endif
#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
#include <stdatomic.h>
#define zig_cmpxchg_strong(obj, expected, desired, succ, fail) atomic_compare_exchange_strong_explicit(obj, &(expected), desired, succ, fail)