Пример #1
0
int postfix_expression(void) {
	if ( primary_expression() ) {
	} else if( postfix_expression() ) {
		if( lookaheadT.type == LBRACKET ) {
			match(LBRACKET);
			expression();
			match(RBRACKET);
		} else if( lookaheadT.type == LPAREN ) {
			match(LPAREN);
			if( argument_expression_list() ) {
			}
			match(RPAREN);
		} else if( lookaheadT.type == PERIOD ) {
			match(PERIOD);
			match(ID);
		} else if( lookaheadT.type == PTR_OP ) {	
			match(PTR_OP);
			match(ID);
		} else if( lookaheadT.type == INC_OP ) {
			match(INC_OP);
		} else if( lookaheadT.type == DEC_OP ) {	
			match(DEC_OP);
		} else {
			abort();
		}
	} else {
		abort();
	}
}
Пример #2
0
/**
一元表达式
<unary_expression>::=<postfix_expression>
                    |<TK_AND><unary_expression>
                    |<TK_STAE><unary_expression>
                    |<TK_PLUS><unary_expression>
                    |<TK_MINUS><unary_expression>
                    |<sizeof_expression>

*/
void unary_expression(){
    switch(token){
        case TK_AND:case TK_STAR:case TK_PLUS:case TK_MINUS:
            get_token();
            unary_expression();
            break;
        case KW_SIZEOF:
            sizeof_expression();
            break;
        default:
            postfix_expression();
            break;
    }
}
Пример #3
0
// Parse a unary expression.
//
//    unary-expression:
//      postfix-expression
//      '!' unary-expression
//      '-' unary-expression
//      '+' unary-expression
//      '~' unary-expression
//
Expr&
Parser::unary_expression()
{
  if (Token tok = match_if(tk::bang_tok)) {
    Expr& e = unary_expression();
    return on_logical_not_expression(tok, e);
  } else if (Token tok = match_if(tk::minus_tok)) {
    Expr& e = unary_expression();
    return on_neg_expression(tok, e);
  } else if (Token tok = match_if(tk::plus_tok)) {
    Expr& e = unary_expression();
    return on_pos_expression(tok, e);
  } else if (Token tok = match_if(tk::caret_tok)) {
    Expr& e = unary_expression();
    return on_compl_expression(tok, e);
  } else {
    return postfix_expression();
  }
}
Пример #4
0
int unary_expression(void) {
	if( postfix_expression() ) {
	} else if( lookaheadT.type == INC_OP ) {	
		match(INC_OP);
		unary_expression();
	} else if( lookaheadT.type == DEC_OP ) {
		match(DEC_OP);
		unary_expression();
	} else if( unary_operator() ) {
		cast_expression();
	} else if( lookaheadT.type == SIZEOF ) {
		match(SIZEOF);
		if( unary_expression() ) {
		} else if( lookaheadT.type == LPAREN ) {
			match(LPAREN);
			type_name();
			match(RPAREN);
		} else {
			abort();
		}
	} else {
		abort();
	}
}
Пример #5
0
static sl_node_base_t*
expression(sl_parse_state_t* ps)
{
    return postfix_expression(ps);
}
Пример #6
0
static struct block *unary_expression(struct block *block)
{
    struct var value;

    switch (peek().token) {
    case '&':
        consume('&');
        block = cast_expression(block);
        block->expr = eval_addr(block, block->expr);
        break;
    case '*':
        consume('*');
        block = cast_expression(block);
        block->expr = eval_deref(block, block->expr);
        break;
    case '!':
        consume('!');
        block = cast_expression(block);
        block->expr = eval_expr(block, IR_OP_EQ, var_int(0), block->expr);
        break;
    case '~':
        consume('~');
        block = cast_expression(block);
        block->expr = eval_expr(block, IR_NOT, block->expr);
        break;
    case '+':
        consume('+');
        block = cast_expression(block);
        block->expr.lvalue = 0;
        break;
    case '-':
        consume('-');
        block = cast_expression(block);
        block->expr = eval_expr(block, IR_OP_SUB, var_int(0), block->expr);
        break;
    case SIZEOF: {
        struct typetree *type;
        struct block *head = cfg_block_init(), *tail;
        consume(SIZEOF);
        if (peek().token == '(') {
            switch (peekn(2).token) {
            case FIRST(type_name):
                consume('(');
                type = declaration_specifiers(NULL);
                if (peek().token != ')') {
                    type = declarator(type, NULL);
                }
                consume(')');
                break;
            default:
                tail = unary_expression(head);
                type = (struct typetree *) tail->expr.type;
                break;
            }
        } else {
            tail = unary_expression(head);
            type = (struct typetree *) tail->expr.type;
        }
        if (is_function(type)) {
            error("Cannot apply 'sizeof' to function type.");
        }
        if (!size_of(type)) {
            error("Cannot apply 'sizeof' to incomplete type.");
        }
        block->expr = var_int(size_of(type));
        break;
    }
    case INCREMENT:
        consume(INCREMENT);
        block = unary_expression(block);
        value = block->expr;
        block->expr = eval_expr(block, IR_OP_ADD, value, var_int(1));
        block->expr = eval_assign(block, value, block->expr);
        break;
    case DECREMENT:
        consume(DECREMENT);
        block = unary_expression(block);
        value = block->expr;
        block->expr = eval_expr(block, IR_OP_SUB, value, var_int(1));
        block->expr = eval_assign(block, value, block->expr);
        break;
    default:
        block = postfix_expression(block);
        break;
    }

