#define isfloat(X) (((ast_expression*)(X))->vtype == TYPE_FLOAT)
#define isvector(X) (((ast_expression*)(X))->vtype == TYPE_VECTOR)
#define isstring(X) (((ast_expression*)(X))->vtype == TYPE_STRING)
+#define isarray(X) (((ast_expression*)(X))->vtype == TYPE_ARRAY)
#define isfloats(X,Y) (isfloat (X) && isfloat (Y))
/*
return NULL;
}
+static GMQCC_INLINE ast_expression *fold_op_length(fold_t *fold, ast_value *a) {
+ if (fold_can_1(a) && isstring(a))
+ return fold_constgen_float(fold, strlen(fold_immvalue_string(a)), false);
+ if (isarray(a))
+ return fold_constgen_float(fold, vec_size(a->initlist), false);
+ return NULL;
+}
+
ast_expression *fold_op(fold_t *fold, const oper_info *info, ast_expression **opexprs) {
ast_value *a = (ast_value*)opexprs[0];
ast_value *b = (ast_value*)opexprs[1];
return e
switch(info->id) {
- fold_op_case(2, ('-', 'P'), neg, (fold, a));
- fold_op_case(2, ('!', 'P'), not, (fold, a));
- fold_op_case(1, ('+'), add, (fold, a, b));
- fold_op_case(1, ('-'), sub, (fold, a, b));
- fold_op_case(1, ('*'), mul, (fold, a, b));
- fold_op_case(1, ('/'), div, (fold, a, b));
- fold_op_case(1, ('%'), mod, (fold, a, b));
- fold_op_case(1, ('|'), bor, (fold, a, b));
- fold_op_case(1, ('&'), band, (fold, a, b));
- fold_op_case(1, ('^'), xor, (fold, a, b));
- fold_op_case(1, ('<'), ltgt, (fold, a, b, true));
- fold_op_case(1, ('>'), ltgt, (fold, a, b, false));
- fold_op_case(2, ('<', '<'), lshift, (fold, a, b));
- fold_op_case(2, ('>', '>'), rshift, (fold, a, b));
- fold_op_case(2, ('|', '|'), andor, (fold, a, b, true));
- fold_op_case(2, ('&', '&'), andor, (fold, a, b, false));
- fold_op_case(2, ('?', ':'), tern, (fold, a, b, c));
- fold_op_case(2, ('*', '*'), exp, (fold, a, b));
- fold_op_case(3, ('<','=','>'), lteqgt, (fold, a, b));
- fold_op_case(2, ('!', '='), cmp, (fold, a, b, true));
- fold_op_case(2, ('=', '='), cmp, (fold, a, b, false));
- fold_op_case(2, ('~', 'P'), bnot, (fold, a));
- fold_op_case(2, ('>', '<'), cross, (fold, a, b));
+ fold_op_case(2, ('-', 'P'), neg, (fold, a));
+ fold_op_case(2, ('!', 'P'), not, (fold, a));
+ fold_op_case(1, ('+'), add, (fold, a, b));
+ fold_op_case(1, ('-'), sub, (fold, a, b));
+ fold_op_case(1, ('*'), mul, (fold, a, b));
+ fold_op_case(1, ('/'), div, (fold, a, b));
+ fold_op_case(1, ('%'), mod, (fold, a, b));
+ fold_op_case(1, ('|'), bor, (fold, a, b));
+ fold_op_case(1, ('&'), band, (fold, a, b));
+ fold_op_case(1, ('^'), xor, (fold, a, b));
+ fold_op_case(1, ('<'), ltgt, (fold, a, b, true));
+ fold_op_case(1, ('>'), ltgt, (fold, a, b, false));
+ fold_op_case(2, ('<', '<'), lshift, (fold, a, b));
+ fold_op_case(2, ('>', '>'), rshift, (fold, a, b));
+ fold_op_case(2, ('|', '|'), andor, (fold, a, b, true));
+ fold_op_case(2, ('&', '&'), andor, (fold, a, b, false));
+ fold_op_case(2, ('?', ':'), tern, (fold, a, b, c));
+ fold_op_case(2, ('*', '*'), exp, (fold, a, b));
+ fold_op_case(3, ('<','=','>'), lteqgt, (fold, a, b));
+ fold_op_case(2, ('!', '='), cmp, (fold, a, b, true));
+ fold_op_case(2, ('=', '='), cmp, (fold, a, b, false));
+ fold_op_case(2, ('~', 'P'), bnot, (fold, a));
+ fold_op_case(2, ('>', '<'), cross, (fold, a, b));
+ fold_op_case(3, ('l', 'e', 'n'), length, (fold, a));
}
#undef fold_op_case
compile_error(fold_ctx(fold), "internal error: attempted to constant-fold for unsupported operator");
#define opid3(a,b,c) (((uint8_t)a<<16)|((uint8_t)b<<8)|(uint8_t)c)
static const oper_info c_operators[] = {
- { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX, false}, /* paren expression - non function call */
+ { "(", 0, opid1('('), ASSOC_LEFT, 99, OP_PREFIX, false}, /* paren expression - non function call */
+ { "length", 1, opid3('l','e','n'), ASSOC_RIGHT, 98, OP_PREFIX, true},
- { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 17, OP_SUFFIX, false},
- { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 17, OP_SUFFIX, false},
- { ".", 2, opid1('.'), ASSOC_LEFT, 17, 0, false},
- { "(", 0, opid1('('), ASSOC_LEFT, 17, 0, false}, /* function call */
- { "[", 2, opid1('['), ASSOC_LEFT, 17, 0, false}, /* array subscript */
+ { "++", 1, opid3('S','+','+'), ASSOC_LEFT, 17, OP_SUFFIX, false},
+ { "--", 1, opid3('S','-','-'), ASSOC_LEFT, 17, OP_SUFFIX, false},
+ { ".", 2, opid1('.'), ASSOC_LEFT, 17, 0, false},
+ { "(", 0, opid1('('), ASSOC_LEFT, 17, 0, false}, /* function call */
+ { "[", 2, opid1('['), ASSOC_LEFT, 17, 0, false}, /* array subscript */
- { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
- { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
+ { "++", 1, opid3('+','+','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
+ { "--", 1, opid3('-','-','P'), ASSOC_RIGHT, 16, OP_PREFIX, false},
- { "**", 2, opid2('*','*'), ASSOC_RIGHT, 14, 0, true},
- { "!", 1, opid2('!','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
- { "~", 1, opid2('~','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
- { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
- { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
-/* { "&", 1, opid2('&','P'), ASSOC_RIGHT, 14, OP_PREFIX, false}, */
+ { "**", 2, opid2('*','*'), ASSOC_RIGHT, 14, 0, true},
+ { "!", 1, opid2('!','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
+ { "~", 1, opid2('~','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
+ { "+", 1, opid2('+','P'), ASSOC_RIGHT, 14, OP_PREFIX, false},
+ { "-", 1, opid2('-','P'), ASSOC_RIGHT, 14, OP_PREFIX, true},
+/* { "&", 1, opid2('&','P'), ASSOC_RIGHT, 14, OP_PREFIX, false}, */
- { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0, true},
- { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0, true},
- { "%", 2, opid1('%'), ASSOC_LEFT, 13, 0, true},
- { "><", 2, opid2('>','<'), ASSOC_LEFT, 13, 0, true},
+ { "*", 2, opid1('*'), ASSOC_LEFT, 13, 0, true},
+ { "/", 2, opid1('/'), ASSOC_LEFT, 13, 0, true},
+ { "%", 2, opid1('%'), ASSOC_LEFT, 13, 0, true},
+ { "><", 2, opid2('>','<'), ASSOC_LEFT, 13, 0, true},
- { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0, true},
- { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0, true},
+ { "+", 2, opid1('+'), ASSOC_LEFT, 12, 0, true},
+ { "-", 2, opid1('-'), ASSOC_LEFT, 12, 0, true},
- { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0, true},
- { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0, true},
+ { "<<", 2, opid2('<','<'), ASSOC_LEFT, 11, 0, true},
+ { ">>", 2, opid2('>','>'), ASSOC_LEFT, 11, 0, true},
- { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0, false},
- { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0, false},
- { "<=>", 2, opid3('<','=','>'), ASSOC_LEFT, 10, 0, true},
- { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0, false},
- { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0, false},
+ { "<", 2, opid1('<'), ASSOC_LEFT, 10, 0, false},
+ { ">", 2, opid1('>'), ASSOC_LEFT, 10, 0, false},
+ { "<=>", 2, opid3('<','=','>'), ASSOC_LEFT, 10, 0, true},
+ { "<=", 2, opid2('<','='), ASSOC_LEFT, 10, 0, false},
+ { ">=", 2, opid2('>','='), ASSOC_LEFT, 10, 0, false},
- { "==", 2, opid2('=','='), ASSOC_LEFT, 9, 0, true},
- { "!=", 2, opid2('!','='), ASSOC_LEFT, 9, 0, true},
+ { "==", 2, opid2('=','='), ASSOC_LEFT, 9, 0, true},
+ { "!=", 2, opid2('!','='), ASSOC_LEFT, 9, 0, true},
- { "&", 2, opid1('&'), ASSOC_LEFT, 8, 0, true},
+ { "&", 2, opid1('&'), ASSOC_LEFT, 8, 0, true},
- { "^", 2, opid1('^'), ASSOC_LEFT, 7, 0, true},
+ { "^", 2, opid1('^'), ASSOC_LEFT, 7, 0, true},
- { "|", 2, opid1('|'), ASSOC_LEFT, 6, 0, true},
+ { "|", 2, opid1('|'), ASSOC_LEFT, 6, 0, true},
- { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0, true},
+ { "&&", 2, opid2('&','&'), ASSOC_LEFT, 5, 0, true},
- { "||", 2, opid2('|','|'), ASSOC_LEFT, 4, 0, true},
+ { "||", 2, opid2('|','|'), ASSOC_LEFT, 4, 0, true},
- { "?", 3, opid2('?',':'), ASSOC_RIGHT, 3, 0, true},
+ { "?", 3, opid2('?',':'), ASSOC_RIGHT, 3, 0, true},
- { "=", 2, opid1('='), ASSOC_RIGHT, 2, 0, false},
- { "+=", 2, opid2('+','='), ASSOC_RIGHT, 2, 0, false},
- { "-=", 2, opid2('-','='), ASSOC_RIGHT, 2, 0, false},
- { "*=", 2, opid2('*','='), ASSOC_RIGHT, 2, 0, false},
- { "/=", 2, opid2('/','='), ASSOC_RIGHT, 2, 0, false},
- { "%=", 2, opid2('%','='), ASSOC_RIGHT, 2, 0, false},
- { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2, 0, false},
- { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2, 0, false},
- { "&=", 2, opid2('&','='), ASSOC_RIGHT, 2, 0, false},
- { "^=", 2, opid2('^','='), ASSOC_RIGHT, 2, 0, false},
- { "|=", 2, opid2('|','='), ASSOC_RIGHT, 2, 0, false},
+ { "=", 2, opid1('='), ASSOC_RIGHT, 2, 0, false},
+ { "+=", 2, opid2('+','='), ASSOC_RIGHT, 2, 0, false},
+ { "-=", 2, opid2('-','='), ASSOC_RIGHT, 2, 0, false},
+ { "*=", 2, opid2('*','='), ASSOC_RIGHT, 2, 0, false},
+ { "/=", 2, opid2('/','='), ASSOC_RIGHT, 2, 0, false},
+ { "%=", 2, opid2('%','='), ASSOC_RIGHT, 2, 0, false},
+ { ">>=", 2, opid3('>','>','='), ASSOC_RIGHT, 2, 0, false},
+ { "<<=", 2, opid3('<','<','='), ASSOC_RIGHT, 2, 0, false},
+ { "&=", 2, opid2('&','='), ASSOC_RIGHT, 2, 0, false},
+ { "^=", 2, opid2('^','='), ASSOC_RIGHT, 2, 0, false},
+ { "|=", 2, opid2('|','='), ASSOC_RIGHT, 2, 0, false},
- { ":", 0, opid2(':','?'), ASSOC_RIGHT, 1, 0, false},
+ { ":", 0, opid2(':','?'), ASSOC_RIGHT, 1, 0, false},
- { ",", 2, opid1(','), ASSOC_LEFT, 0, 0, false}
+ { ",", 2, opid1(','), ASSOC_LEFT, 0, 0, false}
};
static const oper_info fte_operators[] = {