Example #1
0
static expr *expr_new_uop(e_op op, expr *sub)
{
	expr *e = expr_new(E_UOP);
	e->bits.uop.e  = sub;
	e->bits.uop.op = op;
	return e;
}
Example #2
0
static void
workspace_link( Workspace *ws, Workspacegroup *wsg, const char *name )
{
	Workspaceroot *wsr = wsg->wsr;

	Symbol *sym;

#ifdef DEBUG
	printf( "workspace_link: naming ws %p as %s\n", ws, name );
#endif /*DEBUG*/

	sym = symbol_new_defining( wsr->sym->expr->compile, name );

	ws->sym = sym;
	sym->type = SYM_WORKSPACE;
	sym->ws = ws;
	sym->expr = expr_new( sym );
	(void) compile_new( sym->expr );
	symbol_made( sym );
	iobject_set( IOBJECT( ws ), name, NULL );

	ws->local_kitg = toolkitgroup_new( ws->sym );
	g_object_ref( G_OBJECT( ws->local_kitg ) );
	iobject_sink( IOBJECT( ws->local_kitg ) );
}
Example #3
0
static expr *expr_new_top(expr *e, expr *l, expr *r)
{
	expr *top = expr_new(E_TOP);
	top->bits.top.e = e;
	top->bits.top.if_true  = l;
	top->bits.top.if_false = r;
	return top;
}
Example #4
0
static expr *expr_op(e_op op, expr *l, expr *r)
{
	expr *e = expr_new(E_OP);
	e->bits.op.lhs = l;
	e->bits.op.rhs = r;
	e->bits.op.op  = op;
	return e;
}
Example #5
0
struct process_parameter_results process_address(struct ast_node_address* param)
{
    struct process_parameter_results result;
    struct register_mapping* registr;
    bstring btmp = NULL;
    result.v_raw = NULL;

    if (param->value != NULL)
        btmp = expr_representation(param->value);

    if (param->bracketed && param->added)
    {
        // This is of the form [0x1000+I].
        registr = get_register_by_name_next(param->addcmpt);

        if (registr == NULL)
        {
            // Attempt to use a label in square brackets.  Convert this
            // to an expression and then reinvoke ourselves with the
            // evaluated value.
            param->value = expr_new(expr_new_label(bautofree(bfromcstr(param->addcmpt))), EXPR_OP_ADD, param->value);
            param->addcmpt = "";
            param->added = 0;
            param->bracketed = 0;

            bdestroy(btmp);
            return process_address(param);
        }
        else if (registr->value == VALUE_NEXT_UNSUPPORTED)
        {
            // Attempt to use a register in brackets that can't be.
            printd(LEVEL_VERBOSE, "\n");
            dhalt(ERR_NEXTED_REGISTER_UNSUPPORTED, param->addcmpt);
        }

        printd(LEVEL_VERBOSE, "[%s+%s]", btmp->data, registr->name);
        result.v = registr->value;
        result.v_extra = param->value;
        result.v_extra_used = true;
        result.v_label = NULL;
    }
    else
    {
        // This is either the form 0x1000 or [0x1000].
        if (param->bracketed)
        {
            printd(LEVEL_VERBOSE, "[%s]", btmp->data);
            result.v = NXT;
        }
        else
        {
            printd(LEVEL_VERBOSE, "%s", btmp->data);
            result.v = NXT_LIT;
        }

        result.v_extra = param->value;
        result.v_extra_used = true;
        result.v_label = NULL;
    }

    if (btmp != NULL)
        bdestroy(btmp);

