assembly 如何查看Ziglang程序的汇编输出?

yhqotfr8  于 2023-05-29  发布在  其他
关注(0)|答案(1)|浏览(205)

Zig版本

0.11.0-dev.3299 +34865d693

问题

我希望能够看到我的程序的汇编输出,理想情况下,文件名和行号Map到汇编指令,这样我就可以快速推理它们来自哪里。

项目中的文件

build.zig

const std = @import("std");

pub fn build(b: *std.Build.Builder) !void {
    // Determine compilation target
    const target = b.standardTargetOptions(.{});

    // Setup optimisation settings
    const optimize = b.standardOptimizeOption(.{});

    // Create executable for our example
    const exe = b.addExecutable(.{
        .name = "app",
        .root_source_file = .{ .path = "main.zig" },
        .target = target,
        .optimize = optimize,
    });

    // Install the executable into the prefix when invoking "zig build"
    b.installArtifact(exe);
}

main.zig

const std = @import("std");

pub fn main() void {
    std.debug.print("Hello, {s}!\n", .{"World"});
}
snvhrwxg

snvhrwxg1#

通过将以下内容添加到构建设置来更新build.zig以发出程序集。这将使Zig输出一个“app.s”文件。

// Get assembly output of build
exe.emit_asm = .emit;

据我所知,目前似乎没有办法获得与汇编指令相关的文件名和行号。

完整示例:

const std = @import("std");

pub fn build(b: *std.Build.Builder) !void {
    // Determine compilation target
    const target = b.standardTargetOptions(.{});

    // Setup optimisation settings
    const optimize = b.standardOptimizeOption(.{});

    // Create executable for our example
    const exe = b.addExecutable(.{
        .name = "app",
        .root_source_file = .{ .path = "main.zig" },
        .target = target,
        .optimize = optimize,
    });

    // Install the executable into the prefix when invoking "zig build"
    b.installArtifact(exe);

    // Get assembly output of build
    exe.emit_asm = .emit;
}

汇编输出片段:

main.main:
.Lfunc_begin6:
    .cv_func_id 8
    .cv_file    5 "C:\\ZigProjects\\hello-world\\main.zig"
    .cv_loc 8 5 3 0
.seh_proc main.main
    push    rbp
    .seh_pushreg rbp
    sub rsp, 32
    .seh_stackalloc 32
    lea rbp, [rsp + 32]
    .seh_setframe rbp, 32
    .seh_endprologue
.Ltmp22:
    .cv_loc 8 5 4 20
    call    debug.print__anon_2816
    nop
    add rsp, 32
    pop rbp
    ret

相关问题