summaryrefslogtreecommitdiff
path: root/support/syscall-gen/c2myr.py
blob: 9c262c5e926a99de57841fb996f7481cd5c9e93d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import sys
import re
import os
import traceback
import pycparser
import StringIO
from pycparser import c_parser, c_ast
from pycparser.plyparser import Coord
#from pycparser import c_parser, c_ast, parse_file

#from pycparser.plyparser import Coord

ctypes = {}
myrtypes = {
    'void':                     'void',
    'char':                     'byte',
    'short':                    'int16',
    'int':                      'int',
    'long':                     'int64',
    'long long':                'int64',

    'unsigned char':            'byte',
    'unsigned short':           'uint16',
    'unsigned':                 'uint',
    'unsigned int':             'uint',
    'unsigned long':            'uint64',
    'unsigned long long':       'uint64',

    'unsigned short int':       'uint16',
    'unsigned long int':        'uint64',
    'unsigned long long int':   'uint64',

    'short int':                'int16',
    'long int':                 'int64',
    'long long int':            'int64',

    'int8_t':                   'byte',
    'int16_t':                  'int16',
    'int32_t':                  'int32',
    'int64_t':                  'int64',

    'uint8_t':                  'byte',
    'uint16_t':                 'uint16',
    'uint32_t':                 'uint32',
    'uint64_t':                 'uint64',

    'size_t':                   'size',
    'ssize_t':                  'size', # myrddin sizes are signed

    # kernel-defined types?
    'u_char':                   'byte',
    'u_short':                  'uint16',
    'u_int':                    'uint',
    'u_long':                   'uint64',

    'u_int8':                   'uint8',
    'u_int16':                  'uint16',
    'u_int32':                  'uint32',
    'u_int64':                  'uint64',

    'int8':                     'int8',
    'int16':                    'int16',
    'int32':                    'int32',
    'int64':                    'int64',

    'i8':                       'int8',
    'i16':                      'int16',
    'i32':                      'int32',
    'i64':                      'int64',

    'u8':                       'uint8',
    'u16':                      'uint16',
    'u32':                      'uint32',
    'u64':                      'uint64',

    '__int64_t':                'int64',

    'char':                     'byte',
    'short':                    'int16',
    'int':                      'int',
    'long':                     'int64',

    'caddr_t':                  'void#',
}

have = set()

#valid for all unixes we support...
sizes = {
        'char':                     1,
        'short':                    2,
        'int':                      4,
        'long':                     8,
        'long int':                 8,
        'long long':                8,
        'long long int':            8,
        'unsigned char':            1,
        'unsigned short':           2,
        'unsigned short int':       2,
        'unsigned int':             4,
        'unsigned long':            8,
        'unsigned long int':        8,
        'unsigned long long':       8,
        'unsigned long long int':   8,
        'void*':                    1,
}

def fullydefined(t):
    return type(t) == c_ast.Struct and t.decls

def collectdef(n, t):
    if t in [c_ast.Decl, c_ast.TypeDecl] and type(n.type) == c_ast.Struct:
        if fullydefined(n.type):
            ctypes[n.type.name] = n.type
    elif t == c_ast.Typedef:
        # typedef struct foo foo is valid.
        if n.name not in ctypes:
            ctypes[n.name] = n
        # as is 'typedef struct foo { ... } bar'
        if type(n.type) is c_ast.TypeDecl:
            if type(n.type.type) is c_ast.Struct:
                collectdef(n.type, type(n.type))

def collect(ast):
    for n in ast.ext:
        collectdef(n, type(n))

def typename(t):
    if type(t) == c_ast.Struct:
        return t.name
    elif type(t) == c_ast.IdentifierType:
        return ' '.join(t.names)
    elif type(t) == c_ast.Typedef:
        return t.name
    elif type(t) == c_ast.Struct:
        return t.name
    elif type(t) == c_ast.TypeDecl:
        return typename(t.type)
    elif type(t) == c_ast.TypeDecl:
        return typename(t.type)
    else:
        t.show()
        raise ValueError('unknown type')

