_slang_free_ir_tree(slang_ir_node *n)
{
#if 0
+ GLuint i;
if (!n)
return;
- _slang_free_ir_tree(n->Children[0]);
- _slang_free_ir_tree(n->Children[1]);
+ for (i = 0; i < 3; i++)
+ _slang_free_ir_tree(n->Children[i]);
+ /* Do not free n->BranchNode since it's a child elsewhere */
free(n);
#endif
}
static slang_ir_node *
new_seq(slang_ir_node *left, slang_ir_node *right)
{
- /* XXX if either left or right is null, just return pointer to other?? */
- assert(left);
- assert(right);
+ if (!left)
+ return right;
+ if (!right)
+ return left;
return new_node2(IR_SEQ, left, right);
}
static slang_ir_node *
-new_begin_loop(void)
-{
- slang_ir_node *n = new_node0(IR_BEGIN_LOOP);
- return n;
-}
-
-
-static slang_ir_node *
-new_end_loop(slang_ir_node *beginNode)
+new_loop(slang_ir_node *body)
{
- slang_ir_node *n = new_node0(IR_END_LOOP);
- assert(beginNode);
- if (n) {
- n->BranchNode = beginNode;
- }
- return n;
+ return new_node1(IR_LOOP, body);
}
static slang_ir_node *
-new_break(slang_ir_node *beginNode)
+new_break(slang_ir_node *loopNode)
{
slang_ir_node *n = new_node0(IR_BREAK);
- assert(beginNode);
+ assert(loopNode);
+ assert(loopNode->Opcode == IR_LOOP);
if (n) {
- n->BranchNode = beginNode;
+ n->BranchNode = loopNode;
}
return n;
}
/**
- * Generate IR tree for a while-loop using high-level BGNLOOP/ENDLOOP,
- * IF/ENDIF instructions.
+ * Generate IR tree for a while-loop using high-level LOOP, IF instructions.
*/
static slang_ir_node *
_slang_gen_hl_while(slang_assemble_ctx * A, const slang_operation *oper)
{
+ slang_ir_node *prevLoop;
/*
- * BGNLOOP
+ * LOOP:
* eval expr (child[0]), updating condcodes
- * IF !expr THEN
+ * IF !expr:
* BRK
- * ENDIF
* body code (child[1])
- * ENDLOOP
*/
- slang_ir_node *beginLoop, *endLoop, *ifThen;
- slang_ir_node *cond, *body, *tree;
+ slang_ir_node *ifThen, *cond, *body, *loop;
- beginLoop = new_begin_loop();
+ loop = new_loop(NULL);
+
+ /* save old, push new loop */
+ prevLoop = A->CurLoop;
+ A->CurLoop = loop;
cond = _slang_gen_operation(A, &oper->children[0]);
cond = new_node1(IR_NOT, cond);
cond = _slang_gen_cond(cond);
ifThen = new_if(cond,
- new_break(beginLoop),
+ new_break(A->CurLoop),
NULL);
- tree = new_seq(beginLoop, ifThen);
body = _slang_gen_operation(A, &oper->children[1]);
- if (body)
- tree = new_seq(tree, body);
- endLoop = new_end_loop(beginLoop);
- tree = new_seq(tree, endLoop);
+ loop->Children[0] = new_seq(ifThen, body);
- return tree;
+
+ A->CurLoop = prevLoop; /* pop loop, restore prev */
+
+ return loop;
}
else
return _slang_gen_for(A, oper);
case slang_oper_break:
- if (!A->CurLoopBreak) {
+ if (!A->CurLoop && !A->CurLoopBreak) {
RETURN_ERROR("'break' not in loop", 0);
}
- /* XXX emit IR_BREAK instruction */
- return new_jump(A->CurLoopBreak);
+ if (UseHighLevelInstructions) {
+ return new_break(A->CurLoop);
+ }
+ else {
+ return new_jump(A->CurLoopBreak);
+ }
case slang_oper_continue:
if (!A->CurLoopCont) {
RETURN_ERROR("'continue' not in loop", 0);
printf("CALL\n");
break;
- case IR_BEGIN_LOOP:
- printf("BEGIN_LOOP\n");
- break;
- case IR_END_LOOP:
- printf("END_LOOP\n");
+ case IR_LOOP:
+ printf("LOOP\n");
+ slang_print_ir(n->Children[0], indent+3);
+ spaces(indent);
+ printf("ENDLOOP\n");
break;
case IR_CONT:
printf("CONT\n");
case IR_IF:
return emit_if(vt, n, prog);
- case IR_BEGIN_LOOP:
+ case IR_LOOP:
{
+ struct prog_instruction *beginInst;
+
/* save location of this instruction, used by OPCODE_ENDLOOP */
n->InstLocation = prog->NumInstructions;
(void) new_instruction(prog, OPCODE_BGNLOOP);
- }
- break;
- case IR_END_LOOP:
- {
- struct prog_instruction *inst, *beginInst;
+
+ /* body */
+ emit(vt, n->Children[0], prog);
+
inst = new_instruction(prog, OPCODE_ENDLOOP);
- assert(n->BranchNode);
- assert(n->BranchNode->InstLocation >= 0);
/* The instruction BranchTarget points to top of loop */
- inst->BranchTarget = n->BranchNode->InstLocation;
- /* Update BEGIN_LOOP's BranchTarget to point to this instruction */
- beginInst = prog->Instructions + n->BranchNode->InstLocation;
+ inst->BranchTarget = n->InstLocation;
+ /* Update BGNLOOP's BranchTarget to point to this instruction */
+ beginInst = prog->Instructions + n->InstLocation;
beginInst->BranchTarget = prog->NumInstructions - 1;
return inst;
}
inst->BranchTarget = n->BranchNode->InstLocation;
return inst;
}
+
case IR_BEGIN_SUB:
return new_instruction(prog, OPCODE_BGNSUB);
case IR_END_SUB:
case IR_RETURN:
return new_instruction(prog, OPCODE_RET);
+ case IR_NOP:
+ return NULL;
+
default:
_mesa_problem(NULL, "Unexpected IR opcode in emit()\n");
abort();