mapi: Fix a couple of warning in generated code
[mesa.git] / src / mapi / glapi / gen / gl_marshal.py
index b7e05acb133144f65436c6080d5f0b92e5d0a8ec..c7019a80328799ada4f46a323a22e39c3d183f8b 100644 (file)
@@ -1,4 +1,3 @@
-#!/usr/bin/env python
 
 # Copyright (C) 2012 Intel Corporation
 #
@@ -21,6 +20,8 @@
 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 # IN THE SOFTWARE.
 
+from __future__ import print_function
+
 import contextlib
 import getopt
 import gl_XML
@@ -30,22 +31,32 @@ import sys
 
 header = """
 #include "api_exec.h"
-#include "context.h"
+#include "glthread_marshal.h"
+#include "bufferobj.h"
 #include "dispatch.h"
-#include "glthread.h"
-#include "marshal.h"
-#include "marshal_generated.h"
+
+#define COMPAT (ctx->API != API_OPENGL_CORE)
+
+UNUSED static inline int safe_mul(int a, int b)
+{
+    if (a < 0 || b < 0) return -1;
+    if (a == 0 || b == 0) return 0;
+    if (a > INT_MAX / b) return -1;
+    return a * b;
+}
 """
 
 
+file_index = 0
+file_count = 1
 current_indent = 0
 
 
 def out(str):
     if str:
-        print ' '*current_indent + str
+        print(' '*current_indent + str)
     else:
-        print ''
+        print('')
 
 
 @contextlib.contextmanager
@@ -65,42 +76,33 @@ class PrintCode(gl_XML.gl_print_base):
             'Copyright (C) 2012 Intel Corporation', 'INTEL CORPORATION')
 
     def printRealHeader(self):
-        print header
-        print '#include <X11/Xlib-xcb.h>'
-        print
-        print 'static _X_INLINE int safe_mul(int a, int b)'
-        print '{'
-        print '    if (a < 0 || b < 0) return -1;'
-        print '    if (a == 0 || b == 0) return 0;'
-        print '    if (a > INT_MAX / b) return -1;'
-        print '    return a * b;'
-        print '}'
-        print
+        print(header)
 
     def printRealFooter(self):
         pass
 
-    def print_sync_call(self, func):
+    def print_sync_call(self, func, unmarshal = 0):
         call = 'CALL_{0}(ctx->CurrentServerDispatch, ({1}))'.format(
             func.name, func.get_called_parameter_string())
         if func.return_type == 'void':
             out('{0};'.format(call))
+            if func.marshal_call_after and not unmarshal:
+                out(func.marshal_call_after);
         else:
             out('return {0};'.format(call))
+            assert not func.marshal_call_after
 
     def print_sync_dispatch(self, func):
-        out('_mesa_glthread_finish(ctx);')
         self.print_sync_call(func)
 
     def print_sync_body(self, func):
         out('/* {0}: marshalled synchronously */'.format(func.name))
-        out('static {0} GLAPIENTRY'.format(func.return_type))
+        out('{0} GLAPIENTRY'.format(func.return_type))
         out('_mesa_marshal_{0}({1})'.format(func.name, func.get_parameter_string()))
         out('{')
         with indent():
             out('GET_CURRENT_CONTEXT(ctx);')
-            out('_mesa_glthread_finish(ctx);')
-            out('debug_print_sync("{0}");'.format(func.name))
+            out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func.name))
             self.print_sync_call(func)
         out('}')
         out('')
@@ -117,88 +119,199 @@ class PrintCode(gl_XML.gl_print_base):
                 out('cmd->{0} = {0};'.format(p.name))
         if func.variable_params:
             out('char *variable_data = (char *) (cmd + 1);')
+            i = 1
             for p in func.variable_params:
-                out(('memcpy(variable_data, {0}, {1});').format(
-                        p.name, p.size_string(False)))
-                out('variable_data += {0};'.format(
-                        p.size_string(False)))
+                if p.img_null_flag:
+                    out('cmd->{0}_null = !{0};'.format(p.name))
+                    out('if (!cmd->{0}_null) {{'.format(p.name))
+                    with indent():
+                        out(('memcpy(variable_data, {0}, {0}_size);').format(p.name))
+                        if i < len(func.variable_params):
+                            out('variable_data += {0}_size;'.format(p.name))
+                    out('}')
+                else:
+                    out(('memcpy(variable_data, {0}, {0}_size);').format(p.name))
+                    if i < len(func.variable_params):
+                        out('variable_data += {0}_size;'.format(p.name))
+                i += 1
+
         if not func.fixed_params and not func.variable_params:
