new makefile
[xonotic/gmqcc.git] / intrin.cpp
index 9ef02077b692eba828741cc7babefad9f928fffd..8053b313e28c45193679d3fbbcf635d411302267 100644 (file)
 #include <string.h>
+
+#include "ast.h"
+#include "fold.h"
 #include "parser.h"
 
-#define intrin_ctx(I) parser_ctx((I)->parser)
+lex_ctx_t intrin::ctx() const {
+    return parser_ctx(m_parser);
+}
 
-static GMQCC_INLINE ast_function *intrin_value(intrin_t *intrin, ast_value **out, const char *name, qcint_t vtype) {
-    ast_value    *value = NULL;
-    ast_function *func  = NULL;
-    char          buffer[1024];
-    char          stype [1024];
+ast_function *intrin::value(ast_value **out, const char *name, qc_type vtype) {
+    ast_value *value = nullptr;
+    ast_function *func  = nullptr;
+    char buffer[1024];
+    char stype [1024];
 
     util_snprintf(buffer, sizeof(buffer), "__builtin_%s", name);
     util_snprintf(stype,  sizeof(stype),   "<%s>",        type_name[vtype]);
 
-    value                    = ast_value_new(intrin_ctx(intrin), buffer, TYPE_FUNCTION);
-    value->intrinsic         = true;
-    value->expression.next   = (ast_expression*)ast_value_new(intrin_ctx(intrin), stype, vtype);
-    func                     = ast_function_new(intrin_ctx(intrin), buffer, value);
-    value->expression.flags |= AST_FLAG_ERASEABLE;
+    value = new ast_value(ctx(), buffer, TYPE_FUNCTION);
+    value->m_intrinsic = true;
+    value->m_next = new ast_value(ctx(), stype, vtype);
+    func = ast_function::make(ctx(), buffer, value);
+    value->m_flags |= AST_FLAG_ERASEABLE;
 
     *out = value;
     return func;
 }
 
-static GMQCC_INLINE void intrin_reg(intrin_t *intrin, ast_value *const value, ast_function *const func) {
-    vec_push(intrin->parser->functions, func);
-    vec_push(intrin->parser->globals,   (ast_expression*)value);
+void intrin::reg(ast_value *const value, ast_function *const func) {
+    m_parser->functions.push_back(func);
+    m_parser->globals.push_back(value);
 }
 
-#define QC_POW_EPSILON 0.00001f
-
-/*
- * since some intrinsics depend on each other there is the possibility
- * that an intrinsic will fail to get a 'depended' function that a
- * builtin needs, causing some dependency in the chain to have a NULL
- * function. This will cause a segmentation fault at code generation,
- * even though an error was raised. To contiue to allow it (instead
- * of stopping compilation right away). We need to return from the
- * parser, before compilation stops after all the collected errors.
- */
-static ast_expression *intrin_func_self(intrin_t *intrin, const char *name, const char *from);
-static ast_expression *intrin_nullfunc(intrin_t *intrin) {
-    ast_value    *value = NULL;
-    ast_function *func  = intrin_value(intrin, &value, NULL, TYPE_VOID);
-    intrin_reg(intrin, value, func);
-    return (ast_expression*)value;
+ast_expression *intrin::nullfunc() {
+    ast_value *val = nullptr;
+    ast_function *func = value(&val, nullptr, TYPE_VOID);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_isfinite(intrin_t *intrin) {
+ast_expression *intrin::isfinite_() {
     /*
      * float isfinite(float x) {
      *     return !(isnan(x) || isinf(x));
      * }
      */
-    ast_value    *value     = NULL;
-    ast_value    *x         = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_function *func      = intrin_value(intrin, &value, "isfinite", TYPE_FLOAT);
-    ast_call     *callisnan = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "isnan", "isfinite"));
-    ast_call     *callisinf = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "isinf", "isfinite"));
-    ast_block    *block     = ast_block_new(intrin_ctx(intrin));
+    ast_value    *val     = nullptr;
+    ast_value    *x         = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_function *func      = value(&val, "isfinite", TYPE_FLOAT);
+    ast_call     *callisnan = ast_call::make(ctx(), func_self("isnan", "isfinite"));
+    ast_call     *callisinf = ast_call::make(ctx(), func_self("isinf", "isfinite"));
+    ast_block    *block     = new ast_block(ctx());
 
     /* float x; */
-    vec_push(value->expression.params, x);
+    val->m_type_params.emplace_back(x);
 
     /* <callisnan> = isnan(x); */
-    vec_push(callisnan->params, (ast_expression*)x);
+    callisnan->m_params.push_back(x);
 
     /* <callisinf> = isinf(x); */
-    vec_push(callisinf->params, (ast_expression*)x);
+    callisinf->m_params.push_back(x);
 
     /* return (!<callisnan> || <callisinf>); */
-    vec_push(block->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_unary_new(
-                intrin_ctx(intrin),
+    block->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            ast_unary::make(
+                ctx(),
                 INSTR_NOT_F,
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+                new ast_binary(
+                    ctx(),
                     INSTR_OR,
-                    (ast_expression*)callisnan,
-                    (ast_expression*)callisinf
+                    callisnan,
+                    callisinf
                 )
             )
         )
     );
 
-    vec_push(func->blocks, block);
-    intrin_reg(intrin, value, func);
+    func->m_blocks.emplace_back(block);
+    reg(val, func);
 
-    return (ast_expression*)value;;
+    return val;;
 }
 
-static ast_expression *intrin_isinf(intrin_t *intrin) {
+ast_expression *intrin::isinf_() {
     /*
      * float isinf(float x) {
      *     return (x != 0.0) && (x + x == x);
      * }
      */
-    ast_value    *value = NULL;
-    ast_value    *x     = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_block    *body  = ast_block_new(intrin_ctx(intrin));
-    ast_function *func  = intrin_value(intrin, &value, "isinf", TYPE_FLOAT);
-
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    ast_value *val = nullptr;
+    ast_value *x = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_block *body = new ast_block(ctx());
+    ast_function *func = value(&val, "isinf", TYPE_FLOAT);
+
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_AND,
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+                new ast_binary(
+                    ctx(),
                     INSTR_NE_F,
-                    (ast_expression*)x,
-                    (ast_expression*)intrin->fold->imm_float[0]
+                    x,
+                    m_fold->m_imm_float[0]
                 ),
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+                new ast_binary(
+                    ctx(),
                     INSTR_EQ_F,
-                    (ast_expression*)ast_binary_new(
-                        intrin_ctx(intrin),
+                    new ast_binary(
+                        ctx(),
                         INSTR_ADD_F,
-                        (ast_expression*)x,
-                        (ast_expression*)x
+                        x,
+                        x
                     ),
-                    (ast_expression*)x
+                    x
                 )
             )
         )
     );
 
-    vec_push(value->expression.params, x);
-    vec_push(func->blocks, body);
+    val->m_type_params.emplace_back(x);
+    func->m_blocks.emplace_back(body);
 
-    intrin_reg(intrin, value, func);
+    reg(val, func);
 
-    return (ast_expression*)value;
+    return val;
 }
 
-static ast_expression *intrin_isnan(intrin_t *intrin) {
+ast_expression *intrin::isnan_() {
     /*
      * float isnan(float x) {
      *   float local;
@@ -146,262 +139,262 @@ static ast_expression *intrin_isnan(intrin_t *intrin) {
      *   return (x != local);
      * }
      */
-    ast_value    *value  = NULL;
-    ast_value    *arg1   = ast_value_new(intrin_ctx(intrin), "x",     TYPE_FLOAT);
-    ast_value    *local  = ast_value_new(intrin_ctx(intrin), "local", TYPE_FLOAT);
-    ast_block    *body   = ast_block_new(intrin_ctx(intrin));
-    ast_function *func   = intrin_value(intrin, &value, "isnan", TYPE_FLOAT);
-
-    vec_push(body->locals, local);
-    vec_push(body->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    ast_value *val = nullptr;
+    ast_value *arg1 = new ast_value(ctx(), "x",TYPE_FLOAT);
+    ast_value *local = new ast_value(ctx(), "local", TYPE_FLOAT);
+    ast_block *body  = new ast_block(ctx());
+    ast_function *func = value(&val, "isnan", TYPE_FLOAT);
+
+    body->m_locals.push_back(local);
+    body->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)local,
-            (ast_expression*)arg1
+            local,
+            arg1
         )
     );
 
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_NE_F,
-                (ast_expression*)arg1,
-                (ast_expression*)local
+                arg1,
+                local
             )
         )
     );
 
-    vec_push(value->expression.params, arg1);
-    vec_push(func->blocks, body);
+    val->m_type_params.emplace_back(arg1);
+    func->m_blocks.emplace_back(body);
 
-    intrin_reg(intrin, value, func);
+    reg(val, func);
 
-    return (ast_expression*)value;
+    return val;
 }
 
-static ast_expression *intrin_isnormal(intrin_t *intrin) {
+ast_expression *intrin::isnormal_() {
     /*
      * float isnormal(float x) {
      *     return isfinite(x);
      * }
      */
-    ast_value    *value         = NULL;
-    ast_call     *callisfinite  = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "isfinite", "isnormal"));
-    ast_value    *x             = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_block    *body          = ast_block_new(intrin_ctx(intrin));
-    ast_function *func          = intrin_value(intrin, &value, "isnormal", TYPE_FLOAT);
+    ast_value *val = nullptr;
+    ast_call *callisfinite = ast_call::make(ctx(), func_self("isfinite", "isnormal"));
+    ast_value *x = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_block *body = new ast_block(ctx());
+    ast_function *func = value(&val, "isnormal", TYPE_FLOAT);
 
-    vec_push(value->expression.params, x);
-    vec_push(callisfinite->params, (ast_expression*)x);
+    val->m_type_params.emplace_back(x);
+    callisfinite->m_params.push_back(x);
 
     /* return <callisfinite> */
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)callisfinite
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            callisfinite
         )
     );
 
-    vec_push(func->blocks, body);
-    intrin_reg(intrin, value, func);
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_signbit(intrin_t *intrin) {
+ast_expression *intrin::signbit_() {
     /*
      * float signbit(float x) {
      *     return (x < 0);
      * }
      */
-    ast_value    *value  = NULL;
-    ast_value    *x      = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_block    *body   = ast_block_new(intrin_ctx(intrin));
-    ast_function *func   = intrin_value(intrin, &value, "signbit", TYPE_FLOAT);
+    ast_value *val = nullptr;
+    ast_value *x = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_block *body = new ast_block(ctx());
+    ast_function *func = value(&val, "signbit", TYPE_FLOAT);
 
-    vec_push(value->expression.params, x);
+    val->m_type_params.emplace_back(x);
 
     /* return (x < 0); */
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_ternary_new(
-                intrin_ctx(intrin),
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            new ast_ternary(
+                ctx(),
+                new ast_binary(
+                    ctx(),
                     INSTR_LT,
-                    (ast_expression*)x,
-                    (ast_expression*)intrin->fold->imm_float[0]
+                    x,
+                    m_fold->m_imm_float[0]
                 ),
-                (ast_expression*)intrin->fold->imm_float[1],
-                (ast_expression*)intrin->fold->imm_float[0]
+                m_fold->m_imm_float[1],
+                m_fold->m_imm_float[0]
             )
         )
     );
 
-    vec_push(func->blocks, body);
-    intrin_reg(intrin, value, func);
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_acosh(intrin_t *intrin) {
+ast_expression *intrin::acosh_() {
     /*
      * float acosh(float x) {
      *     return log(x + sqrt((x * x) - 1));
      * }
      */
-    ast_value    *value    = NULL;
-    ast_value    *x        = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_call     *calllog  = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "log", "acosh"));
-    ast_call     *callsqrt = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "sqrt", "acosh"));
-    ast_block    *body     = ast_block_new(intrin_ctx(intrin));
-    ast_function *func     = intrin_value(intrin, &value, "acosh", TYPE_FLOAT);
+    ast_value    *val    = nullptr;
+    ast_value    *x        = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_call     *calllog  = ast_call::make(ctx(), func_self("log", "acosh"));
+    ast_call     *callsqrt = ast_call::make(ctx(), func_self("sqrt", "acosh"));
+    ast_block    *body     = new ast_block(ctx());
+    ast_function *func     = value(&val, "acosh", TYPE_FLOAT);
 