    return block;
}
Пример #7
0
struct Symbol* postfix_expression(struct PostfixExpression* node, struct Symbol** orig_symbol)
{
    struct Symbol* symbol, *ref, *newSymbol = 0, *symbol1;
    switch (node->type) {
        case 0:
            return primary_expression(node->primaryExpression, orig_symbol);
        case 1:
            symbol = load_symbol(postfix_expression(node->postfixExpression, orig_symbol));
            test_referenceable(symbol);
            ref = load_symbol(expression_func(node->expression));
            *orig_symbol = new_symbol("", symbol->storage, 0, symbol->specifier, symbol->stars - 1, 0, 0);
            if (symbol->stars == 1)
                (*orig_symbol)->specifier = 32;
            ADDSTRING("  ");
            code_gen_symbol('%', *orig_symbol);
            ADDSTRING(" = getelementptr inbounds ");
            code_gen_type_specifier(symbol->specifier, 0, symbol->length, symbol->stars);
            ADDSTRING(" ");
            if (symbol->depth == 0)
                code_gen_symbol('@', symbol);
            else
                code_gen_symbol('%', symbol);
            if (symbol->length)
                ADDSTRING(", i32 0");
            ADDSTRING(", ");
            test_integer(ref);
            code_gen_type_specifier(ref->specifier, 0, 0, 0);
            ADDSTRING(" ");
            code_gen_symbol('%', ref);
            ADDSTRING("\n");
            ADDSTRING("  ");
            newSymbol = new_symbol("", 0, 2, symbol->specifier, symbol->stars - 1, 0, 0);
            newSymbol->reference = *orig_symbol;
            code_gen_symbol('%', newSymbol);
            ADDSTRING(" = load ");
            code_gen_type_specifier(symbol->specifier, 0, (*orig_symbol)->length, symbol->stars);
            ADDSTRING(" ");
            code_gen_symbol('%', *orig_symbol);
            ADDSTRING(", align ");
            if (newSymbol->stars)
            {
                if (PTR_LENGTH == 8)
                {
                    ADDSTRING("8\n");
                }
                else
                {
                    ADDSTRING("4\n");
                }
            }
            else
            {
                int len = len_gen_type_specifier(newSymbol->specifier);
                sprintf(buf, "%d\n", len);
                ADDSTRING(buf);
            }
            return newSymbol;
        case 2:
        case 3:
            symbol = postfix_expression(node->postfixExpression, orig_symbol);
            test_functionable(symbol);
            if (node->type == 3)
            {
                argument_expression_list(node->argumentExpressionList, symbol->parameterlist);
            }
            if (!(symbol->specifier & 0x02)) {
                newSymbol = new_symbol("", symbol->storage, 2, symbol->specifier, symbol->stars, 0, symbol->length);
                ADDSTRING("  ");
                code_gen_symbol('%', newSymbol);
                ADDSTRING(" = call ");
            } else {
                ADDSTRING("  call ");
                newSymbol = symbol;
            }
            if (strcmp(symbol->name, "free") != 0)
            {
                code_gen_type_specifier(symbol->specifier, 0, symbol->length, symbol->stars);
                ADDSTRING(" ");
                if (strcmp(symbol->name, "printf") == 0 || strcmp(symbol->name, "scanf") == 0)
                {
                    ADDSTRING("(i8*, ...)* ");
                }
                code_gen_symbol('@', symbol);
            }
            else
            {
                ADDSTRING("i32 (");
                code_gen_type_specifier(symbol_arg[0]->specifier, 0, 0, symbol_arg[0]->stars);
                ADDSTRING(", ...)* bitcast (i32 (...)* @free to i32 (");
                code_gen_type_specifier(symbol_arg[0]->specifier, 0, 0, symbol_arg[0]->stars);
                ADDSTRING(", ...)*)");
            }
            ADDSTRING("(");
            if (node->type == 3)
            {
                pop_arg();
            }
            ADDSTRING(")\n");
            *orig_symbol = newSymbol;
            return newSymbol;
        case 4:
        case 5:
            // to do: struct
            return 0;
        case 6:
        case 7:
            symbol = load_symbol(postfix_expression(node->postfixExpression, orig_symbol));
            test_changeable(*orig_symbol);
            symbol1 = new_symbol("", symbol->storage, 2, symbol->specifier, symbol->stars, 0, symbol->length);
            ADDSTRING("  ");
            code_gen_symbol('%', symbol1);
            ADDSTRING(" = ");
            if ((symbol->specifier & (3 << 6)) > 0)
            {
                ADDSTRING("f");
            }
            if (node->type == 6)
            {
                ADDSTRING("add ");
            }
            else
            {
                ADDSTRING("sub ");
            }
            code_gen_type_specifier(symbol->specifier,1,symbol->length, symbol->stars);
            ADDSTRING(" ");
            code_gen_symbol('%', symbol);
            ADDSTRING(", ");
            if ((symbol->specifier & (3 << 6)) > 0)
            {
                ADDSTRING("1.000000e+00\n");
            }
            else
            {
                ADDSTRING("1\n");
            }
            ADDSTRING("  store ");
            code_gen_type_specifier(symbol->specifier,0,symbol->length, symbol->stars);
            ADDSTRING(" ");
            code_gen_symbol('%', symbol1);
            ADDSTRING(", ");
            code_gen_type_specifier(symbol->specifier,0,symbol->length, symbol->stars);
            ADDSTRING("* ");
            code_gen_symbol('%', *orig_symbol);
            ADDSTRING(", align ");
            int l = len_gen_type_specifier(symbol->specifier);
            sprintf(buf, "%d", l);
            ADDSTRING(buf);
            ADDSTRING("\n");
            return symbol;
        default:
            return 0;
    }
}
Пример #8
0
struct Symbol* unary_expression(struct UnaryExpression* node, struct Symbol** orig_symbol)
{
    int len;
    struct Symbol *symbol, *symbol1 = NULL;
    switch (node->type) {
        case 0:
            return postfix_expression(node->postfixExpression, orig_symbol);
        case 1:
        case 2:
            symbol = load_symbol(unary_expression(node->unaryExpression, orig_symbol));
            test_changeable(*orig_symbol);
            if (node->type == 2)
                symbol1 = test_calculable(symbol, '-');
            else
                symbol1 = test_calculable(symbol, '+');
            if (symbol1)
                return symbol1;
            ADDSTRING("  ");
            symbol1 = new_symbol("", symbol->storage, symbol->qualifier, symbol->specifier, symbol->stars - 1, 0, symbol->length);
            code_gen_symbol('%', symbol1);
            ADDSTRING(" = ");
            if ((symbol->specifier & (3 << 6)) > 0)
            {
                ADDSTRING("f");
            }
            if (node->type == 1)
            {
                ADDSTRING("add ");
            }
            else
            {
                ADDSTRING("sub ");
            }
            code_gen_type_specifier(symbol->specifier,1,symbol->length, symbol->stars);
            ADDSTRING(" ");
            code_gen_symbol('%', symbol);
            ADDSTRING(", ");
            if ((symbol->specifier & (3 << 6)) > 0)
            {
                ADDSTRING("1.000000e+00\n");
            }
            else
            {
                ADDSTRING("1\n");
            }
            ADDSTRING("  store ");
            code_gen_type_specifier(symbol->specifier,0,symbol->length, symbol->stars);
            ADDSTRING(" ");
            code_gen_symbol('%', symbol1);
            ADDSTRING(", ");
            code_gen_type_specifier(symbol->specifier,0,symbol->length, symbol->stars);
            ADDSTRING("* ");
            code_gen_symbol('%', *orig_symbol);
            ADDSTRING(", align ");
            int l = len_gen_type_specifier(symbol->specifier);
            sprintf(buf, "%d", l);
            ADDSTRING(buf);
            ADDSTRING("\n");
            *orig_symbol = 0;
            break;
            
        case 3:
            symbol = cast_expression(node->castExpression);
            switch (node->unaryOperator) {
                case 1:
                    test_pointable(symbol);
                    symbol = symbol->reference;
                    *orig_symbol = 0;
                    symbol1 = new_symbol("", symbol->storage, 2, symbol->specifier, symbol->stars + 1, 0, symbol->length);
                    ADDSTRING("  ");
                    code_gen_symbol('%', symbol1);
                    ADDSTRING(" = getelementptr inbounds ");
                    code_gen_type_specifier(symbol->specifier, 0, symbol->length, symbol1->stars);
                    ADDSTRING(" ");
                    code_gen_symbol('%', symbol);
                    ADDSTRING(", ");
                    ADDSTRING(PTR_LEN_TYPE);
                    ADDSTRING(" 0\n");
                    break;
                case 2:
                    test_referenceable(symbol);
                    symbol = load_symbol(symbol);
                    symbol1 = new_symbol("", symbol->storage, 0, symbol->specifier, symbol->stars - 1, 0, symbol->length);
                    *orig_symbol = symbol1;
                    if (symbol1->stars == 0)
                        symbol1->specifier = 32;
                    symbol1->reference = symbol;
                    ADDSTRING("  ");
                    code_gen_symbol('%', symbol1);
                    ADDSTRING(" = load ");
                    code_gen_type_specifier(symbol->specifier, 0, symbol->length, symbol->stars);
                    ADDSTRING(" ");
                    code_gen_symbol('%', symbol);
                    ADDSTRING(" ");
                    code_gen_symbol('%', symbol);
                    ADDSTRING(", align 8\n");
                    break;
                case 3:
                    test_regular(symbol);
                    symbol1 = symbol;
                    *orig_symbol = 0;
                    break;
                case 4:
                    test_regular(symbol);
                    symbol = load_symbol(symbol);
                    symbol1 = test_calculable(symbol, 'n');
                    if (symbol1)
                    {
                        return symbol1;
                    }
                    symbol1 = new_symbol("", symbol->storage, 2, symbol->specifier, symbol->stars, 0, 0);
                    *orig_symbol = symbol;
                    ADDSTRING("  ");
                    code_gen_symbol('%', symbol1);
                    if ((symbol->specifier & (3 << 6)) != 0)
                    {
                        ADDSTRING(" = f");
                    }
                    else
                    {
                        ADDSTRING(" = ");
                    }
                    ADDSTRING("sub ");
                    code_gen_type_specifier(symbol->specifier,1, symbol->length, symbol->stars);
                    ADDSTRING(" ");
                    if ((symbol->specifier & (3 << 6)) != 0)
                    {
                        ADDSTRING("0.000000e+00");
                    }
                    else
                    {
                        ADDSTRING("0");
                    }
                    ADDSTRING(", ");
                    code_gen_symbol('%', symbol);
                    ADDSTRING("\n");
                    *orig_symbol = 0;
                    break;
                case 5:
                    test_integer(symbol);
                    symbol = load_symbol(symbol);
                    symbol1 = test_calculable(symbol, '~');
                    if (symbol1)
                    {
                        return symbol1;
                    }
                    *orig_symbol = 0;
                    symbol = cast_symbol(symbol, 16, 0);
                    symbol1 = new_symbol("", symbol->storage, symbol->qualifier, symbol->specifier, symbol->stars, 0, symbol->length);
                    ADDSTRING("  ");
                    code_gen_symbol('%', symbol1);
                    ADDSTRING(" = xor ");
                    code_gen_type_specifier(symbol->specifier,0, symbol->length, symbol->stars);
                    ADDSTRING(" ");
                    code_gen_symbol('%', symbol);
                    ADDSTRING(", -1");
                    ADDSTRING("\n");
                    break;
                case 6:
                    *orig_symbol = 0;
                    symbol1 = test_calculable(symbol, '!');
                    symbol = load_symbol(symbol);
                    if (symbol1)
                    {
                        return symbol1;
                    }
                    symbol1 = new_symbol("0", 0, 2, 16, 0, 2, 0);
                    symbol1 = equality_symbol(symbol, symbol1, 2, 1);
                    break;
                default:
                    break;
            }
            break;
            
        case 4:
            *orig_symbol = 0;
            symbol = unary_expression(node->unaryExpression, orig_symbol);
            if (symbol->stars > (symbol->length > 0))
                len = PTR_LENGTH;
            else
            {
                len = len_gen_type_specifier(symbol->specifier);
                if (symbol->length)
                    len *= symbol->length;
            }
            sprintf(buf, "%d", len);
            symbol1 = new_symbol(buf, 0, 2, 4, 0, 2, 0);
            break;
            
        case 5:
            *orig_symbol = 0;
            len = len_gen_type_name(node->typeName);
            sprintf(buf, "%d", len);
            symbol1 = new_symbol(buf, 0, 2, 4, 0, 2, 0);
            break;
            
        default:
            break;
    }
    return symbol1;
}