mojoshader_assembler.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 02 Feb 2009 16:13:12 -0500
changeset 532 19e2f562b9ce
parent 529 e77225cd13ae
child 536 5af65fe6e917
permissions -rw-r--r--
Added some FIXMEs.
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
532
19e2f562b9ce Added some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 529
diff changeset
    10
// !!! FIXME: this should report all errors, not quit on the first fail().
19e2f562b9ce Added some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 529
diff changeset
    11
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#define __MOJOSHADER_INTERNAL__ 1
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#include "mojoshader_internal.h"
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
497
5b2465d40b57 Disabled tokenizer debug output for now.
Ryan C. Gordon <icculus@icculus.org>
parents: 495
diff changeset
    15
#define DEBUG_TOKENIZER 0
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
498
6a4b881704df Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 497
diff changeset
    17
// !!! FIXME: no #define support yet.
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
    19
typedef struct TokenizerContext
514
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
    20
{
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
    21
    const char *source;
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
    22
    int on_endline;
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
    23
    unsigned int linenum;
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
    24
    char prevchar;
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
    25
    char token[64];
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
    26
    char pushedback;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
    27
} TokenizerContext;
514
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
    28
ba913834b491 The parse_args_DCL fiasco continues.
Ryan C. Gordon <icculus@icculus.org>
parents: 513
diff changeset
    29
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
typedef struct Context Context;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
// Context...this is state that changes as we assemble a shader...
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
struct Context
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
    MOJOSHADER_malloc malloc;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
    MOJOSHADER_free free;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
    void *malloc_data;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    const char *failstr;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
    39
    TokenizerContext tctx;
523
699696afd731 Allow errors to specify post-processing problems.
Ryan C. Gordon <icculus@icculus.org>
parents: 522
diff changeset
    40
    MOJOSHADER_parsePhase parse_phase;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    MOJOSHADER_shaderType shader_type;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    uint8 major_ver;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    uint8 minor_ver;
524
03eea2f0762c First (incomplete!) shot at more robust CTAB support.
Ryan C. Gordon <icculus@icculus.org>
parents: 523
diff changeset
    44
    uint32 version_token;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    uint32 tokenbuf[16];
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
    int tokenbufpos;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
    DestArgInfo dest_arg;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
    48
    uint32 *output;
486
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
    49
    uint32 *token_to_line;
524
03eea2f0762c First (incomplete!) shot at more robust CTAB support.
Ryan C. Gordon <icculus@icculus.org>
parents: 523
diff changeset
    50
    uint8 *ctab;
03eea2f0762c First (incomplete!) shot at more robust CTAB support.
Ryan C. Gordon <icculus@icculus.org>
parents: 523
diff changeset
    51
    uint32 ctab_len;
