2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file slang_codegen.c
27 * Generate IR tree from AST.
34 *** The new_() functions return a new instance of a simple IR node.
35 *** The gen_() functions generate larger IR trees from the simple nodes.
44 #include "prog_instruction.h"
45 #include "prog_parameter.h"
46 #include "prog_statevars.h"
47 #include "slang_typeinfo.h"
48 #include "slang_codegen.h"
49 #include "slang_compile.h"
50 #include "slang_label.h"
51 #include "slang_simplify.h"
52 #include "slang_emit.h"
53 #include "slang_vartable.h"
55 #include "slang_print.h"
58 static slang_ir_node
*
59 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
);
63 is_sampler_type(const slang_fully_specified_type
*t
)
65 switch (t
->specifier
.type
) {
66 case SLANG_SPEC_SAMPLER1D
:
67 case SLANG_SPEC_SAMPLER2D
:
68 case SLANG_SPEC_SAMPLER3D
:
69 case SLANG_SPEC_SAMPLERCUBE
:
70 case SLANG_SPEC_SAMPLER1DSHADOW
:
71 case SLANG_SPEC_SAMPLER2DSHADOW
:
80 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
87 case SLANG_SPEC_BVEC2
:
89 case SLANG_SPEC_BVEC3
:
91 case SLANG_SPEC_BVEC4
:
95 case SLANG_SPEC_IVEC2
:
97 case SLANG_SPEC_IVEC3
:
99 case SLANG_SPEC_IVEC4
:
101 case SLANG_SPEC_FLOAT
:
103 case SLANG_SPEC_VEC2
:
105 case SLANG_SPEC_VEC3
:
107 case SLANG_SPEC_VEC4
:
109 case SLANG_SPEC_MAT2
:
111 case SLANG_SPEC_MAT3
:
113 case SLANG_SPEC_MAT4
:
115 case SLANG_SPEC_SAMPLER1D
:
116 case SLANG_SPEC_SAMPLER2D
:
117 case SLANG_SPEC_SAMPLER3D
:
118 case SLANG_SPEC_SAMPLERCUBE
:
119 case SLANG_SPEC_SAMPLER1DSHADOW
:
120 case SLANG_SPEC_SAMPLER2DSHADOW
:
121 return 1; /* special case */
122 case SLANG_SPEC_STRUCT
:
125 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
126 slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
127 GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
128 /* XXX verify padding */
135 case SLANG_SPEC_ARRAY
:
136 return _slang_sizeof_type_specifier(spec
->_array
);
138 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
146 * Establish the binding between a slang_ir_node and a slang_variable.
147 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
148 * The IR node must be a IR_VAR or IR_VAR_DECL node.
149 * \param n the IR node
150 * \param var the variable to associate with the IR node
153 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
157 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
158 assert(!n
->Var
|| n
->Var
== var
);
163 /* need to setup storage */
164 if (n
->Var
&& n
->Var
->aux
) {
165 /* node storage info = var storage info */
166 n
->Store
= (slang_ir_storage
*) n
->Var
->aux
;
169 /* alloc new storage info */
170 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -5);
172 n
->Var
->aux
= n
->Store
;
180 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
181 * or -1 if the type is not a sampler.
184 sampler_to_texture_index(const slang_type_specifier_type type
)
187 case SLANG_SPEC_SAMPLER1D
:
188 return TEXTURE_1D_INDEX
;
189 case SLANG_SPEC_SAMPLER2D
:
190 return TEXTURE_2D_INDEX
;
191 case SLANG_SPEC_SAMPLER3D
:
192 return TEXTURE_3D_INDEX
;
193 case SLANG_SPEC_SAMPLERCUBE
:
194 return TEXTURE_CUBE_INDEX
;
195 case SLANG_SPEC_SAMPLER1DSHADOW
:
196 return TEXTURE_1D_INDEX
; /* XXX fix */
197 case SLANG_SPEC_SAMPLER2DSHADOW
:
198 return TEXTURE_2D_INDEX
; /* XXX fix */
206 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
207 * a vertex or fragment program input variable. Return -1 if the input
209 * XXX return size too
212 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
219 static const struct input_info vertInputs
[] = {
220 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
221 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
222 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
223 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
224 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
225 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
226 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
227 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
228 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
229 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
230 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
231 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
232 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
233 { NULL
, 0, SWIZZLE_NOOP
}
235 static const struct input_info fragInputs
[] = {
236 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
237 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
238 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
239 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
240 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
241 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
242 { NULL
, 0, SWIZZLE_NOOP
}
245 const struct input_info
*inputs
246 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
248 ASSERT(MAX_TEXTURE_UNITS
== 8); /* if this fails, fix vertInputs above */
250 for (i
= 0; inputs
[i
].Name
; i
++) {
251 if (strcmp(inputs
[i
].Name
, name
) == 0) {
253 *swizzleOut
= inputs
[i
].Swizzle
;
254 return inputs
[i
].Attrib
;
262 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
263 * a vertex or fragment program output variable. Return -1 for an invalid
267 _slang_output_index(const char *name
, GLenum target
)
273 static const struct output_info vertOutputs
[] = {
274 { "gl_Position", VERT_RESULT_HPOS
},
275 { "gl_FrontColor", VERT_RESULT_COL0
},
276 { "gl_BackColor", VERT_RESULT_BFC0
},
277 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
278 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
279 { "gl_TexCoord", VERT_RESULT_TEX0
}, /* XXX indexed */
280 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
281 { "gl_PointSize", VERT_RESULT_PSIZ
},
284 static const struct output_info fragOutputs
[] = {
285 { "gl_FragColor", FRAG_RESULT_COLR
},
286 { "gl_FragDepth", FRAG_RESULT_DEPR
},
290 const struct output_info
*outputs
291 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
293 for (i
= 0; outputs
[i
].Name
; i
++) {
294 if (strcmp(outputs
[i
].Name
, name
) == 0) {
296 return outputs
[i
].Attrib
;
304 /**********************************************************************/
308 * Map "_asm foo" to IR_FOO, etc.
313 slang_ir_opcode Opcode
;
314 GLuint HaveRetValue
, NumParams
;
318 static slang_asm_info AsmInfo
[] = {
320 { "vec4_add", IR_ADD
, 1, 2 },
321 { "vec4_subtract", IR_SUB
, 1, 2 },
322 { "vec4_multiply", IR_MUL
, 1, 2 },
323 { "vec4_dot", IR_DOT4
, 1, 2 },
324 { "vec3_dot", IR_DOT3
, 1, 2 },
325 { "vec3_cross", IR_CROSS
, 1, 2 },
326 { "vec4_lrp", IR_LRP
, 1, 3 },
327 { "vec4_min", IR_MIN
, 1, 2 },
328 { "vec4_max", IR_MAX
, 1, 2 },
329 { "vec4_clamp", IR_CLAMP
, 1, 3 },
330 { "vec4_seq", IR_SEQUAL
, 1, 2 },
331 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
332 { "vec4_sge", IR_SGE
, 1, 2 },
333 { "vec4_sgt", IR_SGT
, 1, 2 },
334 { "vec4_sle", IR_SLE
, 1, 2 },
335 { "vec4_slt", IR_SLT
, 1, 2 },
337 { "vec4_floor", IR_FLOOR
, 1, 1 },
338 { "vec4_frac", IR_FRAC
, 1, 1 },
339 { "vec4_abs", IR_ABS
, 1, 1 },
340 { "vec4_negate", IR_NEG
, 1, 1 },
341 { "vec4_ddx", IR_DDX
, 1, 1 },
342 { "vec4_ddy", IR_DDY
, 1, 1 },
343 /* float binary op */
344 { "float_add", IR_ADD
, 1, 2 },
345 { "float_multiply", IR_MUL
, 1, 2 },
346 { "float_divide", IR_DIV
, 1, 2 },
347 { "float_power", IR_POW
, 1, 2 },
348 /* texture / sampler */
349 { "vec4_tex1d", IR_TEX
, 1, 2 },
350 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
351 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
352 { "vec4_tex2d", IR_TEX
, 1, 2 },
353 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
354 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
355 { "vec4_tex3d", IR_TEX
, 1, 2 },
356 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
357 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
358 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
361 { "int_to_float", IR_I_TO_F
, 1, 1 },
362 { "float_to_int", IR_F_TO_I
, 1, 1 },
363 { "float_exp", IR_EXP
, 1, 1 },
364 { "float_exp2", IR_EXP2
, 1, 1 },
365 { "float_log2", IR_LOG2
, 1, 1 },
366 { "float_rsq", IR_RSQ
, 1, 1 },
367 { "float_rcp", IR_RCP
, 1, 1 },
368 { "float_sine", IR_SIN
, 1, 1 },
369 { "float_cosine", IR_COS
, 1, 1 },
370 { "float_noise1", IR_NOISE1
, 1, 1},
371 { "float_noise2", IR_NOISE2
, 1, 1},
372 { "float_noise3", IR_NOISE3
, 1, 1},
373 { "float_noise4", IR_NOISE4
, 1, 1},
375 { NULL
, IR_NOP
, 0, 0 }
380 * Recursively free an IR tree.
383 _slang_free_ir_tree(slang_ir_node
*n
)
389 for (i
= 0; i
< 3; i
++)
390 _slang_free_ir_tree(n
->Children
[i
]);
391 /* Do not free n->BranchNode since it's a child elsewhere */
397 static slang_ir_node
*
398 new_node3(slang_ir_opcode op
,
399 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
401 slang_ir_node
*n
= (slang_ir_node
*) calloc(1, sizeof(slang_ir_node
));
407 n
->Writemask
= WRITEMASK_XYZW
;
408 n
->InstLocation
= -1;
413 static slang_ir_node
*
414 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
416 return new_node3(op
, c0
, c1
, NULL
);
419 static slang_ir_node
*
420 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
422 return new_node3(op
, c0
, NULL
, NULL
);
425 static slang_ir_node
*
426 new_node0(slang_ir_opcode op
)
428 return new_node3(op
, NULL
, NULL
, NULL
);
432 static slang_ir_node
*
433 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
439 return new_node2(IR_SEQ
, left
, right
);
442 static slang_ir_node
*
443 new_label(slang_label
*label
)
445 slang_ir_node
*n
= new_node0(IR_LABEL
);
452 static slang_ir_node
*
453 new_float_literal(const float v
[4])
455 const GLuint size
= (v
[0] == v
[1] && v
[0] == v
[2] && v
[0] == v
[3]) ? 1 : 4;
456 slang_ir_node
*n
= new_node0(IR_FLOAT
);
457 COPY_4V(n
->Value
, v
);
458 /* allocate a storage object, but compute actual location (Index) later */
459 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
464 * Unconditional jump.
466 static slang_ir_node
*
467 new_jump(slang_label
*dest
)
469 slang_ir_node
*n
= new_node0(IR_JUMP
);
477 static slang_ir_node
*
478 new_loop(slang_ir_node
*body
)
480 return new_node1(IR_LOOP
, body
);
484 static slang_ir_node
*
485 new_break(slang_ir_node
*loopNode
)
487 slang_ir_node
*n
= new_node0(IR_BREAK
);
489 assert(loopNode
->Opcode
== IR_LOOP
);
491 /* insert this node at head of linked list */
492 n
->BranchNode
= loopNode
->BranchNode
;
493 loopNode
->BranchNode
= n
;
500 * Make new IR_BREAK_IF_TRUE or IR_BREAK_IF_FALSE node.
502 static slang_ir_node
*
503 new_break_if(slang_ir_node
*loopNode
, slang_ir_node
*cond
, GLboolean breakTrue
)
507 assert(loopNode
->Opcode
== IR_LOOP
);
508 n
= new_node1(breakTrue
? IR_BREAK_IF_TRUE
: IR_BREAK_IF_FALSE
, cond
);
510 /* insert this node at head of linked list */
511 n
->BranchNode
= loopNode
->BranchNode
;
512 loopNode
->BranchNode
= n
;
519 * Make new IR_CONT_IF_TRUE or IR_CONT_IF_FALSE node.
521 static slang_ir_node
*
522 new_cont_if(slang_ir_node
*loopNode
, slang_ir_node
*cond
, GLboolean contTrue
)
526 assert(loopNode
->Opcode
== IR_LOOP
);
527 n
= new_node1(contTrue
? IR_CONT_IF_TRUE
: IR_CONT_IF_FALSE
, cond
);
529 /* insert this node at head of linked list */
530 n
->BranchNode
= loopNode
->BranchNode
;
531 loopNode
->BranchNode
= n
;
537 static slang_ir_node
*
538 new_cont(slang_ir_node
*loopNode
)
540 slang_ir_node
*n
= new_node0(IR_CONT
);
542 assert(loopNode
->Opcode
== IR_LOOP
);
544 /* insert this node at head of linked list */
545 n
->BranchNode
= loopNode
->BranchNode
;
546 loopNode
->BranchNode
= n
;
552 static slang_ir_node
*
553 new_cond(slang_ir_node
*n
)
555 slang_ir_node
*c
= new_node1(IR_COND
, n
);
560 static slang_ir_node
*
561 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
563 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
568 * New IR_VAR node - a reference to a previously declared variable.
570 static slang_ir_node
*
571 new_var(slang_assemble_ctx
*A
, slang_operation
*oper
, slang_atom name
)
574 slang_variable
*var
= _slang_locate_variable(oper
->locals
, name
, GL_TRUE
);
578 assert(!oper
->var
|| oper
->var
== var
);
580 n
= new_node0(IR_VAR
);
582 _slang_attach_storage(n
, var
);
589 * Check if the given function is really just a wrapper for a
590 * basic assembly instruction.
593 slang_is_asm_function(const slang_function
*fun
)
595 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
596 fun
->body
->num_children
== 1 &&
597 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
605 _slang_is_noop(const slang_operation
*oper
)
608 oper
->type
== SLANG_OPER_VOID
||
609 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
617 * Produce inline code for a call to an assembly instruction.
618 * XXX Note: children are passed as asm args in-order, not by name!
620 static slang_operation
*
621 slang_inline_asm_function(slang_assemble_ctx
*A
,
622 slang_function
*fun
, slang_operation
*oper
)
624 const GLuint numArgs
= oper
->num_children
;
625 const slang_operation
*args
= oper
->children
;
627 slang_operation
*inlined
= slang_operation_new(1);
629 /*assert(oper->type == SLANG_OPER_CALL); or vec4_add, etc */
631 printf("Inline asm %s\n", (char*) fun->header.a_name);
633 inlined
->type
= fun
->body
->children
[0].type
;
634 inlined
->a_id
= fun
->body
->children
[0].a_id
;
635 inlined
->num_children
= numArgs
;
636 inlined
->children
= slang_operation_new(numArgs
);
637 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
639 for (i
= 0; i
< numArgs
; i
++) {
640 slang_operation_copy(inlined
->children
+ i
, args
+ i
);
648 slang_resolve_variable(slang_operation
*oper
)
650 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
651 oper
->var
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
657 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
660 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
661 GLuint substCount
, slang_variable
**substOld
,
662 slang_operation
**substNew
, GLboolean isLHS
)
664 switch (oper
->type
) {
665 case SLANG_OPER_VARIABLE_DECL
:
667 slang_variable
*v
= _slang_locate_variable(oper
->locals
,
668 oper
->a_id
, GL_TRUE
);
670 if (v
->initializer
&& oper
->num_children
== 0) {
671 /* set child of oper to copy of initializer */
672 oper
->num_children
= 1;
673 oper
->children
= slang_operation_new(1);
674 slang_operation_copy(&oper
->children
[0], v
->initializer
);
676 if (oper
->num_children
== 1) {
677 /* the initializer */
678 slang_substitute(A
, &oper
->children
[0], substCount
,
679 substOld
, substNew
, GL_FALSE
);
683 case SLANG_OPER_IDENTIFIER
:
684 assert(oper
->num_children
== 0);
685 if (1/**!isLHS XXX FIX */) {
686 slang_atom id
= oper
->a_id
;
689 v
= _slang_locate_variable(oper
->locals
, id
, GL_TRUE
);
691 printf("var %s not found!\n", (char *) oper
->a_id
);
692 _slang_print_var_scope(oper
->locals
, 6);
698 /* look for a substitution */
699 for (i
= 0; i
< substCount
; i
++) {
700 if (v
== substOld
[i
]) {
701 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
702 #if 0 /* DEBUG only */
703 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
704 assert(substNew
[i
]->var
);
705 assert(substNew
[i
]->var
->a_name
);
706 printf("Substitute %s with %s in id node %p\n",
707 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
711 printf("Substitute %s with %f in id node %p\n",
712 (char*)v
->a_name
, substNew
[i
]->literal
[0],
716 slang_operation_copy(oper
, substNew
[i
]);
723 case SLANG_OPER_RETURN
:
724 /* do return replacement here too */
725 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
726 if (!_slang_is_noop(oper
)) {
732 * then do substitutions on the assignment.
734 slang_operation
*blockOper
, *assignOper
, *returnOper
;
735 blockOper
= slang_operation_new(1);
736 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
737 blockOper
->num_children
= 2;
738 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
739 blockOper
->children
= slang_operation_new(2);
740 assignOper
= blockOper
->children
+ 0;
741 returnOper
= blockOper
->children
+ 1;
743 assignOper
->type
= SLANG_OPER_ASSIGN
;
744 assignOper
->num_children
= 2;
745 assignOper
->locals
->outer_scope
= blockOper
->locals
;
746 assignOper
->children
= slang_operation_new(2);
747 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
748 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
749 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
751 slang_operation_copy(&assignOper
->children
[1],
754 returnOper
->type
= SLANG_OPER_RETURN
;
755 assert(returnOper
->num_children
== 0);
757 /* do substitutions on the "__retVal = expr" sub-tree */
758 slang_substitute(A
, assignOper
,
759 substCount
, substOld
, substNew
, GL_FALSE
);
761 /* install new code */
762 slang_operation_copy(oper
, blockOper
);
763 slang_operation_destruct(blockOper
);
767 case SLANG_OPER_ASSIGN
:
768 case SLANG_OPER_SUBSCRIPT
:
770 * child[0] can't have substitutions but child[1] can.
772 slang_substitute(A
, &oper
->children
[0],
773 substCount
, substOld
, substNew
, GL_TRUE
);
774 slang_substitute(A
, &oper
->children
[1],
775 substCount
, substOld
, substNew
, GL_FALSE
);
777 case SLANG_OPER_FIELD
:
779 slang_substitute(A
, &oper
->children
[0],
780 substCount
, substOld
, substNew
, GL_TRUE
);
785 for (i
= 0; i
< oper
->num_children
; i
++)
786 slang_substitute(A
, &oper
->children
[i
],
787 substCount
, substOld
, substNew
, GL_FALSE
);
795 * Inline the given function call operation.
796 * Return a new slang_operation that corresponds to the inlined code.
798 static slang_operation
*
799 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
800 slang_operation
*oper
, slang_operation
*returnOper
)
807 ParamMode
*paramMode
;
808 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
809 const GLuint numArgs
= oper
->num_children
;
810 const GLuint totalArgs
= numArgs
+ haveRetValue
;
811 slang_operation
*args
= oper
->children
;
812 slang_operation
*inlined
, *top
;
813 slang_variable
**substOld
;
814 slang_operation
**substNew
;
815 GLuint substCount
, numCopyIn
, i
;
817 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
818 assert(fun
->param_count
== totalArgs
);
820 /* allocate temporary arrays */
821 paramMode
= (ParamMode
*)
822 _mesa_calloc(totalArgs
* sizeof(ParamMode
));
823 substOld
= (slang_variable
**)
824 _mesa_calloc(totalArgs
* sizeof(slang_variable
*));
825 substNew
= (slang_operation
**)
826 _mesa_calloc(totalArgs
* sizeof(slang_operation
*));
829 printf("Inline call to %s (total vars=%d nparams=%d)\n",
830 (char *) fun
->header
.a_name
,
831 fun
->parameters
->num_variables
, numArgs
);
834 if (haveRetValue
&& !returnOper
) {
835 /* Create 3-child comma sequence for inlined code:
836 * child[0]: declare __resultTmp
837 * child[1]: inlined function body
838 * child[2]: __resultTmp
840 slang_operation
*commaSeq
;
841 slang_operation
*declOper
= NULL
;
842 slang_variable
*resultVar
;
844 commaSeq
= slang_operation_new(1);
845 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
846 assert(commaSeq
->locals
);
847 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
848 commaSeq
->num_children
= 3;
849 commaSeq
->children
= slang_operation_new(3);
850 /* allocate the return var */
851 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
853 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
854 (void*)commaSeq->locals, (char *) fun->header.a_name);
857 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
858 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
859 resultVar
->isTemp
= GL_TRUE
;
861 /* child[0] = __resultTmp declaration */
862 declOper
= &commaSeq
->children
[0];
863 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
864 declOper
->a_id
= resultVar
->a_name
;
865 declOper
->locals
->outer_scope
= commaSeq
->locals
;
867 /* child[1] = function body */
868 inlined
= &commaSeq
->children
[1];
869 inlined
->locals
->outer_scope
= commaSeq
->locals
;
871 /* child[2] = __resultTmp reference */
872 returnOper
= &commaSeq
->children
[2];
873 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
874 returnOper
->a_id
= resultVar
->a_name
;
875 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
880 top
= inlined
= slang_operation_new(1);
881 /* XXXX this may be inappropriate!!!! */
882 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
886 assert(inlined
->locals
);
888 /* Examine the parameters, look for inout/out params, look for possible
889 * substitutions, etc:
890 * param type behaviour
891 * in copy actual to local
892 * const in substitute param with actual
896 for (i
= 0; i
< totalArgs
; i
++) {
897 slang_variable
*p
= fun
->parameters
->variables
[i
];
899 printf("Param %d: %s %s \n", i,
900 slang_type_qual_string(p->type.qualifier),
903 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
904 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
905 /* an output param */
906 slang_operation
*arg
;
911 paramMode
[i
] = SUBST
;
913 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
914 slang_resolve_variable(arg
);
916 /* replace parameter 'p' with argument 'arg' */
917 substOld
[substCount
] = p
;
918 substNew
[substCount
] = arg
; /* will get copied */
921 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
922 /* a constant input param */
923 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
924 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
925 /* replace all occurances of this parameter variable with the
926 * actual argument variable or a literal.
928 paramMode
[i
] = SUBST
;
929 slang_resolve_variable(&args
[i
]);
930 substOld
[substCount
] = p
;
931 substNew
[substCount
] = &args
[i
]; /* will get copied */
935 paramMode
[i
] = COPY_IN
;
939 paramMode
[i
] = COPY_IN
;
941 assert(paramMode
[i
]);
944 /* actual code inlining: */
945 slang_operation_copy(inlined
, fun
->body
);
947 /*** XXX review this */
948 assert(inlined
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
);
949 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
952 printf("======================= orig body code ======================\n");
953 printf("=== params scope = %p\n", (void*) fun
->parameters
);
954 slang_print_tree(fun
->body
, 8);
955 printf("======================= copied code =========================\n");
956 slang_print_tree(inlined
, 8);
959 /* do parameter substitution in inlined code: */
960 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
963 printf("======================= subst code ==========================\n");
964 slang_print_tree(inlined
, 8);
965 printf("=============================================================\n");
968 /* New prolog statements: (inserted before the inlined code)
969 * Copy the 'in' arguments.
972 for (i
= 0; i
< numArgs
; i
++) {
973 if (paramMode
[i
] == COPY_IN
) {
974 slang_variable
*p
= fun
->parameters
->variables
[i
];
975 /* declare parameter 'p' */
976 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
980 printf("COPY_IN %s from expr\n", (char*)p->a_name);
982 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
983 assert(decl
->locals
);
984 decl
->locals
->outer_scope
= inlined
->locals
;
985 decl
->a_id
= p
->a_name
;
986 decl
->num_children
= 1;
987 decl
->children
= slang_operation_new(1);
989 /* child[0] is the var's initializer */
990 slang_operation_copy(&decl
->children
[0], args
+ i
);
996 /* New epilog statements:
997 * 1. Create end of function label to jump to from return statements.
998 * 2. Copy the 'out' parameter vars
1001 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1003 inlined
->num_children
);
1004 lab
->type
= SLANG_OPER_LABEL
;
1005 lab
->label
= A
->curFuncEndLabel
;
1008 for (i
= 0; i
< totalArgs
; i
++) {
1009 if (paramMode
[i
] == COPY_OUT
) {
1010 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1011 /* actualCallVar = outParam */
1012 /*if (i > 0 || !haveRetValue)*/
1013 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1015 inlined
->num_children
);
1016 ass
->type
= SLANG_OPER_ASSIGN
;
1017 ass
->num_children
= 2;
1018 ass
->locals
->outer_scope
= inlined
->locals
;
1019 ass
->children
= slang_operation_new(2);
1020 ass
->children
[0] = args
[i
]; /*XXX copy */
1021 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1022 ass
->children
[1].a_id
= p
->a_name
;
1023 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1027 _mesa_free(paramMode
);
1028 _mesa_free(substOld
);
1029 _mesa_free(substNew
);
1032 printf("Done Inline call to %s (total vars=%d nparams=%d)\n",
1033 (char *) fun
->header
.a_name
,
1034 fun
->parameters
->num_variables
, numArgs
);
1035 slang_print_tree(top
, 0);
1041 static slang_ir_node
*
1042 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1043 slang_operation
*oper
, slang_operation
*dest
)
1046 slang_operation
*inlined
;
1047 slang_label
*prevFuncEndLabel
;
1050 prevFuncEndLabel
= A
->curFuncEndLabel
;
1051 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1052 A
->curFuncEndLabel
= _slang_label_new(name
);
1053 assert(A
->curFuncEndLabel
);
1055 if (slang_is_asm_function(fun
) && !dest
) {
1056 /* assemble assembly function - tree style */
1057 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1060 /* non-assembly function */
1061 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1064 /* Replace the function call with the inlined block */
1066 slang_operation_construct(oper
);
1067 slang_operation_copy(oper
, inlined
);
1074 assert(inlined
->locals
);
1075 printf("*** Inlined code for call to %s:\n",
1076 (char*) fun
->header
.a_name
);
1077 slang_print_tree(oper
, 10);
1081 n
= _slang_gen_operation(A
, oper
);
1083 /*_slang_label_delete(A->curFuncEndLabel);*/
1084 A
->curFuncEndLabel
= prevFuncEndLabel
;
1085 assert(A
->curFuncEndLabel
);
1091 static slang_asm_info
*
1092 slang_find_asm_info(const char *name
)
1095 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1096 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1105 make_writemask(const char *field
)
1111 mask
|= WRITEMASK_X
;
1114 mask
|= WRITEMASK_Y
;
1117 mask
|= WRITEMASK_Z
;
1120 mask
|= WRITEMASK_W
;
1128 return WRITEMASK_XYZW
;
1135 * Generate IR tree for an asm instruction/operation such as:
1136 * __asm vec4_dot __retVal.x, v1, v2;
1138 static slang_ir_node
*
1139 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1140 slang_operation
*dest
)
1142 const slang_asm_info
*info
;
1143 slang_ir_node
*kids
[3], *n
;
1144 GLuint j
, firstOperand
;
1146 assert(oper
->type
== SLANG_OPER_ASM
);
1148 info
= slang_find_asm_info((char *) oper
->a_id
);
1150 _mesa_problem(NULL
, "undefined __asm function %s\n",
1151 (char *) oper
->a_id
);
1154 assert(info
->NumParams
<= 3);
1156 if (info
->NumParams
== oper
->num_children
) {
1157 /* Storage for result is not specified.
1158 * Children[0], [1] are the operands.
1163 /* Storage for result (child[0]) is specified.
1164 * Children[1], [2] are the operands.
1169 /* assemble child(ren) */
1170 kids
[0] = kids
[1] = kids
[2] = NULL
;
1171 for (j
= 0; j
< info
->NumParams
; j
++) {
1172 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1177 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1180 /* Setup n->Store to be a particular location. Otherwise, storage
1181 * for the result (a temporary) will be allocated later.
1183 GLuint writemask
= WRITEMASK_XYZW
;
1184 slang_operation
*dest_oper
;
1187 dest_oper
= &oper
->children
[0];
1188 while (dest_oper
->type
== SLANG_OPER_FIELD
) {
1190 writemask
&= make_writemask((char*) dest_oper
->a_id
);
1191 dest_oper
= &dest_oper
->children
[0];
1194 n0
= _slang_gen_operation(A
, dest_oper
);
1198 n
->Store
= n0
->Store
;
1199 n
->Writemask
= writemask
;
1209 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1212 for (i
= 0; i
< scope
->num_functions
; i
++) {
1213 slang_function
*f
= &scope
->functions
[i
];
1214 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1215 printf(" %s (%d args)\n", name
, f
->param_count
);
1218 if (scope
->outer_scope
)
1219 print_funcs(scope
->outer_scope
, name
);
1224 * Return first function in the scope that has the given name.
1225 * This is the function we'll try to call when there is no exact match
1226 * between function parameters and call arguments.
1228 * XXX we should really create a list of candidate functions and try
1231 static slang_function
*
1232 _slang_first_function(struct slang_function_scope_
*scope
, const char *name
)
1235 for (i
= 0; i
< scope
->num_functions
; i
++) {
1236 slang_function
*f
= &scope
->functions
[i
];
1237 if (strcmp(name
, (char*) f
->header
.a_name
) == 0)
1240 if (scope
->outer_scope
)
1241 return _slang_first_function(scope
->outer_scope
, name
);
1248 * Assemble a function call, given a particular function name.
1249 * \param name the function's name (operators like '*' are possible).
1251 static slang_ir_node
*
1252 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
1253 slang_operation
*oper
, slang_operation
*dest
)
1255 slang_operation
*params
= oper
->children
;
1256 const GLuint param_count
= oper
->num_children
;
1258 slang_function
*fun
;
1260 atom
= slang_atom_pool_atom(A
->atoms
, name
);
1261 if (atom
== SLANG_ATOM_NULL
)
1265 * Use 'name' to find the function to call
1267 fun
= _slang_locate_function(A
->space
.funcs
, atom
, params
, param_count
,
1268 &A
->space
, A
->atoms
, A
->log
);
1270 /* A function with exactly the right parameters/types was not found.
1271 * Try adapting the parameters.
1273 fun
= _slang_first_function(A
->space
.funcs
, name
);
1274 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
1275 slang_info_log_error(A
->log
, "Undefined function '%s'", name
);
1281 return _slang_gen_function_call(A
, fun
, oper
, dest
);
1286 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
1288 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
1289 oper
->type
== SLANG_OPER_LITERAL_INT
||
1290 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
1291 if (oper
->literal
[0])
1297 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
1298 oper
->num_children
== 1) {
1299 return _slang_is_constant_cond(&oper
->children
[0], value
);
1307 * Generate loop code using high-level IR_LOOP instruction
1309 static slang_ir_node
*
1310 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1314 * BREAK if !expr (child[0])
1315 * body code (child[1])
1317 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
;
1318 GLboolean isConst
, constTrue
;
1320 /* Check if loop condition is a constant */
1321 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1323 if (isConst
&& !constTrue
) {
1324 /* loop is never executed! */
1325 return new_node0(IR_NOP
);
1328 loop
= new_loop(NULL
);
1330 /* save old, push new loop */
1331 prevLoop
= A
->CurLoop
;
1334 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[0]));
1335 if (isConst
&& constTrue
) {
1336 /* while(nonzero constant), no conditional break */
1340 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1342 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1343 loop
->Children
[0] = new_seq(breakIf
, body
);
1345 /* Do infinite loop detection */
1346 if (loop
->BranchNode
== 0 && isConst
&& constTrue
) {
1347 /* infinite loop detected */
1348 A
->CurLoop
= prevLoop
; /* clean-up */
1349 slang_info_log_error(A
->log
, "Infinite loop detected!");
1353 /* pop loop, restore prev */
1354 A
->CurLoop
= prevLoop
;
1361 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
1363 static slang_ir_node
*
1364 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1368 * body code (child[0])
1369 * BREAK if !expr (child[1])
1371 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
;
1372 GLboolean isConst
, constTrue
;
1374 /* Check if loop condition is a constant */
1375 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1377 loop
= new_loop(NULL
);
1379 /* save old, push new loop */
1380 prevLoop
= A
->CurLoop
;
1383 body
= _slang_gen_operation(A
, &oper
->children
[0]);
1384 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[1]));
1385 if (isConst
&& constTrue
) {
1386 /* while(nonzero constant), no conditional break */
1390 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1392 loop
->Children
[0] = new_seq(body
, breakIf
);
1394 /* pop loop, restore prev */
1395 A
->CurLoop
= prevLoop
;
1402 * Generate for-loop using high-level IR_LOOP instruction.
1404 static slang_ir_node
*
1405 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1410 * BREAK if !expr (child[1])
1411 * body code (child[3])
1412 * incr code (child[2]) // XXX continue here
1414 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
1416 init
= _slang_gen_operation(A
, &oper
->children
[0]);
1417 loop
= new_loop(NULL
);
1419 /* save old, push new loop */
1420 prevLoop
= A
->CurLoop
;
1423 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[1]));
1424 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1425 body
= _slang_gen_operation(A
, &oper
->children
[3]);
1426 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
1427 loop
->Children
[0] = new_seq(breakIf
,
1428 new_seq(body
, incr
));
1430 /* pop loop, restore prev */
1431 A
->CurLoop
= prevLoop
;
1433 return new_seq(init
, loop
);
1438 * Determine if the given operation is of a specific type.
1441 is_operation_type(const const slang_operation
*oper
, slang_operation_type type
)
1443 if (oper
->type
== type
)
1445 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1446 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
1447 oper
->num_children
== 1)
1448 return is_operation_type(&oper
->children
[0], type
);
1455 * Generate IR tree for an if/then/else conditional using high-level
1456 * IR_IF instruction.
1458 static slang_ir_node
*
1459 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1462 * eval expr (child[0]), updating condcodes
1469 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
1470 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
1472 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1473 cond
= new_cond(cond
);
1475 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)) {
1476 /* Special case: generate a conditional break */
1477 ifBody
= new_break_if(A
->CurLoop
, cond
, GL_TRUE
);
1478 if (haveElseClause
) {
1479 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1480 return new_seq(ifBody
, elseBody
);
1484 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)) {
1485 /* Special case: generate a conditional break */
1486 ifBody
= new_cont_if(A
->CurLoop
, cond
, GL_TRUE
);
1487 if (haveElseClause
) {
1488 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1489 return new_seq(ifBody
, elseBody
);
1495 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
1497 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1500 ifNode
= new_if(cond
, ifBody
, elseBody
);
1508 * Generate IR node for storage of a temporary of given size.
1510 static slang_ir_node
*
1511 _slang_gen_temporary(GLint size
)
1513 slang_ir_storage
*store
;
1516 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
1518 n
= new_node0(IR_VAR_DECL
);
1531 * Generate IR node for allocating/declaring a variable.
1533 static slang_ir_node
*
1534 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
)
1537 assert(!is_sampler_type(&var
->type
));
1538 n
= new_node0(IR_VAR_DECL
);
1540 _slang_attach_storage(n
, var
);
1543 assert(n
->Store
== var
->aux
);
1545 assert(n
->Store
->Index
< 0);
1547 n
->Store
->File
= PROGRAM_TEMPORARY
;
1548 n
->Store
->Size
= _slang_sizeof_type_specifier(&n
->Var
->type
.specifier
);
1549 assert(n
->Store
->Size
> 0);
1556 * Generate code for a selection expression: b ? x : y
1557 * XXX In some cases we could implement a selection expression
1558 * with an LRP instruction (use the boolean as the interpolant).
1559 * Otherwise, we use an IF/ELSE/ENDIF construct.
1561 static slang_ir_node
*
1562 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
1564 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
1565 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
1566 slang_typeinfo type
;
1569 assert(oper
->type
== SLANG_OPER_SELECT
);
1570 assert(oper
->num_children
== 3);
1572 /* size of x or y's type */
1573 slang_typeinfo_construct(&type
);
1574 _slang_typeof_operation(A
, &oper
->children
[1], &type
);
1575 size
= _slang_sizeof_type_specifier(&type
.spec
);
1579 tmpDecl
= _slang_gen_temporary(size
);
1581 /* the condition (child 0) */
1582 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1583 cond
= new_cond(cond
);
1585 /* if-true body (child 1) */
1586 tmpVar
= new_node0(IR_VAR
);
1587 tmpVar
->Store
= tmpDecl
->Store
;
1588 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
1589 trueNode
= new_node2(IR_MOVE
, tmpVar
, trueExpr
);
1591 /* if-false body (child 2) */
1592 tmpVar
= new_node0(IR_VAR
);
1593 tmpVar
->Store
= tmpDecl
->Store
;
1594 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
1595 falseNode
= new_node2(IR_MOVE
, tmpVar
, falseExpr
);
1597 ifNode
= new_if(cond
, trueNode
, falseNode
);
1600 tmpVar
= new_node0(IR_VAR
);
1601 tmpVar
->Store
= tmpDecl
->Store
;
1603 tree
= new_seq(ifNode
, tmpVar
);
1604 tree
= new_seq(tmpDecl
, tree
);
1606 slang_print_ir(tree
, 10);
1612 * Generate code for &&.
1614 static slang_ir_node
*
1615 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
1617 /* rewrite "a && b" as "a ? b : false" */
1618 slang_operation
*select
;
1621 select
= slang_operation_new(1);
1622 select
->type
= SLANG_OPER_SELECT
;
1623 select
->num_children
= 3;
1624 select
->children
= slang_operation_new(3);
1626 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1627 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
1628 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
1629 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
1630 select
->children
[2].literal_size
= 1;
1632 n
= _slang_gen_select(A
, select
);
1635 free(select
->children
);
1643 * Generate code for ||.
1645 static slang_ir_node
*
1646 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
1648 /* rewrite "a || b" as "a ? true : b" */
1649 slang_operation
*select
;
1652 select
= slang_operation_new(1);
1653 select
->type
= SLANG_OPER_SELECT
;
1654 select
->num_children
= 3;
1655 select
->children
= slang_operation_new(3);
1657 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1658 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
1659 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
1660 select
->children
[1].literal_size
= 1;
1661 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
1663 n
= _slang_gen_select(A
, select
);
1666 free(select
->children
);
1674 * Generate IR tree for a return statement.
1676 static slang_ir_node
*
1677 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
1679 if (oper
->num_children
== 0 ||
1680 (oper
->num_children
== 1 &&
1681 oper
->children
[0].type
== SLANG_OPER_VOID
)) {
1685 * goto __endOfFunction;
1688 slang_operation gotoOp
;
1689 slang_operation_construct(&gotoOp
);
1690 gotoOp
.type
= SLANG_OPER_GOTO
;
1691 gotoOp
.label
= A
->curFuncEndLabel
;
1692 assert(gotoOp
.label
);
1694 /* assemble the new code */
1695 n
= _slang_gen_operation(A
, &gotoOp
);
1696 /* destroy temp code */
1697 slang_operation_destruct(&gotoOp
);
1706 * goto __endOfFunction;
1708 slang_operation
*block
, *assign
, *jump
;
1709 slang_atom a_retVal
;
1712 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1718 = _slang_locate_variable(oper
->locals
, a_retVal
, GL_TRUE
);
1723 block
= slang_operation_new(1);
1724 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1725 assert(block
->locals
);
1726 block
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1727 block
->num_children
= 2;
1728 block
->children
= slang_operation_new(2);
1730 /* child[0]: __retVal = expr; */
1731 assign
= &block
->children
[0];
1732 assign
->type
= SLANG_OPER_ASSIGN
;
1733 assign
->locals
->outer_scope
= block
->locals
;
1734 assign
->num_children
= 2;
1735 assign
->children
= slang_operation_new(2);
1736 /* lhs (__retVal) */
1737 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1738 assign
->children
[0].a_id
= a_retVal
;
1739 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
1741 /* XXX we might be able to avoid this copy someday */
1742 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
1744 /* child[1]: goto __endOfFunction */
1745 jump
= &block
->children
[1];
1746 jump
->type
= SLANG_OPER_GOTO
;
1747 assert(A
->curFuncEndLabel
);
1748 /* XXX don't call function? */
1749 jump
->label
= A
->curFuncEndLabel
;
1750 assert(jump
->label
);
1753 printf("NEW RETURN:\n");
1754 slang_print_tree(block
, 0);
1757 /* assemble the new code */
1758 n
= _slang_gen_operation(A
, block
);
1759 slang_operation_delete(block
);
1766 * Generate IR tree for a variable declaration.
1768 static slang_ir_node
*
1769 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
1772 slang_ir_node
*varDecl
;
1774 const char *varName
= (char *) oper
->a_id
;
1776 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
1778 v
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
1781 varDecl
= _slang_gen_var_decl(A
, v
);
1783 if (oper
->num_children
> 0) {
1784 /* child is initializer */
1785 slang_ir_node
*var
, *init
, *rhs
;
1786 assert(oper
->num_children
== 1);
1787 var
= new_var(A
, oper
, oper
->a_id
);
1789 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
1792 /* XXX make copy of this initializer? */
1793 rhs
= _slang_gen_operation(A
, &oper
->children
[0]);
1795 init
= new_node2(IR_MOVE
, var
, rhs
);
1796 /*assert(rhs->Opcode != IR_SEQ);*/
1797 n
= new_seq(varDecl
, init
);
1799 else if (v
->initializer
) {
1800 slang_ir_node
*var
, *init
, *rhs
;
1801 var
= new_var(A
, oper
, oper
->a_id
);
1803 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
1807 /* XXX make copy of this initializer? */
1809 slang_operation dup
;
1810 slang_operation_construct(&dup
);
1811 slang_operation_copy(&dup
, v
->initializer
);
1812 _slang_simplify(&dup
, &A
->space
, A
->atoms
);
1813 rhs
= _slang_gen_operation(A
, &dup
);
1816 _slang_simplify(v
->initializer
, &A
->space
, A
->atoms
);
1817 rhs
= _slang_gen_operation(A
, v
->initializer
);
1820 init
= new_node2(IR_MOVE
, var
, rhs
);
1822 assert(rhs->Opcode != IR_SEQ);
1824 n
= new_seq(varDecl
, init
);
1834 * Generate IR tree for a variable (such as in an expression).
1836 static slang_ir_node
*
1837 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
1839 /* If there's a variable associated with this oper (from inlining)
1840 * use it. Otherwise, use the oper's var id.
1842 slang_atom aVar
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
1843 slang_ir_node
*n
= new_var(A
, oper
, aVar
);
1845 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) aVar
);
1853 * Some write-masked assignments are simple, but others are hard.
1856 * v.xy = vec2(a, b);
1859 * v.zy = vec2(a, b);
1860 * this gets transformed/swizzled into:
1861 * v.zy = vec2(a, b).*yx* (* = don't care)
1862 * This function helps to determine simple vs. non-simple.
1865 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1867 switch (writemask
) {
1869 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1871 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1873 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1875 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1877 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1878 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1880 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1881 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1882 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1883 case WRITEMASK_XYZW
:
1884 return swizzle
== SWIZZLE_NOOP
;
1892 * Convert the given swizzle into a writemask. In some cases this
1893 * is trivial, in other cases, we'll need to also swizzle the right
1894 * hand side to put components in the right places.
1895 * \param swizzle the incoming swizzle
1896 * \param writemaskOut returns the writemask
1897 * \param swizzleOut swizzle to apply to the right-hand-side
1898 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1901 swizzle_to_writemask(GLuint swizzle
,
1902 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1904 GLuint mask
= 0x0, newSwizzle
[4];
1907 /* make new dst writemask, compute size */
1908 for (i
= 0; i
< 4; i
++) {
1909 const GLuint swz
= GET_SWZ(swizzle
, i
);
1910 if (swz
== SWIZZLE_NIL
) {
1914 assert(swz
>= 0 && swz
<= 3);
1917 assert(mask
<= 0xf);
1918 size
= i
; /* number of components in mask/swizzle */
1920 *writemaskOut
= mask
;
1922 /* make new src swizzle, by inversion */
1923 for (i
= 0; i
< 4; i
++) {
1924 newSwizzle
[i
] = i
; /*identity*/
1926 for (i
= 0; i
< size
; i
++) {
1927 const GLuint swz
= GET_SWZ(swizzle
, i
);
1928 newSwizzle
[swz
] = i
;
1930 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1935 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1937 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1939 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1941 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1943 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1951 static slang_ir_node
*
1952 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
1954 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
1956 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -1);
1957 n
->Store
->Swizzle
= swizzle
;
1964 * Generate IR tree for an assignment (=).
1966 static slang_ir_node
*
1967 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
1969 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
1970 oper
->children
[1].type
== SLANG_OPER_CALL
) {
1971 /* Special case of: x = f(a, b)
1972 * Replace with f(a, b, x) (where x == hidden __retVal out param)
1974 * XXX this could be even more effective if we could accomodate
1975 * cases such as "v.x = f();" - would help with typical vertex
1979 n
= _slang_gen_function_call_name(A
,
1980 (const char *) oper
->children
[1].a_id
,
1981 &oper
->children
[1], &oper
->children
[0]);
1985 slang_ir_node
*n
, *lhs
, *rhs
;
1986 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
1987 if (lhs
->Store
->File
!= PROGRAM_OUTPUT
&&
1988 lhs
->Store
->File
!= PROGRAM_TEMPORARY
&&
1989 lhs
->Store
->File
!= PROGRAM_VARYING
&&
1990 lhs
->Store
->File
!= PROGRAM_UNDEFINED
) {
1991 slang_info_log_error(A
->log
, "Assignment to read-only variable");
1995 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
1997 /* convert lhs swizzle into writemask */
1998 GLuint writemask
, newSwizzle
;
1999 if (!swizzle_to_writemask(lhs
->Store
->Swizzle
,
2000 &writemask
, &newSwizzle
)) {
2001 /* Non-simple writemask, need to swizzle right hand side in
2002 * order to put components into the right place.
2004 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
2006 n
= new_node2(IR_MOVE
, lhs
, rhs
);
2007 n
->Writemask
= writemask
;
2018 * Generate IR tree for referencing a field in a struct (or basic vector type)
2020 static slang_ir_node
*
2021 _slang_gen_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
2025 slang_typeinfo_construct(&ti
);
2026 _slang_typeof_operation(A
, &oper
->children
[0], &ti
);
2028 if (_slang_type_is_vector(ti
.spec
.type
)) {
2029 /* the field should be a swizzle */
2030 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
2034 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2035 slang_info_log_error(A
->log
, "Bad swizzle");
2037 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2042 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2043 /* create new parent node with swizzle */
2044 n
= _slang_gen_swizzle(n
, swizzle
);
2047 else if (ti
.spec
.type
== SLANG_SPEC_FLOAT
) {
2048 const GLuint rows
= 1;
2052 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2053 slang_info_log_error(A
->log
, "Bad swizzle");
2055 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2059 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2060 /* create new parent node with swizzle */
2061 n
= _slang_gen_swizzle(n
, swizzle
);
2065 /* the field is a structure member (base.field) */
2066 /* oper->children[0] is the base */
2067 /* oper->a_id is the field name */
2068 slang_ir_node
*base
, *n
;
2069 GLint size
= 4; /* XXX fix? */
2071 base
= _slang_gen_operation(A
, &oper
->children
[0]);
2073 /* error previously found */
2077 n
= new_node1(IR_FIELD
, base
);
2079 n
->Field
= (char *) oper
->a_id
;
2080 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
2087 _mesa_problem(NULL
, "glsl structs/fields not supported yet");
2095 * Gen code for array indexing.
2097 static slang_ir_node
*
2098 _slang_gen_subscript(slang_assemble_ctx
* A
, slang_operation
*oper
)
2100 slang_typeinfo array_ti
;
2102 /* get array's type info */
2103 slang_typeinfo_construct(&array_ti
);
2104 _slang_typeof_operation(A
, &oper
->children
[0], &array_ti
);
2106 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
2107 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
2108 /* translate the index into a swizzle/writemask: "v.x=p" */
2109 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
2113 index
= (GLint
) oper
->children
[1].literal
[0];
2114 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
2116 slang_info_log_error(A
->log
, "Invalid array index for vector type");
2120 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2122 /* use swizzle to access the element */
2123 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
2127 n
= _slang_gen_swizzle(n
, swizzle
);
2128 /*n->Store = _slang_clone_ir_storage_swz(n->Store, */
2129 n
->Writemask
= WRITEMASK_X
<< index
;
2134 /* conventional array */
2135 slang_typeinfo elem_ti
;
2136 slang_ir_node
*elem
, *array
, *index
;
2139 /* size of array element */
2140 slang_typeinfo_construct(&elem_ti
);
2141 _slang_typeof_operation(A
, oper
, &elem_ti
);
2142 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
2143 if (elemSize
<= 0) {
2144 /* unknown var or type */
2145 slang_info_log_error(A
->log
, "Undefined var or type");
2149 array
= _slang_gen_operation(A
, &oper
->children
[0]);
2150 index
= _slang_gen_operation(A
, &oper
->children
[1]);
2151 if (array
&& index
) {
2152 elem
= new_node2(IR_ELEMENT
, array
, index
);
2153 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
2154 array
->Store
->Index
,
2156 /* XXX try to do some array bounds checking here */
2167 * Look for expressions such as: gl_ModelviewMatrix * gl_Vertex
2168 * and replace with this: gl_Vertex * gl_ModelviewMatrixTranpose
2169 * Since matrices are stored in column-major order, the second form of
2170 * multiplication is much more efficient (just 4 dot products).
2173 _slang_check_matmul_optimization(slang_assemble_ctx
*A
, slang_operation
*oper
)
2175 static const struct {
2177 const char *tranpose
;
2179 {"gl_ModelViewMatrix", "gl_ModelViewMatrixTranspose"},
2180 {"gl_ProjectionMatrix", "gl_ProjectionMatrixTranspose"},
2181 {"gl_ModelViewProjectionMatrix", "gl_ModelViewProjectionMatrixTranspose"},
2182 {"gl_TextureMatrix", "gl_TextureMatrixTranspose"},
2183 {"gl_NormalMatrix", "__NormalMatrixTranspose"},
2187 assert(oper
->type
== SLANG_OPER_MULTIPLY
);
2188 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
2190 for (i
= 0; matrices
[i
].orig
; i
++) {
2191 if (oper
->children
[0].a_id
2192 == slang_atom_pool_atom(A
->atoms
, matrices
[i
].orig
)) {
2194 _mesa_printf("Replace %s with %s\n",
2195 matrices[i].orig, matrices[i].tranpose);
2197 assert(oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
);
2198 oper
->children
[0].a_id
2199 = slang_atom_pool_atom(A
->atoms
, matrices
[i
].tranpose
);
2200 /* finally, swap the operands */
2201 _slang_operation_swap(&oper
->children
[0], &oper
->children
[1]);
2210 * Generate IR tree for a slang_operation (AST node)
2212 static slang_ir_node
*
2213 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
2215 switch (oper
->type
) {
2216 case SLANG_OPER_BLOCK_NEW_SCOPE
:
2220 _slang_push_var_table(A
->vartable
);
2222 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
2223 n
= _slang_gen_operation(A
, oper
);
2224 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
2226 _slang_pop_var_table(A
->vartable
);
2229 n
= new_node1(IR_SCOPE
, n
);
2234 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
2235 /* list of operations */
2236 if (oper
->num_children
> 0)
2238 slang_ir_node
*n
, *tree
= NULL
;
2241 for (i
= 0; i
< oper
->num_children
; i
++) {
2242 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2244 _slang_free_ir_tree(tree
);
2245 return NULL
; /* error must have occured */
2247 tree
= tree
? new_seq(tree
, n
) : n
;
2251 if (oper
->locals
->num_variables
> 0) {
2254 printf("\n****** Deallocate vars in scope!\n");
2256 for (i
= 0; i
< oper
->locals
->num_variables
; i
++) {
2257 slang_variable
*v
= oper
->locals
->variables
+ i
;
2259 slang_ir_storage
*store
= (slang_ir_storage
*) v
->aux
;
2261 printf(" Deallocate var %s\n", (char*) v->a_name);
2263 assert(store
->File
== PROGRAM_TEMPORARY
);
2264 assert(store
->Index
>= 0);
2265 _slang_free_temp(A
->vartable
, store
->Index
, store
->Size
);
2273 return new_node0(IR_NOP
);
2276 case SLANG_OPER_EXPRESSION
:
2277 return _slang_gen_operation(A
, &oper
->children
[0]);
2279 case SLANG_OPER_FOR
:
2280 return _slang_gen_for(A
, oper
);
2282 return _slang_gen_do(A
, oper
);
2283 case SLANG_OPER_WHILE
:
2284 return _slang_gen_while(A
, oper
);
2285 case SLANG_OPER_BREAK
:
2287 slang_info_log_error(A
->log
, "'break' not in loop");
2289 return new_break(A
->CurLoop
);
2290 case SLANG_OPER_CONTINUE
:
2292 slang_info_log_error(A
->log
, "'continue' not in loop");
2294 return new_cont(A
->CurLoop
);
2295 case SLANG_OPER_DISCARD
:
2296 return new_node0(IR_KILL
);
2298 case SLANG_OPER_EQUAL
:
2299 return new_node2(IR_SEQUAL
,
2300 _slang_gen_operation(A
, &oper
->children
[0]),
2301 _slang_gen_operation(A
, &oper
->children
[1]));
2302 case SLANG_OPER_NOTEQUAL
:
2303 return new_node2(IR_SNEQUAL
,
2304 _slang_gen_operation(A
, &oper
->children
[0]),
2305 _slang_gen_operation(A
, &oper
->children
[1]));
2306 case SLANG_OPER_GREATER
:
2307 return new_node2(IR_SGT
,
2308 _slang_gen_operation(A
, &oper
->children
[0]),
2309 _slang_gen_operation(A
, &oper
->children
[1]));
2310 case SLANG_OPER_LESS
:
2311 return new_node2(IR_SLT
,
2312 _slang_gen_operation(A
, &oper
->children
[0]),
2313 _slang_gen_operation(A
, &oper
->children
[1]));
2314 case SLANG_OPER_GREATEREQUAL
:
2315 return new_node2(IR_SGE
,
2316 _slang_gen_operation(A
, &oper
->children
[0]),
2317 _slang_gen_operation(A
, &oper
->children
[1]));
2318 case SLANG_OPER_LESSEQUAL
:
2319 return new_node2(IR_SLE
,
2320 _slang_gen_operation(A
, &oper
->children
[0]),
2321 _slang_gen_operation(A
, &oper
->children
[1]));
2322 case SLANG_OPER_ADD
:
2325 assert(oper
->num_children
== 2);
2326 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
2329 case SLANG_OPER_SUBTRACT
:
2332 assert(oper
->num_children
== 2);
2333 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2336 case SLANG_OPER_MULTIPLY
:
2339 assert(oper
->num_children
== 2);
2340 _slang_check_matmul_optimization(A
, oper
);
2341 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
2344 case SLANG_OPER_DIVIDE
:
2347 assert(oper
->num_children
== 2);
2348 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
2351 case SLANG_OPER_MINUS
:
2354 assert(oper
->num_children
== 1);
2355 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2358 case SLANG_OPER_PLUS
:
2359 /* +expr --> do nothing */
2360 return _slang_gen_operation(A
, &oper
->children
[0]);
2361 case SLANG_OPER_VARIABLE_DECL
:
2362 return _slang_gen_declaration(A
, oper
);
2363 case SLANG_OPER_ASSIGN
:
2364 return _slang_gen_assignment(A
, oper
);
2365 case SLANG_OPER_ADDASSIGN
:
2368 assert(oper
->num_children
== 2);
2369 n
= _slang_gen_function_call_name(A
, "+=", oper
, &oper
->children
[0]);
2372 case SLANG_OPER_SUBASSIGN
:
2375 assert(oper
->num_children
== 2);
2376 n
= _slang_gen_function_call_name(A
, "-=", oper
, &oper
->children
[0]);
2380 case SLANG_OPER_MULASSIGN
:
2383 assert(oper
->num_children
== 2);
2384 n
= _slang_gen_function_call_name(A
, "*=", oper
, &oper
->children
[0]);
2387 case SLANG_OPER_DIVASSIGN
:
2390 assert(oper
->num_children
== 2);
2391 n
= _slang_gen_function_call_name(A
, "/=", oper
, &oper
->children
[0]);
2394 case SLANG_OPER_LOGICALAND
:
2397 assert(oper
->num_children
== 2);
2398 n
= _slang_gen_logical_and(A
, oper
);
2401 case SLANG_OPER_LOGICALOR
:
2404 assert(oper
->num_children
== 2);
2405 n
= _slang_gen_logical_or(A
, oper
);
2408 case SLANG_OPER_LOGICALXOR
:
2411 assert(oper
->num_children
== 2);
2412 n
= _slang_gen_function_call_name(A
, "__logicalXor", oper
, NULL
);
2415 case SLANG_OPER_NOT
:
2418 assert(oper
->num_children
== 1);
2419 n
= _slang_gen_function_call_name(A
, "__logicalNot", oper
, NULL
);
2423 case SLANG_OPER_SELECT
: /* b ? x : y */
2426 assert(oper
->num_children
== 3);
2427 n
= _slang_gen_select(A
, oper
);
2431 case SLANG_OPER_ASM
:
2432 return _slang_gen_asm(A
, oper
, NULL
);
2433 case SLANG_OPER_CALL
:
2434 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
2436 case SLANG_OPER_RETURN
:
2437 return _slang_gen_return(A
, oper
);
2438 case SLANG_OPER_GOTO
:
2439 return new_jump(oper
->label
);
2440 case SLANG_OPER_LABEL
:
2441 return new_label(oper
->label
);
2442 case SLANG_OPER_IDENTIFIER
:
2443 return _slang_gen_variable(A
, oper
);
2445 return _slang_gen_if(A
, oper
);
2446 case SLANG_OPER_FIELD
:
2447 return _slang_gen_field(A
, oper
);
2448 case SLANG_OPER_SUBSCRIPT
:
2449 return _slang_gen_subscript(A
, oper
);
2450 case SLANG_OPER_LITERAL_FLOAT
:
2452 case SLANG_OPER_LITERAL_INT
:
2454 case SLANG_OPER_LITERAL_BOOL
:
2455 return new_float_literal(oper
->literal
);
2457 case SLANG_OPER_POSTINCREMENT
: /* var++ */
2460 assert(oper
->num_children
== 1);
2461 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
2464 case SLANG_OPER_POSTDECREMENT
: /* var-- */
2467 assert(oper
->num_children
== 1);
2468 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
2471 case SLANG_OPER_PREINCREMENT
: /* ++var */
2474 assert(oper
->num_children
== 1);
2475 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
2478 case SLANG_OPER_PREDECREMENT
: /* --var */
2481 assert(oper
->num_children
== 1);
2482 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
2486 case SLANG_OPER_SEQUENCE
:
2488 slang_ir_node
*tree
= NULL
;
2490 for (i
= 0; i
< oper
->num_children
; i
++) {
2491 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2492 tree
= tree
? new_seq(tree
, n
) : n
;
2497 case SLANG_OPER_NONE
:
2498 case SLANG_OPER_VOID
:
2499 /* returning NULL here would generate an error */
2500 return new_node0(IR_NOP
);
2503 printf("Unhandled node type %d\n", oper
->type
);
2505 return new_node0(IR_NOP
);
2514 * Called by compiler when a global variable has been parsed/compiled.
2515 * Here we examine the variable's type to determine what kind of register
2516 * storage will be used.
2518 * A uniform such as "gl_Position" will become the register specification
2519 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
2520 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
2522 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
2523 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
2524 * actual texture unit (as specified by the user calling glUniform1i()).
2527 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
2528 slang_unit_type type
)
2530 struct gl_program
*prog
= A
->program
;
2531 const char *varName
= (char *) var
->a_name
;
2532 GLboolean success
= GL_TRUE
;
2534 slang_ir_storage
*store
= NULL
;
2537 texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
2539 if (texIndex
!= -1) {
2541 * store->File = PROGRAM_SAMPLER
2542 * store->Index = sampler uniform location
2543 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
2545 GLint samplerUniform
= _mesa_add_sampler(prog
->Parameters
, varName
);
2546 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, samplerUniform
, texIndex
);
2547 if (dbg
) printf("SAMPLER ");
2549 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2550 /* Uniform variable */
2551 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
)
2552 * MAX2(var
->array_len
, 1);
2554 /* user-defined uniform */
2555 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
, size
);
2556 store
= _slang_new_ir_storage(PROGRAM_UNIFORM
, uniformLoc
, size
);
2559 /* pre-defined uniform, like gl_ModelviewMatrix */
2560 /* We know it's a uniform, but don't allocate storage unless
2563 store
= _slang_new_ir_storage(PROGRAM_STATE_VAR
, -1, size
);
2565 if (dbg
) printf("UNIFORM (sz %d) ", size
);
2567 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
2568 const GLint size
= 4; /* XXX fix */
2570 /* user-defined varying */
2571 GLint varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
, size
);
2572 store
= _slang_new_ir_storage(PROGRAM_VARYING
, varyingLoc
, size
);
2575 /* pre-defined varying, like gl_Color or gl_TexCoord */
2576 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
2578 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
2581 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2582 store
->Swizzle
= swizzle
;
2583 assert(index
< FRAG_ATTRIB_MAX
);
2586 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2588 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
2589 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2590 assert(index
< VERT_RESULT_MAX
);
2592 if (dbg
) printf("V/F ");
2594 if (dbg
) printf("VARYING ");
2596 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
2598 /* user-defined vertex attribute */
2599 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2600 const GLint attr
= -1; /* unknown */
2601 GLint index
= _mesa_add_attribute(prog
->Attributes
, varName
,
2604 store
= _slang_new_ir_storage(PROGRAM_INPUT
,
2605 VERT_ATTRIB_GENERIC0
+ index
, size
);
2608 /* pre-defined vertex attrib */
2610 GLint index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
,
2612 GLint size
= 4; /* XXX? */
2614 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2615 store
->Swizzle
= swizzle
;
2617 if (dbg
) printf("ATTRIB ");
2619 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
2621 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
2623 GLint size
= 4; /* XXX? */
2624 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2625 store
->Swizzle
= swizzle
;
2626 if (dbg
) printf("INPUT ");
2628 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
2629 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
2630 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2631 GLint size
= 4; /* XXX? */
2632 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2635 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
2636 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2637 GLint size
= 4; /* XXX? */
2638 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2640 if (dbg
) printf("OUTPUT ");
2642 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
2643 /* pre-defined global constant, like gl_MaxLights */
2644 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2645 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
2646 if (dbg
) printf("CONST ");
2649 /* ordinary variable (may be const) */
2652 /* IR node to declare the variable */
2653 n
= _slang_gen_var_decl(A
, var
);
2655 /* IR code for the var's initializer, if present */
2656 if (var
->initializer
) {
2657 slang_ir_node
*lhs
, *rhs
, *init
;
2659 /* Generate IR_MOVE instruction to initialize the variable */
2660 lhs
= new_node0(IR_VAR
);
2662 lhs
->Store
= n
->Store
;
2664 /* constant folding, etc */
2665 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
2667 rhs
= _slang_gen_operation(A
, var
->initializer
);
2669 init
= new_node2(IR_MOVE
, lhs
, rhs
);
2670 n
= new_seq(n
, init
);
2673 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
2675 _slang_free_ir_tree(n
);
2678 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
2679 store
? store
->Index
: -2);
2682 var
->aux
= store
; /* save var's storage info */
2689 * Produce an IR tree from a function AST (fun->body).
2690 * Then call the code emitter to convert the IR tree into gl_program
2694 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
2697 GLboolean success
= GL_TRUE
;
2699 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
2700 /* we only really generate code for main, all other functions get
2703 return GL_TRUE
; /* not an error */
2707 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
2710 slang_print_function(fun
, 1);
2713 /* should have been allocated earlier: */
2714 assert(A
->program
->Parameters
);
2715 assert(A
->program
->Varying
);
2716 assert(A
->vartable
);
2718 /* fold constant expressions, etc. */
2719 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
2721 /* Create an end-of-function label */
2722 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
2724 /* push new vartable scope */
2725 _slang_push_var_table(A
->vartable
);
2727 /* Generate IR tree for the function body code */
2728 n
= _slang_gen_operation(A
, fun
->body
);
2730 n
= new_node1(IR_SCOPE
, n
);
2732 /* pop vartable, restore previous */
2733 _slang_pop_var_table(A
->vartable
);
2736 /* XXX record error */
2740 /* append an end-of-function-label to IR tree */
2741 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
2743 /*_slang_label_delete(A->curFuncEndLabel);*/
2744 A
->curFuncEndLabel
= NULL
;
2747 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
2748 slang_print_function(fun
, 1);
2751 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
2752 slang_print_ir(n
, 0);
2755 printf("************* End codegen function ************\n\n");
2758 /* Emit program instructions */
2759 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
2760 _slang_free_ir_tree(n
);
2762 /* free codegen context */
2764 _mesa_free(A->codegen);