]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - fold.cpp
Forgot about this file
[xonotic/gmqcc.git] / fold.cpp
index 64b20d94e7662f7d1802293c39ebe670e2803da1..a4bc8780270d017d8b09f232d667c2238833617b 100644 (file)
--- a/fold.cpp
+++ b/fold.cpp
  */
 typedef uint32_t sfloat_t;
 
-typedef union {
+union sfloat_cast_t {
     qcfloat_t f;
     sfloat_t  s;
-} sfloat_cast_t;
+};
 
 /* Exception flags */
-typedef enum {
+enum sfloat_exceptionflags_t {
     SFLOAT_NOEXCEPT  = 0,
     SFLOAT_INVALID   = 1,
     SFLOAT_DIVBYZERO = 4,
     SFLOAT_OVERFLOW  = 8,
     SFLOAT_UNDERFLOW = 16,
     SFLOAT_INEXACT   = 32
-} sfloat_exceptionflags_t;
+};
 
 /* Rounding modes */
-typedef enum {
+enum sfloat_roundingmode_t {
     SFLOAT_ROUND_NEAREST_EVEN,
     SFLOAT_ROUND_DOWN,
     SFLOAT_ROUND_UP,
     SFLOAT_ROUND_TO_ZERO
-} sfloat_roundingmode_t;
+};
 
 /* Underflow tininess-detection mode */
-typedef enum {
+enum sfloat_tdetect_t {
     SFLOAT_TAFTER,
     SFLOAT_TBEFORE
-} sfloat_tdetect_t;
+};
 
-typedef struct {
-    sfloat_roundingmode_t   roundingmode;
+struct sfloat_state_t {
+    sfloat_roundingmode_t roundingmode;
     sfloat_exceptionflags_t exceptionflags;
-    sfloat_tdetect_t        tiny;
-} sfloat_state_t;
+    sfloat_tdetect_t tiny;
+};
 
 /* Counts the number of leading zero bits before the most-significand one bit. */
 #ifdef _MSC_VER
