]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - parser.c
Pushing the -fexpressions-for-builtins stuff and the modff for catching fractional...
[xonotic/gmqcc.git] / parser.c
index bd4f1c14b75936394f238fc9d91547c57955a30b..14cf583647c0a79362691193440f75b0d8761572 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -2,7 +2,7 @@
  * Copyright (C) 2012, 2013
  *     Wolfgang Bumiller
  *     Dale Weiler
- * 
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  * this software and associated documentation files (the "Software"), to deal in
  * the Software without restriction, including without limitation the rights to
@@ -35,7 +35,7 @@
 #define PARSER_HT_SIZE    128
 #define TYPEDEF_HT_SIZE   16
 
-typedef struct {
+typedef struct parser_s {
     lex_file *lex;
     int      tok;
 
@@ -47,6 +47,8 @@ typedef struct {
     ast_value    **imm_vector;
     size_t         translated;
 
+    ht ht_imm_string;
+
     /* must be deleted first, they reference immediates and values */
     ast_value    **accessors;
 
@@ -101,6 +103,9 @@ typedef struct {
 
     /* collected information */
     size_t     max_param_count;
+
+    /* code generator */
+    code_t     *code;
 } parser_t;
 
 static ast_expression * const intrinsic_debug_typestring = (ast_expression*)0x1;
@@ -120,8 +125,6 @@ static ast_expression* parse_expression(parser_t *parser, bool stopatcomma, bool
 static ast_value* parser_create_array_setter_proto(parser_t *parser, ast_value *array, const char *funcname);
 static ast_value* parser_create_array_getter_proto(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname);
 static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
-static ast_expression *parser_builtin_pow(parser_t *);
-static ast_expression *parser_builtin_exp(parser_t *);
 
 static void parseerror(parser_t *parser, const char *fmt, ...)
 {
@@ -217,6 +220,7 @@ static ast_value* parser_const_float(parser_t *parser, double d)
     out = ast_value_new(ctx, "#IMMEDIATE", TYPE_FLOAT);
     out->cvq      = CV_CONST;
     out->hasvalue = true;
+    out->isimm    = true;
     out->constval.vfloat = d;
     vec_push(parser->imm_float, out);
     return out;
@@ -255,22 +259,34 @@ static char *parser_strdup(const char *str)
 
 static ast_value* parser_const_string(parser_t *parser, const char *str, bool dotranslate)
 {
-    size_t i;
+    size_t hash = util_hthash(parser->ht_imm_string, str);
     ast_value *out;
+    if ( (out = (ast_value*)util_htgeth(parser->ht_imm_string, str, hash)) ) {
+        if (dotranslate && out->name[0] == '#') {
+            char name[32];
+            util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(parser->translated++));
+            ast_value_set_name(out, name);
+        }
+        return out;
+    }
+    /*
     for (i = 0; i < vec_size(parser->imm_string); ++i) {
         if (!strcmp(parser->imm_string[i]->constval.vstring, str))
             return parser->imm_string[i];
     }
+    */
     if (dotranslate) {
         char name[32];
-        snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(parser->translated++));
+        util_snprintf(name, sizeof(name), "dotranslate_%lu", (unsigned long)(parser->translated++));
         out = ast_value_new(parser_ctx(parser), name, TYPE_STRING);
     } else
         out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_STRING);
     out->cvq      = CV_CONST;
     out->hasvalue = true;
+    out->isimm    = true;
     out->constval.vstring = parser_strdup(str);
     vec_push(parser->imm_string, out);
+    util_htseth(parser->ht_imm_string, str, hash, out);
     return out;
 }
 
@@ -285,6 +301,7 @@ static ast_value* parser_const_vector(parser_t *parser, vector v)
     out = ast_value_new(parser_ctx(parser), "#IMMEDIATE", TYPE_VECTOR);
     out->cvq      = CV_CONST;
     out->hasvalue = true;
+    out->isimm    = true;
     out->constval.vvec = v;
     vec_push(parser->imm_vector, out);
     return out;
@@ -382,6 +399,9 @@ static ast_value* parser_find_typedef(parser_t *parser, const char *name, size_t
     return NULL;
 }
 
+/* include intrinsics */
+#include "intrin.h"
+
 typedef struct
 {
     size_t etype; /* 0 = expression, others are operators */
@@ -964,10 +984,35 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
                 return false;
             }
             break;
+
         case opid1('%'):
+            if (NotSameType(TYPE_FLOAT)) {
+                compile_error(ctx, "invalid types used in expression: cannot perform modulo operation between types %s and %s",
+                    type_name[exprs[0]->expression.vtype],
+                    type_name[exprs[1]->expression.vtype]);
+                return false;
+            }
+            if (CanConstFold(exprs[0], exprs[1])) {
+                out = (ast_expression*)parser_const_float(parser,
+                            (float)(((qcint)ConstF(0)) % ((qcint)ConstF(1))));
+            } else {
+                /* generate a call to __builtin_mod */
+                ast_expression *mod  = intrin_func(parser, "mod");
+                ast_call       *call = NULL;
+                if (!mod) return false; /* can return null for missing floor */
+
+                call = ast_call_new(parser_ctx(parser), mod);
+                vec_push(call->params, exprs[0]);
+                vec_push(call->params, exprs[1]);
+
+                out = (ast_expression*)call;
+            }
+            break;
+
         case opid2('%','='):
-            compile_error(ctx, "qc does not have a modulo operator");
+            compile_error(ctx, "%= is unimplemented");
             return false;
+
         case opid1('|'):
         case opid1('&'):
             if (NotSameType(TYPE_FLOAT)) {
@@ -1087,7 +1132,7 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy)
             if (CanConstFold(exprs[0], exprs[1])) {
                 out = (ast_expression*)parser_const_float(parser, powf(ConstF(0), ConstF(1)));
             } else {
-                ast_call *gencall = ast_call_new(parser_ctx(parser), parser_builtin_pow(parser));
+                ast_call *gencall = ast_call_new(parser_ctx(parser), intrin_func(parser, "pow"));
                 vec_push(gencall->params, exprs[0]);
                 vec_push(gencall->params, exprs[1]);
                 out = (ast_expression*)gencall;
@@ -1743,6 +1788,9 @@ static ast_expression* parse_vararg(parser_t *parser)
     return out;
 }
 
+/* not to be exposed */
+extern bool ftepp_predef_exists(const char *name);
+
 static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels)
 {
     if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
@@ -1849,16 +1897,21 @@ static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels)
                 vec_push(parser->labels, lbl);
             }
         }
