medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

generator.py (17549B)


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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
from itertools import chain
import os.path
import sys

from glad.lang.common.generator import Generator
from glad.lang.common.util import makefiledir


if sys.version_info >= (3, 0):
    from io import StringIO
    basestring = str
else:
    from StringIO import StringIO


def _gl_types(gen, f):
    f.write('''
  GLdebugProc* = proc (
    source: GLenum,
    typ: GLenum,
    id: GLuint,
    severity: GLenum,
    length: GLsizei,
    message: ptr GLchar,
    userParam: pointer) {.stdcall.}

  GLdebugProcArb* = proc (
    source: GLenum,
    typ: GLenum,
    id: GLuint,
    severity: GLenum,
    len: GLsizei,
    message: ptr GLchar,
    userParam: pointer) {.stdcall.}

  GLdebugProcAmd* = proc (
    id: GLuint,
    category: GLenum,
    severity: GLenum,
    len: GLsizei,
    message: ptr GLchar,
    userParam: pointer) {.stdcall.}

  GLdebugProcKhr* = proc (
    source, typ: GLenum,
    id: GLuint,
    severity: GLenum,
    length: GLsizei,
    message: ptr GLchar,
    userParam: pointer) {.stdcall.}
''')


# TODO finish converting the egl, glx & wgl loaders to Nim

# def _egl_types(gen, f):
# def _glx_types(gen, f):
# def _wgl_types(gen, f):

