Commit 4e3f6de027

Andrew Kelley <superjoe30@gmail.com>
2016-02-12 20:07:11
c integer size takes into account architecture and OS
1 parent 7828456
src/all_types.hpp
@@ -1050,19 +1050,6 @@ struct BuiltinFnEntry {
     LLVMValueRef fn_val;
 };
 
-enum CIntType {
-    CIntTypeShort,
-    CIntTypeUShort,
-    CIntTypeInt,
-    CIntTypeUInt,
-    CIntTypeLong,
-    CIntTypeULong,
-    CIntTypeLongLong,
-    CIntTypeULongLong,
-
-    CIntTypeCount,
-};
-
 struct CodeGen {
     LLVMModuleRef module;
     ZigList<ErrorMsg*> errors;
src/codegen.cpp
@@ -3195,27 +3195,6 @@ static const CIntTypeInfo c_int_type_infos[] = {
     {CIntTypeULongLong, "c_ulonglong", false},
 };
 
-static int get_c_type_size_in_bits(CodeGen *g, CIntType id) {
-    // TODO other architectures besides x86_64
-    // other operating systems besides linux
-    switch (id) {
-        case CIntTypeShort:
-        case CIntTypeUShort:
-            return 16;
-        case CIntTypeInt:
-        case CIntTypeUInt:
-            return 32;
-        case CIntTypeLong:
-        case CIntTypeULong:
-        case CIntTypeLongLong:
-        case CIntTypeULongLong:
-            return 64;
-        case CIntTypeCount:
-            zig_unreachable();
-    }
-    zig_unreachable();
-}
-
 static void define_builtin_types(CodeGen *g) {
     {
         // if this type is anywhere in the AST, we should never hit codegen.
@@ -3288,7 +3267,7 @@ static void define_builtin_types(CodeGen *g) {
 
     for (int i = 0; i < array_length(c_int_type_infos); i += 1) {
         const CIntTypeInfo *info = &c_int_type_infos[i];
-        uint64_t size_in_bits = get_c_type_size_in_bits(g, info->id);
+        uint64_t size_in_bits = get_c_type_size_in_bits(&g->zig_target, info->id);
         bool is_signed = info->is_signed;
 
         TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
src/target.cpp
@@ -292,3 +292,108 @@ void resolve_target_object_format(ZigTarget *target) {
     }
     target->oformat = ZigLLVM_ELF;
 }
+
+
+static int get_arch_pointer_bit_width(ZigLLVM_ArchType arch) {
+    switch (arch) {
+        case ZigLLVM_UnknownArch:
+            return 0;
+
+        case ZigLLVM_msp430:
+            return 16;
+
+        case ZigLLVM_arm:
+        case ZigLLVM_armeb:
+        case ZigLLVM_hexagon:
+        case ZigLLVM_le32:
+        case ZigLLVM_mips:
+        case ZigLLVM_mipsel:
+        case ZigLLVM_nvptx:
+        case ZigLLVM_ppc:
+        case ZigLLVM_r600:
+        case ZigLLVM_sparc:
+        case ZigLLVM_sparcel:
+        case ZigLLVM_tce:
+        case ZigLLVM_thumb:
+        case ZigLLVM_thumbeb:
+        case ZigLLVM_x86:
+        case ZigLLVM_xcore:
+        case ZigLLVM_amdil:
+        case ZigLLVM_hsail:
+        case ZigLLVM_spir:
+        case ZigLLVM_kalimba:
+        case ZigLLVM_shave:
+        case ZigLLVM_wasm32:
+            return 32;
+
+        case ZigLLVM_aarch64:
+        case ZigLLVM_aarch64_be:
+        case ZigLLVM_amdgcn:
+        case ZigLLVM_bpfel:
+        case ZigLLVM_bpfeb:
+        case ZigLLVM_le64:
+        case ZigLLVM_mips64:
+        case ZigLLVM_mips64el:
+        case ZigLLVM_nvptx64:
+        case ZigLLVM_ppc64:
+        case ZigLLVM_ppc64le:
+        case ZigLLVM_sparcv9:
+        case ZigLLVM_systemz:
+        case ZigLLVM_x86_64:
+        case ZigLLVM_amdil64:
+        case ZigLLVM_hsail64:
+        case ZigLLVM_spir64:
+        case ZigLLVM_wasm64:
+            return 64;
+    }
+    zig_unreachable();
+}
+
+int get_c_type_size_in_bits(const ZigTarget *target, CIntType id) {
+    switch (target->os) {
+        case ZigLLVM_UnknownOS:
+            zig_unreachable();
+        case ZigLLVM_Linux:
+            switch (id) {
+                case CIntTypeShort:
+                case CIntTypeUShort:
+                    return 16;
+                case CIntTypeInt:
+                case CIntTypeUInt:
+                    return 32;
+                case CIntTypeLong:
+                case CIntTypeULong:
+                    return get_arch_pointer_bit_width(target->arch.arch);
+                case CIntTypeLongLong:
+                case CIntTypeULongLong:
+                    return 64;
+                case CIntTypeCount:
+                    zig_unreachable();
+            }
+        case ZigLLVM_CloudABI:
+        case ZigLLVM_Darwin:
+        case ZigLLVM_DragonFly:
+        case ZigLLVM_FreeBSD:
+        case ZigLLVM_IOS:
+        case ZigLLVM_KFreeBSD:
+        case ZigLLVM_Lv2:
+        case ZigLLVM_MacOSX:
+        case ZigLLVM_NetBSD:
+        case ZigLLVM_OpenBSD:
+        case ZigLLVM_Solaris:
+        case ZigLLVM_Win32:
+        case ZigLLVM_Haiku:
+        case ZigLLVM_Minix:
+        case ZigLLVM_RTEMS:
+        case ZigLLVM_NaCl:
+        case ZigLLVM_CNK:
+        case ZigLLVM_Bitrig:
+        case ZigLLVM_AIX:
+        case ZigLLVM_CUDA:
+        case ZigLLVM_NVCL:
+        case ZigLLVM_AMDHSA:
+        case ZigLLVM_PS4:
+            zig_panic("TODO c type size in bits for this target");
+    }
+    zig_unreachable();
+}
src/target.hpp
@@ -25,6 +25,19 @@ struct ZigTarget {
     ZigLLVM_ObjectFormatType oformat;
 };
 
+enum CIntType {
+    CIntTypeShort,
+    CIntTypeUShort,
+    CIntTypeInt,
+    CIntTypeUInt,
+    CIntTypeLong,
+    CIntTypeULong,
+    CIntTypeLongLong,
+    CIntTypeULongLong,
+
+    CIntTypeCount,
+};
+
 int target_arch_count(void);
 const ArchType *get_target_arch(int index);
 void get_arch_name(char *out_str, const ArchType *arch);
@@ -52,5 +65,6 @@ void get_target_triple(Buf *triple, const ZigTarget *target);
 
 void resolve_target_object_format(ZigTarget *target);
 
+int get_c_type_size_in_bits(const ZigTarget *target, CIntType id);
 
 #endif