-    vec_push(value->expression.params, x);
+    val->m_type_params.emplace_back(x);
 
     /* <callsqrt> = sqrt((x * x) - 1); */
-    vec_push(callsqrt->params,
-        (ast_expression*)ast_binary_new(
-            intrin_ctx(intrin),
+    callsqrt->m_params.push_back(
+        new ast_binary(
+            ctx(),
             INSTR_SUB_F,
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+            new ast_binary(
+                ctx(),
                 INSTR_MUL_F,
-                (ast_expression*)x,
-                (ast_expression*)x
+                x,
+                x
             ),
-            (ast_expression*)intrin->fold->imm_float[1]
+            m_fold->m_imm_float[1]
         )
     );
 
     /* <calllog> = log(x + <callsqrt>); */
-    vec_push(calllog->params,
-        (ast_expression*)ast_binary_new(
-            intrin_ctx(intrin),
+    calllog->m_params.push_back(
+        new ast_binary(
+            ctx(),
             INSTR_ADD_F,
-            (ast_expression*)x,
-            (ast_expression*)callsqrt
+            x,
+            callsqrt
         )
     );
 
     /* return <calllog>; */
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)calllog
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            calllog
         )
     );
 
-    vec_push(func->blocks, body);
-    intrin_reg(intrin, value, func);
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_asinh(intrin_t *intrin) {
+ast_expression *intrin::asinh_() {
     /*
      * float asinh(float x) {
      *     return log(x + sqrt((x * x) + 1));
      * }
      */
-    ast_value    *value    = NULL;
-    ast_value    *x        = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_call     *calllog  = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "log", "asinh"));
-    ast_call     *callsqrt = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "sqrt", "asinh"));
-    ast_block    *body     = ast_block_new(intrin_ctx(intrin));
-    ast_function *func     = intrin_value(intrin, &value, "asinh", TYPE_FLOAT);
+    ast_value *val = nullptr;
+    ast_value *x = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_call *calllog = ast_call::make(ctx(), func_self("log", "asinh"));
+    ast_call *callsqrt = ast_call::make(ctx(), func_self("sqrt", "asinh"));
+    ast_block *body = new ast_block(ctx());
+    ast_function *func = value(&val, "asinh", TYPE_FLOAT);
 
-    vec_push(value->expression.params, x);
+    val->m_type_params.emplace_back(x);
 
     /* <callsqrt> = sqrt((x * x) + 1); */
-    vec_push(callsqrt->params,
-        (ast_expression*)ast_binary_new(
-            intrin_ctx(intrin),
+    callsqrt->m_params.push_back(
+        new ast_binary(
+            ctx(),
             INSTR_ADD_F,
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+            new ast_binary(
+                ctx(),
                 INSTR_MUL_F,
-                (ast_expression*)x,
-                (ast_expression*)x
+                x,
+                x
             ),
-            (ast_expression*)intrin->fold->imm_float[1]
+            m_fold->m_imm_float[1]
         )
     );
 
     /* <calllog> = log(x + <callsqrt>); */
-    vec_push(calllog->params,
-        (ast_expression*)ast_binary_new(
-            intrin_ctx(intrin),
+    calllog->m_params.push_back(
+        new ast_binary(
+            ctx(),
             INSTR_ADD_F,
-            (ast_expression*)x,
-            (ast_expression*)callsqrt
+            x,
+            callsqrt
         )
     );
 
     /* return <calllog>; */
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)calllog
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            calllog
         )
     );
 
-    vec_push(func->blocks, body);
-    intrin_reg(intrin, value, func);
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_atanh(intrin_t *intrin) {
+ast_expression *intrin::atanh_() {
     /*
      * float atanh(float x) {
      *     return 0.5 * log((1 + x) / (1 - x))
      * }
      */
-    ast_value    *value   = NULL;
-    ast_value    *x       = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_call     *calllog = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "log", "atanh"));
-    ast_block    *body    = ast_block_new(intrin_ctx(intrin));
-    ast_function *func    = intrin_value(intrin, &value, "atanh", TYPE_FLOAT);
+    ast_value    *val   = nullptr;
+    ast_value    *x       = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_call     *calllog = ast_call::make(ctx(), func_self("log", "atanh"));
+    ast_block    *body    = new ast_block(ctx());
+    ast_function *func    = value(&val, "atanh", TYPE_FLOAT);
 
-    vec_push(value->expression.params, x);
+    val->m_type_params.emplace_back(x);
 
     /* <callog> = log((1 + x) / (1 - x)); */
-    vec_push(calllog->params,
-        (ast_expression*)ast_binary_new(
-            intrin_ctx(intrin),
+    calllog->m_params.push_back(
+        new ast_binary(
+            ctx(),
             INSTR_DIV_F,
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+            new ast_binary(
+                ctx(),
                 INSTR_ADD_F,
-                (ast_expression*)intrin->fold->imm_float[1],
-                (ast_expression*)x
+                m_fold->m_imm_float[1],
+                x
             ),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+            new ast_binary(
+                ctx(),
                 INSTR_SUB_F,
-                (ast_expression*)intrin->fold->imm_float[1],
-                (ast_expression*)x
+                m_fold->m_imm_float[1],
+                x
             )
         )
     );
 
     /* return 0.5 * <calllog>; */
-    vec_push(body->exprs,
-        (ast_expression*)ast_binary_new(
-            intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_binary(
+            ctx(),
             INSTR_MUL_F,
-            (ast_expression*)fold_constgen_float(intrin->fold, 0.5, false),
-            (ast_expression*)calllog
+            m_fold->constgen_float(0.5, false),
+            calllog
         )
     );
 
-    vec_push(func->blocks, body);
-    intrin_reg(intrin, value, func);
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_exp(intrin_t *intrin) {
+ast_expression *intrin::exp_() {
     /*
      * float exp(float x) {
      *     float sum = 1.0;
@@ -413,36 +406,37 @@ static ast_expression *intrin_exp(intrin_t *intrin) {
      *     return sum;
      * }
      */
-    ast_value    *value = NULL;
-    ast_value    *x     = ast_value_new(intrin_ctx(intrin), "x",   TYPE_FLOAT);
-    ast_value    *sum   = ast_value_new(intrin_ctx(intrin), "sum", TYPE_FLOAT);
-    ast_value    *acc   = ast_value_new(intrin_ctx(intrin), "acc", TYPE_FLOAT);
-    ast_value    *i     = ast_value_new(intrin_ctx(intrin), "i",   TYPE_FLOAT);
-    ast_block    *body  = ast_block_new(intrin_ctx(intrin));
-    ast_function *func  = intrin_value(intrin, &value, "exp", TYPE_FLOAT);
-
-    vec_push(value->expression.params, x);
-    vec_push(body->locals, sum);
-    vec_push(body->locals, acc);
-    vec_push(body->locals, i);
+    ast_value *val = nullptr;
+    ast_value *x = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_value *sum = new ast_value(ctx(), "sum", TYPE_FLOAT);
+    ast_value *acc = new ast_value(ctx(), "acc", TYPE_FLOAT);
+    ast_value *i = new ast_value(ctx(), "i",   TYPE_FLOAT);
+    ast_block *body = new ast_block(ctx());
+    ast_function *func = value(&val, "exp", TYPE_FLOAT);
+
+    val->m_type_params.emplace_back(x);
+
+    body->m_locals.push_back(sum);
+    body->m_locals.push_back(acc);
+    body->m_locals.push_back(i);
 
     /* sum = 1.0; */
-    vec_push(body->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)sum,
-            (ast_expression*)intrin->fold->imm_float[1]
+            sum,
+            m_fold->m_imm_float[1]
         )
     );
 
     /* acc = 1.0; */
-    vec_push(body->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)acc,
-            (ast_expression*)intrin->fold->imm_float[1]
+            acc,
+            m_fold->m_imm_float[1]
         )
     );
 
@@ -450,50 +444,50 @@ static ast_expression *intrin_exp(intrin_t *intrin) {
      * for (i = 1; i < 200; ++i)
      *     sum += (acc *= x / i);
      */
-    vec_push(body->exprs,
-        (ast_expression*)ast_loop_new(
-            intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_loop(
+            ctx(),
             /* i = 1; */
-            (ast_expression*)ast_store_new(
-                intrin_ctx(intrin),
+            new ast_store(
+                ctx(),
                 INSTR_STORE_F,
-                (ast_expression*)i,
-                (ast_expression*)intrin->fold->imm_float[1]
+                i,
+                m_fold->m_imm_float[1]
             ),
             /* i < 200; */
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+            new ast_binary(
+                ctx(),
                 INSTR_LT,
-                (ast_expression*)i,
-                (ast_expression*)fold_constgen_float(intrin->fold, 200.0f, false)
+                i,
+                m_fold->constgen_float(200.0f, false)
             ),
             false,
-            NULL,
+            nullptr,
             false,
             /* ++i; */
-            (ast_expression*)ast_binstore_new(
-                intrin_ctx(intrin),
+            new ast_binstore(
+                ctx(),
                 INSTR_STORE_F,
                 INSTR_ADD_F,
-                (ast_expression*)i,
-                (ast_expression*)intrin->fold->imm_float[1]
+                i,
+                m_fold->m_imm_float[1]
             ),
             /* sum += (acc *= (x / i)) */
-            (ast_expression*)ast_binstore_new(
-                intrin_ctx(intrin),
+            new ast_binstore(
+                ctx(),
                 INSTR_STORE_F,
                 INSTR_ADD_F,
-                (ast_expression*)sum,
-                (ast_expression*)ast_binstore_new(
-                    intrin_ctx(intrin),
+                sum,
+                new ast_binstore(
+                    ctx(),
                     INSTR_STORE_F,
                     INSTR_MUL_F,
-                    (ast_expression*)acc,
-                    (ast_expression*)ast_binary_new(
-                        intrin_ctx(intrin),
+                    acc,
+                    new ast_binary(
+                        ctx(),
                         INSTR_DIV_F,
-                        (ast_expression*)x,
-                        (ast_expression*)i
+                        x,
+                        i
                     )
                 )
             )
@@ -501,86 +495,86 @@ static ast_expression *intrin_exp(intrin_t *intrin) {
     );
 
     /* return sum; */
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)sum
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            sum
         )
     );
 
-    vec_push(func->blocks, body);
-
-    intrin_reg(intrin, value, func);
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_exp2(intrin_t *intrin) {
+ast_expression *intrin::exp2_() {
     /*
      * float exp2(float x) {
      *     return pow(2, x);
      * }
      */
-    ast_value    *value     = NULL;
-    ast_call     *callpow   = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "pow", "exp2"));
-    ast_value    *arg1      = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_block    *body      = ast_block_new(intrin_ctx(intrin));
-    ast_function *func      = intrin_value(intrin, &value, "exp2", TYPE_FLOAT);
+    ast_value *val = nullptr;
+    ast_call  *callpow = ast_call::make(ctx(), func_self("pow", "exp2"));
+    ast_value *arg1 = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_block *body = new ast_block(ctx());
+    ast_function *func = value(&val, "exp2", TYPE_FLOAT);
 
-    vec_push(value->expression.params, arg1);
+    val->m_type_params.emplace_back(arg1);
 
-    vec_push(callpow->params, (ast_expression*)intrin->fold->imm_float[3]);
-    vec_push(callpow->params, (ast_expression*)arg1);
+    callpow->m_params.push_back(m_fold->m_imm_float[3]);
+    callpow->m_params.push_back(arg1);
 
     /* return <callpow> */
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)callpow
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            callpow
         )
     );
 
-    vec_push(func->blocks, body);
-
-    intrin_reg(intrin, value, func);
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_expm1(intrin_t *intrin) {
+ast_expression *intrin::expm1_() {
     /*
      * float expm1(float x) {
      *     return exp(x) - 1;
      * }
      */
-    ast_value    *value    = NULL;
-    ast_call     *callexp  = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "exp", "expm1"));
-    ast_value    *x        = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_block    *body     = ast_block_new(intrin_ctx(intrin));
-    ast_function *func     = intrin_value(intrin, &value, "expm1", TYPE_FLOAT);
+    ast_value *val = nullptr;
+    ast_call *callexp = ast_call::make(ctx(), func_self("exp", "expm1"));
+    ast_value *x = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_block *body = new ast_block(ctx());
+    ast_function *func = value(&val, "expm1", TYPE_FLOAT);
 
-    vec_push(value->expression.params, x);
+    val->m_type_params.emplace_back(x);
 
     /* <callexp> = exp(x); */
-    vec_push(callexp->params, (ast_expression*)x);
+    callexp->m_params.push_back(x);
 
     /* return <callexp> - 1; */
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_SUB_F,
-                (ast_expression*)callexp,
-                (ast_expression*)intrin->fold->imm_float[1]
+                callexp,
+                m_fold->m_imm_float[1]
             )
         )
     );
 
-    vec_push(func->blocks, body);
-    intrin_reg(intrin, value, func);
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_pow(intrin_t *intrin) {
+ast_expression *intrin::pow_() {
+               #define QC_POW_EPSILON 0.00001f
+
     /*
      *
      * float pow(float base, float exp) {
@@ -622,27 +616,27 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
      *     return accumulate;
      * }
      */
-    ast_value    *value = NULL;
-    ast_function *func = intrin_value(intrin, &value, "pow", TYPE_FLOAT);
+    ast_value    *val = nullptr;
+    ast_function *func = value(&val, "pow", TYPE_FLOAT);
 
     /* prepare some calls for later */
-    ast_call *callpow1  = ast_call_new(intrin_ctx(intrin), (ast_expression*)value);                  /* for pow(base, -exp)    */
-    ast_call *callpow2  = ast_call_new(intrin_ctx(intrin), (ast_expression*)value);                  /* for pow(vase, exp / 2) */
-    ast_call *callsqrt1 = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "sqrt", "pow")); /* for sqrt(base)         */
-    ast_call *callsqrt2 = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "sqrt", "pow")); /* for sqrt(square)       */
-    ast_call *callfabs  = ast_call_new(intrin_ctx(intrin), intrin_func_self(intrin, "fabs", "pow")); /* for fabs(mid - exp)    */
+    ast_call *callpow1  = ast_call::make(ctx(), val);                  /* for pow(base, -exp)    */
+    ast_call *callpow2  = ast_call::make(ctx(), val);                  /* for pow(vase, exp / 2) */
+    ast_call *callsqrt1 = ast_call::make(ctx(), func_self("sqrt", "pow")); /* for sqrt(base)         */
+    ast_call *callsqrt2 = ast_call::make(ctx(), func_self("sqrt", "pow")); /* for sqrt(square)       */
+    ast_call *callfabs  = ast_call::make(ctx(), func_self("fabs", "pow")); /* for fabs(mid - exp)    */
 
     /* prepare some blocks for later */
