summary refs log tree commit diff
path: root/src/cimport.zig
blob: 19416afc530a4935269f60ef868a42be81c6ad65 (plain) (blame)
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
pub const FILE = @import("std").c.FILE;

pub const bits = u64;

pub const Target = extern struct {
    name: [16:0]u8,
    apple: u8,
    /// First general-purpose register.
    gpr0: c_int,
    ngpr: c_int,
    /// First floating-point register.
    fpr0: c_int,
    nfpr: c_int,
    /// Globally live registers, e.g. sp, fp.
    rglob: bits,
    nrglob: c_int,
    /// Caller-save.
    rsave: [*c]c_int,
    nrsave: [2]c_int,
    retregs: *const fn (Ref, [*c]c_int) callconv(.C) bits,
    argregs: *const fn (Ref, [*c]c_int) callconv(.C) bits,
    memargs: *const fn (c_int) callconv(.C) c_int,
    abi0: *const fn (?*Fn) callconv(.C) void,
    abi1: *const fn (?*Fn) callconv(.C) void,
    isel: *const fn (?*Fn) callconv(.C) void,
    emitfn: *const fn (?*Fn, *FILE) callconv(.C) void,
    emitfin: *const fn (*FILE) callconv(.C) void,
    asloc: [4]u8,
    assym: [4]u8,
};

pub const BSet = extern struct {
    nt: c_uint,
    t: [*c]bits,
};

pub const Ref = extern struct {
    // type: u3,
    // val: u29,
    ref: u32,
};

pub const Ins = extern struct {
    // op: u30,
    // cls: u2,
    op: u32,
    to: Ref,
    arg: [2]Ref,
};

pub const Phi = extern struct {
    to: Ref,
    arg: ?*Ref,
    blk: [*c]?*Blk,
    narg: c_uint,
    cls: c_int,
    link: ?*Phi,
};

pub const Blk = extern struct {
    phi: ?*Phi,
    ins: ?*Ins,
    nins: c_uint,
    jmp: extern struct {
        type: c_short,
        arg: Ref,
    },
    s1: ?*Blk,
    s2: ?*Blk,
    link: ?*Blk,
    id: c_uint,
    visit: c_uint,
    idom: ?*Blk,
    dom: ?*Blk,
    dlink: ?*Blk,
    fron: [*c]?*Blk,
    nfron: c_uint,
    pred: [*c]?*Blk,
    npred: c_uint,
    in: [1]BSet,
    out: [1]BSet,
    gen: [1]BSet,
    nlive: [2]c_int,
    loop: c_int,
    name: [80]u8,
};

pub const Use = extern struct {
    type: enum(c_uint) { UXXX, UPhi, UIns, UJmp },
    bid: c_uint,
    u: extern union {
        ins: ?*Ins,
        phi: ?*Phi,
    },
};

pub const Sym = extern struct {
    type: enum (c_uint) { SGlo, SThr },
    id: u32,
};

pub const Alias = extern struct {
    type: enum (c_uint) {
        ABot = 0,
        ALoc = 1, // stack local
        ACon = 2,
        AEsc = 3, // stack escaping
        ASym = 4,
        AUnk = 6,
    },
    base: c_int,
    offset: i64,
    u: extern union {
        sym: Sym,
        loc: extern struct {
            sz: c_int,
            m: bits,
        },
    },
    slot: [*c]Alias,
};

pub const Tmp = extern struct {
    name: [80]u8,
    def: ?*Ins,
    use: [*c]Use,
    ndef: c_uint,
    nuse: c_uint,
    bid: c_uint,
    cost: c_uint,
    slot: c_int,
    cls: c_short,
    hint: extern struct {
        r: c_int,
        w: c_int,
        m: bits,
    },
    phi: c_int,
    alias: Alias,
    width: c_uint,
    visit: c_int,
};

pub const Con = extern struct {
    type: enum (c_uint) { CUndef, CBits, CAddr },
    sym: Sym,
    bits: extern union {
        i: i64,
        d: f64,
        s: f32,
    },
    flt: u8,
};

pub const Addr = extern struct {
    offset: Con,
    base: Ref,
    index: Ref,
    scale: c_int,
};

pub const Mem = Addr;

pub const Lnk = extern struct {
    @"export": u8,
    thread: u8,
    @"align": u8,
    sec: [*c]u8,
    secf: [*c]u8,
};

pub const Fn = extern struct {
    start: ?*Blk,
    tmp: [*c]Tmp,
    con: [*c]Con,
    mem: ?*Mem,
    ntmp: c_int,
    ncon: c_int,
    nmem: c_int,
    nblk: c_uint,
    retty: c_int,
    retr: Ref,
    rpo: [*c]?*Blk,
    reg: bits,
    slot: c_int,
    vararg: u8,
    dynalloc: u8,
    name: [80]u8,
    lnk: Lnk,
};

pub const Dat = extern struct {
    type: enum(c_uint) { DStart, DEnd, DB, DH, DW, DL, DZ },
    name: [*:0]u8,
    lnk: [*c]Lnk,
    u: extern union {
        num: i64,
        fltd: f64,
        flts: f32,
        str: [*c]u8,
        ref: extern struct {
            name: [*:0]u8,
            off: i64,
        },
    },
    isref: u8,
    isstr: u8,
};

pub extern const T_arm64_apple: Target;
pub extern const T_amd64_apple: Target;
pub extern const T_arm64: Target;
pub extern const T_rv64: Target;
pub extern const T_amd64_sysv: Target;
pub export const targets = [_]*Target{
    &T_amd64_sysv,
    &T_amd64_apple,
    &T_arm64,
    &T_arm64_apple,
    &T_rv64,
};
pub export var T: Target = undefined;
pub export var debug = [_]bool{ false } ** ('Z' + 1);

// util.c
pub extern fn freeall() void;

// parse.c
pub extern fn parse(in: *FILE, path: [*:0]const u8, out: *FILE,
    *const fn (*Dat, *FILE) callconv(.C) void,
    *const fn (*Fn, *FILE) callconv(.C) void) void;
pub extern fn printfn(fun: *Fn, out: *FILE) void;

// cfg.c
pub extern fn fillpreds(fun: *Fn) void;
pub extern fn fillrpo(fun: *Fn) void;
pub extern fn fillloop(fun: *Fn) void;
pub extern fn simpljmp(fun: *Fn) void;

// mem.c
pub extern fn promote(fun: *Fn) void;
pub extern fn coalesce(fun: *Fn) void;

// alias.c
pub extern fn fillalias(fun: *Fn) void;

// load.c
pub extern fn loadopt(fun: *Fn) void;

// ssa.c
pub extern fn filluse(fun: *Fn) void;
pub extern fn ssa(fun: *Fn) void;
pub extern fn ssacheck(fun: *Fn) void;

// copy.c
pub extern fn copy(fun: *Fn) void;

// fold.c
pub extern fn fold(fun: *Fn) void;

// simpl.c
pub extern fn simpl(fun: *Fn) void;

// live.c
pub extern fn filllive(fun: *Fn) void;

// spill.c
pub extern fn fillcost(fun: *Fn) void;
pub extern fn spill(fun: *Fn) void;

// rega.c
pub extern fn rega(fun: *Fn) void;

// emit.c
pub extern fn emitdat(dat: *Dat, out: *FILE) void;
pub extern fn emitdbgfile(fun: [*:0]const u8, out: *FILE) void;