NIMTYPES = {
    '__other': {
        'gl': _gl_types
#        'egl': _egl_types,
#        'glx': _glx_types,
#        'wgl': _wgl_types
    },

    'gl': {
        'GLbitfield': 'uint32',
        'GLboolean': 'bool',
        'GLbyte': 'int8',
        'GLchar': 'char',
        'GLcharARB': 'byte',
        'GLclampd': 'float64',
        'GLclampf': 'float32',
        'GLclampx': 'int32',
        'GLdouble': 'float64',
        'GLeglImageOES': 'distinct pointer',
        'GLenum': 'uint32',
        'GLfixed': 'int32',
        'GLfloat': 'float32',
        'GLhalf': 'uint16',
        'GLhalfARB': 'uint16',
        'GLhalfNV': 'uint16',
        'GLhandleARB': 'uint32',
        'GLint': 'int32',
        'GLint64': 'int64',
        'GLint64EXT': 'int64',
        'GLintptr': 'int32',
        'GLintptrARB': 'int32',
        'GLshort': 'int16',
        'GLsizei': 'int32',
        'GLsizeiptr': 'int32',
        'GLsizeiptrARB': 'int32',
        'GLubyte': 'uint8',
        'GLuint': 'uint32',
        'GLuint64': 'uint64',
        'GLuint64EXT': 'uint64',
        'GLushort': 'uint16',
        'GLvdpauSurfaceNV': 'int32',
        'GLvoid': 'pointer',
        'GLsync': 'distinct pointer',
        'ClContext': 'distinct pointer',
        'ClEvent': 'distinct pointer'
    },
    'egl': {
        'EGLAttrib': 'int32',
        'EGLAttribKHR': 'int32',
        'EGLBoolean': 'bool',
        'EGLClientBuffer': 'distinct pointer',
        'EGLConfig': 'distinct pointer',
        'EGLContext': 'distinct pointer',
        'EGLDeviceEXT': 'distinct pointer',
        'EGLDisplay': 'distinct pointer',
        'EGLImage': 'distinct pointer',
        'EGLImageKHR': 'distinct pointer',
        'EGLNativeFileDescriptorKHR': 'int32',
        'EGLOutputLayerEXT': 'distinct pointer',
        'EGLOutputPortEXT': 'distinct pointer',
        'EGLStreamKHR': 'distinct pointer',
        'EGLSurface': 'distinct pointer',
        'EGLSync': 'distinct pointer',
        'EGLSyncKHR': 'distinct pointer',
        'EGLSyncNV': 'distinct pointer',
        'EGLTimeKHR': 'uint64',
        'EGLTime': 'uint64',
        'EGLTimeNV': 'uint64',
        'EGLenum': 'uint32',
        'EGLint': 'int32',
        'EGLsizeiANDROID': 'distinct pointer',
        'EGLuint64KHR': 'uint64',
        'EGLuint64MESA': 'uint64',
        'EGLuint64NV': 'uint64',
#        '__eglMustCastToProperFunctionPointerType': 'void function()'
    },
    'glx': {
        'GLbitfield': 'uint32',
        'GLboolean': 'uint8',
        'GLenum': 'uint32',
        'GLfloat': 'float32',
        'GLint': 'int32',
        'GLintptr': 'int32',
        'GLsizei': 'int32',
        'GLsizeiptr': 'int32',
        'GLubyte': 'uint8',
        'GLuint': 'uint32'
    },
    'wgl': {
        'GLbitfield': 'uint32',
        'GLboolean': 'uint8',
        'GLenum': 'uint32',
        'GLfloat': 'float32',
        'GLint': 'int32',
        'GLsizei': 'int32',
        'GLuint': 'uint32',
        'GLushort': 'uint16',
        'INT32': 'int32',
        'INT64': 'int64'
    },

    'SpecialNumbers': {
        'gl': [
            ('GL_FALSE', '0', None),
            ('GL_INVALID_INDEX', '0xFFFFFFFF', 'uint32'),
            ('GL_NONE', '0', None),
            ('GL_NONE_OES', '0', None),
            ('GL_NO_ERROR', '0', None),
            ('GL_ONE', '1', None),
            ('GL_TIMEOUT_IGNORED', '0xFFFFFFFFFFFFFFFF', 'uint64'),
            ('GL_TIMEOUT_IGNORED_APPLE', '0xFFFFFFFFFFFFFFFF', 'uint64'),
            ('GL_TRUE', '1', None),
            ('GL_VERSION_ES_CL_1_0', '1', None),
            ('GL_VERSION_ES_CL_1_1', '1', None),
            ('GL_VERSION_ES_CM_1_1', '1', None),
            ('GL_ZERO', '0', None),
        ],
        'egl': [
#            ('EGL_DONT_CARE', '-1', 'int'), ('EGL_UNKNOWN', '-1', 'int'),
#            ('EGL_NO_NATIVE_FENCE_FD_ANDROID', '-1', 'uint'),
#            ('EGL_DEPTH_ENCODING_NONE_NV', '0', 'uint'),
#            ('EGL_NO_CONTEXT', 'cast(EGLContext)0', 'EGLContext'),
#            ('EGL_NO_DEVICE_EXT', 'cast(EGLDeviceEXT)0', 'EGLDeviceEXT'),
#            ('EGL_NO_DISPLAY', 'cast(EGLDisplay)0', 'EGLDisplay'),
#            ('EGL_NO_IMAGE', 'cast(EGLImage)0', 'EGLImage'),
#            ('EGL_NO_IMAGE_KHR', 'cast(EGLImageKHR)0', 'EGLImageKHR'),
#            ('EGL_DEFAULT_DISPLAY', 'cast(EGLNativeDisplayType)0', 'EGLNativeDisplayType'),
#            ('EGL_NO_FILE_DESCRIPTOR_KHR', 'cast(EGLNativeFileDescriptorKHR)-1', 'EGLNativeFileDescriptorKHR'),
#            ('EGL_NO_OUTPUT_LAYER_EXT', 'cast(EGLOutputLayerEXT)0', 'EGLOutputLayerEXT'),
#            ('EGL_NO_OUTPUT_PORT_EXT', 'cast(EGLOutputPortEXT)0', 'EGLOutputPortEXT'),
#            ('EGL_NO_STREAM_KHR', 'cast(EGLStreamKHR)0', 'EGLStreamKHR'),
#            ('EGL_NO_SURFACE', 'cast(EGLSurface)0', 'EGLSurface'),
#            ('EGL_NO_SYNC', 'cast(EGLSync)0', 'EGLSync'),
#            ('EGL_NO_SYNC_KHR', 'cast(EGLSyncKHR)0', 'EGLSyncKHR'),
#            ('EGL_NO_SYNC_NV', 'cast(EGLSyncNV)0', 'EGLSyncNV'),
#            ('EGL_DISPLAY_SCALING', '10000', 'uint'),
#            ('EGL_FOREVER', '0xFFFFFFFFFFFFFFFF', 'ulong'),
#            ('EGL_FOREVER_KHR', '0xFFFFFFFFFFFFFFFF', 'ulong'),
#            ('EGL_FOREVER_NV', '0xFFFFFFFFFFFFFFFF', 'ulong')
        ],
        'glx': [
#            ('GLX_DONT_CARE', '0xFFFFFFFF', 'uint'),
#            ('GLX_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB', '0', 'uint')
        ],
        'wgl': [
#            ('WGL_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB', '0', 'uint'),
#            ('WGL_FONT_LINES', '0', 'uint'),
#            ('WGL_FONT_POLYGONS', 1, 'uint')
        ]
    },

    'SpecialEnumNames': {
        'gl': {
             'GL_BYTE': 'cGL_BYTE',
             'GL_SHORT': 'cGL_SHORT',
             'GL_INT': 'cGL_INT',
             'GL_FLOAT': 'cGL_FLOAT',
             'GL_DOUBLE': 'cGL_DOUBLE',
             'GL_FIXED': 'cGL_FIXED'
        },
        'egl': {},
        'glx': {},
        'wgl': {}
    },

    'SpecialFuncNames': {
        'gl': {
             'glGetTransformFeedbacki_v': 'glGetTransformFeedbacki_v2'
        },
        'egl': {},
        'glx': {},
        'wgl': {}
    }
}


