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