authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-12 12:07:11-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-12 12:07:11-07:00
log4e3f6de027b5b4cb88d0ac83b05db083e9f0ce9e
tree0f0d851b4a279c0e2ffbccaa708d10e7171e4c0e
parent7828456b30829e68a959f74e86d02e737d590cc2

c integer size takes into account architecture and OS


4 files changed, 120 insertions(+), 35 deletions(-)

src/all_types.hpp-13
......@@ -1050,19 +1050,6 @@ struct BuiltinFnEntry {
10501050 LLVMValueRef fn_val;
10511051};
10521052
1053enum CIntType {
1054 CIntTypeShort,
1055 CIntTypeUShort,
1056 CIntTypeInt,
1057 CIntTypeUInt,
1058 CIntTypeLong,
1059 CIntTypeULong,
1060 CIntTypeLongLong,
1061 CIntTypeULongLong,
1062
1063 CIntTypeCount,
1064};
1065
10661053struct CodeGen {
10671054 LLVMModuleRef module;
10681055 ZigList<ErrorMsg*> errors;
src/codegen.cpp+1-22
......@@ -3195,27 +3195,6 @@ static const CIntTypeInfo c_int_type_infos[] = {
31953195 {CIntTypeULongLong, "c_ulonglong", false},
31963196};
31973197
3198static int get_c_type_size_in_bits(CodeGen *g, CIntType id) {
3199 // TODO other architectures besides x86_64
3200 // other operating systems besides linux
3201 switch (id) {
3202 case CIntTypeShort:
3203 case CIntTypeUShort:
3204 return 16;
3205 case CIntTypeInt:
3206 case CIntTypeUInt:
3207 return 32;
3208 case CIntTypeLong:
3209 case CIntTypeULong:
3210 case CIntTypeLongLong:
3211 case CIntTypeULongLong:
3212 return 64;
3213 case CIntTypeCount:
3214 zig_unreachable();
3215 }
3216 zig_unreachable();
3217}
3218
32193198static void define_builtin_types(CodeGen *g) {
32203199 {
32213200 // if this type is anywhere in the AST, we should never hit codegen.
......@@ -3288,7 +3267,7 @@ static void define_builtin_types(CodeGen *g) {
32883267
32893268 for (int i = 0; i < array_length(c_int_type_infos); i += 1) {
32903269 const CIntTypeInfo *info = &c_int_type_infos[i];
3291 uint64_t size_in_bits = get_c_type_size_in_bits(g, info->id);
3270 uint64_t size_in_bits = get_c_type_size_in_bits(&g->zig_target, info->id);
32923271 bool is_signed = info->is_signed;
32933272
32943273 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
src/target.cpp+105
......@@ -292,3 +292,108 @@ void resolve_target_object_format(ZigTarget *target) {
292292 }
293293 target->oformat = ZigLLVM_ELF;
294294}
295
296
297static int get_arch_pointer_bit_width(ZigLLVM_ArchType arch) {
298 switch (arch) {
299 case ZigLLVM_UnknownArch:
300 return 0;
301
302 case ZigLLVM_msp430:
303 return 16;
304
305 case ZigLLVM_arm:
306 case ZigLLVM_armeb:
307 case ZigLLVM_hexagon:
308 case ZigLLVM_le32:
309 case ZigLLVM_mips:
310 case ZigLLVM_mipsel:
311 case ZigLLVM_nvptx:
312 case ZigLLVM_ppc:
313 case ZigLLVM_r600:
314 case ZigLLVM_sparc:
315 case ZigLLVM_sparcel:
316 case ZigLLVM_tce:
317 case ZigLLVM_thumb:
318 case ZigLLVM_thumbeb:
319 case ZigLLVM_x86:
320 case ZigLLVM_xcore:
321 case ZigLLVM_amdil:
322 case ZigLLVM_hsail:
323 case ZigLLVM_spir:
324 case ZigLLVM_kalimba:
325 case ZigLLVM_shave:
326 case ZigLLVM_wasm32:
327 return 32;
328
329 case ZigLLVM_aarch64:
330 case ZigLLVM_aarch64_be:
331 case ZigLLVM_amdgcn:
332 case ZigLLVM_bpfel:
333 case ZigLLVM_bpfeb:
334 case ZigLLVM_le64:
335 case ZigLLVM_mips64:
336 case ZigLLVM_mips64el:
337 case ZigLLVM_nvptx64:
338 case ZigLLVM_ppc64:
339 case ZigLLVM_ppc64le:
340 case ZigLLVM_sparcv9:
341 case ZigLLVM_systemz:
342 case ZigLLVM_x86_64:
343 case ZigLLVM_amdil64:
344 case ZigLLVM_hsail64:
345 case ZigLLVM_spir64:
346 case ZigLLVM_wasm64:
347 return 64;
348 }
349 zig_unreachable();
350}
351
352int get_c_type_size_in_bits(const ZigTarget *target, CIntType id) {
353 switch (target->os) {
354 case ZigLLVM_UnknownOS:
355 zig_unreachable();
356 case ZigLLVM_Linux:
357 switch (id) {
358 case CIntTypeShort:
359 case CIntTypeUShort:
360 return 16;
361 case CIntTypeInt:
362 case CIntTypeUInt:
363 return 32;
364 case CIntTypeLong:
365 case CIntTypeULong:
366 return get_arch_pointer_bit_width(target->arch.arch);
367 case CIntTypeLongLong:
368 case CIntTypeULongLong:
369 return 64;
370 case CIntTypeCount:
371 zig_unreachable();
372 }
373 case ZigLLVM_CloudABI:
374 case ZigLLVM_Darwin:
375 case ZigLLVM_DragonFly:
376 case ZigLLVM_FreeBSD:
377 case ZigLLVM_IOS:
378 case ZigLLVM_KFreeBSD:
379 case ZigLLVM_Lv2:
380 case ZigLLVM_MacOSX:
381 case ZigLLVM_NetBSD:
382 case ZigLLVM_OpenBSD:
383 case ZigLLVM_Solaris:
384 case ZigLLVM_Win32:
385 case ZigLLVM_Haiku:
386 case ZigLLVM_Minix:
387 case ZigLLVM_RTEMS:
388 case ZigLLVM_NaCl:
389 case ZigLLVM_CNK:
390 case ZigLLVM_Bitrig:
391 case ZigLLVM_AIX:
392 case ZigLLVM_CUDA:
393 case ZigLLVM_NVCL:
394 case ZigLLVM_AMDHSA:
395 case ZigLLVM_PS4:
396 zig_panic("TODO c type size in bits for this target");
397 }
398 zig_unreachable();
399}
src/target.hpp+14
......@@ -25,6 +25,19 @@ struct ZigTarget {
2525 ZigLLVM_ObjectFormatType oformat;
2626};
2727
28enum CIntType {
29 CIntTypeShort,
30 CIntTypeUShort,
31 CIntTypeInt,
32 CIntTypeUInt,
33 CIntTypeLong,
34 CIntTypeULong,
35 CIntTypeLongLong,
36 CIntTypeULongLong,
37
38 CIntTypeCount,
39};
40
2841int target_arch_count(void);
2942const ArchType *get_target_arch(int index);
3043void get_arch_name(char *out_str, const ArchType *arch);
......@@ -52,5 +65,6 @@ void get_target_triple(Buf *triple, const ZigTarget *target);
5265
5366void resolve_target_object_format(ZigTarget *target);
5467
68int get_c_type_size_in_bits(const ZigTarget *target, CIntType id);
5569
5670#endif