aboutsummaryrefslogtreecommitdiff
path: root/src/type.cff
blob: 641694c9daf6bc2d821f85a8f0840dd265589e19 (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
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
import "cffc.hff";
import "util.hff";
import "common.hff";
import "set.hff";

struct TypeTraits {
   fn hash(ty *const Type) u32 {
      let h = FNV1A_INI;
      h = fnv1a_i(h, as(int)ty.u.#tag);
      h = fnv1a_i(h, as(int)ty.konst);

      switch ty.u {
      case Void;

      case Bool;
         h = fnv1a_i(h, ty.size);

      case Flo;
         h = fnv1a_i(h, ty.size);

      case Int i;
         h = fnv1a_i(h, i.sgn ? 1 : 0);
         h = fnv1a_i(h, ty.size);

      case Ptr child;
         h = fnv1a_i(h, child.id);

      case Arr arr;
         h = fnv1a_i(h, arr.child.id);
         h = fnv1a_i(h, arr.length);

      case Slice child;
         h = fnv1a_i(h, child.id);

      case Fn f;
         h = fnv1a_i(h, f.ret.id);
         foreach(p, _, f.params) {
            h = fnv1a_i(h, p.id);
         }

      case Agg agg;
         h = fnv1a_i(h, agg.id);

      case Enum enu;
         h = fnv1a_i(h, enu.id);

      case BitF bitf;
         h = fnv1a_i(h, bitf.id);
      }

      return h;
   }

   fn eq(a *const Type, b *const Type) bool {
      if a.u.#tag != b.u.#tag or a.konst != b.konst {
         return #f;
      }
      switch a.u {
      case Void;
         return #t;
      case Bool;
         return a.size == b.size;
      case Flo;
         return a.size == b.size;

      case Int i;
         return a.size == b.size and i.sgn == b.u.Int.sgn;

      case Ptr child;
         return child == b.u.Ptr;

      case Arr arr;
         let brr = b.u.Arr;
         return arr.length == brr.length and arr.child == brr.child;

      case Slice child;
         return child == b.u.Slice;

      case Fn f0;
         let f1 = b.u.Fn;
         if f0.variadic != f1.variadic {
            return #f;
         }
         if f0.ret != f1.ret {
            return #f;
         }
         if f0.params.#len != f1.params.#len {
            return #f;
         }
         foreach(p, i, f0.params) {
            if p != f1.params[i] {
               return #f;
            }
         }
         return #t;

      case Agg agg;
         let bgg = b.u.Agg;
         return agg.id == bgg.id;

      case Enum enu;
         return enu.id == b.u.Enum.id;

      case BitF bitf;
         return bitf.id == b.u.BitF.id;
      }
      return #f;
   }

   fn dup(ty *const Type) *const Type {
      static arena Arena = {};
      let p *Type = Arena:allocf(&arena, sizeof Type, alignof Type);
      static id int = 0;
      *p = *ty;
      p.id = id++;
      switch p.u {
      case Fn *f;
        let params = f.params;
        f.params = (as(**const Type)Arena:allocf(&arena, params.#len * sizeof *Type, alignof *Type))[0::params.#len];
        memcpy(f.params.#ptr, params.#ptr, params.#len * sizeof *Type);
      }
      return p;
   }
}

static types_set Set<*const Type, TypeTraits> = {};
extern fn interntype(ty0 *Type) *const Type {
   if ty0.align == 0 {
      ty0.align = ty0.size == 0 ? 1 : ty0.size;
   }

   return *types_set->intern(ty0);
}

extern fn completetype(ty *const Type) bool {
   if ty.konst {
      return completetype(unconstify(ty));
   }
   switch ty.u {
   case Void; return #f;
   case Fn; return #f;
   case Arr a; return a.length >= 0 and completetype(a.child);
   case Agg agg; return !agg.fwd;
   }
   return #t;
}

extern static ty_void     *const Type = {},
              ty_bool     *const Type = {},
              ty_intbool  *const Type = {},
              ty_i8       *const Type = {},
              ty_u8       *const Type = {},
              ty_i16      *const Type = {},
              ty_u16      *const Type = {},
              ty_i32      *const Type = {},
              ty_u32      *const Type = {},
              ty_int      *const Type = {},
              ty_uint     *const Type = {},
              ty_i64      *const Type = {},
              ty_u64      *const Type = {},
              ty_isize    *const Type = {},
              ty_usize    *const Type = {},
              ty_iptrint  *const Type = {},
              ty_uptrint  *const Type = {},
              ty_f32      *const Type = {},
              ty_f64      *const Type = {},
              ty_voidptr  *const Type = {},
              ty_c_char   *const Type = {},
              ty_c_long   *const Type = {},
              ty_c_ulong  *const Type = {},
              ty_c_llong  *const Type = {},
              ty_c_ullong *const Type = {},
              ty_c_valist *const Type = {};

extern fn putprimtypes(env *Env) void {
   static types []struct { name *const u8, gty **const Type, ty Type } = {
      { "void",   &ty_void, { .size: 0, .align: 1, .u: :Void }},
      { "bool",   &ty_bool, { .size: 1, .align: 1, .u: :Bool }},
      { "f32",    &ty_f32,  { .size: 4, .u: :Flo }},
      { "f64",    &ty_f64,  { .size: 8, .u: :Flo }},
      { "i8",     &ty_i8,   { .size: 1, .u: :Int { .sgn: #t }}},
      { "u8",     &ty_u8,   { .size: 1, .u: :Int { .sgn: #f }}},
      { "i16",    &ty_i16,  { .size: 2, .u: :Int { .sgn: #t }}},
      { "u16",    &ty_u16,  { .size: 2, .u: :Int { .sgn: #f }}},
      { "i32",    &ty_i32,  { .size: 4, .u: :Int { .sgn: #t }}},
      { "u32",    &ty_u32,  { .size: 4, .u: :Int { .sgn: #f }}},
      { "int",    &ty_int,  { .u: :Int { .sgn: #t }}},
      { "uint",   &ty_uint, { .u: :Int { .sgn: #f }}},
      { "intbool",&ty_intbool,{ .u: :Bool }},
      { "isize",  &ty_isize,{ .u: :Int { .sgn: #t }}},
      { "usize",  &ty_usize,{ .u: :Int { .sgn: #f }}},
      { "i64",    &ty_i64,  { .size: 8, .u: :Int { .sgn: #t }}},
      { "u64",    &ty_u64,  { .size: 8, .u: :Int { .sgn: #f }}},
      { "iptrint",&ty_iptrint, { .u: :Int { .sgn: #t }}},
      { "uptrint",&ty_uptrint, { .u: :Int { .sgn: #f }}},
      { "c_char", &ty_c_char, { .size: 1 }},
      { "c_long", &ty_c_long, { .u: :Int { .sgn : #t }}},
      { "c_ulong",&ty_c_ulong, { .u: :Int { .sgn : #f }}},
      { "c_llong",&ty_c_llong, { .u: :Int { .sgn : #t }}},
      { "c_ullong",&ty_c_ullong,{.u: :Int { .sgn : #f }}},
      { "va_list",&ty_c_valist,{ .u: :VaList }},
   };

   foreach_ptr(type, _, types[0::]) {
      let ty = &type.ty;
      let gty = type.gty;
      switch {
      case gty == &ty_f64;
         ty.align = g_targ.f64align;

      case gty == &ty_int or gty == &ty_uint or gty == &ty_intbool;
         ty.size = g_targ.intsize;

      case gty == &ty_isize or gty == &ty_usize;
         ty.size = g_targ.sizesize;

      case gty == &ty_i64 or gty == &ty_u64;
         ty.align = g_targ.i64align;

      case gty == &ty_iptrint or gty == &ty_uptrint;
         ty.size = g_targ.ptrsize;

      case gty == &ty_c_char;
         ty.u.Int.sgn = g_targ.charsigned;

      case gty == &ty_c_long or gty == &ty_c_ulong;
         ty.size = g_targ.longsize;
         ty.align = g_targ.longalign;

      case gty == &ty_c_llong or gty == &ty_c_ullong;
         ty.size = g_targ.longsize;
         ty.align = g_targ.longalign;

      case gty == &ty_c_valist;
         ty.size = g_targ.valistsize;
         ty.align = g_targ.valistalign;
      }
      envput(env, { type.name, .u: :Ty(*gty = interntype(ty)) }, #null);
   }
   ty_voidptr = interntype(&Type{ .size: g_targ.ptrsize, .u: :Ptr(ty_void) });
}

extern fn mkslicetype(child *const Type) *const Type {
   let align = MAX(g_targ.ptrsize, g_targ.sizesize);
   return interntype(&Type{
      ALIGNUP(g_targ.ptrsize + g_targ.sizesize, align), align,
      .u: :Slice(child)
   });
}

fn numtype2rank(ty *const Type) int {
   ty = unconstify(ty);
   switch {
   case ty->is(:Enum);
      assert(ty.u.Enum.lax, "lax");
      return numtype2rank(ty.u.Enum.intty);
   case ty->is(:Int) and (ty == ty_int or ty.size < ty_int.size);
      return 0;
   case ty == ty_uint; return 1;
   case ty == ty_i32;  return 2;
   case ty == ty_u32;  return 3;
   case ty == ty_i64;  return 4;
   case ty == ty_u64;  return 5;
   case ty == ty_f32;  return 6;
   case ty == ty_f64;  return 7;
   }
   return -1;
}

fn rank2numtype(r int) *const Type {
   static const types []**const Type = {
      &ty_int, &ty_uint, &ty_i32, &ty_u32,
      &ty_i64, &ty_u64, &ty_f32, &ty_f64,
   };
   assert(r >= 0 and r < types.#len, "rank");
   return *types[r];
}

fn arraydecay(ty *const Type) *const Type {
   switch ty.u {
   case Arr arr; return mkptrtype(arr.child);
   case else; return ty;
   }
}

fn constifychild(ty *const Type) *const Type {
   switch ty.u {
   case Ptr child;
      return mkptrtype(constify(child));
   case Slice child;
      return mkslicetype(constify(child));
   case else;
      assert(#f, "constifychild: not ptr or slice");
   }
}

extern fn typeof2(a *const Type, b *const Type) *const Type {
   if a == b and !a->is(:Int) {
      return a;
   }
   if isnumtype(a) and isnumtype(b) {
      let ra = numtype2rank(a),
          rb = numtype2rank(b);
      return rank2numtype(MAX(ra, rb));
   }
   if unconstify(a) == b {
      return a;
   }
   if a == unconstify(b) {
      return b;
   }
   if a->is(:Bool) and b->is(:Bool) {
      return a.size > b.size ? a : b;
   }
   if a->is(:Arr) and b->is(:Arr) {
      a = arraydecay(a);
   }
   if a->is(:Ptr) and b->is(:Arr) {
      b = arraydecay(b);
   }
   if a->is(:Arr) and b->is(:Ptr) {
      a = arraydecay(a);
   }
   if a.u.#tag == b.u.#tag and (a->is(:Ptr) or a->is(:Slice)) {
      let akonst = a.u.Ptr.konst,
          bkonst = b.u.Ptr.konst,
          uac = unconstify(a.u.Ptr),
          ubc = unconstify(b.u.Ptr);
      if uac == ubc {
         return akonst ? a : b;
      }
      if uac == ty_void {
         if !bkonst and akonst { return constifychild(b); }
         return b;
      }
      if ubc == ty_void {
         if !akonst and bkonst { return constifychild(a); }
         return a;
      }
   }
   return #null;
}

extern fn visittypes(proc *fn(*const Type, *void) void, arg *void) void {
   set_each(ty, types_set) {
      proc(ty, arg);
   }
}