Changeset ce55b43 in mainline


Ignore:
Timestamp:
2017-10-24T04:56:35Z (7 years ago)
Author:
jzr <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c9f3e7e9
Parents:
8ddaaac
Message:

Gut autotool.py and use it to generate the macros we want.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/autotool.py

    r8ddaaac rce55b43  
    1 #!/usr/bin/env python
     1#!/usr/bin/env python2
    22#
    33# Copyright (c) 2010 Martin Decky
     
    4343MAKEFILE = 'Makefile.common'
    4444HEADER = 'common.h.new'
    45 GUARD = 'AUTOTOOL_COMMON_H_'
     45GUARD = '_AUTOTOOL_COMMON_H_'
    4646
    4747PROBE_SOURCE = 'probe.c'
    4848PROBE_OUTPUT = 'probe.s'
    49 
    50 PROBE_INT128_SOURCE = 'probe_int128.c'
    51 PROBE_INT128_OUTPUT = 'probe_int128.s'
    5249
    5350PACKAGE_BINUTILS = "usually part of binutils"
     
    5956COMPILER_WARNING = "The compilation of HelenOS might fail."
    6057
    61 PROBE_HEAD = """#define AUTOTOOL_DECLARE(category, subcategory, tag, name, strc, conc, value) \\
     58PROBE_HEAD = """#define AUTOTOOL_DECLARE(category, tag, name, signedness, base, size, compatible) \\
    6259        asm volatile ( \\
    63                 "AUTOTOOL_DECLARE\\t" category "\\t" subcategory "\\t" tag "\\t" name "\\t" strc "\\t" conc "\\t%[val]\\n" \\
     60                "AUTOTOOL_DECLARE\\t" category "\\t" tag "\\t" name "\\t" signedness "\\t" base "\\t%[size_val]\\t%[cmp_val]\\n" \\
    6461                : \\
    65                 : [val] "n" (value) \\
     62                : [size_val] "n" (size), [cmp_val] "n" (compatible) \\
    6663        )
    6764
     
    7067
    7168#define DECLARE_BUILTIN_TYPE(tag, type) \\
    72         AUTOTOOL_DECLARE("builtin_size", "", tag, STRING(type), "", "", sizeof(type)); \\
    73         AUTOTOOL_DECLARE("builtin_sign", "unsigned long long int", tag, STRING(type), "unsigned", "", __builtin_types_compatible_p(type, unsigned long long int)); \\
    74         AUTOTOOL_DECLARE("builtin_sign", "unsigned long int", tag, STRING(type), "unsigned", "", __builtin_types_compatible_p(type, unsigned long int)); \\
    75         AUTOTOOL_DECLARE("builtin_sign", "unsigned int", tag, STRING(type), "unsigned", "", __builtin_types_compatible_p(type, unsigned int)); \\
    76         AUTOTOOL_DECLARE("builtin_sign", "unsigned short int", tag, STRING(type), "unsigned", "", __builtin_types_compatible_p(type, unsigned short int)); \\
    77         AUTOTOOL_DECLARE("builtin_sign", "unsigned char", tag, STRING(type), "unsigned", "", __builtin_types_compatible_p(type, unsigned char)); \\
    78         AUTOTOOL_DECLARE("builtin_sign", "signed long long int", tag, STRING(type), "signed", "", __builtin_types_compatible_p(type, signed long long int)); \\
    79         AUTOTOOL_DECLARE("builtin_sign", "signed long int", tag, STRING(type), "signed", "", __builtin_types_compatible_p(type, signed long int)); \\
    80         AUTOTOOL_DECLARE("builtin_sign", "signed int", tag, STRING(type), "signed", "", __builtin_types_compatible_p(type, signed int)); \\
    81         AUTOTOOL_DECLARE("builtin_sign", "signed short int", tag, STRING(type), "signed", "", __builtin_types_compatible_p(type, signed short int)); \\
    82         AUTOTOOL_DECLARE("builtin_sign", "signed char", tag, STRING(type), "signed", "", __builtin_types_compatible_p(type, signed char));
    83 
    84 #define DECLARE_INTSIZE(tag, type, strc, conc) \\
    85         AUTOTOOL_DECLARE("intsize", "unsigned", tag, #type, strc, conc, sizeof(unsigned type)); \\
    86         AUTOTOOL_DECLARE("intsize", "signed", tag, #type, strc, conc, sizeof(signed type));
    87 
    88 #define DECLARE_FLOATSIZE(tag, type) \\
    89         AUTOTOOL_DECLARE("floatsize", "", tag, #type, "", "", sizeof(type));
    90 
    91 extern int main(int, char *[]);
    92 
    93 int main(int argc, char *argv[])
    94 {
    95 #ifdef __SIZE_TYPE__
    96         DECLARE_BUILTIN_TYPE("size", __SIZE_TYPE__);
    97 #endif
    98 #ifdef __WCHAR_TYPE__
    99         DECLARE_BUILTIN_TYPE("wchar", __WCHAR_TYPE__);
    100 #endif
    101 #ifdef __WINT_TYPE__
    102         DECLARE_BUILTIN_TYPE("wint", __WINT_TYPE__);
    103 #endif
    104 """
    105 
    106 PROBE_TAIL = """}
    107 """
    108 
    109 PROBE_INT128_HEAD = """#define AUTOTOOL_DECLARE(category, subcategory, tag, name, strc, conc, value) \\
    110         asm volatile ( \\
    111                 "AUTOTOOL_DECLARE\\t" category "\\t" subcategory "\\t" tag "\\t" name "\\t" strc "\\t" conc "\\t%[val]\\n" \\
    112                 : \\
    113                 : [val] "n" (value) \\
    114         )
    115 
    116 #define DECLARE_INTSIZE(tag, type) \\
    117         AUTOTOOL_DECLARE("intsize", "unsigned", tag, #type, "", "", sizeof(unsigned type)); \\
    118         AUTOTOOL_DECLARE("intsize", "signed", tag, #type, "", "", sizeof(signed type));
     69        AUTOTOOL_DECLARE("unsigned long long int", tag, STRING(type), "unsigned", "long long", sizeof(type), __builtin_types_compatible_p(type, unsigned long long int)); \\
     70        AUTOTOOL_DECLARE("unsigned long int", tag, STRING(type), "unsigned", "long", sizeof(type), __builtin_types_compatible_p(type, unsigned long int)); \\
     71        AUTOTOOL_DECLARE("unsigned int", tag, STRING(type), "unsigned", "int", sizeof(type), __builtin_types_compatible_p(type, unsigned int)); \\
     72        AUTOTOOL_DECLARE("unsigned short int", tag, STRING(type), "unsigned", "short", sizeof(type), __builtin_types_compatible_p(type, unsigned short int)); \\
     73        AUTOTOOL_DECLARE("unsigned char", tag, STRING(type), "unsigned", "char", sizeof(type), __builtin_types_compatible_p(type, unsigned char)); \\
     74        AUTOTOOL_DECLARE("signed long long int", tag, STRING(type), "signed", "long long", sizeof(type), __builtin_types_compatible_p(type, signed long long int)); \\
     75        AUTOTOOL_DECLARE("signed long int", tag, STRING(type), "signed", "long", sizeof(type), __builtin_types_compatible_p(type, signed long int)); \\
     76        AUTOTOOL_DECLARE("signed int", tag, STRING(type), "signed", "int", sizeof(type), __builtin_types_compatible_p(type, signed int)); \\
     77        AUTOTOOL_DECLARE("signed short int", tag, STRING(type), "signed", "short", sizeof(type), __builtin_types_compatible_p(type, signed short int)); \\
     78        AUTOTOOL_DECLARE("signed char", tag, STRING(type), "signed", "char", sizeof(type), __builtin_types_compatible_p(type, signed char)); \\
     79        AUTOTOOL_DECLARE("pointer", tag, STRING(type), "N/A", "pointer", sizeof(type), __builtin_types_compatible_p(type, void*)); \\
     80        AUTOTOOL_DECLARE("long double", tag, STRING(type), "signed", "long double", sizeof(type), __builtin_types_compatible_p(type, long double)); \\
     81        AUTOTOOL_DECLARE("double", tag, STRING(type), "signed", "double", sizeof(type), __builtin_types_compatible_p(type, double)); \\
     82        AUTOTOOL_DECLARE("float", tag, STRING(type), "signed", "float", sizeof(type), __builtin_types_compatible_p(type, float));
    11983
    12084extern int main(int, char *[]);
     
    12488"""
    12589
    126 PROBE_INT128_TAIL = """}
     90PROBE_TAIL = """}
    12791"""
    12892
     
    414378        return int(value, base)
    415379
    416 def probe_compiler(common, intsizes, floatsizes):
     380def probe_compiler(common, typesizes):
    417381        "Generate, compile and parse probing source"
    418382       
     
    422386        outf.write(PROBE_HEAD)
    423387       
    424         for typedef in intsizes:
    425                 outf.write("\tDECLARE_INTSIZE(\"%s\", %s, %s, %s);\n" % (typedef['tag'], typedef['type'], typedef['strc'], typedef['conc']))
    426        
    427         for typedef in floatsizes:
    428                 outf.write("\tDECLARE_FLOATSIZE(\"%s\", %s);\n" % (typedef['tag'], typedef['type']))
     388        for typedef in typesizes:
     389                if 'def' in typedef:
     390                        outf.write("#ifdef %s\n" % typedef['def'])
     391                outf.write("\tDECLARE_BUILTIN_TYPE(\"%s\", %s);\n" % (typedef['tag'], typedef['type']))
     392                if 'def' in typedef:
     393                        outf.write("#endif\n")
    429394       
    430395        outf.write(PROBE_TAIL)
     
    457422        inf.close()
    458423       
    459         unsigned_sizes = {}
    460         signed_sizes = {}
    461        
    462         unsigned_tags = {}
    463         signed_tags = {}
    464        
    465         unsigned_strcs = {}
    466         signed_strcs = {}
    467        
    468         unsigned_concs = {}
    469         signed_concs = {}
    470        
    471         float_tags = {}
    472        
    473         builtin_sizes = {}
    474         builtin_signs = {}
     424        builtins = {}
    475425       
    476426        for j in range(len(lines)):
     
    479429                if (len(tokens) > 0):
    480430                        if (tokens[0] == "AUTOTOOL_DECLARE"):
    481                                 if (len(tokens) < 7):
     431                                if (len(tokens) < 8):
    482432                                        print_error(["Malformed declaration in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
    483433                               
    484434                                category = tokens[1]
    485                                 subcategory = tokens[2]
    486                                 tag = tokens[3]
    487                                 name = tokens[4]
    488                                 strc = tokens[5]
    489                                 conc = tokens[6]
    490                                 value = tokens[7]
     435                                tag = tokens[2]
     436                                name = tokens[3]
     437                                signedness = tokens[4]
     438                                base = tokens[5]
     439                                size = tokens[6]
     440                                compatible = tokens[7]
    491441                               
    492                                 if (category == "intsize"):
    493                                         try:
    494                                                 value_int = decode_value(value)
    495                                         except:
    496                                                 print_error(["Integer value expected in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
    497                                        
    498                                         if (subcategory == "unsigned"):
    499                                                 unsigned_sizes[value_int] = name
    500                                                 unsigned_tags[tag] = value_int
    501                                                 unsigned_strcs[value_int] = strc
    502                                                 unsigned_concs[value_int] = conc
    503                                         elif (subcategory == "signed"):
    504                                                 signed_sizes[value_int] = name
    505                                                 signed_tags[tag] = value_int
    506                                                 signed_strcs[value_int] = strc
    507                                                 signed_concs[value_int] = conc
    508                                         else:
    509                                                 print_error(["Unexpected keyword \"%s\" in \"%s\" on line %s." % (subcategory, PROBE_OUTPUT, j), COMPILER_FAIL])
     442                                try:
     443                                        compatible_int = decode_value(compatible)
     444                                        size_int = decode_value(size)
     445                                except:
     446                                        print_error(["Integer value expected in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
    510447                               
    511                                 if (category == "floatsize"):
    512                                         try:
    513                                                 value_int = decode_value(value)
    514                                         except:
    515                                                 print_error(["Integer value expected in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
    516                                        
    517                                         float_tags[tag] = value_int
    518                                
    519                                 if (category == "builtin_size"):
    520                                         try:
    521                                                 value_int = decode_value(value)
    522                                         except:
    523                                                 print_error(["Integer value expected in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
    524                                        
    525                                         builtin_sizes[tag] = {'name': name, 'value': value_int}
    526                                
    527                                 if (category == "builtin_sign"):
    528                                         try:
    529                                                 value_int = decode_value(value)
    530                                         except:
    531                                                 print_error(["Integer value expected in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
    532                                        
    533                                         if (value_int == 1):
    534                                                 if (not tag in builtin_signs):
    535                                                         builtin_signs[tag] = strc;
    536                                                 elif (builtin_signs[tag] != strc):
    537                                                         print_error(["Inconsistent builtin type detection in \"%s\" on line %s." % (PROBE_OUTPUT, j), COMPILER_FAIL])
    538        
    539         return {'unsigned_sizes': unsigned_sizes, 'signed_sizes': signed_sizes, 'unsigned_tags': unsigned_tags, 'signed_tags': signed_tags, 'unsigned_strcs': unsigned_strcs, 'signed_strcs': signed_strcs, 'unsigned_concs': unsigned_concs, 'signed_concs': signed_concs, 'float_tags': float_tags, 'builtin_sizes': builtin_sizes, 'builtin_signs': builtin_signs}
    540 
    541 def probe_int128(common):
    542         "Generate, compile and parse probing source for 128-bit integers"
    543        
    544         check_common(common, "CC")
    545        
    546         outf = open(PROBE_INT128_SOURCE, 'w')
    547         outf.write(PROBE_INT128_HEAD)
    548         outf.write("\tDECLARE_INTSIZE(\"INT128\", int __attribute((mode(TI))));\n")
    549         outf.write(PROBE_INT128_TAIL)
    550         outf.close()
    551        
    552         args = common['CC_AUTOGEN'].split(' ')
    553         args.extend(["-S", "-o", PROBE_INT128_OUTPUT, PROBE_INT128_SOURCE])
    554        
    555         try:
    556                 sys.stderr.write("Checking whether the compiler has intrinsic support for 128-bit integers ... ")
    557                 output = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
    558         except:
    559                 sys.stderr.write("no\n")
    560                 return False
    561        
    562         if (not os.path.isfile(PROBE_INT128_OUTPUT)):
    563                 sys.stderr.write("no\n")
    564                 return False
    565        
    566         inf = open(PROBE_INT128_OUTPUT, 'r')
    567         lines = inf.readlines()
    568         inf.close()
    569        
    570         for j in range(len(lines)):
    571                 tokens = lines[j].strip().split("\t")
    572                
    573                 if (len(tokens) > 0):
    574                         if (tokens[0] == "AUTOTOOL_DECLARE"):
    575                                 if (len(tokens) < 7):
    576                                         print_error(["Malformed declaration in \"%s\" on line %s." % (PROBE_INT128_OUTPUT, j), COMPILER_FAIL])
    577                                
    578                                 category = tokens[1]
    579                                 subcategory = tokens[2]
    580                                 tag = tokens[3]
    581                                 name = tokens[4]
    582                                 strc = tokens[5]
    583                                 conc = tokens[6]
    584                                 value = tokens[7]
    585                                
    586                                 if (category == "intsize"):
    587                                         try:
    588                                                 value_int = decode_value(value)
    589                                         except:
    590                                                 print_error(["Integer value expected in \"%s\" on line %s." % (PROBE_INT128_OUTPUT, j), COMPILER_FAIL])
    591                                        
    592                                         if (subcategory == "unsigned"):
    593                                                 if (value_int != 16):
    594                                                         sys.stderr.write("no\n")
    595                                                         return False
    596                                         elif (subcategory == "signed"):
    597                                                 if (value_int != 16):
    598                                                         sys.stderr.write("no\n")
    599                                                         return False
    600                                         else:
    601                                                 print_error(["Unexpected keyword \"%s\" in \"%s\" on line %s." % (subcategory, PROBE_INT128_OUTPUT, j), COMPILER_FAIL])
    602        
    603         sys.stderr.write("yes\n")
    604         return True
    605 
    606 def detect_sizes(probe, bytes, inttags, floattags):
    607         "Detect correct types for fixed-size types"
    608        
    609         macros = []
    610         typedefs = []
    611        
    612         for b in bytes:
    613                 if (not b in probe['unsigned_sizes']):
    614                         print_error(['Unable to find appropriate unsigned integer type for %u bytes.' % b,
    615                                      COMPILER_FAIL])
    616                
    617                 if (not b in probe['signed_sizes']):
    618                         print_error(['Unable to find appropriate signed integer type for %u bytes.' % b,
    619                                      COMPILER_FAIL])
    620                
    621                 if (not b in probe['unsigned_strcs']):
    622                         print_error(['Unable to find appropriate unsigned printf formatter for %u bytes.' % b,
    623                                      COMPILER_FAIL])
    624                
    625                 if (not b in probe['signed_strcs']):
    626                         print_error(['Unable to find appropriate signed printf formatter for %u bytes.' % b,
    627                                      COMPILER_FAIL])
    628                
    629                 if (not b in probe['unsigned_concs']):
    630                         print_error(['Unable to find appropriate unsigned literal macro for %u bytes.' % b,
    631                                      COMPILER_FAIL])
    632                
    633                 if (not b in probe['signed_concs']):
    634                         print_error(['Unable to find appropriate signed literal macro for %u bytes.' % b,
    635                                      COMPILER_FAIL])
    636                
    637                 typedefs.append({'oldtype': "unsigned %s" % probe['unsigned_sizes'][b], 'newtype': "uint%u_t" % (b * 8)})
    638                 typedefs.append({'oldtype': "signed %s" % probe['signed_sizes'][b], 'newtype': "int%u_t" % (b * 8)})
    639                
    640                 macros.append({'oldmacro': "unsigned %s" % probe['unsigned_sizes'][b], 'newmacro': "UINT%u_T" % (b * 8)})
    641                 macros.append({'oldmacro': "signed %s" % probe['signed_sizes'][b], 'newmacro': "INT%u_T" % (b * 8)})
    642                
    643                 macros.append({'oldmacro': "\"%so\"" % probe['unsigned_strcs'][b], 'newmacro': "PRIo%u" % (b * 8)})
    644                 macros.append({'oldmacro': "\"%su\"" % probe['unsigned_strcs'][b], 'newmacro': "PRIu%u" % (b * 8)})
    645                 macros.append({'oldmacro': "\"%sx\"" % probe['unsigned_strcs'][b], 'newmacro': "PRIx%u" % (b * 8)})
    646                 macros.append({'oldmacro': "\"%sX\"" % probe['unsigned_strcs'][b], 'newmacro': "PRIX%u" % (b * 8)})
    647                 macros.append({'oldmacro': "\"%sd\"" % probe['signed_strcs'][b], 'newmacro': "PRId%u" % (b * 8)})
    648                
    649                 name = probe['unsigned_concs'][b]
    650                 if ((name.startswith('@')) or (name == "")):
    651                         macros.append({'oldmacro': "c ## U", 'newmacro': "UINT%u_C(c)" % (b * 8)})
    652                 else:
    653                         macros.append({'oldmacro': "c ## U%s" % name, 'newmacro': "UINT%u_C(c)" % (b * 8)})
    654                
    655                 name = probe['unsigned_concs'][b]
    656                 if ((name.startswith('@')) or (name == "")):
    657                         macros.append({'oldmacro': "c", 'newmacro': "INT%u_C(c)" % (b * 8)})
    658                 else:
    659                         macros.append({'oldmacro': "c ## %s" % name, 'newmacro': "INT%u_C(c)" % (b * 8)})
    660        
    661         for tag in inttags:
    662                 newmacro = "U%s" % tag
    663                 if (not tag in probe['unsigned_tags']):
    664                         print_error(['Unable to find appropriate size macro for %s.' % newmacro,
    665                                      COMPILER_FAIL])
    666                
    667                 oldmacro = "UINT%s" % (probe['unsigned_tags'][tag] * 8)
    668                 macros.append({'oldmacro': "%s_MIN" % oldmacro, 'newmacro': "%s_MIN" % newmacro})
    669                 macros.append({'oldmacro': "%s_MAX" % oldmacro, 'newmacro': "%s_MAX" % newmacro})
    670                 macros.append({'oldmacro': "1", 'newmacro': 'U%s_SIZE_%s' % (tag, probe['unsigned_tags'][tag] * 8)})
    671                
    672                 newmacro = tag
    673                 if (not tag in probe['signed_tags']):
    674                         print_error(['Unable to find appropriate size macro for %s' % newmacro,
    675                                      COMPILER_FAIL])
    676                
    677                 oldmacro = "INT%s" % (probe['signed_tags'][tag] * 8)
    678                 macros.append({'oldmacro': "%s_MIN" % oldmacro, 'newmacro': "%s_MIN" % newmacro})
    679                 macros.append({'oldmacro': "%s_MAX" % oldmacro, 'newmacro': "%s_MAX" % newmacro})
    680                 macros.append({'oldmacro': "1", 'newmacro': '%s_SIZE_%s' % (tag, probe['signed_tags'][tag] * 8)})
    681        
    682         for tag in floattags:
    683                 if (not tag in probe['float_tags']):
    684                         print_error(['Unable to find appropriate size macro for %s' % tag,
    685                                      COMPILER_FAIL])
    686                
    687                 macros.append({'oldmacro': "1", 'newmacro': '%s_SIZE_%s' % (tag, probe['float_tags'][tag] * 8)})
    688        
    689         if (not 'size' in probe['builtin_signs']):
    690                 print_error(['Unable to determine whether size_t is signed or unsigned.',
     448                                if (compatible_int == 1):
     449                                        builtins[tag] = {
     450                                                'tag': tag,
     451                                                'name': name,
     452                                                'sign': signedness,
     453                                                'base': base,
     454                                                'size': size_int,
     455                                        }
     456       
     457        for typedef in typesizes:
     458                if not typedef['tag'] in builtins:
     459                        print_error(['Unable to determine the properties of type %s.' % typedef['tag'],
    691460                             COMPILER_FAIL])
    692        
    693         if (probe['builtin_signs']['size'] != 'unsigned'):
    694                 print_error(['The type size_t is not unsigned.',
    695                              COMPILER_FAIL])
    696        
    697         fnd = True
    698        
    699         if (not 'wchar' in probe['builtin_sizes']):
    700                 print_warning(['The compiler does not provide the macro __WCHAR_TYPE__',
    701                                'for defining the compiler-native type wchar_t. We are',
    702                                'forced to define wchar_t as a hardwired type int32_t.',
    703                                COMPILER_WARNING])
    704                 fnd = False
    705        
    706         if (probe['builtin_sizes']['wchar']['value'] != 4):
    707                 print_warning(['The compiler provided macro __WCHAR_TYPE__ for defining',
    708                                'the compiler-native type wchar_t is not compliant with',
    709                                'HelenOS. We are forced to define wchar_t as a hardwired',
    710                                'type int32_t.',
    711                                COMPILER_WARNING])
    712                 fnd = False
    713        
    714         if (not fnd):
    715                 macros.append({'oldmacro': "int32_t", 'newmacro': "wchar_t"})
     461                if 'sname' in typedef:
     462                        builtins[typedef['tag']]['sname'] = typedef['sname']
     463       
     464        return builtins
     465
     466def get_suffix(type):
     467        if type['sign'] == 'unsigned':
     468                return {
     469                        "char": "",
     470                        "short": "",
     471                        "int": "U",
     472                        "long": "UL",
     473                        "long long": "ULL",
     474                }[type['base']]
    716475        else:
    717                 macros.append({'oldmacro': "__WCHAR_TYPE__", 'newmacro': "wchar_t"})
    718        
    719         if (not 'wchar' in probe['builtin_signs']):
    720                 print_error(['Unable to determine whether wchar_t is signed or unsigned.',
    721                              COMPILER_FAIL])
    722        
    723         if (probe['builtin_signs']['wchar'] == 'unsigned'):
    724                 macros.append({'oldmacro': "1", 'newmacro': 'WCHAR_IS_UNSIGNED'})
    725         if (probe['builtin_signs']['wchar'] == 'signed'):
    726                 macros.append({'oldmacro': "1", 'newmacro': 'WCHAR_IS_SIGNED'})
    727        
    728         fnd = True
    729        
    730         if (not 'wint' in probe['builtin_sizes']):
    731                 print_warning(['The compiler does not provide the macro __WINT_TYPE__',
    732                                'for defining the compiler-native type wint_t. We are',
    733                                'forced to define wint_t as a hardwired type int32_t.',
    734                                COMPILER_WARNING])
    735                 fnd = False
    736        
    737         if (probe['builtin_sizes']['wint']['value'] != 4):
    738                 print_warning(['The compiler provided macro __WINT_TYPE__ for defining',
    739                                'the compiler-native type wint_t is not compliant with',
    740                                'HelenOS. We are forced to define wint_t as a hardwired',
    741                                'type int32_t.',
    742                                COMPILER_WARNING])
    743                 fnd = False
    744        
    745         if (not fnd):
    746                 macros.append({'oldmacro': "int32_t", 'newmacro': "wint_t"})
    747         else:
    748                 macros.append({'oldmacro': "__WINT_TYPE__", 'newmacro': "wint_t"})
    749        
    750         if (not 'wint' in probe['builtin_signs']):
    751                 print_error(['Unable to determine whether wint_t is signed or unsigned.',
    752                              COMPILER_FAIL])
    753        
    754         if (probe['builtin_signs']['wint'] == 'unsigned'):
    755                 macros.append({'oldmacro': "1", 'newmacro': 'WINT_IS_UNSIGNED'})
    756         if (probe['builtin_signs']['wint'] == 'signed'):
    757                 macros.append({'oldmacro': "1", 'newmacro': 'WINT_IS_SIGNED'})
    758        
    759         return {'macros': macros, 'typedefs': typedefs}
     476                return {
     477                        "char": "",
     478                        "short": "",
     479                        "int": "",
     480                        "long": "L",
     481                        "long long": "LL",
     482                }[type['base']]
     483
     484def get_max(type):
     485        val = (1 << (type['size']*8 - 1))
     486        if type['sign'] == 'unsigned':
     487                val *= 2
     488        return val - 1
     489
     490def detect_sizes(probe):
     491        "Detect properties of builtin types"
     492       
     493        macros = {}
     494       
     495        for type in probe.values():
     496                macros['__SIZEOF_%s__' % type['tag']] = type['size']
     497               
     498                if ('sname' in type):
     499                        macros['__%s_TYPE__'  % type['sname']] = type['name']
     500                        macros['__%s_WIDTH__' % type['sname']] = type['size']*8
     501                        macros['__%s_%s__' % (type['sname'], type['sign'].upper())] = "1"
     502                        macros['__%s_C_SUFFIX__' % type['sname']] = get_suffix(type)
     503                        macros['__%s_MAX__' % type['sname']] = "%d%s" % (get_max(type), get_suffix(type))
     504       
     505        if (probe['SIZE_T']['sign'] != 'unsigned'):
     506                print_error(['The type size_t is not unsigned.', COMPILER_FAIL])
     507       
     508        return macros
    760509
    761510def create_makefile(mkname, common):
     
    777526        outmk.close()
    778527
    779 def create_header(hdname, maps, int128):
     528def create_header(hdname, macros):
    780529        "Create header output"
    781530       
     
    790539        outhd.write('#define %s\n\n' % GUARD)
    791540       
    792         for macro in maps['macros']:
    793                 outhd.write('#define %s  %s\n' % (macro['newmacro'], macro['oldmacro']))
    794        
    795         outhd.write('\n')
    796        
    797         for typedef in maps['typedefs']:
    798                 outhd.write('typedef %s %s;\n' % (typedef['oldtype'], typedef['newtype']))
    799        
    800         if (int128):
    801                 outhd.write('typedef unsigned int __attribute((mode(TI))) uint128_t;\n')
    802                 outhd.write('typedef signed int __attribute((mode(TI))) int128_t;\n')
     541        for macro in sorted(macros):
     542                outhd.write('#ifndef %s\n' % macro)
     543                outhd.write('#define %s  %s\n' % (macro, macros[macro]))
     544                outhd.write('#endif\n\n')
    803545       
    804546        outhd.write('\n#endif\n')
     
    916658                probe = probe_compiler(common,
    917659                        [
    918                                 {'type': 'long long int', 'tag': 'LLONG', 'strc': '"ll"', 'conc': '"LL"'},
    919                                 {'type': 'long int', 'tag': 'LONG', 'strc': '"l"', 'conc': '"L"'},
    920                                 {'type': 'int', 'tag': 'INT', 'strc': '""', 'conc': '""'},
    921                                 {'type': 'short int', 'tag': 'SHRT', 'strc': '"h"', 'conc': '"@"'},
    922                                 {'type': 'char', 'tag': 'CHAR', 'strc': '"hh"', 'conc': '"@@"'}
    923                         ],
    924                         [
     660                                {'type': 'long long int', 'tag': 'LONG_LONG', 'sname': 'LLONG' },
     661                                {'type': 'long int', 'tag': 'LONG', 'sname': 'LONG' },
     662                                {'type': 'int', 'tag': 'INT', 'sname': 'INT' },
     663                                {'type': 'short int', 'tag': 'SHORT', 'sname': 'SHRT'},
     664                                {'type': 'void*', 'tag': 'POINTER'},
    925665                                {'type': 'long double', 'tag': 'LONG_DOUBLE'},
    926666                                {'type': 'double', 'tag': 'DOUBLE'},
    927                                 {'type': 'float', 'tag': 'FLOAT'}
     667                                {'type': 'float', 'tag': 'FLOAT'},
     668                                {'type': '__SIZE_TYPE__', 'tag': 'SIZE_T', 'def': '__SIZE_TYPE__', 'sname': 'SIZE' },
     669                                {'type': '__PTRDIFF_TYPE__', 'tag': 'PTRDIFF_T', 'def': '__PTRDIFF_TYPE__', 'sname': 'PTRDIFF' },
     670                                {'type': '__WINT_TYPE__', 'tag': 'WINT_T', 'def': '__WINT_TYPE__', 'sname': 'WINT' },
     671                                {'type': '__WCHAR_TYPE__', 'tag': 'WCHAR_T', 'def': '__WCHAR_TYPE__', 'sname': 'WCHAR' },
     672                                {'type': '__INTMAX_TYPE__', 'tag': 'INTMAX_T', 'def': '__INTMAX_TYPE__', 'sname': 'INTMAX' },
     673                                {'type': 'unsigned __INTMAX_TYPE__', 'tag': 'UINTMAX_T', 'def': '__INTMAX_TYPE__', 'sname': 'UINTMAX' },
    928674                        ]
    929675                )
    930676               
    931                 int128 = probe_int128(common)
    932                
    933                 maps = detect_sizes(probe, [1, 2, 4, 8], ['CHAR', 'SHRT', 'INT', 'LONG', 'LLONG'], ['LONG_DOUBLE', 'DOUBLE', 'FLOAT'])
     677                macros = detect_sizes(probe)
    934678               
    935679        finally:
     
    939683       
    940684        create_makefile(MAKEFILE, common)
    941         create_header(HEADER, maps, int128)
     685        create_header(HEADER, macros)
    942686       
    943687        return 0
Note: See TracChangeset for help on using the changeset viewer.