class NimGenerator(Generator):
    NAME = 'nim'
    NAME_LONG = 'Nim'

    MODULE = 'glad'
    FILE_EXTENSION = '.nim'
    TYPE_DICT = NIMTYPES

    LOAD_GL_PREFIX = 'glad'
    EXT_PREFIX = 'GLAD_'

    def open(self):
        self._f_gl = open(self.make_path(self.spec.NAME), 'w')

    def close(self):
        self._f_gl.close()

    def generate_header(self):
        self._f_gl.write('#[')
        self._f_gl.write(self.header)
        self._f_gl.write(']#\n\n')
        self._f_gl.write('import strutils\n\n')
        self._f_gl.write('var glVersionMajor, glVersionMinor: int\n\n')

    def generate_loader(self, features, extensions):
        f = self._f_gl

        rfeatures = features
        if self.spec.NAME in ('egl', 'wgl'):
            features = {'egl': [], 'wgl': []}

        self.loader.write(f)
        self.loader.write_has_ext(f, self.get_version())

        written = set()
        for api, version in self.api.items():
            # core load procs
            for feature in features[api]:
                f.write('proc load_{}(load: proc) =\n'
                            .format(feature.name))
                if self.spec.NAME == 'gl':
                    f.write('  if not {}{}: return\n\n'.format(self.EXT_PREFIX,
                                                               feature.name))
                for func in feature.functions:
                    self.write_func_definition(f, func)
                f.write('\n\n')

            # extension load procs
            for ext in extensions[api]:
                if len(list(ext.functions)) == 0 or ext.name in written:
                    continue

                f.write('proc load_{}(load: proc) =\n'
                    .format(ext.name))
                if self.spec.NAME == 'gl':
                    f.write('  if not {}{}: return\n'.format(self.EXT_PREFIX,
                                                             ext.name))
                for func in ext.functions:
                    # even if they were in written we need to load it
                    self.write_func_definition(f, func)

                f.write('\n\n')

                written.add(ext.name)

            # findExtensions proc
            f.write('proc findExtensions{}() =\n'.format(api.upper()))
            if self.spec.NAME == 'gl':
                for ext in extensions[api]:
                    f.write('  {0}{1} = hasExt("{1}")\n'.format(self.EXT_PREFIX,
                                                                ext.name))
            f.write('\n\n')

            # findCore proc
            f.write('proc findCore{}(glVersion: string) =\n'.format(api.upper()))
            self.loader.write_find_core(f)
            if self.spec.NAME == 'gl':
                for feature in features[api]:
                    f.write('  {}{} = (major == {num[0]} and minor >= {num[1]}) or'
                        ' major > {num[0]}\n'.format(self.EXT_PREFIX, feature.name,
                                                     num=feature.number))
            f.write('\n\n')

            # main loader proc
            loadername = 'Load' if self.LOAD_GL_PREFIX else 'load'
            f.write('proc {}{}{}*(load: proc): bool =\n'
                    .format(self.LOAD_GL_PREFIX, loadername, api.upper()))
            self.loader.write_begin_load(f)

            f.write('  findCore{}($glVersion)\n\n'.format(api.upper()))
            for feature in features[api]:
                f.write('  load_{}(load)\n'.format(feature.name))
            f.write('\n  findExtensions{}()\n\n'.format(api.upper()))
            for ext in extensions[api]:
                if len(list(ext.functions)) == 0:
                    continue
                f.write('  load_{}(load);\n'.format(ext.name))
            self.loader.write_end_load(f)
            f.write('\n')


    def write_func_definition(self, fobj, func):
        func_name = self.map_func_name(func)
        fobj.write('  {} = cast['.format(func_name))
        self.write_function_declaration(fobj, func)
        fobj.write('](load("{}"))\n'.format(func_name))


    def map_func_name(self, func):
        name = func.proto.name
        m = self.TYPE_DICT['SpecialFuncNames'][self.spec.NAME]
        return m[name] if name in m else name


    def generate_types(self, types):
        f = self._f_gl

        f.write('# Types\ntype\n')
        for ogl, d in sorted(self.TYPE_DICT[self.spec.NAME].items()):
            f.write('  {}* = {}\n'.format(ogl, d))
        self.TYPE_DICT['__other'][self.spec.NAME](self, f)
        f.write('\n')


    def generate_features(self, features):
        self.write_enums(features)
        self.write_funcs(features)


    def write_enums(self, features):
        f = self._f_gl

        f.write('\n# Enums\nconst\n')
        for v in sorted(self.TYPE_DICT['SpecialNumbers'][self.spec.NAME]):
            self.write_enum(f, *v)
        f.write('\n')

        written = set()
        for feature in features:
            for enum in feature.enums:
                if enum.group == 'SpecialNumbers':
                    written.add(enum)
                    continue
                if not enum in written:
                    self.write_enum(f, enum.name, enum.value)
                written.add(enum)
        f.write('\n')


    def write_funcs(self, features):
        f = self._f_gl

        f.write('\n# Functions\nvar\n')
        if self.spec.NAME == 'gl':
            for feature in features:
                self.write_boolean(f, feature.name)
            f.write('\n')

        # TODO
        if self.spec.NAME in ('egl', 'wgl'):
            for feature in features:
                for func in feature.functions:
                    self.write_function_def(f, func) # TODO
                f.write('\n')
        else:
            self.write_functions(f, set(), set(), features)
        f.write('\n\n')


    # TODO
    def write_function_def(self, fobj, func):
        fobj.write('{} {}('.format(func.proto.ret.to_nim(), self.map_func_name(func)))
        fobj.write(', '.join(param.type.to_nim() for param in func.params))
        fobj.write(');\n')


    def generate_extensions(self, extensions, enums, functions):
        f = self._f_gl

        write = set()
        written = set(enum.name for enum in enums) | \
                  set(function.proto.name for function in functions)

        f.write('# Extensions\nvar\n')
        for ext in extensions:
            if self.spec.NAME == 'gl' and not ext.name in written:
                self.write_boolean(f, ext.name)

            for enum in ext.enums:
                if not enum.name in written and not enum.group == 'SpecialNumbers':
                    type = (None if enum.group == 'TransformFeedbackTokenNV'
                                 else 'GLenum')
                    self.write_enum(f, enum.name, enum.value, type)
                written.add(enum.name)
            written.add(ext.name)
            f.write('\n')

        self.write_functions(f, write, written, extensions)
        f.write('\n\n')


    def write_functions(self, f, write, written, extensions):
        for ext in extensions:
            for func in ext.functions:
                if not func.proto.name in written:
                    self.write_function_var(f, func)
                    write.add(func)
                written.add(func.proto.name)


    def write_function_var(self, fobj, func):
        fobj.write('  {}*: '.format(self.map_func_name(func)))
        self.write_function_declaration(fobj, func)
        fobj.write('\n')


    def write_function_declaration(self, fobj, func):
        fobj.write('proc ('.format(self.map_func_name(func)))
        fobj.write(', '.join('{}: {}'.format(self.to_nim_param_name(param.name),
                                             param.type.to_nim())
                   for param in func.params))
        fobj.write(')')

        ret = func.proto.ret.to_nim()
        if (ret != 'void'):
          fobj.write(': {}'.format(ret))

        fobj.write(' {.cdecl.}')