+        if (!var && !strcmp(parser_tokval(parser), "__FUNC__"))
+            var = (ast_expression*)parser_const_string(parser, parser->function->name, false);
         if (!var) {
             /* intrinsics */
             if (!strcmp(parser_tokval(parser), "__builtin_debug_typestring")) {
                 var = (ast_expression*)intrinsic_debug_typestring;
             }
-            if (!strcmp(parser_tokval(parser), "__builtin_pow"))
-                var = parser_builtin_pow(parser);
-            if (!strcmp(parser_tokval(parser), "__builtin_exp"))
-                var = parser_builtin_exp(parser);
-                
+            /* now we try for the real intrinsic hashtable. If the string
+             * begins with __builtin, we simply skip past it, otherwise we
+             * use the identifier as is.
+             */
+            else if (!strncmp(parser_tokval(parser), "__builtin_", 10)) {
+                var = intrin_func(parser, parser_tokval(parser) + 10 /* skip __builtin */);
+            }
+
             if (!var) {
                 char *correct = NULL;
                 size_t i;
@@ -1868,13 +1921,9 @@ static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels)
                  * i've done this thousands of times already myself.  Lets check for
                  * it in the predef table.  And diagnose it better :)
                  */
-                if (!OPTS_FLAG(FTEPP_PREDEFS)) {
-                    for (i = 0; i < sizeof(ftepp_predefs)/sizeof(*ftepp_predefs); i++) {
-                        if (!strcmp(ftepp_predefs[i].name, parser_tokval(parser))) {
-                            parseerror(parser, "unexpected ident: %s (use -fftepp-predef to enable pre-defined macros)", parser_tokval(parser));
-                            return false;
-                        }
-                    }
+                if (!OPTS_FLAG(FTEPP_PREDEFS) && ftepp_predef_exists(parser_tokval(parser))) {
+                    parseerror(parser, "unexpected ident: %s (use -fftepp-predef to enable pre-defined macros)", parser_tokval(parser));
+                    return false;
                 }
 
                 /*
@@ -2138,8 +2187,27 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
             wantop = true;
         }
         else {
-            parseerror(parser, "expected operator or end of statement");
-            goto onerr;
+            /* in this case we might want to allow constant string concatenation */
+            bool concatenated = false;
+            if (parser->tok == TOKEN_STRINGCONST && vec_size(sy.out)) {
+                ast_expression *lexpr = vec_last(sy.out).out;
+                if (ast_istype(lexpr, ast_value)) {
+                    ast_value *last = (ast_value*)lexpr;
+                    if (last->isimm == true && last->cvq == CV_CONST &&
+                        last->hasvalue && last->expression.vtype == TYPE_STRING)
+                    {
+                        char *newstr = NULL;
+                        util_asprintf(&newstr, "%s%s", last->constval.vstring, parser_tokval(parser));
+                        vec_last(sy.out).out = (ast_expression*)parser_const_string(parser, newstr, false);
+                        mem_d(newstr);
+                        concatenated = true;
+                    }
+                }
+            }
+            if (!concatenated) {
+                parseerror(parser, "expected operator or end of statement");
+                goto onerr;
+            }
         }
 
         if (!parser_next(parser)) {
@@ -3322,185 +3390,6 @@ static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression *
     return true;
 }
 
