2 * Copyright (C) 2012, 2013
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Provides all the "intrinsics" / "builtins" for GMQCC. These can do
26 * a few things, they can provide fall back implementations for math
27 * functions if the definitions don't exist for some given engine. Or
28 * then can determine definitions for existing builtins, and simply
29 * wrap back to them instead. This is like a "portable" intrface that
30 * is entered when -fintrin is used (causing all existing builtins to
31 * be ignored by the compiler and instead interface through here.
34 ast_expression *(*intrin)(parser_t *);
39 static ht intrin_intrinsics(void) {
40 static ht intrinsics = NULL;
42 intrinsics = util_htnew(PARSER_HT_SIZE);
47 #define INTRIN_VAL(VALUE, NAME, FUNC, STYPE, VTYPE) \
49 (VALUE) = ast_value_new ( \
54 (VALUE)->expression.next = (ast_expression*)ast_value_new ( \
59 (FUNC) = ast_function_new ( \
66 #define INTRIN_REG(FUNC, VALUE) \
68 vec_push(parser->functions, (FUNC)); \
69 vec_push(parser->globals, (ast_expression*)(VALUE)); \
72 #define QC_M_E 2.71828182845905
74 static ast_expression *intrin_func(parser_t *parser, const char *name);
75 static ast_expression *intrin_pow (parser_t *parser) {
77 * float pow(float x, float y) {
90 static ast_value *value = NULL;
93 ast_value *arg1 = ast_value_new(parser_ctx(parser), "x", TYPE_FLOAT);
94 ast_value *arg2 = ast_value_new(parser_ctx(parser), "y", TYPE_FLOAT);
95 ast_value *local = ast_value_new(parser_ctx(parser), "local", TYPE_FLOAT);
96 ast_block *body = ast_block_new(parser_ctx(parser));
97 ast_block *l1b = ast_block_new(parser_ctx(parser)); /* loop 1 body */
98 ast_block *l2b = ast_block_new(parser_ctx(parser)); /* looo 2 body */
99 ast_loop *loop1 = NULL;
100 ast_loop *loop2 = NULL;
101 ast_function *func = NULL;
103 INTRIN_VAL(value, "pow", func, "<float>", TYPE_FLOAT);
106 vec_push(value->expression.params, arg1);
107 vec_push(value->expression.params, arg2);
110 vec_push(body->locals, local);
112 /* assignment to local of value 1.0f */
113 vec_push(body->exprs,
114 (ast_expression*)ast_store_new (
117 (ast_expression*)local,
118 (ast_expression*)parser_const_float_1(parser)
124 (ast_expression*)ast_binstore_new (
128 (ast_expression*)arg2,
129 (ast_expression*)parser_const_float(parser, 0.25f)
135 (ast_expression*)ast_binstore_new (
139 (ast_expression*)arg1,
140 (ast_expression*)arg1
145 loop2 = ast_loop_new (
148 (ast_expression*)ast_binary_new (
151 (ast_expression*)arg2,
152 (ast_expression*)parser_const_float_1(parser)
161 /* push nested loop into loop expressions */
162 vec_push(l1b->exprs, (ast_expression*)loop2);
166 (ast_expression*)ast_binstore_new (
170 (ast_expression*)arg2,
171 (ast_expression*)parser_const_float_1(parser)
176 (ast_expression*)ast_binstore_new (
180 (ast_expression*)local,
181 (ast_expression*)arg1
186 loop1 = ast_loop_new (
189 (ast_expression*)ast_binary_new (
192 (ast_expression*)arg2,
193 (ast_expression*)parser_const_float_0(parser)
202 /* push the loop1 into the body for the function */
203 vec_push(body->exprs, (ast_expression*)loop1);
206 vec_push(body->exprs,
207 (ast_expression*)ast_return_new (
209 (ast_expression*)local
213 /* push block and register intrin for codegen */
214 vec_push(func->blocks, body);
216 INTRIN_REG(func, value);
219 return (ast_expression*)value;
222 static ast_expression *intrin_mod(parser_t *parser) {
224 * float mod(float x, float y) {
225 * return x - y * floor(x / y);
228 static ast_value *value = NULL;
231 ast_call *call = ast_call_new (parser_ctx(parser), intrin_func(parser, "floor"));
232 ast_value *arg1 = ast_value_new(parser_ctx(parser), "x", TYPE_FLOAT);
233 ast_value *arg2 = ast_value_new(parser_ctx(parser), "y", TYPE_FLOAT);
234 ast_block *body = ast_block_new(parser_ctx(parser));
235 ast_function *func = NULL;
237 INTRIN_VAL(value, "mod", func, "<float>", TYPE_FLOAT);
240 vec_push(call->params,
241 (ast_expression*)ast_binary_new (
244 (ast_expression*)arg1,
245 (ast_expression*)arg2
249 vec_push(body->exprs,
250 (ast_expression*)ast_return_new(
252 (ast_expression*)ast_binary_new(
255 (ast_expression*)arg1,
256 (ast_expression*)ast_binary_new(
259 (ast_expression*)arg2,
260 (ast_expression*)call
266 vec_push(value->expression.params, arg1); /* float x (for param) */
267 vec_push(value->expression.params, arg2); /* float y (for param) */
269 vec_push(func->blocks, body); /* {{{ body }}} */
271 INTRIN_REG(func, value);
274 return (ast_expression*)value;
277 static ast_expression *intrin_exp(parser_t *parser) {
279 * float exp(float x) {
280 * return pow(QC_M_E, x);
283 static ast_value *value = NULL;
286 ast_call *call = ast_call_new (parser_ctx(parser), intrin_func(parser, "pow"));
287 ast_value *arg1 = ast_value_new (parser_ctx(parser), "x", TYPE_FLOAT);
288 ast_block *body = ast_block_new (parser_ctx(parser));
289 ast_function *func = NULL;
291 INTRIN_VAL(value, "exp", func, "<float>", TYPE_FLOAT);
293 /* push arguments for params to call */
294 vec_push(call->params, (ast_expression*)parser_const_float(parser, QC_M_E));
295 vec_push(call->params, (ast_expression*)arg1);
297 /* return pow(QC_M_E, x) */
298 vec_push(body->exprs,
299 (ast_expression*)ast_return_new(
301 (ast_expression*)call
305 vec_push(value->expression.params, arg1); /* float x (for param) */
307 vec_push(func->blocks, body); /* {{{ body }}} */
309 INTRIN_REG(func, value);
312 return (ast_expression*)value;
315 static ast_expression *intrin_isnan(parser_t *parser) {
317 * float isnan(float x) {
321 * return (x != local);
324 static ast_value *value = NULL;
327 ast_value *arg1 = ast_value_new (parser_ctx(parser), "x", TYPE_FLOAT);
328 ast_value *local = ast_value_new (parser_ctx(parser), "local", TYPE_FLOAT);
329 ast_block *body = ast_block_new (parser_ctx(parser));
330 ast_function *func = NULL;
332 INTRIN_VAL(value, "isnan", func, "<float>", TYPE_FLOAT);
334 vec_push(body->locals, local);
335 vec_push(body->exprs,
336 (ast_expression*)ast_store_new(
339 (ast_expression*)local,
340 (ast_expression*)arg1
344 vec_push(body->exprs,
345 (ast_expression*)ast_return_new(
347 (ast_expression*)ast_binary_new(
350 (ast_expression*)arg1,
351 (ast_expression*)local
356 vec_push(value->expression.params, arg1);
358 vec_push(func->blocks, body);
360 INTRIN_REG(func, value);
363 return (ast_expression*)value;
366 static intrin_t intrinsics[] = {
367 {&intrin_exp, "__builtin_exp", "exp"},
368 {&intrin_mod, "__builtin_mod", "mod"},
369 {&intrin_pow, "__builtin_pow", "pow"},
370 {&intrin_isnan, "__builtin_isnan", "isnan"}
373 void intrin_intrinsics_destroy(parser_t *parser) {
376 util_htdel(intrin_intrinsics());
378 for (i = 0; i < sizeof(intrinsics)/sizeof(intrin_t); i++)
379 ast_value_delete( (ast_value*) intrinsics[i].intrin(parser));
384 static ast_expression *intrin_func(parser_t *parser, const char *name) {
385 static bool init = false;
389 /* register the intrinsics in the hashtable for O(1) lookup */
391 for (i = 0; i < sizeof(intrinsics)/sizeof(*intrinsics); i++)
392 util_htset(intrin_intrinsics(), intrinsics[i].alias, &intrinsics[i]);
394 init = true; /* only once */
398 * jesus fucking christ, Blub design something less fucking
399 * impossible to use, like a ast_is_builtin(ast_expression *), also
402 if ((find = (void*)parser_find_global(parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
403 for (i = 0; i < vec_size(parser->functions); ++i)
404 if (((ast_value*)find)->name && !strcmp(parser->functions[i]->name, ((ast_value*)find)->name) && parser->functions[i]->builtin < 0)
405 return (ast_expression*)find;
407 if ((find = util_htget(intrin_intrinsics(), name))) {
408 /* intrinsic is in table. This will "generate the function" so
409 * to speak (if it's not already generated).
411 return ((intrin_t*)find)->intrin(parser);
414 parseerror(parser, "need function: `%s` compiler depends on it", name);