/* Insert an if node around "node" testing the condition encoded * in guard "guard". * * If the user does not want any disjunctions in the if conditions * and if "guard" does involve a disjunction, then we make the different * disjuncts disjoint and insert an if node corresponding to each disjunct * around a copy of "node". The result is then a block node containing * this sequence of guarded copies of "node". */ static __isl_give isl_ast_node *ast_node_insert_if( __isl_take isl_ast_node *node, __isl_take isl_set *guard, __isl_keep isl_ast_build *build) { struct isl_insert_if_data data; isl_ctx *ctx; ctx = isl_ast_build_get_ctx(build); if (isl_options_get_ast_build_allow_or(ctx) || isl_set_n_basic_set(guard) <= 1) { isl_ast_node *if_node; isl_ast_expr *expr; expr = isl_ast_build_expr_from_set_internal(build, guard); if_node = isl_ast_node_alloc_if(expr); return isl_ast_node_if_set_then(if_node, node); } guard = isl_set_make_disjoint(guard); data.list = isl_ast_node_list_alloc(ctx, 0); data.node = node; data.build = build; if (isl_set_foreach_basic_set(guard, &insert_if, &data) < 0) data.list = isl_ast_node_list_free(data.list); isl_set_free(guard); isl_ast_node_free(data.node); return isl_ast_node_alloc_block(data.list); }
/* Create a graft for "node" with no guards and no enforced conditions. */ __isl_give isl_ast_graft *isl_ast_graft_alloc( __isl_take isl_ast_node *node, __isl_keep isl_ast_build *build) { isl_ctx *ctx; isl_space *space; isl_ast_graft *graft; if (!node) return NULL; ctx = isl_ast_node_get_ctx(node); graft = isl_calloc_type(ctx, isl_ast_graft); if (!graft) goto error; space = isl_ast_build_get_space(build, 1); graft->ref = 1; graft->node = node; graft->guard = isl_set_universe(isl_space_copy(space)); graft->enforced = isl_basic_set_universe(space); if (!graft->guard || !graft->enforced) return isl_ast_graft_free(graft); return graft; error: isl_ast_node_free(node); return NULL; }
void cpp_from_isl::process_block(isl_ast_node *node) { // The generate AST is weird: // Blocks have at most 2 statements. // If there are more consecutive statements, // all but the last are grouped into a nested block, // and so on recursively. //auto block = make_shared<block_statement>(); //m_ctx->push(&block->statements); auto list = isl_ast_node_block_get_children(node); int n_children = isl_ast_node_list_n_ast_node(list); for(int i = 0; i < n_children; ++i) { auto child = isl_ast_node_list_get_ast_node(list, i); process_node(child); isl_ast_node_free(child); } isl_ast_node_list_free(list); //m_ctx->pop(); //m_ctx->add(block); }
void cpp_from_isl::process_node(isl_ast_node *node) { auto type = isl_ast_node_get_type(node); switch(type) { case isl_ast_node_for: process_for(node); break; case isl_ast_node_if: process_if(node); break; case isl_ast_node_block: process_block(node); break; case isl_ast_node_user: process_user(node); break; case isl_ast_node_mark: { // TODO: label the stmt? auto marked_node = isl_ast_node_mark_get_node(node); process_node(marked_node); isl_ast_node_free(marked_node); break; } default: throw error("Unexpected AST node type."); } }
void cpp_from_isl::process_if(isl_ast_node *node) { auto cond_expr = isl_ast_node_if_get_cond(node); auto true_node = isl_ast_node_if_get_then(node); auto false_node = isl_ast_node_if_get_else(node); auto if_stmt = make_shared<if_statement>(); if_stmt->condition = process_expr(cond_expr); { vector<statement_ptr> stmts; m_ctx->push(&stmts); process_node(true_node); m_ctx->pop(); if (stmts.size() == 1) if_stmt->true_part = stmts.front(); else if_stmt->true_part = block(stmts); } if (false_node) { vector<statement_ptr> stmts; m_ctx->push(&stmts); process_node(false_node); m_ctx->pop(); if (stmts.size() == 1) if_stmt->false_part = stmts.front(); else if_stmt->false_part = block(stmts); } m_ctx->add(if_stmt); isl_ast_expr_free(cond_expr); isl_ast_node_free(true_node); isl_ast_node_free(false_node); }
void cpp_from_isl::process_for(isl_ast_node *node) { auto iter_expr = isl_ast_node_for_get_iterator(node); auto init_expr = isl_ast_node_for_get_init(node); auto cond_expr = isl_ast_node_for_get_cond(node); auto inc_expr = isl_ast_node_for_get_inc(node); auto body_node = isl_ast_node_for_get_body(node); auto iter = process_expr(iter_expr); auto init = process_expr(init_expr); auto cond = process_expr(cond_expr); auto inc = process_expr(inc_expr); auto iter_id = dynamic_pointer_cast<id_expression>(iter); if (!iter_id) throw error("Iterator expression is not an identifier."); auto iter_decl = decl_expr(make_shared<basic_type>("int"), *iter_id, init); auto for_stmt = make_shared<for_statement>(); for_stmt->initialization = iter_decl; for_stmt->condition = cond; for_stmt->update = binop(op::assign_add, iter, inc); { vector<statement_ptr> stmts; m_ctx->push(&stmts); process_node(body_node); m_ctx->pop(); if (stmts.size() == 1) for_stmt->body = stmts.front(); else for_stmt->body = block(stmts); } m_ctx->add(for_stmt); isl_ast_expr_free(iter_expr); isl_ast_expr_free(init_expr); isl_ast_expr_free(cond_expr); isl_ast_expr_free(inc_expr); isl_ast_node_free(body_node); }
/* Extend the node at *body with node. * * If body points to the else branch, then *body may still be NULL. * If so, we simply attach node to this else branch. * Otherwise, we attach a list containing the statements already * attached at *body followed by node. */ static void extend_body(__isl_keep isl_ast_node **body, __isl_take isl_ast_node *node) { isl_ast_node_list *list; if (!*body) { *body = node; return; } if ((*body)->type == isl_ast_node_block) { list = isl_ast_node_block_get_children(*body); isl_ast_node_free(*body); } else list = isl_ast_node_list_from_ast_node(*body); list = isl_ast_node_list_add(list, node); *body = isl_ast_node_alloc_block(list); }