Esempio n. 1
0
bool binspector_parser_t::is_scope_or_statement()
{
    return is_conditional_scope() ||
           is_enum_scope() ||
           is_sentry_scope() ||
           is_statement();
}
Esempio n. 2
0
void test_block_as_assertions_list(Block* block, std::string const& contextStr)
{
    if (has_static_errors(block)) {
        std::cout << "Static error " << contextStr << ":" << std::endl;
        print_static_errors_formatted(block, std::cout);
        declare_current_test_failed();
        return;
    }

    std::stringstream checkInvariantsOutput;
    if (!block_check_invariants_print_result(block, checkInvariantsOutput)) {
        std::cout << "Failed invariant " << contextStr << std::endl;
        std::cout << checkInvariantsOutput.str() << std::endl;
        declare_current_test_failed();
        return;
    }

    Stack context;
    evaluate_block(&context, block);

    if (context.errorOccurred) {
        std::cout << "Runtime error " << contextStr << std::endl;
        print_error_stack(&context, std::cout);
        declare_current_test_failed();
        return;
    }

    int boolean_statements_found = 0;
    for (int i=0; i < block->length(); i++) {
        Term* term = block->get(i);
        if (!is_statement(term))
            continue;

        if (!is_bool(term_value(term)))
            continue;

        boolean_statements_found++;

        if (!as_bool(term_value(term))) {
            std::cout << "Assertion failed " << contextStr << std::endl;
            std::cout << "failed: " << get_term_source_text(term) << std::endl;
            declare_current_test_failed();
            return;
        }
    }

    if (boolean_statements_found == 0) {
        std::cout << "No boolean statements found " << contextStr << std::endl;
        declare_current_test_failed();
        return;
    }
}
Esempio n. 3
0
void Block__statements(VM* vm)
{
    Block* block = (Block*) circa_block(vm->input(0));
    if (block == NULL)
        return vm->throw_str("NULL block");

    Value* out = vm->output();

    circa_set_list(out, 0);

    for (int i=0; i < block->length(); i++)
        if (is_statement(block->get(i)))
            circa_set_term(circa_append(out), (caTerm*) block->get(i));
}
Esempio n. 4
0
void Branch__statements(caStack* stack)
{
    Branch* branch = (Branch*) circa_branch(circa_input(stack, 0));
    if (branch == NULL)
        return circa_output_error(stack, "NULL branch");

    caValue* out = circa_output(stack, 0);

    circa_set_list(out, 0);

    for (int i=0; i < branch->length(); i++)
        if (is_statement(branch->get(i)))
            circa_set_term(circa_append(out), (caTerm*) branch->get(i));
}
Esempio n. 5
0
void write_statement(CppWriter& writer, Term* term)
{
    if (is_comment(term)) {
        if (term->stringProp("comment") != "") {
            writer.write("//");
            writer.write(term->stringProp("comment"));
        }
    } else if (is_function(term)) {
        write_function(writer, term);
    } else if (is_statement(term)) {
        if (term->name != "") {
            write_type_name(writer, term->type);
            writer.write(" ");
            writer.write(term->name);
            writer.write(" = ");
        }
        write_expression(writer, term);
        writer.write(";");
    }
}
Esempio n. 6
0
void TRANS_tree(bool check_statement, TRANS_TREE **result, int *count)
{
	#ifdef DEBUG
	int i;
	#endif

	tree_length = 0;
	current = JOB->current;
	level = 0;

	TRY
	{
		analyze_expr(0, RS_NONE);
		JOB->current = current;
	}
	CATCH
	{
		JOB->current = current;
		PROPAGATE();
	}
	END_TRY

	#ifdef DEBUG
		printf("\n");
		for (i = 0; i < tree_length; i++)
		{
			printf("[%d] ", i);
			READ_dump_pattern(&tree[i]);
		}
	#endif

	if (check_statement && (!is_statement()))
		THROW("This expression cannot be a statement");

	if (result)
	{
		ALLOC(result, sizeof(PATTERN) * tree_length);
		memcpy(*result, tree, sizeof(PATTERN) * tree_length);
		*count = tree_length;
	}
}
Esempio n. 7
0
/*
 * compute_ir
 * Purpose: given a parse tree with symbol table entries,
 *          compute IR instructions
 *
 * Parameters:
 *  n - Node * - the current node in the parse tree during traversal
 *
 * Returns:
 *  None
 *
 * Side Effects:
 *  Appends IrNodes to the master IrList. Allocates heap memory.
 */
