Merge branch '7.8'
[mesa.git] / src / mesa / glapi / glapi_getproc.c
index 1401c1cb586ce5154b4c91ffe992da83a3f54323..c73e8dd3b040a7ddfbce7ef59400ee3824b3d9c1 100644 (file)
  */
 
 
-#include <stdlib.h>
-#include <string.h>
+#ifdef HAVE_DIX_CONFIG_H
+#include <dix-config.h>
+#include "glapi/mesa.h"
+#else
 #include "main/glheader.h"
 #include "main/compiler.h"
-#include "glapi.h"
-#include "glapioffsets.h"
-#include "glapitable.h"
-
+#endif
 
-static void
-fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset);
+#include "glapi/glapi.h"
+#include "glapi/glapi_priv.h"
+#include "glapi/glapitable.h"
+#include "glapi/glapioffsets.h"
 
 
-/**
- * strdup() is actually not a standard ANSI C or POSIX routine.
- * Irix will not define it if ANSI mode is in effect.
+/**********************************************************************
+ * Static function management.
  */
-static char *
-str_dup(const char *str)
-{
-   char *copy;
-   copy = (char*) malloc(strlen(str) + 1);
-   if (!copy)
-      return NULL;
-   strcpy(copy, str);
-   return copy;
-}
-
 
 
-#if defined(USE_X64_64_ASM) && defined(GLX_USE_TLS)
-# define DISPATCH_FUNCTION_SIZE  16
-#elif defined(USE_X86_ASM)
-# if defined(THREADS) && !defined(GLX_USE_TLS)
-#  define DISPATCH_FUNCTION_SIZE  32
-# else
-#  define DISPATCH_FUNCTION_SIZE  16
-# endif
-#endif
-
-#if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server) && !defined(XGLServer)
+#if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server)
 # define NEED_FUNCTION_POINTER
 #endif
-
-/* The code in this file is auto-generated with Python */
-#include "glprocs.h"
+#include "glapi/glprocs.h"
 
 
 /**
@@ -83,13 +60,13 @@ str_dup(const char *str)
  * and return the corresponding glprocs_table_t entry.
  */
 static const glprocs_table_t *
