*/
#include <stdio.h>
#include <stdarg.h>
+#include <math.h>
#include "gmqcc.h"
#include "lexer.h"
static ast_value* parser_create_array_setter_proto(parser_t *parser, ast_value *array, const char *funcname);
static ast_value* parser_create_array_getter_proto(parser_t *parser, ast_value *array, const ast_expression *elemtype, const char *funcname);
static ast_value *parse_typename(parser_t *parser, ast_value **storebase, ast_value *cached_typedef);
+static ast_expression *parser_builtin_pow(parser_t *);
+static ast_expression *parser_builtin_exp(parser_t *);
static void parseerror(parser_t *parser, const char *fmt, ...)
{
static ast_expression* parser_find_global(parser_t *parser, const char *name)
{
+ ast_expression *var = (ast_expression*)util_htget(parser->aliases, parser_tokval(parser));
+ if (var)
+ return var;
return (ast_expression*)util_htget(parser->htglobals, name);
}
out = (ast_expression*)ast_ternary_new(ctx, exprs[0], exprs[1], exprs[2]);
break;
+ case opid2('*', '*'):
+ if (NotSameType(TYPE_FLOAT)) {
+ ast_type_to_string(exprs[0], ty1, sizeof(ty1));
+ ast_type_to_string(exprs[1], ty2, sizeof(ty2));
+ compile_error(ctx, "invalid types used in exponentiation: %s and %s",
+ ty1, ty2);
+
+ return false;
+ }
+
+ if (CanConstFold(exprs[0], exprs[1])) {
+ out = (ast_expression*)parser_const_float(parser, powf(ConstF(0), ConstF(1)));
+ } else {
+ ast_call *gencall = ast_call_new(parser_ctx(parser), parser_builtin_pow(parser));
+ vec_push(gencall->params, exprs[0]);
+ vec_push(gencall->params, exprs[1]);
+ out = (ast_expression*)gencall;
+ }
+ break;
+
case opid3('<','=','>'): /* -1, 0, or 1 */
if (NotSameType(TYPE_FLOAT)) {
ast_type_to_string(exprs[0], ty1, sizeof(ty1));
} else {
ast_binary *eq = ast_binary_new(ctx, INSTR_EQ_F, exprs[0], exprs[1]);
- eq->refs = false; /* references nothing */
+ eq->refs = (ast_binary_ref)false; /* references nothing */
/* if (lt) { */
out = (ast_expression*)ast_ternary_new(ctx,
if(CanConstFold1(exprs[0]))
out = (ast_expression*)parser_const_float(parser, ~(qcint)ConstF(0));
else
- out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, (ast_expression*)parser_const_float_neg1(parser), exprs[0]);
+ out = (ast_expression*)
+ ast_binary_new(ctx, INSTR_SUB_F, (ast_expression*)parser_const_float_neg1(parser), exprs[0]);
break;
}
#undef NotSameType
/* intrinsics */
if (!strcmp(parser_tokval(parser), "__builtin_debug_typestring")) {
var = (ast_expression*)intrinsic_debug_typestring;
- } else {
- var = (ast_expression*)parser_find_var(parser, (const char *)util_htget(parser->aliases, parser_tokval(parser)));
}
+ if (!strcmp(parser_tokval(parser), "__builtin_pow"))
+ var = parser_builtin_pow(parser);
+ if (!strcmp(parser_tokval(parser), "__builtin_exp"))
+ var = parser_builtin_exp(parser);
if (!var) {
char *correct = NULL;
return true;
}
+ast_expression *parser_builtin_pow(parser_t *parser) {
+ /*
+ * float __builtin_pow(float x, float y) {
+ * float value = 1.0f;
+ * while (y > 0) {
+ * while (!(y&1)) {
+ * y *= 0.25f;
+ * x *= x;
+ * }
+ * y--;
+ * value *= x;
+ * }
+ * return value;
+ * }
+ */
+ static ast_function *pow_func = NULL;
+ static ast_value *pow_func_val = NULL;
+ if (!pow_func) {
+ ast_value *pow_arguments[2];
+ ast_value *pow_value = ast_value_new (parser_ctx(parser), "value", TYPE_FLOAT);
+ ast_block *pow_body = ast_block_new (parser_ctx(parser));
+ ast_block *pow_loop_body = ast_block_new (parser_ctx(parser));
+ ast_block *pow_loop_nest_body = ast_block_new (parser_ctx(parser));
+ ast_loop *pow_loop = NULL;
+ ast_loop *pow_loop_nest = NULL;
+
+ pow_arguments[0] = ast_value_new (parser_ctx(parser), "x", TYPE_FLOAT);
+ pow_arguments[1] = ast_value_new (parser_ctx(parser), "x", TYPE_FLOAT);
+ pow_func_val = ast_value_new (parser_ctx(parser), "__builtin_pow", TYPE_FUNCTION);
+ pow_func_val->expression.next = (ast_expression*)ast_value_new(parser_ctx(parser), "<float>", TYPE_FLOAT);
+
+ vec_push(pow_func_val->expression.params, pow_arguments[0]);
+ vec_push(pow_func_val->expression.params, pow_arguments[1]);
+
+ pow_func = ast_function_new(parser_ctx(parser), "__builtin_pow", pow_func_val);
+
+ /* float value; */
+ vec_push(pow_body->locals, pow_value);
+ /* value = 1.0f; */
+ vec_push(pow_body->exprs,
+ (ast_expression*)ast_store_new(
+ parser_ctx(parser),
+ INSTR_STORE_F,
+ (ast_expression*)pow_value,
+ (ast_expression*)parser_const_float_1(parser)
+ )
+ );
+
+ /* y >>= 2 */
+ vec_push(pow_loop_nest_body->exprs,
+ (ast_expression*)ast_binstore_new(
+ parser_ctx(parser),
+ INSTR_STORE_F,
+ INSTR_MUL_F,
+ (ast_expression*)pow_arguments[1],
+ (ast_expression*)parser_const_float(parser, 0.25f)
+ )
+ );
+ vec_push(pow_loop_nest_body->exprs,
+ (ast_expression*)ast_binstore_new(
+ parser_ctx(parser),
+ INSTR_STORE_F,
+ INSTR_MUL_F,
+ (ast_expression*)pow_arguments[0],
+ (ast_expression*)pow_arguments[0]
+ )
+ );
+
+ /* while (!(y&1)) */
+ pow_loop_nest = ast_loop_new (
+ parser_ctx(parser),
+ NULL,
+ (ast_expression*)ast_binary_new(
+ parser_ctx(parser),
+ INSTR_AND,
+ (ast_expression*)pow_arguments[1],
+ (ast_expression*)parser_const_float_1(parser)
+ ),
+ true,
+ NULL,
+ false,
+ NULL,
+ (ast_expression*)pow_loop_nest_body
+ );
+
+ vec_push(pow_loop_body->exprs, (ast_expression*)pow_loop_nest);
+ vec_push(pow_loop_body->exprs,
+ (ast_expression*)ast_binstore_new(
+ parser_ctx(parser),
+ INSTR_STORE_F,
+ INSTR_SUB_F,
+ (ast_expression*)pow_arguments[1],
+ (ast_expression*)parser_const_float_1(parser)
+ )
+ );
+ vec_push(pow_loop_body->exprs,
+ (ast_expression*)ast_binstore_new(
+ parser_ctx(parser),
+ INSTR_STORE_F,
+ INSTR_MUL_F,
+ (ast_expression*)pow_value,
+ (ast_expression*)pow_arguments[0]
+ )
+ );
+
+ /* while (y > 0) { */
+ pow_loop = ast_loop_new(
+ parser_ctx(parser),
+ NULL,
+ (ast_expression*)ast_binary_new(
+ parser_ctx(parser),
+ INSTR_GT,
+ (ast_expression*)pow_arguments[1],
+ (ast_expression*)parser_const_float_0(parser)
+ ),
+ false,
+ NULL,
+ false,
+ NULL,
+ (ast_expression*)pow_loop_body
+ );
+ /* } */
+ vec_push(pow_body->exprs, (ast_expression*)pow_loop);
+ /* return value; */
+ vec_push(pow_body->exprs,
+ (ast_expression*)ast_return_new(
+ parser_ctx(parser),
+ (ast_expression*)pow_value
+ )
+ );
+
+ vec_push(pow_func->blocks, pow_body);
+ vec_push(parser->globals, (ast_expression*)pow_func_val);
+ vec_push(parser->functions, pow_func);
+ }
+
+ return (ast_expression*)pow_func_val;
+}
+
+#ifndef M_E
+#define M_E 2.71828182845905
+#endif
+static ast_expression *parser_builtin_exp(parser_t *parser) {
+ /*
+ * float __builtin_exp(float x) {
+ * return __builtin_exp(E, x);
+ * }
+ */
+ static ast_value *exp_func_val = NULL;
+
+ if (!exp_func_val) {
+ ast_function *exp_func = NULL;
+ ast_value *arg = ast_value_new (parser_ctx(parser), "x", TYPE_FLOAT);
+ ast_block *exp_body = ast_block_new (parser_ctx(parser));
+ ast_call *exp_call = ast_call_new (parser_ctx(parser), parser_builtin_pow(parser));
+ exp_func_val = ast_value_new (parser_ctx(parser), "__builtin_exp", TYPE_FUNCTION);
+ exp_func_val->expression.next = (ast_expression*)ast_value_new(parser_ctx(parser), "<float>", TYPE_FLOAT);
+ exp_func = ast_function_new(parser_ctx(parser), "__builtin_exp", exp_func_val);
+
+ vec_push(exp_call->params, (ast_expression*)parser_const_float(parser, M_E));
+ vec_push(exp_call->params, (ast_expression*)arg);
+
+ vec_push(exp_body->exprs,
+ (ast_expression*)ast_return_new(
+ parser_ctx(parser),
+ (ast_expression*)exp_call
+ )
+ );
+
+ vec_push(exp_func_val->expression.params, arg);
+ vec_push(exp_func->blocks, exp_body);
+
+ vec_push(parser->functions, exp_func);
+ vec_push(parser->globals, (ast_expression*)exp_func_val);
+ }
+
+ return (ast_expression*)exp_func_val;
+}
+
/* parse computed goto sides */
static ast_expression *parse_goto_computed(parser_t *parser, ast_expression **side) {
ast_expression *on_true;
static bool parse_enum(parser_t *parser)
{
+ bool flag = false;
qcfloat num = 0;
ast_value **values = NULL;
ast_value *var = NULL;
ast_expression *old;
- if (!parser_next(parser) || parser->tok != '{') {
- parseerror(parser, "expected `{` after `enum` keyword");
+ if (!parser_next(parser) || (parser->tok != '{' && parser->tok != ':')) {
+ parseerror(parser, "expected `{` or `:` after `enum` keyword");
return false;
}
+ /* enumeration attributes (can add more later) */
+ if (parser->tok == ':') {
+ if (!parser_next(parser) || parser->tok != TOKEN_IDENT || strcmp(parser_tokval(parser), "flag")) {
+ parseerror(parser, "expected `flag` after enumeration attribute ':'");
+ return false;
+ }
+
+ if (!parser_next(parser) || parser->tok != '{') {
+ parseerror(parser, "expected `{` after enum attribute `flag`");
+ return false;
+ }
+
+ /* flagged enumeration start from 1 */
+ num = 1;
+ flag = true;
+ }
+
while (true) {
if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
if (parser->tok == '}') {
vec_push(values, var);
var->cvq = CV_CONST;
var->hasvalue = true;
- var->constval.vfloat = num++;
+
+ /* for flagged enumerations increment in POTs of TWO */
+ var->constval.vfloat = (flag) ? (num *= 2) : (num ++);
parser_addglobal(parser, var->name, (ast_expression*)var);
}
}
} else {
- util_htset(parser->aliases, var->name, (void*)var->desc);
+ ast_expression *find = parser_find_global(parser, var->desc);
+
+ if (!find) {
+ compile_error(parser_ctx(parser), "undeclared variable `%s` for alias `%s`", var->desc, var->name);
+ return false;
+ }
+
+ if (var->expression.vtype != find->expression.vtype) {
+ char ty1[1024];
+ char ty2[1024];
+
+ ast_type_to_string(find, ty1, sizeof(ty1));
+ ast_type_to_string((ast_expression*)var, ty2, sizeof(ty2));
+
+ compile_error(parser_ctx(parser), "incompatible types `%s` and `%s` for alias `%s`",
+ ty1, ty2, var->name
+ );
+ return false;
+ }
+
+ /*
+ * add alias to aliases table and to corrector
+ * so corrections can apply for aliases as well.
+ */
+ util_htset(parser->aliases, var->name, find);
+
+ /*
+ * add to corrector so corrections can work
+ * even for aliases too.
+ */
+ correct_add (
+ vec_last(parser->correct_variables),
+ &vec_last(parser->correct_variables_score),
+ var->name
+ );
+
+ /* generate aliases for vector components */
+ if (isvector) {
+ char *buffer[3];
+
+ util_asprintf(&buffer[0], "%s_x", var->desc);
+ util_asprintf(&buffer[1], "%s_y", var->desc);
+ util_asprintf(&buffer[2], "%s_z", var->desc);
+
+ util_htset(parser->aliases, me[0]->name, parser_find_global(parser, buffer[0]));
+ util_htset(parser->aliases, me[1]->name, parser_find_global(parser, buffer[1]));
+ util_htset(parser->aliases, me[2]->name, parser_find_global(parser, buffer[2]));
+
+ mem_d(buffer[0]);
+ mem_d(buffer[1]);
+ mem_d(buffer[2]);
+
+ /*
+ * add to corrector so corrections can work
+ * even for aliases too.
+ */
+ correct_add (
+ vec_last(parser->correct_variables),
+ &vec_last(parser->correct_variables_score),
+ me[0]->name
+ );
+ correct_add (
+ vec_last(parser->correct_variables),
+ &vec_last(parser->correct_variables_score),
+ me[1]->name
+ );
+ correct_add (
+ vec_last(parser->correct_variables),
+ &vec_last(parser->correct_variables_score),
+ me[2]->name
+ );
+ }
}
}
} else {