zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit f7835000b62fb5b501f1d84c90f56e2aa11bc55a (tree)
parent ea5cedced1aa6bb5e1c1a07fcd16c5bed08c971f
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Sat,  2 Mar 2019 15:34:58 -0500

@returnAddress and @frameAddress return usize now

Diffstat:
Mdoc/langref.html.in | 11++++++-----
Msrc/codegen.cpp | 6++++--
Msrc/ir.cpp | 8++------
Mstd/debug/index.zig | 4++--
Mstd/special/panic.zig | 2+-
5 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/doc/langref.html.in b/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#} diff --git a/src/codegen.cpp b/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) { diff --git a/src/ir.cpp b/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; } diff --git a/std/debug/index.zig b/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(), }; } diff --git a/std/special/panic.zig b/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); }, }