-    ast_block *expgt1       = ast_block_new(intrin_ctx(intrin));
-    ast_block *midltexp     = ast_block_new(intrin_ctx(intrin));
-    ast_block *midltexpelse = ast_block_new(intrin_ctx(intrin));
-    ast_block *whileblock   = ast_block_new(intrin_ctx(intrin));
+    ast_block *expgt1       = new ast_block(ctx());
+    ast_block *midltexp     = new ast_block(ctx());
+    ast_block *midltexpelse = new ast_block(ctx());
+    ast_block *whileblock   = new ast_block(ctx());
 
     /* float pow(float base, float exp) */
-    ast_value    *base = ast_value_new(intrin_ctx(intrin), "base", TYPE_FLOAT);
-    ast_value    *exp  = ast_value_new(intrin_ctx(intrin), "exp",  TYPE_FLOAT);
+    ast_value    *base = new ast_value(ctx(), "base", TYPE_FLOAT);
+    ast_value    *exp  = new ast_value(ctx(), "exp",  TYPE_FLOAT);
     /* { */
-    ast_block    *body = ast_block_new(intrin_ctx(intrin));
+    ast_block    *body = new ast_block(ctx());
 
     /*
      * float result;
@@ -652,40 +646,40 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
      * float accumulate;
      * float mid;
      */
-    ast_value *result     = ast_value_new(intrin_ctx(intrin), "result",     TYPE_FLOAT);
-    ast_value *low        = ast_value_new(intrin_ctx(intrin), "low",        TYPE_FLOAT);
-    ast_value *high       = ast_value_new(intrin_ctx(intrin), "high",       TYPE_FLOAT);
-    ast_value *square     = ast_value_new(intrin_ctx(intrin), "square",     TYPE_FLOAT);
-    ast_value *accumulate = ast_value_new(intrin_ctx(intrin), "accumulate", TYPE_FLOAT);
-    ast_value *mid        = ast_value_new(intrin_ctx(intrin), "mid",        TYPE_FLOAT);
-    vec_push(body->locals, result);
-    vec_push(body->locals, low);
-    vec_push(body->locals, high);
-    vec_push(body->locals, square);
-    vec_push(body->locals, accumulate);
-    vec_push(body->locals, mid);
-
-    vec_push(value->expression.params, base);
-    vec_push(value->expression.params, exp);
+    ast_value *result     = new ast_value(ctx(), "result",     TYPE_FLOAT);
+    ast_value *low        = new ast_value(ctx(), "low",        TYPE_FLOAT);
+    ast_value *high       = new ast_value(ctx(), "high",       TYPE_FLOAT);
+    ast_value *square     = new ast_value(ctx(), "square",     TYPE_FLOAT);
+    ast_value *accumulate = new ast_value(ctx(), "accumulate", TYPE_FLOAT);
+    ast_value *mid        = new ast_value(ctx(), "mid",        TYPE_FLOAT);
+    body->m_locals.push_back(result);
+    body->m_locals.push_back(low);
+    body->m_locals.push_back(high);
+    body->m_locals.push_back(square);
+    body->m_locals.push_back(accumulate);
+    body->m_locals.push_back(mid);
+
+    val->m_type_params.emplace_back(base);
+    val->m_type_params.emplace_back(exp);
 
     /*
      * if (exp == 0.0)
      *     return 1;
      */
-    vec_push(body->exprs,
-        (ast_expression*)ast_ifthen_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_ifthen(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_EQ_F,
-                (ast_expression*)exp,
-                (ast_expression*)intrin->fold->imm_float[0]
+                exp,
+                m_fold->m_imm_float[0]
             ),
-            (ast_expression*)ast_return_new(
-                intrin_ctx(intrin),
-                (ast_expression*)intrin->fold->imm_float[1]
+            new ast_return(
+                ctx(),
+                m_fold->m_imm_float[1]
             ),
-            NULL
+            nullptr
         )
     );
 
@@ -693,30 +687,30 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
      * if (exp == 1.0)
      *     return base;
      */
-    vec_push(body->exprs,
-        (ast_expression*)ast_ifthen_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_ifthen(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_EQ_F,
-                (ast_expression*)exp,
-                (ast_expression*)intrin->fold->imm_float[1]
+                exp,
+                m_fold->m_imm_float[1]
             ),
-            (ast_expression*)ast_return_new(
-                intrin_ctx(intrin),
-                (ast_expression*)base
+            new ast_return(
+                ctx(),
+                base
             ),
-            NULL
+            nullptr
         )
     );
 
     /* <callpow1> = pow(base, -exp) */
-    vec_push(callpow1->params, (ast_expression*)base);
-    vec_push(callpow1->params,
-        (ast_expression*)ast_unary_new(
-            intrin_ctx(intrin),
+    callpow1->m_params.push_back(base);
+    callpow1->m_params.push_back(
+        ast_unary::make(
+            ctx(),
             VINSTR_NEG_F,
-            (ast_expression*)exp
+            exp
         )
     );
 
@@ -724,36 +718,36 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
      * if (exp < 0)
      *     return 1.0 / <callpow1>;
      */
-    vec_push(body->exprs,
-        (ast_expression*)ast_ifthen_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_ifthen(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_LT,
-                (ast_expression*)exp,
-                (ast_expression*)intrin->fold->imm_float[0]
+                exp,
+                m_fold->m_imm_float[0]
             ),
-            (ast_expression*)ast_return_new(
-                intrin_ctx(intrin),
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+            new ast_return(
+                ctx(),
+                new ast_binary(
+                    ctx(),
                     INSTR_DIV_F,
-                    (ast_expression*)intrin->fold->imm_float[1],
-                    (ast_expression*)callpow1
+                    m_fold->m_imm_float[1],
+                    callpow1
                 )
             ),
-            NULL
+            nullptr
         )
     );
 
     /* <callpow2> = pow(base, exp / 2) */
-    vec_push(callpow2->params, (ast_expression*)base);
-    vec_push(callpow2->params,
-        (ast_expression*)ast_binary_new(
-            intrin_ctx(intrin),
+    callpow2->m_params.push_back(base);
+    callpow2->m_params.push_back(
+        new ast_binary(
+            ctx(),
             INSTR_DIV_F,
-            (ast_expression*)exp,
-            (ast_expression*)intrin->fold->imm_float[3] /* 2.0f */
+            exp,
+            m_fold->m_imm_float[3] /* 2.0f */
         )
     );
 
@@ -763,22 +757,22 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
      *     return result * result;
      * }
      */
-    vec_push(expgt1->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    expgt1->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)result,
-            (ast_expression*)callpow2
+            result,
+            callpow2
         )
     );
-    vec_push(expgt1->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    expgt1->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_MUL_F,
-                (ast_expression*)result,
-                (ast_expression*)result
+                result,
+                result
             )
         )
     );