-            out('(void) cmd;\n')
-        out('_mesa_post_marshal_hook(ctx);')
+            out('(void) cmd;')
+
+        if func.marshal_call_after:
+            out(func.marshal_call_after);
+
+        # Uncomment this if you want to call _mesa_glthread_finish for debugging
+        #out('_mesa_glthread_finish(ctx);')
+
+    def get_type_size(self, str):
+        if str.find('*') != -1:
+            return 8;
+
+        mapping = {
+            'GLboolean': 1,
+            'GLbyte': 1,
+            'GLubyte': 1,
+            'GLshort': 2,
+            'GLushort': 2,
+            'GLenum': 4,
+            'GLint': 4,
+            'GLuint': 4,
+            'GLbitfield': 4,
+            'GLsizei': 4,
+            'GLfloat': 4,
+            'GLclampf': 4,
+            'GLfixed': 4,
+            'GLclampx': 4,
+            'GLhandleARB': 4,
+            'int': 4,
+            'float': 4,
+            'GLdouble': 8,
+            'GLclampd': 8,
+            'GLintptr': 8,
+            'GLsizeiptr': 8,
+            'GLint64': 8,
+            'GLuint64': 8,
+            'GLuint64EXT': 8,
+            'GLsync': 8,
+        }
+        val = mapping.get(str, 9999)
+        if val == 9999:
+            print('Unhandled type in gl_marshal.py.get_type_size: ' + str, file=sys.stderr)
+        return val
 
     def print_async_struct(self, func):
         out('struct marshal_cmd_{0}'.format(func.name))
         out('{')
         with indent():
             out('struct marshal_cmd_base cmd_base;')
-            for p in func.fixed_params:
+
+            # Sort the parameters according to their size to pack the structure optimally
+            for p in sorted(func.fixed_params, key=lambda p: self.get_type_size(p.type_string())):
                 if p.count:
                     out('{0} {1}[{2}];'.format(
                             p.get_base_type_string(), p.name, p.count))
                 else:
                     out('{0} {1};'.format(p.type_string(), p.name))
+
+            for p in func.variable_params:
+                if p.img_null_flag:
+                    out('bool {0}_null; /* If set, no data follows '
+                        'for "{0}" */'.format(p.name))
+
             for p in func.variable_params:
                 if p.count_scale != 1:
                     out(('/* Next {0} bytes are '
                          '{1} {2}[{3}][{4}] */').format(
-                            p.size_string(), p.get_base_type_string(),
+                            p.size_string(marshal = 1), p.get_base_type_string(),
                             p.name, p.counter, p.count_scale))
                 else:
                     out(('/* Next {0} bytes are '
                          '{1} {2}[{3}] */').format(
-                            p.size_string(), p.get_base_type_string(),
+                            p.size_string(marshal = 1), p.get_base_type_string(),
                             p.name, p.counter))
         out('};')
 
     def print_async_unmarshal(self, func):
-        out('static inline void')
+        out('void')
         out(('_mesa_unmarshal_{0}(struct gl_context *ctx, '
              'const struct marshal_cmd_{0} *cmd)').format(func.name))
         out('{')
         with indent():
             for p in func.fixed_params:
                 if p.count:
-                    out('const {0} * {1} = cmd->{1};'.format(
-                            p.get_base_type_string(), p.name))
+                    p_decl = '{0} * {1} = cmd->{1};'.format(
+                            p.get_base_type_string(), p.name)
                 else:
-                    out('const {0} {1} = cmd->{1};'.format(
-                            p.type_string(), p.name))
+                    p_decl = '{0} {1} = cmd->{1};'.format(
+                            p.type_string(), p.name)
+
+                if not p_decl.startswith('const '):
+                    # Declare all local function variables as const, even if
+                    # the original parameter is not const.
+                    p_decl = 'const ' + p_decl
+
+                out(p_decl)
+
             if func.variable_params:
                 for p in func.variable_params:
