Skip to content

Commit

Permalink
Bunch of small tweaks to make this compile as C++ code without errors…
Browse files Browse the repository at this point in the history
…/warnings.
  • Loading branch information
icculus committed Nov 15, 2009
1 parent 8685b2e commit 1119a00
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 82 deletions.
68 changes: 36 additions & 32 deletions mojoshader.c
Expand Up @@ -50,7 +50,7 @@ typedef struct RegisterList
RegisterType regtype;
int regnum;
MOJOSHADER_usage usage;
int index;
unsigned int index;
int writemask;
int misc;
const VariableList *array;
Expand Down Expand Up @@ -283,7 +283,7 @@ static inline void Free(Context *ctx, void *ptr)

static inline void push_output(Context *ctx, OutputList *section)
{
assert(ctx->output_stack_len < STATICARRAYLEN(ctx->output_stack));
assert(ctx->output_stack_len < (int) (STATICARRAYLEN(ctx->output_stack)));
ctx->output_stack[ctx->output_stack_len] = ctx->output;
ctx->indent_stack[ctx->output_stack_len] = ctx->indent;
ctx->output_stack_len++;
Expand Down Expand Up @@ -938,7 +938,7 @@ static const char *make_D3D_srcarg_string_in_buf(Context *ctx,
} // if

char swizzle_str[6];
int i = 0;
size_t i = 0;
const int scalar = scalar_register(ctx->shader_type, arg->regtype, arg->regnum);
if (!scalar && !no_swizzle(arg->swizzle))
{
Expand Down Expand Up @@ -995,7 +995,7 @@ static const char *make_D3D_destarg_string(Context *ctx)
} // if

char writemask_str[6];
int i = 0;
size_t i = 0;
const int scalar = scalar_register(ctx->shader_type, arg->regtype, arg->regnum);
if (!scalar && !writemask_xyzw(arg->writemask))
{
Expand Down Expand Up @@ -1030,7 +1030,7 @@ static const char *make_D3D_destarg_string(Context *ctx)
} // make_D3D_destarg_string


static const char *make_D3D_srcarg_string(Context *ctx, const int idx)
static const char *make_D3D_srcarg_string(Context *ctx, const size_t idx)
{
if (idx >= STATICARRAYLEN(ctx->source_args))
{
Expand Down Expand Up @@ -1730,7 +1730,7 @@ static const char *get_GLSL_destarg_varname(Context *ctx)
return get_GLSL_varname(ctx, arg->regtype, arg->regnum);
} // get_GLSL_destarg_varname

static const char *get_GLSL_srcarg_varname(Context *ctx, int idx)
static const char *get_GLSL_srcarg_varname(Context *ctx, const size_t idx)
{
if (idx >= STATICARRAYLEN(ctx->source_args))
{
Expand Down Expand Up @@ -1808,7 +1808,7 @@ static const char *make_GLSL_destarg_assign(Context *ctx, const char *fmt, ...)
arg->regnum, regnum_str,
sizeof (regnum_str));
char writemask_str[6];
int i = 0;
size_t i = 0;
const int scalar = scalar_register(ctx->shader_type, arg->regtype, arg->regnum);
if (!scalar && !writemask_xyzw(arg->writemask))
{
Expand Down Expand Up @@ -1862,8 +1862,8 @@ static char *make_GLSL_swizzle_string(char *swiz_str, const size_t strsize,
} // make_GLSL_swizzle_string


static char *make_GLSL_srcarg_string(Context *ctx, const int idx,
const int writemask)
static const char *make_GLSL_srcarg_string(Context *ctx, const size_t idx,
const int writemask)
{
if (idx >= STATICARRAYLEN(ctx->source_args))
{
Expand Down Expand Up @@ -2020,47 +2020,47 @@ static char *make_GLSL_srcarg_string(Context *ctx, const int idx,
return retval;
} // make_GLSL_srcarg_string

static inline char *make_GLSL_srcarg_string_x(Context *ctx, const int idx)
static inline const char *make_GLSL_srcarg_string_x(Context *ctx, const size_t idx)
{
return make_GLSL_srcarg_string(ctx, idx, (1 << 0));
} // make_GLSL_srcarg_string_x

static inline char *make_GLSL_srcarg_string_y(Context *ctx, const int idx)
static inline const char *make_GLSL_srcarg_string_y(Context *ctx, const size_t idx)
{
return make_GLSL_srcarg_string(ctx, idx, (1 << 1));
} // make_GLSL_srcarg_string_y

static inline char *make_GLSL_srcarg_string_z(Context *ctx, const int idx)
static inline const char *make_GLSL_srcarg_string_z(Context *ctx, const size_t idx)
{
return make_GLSL_srcarg_string(ctx, idx, (1 << 2));
} // make_GLSL_srcarg_string_z

static inline char *make_GLSL_srcarg_string_w(Context *ctx, const int idx)
static inline const char *make_GLSL_srcarg_string_w(Context *ctx, const size_t idx)
{
return make_GLSL_srcarg_string(ctx, idx, (1 << 3));
} // make_GLSL_srcarg_string_w

static inline char *make_GLSL_srcarg_string_scalar(Context *ctx, const int idx)
static inline const char *make_GLSL_srcarg_string_scalar(Context *ctx, const size_t idx)
{
return make_GLSL_srcarg_string_x(ctx, idx);
} // make_GLSL_srcarg_string_scalar

static inline char *make_GLSL_srcarg_string_full(Context *ctx, const int idx)
static inline const char *make_GLSL_srcarg_string_full(Context *ctx, const size_t idx)
{
return make_GLSL_srcarg_string(ctx, idx, 0xF);
} // make_GLSL_srcarg_string_full

static inline char *make_GLSL_srcarg_string_masked(Context *ctx, const int idx)
static inline const char *make_GLSL_srcarg_string_masked(Context *ctx, const size_t idx)
{
return make_GLSL_srcarg_string(ctx, idx, ctx->dest_arg.writemask);
} // make_GLSL_srcarg_string_masked

static inline char *make_GLSL_srcarg_string_vec3(Context *ctx, const int idx)
static inline const char *make_GLSL_srcarg_string_vec3(Context *ctx, const size_t idx)
{
return make_GLSL_srcarg_string(ctx, idx, 0x7);
} // make_GLSL_srcarg_string_vec3

static inline char *make_GLSL_srcarg_string_vec2(Context *ctx, const int idx)
static inline const char *make_GLSL_srcarg_string_vec2(Context *ctx, const size_t idx)
{
return make_GLSL_srcarg_string(ctx, idx, 0x3);
} // make_GLSL_srcarg_string_vec2
Expand Down Expand Up @@ -3565,7 +3565,7 @@ static const char *make_ARB1_srcarg_string_in_buf(Context *ctx,
} // switch

char swizzle_str[6];
int i = 0;
size_t i = 0;

if (ctx->support_nv4) // vFace must be output as "vFace.x" in nv4.
{
Expand Down Expand Up @@ -3612,7 +3612,7 @@ static const char *get_ARB1_destarg_varname(Context *ctx)
return get_ARB1_varname(ctx, arg->regtype, arg->regnum);
} // get_ARB1_destarg_varname

static const char *get_ARB1_srcarg_varname(Context *ctx, int idx)
static const char *get_ARB1_srcarg_varname(Context *ctx, const size_t idx)
{
if (idx >= STATICARRAYLEN(ctx->source_args))
{
Expand Down Expand Up @@ -3666,7 +3666,7 @@ static const char *make_ARB1_destarg_string(Context *ctx)
} // if

char writemask_str[6];
int i = 0;
size_t i = 0;
const int scalar = scalar_register(ctx->shader_type, arg->regtype, arg->regnum);
if (!scalar && !writemask_xyzw(arg->writemask))
{
Expand Down Expand Up @@ -3738,7 +3738,7 @@ static void emit_ARB1_dest_modifiers(Context *ctx)
} // emit_ARB1_dest_modifiers


static const char *make_ARB1_srcarg_string(Context *ctx, const int idx)
static const char *make_ARB1_srcarg_string(Context *ctx, const size_t idx)
{
if (idx >= STATICARRAYLEN(ctx->source_args))
{
Expand Down Expand Up @@ -4641,7 +4641,9 @@ static void emit_ARB1_REP(Context *ctx)
const char *topbranch = get_ARB1_branch_label_name(ctx, toplabel);
const char *failbranch = get_ARB1_branch_label_name(ctx, faillabel);

assert(ctx->branch_labels_stack_index < STATICARRAYLEN(ctx->branch_labels_stack)-1);
assert(((size_t) ctx->branch_labels_stack_index) <
STATICARRAYLEN(ctx->branch_labels_stack)-1);

ctx->branch_labels_stack[ctx->branch_labels_stack_index++] = toplabel;
ctx->branch_labels_stack[ctx->branch_labels_stack_index++] = faillabel;

Expand Down Expand Up @@ -4701,7 +4703,9 @@ static void nv2_if(Context *ctx)
const int label = allocate_branch_label(ctx);
const char *failbranch = get_ARB1_branch_label_name(ctx, label);

assert(ctx->branch_labels_stack_index < STATICARRAYLEN(ctx->branch_labels_stack));
assert(((size_t) ctx->branch_labels_stack_index)
< STATICARRAYLEN(ctx->branch_labels_stack));

ctx->branch_labels_stack[ctx->branch_labels_stack_index++] = label;

output_line(ctx, "BRA %s (EQ.x);", failbranch);
Expand Down Expand Up @@ -5368,10 +5372,10 @@ static int adjust_swizzle(const Context *ctx, const RegisterType regtype,
if (reg == NULL)
return swizzle;

int i;
const MOJOSHADER_swizzle *swiz = ctx->swizzles;
for (i = 0; i < ctx->swizzles_count; i++, swiz++)
size_t i;
for (i = 0; i < ctx->swizzles_count; i++)
{
const MOJOSHADER_swizzle *swiz = &ctx->swizzles[i];
if ((swiz->usage == reg->usage) && (swiz->index == reg->index))
{
return ( (((int)(swiz->swizzles[((swizzle >> 0) & 0x3)])) << 0) |
Expand Down Expand Up @@ -6635,7 +6639,7 @@ static int parse_instruction_token(Context *ctx)
} // if
else
{
if (retval != (insttoks+1))
if (((uint32)retval) != (insttoks+1))
{
failf(ctx, "wrong token count (%u, not %u) for opcode '%s'.",
(uint) retval, (uint) (insttoks+1),
Expand Down Expand Up @@ -6849,7 +6853,7 @@ static int parse_token(Context *ctx)

static int find_profile_id(const char *profile)
{
int i;
size_t i;
for (i = 0; i < STATICARRAYLEN(profileMap); i++)
{
const char *name = profileMap[i].from;
Expand Down Expand Up @@ -7596,11 +7600,11 @@ static void process_definitions(Context *ctx)

static void verify_swizzles(Context *ctx)
{
int i;
size_t i;
const char *failmsg = "invalid swizzle";
const MOJOSHADER_swizzle *swiz = ctx->swizzles;
for (i = 0; i < ctx->swizzles_count; i++, swiz++)
for (i = 0; i < ctx->swizzles_count; i++)
{
const MOJOSHADER_swizzle *swiz = &ctx->swizzles[i];
if (swiz->swizzles[0] > 3) { fail(ctx, failmsg); return; }
if (swiz->swizzles[1] > 3) { fail(ctx, failmsg); return; }
if (swiz->swizzles[2] > 3) { fail(ctx, failmsg); return; }
Expand Down
52 changes: 29 additions & 23 deletions mojoshader_assembler.c
Expand Up @@ -357,7 +357,7 @@ static int check_token(Context *ctx, const char *str)

static int ui32fromtoken(Context *ctx, uint32 *_val)
{
int i;
unsigned int i;
for (i = 0; i < ctx->tokenlen; i++)
{
if ((ctx->token[i] < '0') || (ctx->token[i] > '9'))
Expand All @@ -370,7 +370,7 @@ static int ui32fromtoken(Context *ctx, uint32 *_val)
return 0;
} // if

const int len = i;
const unsigned int len = i;
uint32 val = 0;
uint32 mult = 1;
while (i--)
Expand Down Expand Up @@ -540,6 +540,18 @@ static void set_result_shift(Context *ctx, DestArgInfo *info, const int val)
} // set_result_shift


static inline int tokenbuf_overflow(Context *ctx)
{
if ( ctx->tokenbufpos >= ((int) (STATICARRAYLEN(ctx->tokenbuf))) )
{
fail(ctx, "Too many tokens");
return 1;
} // if

return 0;
} // tokenbuf_overflow


static int parse_destination_token(Context *ctx)
{
DestArgInfo *info = &ctx->dest_arg;
Expand Down Expand Up @@ -641,11 +653,8 @@ static int parse_destination_token(Context *ctx)

info->orig_writemask = info->writemask;

if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
{
fail(ctx, "Too many tokens");
if (tokenbuf_overflow(ctx))
return 1;
} // if

ctx->tokenbuf[ctx->tokenbufpos++] =
( ((((uint32) 1)) << 31) |
Expand Down Expand Up @@ -676,11 +685,8 @@ static int parse_source_token_maybe_relative(Context *ctx, const int relok)
{
int retval = 1;

if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
{
fail(ctx, "Too many tokens");
if (tokenbuf_overflow(ctx))
return 0;
} // if

// mark this now, so optional relative addressing token is placed second.
uint32 *outtoken = &ctx->tokenbuf[ctx->tokenbufpos++];
Expand Down Expand Up @@ -943,7 +949,7 @@ static int parse_args_DEFB(Context *ctx)

static int parse_dcl_usage(Context *ctx, uint32 *val, int *issampler)
{
int i;
size_t i;
static const char *samplerusagestrs[] = { "_2d", "_cube", "_volume" };
static const char *usagestrs[] = {
"_position", "_blendweight", "_blendindices", "_normal", "_psize",
Expand Down Expand Up @@ -1147,15 +1153,15 @@ static const Instruction instructions[] =
static int parse_condition(Context *ctx, uint32 *controls)
{
static const char *comps[] = { "_gt", "_eq", "_ge", "_lt", "_ne", "_le" };
int i;
size_t i;

if (ctx->tokenlen >= 3)
{
for (i = 1; i < STATICARRAYLEN(comps); i++)
{
if (check_token_segment(ctx, comps[i]))
{
*controls = i + 1;
*controls = (uint32) (i + 1);
return 1;
} // if
} // for
Expand Down Expand Up @@ -1197,7 +1203,7 @@ static int parse_instruction_token(Context *ctx, Token token)

else // find the instruction.
{
int i;
size_t i;
for (i = 0; i < STATICARRAYLEN(instructions); i++)
{
const char *opcode_string = instructions[i].opcode_string;
Expand Down Expand Up @@ -1558,18 +1564,18 @@ static uint32 add_ctab_bytes(Context *ctx, const uint8 *bytes, const size_t len)
const size_t extra = CTAB_SIZE + sizeof (uint32);
if (len <= (ctx->ctab_len - extra))
{
void *ptr = ctx->ctab + extra;
uint8 *ptr = ctx->ctab + extra;
if (len == 0)
return ( (uint32) (((uint8 *) ptr) - ctx->ctab) ) - sizeof (uint32);
else if ((len == 1) && ((ptr = memchr(ptr, bytes[0], ctx->ctab_len - len)) != NULL))
return ( (uint32) (((uint8 *) ptr) - ctx->ctab) ) - sizeof (uint32);
return ( (uint32) (ptr - ctx->ctab) ) - sizeof (uint32);
else if ((len == 1) && ((ptr = (uint8 *) memchr(ptr, bytes[0], ctx->ctab_len - len)) != NULL))
return ( (uint32) (ptr - ctx->ctab) ) - sizeof (uint32);
else // search for the string of bytes...
{
while ((ptr = memchr(ptr, bytes[0], ctx->ctab_len - len)) != NULL)
while ((ptr = (uint8 *) memchr(ptr, bytes[0], ctx->ctab_len - len)) != NULL)
{
if (memcmp(ptr, bytes, len) == 0) // this is it?
return ( (uint32) (((uint8 *) ptr) - ctx->ctab) ) - sizeof (uint32);
ptr++;
return ( (uint32) (ptr - ctx->ctab) ) - sizeof (uint32);
ptr++; // !!! FIXME: should this be "ptr += len;" ?
} // while
} // else
} // if
Expand Down Expand Up @@ -1721,7 +1727,7 @@ static void output_comments(Context *ctx, const char **comments,
else
output_comment_string(ctx, creator);

int i;
unsigned int i;
for (i = 0; i < comment_count; i++)
output_comment_string(ctx, comments[i]);

Expand Down Expand Up @@ -1791,7 +1797,7 @@ const MOJOSHADER_parseData *MOJOSHADER_assemble(const char *filename,
assert(retval != &MOJOSHADER_out_of_mem_data);
assert((error->error_position % sizeof (uint32)) == 0);

const int pos = error->error_position / sizeof (uint32);
const size_t pos = error->error_position / sizeof (uint32);
if (pos >= ctx->output_len)
error->error_position = -1; // oh well.
else
Expand Down
2 changes: 1 addition & 1 deletion mojoshader_lexer.re
Expand Up @@ -25,7 +25,7 @@
typedef unsigned char uchar;

/*!max:re2c */
#define RET(t) do { return update_state(s, eoi, cursor, token, t); } while (0)
#define RET(t) return update_state(s, eoi, cursor, token, (Token) t)
#define YYCTYPE uchar
#define YYCURSOR cursor
#define YYLIMIT limit
Expand Down

0 comments on commit 1119a00

Please sign in to comment.