aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/aarch64/Mir.zig
blob: d71c410b1dc10694a32865b9dc7a50e9eefc8522 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
prologue: []const Instruction,
body: []const Instruction,
epilogue: []const Instruction,
literals: []const u32,
nav_relocs: []const Reloc.Nav,
uav_relocs: []const Reloc.Uav,
lazy_relocs: []const Reloc.Lazy,
global_relocs: []const Reloc.Global,
literal_relocs: []const Reloc.Literal,

pub const Reloc = struct {
    label: u32,
    addend: u64 align(@alignOf(u32)) = 0,

    pub const Nav = struct {
        nav: InternPool.Nav.Index,
        reloc: Reloc,
    };

    pub const Uav = struct {
        uav: InternPool.Key.Ptr.BaseAddr.Uav,
        reloc: Reloc,
    };

    pub const Lazy = struct {
        symbol: link.File.LazySymbol,
        reloc: Reloc,
    };

    pub const Global = struct {
        name: [*:0]const u8,
        reloc: Reloc,
    };

    pub const Literal = struct {
        label: u32,
    };
};

pub fn deinit(mir: *Mir, gpa: std.mem.Allocator) void {
    assert(mir.body.ptr + mir.body.len == mir.prologue.ptr);
    assert(mir.prologue.ptr + mir.prologue.len == mir.epilogue.ptr);
    gpa.free(mir.body.ptr[0 .. mir.body.len + mir.prologue.len + mir.epilogue.len]);
    gpa.free(mir.literals);
    gpa.free(mir.nav_relocs);
    gpa.free(mir.uav_relocs);
    gpa.free(mir.lazy_relocs);
    gpa.free(mir.global_relocs);
    gpa.free(mir.literal_relocs);
    mir.* = undefined;
}

pub fn emit(
    mir: Mir,
    lf: *link.File,
    pt: Zcu.PerThread,
    src_loc: Zcu.LazySrcLoc,
    func_index: InternPool.Index,
    atom_index: u32,
    w: *std.Io.Writer,
    debug_output: link.File.DebugInfoOutput,
) !void {
    _ = debug_output;
    const zcu = pt.zcu;
    const ip = &zcu.intern_pool;
    const func = zcu.funcInfo(func_index);
    const nav = ip.getNav(func.owner_nav);
    const mod = zcu.navFileScope(func.owner_nav).mod.?;
    const target = &mod.resolved_target.result;
    mir_log.debug("{f}:", .{nav.fqn.fmt(ip)});

    const func_align = switch (nav.status.fully_resolved.alignment) {
        .none => switch (mod.optimize_mode) {
            .Debug, .ReleaseSafe, .ReleaseFast => target_util.defaultFunctionAlignment(target),
            .ReleaseSmall => target_util.minFunctionAlignment(target),
        },
        else => |a| a.maxStrict(target_util.minFunctionAlignment(target)),
    };
    const code_len = mir.prologue.len + mir.body.len + mir.epilogue.len;
    const literals_align_gap = -%code_len & (@divExact(
        @as(u5, @intCast(func_align.minStrict(.@"16").toByteUnits().?)),
        Instruction.size,
    ) - 1);
    try w.rebase(w.end, Instruction.size * (code_len + literals_align_gap + mir.literals.len));
    emitInstructionsForward(w, mir.prologue) catch unreachable;
    emitInstructionsBackward(w, mir.body) catch unreachable;
    const body_end: u32 = @intCast(w.end);
    emitInstructionsBackward(w, mir.epilogue) catch unreachable;
    w.splatByteAll(0, Instruction.size * literals_align_gap) catch unreachable;
    w.writeAll(@ptrCast(mir.literals)) catch unreachable;
    mir_log.debug("", .{});

    for (mir.nav_relocs) |nav_reloc| try emitReloc(
        lf,
        zcu,
        atom_index,
        switch (try @import("../../codegen.zig").genNavRef(
            lf,
            pt,
            src_loc,
            nav_reloc.nav,
            &mod.resolved_target.result,
        )) {
            .sym_index => |sym_index| sym_index,
            .fail => |em| return zcu.codegenFailMsg(func.owner_nav, em),
        },
        mir.body[nav_reloc.reloc.label],
        body_end - Instruction.size * (1 + nav_reloc.reloc.label),
        nav_reloc.reloc.addend,
        if (ip.getNav(nav_reloc.nav).getExtern(ip)) |_| .got_load else .direct,
    );
    for (mir.uav_relocs) |uav_reloc| try emitReloc(
        lf,
        zcu,
        atom_index,
        switch (try lf.lowerUav(
            pt,
            uav_reloc.uav.val,
            ZigType.fromInterned(uav_reloc.uav.orig_ty).ptrAlignment(zcu),
            src_loc,
        )) {
            .sym_index => |sym_index| sym_index,
            .fail => |em| return zcu.codegenFailMsg(func.owner_nav, em),
        },
        mir.body[uav_reloc.reloc.label],
        body_end - Instruction.size * (1 + uav_reloc.reloc.label),
        uav_reloc.reloc.addend,
        .direct,
    );
    for (mir.lazy_relocs) |lazy_reloc| try emitReloc(
        lf,
        zcu,
        atom_index,
        if (lf.cast(.elf)) |ef|
            ef.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(ef, pt, lazy_reloc.symbol) catch |err|
                return zcu.codegenFail(func.owner_nav, "{s} creating lazy symbol", .{@errorName(err)})
        else if (lf.cast(.macho)) |mf|
            mf.getZigObject().?.getOrCreateMetadataForLazySymbol(mf, pt, lazy_reloc.symbol) catch |err|
                return zcu.codegenFail(func.owner_nav, "{s} creating lazy symbol", .{@errorName(err)})
        else
            return zcu.codegenFail(func.owner_nav, "external symbols unimplemented for {t}", .{lf.tag}),
        mir.body[lazy_reloc.reloc.label],
        body_end - Instruction.size * (1 + lazy_reloc.reloc.label),
        lazy_reloc.reloc.addend,
        .direct,
    );
    for (mir.global_relocs) |global_reloc| try emitReloc(
        lf,
        zcu,
        atom_index,
        if (lf.cast(.elf)) |ef|
            try ef.getGlobalSymbol(std.mem.span(global_reloc.name), null)
        else if (lf.cast(.macho)) |mf|
            try mf.getGlobalSymbol(std.mem.span(global_reloc.name), null)
        else
            return zcu.codegenFail(func.owner_nav, "external symbols unimplemented for {t}", .{lf.tag}),
        mir.body[global_reloc.reloc.label],
        body_end - Instruction.size * (1 + global_reloc.reloc.label),
        global_reloc.reloc.addend,
        .direct,
    );
    const literal_reloc_offset: i19 = @intCast(mir.epilogue.len + literals_align_gap);
    for (mir.literal_relocs) |literal_reloc| {
        var instruction = mir.body[literal_reloc.label];
        instruction.load_store.register_literal.group.imm19 += literal_reloc_offset;
        instruction.write(
            w.buffered()[body_end - Instruction.size * (1 + literal_reloc.label) ..][0..Instruction.size],
        );
    }
}