@@ -544,22 +544,22 @@ static GMQCC_INLINE void sfloat_init(sfloat_state_t *state) {
  *
  * TODO: gcc/clang hinting for autovectorization
  */
-typedef enum {
+enum vec3_comp_t {
     VEC_COMP_X = 1 << 0,
     VEC_COMP_Y = 1 << 1,
     VEC_COMP_Z = 1 << 2
-} vec3_comp_t;
+};
 
-typedef struct {
+struct vec3_soft_t {
     sfloat_cast_t x;
     sfloat_cast_t y;
     sfloat_cast_t z;
-} vec3_soft_t;
+};
 
-typedef struct {
-    vec3_comp_t    faults;
+struct vec3_soft_state_t {
+    vec3_comp_t faults;
     sfloat_state_t state[3];
-} vec3_soft_state_t;
+};
 
 static GMQCC_INLINE vec3_soft_t vec3_soft_convert(vec3_t vec) {
     vec3_soft_t soft;
@@ -651,9 +651,9 @@ static GMQCC_INLINE vec3_t vec3_neg(lex_ctx_t ctx, vec3_t a) {
     sfloat_neg(&s[1], v[1].s);
     sfloat_neg(&s[2], v[2].s);
 
-    sfloat_check(ctx, &s[0], NULL);
-    sfloat_check(ctx, &s[1], NULL);
-    sfloat_check(ctx, &s[2], NULL);
+    sfloat_check(ctx, &s[0], nullptr);
+    sfloat_check(ctx, &s[1], nullptr);
+    sfloat_check(ctx, &s[2], nullptr);
 
 end:
     out.x = -a.x;
@@ -742,11 +742,11 @@ static GMQCC_INLINE qcfloat_t vec3_mulvv(lex_ctx_t ctx, vec3_t a, vec3_t b) {
     r[3] = sfloat_add(&s[3], r[0],   r[1]);
     r[4] = sfloat_add(&s[4], r[3],   r[2]);
 
-    sfloat_check(ctx, &s[0], NULL);
-    sfloat_check(ctx, &s[1], NULL);
-    sfloat_check(ctx, &s[2], NULL);
-    sfloat_check(ctx, &s[3], NULL);
-    sfloat_check(ctx, &s[4], NULL);
+    sfloat_check(ctx, &s[0], nullptr);
+    sfloat_check(ctx, &s[1], nullptr);
+    sfloat_check(ctx, &s[2], nullptr);
+    sfloat_check(ctx, &s[3], nullptr);
+    sfloat_check(ctx, &s[4], nullptr);
 
 end:
     return (a.x * b.x + a.y * b.y + a.z * b.z);
@@ -837,12 +837,12 @@ static GMQCC_INLINE vec3_t vec3_cross(lex_ctx_t ctx, vec3_t a, vec3_t b) {
     r[7] = sfloat_sub(&s[7], r[2],   r[3]);
     r[8] = sfloat_sub(&s[8], r[4],   r[5]);
 
-    sfloat_check(ctx, &s[0], NULL);
-    sfloat_check(ctx, &s[1], NULL);
-    sfloat_check(ctx, &s[2], NULL);
-    sfloat_check(ctx, &s[3], NULL);
-    sfloat_check(ctx, &s[4], NULL);
-    sfloat_check(ctx, &s[5], NULL);
+    sfloat_check(ctx, &s[0], nullptr);
+    sfloat_check(ctx, &s[1], nullptr);
+    sfloat_check(ctx, &s[2], nullptr);
+    sfloat_check(ctx, &s[3], nullptr);
+    sfloat_check(ctx, &s[4], nullptr);
+    sfloat_check(ctx, &s[5], nullptr);
     sfloat_check(ctx, &s[6], "x");
     sfloat_check(ctx, &s[7], "y");
     sfloat_check(ctx, &s[8], "z");
@@ -858,7 +858,6 @@ static lex_ctx_t fold_ctx(fold_t *fold) {
     lex_ctx_t ctx;
     if (fold->parser->lex)
         return parser_ctx(fold->parser);
-
     memset(&ctx, 0, sizeof(ctx));
     return ctx;
 }
@@ -898,11 +897,8 @@ static GMQCC_INLINE bool fold_immediate_true(fold_t *fold, ast_value *v) {
 #define fold_immvalue_string(E) ((E)->constval.vstring)
 
 fold_t *fold_init(parser_t *parser) {
-    fold_t *fold                 = (fold_t*)mem_a(sizeof(fold_t));
-    fold->parser                 = parser;
-    fold->imm_float              = NULL;
-    fold->imm_vector             = NULL;
-    fold->imm_string             = NULL;
+    fold_t *fold = new fold_t;
+    fold->parser = parser;
     fold->imm_string_untranslate = util_htnew(FOLD_STRING_UNTRANSLATE_HTSIZE);
     fold->imm_string_dotranslate = util_htnew(FOLD_STRING_DOTRANSLATE_HTSIZE);
 
@@ -910,10 +906,10 @@ fold_t *fold_init(parser_t *parser) {
      * prime the tables with common constant values at constant
      * locations.
      */
-    (void)fold_constgen_float (fold,  0.0f, false);
-    (void)fold_constgen_float (fold,  1.0f, false);
-    (void)fold_constgen_float (fold, -1.0f, false);
-    (void)fold_constgen_float (fold,  2.0f, false);
+    (void)fold_constgen_float(fold,  0.0f, false);
+    (void)fold_constgen_float(fold,  1.0f, false);
+    (void)fold_constgen_float(fold, -1.0f, false);
+    (void)fold_constgen_float(fold,  2.0f, false);
 
     (void)fold_constgen_vector(fold, vec3_create(0.0f, 0.0f, 0.0f));
     (void)fold_constgen_vector(fold, vec3_create(-1.0f, -1.0f, -1.0f));
@@ -922,19 +918,15 @@ fold_t *fold_init(parser_t *parser) {
 }
 
 bool fold_generate(fold_t *fold, ir_builder *ir) {
-    /* generate globals for immediate folded values */
-    size_t     i;
+    // generate globals for immediate folded values
     ast_value *cur;
-
-    for (i = 0; i < vec_size(fold->imm_float);   ++i)
-        if (!ast_global_codegen ((cur = fold->imm_float[i]), ir, false)) goto err;
-    for (i = 0; i < vec_size(fold->imm_vector);  ++i)
-        if (!ast_global_codegen((cur = fold->imm_vector[i]), ir, false)) goto err;
-    for (i = 0; i < vec_size(fold->imm_string);  ++i)
-        if (!ast_global_codegen((cur = fold->imm_string[i]), ir, false)) goto err;
-
+    for (auto &it : fold->imm_float)
+        if (!ast_global_codegen((cur = it), ir, false)) goto err;
+    for (auto &it : fold->imm_vector)
+        if (!ast_global_codegen((cur = it), ir, false)) goto err;
+    for (auto &it : fold->imm_string)
+        if (!ast_global_codegen((cur = it), ir, false)) goto err;
     return true;
-
 err:
     con_out("failed to generate global %s\n", cur->name);
     ir_builder_delete(ir);
@@ -942,65 +934,51 @@ err:
 }
 
 void fold_cleanup(fold_t *fold) {
-    size_t i;
-
-    for (i = 0; i < vec_size(fold->imm_float);  ++i) ast_delete(fold->imm_float[i]);
-    for (i = 0; i < vec_size(fold->imm_vector); ++i) ast_delete(fold->imm_vector[i]);
-    for (i = 0; i < vec_size(fold->imm_string); ++i) ast_delete(fold->imm_string[i]);
-
-    vec_free(fold->imm_float);
-    vec_free(fold->imm_vector);
-    vec_free(fold->imm_string);
+    for (auto &it : fold->imm_float) ast_delete(it);
+    for (auto &it : fold->imm_vector) ast_delete(it);
+    for (auto &it : fold->imm_string) ast_delete(it);
 
     util_htdel(fold->imm_string_untranslate);
     util_htdel(fold->imm_string_dotranslate);
 
-    mem_d(fold);
+    delete fold;
 }
 
 ast_expression *fold_constgen_float(fold_t *fold, qcfloat_t value, bool inexact) {
-    ast_value  *out = NULL;
-    size_t      i;
-
-    for (i = 0; i < vec_size(fold->imm_float); i++) {
-        if (!memcmp(&fold->imm_float[i]->constval.vfloat, &value, sizeof(qcfloat_t)))
-            return (ast_expression*)fold->imm_float[i];
-    }
-
-    out                  = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_FLOAT);
-    out->cvq             = CV_CONST;
-    out->hasvalue        = true;
-    out->inexact         = inexact;
+    for (auto &it : fold->imm_float)
+        if (!memcmp(&it->constval.vfloat, &value, sizeof(qcfloat_t)))
+            return (ast_expression*)it;
+
+    ast_value *out  = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_FLOAT);
+    out->cvq = CV_CONST;
+    out->hasvalue = true;
+    out->inexact = inexact;
     out->constval.vfloat = value;
 
-    vec_push(fold->imm_float, out);
+    fold->imm_float.push_back(out);
 
     return (ast_expression*)out;
 }
 
 ast_expression *fold_constgen_vector(fold_t *fold, vec3_t value) {
-    ast_value *out;
-    size_t     i;
+    for (auto &it : fold->imm_vector)
+        if (vec3_cmp(it->constval.vvec, value))
+            return (ast_expression*)it;
 
-    for (i = 0; i < vec_size(fold->imm_vector); i++) {
-        if (vec3_cmp(fold->imm_vector[i]->constval.vvec, value))
-            return (ast_expression*)fold->imm_vector[i];
-    }
-
-    out                = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_VECTOR);
-    out->cvq           = CV_CONST;
-    out->hasvalue      = true;
+    ast_value *out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_VECTOR);
+    out->cvq = CV_CONST;
+    out->hasvalue = true;
     out->constval.vvec = value;
 
-    vec_push(fold->imm_vector, out);
+    fold->imm_vector.push_back(out);
 
     return (ast_expression*)out;
 }
 
 ast_expression *fold_constgen_string(fold_t *fold, const char *str, bool translate) {
     hash_table_t *table = (translate) ? fold->imm_string_untranslate : fold->imm_string_dotranslate;
-    ast_value    *out   = NULL;
-    size_t        hash  = util_hthash(table, str);
+    ast_value *out = nullptr;
+    size_t hash = util_hthash(table, str);
 
     if ((out = (ast_value*)util_htgeth(table, str, hash)))
         return (ast_expression*)out;
@@ -1008,17 +986,18 @@ ast_expression *fold_constgen_string(fold_t *fold, const char *str, bool transla
     if (translate) {
         char name[32];
         util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(fold->parser->translated++));
-        out                    = ast_value_new(parser_ctx(fold->parser), name, TYPE_STRING);
+        out = ast_value_new(parser_ctx(fold->parser), name, TYPE_STRING);
         out->expression.flags |= AST_FLAG_INCLUDE_DEF; /* def needs to be included for translatables */
-    } else
-        out                    = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_STRING);
+    } else {
+        out = ast_value_new(fold_ctx(fold), "#IMMEDIATE", TYPE_STRING);
+    }
 
-    out->cvq              = CV_CONST;
-    out->hasvalue         = true;
-    out->isimm            = true;
+    out->cvq = CV_CONST;
+    out->hasvalue = true;
+    out->isimm = true;
     out->constval.vstring = parser_strdup(str);
 
-    vec_push(fold->imm_string, out);
+    fold->imm_string.push_back(out);
     util_htseth(table, str, hash, out);
 
     return (ast_expression*)out;
@@ -1059,7 +1038,7 @@ static bool fold_check_except_float_impl(void     (*callback)(void),
     if (!OPTS_FLAG(ARITHMETIC_EXCEPTIONS))
         goto inexact_possible;
 
-    sfloat_check(fold_ctx(fold), &s, NULL);
+    sfloat_check(fold_ctx(fold), &s, nullptr);
 
 inexact_possible:
     return s.exceptionflags & SFLOAT_INEXACT;
@@ -1084,13 +1063,13 @@ static GMQCC_INLINE ast_expression *fold_op_mul_vec(fold_t *fold, vec3_t vec, as
     if (!y && !z) {
         ast_expression *out;
         ++opts_optimizationcount[OPTIM_VECTOR_COMPONENTS];
-        out                        = (ast_expression*)ast_member_new(fold_ctx(fold), (ast_expression*)sel, set[0]-'x', NULL);
+        out                        = (ast_expression*)ast_member_new(fold_ctx(fold), (ast_expression*)sel, set[0]-'x', nullptr);
         out->node.keep             = false;
         ((ast_member*)out)->rvalue = true;
         if (x != -1.0f)
             return (ast_expression*)ast_binary_new(fold_ctx(fold), INSTR_MUL_F, fold_constgen_float(fold, x, false), out);
     }
-    return NULL;
+    return nullptr;
 }
 
 
@@ -1098,14 +1077,14 @@ static GMQCC_INLINE ast_expression *fold_op_neg(fold_t *fold, ast_value *a) {
     if (isfloat(a)) {
         if (fold_can_1(a)) {
             /* Negation can produce inexact as well */
-            bool inexact = fold_check_except_float(&sfloat_neg, fold, a, NULL);
+            bool inexact = fold_check_except_float(&sfloat_neg, fold, a, nullptr);
             return fold_constgen_float(fold, -fold_immvalue_float(a), inexact);
         }
     } else if (isvector(a)) {
         if (fold_can_1(a))
             return fold_constgen_vector(fold, vec3_neg(fold_ctx(fold), fold_immvalue_vector(a)));
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_not(fold_t *fold, ast_value *a) {
@@ -1123,7 +1102,7 @@ static GMQCC_INLINE ast_expression *fold_op_not(fold_t *fold, ast_value *a) {
                 return fold_constgen_float(fold, !fold_immvalue_string(a) || !*fold_immvalue_string(a), false);
         }
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_add(fold_t *fold, ast_value *a, ast_value *b) {
@@ -1138,7 +1117,7 @@ static GMQCC_INLINE ast_expression *fold_op_add(fold_t *fold, ast_value *a, ast_
                                                        fold_immvalue_vector(a),
                                                        fold_immvalue_vector(b)));
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_sub(fold_t *fold, ast_value *a, ast_value *b) {
@@ -1153,7 +1132,7 @@ static GMQCC_INLINE ast_expression *fold_op_sub(fold_t *fold, ast_value *a, ast_
                                                        fold_immvalue_vector(a),
                                                        fold_immvalue_vector(b)));
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_mul(fold_t *fold, ast_value *a, ast_value *b) {
@@ -1187,7 +1166,7 @@ static GMQCC_INLINE ast_expression *fold_op_mul(fold_t *fold, ast_value *a, ast_
             }
         }
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_div(fold_t *fold, ast_value *a, ast_value *b) {
@@ -1222,13 +1201,13 @@ static GMQCC_INLINE ast_expression *fold_op_div(fold_t *fold, ast_value *a, ast_
             );
         }
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_mod(fold_t *fold, ast_value *a, ast_value *b) {
     return (fold_can_2(a, b))
                 ? fold_constgen_float(fold, fmod(fold_immvalue_float(a), fold_immvalue_float(b)), false)
-                : NULL;
+                : nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_bor(fold_t *fold, ast_value *a, ast_value *b) {
@@ -1244,7 +1223,7 @@ static GMQCC_INLINE ast_expression *fold_op_bor(fold_t *fold, ast_value *a, ast_
                 return fold_constgen_vector(fold, vec3_orvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
         }
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_band(fold_t *fold, ast_value *a, ast_value *b) {
@@ -1260,7 +1239,7 @@ static GMQCC_INLINE ast_expression *fold_op_band(fold_t *fold, ast_value *a, ast
                 return fold_constgen_vector(fold, vec3_andvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
         }
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_xor(fold_t *fold, ast_value *a, ast_value *b) {
@@ -1275,19 +1254,19 @@ static GMQCC_INLINE ast_expression *fold_op_xor(fold_t *fold, ast_value *a, ast_
                 return fold_constgen_vector(fold, vec3_xorvf(fold_immvalue_vector(a), fold_immvalue_float(b)));
         }
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_lshift(fold_t *fold, ast_value *a, ast_value *b) {
     if (fold_can_2(a, b) && isfloats(a, b))
         return fold_constgen_float(fold, (qcfloat_t)floorf(fold_immvalue_float(a) * powf(2.0f, fold_immvalue_float(b))), false);
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_rshift(fold_t *fold, ast_value *a, ast_value *b) {
     if (fold_can_2(a, b) && isfloats(a, b))
         return fold_constgen_float(fold, (qcfloat_t)floorf(fold_immvalue_float(a) / powf(2.0f, fold_immvalue_float(b))), false);
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_andor(fold_t *fold, ast_value *a, ast_value *b, float expr) {
@@ -1308,7 +1287,7 @@ static GMQCC_INLINE ast_expression *fold_op_andor(fold_t *fold, ast_value *a, as
             );
         }
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_tern(fold_t *fold, ast_value *a, ast_value *b, ast_value *c) {
@@ -1317,13 +1296,13 @@ static GMQCC_INLINE ast_expression *fold_op_tern(fold_t *fold, ast_value *a, ast
                     ? (ast_expression*)b
                     : (ast_expression*)c;
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_exp(fold_t *fold, ast_value *a, ast_value *b) {
     if (fold_can_2(a, b))
         return fold_constgen_float(fold, (qcfloat_t)powf(fold_immvalue_float(a), fold_immvalue_float(b)), false);
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_lteqgt(fold_t *fold, ast_value *a, ast_value *b) {
@@ -1333,7 +1312,7 @@ static GMQCC_INLINE ast_expression *fold_op_lteqgt(fold_t *fold, ast_value *a, a
         if (fold_immvalue_float(a) == fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[0];
         if (fold_immvalue_float(a) >  fold_immvalue_float(b)) return (ast_expression*)fold->imm_float[1];
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_ltgt(fold_t *fold, ast_value *a, ast_value *b, bool lt) {
@@ -1342,7 +1321,7 @@ static GMQCC_INLINE ast_expression *fold_op_ltgt(fold_t *fold, ast_value *a, ast
         return (lt) ? (ast_expression*)fold->imm_float[!!(fold_immvalue_float(a) < fold_immvalue_float(b))]
                     : (ast_expression*)fold->imm_float[!!(fold_immvalue_float(a) > fold_immvalue_float(b))];
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_cmp(fold_t *fold, ast_value *a, ast_value *b, bool ne) {
@@ -1358,7 +1337,7 @@ static GMQCC_INLINE ast_expression *fold_op_cmp(fold_t *fold, ast_value *a, ast_
             return (ast_expression*)fold->imm_float[!(ne ? vec3_cmp(la, lb) : !vec3_cmp(la, lb))];
         }
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_bnot(fold_t *fold, ast_value *a) {
@@ -1371,7 +1350,7 @@ static GMQCC_INLINE ast_expression *fold_op_bnot(fold_t *fold, ast_value *a) {
                 return fold_constgen_vector(fold, vec3_not(fold_immvalue_vector(a)));
         }
     }
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_cross(fold_t *fold, ast_value *a, ast_value *b) {
@@ -1379,34 +1358,34 @@ static GMQCC_INLINE ast_expression *fold_op_cross(fold_t *fold, ast_value *a, as
         return fold_constgen_vector(fold, vec3_cross(fold_ctx(fold),
                                                      fold_immvalue_vector(a),
                                                      fold_immvalue_vector(b)));
-    return NULL;
+    return nullptr;
 }
 
 static GMQCC_INLINE ast_expression *fold_op_length(fold_t *fold, ast_value *a) {
     if (fold_can_1(a) && isstring(a))
         return fold_constgen_float(fold, strlen(fold_immvalue_string(a)), false);
     if (isarray(a))
-        return fold_constgen_float(fold, vec_size(a->initlist), false);
-    return NULL;
+        return fold_constgen_float(fold, a->initlist.size(), false);
+    return nullptr;
 }
 
 ast_expression *fold_op(fold_t *fold, const oper_info *info, ast_expression **opexprs) {
     ast_value      *a = (ast_value*)opexprs[0];
     ast_value      *b = (ast_value*)opexprs[1];
     ast_value      *c = (ast_value*)opexprs[2];
-    ast_expression *e = NULL;
+    ast_expression *e = nullptr;
 
     /* can a fold operation be applied to this operator usage? */
     if (!info->folds)
-        return NULL;
+        return nullptr;
 
     switch(info->operands) {
-        case 3: if(!c) return NULL;
-        case 2: if(!b) return NULL;
+        case 3: if(!c) return nullptr;
+        case 2: if(!b) return nullptr;
         case 1:
         if(!a) {
             compile_error(fold_ctx(fold), "internal error: fold_op no operands to fold\n");
-            return NULL;
+            return nullptr;
         }
     }
 
@@ -1454,7 +1433,7 @@ ast_expression *fold_op(fold_t *fold, const oper_info *info, ast_expression **op
     }
     #undef fold_op_case
     compile_error(fold_ctx(fold), "internal error: attempted to constant-fold for unsupported operator");
-    return NULL;
+    return nullptr;
 }
 
 /*
@@ -1507,7 +1486,7 @@ static GMQCC_INLINE ast_expression *fold_intrin_fabs(fold_t *fold, ast_value *a)
 
 
 ast_expression *fold_intrin(fold_t *fold, const char *intrin, ast_expression **arg) {
-    ast_expression *ret = NULL;
+    ast_expression *ret = nullptr;
     ast_value      *a   = (ast_value*)arg[0];
     ast_value      *b   = (ast_value*)arg[1];
 
@@ -1558,7 +1537,7 @@ ast_expression *fold_intrin(fold_t *fold, const char *intrin, ast_expression **a
 /*#define fold_can_2(X,Y)         (fold_can_1(X) && fold_can_1(Y))*/
 
 static ast_expression *fold_superfluous(ast_expression *left, ast_expression *right, int op) {
-    ast_expression *swapped = NULL; /* using this as bool */
+    ast_expression *swapped = nullptr; /* using this as bool */
     ast_value *load;
 
     if (!ast_istype(right, ast_value) || !fold_can_1((load = (ast_value*)right))) {
@@ -1568,12 +1547,12 @@ static ast_expression *fold_superfluous(ast_expression *left, ast_expression *ri
     }
 
     if (!ast_istype(right, ast_value) || !fold_can_1((load = (ast_value*)right)))
-        return NULL;
+        return nullptr;
 
     switch (op) {
         case INSTR_DIV_F:
             if (swapped)
-                return NULL;
+                return nullptr;
         case INSTR_MUL_F:
             if (fold_immvalue_float(load) == 1.0f) {
                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
@@ -1585,7 +1564,7 @@ static ast_expression *fold_superfluous(ast_expression *left, ast_expression *ri
 
         case INSTR_SUB_F:
             if (swapped)
-                return NULL;
+                return nullptr;
         case INSTR_ADD_F:
             if (fold_immvalue_float(load) == 0.0f) {
                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
@@ -1604,7 +1583,7 @@ static ast_expression *fold_superfluous(ast_expression *left, ast_expression *ri
 
         case INSTR_SUB_V:
             if (swapped)
-                return NULL;
+                return nullptr;
         case INSTR_ADD_V:
             if (vec3_cmp(fold_immvalue_vector(load), vec3_create(0, 0, 0))) {
                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
@@ -1614,7 +1593,7 @@ static ast_expression *fold_superfluous(ast_expression *left, ast_expression *ri
             break;
     }
 
-    return NULL;
+    return nullptr;
 }
 
 ast_expression *fold_binary(lex_ctx_t ctx, int op, ast_expression *left, ast_expression *right) {
@@ -1632,7 +1611,7 @@ static GMQCC_INLINE int fold_cond(ir_value *condval, ast_function *func, ast_ift
         bool                    istrue  = (fold_immvalue_float(condval) != 0.0f && branch->on_true);
         bool                    isfalse = (fold_immvalue_float(condval) == 0.0f && branch->on_false);
         ast_expression         *path    = (istrue)  ? branch->on_true  :
-                                          (isfalse) ? branch->on_false : NULL;
+                                          (isfalse) ? branch->on_false : nullptr;
         if (!path) {
             /*
              * no path to take implies that the evaluation is if(0) and there