    return result;
}
Example #6
0
static expr *expr_num(expr_n num)
{
	expr *e = expr_new(E_NUM);
	e->bits.num = num;
	return e;
}
Example #7
0
static expr *expr_ident(void)
{
	expr *e = expr_new(E_IDENT);
	return e;
}
Example #8
0
struct expr *eval_binary_op(struct evaluator_state *state, struct expr* e, struct env_node *env) {
    assert(e->type == EXPR_BINARY);
    switch (e->binary_op.op) {
    case BIN_MEMBER: {
        // if left is union:
        //     return eval(x.right_ident for x in left_union)
        // else:
        //     return lookup(left_obj, right_ident)
        assert(e->binary_op.right->type == EXPR_IDENT);
        struct expr *left = eval_footprint_expr(state, e->binary_op.left, env);
        if (left->type == EXPR_UNION) {
            char *loop_var_name = new_ident_not_in(env, "loop_var");

            struct expr *loop_var_ident = expr_new_with(e->direction, EXPR_IDENT);
            loop_var_ident->ident = loop_var_name;

            struct expr *loop_body = expr_new();
            memcpy(loop_body, e, sizeof(struct expr));
            loop_body->binary_op.left = loop_var_ident;

            struct expr *loop = expr_new_with(e->direction, EXPR_FOR);
            loop->for_loop.body = loop_body;
            loop->for_loop.ident = loop_var_name;
            loop->for_loop.over = left;

            return eval_footprint_expr(state, loop, env);
        } else if (left->type == EXPR_OBJECT) {
            return lookup_in_object(&left->object, e->binary_op.right->ident, e->direction);
        } else {
            assert(false);
        }
    }
    break;
    case BIN_APP: {
        struct expr *left = eval_footprint_expr(state, e->binary_op.left, env);
        struct expr *right = e->binary_op.right;
        assert(left->type == EXPR_FUNCTION);
        assert(right->type == EXPR_FUNCTION_ARGS);
        struct function func = left->func;
        struct env_node *function_env = env;
        struct string_node *current_arg_name = func.args;
        struct union_node *current_arg_value = e->binary_op.right->unioned;
        while (current_arg_value != NULL) {
            assert(current_arg_name != NULL); // not too many arguments
            struct expr *e = eval_footprint_expr(state, current_arg_value->expr, env);
            function_env = env_new_with(current_arg_name->value, e, function_env);
            current_arg_name = current_arg_name->next;
            current_arg_value = current_arg_value->next;
        }
        assert(current_arg_name == NULL); // not too few arguments
        function_env = env_new_with(func.name, construct_function(func, e->direction), function_env);
        struct expr *new_expr = expr_clone(func.expr);
        set_direction_recursive(new_expr, e->direction);
        return eval_footprint_expr(state, new_expr, function_env);
    }
    break;
    default: {
        int64_t left, right;
        _Bool left_success, right_success;
        struct expr *partial_left, *partial_right;
        left_success = eval_to_value(state, e->binary_op.left, env, &partial_left, &left);
        right_success = eval_to_value(state, e->binary_op.right, env, &partial_right, &right);
        if (!left_success || !right_success) {
            // cache miss, state modified
            struct expr *new_expr = expr_clone(e);
            if (left_success) {
                new_expr->binary_op.left = construct_value(left, e->direction);
            } else {
                new_expr->binary_op.left = partial_left;
            }
            if (right_success) {
                new_expr->binary_op.right = construct_value(right, e->direction);
            } else {
                new_expr->binary_op.right = partial_right;
            }
            return new_expr;
        }
        switch (e->binary_op.op) {
        case BIN_GT: {
            return construct_value(left > right ? 1 : 0, e->direction);
        }
        break;
        case BIN_LT: {
            return construct_value(left < right ? 1 : 0, e->direction);
        }
        break;
        case BIN_GTE: {
            return construct_value(left >= right ? 1 : 0, e->direction);
        }
        break;
        case BIN_LTE: {
            return construct_value(left <= right ? 1 : 0, e->direction);
        }
        break;
        case BIN_EQ: {
            return construct_value(left == right ? 1 : 0, e->direction);
        }
        break;
        case BIN_NE: {
            return construct_value(left != right ? 1 : 0, e->direction);
        }
        break;
        case BIN_AND: {
            return construct_value(!!left && !!right ? 1 : 0, e->direction);
        }
        break;
        case BIN_OR: {
            return construct_value(!!left || !!right ? 1 : 0, e->direction);
        }
        break;
        case BIN_ADD: {
            return construct_value(left + right, e->direction);
        }
        break;
        case BIN_SUB: {
            return construct_value(left - right, e->direction);
        }
        break;
        case BIN_MUL: {
            return construct_value(left * right, e->direction);
        }
        break;
        case BIN_DIV: {
            return construct_value(left / right, e->direction);
        }
        break;
        case BIN_MOD: {
            return construct_value(left % right, e->direction);
        }
        break;
        case BIN_SHL: {
            return construct_value(left << right, e->direction);
        }
        break;
        case BIN_SHR: {
            return construct_value(left >> right, e->direction);
        }
        break;
        case BIN_BITAND: {
            return construct_value(left & right, e->direction);
        }
        break;
        case BIN_BITOR: {
            return construct_value(left | right, e->direction);
        }
        break;
        case BIN_BITXOR: {
            return construct_value(left ^ right, e->direction);
        }
        break;
        default:
            assert(false);
            break;
        }
    }
    break;
    }
}