History

Add __builtin_return_address.

Implementation is mostly shared with __builtin_frame_address. It seems to work on arm64, i386 and x86_64. It may need to be adapted for other targets.

Edmund Grimley Evans committed 11 years ago

Commit 238e7601 parent 87649932 files · +14 −1Browse files

Unified · Split

2 files
  1. tccgen.c +13 −1
  2. tcctok.h +1 −0
tccgen.c+13−1
@@ -3874,13 +3874,18 @@ ST_FUNC void unary(void) · 3873 unmodified lines
38743874 }
38753875 break;
38763876 case TOK_builtin_frame_address:
3877+ case TOK_builtin_return_address:
38773878 {
3879+ int tok1 = tok;
38783880 int level;
38793881 CType type;
38803882 next();
38813883 skip('(');
38823884 if (tok != TOK_CINT || tokc.i < 0) {
3883− tcc_error("__builtin_frame_address only takes positive integers");
3885+ tcc_error("%s only takes positive integers",
3886+ tok1 == TOK_builtin_return_address ?
3887+ "__builtin_return_address" :
3888+ "__builtin_frame_address");
38843889 }
38853890 level = tokc.i;
38863891 next();
@@ -3892,6 +3897,13 @@ ST_FUNC void unary(void) · 5 unmodified lines
38923897 mk_pointer(&vtop->type);
38933898 indir(); /* -> parent frame */
38943899 }
3900+ if (tok1 == TOK_builtin_return_address) {
3901+ // assume return address is just above frame pointer on stack
3902+ vpushi(PTR_SIZE);
3903+ gen_op('+');
3904+ mk_pointer(&vtop->type);
3905+ indir();
3906+ }
38953907 }
38963908 break;
38973909 #ifdef TCC_TARGET_X86_64
tcctok.h+1−0
@@ -130,6 +130,7 @@ · 129 unmodified lines
130130 DEF(TOK_builtin_types_compatible_p, "__builtin_types_compatible_p")
131131 DEF(TOK_builtin_constant_p, "__builtin_constant_p")
132132 DEF(TOK_builtin_frame_address, "__builtin_frame_address")
133+ DEF(TOK_builtin_return_address, "__builtin_return_address")
133134 #ifdef TCC_TARGET_X86_64
134135 #ifdef TCC_TARGET_PE
135136 DEF(TOK_builtin_va_start, "__builtin_va_start")