Now && and || short circuiting is done in obj_lower
This commit is contained in:
@@ -141,7 +141,7 @@ fun adt_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
}
|
||||
}
|
||||
}
|
||||
run_on_tree(helper_before, empty_pass_half, syntax_ast_pair.second)
|
||||
run_on_tree(helper_before, empty_pass_second_half, syntax_ast_pair.second)
|
||||
})
|
||||
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
|
||||
var second_helper = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
|
||||
@@ -198,7 +198,7 @@ fun adt_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
}
|
||||
}
|
||||
}
|
||||
run_on_tree(second_helper, empty_pass_half, syntax_ast_pair.second)
|
||||
run_on_tree(second_helper, empty_pass_second_half, syntax_ast_pair.second)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -657,23 +657,7 @@ obj c_generator (Object) {
|
||||
))
|
||||
return code_triple("(") + generate(parameters[0], enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>(), false) + func_name + generate(parameters[1], enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>(), false) + string(")")
|
||||
if ( parameters.size == 2 && (func_name == "||" || func_name == "&&")) {
|
||||
var first = generate(parameters[0], enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>(), false)
|
||||
var second = generate(parameters[1], enclosing_object, enclosing_func, null<stack<pair<bool,stack<*ast_node>>>>(), false)
|
||||
var result = code_triple()
|
||||
result.pre += first.pre;
|
||||
var temp_bool = string("temp_bool") + get_id()
|
||||
result.pre += string("bool ") + temp_bool + " = " + first.value + ";\n"
|
||||
result.pre += first.post;
|
||||
if (func_name == "||")
|
||||
result.pre += string("if (!") + temp_bool + ") {"
|
||||
else
|
||||
result.pre += string("if (") + temp_bool + ") {"
|
||||
result.pre += second.pre
|
||||
result.pre += temp_bool + " = " + second.value + ";\n"
|
||||
result.pre += second.post
|
||||
result.pre += "}"
|
||||
result.value = temp_bool
|
||||
return result
|
||||
error("Remaining || or &&")
|
||||
}
|
||||
// don't propegate enclosing function down right of access
|
||||
// XXX what about enclosing object? should it be the thing on the left?
|
||||
|
||||
@@ -31,7 +31,7 @@ fun c_line_control(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, as
|
||||
}
|
||||
}
|
||||
if (first)
|
||||
run_on_tree(helper, empty_pass_half, syntax_ast_pair.second)
|
||||
run_on_tree(helper, empty_pass_second_half, syntax_ast_pair.second)
|
||||
first = false
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,16 +11,19 @@ import ast_transformation:*
|
||||
import pass_common:*
|
||||
|
||||
// Here's how we lower objects
|
||||
// - first, we make sure that all functions, if statments, while loops and for loops have code blocks
|
||||
// 1 first, we make sure that all functions, if statments, while loops and for loops have code blocks
|
||||
// as children, not just statements.
|
||||
// - then, we "uglify" for loops and while loops so that functions that need pre and post statements always
|
||||
// 1 during the same pass, we "uglify" for loops and while loops so that functions that need pre and post statements always
|
||||
// have a code block to insert them into that makes sure that they get run.
|
||||
// - then on the pass up the chain, at function calls we add in they copy_construct in and defer destruct out
|
||||
// 2 in another pass (more complicated because different children have different parent scopes)
|
||||
// we transform the short circuit operators
|
||||
// 3 then on the pass up the chain, at function calls we add in they copy_construct in and defer destruct out
|
||||
// temporaries.
|
||||
// - this is also when we add in defer destructs for function parameters (inside the function) and declaration statements
|
||||
// 3 this is also when we add in defer destructs for function parameters (inside the function) and declaration statements
|
||||
fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
|
||||
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
|
||||
var helper_before = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
|
||||
// Pass 1
|
||||
var ensure_block_and_munge = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
|
||||
match(*node) {
|
||||
ast_node::function(backing) if (backing.body_statement && !is_code_block(backing.body_statement)) backing.body_statement = ast_statement_ptr(ast_code_block_ptr(backing.body_statement))
|
||||
ast_node::if_statement(backing) {
|
||||
@@ -64,7 +67,48 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
}
|
||||
}
|
||||
}
|
||||
var helper_after = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
|
||||
run_on_tree(ensure_block_and_munge, empty_pass_second_half, syntax_ast_pair.second)
|
||||
// Pass 2
|
||||
var short_circut_op: fun(*ast_node,*stack<*ast_node>): bool = fun(node: *ast_node, parent_chain: *stack<*ast_node>): bool {
|
||||
match(*node) {
|
||||
ast_node::function_call(backing) {
|
||||
var func_name = string()
|
||||
if (is_function(backing.func)) {
|
||||
func_name = backing.func->function.name
|
||||
if (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/"
|
||||
|| func_name == "<" || func_name == ">" || func_name == "<=" || func_name == ">="
|
||||
|| func_name == "==" || func_name == "!=" || func_name == "%" || func_name == "^"
|
||||
|| func_name == "|" || func_name == "&" || func_name == "." || func_name == "->"
|
||||
|| func_name == "." || func_name == "->" || func_name == "[]" || func_name == "++p" || func_name == "--p"
|
||||
|| func_name == "*" || func_name == "&"
|
||||
)
|
||||
return true
|
||||
}
|
||||
if (func_name == "||" || func_name == "&&") {
|
||||
var enclosing_block_idx = parent_chain->index_from_top_satisfying(fun(i: *ast_node): bool return is_code_block(i);)
|
||||
var short_circuit_result = ast_identifier_ptr("short_circut_result", type_ptr(base_type::boolean()), parent_chain->from_top(enclosing_block_idx))
|
||||
var short_circuit_declaration = ast_statement_ptr(ast_declaration_statement_ptr(short_circuit_result, backing.parameters[0], false))
|
||||
var condition = short_circuit_result
|
||||
if (func_name == "||")
|
||||
condition = make_operator_call("!", vector(condition))
|
||||
var short_circuit_if = ast_if_statement_ptr(condition)
|
||||
// how to get proper parent scoping working for this part
|
||||
short_circuit_if->if_statement.then_part = ast_code_block_ptr(ast_statement_ptr(ast_assignment_statement_ptr(short_circuit_result, backing.parameters[1])))
|
||||
add_before_in(short_circuit_declaration, parent_chain->from_top(enclosing_block_idx-1), parent_chain->from_top(enclosing_block_idx))
|
||||
add_before_in(ast_statement_ptr(short_circuit_if), parent_chain->from_top(enclosing_block_idx-1), parent_chain->from_top(enclosing_block_idx))
|
||||
replace_with_in(node, short_circuit_result, parent_chain)
|
||||
var shorter_tree =stack_from_vector( parent_chain->data.slice(0, parent_chain->size()-enclosing_block_idx))
|
||||
run_on_tree_helper(short_circut_op, empty_pass_second_half, short_circuit_declaration, &shorter_tree, false)
|
||||
run_on_tree_helper(short_circut_op, empty_pass_second_half, short_circuit_if, &shorter_tree, false)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
run_on_tree(short_circut_op, empty_pass_second_half, syntax_ast_pair.second)
|
||||
// Pass 3
|
||||
var construct_in_destruct_out = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
|
||||
match(*node) {
|
||||
ast_node::function(backing) {
|
||||
var order = 0;
|
||||
@@ -89,7 +133,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
}
|
||||
}
|
||||
}
|
||||
run_on_tree(helper_before, helper_after, syntax_ast_pair.second)
|
||||
run_on_tree(empty_pass_first_half, construct_in_destruct_out, syntax_ast_pair.second)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -62,19 +62,94 @@ fun remove(orig: *ast_node, in: *ast_node): *ast_node {
|
||||
fun replace_with_in(orig: *ast_node, new: *ast_node, in: *stack<*ast_node>)
|
||||
replace_with_in(orig, new, in->top())
|
||||
fun replace_with_in(orig: *ast_node, new: *ast_node, in: *ast_node) {
|
||||
if (is_statement(in)) {
|
||||
in->statement.child = new
|
||||
} else {
|
||||
var bc = get_children_pointer(in)
|
||||
if (bc) {
|
||||
var i = bc->find(orig)
|
||||
if (i >= 0) {
|
||||
bc->set(i, new)
|
||||
match (*in) {
|
||||
ast_node::statement(backing) { backing.child = new; return; }
|
||||
ast_node::return_statement(backing) { backing.return_value = new; return; }
|
||||
ast_node::assignment_statement(backing) {
|
||||
if (backing.to == orig) {
|
||||
backing.to = new
|
||||
return
|
||||
}
|
||||
if (backing.from == orig) {
|
||||
backing.from = new
|
||||
return
|
||||
}
|
||||
}
|
||||
error(string("cannot replace_with_in inside ") + get_ast_name(in))
|
||||
ast_node::declaration_statement(backing) {
|
||||
if (backing.identifier == orig) {
|
||||
backing.identifier = new
|
||||
return
|
||||
}
|
||||
if (backing.expression == orig) {
|
||||
backing.expression = new
|
||||
return
|
||||
}
|
||||
}
|
||||
ast_node::if_statement(backing) {
|
||||
if (backing.condition == orig) {
|
||||
backing.condition = new
|
||||
return
|
||||
}
|
||||
if (backing.then_part == orig) {
|
||||
backing.then_part = new
|
||||
return
|
||||
}
|
||||
if (backing.else_part == orig) {
|
||||
backing.else_part = new
|
||||
return
|
||||
}
|
||||
}
|
||||
ast_node::for_loop(backing) {
|
||||
if (backing.init == orig) {
|
||||
backing.init = new
|
||||
return
|
||||
}
|
||||
if (backing.condition == orig) {
|
||||
backing.condition = new
|
||||
return
|
||||
}
|
||||
if (backing.update == orig) {
|
||||
backing.update = new
|
||||
return
|
||||
}
|
||||
if (backing.body == orig) {
|
||||
backing.body = new
|
||||
return
|
||||
}
|
||||
}
|
||||
ast_node::while_loop(backing) {
|
||||
if (backing.condition == orig) {
|
||||
backing.condition = new
|
||||
return
|
||||
}
|
||||
if (backing.statement == orig) {
|
||||
backing.statement = new
|
||||
return
|
||||
}
|
||||
}
|
||||
ast_node::function_call(backing) {
|
||||
if (backing.func == orig) {
|
||||
backing.func = new
|
||||
return
|
||||
}
|
||||
for (var i = 0; i < backing.parameters.size; i++;) {
|
||||
if (backing.parameters[i] == orig) {
|
||||
backing.parameters[i] = new
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var bc = get_children_pointer(in)
|
||||
if (bc) {
|
||||
var i = bc->find(orig)
|
||||
if (i >= 0) {
|
||||
bc->set(i, new)
|
||||
return
|
||||
}
|
||||
}
|
||||
error(string("cannot replace_with_in inside ") + get_ast_name(in))
|
||||
}
|
||||
|
||||
fun add_before_in(to_add: ref vector<*ast_node>, before: *ast_node, in: *stack<*ast_node>)
|
||||
@@ -109,39 +184,45 @@ fun add_after_in(to_add: *ast_node, before: *ast_node, in: *ast_node) {
|
||||
error(string("cannot add_after_in to ") + get_ast_name(in))
|
||||
}
|
||||
|
||||
fun empty_pass_half(node: *ast_node, parent_chain: *stack<*ast_node>) {}
|
||||
fun run_on_tree(func_before: fun(*ast_node,*stack<*ast_node>):void, func_after: fun(*ast_node,*stack<*ast_node>):void, tree: *ast_node) {
|
||||
fun empty_pass_first_half(node: *ast_node, parent_chain: *stack<*ast_node>): bool { return true; }
|
||||
fun empty_pass_second_half(node: *ast_node, parent_chain: *stack<*ast_node>) {}
|
||||
fun run_on_tree(func_before: fun(*ast_node,*stack<*ast_node>):void, func_after: fun(*ast_node,*stack<*ast_node>):void, tree: *ast_node)
|
||||
run_on_tree(fun(n: *ast_node, s: *stack<*ast_node>): bool {func_before(n, s);return true;}, func_after, tree)
|
||||
|
||||
fun run_on_tree(func_before: fun(*ast_node,*stack<*ast_node>):bool, func_after: fun(*ast_node,*stack<*ast_node>):void, tree: *ast_node) {
|
||||
var parent_stack = stack<*ast_node>()
|
||||
run_on_tree_helper(func_before, func_after, tree, &parent_stack, false)
|
||||
}
|
||||
fun run_on_tree_helper(func_before: fun(*ast_node,*stack<*ast_node>):void, func_after: fun(*ast_node,*stack<*ast_node>):void, node: *ast_node, parent_chain: *stack<*ast_node>, do_func: bool) {
|
||||
fun run_on_tree_helper(func_before: fun(*ast_node,*stack<*ast_node>):bool, func_after: fun(*ast_node,*stack<*ast_node>):void, node: *ast_node, parent_chain: *stack<*ast_node>, do_func: bool) {
|
||||
if (!node || (!do_func && is_function(node))) return
|
||||
func_before(node, parent_chain)
|
||||
var do_children = func_before(node, parent_chain)
|
||||
parent_chain->push(node)
|
||||
match(*node) {
|
||||
ast_node::translation_unit(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, true);)
|
||||
ast_node::type_def(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, true);)
|
||||
ast_node::adt_def(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, true);)
|
||||
ast_node::function(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::template(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, true);)
|
||||
ast_node::code_block(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::if_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::match_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::case_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::while_loop(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::for_loop(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::return_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::defer_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::assignment_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::declaration_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::if_comp(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, true);)
|
||||
ast_node::function_call(backing) {
|
||||
if (!is_function(backing.func))
|
||||
run_on_tree_helper(func_before, func_after, backing.func, parent_chain, false)
|
||||
node->function_call.parameters.for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
if (do_children) {
|
||||
match(*node) {
|
||||
ast_node::translation_unit(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, true);)
|
||||
ast_node::type_def(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, true);)
|
||||
ast_node::adt_def(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, true);)
|
||||
ast_node::function(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::template(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, true);)
|
||||
ast_node::code_block(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::if_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::match_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::case_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::while_loop(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::for_loop(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::return_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::defer_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::assignment_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::declaration_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
ast_node::if_comp(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, true);)
|
||||
ast_node::function_call(backing) {
|
||||
if (!is_function(backing.func))
|
||||
run_on_tree_helper(func_before, func_after, backing.func, parent_chain, false)
|
||||
node->function_call.parameters.for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
}
|
||||
ast_node::cast(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
}
|
||||
ast_node::cast(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, false);)
|
||||
}
|
||||
// function may have messed with the parent chain
|
||||
if (parent_chain->data.contains(node))
|
||||
|
||||
@@ -2,6 +2,11 @@ import vector
|
||||
import serialize
|
||||
import util
|
||||
|
||||
fun stack_from_vector<T>(i: vector::vector<T>): stack<T> {
|
||||
var to_ret.construct(): stack<T>
|
||||
to_ret.data = i
|
||||
return to_ret
|
||||
}
|
||||
|
||||
fun stack<T>():stack<T> {
|
||||
var out.construct():stack<T>
|
||||
@@ -32,8 +37,6 @@ obj stack<T> (Object, Serializable) {
|
||||
return serialize::serialize(data)
|
||||
}
|
||||
fun unserialize(it: ref vector::vector<char>, pos: int): int {
|
||||
/*construct()*/
|
||||
/*util::unpack(data, pos) = serialize::unserialize<vector::vector<T>>(it, pos)*/
|
||||
return data.unserialize(it, pos)
|
||||
}
|
||||
fun push(it: ref T) {
|
||||
@@ -73,4 +76,10 @@ obj stack<T> (Object, Serializable) {
|
||||
}
|
||||
fun reverse_vector(): vector::vector<T>
|
||||
return data.reverse()
|
||||
fun index_from_top_satisfying(func: fun(T):bool): int {
|
||||
for (var i = 0; i < data.size; i++;)
|
||||
if (func(from_top(i)))
|
||||
return i
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user