-ast_expression *parser_builtin_pow(parser_t *parser) {
-    /*
-     * float __builtin_pow(float x, float y) { 
-     *   float value = 1.0f;
-     *   while (y > 0) {
-     *     while (!(y&1)) {
-     *       y *= 0.25f;
-     *       x *= x;
-     *     }
-     *     y--;
-     *     value *= x;
-     *   }
-     *   return value;
-     * }      
-     */
-    static ast_function  *pow_func       = NULL;
-    static ast_value     *pow_func_val   = NULL; 
-    if (!pow_func) {
-        ast_value    *pow_arguments[2];
-        ast_value    *pow_value          = ast_value_new   (parser_ctx(parser), "value",         TYPE_FLOAT);
-        ast_block    *pow_body           = ast_block_new   (parser_ctx(parser));
-        ast_block    *pow_loop_body      = ast_block_new   (parser_ctx(parser));
-        ast_block    *pow_loop_nest_body = ast_block_new   (parser_ctx(parser));
-        ast_loop     *pow_loop           = NULL;
-        ast_loop     *pow_loop_nest      = NULL;
-
-        pow_arguments[0]                 = ast_value_new   (parser_ctx(parser), "x", TYPE_FLOAT);
-        pow_arguments[1]                 = ast_value_new   (parser_ctx(parser), "x", TYPE_FLOAT);
-        pow_func_val                     = ast_value_new   (parser_ctx(parser), "__builtin_pow", TYPE_FUNCTION);
-        pow_func_val->expression.next    = (ast_expression*)ast_value_new(parser_ctx(parser), "<float>", TYPE_FLOAT);
-
-        vec_push(pow_func_val->expression.params, pow_arguments[0]);
-        vec_push(pow_func_val->expression.params, pow_arguments[1]);
-
-        pow_func = ast_function_new(parser_ctx(parser), "__builtin_pow", pow_func_val);
-
-        /* float value; */
-        vec_push(pow_body->locals, pow_value);
-        /* value = 1.0f; */
-        vec_push(pow_body->exprs,
-            (ast_expression*)ast_store_new(
-                parser_ctx(parser),
-                INSTR_STORE_F,
-                (ast_expression*)pow_value,
-                (ast_expression*)parser_const_float_1(parser)
-            )
-        );
-
-        /* y >>= 2 */
-        vec_push(pow_loop_nest_body->exprs,
-            (ast_expression*)ast_binstore_new(
-                parser_ctx(parser),
-                INSTR_STORE_F,
-                INSTR_MUL_F,
-                (ast_expression*)pow_arguments[1],
-                (ast_expression*)parser_const_float(parser, 0.25f)
-            )
-        );
-        vec_push(pow_loop_nest_body->exprs,
-            (ast_expression*)ast_binstore_new(
-                parser_ctx(parser),
-                INSTR_STORE_F,
-                INSTR_MUL_F,
-                (ast_expression*)pow_arguments[0],
-                (ast_expression*)pow_arguments[0]
-            )
-        );
-
-        /* while (!(y&1)) */
-        pow_loop_nest = ast_loop_new (
-            parser_ctx(parser),
-            NULL,
-            (ast_expression*)ast_binary_new(
-                parser_ctx(parser),
-                INSTR_AND,
-                (ast_expression*)pow_arguments[1],
-                (ast_expression*)parser_const_float_1(parser)
-            ),
-            true,
-            NULL,
-            false,
-            NULL,
-            (ast_expression*)pow_loop_nest_body
-        );
-        
-        vec_push(pow_loop_body->exprs, (ast_expression*)pow_loop_nest);
-        vec_push(pow_loop_body->exprs,
-            (ast_expression*)ast_binstore_new(
-                parser_ctx(parser),
-                INSTR_STORE_F,
-                INSTR_SUB_F,
-                (ast_expression*)pow_arguments[1],
-                (ast_expression*)parser_const_float_1(parser)
-            )
-        );
-        vec_push(pow_loop_body->exprs,
-            (ast_expression*)ast_binstore_new(
-                parser_ctx(parser),
-                INSTR_STORE_F,
-                INSTR_MUL_F,
-                (ast_expression*)pow_value,
-                (ast_expression*)pow_arguments[0]
-            )
-        );
-
-        /* while (y > 0) { */
-        pow_loop = ast_loop_new(
-            parser_ctx(parser),
-            NULL,
-            (ast_expression*)ast_binary_new(
-                parser_ctx(parser),
-                INSTR_GT,
-                (ast_expression*)pow_arguments[1],
-                (ast_expression*)parser_const_float_0(parser)
-            ),
-            false,
-            NULL,
-            false,
-            NULL,
-            (ast_expression*)pow_loop_body
-        );
-        /* } */
-        vec_push(pow_body->exprs, (ast_expression*)pow_loop);
-        /* return value; */
-        vec_push(pow_body->exprs,
-            (ast_expression*)ast_return_new(
-                parser_ctx(parser),
-                (ast_expression*)pow_value
-            )
-        );
-
-        vec_push(pow_func->blocks, pow_body);
-        vec_push(parser->globals, (ast_expression*)pow_func_val);
-        vec_push(parser->functions, pow_func);
-    }
-
-    return (ast_expression*)pow_func_val;
-}
-
-#ifndef M_E
-#define M_E 2.71828182845905
-#endif
-static ast_expression *parser_builtin_exp(parser_t *parser) {
-    /*
-     * float __builtin_exp(float x) {
-     *     return __builtin_exp(E, x);
-     * }
-     */ 
-    static ast_value *exp_func_val = NULL;
-
-    if (!exp_func_val) {
-        ast_function *exp_func         = NULL;
-        ast_value    *arg              = ast_value_new   (parser_ctx(parser), "x", TYPE_FLOAT);
-        ast_block    *exp_body         = ast_block_new   (parser_ctx(parser));
-        ast_call     *exp_call         = ast_call_new    (parser_ctx(parser), parser_builtin_pow(parser));
-        exp_func_val                   = ast_value_new   (parser_ctx(parser), "__builtin_exp", TYPE_FUNCTION);
-        exp_func_val->expression.next  = (ast_expression*)ast_value_new(parser_ctx(parser), "<float>", TYPE_FLOAT);
-        exp_func                       = ast_function_new(parser_ctx(parser), "__builtin_exp", exp_func_val);
-
-        vec_push(exp_call->params, (ast_expression*)parser_const_float(parser, M_E));
-        vec_push(exp_call->params, (ast_expression*)arg);
-
-        vec_push(exp_body->exprs,
-            (ast_expression*)ast_return_new(
-                parser_ctx(parser),
-                (ast_expression*)exp_call
-            )
-        );
-
-        vec_push(exp_func_val->expression.params, arg);
-        vec_push(exp_func->blocks, exp_body);
-
-        vec_push(parser->functions, exp_func);
-        vec_push(parser->globals, (ast_expression*)exp_func_val);
-    }
-
-    return (ast_expression*)exp_func_val;
-}
-
 /* parse computed goto sides */
 static ast_expression *parse_goto_computed(parser_t *parser, ast_expression **side) {
     ast_expression *on_true;
@@ -3862,6 +3751,7 @@ static bool parse_statement(parser_t *parser, ast_block *block, ast_expression *
 static bool parse_enum(parser_t *parser)
 {
     bool        flag = false;
+    bool        reverse = false;
     qcfloat     num = 0;
     ast_value **values = NULL;
     ast_value  *var = NULL;
@@ -3876,19 +3766,28 @@ static bool parse_enum(parser_t *parser)
 
     /* enumeration attributes (can add more later) */
     if (parser->tok == ':') {
-        if (!parser_next(parser) || parser->tok != TOKEN_IDENT || strcmp(parser_tokval(parser), "flag")) {
-            parseerror(parser, "expected `flag` after enumeration attribute ':'");
+        if (!parser_next(parser) || parser->tok != TOKEN_IDENT){
+            parseerror(parser, "expected `flag` or `reverse` for enumeration attribute");
             return false;
         }
 
-        if (!parser_next(parser) || parser->tok != '{') {
-            parseerror(parser, "expected `{` after enum attribute `flag`");
+        /* attributes? */
+        if (!strcmp(parser_tokval(parser), "flag")) {
+            num  = 1;
+            flag = true;
+        }
+        else if (!strcmp(parser_tokval(parser), "reverse")) {
+            reverse = true;
+        }
+        else {
+            parseerror(parser, "invalid attribute `%s` for enumeration", parser_tokval(parser));
             return false;
         }
 
-        /* flagged enumeration start from 1 */
-        num  = 1;
-        flag = true;
+        if (!parser_next(parser) || parser->tok != '{') {
+            parseerror(parser, "expected `{` after enum attribute ");
+            return false;
+        }
     }
 
     while (true) {
@@ -3917,7 +3816,6 @@ static bool parse_enum(parser_t *parser)
 
         /* for flagged enumerations increment in POTs of TWO */
         var->constval.vfloat = (flag) ? (num *= 2) : (num ++);
-
         parser_addglobal(parser, var->name, (ast_expression*)var);
 
         if (!parser_next(parser)) {
@@ -3956,6 +3854,13 @@ static bool parse_enum(parser_t *parser)
         }
     }
 
+    /* patch them all (for reversed attribute) */
+    if (reverse) {
+        size_t i;
+        for (i = 0; i < vec_size(values); i++)
+            values[i]->constval.vfloat = vec_size(values) - i - 1;
+    }
+
     if (parser->tok != '}') {
         parseerror(parser, "internal error: breaking without `}`");
         goto onerror;
@@ -4346,13 +4251,13 @@ static bool parse_function_body(parser_t *parser, ast_value *var)
         varargs->expression.flags |= AST_FLAG_IS_VARARG;
         varargs->expression.next = (ast_expression*)ast_value_new(ast_ctx(var), NULL, TYPE_VECTOR);
         varargs->expression.count = 0;
-        snprintf(name, sizeof(name), "%s##va##SET", var->name);
+        util_snprintf(name, sizeof(name), "%s##va##SET", var->name);
         if (!parser_create_array_setter_proto(parser, varargs, name)) {
             ast_delete(varargs);
             ast_block_delete(block);
             goto enderrfn;
         }
-        snprintf(name, sizeof(name), "%s##va##GET", var->name);
+        util_snprintf(name, sizeof(name), "%s##va##GET", var->name);
         if (!parser_create_array_getter_proto(parser, varargs, varargs->expression.next, name)) {
             ast_delete(varargs);
             ast_block_delete(block);
@@ -4923,6 +4828,8 @@ static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
 on_error:
     if (argcounter)
         mem_d(argcounter);
+    if (varparam)
+        ast_delete(varparam);
     ast_delete(var);
     for (i = 0; i < vec_size(params); ++i)
         ast_delete(params[i]);
@@ -5231,7 +5138,7 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
     bool      cleanvar  = true;
     bool      wasarray  = false;
 
-    ast_member *me[3];
+    ast_member *me[3] = { NULL, NULL, NULL };
 
     if (!localblock && is_static)
         parseerror(parser, "`static` qualifier is not supported in global scope");
@@ -5288,7 +5195,7 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
         /*
          * store the vstring back to var for alias and
          * deprecation messages.
-         */   
+         */
         if (var->expression.flags & AST_FLAG_DEPRECATED ||
             var->expression.flags & AST_FLAG_ALIAS)
             var->desc = vstring;
@@ -5385,7 +5292,7 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
                         ast_value_set_name(proto->expression.params[i], var->expression.params[i]->name);
                     if (!parser_check_qualifiers(parser, var, proto)) {
                         retval = false;
-                        if (proto->desc) 
+                        if (proto->desc)
                             mem_d(proto->desc);
                         proto = NULL;
                         goto cleanup;
@@ -5526,13 +5433,13 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
                         /*
                          * add alias to aliases table and to corrector
                          * so corrections can apply for aliases as well.
-                         */  
+                         */
                         util_htset(parser->aliases, var->name, find);
 
                         /*
                          * add to corrector so corrections can work
                          * even for aliases too.
-                         */ 
+                         */
                         correct_add (
                              vec_last(parser->correct_variables),
                             &vec_last(parser->correct_variables_score),
@@ -5558,7 +5465,7 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
                             /*
                              * add to corrector so corrections can work
                              * even for aliases too.
-                             */  
+                             */
                             correct_add (
                                  vec_last(parser->correct_variables),
                                 &vec_last(parser->correct_variables_score),
@@ -5650,10 +5557,10 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
          */
         if (var->expression.vtype == TYPE_ARRAY) {
             char name[1024];
-            snprintf(name, sizeof(name), "%s##SET", var->name);
+            util_snprintf(name, sizeof(name), "%s##SET", var->name);
             if (!parser_create_array_setter(parser, var, name))
                 goto cleanup;
-            snprintf(name, sizeof(name), "%s##GET", var->name);
+            util_snprintf(name, sizeof(name), "%s##GET", var->name);
             if (!parser_create_array_getter(parser, var, var->expression.next, name))
                 goto cleanup;
         }
@@ -5671,14 +5578,14 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
                 goto cleanup;
             }
 
-            snprintf(name, sizeof(name), "%s##SETF", var->name);
+            util_snprintf(name, sizeof(name), "%s##SETF", var->name);
             if (!parser_create_array_field_setter(parser, array, name))
                 goto cleanup;
 
             telem = ast_type_copy(ast_ctx(var), array->expression.next);
             tfield = ast_value_new(ast_ctx(var), "<.type>", TYPE_FIELD);
             tfield->expression.next = telem;
-            snprintf(name, sizeof(name), "%s##GETFP", var->name);
+            util_snprintf(name, sizeof(name), "%s##GETFP", var->name);
             if (!parser_create_array_getter(parser, array, (ast_expression*)tfield, name)) {
                 ast_delete(tfield);
                 goto cleanup;
@@ -5732,7 +5639,11 @@ skipvar:
         }
 
         if (parser->tok == '#') {
-            ast_function *func = NULL;
+            ast_function *func   = NULL;
+            ast_value    *number = NULL;
+            float         fractional;
+            float         integral;
+            int           builtin_num;
 
             if (localblock) {
                 parseerror(parser, "cannot declare builtins within functions");
@@ -5746,12 +5657,40 @@ skipvar:
                 parseerror(parser, "expected builtin number");
                 break;
             }
-            if (parser->tok != TOKEN_INTCONST) {
-                parseerror(parser, "builtin number must be an integer constant");
-                break;
-            }
-            if (parser_token(parser)->constval.i < 0) {
-                parseerror(parser, "builtin number must be an integer greater than zero");
+
+            if (OPTS_FLAG(EXPRESSIONS_FOR_BUILTINS)) {
+                number = (ast_value*)parse_expression_leave(parser, true, false, false);
+                if (!number) {
+                    parseerror(parser, "builtin number expected");
+                    break;
+                }
+                if (!ast_istype(number, ast_value) || !number->hasvalue || number->cvq != CV_CONST)
+                {
+                    ast_unref(number);
+                    parseerror(parser, "builtin number must be a compile time constant");
+                    break;
+                }
+                if (number->expression.vtype == TYPE_INTEGER)
+                    builtin_num = number->constval.vint;
+                else if (number->expression.vtype == TYPE_FLOAT)
+                    builtin_num = number->constval.vfloat;
+                else {
+                    ast_unref(number);
+                    parseerror(parser, "builtin number must be an integer constant");
+                    break;
+                }
+                ast_unref(number);
+
+                fractional = modff(builtin_num, &integral);
+                if (builtin_num < 0 || fractional != 0) {
+                    parseerror(parser, "builtin number must be an integer greater than zero");
+                    break;
+                }
+
+                /* we only want the integral part anyways */
+                builtin_num = integral;
+            } else if (parser->tok != TOKEN_INTCONST) {
+                parseerror(parser, "builtin number must be a compile time constant");
                 break;
             }
 
@@ -5770,10 +5709,15 @@ skipvar:
                 }
                 vec_push(parser->functions, func);
 
-                func->builtin = -parser_token(parser)->constval.i-1;
+                func->builtin = -((OPTS_FLAG(EXPRESSIONS_FOR_BUILTINS))
+                                    ? builtin_num
+                                    : parser_token(parser)->constval.i) - 1;
             }
 
-            if (!parser_next(parser)) {
+            if (OPTS_FLAG(EXPRESSIONS_FOR_BUILTINS)
+                    ? (parser->tok != ',' && parser->tok != ';')
+                    : (!parser_next(parser)))
+            {
                 parseerror(parser, "expected comma or semicolon");
                 if (func)
                     ast_function_delete(func);
@@ -6038,22 +5982,27 @@ static void generate_checksum(parser_t *parser)
     }
     crc = progdefs_crc_both(crc, "} entvars_t;\n\n");
 
-    code_crc = crc;
+    parser->code->crc = crc;
 }
 
-static parser_t *parser;
-
-bool parser_init()
+parser_t *parser_create()
 {
+    parser_t *parser;
     lex_ctx empty_ctx;
     size_t i;
 
     parser = (parser_t*)mem_a(sizeof(parser_t));
     if (!parser)
-        return false;
+        return NULL;
 
     memset(parser, 0, sizeof(*parser));
 
+    if (!(parser->code = code_init())) {
+        mem_d(parser);
+        return NULL;
+    }
+    
+
     for (i = 0; i < operator_count; ++i) {
         if (operators[i].id == opid1('=')) {
             parser->assign_op = operators+i;
@@ -6063,7 +6012,7 @@ bool parser_init()
     if (!parser->assign_op) {
         printf("internal error: initializing parser: failed to find assign operator\n");
         mem_d(parser);
-        return false;
+        return NULL;
     }
 
     vec_push(parser->variables, parser->htfields  = util_htnew(PARSER_HT_SIZE));
@@ -6073,6 +6022,8 @@ bool parser_init()
 
     parser->aliases = util_htnew(PARSER_HT_SIZE);
 
+    parser->ht_imm_string = util_htnew(512);
+
     /* corrector */
     vec_push(parser->correct_variables, correct_trie_new());
     vec_push(parser->correct_variables_score, NULL);
@@ -6099,10 +6050,11 @@ bool parser_init()
     } else {
         parser->reserved_version = NULL;
     }
-    return true;
+
+    return parser;
 }
 
-bool parser_compile()
+bool parser_compile(parser_t *parser)
 {
     /* initial lexer/parser state */
     parser->lex->flags.noops = true;
@@ -6134,27 +6086,27 @@ bool parser_compile()
     return !compile_errors;
 }
 
-bool parser_compile_file(const char *filename)
+bool parser_compile_file(parser_t *parser, const char *filename)
 {
     parser->lex = lex_open(filename);
     if (!parser->lex) {
         con_err("failed to open file \"%s\"\n", filename);
         return false;
     }
-    return parser_compile();
+    return parser_compile(parser);
 }
 
-bool parser_compile_string(const char *name, const char *str, size_t len)
+bool parser_compile_string(parser_t *parser, const char *name, const char *str, size_t len)
 {
     parser->lex = lex_open_string(str, len, name);
     if (!parser->lex) {
         con_err("failed to create lexer for string \"%s\"\n", name);
         return false;
     }
-    return parser_compile();
+    return parser_compile(parser);
 }
 
-void parser_cleanup()
+void parser_cleanup(parser_t *parser)
 {
     size_t i;
     for (i = 0; i < vec_size(parser->accessors); ++i) {
@@ -6184,6 +6136,7 @@ void parser_cleanup()
     vec_free(parser->functions);
     vec_free(parser->imm_vector);
     vec_free(parser->imm_string);
+    util_htdel(parser->ht_imm_string);
     vec_free(parser->imm_float);
     vec_free(parser->globals);
     vec_free(parser->fields);
@@ -6225,10 +6178,12 @@ void parser_cleanup()
 
     util_htdel(parser->aliases);
 
+    intrin_intrinsics_destroy(parser);
+
     mem_d(parser);
 }
 
-bool parser_finish(const char *output)
+bool parser_finish(parser_t *parser, const char *output)
 {
     size_t i;
     ir_builder *ir;
@@ -6403,7 +6358,7 @@ bool parser_finish(const char *output)
 
         generate_checksum(parser);
 
-        if (!ir_builder_generate(ir, output)) {
+        if (!ir_builder_generate(parser->code, ir, output)) {
             con_out("*** failed to generate output file\n");
             ir_builder_delete(ir);
             return false;