Skip to content
Permalink
Browse files
First shot at reworking assembly parser to use preprocessor/lexer.
MUCH less hacky now. Probably a lot of bugs to weed out, still, though.
  • Loading branch information
icculus committed Feb 11, 2009
1 parent bf262fc commit 9129dcf5f04d89bf479706cf11f923d10a792f76
Showing with 536 additions and 783 deletions.
  1. +6 −6 assemble.c
  2. +3 −2 finderrors.c
  3. +22 −7 mojoshader.h
  4. +501 −768 mojoshader_assembler.c
  5. +4 −0 mojoshader_internal.h
@@ -11,7 +11,7 @@
#include <stdlib.h>
#include "mojoshader.h"

static int assemble(const char *buf, const char *outfile)
static int assemble(const char *buf, int len, const char *outfile)
{
FILE *io = fopen(outfile, "wb");
if (io == NULL)
@@ -23,7 +23,8 @@ static int assemble(const char *buf, const char *outfile)
const MOJOSHADER_parseData *pd;
int retval = 0;

pd = MOJOSHADER_assemble(buf, NULL, 0, NULL, 0, NULL, NULL, NULL);
pd = MOJOSHADER_assemble(buf, len, NULL, 0, NULL, 0, NULL, 0,
NULL, NULL, NULL, NULL, NULL);
if (pd->error_count > 0)
{
int i;
@@ -57,7 +58,7 @@ int main(int argc, char **argv)
int retval = 1;

if (argc != 3)
printf("\n\nUSAGE: %s <d3dasmfile> <outputfile>\n\n", argv[0]);
printf("\n\nUSAGE: %s <inputfile> <outputfile>\n\n", argv[0]);
else
{
const char *infile = argv[1];
@@ -68,14 +69,13 @@ int main(int argc, char **argv)
else
{
char *buf = (char *) malloc(1000000);
int rc = fread(buf, 1, 1000000-1, io);
int rc = fread(buf, 1, 1000000, io);
fclose(io);
if (rc == EOF)
printf(" ... fread('%s') failed.\n", infile);
else
{
buf[rc] = '\0';
if (assemble(buf, outfile))
if (assemble(buf, rc, outfile))
retval = 0;
else
remove(outfile);
@@ -71,7 +71,7 @@ static int do_file(const char *profile, const char *dname, const char *fn, int *
} // if

static unsigned char buf[1024 * 256];
int rc = fread(buf, 1, sizeof (buf)-1, io);
int rc = fread(buf, 1, sizeof (buf), io);
fclose(io);
if (rc == -1)
{
@@ -84,7 +84,8 @@ static int do_file(const char *profile, const char *dname, const char *fn, int *
const MOJOSHADER_parseData *a;

buf[rc] = '\0'; // make sure the source is null-terminated.
a = MOJOSHADER_assemble((char *) buf, 0, 0, 0, 0, 0, 0, 0);
a = MOJOSHADER_assemble((char *) buf, rc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

if (a->error_count > 0)
{
report("FAIL: %s (line %d) %s\n", fname, a->errors[0].error_position, a->errors[0].error);
@@ -808,8 +808,10 @@ void MOJOSHADER_freePreprocessData(const MOJOSHADER_preprocessData *data);
* This function is optional. Use this to convert Direct3D shader assembly
* language into bytecode, which can be handled by MOJOSHADER_parse().
*
* (source) is an ASCII, NULL-terminated string of valid Direct3D shader
* assembly source code.
* (source) is an ASCII string of valid Direct3D shader assembly source code.
* It does not need to be NULL-terminated.
*
* (sourcelen) is the length of the string pointed to by (source), in bytes.
*
* (comments) points to (comment_count) NULL-terminated ASCII strings, and
* can be NULL. These strings are inserted as comments in the bytecode.
@@ -822,7 +824,15 @@ void MOJOSHADER_freePreprocessData(const MOJOSHADER_preprocessData *data);
* at this time; it will not be checked to see if it matches what was
* assembled in any way whatsoever.
*
* This will return a MOJOSHADER_parseData(), like MOJOSHADER_parse() would,
* (defines) points to (define_count) preprocessor definitions, and can be
* NULL. These are treated by the preprocessor as if the source code started
* with one #define for each entry you pass in here.
*
* (include_open) and (include_close) let the app control the preprocessor's
* behaviour for #include statements. Both are optional and can be NULL, but
* both must be specified if either is specified.
*
* This will return a MOJOSHADER_parseData, like MOJOSHADER_parse() would,
* except the profile will be MOJOSHADER_PROFILE_BYTECODE and the output
* will be the assembled bytecode instead of some other language. This output
* can be pushed back through MOJOSHADER_parseData() with a different profile.
@@ -839,18 +849,23 @@ void MOJOSHADER_freePreprocessData(const MOJOSHADER_preprocessData *data);
* If your allocator needs instance-specific data, you may supply it with the
* (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
*
* This function is thread safe, so long as (m) and (f) are too, and that
* (source) remains intact for the duration of the call. This allows you
* to assemble several shaders on separate CPU cores at the same time.
* This function is thread safe, so long as the various callback functions
* are, too, and that the parameters remains intact for the duration of the
* call. This allows you to assemble several shaders on separate CPU cores
* at the same time.
*/
const MOJOSHADER_parseData *MOJOSHADER_assemble(const char *source,
unsigned int sourcelen,
const char **comments, unsigned int comment_count,
const MOJOSHADER_symbol *symbols,
unsigned int symbol_count,
const MOJOSHADER_preprocessorDefine **defines,
unsigned int define_count,
MOJOSHADER_includeOpen include_open,
MOJOSHADER_includeClose include_close,
MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);



/* OpenGL interface... */

/*

0 comments on commit 9129dcf

Please sign in to comment.