# TODO
#    def write_function_var(self, fobj, func):
#        fobj.write('  {} = cast[proc ('.format(func.proto.name))
#        fobj.write(', '.join('{}: {}'.format(self.to_nim_param_name(param.name),
#                                             param.type.to_nim())
#                   for param in func.params))
#        fobj.write(')')
#
#        ret = func.proto.ret.to_nim()
#        if (ret != 'void'):
#          fobj.write(': {}'.format(ret))
#
#        fobj.write(' {.cdecl.}]')
#        fobj.write(' (getProcAddress("{}"))\n'.format(func.proto.name))


    NIM_KEYWORDS = [   # as of Nim 0.13.0
      'addr', 'and', 'as', 'asm', 'atomic',
      'bind', 'block', 'break',
      'case', 'cast', 'concept', 'const', 'continue', 'converter',
      'defer', 'discard', 'distinct', 'div', 'do',
      'elif', 'else', 'end', 'enum', 'except', 'export',
      'finally', 'for', 'from', 'func',
      'generic',
      'if', 'import', 'in', 'include', 'interface', 'is', 'isnot', 'iterator',
      'let',
      'macro', 'method', 'mixin', 'mod',
      'nil', 'not', 'notin',
      'object', 'of', 'or', 'out',
      'proc', 'ptr',
      'raise', 'ref', 'return',
      'shl', 'shr', 'static',
      'template', 'try', 'tuple', 'type',
      'using',
      'var',
      'when', 'while', 'with', 'without',
      'xor',
      'yield'
    ]

    def to_nim_param_name(self, name):
        return '`{}`'.format(name) if name in self.NIM_KEYWORDS else name

    def make_path(self, name):
        path = os.path.join(self.path, self.MODULE.split('.')[-1],
                            name.split('.')[-1] + self.FILE_EXTENSION)
        makefiledir(path)
        return path

    def write_boolean(self, fobj, name):
        fobj.write('  {}{}*: bool\n'.format(self.EXT_PREFIX, name))

    def write_enum(self, fobj, name, value, type='GLenum'):
        fobj.write('  {}*'.format(self.map_enum_name(name)))
        if type:
          fobj.write(': {0} = {0}({1})'.format(type, value))
        else:
          fobj.write(' = {}'.format(value))
        fobj.write('\n')

    def map_enum_name(self, name):
        m = self.TYPE_DICT['SpecialEnumNames'][self.spec.NAME]
        return m[name] if name in m else name

    def get_version(self):
        return self.api[self.spec.NAME]