def deprefix(decls):
    pfx = None
    for d in decls:
        name = d.name
        if not name:
            return
        if name.startswith('__'):
            name = name[2:]
        sp = name.split('_', 1)
        if len(sp) != 2:
            return
        if pfx and sp[0] != pfx:
            return
        pfx = sp[0]

    for d in decls:
        if d.name.startswith('__'):
            d.name = d.name[2:]
        (hd, tl) = d.name.split('_', 1)
        d.name = tl

def myrname(n):
    namemap = {
        'acl_type_t':           'acltype',
	'acl_tag_t':	        'acltag',
	'acl_perm_t':	        'aclperm',
	'acl_entry_type_t':     'aclenttype',
	'mode_t':               'filemode',
	'ssize_t':              'size',
	'__socklen_t':          'int32',
	'socklen_t':            'int32',
	'fd_set':               'fdset',
	'__ucontext':           'ucontext',
	'cap_rights_t':         'caprights',
	'stack_t':              'sigstack',
        'thr_param':            'thrparam',
        'stat':                 'statbuf',
        'nstat':                'statbuf',
        'newstat':              'statbuf',
    }
    if n in namemap:
        return namemap[n]
    if n.endswith('_t'):
        n = n[:-2]
    if n.startswith('__'):
        n = n[2:]
    return n

def tysize(t):
    if type(t) == c_ast.Typename:
        return tysize(t.type)
    if type(t) == c_ast.TypeDecl:
        return tysize(t.type)
    if type(t) == c_ast.Typedef:
        return tysize(t.type)
    if type(t) == c_ast.Typedef:
        return tysize(t.type)
    if type(t) == c_ast.PtrDecl:
        return sizes['void*']
    if type(t) == c_ast.ArrayDecl:
        return tysize(t.type) * fold(t.dim)
    elif type(t) == c_ast.IdentifierType:
        name = ' '.join(t.names)
        if name in ctypes:
            return tysize(ctypes[name])
        else:
            return sizes[name]
    else:
        raise ValueError('unsupported type' + str(t))

def fold(expr):
    try:
        if type(expr) is c_ast.BinaryOp:
            if expr.op == '+':  return fold(expr.left) + fold(expr.right)
            if expr.op == '-':  return fold(expr.left) - fold(expr.right)
            if expr.op == '*':  return fold(expr.left) * fold(expr.right)
            if expr.op == '/':  return fold(expr.left) / fold(expr.right)
            if expr.op == '%':  return fold(expr.left) % fold(expr.right)
            if expr.op == '&':  return fold(expr.left) & fold(expr.right)
            if expr.op == '|':  return fold(expr.left) | fold(expr.right)
            if expr.op == '^':  return fold(expr.left) >> fold(expr.right)
            if expr.op == '<<': return fold(expr.left) << fold(expr.right)
            if expr.op == '>>': return fold(expr.left) >> fold(expr.right)
        elif type(expr) is c_ast.UnaryOp:
            if expr.op == '+':  return fold(expr.expr)
            if expr.op == '-':  return -fold(expr.expr)
            if expr.op == 'sizeof':
                return tysize(expr.expr)
        elif type(expr) is c_ast.Cast:
            return fold(expr.expr)
        else:
            return int(expr.value)
    except Exception:
        raise

def emitstruct(t, defname):
    fields = []
    if t.name:
        name = myrname(t.name)
    else:
        name = defname
    deprefix(t.decls)
    for d in t.decls:
        if not d.name:
            raise ValueError('null name in type')
        etype = emit(d.type)
        fname = d.name
        if fname == 'type':
            fname = 'kind'
        fields.append('{}\t: {}'.format(fname, etype))
    structs.write('type {} = struct\n\t{}\n\n;;\n\n'.format(name, '\n\t'.join(fields)))
    return name