@@ -788,24 +782,24 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
      *     <expgt1>
      * }
      */
-    vec_push(body->exprs,
-        (ast_expression*)ast_ifthen_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_ifthen(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_GE,
-                (ast_expression*)exp,
-                (ast_expression*)intrin->fold->imm_float[1]
+                exp,
+                m_fold->m_imm_float[1]
             ),
-            (ast_expression*)expgt1,
-            NULL
+            expgt1,
+            nullptr
         )
     );
 
     /*
      * <callsqrt1> = sqrt(base)
      */
-    vec_push(callsqrt1->params, (ast_expression*)base);
+    callsqrt1->m_params.push_back(base);
 
     /*
      * low        = 0.0f;
@@ -814,49 +808,49 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
      * accumulate = square;
      * mid        = high / 2.0f;
      */
-    vec_push(body->exprs,
-        (ast_expression*)ast_store_new(intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_store(ctx(),
             INSTR_STORE_F,
-            (ast_expression*)low,
-            (ast_expression*)intrin->fold->imm_float[0]
+            low,
+            m_fold->m_imm_float[0]
         )
     );
-    vec_push(body->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)high,
-            (ast_expression*)intrin->fold->imm_float[1]
+            high,
+            m_fold->m_imm_float[1]
         )
     );
 
-    vec_push(body->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)square,
-            (ast_expression*)callsqrt1
+            square,
+            callsqrt1
         )
     );
 
-    vec_push(body->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)accumulate,
-            (ast_expression*)square
+            accumulate,
+            square
         )
     );
-    vec_push(body->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)mid,
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+            mid,
+            new ast_binary(
+                ctx(),
                 INSTR_DIV_F,
-                (ast_expression*)high,
-                (ast_expression*)intrin->fold->imm_float[3] /* 2.0f */
+                high,
+                m_fold->m_imm_float[3] /* 2.0f */
             )
         )
     );
@@ -867,21 +861,21 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
      *     accumulate *= square;
      * }
      */
-    vec_push(midltexp->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    midltexp->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)low,
-            (ast_expression*)mid
+            low,
+            mid
         )
     );
-    vec_push(midltexp->exprs,
-        (ast_expression*)ast_binstore_new(
-            intrin_ctx(intrin),
+    midltexp->m_exprs.push_back(
+        new ast_binstore(
+            ctx(),
             INSTR_STORE_F,
             INSTR_MUL_F,
-            (ast_expression*)accumulate,
-            (ast_expression*)square
+            accumulate,
+            square
         )
     );
 
@@ -891,25 +885,25 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
      *     accumulate *= (1.0 / square);
      * }
      */
-    vec_push(midltexpelse->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    midltexpelse->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)high,
-            (ast_expression*)mid
+            high,
+            mid
         )
     );
-    vec_push(midltexpelse->exprs,
-        (ast_expression*)ast_binstore_new(
-            intrin_ctx(intrin),
+    midltexpelse->m_exprs.push_back(
+        new ast_binstore(
+            ctx(),
             INSTR_STORE_F,
             INSTR_MUL_F,
-            (ast_expression*)accumulate,
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+            accumulate,
+            new ast_binary(
+                ctx(),
                 INSTR_DIV_F,
-                (ast_expression*)intrin->fold->imm_float[1],
-                (ast_expression*)square
+                m_fold->m_imm_float[1],
+                square
             )
         )
     );
@@ -917,7 +911,7 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
     /*
      * <callsqrt2> = sqrt(square)
      */
-    vec_push(callsqrt2->params, (ast_expression*)square);
+    callsqrt2->m_params.push_back(square);
 
     /*
      * <whileblock> = {
@@ -930,42 +924,42 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
      *     mid = (low + high) / 2;
      * }
      */
-    vec_push(whileblock->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    whileblock->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)square,
-            (ast_expression*)callsqrt2
+            square,
+            callsqrt2
         )
     );
-    vec_push(whileblock->exprs,
-        (ast_expression*)ast_ifthen_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    whileblock->m_exprs.push_back(
+        new ast_ifthen(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_LT,
-                (ast_expression*)mid,
-                (ast_expression*)exp
+                mid,
+                exp
             ),
-            (ast_expression*)midltexp,
-            (ast_expression*)midltexpelse
+            midltexp,
+            midltexpelse
         )
     );
-    vec_push(whileblock->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    whileblock->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)mid,
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+            mid,
+            new ast_binary(
+                ctx(),
                 INSTR_DIV_F,
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+                new ast_binary(
+                    ctx(),
                     INSTR_ADD_F,
-                    (ast_expression*)low,
-                    (ast_expression*)high
+                    low,
+                    high
                 ),
-                (ast_expression*)intrin->fold->imm_float[3] /* 2.0f */
+                m_fold->m_imm_float[3] /* 2.0f */
             )
         )
     );
@@ -973,12 +967,12 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
     /*
      * <callabs> = fabs(mid - exp)
      */
-    vec_push(callfabs->params,
-        (ast_expression*)ast_binary_new(
-            intrin_ctx(intrin),
+    callfabs->m_params.push_back(
+        new ast_binary(
+            ctx(),
             INSTR_SUB_F,
-            (ast_expression*)mid,
-            (ast_expression*)exp
+            mid,
+            exp
         )
     );
 
@@ -986,47 +980,46 @@ static ast_expression *intrin_pow(intrin_t *intrin) {
      * while (<callfabs>  > epsilon)
      *     <whileblock>
      */
-    vec_push(body->exprs,
-        (ast_expression*)ast_loop_new(
-            intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_loop(
+            ctx(),
             /* init */
-            NULL,
+            nullptr,
             /* pre condition */
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+            new ast_binary(
+                ctx(),
                 INSTR_GT,
-                (ast_expression*)callfabs,
-                (ast_expression*)fold_constgen_float(intrin->fold, QC_POW_EPSILON, false)
+                callfabs,
+                m_fold->constgen_float(QC_POW_EPSILON, false)
             ),
             /* pre not */
             false,
             /* post condition */
-            NULL,
+            nullptr,
             /* post not */
             false,
             /* increment expression */
-            NULL,
+            nullptr,
             /* code block */
-            (ast_expression*)whileblock
+            whileblock
         )
     );
 
     /* return accumulate */
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)accumulate
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            accumulate
         )
     );
 
     /* } */
-    vec_push(func->blocks, body);
-
-    intrin_reg(intrin, value, func);
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_mod(intrin_t *intrin) {
+ast_expression *intrin::mod_() {
     /*
      * float mod(float a, float b) {
      *     float div = a / b;
@@ -1034,136 +1027,134 @@ static ast_expression *intrin_mod(intrin_t *intrin) {
      *     return a - b * sign * floor(sign * div);
      * }
      */
-    ast_value    *value = NULL;
-    ast_call     *call  = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "floor", "mod"));
-    ast_value    *a     = ast_value_new(intrin_ctx(intrin), "a",    TYPE_FLOAT);
-    ast_value    *b     = ast_value_new(intrin_ctx(intrin), "b",    TYPE_FLOAT);
-    ast_value    *div   = ast_value_new(intrin_ctx(intrin), "div",  TYPE_FLOAT);
-    ast_value    *sign  = ast_value_new(intrin_ctx(intrin), "sign", TYPE_FLOAT);
-    ast_block    *body  = ast_block_new(intrin_ctx(intrin));
-    ast_function *func  = intrin_value(intrin, &value, "mod", TYPE_FLOAT);
+    ast_value    *val = nullptr;
+    ast_call     *call  = ast_call::make(ctx(), func_self("floor", "mod"));
+    ast_value    *a     = new ast_value(ctx(), "a",    TYPE_FLOAT);
+    ast_value    *b     = new ast_value(ctx(), "b",    TYPE_FLOAT);
+    ast_value    *div   = new ast_value(ctx(), "div",  TYPE_FLOAT);
+    ast_value    *sign  = new ast_value(ctx(), "sign", TYPE_FLOAT);
+    ast_block    *body  = new ast_block(ctx());
+    ast_function *func  = value(&val, "mod", TYPE_FLOAT);
 
-    vec_push(value->expression.params, a);
-    vec_push(value->expression.params, b);
+    val->m_type_params.emplace_back(a);
+    val->m_type_params.emplace_back(b);
 
-    vec_push(body->locals, div);
-    vec_push(body->locals, sign);
+    body->m_locals.push_back(div);
+    body->m_locals.push_back(sign);
 
     /* div = a / b; */
-    vec_push(body->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)div,
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+            div,
+            new ast_binary(
+                ctx(),
                 INSTR_DIV_F,
-                (ast_expression*)a,
-                (ast_expression*)b
+                a,
+                b
             )
         )
     );
 
     /* sign = (div < 0.0f) ? -1 : 1; */
-    vec_push(body->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)sign,
-            (ast_expression*)ast_ternary_new(
-                intrin_ctx(intrin),
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+            sign,
+            new ast_ternary(
+                ctx(),
+                new ast_binary(
+                    ctx(),
                     INSTR_LT,
-                    (ast_expression*)div,
-                    (ast_expression*)intrin->fold->imm_float[0]
+                    div,
+                    m_fold->m_imm_float[0]
                 ),
-                (ast_expression*)intrin->fold->imm_float[2],
-                (ast_expression*)intrin->fold->imm_float[1]
+                m_fold->m_imm_float[2],
+                m_fold->m_imm_float[1]
             )
         )
     );
 
     /* floor(sign * div) */
-    vec_push(call->params,
-        (ast_expression*)ast_binary_new(
-            intrin_ctx(intrin),
+    call->m_params.push_back(
+        new ast_binary(
+            ctx(),
             INSTR_MUL_F,
-            (ast_expression*)sign,
-            (ast_expression*)div
+            sign,
+            div
         )
     );
 
     /* return a - b * sign * <call> */
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_SUB_F,
-                (ast_expression*)a,
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+                a,
+                new ast_binary(
+                    ctx(),
                     INSTR_MUL_F,
-                    (ast_expression*)b,
-                    (ast_expression*)ast_binary_new(
-                        intrin_ctx(intrin),
+                    b,
+                    new ast_binary(
+                        ctx(),
                         INSTR_MUL_F,
-                        (ast_expression*)sign,
-                        (ast_expression*)call
+                        sign,
+                        call
                     )
                 )
             )
         )
     );
 
