Commit f7835000b6

Andrew Kelley <andrew@ziglang.org>
2019-03-02 21:34:58
@returnAddress and @frameAddress return usize now
1 parent ea5cedc
Changed files (5)
doc/langref.html.in
@@ -6116,7 +6116,7 @@ test "main" {
       {#header_close#}
 
       {#header_open|@frameAddress#}
-      <pre>{#syntax#}@frameAddress(){#endsyntax#}</pre>
+      <pre>{#syntax#}@frameAddress() usize{#endsyntax#}</pre>
       <p>
       This function returns the base pointer of the current stack frame.
       </p>
@@ -6482,17 +6482,18 @@ test "call foo" {
       {#header_close#}
 
       {#header_open|@returnAddress#}
-      <pre>{#syntax#}@returnAddress(){#endsyntax#}</pre>
+      <pre>{#syntax#}@returnAddress() usize{#endsyntax#}</pre>
       <p>
-      This function returns a pointer to the return address of the current stack
-      frame.
+      This function returns the address of the next machine code instruction that will be executed
+      when the current function returns.
       </p>
       <p>
       The implications of this are target specific and not consistent across
       all platforms.
       </p>
       <p>
-      This function is only valid within function scope.
+      This function is only valid within function scope. If the function gets inlined into
+      a calling function, the returned address will apply to the calling function.
       </p>
       {#header_close#}
       {#header_open|@setAlignStack#}
src/codegen.cpp
@@ -4661,7 +4661,8 @@ static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutable *executabl
         IrInstructionReturnAddress *instruction)
 {
     LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);
-    return LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
+    LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
+    return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->type_ref, "");
 }
 
 static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
@@ -4682,7 +4683,8 @@ static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable
         IrInstructionFrameAddress *instruction)
 {
     LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);
-    return LLVMBuildCall(g->builder, get_frame_address_fn_val(g), &zero, 1, "");
+    LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_frame_address_fn_val(g), &zero, 1, "");
+    return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->type_ref, "");
 }
 
 static LLVMValueRef get_handle_fn_val(CodeGen *g) {
src/ir.cpp
@@ -20142,18 +20142,14 @@ static IrInstruction *ir_analyze_instruction_breakpoint(IrAnalyze *ira, IrInstru
 static IrInstruction *ir_analyze_instruction_return_address(IrAnalyze *ira, IrInstructionReturnAddress *instruction) {
     IrInstruction *result = ir_build_return_address(&ira->new_irb,
         instruction->base.scope, instruction->base.source_node);
-    ZigType *u8 = ira->codegen->builtin_types.entry_u8;
-    ZigType *u8_ptr_const = get_pointer_to_type(ira->codegen, u8, true);
-    result->value.type = u8_ptr_const;
+    result->value.type = ira->codegen->builtin_types.entry_usize;
     return result;
 }
 
 static IrInstruction *ir_analyze_instruction_frame_address(IrAnalyze *ira, IrInstructionFrameAddress *instruction) {
     IrInstruction *result = ir_build_frame_address(&ira->new_irb,
             instruction->base.scope, instruction->base.source_node);
-    ZigType *u8 = ira->codegen->builtin_types.entry_u8;
-    ZigType *u8_ptr_const = get_pointer_to_type(ira->codegen, u8, true);
-    result->value.type = u8_ptr_const;
+    result->value.type = ira->codegen->builtin_types.entry_usize;
     return result;
 }
 
std/debug/index.zig
@@ -171,7 +171,7 @@ pub fn assert(ok: bool) void {
 
 pub fn panic(comptime format: []const u8, args: ...) noreturn {
     @setCold(true);
-    const first_trace_addr = @ptrToInt(@returnAddress());
+    const first_trace_addr = @returnAddress();
     panicExtra(null, first_trace_addr, format, args);
 }
 
@@ -232,7 +232,7 @@ pub const StackIterator = struct {
     pub fn init(first_addr: ?usize) StackIterator {
         return StackIterator{
             .first_addr = first_addr,
-            .fp = @ptrToInt(@frameAddress()),
+            .fp = @frameAddress(),
         };
     }
 
std/special/panic.zig
@@ -18,7 +18,7 @@ pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn
             std.os.abort();
         },
         else => {
-            const first_trace_addr = @ptrToInt(@returnAddress());
+            const first_trace_addr = @returnAddress();
             std.debug.panicExtra(error_return_trace, first_trace_addr, "{}", msg);
         },
     }