mojoshader_assembler.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 13 Feb 2009 01:41:47 -0500
changeset 603 c7a964b1b0eb
parent 598 decc32dc03a7
child 606 63e7a66ac320
permissions -rw-r--r--
More minor comment tweaks.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/**
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *  Direct3D shaders.
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 *
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *  This file written by Ryan C. Gordon.
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 */
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
#define __MOJOSHADER_INTERNAL__ 1
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include "mojoshader_internal.h"
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
587
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    13
#if DEBUG_ASSEMBLY_PARSER
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    14
    #define print_debug_token(token, len, val) \
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    15
        MOJOSHADER_print_debug_token("ASSEMBLER", token, len, val)
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    16
#else
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    17
    #define print_debug_token(token, len, val)
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    18
#endif
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
    19
566
6bd82a5acf62 Added more debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 562
diff changeset
    20
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    21
// Simple linked list to cache source filenames, so we don't have to copy
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    22
//  the same string over and over for each opcode.
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    23
typedef struct FilenameCache
514
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
    24
{
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    25
    char *filename;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    26
    struct FilenameCache *next;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    27
} FilenameCache;
514
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
    28
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
    29
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
    30
typedef struct SourcePos
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
    31
{
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
    32
    const char *filename;
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
    33
    uint32 line;
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
    34
} SourcePos;
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
    35
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
// Context...this is state that changes as we assemble a shader...
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
    38
