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 ht intrin_intrinsics() {
40 static ht intrinsics = NULL;
42 intrinsics = util_htnew(PARSER_HT_SIZE);
47 void intrin_intrinsics_destroy() {
48 util_htdel(intrin_intrinsics());
51 #define INTRIN_VAL(VALUE, NAME, FUNC, STYPE, VTYPE) \
53 (VALUE) = ast_value_new ( \
58 (VALUE)->expression.next = (ast_expression*)ast_value_new ( \
63 (FUNC) = ast_function_new ( \
70 #define INTRIN_REG(FUNC, VALUE) \
72 vec_push(parser->functions, (FUNC)); \
73 vec_push(parser->globals, (ast_expression*)(VALUE)); \
77 ast_expression *intrin_func (parser_t *parser, const char *name);
79 #define QC_M_E 2.71828182845905
81 ast_expression *intrin_pow(parser_t *parser) {
83 * float pow(float x, float y) {
96 static ast_value *value = NULL;
99 ast_value *arg1 = ast_value_new(parser_ctx(parser), "x", TYPE_FLOAT);
100 ast_value *arg2 = ast_value_new(parser_ctx(parser), "y", TYPE_FLOAT);
101 ast_value *local = ast_value_new(parser_ctx(parser), "local", TYPE_FLOAT);
102 ast_block *body = ast_block_new(parser_ctx(parser));
103 ast_block *l1b = ast_block_new(parser_ctx(parser)); /* loop 1 body */
104 ast_block *l2b = ast_block_new(parser_ctx(parser)); /* looo 2 body */
105 ast_loop *loop1 = NULL;
106 ast_loop *loop2 = NULL;
107 ast_function *func = NULL;
109 INTRIN_VAL(value, "pow", func, "<float>", TYPE_FLOAT);
112 vec_push(value->expression.params, arg1);
113 vec_push(value->expression.params, arg2);
116 vec_push(body->locals, local);
118 /* assignment to local of value 1.0f */
119 vec_push(body->exprs,
120 (ast_expression*)ast_store_new (
123 (ast_expression*)local,
124 (ast_expression*)parser_const_float_1(parser)
130 (ast_expression*)ast_binstore_new (
134 (ast_expression*)arg2,
135 (ast_expression*)parser_const_float(parser, 0.25f)
141 (ast_expression*)ast_binstore_new (
145 (ast_expression*)arg1,
146 (ast_expression*)arg1
151 loop2 = ast_loop_new (
154 (ast_expression*)ast_binary_new (
157 (ast_expression*)arg2,
158 (ast_expression*)parser_const_float_1(parser)
167 /* push nested loop into loop expressions */
168 vec_push(l1b->exprs, (ast_expression*)loop2);
172 (ast_expression*)ast_binstore_new (
176 (ast_expression*)arg2,
177 (ast_expression*)parser_const_float_1(parser)
182 (ast_expression*)ast_binstore_new (
186 (ast_expression*)local,
187 (ast_expression*)arg1
192 loop1 = ast_loop_new (
195 (ast_expression*)ast_binary_new (
198 (ast_expression*)arg2,
199 (ast_expression*)parser_const_float_0(parser)
208 /* push the loop1 into the body for the function */
209 vec_push(body->exprs, (ast_expression*)loop1);
212 vec_push(body->exprs,
213 (ast_expression*)ast_return_new (
215 (ast_expression*)local
219 /* push block and register intrin for codegen */
220 vec_push(func->blocks, body);
222 INTRIN_REG(func, value);
225 return (ast_expression*)value;
228 ast_expression *intrin_mod(parser_t *parser) {
230 * float mod(float x, float y) {
231 * return x - y * floor(x / y);
234 static ast_value *value = NULL;
237 ast_call *call = ast_call_new (parser_ctx(parser), intrin_func(parser, "floor"));
238 ast_value *arg1 = ast_value_new(parser_ctx(parser), "x", TYPE_FLOAT);
239 ast_value *arg2 = ast_value_new(parser_ctx(parser), "y", TYPE_FLOAT);
240 ast_block *body = ast_block_new(parser_ctx(parser));
241 ast_function *func = NULL;
243 INTRIN_VAL(value, "mod", func, "<float>", TYPE_FLOAT);
246 vec_push(call->params,
247 (ast_expression*)ast_binary_new (
250 (ast_expression*)arg1,
251 (ast_expression*)arg2
255 vec_push(body->exprs,
256 (ast_expression*)ast_return_new(
258 (ast_expression*)ast_binary_new(
261 (ast_expression*)arg1,
262 (ast_expression*)ast_binary_new(
265 (ast_expression*)arg2,
266 (ast_expression*)call
272 vec_push(value->expression.params, arg1); /* float x (for param) */
273 vec_push(value->expression.params, arg2); /* float y (for param) */
275 vec_push(func->blocks, body); /* {{{ body }}} */
277 INTRIN_REG(func, value);
280 return (ast_expression*)value;
283 ast_expression *intrin_exp(parser_t *parser) {
285 * float exp(float x) {
286 * return pow(QC_M_E, x);
289 static ast_value *value = NULL;
292 ast_call *call = ast_call_new (parser_ctx(parser), intrin_func(parser, "pow"));
293 ast_value *arg1 = ast_value_new (parser_ctx(parser), "x", TYPE_FLOAT);
294 ast_block *body = ast_block_new (parser_ctx(parser));
295 ast_function *func = NULL;
297 INTRIN_VAL(value, "exp", func, "<float>", TYPE_FLOAT);
299 /* push arguments for params to call */
300 vec_push(call->params, (ast_expression*)parser_const_float(parser, QC_M_E));
301 vec_push(call->params, (ast_expression*)arg1);
303 /* return pow(QC_M_E, x) */
304 vec_push(body->exprs,
305 (ast_expression*)ast_return_new(
307 (ast_expression*)call
311 vec_push(value->expression.params, arg1); /* float x (for param) */
313 vec_push(func->blocks, body); /* {{{ body }}} */
315 INTRIN_REG(func, value);
318 return (ast_expression*)value;
321 ast_expression *intrin_isnan(parser_t *parser) {
323 * float isnan(float x) {
327 * return (x != local);
330 static ast_value *value = NULL;
333 ast_value *arg1 = ast_value_new (parser_ctx(parser), "x", TYPE_FLOAT);
334 ast_value *local = ast_value_new (parser_ctx(parser), "local", TYPE_FLOAT);
335 ast_block *body = ast_block_new (parser_ctx(parser));
336 ast_function *func = NULL;
338 INTRIN_VAL(value, "isnan", func, "<float>", TYPE_FLOAT);
340 vec_push(body->locals, local);
341 vec_push(body->exprs,
342 (ast_expression*)ast_store_new(
345 (ast_expression*)local,
346 (ast_expression*)arg1
350 vec_push(body->exprs,
351 (ast_expression*)ast_return_new(
353 (ast_expression*)ast_binary_new(
356 (ast_expression*)arg1,
357 (ast_expression*)local
362 vec_push(value->expression.params, arg1);
364 vec_push(func->blocks, body);
366 INTRIN_REG(func, value);
369 return (ast_expression*)value;
372 static intrin_t intrinsics[] = {
373 {&intrin_exp, "__builtin_exp", "exp"},
374 {&intrin_mod, "__builtin_mod", "mod"},
375 {&intrin_pow, "__builtin_pow", "pow"},
376 {&intrin_isnan, "__builtin_isnan", "isnan"}
379 ast_expression *intrin_func(parser_t *parser, const char *name) {
380 static bool init = false;
384 /* register the intrinsics in the hashtable for O(1) lookup */
386 for (i = 0; i < sizeof(intrinsics)/sizeof(*intrinsics); i++)
387 util_htset(intrin_intrinsics(), intrinsics[i].alias, &intrinsics[i]);
389 init = true; /* only once */
393 * jesus fucking christ, Blub design something less fucking
394 * impossible to use, like a ast_is_builtin(ast_expression *), also
397 if ((find = (void*)parser_find_global(parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
398 for (i = 0; i < vec_size(parser->functions); ++i)
399 if (((ast_value*)find)->name && !strcmp(parser->functions[i]->name, ((ast_value*)find)->name) && parser->functions[i]->builtin < 0)
402 if ((find = util_htget(intrin_intrinsics(), name))) {
403 /* intrinsic is in table. This will "generate the function" so
404 * to speak (if it's not already generated).
406 return ((intrin_t*)find)->intrin(parser);
409 parseerror(parser, "need function: `%s` compiler depends on it", name);