-/**
- * Allocate storage for given variable, attach it to 'ir'.
- */
-static GLboolean
-slang_alloc_var_storage(slang_variable *variable, slang_ir_node *ir)
-{
- slang_ir_storage *store;
-
- assert(variable);
-
- /*assert(!variable->aux);*/
-
- if (variable->aux) {
- store = (slang_ir_storage *) variable->aux;
- ir->Store = store;
- if (store)
- store->Size = -12;
- }
- else {
- /* alloc storage */
- store = (slang_ir_storage *) _mesa_calloc(sizeof(*store));
- store->File = PROGRAM_TEMPORARY;
- store->Index = -1;
- store->Size = -10;
- variable->aux = store;
- ir->Store = store;
- }
- return GL_TRUE;
-}
-
-
static slang_ir_node *
new_node(slang_ir_opcode op, slang_ir_node *left, slang_ir_node *right)
{
oper->var = v;
n->Swizzle = swizzle;
n->Var = v;
- slang_resolve_storage(NULL, n, A->program);
+ slang_resolve_storage(A->codegen/**NULL**/, n, A->program);
return n;
}
}
+/**
+ * Assemble a "return" statement.
+ */
static slang_ir_node *
slang_assemble_return(slang_assemble_ctx * A, slang_operation *oper)
{
- if (oper->num_children == 0) {
- /* Convert to:
+ if (oper->num_children == 0 ||
+ (oper->num_children == 1 &&
+ oper->children[0].type == slang_oper_void)) {
+ /* Convert from:
+ * return;
+ * To:
* goto __endOfFunction;
*/
- oper->type = slang_oper_goto;
- oper->a_id = slang_atom_pool_atom(A->atoms, CurFunction->end_label);
+ slang_ir_node *n;
+ slang_operation gotoOp;
+ slang_operation_construct(&gotoOp);
+ gotoOp.type = slang_oper_goto;
+ gotoOp.a_id = slang_atom_pool_atom(A->atoms, CurFunction->end_label);
+ /* assemble the new code */
+ n = slang_assemble_operation(A, &gotoOp);
+ /* destroy temp code */
+ slang_operation_destruct(&gotoOp);
+ return n;
}
else {
/*
*/
slang_operation *block, *assign, *jump;
slang_atom a_retVal;
+ slang_ir_node *n;
a_retVal = slang_atom_pool_atom(A->atoms, "__retVal");
assert(a_retVal);
-#if 1
+#if 1 /* DEBUG */
{
slang_variable *v
= _slang_locate_variable(oper->locals, a_retVal, GL_TRUE);
assign->locals->outer_scope = block->locals;
assign->num_children = 2;
assign->children = slang_operation_new(2);
- /* lhs */
+ /* lhs (__retVal) */
assign->children[0].type = slang_oper_identifier;
assign->children[0].a_id = a_retVal;
assign->children[0].locals->outer_scope = assign->locals;
- /* rhs */
-#if 0
- assign->children[1] = oper->children[0]; /* XXX copy */
-#else
+ /* rhs (expr) */
+ /* XXX we might be able to avoid this copy someday */
slang_operation_copy(&assign->children[1], &oper->children[0]);
-#endif
-
/* child[1]: goto __endOfFunction */
jump = &block->children[1];
assert(CurFunction->end_label);
jump->a_id = slang_atom_pool_atom(A->atoms, CurFunction->end_label);
-#if 00
+#if 0 /* debug */
printf("NEW RETURN:\n");
slang_print_tree(block, 0);
#endif
- slang_operation_copy(oper, block);
- /* XXX destruct block */
+ /* assemble the new code */
+ n = slang_assemble_operation(A, block);
+ slang_operation_delete(block);
+ return n;
}
-
- /* assemble the new code */
- return slang_assemble_operation(A, oper);
}
}
}
break;
+#if 0 /* XXX rely on default case below */
case slang_oper_return:
/* do return replacement here too */
- slang_assemble_return(A, oper);
- slang_substitute(A, oper, substCount, substOld, substNew, GL_FALSE);
+ assert(oper->num_children == 0 || oper->num_children == 1);
+ if (oper->num_children == 1) {
+ slang_substitute(A, &oper->children[0],
+ substCount, substOld, substNew, GL_FALSE);
+ }
break;
+#endif
case slang_oper_assign:
case slang_oper_subscript:
/* special case:
* child[0] can't have substitutions but child[1] can.
*/
- slang_substitute(A, &oper->children[0], substCount, substOld, substNew, GL_TRUE);
- slang_substitute(A, &oper->children[1], substCount, substOld, substNew, GL_FALSE);
+ slang_substitute(A, &oper->children[0],
+ substCount, substOld, substNew, GL_TRUE);
+ slang_substitute(A, &oper->children[1],
+ substCount, substOld, substNew, GL_FALSE);
break;
case slang_oper_field:
/* XXX NEW - test */
- slang_substitute(A, &oper->children[0], substCount, substOld, substNew, GL_TRUE);
+ slang_substitute(A, &oper->children[0],
+ substCount, substOld, substNew, GL_TRUE);
break;
default:
{
GLuint i;
for (i = 0; i < oper->num_children; i++)
- slang_substitute(A, &oper->children[i], substCount, substOld, substNew, GL_FALSE);
+ slang_substitute(A, &oper->children[i],
+ substCount, substOld, substNew, GL_FALSE);
}
}
}
assert(oper->num_children == 0 || oper->num_children == 1);
- v = _slang_locate_variable(oper->locals,
- oper->a_id, GL_TRUE);
+ v = _slang_locate_variable(oper->locals, oper->a_id, GL_TRUE);
assert(v);
varDecl = new_var_decl(A, v);
- slang_alloc_var_storage(v, varDecl);
+ slang_resolve_storage(A->codegen, varDecl, A->program);
if (oper->num_children > 0) {
/* child is initializer */
assert(oper->num_children == 1);
var = new_var(A, oper, oper->a_id, SWIZZLE_NOOP);
/* XXX make copy of this initializer? */
+ /*
printf("\n*** ASSEMBLE INITIALIZER %p\n", (void*) v->initializer);
+ */
rhs = slang_assemble_operation(A, &oper->children[0]);
init = new_node(IR_MOVE, var, rhs);
+ /*assert(rhs->Opcode != IR_SEQ);*/
n = new_seq(varDecl, init);
}
else if (v->initializer) {
slang_ir_node *var, *init, *rhs;
var = new_var(A, oper, oper->a_id, SWIZZLE_NOOP);
/* XXX make copy of this initializer? */
+ /*
printf("\n*** ASSEMBLE INITIALIZER %p\n", (void*) v->initializer);
+ */
rhs = slang_assemble_operation(A, v->initializer);
init = new_node(IR_MOVE, var, rhs);
+ /*
+ assert(rhs->Opcode != IR_SEQ);
+ */
n = new_seq(varDecl, init);
}
else {
oper->children[1].type == slang_oper_call) {
/* special case */
slang_ir_node *n;
- printf(">>>>>>>>>>>>>> Assign function call\n");
n = slang_assemble_function_call_name(A,
- (const char *) oper->children[1].a_id,
- &oper->children[1], &oper->children[0]);
+ (const char *) oper->children[1].a_id,
+ &oper->children[1], &oper->children[0]);
return n;
}
else
c1 = slang_assemble_operation(A, &oper->children[1]);
n = new_node(IR_MOVE, c0, c1);
+ /*
+ assert(c1->Opcode != IR_SEQ);
+ */
n->Writemask = mask;
return n;
}
/* array dereference */
if (oper->children[1].type == slang_oper_literal_int) {
/* compile-time constant index - OK */
- slang_assembly_typeinfo ti;
+ slang_assembly_typeinfo elem_ti;
slang_ir_node *base;
- slang_ir_storage *store2;
GLint index;
- slang_assembly_typeinfo_construct(&ti);
- _slang_typeof_operation(A, &oper->children[0], &ti);
+
+ /* get type of array element */
+ slang_assembly_typeinfo_construct(&elem_ti);
+ _slang_typeof_operation(A, oper, &elem_ti);
base = slang_assemble_operation(A, &oper->children[0]);
assert(base->Opcode == IR_VAR);
+ assert(base->Store);
index = (GLint) oper->children[1].literal[0];
- /*
- printf("element[%d]\n", index);
- */
-#if 1
- store2 = (slang_ir_storage *) _mesa_calloc(sizeof(*store2));
- *store2 = *base->Store;
- base->Store = store2;
- base->Store->Size = -15;
-#endif
- assert(base->Store);
+ /*printf("element[%d]\n", index);*/
+ /* new storage info since we don't want to change the original */
+ base->Store = _slang_clone_ir_storage(base->Store);
+ /* bias Index by array subscript, update storage size */
base->Store->Index += index;
- base->Store->Size = 1;
+ base->Store->Size = _slang_sizeof_type_specifier(&elem_ti.spec);
return base;
}
else {
- /* run-time index - TBD */
+ /* run-time index - not supported yet - TBD */
abort();
}
return NULL;
slang_ir_node *one = new_float_literal(1.0, 1.0, 1.0, 1.0);
slang_ir_node *sum = new_node(IR_ADD, var, one);
slang_ir_node *assign = new_node(IR_MOVE, var, sum);
+ assert(sum->Opcode != IR_SEQ);
return assign;
}
break;
return 0;
printf("\n*********** Assemble function2(%s)\n", (char*)fun->header.a_name);
-#if 0
+#if 1
slang_print_function(fun, 1);
#endif
A->program->Parameters = _mesa_new_parameter_list();
A->program->Varying = _mesa_new_parameter_list();
+ A->codegen = _slang_new_codegen_context();
/*printf("** Begin Simplify\n");*/
slang_simplify(fun->body, &A->space, A->atoms);
CurFunction = fun;
- n = slang_assemble_operation(A, fun->body);
-
if (!CurFunction->end_label)
CurFunction->end_label = slang_atom_pool_gen(A->atoms, "__endOfFunction_Main");
+ n = slang_assemble_operation(A, fun->body);
+
endLabel = new_label(fun->end_label);
n = new_seq(n, endLabel);
printf("************* IR for %s *******\n", (char*)fun->header.a_name);
slang_print_ir(n, 0);
+ printf("************* End assemble function2 ************\n\n");
#endif
if (_mesa_strcmp((char*) fun->header.a_name, "main") == 0) {
- _slang_emit_code(n, A->program);
+ _slang_emit_code(n, A->codegen, A->program);
}
- printf("************* End assemble function2 ************\n\n");
-
return n;
}
}
+slang_ir_storage *
+_slang_clone_ir_storage(slang_ir_storage *store)
+{
+ slang_ir_storage *clone
+ = _slang_new_ir_storage(store->File, store->Index, store->Size);
+ return clone;
+}
+
+
static const char *
swizzle_string(GLuint swizzle)
{
}
-static GLuint
-sizeof_type(const slang_fully_specified_type *t)
+GLuint
+_slang_sizeof_type_specifier(const slang_type_specifier *spec)
{
- switch (t->specifier.type) {
+ switch (spec->type) {
case slang_spec_void:
abort();
return 0;
abort();
return 0;
case slang_spec_struct:
- return sizeof_struct(t->specifier._struct);
+ return sizeof_struct(spec->_struct);
case slang_spec_array:
return 1; /* XXX */
default:
}
+
+static GLuint
+sizeof_type(const slang_fully_specified_type *t)
+{
+ return _slang_sizeof_type_specifier(&t->specifier);
+}
+
+
#define IND 0
void
slang_print_ir(const slang_ir_node *n, int indent)
switch (n->Opcode) {
case IR_SEQ:
#if IND
- printf("SEQ store %p\n", (void*) n->Store);
+ printf("SEQ at %p\n", (void*) n);
#endif
assert(n->Children[0]);
assert(n->Children[1]);
static GLint
-alloc_temporary(slang_gen_context *gc)
+alloc_temporary(slang_gen_context *gc, GLint size)
{
- GLuint i;
+ const GLuint sz4 = (size + 3) / 4;
+ GLuint i, j;
+ ASSERT(size > 0); /* number of floats */
for (i = 0; i < MAX_PROGRAM_TEMPS; i++) {
- if (!gc->TempUsed[i]) {
- gc->TempUsed[i] = GL_TRUE;
+ GLuint found = 0;
+ for (j = 0; j < sz4; j++) {
+ if (!gc->TempUsed[i + j]) {
+ found++;
+ }
+ }
+ if (found == sz4) {
+ /* found block of size/4 free regs */
+ for (j = 0; j < sz4; j++)
+ gc->TempUsed[i + j] = GL_TRUE;
return i;
}
}
static void
-free_temporary(slang_gen_context *gc, GLuint r)
+free_temporary(slang_gen_context *gc, GLuint r, GLint size)
{
- if (gc->TempUsed[r])
- gc->TempUsed[r] = GL_FALSE;
+ const GLuint sz4 = (size + 3) / 4;
+ GLuint i;
+ for (i = 0; i < sz4; i++) {
+ if (gc->TempUsed[r + i])
+ gc->TempUsed[r + i] = GL_FALSE;
+ }
}
assert(!n->Var);
assert(!n->Store);
assert(size > 0);
- indx = alloc_temporary(gc);
+ indx = alloc_temporary(gc, size);
n->Store = _slang_new_ir_storage(PROGRAM_TEMPORARY, indx, size);
}
slang_resolve_storage(slang_gen_context *gc, slang_ir_node *n,
struct gl_program *prog)
{
- int k = 0;
+ assert(gc);
+ assert(n);
+ assert(prog);
+
if (!n->Store) {
- /**assert(n->Var);**/
+ /* allocate storage info for this node */
if (n->Var && n->Var->aux) {
/* node storage info = var storage info */
n->Store = (slang_ir_storage *) n->Var->aux;
else {
/* alloc new storage info */
n->Store = _slang_new_ir_storage(PROGRAM_UNDEFINED, -1, -5);
- k = 1;
- /*XXX n->Store->Size = sizeof(var's type) */
if (n->Var)
n->Var->aux = n->Store;
}
}
if (n->Opcode == IR_VAR_DECL) {
- /* allocate storage for a user's variable */
+ /* storage declaration */
assert(n->Var);
- if (n->Store->Index < 0) {
+ if (n->Store->Index < 0) { /* XXX assert this? */
assert(gc);
n->Store->File = PROGRAM_TEMPORARY;
- n->Store->Index = alloc_temporary(gc);
n->Store->Size = sizeof_type(&n->Var->type);
+ n->Store->Index = alloc_temporary(gc, n->Store->Size);
printf("alloc var %s storage at %d (size %d)\n",
(char *) n->Var->a_name,
n->Store->Index,
assert(n->Store->Size > 0);
n->Var->declared = GL_TRUE;
}
+ assert(n->Store->Size > 0);
return;
}
GLint i;
assert(n->Var);
- assert(prog);
+
+ if (n->Store->Size < 0) {
+ /* determine var/storage size now */
+ n->Store->Size = sizeof_type(&n->Var->type);
+ assert(n->Store->Size > 0);
+ }
#if 0
assert(n->Var->declared ||
if (i >= 0) {
n->Store->File = PROGRAM_INPUT;
n->Store->Index = i;
- n->Store->Size = sizeof_type(&n->Var->type);
assert(n->Store->Size > 0);
prog->InputsRead |= (1 << i);
return;
if (i >= 0) {
n->Store->File = PROGRAM_OUTPUT;
n->Store->Index = i;
- n->Store->Size = sizeof_type(&n->Var->type);
prog->OutputsWritten |= (1 << i);
return;
}
if (i >= 0) {
n->Store->File = PROGRAM_STATE_VAR;
n->Store->Index = i;
- n->Store->Size = sizeof_type(&n->Var->type);
return;
}
if (i >= 0) {
n->Store->File = PROGRAM_CONSTANT;
n->Store->Index = i;
- n->Store->Size = sizeof_type(&n->Var->type);
return;
}
if (i >= 0) {
n->Store->File = PROGRAM_UNIFORM;
n->Store->Index = i;
- n->Store->Size = sizeof_type(&n->Var->type);
return;
}
}
#else
n->Store->File = PROGRAM_VARYING;
#endif
- n->Store->Size = sizeof_type(&n->Var->type);
n->Store->Index = i;
return;
}
}
- /* what is this?!? */
- /*
- abort();
- */
- }
-
- if (n->Store->File == PROGRAM_TEMPORARY && n->Store->Index < 0) {
- /* unnamed intermediate temporary */
- if (gc)
- n->Store->Index = alloc_temporary(gc);
- return;
- }
-
- if (gc && n->Store->File == PROGRAM_UNDEFINED && n->Store->Size < 0) {
- abort();
+ if (n->Store->File == PROGRAM_UNDEFINED && n->Store->Index < 0) {
+ /* ordinary local var */
+ assert(n->Store->Size > 0);
+ n->Store->File = PROGRAM_TEMPORARY;
+ n->Store->Index = alloc_temporary(gc, n->Store->Size);
+ }
}
}
/**
* Swizzle a swizzle.
*/
+#if 0
static GLuint
swizzle_compose(GLuint swz1, GLuint swz2)
{
swz = MAKE_SWIZZLE4(s[0], s[1], s[2], s[3]);
return swz;
}
+#endif
/**
};
dst->File = st->File;
dst->Index = st->Index;
+ assert(st->File != PROGRAM_UNDEFINED);
assert(st->Size >= 1);
assert(st->Size <= 4);
dst->WriteMask = defaultWritemask[st->Size - 1] & writemask;
src->File = st->File;
src->Index = st->Index;
+ assert(st->File != PROGRAM_UNDEFINED);
assert(st->Size >= 1);
assert(st->Size <= 4);
/* XXX swizzling logic here may need some work */
* Just modify the RHS to put its result into the dest of this
* MOVE operation. Then, this MOVE is a no-op.
*/
- free_temporary(gc, n->Children[1]->Store->Index);
+ free_temporary(gc, n->Children[1]->Store->Index,
+ n->Children[1]->Store->Size);
*n->Children[1]->Store = *n->Children[0]->Store;
/* fixup the prev (RHS) instruction */
storage_to_dst_reg(&inst->DstReg, n->Children[0]->Store, n->Writemask);
else
#endif
{
- inst = new_instruction(prog, OPCODE_MOV);
- storage_to_dst_reg(&inst->DstReg, n->Children[0]->Store, n->Writemask);
- storage_to_src_reg(&inst->SrcReg[0], n->Children[1]->Store,
- n->Children[1]->Swizzle);
+#if 1
+ if (n->Children[0]->Store->Size > 4) {
+ /* move matrix/struct etc */
+ slang_ir_storage dstStore = *n->Children[0]->Store;
+ slang_ir_storage srcStore = *n->Children[1]->Store;
+ GLint size = srcStore.Size;
+ ASSERT(n->Children[0]->Writemask == WRITEMASK_XYZW);
+ ASSERT(n->Children[1]->Swizzle == SWIZZLE_NOOP);
+ dstStore.Size = 4;
+ srcStore.Size = 4;
+ while (size >= 4) {
+ inst = new_instruction(prog, OPCODE_MOV);
+ inst->Comment = _mesa_strdup("IR_MOVE block");
+ storage_to_dst_reg(&inst->DstReg, &dstStore, n->Writemask);
+ storage_to_src_reg(&inst->SrcReg[0], &srcStore,
+ n->Children[1]->Swizzle);
+ srcStore.Index++;
+ dstStore.Index++;
+ size -= 4;
+ }
+ }
+ else
+#endif
+ {
+ inst = new_instruction(prog, OPCODE_MOV);
+ storage_to_dst_reg(&inst->DstReg, n->Children[0]->Store, n->Writemask);
+ storage_to_src_reg(&inst->SrcReg[0], n->Children[1]->Store,
+ n->Children[1]->Swizzle);
+ }
if (n->Children[1]->Store->File == PROGRAM_TEMPORARY) {
- free_temporary(gc, n->Children[1]->Store->Index);
+ free_temporary(gc, n->Children[1]->Store->Index,
+ n->Children[1]->Store->Size);
}
- inst->Comment = n->Comment;
+ /*inst->Comment = _mesa_strdup("IR_MOVE");*/
n->Store = n->Children[0]->Store; /*XXX new */
return inst;
}
}
+slang_gen_context *
+_slang_new_codegen_context(void)
+{
+ slang_gen_context *gc = (slang_gen_context *) _mesa_calloc(sizeof(*gc));
+ return gc;
+}
+
+
GLboolean
-_slang_emit_code(slang_ir_node *n, struct gl_program *prog)
+_slang_emit_code(slang_ir_node *n, slang_gen_context *gc,
+ struct gl_program *prog)
{
- slang_gen_context *gc;
/*GET_CURRENT_CONTEXT(ctx);*/
- gc = (slang_gen_context *) _mesa_calloc(sizeof(*gc));
+ /*
+ gc = _slang_new_codegen_context();
+ */
printf("************ Begin generate code\n");