03eea2f0762c First (incomplete!) shot at more robust CTAB support.
Ryan C. Gordon <icculus@icculus.org>
parents: 523
diff changeset
    52
    uint32 ctab_allocation;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
    53
    size_t output_len;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
    54
    size_t output_allocation;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
};
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
// Convenience functions for allocators...
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
static inline int out_of_memory(Context *ctx)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    if (ctx->failstr == NULL)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
        ctx->failstr = out_of_mem_str;  // fail() would call malloc().
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    return FAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
} // out_of_memory
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
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
    68
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    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
    70
    if (retval == NULL)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
        out_of_memory(ctx);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    return retval;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
} // Malloc
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 Free(Context *ctx, void *ptr)
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
    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
    78
        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
    79
} // Free
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
static int failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
static int failf(Context *ctx, const char *fmt, ...)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    if (ctx->failstr == NULL)  // don't change existing error.
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
        char scratch = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
        va_list ap;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
        va_start(ap, fmt);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
        const int len = vsnprintf(&scratch, sizeof (scratch), fmt, ap);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
        va_end(ap);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
        char *failstr = (char *) Malloc(ctx, len + 1);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
        if (failstr != NULL)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
            va_start(ap, fmt);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
            vsnprintf(failstr, len + 1, fmt, ap);  // rebuild it.
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
            va_end(ap);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
            ctx->failstr = failstr;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
    } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    return FAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
} // failf
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
static inline int fail(Context *ctx, const char *reason)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    return failf(ctx, "%s", reason);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
} // fail
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
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
   111
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    return (ctx->failstr != NULL);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
} // isfail
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   116
static inline int tokeq(const TokenizerContext *tctx, const char *token)
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   117
{
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   118
    return (strcasecmp(tctx->token, token) == 0);
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   119
} // tokeq
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   120
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   121
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
// Shader model version magic...
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
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
   125
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
    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
   127
} // version_ui32
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
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
   130
                                         const uint8 min)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    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
   133
} // shader_version_atleast
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
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
   136
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    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
   138
} // shader_is_pixel
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
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
   141
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    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
   143
} // shader_is_vertex
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   146
static int ui32fromstr(const char *str, uint32 *ui32)
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   147
{
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   148
    //*ui32 = (uint32) atoi(minstr);
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   149
    char *endptr = NULL;
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   150
    const long val = strtol(str, &endptr, 10);
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   151
    *ui32 = (uint32) val;
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   152
    return ((val >= 0) && (*str != '\0') && (*endptr == '\0'));
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   153
} // ui32fromstr
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   154
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
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
   157
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    if (isfail(ctx))
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
        return;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   161
    if (ctx->output_len >= ctx->output_allocation)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   162
    {
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   163
        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
   164
        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
   165
        void *ptr;
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   166
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   167
        ptr = Malloc(ctx, newsize * sizeof (uint32));
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   168
        if (ptr == NULL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   169
            return;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   170
        if (ctx->output_len > 0)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   171
            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
   172
        Free(ctx, ctx->output);
486
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   173
        ctx->output = (uint32 *) ptr;
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   174
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   175
        ptr = Malloc(ctx, newsize * sizeof (uint32));
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   176
        if (ptr == NULL)
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   177
            return;
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   178
        if (ctx->output_len > 0)
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   179
            memcpy(ptr, ctx->token_to_line, ctx->output_len * sizeof (uint32));
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   180
        Free(ctx, ctx->token_to_line);
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   181
        ctx->token_to_line = (uint32 *) ptr;
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   182
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   183
        ctx->output_allocation = newsize;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   184
    } // if
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   185
486
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   186
    ctx->output[ctx->output_len] = token;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   187
    ctx->token_to_line[ctx->output_len] = ctx->tctx.linenum;
486
45efac751027 Added error_position to assembly results.
Ryan C. Gordon <icculus@icculus.org>
parents: 485
diff changeset
   188
    ctx->output_len++;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
} // output_token_noswap
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
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
   193
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    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
   195
} // output_token
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
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
   199
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
    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
   201
        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
   202
    else if (!isfail(ctx))
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
        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
   205
        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
   206
        while (len >= 4)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
            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
   209
            len -= 4;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
            buf += 4;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
        } // while
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
        if (len > 0)  // handle spillover...
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
            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
   216
            overflow.ui32 = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
            memcpy(overflow.ui8, buf, len);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
            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
   219
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
    } // else if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
} // output_comment_bytes
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
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
   225
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
    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
   227
} // output_comment_string
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   230
static int tokenize_ctx(Context *ctx, TokenizerContext *tctx)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
    int idx = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    if (isfail(ctx))
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
        return FAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   237
    if (tctx->pushedback)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   239
        tctx->pushedback = 0;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
        return NOFAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
    } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   243
    if (tctx->on_endline)
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   244
    {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   245
        tctx->on_endline = 0;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   246
        tctx->linenum++;  // passed a newline, update.
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   247
    } // if
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   248
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
    while (1)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    {
477
e8c74c310a11 Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 472
diff changeset
   251
        // !!! FIXME: carefully crafted (but legal) comments can trigger this.
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   252
        if (idx >= sizeof (tctx->token))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
            return fail(ctx, "buffer overflow");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   255
        char ch = *tctx->source;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
        if (ch == '\t')
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
            ch = ' ';  // collapse tabs into single spaces.
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
        else if (ch == '\r')
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
        {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   260
            if (tctx->source[1] == '\n')
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
               continue;  // ignore '\r' if this is "\r\n" ...
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
            ch = '\n';
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
        } // else if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
478
af4bfa5d615c Some basic tokenizer logic fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 477
diff changeset
   265
        if ((ch >= '0') && (ch <= '9'))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
            // starting a number, but rest of current token was not number.
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   268
            if ((idx > 0) && ((tctx->prevchar < '0') || (tctx->prevchar > '9')))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
            {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   270
                tctx->token[idx++] = '\0';
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
                return NOFAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
            } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
        else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
            // starting a non-number, but rest of current token was numbers.
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   277
            if ((idx > 0) && ((tctx->prevchar >= '0') && (tctx->prevchar <= '9')))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
            {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   279
                tctx->token[idx++] = '\0';
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
                return NOFAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
            } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
        } // else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
        switch (ch)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
            case '/':
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
            case ';':  // !!! FIXME: comment, right?
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
                if (idx != 0)  // finish off existing token.
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   289
                    tctx->token[idx] = '\0';
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
                else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
                {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   292
                    tctx->token[idx++] = ch;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   293
                    tctx->source++;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   294
                    if ((ch == '/') && (*tctx->source == '/'))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
                    {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   296
                        tctx->token[idx++] = '/';
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   297
                        tctx->source++;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
                    } // if
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   299
                    tctx->token[idx++] = '\0';
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
                } // else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
                return NOFAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
            case ' ':
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   304
                if (tctx->prevchar == ' ')
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
                    break;   // multiple whitespace collapses into one.
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
                // intentional fall-through...
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
            case '_':
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
            case '[':
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
            case ']':
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
            case '(':
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
            case ')':
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
            case '!':
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
            case '+':
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
            case '-':
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
            case ',':
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
            case '.':
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
            case '\n':
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
                if (idx != 0)  // finish off existing token.
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   320
                    tctx->token[idx] = '\0';
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
                else  // this is a token in itself.
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
                    if (ch == '\n')
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   324
                        tctx->on_endline = 1;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   325
                    tctx->source++;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   326
                    tctx->token[idx++] = ch;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   327
                    tctx->token[idx++] = '\0';
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
                } // else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
                return NOFAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
            case '\0':
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   332
                tctx->token[idx] = '\0';
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
                if (idx != 0)  // had any chars? It's a token.
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
                    return NOFAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
                return END_OF_STREAM;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
            default:
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   338
                tctx->source++;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   339
                tctx->token[idx++] = ch;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
                break;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
        } // switch
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   343
        tctx->prevchar = ch;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
    } // while
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
    return fail(ctx, "???");  // shouldn't hit this.
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   347
} // tokenize_ctx
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
static inline int tokenize(Context *ctx)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
{
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   352
    const int rc = tokenize_ctx(ctx, &ctx->tctx);
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   353
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    #if DEBUG_TOKENIZER
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
    printf("TOKENIZE: %s '%s'\n",
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
           (rc == END_OF_STREAM) ? "END_OF_STREAM" :
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
           (rc == FAIL) ? "FAIL" :
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
           (rc == NOFAIL) ? "NOFAIL" : "???",
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   359
           (ctx->tctx.token[0] == '\n') ? "\\n" : ctx->tctx.token);
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
    #endif
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   361
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
    return rc;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
} // tokenize
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   366
static int pushback_ctx(Context *ctx, TokenizerContext *tctx)
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   367
{
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   368
    if (tctx->pushedback)
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   369
        return fail(ctx, "BUG: Double pushback in parser");
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   370
    else
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   371
        tctx->pushedback = 1;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   372
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   373
    return NOFAIL;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   374
}
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   375
490
14b6586d9c43 Fixed parsing of DCL instructions.
Ryan C. Gordon <icculus@icculus.org>
parents: 489
diff changeset
   376