def emit(t, defname=None):
    if type(t) == c_ast.Struct:
        if t.name:
            if myrname(t.name) in have:
                return t.name
            # linked lists can be a problem...
            have.add(myrname(t.name))
        if not t.decls:
            t = ctypes[t.name]
        return emitstruct(t, defname)
    elif type(t) == c_ast.IdentifierType:
        name = ' '.join(t.names)
        if name in myrtypes:
            return myrtypes[' '.join(t.names)]
        return myrtype(name, defname)
    elif type(t) == c_ast.Typedef:
        if t.name in myrtypes:
            return myrtypes[t.name]
        name = myrname(t.name)
        if name in have:
            return name
        # fold chains of typedefs
        sub = emit(t.type, name)
        if defname:
            return myrname(sub)
        else:
            if name != sub and name not in have:
                have.add(name)
                defs.write('type {} = {}\n'.format(name, sub))
            return name
    elif type(t) == c_ast.TypeDecl:
        return emit(t.type, defname)
    elif type(t) == c_ast.ArrayDecl:
        ret = emit(t.type)
        return '{}[{}]'.format(ret, fold(t.dim))
    elif type(t) == c_ast.PtrDecl:
        # Myrddin functions aren't easy to translate to C.
        if type(t.type) == c_ast.FuncDecl:
            return 'void#'
        return emit(t.type) + '#'
    elif type(t) == c_ast.Union:
        raise ValueError('unions unsupported')
    else:
        t.show()
        raise ValueError('unknown type')


def myrtype(name, defname=None):
    if name is None:
        return None
    if myrname(name) in myrtypes:
        return myrtypes[myrname(name)]

    t = ctypes[name]
    gen = emit(t, defname)
    have.add(myrname(gen))
    return gen

if __name__ == '__main__':
    structs = StringIO.StringIO()
    defs = StringIO.StringIO()

    prehdr = '''
#define __builtin_va_list int
#define __inline
#define __inline__
#define __restrict
#define __attribute__(_)
#define __asm(x)
#define __asm__(x)
#define __extension__
#define __signed__
#define __extern_inline__
#define volatile(_) /* catches '__asm volatile(asm)' on openbsd */

#include <sys/types.h>
#include <sys/cdefs.h>
#ifdef __linux__
#include <sys/sysinfo.h>
#include <utime.h>
#include <ucontext.h>
#include <linux/futex.h>
#endif
    '''
    includes = []
    # A bunch of these headers are system-only. We wouldn't
    # care, but they also try to prevent people from depending
    # on them accidentally. As a result, let's just try including
    # each one ahead of time, and see if it works.
    #
    # if so, we generate a mega-header with all the types.
    syshdr = []
    if os.path.exists("/usr/include/sys"):
        syshdr = ["sys/" + h for h in os.listdir('/usr/include/sys')]
    if os.path.exists("/usr/include/linux"):
        syshdr = syshdr + ["linux/" + h for h in os.listdir("/usr/include/linux")]
    # it seems that sys headers can also be stashed here. Shit.
    if os.path.exists("/usr/include/x86_64-linux-gnu/sys"):
        syshdr = syshdr + ["linux/" + h for h in os.listdir("/usr/include/linux")]
    for hdr in syshdr:
        print 'trying {}'.format(hdr)
        if not hdr.endswith(".h"):
            continue
        try:
            include = prehdr + '\n#include <{}>'.format(hdr)
            with open('/tmp/myr-includes.h', 'w') as f:
                f.write(include)
            pycparser.parse_file('/tmp/myr-includes.h', use_cpp=True)
            includes.append('#include <{}>'.format(hdr))
        except Exception:
            print '...skip'

    print('importing' + ' '.join(includes))
    with open('/tmp/myr-includes.h', 'w') as f:
        f.write(prehdr + '\n' + '\n'.join(includes))
    with open('want.txt') as f:
        want = f.read().split()
    with open('have.txt') as f:
        have |= set(f.read().split())
        for sz in ['', 8, 16, 32, 64]:
            have.add('int{}'.format(sz))
            have.add('uint{}'.format(sz))
    ast = pycparser.parse_file('/tmp/myr-includes.h', use_cpp=True)
    collect(ast)
    for w in want:
        try:
            myrtype(w)
        except (ValueError,KeyError) as e:
            print('unconvertable type {}: add a manual override: {}'.format(w, e))
    f = 'gentypes+{}-{}.frag'.format(sys.argv[1], sys.argv[2])
    with open(f, 'w') as f:
        f.write(defs.getvalue())
        f.write('\n')
        f.write(structs.getvalue())