| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import py |
| 6 |
import os |
| 7 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 8 |
from rpython.tool.udir import udir |
| 9 |
from rpython.rlib import rarithmetic |
| 10 |
from rpython.rtyper.lltypesystem import lltype |
| 11 |
from rpython.tool.gcc_cache import build_executable_cache |
| 12 |
|
| 13 |
def ask_gcc(question, add_source="", ignore_errors=False): |
| 14 |
from rpython.translator.platform import platform |
| 15 |
includes = ['stdlib.h', 'stdio.h', 'sys/types.h'] |
| 16 |
if platform.name != 'msvc': |
| 17 |
includes += ['inttypes.h', 'stddef.h'] |
| 18 |
include_string = "\n".join(["#include <%s>" % i for i in includes]) |
| 19 |
c_source = py.code.Source(''' |
| 20 |
// includes |
| 21 |
%s |
| 22 |
|
| 23 |
%s |
| 24 |
|
| 25 |
// checking code |
| 26 |
int main(void) |
| 27 |
{ |
| 28 |
%s |
| 29 |
return (0); |
| 30 |
} |
| 31 |
''' % (include_string, add_source, str(question))) |
| 32 |
c_file = udir.join("gcctest.c") |
| 33 |
c_file.write(str(c_source) + '\n') |
| 34 |
eci = ExternalCompilationInfo() |
| 35 |
return build_executable_cache([c_file], eci, ignore_errors=ignore_errors) |
| 36 |
|
| 37 |
def sizeof_c_type(c_typename, **kwds): |
| 38 |
return sizeof_c_types([c_typename], **kwds)[0] |
| 39 |
|
| 40 |
def sizeof_c_types(typenames_c, **kwds): |
| 41 |
lines = ['printf("sizeof %s=%%ld\\n", (long)sizeof(%s));' % (c_typename, |
| 42 |
c_typename) |
| 43 |
for c_typename in typenames_c] |
| 44 |
question = '\n\t'.join(lines) |
| 45 |
answer = ask_gcc(question, **kwds) |
| 46 |
lines = answer.splitlines() |
| 47 |
assert len(lines) == len(typenames_c) |
| 48 |
result = [] |
| 49 |
for line, c_typename in zip(lines, typenames_c): |
| 50 |
answer = line.split('=') |
| 51 |
assert answer[0] == "sizeof " + c_typename |
| 52 |
result.append(int(answer[1])) |
| 53 |
return result |
| 54 |
|
| 55 |
def signof_c_type(c_typename, **kwds): |
| 56 |
question = 'printf("sign %s=%%d\\n", ((%s) -1) <= (%s)0);' % (c_typename, |
| 57 |
c_typename, |
| 58 |
c_typename) |
| 59 |
answer = ask_gcc(question, **kwds).strip() |
| 60 |
if answer == 'sign %s=0' % (c_typename,): |
| 61 |
return False |
| 62 |
if answer == 'sign %s=1' % (c_typename,): |
| 63 |
return True |
| 64 |
raise ValueError(answer) |
| 65 |
|
| 66 |
|
| 67 |
class Platform: |
| 68 |
def __init__(self): |
| 69 |
self.types = {} |
| 70 |
self.numbertype_to_rclass = {} |
| 71 |
|
| 72 |
def inttype(self, name, c_name, signed, **kwds): |
| 73 |
try: |
| 74 |
return self.types[name] |
| 75 |
except KeyError: |
| 76 |
size = sizeof_c_type(c_name, **kwds) |
| 77 |
return self._make_type(name, signed, size) |
| 78 |
|
| 79 |
def _make_type(self, name, signed, size): |
| 80 |
inttype = rarithmetic.build_int('r_' + name, signed, size*8) |
| 81 |
tp = lltype.build_number(name, inttype) |
| 82 |
self.numbertype_to_rclass[tp] = inttype |
| 83 |
self.types[name] = tp |
| 84 |
return tp |
| 85 |
|
| 86 |
def populate_inttypes(self, list, **kwds): |
| 87 |
"""'list' is a list of (name, c_name, signed).""" |
| 88 |
missing = [] |
| 89 |
names_c = [] |
| 90 |
for name, c_name, signed in list: |
| 91 |
if name not in self.types: |
| 92 |
missing.append((name, signed)) |
| 93 |
names_c.append(c_name) |
| 94 |
if names_c: |
| 95 |
sizes = sizeof_c_types(names_c, **kwds) |
| 96 |
assert len(sizes) == len(missing) |
| 97 |
for (name, signed), size in zip(missing, sizes): |
| 98 |
self._make_type(name, signed, size) |
| 99 |
|
| 100 |
platform = Platform() |