fn emitInstructionsForward(w: *std.Io.Writer, instructions: []const Instruction) !void {
    for (instructions) |instruction| try emitInstruction(w, instruction);
}
fn emitInstructionsBackward(w: *std.Io.Writer, instructions: []const Instruction) !void {
    var instruction_index = instructions.len;
    while (instruction_index > 0) {
        instruction_index -= 1;
        try emitInstruction(w, instructions[instruction_index]);
    }
}
fn emitInstruction(w: *std.Io.Writer, instruction: Instruction) !void {
    mir_log.debug("    {f}", .{instruction});
    instruction.write(try w.writableArray(Instruction.size));
}

fn emitReloc(
    lf: *link.File,
    zcu: *Zcu,
    atom_index: u32,
    sym_index: u32,
    instruction: Instruction,
    offset: u32,
    addend: u64,
    kind: enum { direct, got_load },
) !void {
    const gpa = zcu.gpa;
    switch (instruction.decode()) {
        else => unreachable,
        .data_processing_immediate => |decoded| if (lf.cast(.elf)) |ef| {
            const zo = ef.zigObjectPtr().?;
            const atom = zo.symbol(atom_index).atom(ef).?;
            const r_type: std.elf.R_AARCH64 = switch (decoded.decode()) {
                else => unreachable,
                .pc_relative_addressing => |pc_relative_addressing| switch (pc_relative_addressing.group.op) {
                    .adr => switch (kind) {
                        .direct => .ADR_PREL_LO21,
                        .got_load => unreachable,
                    },
                    .adrp => switch (kind) {
                        .direct => .ADR_PREL_PG_HI21,
                        .got_load => .ADR_GOT_PAGE,
                    },
                },
                .add_subtract_immediate => |add_subtract_immediate| switch (add_subtract_immediate.group.op) {
                    .add => switch (kind) {
                        .direct => .ADD_ABS_LO12_NC,
                        .got_load => unreachable,
                    },
                    .sub => unreachable,
                },
            };
            try atom.addReloc(gpa, .{
                .r_offset = offset,
                .r_info = @as(u64, sym_index) << 32 | @intFromEnum(r_type),
                .r_addend = @bitCast(addend),
            }, zo);
        } else if (lf.cast(.macho)) |mf| {
            const zo = mf.getZigObject().?;
            const atom = zo.symbols.items[atom_index].getAtom(mf).?;
            switch (decoded.decode()) {
                else => unreachable,
                .pc_relative_addressing => |pc_relative_addressing| switch (pc_relative_addressing.group.op) {
                    .adr => unreachable,
                    .adrp => try atom.addReloc(mf, .{
                        .tag = .@"extern",
                        .offset = offset,
                        .target = sym_index,
                        .addend = @bitCast(addend),
                        .type = switch (kind) {
                            .direct => .page,
                            .got_load => .got_load_page,
                        },
                        .meta = .{
                            .pcrel = true,
                            .has_subtractor = false,
                            .length = 2,
                            .symbolnum = @intCast(sym_index),
                        },
                    }),
                },
                .add_subtract_immediate => |add_subtract_immediate| switch (add_subtract_immediate.group.op) {
                    .add => try atom.addReloc(mf, .{
                        .tag = .@"extern",
                        .offset = offset,
                        .target = sym_index,
                        .addend = @bitCast(addend),
                        .type = switch (kind) {
                            .direct => .pageoff,
                            .got_load => .got_load_pageoff,
                        },
                        .meta = .{
                            .pcrel = false,
                            .has_subtractor = false,
                            .length = 2,
                            .symbolnum = @intCast(sym_index),
                        },
                    }),
                    .sub => unreachable,
                },
            }
        },
        .branch_exception_generating_system => |decoded| if (lf.cast(.elf)) |ef| {
            const zo = ef.zigObjectPtr().?;
            const atom = zo.symbol(atom_index).atom(ef).?;
            const r_type: std.elf.R_AARCH64 = switch (decoded.decode().unconditional_branch_immediate.group.op) {
                .b => .JUMP26,
                .bl => .CALL26,
            };
            try atom.addReloc(gpa, .{
                .r_offset = offset,
                .r_info = @as(u64, sym_index) << 32 | @intFromEnum(r_type),
                .r_addend = @bitCast(addend),
            }, zo);
        } else if (lf.cast(.macho)) |mf| {
            const zo = mf.getZigObject().?;
            const atom = zo.symbols.items[atom_index].getAtom(mf).?;
            try atom.addReloc(mf, .{
                .tag = .@"extern",
                .offset = offset,
                .target = sym_index,
                .addend = @bitCast(addend),
                .type = .branch,
                .meta = .{
                    .pcrel = true,
                    .has_subtractor = false,
                    .length = 2,
                    .symbolnum = @intCast(sym_index),
                },
            });
        },
        .load_store => |decoded| if (lf.cast(.elf)) |ef| {
            const zo = ef.zigObjectPtr().?;
            const atom = zo.symbol(atom_index).atom(ef).?;
            const r_type: std.elf.R_AARCH64 = switch (decoded.decode().register_unsigned_immediate.decode()) {
                .integer => |integer| switch (integer.decode()) {
                    .unallocated, .prfm => unreachable,
                    .strb, .ldrb, .ldrsb => switch (kind) {
                        .direct => .LDST8_ABS_LO12_NC,
                        .got_load => unreachable,
                    },
                    .strh, .ldrh, .ldrsh => switch (kind) {
                        .direct => .LDST16_ABS_LO12_NC,
                        .got_load => unreachable,
                    },
                    .ldrsw => switch (kind) {
                        .direct => .LDST32_ABS_LO12_NC,
                        .got_load => unreachable,
                    },
                    inline .str, .ldr => |encoded, mnemonic| switch (encoded.sf) {
                        .word => .LDST32_ABS_LO12_NC,
                        .doubleword => switch (kind) {
                            .direct => .LDST64_ABS_LO12_NC,
                            .got_load => switch (mnemonic) {
                                else => comptime unreachable,
                                .str => unreachable,
                                .ldr => .LD64_GOT_LO12_NC,
                            },
                        },
                    },
                },
                .vector => |vector| switch (kind) {
                    .direct => switch (vector.group.opc1.decode(vector.group.size)) {
                        .byte => .LDST8_ABS_LO12_NC,
                        .half => .LDST16_ABS_LO12_NC,
                        .single => .LDST32_ABS_LO12_NC,
                        .double => .LDST64_ABS_LO12_NC,
                        .quad => .LDST128_ABS_LO12_NC,
                    },
                    .got_load => unreachable,
                },
            };
            try atom.addReloc(gpa, .{
                .r_offset = offset,
                .r_info = @as(u64, sym_index) << 32 | @intFromEnum(r_type),
                .r_addend = @bitCast(addend),
            }, zo);
        } else if (lf.cast(.macho)) |mf| {
            const zo = mf.getZigObject().?;
            const atom = zo.symbols.items[atom_index].getAtom(mf).?;
            try atom.addReloc(mf, .{
                .tag = .@"extern",
                .offset = offset,
                .target = sym_index,
                .addend = @bitCast(addend),
                .type = switch (kind) {
                    .direct => .pageoff,
                    .got_load => .got_load_pageoff,
                },
                .meta = .{
                    .pcrel = false,
                    .has_subtractor = false,
                    .length = 2,
                    .symbolnum = @intCast(sym_index),
                },
            });
        },
    }
}

const Air = @import("../../Air.zig");
const assert = std.debug.assert;
const mir_log = std.log.scoped(.mir);
const Instruction = @import("encoding.zig").Instruction;
const InternPool = @import("../../InternPool.zig");
const link = @import("../../link.zig");
const Mir = @This();
const std = @import("std");
const target_util = @import("../../target.zig");
const Zcu = @import("../../Zcu.zig");
const ZigType = @import("../../Type.zig");