-                    out('const {0} * {1};'.format(
+                    out('{0} * {1};'.format(
                             p.get_base_type_string(), p.name))
                 out('const char *variable_data = (const char *) (cmd + 1);')
+                i = 1
                 for p in func.variable_params:
-                    out('{0} = (const {1} *) variable_data;'.format(
+                    out('{0} = ({1} *) variable_data;'.format(
                             p.name, p.get_base_type_string()))
-                    out('variable_data += {0};'.format(p.size_string(False)))
-            self.print_sync_call(func)
+
+                    if p.img_null_flag:
+                        out('if (cmd->{0}_null)'.format(p.name))
+                        with indent():
+                            out('{0} = NULL;'.format(p.name))
+                        if i < len(func.variable_params):
+                            out('else')
+                            with indent():
+                                out('variable_data += {0};'.format(p.size_string(False, marshal = 1)))
+                    elif i < len(func.variable_params):
+                        out('variable_data += {0};'.format(p.size_string(False, marshal = 1)))
+                    i += 1
+
+            self.print_sync_call(func, unmarshal = 1)
+        out('}')
+
+    def validate_count_or_fallback(self, func):
+        # Check that any counts for variable-length arguments might be < 0, in
+        # which case the command alloc or the memcpy would blow up before we
+        # get to the validation in Mesa core.
+        list = []
+        for p in func.parameters:
+            if p.is_variable_length():
+                list.append('{0}_size < 0'.format(p.name))
+                list.append('({0}_size > 0 && !{0})'.format(p.name))
+
+        if len(list) == 0:
+            return
+
+        list.append('(unsigned)cmd_size > MARSHAL_MAX_CMD_SIZE')
+
+        out('if (unlikely({0})) {{'.format(' || '.join(list)))
+        with indent():
+            out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func.name))
+            self.print_sync_dispatch(func)
+            out('return;')
         out('}')
 
     def print_async_marshal(self, func):
-        out('static void GLAPIENTRY')
+        out('void GLAPIENTRY')
         out('_mesa_marshal_{0}({1})'.format(
                 func.name, func.get_parameter_string()))
         out('{')
         with indent():
             out('GET_CURRENT_CONTEXT(ctx);')
+            for p in func.variable_params:
+                out('int {0}_size = {1};'.format(p.name, p.size_string(marshal = 1)))
+
             struct = 'struct marshal_cmd_{0}'.format(func.name)
             size_terms = ['sizeof({0})'.format(struct)]
             for p in func.variable_params:
-                size_terms.append(p.size_string())
-            out('size_t cmd_size = {0};'.format(' + '.join(size_terms)))
+                if p.img_null_flag:
+                    size_terms.append('({0} ? {0}_size : 0)'.format(p.name))
+                else:
+                    size_terms.append('{0}_size'.format(p.name))
+            out('int cmd_size = {0};'.format(' + '.join(size_terms)))
             out('{0} *cmd;'.format(struct))
 
-            out('debug_print_marshal("{0}");'.format(func.name))
+            self.validate_count_or_fallback(func)
 
-            out('if (cmd_size <= MARSHAL_MAX_CMD_SIZE) {')
-            with indent():
-                self.print_async_dispatch(func)
-            out('} else {')
-            with indent():
-                self.print_sync_dispatch(func)
-            out('}')
+            if func.marshal_sync:
+                out('if ({0}) {{'.format(func.marshal_sync))
+                with indent():
+                    out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func.name))
+                    self.print_sync_dispatch(func)
+                    out('return;')
+                out('}')
 
+        with indent():
+            self.print_async_dispatch(func)
         out('}')
 
     def print_async_body(self, func):
@@ -210,35 +323,22 @@ class PrintCode(gl_XML.gl_print_base):
         out('')
 
     def print_unmarshal_dispatch_cmd(self, api):
-        out('size_t')
-        out('_mesa_unmarshal_dispatch_cmd(struct gl_context *ctx, '
-            'const void *cmd)')
-        out('{')
+        out('const _mesa_unmarshal_func _mesa_unmarshal_dispatch[NUM_DISPATCH_CMD] = {')
         with indent():
