X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=util.c;h=ecae50516514e45db673cd10966ac408a010b0d1;hp=f1bb1212cb3292f53f5ba09aee7f1f3cebda6509;hb=a68f0fcb355db42acabe72da5939fbd1b04f6016;hpb=b773702a4755f0849ecfaf6c5dfd4221cdd6fb4d diff --git a/util.c b/util.c index f1bb121..ecae505 100644 --- a/util.c +++ b/util.c @@ -22,11 +22,38 @@ * SOFTWARE. */ #include -#include #include #include "gmqcc.h" +/* + * Initially this was handled with a table in the gmqcc.h header, but + * much to my surprise the contents of the table was duplicated for + * each translation unit, causing all these strings to be duplicated + * for every .c file it was included into. This method culls back on + * it. This is a 'utility' function because the executor also depends + * on this for dissasembled bytecode. + */ +const char *util_instr_str[VINSTR_END] = { + "DONE", "MUL_F", "MUL_V", "MUL_FV", + "MUL_VF", "DIV_F", "ADD_F", "ADD_V", + "SUB_F", "SUB_V", "EQ_F", "EQ_V", + "EQ_S", "EQ_E", "EQ_FNC", "NE_F", + "NE_V", "NE_S", "NE_E", "NE_FNC", + "LE", "GE", "LT", "GT", + "LOAD_F", "LOAD_V", "LOAD_S", "LOAD_ENT", + "LOAD_FLD", "LOAD_FNC", "ADDRESS", "STORE_F", + "STORE_V", "STORE_S", "STORE_ENT", "STORE_FLD", + "STORE_FNC", "STOREP_F", "STOREP_V", "STOREP_S", + "STOREP_ENT", "STOREP_FLD", "STOREP_FNC", "RETURN", + "NOT_F", "NOT_V", "NOT_S", "NOT_ENT", + "NOT_FNC", "IF", "IFNOT", "CALL0", + "CALL1", "CALL2", "CALL3", "CALL4", + "CALL5", "CALL6", "CALL7", "CALL8", + "STATE", "GOTO", "AND", "OR", + "BITAND", "BITOR" +}; + void util_debug(const char *area, const char *ms, ...) { va_list va; if (!OPTS_OPTION_BOOL(OPTION_DEBUG)) @@ -198,20 +225,31 @@ uint16_t util_crc16(const char *k, int len, const short clamp) { } #endif -size_t util_strtocmd(const char *in, char *out, size_t outsz) { +/* + * modifier is the match to make and the transpsition from it, while add is the upper-value that determines the + * transposion from uppercase to lower case. + */ +static GMQCC_INLINE size_t util_strtransform(const char *in, char *out, size_t outsz, const char *mod, int add) { size_t sz = 1; - for (; *in && sz < outsz; ++in, ++out, ++sz) - *out = (*in == '-') ? '_' : (isalpha(*in) && !isupper(*in)) ? *in + 'A' - 'a': *in; + for (; *in && sz < outsz; ++in, ++out, ++sz) { + *out = (*in == mod[0]) + ? mod[1] + : (util_isalpha(*in) && util_isupper(*in + add)) + ? *in + add + : *in; + } *out = 0; return sz-1; } +size_t util_strtocmd(const char *in, char *out, size_t outsz) { + return util_strtransform(in, out, outsz, "-_", 'A'-'a'); +} size_t util_strtononcmd(const char *in, char *out, size_t outsz) { - size_t sz = 1; - for (; *in && sz < outsz; ++in, ++out, ++sz) - *out = (*in == '_') ? '-' : (isalpha(*in) && isupper(*in)) ? *in + 'a' - 'A' : *in; - *out = 0; - return sz-1; + return util_strtransform(in, out, outsz, "_-", 'a'-'A'); +} +size_t util_optimizationtostr(const char *in, char *out, size_t outsz) { + return util_strtransform(in, out, outsz, "_ ", 'a'-'A'); } /* @@ -321,7 +359,7 @@ int util_asprintf(char **ret, const char *fmt, ...) { allocated = (char*)mem_a(4096); /* A page must be enough */ strerror_s(allocated, 4096, num); - + vec_push(vector, allocated); return (const char *)allocated; } @@ -426,7 +464,7 @@ static GMQCC_INLINE void mt_generate(void) { * Said loop has been unrolled for MT_SPACE (226 iterations), opposed * to [0, MT_SIZE) (634 iterations). */ - for (i = 0; i < MT_SPACE; ++i) { + for (i = 0; i < MT_SPACE-1; ++i) { y = (0x80000000 & mt_state[i]) | (0x7FFFFFF & mt_state[i + 1]); mt_state[i] = mt_state[i + MT_PERIOD] ^ (y >> 1) ^ matrix[y & 1]; @@ -441,7 +479,7 @@ static GMQCC_INLINE void mt_generate(void) { * = 2*2*3*3*11]) */ i = MT_SPACE; - while (i < MT_SIZE - 1) { + while (i < MT_SIZE-2) { /* * We expand this 11 times .. manually, no macros are required * here. This all fits in the CPU cache.