From 1119a00c82fac9816349aa032e1d5c34a6e2ed56 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sun, 15 Nov 2009 14:02:18 -0500 Subject: [PATCH] Bunch of small tweaks to make this compile as C++ code without errors/warnings. --- mojoshader.c | 68 +++++++++++++++++++++------------------ mojoshader_assembler.c | 52 +++++++++++++++++------------- mojoshader_lexer.re | 2 +- mojoshader_opengl.c | 22 ++++++------- mojoshader_preprocessor.c | 33 ++++++++++--------- 5 files changed, 95 insertions(+), 82 deletions(-) diff --git a/mojoshader.c b/mojoshader.c index 838df105..cdef6159 100644 --- a/mojoshader.c +++ b/mojoshader.c @@ -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; @@ -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++; @@ -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)) { @@ -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)) { @@ -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)) { @@ -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)) { @@ -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)) { @@ -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)) { @@ -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 @@ -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. { @@ -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)) { @@ -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)) { @@ -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)) { @@ -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; @@ -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); @@ -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) | @@ -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), @@ -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; @@ -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; } diff --git a/mojoshader_assembler.c b/mojoshader_assembler.c index 63d71d90..24ccdec1 100644 --- a/mojoshader_assembler.c +++ b/mojoshader_assembler.c @@ -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')) @@ -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--) @@ -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; @@ -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) | @@ -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++]; @@ -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", @@ -1147,7 +1153,7 @@ 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) { @@ -1155,7 +1161,7 @@ static int parse_condition(Context *ctx, uint32 *controls) { if (check_token_segment(ctx, comps[i])) { - *controls = i + 1; + *controls = (uint32) (i + 1); return 1; } // if } // for @@ -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; @@ -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 @@ -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]); @@ -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 diff --git a/mojoshader_lexer.re b/mojoshader_lexer.re index d40b0281..7fd2442c 100644 --- a/mojoshader_lexer.re +++ b/mojoshader_lexer.re @@ -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 diff --git a/mojoshader_opengl.c b/mojoshader_opengl.c index c2a3c117..a4a3f662 100644 --- a/mojoshader_opengl.c +++ b/mojoshader_opengl.c @@ -54,13 +54,13 @@ typedef struct { MOJOSHADER_shaderType shader_type; const MOJOSHADER_uniform *uniform; - GLuint location; + GLint location; } UniformMap; typedef struct { const MOJOSHADER_attribute *attribute; - GLuint location; + GLint location; } AttributeMap; struct MOJOSHADER_glProgram @@ -132,7 +132,7 @@ struct MOJOSHADER_glContext uint32 generation; // This tells us which vertex attribute arrays we have enabled. - int max_attrs; + GLint max_attrs; uint8 want_attr[32]; uint8 have_attr[32]; @@ -436,7 +436,7 @@ static void impl_GLSL_PushConstantArray(MOJOSHADER_glProgram *program, const GLfloat *f) { const GLint loc = ctx->glGetUniformLocation(program->handle, u->name); - if (loc != -1) // not optimized out? + if (loc >= 0) // not optimized out? ctx->glUniform4fv(loc, u->array_count, f); } // impl_GLSL_PushConstantArray @@ -1034,7 +1034,7 @@ int MOJOSHADER_glAvailableProfiles(void *(*lookup)(const char *fnname), if (ctx->have_base_opengl) { - int i; + size_t i; for (i = 0; i < STATICARRAYLEN(profile_priorities); i++) { const char *profile = profile_priorities[i]; @@ -1406,7 +1406,7 @@ static void lookup_samplers(MOJOSHADER_glProgram *program, for (i = 0; i < pd->sampler_count; i++) { const GLint loc = ctx->profileGetSamplerLocation(program, shader, i); - if (loc != -1) // maybe the Sampler was optimized out? + if (loc >= 0) // maybe the Sampler was optimized out? ctx->profilePushSampler(loc, s[i].index); } // for } // lookup_samplers @@ -1421,14 +1421,14 @@ static int lookup_attributes(MOJOSHADER_glProgram *program) for (i = 0; i < pd->attribute_count; i++) { const GLint loc = ctx->profileGetAttribLocation(program, i); - if (loc != -1) // maybe the Attribute was optimized out? + if (loc >= 0) // maybe the Attribute was optimized out? { AttributeMap *map = &program->attributes[program->attribute_count]; map->attribute = &a[i]; - map->location = (GLuint) loc; + map->location = loc; program->attribute_count++; - if (loc > STATICARRAYLEN(ctx->want_attr)) + if (((size_t)loc) > STATICARRAYLEN(ctx->want_attr)) { assert(0 && "Static array is too small."); // laziness fail. return 0; @@ -1722,7 +1722,7 @@ void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage, const GLenum gl_type = opengl_attr_type(type); const GLboolean norm = (normalized) ? GL_TRUE : GL_FALSE; const int count = ctx->bound_program->attribute_count; - GLuint gl_index = 0; + GLint gl_index = 0; int i; for (i = 0; i < count; i++) @@ -1862,7 +1862,7 @@ void MOJOSHADER_glProgramReady(void) { const size_t count = size; const uint8 *b = &srcb[index]; - int i; + size_t i; for (i = 0; i < count; i++) dstb[i] = (GLint) b[i]; dstb += count; diff --git a/mojoshader_preprocessor.c b/mojoshader_preprocessor.c index 4f972658..48daf3f3 100644 --- a/mojoshader_preprocessor.c +++ b/mojoshader_preprocessor.c @@ -306,7 +306,7 @@ static int add_to_buffer(Buffer *buffer, const char *data, static char *flatten_buffer(Buffer *buffer, MOJOSHADER_malloc m, void *d) { - char *retval = m(buffer->total_bytes + 1, d); + char *retval = (char *) m(buffer->total_bytes + 1, d); if (retval == NULL) return NULL; BufferList *item = &buffer->head; @@ -490,7 +490,7 @@ static const Define *find_define_by_token(Context *ctx) static void put_all_defines(Context *ctx) { - int i; + size_t i; for (i = 0; i < STATICARRAYLEN(ctx->define_hashtable); i++) { Define *bucket = ctx->define_hashtable[i]; @@ -651,7 +651,7 @@ Preprocessor *preprocessor_start(const char *fname, const char *source, MOJOSHADER_malloc m, MOJOSHADER_free f, void *d) { int okay = 1; - int i = 0; + unsigned int i = 0; // the preprocessor is internal-only, so we verify all these are != NULL. assert(m != NULL); @@ -946,6 +946,7 @@ static void handle_pp_error(Context *ctx) static void handle_pp_define(Context *ctx) { IncludeState *state = ctx->include_stack; + int done = 0; if (lexer(state) != TOKEN_IDENTIFIER) { @@ -974,6 +975,9 @@ static void handle_pp_define(Context *ctx) int params = 0; char **idents = NULL; + MOJOSHADER_malloc m = ctx->malloc; + void *d = ctx->malloc_data; + static const char space = ' '; if (state->tokenval == ((Token) ' ')) lexer(state); // skip it. @@ -1047,12 +1051,7 @@ static void handle_pp_define(Context *ctx) Buffer buffer; init_buffer(&buffer); - MOJOSHADER_malloc m = ctx->malloc; - void *d = ctx->malloc_data; - static const char space = ' '; - state->report_whitespace = 1; - int done = 0; while ((!done) && (!ctx->out_of_memory)) { const Token token = lexer(state); @@ -1196,6 +1195,10 @@ static int handle_pp_identifier(Context *ctx) return 0; } // if + const char *fname = NULL; + const char *val = NULL; + size_t vallen = 0; + IncludeState *state = ctx->include_stack; char *sym = (char *) alloca(state->tokenlen+1); memcpy(sym, state->token, state->tokenlen); @@ -1311,9 +1314,9 @@ static int handle_pp_identifier(Context *ctx) } // if } // if - const char *val = def->definition; - const size_t vallen = strlen(val); - const char *fname = state->filename; + val = def->definition; + vallen = strlen(val); + fname = state->filename; if (!push_source(ctx, fname, val, vallen, state->line, NULL, params)) { assert(ctx->out_of_memory); @@ -1349,7 +1352,7 @@ static int find_precedence(const Token token) { 11, ((Token) '!') }, { 11, ((Token) '~') }, }; - int i; + size_t i; for (i = 0; i < STATICARRAYLEN(ops); i++) { if (ops[i].token == token) @@ -1369,7 +1372,7 @@ typedef struct RpnTokens static long interpret_rpn(const RpnTokens *tokens, int tokencount, int *error) { long stack[128]; - int stacksize = 0; + size_t stacksize = 0; *error = 1; @@ -1449,8 +1452,8 @@ static int reduce_pp_expression(Context *ctx) RpnTokens output[128]; Token stack[64]; Token previous_token = TOKEN_UNKNOWN; - int outputsize = 0; - int stacksize = 0; + size_t outputsize = 0; + size_t stacksize = 0; int matched = 0; int done = 0;