]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - intrin.c
Constant fold intrinsics if their arguments are constant. TODO: reference count intri...
[xonotic/gmqcc.git] / intrin.c
index dbf9670f27098e2018c33fef35d1f52853a47cd7..0075ece0ee16a6465587a6c78b8ca8b6cfb8d712 100644 (file)
--- a/intrin.c
+++ b/intrin.c
@@ -20,8 +20,8 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#include "ast.h"
-
+#include <string.h>
+#include "parser.h"
 
 /*
  * Provides all the "intrinsics" / "builtins" for GMQCC. These can do
  * is entered when -fintrin is used (causing all existing builtins to
  * be ignored by the compiler and instead interface through here.
  */
-typedef struct {
-    ast_expression (*intrin)(parser_t *);
-    const char      *name;
-    const char      *alias;
-} intrin_t;
+#define INTRIN_VAL(VALUE, NAME, FUNC, STYPE, VTYPE)                    \
+    do {                                                               \
+        (VALUE) = ast_value_new (                                      \
+            parser_ctx(intrin->parser),                                \
+            "__builtin_" NAME,                                         \
+            TYPE_FUNCTION                                              \
+        );                                                             \
+        (VALUE)->intrinsic = true;                                     \
+        (VALUE)->expression.next = (ast_expression*)ast_value_new (    \
+            parser_ctx(intrin->parser),                                \
+            STYPE,                                                     \
+            VTYPE                                                      \
+        );                                                             \
+        (FUNC) = ast_function_new (                                    \
+            parser_ctx(intrin->parser),                                \
+            "__builtin_" NAME,                                         \
+            (VALUE)                                                    \
+        );                                                             \
+    } while (0)
 
+#define INTRIN_REG(FUNC, VALUE)                                        \
+    do {                                                               \
+        vec_push(intrin->parser->functions, (FUNC));                   \
+        vec_push(intrin->parser->globals,   (ast_expression*)(VALUE)); \
+    } while (0)
 
-/*
- * Some helper macros for generating def, doing ast_value from func, with
- * assignment, and a nice aggregate builder for generating the intrinsic
- * table at the bottom of this file, and for registering functions and
- * globals with the parser (so that they can be codegen'ed) 
- */   
-#define INTRIN_IMP(NAME) \
-    ast_expression intrin_##NAME (parser_t *parser)
+#define QC_M_E 2.71828182845905f
 