typedef struct Context
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
{
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    40
    int isfail;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    41
    int out_of_memory;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    MOJOSHADER_malloc malloc;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    MOJOSHADER_free free;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
    void *malloc_data;
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    45
    int error_count;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    46
    ErrorList *errors;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    47
    Preprocessor *preprocessor;
523
699696afd731 Allow errors to specify post-processing problems.
Ryan C. Gordon <icculus@icculus.org>
parents: 522
diff changeset
    48
    MOJOSHADER_parsePhase parse_phase;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    MOJOSHADER_shaderType shader_type;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    uint8 major_ver;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    uint8 minor_ver;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    52
    int pushedback;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    53
    const char *token;      // assembler token!
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    54
    unsigned int tokenlen;  // assembler token!
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    55
    Token tokenval;         // assembler token!
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    56
    uint32 version_token;   // bytecode token!
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    57
    uint32 tokenbuf[16];    // bytecode tokens!
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    58
    int tokenbufpos;        // bytecode tokens!
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
    DestArgInfo dest_arg;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
    60
    uint32 *output;
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
    61
    SourcePos *token_to_source;
524
03eea2f0762c First (incomplete!) shot at more robust CTAB support.
Ryan C. Gordon <icculus@icculus.org>
parents: 523
diff changeset
    62
    uint8 *ctab;
03eea2f0762c First (incomplete!) shot at more robust CTAB support.
Ryan C. Gordon <icculus@icculus.org>
parents: 523
diff changeset
    63
    uint32 ctab_len;
03eea2f0762c First (incomplete!) shot at more robust CTAB support.
Ryan C. Gordon <icculus@icculus.org>
parents: 523
diff changeset
    64
    uint32 ctab_allocation;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
    65
    size_t output_len;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
    66
    size_t output_allocation;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
    67
    FilenameCache *filename_cache;
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
    68
} Context;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
// Convenience functions for allocators...
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    73
static inline void out_of_memory(Context *ctx)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
{
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    75
    ctx->isfail = ctx->out_of_memory = 1;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
} // out_of_memory
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
static inline void *Malloc(Context *ctx, const size_t len)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    void *retval = ctx->malloc((int) len, ctx->malloc_data);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    if (retval == NULL)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
        out_of_memory(ctx);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    return retval;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
} // Malloc
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    86
static inline char *StrDup(Context *ctx, const char *str)
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    87
{
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    88
    char *retval = (char *) Malloc(ctx, strlen(str) + 1);
554
42dd28107cd8 Simplify StrDup().
Ryan C. Gordon <icculus@icculus.org>
parents: 553
diff changeset
    89
    if (retval != NULL)
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    90
        strcpy(retval, str);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    91
    return retval;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    92
} // StrDup
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
    93
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
static inline void Free(Context *ctx, void *ptr)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    if (ptr != NULL)  // check for NULL in case of dumb free() impl.
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
        ctx->free(ptr, ctx->malloc_data);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
} // Free
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   100
static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   101
static void failf(Context *ctx, const char *fmt, ...)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
{
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   103
    const char *fname = NULL;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   104
    unsigned int linenum = 0;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   105
    int error_position = 0;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   106
550
2f977a75d2b5 Fixed error reporting in assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 549
diff changeset
   107
    ctx->isfail = 1;
2f977a75d2b5 Fixed error reporting in assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 549
diff changeset
   108
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   109
    switch (ctx->parse_phase)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    {
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   111
        case MOJOSHADER_PARSEPHASE_NOTSTARTED:
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   112
            error_position = -2;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   113
            break;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   114
        case MOJOSHADER_PARSEPHASE_WORKING:
567
1a61d0cf86ba Fixed assembler error line numbers.
Ryan C. Gordon <icculus@icculus.org>
parents: 566
diff changeset
   115
            fname = preprocessor_sourcepos(ctx->preprocessor, &linenum);
1a61d0cf86ba Fixed assembler error line numbers.
Ryan C. Gordon <icculus@icculus.org>
parents: 566
diff changeset
   116
            error_position = (int) linenum;
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   117
            break;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   118
        case MOJOSHADER_PARSEPHASE_DONE:
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   119
            error_position = -1;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   120
            break;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   121
        default:
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   122
            assert(0 && "Unexpected value");
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   123
            return;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   124
    } // switch
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   125
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   126
    ErrorList *error = (ErrorList *) Malloc(ctx, sizeof (ErrorList));
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   127
    if (error == NULL)
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   128
        return;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   129
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   130
    char scratch = 0;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   131
    va_list ap;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   132
    va_start(ap, fmt);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   133
    const int len = vsnprintf(&scratch, sizeof (scratch), fmt, ap);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   134
    va_end(ap);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   135
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   136
    char *failstr = (char *) Malloc(ctx, len + 1);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   137
    if (failstr == NULL)
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   138
        Free(ctx, error);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   139
    else
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   140
    {
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
        va_start(ap, fmt);
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   142
        vsnprintf(failstr, len + 1, fmt, ap);  // rebuild it.
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
        va_end(ap);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   145
        error->error.error = failstr;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   146
        error->error.filename = fname ? StrDup(ctx, fname) : NULL;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   147
        error->error.error_position = error_position;
544
f829f48e5214 Fixed stupid linked list bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 543
diff changeset
   148
        error->next = NULL;
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   149
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   150
        ErrorList *prev = NULL;
544
f829f48e5214 Fixed stupid linked list bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 543
diff changeset
   151
        ErrorList *item = ctx->errors;
f829f48e5214 Fixed stupid linked list bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 543
diff changeset
   152
        while (item != NULL)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
        {
544
f829f48e5214 Fixed stupid linked list bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 543
diff changeset
   154
            prev = item;
558
314c86ff14dd Fixed stupid linked list bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 554
diff changeset
   155
            item = item->next;
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   156
        } // while
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
544
f829f48e5214 Fixed stupid linked list bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 543
diff changeset
   158
        if (prev == NULL)
f829f48e5214 Fixed stupid linked list bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 543
diff changeset
   159
            ctx->errors = error;
f829f48e5214 Fixed stupid linked list bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 543
diff changeset
   160
        else
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   161
            prev->next = error;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   162
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   163
        ctx->error_count++;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   164
    } // else
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
} // failf
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   167
static inline void fail(Context *ctx, const char *reason)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
{
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   169
    failf(ctx, "%s", reason);
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
} // fail
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
static inline int isfail(const Context *ctx)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
{
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   174
    return ctx->isfail;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
} // isfail
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
// Shader model version magic...
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
static inline uint32 ver_ui32(const uint8 major, const uint8 minor)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
    return ( (((uint32) major) << 16) | (((minor) == 0xFF) ? 0 : (minor)) );
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
} // version_ui32
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
static inline int shader_version_atleast(const Context *ctx, const uint8 maj,
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
                                         const uint8 min)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
    return (ver_ui32(ctx->major_ver, ctx->minor_ver) >= ver_ui32(maj, min));
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
} // shader_version_atleast
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
static inline int shader_is_pixel(const Context *ctx)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    return (ctx->shader_type == MOJOSHADER_TYPE_PIXEL);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
} // shader_is_pixel
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
static inline int shader_is_vertex(const Context *ctx)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
    return (ctx->shader_type == MOJOSHADER_TYPE_VERTEX);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
} // shader_is_vertex
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   201
static inline void pushback(Context *ctx)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   202
{
566
6bd82a5acf62 Added more debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 562
diff changeset
   203
    #if DEBUG_ASSEMBLY_PARSER
6bd82a5acf62 Added more debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 562
diff changeset
   204
    printf("ASSEMBLER PUSHBACK\n");
6bd82a5acf62 Added more debug output.
Ryan C. Gordon <icculus@icculus.org>
parents: 562
diff changeset
   205
    #endif
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   206
    assert(!ctx->pushedback);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   207
    ctx->pushedback = 1;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   208
} // pushback
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   210
static Token _nexttoken(Context *ctx)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   211
{
598
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   212
    while (1)
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   213
    {
598
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   214
        ctx->token = preprocessor_nexttoken(ctx->preprocessor, &ctx->tokenlen,
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   215
                                            &ctx->tokenval);
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   216
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   217
        if (preprocessor_outofmemory(ctx->preprocessor))
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   218
        {
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   219
            out_of_memory(ctx);
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   220
            ctx->tokenval = TOKEN_EOI;
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   221
            ctx->token = NULL;
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   222
            ctx->tokenlen = 0;
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   223
            break;
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   224
        } // if
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   225
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   226
        if (ctx->tokenval == TOKEN_PREPROCESSING_ERROR)
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   227
        {
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   228
            fail(ctx, ctx->token);
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   229
            continue;
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   230
        } // else
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   231
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   232
        break;
decc32dc03a7 Removed preprocessor_error(). Returns a Token to specify an error state now.
Ryan C. Gordon <icculus@icculus.org>
parents: 587
diff changeset
   233
    } // while
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   234
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   235
    return ctx->tokenval;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   236
} // _nexttoken
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   237
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   238
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   239
static Token nexttoken(Context *ctx)
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   240
{
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   241
    if (ctx->pushedback)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   242
    {
587
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   243
        print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval);
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   244
        ctx->pushedback = 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   245
        return ctx->tokenval;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   246
    } // if
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   247
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   248
    Token token = _nexttoken(ctx);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   249
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   250
    while (token == ((Token) '\n'))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   251
        token = _nexttoken(ctx);  // skip endlines.
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   252
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   253
    if (token == ((Token) ';'))  // single line comment in assembler.
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   254
    {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   255
        do
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   256
        {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   257
            token = _nexttoken(ctx);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   258
        } while ((token != ((Token) '\n')) && (token != TOKEN_EOI));
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   259
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   260
        while (token == ((Token) '\n'))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   261
            token = _nexttoken(ctx);  // skip endlines.
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   262
    } // if
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   263
587
202354e004fc Unified some cut-and-paste code.
Ryan C. Gordon <icculus@icculus.org>
parents: 586
diff changeset
   264
    print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval);
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   265
    return token;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   266
} // nexttoken
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   267
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   268
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   269
static const char *cache_filename(Context *ctx, const char *fname)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   270
{
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   271
    if (fname == NULL)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   272
        return NULL;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   273
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   274
    // !!! FIXME: this could be optimized into a hash table, but oh well.
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   275
    FilenameCache *item = ctx->filename_cache;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   276
    while (item != NULL)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   277
    {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   278
        if (strcmp(item->filename, fname) == 0)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   279
            return item->filename;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   280
        item = item->next;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   281
    } // while
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   282
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   283
    // new cache item.
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   284
    item = (FilenameCache *) Malloc(ctx, sizeof (FilenameCache));
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   285
    if (item == NULL)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   286
        return NULL;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   287
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   288
    item->filename = (char *) Malloc(ctx, strlen(fname) + 1);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   289
    if (item->filename == NULL)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   290
    {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   291
        Free(ctx, item);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   292
        return NULL;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   293
    } // if
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   294
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   295
    strcpy(item->filename, fname);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   296
    item->next = ctx->filename_cache;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   297
    ctx->filename_cache = item;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   298
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   299
    return item->filename;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   300
} // cache_filename
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   301
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   302
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   303
static void free_filename_cache(Context *ctx)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   304
{
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   305
    FilenameCache *item = ctx->filename_cache;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   306
    while (item != NULL)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   307
    {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   308
        FilenameCache *next = item->next;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   309
        Free(ctx, item->filename);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   310
        Free(ctx, item);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   311
        item = next;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   312
    } // while
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   313
} // free_filename_cache
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   314
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
   316