-    vec_push(func->blocks, body);
-    intrin_reg(intrin, value, func);
-
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_fabs(intrin_t *intrin) {
+ast_expression *intrin::fabs_() {
     /*
      * float fabs(float x) {
      *     return x < 0 ? -x : x;
      * }
      */
-    ast_value    *value  = NULL;
-    ast_value    *arg1   = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_block    *body   = ast_block_new(intrin_ctx(intrin));
-    ast_function *func   = intrin_value(intrin, &value, "fabs", TYPE_FLOAT);
-
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_ternary_new(
-                intrin_ctx(intrin),
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+    ast_value    *val  = nullptr;
+    ast_value    *arg1   = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_block    *body   = new ast_block(ctx());
+    ast_function *func   = value(&val, "fabs", TYPE_FLOAT);
+
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            new ast_ternary(
+                ctx(),
+                new ast_binary(
+                    ctx(),
                     INSTR_LE,
-                    (ast_expression*)arg1,
-                    (ast_expression*)intrin->fold->imm_float[0]
+                    arg1,
+                    m_fold->m_imm_float[0]
                 ),
-                (ast_expression*)ast_unary_new(
-                    intrin_ctx(intrin),
+                ast_unary::make(
+                    ctx(),
                     VINSTR_NEG_F,
-                    (ast_expression*)arg1
+                    arg1
                 ),
-                (ast_expression*)arg1
+                arg1
             )
         )
     );
 
-    vec_push(value->expression.params, arg1);
-    vec_push(func->blocks, body);
-
-    intrin_reg(intrin, value, func);
+    val->m_type_params.emplace_back(arg1);
 
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_epsilon(intrin_t *intrin) {
+ast_expression *intrin::epsilon_() {
     /*
      * float epsilon(void) {
      *     float eps = 1.0f;
@@ -1171,113 +1162,111 @@ static ast_expression *intrin_epsilon(intrin_t *intrin) {
      *     return eps;
      * }
      */
-    ast_value    *value  = NULL;
-    ast_value    *eps    = ast_value_new(intrin_ctx(intrin), "eps", TYPE_FLOAT);
-    ast_block    *body   = ast_block_new(intrin_ctx(intrin));
-    ast_function *func   = intrin_value(intrin, &value, "epsilon", TYPE_FLOAT);
+    ast_value    *val  = nullptr;
+    ast_value    *eps    = new ast_value(ctx(), "eps", TYPE_FLOAT);
+    ast_block    *body   = new ast_block(ctx());
+    ast_function *func   = value(&val, "epsilon", TYPE_FLOAT);
 
-    vec_push(body->locals, eps);
+    body->m_locals.push_back(eps);
 
     /* eps = 1.0f; */
-    vec_push(body->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    body->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)eps,
-            (ast_expression*)intrin->fold->imm_float[0]
+            eps,
+            m_fold->m_imm_float[0]
         )
     );
 
-    vec_push(body->exprs,
-        (ast_expression*)ast_loop_new(
-            intrin_ctx(intrin),
-            NULL,
-            NULL,
+    body->m_exprs.push_back(
+        new ast_loop(
+            ctx(),
+            nullptr,
+            nullptr,
             false,
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+            new ast_binary(
+                ctx(),
                 INSTR_NE_F,
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+                new ast_binary(
+                    ctx(),
                     INSTR_ADD_F,
-                    (ast_expression*)intrin->fold->imm_float[1],
-                    (ast_expression*)ast_binary_new(
-                        intrin_ctx(intrin),
+                    m_fold->m_imm_float[1],
+                    new ast_binary(
+                        ctx(),
                         INSTR_MUL_F,
-                        (ast_expression*)eps,
-                        (ast_expression*)intrin->fold->imm_float[3] /* 2.0f */
+                        eps,
+                        m_fold->m_imm_float[3] /* 2.0f */
                     )
                 ),
-                (ast_expression*)intrin->fold->imm_float[1]
+                m_fold->m_imm_float[1]
             ),
             false,
-            NULL,
-            (ast_expression*)ast_binstore_new(
-                intrin_ctx(intrin),
+            nullptr,
+            new ast_binstore(
+                ctx(),
                 INSTR_STORE_F,
                 INSTR_DIV_F,
-                (ast_expression*)eps,
-                (ast_expression*)intrin->fold->imm_float[3] /* 2.0f */
+                eps,
+                m_fold->m_imm_float[3] /* 2.0f */
             )
         )
     );
 
     /* return eps; */
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)eps
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            eps
         )
     );
 
-    vec_push(func->blocks, body);
-    intrin_reg(intrin, value, func);
-
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_nan(intrin_t *intrin) {
+ast_expression *intrin::nan_() {
     /*
      * float nan(void) {
      *     float x = 0.0f;
      *     return x / x;
      * }
      */
-    ast_value    *value  = NULL;
-    ast_value    *x      = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_function *func   = intrin_value(intrin, &value, "nan", TYPE_FLOAT);
-    ast_block    *block  = ast_block_new(intrin_ctx(intrin));
+    ast_value    *val  = nullptr;
+    ast_value    *x      = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_function *func   = value(&val, "nan", TYPE_FLOAT);
+    ast_block    *block  = new ast_block(ctx());
 
-    vec_push(block->locals, x);
+    block->m_locals.push_back(x);
 
-    vec_push(block->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    block->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)x,
-            (ast_expression*)intrin->fold->imm_float[0]
+            x,
+            m_fold->m_imm_float[0]
         )
     );
 
-    vec_push(block->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    block->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_DIV_F,
-                (ast_expression*)x,
-                (ast_expression*)x
+                x,
+                x
             )
         )
     );
 
-    vec_push(func->blocks, block);
-    intrin_reg(intrin, value, func);
-
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(block);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_inf(intrin_t *intrin) {
+ast_expression *intrin::inf_() {
     /*
      * float inf(void) {
      *     float x = 1.0f;
@@ -1285,47 +1274,46 @@ static ast_expression *intrin_inf(intrin_t *intrin) {
      *     return x / y;
      * }
      */
-    ast_value    *value  = NULL;
-    ast_value    *x      = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_value    *y      = ast_value_new(intrin_ctx(intrin), "y", TYPE_FLOAT);
-    ast_function *func   = intrin_value(intrin, &value, "inf", TYPE_FLOAT);
-    ast_block    *block  = ast_block_new(intrin_ctx(intrin));
+    ast_value    *val  = nullptr;
+    ast_value    *x      = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_value    *y      = new ast_value(ctx(), "y", TYPE_FLOAT);
+    ast_function *func   = value(&val, "inf", TYPE_FLOAT);
+    ast_block    *block  = new ast_block(ctx());
     size_t        i;
 
-    vec_push(block->locals, x);
-    vec_push(block->locals, y);
+    block->m_locals.push_back(x);
+    block->m_locals.push_back(y);
 
     /* to keep code size down */
     for (i = 0; i <= 1; i++) {
-        vec_push(block->exprs,
-            (ast_expression*)ast_store_new(
-                intrin_ctx(intrin),
+        block->m_exprs.push_back(
+            new ast_store(
+                ctx(),
                 INSTR_STORE_F,
-                (ast_expression*)((i == 0) ? x : y),
-                (ast_expression*)intrin->fold->imm_float[i]
+                ((i == 0) ? x : y),
+                m_fold->m_imm_float[i]
             )
         );
     }
 
-    vec_push(block->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    block->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_DIV_F,
-                (ast_expression*)x,
-                (ast_expression*)y
+                x,
+                y
             )
         )
     );
 
-    vec_push(func->blocks, block);
-    intrin_reg(intrin, value, func);
-
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(block);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_ln(intrin_t *intrin) {
+ast_expression *intrin::ln_() {
     /*
      * float log(float power, float base) {
      *   float whole;
@@ -1391,64 +1379,64 @@ static ast_expression *intrin_ln(intrin_t *intrin) {
      * }
      */
 
-    ast_value    *value      = NULL;
-    ast_value    *power      = ast_value_new(intrin_ctx(intrin), "power",     TYPE_FLOAT);
-    ast_value    *base       = ast_value_new(intrin_ctx(intrin), "base",      TYPE_FLOAT);
-    ast_value    *whole      = ast_value_new(intrin_ctx(intrin), "whole",     TYPE_FLOAT);
-    ast_value    *nth        = ast_value_new(intrin_ctx(intrin), "nth",       TYPE_FLOAT);
-    ast_value    *sign       = ast_value_new(intrin_ctx(intrin), "sign",      TYPE_FLOAT);
-    ast_value    *A_i        = ast_value_new(intrin_ctx(intrin), "A_i",       TYPE_FLOAT);
-    ast_value    *B_i        = ast_value_new(intrin_ctx(intrin), "B_i",       TYPE_FLOAT);
-    ast_value    *A_iminus1  = ast_value_new(intrin_ctx(intrin), "A_iminus1", TYPE_FLOAT);
-    ast_value    *B_iminus1  = ast_value_new(intrin_ctx(intrin), "B_iminus1", TYPE_FLOAT);
-    ast_value    *b_iplus1   = ast_value_new(intrin_ctx(intrin), "b_iplus1",  TYPE_FLOAT);
-    ast_value    *A_iplus1   = ast_value_new(intrin_ctx(intrin), "A_iplus1",  TYPE_FLOAT);
-    ast_value    *B_iplus1   = ast_value_new(intrin_ctx(intrin), "B_iplus1",  TYPE_FLOAT);
-    ast_value    *eps        = ast_value_new(intrin_ctx(intrin), "eps",       TYPE_FLOAT);
-    ast_value    *base2      = ast_value_new(intrin_ctx(intrin), "base2",     TYPE_FLOAT);
-    ast_value    *n2         = ast_value_new(intrin_ctx(intrin), "n2",        TYPE_FLOAT);
-    ast_value    *newbase2   = ast_value_new(intrin_ctx(intrin), "newbase2",  TYPE_FLOAT);
-    ast_block    *block      = ast_block_new(intrin_ctx(intrin));
-    ast_block    *plt1orblt1 = ast_block_new(intrin_ctx(intrin)); /* (power <= 1.0f || base <= 1.0f) */
-    ast_block    *plt1       = ast_block_new(intrin_ctx(intrin)); /* (power < 1.0f) */
-    ast_block    *blt1       = ast_block_new(intrin_ctx(intrin)); /* (base  < 1.0f) */
-    ast_block    *forloop    = ast_block_new(intrin_ctx(intrin)); /* for(;;) */
-    ast_block    *whileloop  = ast_block_new(intrin_ctx(intrin)); /* while (whole >= base) */
-    ast_block    *nestwhile  = ast_block_new(intrin_ctx(intrin)); /* while (whole >= newbase2) */
-    ast_function *func       = intrin_value(intrin, &value, "ln", TYPE_FLOAT);
-    size_t        i;
+    ast_value *val = nullptr;
+    ast_value *power = new ast_value(ctx(), "power", TYPE_FLOAT);
+    ast_value *base = new ast_value(ctx(), "base",TYPE_FLOAT);
+    ast_value *whole= new ast_value(ctx(), "whole", TYPE_FLOAT);
+    ast_value *nth = new ast_value(ctx(), "nth", TYPE_FLOAT);
+    ast_value *sign = new ast_value(ctx(), "sign", TYPE_FLOAT);
+    ast_value *A_i = new ast_value(ctx(), "A_i", TYPE_FLOAT);
+    ast_value *B_i = new ast_value(ctx(), "B_i", TYPE_FLOAT);
+    ast_value *A_iminus1 = new ast_value(ctx(), "A_iminus1", TYPE_FLOAT);
+    ast_value *B_iminus1 = new ast_value(ctx(), "B_iminus1", TYPE_FLOAT);
+    ast_value *b_iplus1 = new ast_value(ctx(), "b_iplus1", TYPE_FLOAT);
+    ast_value *A_iplus1 = new ast_value(ctx(), "A_iplus1", TYPE_FLOAT);
+    ast_value *B_iplus1 = new ast_value(ctx(), "B_iplus1", TYPE_FLOAT);
+    ast_value *eps = new ast_value(ctx(), "eps", TYPE_FLOAT);
+    ast_value *base2 = new ast_value(ctx(), "base2", TYPE_FLOAT);
+    ast_value *n2 = new ast_value(ctx(), "n2",TYPE_FLOAT);
+    ast_value *newbase2 = new ast_value(ctx(), "newbase2", TYPE_FLOAT);
+    ast_block *block = new ast_block(ctx());
+    ast_block *plt1orblt1 = new ast_block(ctx()); // (power <= 1.0f || base <= 1.0f)
+    ast_block *plt1 = new ast_block(ctx()); // (power < 1.0f)
+    ast_block *blt1 = new ast_block(ctx()); // (base< 1.0f)
+    ast_block *forloop = new ast_block(ctx()); // for(;;)
+    ast_block *whileloop = new ast_block(ctx()); // while (whole >= base)
+    ast_block *nestwhile= new ast_block(ctx()); // while (whole >= newbase2)
+    ast_function *func = value(&val, "ln", TYPE_FLOAT);
+    size_t i;
 
-    vec_push(value->expression.params, power);
-    vec_push(value->expression.params, base);
+    val->m_type_params.emplace_back(power);
+    val->m_type_params.emplace_back(base);
 
-    vec_push(block->locals, whole);
-    vec_push(block->locals, nth);
-    vec_push(block->locals, sign);
-    vec_push(block->locals, eps);
-    vec_push(block->locals, A_i);
-    vec_push(block->locals, B_i);
-    vec_push(block->locals, A_iminus1);
-    vec_push(block->locals, B_iminus1);
+    block->m_locals.push_back(whole);
+    block->m_locals.push_back(nth);
+    block->m_locals.push_back(sign);
+    block->m_locals.push_back(eps);
+    block->m_locals.push_back(A_i);
+    block->m_locals.push_back(B_i);
+    block->m_locals.push_back(A_iminus1);
+    block->m_locals.push_back(B_iminus1);
 
     /* sign = 1.0f; */
-    vec_push(block->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    block->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)sign,
-            (ast_expression*)intrin->fold->imm_float[1]
+            sign,
+            m_fold->m_imm_float[1]
         )
     );
 
     /* eps = __builtin_epsilon(); */
-    vec_push(block->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    block->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)eps,
-            (ast_expression*)ast_call_new(
-                intrin_ctx(intrin),
-                intrin_func_self(intrin, "__builtin_epsilon", "ln")
+            eps,
+            ast_call::make(
+                ctx(),
+                func_self("__builtin_epsilon", "ln")
             )
         )
     );
@@ -1462,13 +1450,13 @@ static ast_expression *intrin_ln(intrin_t *intrin) {
     for (i = 0; i <= 1; i++) {
         int j;
         for (j = 1; j >= 0; j--) {
-            vec_push(block->exprs,
-                (ast_expression*)ast_store_new(
-                    intrin_ctx(intrin),
+            block->m_exprs.push_back(
+                new ast_store(
+                    ctx(),
                     INSTR_STORE_F,
-                    (ast_expression*)((j) ? ((i) ? B_iminus1 : A_i)
+                    ((j) ? ((i) ? B_iminus1 : A_i)
                                           : ((i) ? A_iminus1 : B_i)),
-                    (ast_expression*)intrin->fold->imm_float[j]
+                    m_fold->m_imm_float[j]
                 )
             );
         }
@@ -1485,26 +1473,26 @@ static ast_expression *intrin_ln(intrin_t *intrin) {
      * }
      */
     for (i = 0; i <= 1; i++) {
-        vec_push(((i) ? blt1 : plt1)->exprs,
-            (ast_expression*)ast_store_new(
-                intrin_ctx(intrin),
+        ((i) ? blt1 : plt1)->m_exprs.push_back(
+            new ast_store(
+                ctx(),
                 INSTR_STORE_F,
-                (ast_expression*)((i) ? base : power),
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+                ((i) ? base : power),
+                new ast_binary(
+                    ctx(),
                     INSTR_DIV_F,
-                    (ast_expression*)intrin->fold->imm_float[1],
-                    (ast_expression*)((i) ? base : power)
+                    m_fold->m_imm_float[1],
+                    ((i) ? base : power)
                 )
             )
         );
-        vec_push(plt1->exprs,
-            (ast_expression*)ast_binstore_new(
-                intrin_ctx(intrin),
+        plt1->m_exprs.push_back(
+            new ast_binstore(
+                ctx(),
                 INSTR_STORE_F,
                 INSTR_MUL_F,
-                (ast_expression*)sign,
-                (ast_expression*)intrin->fold->imm_float[2]
+                sign,
+                m_fold->m_imm_float[2]
             )
         );
     }
@@ -1519,218 +1507,218 @@ static ast_expression *intrin_ln(intrin_t *intrin) {
      *         <blt1>
      * }
      */
-    vec_push(plt1orblt1->exprs,
-        (ast_expression*)ast_ifthen_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    plt1orblt1->m_exprs.push_back(
+        new ast_ifthen(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_OR,
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+                new ast_binary(
+                    ctx(),
                     INSTR_LE,
-                    (ast_expression*)power,
-                    (ast_expression*)intrin->fold->imm_float[0]
+                    power,
+                    m_fold->m_imm_float[0]
                 ),
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+                new ast_binary(
+                    ctx(),
                     INSTR_LE,
-                    (ast_expression*)base,
-                    (ast_expression*)intrin->fold->imm_float[0]
+                    base,
+                    m_fold->m_imm_float[0]
                 )
             ),
-            (ast_expression*)ast_return_new(
-                intrin_ctx(intrin),
-                (ast_expression*)ast_call_new(
-                    intrin_ctx(intrin),
-                    intrin_func_self(intrin, "__builtin_nan", "ln")
+            new ast_return(
+                ctx(),
+                ast_call::make(
+                    ctx(),
+                    func_self("__builtin_nan", "ln")
                 )
             ),
-            NULL
+            nullptr
         )
     );
 
     for (i = 0; i <= 1; i++) {
-        vec_push(plt1orblt1->exprs,
-            (ast_expression*)ast_ifthen_new(
-                intrin_ctx(intrin),
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+        plt1orblt1->m_exprs.push_back(
+            new ast_ifthen(
+                ctx(),
+                new ast_binary(
+                    ctx(),
                     INSTR_LT,
-                    (ast_expression*)((i) ? base : power),
-                    (ast_expression*)intrin->fold->imm_float[1]
+                    ((i) ? base : power),
+                    m_fold->m_imm_float[1]
                 ),
-                (ast_expression*)((i) ? blt1 : plt1),
-                NULL
+                ((i) ? blt1 : plt1),
+                nullptr
             )
         );
     }
 
-    vec_push(block->exprs, (ast_expression*)plt1orblt1);
+    block->m_exprs.push_back(plt1orblt1);
 
 
     /* whole = power; */
-    vec_push(forloop->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    forloop->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)whole,
-            (ast_expression*)power
+            whole,
+            power
         )
     );
 
     /* nth = 0.0f; */
-    vec_push(forloop->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    forloop->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)nth,
-            (ast_expression*)intrin->fold->imm_float[0]
+            nth,
+            m_fold->m_imm_float[0]
         )
     );
 
     /* base2 = base; */
-    vec_push(whileloop->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    whileloop->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)base2,
-            (ast_expression*)base
+            base2,
+            base
         )
     );
 
     /* n2 = 1.0f; */
-    vec_push(whileloop->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    whileloop->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)n2,
-            (ast_expression*)intrin->fold->imm_float[1]
+            n2,
+            m_fold->m_imm_float[1]
         )
     );
 
     /* newbase2 = base2 * base2; */
-    vec_push(whileloop->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    whileloop->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)newbase2,
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+            newbase2,
+            new ast_binary(
+                ctx(),
                 INSTR_MUL_F,
-                (ast_expression*)base2,
-                (ast_expression*)base2
+                base2,
+                base2
             )
         )
     );
 
     /* while loop locals */
-    vec_push(whileloop->locals, base2);
-    vec_push(whileloop->locals, n2);
-    vec_push(whileloop->locals, newbase2);
+    whileloop->m_locals.push_back(base2);
+    whileloop->m_locals.push_back(n2);
+    whileloop->m_locals.push_back(newbase2);
 
     /* base2 = newbase2; */
-    vec_push(nestwhile->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    nestwhile->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)base2,
-            (ast_expression*)newbase2
+            base2,
+            newbase2
         )
     );
 
     /* n2 *= 2; */
-    vec_push(nestwhile->exprs,
-        (ast_expression*)ast_binstore_new(
-            intrin_ctx(intrin),
+    nestwhile->m_exprs.push_back(
+        new ast_binstore(
+            ctx(),
             INSTR_STORE_F,
             INSTR_MUL_F,
-            (ast_expression*)n2,
-            (ast_expression*)intrin->fold->imm_float[3] /* 2.0f */
+            n2,
+            m_fold->m_imm_float[3] /* 2.0f */
         )
     );
 
     /* newbase2 *= newbase2; */
-    vec_push(nestwhile->exprs,
-        (ast_expression*)ast_binstore_new(
-            intrin_ctx(intrin),
+    nestwhile->m_exprs.push_back(
+        new ast_binstore(
+            ctx(),
             INSTR_STORE_F,
             INSTR_MUL_F,
-            (ast_expression*)newbase2,
-            (ast_expression*)newbase2
+            newbase2,
+            newbase2
         )
     );
 
     /* while (whole >= newbase2) */
-    vec_push(whileloop->exprs,
-        (ast_expression*)ast_loop_new(
-            intrin_ctx(intrin),
-            NULL,
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    whileloop->m_exprs.push_back(
+        new ast_loop(
+            ctx(),
+            nullptr,
+            new ast_binary(
+                ctx(),
                 INSTR_GE,
-                (ast_expression*)whole,
-                (ast_expression*)newbase2
+                whole,
+                newbase2
             ),
             false,
-            NULL,
+            nullptr,
             false,
-            NULL,
-            (ast_expression*)nestwhile
+            nullptr,
+            nestwhile
         )
     );
 
     /* whole /= base2; */
-    vec_push(whileloop->exprs,
-        (ast_expression*)ast_binstore_new(
-            intrin_ctx(intrin),
+    whileloop->m_exprs.push_back(
+        new ast_binstore(
+            ctx(),
             INSTR_STORE_F,
             INSTR_DIV_F,
-            (ast_expression*)whole,
-            (ast_expression*)base2
+            whole,
+            base2
         )
     );
 
     /* nth += n2; */
-    vec_push(whileloop->exprs,
-        (ast_expression*)ast_binstore_new(
-            intrin_ctx(intrin),
+    whileloop->m_exprs.push_back(
+        new ast_binstore(
+            ctx(),
             INSTR_STORE_F,
             INSTR_ADD_F,
-            (ast_expression*)nth,
-            (ast_expression*)n2
+            nth,
+            n2
         )
     );
 
     /* while (whole >= base) */
-    vec_push(forloop->exprs,
-        (ast_expression*)ast_loop_new(
-            intrin_ctx(intrin),
-            NULL,
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    forloop->m_exprs.push_back(
+        new ast_loop(
+            ctx(),
+            nullptr,
+            new ast_binary(
+                ctx(),
                 INSTR_GE,
-                (ast_expression*)whole,
-                (ast_expression*)base
+                whole,
+                base
             ),
             false,
-            NULL,
+            nullptr,
             false,
-            NULL,
-            (ast_expression*)whileloop
+            nullptr,
+            whileloop
         )
     );
 
-    vec_push(forloop->locals, b_iplus1);
-    vec_push(forloop->locals, A_iplus1);
-    vec_push(forloop->locals, B_iplus1);
+    forloop->m_locals.push_back(b_iplus1);
+    forloop->m_locals.push_back(A_iplus1);
+    forloop->m_locals.push_back(B_iplus1);
 
     /* b_iplus1 = nth; */
-    vec_push(forloop->exprs,
-        (ast_expression*)ast_store_new(
-            intrin_ctx(intrin),
+    forloop->m_exprs.push_back(
+        new ast_store(
+            ctx(),
             INSTR_STORE_F,
-            (ast_expression*)b_iplus1,
-            (ast_expression*)nth
+            b_iplus1,
+            nth
         )
     );
 
@@ -1739,21 +1727,21 @@ static ast_expression *intrin_ln(intrin_t *intrin) {
      * B_iplus1 = b_iplus1 * B_i + B_iminus1;
      */
     for (i = 0; i <= 1; i++) {
-        vec_push(forloop->exprs,
-            (ast_expression*)ast_store_new(
-                intrin_ctx(intrin),
+        forloop->m_exprs.push_back(
+            new ast_store(
+                ctx(),
                 INSTR_STORE_F,
-                (ast_expression*)((i) ? B_iplus1 : A_iplus1),
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+                ((i) ? B_iplus1 : A_iplus1),
+                new ast_binary(
+                    ctx(),
                     INSTR_ADD_F,
-                    (ast_expression*)ast_binary_new(
-                        intrin_ctx(intrin),
+                    new ast_binary(
+                        ctx(),
                         INSTR_MUL_F,
-                        (ast_expression*)b_iplus1,
-                        (ast_expression*) ((i) ? B_i : A_i)
+                        b_iplus1,
+                         ((i) ? B_i : A_i)
                     ),
-                    (ast_expression*)((i) ? B_iminus1 : A_iminus1)
+                    ((i) ? B_iminus1 : A_iminus1)
                 )
             )
         );
@@ -1764,12 +1752,12 @@ static ast_expression *intrin_ln(intrin_t *intrin) {
      * B_iminus1 = B_i;
      */
     for (i = 0; i <= 1; i++) {
-        vec_push(forloop->exprs,
-            (ast_expression*)ast_store_new(
-                intrin_ctx(intrin),
+        forloop->m_exprs.push_back(
+            new ast_store(
+                ctx(),
                 INSTR_STORE_F,
-                (ast_expression*)((i) ? B_iminus1 : A_iminus1),
-                (ast_expression*)((i) ? B_i       : A_i)
+                ((i) ? B_iminus1 : A_iminus1),
+                ((i) ? B_i       : A_i)
             )
         );
     }
@@ -1779,12 +1767,12 @@ static ast_expression *intrin_ln(intrin_t *intrin) {
      * B_i = B_iplus1;
      */
     for (i = 0; i <= 1; i++) {
-        vec_push(forloop->exprs,
-            (ast_expression*)ast_store_new(
-                intrin_ctx(intrin),
+        forloop->m_exprs.push_back(
+            new ast_store(
+                ctx(),
                 INSTR_STORE_F,
-                (ast_expression*)((i) ? B_i      : A_i),
-                (ast_expression*)((i) ? B_iplus1 : A_iplus1)
+                ((i) ? B_i      : A_i),
+                ((i) ? B_iplus1 : A_iplus1)
             )
         );
     }
@@ -1793,26 +1781,26 @@ static ast_expression *intrin_ln(intrin_t *intrin) {
      * if (whole <= 1.0f + eps)
      *     break;
      */
-    vec_push(forloop->exprs,
-        (ast_expression*)ast_ifthen_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    forloop->m_exprs.push_back(
+        new ast_ifthen(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_LE,
-                (ast_expression*)whole,
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+                whole,
+                new ast_binary(
+                    ctx(),
                     INSTR_ADD_F,
-                    (ast_expression*)intrin->fold->imm_float[1],
-                    (ast_expression*)eps
+                    m_fold->m_imm_float[1],
+                    eps
                 )
             ),
-            (ast_expression*)ast_breakcont_new(
-                intrin_ctx(intrin),
+            new ast_breakcont(
+                ctx(),
                 false,
                 0
             ),
-            NULL
+            nullptr
         )
     );
 
@@ -1821,262 +1809,240 @@ static ast_expression *intrin_ln(intrin_t *intrin) {
      * base  = whole;
      */
     for (i = 0; i <= 1; i++) {
-        vec_push(forloop->exprs,
-            (ast_expression*)ast_store_new(
-                intrin_ctx(intrin),
+        forloop->m_exprs.push_back(
+            new ast_store(
+                ctx(),
                 INSTR_STORE_F,
-                (ast_expression*)((i) ? base  : power),
-                (ast_expression*)((i) ? whole : base)
+                ((i) ? base  : power),
+                ((i) ? whole : base)
             )
         );
     }
 
     /* add the for loop block */
-    vec_push(block->exprs,
-        (ast_expression*)ast_loop_new(
-            intrin_ctx(intrin),
-            NULL,
-            /* for(; 1; ) ?? (can this be NULL too?) */
-            (ast_expression*)intrin->fold->imm_float[1],
+    block->m_exprs.push_back(
+        new ast_loop(
+            ctx(),
+            nullptr,
+            /* for(; 1; ) ?? (can this be nullptr too?) */
+            m_fold->m_imm_float[1],
             false,
-            NULL,
+            nullptr,
             false,
-            NULL,
-            (ast_expression*)forloop
+            nullptr,
+            forloop
         )
     );
 
     /* return sign * A_i / B_il */
-    vec_push(block->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)ast_binary_new(
-                intrin_ctx(intrin),
+    block->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            new ast_binary(
+                ctx(),
                 INSTR_MUL_F,
-                (ast_expression*)sign,
-                (ast_expression*)ast_binary_new(
-                    intrin_ctx(intrin),
+                sign,
+                new ast_binary(
+                    ctx(),
                     INSTR_DIV_F,
-                    (ast_expression*)A_i,
-                    (ast_expression*)B_i
+                    A_i,
+                    B_i
                 )
             )
         )
     );
 
-    vec_push(func->blocks, block);
-    intrin_reg(intrin, value, func);
-
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(block);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_log_variant(intrin_t *intrin, const char *name, float base) {
-    ast_value    *value  = NULL;
-    ast_call     *callln = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "__builtin_ln", name));
-    ast_value    *arg1   = ast_value_new(intrin_ctx(intrin), "x", TYPE_FLOAT);
-    ast_block    *body   = ast_block_new(intrin_ctx(intrin));
-    ast_function *func   = intrin_value(intrin, &value, name, TYPE_FLOAT);
+ast_expression *intrin::log_variant(const char *name, float base) {
+    ast_value *val = nullptr;
+    ast_call *callln = ast_call::make(ctx(), func_self("__builtin_ln", name));
+    ast_value *arg1 = new ast_value(ctx(), "x", TYPE_FLOAT);
+    ast_block *body = new ast_block(ctx());
+    ast_function *func = value(&val, name, TYPE_FLOAT);
 
-    vec_push(value->expression.params, arg1);
+    val->m_type_params.emplace_back(arg1);
 
-    vec_push(callln->params, (ast_expression*)arg1);
-    vec_push(callln->params, (ast_expression*)fold_constgen_float(intrin->fold, base, false));
+    callln->m_params.push_back(arg1);
+    callln->m_params.push_back(m_fold->constgen_float(base, false));
 
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)callln
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            callln
         )
     );
 
-    vec_push(func->blocks, body);
-    intrin_reg(intrin, value, func);
-    return (ast_expression*)value;
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_log(intrin_t *intrin) {
-    return intrin_log_variant(intrin, "log", 2.7182818284590452354);
+ast_expression *intrin::log_() {
+    return log_variant("log", 2.7182818284590452354);
 }
-static ast_expression *intrin_log10(intrin_t *intrin) {
-    return intrin_log_variant(intrin, "log10", 10);
+ast_expression *intrin::log10_() {
+    return log_variant("log10", 10);
 }
-static ast_expression *intrin_log2(intrin_t *intrin) {
-    return intrin_log_variant(intrin, "log2", 2);
+ast_expression *intrin::log2_() {
+    return log_variant("log2", 2);
 }
-static ast_expression *intrin_logb(intrin_t *intrin) {
+ast_expression *intrin::logb_() {
     /* FLT_RADIX == 2 for now */
-    return intrin_log_variant(intrin, "log2", 2);
+    return log_variant("log2", 2);
 }
 
-static ast_expression *intrin_shift_variant(intrin_t *intrin, const char *name, size_t instr) {
+ast_expression *intrin::shift_variant(const char *name, size_t instr) {
     /*
      * float [shift] (float a, float b) {
      *   return floor(a [instr] pow(2, b));
      */
-    ast_value    *value     = NULL;
-    ast_call     *callpow   = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "pow", name));
-    ast_call     *callfloor = ast_call_new (intrin_ctx(intrin), intrin_func_self(intrin, "floor", name));
-    ast_value    *a         = ast_value_new(intrin_ctx(intrin), "a", TYPE_FLOAT);
-    ast_value    *b         = ast_value_new(intrin_ctx(intrin), "b", TYPE_FLOAT);
-    ast_block    *body      = ast_block_new(intrin_ctx(intrin));
-    ast_function *func      = intrin_value(intrin, &value, name, TYPE_FLOAT);
+    ast_value *val = nullptr;
+    ast_call *callpow = ast_call::make(ctx(), func_self("pow", name));
+    ast_call *callfloor = ast_call::make(ctx(), func_self("floor", name));
+    ast_value *a = new ast_value(ctx(), "a", TYPE_FLOAT);
+    ast_value *b = new ast_value(ctx(), "b", TYPE_FLOAT);
+    ast_block *body = new ast_block(ctx());
+    ast_function *func = value(&val, name, TYPE_FLOAT);
 
-    vec_push(value->expression.params, a);
-    vec_push(value->expression.params, b);
+    val->m_type_params.emplace_back(a);
+    val->m_type_params.emplace_back(b);
 
     /* <callpow> = pow(2, b) */
-    vec_push(callpow->params, (ast_expression*)intrin->fold->imm_float[3]);
-    vec_push(callpow->params, (ast_expression*)b);
+    callpow->m_params.push_back(m_fold->m_imm_float[3]);
+    callpow->m_params.push_back(b);
 
     /* <callfloor> = floor(a [instr] <callpow>) */
-    vec_push(
-        callfloor->params,
-        (ast_expression*)ast_binary_new(
-            intrin_ctx(intrin),
+    callfloor->m_params.push_back(
+        new ast_binary(
+            ctx(),
             instr,
-            (ast_expression*)a,
-            (ast_expression*)callpow
+            a,
+            callpow
         )
     );
 
     /* return <callfloor> */
-    vec_push(body->exprs,
-        (ast_expression*)ast_return_new(
-            intrin_ctx(intrin),
-            (ast_expression*)callfloor
+    body->m_exprs.push_back(
+        new ast_return(
+            ctx(),
+            callfloor
         )
     );
 
-    vec_push(func->blocks, body);
-    intrin_reg(intrin, value, func);
-    return (ast_expression*)value;
-}
-
-static ast_expression *intrin_lshift(intrin_t *intrin) {
-    return intrin_shift_variant(intrin, "lshift", INSTR_MUL_F);
+    func->m_blocks.emplace_back(body);
+    reg(val, func);
+    return val;
 }
 
-static ast_expression *intrin_rshift(intrin_t *intrin) {
-    return intrin_shift_variant(intrin, "rshift", INSTR_DIV_F);
+ast_expression *intrin::lshift() {
+    return shift_variant("lshift", INSTR_MUL_F);
 }
 
-/*
- * TODO: make static (and handle ast_type_string) here for the builtin
- * instead of in SYA parse close.
- */
-ast_expression *intrin_debug_typestring(intrin_t *intrin) {
-    (void)intrin;
-    return (ast_expression*)0x1;
+ast_expression *intrin::rshift() {
+    return shift_variant("rshift", INSTR_DIV_F);
 }
 
-static const intrin_func_t intrinsics[] = {
-    {&intrin_isfinite,         "__builtin_isfinite",         "isfinite", 1},
-    {&intrin_isinf,            "__builtin_isinf",            "isinf",    1},
-    {&intrin_isnan,            "__builtin_isnan",            "isnan",    1},
-    {&intrin_isnormal,         "__builtin_isnormal",         "isnormal", 1},
-    {&intrin_signbit,          "__builtin_signbit",          "signbit",  1},
-    {&intrin_acosh,            "__builtin_acosh",            "acosh",    1},
-    {&intrin_asinh,            "__builtin_asinh",            "asinh",    1},
-    {&intrin_atanh,            "__builtin_atanh",            "atanh",    1},
-    {&intrin_exp,              "__builtin_exp",              "exp",      1},
-    {&intrin_exp2,             "__builtin_exp2",             "exp2",     1},
-    {&intrin_expm1,            "__builtin_expm1",            "expm1",    1},
-    {&intrin_mod,              "__builtin_mod",              "mod",      2},
-    {&intrin_pow,              "__builtin_pow",              "pow",      2},
-    {&intrin_fabs,             "__builtin_fabs",             "fabs",     1},
-    {&intrin_log,              "__builtin_log",              "log",      1},
-    {&intrin_log10,            "__builtin_log10",            "log10",    1},
-    {&intrin_log2,             "__builtin_log2",             "log2",     1},
-    {&intrin_logb,             "__builtin_logb",             "logb",     1},
-    {&intrin_lshift,           "__builtin_lshift",           "",         2},
-    {&intrin_rshift,           "__builtin_rshift",           "",         2},
-    {&intrin_epsilon,          "__builtin_epsilon",          "",         0},
-    {&intrin_nan,              "__builtin_nan",              "",         0},
-    {&intrin_inf,              "__builtin_inf",              "",         0},
-    {&intrin_ln,               "__builtin_ln",               "",         2},
-    {&intrin_debug_typestring, "__builtin_debug_typestring", "",         0},
-    {&intrin_nullfunc,         "#nullfunc",                  "",         0}
-};
-
-static void intrin_error(intrin_t *intrin, const char *fmt, ...) {
+void intrin::error(const char *fmt, ...) {
     va_list ap;
     va_start(ap, fmt);
-    vcompile_error(intrin->parser->lex->tok.ctx, fmt, ap);
+    vcompile_error(ctx(), fmt, ap);
     va_end(ap);
 }
 
 /* exposed */
-intrin_t *intrin_init(parser_t *parser) {
-    intrin_t *intrin = (intrin_t*)mem_a(sizeof(intrin_t));
-    size_t    i;
-
-    intrin->parser     = parser;
-    intrin->fold       = parser->fold;
-    intrin->intrinsics = NULL;
-    intrin->generated  = NULL;
-
-    vec_append(intrin->intrinsics, GMQCC_ARRAY_COUNT(intrinsics), intrinsics);
-
-    /* populate with null pointers for tracking generation */
-    for (i = 0; i < GMQCC_ARRAY_COUNT(intrinsics); i++)
-        vec_push(intrin->generated, NULL);
-
-    return intrin;
+ast_expression *intrin::debug_typestring() {
+    return (ast_expression*)0x1;
 }
 
-void intrin_cleanup(intrin_t *intrin) {
-    vec_free(intrin->intrinsics);
-    vec_free(intrin->generated);
-    mem_d(intrin);
+intrin::intrin(parser_t *parser)
+    : m_parser(parser)
+    , m_fold(&parser->m_fold)
+{
+    static const intrin_func_t intrinsics[] = {
+        {&intrin::isfinite_,        "__builtin_isfinite",         "isfinite", 1},
+        {&intrin::isinf_,           "__builtin_isinf",            "isinf",    1},
+        {&intrin::isnan_,           "__builtin_isnan",            "isnan",    1},
+        {&intrin::isnormal_,        "__builtin_isnormal",         "isnormal", 1},
+        {&intrin::signbit_,         "__builtin_signbit",          "signbit",  1},
+        {&intrin::acosh_,           "__builtin_acosh",            "acosh",    1},
+        {&intrin::asinh_,           "__builtin_asinh",            "asinh",    1},
+        {&intrin::atanh_,           "__builtin_atanh",            "atanh",    1},
+        {&intrin::exp_,             "__builtin_exp",              "exp",      1},
+        {&intrin::exp2_,            "__builtin_exp2",             "exp2",     1},
+        {&intrin::expm1_,           "__builtin_expm1",            "expm1",    1},
+        {&intrin::mod_,             "__builtin_mod",              "mod",      2},
+        {&intrin::pow_,             "__builtin_pow",              "pow",      2},
+        {&intrin::fabs_,            "__builtin_fabs",             "fabs",     1},
+        {&intrin::log_,             "__builtin_log",              "log",      1},
+        {&intrin::log10_,           "__builtin_log10",            "log10",    1},
+        {&intrin::log2_,            "__builtin_log2",             "log2",     1},
+        {&intrin::logb_,            "__builtin_logb",             "logb",     1},
+        {&intrin::lshift,           "__builtin_lshift",           "",         2},
+        {&intrin::rshift,           "__builtin_rshift",           "",         2},
+        {&intrin::epsilon_,         "__builtin_epsilon",          "",         0},
+        {&intrin::nan_,             "__builtin_nan",              "",         0},
+        {&intrin::inf_,             "__builtin_inf",              "",         0},
+        {&intrin::ln_,              "__builtin_ln",               "",         2},
+        {&intrin::debug_typestring, "__builtin_debug_typestring", "",         0},
+        {&intrin::nullfunc,         "#nullfunc",                  "",         0}
+    };
+
+    for (auto &it : intrinsics) {
+        m_intrinsics.push_back(it);
+        m_generated.push_back(nullptr);
+    }
 }
 
-ast_expression *intrin_fold(intrin_t *intrin, ast_value *value, ast_expression **exprs) {
-    size_t i;
-    if (!value || !value->name)
-        return NULL;
-    for (i = 0; i < vec_size(intrin->intrinsics); i++)
-        if (!strcmp(value->name, intrin->intrinsics[i].name))
-            return (vec_size(exprs) != intrin->intrinsics[i].args)
-                        ? NULL
-                        : fold_intrin(intrin->fold, value->name + 10, exprs);
-    return NULL;
+ast_expression *intrin::do_fold(ast_value *val, ast_expression **exprs) {
+    if (!val || !val->m_name.length())
+        return nullptr;
+    static constexpr size_t kPrefixLength = 10; // "__builtin_"
+    for (auto &it : m_intrinsics) {
+        if (val->m_name == it.name)
+            return (vec_size(exprs) != it.args)
+                        ? nullptr
+                        : m_fold->intrinsic(val->m_name.c_str() + kPrefixLength, it.args, exprs);
+    }
+    return nullptr;
 }
 
-static GMQCC_INLINE ast_expression *intrin_func_try(intrin_t *intrin, size_t offset, const char *compare) {
-    size_t i;
-    for (i = 0; i < vec_size(intrin->intrinsics); i++) {
-        if (strcmp(*(char **)((char *)&intrin->intrinsics[i] + offset), compare))
+ast_expression *intrin::func_try(size_t offset, const char *compare) {
+    for (auto &it : m_intrinsics) {
+        const size_t index = &it - &m_intrinsics[0];
+        if (strcmp(*(char **)((char *)&it + offset), compare))
             continue;
-        if (intrin->generated[i])
-            return intrin->generated[i];
-        return intrin->generated[i] = intrin->intrinsics[i].intrin(intrin);
+        if (m_generated[index])
+            return m_generated[index];
+        return m_generated[index] = (this->*it.intrin_func_t::function)();
     }
-    return NULL;
+    return nullptr;
 }
 
-static ast_expression *intrin_func_self(intrin_t *intrin, const char *name, const char *from) {
-    size_t           i;
-    ast_expression  *find;
-
+ast_expression *intrin::func_self(const char *name, const char *from) {
+    ast_expression *find;
     /* try current first */
-    if ((find = parser_find_global(intrin->parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
-        for (i = 0; i < vec_size(intrin->parser->functions); ++i)
-            if (((ast_value*)find)->name && !strcmp(intrin->parser->functions[i]->name, ((ast_value*)find)->name) && intrin->parser->functions[i]->builtin < 0)
+    if ((find = parser_find_global(m_parser, name)) && ((ast_value*)find)->m_vtype == TYPE_FUNCTION)
+        for (auto &it : m_parser->functions)
+            if (reinterpret_cast<ast_value*>(find)->m_name.length() && it->m_name == reinterpret_cast<ast_value*>(find)->m_name && it->m_builtin < 0)
                 return find;
     /* try name second */
-    if ((find = intrin_func_try(intrin, offsetof(intrin_func_t, name),  name)))
+    if ((find = func_try(offsetof(intrin_func_t, name),  name)))
         return find;
     /* try alias third */
-    if ((find = intrin_func_try(intrin, offsetof(intrin_func_t, alias), name)))
+    if ((find = func_try(offsetof(intrin_func_t, alias), name)))
         return find;
 
     if (from) {
-        intrin_error(intrin, "need function `%s', compiler depends on it for `__builtin_%s'", name, from);
-        return intrin_func_self(intrin, "#nullfunc", NULL);
+        error("need function `%s', compiler depends on it for `__builtin_%s'", name, from);
+        return func_self("#nullfunc", nullptr);
     }
-    return NULL;
+    return nullptr;
 }
 
-ast_expression *intrin_func(intrin_t *intrin, const char *name) {
-    return intrin_func_self(intrin, name, NULL);
+ast_expression *intrin::func(const char *name) {
+    return func_self(name, nullptr);
 }