-            out('const struct marshal_cmd_base *cmd_base = cmd;')
-            out('switch (cmd_base->cmd_id) {')
             for func in api.functionIterateAll():
                 flavor = func.marshal_flavor()
                 if flavor in ('skip', 'sync'):
                     continue
-                out('case DISPATCH_CMD_{0}:'.format(func.name))
-                with indent():
-                    out('debug_print_unmarshal("{0}");'.format(func.name))
-                    out(('_mesa_unmarshal_{0}(ctx, (const struct marshal_cmd_{0} *)'
-                         ' cmd);').format(func.name))
-                    out('break;')
-            out('default:')
-            with indent():
-                out('assert(!"Unrecognized command ID");')
-                out('break;')
-            out('}')
-            out('')
-            out('return cmd_base->cmd_size;')
-        out('}')
+                out('[DISPATCH_CMD_{0}] = (_mesa_unmarshal_func)_mesa_unmarshal_{0},'.format(func.name))
+        out('};')
         out('')
         out('')
 
     def print_create_marshal_table(self, api):
+        out('/* _mesa_create_marshal_table takes a long time to compile with -O2 */')
+        out('#if defined(__GNUC__) && !defined(__clang__)')
+        out('__attribute__((optimize("O1")))')
+        out('#endif')
         out('struct _glapi_table *')
         out('_mesa_create_marshal_table(const struct gl_context *ctx)')
         out('{')
@@ -253,7 +353,11 @@ class PrintCode(gl_XML.gl_print_base):
             for func in api.functionIterateAll():
                 if func.marshal_flavor() == 'skip':
                     continue
-                out('SET_{0}(table, _mesa_marshal_{0});'.format(func.name))
+                # Don't use the SET_* functions, because they increase compile time
+                # by 20 seconds (on Ryzen 1700X).
+                out('if (_gloffset_{0} >= 0)'.format(func.name))
+                out('   ((_glapi_proc *)(table))[_gloffset_{0}] = (_glapi_proc)_mesa_marshal_{0};'
+                    .format(func.name))
             out('')
             out('return table;')
         out('}')
@@ -261,22 +365,31 @@ class PrintCode(gl_XML.gl_print_base):
         out('')
 
     def printBody(self, api):
-        async_funcs = []
+        # The first file only contains the dispatch tables
+        if file_index == 0:
+            self.print_unmarshal_dispatch_cmd(api)
+            self.print_create_marshal_table(api)
+            return
+
+        # The remaining files contain the marshal and unmarshal functions
+        func_per_file = (len(api.functionIterateAll()) // (file_count - 1)) + 1
+        i = -1
         for func in api.functionIterateAll():
+            i += 1
+            if i // func_per_file != (file_index - 1):
+                continue
+
             flavor = func.marshal_flavor()
             if flavor in ('skip', 'custom'):
                 continue
             elif flavor == 'async':
                 self.print_async_body(func)
-                async_funcs.append(func)
             elif flavor == 'sync':
                 self.print_sync_body(func)
-        self.print_unmarshal_dispatch_cmd(api)
-        self.print_create_marshal_table(api)
 
 
 def show_usage():
-    print 'Usage: %s [-f input_file_name]' % sys.argv[0]
+    print('Usage: %s [-f input_file_name]' % sys.argv[0])
     sys.exit(1)
 
 
@@ -284,14 +397,19 @@ if __name__ == '__main__':
     file_name = 'gl_API.xml'
 
     try:
-        (args, trail) = getopt.getopt(sys.argv[1:], 'm:f:')
-    except Exception,e:
+        (args, trail) = getopt.getopt(sys.argv[1:], 'm:f:i:n:')
+    except Exception:
         show_usage()
 
     for (arg,val) in args:
         if arg == '-f':
             file_name = val
+        elif arg == '-i':
+            file_index = int(val)
+        elif arg == '-n':
+            file_count = int(val)
 
+    assert file_index < file_count
     printer = PrintCode()
 
     api = gl_XML.parse_GL_API(file_name, marshal_XML.marshal_item_factory())