static inline int pushback(Context *ctx)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
{
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   378
    const int rc = pushback_ctx(ctx, &ctx->tctx);
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   379
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
    #if DEBUG_TOKENIZER
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   381
    printf("PUSHBACK: %s\n",
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   382
           (rc == END_OF_STREAM) ? "END_OF_STREAM" :
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   383
           (rc == FAIL) ? "FAIL" :
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   384
           (rc == NOFAIL) ? "NOFAIL" : "???");
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
    #endif
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   387
    return rc;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
} // pushback
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   391
static int nexttoken_ctx(Context *ctx, TokenizerContext *tctx,
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   392
                     const int ignoreeol, const int ignorewhitespace,
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   393
                     const int eolok, const int eosok)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
    int rc = NOFAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   397
    while ((rc = tokenize_ctx(ctx, tctx)) == NOFAIL)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
    {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   399
        if (tokeq(tctx, "\n"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
            if (ignoreeol)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
                continue;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
            else if (!eolok)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
                return fail(ctx, "Unexpected EOL");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   407
        else if (tokeq(tctx, " "))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
            if (ignorewhitespace)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
                continue;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
        } // else if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
        // skip comments...
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   414
        else if (tokeq(tctx, "//") || tokeq(tctx, ";"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
        {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   416
            while ((rc = tokenize_ctx(ctx, tctx)) == NOFAIL)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
            {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   418
                if (tokeq(tctx, "\n"))
479
f7beff48de5e nexttoken() needs to report the ending newline when throwing away comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 478
diff changeset
   419
                {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   420
                    pushback_ctx(ctx, tctx);
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
                    break;
479
f7beff48de5e nexttoken() needs to report the ending newline when throwing away comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 478
diff changeset
   422
                } // if
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
            } // while
479
f7beff48de5e nexttoken() needs to report the ending newline when throwing away comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 478
diff changeset
   424
            continue;  // pick up from newline, go again.
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
        } // if
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
        break;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
    } // while
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   430
    if ((rc == END_OF_STREAM) && (!eosok))
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   431
        return fail(ctx, "Unexpected EOF");
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   432
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   433
    return rc;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   434
} // nexttoken_ctx
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   435
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   436
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   437
static inline int nexttoken(Context *ctx, const int ignoreeol,
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   438
                     const int ignorewhitespace, const int eolok,
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   439
                     const int eosok)
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   440
{
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   441
    const int rc = nexttoken_ctx(ctx, &ctx->tctx, ignoreeol,
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   442
                                 ignorewhitespace, eolok, eosok);
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   443
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
    #if DEBUG_TOKENIZER
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
    printf("NEXTTOKEN: %s '%s'\n",
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
           (rc == END_OF_STREAM) ? "END_OF_STREAM" :
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
           (rc == FAIL) ? "FAIL" :
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
           (rc == NOFAIL) ? "NOFAIL" : "???",
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   449
           (ctx->tctx.token[0] == '\n') ? "\\n" : ctx->tctx.token);
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
    #endif
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   451
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
    return rc;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
} // nexttoken
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   455
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   456
static int require_endline(Context *ctx)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   457
{
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   458
    TokenizerContext *tctx = &ctx->tctx;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
    const int rc = nexttoken(ctx, 0, 1, 1, 1);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
    if (rc == FAIL)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
        return FAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
    else if (rc == END_OF_STREAM)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   463
        return NOFAIL;  // we'll call this an EOL.
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   464
    else if (!tokeq(tctx, "\n"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465
        return fail(ctx, "Endline expected");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
    return NOFAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
} // require_endline
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   469
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   470
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
   471
{
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   472
    TokenizerContext *tctx = &ctx->tctx;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   473
    const int rc = nexttoken(ctx, 0, 1, 0, 0);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   474
    if (rc == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   475
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   476
    else if (!tokeq(tctx, ","))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   477
        return fail(ctx, "Comma expected");
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
    return NOFAIL;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   479
} // require_comma
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
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
   483
{
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   484
    TokenizerContext *tctx = &ctx->tctx;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
    if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
        return FAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
    int neednum = 1;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
    int regnum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
    RegisterType regtype = REG_TYPE_TEMP;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   491
    if (tokeq(tctx, "r"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
        regtype = REG_TYPE_TEMP;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   493
    else if (tokeq(tctx, "v"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
        regtype = REG_TYPE_INPUT;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   495
    else if (tokeq(tctx, "c"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
        regtype = REG_TYPE_CONST;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   497
    else if (tokeq(tctx, "i"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
        regtype = REG_TYPE_CONSTINT;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   499
    else if (tokeq(tctx, "b"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
        regtype = REG_TYPE_CONSTBOOL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   501
    else if (tokeq(tctx, "oC"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
        regtype = REG_TYPE_COLOROUT;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   503
    else if (tokeq(tctx, "s"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
        regtype = REG_TYPE_SAMPLER;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   505
    else if (tokeq(tctx, "oD"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
        regtype = REG_TYPE_ATTROUT;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   507
    else if (tokeq(tctx, "l"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
        regtype = REG_TYPE_LABEL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   509
    else if (tokeq(tctx, "p"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
        regtype = REG_TYPE_PREDICATE;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   511
    else if (tokeq(tctx, "oDepth"))
513
abd9c85ba168 oDepth register doesn't have an index.
Ryan C. Gordon <icculus@icculus.org>
parents: 512
diff changeset
   512
    {
abd9c85ba168 oDepth register doesn't have an index.
Ryan C. Gordon <icculus@icculus.org>
parents: 512
diff changeset
   513
        regtype = REG_TYPE_DEPTHOUT;
abd9c85ba168 oDepth register doesn't have an index.
Ryan C. Gordon <icculus@icculus.org>
parents: 512
diff changeset
   514
        neednum = 0;
abd9c85ba168 oDepth register doesn't have an index.
Ryan C. Gordon <icculus@icculus.org>
parents: 512
diff changeset
   515
    } // else if
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   516
    else if (tokeq(tctx, "aL"))
465
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_LOOP;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
        neednum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
    } // else if
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   521
    else if (tokeq(tctx, "o"))
465
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
        if (!shader_is_vertex(ctx) || !shader_version_atleast(ctx, 3, 0))
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
            return fail(ctx, "Output register not valid in this shader type");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
        regtype = REG_TYPE_OUTPUT;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
    } // else if
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   527
    else if (tokeq(tctx, "oT"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
    {
502
3cb501248990 Fixed logic error in assembler, to decide if oTx registers are available.
Ryan C. Gordon <icculus@icculus.org>
parents: 500
diff changeset
   529
        if (shader_is_vertex(ctx) && shader_version_atleast(ctx, 3, 0))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
            return fail(ctx, "Output register not valid in this shader type");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
        regtype = REG_TYPE_OUTPUT;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
    } // else if
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   533
    else if (tokeq(tctx, "a"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   535
        if (!shader_is_vertex(ctx))
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
            return fail(ctx, "Address register only valid in vertex shaders.");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
        regtype = REG_TYPE_ADDRESS;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
    } // else if
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   539
    else if (tokeq(tctx, "t"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
        if (!shader_is_pixel(ctx))
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
            return fail(ctx, "Address register only valid in pixel shaders.");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
        regtype = REG_TYPE_ADDRESS;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
    } // else if
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   545
    else if (tokeq(tctx, "vPos"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
        regtype = REG_TYPE_MISCTYPE;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
        regnum = (int) MISCTYPE_TYPE_POSITION;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
        neednum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
    } // else if
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   551
    else if (tokeq(tctx, "vFace"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
        regtype = REG_TYPE_MISCTYPE;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
        regnum = (int) MISCTYPE_TYPE_FACE;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
        neednum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   556
    } // else if
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   557
    else if (tokeq(tctx, "oPos"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
        regtype = REG_TYPE_RASTOUT;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
        regnum = (int) RASTOUT_TYPE_POSITION;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
        neednum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
    } // else if
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   563
    else if (tokeq(tctx, "oFog"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
        regtype = REG_TYPE_RASTOUT;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
        regnum = (int) RASTOUT_TYPE_FOG;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
        neednum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
    } // else if
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   569
    else if (tokeq(tctx, "oPts"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
        regtype = REG_TYPE_RASTOUT;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
        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
   573
        neednum = 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
    } // else if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
        
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
    //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
   577
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
    else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
        return fail(ctx, "expected register type");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
    } // else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
    if (neednum)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
    {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   585
        // Make a temp TokenizerContext, since we need to skip whitespace here,
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   586
        //  but if the next non-whitespace token isn't '[', we'll want to get
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   587
        //  that whitespace back.
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   588
        TokenizerContext tmptctx;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   589
        memcpy(&tmptctx, tctx, sizeof (TokenizerContext));
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   590
        if (nexttoken_ctx(ctx, &tmptctx, 0, 1, 1, 1) == FAIL)
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   591
            return FAIL;
522
3c08f4d2aa2e Fixed relative addressing parsing, again.
Ryan C. Gordon <icculus@icculus.org>
parents: 521
diff changeset
   592
        else if (tokeq(&tmptctx, "["))
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   593
            neednum = 0;
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   594
    } // if
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   595
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   596
    if (neednum)
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   597
    {
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
        if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
            return FAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   601
        uint32 ui32 = 0;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   602
        if (!ui32fromstr(tctx->token, &ui32))
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   603
            return fail(ctx, "Invalid register index");
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   604
        regnum = (int) ui32;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   605
    } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   607
    // split up REG_TYPE_CONST
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   608
    if (regtype == REG_TYPE_CONST)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   609
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   610
        if (regnum < 2048)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   611
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   612
            regtype = REG_TYPE_CONST;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
            regnum -= 0;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   614
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
        else if (regnum < 4096)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   616
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
            regtype = REG_TYPE_CONST2;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   618
            regnum -= 2048;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   619
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
        else if (regnum < 6144)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   621
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   622
            regtype = REG_TYPE_CONST3;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   623
            regnum -= 4096;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   624
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   625
        else if (regnum < 8192)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   626
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
            regtype = REG_TYPE_CONST4;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   628
            regnum -= 6144;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   629
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   630
        else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
        {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   632
            return fail(ctx, "Invalid const register index");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
        } // else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
    } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
    *rtype = regtype;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
    *rnum = regnum;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
    return NOFAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
} // parse_register_name
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   643
static int set_result_shift(Context *ctx, DestArgInfo *info, const int val)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   644
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   645
    if (info->result_shift != 0)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   646
        return fail(ctx, "Multiple result shift modifiers");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   647
    info->result_shift = val;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   648
    return NOFAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   649
} // set_result_shift
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   650
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   651
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   652
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
   653
{
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   654
    TokenizerContext *tctx = &ctx->tctx;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   655
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   656
    DestArgInfo *info = &ctx->dest_arg;
510
f4433db86f6e Fixed wrong sizeof for a memset() call.
Ryan C. Gordon <icculus@icculus.org>
parents: 508
diff changeset
   657
    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
   658
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   659
    // See if there are destination modifiers on the instruction itself...
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   660
    while (1)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   661
    {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
        if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   663
            return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   664
        else if (tokeq(tctx, " "))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
            break;  // done with modifiers.
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   666
        else if (!tokeq(tctx, "_"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
            return fail(ctx, "Expected modifier or whitespace");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
        else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
            return FAIL;
511
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   670
        // !!! FIXME: this can be cleaned up when tokenizer is fixed.
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   671
        else if (tokeq(tctx, "x"))
511
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   672
        {
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   673
            if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   674
                return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   675
            else if (tokeq(tctx, "2"))
511
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   676
                set_result_shift(ctx, info, 0x1);
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   677
            else if (tokeq(tctx, "4"))
511
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   678
                set_result_shift(ctx, info, 0x2);
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   679
            else if (tokeq(tctx, "8"))
511
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   680
                set_result_shift(ctx, info, 0x3);
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   681
            else
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   682
                return fail(ctx, "Expected modifier");
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   683
        } // else if
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   684
        // !!! FIXME: this can be cleaned up when tokenizer is fixed.
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   685
        else if (tokeq(tctx, "d"))
511
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   686
        {
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   687
            if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   688
                return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   689
            else if (tokeq(tctx, "8"))
511
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   690
                set_result_shift(ctx, info, 0xD);
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   691
            else if (tokeq(tctx, "4"))
511
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   692
                set_result_shift(ctx, info, 0xE);
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   693
            else if (tokeq(tctx, "2"))
511
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   694
                set_result_shift(ctx, info, 0xF);
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   695
            else
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   696
                return fail(ctx, "Expected modifier");
67d7efa9206b Corrected parsing of result modifiers for my stupid tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 510
diff changeset
   697
        } // else if
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   698
        else if (tokeq(tctx, "sat"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   699
            info->result_mod |= MOD_SATURATE;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   700
        else if (tokeq(tctx, "pp"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   701
            info->result_mod |= MOD_PP;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   702
        else if (tokeq(tctx, "centroid"))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   703
            info->result_mod |= MOD_CENTROID;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   704
        else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   705
            return fail(ctx, "Expected modifier");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   706
    } // while
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   708
    if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
        return FAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   710
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   711
    // !!! FIXME: predicates.
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   712
    if (tokeq(tctx, "("))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   713
        return fail(ctx, "Predicates unsupported at this time");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   714
    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
   715
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   716
    if (parse_register_name(ctx, &info->regtype, &info->regnum) == FAIL)
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   717
        return FAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   718
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   719
    if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   720
        return FAIL;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   721
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   722
    // !!! 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
   723
521
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   724
    int implicit_writemask = 0;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   725
    if (!tokeq(tctx, "."))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   726
    {
521
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   727
        implicit_writemask = 1;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   728
        info->writemask = 0xF;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   729
        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
   730
        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
   731
    } // if
521
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   732
    // !!! 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
   733
    //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
   734
    else if ( (scalar_register(ctx->shader_type, info->regtype, info->regnum)) && (info->regtype != REG_TYPE_DEPTHOUT) )
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   735
        return fail(ctx, "Writemask specified for scalar register");
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   736
    else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   737
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   738
    else if (tokeq(tctx, ""))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   739
        return fail(ctx, "Invalid writemask");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   740
    else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   741
    {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   742
        char *ptr = tctx->token;
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   743
        info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 0;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   744
        if (*ptr == 'x') { info->writemask0 = 1; ptr++; }
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   745
        if (*ptr == 'y') { info->writemask1 = 1; ptr++; }
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   746
        if (*ptr == 'z') { info->writemask2 = 1; ptr++; }
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   747
        if (*ptr == 'w') { info->writemask3 = 1; ptr++; }
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   748
        if ((ptr == tctx->token) && (shader_is_pixel(ctx)))
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   749
        {
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   750
            if (*ptr == 'r') { info->writemask0 = 1; ptr++; }
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   751
            if (*ptr == 'g') { info->writemask1 = 1; ptr++; }
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   752
            if (*ptr == 'b') { info->writemask2 = 1; ptr++; }
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   753
            if (*ptr == 'a') { info->writemask3 = 1; ptr++; }
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   754
        } // if
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   755
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   756
        if (*ptr != '\0')
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   757
            return fail(ctx, "Invalid writemask");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   758
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   759
        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
   760
                            ((info->writemask1 & 0x1) << 1) |
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   761
                            ((info->writemask2 & 0x1) << 2) |
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   762
                            ((info->writemask3 & 0x1) << 3) );
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   763
    } // else
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   764
521
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   765
    // !!! 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
   766
    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
   767
    {
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   768
        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
   769
               info->writemask2 + info->writemask3) > 1) )
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   770
            return fail(ctx, "Writemask specified for scalar register");
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   771
    } // if
57e1945104cb Workaround for incorrect assembly code generated by Cg.
Ryan C. Gordon <icculus@icculus.org>
parents: 519
diff changeset
   772
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   773
    info->orig_writemask = info->writemask;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   774
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   775
    if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   776
        return fail(ctx, "Too many tokens");
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   777
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   778
    ctx->tokenbuf[ctx->tokenbufpos++] =
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   779
            ( ((((uint32) 1)) << 31) |
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   780
              ((((uint32) info->regnum) & 0x7ff) << 0) |
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   781
              ((((uint32) info->relative) & 0x1) << 13) |
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   782
              ((((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
   783
              ((((uint32) info->result_shift) & 0xF) << 24) |
500
38ce929323c2 Fixed writemask bits in assembled bytecode.
Ryan C. Gordon <icculus@icculus.org>
parents: 499
diff changeset
   784
              ((((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
   785
              ((((uint32) info->regtype) & 0x7) << 28) |
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   786
              ((((uint32) info->regtype) & 0x18) << 8) );
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   787
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   788
    return 1;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   789
} // parse_destination_token
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   790
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   791
472
e52d487e6d91 Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents: 470
diff changeset
   792
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
   793
                           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
   794
                           SourceMod *srcmod)
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   795
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   796
    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
   797
        fail(ctx, "Incompatible source modifiers");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   798
    else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   799
        *srcmod = ((negate) ? negated : norm);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   800
} // set_source_mod
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   801
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   802
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   803
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
   804
{
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   805
    TokenizerContext *tctx = &ctx->tctx;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   806
    int retval = 1;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   807
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   808
    if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   809
        return fail(ctx, "Too many tokens");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   810
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   811
    // mark this now, so optional relative addressing token is placed second.
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   812
    uint32 *token = &ctx->tokenbuf[ctx->tokenbufpos++];
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   813
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   814
    SourceMod srcmod = SRCMOD_NONE;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   815
    int negate = 0;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   816
    if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   817
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   818
    else if (tokeq(tctx, "1"))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   819
    {
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   820
        if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   821
            return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   822
        else if (!tokeq(tctx, "-"))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   823
            return fail(ctx, "Unexpected value");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   824
        else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   825
            srcmod = SRCMOD_COMPLEMENT;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   826
    } // else
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   827
    else if (tokeq(tctx, "!"))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   828
        srcmod = SRCMOD_NOT;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   829
    else if (tokeq(tctx, "-"))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   830
        negate = 1;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   831
    else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   832
        pushback(ctx);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   833
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   834
    RegisterType regtype;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   835
    int regnum;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   836
    if (parse_register_name(ctx, &regtype, &regnum) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   837
        return FAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   838
    else if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   839
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   840
    else if (!tokeq(tctx, "_"))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   841
        pushback(ctx);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   842
    else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   843
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   844
    else if (tokeq(tctx, "bias"))
472
e52d487e6d91 Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents: 470
diff changeset
   845
        set_source_mod(ctx, negate, SRCMOD_BIAS, SRCMOD_BIASNEGATE, &srcmod);
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   846
    else if (tokeq(tctx, "bx2"))
472
e52d487e6d91 Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents: 470
diff changeset
   847
        set_source_mod(ctx, negate, SRCMOD_SIGN, SRCMOD_SIGNNEGATE, &srcmod);
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   848
    else if (tokeq(tctx, "x2"))
472
e52d487e6d91 Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents: 470
diff changeset
   849
        set_source_mod(ctx, negate, SRCMOD_X2, SRCMOD_X2NEGATE, &srcmod);
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   850
    else if (tokeq(tctx, "dz"))
472
e52d487e6d91 Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents: 470
diff changeset
   851
        set_source_mod(ctx, negate, SRCMOD_DZ, SRCMOD_NONE, &srcmod);
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   852
    else if (tokeq(tctx, "dw"))
472
e52d487e6d91 Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents: 470
diff changeset
   853
        set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod);
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   854
    else if (tokeq(tctx, "abs"))
472
e52d487e6d91 Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents: 470
diff changeset
   855
        set_source_mod(ctx, negate, SRCMOD_ABS, SRCMOD_ABSNEGATE, &srcmod);
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   856
    else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   857
        return fail(ctx, "Invalid source modifier");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   858
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   859
    if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   860
        return FAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   861
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   862
    uint32 relative = 0;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   863
    if (!tokeq(tctx, "["))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   864
        pushback(ctx);  // not relative addressing?
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   865
    else if (!relok)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   866
        return fail(ctx, "Relative addressing not permitted here.");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   867
    else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   868
    {
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   869
        const int rc = parse_source_token_maybe_relative(ctx, 0);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   870
        if (rc == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   871
            return FAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   872
        retval += rc;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   873
        relative = 1;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   874
        if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   875
            return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   876
        else if (!tokeq(tctx, "+"))
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   877
            pushback(ctx);
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   878
        else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   879
            return FAIL;
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   880
        else
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   881
        {
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   882
            if (regnum != 0)  // !!! FIXME: maybe c3[a0.x + 5] is legal and becomes c[a0.x + 8] ?
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   883
                fail(ctx, "Relative addressing with explicit register number.");
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   884
            uint32 ui32 = 0;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   885
            if (!ui32fromstr(tctx->token, &ui32))
482
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   886
                return fail(ctx, "Invalid relative addressing offset");
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   887
            regnum += (int) ui32;
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   888
        } // else
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   889
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   890
        if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
3f740f25bd7e Fixed relative addressing parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 481
diff changeset
   891
            return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   892
        else if (!tokeq(tctx, "]"))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   893
            return fail(ctx, "Expected ']'");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   894
    } // else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   895
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   896
    if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   897
        return FAIL;
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
    uint32 swizzle = 0;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   900
    if (!tokeq(tctx, "."))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   901
    {
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   902
        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
   903
        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
   904
    } // if
491
bcc3c215807a Fixed wrong data from scalar_register().
Ryan C. Gordon <icculus@icculus.org>
parents: 490
diff changeset
   905
    else if (scalar_register(ctx->shader_type, regtype, regnum))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   906
        return fail(ctx, "Swizzle specified for scalar register");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   907
    else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   908
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   909
    else if (tokeq(tctx, ""))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   910
        return fail(ctx, "Invalid swizzle");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   911
    else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   912
    {
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   913
        // deal with shortened form (.x = .xxxx, etc).
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   914
        if (tctx->token[1] == '\0')
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   915
            tctx->token[1] = tctx->token[2] = tctx->token[3] = tctx->token[0];
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   916
        else if (tctx->token[2] == '\0')
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   917
            tctx->token[2] = tctx->token[3] = tctx->token[1];
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   918
        else if (tctx->token[3] == '\0')
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   919
            tctx->token[3] = tctx->token[2];
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   920
        else if (tctx->token[4] != '\0')
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   921
            return fail(ctx, "Invalid swizzle");
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   922
        tctx->token[4] = '\0';
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   923
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   924
        uint32 val;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   925
        int saw_xyzw = 0;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   926
        int saw_rgba = 0;
472
e52d487e6d91 Bunch More Work on the assembler. Feature complete now?
Ryan C. Gordon <icculus@icculus.org>
parents: 470
diff changeset
   927
        int i;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   928
        for (i = 0; i < 4; i++)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   929
        {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   930
            const int component = (int) tctx->token[i];
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   931
            switch (component)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   932
            {
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   933
                case 'x': val = 0; saw_xyzw = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   934
                case 'y': val = 1; saw_xyzw = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   935
                case 'z': val = 2; saw_xyzw = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   936
                case 'w': val = 3; saw_xyzw = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   937
                case 'r': val = 0; saw_rgba = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   938
                case 'g': val = 1; saw_rgba = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   939
                case 'b': val = 2; saw_rgba = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   940
                case 'a': val = 3; saw_rgba = 1; break;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   941
                default: return fail(ctx, "Invalid swizzle");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   942
            } // switch
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   943
            swizzle |= (val << (i * 2));
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   944
        } // for
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   945
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   946
        if (saw_xyzw && saw_rgba)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   947
            return fail(ctx, "Invalid swizzle");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   948
    } // else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   949
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   950
    *token = ( ((((uint32) 1)) << 31) |
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   951
               ((((uint32) regnum) & 0x7ff) << 0) |
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   952
               ((((uint32) relative) & 0x1) << 13) |
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   953
               ((((uint32) swizzle) & 0xFF) << 16) |
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   954
               ((((uint32) srcmod) & 0xF) << 24) |
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   955
               ((((uint32) regtype) & 0x7) << 28) |
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   956
               ((((uint32) regtype) & 0x18) << 8) );
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   957
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   958
    return retval;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   959
} // parse_source_token_maybe_relative
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   960
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   961
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   962
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
   963
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   964
    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
   965
} // parse_source_token
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   966
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   967
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   968
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
   969
{
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   970
    return (isfail(ctx) ? FAIL : 1);
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   971
} // parse_args_NULL
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   972
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   973
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   974
static int parse_num(Context *ctx, const int floatok, uint32 *token)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   975
{
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   976
    TokenizerContext *tctx = &ctx->tctx;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   977
    int32 negative = 1;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   978
    union { float f; int32 si32; uint32 ui32; } cvt;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   979
    cvt.si32 = 0;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   980
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   981
    if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   982
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   983
    else if (tokeq(tctx, "-"))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   984
        negative = -1;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   985
    else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   986
        pushback(ctx);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   987
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   988
    uint32 val = 0;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   989
    if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   990
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   991
    else if (!ui32fromstr(tctx->token, &val))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   992
        return fail(ctx, "Expected number");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   993
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   994
    uint32 fraction = 0;
488
1a318d011c00 Handle endlines on DEF statements.
Ryan C. Gordon <icculus@icculus.org>
parents: 487
diff changeset
   995
    if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   996
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
   997
    else if (!tokeq(tctx, "."))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   998
        pushback(ctx);  // whole number
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
   999
    else if (!floatok)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1000
        return fail(ctx, "Expected whole number");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1001
    else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1002
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
  1003
    else if (!ui32fromstr(tctx->token, &fraction))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1004
        return fail(ctx, "Expected number");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1005
492
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1006
    uint32 exponent = 0;
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1007
    int negexp = 0;
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1008
    if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1009
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
  1010
    else if (!tokeq(tctx, "e"))
492
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1011
        pushback(ctx);
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1012
    else if (!floatok)
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1013
        return fail(ctx, "Exponent on whole number");  // !!! FIXME: illegal?
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1014
    else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1015
        return FAIL;
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1016
    else
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1017
    {
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
  1018
        if (!tokeq(tctx, "-"))
492
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1019
            pushback(ctx);
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1020
        else
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1021
            negexp = 1;
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1022
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1023
        if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1024
            return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
  1025
        else if (!ui32fromstr(tctx->token, &exponent))
492
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1026
            return fail(ctx, "Expected exponent");
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1027
    } // else
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1028
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1029
    if (!floatok)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1030
        cvt.si32 = ((int32) val) * negative;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1031
    else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1032
    {
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1033
        // !!! FIXME: this is lame.
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1034
        char buf[128];
492
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1035
        snprintf(buf, sizeof (buf), "%s%u.%u", (negative < 0) ? "-" : "",
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1036
                 (uint) val, (uint) fraction);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1037
        sscanf(buf, "%f", &cvt.f);
492
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1038
        cvt.f *= (float) negative;
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1039
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1040
        if (exponent)
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1041
        {
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1042
            int i;
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1043
            if (negexp)
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1044
            {
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1045
                for (i = 0; i > exponent; i--)
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1046
                    cvt.f /= 10.0f;
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1047
            } // if
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1048
            else
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1049
            {
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1050
                for (i = 0; i < exponent; i++)
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1051
                    cvt.f *= 10.0f;
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1052
            } // else
29bfa3448549 Handle exponents in number parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 491
diff changeset
  1053
        } // if
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1054
    } // else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1055
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1056
    *token = cvt.ui32;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1057
    return NOFAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1058
} // parse_num
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1059
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1060
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1061
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
  1062
{
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
  1063
    if (parse_destination_token(ctx) == FAIL)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1064
        return FAIL;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1065
    else if (require_comma(ctx) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1066
        return FAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1067
    else if (parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1068
        return FAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1069
    else if (require_comma(ctx) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1070
        return FAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1071
    else if (parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1072
        return FAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1073
    else if (require_comma(ctx) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1074
        return FAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1075
    else if (parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1076
        return FAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1077
    else if (require_comma(ctx) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1078
        return FAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1079
    else if (parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1080
        return FAIL;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1081
    return 6;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1082
} // parse_args_DEFx
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1083
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1084
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1085
static int parse_args_DEF(Context *ctx)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1086
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1087
    return parse_args_DEFx(ctx, 1);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1088
} // parse_args_DEF
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1089
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1090
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1091
static int parse_args_DEFI(Context *ctx)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1092
{
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1093
    return parse_args_DEFx(ctx, 0);
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1094
} // parse_args_DEFI
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1095
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1096
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1097
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
  1098
{
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
  1099
    TokenizerContext *tctx = &ctx->tctx;
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
  1100
    if (parse_destination_token(ctx) == FAIL)
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1101
        return FAIL;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1102
    else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1103
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
  1104
    else if (!tokeq(tctx, ","))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1105
        return fail(ctx, "Expected ','");
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1106
    else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1107
        return FAIL;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
  1108
    else if (tokeq(tctx, "true"))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1109
        ctx->tokenbuf[ctx->tokenbufpos++] = 1;
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
  1110
    else if (tokeq(tctx, "false"))
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1111
        ctx->tokenbuf[ctx->tokenbufpos++] = 0;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1112
    else
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1113
        return fail(ctx, "Expected 'true' or 'false'");
465
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1114
    return 3;
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1115
} // parse_args_DEFB
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1116
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1117
504
d79ae3b98f47 Fixed dcl_2d usage.
Ryan C. Gordon <icculus@icculus.org>
parents: 502
diff changeset
  1118
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
  1119
{
515
58c1a7d98176 First steps of reworking tokenizer.
Ryan C. Gordon <icculus@icculus.org>
parents: 514
diff changeset
  1120
    TokenizerContext *tctx = &ctx->tctx;
470
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1121
    int i;
7d84d3452125 Bunch More Work on the assembler.
Ryan C. Gordon <icculus@icculus.org>
parents: 465
diff changeset
  1122
    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
  1123
    static const char *usagestrs[] = {
0a75f98f785b Initial work on assembler. Not even close to done.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1124
        "position", "blendweight", "blendindices", "normal", "psize",