* Count the number of operations of the given time rooted at 'oper'.
*/
static GLuint
-_slang_count_node_type(slang_operation *oper, slang_operation_type type)
+_slang_count_node_type(const slang_operation *oper, slang_operation_type type)
{
GLuint i, count = 0;
if (oper->type == type) {
{
slang_operation *assignOper;
- if (!A->EmitContReturn) {
+ if (A->UseReturnFlag) {
slang_operation *ifOper = slang_oper_child(blockOper, 0);
ifOper->type = SLANG_OPER_IF;
slang_operation_add_children(ifOper, 3);
slang_generate_declaration(A, oper->locals, decl,
SLANG_SPEC_BOOL, "__returnFlag", GL_TRUE);
- slang_print_tree(oper, 0);
+ /*slang_print_tree(oper, 0);*/
}
var = _slang_variable_locate(oper->locals, id, GL_TRUE);
assert(var);
}
+}
+
+/**
+ * Test if the given function body has an "early return". That is, there's
+ * a 'return' statement that's not the very last instruction in the body.
+ */
+static GLboolean
+has_early_return(const slang_operation *funcBody)
+{
+ GLuint retCount = _slang_count_node_type(funcBody, SLANG_OPER_RETURN);
+ if (retCount == 0)
+ return GL_FALSE;
+ else if (retCount == 1 && _slang_is_tail_return(funcBody))
+ return GL_FALSE;
+ else
+ return GL_TRUE;
}
+/**
+ * Emit IR code for a function call. This does one of two things:
+ * 1. Inline the function's code
+ * 2. Create an IR for the function's body and create a real call to it.
+ */
static slang_ir_node *
_slang_gen_function_call(slang_assemble_ctx *A, slang_function *fun,
slang_operation *oper, slang_operation *dest)
* 1. insert the inline code
* 2. Generate a call to the "inline" code as a subroutine
*/
-
-
+ const GLboolean earlyReturn = has_early_return(fun->body);
slang_operation *ret = NULL;
+ if (earlyReturn && !A->EmitContReturn) {
+ A->UseReturnFlag = GL_TRUE;
+ }
+
inlined = slang_inline_function_call(A, fun, oper, dest);
if (!inlined)
return NULL;
ret = _slang_find_node_type(inlined, SLANG_OPER_RETURN);
if (ret) {
/* check if this is a "tail" return */
- if (_slang_count_node_type(inlined, SLANG_OPER_RETURN) == 1 &&
- _slang_is_tail_return(inlined)) {
+ if (!earlyReturn) {
/* The only RETURN is the last stmt in the function, no-op it
* and inline the function body.
*/
callOper = inlined;
}
- if (!A->EmitContReturn) {
+ if (A->UseReturnFlag) {
/* Early returns not supported. Create a _returnFlag variable
* that's set upon 'return' and tested elsewhere to no-op any
* remaining instructions in the subroutine.
n = new_seq(_slang_gen_operation(A, assign),
new_return(A->curFuncEndLabel));
}
- else {
+ else if (A->UseReturnFlag) {
/* set __returnFlag = false; */
slang_operation *setFlag = slang_operation_new(1);
setFlag->type = SLANG_OPER_ASSIGN;
assert(A->program->Parameters );
assert(A->program->Varying);
assert(A->vartable);
-#if 0
- A->CurLoop = NULL;
- A->CurLoopOper = NULL;
-#else
+
A->LoopDepth = 0;
-#endif
+ A->UseReturnFlag = GL_FALSE;
A->CurFunction = fun;
/* fold constant expressions, etc. */