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
27 * Provides all the "intrinsics" / "builtins" for GMQCC. These can do
28 * a few things, they can provide fall back implementations for math
29 * functions if the definitions don't exist for some given engine. Or
30 * then can determine definitions for existing builtins, and simply
31 * wrap back to them instead. This is like a "portable" intrface that
32 * is entered when -fintrin is used (causing all existing builtins to
33 * be ignored by the compiler and instead interface through here.
35 #define INTRIN_VAL(VALUE, NAME, FUNC, STYPE, VTYPE) \
37 (VALUE) = ast_value_new ( \
38 parser_ctx(intrin->parser), \
42 (VALUE)->intrinsic = true; \
43 (VALUE)->expression.next = (ast_expression*)ast_value_new ( \
44 parser_ctx(intrin->parser), \
48 (FUNC) = ast_function_new ( \
49 parser_ctx(intrin->parser), \
55 #define INTRIN_REG(FUNC, VALUE) \
57 vec_push(intrin->parser->functions, (FUNC)); \
58 vec_push(intrin->parser->globals, (ast_expression*)(VALUE)); \
61 #define QC_M_E 2.71828182845905f
63 static ast_expression *intrin_pow (intrin_t *intrin) {
65 * float pow(float x, float y) {
78 static ast_value *value = NULL;
81 ast_value *arg1 = ast_value_new(parser_ctx(intrin->parser), "x", TYPE_FLOAT);
82 ast_value *arg2 = ast_value_new(parser_ctx(intrin->parser), "y", TYPE_FLOAT);
83 ast_value *local = ast_value_new(parser_ctx(intrin->parser), "local", TYPE_FLOAT);
84 ast_block *body = ast_block_new(parser_ctx(intrin->parser));
85 ast_block *l1b = ast_block_new(parser_ctx(intrin->parser)); /* loop 1 body */
86 ast_block *l2b = ast_block_new(parser_ctx(intrin->parser)); /* loop 2 body */
87 ast_loop *loop1 = NULL;
88 ast_loop *loop2 = NULL;
89 ast_function *func = NULL;
91 INTRIN_VAL(value, "pow", func, "<float>", TYPE_FLOAT);
94 vec_push(value->expression.params, arg1);
95 vec_push(value->expression.params, arg2);
98 vec_push(body->locals, local);
100 /* assignment to local of value 1.0f */
101 vec_push(body->exprs,
102 (ast_expression*)ast_store_new (
103 parser_ctx(intrin->parser),
105 (ast_expression*)local,
106 (ast_expression*)intrin->fold->imm_float[1] /* 1 == 1.0f */
112 (ast_expression*)ast_binstore_new (
113 parser_ctx(intrin->parser),
116 (ast_expression*)arg2,
117 (ast_expression*)fold_constgen_float(intrin->parser->fold, 0.25f)
123 (ast_expression*)ast_binstore_new (
124 parser_ctx(intrin->parser),
127 (ast_expression*)arg1,
128 (ast_expression*)arg1
133 loop2 = ast_loop_new (
134 parser_ctx(intrin->parser),
136 (ast_expression*)ast_binary_new (
137 parser_ctx(intrin->parser),
139 (ast_expression*)arg2,
140 (ast_expression*)intrin->fold->imm_float[1] /* 1 == 1.0f */
149 /* push nested loop into loop expressions */
150 vec_push(l1b->exprs, (ast_expression*)loop2);
154 (ast_expression*)ast_binstore_new (
155 parser_ctx(intrin->parser),
158 (ast_expression*)arg2,
159 (ast_expression*)intrin->fold->imm_float[1] /* 1 == 1.0f */
164 (ast_expression*)ast_binstore_new (
165 parser_ctx(intrin->parser),
168 (ast_expression*)local,
169 (ast_expression*)arg1
174 loop1 = ast_loop_new (
175 parser_ctx(intrin->parser),
177 (ast_expression*)ast_binary_new (
178 parser_ctx(intrin->parser),
180 (ast_expression*)arg2,
181 (ast_expression*)intrin->fold->imm_float[0] /* 0 == 0.0f */
190 /* push the loop1 into the body for the function */
191 vec_push(body->exprs, (ast_expression*)loop1);
194 vec_push(body->exprs,
195 (ast_expression*)ast_return_new (
196 parser_ctx(intrin->parser),
197 (ast_expression*)local
201 /* push block and register intrin for codegen */
202 vec_push(func->blocks, body);
204 INTRIN_REG(func, value);
207 return (ast_expression*)value;
210 static ast_expression *intrin_mod(intrin_t *intrin) {
212 * float mod(float x, float y) {
213 * return x - y * floor(x / y);
216 static ast_value *value = NULL;
219 ast_call *call = ast_call_new (parser_ctx(intrin->parser), intrin_func(intrin, "floor"));
220 ast_value *arg1 = ast_value_new(parser_ctx(intrin->parser), "x", TYPE_FLOAT);
221 ast_value *arg2 = ast_value_new(parser_ctx(intrin->parser), "y", TYPE_FLOAT);
222 ast_block *body = ast_block_new(parser_ctx(intrin->parser));
223 ast_function *func = NULL;
225 INTRIN_VAL(value, "mod", func, "<float>", TYPE_FLOAT);
228 vec_push(call->params,
229 (ast_expression*)ast_binary_new (
230 parser_ctx(intrin->parser),
232 (ast_expression*)arg1,
233 (ast_expression*)arg2
237 vec_push(body->exprs,
238 (ast_expression*)ast_return_new(
239 parser_ctx(intrin->parser),
240 (ast_expression*)ast_binary_new(
241 parser_ctx(intrin->parser),
243 (ast_expression*)arg1,
244 (ast_expression*)ast_binary_new(
245 parser_ctx(intrin->parser),
247 (ast_expression*)arg2,
248 (ast_expression*)call
254 vec_push(value->expression.params, arg1); /* float x (for param) */
255 vec_push(value->expression.params, arg2); /* float y (for param) */
257 vec_push(func->blocks, body); /* {{{ body }}} */
259 INTRIN_REG(func, value);
262 return (ast_expression*)value;
265 static ast_expression *intrin_exp(intrin_t *intrin) {
267 * float exp(float x) {
268 * return pow(QC_M_E, x);
271 static ast_value *value = NULL;
274 ast_call *call = ast_call_new (parser_ctx(intrin->parser), intrin_func(intrin, "pow"));
275 ast_value *arg1 = ast_value_new(parser_ctx(intrin->parser), "x", TYPE_FLOAT);
276 ast_block *body = ast_block_new(parser_ctx(intrin->parser));
277 ast_function *func = NULL;
279 INTRIN_VAL(value, "exp", func, "<float>", TYPE_FLOAT);
281 /* push arguments for params to call */
282 vec_push(call->params, (ast_expression*)fold_constgen_float(intrin->fold, QC_M_E));
283 vec_push(call->params, (ast_expression*)arg1);
285 /* return pow(QC_M_E, x) */
286 vec_push(body->exprs,
287 (ast_expression*)ast_return_new(
288 parser_ctx(intrin->parser),
289 (ast_expression*)call
293 vec_push(value->expression.params, arg1); /* float x (for param) */
295 vec_push(func->blocks, body); /* {{{ body }}} */
297 INTRIN_REG(func, value);
300 return (ast_expression*)value;
303 static ast_expression *intrin_isnan(intrin_t *intrin) {
305 * float isnan(float x) {
309 * return (x != local);
312 static ast_value *value = NULL;
315 ast_value *arg1 = ast_value_new(parser_ctx(intrin->parser), "x", TYPE_FLOAT);
316 ast_value *local = ast_value_new(parser_ctx(intrin->parser), "local", TYPE_FLOAT);
317 ast_block *body = ast_block_new(parser_ctx(intrin->parser));
318 ast_function *func = NULL;
320 INTRIN_VAL(value, "isnan", func, "<float>", TYPE_FLOAT);
322 vec_push(body->locals, local);
323 vec_push(body->exprs,
324 (ast_expression*)ast_store_new(
325 parser_ctx(intrin->parser),
327 (ast_expression*)local,
328 (ast_expression*)arg1
332 vec_push(body->exprs,
333 (ast_expression*)ast_return_new(
334 parser_ctx(intrin->parser),
335 (ast_expression*)ast_binary_new(
336 parser_ctx(intrin->parser),
338 (ast_expression*)arg1,
339 (ast_expression*)local
344 vec_push(value->expression.params, arg1);
345 vec_push(func->blocks, body);
347 INTRIN_REG(func, value);
350 return (ast_expression*)value;
353 static ast_expression *intrin_fabs(intrin_t *intrin) {
355 * float fabs(float x) {
356 * return x < 0 ? -x : x;
359 static ast_value *value = NULL;
361 ast_value *arg1 = ast_value_new(parser_ctx(intrin->parser), "x", TYPE_FLOAT);
362 ast_block *body = ast_block_new(parser_ctx(intrin->parser));
363 ast_function *func = NULL;
365 INTRIN_VAL(value, "fabs", func, "<float>", TYPE_FLOAT);
367 vec_push(body->exprs,
368 (ast_expression*)ast_return_new(
369 parser_ctx(intrin->parser),
370 (ast_expression*)ast_ternary_new(
371 parser_ctx(intrin->parser),
372 (ast_expression*)ast_binary_new(
373 parser_ctx(intrin->parser),
375 (ast_expression*)arg1,
376 (ast_expression*)intrin->fold->imm_float[0]
378 (ast_expression*)ast_binary_new(
379 parser_ctx(intrin->parser),
381 (ast_expression*)intrin->fold->imm_float[0],
382 (ast_expression*)arg1
384 (ast_expression*)arg1
389 vec_push(value->expression.params, arg1);
390 vec_push(func->blocks, body);
392 INTRIN_REG(func, value);
395 return (ast_expression*)value;
402 * TODO: make static (and handle ast_type_string) here for the builtin
403 * instead of in SYA parse close.
405 ast_expression *intrin_debug_typestring(intrin_t *intrin) {
407 return (ast_expression*)0x1;
410 static const intrin_func_t intrinsics[] = {
411 {&intrin_exp, "__builtin_exp", "exp"},
412 {&intrin_mod, "__builtin_mod", "mod"},
413 {&intrin_pow, "__builtin_pow", "pow"},
414 {&intrin_isnan, "__builtin_isnan", "isnan"},
415 {&intrin_fabs, "__builtin_fabs", "fabs"},
416 {&intrin_debug_typestring, "__builtin_debug_typestring", ""}
419 static void intrin_error(intrin_t *intrin, const char *fmt, ...) {
422 vcompile_error(intrin->parser->lex->tok.ctx, fmt, ap);
427 intrin_t *intrin_init(parser_t *parser) {
428 intrin_t *intrin = (intrin_t*)mem_a(sizeof(intrin_t));
429 intrin->parser = parser;
430 intrin->fold = parser->fold;
431 intrin->intrinsics = NULL;
433 vec_append(intrin->intrinsics, sizeof(intrinsics)/sizeof(*intrinsics), intrinsics);
438 void intrin_cleanup(intrin_t *intrin) {
439 vec_free(intrin->intrinsics);
443 ast_expression *intrin_fold(intrin_t *intrin, ast_value *value, ast_expression **exprs) {
446 if (!value || !value->name)
449 for (i = 0; i < vec_size(intrin->intrinsics); i++)
450 if (!strcmp(value->name, intrin->intrinsics[i].name))
451 return fold_intrin(intrin->fold, value->name, exprs);
456 ast_expression *intrin_func(intrin_t *intrin, const char *name) {
460 /* try current first */
461 if ((find = (void*)parser_find_global(intrin->parser, name)) && ((ast_value*)find)->expression.vtype == TYPE_FUNCTION)
462 for (i = 0; i < vec_size(intrin->parser->functions); ++i)
463 if (((ast_value*)find)->name && !strcmp(intrin->parser->functions[i]->name, ((ast_value*)find)->name) && intrin->parser->functions[i]->builtin < 0)
464 return (ast_expression*)find;
465 /* try name second */
466 for (i = 0; i < vec_size(intrin->intrinsics); i++)
467 if (!strcmp(intrin->intrinsics[i].name, name))
468 return intrin->intrinsics[i].intrin(intrin);
469 /* try alias third */
470 for (i = 0; i < vec_size(intrin->intrinsics); i++)
471 if (!strcmp(intrin->intrinsics[i].alias, name))
472 return intrin->intrinsics[i].intrin(intrin);
474 intrin_error(intrin, "need function: `%s` compiler depends on it", name);