| 1 |
from rpython.rtyper.lltypesystem import lltype |
| 2 |
from rpython.translator.gensupp import NameManager |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
USESLOTS = True |
| 9 |
|
| 10 |
def barebonearray(ARRAY): |
| 11 |
"""Check if ARRAY is a 'simple' array type, |
| 12 |
i.e. doesn't need a length nor GC headers.""" |
| 13 |
return (ARRAY._hints.get('nolength', False) and ARRAY._gckind != 'gc' |
| 14 |
and ARRAY.OF is not lltype.Void) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
def cdecl(ctype, cname, is_thread_local=False): |
| 21 |
""" |
| 22 |
Produce a C declaration from a 'type template' and an identifier. |
| 23 |
The type template must contain a '@' sign at the place where the |
| 24 |
name should be inserted, according to the strange C syntax rules. |
| 25 |
""" |
| 26 |
|
| 27 |
|
| 28 |
__thread = "" |
| 29 |
if is_thread_local: |
| 30 |
__thread = "__thread " |
| 31 |
return __thread + ctype.replace('(@)', '@').replace('@', cname).strip() |
| 32 |
|
| 33 |
def forward_cdecl(ctype, cname, standalone, is_thread_local=False, |
| 34 |
is_exported=False): |
| 35 |
|
| 36 |
if is_exported: |
| 37 |
assert not is_thread_local |
| 38 |
prefix = "RPY_EXPORTED " |
| 39 |
else: |
| 40 |
prefix = "RPY_EXTERN " |
| 41 |
if is_thread_local: |
| 42 |
prefix += "__thread " |
| 43 |
return prefix + cdecl(ctype, cname) |
| 44 |
|
| 45 |
def somelettersfrom(s): |
| 46 |
upcase = [c for c in s if c.isupper()] |
| 47 |
if not upcase: |
| 48 |
upcase = [c for c in s.title() if c.isupper()] |
| 49 |
locase = [c for c in s if c.islower()] |
| 50 |
if locase and upcase: |
| 51 |
return ''.join(upcase).lower() |
| 52 |
else: |
| 53 |
return s[:2].lower() |
| 54 |
|
| 55 |
def is_pointer_to_forward_ref(T): |
| 56 |
if not isinstance(T, lltype.Ptr): |
| 57 |
return False |
| 58 |
return isinstance(T.TO, lltype.ForwardReference) |
| 59 |
|
| 60 |
def llvalue_from_constant(c): |
| 61 |
T = c.concretetype |
| 62 |
if T == lltype.Void: |
| 63 |
return None |
| 64 |
else: |
| 65 |
ACTUAL_TYPE = lltype.typeOf(c.value) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
if not is_pointer_to_forward_ref(ACTUAL_TYPE): |
| 70 |
assert ACTUAL_TYPE == T |
| 71 |
return c.value |
| 72 |
|
| 73 |
|
| 74 |
class CNameManager(NameManager): |
| 75 |
def __init__(self, global_prefix='pypy_'): |
| 76 |
NameManager.__init__(self, global_prefix=global_prefix) |
| 77 |
|
| 78 |
self.make_reserved_names(''' |
| 79 |
auto enum restrict unsigned |
| 80 |
break extern return void |
| 81 |
case float short volatile |
| 82 |
char for signed while |
| 83 |
const goto sizeof _Bool |
| 84 |
continue if static _Complex |
| 85 |
default inline struct _Imaginary |
| 86 |
do int switch |
| 87 |
double long typedef |
| 88 |
else register union |
| 89 |
''') |
| 90 |
|
| 91 |
def _char_repr(c): |
| 92 |
|
| 93 |
if c in '\\"?': return '\\' + c |
| 94 |
if ' ' <= c < '\x7F': return c |
| 95 |
return '\\%03o' % ord(c) |
| 96 |
|
| 97 |
def _line_repr(s): |
| 98 |
return ''.join([_char_repr(c) for c in s]) |
| 99 |
|
| 100 |
|
| 101 |
def c_string_constant(s): |
| 102 |
'''Returns a " "-delimited string literal for C.''' |
| 103 |
lines = [] |
| 104 |
for i in range(0, len(s), 64): |
| 105 |
lines.append('"%s"' % _line_repr(s[i:i+64])) |
| 106 |
return '\n'.join(lines) |
| 107 |
|
| 108 |
|
| 109 |
def c_char_array_constant(s): |
| 110 |
'''Returns an initializer for a constant char[N] array, |
| 111 |
where N is exactly len(s). This is either a " "-delimited |
| 112 |
string or a { }-delimited array of small integers. |
| 113 |
''' |
| 114 |
if s.endswith('\x00') and 1 < len(s) < 1024: |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
return c_string_constant(s[:-1]) |
| 119 |
else: |
| 120 |
lines = [] |
| 121 |
for i in range(0, len(s), 20): |
| 122 |
lines.append(','.join([str(ord(c)) for c in s[i:i+20]])) |
| 123 |
if len(lines) > 1: |
| 124 |
return '{\n%s}' % ',\n'.join(lines) |
| 125 |
else: |
| 126 |
return '{%s}' % ', '.join(lines) |
| 127 |
|
| 128 |
|
| 129 |
def gen_assignments(assignments): |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
srccount = {} |
| 135 |
dest2src = {} |
| 136 |
for typename, dest, src in assignments: |
| 137 |
if src != dest: |
| 138 |
srccount[src] = srccount.get(src, 0) + 1 |
| 139 |
dest2src[dest] = src, typename |
| 140 |
|
| 141 |
while dest2src: |
| 142 |
progress = False |
| 143 |
for dst in dest2src.keys(): |
| 144 |
if dst not in srccount: |
| 145 |
src, typename = dest2src.pop(dst) |
| 146 |
yield '%s = %s;' % (dst, src) |
| 147 |
srccount[src] -= 1 |
| 148 |
if not srccount[src]: |
| 149 |
del srccount[src] |
| 150 |
progress = True |
| 151 |
if not progress: |
| 152 |
|
| 153 |
while dest2src: |
| 154 |
dst, (src, typename) = dest2src.popitem() |
| 155 |
assert srccount[dst] == 1 |
| 156 |
startingpoint = dst |
| 157 |
tmpdecl = cdecl(typename, 'tmp') |
| 158 |
code = ['{ %s = %s;' % (tmpdecl, dst)] |
| 159 |
while src != startingpoint: |
| 160 |
code.append('%s = %s;' % (dst, src)) |
| 161 |
dst = src |
| 162 |
src, typename = dest2src.pop(dst) |
| 163 |
assert srccount[dst] == 1 |
| 164 |
code.append('%s = tmp; }' % (dst,)) |
| 165 |
yield ' '.join(code) |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
from rpython.tool.ansi_print import AnsiLogger |
| 170 |
log = AnsiLogger("c") |