-/*
- * For intrinsics that are not to take precedence over a builtin, leave
- * ALIAS as an empty string.
- */  
-#define INTRIN_DEF(NAME, ALIAS) \
-    { &intrin_##NAME, #NAME, ALIAS }
-
-
-#define INTRIN_VAL(NAME, FUNC, STYPE, VTYPE)                          \
-    do {                                                              \
-        (NAME) = ast_value_new (                                      \
-            parser_ctx(parser),                                       \
-            "__builtin_" #NAME,                                       \
-            TYPE_FUNCTION                                             \
-        );                                                            \
-        (NAME)->expression.next = (ast_expression*)ast_value_new (    \
-            parser_ctx(parser),                                       \
-            STYPE,                                                    \
-            VTYPE                                                     \
-        );                                                            \
-        (FUNC) = ast_function_new (                                   \
-            parser_ctx(parser),                                       \
-            "__builtin_" #NAME,                                       \
-            (NAME)                                                    \
-        );                                                            \
-    } while (0)
+static ast_expression *intrin_pow (intrin_t *intrin) {
+    /*
+     * float pow(float x, float y) {
+     *   float local = 1.0f;
+     *   while (y > 0) {
+     *     while (!(y & 1)) {
+     *       y >>= 2;
+     *       x *=  x;
+     *     }
+     *     y--;
+     *     local *= x;
+     *   }
+     *   return local;
+     * }
+     */
+    static ast_value *value = NULL;
 
-#define INTRIN_REG(FUNC, VALUE)                                       \
-    do {                                                              \
-        vec_push(parser->functions, (FUNC));                          \
-        vec_push(parser->globals,   (ast_expression*)(VALUE));        \
-    } while (0)
+    if (!value) {
+        ast_value    *arg1  = ast_value_new(parser_ctx(intrin->parser), "x",     TYPE_FLOAT);
+        ast_value    *arg2  = ast_value_new(parser_ctx(intrin->parser), "y",     TYPE_FLOAT);
+        ast_value    *local = ast_value_new(parser_ctx(intrin->parser), "local", TYPE_FLOAT);
+        ast_block    *body  = ast_block_new(parser_ctx(intrin->parser));
+        ast_block    *l1b   = ast_block_new(parser_ctx(intrin->parser)); /* loop 1 body */
+        ast_block    *l2b   = ast_block_new(parser_ctx(intrin->parser)); /* loop 2 body */
+        ast_loop     *loop1 = NULL;
+        ast_loop     *loop2 = NULL;
+        ast_function *func  = NULL;
 
+        INTRIN_VAL(value, "pow", func, "<float>", TYPE_FLOAT);
 
-typedef enum {
-    QC_FP_NAN,
-    QC_FP_INFINITE,
-    QC_FP_ZERO,
-    QC_FP_SUBNORMAL,
-    QC_FP_NORMAL
-} intrin_fp_t;
+        /* arguments */
+        vec_push(value->expression.params, arg1);
+        vec_push(value->expression.params, arg2);
 
+        /* local */
+        vec_push(body->locals, local);
 
-#if 0
-/*
- * Implementation of intrinsics.  Each new intrinsic needs to be added
- * to the intrinsic table below.
- */  
-INTRIN_IMP(isnan) {
-    /*
-     * float isnan(float x) {
-     *     float y;
-     *
-     *     y = x;
-     *     return (x != y);
-     */      
-}
+        /* assignment to local of value 1.0f */
+        vec_push(body->exprs,
+            (ast_expression*)ast_store_new (
+                parser_ctx(intrin->parser),
+                INSTR_STORE_F,
+                (ast_expression*)local,
+                (ast_expression*)intrin->fold->imm_float[1] /* 1 == 1.0f */
+            )
+        );
 
-INTRIN_IMP(isinf) {
-    /*
-     * float isinf(float x) {
-     *   return (x != 0) && (x + x == x);
-     * }
-     */    
+        /* y >>= 2 */
+        vec_push(l2b->exprs,
+            (ast_expression*)ast_binstore_new (
+                parser_ctx(intrin->parser),
+                INSTR_STORE_F,
+                INSTR_MUL_F,
+                (ast_expression*)arg2,
+                (ast_expression*)fold_constgen_float(intrin->parser->fold, 0.25f)
+            )
+        );
+
+        /* x *= x */
+        vec_push(l2b->exprs,
+            (ast_expression*)ast_binstore_new (
+                parser_ctx(intrin->parser),
+                INSTR_STORE_F,
+                INSTR_MUL_F,
+                (ast_expression*)arg1,
+                (ast_expression*)arg1
+            )
+        );
+
+        /* while (!(y&1)) */
+        loop2 = ast_loop_new (
+            parser_ctx(intrin->parser),
+            NULL,
+            (ast_expression*)ast_binary_new (
+                parser_ctx(intrin->parser),
+                INSTR_AND,
+                (ast_expression*)arg2,
+                (ast_expression*)intrin->fold->imm_float[1] /* 1 == 1.0f */
+            ),
+            true, /* ! not */
+            NULL,
+            false,
+            NULL,
+            (ast_expression*)l2b
+        );
+
+        /* push nested loop into loop expressions */
+        vec_push(l1b->exprs, (ast_expression*)loop2);
+
+        /* y-- */
+        vec_push(l1b->exprs,
+            (ast_expression*)ast_binstore_new (
+                parser_ctx(intrin->parser),
+                INSTR_STORE_F,
+                INSTR_SUB_F,
+                (ast_expression*)arg2,
+                (ast_expression*)intrin->fold->imm_float[1] /* 1 == 1.0f */
+            )
+        );
+        /* local *= x */
+        vec_push(l1b->exprs,
+            (ast_expression*)ast_binstore_new (
+                parser_ctx(intrin->parser),
+                INSTR_STORE_F,
+                INSTR_MUL_F,
+                (ast_expression*)local,
+                (ast_expression*)arg1
+            )
+        );
+
+        /* while (y > 0) */
+        loop1 = ast_loop_new (
+            parser_ctx(intrin->parser),
+            NULL,
+            (ast_expression*)ast_binary_new (
+                parser_ctx(intrin->parser),
+                INSTR_GT,
+                (ast_expression*)arg2,
+                (ast_expression*)intrin->fold->imm_float[0] /* 0 == 0.0f */
+            ),
+            false,
+            NULL,
+            false,
+            NULL,
+            (ast_expression*)l1b
+        );
+
+        /* push the loop1 into the body for the function */
+        vec_push(body->exprs, (ast_expression*)loop1);
+
+        /* return local; */
+        vec_push(body->exprs,
+            (ast_expression*)ast_return_new (
+                parser_ctx(intrin->parser),
+                (ast_expression*)local
+            )
+        );
+
+        /* push block and register intrin for codegen */
+        vec_push(func->blocks, body);
+
+        INTRIN_REG(func, value);
+    }
+
+    return (ast_expression*)value;
 }
 
-INTRIN_IMP(fpclassify) {
+static ast_expression *intrin_mod(intrin_t *intrin) {
     /*
-     * float __builtin_fpclassify(float x) {
-     *     if (isnan(x)) {
-     *         return QC_FP_NAN;
-     *     }
-     *     if (isinf(x)) {
-     *         return QC_FP_INFINITE;
-     *     }
-     *     if (x == 0.0f) {
-     *         return QC_FP_ZERO;
-     *     }
-     *     return QC_FP_NORMAL;
+     * float mod(float x, float y) {
+     *   return x - y * floor(x / y);
      * }
-     */             
-}
-#endif
+     */
+    static ast_value *value = NULL;
+
+    if (!value) {
+        ast_call     *call  = ast_call_new (parser_ctx(intrin->parser), intrin_func(intrin, "floor"));
+        ast_value    *arg1  = ast_value_new(parser_ctx(intrin->parser), "x", TYPE_FLOAT);
+        ast_value    *arg2  = ast_value_new(parser_ctx(intrin->parser), "y", TYPE_FLOAT);
+        ast_block    *body  = ast_block_new(parser_ctx(intrin->parser));
+        ast_function *func  = NULL;
+
+        INTRIN_VAL(value, "mod", func, "<float>", TYPE_FLOAT);
+
+        /* floor(x/y) */
+        vec_push(call->params,
+            (ast_expression*)ast_binary_new (
+                parser_ctx(intrin->parser),
+                INSTR_DIV_F,
+                (ast_expression*)arg1,
+                (ast_expression*)arg2
+            )
+        );
+
+        vec_push(body->exprs,
+            (ast_expression*)ast_return_new(
+                parser_ctx(intrin->parser),
+                (ast_expression*)ast_binary_new(
+                    parser_ctx(intrin->parser),
+                    INSTR_SUB_F,
+                    (ast_expression*)arg1,
+                    (ast_expression*)ast_binary_new(
+                        parser_ctx(intrin->parser),
+                        INSTR_MUL_F,
+                        (ast_expression*)arg2,
+                        (ast_expression*)call
+                    )
+                )
+            )
+        );
+
+        vec_push(value->expression.params, arg1); /* float x (for param) */
+        vec_push(value->expression.params, arg2); /* float y (for param) */
 
+        vec_push(func->blocks,             body); /* {{{ body }}} */
 
-INTRIN_IMP(exp) {
+        INTRIN_REG(func, value);
+    }
+
+    return (ast_expression*)value;
+}
+
+static ast_expression *intrin_exp(intrin_t *intrin) {
     /*
-     * float __builtin_exp(float x) {
-     *     return __builtin_pow(QC_M_E, x);
+     * float exp(float x) {
+     *     return pow(QC_M_E, x);
      * }
      */
     static ast_value *value = NULL;
 
     if (!value) {
-        ast_call     *call = ast_call_new    (parser_ctx(parser), intrin_func(parser, "pow"));
-        ast_value    *arg1 = ast_value_new   (parser_ctx(parser), "x", TYPE_FLOAT);
-        ast_block    *body = ast_block_new   (parser_ctx(parser));
+        ast_call     *call = ast_call_new (parser_ctx(intrin->parser), intrin_func(intrin, "pow"));
+        ast_value    *arg1 = ast_value_new(parser_ctx(intrin->parser), "x", TYPE_FLOAT);
+        ast_block    *body = ast_block_new(parser_ctx(intrin->parser));
         ast_function *func = NULL;
 
-        INTRIN_VAL("exp", func, "<float>", TYPE_FLOAT);
+        INTRIN_VAL(value, "exp", func, "<float>", TYPE_FLOAT);
 
         /* push arguments for params to call */
-        vec_push(call->params, (ast_expression*)parser_const_float(parser, QC_M_E));
+        vec_push(call->params, (ast_expression*)fold_constgen_float(intrin->fold, QC_M_E));
         vec_push(call->params, (ast_expression*)arg1);
 
         /* return pow(QC_M_E, x) */
         vec_push(body->exprs,
             (ast_expression*)ast_return_new(
-                parser_ctx(parser),
+                parser_ctx(intrin->parser),
                 (ast_expression*)call
             )
         );
 
-        vec_push(value->expressions.param, arg1); /* float x (for param) */
-        vec_push(func ->blocks,            body); /* {{{ body }}} */
+        vec_push(value->expression.params, arg1); /* float x (for param) */
+
+        vec_push(func->blocks,             body); /* {{{ body }}} */
 
         INTRIN_REG(func, value);
     }
@@ -170,24 +300,177 @@ INTRIN_IMP(exp) {
     return (ast_expression*)value;
 }
 
-#if 0
-INTRIN_IMP(exp2) {
+static ast_expression *intrin_isnan(intrin_t *intrin) {
     /*
-     * float __builtin_exp2(float x) {
-     *     return __builin_pow(2, x);
+     * float isnan(float x) {
+     *   float local;
+     *   local = x;
+     *
+     *   return (x != local);
      * }
-     */ 
+     */
+    static ast_value *value = NULL;
+
+    if (!value) {
+        ast_value    *arg1   = ast_value_new(parser_ctx(intrin->parser), "x",     TYPE_FLOAT);
+        ast_value    *local  = ast_value_new(parser_ctx(intrin->parser), "local", TYPE_FLOAT);
+        ast_block    *body   = ast_block_new(parser_ctx(intrin->parser));
+        ast_function *func   = NULL;
+
+        INTRIN_VAL(value, "isnan", func, "<float>", TYPE_FLOAT);
+
+        vec_push(body->locals, local);
+        vec_push(body->exprs,
+            (ast_expression*)ast_store_new(
+                parser_ctx(intrin->parser),
+                INSTR_STORE_F,
+                (ast_expression*)local,
+                (ast_expression*)arg1
+            )
+        );
+
+        vec_push(body->exprs,
+            (ast_expression*)ast_return_new(
+                parser_ctx(intrin->parser),
+                (ast_expression*)ast_binary_new(
+                    parser_ctx(intrin->parser),
+                    INSTR_NE_F,
+                    (ast_expression*)arg1,
+                    (ast_expression*)local
+                )
+            )
+        );
+
+        vec_push(value->expression.params, arg1);
+        vec_push(func->blocks, body);
+
+        INTRIN_REG(func, value);
+    }
+
+    return (ast_expression*)value;
 }
 
-INTRIN_IMP(expm1) {
+static ast_expression *intrin_fabs(intrin_t *intrin) {
     /*
-     * float __builtin_expm1(float x) {
-     *     return __builtin_exp(x) - 1;
+     * float fabs(float x) {
+     *     return x < 0 ? -x : x;
      * }
-     */    
+     */
+    static ast_value *value = NULL;
+    if (!value) {
+        ast_value    *arg1   = ast_value_new(parser_ctx(intrin->parser), "x",     TYPE_FLOAT);
+        ast_block    *body   = ast_block_new(parser_ctx(intrin->parser));
+        ast_function *func   = NULL;
+
+        INTRIN_VAL(value, "fabs", func, "<float>", TYPE_FLOAT);
+
+        vec_push(body->exprs,
+            (ast_expression*)ast_return_new(
+                parser_ctx(intrin->parser),
+                (ast_expression*)ast_ternary_new(
+                    parser_ctx(intrin->parser),
+                    (ast_expression*)ast_binary_new(
+                        parser_ctx(intrin->parser),
+                        INSTR_LE,
+                        (ast_expression*)arg1,
+                        (ast_expression*)intrin->fold->imm_float[0]
+                    ),
+                    (ast_expression*)ast_binary_new(
+                        parser_ctx(intrin->parser),
+                        INSTR_SUB_F,
+                        (ast_expression*)intrin->fold->imm_float[0],
+                        (ast_expression*)arg1
+                    ),
+                    (ast_expression*)arg1
+                )
+            )
+        );
+
+        vec_push(value->expression.params, arg1);
+        vec_push(func->blocks, body);
+
+        INTRIN_REG(func, value);
+    }
+
+    return (ast_expression*)value;
+}
+
+#undef INTRIN_REG
+#undef INTRIN_VAL
+
+/*
+ * 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;
 }
-#endif
 
-intrin_t intrin_intrinsics[] = {
-    INTRIN_DEF(exp, "exp")
+static const intrin_func_t intrinsics[] = {
+    {&intrin_exp,              "__builtin_exp",              "exp"},
+    {&intrin_mod,              "__builtin_mod",              "mod"},
+    {&intrin_pow,              "__builtin_pow",              "pow"},
+    {&intrin_isnan,            "__builtin_isnan",            "isnan"},
+    {&intrin_fabs,             "__builtin_fabs",             "fabs"},
+    {&intrin_debug_typestring, "__builtin_debug_typestring", ""}
 };
+
+static void intrin_error(intrin_t *intrin, const char *fmt, ...) {
+    va_list ap;
+    va_start(ap, fmt);
+    vcompile_error(intrin->parser->lex->tok.ctx, fmt, ap);
+    va_end(ap);
+}
+
+/* exposed */
+intrin_t *intrin_init(parser_t *parser) {
+    intrin_t *intrin = (intrin_t*)mem_a(sizeof(intrin_t));
+    intrin->parser     = parser;
+    intrin->fold       = parser->fold;
+    intrin->intrinsics = NULL;
+
+    vec_append(intrin->intrinsics, sizeof(intrinsics)/sizeof(*intrinsics), intrinsics);
+
+    return intrin;
+}
+
+void intrin_cleanup(intrin_t *intrin) {
+    vec_free(intrin->intrinsics);
+    mem_d(intrin);
+}
+
+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 fold_intrin(intrin->fold, value->name, exprs);
+
+    return NULL;
+}
+
+ast_expression *intrin_func(intrin_t *intrin, const char *name) {
+    size_t       i    = 0;
+    void        *find;
+
+    /* try current first */
+    if ((find = (void*)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)
+                return (ast_expression*)find;
+    /* try name second */
+    for (i = 0; i < vec_size(intrin->intrinsics); i++)
+        if (!strcmp(intrin->intrinsics[i].name, name))
+            return intrin->intrinsics[i].intrin(intrin);
+    /* try alias third */
+    for (i = 0; i < vec_size(intrin->intrinsics); i++)
+        if (!strcmp(intrin->intrinsics[i].alias, name))
+            return intrin->intrinsics[i].intrin(intrin);
+
+    intrin_error(intrin, "need function: `%s` compiler depends on it", name);
+    return NULL;
+}