void compute_ir(Node *n, IrList *irl) {
    IrNode *irn1, *irn2, *irn3;
    Node *child1, *child2;
    if (n == NULL) {
        return;
    }

    if (is_statement(n)) {
        reg_idx = 0;
    }

    /* for expressions we will update the parse tree node n:
     * type: assume SIGNED_INT unless node is a symbol with a known type
     * lvalue: yes or no
     * location: register index of its result
     */
    switch (n->n_type) {
        case FUNCTION_DEFINITION:
            /* first child: function def spec */
            /* recurse over it to obtain the function symbol */
            compute_ir(n->children.child1, irl);
            /* now we have appended a BEGIN_PROC node to ir_list */
            /* it has the function symbol */
            cur_end_proc_label = irn_label(LABEL, label_idx++);
            irn2 = irn_function(END_PROC, ir_list->tail->s);
            /* second child: compound statement */
            /* recurse over it to obtain IR nodes for the function body */
            compute_ir(n->children.child2, irl);
            /* finally end the proc */
            append_ir_node(cur_end_proc_label, irl);
            append_ir_node(irn2, irl);
            break;
        case FUNCTION_DEF_SPEC:
            is_function_def_spec = TRUE;
            /* only need to recurse over the declarator */
            /* to go get the function symbol */
            compute_ir(n->children.child2, irl);
            is_function_def_spec = FALSE;
            break;
        case POINTER_DECLARATOR:
            if (is_function_def_spec) {
                compute_ir(n->children.child2, irl);
            }
        case FUNCTION_DECLARATOR:
            if (is_function_def_spec) {
                compute_ir(n->children.child1, irl);
            }
            break;
        case SIMPLE_DECLARATOR:
            if (is_function_def_spec) {
                irn1 = irn_function(BEGIN_PROC, n->st_entry);
                append_ir_node(irn1, irl);
            }
            break;
        case ASSIGNMENT_EXPR:
            child1 = n->children.child1;
            child2 = n->children.child2;
            compute_ir(child1, irl);
            compute_ir(child2, irl);
            irn1 = irn_store(STORE_WORD_INDIRECT,
                    child2->expr->location, child1->expr->location);
            append_ir_node(irn1, irl);
            break;
        case BINARY_EXPR:
            child1 = n->children.child1;
            child2 = n->children.child2;
            compute_ir(child1, irl);
            compute_ir(child2, irl);

            n->expr->lvalue = FALSE;
            n->expr->location = reg_idx++;
            irn1 = irn_binary_expr(LOG_OR, n->expr->location,
                                child2->expr->location, child1->expr->location);
            append_ir_node(irn1, irl);
            break;
        case IDENTIFIER_EXPR:
            n->expr->lvalue = TRUE;
            n->expr->location = reg_idx++;
            if (is_function_call && !is_function_argument) {
                irn1 = irn_function(BEGIN_CALL, n->st_entry);
                append_ir_node(irn1, irl);
            } else {
                irn1 = irn_load(LOAD_ADDRESS,
                        n->expr->location, NO_ARG, n->st_entry);
                append_ir_node(irn1, irl);
                if (is_function_argument) {
                    /* TODO: support more than 1 argument */
                    irn2 = irn_load(LOAD_WORD_INDIRECT,
                                reg_idx, n->expr->location, NULL);
                    irn3 = irn_param(PARAM, 0, reg_idx++);
                    append_ir_node(irn2, irl);
                    append_ir_node(irn3, irl);
                }
            }
            break;
        case NUMBER_CONSTANT:
            n->expr->lvalue = FALSE;
            n->expr->location = reg_idx++;
            irn1 = irn_load(LOAD_CONSTANT,
                    n->expr->location, n->data.num, NULL);
            append_ir_node(irn1, irl);
            if (is_function_argument) {
                irn1 = irn_param(PARAM, 0, n->expr->location);
                append_ir_node(irn1, irl);
            }
            break;
        case RETURN_STATEMENT:
            compute_ir(n->children.child1, irl);
            if (n->children.child1 != NULL) {
                if (n->children.child1->expr->lvalue) {
                    irn1 = irn_load(LOAD_WORD_INDIRECT,
                            reg_idx, n->children.child1->expr->location, NULL);
                    append_ir_node(irn1, irl);
                    irn2 = irn_statement(RETURN_FROM_PROC,
                                reg_idx++, cur_end_proc_label);
                } else {
                    irn2 = irn_statement(RETURN_FROM_PROC,
                        n->children.child1->expr->location, cur_end_proc_label);
                }
                append_ir_node(irn2, irl);
            } else {
                irn1 = irn_statement(RETURN_FROM_PROC,
                                        NO_ARG, cur_end_proc_label);
                append_ir_node(irn1, irl);
            }

            break;
        case FUNCTION_CALL:
            is_function_call = TRUE;
            /* get func symbol to get name and parameters */
            /* will append BEGIN_CALL node */
            compute_ir(n->children.child1, irl);
            function_symbol = ir_list->tail->s;
            /* arguments */
            is_function_argument = TRUE;
            compute_ir(n->children.child2, irl);
            is_function_argument = FALSE;
            irn2 = irn_function(CALL, function_symbol);
            irn3 = irn_function(END_CALL, function_symbol);
            append_ir_node(irn2, irl);
            append_ir_node(irn3, irl);
            is_function_call = FALSE;
            break;
        default:
            compute_ir_pass_through(n, irl);
    }
}