-find_entry( const char * n )
+get_static_proc( const char * n )
 {
    GLuint i;
    for (i = 0; static_functions[i].Name_offset >= 0; i++) {
       const char *testName = gl_string_table + static_functions[i].Name_offset;
 #ifdef MANGLE
-      /* skip the "m" prefix on the name */
+      /* skip the prefix on the name */
       if (strcmp(testName, n + 1) == 0)
 #else
       if (strcmp(testName, n) == 0)
@@ -109,26 +86,16 @@ find_entry( const char * n )
 static GLint
 get_static_proc_offset(const char *funcName)
 {
-   const glprocs_table_t * const f = find_entry( funcName );
-   if (f) {
-      return f->Offset;
+   const glprocs_table_t * const f = get_static_proc( funcName );
+   if (f == NULL) {
+      return -1;
    }
-   return -1;
-}
-
 
-#if !defined(XFree86Server) && !defined(XGLServer)
-#ifdef USE_X86_ASM
-
-#if defined( GLX_USE_TLS )
-extern       GLubyte gl_dispatch_functions_start[];
-extern       GLubyte gl_dispatch_functions_end[];
-#else
-extern const GLubyte gl_dispatch_functions_start[];
-#endif
+   return f->Offset;
+}
 
-#endif /* USE_X86_ASM */
 
+#if !defined(XFree86Server)
 
 /**
  * Return dispatch function address for the named static (built-in) function.
@@ -137,27 +104,32 @@ extern const GLubyte gl_dispatch_functions_start[];
 static _glapi_proc
 get_static_proc_address(const char *funcName)
 {
-   const glprocs_table_t * const f = find_entry( funcName );
-   if (f) {
+   const glprocs_table_t * const f = get_static_proc( funcName );
+   if (f == NULL) {
+      return NULL;
+   }
+
 #if defined(DISPATCH_FUNCTION_SIZE) && defined(GLX_INDIRECT_RENDERING)
-      return (f->Address == NULL)
-        ? (_glapi_proc) (gl_dispatch_functions_start
-                         + (DISPATCH_FUNCTION_SIZE * f->Offset))
-         : f->Address;
+   return (f->Address == NULL)
+      ? get_entrypoint_address(f->Offset)
+      : f->Address;
 #elif defined(DISPATCH_FUNCTION_SIZE)
-      return (_glapi_proc) (gl_dispatch_functions_start 
-                            + (DISPATCH_FUNCTION_SIZE * f->Offset));
+   return get_entrypoint_address(f->Offset);
 #else
-      return f->Address;
+   return f->Address;
 #endif
-   }
-   else {
-      return NULL;
-   }
 }
 
-#endif /* !defined(XFree86Server) && !defined(XGLServer) */
+#else
+
+static _glapi_proc
+get_static_proc_address(const char *funcName)
+{
+   (void) funcName;
+   return NULL;
+}
 
+#endif /* !defined(XFree86Server) */
 
 
 /**
@@ -231,100 +203,70 @@ struct _glapi_function {
 static struct _glapi_function ExtEntryTable[MAX_EXTENSION_FUNCS];
 static GLuint NumExtEntryPoints = 0;
 
-#ifdef USE_SPARC_ASM
-extern void __glapi_sparc_icache_flush(unsigned int *);
-#endif
 
-/**
- * Generate a dispatch function (entrypoint) which jumps through
- * the given slot number (offset) in the current dispatch table.
- * We need assembly language in order to accomplish this.
- */
-static _glapi_proc
-generate_entrypoint(GLuint functionOffset)
+static struct _glapi_function *
+get_extension_proc(const char *funcName)
 {
-#if defined(USE_X86_ASM)
-   /* 32 is chosen as something of a magic offset.  For x86, the dispatch
-    * at offset 32 is the first one where the offset in the
-    * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
-    */
-   const GLubyte * const template_func = gl_dispatch_functions_start 
-     + (DISPATCH_FUNCTION_SIZE * 32);
-   GLubyte * const code = (GLubyte *) malloc(DISPATCH_FUNCTION_SIZE);
+   GLuint i;
+   for (i = 0; i < NumExtEntryPoints; i++) {
+      if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
+         return & ExtEntryTable[i];
+      }
+   }
+   return NULL;
+}
 
 
-   if ( code != NULL ) {
-      (void) memcpy(code, template_func, DISPATCH_FUNCTION_SIZE);
-      fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );
+static GLint
+get_extension_proc_offset(const char *funcName)
+{
+   const struct _glapi_function * const f = get_extension_proc( funcName );
+   if (f == NULL) {
+      return -1;
    }
 
-   return (_glapi_proc) code;
-#elif defined(USE_SPARC_ASM) && (defined(PTHREADS) || defined(GLX_USE_TLS))
-   static const unsigned int template[] = {
-      0x07000000, /* sethi %hi(0), %g3 */
-      0x8210000f, /* mov  %o7, %g1 */
-      0x40000000, /* call */
-      0x9e100001, /* mov  %g1, %o7 */
-   };
-#ifdef GLX_USE_TLS
-   extern unsigned int __glapi_sparc_tls_stub;
-   unsigned long call_dest = (unsigned long ) &__glapi_sparc_tls_stub;
-#else
-   extern unsigned int __glapi_sparc_pthread_stub;
-   unsigned long call_dest = (unsigned long ) &__glapi_sparc_pthread_stub;
-#endif
-   unsigned int *code = (unsigned int *) malloc(sizeof(template));
-   if (code) {
-      code[0] = template[0] | (functionOffset & 0x3fffff);
-      code[1] = template[1];
-      __glapi_sparc_icache_flush(&code[0]);
-      code[2] = template[2] |
-         (((call_dest - ((unsigned long) &code[2]))
-          >> 2) & 0x3fffffff);
-      code[3] = template[3];
-      __glapi_sparc_icache_flush(&code[2]);
+   return f->dispatch_offset;
+}
+
+
+static _glapi_proc
+get_extension_proc_address(const char *funcName)
+{
+   const struct _glapi_function * const f = get_extension_proc( funcName );
+   if (f == NULL) {
+      return NULL;
+   }
+
+   return f->dispatch_stub;
+}
+
+
+static const char *
+get_extension_proc_name(GLuint offset)
+{
+   GLuint i;
+   for (i = 0; i < NumExtEntryPoints; i++) {
+      if (ExtEntryTable[i].dispatch_offset == offset) {
+         return ExtEntryTable[i].name;
+      }
    }
-   return (_glapi_proc) code;
-#else
-   (void) functionOffset;
    return NULL;
-#endif /* USE_*_ASM */
 }
 
 
 /**
- * This function inserts a new dispatch offset into the assembly language
- * stub that was generated with the preceeding function.
+ * strdup() is actually not a standard ANSI C or POSIX routine.
+ * Irix will not define it if ANSI mode is in effect.
  */
-static void
-fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
+static char *
+str_dup(const char *str)
 {
-#if defined(USE_X86_ASM)
-   GLubyte * const code = (GLubyte *) entrypoint;
-
-#if DISPATCH_FUNCTION_SIZE == 32
-   *((unsigned int *)(code + 11)) = 4 * offset;
-   *((unsigned int *)(code + 22)) = 4 * offset;
-#elif DISPATCH_FUNCTION_SIZE == 16 && defined( GLX_USE_TLS )
-   *((unsigned int *)(code +  8)) = 4 * offset;
-#elif DISPATCH_FUNCTION_SIZE == 16
-   *((unsigned int *)(code +  7)) = 4 * offset;
-#else
-# error Invalid DISPATCH_FUNCTION_SIZE!
-#endif
-
-#elif defined(USE_SPARC_ASM)
-   unsigned int *code = (unsigned int *) entrypoint;
-   code[0] &= ~0x3fffff;
-   code[0] |= (offset * sizeof(void *)) & 0x3fffff;
-   __glapi_sparc_icache_flush(&code[0]);
-#else
-
-   /* an unimplemented architecture */
-   (void) entrypoint;
-   (void) offset;
-
-#endif /* USE_*_ASM */
+   char *copy;
+   copy = (char*) malloc(strlen(str) + 1);
+   if (!copy)
+      return NULL;
+   strcpy(copy, str);
+   return copy;
 }
 
 
@@ -345,20 +287,55 @@ static struct _glapi_function *
 add_function_name( const char * funcName )
 {
    struct _glapi_function * entry = NULL;
-   
-   if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) {
-      _glapi_proc entrypoint = generate_entrypoint(~0);
-      if (entrypoint != NULL) {
-        entry = & ExtEntryTable[NumExtEntryPoints];
-
-        ExtEntryTable[NumExtEntryPoints].name = str_dup(funcName);
-        ExtEntryTable[NumExtEntryPoints].parameter_signature = NULL;
-        ExtEntryTable[NumExtEntryPoints].dispatch_offset = ~0;
-        ExtEntryTable[NumExtEntryPoints].dispatch_stub = entrypoint;
-        NumExtEntryPoints++;
-      }
+   _glapi_proc entrypoint = NULL;
+   char * name_dup = NULL;
+
+   if (NumExtEntryPoints >= MAX_EXTENSION_FUNCS)
+      return NULL;
+
+   if (funcName == NULL)
+      return NULL;
+
+   name_dup = str_dup(funcName);
+   if (name_dup == NULL)
+      return NULL;
+
+   entrypoint = generate_entrypoint(~0);
+
+   if (entrypoint == NULL) {
+      free(name_dup);
+      return NULL;
    }
 
+   entry = & ExtEntryTable[NumExtEntryPoints];
+   NumExtEntryPoints++;
+
+   entry->name = name_dup;
+   entry->parameter_signature = NULL;
+   entry->dispatch_offset = ~0;
+   entry->dispatch_stub = entrypoint;
+
+   return entry;
+}
+
+
+static struct _glapi_function *
+set_entry_info( struct _glapi_function * entry, const char * signature, unsigned offset )
+{
+   char * sig_dup = NULL;
+
+   if (signature == NULL)
+      return NULL;
+
+   sig_dup = str_dup(signature);
+   if (sig_dup == NULL)
+      return NULL;
+
+   fill_in_entrypoint_offset(entry->dispatch_stub, offset);
+
+   entry->parameter_signature = sig_dup;
+   entry->dispatch_offset = offset;
+
    return entry;
 }
 
@@ -420,88 +397,103 @@ _glapi_add_dispatch( const char * const * function_names,
    struct _glapi_function * entry[8];
    GLboolean is_static[8];
    unsigned i;
-   unsigned j;
    int offset = ~0;
-   int new_offset;
 
+   init_glapi_relocs_once();
 
    (void) memset( is_static, 0, sizeof( is_static ) );
    (void) memset( entry, 0, sizeof( entry ) );
 
+   /* Find the _single_ dispatch offset for all function names that already
+    * exist (and have a dispatch offset).
+    */
+
    for ( i = 0 ; function_names[i] != NULL ; i++ ) {
-      /* Do some trivial validation on the name of the function.
-       */
+      const char * funcName = function_names[i];
+      int static_offset;
+      int extension_offset;
 
-      if (!function_names[i] || function_names[i][0] != 'g' || function_names[i][1] != 'l')
+      if (funcName[0] != 'g' || funcName[1] != 'l')
          return -1;
-   
-      /* Determine if the named function already exists.  If the function does
-       * exist, it must have the same parameter signature as the function
-       * being added.
-       */
-
-      new_offset = get_static_proc_offset(function_names[i]);
-      if (new_offset >= 0) {
+
+      /* search built-in functions */
+      static_offset = get_static_proc_offset(funcName);
+
+      if (static_offset >= 0) {
+
+        is_static[i] = GL_TRUE;
+
         /* FIXME: Make sure the parameter signatures match!  How do we get
          * FIXME: the parameter signature for static functions?
          */
 
-        if ( (offset != ~0) && (new_offset != offset) ) {
+        if ( (offset != ~0) && (static_offset != offset) ) {
            return -1;
         }
 
-        is_static[i] = GL_TRUE;
-        offset = new_offset;
+        offset = static_offset;
+
+        continue;
       }
-   
-   
-      for ( j = 0 ; j < NumExtEntryPoints ; j++ ) {
-        if (strcmp(ExtEntryTable[j].name, function_names[i]) == 0) {
-           /* The offset may be ~0 if the function name was added by
-            * glXGetProcAddress but never filled in by the driver.
-            */
-
-           if (ExtEntryTable[j].dispatch_offset != ~0) {
-              if (strcmp(real_sig, ExtEntryTable[j].parameter_signature) 
-                  != 0) {
-                 return -1;
-              }
-
-              if ( (offset != ~0) && (ExtEntryTable[j].dispatch_offset != offset) ) {
-                 return -1;
-              }
-
-              offset = ExtEntryTable[j].dispatch_offset;
-           }
-           
-           entry[i] = & ExtEntryTable[j];
-           break;
+
+      /* search added extension functions */
+      entry[i] = get_extension_proc(funcName);
+
+      if (entry[i] != NULL) {
+        extension_offset = entry[i]->dispatch_offset;
+
+        /* The offset may be ~0 if the function name was added by
+         * glXGetProcAddress but never filled in by the driver.
+         */
+
+        if (extension_offset == ~0) {
+           continue;
+        }
+
+        if (strcmp(real_sig, entry[i]->parameter_signature) != 0) {
+           return -1;
+        }
+
+        if ( (offset != ~0) && (extension_offset != offset) ) {
+           return -1;
         }
+
+        offset = extension_offset;
       }
    }
 
+   /* If all function names are either new (or with no dispatch offset),
+    * allocate a new dispatch offset.
+    */
+
    if (offset == ~0) {
       offset = next_dynamic_offset;
       next_dynamic_offset++;
    }
 
+   /* Fill in the dispatch offset for the new function names (and those with
+    * no dispatch offset).
+    */
+
    for ( i = 0 ; function_names[i] != NULL ; i++ ) {
-      if (! is_static[i] ) {
+      if (is_static[i]) {
+        continue;
+      }
+
+      /* generate entrypoints for new function names */
+      if (entry[i] == NULL) {
+        entry[i] = add_function_name( function_names[i] );
         if (entry[i] == NULL) {
-           entry[i] = add_function_name( function_names[i] );
-           if (entry[i] == NULL) {
-              /* FIXME: Possible memory leak here.
-               */
-              return -1;
-           }
+           /* FIXME: Possible memory leak here. */
+           return -1;
         }
+      }
 
-        entry[i]->parameter_signature = str_dup(real_sig);
-        fill_in_entrypoint_offset(entry[i]->dispatch_stub, offset);
-        entry[i]->dispatch_offset = offset;
+      if (entry[i]->dispatch_offset == ~0) {
+        set_entry_info( entry[i], real_sig, offset );
       }
    }
-   
+
    return offset;
 }
 
@@ -512,13 +504,13 @@ _glapi_add_dispatch( const char * const * function_names,
 PUBLIC GLint
 _glapi_get_proc_offset(const char *funcName)
 {
+   GLint offset;
+
    /* search extension functions first */
-   GLuint i;
-   for (i = 0; i < NumExtEntryPoints; i++) {
-      if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
-         return ExtEntryTable[i].dispatch_offset;
-      }
-   }
+   offset = get_extension_proc_offset(funcName);
+   if (offset >= 0)
+      return offset;
+
    /* search static functions */
    return get_static_proc_offset(funcName);
 }
@@ -533,11 +525,14 @@ _glapi_get_proc_offset(const char *funcName)
 PUBLIC _glapi_proc
 _glapi_get_proc_address(const char *funcName)
 {
+   _glapi_proc func;
    struct _glapi_function * entry;
-   GLuint i;
+
+   init_glapi_relocs_once();
 
 #ifdef MANGLE
-   if (funcName[0] != 'm' || funcName[1] != 'g' || funcName[2] != 'l')
+   /* skip the prefix on the name */
+   if (funcName[1] != 'g' || funcName[2] != 'l')
       return NULL;
 #else
    if (funcName[0] != 'g' || funcName[1] != 'l')
@@ -545,23 +540,21 @@ _glapi_get_proc_address(const char *funcName)
 #endif
 
    /* search extension functions first */
-   for (i = 0; i < NumExtEntryPoints; i++) {
-      if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
-         return ExtEntryTable[i].dispatch_stub;
-      }
-   }
+   func = get_extension_proc_address(funcName);
+   if (func)
+      return func;
 
-#if !defined( XFree86Server ) && !defined( XGLServer )
    /* search static functions */
-   {
-      const _glapi_proc func = get_static_proc_address(funcName);
-      if (func)
-         return func;
-   }
-#endif /* !defined( XFree86Server ) */
+   func = get_static_proc_address(funcName);
+   if (func)
+      return func;
 
+   /* generate entrypoint, dispatch offset must be filled in by the driver */
    entry = add_function_name(funcName);
-   return (entry == NULL) ? NULL : entry->dispatch_stub;
+   if (entry == NULL)
+      return NULL;
+
+   return entry->dispatch_stub;
 }
 
 
@@ -573,7 +566,6 @@ _glapi_get_proc_address(const char *funcName)
 const char *
 _glapi_get_proc_name(GLuint offset)
 {
-   GLuint i;
    const char * n;
 
    /* search built-in functions */
@@ -583,10 +575,121 @@ _glapi_get_proc_name(GLuint offset)
    }
 
    /* search added extension functions */
-   for (i = 0; i < NumExtEntryPoints; i++) {
-      if (ExtEntryTable[i].dispatch_offset == offset) {
-         return ExtEntryTable[i].name;
-      }
+   return get_extension_proc_name(offset);
+}
+
+
+
+/**********************************************************************
+ * GL API table functions.
+ */
+
+
+/*
+ * The dispatch table size (number of entries) is the size of the
+ * _glapi_table struct plus the number of dynamic entries we can add.
+ * The extra slots can be filled in by DRI drivers that register new extension
+ * functions.
+ */
+#define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
+
+
+/**
+ * Return size of dispatch table struct as number of functions (or
+ * slots).
+ */
+PUBLIC GLuint
+_glapi_get_dispatch_table_size(void)
+{
+   return DISPATCH_TABLE_SIZE;
+}
+
+
+/**
+ * Make sure there are no NULL pointers in the given dispatch table.
+ * Intended for debugging purposes.
+ */
+void
+_glapi_check_table_not_null(const struct _glapi_table *table)
+{
+#ifdef EXTRA_DEBUG /* set to DEBUG for extra DEBUG */
+   const GLuint entries = _glapi_get_dispatch_table_size();
+   const void **tab = (const void **) table;
+   GLuint i;
+   for (i = 1; i < entries; i++) {
+      assert(tab[i]);
    }
-   return NULL;
+#else
+   (void) table;
+#endif
+}
+
+
+/**
+ * Do some spot checks to be sure that the dispatch table
+ * slots are assigned correctly. For debugging only.
+ */
+void
+_glapi_check_table(const struct _glapi_table *table)
+{
+#ifdef EXTRA_DEBUG /* set to DEBUG for extra DEBUG */
+   {
+      GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
+      char *BeginFunc = (char*) &table->Begin;
+      GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
+      assert(BeginOffset == _gloffset_Begin);
+      assert(BeginOffset == offset);
+   }
+   {
+      GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
+      char *viewportFunc = (char*) &table->Viewport;
+      GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
+      assert(viewportOffset == _gloffset_Viewport);
+      assert(viewportOffset == offset);
+   }
+   {
+      GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
+      char *VertexPointerFunc = (char*) &table->VertexPointer;
+      GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
+      assert(VertexPointerOffset == _gloffset_VertexPointer);
+      assert(VertexPointerOffset == offset);
+   }
+   {
+      GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
+      char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
+      GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
+      assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
+      assert(ResetMinMaxOffset == offset);
+   }
+   {
+      GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
+      char *blendColorFunc = (char*) &table->BlendColor;
+      GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
+      assert(blendColorOffset == _gloffset_BlendColor);
+      assert(blendColorOffset == offset);
+   }
+   {
+      GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
+      char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
+      GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
+      assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
+      assert(secondaryColor3fOffset == offset);
+   }
+   {
+      GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
+      char *pointParameterivFunc = (char*) &table->PointParameterivNV;
+      GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
+      assert(pointParameterivOffset == _gloffset_PointParameterivNV);
+      assert(pointParameterivOffset == offset);
+   }
+   {
+      GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
+      char *setFenceFunc = (char*) &table->SetFenceNV;
+      GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
+      assert(setFenceOffset == _gloffset_SetFenceNV);
+      assert(setFenceOffset == offset);
+   }
+#else
+   (void) table;
+#endif
 }