static inline void add_token_sourcepos(Context *ctx, const size_t idx)
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
   317
{
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   318
    unsigned int pos = 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   319
    const char *fname = preprocessor_sourcepos(ctx->preprocessor, &pos);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   320
    ctx->token_to_source[idx].line = pos;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   321
    ctx->token_to_source[idx].filename = cache_filename(ctx, fname);
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
   322
} // add_token_sourcepos
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
   323
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
   324
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
static void output_token_noswap(Context *ctx, const uint32 token)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
    if (isfail(ctx))
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
        return;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   330
    if (ctx->output_len >= ctx->output_allocation)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   331
    {
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   332
        const size_t output_alloc_bump = 1024;  // that's tokens, not bytes.
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   333
        const size_t newsize = ctx->output_allocation + output_alloc_bump;
486
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   334
        void *ptr;
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   335
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   336
        ptr = Malloc(ctx, newsize * sizeof (uint32));
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   337
        if (ptr == NULL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   338
            return;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   339
        if (ctx->output_len > 0)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   340
            memcpy(ptr, ctx->output, ctx->output_len * sizeof (uint32));
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   341
        Free(ctx, ctx->output);
486
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   342
        ctx->output = (uint32 *) ptr;
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   343
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
   344
        ptr = Malloc(ctx, newsize * sizeof (SourcePos));
486
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   345
        if (ptr == NULL)
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   346
            return;
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   347
        if (ctx->output_len > 0)
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
   348
            memcpy(ptr, ctx->token_to_source, ctx->output_len * sizeof (SourcePos));
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
   349
        Free(ctx, ctx->token_to_source);
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
   350
        ctx->token_to_source = (SourcePos *) ptr;
486
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   351
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   352
        ctx->output_allocation = newsize;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   353
    } // if
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   354
486
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   355
    ctx->output[ctx->output_len] = token;
536
5af65fe6e917 Allow multiple errors from parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 532
diff changeset
   356
    add_token_sourcepos(ctx, ctx->output_len);
486
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   357
    ctx->output_len++;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
} // output_token_noswap
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
static inline void output_token(Context *ctx, const uint32 token)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
    output_token_noswap(ctx, SWAP32(token));
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
} // output_token
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
static void output_comment_bytes(Context *ctx, const uint8 *buf, size_t len)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    if (len > (0xFFFF * 4))  // length is stored as token count, in 16 bits.
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
        fail(ctx, "Comment field is too big");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
    else if (!isfail(ctx))
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
        const uint32 tokencount = (len / 4) + ((len % 4) ? 1 : 0);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
        output_token(ctx, 0xFFFE | (tokencount << 16));
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
        while (len >= 4)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
            output_token_noswap(ctx, *((const uint32 *) buf));
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
            len -= 4;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
            buf += 4;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
        } // while
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
        if (len > 0)  // handle spillover...
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
            union { uint8 ui8[4]; uint32 ui32; } overflow;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
            overflow.ui32 = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
            memcpy(overflow.ui8, buf, len);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
            output_token_noswap(ctx, overflow.ui32);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
    } // else if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
} // output_comment_bytes
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
static inline void output_comment_string(Context *ctx, const char *str)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
    output_comment_bytes(ctx, (const uint8 *) str, strlen(str));
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
} // output_comment_string
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   399
static int require_comma(Context *ctx)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
{
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   401
    const Token token = nexttoken(ctx);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   402
    if (token != ((Token) ','))
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   403
    {
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   404
        fail(ctx, "Comma expected");
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   405
        return 0;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   406
    } // if
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   407
    return 1;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   408
} // require_comma
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   411
static int check_token_segment(Context *ctx, const char *str)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   412
{
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   413
    // !!! FIXME: these are case-insensitive, right?
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   414
    const size_t len = strlen(str);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   415
    if ( (ctx->tokenlen < len) || (strncasecmp(ctx->token, str, len) != 0) )
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   416
        return 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   417
    ctx->token += len;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   418
    ctx->tokenlen -= len;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   419
    return 1;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   420
} // check_token_segment
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   421
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   422
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   423
static int check_token(Context *ctx, const char *str)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   424
{
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   425
    const size_t len = strlen(str);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   426
    if ( (ctx->tokenlen != len) || (strncasecmp(ctx->token, str, len) != 0) )
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   427
        return 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   428
    ctx->token += len;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   429
    ctx->tokenlen = 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   430
    return 1;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   431
} // check_token
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   432
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   433
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   434
static int ui32fromtoken(Context *ctx, uint32 *_val)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   435
{
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   436
    int i;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   437
    for (i = 0; i < ctx->tokenlen; i++)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   438
    {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   439
        if ((ctx->token[i] < '0') || (ctx->token[i] > '9'))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   440
            break;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   441
    } // for
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   442
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   443
    if (i == 0)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   444
    {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   445
        *_val = 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   446
        return 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   447
    } // if
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   448
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   449
    const int len = i;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   450
    uint32 val = 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   451
    uint32 mult = 1;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   452
    while (i--)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   453
    {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   454
        val += ((uint32) (ctx->token[i] - '0')) * mult;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   455
        mult *= 10;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   456
    } // while
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   457
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   458
    ctx->token += len;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   459
    ctx->tokenlen -= len;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   460
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   461
    *_val = val;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   462
    return 1;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   463
} // ui32fromtoken
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   464
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   465
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
static int parse_register_name(Context *ctx, RegisterType *rtype, int *rnum)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
{
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   468
    if (nexttoken(ctx) != TOKEN_IDENTIFIER)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   469
    {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   470
        fail(ctx, "Expected register");
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   471
        return 0;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   472
    } // if
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
    int neednum = 1;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
    int regnum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
    RegisterType regtype = REG_TYPE_TEMP;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   477
573
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   478
    // Watch out for substrings! oDepth must be checked before oD, since
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   479
    //  the latter will match either case.
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   480
    if (check_token_segment(ctx, "oDepth"))
513
abd9c85ba168 oDepth register doesn't have an index.
Ryan C. Gordon <icculus@icculus.org>
parents: 512
diff changeset
   481
    {
abd9c85ba168 oDepth register doesn't have an index.
Ryan C. Gordon <icculus@icculus.org>
parents: 512
diff changeset
   482
        regtype = REG_TYPE_DEPTHOUT;
abd9c85ba168 oDepth register doesn't have an index.
Ryan C. Gordon <icculus@icculus.org>
parents: 512
diff changeset
   483
        neednum = 0;
abd9c85ba168 oDepth register doesn't have an index.
Ryan C. Gordon <icculus@icculus.org>
parents: 512
diff changeset
   484
    } // else if
573
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   485
    else if (check_token_segment(ctx, "vFace"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
    {
573
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   487
        regtype = REG_TYPE_MISCTYPE;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   488
        regnum = (int) MISCTYPE_TYPE_FACE;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
        neednum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
    } // else if
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   491
    else if (check_token_segment(ctx, "vPos"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
        regtype = REG_TYPE_MISCTYPE;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
        regnum = (int) MISCTYPE_TYPE_POSITION;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
        neednum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
    } // else if
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   497
    else if (check_token_segment(ctx, "oPos"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
        regtype = REG_TYPE_RASTOUT;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
        regnum = (int) RASTOUT_TYPE_POSITION;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
        neednum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
    } // else if
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   503
    else if (check_token_segment(ctx, "oFog"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
        regtype = REG_TYPE_RASTOUT;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
        regnum = (int) RASTOUT_TYPE_FOG;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
        neednum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
    } // else if
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   509
    else if (check_token_segment(ctx, "oPts"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
        regtype = REG_TYPE_RASTOUT;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
        regnum = (int) RASTOUT_TYPE_POINT_SIZE;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
        neednum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
    } // else if
573
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   515
    else if (check_token_segment(ctx, "aL"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   516
    {
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   517
        regtype = REG_TYPE_LOOP;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   518
        neednum = 0;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   519
    } // else if
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   520
    else if (check_token_segment(ctx, "oC"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   521
        regtype = REG_TYPE_COLOROUT;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   522
    else if (check_token_segment(ctx, "oT"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   523
        regtype = REG_TYPE_OUTPUT;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   524
    else if (check_token_segment(ctx, "oD"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   525
        regtype = REG_TYPE_ATTROUT;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   526
    else if (check_token_segment(ctx, "r"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   527
        regtype = REG_TYPE_TEMP;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   528
    else if (check_token_segment(ctx, "v"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   529
        regtype = REG_TYPE_INPUT;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   530
    else if (check_token_segment(ctx, "c"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   531
        regtype = REG_TYPE_CONST;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   532
    else if (check_token_segment(ctx, "i"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   533
        regtype = REG_TYPE_CONSTINT;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   534
    else if (check_token_segment(ctx, "b"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   535
        regtype = REG_TYPE_CONSTBOOL;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   536
    else if (check_token_segment(ctx, "s"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   537
        regtype = REG_TYPE_SAMPLER;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   538
    else if (check_token_segment(ctx, "l"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   539
        regtype = REG_TYPE_LABEL;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   540
    else if (check_token_segment(ctx, "p"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   541
        regtype = REG_TYPE_PREDICATE;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   542
    else if (check_token_segment(ctx, "o"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   543
        regtype = REG_TYPE_OUTPUT;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   544
    else if (check_token_segment(ctx, "a"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   545
        regtype = REG_TYPE_ADDRESS;
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   546
    else if (check_token_segment(ctx, "t"))
1cd1d99a79cb Rearrange how we test for register names to avoid substring matches.
Ryan C. Gordon <icculus@icculus.org>
parents: 572
diff changeset
   547
        regtype = REG_TYPE_ADDRESS;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
        
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
    //case REG_TYPE_TEMPFLOAT16:  // !!! FIXME: don't know this asm string
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
    else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
    {
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   553
        fail(ctx, "expected register type");
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   554
        regtype = REG_TYPE_CONST;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   555
        regnum = 0;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   556
        neednum = 0;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
    } // else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
570
af6bb8728f9e Fixed register name parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 567
diff changeset
   559
    // "c[5]" is the same as "c5", so if the token is done, see if next is '['.
af6bb8728f9e Fixed register name parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 567
diff changeset
   560
    if ((neednum) && (ctx->tokenlen == 0))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
    {
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   562
        if (nexttoken(ctx) == ((Token) '['))
570
af6bb8728f9e Fixed register name parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 567
diff changeset
   563
            neednum = 0;  // don't need a number on register name itself.
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   564
        pushback(ctx);
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   565
    } // if
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   566
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   567
    if (neednum)
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   568
    {
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   569
        uint32 ui32 = 0;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   570
        if (!ui32fromtoken(ctx, &ui32))
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   571
            fail(ctx, "Invalid register index");
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   572
        regnum = (int) ui32;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
    } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
    // split up REG_TYPE_CONST
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
    if (regtype == REG_TYPE_CONST)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
        if (regnum < 2048)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
            regtype = REG_TYPE_CONST;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
            regnum -= 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
        else if (regnum < 4096)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
            regtype = REG_TYPE_CONST2;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
            regnum -= 2048;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
        else if (regnum < 6144)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
            regtype = REG_TYPE_CONST3;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
            regnum -= 4096;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
        else if (regnum < 8192)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   594
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   595
            regtype = REG_TYPE_CONST4;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
            regnum -= 6144;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
        else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
        {
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   600
            fail(ctx, "Invalid const register index");
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601
        } // else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
    } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   604
    *rtype = regtype;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
    *rnum = regnum;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   607
    return 1;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
} // parse_register_name
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   609
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   610
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   611
static void set_result_shift(Context *ctx, DestArgInfo *info, const int val)
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   612
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   613
    if (info->result_shift != 0)
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   614
        fail(ctx, "Multiple result shift modifiers");
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   615
    info->result_shift = val;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   616
} // set_result_shift
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   617
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   618
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   619
static int parse_destination_token(Context *ctx)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
{
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   621
    DestArgInfo *info = &ctx->dest_arg;
510
f4433db86f6e Fixed wrong sizeof for a memset() call.
Ryan C. Gordon <icculus@icculus.org>
parents: 508
diff changeset
   622
    memset(info, '\0', sizeof (DestArgInfo));
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   623
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   624
    // parse_instruction_token() sets ctx->token to the end of the instruction
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   625
    //  so we can see if there are destination modifiers on the instruction
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   626
    //  itself...
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   627
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   628
    int invalid_modifier = 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   629
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   630
    while ((ctx->tokenlen > 0) && (!invalid_modifier))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
    {
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   632
        if (check_token_segment(ctx, "_x2"))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   633
            set_result_shift(ctx, info, 0x1);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   634
        else if (check_token_segment(ctx, "_x4"))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   635
            set_result_shift(ctx, info, 0x2);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   636
        else if (check_token_segment(ctx, "_x8"))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   637
            set_result_shift(ctx, info, 0x3);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   638
        else if (check_token_segment(ctx, "_d8"))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   639
            set_result_shift(ctx, info, 0xD);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   640
        else if (check_token_segment(ctx, "_d4"))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   641
            set_result_shift(ctx, info, 0xE);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   642
        else if (check_token_segment(ctx, "_d2"))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   643
            set_result_shift(ctx, info, 0xF);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   644
        else if (check_token_segment(ctx, "_sat"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   645
            info->result_mod |= MOD_SATURATE;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   646
        else if (check_token_segment(ctx, "_pp"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
            info->result_mod |= MOD_PP;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   648
        else if (check_token_segment(ctx, "_centroid"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
            info->result_mod |= MOD_CENTROID;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
        else
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   651
            invalid_modifier = 1;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
    } // while
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   653
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   654
    if (invalid_modifier)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   655
        fail(ctx, "Invalid destination modifier");
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
    // !!! FIXME: predicates.
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   658
    if (nexttoken(ctx) == ((Token) '('))
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   659
        fail(ctx, "Predicates unsupported at this time");  // !!! FIXME: ...
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   660
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   661
    pushback(ctx);  // parse_register_name calls nexttoken().
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   663
    parse_register_name(ctx, &info->regtype, &info->regnum);
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   664
    // parse_register_name() can't check this: dest regs might have modifiers.
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   665
    if (ctx->tokenlen > 0)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   666
        fail(ctx, "invalid register name");
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
    // !!! FIXME: can dest registers do relative addressing?
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   670
    int invalid_writemask = 0;
521
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   671
    int implicit_writemask = 0;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   672
    if (nexttoken(ctx) != ((Token) '.'))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   673
    {
521
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   674
        implicit_writemask = 1;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   675
        info->writemask = 0xF;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   676
        info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 1;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   677
        pushback(ctx);  // no explicit writemask; do full mask.
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   678
    } // if
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   679
521
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   680
    // !!! FIXME: Cg generates code with oDepth.z ... this is a bug, I think.
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   681
    //else if (scalar_register(ctx->shader_type, info->regtype, info->regnum))
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   682
    else if ( (scalar_register(ctx->shader_type, info->regtype, info->regnum)) && (info->regtype != REG_TYPE_DEPTHOUT) )
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   683
        fail(ctx, "Writemask specified for scalar register");
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   684
    else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   685
        invalid_writemask = 1;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
    else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   687
    {
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   688
        // !!! FIXME: is out-of-order okay (yxzw instead of xyzw?)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   689
        char tokenbytes[5] = { '\0', '\0', '\0', '\0', '\0' };
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   690
        const unsigned int tokenlen = ctx->tokenlen;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   691
        memcpy(tokenbytes, ctx->token, ((tokenlen < 4) ? tokenlen : 4));
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   692
        char *ptr = tokenbytes;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   693
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   694
        info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 0;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   695
        if (*ptr == 'x') { info->writemask0 = 1; ptr++; }
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   696
        if (*ptr == 'y') { info->writemask1 = 1; ptr++; }
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   697
        if (*ptr == 'z') { info->writemask2 = 1; ptr++; }
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   698
        if (*ptr == 'w') { info->writemask3 = 1; ptr++; }
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   699
        if ((ptr == ctx->token) && (shader_is_pixel(ctx)))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   700
        {
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   701
            if (*ptr == 'r') { info->writemask0 = 1; ptr++; }
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   702
            if (*ptr == 'g') { info->writemask1 = 1; ptr++; }
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   703
            if (*ptr == 'b') { info->writemask2 = 1; ptr++; }
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   704
            if (*ptr == 'a') { info->writemask3 = 1; ptr++; }
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   705
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   706
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
        if (*ptr != '\0')
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   708
            invalid_writemask = 1;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   710
        info->writemask = ( ((info->writemask0 & 0x1) << 0) |
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   711
                            ((info->writemask1 & 0x1) << 1) |
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   712
                            ((info->writemask2 & 0x1) << 2) |
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   713
                            ((info->writemask3 & 0x1) << 3) );
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   714
    } // else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   715
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   716
    if (invalid_writemask)
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   717
        fail(ctx, "Invalid writemask");
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   718
521
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   719
    // !!! FIXME: Cg generates code with oDepth.z ... this is a bug, I think.
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   720
    if (info->regtype == REG_TYPE_DEPTHOUT)
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   721
    {
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   722
        if ( (!implicit_writemask) && ((info->writemask0 + info->writemask1 +
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   723
               info->writemask2 + info->writemask3) > 1) )
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   724
            fail(ctx, "Writemask specified for scalar register");
521
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   725
    } // if
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   726
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   727
    info->orig_writemask = info->writemask;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   728
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   729
    if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   730
    {
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   731
        fail(ctx, "Too many tokens");
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   732
        return 1;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   733
    } // if
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   734
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   735
    ctx->tokenbuf[ctx->tokenbufpos++] =
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   736
            ( ((((uint32) 1)) << 31) |
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   737
              ((((uint32) info->regnum) & 0x7ff) << 0) |
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   738
              ((((uint32) info->relative) & 0x1) << 13) |
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   739
              ((((uint32) info->result_mod) & 0xF) << 20) |
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   740
              ((((uint32) info->result_shift) & 0xF) << 24) |
500
38ce929323c2 Fixed writemask bits in assembled bytecode.
Ryan C. Gordon <icculus@icculus.org>
parents: 499
diff changeset
   741
              ((((uint32) info->writemask) & 0xF) << 16) |
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   742
              ((((uint32) info->regtype) & 0x7) << 28) |
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   743
              ((((uint32) info->regtype) & 0x18) << 8) );
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   744
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   745
    return 1;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   746
} // parse_destination_token
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   747
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   748
472
e52d487e6d91 Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents: 470
diff changeset
   749
static void set_source_mod(Context *ctx, const int negate,
e52d487e6d91 Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents: 470
diff changeset
   750
                           const SourceMod norm, const SourceMod negated,
e52d487e6d91 Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents: 470
diff changeset
   751
                           SourceMod *srcmod)
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   752
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   753
    if ( (*srcmod != SRCMOD_NONE) || (negate && (negated == SRCMOD_NONE)) )
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   754
        fail(ctx, "Incompatible source modifiers");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   755
    else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   756
        *srcmod = ((negate) ? negated : norm);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   757
} // set_source_mod
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   758
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   759
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   760
static int parse_source_token_maybe_relative(Context *ctx, const int relok)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   761
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   762
    int retval = 1;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   763
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   764
    if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   765
    {
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   766
        fail(ctx, "Too many tokens");
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   767
        return 0;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   768
    } // if
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   769
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   770
    // mark this now, so optional relative addressing token is placed second.
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   771
    uint32 *outtoken = &ctx->tokenbuf[ctx->tokenbufpos++];
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   772
    *outtoken = 0;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   773
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   774
    SourceMod srcmod = SRCMOD_NONE;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   775
    int negate = 0;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   776
    Token token = nexttoken(ctx);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   777
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   778
    if (token == ((Token) '!'))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   779
        srcmod = SRCMOD_NOT;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   780
    else if (token == ((Token) '-'))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   781
        negate = 1;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   782
    else if ( (token == TOKEN_INT_LITERAL) && (check_token(ctx, "1")) )
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   783
    {
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   784
        if (nexttoken(ctx) != ((Token) '-'))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   785
            fail(ctx, "Unexpected token");
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   786
        else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   787
            srcmod = SRCMOD_COMPLEMENT;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   788
    } // else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   789
    else
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   790
    {
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   791
        pushback(ctx);
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   792
    } // else
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   793
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   794
    RegisterType regtype;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   795
    int regnum;
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   796
    parse_register_name(ctx, &regtype, &regnum);
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   797
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   798
    if (ctx->tokenlen > 0)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   799
    {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   800
        if (check_token_segment(ctx, "_bias"))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   801
            set_source_mod(ctx, negate, SRCMOD_BIAS, SRCMOD_BIASNEGATE, &srcmod);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   802
        else if (check_token_segment(ctx, "_bx2"))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   803
            set_source_mod(ctx, negate, SRCMOD_SIGN, SRCMOD_SIGNNEGATE, &srcmod);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   804
        else if (check_token_segment(ctx, "_x2"))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   805
            set_source_mod(ctx, negate, SRCMOD_X2, SRCMOD_X2NEGATE, &srcmod);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   806
        else if (check_token_segment(ctx, "_dz"))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   807
            set_source_mod(ctx, negate, SRCMOD_DZ, SRCMOD_NONE, &srcmod);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   808
        else if (check_token_segment(ctx, "_dw"))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   809
            set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   810
        else if (check_token_segment(ctx, "_abs"))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   811
            set_source_mod(ctx, negate, SRCMOD_ABS, SRCMOD_ABSNEGATE, &srcmod);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   812
        else
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   813
            fail(ctx, "Invalid source modifier");
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   814
    } // if
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   815
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   816
    uint32 relative = 0;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   817
    if (nexttoken(ctx) != ((Token) '['))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   818
        pushback(ctx);  // not relative addressing?
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   819
    else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   820
    {
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   821
        if (!relok)
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   822
            fail(ctx, "Relative addressing not permitted here.");
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   823
        else
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   824
            retval++;
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   825
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   826
        parse_source_token_maybe_relative(ctx, 0);
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   827
        relative = 1;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   828
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   829
        if (nexttoken(ctx) != ((Token) '+'))
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   830
            pushback(ctx);
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   831
        else
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   832
        {
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   833
            // !!! FIXME: maybe c3[a0.x + 5] is legal and becomes c[a0.x + 8] ?
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   834
            if (regnum != 0)
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   835
                fail(ctx, "Relative addressing with explicit register number.");
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   836
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   837
            uint32 ui32 = 0;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   838
            if ( (nexttoken(ctx) != TOKEN_INT_LITERAL) ||
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   839
                 (!ui32fromtoken(ctx, &ui32)) ||
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   840
                 (ctx->tokenlen != 0) )
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   841
            {
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   842
                fail(ctx, "Invalid relative addressing offset");
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   843
            } // if
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   844
            regnum += (int) ui32;
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   845
        } // else
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   846
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   847
        if (nexttoken(ctx) != ((Token) ']'))
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   848
            fail(ctx, "Expected ']'");
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   849
    } // else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   850
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   851
    int invalid_swizzle = 0;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   852
    uint32 swizzle = 0;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   853
    if (nexttoken(ctx) != ((Token) '.'))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   854
    {
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   855
        swizzle = 0xE4;  // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   856
        pushback(ctx);  // no explicit writemask; do full mask.
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   857
    } // if
491
bcc3c215807a Fixed wrong data from scalar_register().
Ryan C. Gordon <icculus@icculus.org>
parents: 490
diff changeset
   858
    else if (scalar_register(ctx->shader_type, regtype, regnum))
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   859
        fail(ctx, "Swizzle specified for scalar register");
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   860
    else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   861
        invalid_swizzle = 1;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   862
    else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   863
    {
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   864
        char tokenbytes[5] = { '\0', '\0', '\0', '\0', '\0' };
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   865
        const unsigned int tokenlen = ctx->tokenlen;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   866
        memcpy(tokenbytes, ctx->token, ((tokenlen < 4) ? tokenlen : 4));
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   867
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   868
        // deal with shortened form (.x = .xxxx, etc).
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   869
        if (tokenlen == 1)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   870
            tokenbytes[1] = tokenbytes[2] = tokenbytes[3] = tokenbytes[0];
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   871
        else if (tokenlen == 2)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   872
            tokenbytes[2] = tokenbytes[3] = tokenbytes[1];
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   873
        else if (tokenlen == 3)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   874
            tokenbytes[3] = tokenbytes[2];
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   875
        else if (tokenlen != 4)
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   876
            invalid_swizzle = 1;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   877
        tokenbytes[4] = '\0';
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   878
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   879
        uint32 val = 0;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   880
        int saw_xyzw = 0;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   881
        int saw_rgba = 0;
472
e52d487e6d91 Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents: 470
diff changeset
   882
        int i;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   883
        for (i = 0; i < 4; i++)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   884
        {
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   885
            const int component = (int) tokenbytes[i];
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   886
            switch (component)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   887
            {
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   888
                case 'x': val = 0; saw_xyzw = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   889
                case 'y': val = 1; saw_xyzw = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   890
                case 'z': val = 2; saw_xyzw = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   891
                case 'w': val = 3; saw_xyzw = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   892
                case 'r': val = 0; saw_rgba = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   893
                case 'g': val = 1; saw_rgba = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   894
                case 'b': val = 2; saw_rgba = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   895
                case 'a': val = 3; saw_rgba = 1; break;
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   896
                default: invalid_swizzle = 1; break;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   897
            } // switch
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   898
            swizzle |= (val << (i * 2));
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   899
        } // for
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   900
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   901
        if (saw_xyzw && saw_rgba)
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   902
            invalid_swizzle = 1;
548
b37cdacb50d5 Fixed parsing details of swizzles.
Ryan C. Gordon <icculus@icculus.org>
parents: 544
diff changeset
   903
        else if (saw_rgba && !shader_is_pixel(ctx))
b37cdacb50d5 Fixed parsing details of swizzles.
Ryan C. Gordon <icculus@icculus.org>
parents: 544
diff changeset
   904
            invalid_swizzle = 1;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   905
    } // else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   906
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   907
    if (invalid_swizzle)
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   908
        fail(ctx, "Invalid swizzle");
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   909
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   910
    *outtoken = ( ((((uint32) 1)) << 31) |
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   911
                  ((((uint32) regnum) & 0x7ff) << 0) |
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   912
                  ((((uint32) relative) & 0x1) << 13) |
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   913
                  ((((uint32) swizzle) & 0xFF) << 16) |
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   914
                  ((((uint32) srcmod) & 0xF) << 24) |
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   915
                  ((((uint32) regtype) & 0x7) << 28) |
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   916
                  ((((uint32) regtype) & 0x18) << 8) );
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   917
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   918
    return retval;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   919
} // parse_source_token_maybe_relative
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   920
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   921
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   922
static inline int parse_source_token(Context *ctx)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   923
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   924
    return parse_source_token_maybe_relative(ctx, 1);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   925
} // parse_source_token
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   926
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   927
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   928
static int parse_args_NULL(Context *ctx)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   929
{
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   930
    return 1;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   931
} // parse_args_NULL
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   932
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   933
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   934
static int parse_num(Context *ctx, const int floatok, uint32 *value)
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   935
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   936
    union { float f; int32 si32; uint32 ui32; } cvt;
572
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   937
    int negative = 0;
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   938
    Token token = nexttoken(ctx);
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   939
572
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   940
    if (token == ((Token) '-'))
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   941
    {
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   942
        negative = 1;
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   943
        token = nexttoken(ctx);
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   944
    } // if
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   945
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   946
    if (token == TOKEN_INT_LITERAL)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   947
    {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   948
        int d = 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   949
        sscanf(ctx->token, "%d", &d);
572
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   950
        if (floatok)
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   951
            cvt.f = (float) ((negative) ? -d : d);
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   952
        else
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   953
            cvt.si32 = (int32) ((negative) ? -d : d);
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   954
    } // if
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   955
    else if (token == TOKEN_FLOAT_LITERAL)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   956
    {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   957
        if (!floatok)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   958
        {
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   959
            fail(ctx, "Expected whole number");
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   960
            *value = 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   961
            return 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   962
        } // if
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   963
        sscanf(ctx->token, "%f", &cvt.f);
572
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   964
        if (negative)
cdc8bb82f7d2 Fixed parse_num().
Ryan C. Gordon <icculus@icculus.org>
parents: 571
diff changeset
   965
            cvt.f = -cvt.f;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   966
    } // if
492
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
   967
    else
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
   968
    {
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   969
        fail(ctx, "Expected number");
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   970
        *value = 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   971
        return 0;
492
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
   972
    } // else
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
   973
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
   974
    *value = cvt.ui32;
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   975
    return 1;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   976
} // parse_num
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   977
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   978
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   979
static int parse_args_DEFx(Context *ctx, const int isflt)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   980
{
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   981
    parse_destination_token(ctx);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   982
    require_comma(ctx);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   983
    parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   984
    require_comma(ctx);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   985
    parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   986
    require_comma(ctx);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   987
    parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   988
    require_comma(ctx);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
   989
    parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   990
    return 6;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   991
} // parse_args_DEFx
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   992
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   993
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   994
static int parse_args_DEF(Context *ctx)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   995
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   996
    return parse_args_DEFx(ctx, 1);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   997
} // parse_args_DEF
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   998
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   999
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1000
static int parse_args_DEFI(Context *ctx)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1001
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1002
    return parse_args_DEFx(ctx, 0);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1003
} // parse_args_DEFI
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1004
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1005
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1006
static int parse_args_DEFB(Context *ctx)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1007
{
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
  1008
    parse_destination_token(ctx);
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
  1009
    require_comma(ctx);
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1010
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1011
    // !!! FIXME: do a TOKEN_TRUE and TOKEN_FALSE? Is this case-sensitive?
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1012
    const Token token = nexttoken(ctx);
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1013
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1014
    int bad = 0;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1015
    if (token != TOKEN_IDENTIFIER)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1016
        bad = 1;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1017
    else if (check_token_segment(ctx, "true"))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1018
        ctx->tokenbuf[ctx->tokenbufpos++] = 1;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1019
    else if (check_token_segment(ctx, "false"))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1020
        ctx->tokenbuf[ctx->tokenbufpos++] = 0;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1021
    else
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1022
        bad = 1;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1023
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1024
    if (ctx->tokenlen != 0)
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1025
        bad = 1;
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1026
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1027
    if (bad)
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
  1028
        fail(ctx, "Expected 'true' or 'false'");
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1029
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1030
    return 3;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1031
} // parse_args_DEFB
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1032
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1033
504
d79ae3b98f47 Fixed dcl_2d usage.
Ryan C. Gordon <icculus@icculus.org>
parents: 502
diff changeset
  1034
static int parse_dcl_usage(Context *ctx, uint32 *val, int *issampler)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1035
{
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1036
    int i;
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1037
    static const char *samplerusagestrs[] = { "_2d", "_cube", "_volume" };
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1038
    static const char *usagestrs[] = {
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1039
        "_position", "_blendweight", "_blendindices", "_normal", "_psize",
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1040
        "_texcoord", "_tangent", "_binormal", "_tessfactor", "_positiont",
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1041
        "_color", "_fog", "_depth", "_sample"
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1042
    };
504
d79ae3b98f47 Fixed dcl_2d usage.
Ryan C. Gordon <icculus@icculus.org>
parents: 502
diff changeset
  1043
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1044
    for (i = 0; i < STATICARRAYLEN(usagestrs); i++)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1045
    {
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1046
        if (check_token_segment(ctx, usagestrs[i]))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1047
        {
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1048
            *issampler = 0;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1049
            *val = i;
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
  1050
            return 1;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1051
        } // if
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1052
    } // for
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1053
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1054
    for (i = 0; i < STATICARRAYLEN(samplerusagestrs); i++)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1055
    {
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1056
        if (check_token_segment(ctx, samplerusagestrs[i]))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1057
        {
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1058
            *issampler = 1;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1059
            *val = i + 2;
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
  1060
            return 1;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1061
        } // if
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1062
    } // for
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1063
514
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
  1064
    *issampler = 0;
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
  1065
    *val = 0;
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
  1066
    return 0;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1067
} // parse_dcl_usage
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1068
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1069
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1070
static int parse_args_DCL(Context *ctx)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1071
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1072
    int issampler = 0;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1073
    uint32 usage = 0;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1074
    uint32 index = 0;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1075
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1076
    ctx->tokenbufpos++;  // save a spot for the usage/index token.
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
  1077
    ctx->tokenbuf[0] = 0;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1078
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1079
    // parse_instruction_token() sets ctx->token to the end of the instruction
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1080
    //  so we can see if there are destination modifiers on the instruction
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1081
    //  itself...
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1082
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1083
    if (parse_dcl_usage(ctx, &usage, &issampler))
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1084
    {
575
70bb1ba99a07 Fixed DCL usage index parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 574
diff changeset
  1085
        if ((ctx->tokenlen > 0) && (*ctx->token != '_'))
70bb1ba99a07 Fixed DCL usage index parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 574
diff changeset
  1086
        {
70bb1ba99a07 Fixed DCL usage index parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 574
diff changeset
  1087
            if (!ui32fromtoken(ctx, &index))
70bb1ba99a07 Fixed DCL usage index parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 574
diff changeset
  1088
                fail(ctx, "Expected usage index");
70bb1ba99a07 Fixed DCL usage index parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 574
diff changeset
  1089
        } // if
562
c9a2bc5129c9 First shot at reworking assembly parser to use preprocessor/lexer.
Ryan C. Gordon <icculus@icculus.org>
parents: 558
diff changeset
  1090
    } // if
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1091
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
  1092
    parse_destination_token(ctx);
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1093
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1094
    const int samplerreg = (ctx->dest_arg.regtype == REG_TYPE_SAMPLER);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1095
    if (issampler != samplerreg)
542
a56d3bfd2e36 More work on multiple error messages.
Ryan C. Gordon <icculus@icculus.org>
parents: 539
diff changeset
  1096
        fail(ctx, "Invalid usage");
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1097
    else if (samplerreg)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1098</