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
:
72 case SLANG_SPEC_SAMPLER2DRECT
:
73 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
82 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
89 case SLANG_SPEC_BVEC2
:
91 case SLANG_SPEC_BVEC3
:
93 case SLANG_SPEC_BVEC4
:
97 case SLANG_SPEC_IVEC2
:
99 case SLANG_SPEC_IVEC3
:
101 case SLANG_SPEC_IVEC4
:
103 case SLANG_SPEC_FLOAT
:
105 case SLANG_SPEC_VEC2
:
107 case SLANG_SPEC_VEC3
:
109 case SLANG_SPEC_VEC4
:
111 case SLANG_SPEC_MAT2
:
113 case SLANG_SPEC_MAT3
:
115 case SLANG_SPEC_MAT4
:
117 case SLANG_SPEC_SAMPLER1D
:
118 case SLANG_SPEC_SAMPLER2D
:
119 case SLANG_SPEC_SAMPLER3D
:
120 case SLANG_SPEC_SAMPLERCUBE
:
121 case SLANG_SPEC_SAMPLER1DSHADOW
:
122 case SLANG_SPEC_SAMPLER2DSHADOW
:
123 case SLANG_SPEC_SAMPLER2DRECT
:
124 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
125 return 1; /* special case */
126 case SLANG_SPEC_STRUCT
:
129 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
130 slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
131 GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
132 /* XXX verify padding */
139 case SLANG_SPEC_ARRAY
:
140 return _slang_sizeof_type_specifier(spec
->_array
);
142 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
150 * Establish the binding between a slang_ir_node and a slang_variable.
151 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
152 * The IR node must be a IR_VAR or IR_VAR_DECL node.
153 * \param n the IR node
154 * \param var the variable to associate with the IR node
157 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
161 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
162 assert(!n
->Var
|| n
->Var
== var
);
167 /* need to setup storage */
168 if (n
->Var
&& n
->Var
->aux
) {
169 /* node storage info = var storage info */
170 n
->Store
= (slang_ir_storage
*) n
->Var
->aux
;
173 /* alloc new storage info */
174 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -5);
176 n
->Var
->aux
= n
->Store
;
184 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
185 * or -1 if the type is not a sampler.
188 sampler_to_texture_index(const slang_type_specifier_type type
)
191 case SLANG_SPEC_SAMPLER1D
:
192 return TEXTURE_1D_INDEX
;
193 case SLANG_SPEC_SAMPLER2D
:
194 return TEXTURE_2D_INDEX
;
195 case SLANG_SPEC_SAMPLER3D
:
196 return TEXTURE_3D_INDEX
;
197 case SLANG_SPEC_SAMPLERCUBE
:
198 return TEXTURE_CUBE_INDEX
;
199 case SLANG_SPEC_SAMPLER1DSHADOW
:
200 return TEXTURE_1D_INDEX
; /* XXX fix */
201 case SLANG_SPEC_SAMPLER2DSHADOW
:
202 return TEXTURE_2D_INDEX
; /* XXX fix */
203 case SLANG_SPEC_SAMPLER2DRECT
:
204 return TEXTURE_RECT_INDEX
;
205 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
206 return TEXTURE_RECT_INDEX
; /* XXX fix */
214 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
215 * a vertex or fragment program input variable. Return -1 if the input
217 * XXX return size too
220 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
227 static const struct input_info vertInputs
[] = {
228 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
229 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
230 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
231 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
232 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
233 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
234 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
235 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
236 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
237 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
238 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
239 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
240 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
241 { NULL
, 0, SWIZZLE_NOOP
}
243 static const struct input_info fragInputs
[] = {
244 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
245 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
246 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
247 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
248 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
249 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
250 { NULL
, 0, SWIZZLE_NOOP
}
253 const struct input_info
*inputs
254 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
256 ASSERT(MAX_TEXTURE_UNITS
== 8); /* if this fails, fix vertInputs above */
258 for (i
= 0; inputs
[i
].Name
; i
++) {
259 if (strcmp(inputs
[i
].Name
, name
) == 0) {
261 *swizzleOut
= inputs
[i
].Swizzle
;
262 return inputs
[i
].Attrib
;
270 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
271 * a vertex or fragment program output variable. Return -1 for an invalid
275 _slang_output_index(const char *name
, GLenum target
)
281 static const struct output_info vertOutputs
[] = {
282 { "gl_Position", VERT_RESULT_HPOS
},
283 { "gl_FrontColor", VERT_RESULT_COL0
},
284 { "gl_BackColor", VERT_RESULT_BFC0
},
285 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
286 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
287 { "gl_TexCoord", VERT_RESULT_TEX0
},
288 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
289 { "gl_PointSize", VERT_RESULT_PSIZ
},
292 static const struct output_info fragOutputs
[] = {
293 { "gl_FragColor", FRAG_RESULT_COLR
},
294 { "gl_FragDepth", FRAG_RESULT_DEPR
},
295 { "gl_FragData", FRAG_RESULT_DATA0
},
299 const struct output_info
*outputs
300 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
302 for (i
= 0; outputs
[i
].Name
; i
++) {
303 if (strcmp(outputs
[i
].Name
, name
) == 0) {
305 return outputs
[i
].Attrib
;
313 /**********************************************************************/
317 * Map "_asm foo" to IR_FOO, etc.
322 slang_ir_opcode Opcode
;
323 GLuint HaveRetValue
, NumParams
;
327 static slang_asm_info AsmInfo
[] = {
329 { "vec4_add", IR_ADD
, 1, 2 },
330 { "vec4_subtract", IR_SUB
, 1, 2 },
331 { "vec4_multiply", IR_MUL
, 1, 2 },
332 { "vec4_dot", IR_DOT4
, 1, 2 },
333 { "vec3_dot", IR_DOT3
, 1, 2 },
334 { "vec3_cross", IR_CROSS
, 1, 2 },
335 { "vec4_lrp", IR_LRP
, 1, 3 },
336 { "vec4_min", IR_MIN
, 1, 2 },
337 { "vec4_max", IR_MAX
, 1, 2 },
338 { "vec4_clamp", IR_CLAMP
, 1, 3 },
339 { "vec4_seq", IR_SEQUAL
, 1, 2 },
340 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
341 { "vec4_sge", IR_SGE
, 1, 2 },
342 { "vec4_sgt", IR_SGT
, 1, 2 },
343 { "vec4_sle", IR_SLE
, 1, 2 },
344 { "vec4_slt", IR_SLT
, 1, 2 },
346 { "vec4_floor", IR_FLOOR
, 1, 1 },
347 { "vec4_frac", IR_FRAC
, 1, 1 },
348 { "vec4_abs", IR_ABS
, 1, 1 },
349 { "vec4_negate", IR_NEG
, 1, 1 },
350 { "vec4_ddx", IR_DDX
, 1, 1 },
351 { "vec4_ddy", IR_DDY
, 1, 1 },
352 /* float binary op */
353 { "float_power", IR_POW
, 1, 2 },
354 /* texture / sampler */
355 { "vec4_tex1d", IR_TEX
, 1, 2 },
356 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
357 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
358 { "vec4_tex2d", IR_TEX
, 1, 2 },
359 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
360 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
361 { "vec4_tex3d", IR_TEX
, 1, 2 },
362 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
363 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
364 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
365 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
366 { "vec4_texp_rect", IR_TEX
, 1, 2 },/* rectangle w/ projection */
369 { "int_to_float", IR_I_TO_F
, 1, 1 },
370 { "float_to_int", IR_F_TO_I
, 1, 1 },
371 { "float_exp", IR_EXP
, 1, 1 },
372 { "float_exp2", IR_EXP2
, 1, 1 },
373 { "float_log2", IR_LOG2
, 1, 1 },
374 { "float_rsq", IR_RSQ
, 1, 1 },
375 { "float_rcp", IR_RCP
, 1, 1 },
376 { "float_sine", IR_SIN
, 1, 1 },
377 { "float_cosine", IR_COS
, 1, 1 },
378 { "float_noise1", IR_NOISE1
, 1, 1},
379 { "float_noise2", IR_NOISE2
, 1, 1},
380 { "float_noise3", IR_NOISE3
, 1, 1},
381 { "float_noise4", IR_NOISE4
, 1, 1},
383 { NULL
, IR_NOP
, 0, 0 }
388 * Recursively free an IR tree.
391 _slang_free_ir_tree(slang_ir_node
*n
)
397 for (i
= 0; i
< 3; i
++)
398 _slang_free_ir_tree(n
->Children
[i
]);
399 /* Do not free n->BranchNode since it's a child elsewhere */
405 static slang_ir_node
*
406 new_node3(slang_ir_opcode op
,
407 slang_ir_node
*c0
, slang_ir_node
*c1
, slang_ir_node
*c2
)
409 slang_ir_node
*n
= (slang_ir_node
*) calloc(1, sizeof(slang_ir_node
));
415 n
->Writemask
= WRITEMASK_XYZW
;
416 n
->InstLocation
= -1;
421 static slang_ir_node
*
422 new_node2(slang_ir_opcode op
, slang_ir_node
*c0
, slang_ir_node
*c1
)
424 return new_node3(op
, c0
, c1
, NULL
);
427 static slang_ir_node
*
428 new_node1(slang_ir_opcode op
, slang_ir_node
*c0
)
430 return new_node3(op
, c0
, NULL
, NULL
);
433 static slang_ir_node
*
434 new_node0(slang_ir_opcode op
)
436 return new_node3(op
, NULL
, NULL
, NULL
);
440 static slang_ir_node
*
441 new_seq(slang_ir_node
*left
, slang_ir_node
*right
)
447 return new_node2(IR_SEQ
, left
, right
);
450 static slang_ir_node
*
451 new_label(slang_label
*label
)
453 slang_ir_node
*n
= new_node0(IR_LABEL
);
460 static slang_ir_node
*
461 new_float_literal(const float v
[4])
463 const GLuint size
= (v
[0] == v
[1] && v
[0] == v
[2] && v
[0] == v
[3]) ? 1 : 4;
464 slang_ir_node
*n
= new_node0(IR_FLOAT
);
465 COPY_4V(n
->Value
, v
);
466 /* allocate a storage object, but compute actual location (Index) later */
467 n
->Store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
472 * Unconditional jump.
474 static slang_ir_node
*
475 new_jump(slang_label
*dest
)
477 slang_ir_node
*n
= new_node0(IR_JUMP
);
485 static slang_ir_node
*
486 new_loop(slang_ir_node
*body
)
488 return new_node1(IR_LOOP
, body
);
492 static slang_ir_node
*
493 new_break(slang_ir_node
*loopNode
)
495 slang_ir_node
*n
= new_node0(IR_BREAK
);
497 assert(loopNode
->Opcode
== IR_LOOP
);
499 /* insert this node at head of linked list */
500 n
->BranchNode
= loopNode
->BranchNode
;
501 loopNode
->BranchNode
= n
;
508 * Make new IR_BREAK_IF_TRUE or IR_BREAK_IF_FALSE node.
510 static slang_ir_node
*
511 new_break_if(slang_ir_node
*loopNode
, slang_ir_node
*cond
, GLboolean breakTrue
)
515 assert(loopNode
->Opcode
== IR_LOOP
);
516 n
= new_node1(breakTrue
? IR_BREAK_IF_TRUE
: IR_BREAK_IF_FALSE
, cond
);
518 /* insert this node at head of linked list */
519 n
->BranchNode
= loopNode
->BranchNode
;
520 loopNode
->BranchNode
= n
;
527 * Make new IR_CONT_IF_TRUE or IR_CONT_IF_FALSE node.
529 static slang_ir_node
*
530 new_cont_if(slang_ir_node
*loopNode
, slang_ir_node
*cond
, GLboolean contTrue
)
534 assert(loopNode
->Opcode
== IR_LOOP
);
535 n
= new_node1(contTrue
? IR_CONT_IF_TRUE
: IR_CONT_IF_FALSE
, cond
);
537 /* insert this node at head of linked list */
538 n
->BranchNode
= loopNode
->BranchNode
;
539 loopNode
->BranchNode
= n
;
545 static slang_ir_node
*
546 new_cont(slang_ir_node
*loopNode
)
548 slang_ir_node
*n
= new_node0(IR_CONT
);
550 assert(loopNode
->Opcode
== IR_LOOP
);
552 /* insert this node at head of linked list */
553 n
->BranchNode
= loopNode
->BranchNode
;
554 loopNode
->BranchNode
= n
;
560 static slang_ir_node
*
561 new_cond(slang_ir_node
*n
)
563 slang_ir_node
*c
= new_node1(IR_COND
, n
);
568 static slang_ir_node
*
569 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
571 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
576 * New IR_VAR node - a reference to a previously declared variable.
578 static slang_ir_node
*
579 new_var(slang_assemble_ctx
*A
, slang_operation
*oper
, slang_atom name
)
582 slang_variable
*var
= _slang_locate_variable(oper
->locals
, name
, GL_TRUE
);
586 assert(!oper
->var
|| oper
->var
== var
);
588 n
= new_node0(IR_VAR
);
590 _slang_attach_storage(n
, var
);
597 * Check if the given function is really just a wrapper for a
598 * basic assembly instruction.
601 slang_is_asm_function(const slang_function
*fun
)
603 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
604 fun
->body
->num_children
== 1 &&
605 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
613 _slang_is_noop(const slang_operation
*oper
)
616 oper
->type
== SLANG_OPER_VOID
||
617 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
625 * Produce inline code for a call to an assembly instruction.
626 * XXX Note: children are passed as asm args in-order, not by name!
628 static slang_operation
*
629 slang_inline_asm_function(slang_assemble_ctx
*A
,
630 slang_function
*fun
, slang_operation
*oper
)
632 const GLuint numArgs
= oper
->num_children
;
633 const slang_operation
*args
= oper
->children
;
635 slang_operation
*inlined
= slang_operation_new(1);
637 /*assert(oper->type == SLANG_OPER_CALL); or vec4_add, etc */
639 printf("Inline asm %s\n", (char*) fun->header.a_name);
641 inlined
->type
= fun
->body
->children
[0].type
;
642 inlined
->a_id
= fun
->body
->children
[0].a_id
;
643 inlined
->num_children
= numArgs
;
644 inlined
->children
= slang_operation_new(numArgs
);
645 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
647 for (i
= 0; i
< numArgs
; i
++) {
648 slang_operation_copy(inlined
->children
+ i
, args
+ i
);
656 slang_resolve_variable(slang_operation
*oper
)
658 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
659 oper
->var
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
665 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
668 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
669 GLuint substCount
, slang_variable
**substOld
,
670 slang_operation
**substNew
, GLboolean isLHS
)
672 switch (oper
->type
) {
673 case SLANG_OPER_VARIABLE_DECL
:
675 slang_variable
*v
= _slang_locate_variable(oper
->locals
,
676 oper
->a_id
, GL_TRUE
);
678 if (v
->initializer
&& oper
->num_children
== 0) {
679 /* set child of oper to copy of initializer */
680 oper
->num_children
= 1;
681 oper
->children
= slang_operation_new(1);
682 slang_operation_copy(&oper
->children
[0], v
->initializer
);
684 if (oper
->num_children
== 1) {
685 /* the initializer */
686 slang_substitute(A
, &oper
->children
[0], substCount
,
687 substOld
, substNew
, GL_FALSE
);
691 case SLANG_OPER_IDENTIFIER
:
692 assert(oper
->num_children
== 0);
693 if (1/**!isLHS XXX FIX */) {
694 slang_atom id
= oper
->a_id
;
697 v
= _slang_locate_variable(oper
->locals
, id
, GL_TRUE
);
699 printf("var %s not found!\n", (char *) oper
->a_id
);
700 _slang_print_var_scope(oper
->locals
, 6);
706 /* look for a substitution */
707 for (i
= 0; i
< substCount
; i
++) {
708 if (v
== substOld
[i
]) {
709 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
710 #if 0 /* DEBUG only */
711 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
712 assert(substNew
[i
]->var
);
713 assert(substNew
[i
]->var
->a_name
);
714 printf("Substitute %s with %s in id node %p\n",
715 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
719 printf("Substitute %s with %f in id node %p\n",
720 (char*)v
->a_name
, substNew
[i
]->literal
[0],
724 slang_operation_copy(oper
, substNew
[i
]);
731 case SLANG_OPER_RETURN
:
732 /* do return replacement here too */
733 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
734 if (!_slang_is_noop(oper
)) {
740 * then do substitutions on the assignment.
742 slang_operation
*blockOper
, *assignOper
, *returnOper
;
743 blockOper
= slang_operation_new(1);
744 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
745 blockOper
->num_children
= 2;
746 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
747 blockOper
->children
= slang_operation_new(2);
748 assignOper
= blockOper
->children
+ 0;
749 returnOper
= blockOper
->children
+ 1;
751 assignOper
->type
= SLANG_OPER_ASSIGN
;
752 assignOper
->num_children
= 2;
753 assignOper
->locals
->outer_scope
= blockOper
->locals
;
754 assignOper
->children
= slang_operation_new(2);
755 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
756 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
757 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
759 slang_operation_copy(&assignOper
->children
[1],
762 returnOper
->type
= SLANG_OPER_RETURN
;
763 assert(returnOper
->num_children
== 0);
765 /* do substitutions on the "__retVal = expr" sub-tree */
766 slang_substitute(A
, assignOper
,
767 substCount
, substOld
, substNew
, GL_FALSE
);
769 /* install new code */
770 slang_operation_copy(oper
, blockOper
);
771 slang_operation_destruct(blockOper
);
775 case SLANG_OPER_ASSIGN
:
776 case SLANG_OPER_SUBSCRIPT
:
778 * child[0] can't have substitutions but child[1] can.
780 slang_substitute(A
, &oper
->children
[0],
781 substCount
, substOld
, substNew
, GL_TRUE
);
782 slang_substitute(A
, &oper
->children
[1],
783 substCount
, substOld
, substNew
, GL_FALSE
);
785 case SLANG_OPER_FIELD
:
787 slang_substitute(A
, &oper
->children
[0],
788 substCount
, substOld
, substNew
, GL_TRUE
);
793 for (i
= 0; i
< oper
->num_children
; i
++)
794 slang_substitute(A
, &oper
->children
[i
],
795 substCount
, substOld
, substNew
, GL_FALSE
);
803 * Inline the given function call operation.
804 * Return a new slang_operation that corresponds to the inlined code.
806 static slang_operation
*
807 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
808 slang_operation
*oper
, slang_operation
*returnOper
)
815 ParamMode
*paramMode
;
816 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
817 const GLuint numArgs
= oper
->num_children
;
818 const GLuint totalArgs
= numArgs
+ haveRetValue
;
819 slang_operation
*args
= oper
->children
;
820 slang_operation
*inlined
, *top
;
821 slang_variable
**substOld
;
822 slang_operation
**substNew
;
823 GLuint substCount
, numCopyIn
, i
;
825 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
826 assert(fun
->param_count
== totalArgs
);
828 /* allocate temporary arrays */
829 paramMode
= (ParamMode
*)
830 _mesa_calloc(totalArgs
* sizeof(ParamMode
));
831 substOld
= (slang_variable
**)
832 _mesa_calloc(totalArgs
* sizeof(slang_variable
*));
833 substNew
= (slang_operation
**)
834 _mesa_calloc(totalArgs
* sizeof(slang_operation
*));
837 printf("Inline call to %s (total vars=%d nparams=%d)\n",
838 (char *) fun
->header
.a_name
,
839 fun
->parameters
->num_variables
, numArgs
);
842 if (haveRetValue
&& !returnOper
) {
843 /* Create 3-child comma sequence for inlined code:
844 * child[0]: declare __resultTmp
845 * child[1]: inlined function body
846 * child[2]: __resultTmp
848 slang_operation
*commaSeq
;
849 slang_operation
*declOper
= NULL
;
850 slang_variable
*resultVar
;
852 commaSeq
= slang_operation_new(1);
853 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
854 assert(commaSeq
->locals
);
855 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
856 commaSeq
->num_children
= 3;
857 commaSeq
->children
= slang_operation_new(3);
858 /* allocate the return var */
859 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
861 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
862 (void*)commaSeq->locals, (char *) fun->header.a_name);
865 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
866 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
867 resultVar
->isTemp
= GL_TRUE
;
869 /* child[0] = __resultTmp declaration */
870 declOper
= &commaSeq
->children
[0];
871 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
872 declOper
->a_id
= resultVar
->a_name
;
873 declOper
->locals
->outer_scope
= commaSeq
->locals
;
875 /* child[1] = function body */
876 inlined
= &commaSeq
->children
[1];
877 inlined
->locals
->outer_scope
= commaSeq
->locals
;
879 /* child[2] = __resultTmp reference */
880 returnOper
= &commaSeq
->children
[2];
881 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
882 returnOper
->a_id
= resultVar
->a_name
;
883 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
888 top
= inlined
= slang_operation_new(1);
889 /* XXXX this may be inappropriate!!!! */
890 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
894 assert(inlined
->locals
);
896 /* Examine the parameters, look for inout/out params, look for possible
897 * substitutions, etc:
898 * param type behaviour
899 * in copy actual to local
900 * const in substitute param with actual
904 for (i
= 0; i
< totalArgs
; i
++) {
905 slang_variable
*p
= fun
->parameters
->variables
[i
];
907 printf("Param %d: %s %s \n", i,
908 slang_type_qual_string(p->type.qualifier),
911 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
912 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
913 /* an output param */
914 slang_operation
*arg
;
919 paramMode
[i
] = SUBST
;
921 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
922 slang_resolve_variable(arg
);
924 /* replace parameter 'p' with argument 'arg' */
925 substOld
[substCount
] = p
;
926 substNew
[substCount
] = arg
; /* will get copied */
929 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
930 /* a constant input param */
931 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
932 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
933 /* replace all occurances of this parameter variable with the
934 * actual argument variable or a literal.
936 paramMode
[i
] = SUBST
;
937 slang_resolve_variable(&args
[i
]);
938 substOld
[substCount
] = p
;
939 substNew
[substCount
] = &args
[i
]; /* will get copied */
943 paramMode
[i
] = COPY_IN
;
947 paramMode
[i
] = COPY_IN
;
949 assert(paramMode
[i
]);
952 /* actual code inlining: */
953 slang_operation_copy(inlined
, fun
->body
);
955 /*** XXX review this */
956 assert(inlined
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
);
957 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
960 printf("======================= orig body code ======================\n");
961 printf("=== params scope = %p\n", (void*) fun
->parameters
);
962 slang_print_tree(fun
->body
, 8);
963 printf("======================= copied code =========================\n");
964 slang_print_tree(inlined
, 8);
967 /* do parameter substitution in inlined code: */
968 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
971 printf("======================= subst code ==========================\n");
972 slang_print_tree(inlined
, 8);
973 printf("=============================================================\n");
976 /* New prolog statements: (inserted before the inlined code)
977 * Copy the 'in' arguments.
980 for (i
= 0; i
< numArgs
; i
++) {
981 if (paramMode
[i
] == COPY_IN
) {
982 slang_variable
*p
= fun
->parameters
->variables
[i
];
983 /* declare parameter 'p' */
984 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
988 printf("COPY_IN %s from expr\n", (char*)p->a_name);
990 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
991 assert(decl
->locals
);
992 decl
->locals
->outer_scope
= inlined
->locals
;
993 decl
->a_id
= p
->a_name
;
994 decl
->num_children
= 1;
995 decl
->children
= slang_operation_new(1);
997 /* child[0] is the var's initializer */
998 slang_operation_copy(&decl
->children
[0], args
+ i
);
1004 /* New epilog statements:
1005 * 1. Create end of function label to jump to from return statements.
1006 * 2. Copy the 'out' parameter vars
1009 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
1011 inlined
->num_children
);
1012 lab
->type
= SLANG_OPER_LABEL
;
1013 lab
->label
= A
->curFuncEndLabel
;
1016 for (i
= 0; i
< totalArgs
; i
++) {
1017 if (paramMode
[i
] == COPY_OUT
) {
1018 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1019 /* actualCallVar = outParam */
1020 /*if (i > 0 || !haveRetValue)*/
1021 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1023 inlined
->num_children
);
1024 ass
->type
= SLANG_OPER_ASSIGN
;
1025 ass
->num_children
= 2;
1026 ass
->locals
->outer_scope
= inlined
->locals
;
1027 ass
->children
= slang_operation_new(2);
1028 ass
->children
[0] = args
[i
]; /*XXX copy */
1029 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1030 ass
->children
[1].a_id
= p
->a_name
;
1031 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1035 _mesa_free(paramMode
);
1036 _mesa_free(substOld
);
1037 _mesa_free(substNew
);
1040 printf("Done Inline call to %s (total vars=%d nparams=%d)\n",
1041 (char *) fun
->header
.a_name
,
1042 fun
->parameters
->num_variables
, numArgs
);
1043 slang_print_tree(top
, 0);
1049 static slang_ir_node
*
1050 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1051 slang_operation
*oper
, slang_operation
*dest
)
1054 slang_operation
*inlined
;
1055 slang_label
*prevFuncEndLabel
;
1058 prevFuncEndLabel
= A
->curFuncEndLabel
;
1059 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1060 A
->curFuncEndLabel
= _slang_label_new(name
);
1061 assert(A
->curFuncEndLabel
);
1063 if (slang_is_asm_function(fun
) && !dest
) {
1064 /* assemble assembly function - tree style */
1065 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1068 /* non-assembly function */
1069 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1072 /* Replace the function call with the inlined block */
1074 slang_operation_construct(oper
);
1075 slang_operation_copy(oper
, inlined
);
1077 *oper
= *inlined
; /* XXX slang_operation_copy() */
1082 assert(inlined
->locals
);
1083 printf("*** Inlined code for call to %s:\n",
1084 (char*) fun
->header
.a_name
);
1085 slang_print_tree(oper
, 10);
1089 n
= _slang_gen_operation(A
, oper
);
1091 /*_slang_label_delete(A->curFuncEndLabel);*/
1092 A
->curFuncEndLabel
= prevFuncEndLabel
;
1093 assert(A
->curFuncEndLabel
);
1099 static slang_asm_info
*
1100 slang_find_asm_info(const char *name
)
1103 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1104 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1113 make_writemask(const char *field
)
1119 mask
|= WRITEMASK_X
;
1122 mask
|= WRITEMASK_Y
;
1125 mask
|= WRITEMASK_Z
;
1128 mask
|= WRITEMASK_W
;
1136 return WRITEMASK_XYZW
;
1143 * Generate IR tree for an asm instruction/operation such as:
1144 * __asm vec4_dot __retVal.x, v1, v2;
1146 static slang_ir_node
*
1147 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1148 slang_operation
*dest
)
1150 const slang_asm_info
*info
;
1151 slang_ir_node
*kids
[3], *n
;
1152 GLuint j
, firstOperand
;
1154 assert(oper
->type
== SLANG_OPER_ASM
);
1156 info
= slang_find_asm_info((char *) oper
->a_id
);
1158 _mesa_problem(NULL
, "undefined __asm function %s\n",
1159 (char *) oper
->a_id
);
1162 assert(info
->NumParams
<= 3);
1164 if (info
->NumParams
== oper
->num_children
) {
1165 /* Storage for result is not specified.
1166 * Children[0], [1] are the operands.
1171 /* Storage for result (child[0]) is specified.
1172 * Children[1], [2] are the operands.
1177 /* assemble child(ren) */
1178 kids
[0] = kids
[1] = kids
[2] = NULL
;
1179 for (j
= 0; j
< info
->NumParams
; j
++) {
1180 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1185 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1188 /* Setup n->Store to be a particular location. Otherwise, storage
1189 * for the result (a temporary) will be allocated later.
1191 GLuint writemask
= WRITEMASK_XYZW
;
1192 slang_operation
*dest_oper
;
1195 dest_oper
= &oper
->children
[0];
1196 while (dest_oper
->type
== SLANG_OPER_FIELD
) {
1198 writemask
&= make_writemask((char*) dest_oper
->a_id
);
1199 dest_oper
= &dest_oper
->children
[0];
1202 n0
= _slang_gen_operation(A
, dest_oper
);
1206 n
->Store
= n0
->Store
;
1207 n
->Writemask
= writemask
;
1217 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1220 for (i
= 0; i
< scope
->num_functions
; i
++) {
1221 slang_function
*f
= &scope
->functions
[i
];
1222 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1223 printf(" %s (%d args)\n", name
, f
->param_count
);
1226 if (scope
->outer_scope
)
1227 print_funcs(scope
->outer_scope
, name
);
1232 * Return first function in the scope that has the given name.
1233 * This is the function we'll try to call when there is no exact match
1234 * between function parameters and call arguments.
1236 * XXX we should really create a list of candidate functions and try
1239 static slang_function
*
1240 _slang_first_function(struct slang_function_scope_
*scope
, const char *name
)
1243 for (i
= 0; i
< scope
->num_functions
; i
++) {
1244 slang_function
*f
= &scope
->functions
[i
];
1245 if (strcmp(name
, (char*) f
->header
.a_name
) == 0)
1248 if (scope
->outer_scope
)
1249 return _slang_first_function(scope
->outer_scope
, name
);
1256 * Assemble a function call, given a particular function name.
1257 * \param name the function's name (operators like '*' are possible).
1259 static slang_ir_node
*
1260 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
1261 slang_operation
*oper
, slang_operation
*dest
)
1263 slang_operation
*params
= oper
->children
;
1264 const GLuint param_count
= oper
->num_children
;
1266 slang_function
*fun
;
1268 atom
= slang_atom_pool_atom(A
->atoms
, name
);
1269 if (atom
== SLANG_ATOM_NULL
)
1273 * Use 'name' to find the function to call
1275 fun
= _slang_locate_function(A
->space
.funcs
, atom
, params
, param_count
,
1276 &A
->space
, A
->atoms
, A
->log
);
1278 /* A function with exactly the right parameters/types was not found.
1279 * Try adapting the parameters.
1281 fun
= _slang_first_function(A
->space
.funcs
, name
);
1282 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
1283 slang_info_log_error(A
->log
, "Function '%s' not found (check argument types)", name
);
1289 return _slang_gen_function_call(A
, fun
, oper
, dest
);
1294 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
1296 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
1297 oper
->type
== SLANG_OPER_LITERAL_INT
||
1298 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
1299 if (oper
->literal
[0])
1305 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
1306 oper
->num_children
== 1) {
1307 return _slang_is_constant_cond(&oper
->children
[0], value
);
1315 * Generate loop code using high-level IR_LOOP instruction
1317 static slang_ir_node
*
1318 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1322 * BREAK if !expr (child[0])
1323 * body code (child[1])
1325 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
;
1326 GLboolean isConst
, constTrue
;
1328 /* Check if loop condition is a constant */
1329 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1331 if (isConst
&& !constTrue
) {
1332 /* loop is never executed! */
1333 return new_node0(IR_NOP
);
1336 loop
= new_loop(NULL
);
1338 /* save old, push new loop */
1339 prevLoop
= A
->CurLoop
;
1342 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[0]));
1343 if (isConst
&& constTrue
) {
1344 /* while(nonzero constant), no conditional break */
1348 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1350 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1351 loop
->Children
[0] = new_seq(breakIf
, body
);
1353 /* Do infinite loop detection */
1354 if (loop
->BranchNode
== 0 && isConst
&& constTrue
) {
1355 /* infinite loop detected */
1356 A
->CurLoop
= prevLoop
; /* clean-up */
1357 slang_info_log_error(A
->log
, "Infinite loop detected!");
1361 /* pop loop, restore prev */
1362 A
->CurLoop
= prevLoop
;
1369 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
1371 static slang_ir_node
*
1372 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1376 * body code (child[0])
1377 * BREAK if !expr (child[1])
1379 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
;
1380 GLboolean isConst
, constTrue
;
1382 /* Check if loop condition is a constant */
1383 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1385 loop
= new_loop(NULL
);
1387 /* save old, push new loop */
1388 prevLoop
= A
->CurLoop
;
1391 body
= _slang_gen_operation(A
, &oper
->children
[0]);
1392 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[1]));
1393 if (isConst
&& constTrue
) {
1394 /* while(nonzero constant), no conditional break */
1398 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1400 loop
->Children
[0] = new_seq(body
, breakIf
);
1402 /* pop loop, restore prev */
1403 A
->CurLoop
= prevLoop
;
1410 * Generate for-loop using high-level IR_LOOP instruction.
1412 static slang_ir_node
*
1413 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1418 * BREAK if !expr (child[1])
1419 * body code (child[3])
1420 * incr code (child[2]) // XXX continue here
1422 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
1424 init
= _slang_gen_operation(A
, &oper
->children
[0]);
1425 loop
= new_loop(NULL
);
1427 /* save old, push new loop */
1428 prevLoop
= A
->CurLoop
;
1431 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[1]));
1432 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1433 body
= _slang_gen_operation(A
, &oper
->children
[3]);
1434 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
1435 loop
->Children
[0] = new_seq(breakIf
,
1436 new_seq(body
, incr
));
1438 /* pop loop, restore prev */
1439 A
->CurLoop
= prevLoop
;
1441 return new_seq(init
, loop
);
1446 * Determine if the given operation is of a specific type.
1449 is_operation_type(const const slang_operation
*oper
, slang_operation_type type
)
1451 if (oper
->type
== type
)
1453 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1454 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
1455 oper
->num_children
== 1)
1456 return is_operation_type(&oper
->children
[0], type
);
1463 * Generate IR tree for an if/then/else conditional using high-level
1464 * IR_IF instruction.
1466 static slang_ir_node
*
1467 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1470 * eval expr (child[0]), updating condcodes
1477 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
1478 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
1480 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1481 cond
= new_cond(cond
);
1483 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)) {
1484 /* Special case: generate a conditional break */
1485 ifBody
= new_break_if(A
->CurLoop
, cond
, GL_TRUE
);
1486 if (haveElseClause
) {
1487 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1488 return new_seq(ifBody
, elseBody
);
1492 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)) {
1493 /* Special case: generate a conditional break */
1494 ifBody
= new_cont_if(A
->CurLoop
, cond
, GL_TRUE
);
1495 if (haveElseClause
) {
1496 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1497 return new_seq(ifBody
, elseBody
);
1503 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
1505 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1508 ifNode
= new_if(cond
, ifBody
, elseBody
);
1516 * Generate IR node for storage of a temporary of given size.
1518 static slang_ir_node
*
1519 _slang_gen_temporary(GLint size
)
1521 slang_ir_storage
*store
;
1524 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
1526 n
= new_node0(IR_VAR_DECL
);
1539 * Generate IR node for allocating/declaring a variable.
1541 static slang_ir_node
*
1542 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
)
1545 assert(!is_sampler_type(&var
->type
));
1546 n
= new_node0(IR_VAR_DECL
);
1548 _slang_attach_storage(n
, var
);
1551 assert(n
->Store
== var
->aux
);
1553 assert(n
->Store
->Index
< 0);
1555 n
->Store
->File
= PROGRAM_TEMPORARY
;
1556 n
->Store
->Size
= _slang_sizeof_type_specifier(&n
->Var
->type
.specifier
);
1557 assert(n
->Store
->Size
> 0);
1564 * Generate code for a selection expression: b ? x : y
1565 * XXX In some cases we could implement a selection expression
1566 * with an LRP instruction (use the boolean as the interpolant).
1567 * Otherwise, we use an IF/ELSE/ENDIF construct.
1569 static slang_ir_node
*
1570 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
1572 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
1573 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
1574 slang_typeinfo type
;
1577 assert(oper
->type
== SLANG_OPER_SELECT
);
1578 assert(oper
->num_children
== 3);
1580 /* size of x or y's type */
1581 slang_typeinfo_construct(&type
);
1582 _slang_typeof_operation(A
, &oper
->children
[1], &type
);
1583 size
= _slang_sizeof_type_specifier(&type
.spec
);
1587 tmpDecl
= _slang_gen_temporary(size
);
1589 /* the condition (child 0) */
1590 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1591 cond
= new_cond(cond
);
1593 /* if-true body (child 1) */
1594 tmpVar
= new_node0(IR_VAR
);
1595 tmpVar
->Store
= tmpDecl
->Store
;
1596 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
1597 trueNode
= new_node2(IR_MOVE
, tmpVar
, trueExpr
);
1599 /* if-false body (child 2) */
1600 tmpVar
= new_node0(IR_VAR
);
1601 tmpVar
->Store
= tmpDecl
->Store
;
1602 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
1603 falseNode
= new_node2(IR_MOVE
, tmpVar
, falseExpr
);
1605 ifNode
= new_if(cond
, trueNode
, falseNode
);
1608 tmpVar
= new_node0(IR_VAR
);
1609 tmpVar
->Store
= tmpDecl
->Store
;
1611 tree
= new_seq(ifNode
, tmpVar
);
1612 tree
= new_seq(tmpDecl
, tree
);
1614 slang_print_ir(tree
, 10);
1620 * Generate code for &&.
1622 static slang_ir_node
*
1623 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
1625 /* rewrite "a && b" as "a ? b : false" */
1626 slang_operation
*select
;
1629 select
= slang_operation_new(1);
1630 select
->type
= SLANG_OPER_SELECT
;
1631 select
->num_children
= 3;
1632 select
->children
= slang_operation_new(3);
1634 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1635 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
1636 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
1637 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
1638 select
->children
[2].literal_size
= 1;
1640 n
= _slang_gen_select(A
, select
);
1643 free(select
->children
);
1651 * Generate code for ||.
1653 static slang_ir_node
*
1654 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
1656 /* rewrite "a || b" as "a ? true : b" */
1657 slang_operation
*select
;
1660 select
= slang_operation_new(1);
1661 select
->type
= SLANG_OPER_SELECT
;
1662 select
->num_children
= 3;
1663 select
->children
= slang_operation_new(3);
1665 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1666 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
1667 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
1668 select
->children
[1].literal_size
= 1;
1669 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
1671 n
= _slang_gen_select(A
, select
);
1674 free(select
->children
);
1682 * Generate IR tree for a return statement.
1684 static slang_ir_node
*
1685 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
1687 if (oper
->num_children
== 0 ||
1688 (oper
->num_children
== 1 &&
1689 oper
->children
[0].type
== SLANG_OPER_VOID
)) {
1693 * goto __endOfFunction;
1696 slang_operation gotoOp
;
1697 slang_operation_construct(&gotoOp
);
1698 gotoOp
.type
= SLANG_OPER_GOTO
;
1699 gotoOp
.label
= A
->curFuncEndLabel
;
1700 assert(gotoOp
.label
);
1702 /* assemble the new code */
1703 n
= _slang_gen_operation(A
, &gotoOp
);
1704 /* destroy temp code */
1705 slang_operation_destruct(&gotoOp
);
1714 * goto __endOfFunction;
1716 slang_operation
*block
, *assign
, *jump
;
1717 slang_atom a_retVal
;
1720 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1726 = _slang_locate_variable(oper
->locals
, a_retVal
, GL_TRUE
);
1731 block
= slang_operation_new(1);
1732 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1733 assert(block
->locals
);
1734 block
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1735 block
->num_children
= 2;
1736 block
->children
= slang_operation_new(2);
1738 /* child[0]: __retVal = expr; */
1739 assign
= &block
->children
[0];
1740 assign
->type
= SLANG_OPER_ASSIGN
;
1741 assign
->locals
->outer_scope
= block
->locals
;
1742 assign
->num_children
= 2;
1743 assign
->children
= slang_operation_new(2);
1744 /* lhs (__retVal) */
1745 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1746 assign
->children
[0].a_id
= a_retVal
;
1747 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
1749 /* XXX we might be able to avoid this copy someday */
1750 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
1752 /* child[1]: goto __endOfFunction */
1753 jump
= &block
->children
[1];
1754 jump
->type
= SLANG_OPER_GOTO
;
1755 assert(A
->curFuncEndLabel
);
1756 /* XXX don't call function? */
1757 jump
->label
= A
->curFuncEndLabel
;
1758 assert(jump
->label
);
1761 printf("NEW RETURN:\n");
1762 slang_print_tree(block
, 0);
1765 /* assemble the new code */
1766 n
= _slang_gen_operation(A
, block
);
1767 slang_operation_delete(block
);
1774 * Generate IR tree for a variable declaration.
1776 static slang_ir_node
*
1777 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
1780 slang_ir_node
*varDecl
;
1782 const char *varName
= (char *) oper
->a_id
;
1784 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
1786 v
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
1789 varDecl
= _slang_gen_var_decl(A
, v
);
1791 if (oper
->num_children
> 0) {
1792 /* child is initializer */
1793 slang_ir_node
*var
, *init
, *rhs
;
1794 assert(oper
->num_children
== 1);
1795 var
= new_var(A
, oper
, oper
->a_id
);
1797 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
1800 /* XXX make copy of this initializer? */
1801 rhs
= _slang_gen_operation(A
, &oper
->children
[0]);
1803 init
= new_node2(IR_MOVE
, var
, rhs
);
1804 /*assert(rhs->Opcode != IR_SEQ);*/
1805 n
= new_seq(varDecl
, init
);
1807 else if (v
->initializer
) {
1808 slang_ir_node
*var
, *init
, *rhs
;
1809 var
= new_var(A
, oper
, oper
->a_id
);
1811 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
1815 /* XXX make copy of this initializer? */
1817 slang_operation dup
;
1818 slang_operation_construct(&dup
);
1819 slang_operation_copy(&dup
, v
->initializer
);
1820 _slang_simplify(&dup
, &A
->space
, A
->atoms
);
1821 rhs
= _slang_gen_operation(A
, &dup
);
1824 _slang_simplify(v
->initializer
, &A
->space
, A
->atoms
);
1825 rhs
= _slang_gen_operation(A
, v
->initializer
);
1828 init
= new_node2(IR_MOVE
, var
, rhs
);
1830 assert(rhs->Opcode != IR_SEQ);
1832 n
= new_seq(varDecl
, init
);
1842 * Generate IR tree for a variable (such as in an expression).
1844 static slang_ir_node
*
1845 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
1847 /* If there's a variable associated with this oper (from inlining)
1848 * use it. Otherwise, use the oper's var id.
1850 slang_atom aVar
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
1851 slang_ir_node
*n
= new_var(A
, oper
, aVar
);
1853 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) aVar
);
1861 * Some write-masked assignments are simple, but others are hard.
1864 * v.xy = vec2(a, b);
1867 * v.zy = vec2(a, b);
1868 * this gets transformed/swizzled into:
1869 * v.zy = vec2(a, b).*yx* (* = don't care)
1870 * This function helps to determine simple vs. non-simple.
1873 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1875 switch (writemask
) {
1877 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1879 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1881 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1883 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1885 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1886 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1888 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1889 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1890 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1891 case WRITEMASK_XYZW
:
1892 return swizzle
== SWIZZLE_NOOP
;
1900 * Convert the given swizzle into a writemask. In some cases this
1901 * is trivial, in other cases, we'll need to also swizzle the right
1902 * hand side to put components in the right places.
1903 * \param swizzle the incoming swizzle
1904 * \param writemaskOut returns the writemask
1905 * \param swizzleOut swizzle to apply to the right-hand-side
1906 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1909 swizzle_to_writemask(GLuint swizzle
,
1910 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1912 GLuint mask
= 0x0, newSwizzle
[4];
1915 /* make new dst writemask, compute size */
1916 for (i
= 0; i
< 4; i
++) {
1917 const GLuint swz
= GET_SWZ(swizzle
, i
);
1918 if (swz
== SWIZZLE_NIL
) {
1922 assert(swz
>= 0 && swz
<= 3);
1925 assert(mask
<= 0xf);
1926 size
= i
; /* number of components in mask/swizzle */
1928 *writemaskOut
= mask
;
1930 /* make new src swizzle, by inversion */
1931 for (i
= 0; i
< 4; i
++) {
1932 newSwizzle
[i
] = i
; /*identity*/
1934 for (i
= 0; i
< size
; i
++) {
1935 const GLuint swz
= GET_SWZ(swizzle
, i
);
1936 newSwizzle
[swz
] = i
;
1938 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1943 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1945 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1947 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1949 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1951 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1959 static slang_ir_node
*
1960 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
1962 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
1965 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -1);
1966 n
->Store
->Swizzle
= swizzle
;
1973 * Generate IR tree for an assignment (=).
1975 static slang_ir_node
*
1976 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
1978 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
1979 oper
->children
[1].type
== SLANG_OPER_CALL
) {
1980 /* Special case of: x = f(a, b)
1981 * Replace with f(a, b, x) (where x == hidden __retVal out param)
1983 * XXX this could be even more effective if we could accomodate
1984 * cases such as "v.x = f();" - would help with typical vertex
1988 n
= _slang_gen_function_call_name(A
,
1989 (const char *) oper
->children
[1].a_id
,
1990 &oper
->children
[1], &oper
->children
[0]);
1994 slang_ir_node
*n
, *lhs
, *rhs
;
1995 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
1998 if (lhs
->Store
->File
!= PROGRAM_OUTPUT
&&
1999 lhs
->Store
->File
!= PROGRAM_TEMPORARY
&&
2000 lhs
->Store
->File
!= PROGRAM_VARYING
&&
2001 lhs
->Store
->File
!= PROGRAM_UNDEFINED
) {
2002 slang_info_log_error(A
->log
, "Assignment to read-only variable");
2007 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
2009 /* convert lhs swizzle into writemask */
2010 GLuint writemask
, newSwizzle
;
2011 if (!swizzle_to_writemask(lhs
->Store
->Swizzle
,
2012 &writemask
, &newSwizzle
)) {
2013 /* Non-simple writemask, need to swizzle right hand side in
2014 * order to put components into the right place.
2016 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
2018 n
= new_node2(IR_MOVE
, lhs
, rhs
);
2019 n
->Writemask
= writemask
;
2030 * Generate IR tree for referencing a field in a struct (or basic vector type)
2032 static slang_ir_node
*
2033 _slang_gen_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
2037 slang_typeinfo_construct(&ti
);
2038 _slang_typeof_operation(A
, &oper
->children
[0], &ti
);
2040 if (_slang_type_is_vector(ti
.spec
.type
)) {
2041 /* the field should be a swizzle */
2042 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
2046 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2047 slang_info_log_error(A
->log
, "Bad swizzle");
2049 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2054 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2055 /* create new parent node with swizzle */
2057 n
= _slang_gen_swizzle(n
, swizzle
);
2060 else if (ti
.spec
.type
== SLANG_SPEC_FLOAT
) {
2061 const GLuint rows
= 1;
2065 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2066 slang_info_log_error(A
->log
, "Bad swizzle");
2068 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2072 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2073 /* create new parent node with swizzle */
2074 n
= _slang_gen_swizzle(n
, swizzle
);
2078 /* the field is a structure member (base.field) */
2079 /* oper->children[0] is the base */
2080 /* oper->a_id is the field name */
2081 slang_ir_node
*base
, *n
;
2082 GLint size
= 4; /* XXX fix? */
2084 base
= _slang_gen_operation(A
, &oper
->children
[0]);
2086 /* error previously found */
2090 n
= new_node1(IR_FIELD
, base
);
2092 n
->Field
= (char *) oper
->a_id
;
2093 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
2100 _mesa_problem(NULL
, "glsl structs/fields not supported yet");
2108 * Gen code for array indexing.
2110 static slang_ir_node
*
2111 _slang_gen_subscript(slang_assemble_ctx
* A
, slang_operation
*oper
)
2113 slang_typeinfo array_ti
;
2115 /* get array's type info */
2116 slang_typeinfo_construct(&array_ti
);
2117 _slang_typeof_operation(A
, &oper
->children
[0], &array_ti
);
2119 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
2120 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
2121 /* translate the index into a swizzle/writemask: "v.x=p" */
2122 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
2126 index
= (GLint
) oper
->children
[1].literal
[0];
2127 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
2129 slang_info_log_error(A
->log
, "Invalid array index for vector type");
2133 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2135 /* use swizzle to access the element */
2136 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
2140 n
= _slang_gen_swizzle(n
, swizzle
);
2141 /*n->Store = _slang_clone_ir_storage_swz(n->Store, */
2142 n
->Writemask
= WRITEMASK_X
<< index
;
2147 /* conventional array */
2148 slang_typeinfo elem_ti
;
2149 slang_ir_node
*elem
, *array
, *index
;
2150 GLint elemSize
, arrayLen
;
2152 /* size of array element */
2153 slang_typeinfo_construct(&elem_ti
);
2154 _slang_typeof_operation(A
, oper
, &elem_ti
);
2155 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
2157 if (_slang_type_is_matrix(array_ti
.spec
.type
))
2158 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
2160 arrayLen
= array_ti
.array_len
;
2162 slang_typeinfo_destruct(&array_ti
);
2163 slang_typeinfo_destruct(&elem_ti
);
2165 if (elemSize
<= 0) {
2166 /* unknown var or type */
2167 slang_info_log_error(A
->log
, "Undefined variable or type");
2171 array
= _slang_gen_operation(A
, &oper
->children
[0]);
2172 index
= _slang_gen_operation(A
, &oper
->children
[1]);
2173 if (array
&& index
) {
2175 if (index
->Opcode
== IR_FLOAT
&&
2176 ((int) index
->Value
[0] < 0 ||
2177 (int) index
->Value
[0] >= arrayLen
)) {
2178 slang_info_log_error(A
->log
,
2179 "Array index out of bounds (index=%d size=%d)",
2180 (int) index
->Value
[0], arrayLen
);
2181 _slang_free_ir_tree(array
);
2182 _slang_free_ir_tree(index
);
2186 elem
= new_node2(IR_ELEMENT
, array
, index
);
2187 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
2188 array
->Store
->Index
,
2190 /* XXX try to do some array bounds checking here */
2194 _slang_free_ir_tree(array
);
2195 _slang_free_ir_tree(index
);
2203 * Look for expressions such as: gl_ModelviewMatrix * gl_Vertex
2204 * and replace with this: gl_Vertex * gl_ModelviewMatrixTranpose
2205 * Since matrices are stored in column-major order, the second form of
2206 * multiplication is much more efficient (just 4 dot products).
2209 _slang_check_matmul_optimization(slang_assemble_ctx
*A
, slang_operation
*oper
)
2211 static const struct {
2213 const char *tranpose
;
2215 {"gl_ModelViewMatrix", "gl_ModelViewMatrixTranspose"},
2216 {"gl_ProjectionMatrix", "gl_ProjectionMatrixTranspose"},
2217 {"gl_ModelViewProjectionMatrix", "gl_ModelViewProjectionMatrixTranspose"},
2218 {"gl_TextureMatrix", "gl_TextureMatrixTranspose"},
2219 {"gl_NormalMatrix", "__NormalMatrixTranspose"},
2223 assert(oper
->type
== SLANG_OPER_MULTIPLY
);
2224 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
2226 for (i
= 0; matrices
[i
].orig
; i
++) {
2227 if (oper
->children
[0].a_id
2228 == slang_atom_pool_atom(A
->atoms
, matrices
[i
].orig
)) {
2230 _mesa_printf("Replace %s with %s\n",
2231 matrices[i].orig, matrices[i].tranpose);
2233 assert(oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
);
2234 oper
->children
[0].a_id
2235 = slang_atom_pool_atom(A
->atoms
, matrices
[i
].tranpose
);
2236 /* finally, swap the operands */
2237 _slang_operation_swap(&oper
->children
[0], &oper
->children
[1]);
2246 * Generate IR tree for a slang_operation (AST node)
2248 static slang_ir_node
*
2249 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
2251 switch (oper
->type
) {
2252 case SLANG_OPER_BLOCK_NEW_SCOPE
:
2256 _slang_push_var_table(A
->vartable
);
2258 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
2259 n
= _slang_gen_operation(A
, oper
);
2260 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
2262 _slang_pop_var_table(A
->vartable
);
2265 n
= new_node1(IR_SCOPE
, n
);
2270 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
2271 /* list of operations */
2272 if (oper
->num_children
> 0)
2274 slang_ir_node
*n
, *tree
= NULL
;
2277 for (i
= 0; i
< oper
->num_children
; i
++) {
2278 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2280 _slang_free_ir_tree(tree
);
2281 return NULL
; /* error must have occured */
2283 tree
= tree
? new_seq(tree
, n
) : n
;
2287 if (oper
->locals
->num_variables
> 0) {
2290 printf("\n****** Deallocate vars in scope!\n");
2292 for (i
= 0; i
< oper
->locals
->num_variables
; i
++) {
2293 slang_variable
*v
= oper
->locals
->variables
+ i
;
2295 slang_ir_storage
*store
= (slang_ir_storage
*) v
->aux
;
2297 printf(" Deallocate var %s\n", (char*) v->a_name);
2299 assert(store
->File
== PROGRAM_TEMPORARY
);
2300 assert(store
->Index
>= 0);
2301 _slang_free_temp(A
->vartable
, store
->Index
, store
->Size
);
2309 return new_node0(IR_NOP
);
2312 case SLANG_OPER_EXPRESSION
:
2313 return _slang_gen_operation(A
, &oper
->children
[0]);
2315 case SLANG_OPER_FOR
:
2316 return _slang_gen_for(A
, oper
);
2318 return _slang_gen_do(A
, oper
);
2319 case SLANG_OPER_WHILE
:
2320 return _slang_gen_while(A
, oper
);
2321 case SLANG_OPER_BREAK
:
2323 slang_info_log_error(A
->log
, "'break' not in loop");
2325 return new_break(A
->CurLoop
);
2326 case SLANG_OPER_CONTINUE
:
2328 slang_info_log_error(A
->log
, "'continue' not in loop");
2330 return new_cont(A
->CurLoop
);
2331 case SLANG_OPER_DISCARD
:
2332 return new_node0(IR_KILL
);
2334 case SLANG_OPER_EQUAL
:
2335 return new_node2(IR_SEQUAL
,
2336 _slang_gen_operation(A
, &oper
->children
[0]),
2337 _slang_gen_operation(A
, &oper
->children
[1]));
2338 case SLANG_OPER_NOTEQUAL
:
2339 return new_node2(IR_SNEQUAL
,
2340 _slang_gen_operation(A
, &oper
->children
[0]),
2341 _slang_gen_operation(A
, &oper
->children
[1]));
2342 case SLANG_OPER_GREATER
:
2343 return new_node2(IR_SGT
,
2344 _slang_gen_operation(A
, &oper
->children
[0]),
2345 _slang_gen_operation(A
, &oper
->children
[1]));
2346 case SLANG_OPER_LESS
:
2347 return new_node2(IR_SLT
,
2348 _slang_gen_operation(A
, &oper
->children
[0]),
2349 _slang_gen_operation(A
, &oper
->children
[1]));
2350 case SLANG_OPER_GREATEREQUAL
:
2351 return new_node2(IR_SGE
,
2352 _slang_gen_operation(A
, &oper
->children
[0]),
2353 _slang_gen_operation(A
, &oper
->children
[1]));
2354 case SLANG_OPER_LESSEQUAL
:
2355 return new_node2(IR_SLE
,
2356 _slang_gen_operation(A
, &oper
->children
[0]),
2357 _slang_gen_operation(A
, &oper
->children
[1]));
2358 case SLANG_OPER_ADD
:
2361 assert(oper
->num_children
== 2);
2362 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
2365 case SLANG_OPER_SUBTRACT
:
2368 assert(oper
->num_children
== 2);
2369 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2372 case SLANG_OPER_MULTIPLY
:
2375 assert(oper
->num_children
== 2);
2376 _slang_check_matmul_optimization(A
, oper
);
2377 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
2380 case SLANG_OPER_DIVIDE
:
2383 assert(oper
->num_children
== 2);
2384 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
2387 case SLANG_OPER_MINUS
:
2390 assert(oper
->num_children
== 1);
2391 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2394 case SLANG_OPER_PLUS
:
2395 /* +expr --> do nothing */
2396 return _slang_gen_operation(A
, &oper
->children
[0]);
2397 case SLANG_OPER_VARIABLE_DECL
:
2398 return _slang_gen_declaration(A
, oper
);
2399 case SLANG_OPER_ASSIGN
:
2400 return _slang_gen_assignment(A
, oper
);
2401 case SLANG_OPER_ADDASSIGN
:
2404 assert(oper
->num_children
== 2);
2405 n
= _slang_gen_function_call_name(A
, "+=", oper
, &oper
->children
[0]);
2408 case SLANG_OPER_SUBASSIGN
:
2411 assert(oper
->num_children
== 2);
2412 n
= _slang_gen_function_call_name(A
, "-=", oper
, &oper
->children
[0]);
2416 case SLANG_OPER_MULASSIGN
:
2419 assert(oper
->num_children
== 2);
2420 n
= _slang_gen_function_call_name(A
, "*=", oper
, &oper
->children
[0]);
2423 case SLANG_OPER_DIVASSIGN
:
2426 assert(oper
->num_children
== 2);
2427 n
= _slang_gen_function_call_name(A
, "/=", oper
, &oper
->children
[0]);
2430 case SLANG_OPER_LOGICALAND
:
2433 assert(oper
->num_children
== 2);
2434 n
= _slang_gen_logical_and(A
, oper
);
2437 case SLANG_OPER_LOGICALOR
:
2440 assert(oper
->num_children
== 2);
2441 n
= _slang_gen_logical_or(A
, oper
);
2444 case SLANG_OPER_LOGICALXOR
:
2447 assert(oper
->num_children
== 2);
2448 n
= _slang_gen_function_call_name(A
, "__logicalXor", oper
, NULL
);
2451 case SLANG_OPER_NOT
:
2454 assert(oper
->num_children
== 1);
2455 n
= _slang_gen_function_call_name(A
, "__logicalNot", oper
, NULL
);
2459 case SLANG_OPER_SELECT
: /* b ? x : y */
2462 assert(oper
->num_children
== 3);
2463 n
= _slang_gen_select(A
, oper
);
2467 case SLANG_OPER_ASM
:
2468 return _slang_gen_asm(A
, oper
, NULL
);
2469 case SLANG_OPER_CALL
:
2470 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
2472 case SLANG_OPER_RETURN
:
2473 return _slang_gen_return(A
, oper
);
2474 case SLANG_OPER_GOTO
:
2475 return new_jump(oper
->label
);
2476 case SLANG_OPER_LABEL
:
2477 return new_label(oper
->label
);
2478 case SLANG_OPER_IDENTIFIER
:
2479 return _slang_gen_variable(A
, oper
);
2481 return _slang_gen_if(A
, oper
);
2482 case SLANG_OPER_FIELD
:
2483 return _slang_gen_field(A
, oper
);
2484 case SLANG_OPER_SUBSCRIPT
:
2485 return _slang_gen_subscript(A
, oper
);
2486 case SLANG_OPER_LITERAL_FLOAT
:
2488 case SLANG_OPER_LITERAL_INT
:
2490 case SLANG_OPER_LITERAL_BOOL
:
2491 return new_float_literal(oper
->literal
);
2493 case SLANG_OPER_POSTINCREMENT
: /* var++ */
2496 assert(oper
->num_children
== 1);
2497 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
2500 case SLANG_OPER_POSTDECREMENT
: /* var-- */
2503 assert(oper
->num_children
== 1);
2504 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
2507 case SLANG_OPER_PREINCREMENT
: /* ++var */
2510 assert(oper
->num_children
== 1);
2511 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
2514 case SLANG_OPER_PREDECREMENT
: /* --var */
2517 assert(oper
->num_children
== 1);
2518 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
2522 case SLANG_OPER_SEQUENCE
:
2524 slang_ir_node
*tree
= NULL
;
2526 for (i
= 0; i
< oper
->num_children
; i
++) {
2527 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2528 tree
= tree
? new_seq(tree
, n
) : n
;
2533 case SLANG_OPER_NONE
:
2534 case SLANG_OPER_VOID
:
2535 /* returning NULL here would generate an error */
2536 return new_node0(IR_NOP
);
2539 printf("Unhandled node type %d\n", oper
->type
);
2541 return new_node0(IR_NOP
);
2550 * Called by compiler when a global variable has been parsed/compiled.
2551 * Here we examine the variable's type to determine what kind of register
2552 * storage will be used.
2554 * A uniform such as "gl_Position" will become the register specification
2555 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
2556 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
2558 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
2559 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
2560 * actual texture unit (as specified by the user calling glUniform1i()).
2563 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
2564 slang_unit_type type
)
2566 struct gl_program
*prog
= A
->program
;
2567 const char *varName
= (char *) var
->a_name
;
2568 GLboolean success
= GL_TRUE
;
2570 slang_ir_storage
*store
= NULL
;
2573 texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
2575 if (texIndex
!= -1) {
2577 * store->File = PROGRAM_SAMPLER
2578 * store->Index = sampler uniform location
2579 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
2581 GLint samplerUniform
= _mesa_add_sampler(prog
->Parameters
, varName
);
2582 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, samplerUniform
, texIndex
);
2583 if (dbg
) printf("SAMPLER ");
2585 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2586 /* Uniform variable */
2587 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
)
2588 * MAX2(var
->array_len
, 1);
2590 /* user-defined uniform */
2591 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
, size
);
2592 store
= _slang_new_ir_storage(PROGRAM_UNIFORM
, uniformLoc
, size
);
2595 /* pre-defined uniform, like gl_ModelviewMatrix */
2596 /* We know it's a uniform, but don't allocate storage unless
2599 store
= _slang_new_ir_storage(PROGRAM_STATE_VAR
, -1, size
);
2601 if (dbg
) printf("UNIFORM (sz %d) ", size
);
2603 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
2604 const GLint size
= 4; /* XXX fix */
2606 /* user-defined varying */
2607 GLint varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
, size
);
2608 store
= _slang_new_ir_storage(PROGRAM_VARYING
, varyingLoc
, size
);
2611 /* pre-defined varying, like gl_Color or gl_TexCoord */
2612 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
2614 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
2617 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2618 store
->Swizzle
= swizzle
;
2619 assert(index
< FRAG_ATTRIB_MAX
);
2622 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2624 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
2625 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2626 assert(index
< VERT_RESULT_MAX
);
2628 if (dbg
) printf("V/F ");
2630 if (dbg
) printf("VARYING ");
2632 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
2634 /* user-defined vertex attribute */
2635 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2636 const GLint attr
= -1; /* unknown */
2637 GLint index
= _mesa_add_attribute(prog
->Attributes
, varName
,
2640 store
= _slang_new_ir_storage(PROGRAM_INPUT
,
2641 VERT_ATTRIB_GENERIC0
+ index
, size
);
2644 /* pre-defined vertex attrib */
2646 GLint index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
,
2648 GLint size
= 4; /* XXX? */
2650 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2651 store
->Swizzle
= swizzle
;
2653 if (dbg
) printf("ATTRIB ");
2655 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
2657 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
2659 GLint size
= 4; /* XXX? */
2660 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2661 store
->Swizzle
= swizzle
;
2662 if (dbg
) printf("INPUT ");
2664 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
2665 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
2666 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2667 GLint size
= 4; /* XXX? */
2668 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2671 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
2672 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2673 GLint size
= 4; /* XXX? */
2674 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2676 if (dbg
) printf("OUTPUT ");
2678 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
2679 /* pre-defined global constant, like gl_MaxLights */
2680 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2681 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
2682 if (dbg
) printf("CONST ");
2685 /* ordinary variable (may be const) */
2688 /* IR node to declare the variable */
2689 n
= _slang_gen_var_decl(A
, var
);
2691 /* IR code for the var's initializer, if present */
2692 if (var
->initializer
) {
2693 slang_ir_node
*lhs
, *rhs
, *init
;
2695 /* Generate IR_MOVE instruction to initialize the variable */
2696 lhs
= new_node0(IR_VAR
);
2698 lhs
->Store
= n
->Store
;
2700 /* constant folding, etc */
2701 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
2703 rhs
= _slang_gen_operation(A
, var
->initializer
);
2705 init
= new_node2(IR_MOVE
, lhs
, rhs
);
2706 n
= new_seq(n
, init
);
2709 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
2711 _slang_free_ir_tree(n
);
2714 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
2715 store
? store
->Index
: -2);
2718 var
->aux
= store
; /* save var's storage info */
2725 * Produce an IR tree from a function AST (fun->body).
2726 * Then call the code emitter to convert the IR tree into gl_program
2730 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
2733 GLboolean success
= GL_TRUE
;
2735 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
2736 /* we only really generate code for main, all other functions get
2739 return GL_TRUE
; /* not an error */
2743 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
2744 slang_print_function(fun
, 1);
2747 /* should have been allocated earlier: */
2748 assert(A
->program
->Parameters
);
2749 assert(A
->program
->Varying
);
2750 assert(A
->vartable
);
2752 /* fold constant expressions, etc. */
2753 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
2756 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
2757 slang_print_function(fun
, 1);
2760 /* Create an end-of-function label */
2761 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
2763 /* push new vartable scope */
2764 _slang_push_var_table(A
->vartable
);
2766 /* Generate IR tree for the function body code */
2767 n
= _slang_gen_operation(A
, fun
->body
);
2769 n
= new_node1(IR_SCOPE
, n
);
2771 /* pop vartable, restore previous */
2772 _slang_pop_var_table(A
->vartable
);
2775 /* XXX record error */
2779 /* append an end-of-function-label to IR tree */
2780 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
2782 /*_slang_label_delete(A->curFuncEndLabel);*/
2783 A
->curFuncEndLabel
= NULL
;
2786 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
2787 slang_print_function(fun
, 1);
2790 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
2791 slang_print_ir(n
, 0);
2794 printf("************* End codegen function ************\n\n");
2797 /* Emit program instructions */
2798 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
2799 _slang_free_ir_tree(n
);
2801 /* free codegen context */
2803 _mesa_free(A->codegen);