unpack.py revision 236b6e0fdf652661ff4c655314fe488998c5c17d
# Copyright (c) 2001, Stanford University
# All rights reserved.
#
# See the file LICENSE.txt for information on redistributing this software.
import sys
import apiutil
print """/* DO NOT EDIT! THIS CODE IS AUTOGENERATED BY unpack.py */
#include "unpacker.h"
#include "cr_opcodes.h"
#include "cr_error.h"
#include "cr_mem.h"
#include "cr_spu.h"
#include "unpack_extend.h"
#include <stdio.h>
#include <memory.h>
DECLEXPORT(const unsigned char *) cr_unpackData = NULL;
SPUDispatchTable cr_unpackDispatch;
static void crUnpackExtend(void);
static void crUnpackExtendDbg(void);
/*#define CR_UNPACK_DEBUG_OPCODES*/
/*#define CR_UNPACK_DEBUG_LAST_OPCODES*/
"""
nodebug_opcodes = [
"CR_MULTITEXCOORD2FARB_OPCODE",
"CR_VERTEX3F_OPCODE",
"CR_NORMAL3F_OPCODE",
"CR_COLOR4UB_OPCODE",
"CR_LOADIDENTITY_OPCODE",
"CR_MATRIXMODE_OPCODE",
"CR_LOADMATRIXF_OPCODE",
"CR_DISABLE_OPCODE",
"CR_COLOR4F_OPCODE",
"CR_ENABLE_OPCODE",
"CR_BEGIN_OPCODE",
"CR_END_OPCODE",
"CR_SECONDARYCOLOR3FEXT_OPCODE"
]
"CR_ACTIVETEXTUREARB_EXTEND_OPCODE"
]
#
# Useful functions
#
"""Emit a READ_DOUBLE or READ_DATA call for pulling a GL function
argument out of the buffer's operand area."""
else:
return retval
"""For GL functions that return values (either as the return value or
through a pointer parameter) emit a SET_RETURN_PTR call."""
if (return_type != 'void'):
else:
"""Emit a SET_WRITEBACK_PTR call."""
if return_type != 'void':
copy_of_params = params[:]
print '\tcrError( "%s needs to be special cased!" );' % func_name
else:
if return_type != "void":
print "\t(void)",
else:
print "\t",
"""Convert a call like glVertex3f to glVertex3fv."""
print "#ifdef CR_UNALIGNED_ACCESS_OKAY"
print "#else"
if return_type != "void":
print "\t(void) cr_unpackDispatch.%s(" % func_name,
else:
print "\tcr_unpackDispatch.%s(" % func_name,
print "v" + `index`,
print ",",
print ");"
print "#endif"
else:
#
# Generate unpack functions for all the simple functions.
#
continue
print "static void crUnpack%s(void)" % func_name
print "{"
else:
if packet_length == 0:
print "\tINCR_DATA_PTR_NO_ARGS( );"
else:
print "\tINCR_DATA_PTR( %d );" % packet_length
print "}\n"
#
# Emit some code
#
print """
typedef struct __dispatchNode {
const unsigned char *unpackData;
struct __dispatchNode *next;
} DispatchNode;
static DispatchNode *unpackStack = NULL;
static SPUDispatchTable *cr_lastDispatch = NULL;
void crUnpackPush(void)
{
DispatchNode *node = (DispatchNode*)crAlloc( sizeof( *node ) );
node->next = unpackStack;
unpackStack = node;
node->unpackData = cr_unpackData;
}
void crUnpackPop(void)
{
DispatchNode *node = unpackStack;
if (!node)
{
crError( "crUnpackPop called with an empty stack!" );
}
unpackStack = node->next;
cr_unpackData = node->unpackData;
crFree( node );
}
void crUnpack( const void *data, const void *opcodes,
unsigned int num_opcodes, SPUDispatchTable *table )
{
unsigned int i;
const unsigned char *unpack_opcodes;
if (table != cr_lastDispatch)
{
crSPUCopyDispatchTable( &cr_unpackDispatch, table );
cr_lastDispatch = table;
}
unpack_opcodes = (const unsigned char *)opcodes;
cr_unpackData = (const unsigned char *)data;
crDebug("crUnpack: %d opcodes", num_opcodes);
for (i = 0 ; i < num_opcodes ; i++)
{
/*crDebug(\"Unpacking opcode \%d\", *unpack_opcodes);*/
switch( *unpack_opcodes )
{"""
#
# Emit switch cases for all unextended opcodes
#
print """
#ifdef CR_UNPACK_DEBUG_LAST_OPCODES
if (i==(num_opcodes-1))
#endif
#if defined(CR_UNPACK_DEBUG_OPCODES) || defined(CR_UNPACK_DEBUG_LAST_OPCODES)
crDebug("Unpack: %s");
print '\t\t\t\tcrUnpack%s(); \n\t\t\t\tbreak;' % func_name
print """
case CR_EXTEND_OPCODE:
#ifdef CR_UNPACK_DEBUG_OPCODES
crUnpackExtendDbg();
#else
# ifdef CR_UNPACK_DEBUG_LAST_OPCODES
if (i==(num_opcodes-1)) crUnpackExtendDbg();
else
# endif
crUnpackExtend();
#endif
break;
default:
crError( "Unknown opcode: %d", *unpack_opcodes );
break;
}
unpack_opcodes--;
}
}"""
#
# Emit unpack functions for extended opcodes, non-special functions only.
#
print 'static void crUnpackExtend%s(void)' % func_name
print '{'
print '}\n'
print 'static void crUnpackExtend(void)'
print '{'
print ''
print '\t/*crDebug(\"Unpacking extended opcode \%d", extend_opcode);*/'
print '\tswitch( extend_opcode )'
print '\t{'
#
# Emit switch statement for extended opcodes
#
# print '\t\t\t\tcrDebug("Unpack: %s");' % apiutil.ExtendedOpcodeName( func_name )
print '\t\t\tcrUnpackExtend%s( );' % func_name
print '\t\t\tbreak;'
print """ default:
crError( "Unknown extended opcode: %d", (int) extend_opcode );
break;
}
INCR_VAR_PTR();
}"""
print 'static void crUnpackExtendDbg(void)'
print '{'
print ''
print '\t/*crDebug(\"Unpacking extended opcode \%d", extend_opcode);*/'
print '\tswitch( extend_opcode )'
print '\t{'
#
# Emit switch statement for extended opcodes
#
print '\t\t\tcrUnpackExtend%s( );' % func_name
print '\t\t\tbreak;'
print """ default:
crError( "Unknown extended opcode: %d", (int) extend_opcode );
break;
}
INCR_VAR_PTR();
}"""