libk  Diff

Differences From Artifact [8b1f2e0cb2]:

To Artifact [a9978dfd3a]:


     1         ---- kcore/type.h.m → <k/def.h>
            1  +--- kcore/type.h.m → <k/type.h>
     2      2   --- ~ lexi hale <lexi@hale.su>
     3      3   --- this file gathers information on the environment it's
     4      4   --- being compiled in, defining types that our code
     5      5   --- needs. it will be emitted as <k/type.h>.
     6      6   --- vim: ft=c
            7  +#ifndef KItype
            8  +#define KItype
            9  +
           10  +typedef unsigned char      kc_uint_min;
           11  +typedef   signed char      kc_sint_min;
           12  +typedef unsigned long long kc_uint_max;
           13  +typedef   signed long long kc_sint_max;
           14  +
           15  +[ifdef type_bit8]
           16  +	typedef unsigned [type_bit8] u8;
           17  +	typedef   signed [type_bit8] s8;
           18  +[else]
           19  +	typedef kc_uint_min u8;
           20  +	typedef	kc_sint_min s8;
           21  +[endif]
           22  +
           23  +[ifdef type_bit16]
           24  +	typedef unsigned [type_bit16] u16;
           25  +	typedef   signed [type_bit16] s16;
           26  +[else]
           27  +	typedef kc_uint_max u16;
           28  +	typedef	kc_sint_max s16;
           29  +[endif]
           30  +
           31  +[ifdef type_bit32]
           32  +	typedef unsigned [type_bit32] u32;
           33  +	typedef   signed [type_bit32] s32;
           34  +[else]
           35  +	typedef kc_uint_max u32;
           36  +	typedef	kc_sint_max s32;
           37  +[endif]
           38  +
           39  +[ifdef type_bit64]
           40  +	typedef unsigned [type_bit64] u64;
           41  +	typedef   signed [type_bit64] s64;
           42  +	[ifndef type_bit128]
           43  +#		if defined(__GNUC__) || defined(__clang__)
           44  +			typedef unsigned __int128_t u128;
           45  +			typedef   signed __int128_t s128;
           46  +#		else
           47  +			typedef kc_uint_max u128;
           48  +			typedef kc_sint_max s128;
           49  +#		endif
           50  +	[endif]
           51  +[else]
           52  +	typedef kc_uint_max u64;
           53  +	typedef	kc_sint_max s64;
           54  +
           55  +	typedef kc_uint_max u128;
           56  +	typedef	kc_sint_max s128;
           57  +[endif]
           58  +
           59  +[ifdef type_bit128]
           60  +	typedef unsigned [type_bit128] u128;
           61  +	typedef   signed [type_bit128] s128;
           62  +[endif]
           63  +
           64  +enum /* max-min values of each type */ {
           65  +	/* assuming two's complement. TODO: check math */
           66  +	/* TODO: figure out how to calc min */
           67  +	kc_byte_bits = [byte_bits],
           68  +	  u8_min = 0,   u8_max = ((u8)-1),
           69  +	 u16_min = 0,  u16_max = ((u16)-1),
           70  +	 u32_min = 0,  u32_max = ((u32)-1),
           71  +	 u64_min = 0,  u32_max = ((u64)-1),
           72  +	u128_min = 0, u128_max = ((u128)-1),
           73  +
           74  +	[define merge: $1$2]
           75  +	[define [sspec type]:
           76  +		[merge [type],_min] = 0 - ((1 << sizeof([type]) * kc_byte_bits) / 2),
           77  +		[merge [type],_max] =      (1 << sizeof([type]) * kc_byte_bits) / 2 - 1]
           78  +
           79  +	[sspec s8], [sspec s16], [sspec s32],
           80  +	[sspec s64], [sspec s128],
           81  +
           82  +	kc_min_uchar  = 0, kc_max_uchar  = [type_max_u_char],
           83  +	kc_min_ushort = 0, kc_max_ushort = [type_max_u_short],
           84  +	kc_min_uint   = 0, kc_max_uint   = [type_max_u_int],
           85  +	kc_min_ulong  = 0, kc_max_ulong  = [type_max_u_long],
           86  +	kc_min_ullong = 0, kc_max_ullong = [type_max_u_llong],
           87  +
           88  +	kc_min_schar  = [type_min_s_char],  kc_max_schar  = [type_max_s_char],
           89  +	kc_min_sshort = [type_min_s_short], kc_max_sshort = [type_max_s_short],
           90  +	kc_min_sint   = [type_min_s_int],   kc_max_sint   = [type_max_s_int],
           91  +	kc_min_slong  = [type_min_s_long],  kc_max_slong  = [type_max_s_long],
           92  +	kc_min_sllong = [type_min_s_llong], kc_max_sllong = [type_max_s_llong],
           93  +};
     7     94   
     8         -// arch bit length [atom_target_bits]
     9         ---- make some gigantic fucking assumptions
    10         -[if [atom_target_bits] >= 8]
    11         -    typedef unsigned char  u8;
    12         -    typedef signed char    s8;
    13         -    [if [atom_target_bits] >= 16]
    14         -        typedef unsigned short u16;
    15         -        typedef signed short   s16;
    16         -        [if [atom_target_bits] >= 32]
    17         -            typedef unsigned long  u32;
    18         -            typedef signed long    s32;
    19         -            [if [atom_target_bits] >= 64]
    20         -                typedef unsigned long long u64;
    21         -                typedef signed long long s64;
    22         -                [if [atom_target_arch] == x86]
    23         -                    #if defined(__GNUC__) || defined(__clang__)
    24         -                        typedef __uint128_t u128;
    25         -                        typedef __int128_t  s128;
    26         -                    #endif
    27         -                [endif]
    28         -            [endif]
    29         -        [endif]
    30         -    [endif]
           95  +[ifdef type_sz]
           96  +	typedef [type_sz] sz;
           97  +[else]
           98  +#	ifdef __cplusplus
           99  + 	   /* C++ gives us a clean, standardized way to do this */
          100  + 	   typedef decltype (sizeof(char)) sz;
          101  +#	else
          102  +#		if defined(__GNUC__) || defined(__clang__)
          103  +			typedef __typeof__ (sizeof(char)) sz;
          104  +#		else
          105  +			/* we're stumped - set sz to the safest possible value under
          106  +			 * the circumstances, and warn the user. */
          107  +#			warning no authoritative sz (size_t) type definition \
          108  +			        available; defaulting to largest unsigned integer type
          109  +			typedef kc_uint_max sz;
          110  +#		endif
          111  +#	endif
    31    112   [endif]
    32    113   
    33         -typedef u[atom_target_bits]  sz;
    34         -typedef s[atom_target_bits] ssz;
    35         -
    36         ---- make sure something unlikely doesn't happen
    37         -[define [maxtype name, n]:
    38         -	[if [n] > [atom_target_bits]]
    39         -		typedef u[atom_target_bits] [name]
    40         -	[else]
    41         -		typedef u[n] [name]
    42         -	[endif]]
          114  +[ifdef type_offset]
          115  +	typedef [type_offset] offset;
          116  +[else]
          117  +#	ifdef __cplusplus
          118  + 	   /* C++ gives us a clean, standardized way to do this */
          119  +		typedef decltype (((void*)-1) - 1) offset;
          120  +#	else
          121  +#		if defined(__GNUC__) || defined(__clang__)
          122  +			typedef __typeof__ (((void*)10) - ((void*)5)) offset;
          123  +#		else
          124  +			/* no dice - set offset to the safest possible value under
          125  +			 * the circumstances, and warn the user. */
          126  +#			warning no authoritative offset (ptrdiff_t) type definition \
          127  +			        available; defaulting to largest unsigned integer type
          128  +			typedef kc_sint_max offset;
          129  +#		endif
          130  +#	endif
          131  +[endif]
    43    132   
    44    133   // exit status integer types - pls use kbad in <k/magic.h> instead
    45    134   [if [target_posix] == yes]
    46    135   	/* by convention, posix return values are 8-bit,
    47    136   	 * but note that many modern UNIXes do technically
    48    137   	 * support higher-bit values. for this reason,
    49    138   	 * longstat is defined differently under posix. */
    50    139   	typedef u8 stat;
    51         -	[maxtype longstat, 32];
          140  +	typedef u32 stat_long;
    52    141   [else]
    53    142   	[if ([atom_target_os] == win) ||
    54    143   		([atom_target_os] == vms)]
    55         -		[maxtype stat, 32]
          144  +		typedef u32 stat;
    56    145   	[else]
    57    146   		typedef u8 stat;
    58    147   		/* we don't know a specific exit status type
    59    148   		 * for your arch so we're going with a sane
    60    149   		 * default. if this is wrong, help us fix it! */
    61    150   	[endif]
    62         -	typedef stat longstat;
          151  +	typedef stat stat_long;
    63    152   [endif]
          153  +
          154  +#endif