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 * Return the offset (in floats or ints) of the named field within
83 * the given struct. Return -1 if field not found.
84 * If field is NULL, return the size of the struct instead.
87 _slang_field_offset(const slang_type_specifier
*spec
, slang_atom field
)
91 for (i
= 0; i
< spec
->_struct
->fields
->num_variables
; i
++) {
92 const slang_variable
*v
= spec
->_struct
->fields
->variables
[i
];
93 const GLuint sz
= _slang_sizeof_type_specifier(&v
->type
.specifier
);
95 /* types larger than 1 float are register (4-float) aligned */
96 offset
= (offset
+ 3) & ~3;
98 if (field
&& v
->a_name
== field
) {
104 return -1; /* field not found */
106 return offset
; /* struct size */
111 _slang_sizeof_type_specifier(const slang_type_specifier
*spec
)
113 switch (spec
->type
) {
114 case SLANG_SPEC_VOID
:
116 case SLANG_SPEC_BOOL
:
118 case SLANG_SPEC_BVEC2
:
120 case SLANG_SPEC_BVEC3
:
122 case SLANG_SPEC_BVEC4
:
126 case SLANG_SPEC_IVEC2
:
128 case SLANG_SPEC_IVEC3
:
130 case SLANG_SPEC_IVEC4
:
132 case SLANG_SPEC_FLOAT
:
134 case SLANG_SPEC_VEC2
:
136 case SLANG_SPEC_VEC3
:
138 case SLANG_SPEC_VEC4
:
140 case SLANG_SPEC_MAT2
:
142 case SLANG_SPEC_MAT3
:
144 case SLANG_SPEC_MAT4
:
146 case SLANG_SPEC_SAMPLER1D
:
147 case SLANG_SPEC_SAMPLER2D
:
148 case SLANG_SPEC_SAMPLER3D
:
149 case SLANG_SPEC_SAMPLERCUBE
:
150 case SLANG_SPEC_SAMPLER1DSHADOW
:
151 case SLANG_SPEC_SAMPLER2DSHADOW
:
152 case SLANG_SPEC_SAMPLER2DRECT
:
153 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
154 return 1; /* a sampler is basically just an integer index */
155 case SLANG_SPEC_STRUCT
:
156 return _slang_field_offset(spec
, 0); /* special use */
157 case SLANG_SPEC_ARRAY
:
158 return _slang_sizeof_type_specifier(spec
->_array
);
160 _mesa_problem(NULL
, "Unexpected type in _slang_sizeof_type_specifier()");
168 * Establish the binding between a slang_ir_node and a slang_variable.
169 * Then, allocate/attach a slang_ir_storage object to the IR node if needed.
170 * The IR node must be a IR_VAR or IR_VAR_DECL node.
171 * \param n the IR node
172 * \param var the variable to associate with the IR node
175 _slang_attach_storage(slang_ir_node
*n
, slang_variable
*var
)
179 assert(n
->Opcode
== IR_VAR
|| n
->Opcode
== IR_VAR_DECL
);
180 assert(!n
->Var
|| n
->Var
== var
);
185 /* need to setup storage */
186 if (n
->Var
&& n
->Var
->aux
) {
187 /* node storage info = var storage info */
188 n
->Store
= (slang_ir_storage
*) n
->Var
->aux
;
191 /* alloc new storage info */
192 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -5);
194 n
->Var
->aux
= n
->Store
;
202 * Return the TEXTURE_*_INDEX value that corresponds to a sampler type,
203 * or -1 if the type is not a sampler.
206 sampler_to_texture_index(const slang_type_specifier_type type
)
209 case SLANG_SPEC_SAMPLER1D
:
210 return TEXTURE_1D_INDEX
;
211 case SLANG_SPEC_SAMPLER2D
:
212 return TEXTURE_2D_INDEX
;
213 case SLANG_SPEC_SAMPLER3D
:
214 return TEXTURE_3D_INDEX
;
215 case SLANG_SPEC_SAMPLERCUBE
:
216 return TEXTURE_CUBE_INDEX
;
217 case SLANG_SPEC_SAMPLER1DSHADOW
:
218 return TEXTURE_1D_INDEX
; /* XXX fix */
219 case SLANG_SPEC_SAMPLER2DSHADOW
:
220 return TEXTURE_2D_INDEX
; /* XXX fix */
221 case SLANG_SPEC_SAMPLER2DRECT
:
222 return TEXTURE_RECT_INDEX
;
223 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
224 return TEXTURE_RECT_INDEX
; /* XXX fix */
232 * Return the VERT_ATTRIB_* or FRAG_ATTRIB_* value that corresponds to
233 * a vertex or fragment program input variable. Return -1 if the input
235 * XXX return size too
238 _slang_input_index(const char *name
, GLenum target
, GLuint
*swizzleOut
)
245 static const struct input_info vertInputs
[] = {
246 { "gl_Vertex", VERT_ATTRIB_POS
, SWIZZLE_NOOP
},
247 { "gl_Normal", VERT_ATTRIB_NORMAL
, SWIZZLE_NOOP
},
248 { "gl_Color", VERT_ATTRIB_COLOR0
, SWIZZLE_NOOP
},
249 { "gl_SecondaryColor", VERT_ATTRIB_COLOR1
, SWIZZLE_NOOP
},
250 { "gl_FogCoord", VERT_ATTRIB_FOG
, SWIZZLE_XXXX
},
251 { "gl_MultiTexCoord0", VERT_ATTRIB_TEX0
, SWIZZLE_NOOP
},
252 { "gl_MultiTexCoord1", VERT_ATTRIB_TEX1
, SWIZZLE_NOOP
},
253 { "gl_MultiTexCoord2", VERT_ATTRIB_TEX2
, SWIZZLE_NOOP
},
254 { "gl_MultiTexCoord3", VERT_ATTRIB_TEX3
, SWIZZLE_NOOP
},
255 { "gl_MultiTexCoord4", VERT_ATTRIB_TEX4
, SWIZZLE_NOOP
},
256 { "gl_MultiTexCoord5", VERT_ATTRIB_TEX5
, SWIZZLE_NOOP
},
257 { "gl_MultiTexCoord6", VERT_ATTRIB_TEX6
, SWIZZLE_NOOP
},
258 { "gl_MultiTexCoord7", VERT_ATTRIB_TEX7
, SWIZZLE_NOOP
},
259 { NULL
, 0, SWIZZLE_NOOP
}
261 static const struct input_info fragInputs
[] = {
262 { "gl_FragCoord", FRAG_ATTRIB_WPOS
, SWIZZLE_NOOP
},
263 { "gl_Color", FRAG_ATTRIB_COL0
, SWIZZLE_NOOP
},
264 { "gl_SecondaryColor", FRAG_ATTRIB_COL1
, SWIZZLE_NOOP
},
265 { "gl_FogFragCoord", FRAG_ATTRIB_FOGC
, SWIZZLE_XXXX
},
266 { "gl_TexCoord", FRAG_ATTRIB_TEX0
, SWIZZLE_NOOP
},
267 { "gl_FrontFacing", FRAG_ATTRIB_FOGC
, SWIZZLE_YYYY
}, /*XXX*/
268 { NULL
, 0, SWIZZLE_NOOP
}
271 const struct input_info
*inputs
272 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertInputs
: fragInputs
;
274 ASSERT(MAX_TEXTURE_UNITS
== 8); /* if this fails, fix vertInputs above */
276 for (i
= 0; inputs
[i
].Name
; i
++) {
277 if (strcmp(inputs
[i
].Name
, name
) == 0) {
279 *swizzleOut
= inputs
[i
].Swizzle
;
280 return inputs
[i
].Attrib
;
288 * Return the VERT_RESULT_* or FRAG_RESULT_* value that corresponds to
289 * a vertex or fragment program output variable. Return -1 for an invalid
293 _slang_output_index(const char *name
, GLenum target
)
299 static const struct output_info vertOutputs
[] = {
300 { "gl_Position", VERT_RESULT_HPOS
},
301 { "gl_FrontColor", VERT_RESULT_COL0
},
302 { "gl_BackColor", VERT_RESULT_BFC0
},
303 { "gl_FrontSecondaryColor", VERT_RESULT_COL1
},
304 { "gl_BackSecondaryColor", VERT_RESULT_BFC1
},
305 { "gl_TexCoord", VERT_RESULT_TEX0
},
306 { "gl_FogFragCoord", VERT_RESULT_FOGC
},
307 { "gl_PointSize", VERT_RESULT_PSIZ
},
310 static const struct output_info fragOutputs
[] = {
311 { "gl_FragColor", FRAG_RESULT_COLR
},
312 { "gl_FragDepth", FRAG_RESULT_DEPR
},
313 { "gl_FragData", FRAG_RESULT_DATA0
},
317 const struct output_info
*outputs
318 = (target
== GL_VERTEX_PROGRAM_ARB
) ? vertOutputs
: fragOutputs
;
320 for (i
= 0; outputs
[i
].Name
; i
++) {
321 if (strcmp(outputs
[i
].Name
, name
) == 0) {
323 return outputs
[i
].Attrib
;
331 /**********************************************************************/
335 * Map "_asm foo" to IR_FOO, etc.
340 slang_ir_opcode Opcode
;
341 GLuint HaveRetValue
, NumParams
;
345 static slang_asm_info AsmInfo
[] = {
347 { "vec4_add", IR_ADD
, 1, 2 },
348 { "vec4_subtract", IR_SUB
, 1, 2 },
349 { "vec4_multiply", IR_MUL
, 1, 2 },
350 { "vec4_dot", IR_DOT4
, 1, 2 },
351 { "vec3_dot", IR_DOT3
, 1, 2 },
352 { "vec3_cross", IR_CROSS
, 1, 2 },
353 { "vec4_lrp", IR_LRP
, 1, 3 },
354 { "vec4_min", IR_MIN
, 1, 2 },
355 { "vec4_max", IR_MAX
, 1, 2 },
356 { "vec4_clamp", IR_CLAMP
, 1, 3 },
357 { "vec4_seq", IR_SEQUAL
, 1, 2 },
358 { "vec4_sne", IR_SNEQUAL
, 1, 2 },
359 { "vec4_sge", IR_SGE
, 1, 2 },
360 { "vec4_sgt", IR_SGT
, 1, 2 },
361 { "vec4_sle", IR_SLE
, 1, 2 },
362 { "vec4_slt", IR_SLT
, 1, 2 },
364 { "vec4_floor", IR_FLOOR
, 1, 1 },
365 { "vec4_frac", IR_FRAC
, 1, 1 },
366 { "vec4_abs", IR_ABS
, 1, 1 },
367 { "vec4_negate", IR_NEG
, 1, 1 },
368 { "vec4_ddx", IR_DDX
, 1, 1 },
369 { "vec4_ddy", IR_DDY
, 1, 1 },
370 /* float binary op */
371 { "float_power", IR_POW
, 1, 2 },
372 /* texture / sampler */
373 { "vec4_tex1d", IR_TEX
, 1, 2 },
374 { "vec4_texb1d", IR_TEXB
, 1, 2 }, /* 1d w/ bias */
375 { "vec4_texp1d", IR_TEXP
, 1, 2 }, /* 1d w/ projection */
376 { "vec4_tex2d", IR_TEX
, 1, 2 },
377 { "vec4_texb2d", IR_TEXB
, 1, 2 }, /* 2d w/ bias */
378 { "vec4_texp2d", IR_TEXP
, 1, 2 }, /* 2d w/ projection */
379 { "vec4_tex3d", IR_TEX
, 1, 2 },
380 { "vec4_texb3d", IR_TEXB
, 1, 2 }, /* 3d w/ bias */
381 { "vec4_texp3d", IR_TEXP
, 1, 2 }, /* 3d w/ projection */
382 { "vec4_texcube", IR_TEX
, 1, 2 }, /* cubemap */
383 { "vec4_tex_rect", IR_TEX
, 1, 2 }, /* rectangle */
384 { "vec4_texp_rect", IR_TEX
, 1, 2 },/* rectangle w/ projection */
387 { "int_to_float", IR_I_TO_F
, 1, 1 },
388 { "float_to_int", IR_F_TO_I
, 1, 1 },
389 { "float_exp", IR_EXP
, 1, 1 },
390 { "float_exp2", IR_EXP2
, 1, 1 },
391 { "float_log2", IR_LOG2
, 1, 1 },
392 { "float_rsq", IR_RSQ
, 1, 1 },
393 { "float_rcp", IR_RCP
, 1, 1 },
394 { "float_sine", IR_SIN
, 1, 1 },
395 { "float_cosine", IR_COS
, 1, 1 },
396 { "float_noise1", IR_NOISE1
, 1, 1},
397 { "float_noise2", IR_NOISE2
, 1, 1},
398 { "float_noise3", IR_NOISE3
, 1, 1},
399 { "float_noise4", IR_NOISE4
, 1, 1},
401 { NULL
, IR_NOP
, 0, 0 }
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
->List
= loopNode
->List
;
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
->List
= loopNode
->List
;
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
->List
= loopNode
->List
;
545 static slang_ir_node
*
546 new_cond(slang_ir_node
*n
)
548 slang_ir_node
*c
= new_node1(IR_COND
, n
);
553 static slang_ir_node
*
554 new_if(slang_ir_node
*cond
, slang_ir_node
*ifPart
, slang_ir_node
*elsePart
)
556 return new_node3(IR_IF
, cond
, ifPart
, elsePart
);
561 * New IR_VAR node - a reference to a previously declared variable.
563 static slang_ir_node
*
564 new_var(slang_assemble_ctx
*A
, slang_operation
*oper
, slang_atom name
)
567 slang_variable
*var
= _slang_locate_variable(oper
->locals
, name
, GL_TRUE
);
571 assert(!oper
->var
|| oper
->var
== var
);
573 n
= new_node0(IR_VAR
);
575 _slang_attach_storage(n
, var
);
582 * Check if the given function is really just a wrapper for a
583 * basic assembly instruction.
586 slang_is_asm_function(const slang_function
*fun
)
588 if (fun
->body
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
&&
589 fun
->body
->num_children
== 1 &&
590 fun
->body
->children
[0].type
== SLANG_OPER_ASM
) {
598 _slang_is_noop(const slang_operation
*oper
)
601 oper
->type
== SLANG_OPER_VOID
||
602 (oper
->num_children
== 1 && oper
->children
[0].type
== SLANG_OPER_VOID
))
610 * Produce inline code for a call to an assembly instruction.
611 * XXX Note: children are passed as asm args in-order, not by name!
613 static slang_operation
*
614 slang_inline_asm_function(slang_assemble_ctx
*A
,
615 slang_function
*fun
, slang_operation
*oper
)
617 const GLuint numArgs
= oper
->num_children
;
618 const slang_operation
*args
= oper
->children
;
620 slang_operation
*inlined
= slang_operation_new(1);
622 /*assert(oper->type == SLANG_OPER_CALL); or vec4_add, etc */
624 printf("Inline asm %s\n", (char*) fun->header.a_name);
626 inlined
->type
= fun
->body
->children
[0].type
;
627 inlined
->a_id
= fun
->body
->children
[0].a_id
;
628 inlined
->num_children
= numArgs
;
629 inlined
->children
= slang_operation_new(numArgs
);
630 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
632 for (i
= 0; i
< numArgs
; i
++) {
633 slang_operation_copy(inlined
->children
+ i
, args
+ i
);
641 slang_resolve_variable(slang_operation
*oper
)
643 if (oper
->type
== SLANG_OPER_IDENTIFIER
&& !oper
->var
) {
644 oper
->var
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
650 * Replace particular variables (SLANG_OPER_IDENTIFIER) with new expressions.
653 slang_substitute(slang_assemble_ctx
*A
, slang_operation
*oper
,
654 GLuint substCount
, slang_variable
**substOld
,
655 slang_operation
**substNew
, GLboolean isLHS
)
657 switch (oper
->type
) {
658 case SLANG_OPER_VARIABLE_DECL
:
660 slang_variable
*v
= _slang_locate_variable(oper
->locals
,
661 oper
->a_id
, GL_TRUE
);
663 if (v
->initializer
&& oper
->num_children
== 0) {
664 /* set child of oper to copy of initializer */
665 oper
->num_children
= 1;
666 oper
->children
= slang_operation_new(1);
667 slang_operation_copy(&oper
->children
[0], v
->initializer
);
669 if (oper
->num_children
== 1) {
670 /* the initializer */
671 slang_substitute(A
, &oper
->children
[0], substCount
,
672 substOld
, substNew
, GL_FALSE
);
676 case SLANG_OPER_IDENTIFIER
:
677 assert(oper
->num_children
== 0);
678 if (1/**!isLHS XXX FIX */) {
679 slang_atom id
= oper
->a_id
;
682 v
= _slang_locate_variable(oper
->locals
, id
, GL_TRUE
);
684 printf("var %s not found!\n", (char *) oper
->a_id
);
685 _slang_print_var_scope(oper
->locals
, 6);
691 /* look for a substitution */
692 for (i
= 0; i
< substCount
; i
++) {
693 if (v
== substOld
[i
]) {
694 /* OK, replace this SLANG_OPER_IDENTIFIER with a new expr */
695 #if 0 /* DEBUG only */
696 if (substNew
[i
]->type
== SLANG_OPER_IDENTIFIER
) {
697 assert(substNew
[i
]->var
);
698 assert(substNew
[i
]->var
->a_name
);
699 printf("Substitute %s with %s in id node %p\n",
700 (char*)v
->a_name
, (char*) substNew
[i
]->var
->a_name
,
704 printf("Substitute %s with %f in id node %p\n",
705 (char*)v
->a_name
, substNew
[i
]->literal
[0],
709 slang_operation_copy(oper
, substNew
[i
]);
716 case SLANG_OPER_RETURN
:
717 /* do return replacement here too */
718 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
719 if (!_slang_is_noop(oper
)) {
725 * then do substitutions on the assignment.
727 slang_operation
*blockOper
, *assignOper
, *returnOper
;
728 blockOper
= slang_operation_new(1);
729 blockOper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
730 blockOper
->num_children
= 2;
731 blockOper
->locals
->outer_scope
= oper
->locals
->outer_scope
;
732 blockOper
->children
= slang_operation_new(2);
733 assignOper
= blockOper
->children
+ 0;
734 returnOper
= blockOper
->children
+ 1;
736 assignOper
->type
= SLANG_OPER_ASSIGN
;
737 assignOper
->num_children
= 2;
738 assignOper
->locals
->outer_scope
= blockOper
->locals
;
739 assignOper
->children
= slang_operation_new(2);
740 assignOper
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
741 assignOper
->children
[0].a_id
= slang_atom_pool_atom(A
->atoms
, "__retVal");
742 assignOper
->children
[0].locals
->outer_scope
= assignOper
->locals
;
744 slang_operation_copy(&assignOper
->children
[1],
747 returnOper
->type
= SLANG_OPER_RETURN
;
748 assert(returnOper
->num_children
== 0);
750 /* do substitutions on the "__retVal = expr" sub-tree */
751 slang_substitute(A
, assignOper
,
752 substCount
, substOld
, substNew
, GL_FALSE
);
754 /* install new code */
755 slang_operation_copy(oper
, blockOper
);
756 slang_operation_destruct(blockOper
);
760 case SLANG_OPER_ASSIGN
:
761 case SLANG_OPER_SUBSCRIPT
:
763 * child[0] can't have substitutions but child[1] can.
765 slang_substitute(A
, &oper
->children
[0],
766 substCount
, substOld
, substNew
, GL_TRUE
);
767 slang_substitute(A
, &oper
->children
[1],
768 substCount
, substOld
, substNew
, GL_FALSE
);
770 case SLANG_OPER_FIELD
:
772 slang_substitute(A
, &oper
->children
[0],
773 substCount
, substOld
, substNew
, GL_TRUE
);
778 for (i
= 0; i
< oper
->num_children
; i
++)
779 slang_substitute(A
, &oper
->children
[i
],
780 substCount
, substOld
, substNew
, GL_FALSE
);
788 * Inline the given function call operation.
789 * Return a new slang_operation that corresponds to the inlined code.
791 static slang_operation
*
792 slang_inline_function_call(slang_assemble_ctx
* A
, slang_function
*fun
,
793 slang_operation
*oper
, slang_operation
*returnOper
)
800 ParamMode
*paramMode
;
801 const GLboolean haveRetValue
= _slang_function_has_return_value(fun
);
802 const GLuint numArgs
= oper
->num_children
;
803 const GLuint totalArgs
= numArgs
+ haveRetValue
;
804 slang_operation
*args
= oper
->children
;
805 slang_operation
*inlined
, *top
;
806 slang_variable
**substOld
;
807 slang_operation
**substNew
;
808 GLuint substCount
, numCopyIn
, i
;
810 /*assert(oper->type == SLANG_OPER_CALL); (or (matrix) multiply, etc) */
811 assert(fun
->param_count
== totalArgs
);
813 /* allocate temporary arrays */
814 paramMode
= (ParamMode
*)
815 _mesa_calloc(totalArgs
* sizeof(ParamMode
));
816 substOld
= (slang_variable
**)
817 _mesa_calloc(totalArgs
* sizeof(slang_variable
*));
818 substNew
= (slang_operation
**)
819 _mesa_calloc(totalArgs
* sizeof(slang_operation
*));
822 printf("Inline call to %s (total vars=%d nparams=%d)\n",
823 (char *) fun
->header
.a_name
,
824 fun
->parameters
->num_variables
, numArgs
);
827 if (haveRetValue
&& !returnOper
) {
828 /* Create 3-child comma sequence for inlined code:
829 * child[0]: declare __resultTmp
830 * child[1]: inlined function body
831 * child[2]: __resultTmp
833 slang_operation
*commaSeq
;
834 slang_operation
*declOper
= NULL
;
835 slang_variable
*resultVar
;
837 commaSeq
= slang_operation_new(1);
838 commaSeq
->type
= SLANG_OPER_SEQUENCE
;
839 assert(commaSeq
->locals
);
840 commaSeq
->locals
->outer_scope
= oper
->locals
->outer_scope
;
841 commaSeq
->num_children
= 3;
842 commaSeq
->children
= slang_operation_new(3);
843 /* allocate the return var */
844 resultVar
= slang_variable_scope_grow(commaSeq
->locals
);
846 printf("Alloc __resultTmp in scope %p for retval of calling %s\n",
847 (void*)commaSeq->locals, (char *) fun->header.a_name);
850 resultVar
->a_name
= slang_atom_pool_atom(A
->atoms
, "__resultTmp");
851 resultVar
->type
= fun
->header
.type
; /* XXX copy? */
852 resultVar
->isTemp
= GL_TRUE
;
854 /* child[0] = __resultTmp declaration */
855 declOper
= &commaSeq
->children
[0];
856 declOper
->type
= SLANG_OPER_VARIABLE_DECL
;
857 declOper
->a_id
= resultVar
->a_name
;
858 declOper
->locals
->outer_scope
= commaSeq
->locals
;
860 /* child[1] = function body */
861 inlined
= &commaSeq
->children
[1];
862 inlined
->locals
->outer_scope
= commaSeq
->locals
;
864 /* child[2] = __resultTmp reference */
865 returnOper
= &commaSeq
->children
[2];
866 returnOper
->type
= SLANG_OPER_IDENTIFIER
;
867 returnOper
->a_id
= resultVar
->a_name
;
868 returnOper
->locals
->outer_scope
= commaSeq
->locals
;
873 top
= inlined
= slang_operation_new(1);
874 /* XXXX this may be inappropriate!!!! */
875 inlined
->locals
->outer_scope
= oper
->locals
->outer_scope
;
879 assert(inlined
->locals
);
881 /* Examine the parameters, look for inout/out params, look for possible
882 * substitutions, etc:
883 * param type behaviour
884 * in copy actual to local
885 * const in substitute param with actual
889 for (i
= 0; i
< totalArgs
; i
++) {
890 slang_variable
*p
= fun
->parameters
->variables
[i
];
892 printf("Param %d: %s %s \n", i,
893 slang_type_qual_string(p->type.qualifier),
896 if (p
->type
.qualifier
== SLANG_QUAL_INOUT
||
897 p
->type
.qualifier
== SLANG_QUAL_OUT
) {
898 /* an output param */
899 slang_operation
*arg
;
904 paramMode
[i
] = SUBST
;
906 if (arg
->type
== SLANG_OPER_IDENTIFIER
)
907 slang_resolve_variable(arg
);
909 /* replace parameter 'p' with argument 'arg' */
910 substOld
[substCount
] = p
;
911 substNew
[substCount
] = arg
; /* will get copied */
914 else if (p
->type
.qualifier
== SLANG_QUAL_CONST
) {
915 /* a constant input param */
916 if (args
[i
].type
== SLANG_OPER_IDENTIFIER
||
917 args
[i
].type
== SLANG_OPER_LITERAL_FLOAT
) {
918 /* replace all occurances of this parameter variable with the
919 * actual argument variable or a literal.
921 paramMode
[i
] = SUBST
;
922 slang_resolve_variable(&args
[i
]);
923 substOld
[substCount
] = p
;
924 substNew
[substCount
] = &args
[i
]; /* will get copied */
928 paramMode
[i
] = COPY_IN
;
932 paramMode
[i
] = COPY_IN
;
934 assert(paramMode
[i
]);
937 /* actual code inlining: */
938 slang_operation_copy(inlined
, fun
->body
);
940 /*** XXX review this */
941 assert(inlined
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
);
942 inlined
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
;
945 printf("======================= orig body code ======================\n");
946 printf("=== params scope = %p\n", (void*) fun
->parameters
);
947 slang_print_tree(fun
->body
, 8);
948 printf("======================= copied code =========================\n");
949 slang_print_tree(inlined
, 8);
952 /* do parameter substitution in inlined code: */
953 slang_substitute(A
, inlined
, substCount
, substOld
, substNew
, GL_FALSE
);
956 printf("======================= subst code ==========================\n");
957 slang_print_tree(inlined
, 8);
958 printf("=============================================================\n");
961 /* New prolog statements: (inserted before the inlined code)
962 * Copy the 'in' arguments.
965 for (i
= 0; i
< numArgs
; i
++) {
966 if (paramMode
[i
] == COPY_IN
) {
967 slang_variable
*p
= fun
->parameters
->variables
[i
];
968 /* declare parameter 'p' */
969 slang_operation
*decl
= slang_operation_insert(&inlined
->num_children
,
973 printf("COPY_IN %s from expr\n", (char*)p->a_name);
975 decl
->type
= SLANG_OPER_VARIABLE_DECL
;
976 assert(decl
->locals
);
977 decl
->locals
->outer_scope
= inlined
->locals
;
978 decl
->a_id
= p
->a_name
;
979 decl
->num_children
= 1;
980 decl
->children
= slang_operation_new(1);
982 /* child[0] is the var's initializer */
983 slang_operation_copy(&decl
->children
[0], args
+ i
);
989 /* New epilog statements:
990 * 1. Create end of function label to jump to from return statements.
991 * 2. Copy the 'out' parameter vars
994 slang_operation
*lab
= slang_operation_insert(&inlined
->num_children
,
996 inlined
->num_children
);
997 lab
->type
= SLANG_OPER_LABEL
;
998 lab
->label
= A
->curFuncEndLabel
;
1001 for (i
= 0; i
< totalArgs
; i
++) {
1002 if (paramMode
[i
] == COPY_OUT
) {
1003 const slang_variable
*p
= fun
->parameters
->variables
[i
];
1004 /* actualCallVar = outParam */
1005 /*if (i > 0 || !haveRetValue)*/
1006 slang_operation
*ass
= slang_operation_insert(&inlined
->num_children
,
1008 inlined
->num_children
);
1009 ass
->type
= SLANG_OPER_ASSIGN
;
1010 ass
->num_children
= 2;
1011 ass
->locals
->outer_scope
= inlined
->locals
;
1012 ass
->children
= slang_operation_new(2);
1013 ass
->children
[0] = args
[i
]; /*XXX copy */
1014 ass
->children
[1].type
= SLANG_OPER_IDENTIFIER
;
1015 ass
->children
[1].a_id
= p
->a_name
;
1016 ass
->children
[1].locals
->outer_scope
= ass
->locals
;
1020 _mesa_free(paramMode
);
1021 _mesa_free(substOld
);
1022 _mesa_free(substNew
);
1025 printf("Done Inline call to %s (total vars=%d nparams=%d)\n",
1026 (char *) fun
->header
.a_name
,
1027 fun
->parameters
->num_variables
, numArgs
);
1028 slang_print_tree(top
, 0);
1034 static slang_ir_node
*
1035 _slang_gen_function_call(slang_assemble_ctx
*A
, slang_function
*fun
,
1036 slang_operation
*oper
, slang_operation
*dest
)
1039 slang_operation
*inlined
;
1040 slang_label
*prevFuncEndLabel
;
1043 prevFuncEndLabel
= A
->curFuncEndLabel
;
1044 sprintf(name
, "__endOfFunc_%s_", (char *) fun
->header
.a_name
);
1045 A
->curFuncEndLabel
= _slang_label_new(name
);
1046 assert(A
->curFuncEndLabel
);
1048 if (slang_is_asm_function(fun
) && !dest
) {
1049 /* assemble assembly function - tree style */
1050 inlined
= slang_inline_asm_function(A
, fun
, oper
);
1053 /* non-assembly function */
1054 inlined
= slang_inline_function_call(A
, fun
, oper
, dest
);
1057 /* Replace the function call with the inlined block */
1059 slang_operation_construct(oper
);
1060 slang_operation_copy(oper
, inlined
);
1062 *oper
= *inlined
; /* XXX slang_operation_copy() */
1067 assert(inlined
->locals
);
1068 printf("*** Inlined code for call to %s:\n",
1069 (char*) fun
->header
.a_name
);
1070 slang_print_tree(oper
, 10);
1074 n
= _slang_gen_operation(A
, oper
);
1076 /*_slang_label_delete(A->curFuncEndLabel);*/
1077 A
->curFuncEndLabel
= prevFuncEndLabel
;
1078 assert(A
->curFuncEndLabel
);
1084 static slang_asm_info
*
1085 slang_find_asm_info(const char *name
)
1088 for (i
= 0; AsmInfo
[i
].Name
; i
++) {
1089 if (_mesa_strcmp(AsmInfo
[i
].Name
, name
) == 0) {
1098 make_writemask(const char *field
)
1104 mask
|= WRITEMASK_X
;
1107 mask
|= WRITEMASK_Y
;
1110 mask
|= WRITEMASK_Z
;
1113 mask
|= WRITEMASK_W
;
1121 return WRITEMASK_XYZW
;
1128 * Generate IR tree for an asm instruction/operation such as:
1129 * __asm vec4_dot __retVal.x, v1, v2;
1131 static slang_ir_node
*
1132 _slang_gen_asm(slang_assemble_ctx
*A
, slang_operation
*oper
,
1133 slang_operation
*dest
)
1135 const slang_asm_info
*info
;
1136 slang_ir_node
*kids
[3], *n
;
1137 GLuint j
, firstOperand
;
1139 assert(oper
->type
== SLANG_OPER_ASM
);
1141 info
= slang_find_asm_info((char *) oper
->a_id
);
1143 _mesa_problem(NULL
, "undefined __asm function %s\n",
1144 (char *) oper
->a_id
);
1147 assert(info
->NumParams
<= 3);
1149 if (info
->NumParams
== oper
->num_children
) {
1150 /* Storage for result is not specified.
1151 * Children[0], [1] are the operands.
1156 /* Storage for result (child[0]) is specified.
1157 * Children[1], [2] are the operands.
1162 /* assemble child(ren) */
1163 kids
[0] = kids
[1] = kids
[2] = NULL
;
1164 for (j
= 0; j
< info
->NumParams
; j
++) {
1165 kids
[j
] = _slang_gen_operation(A
, &oper
->children
[firstOperand
+ j
]);
1170 n
= new_node3(info
->Opcode
, kids
[0], kids
[1], kids
[2]);
1173 /* Setup n->Store to be a particular location. Otherwise, storage
1174 * for the result (a temporary) will be allocated later.
1176 GLuint writemask
= WRITEMASK_XYZW
;
1177 slang_operation
*dest_oper
;
1180 dest_oper
= &oper
->children
[0];
1181 while (dest_oper
->type
== SLANG_OPER_FIELD
) {
1183 writemask
&= make_writemask((char*) dest_oper
->a_id
);
1184 dest_oper
= &dest_oper
->children
[0];
1187 n0
= _slang_gen_operation(A
, dest_oper
);
1191 n
->Store
= n0
->Store
;
1192 n
->Writemask
= writemask
;
1202 print_funcs(struct slang_function_scope_
*scope
, const char *name
)
1205 for (i
= 0; i
< scope
->num_functions
; i
++) {
1206 slang_function
*f
= &scope
->functions
[i
];
1207 if (!name
|| strcmp(name
, (char*) f
->header
.a_name
) == 0)
1208 printf(" %s (%d args)\n", name
, f
->param_count
);
1211 if (scope
->outer_scope
)
1212 print_funcs(scope
->outer_scope
, name
);
1217 * Return first function in the scope that has the given name.
1218 * This is the function we'll try to call when there is no exact match
1219 * between function parameters and call arguments.
1221 * XXX we should really create a list of candidate functions and try
1224 static slang_function
*
1225 _slang_first_function(struct slang_function_scope_
*scope
, const char *name
)
1228 for (i
= 0; i
< scope
->num_functions
; i
++) {
1229 slang_function
*f
= &scope
->functions
[i
];
1230 if (strcmp(name
, (char*) f
->header
.a_name
) == 0)
1233 if (scope
->outer_scope
)
1234 return _slang_first_function(scope
->outer_scope
, name
);
1241 * Assemble a function call, given a particular function name.
1242 * \param name the function's name (operators like '*' are possible).
1244 static slang_ir_node
*
1245 _slang_gen_function_call_name(slang_assemble_ctx
*A
, const char *name
,
1246 slang_operation
*oper
, slang_operation
*dest
)
1248 slang_operation
*params
= oper
->children
;
1249 const GLuint param_count
= oper
->num_children
;
1251 slang_function
*fun
;
1253 atom
= slang_atom_pool_atom(A
->atoms
, name
);
1254 if (atom
== SLANG_ATOM_NULL
)
1258 * Use 'name' to find the function to call
1260 fun
= _slang_locate_function(A
->space
.funcs
, atom
, params
, param_count
,
1261 &A
->space
, A
->atoms
, A
->log
);
1263 /* A function with exactly the right parameters/types was not found.
1264 * Try adapting the parameters.
1266 fun
= _slang_first_function(A
->space
.funcs
, name
);
1267 if (!_slang_adapt_call(oper
, fun
, &A
->space
, A
->atoms
, A
->log
)) {
1268 slang_info_log_error(A
->log
, "Function '%s' not found (check argument types)", name
);
1274 return _slang_gen_function_call(A
, fun
, oper
, dest
);
1279 _slang_is_constant_cond(const slang_operation
*oper
, GLboolean
*value
)
1281 if (oper
->type
== SLANG_OPER_LITERAL_FLOAT
||
1282 oper
->type
== SLANG_OPER_LITERAL_INT
||
1283 oper
->type
== SLANG_OPER_LITERAL_BOOL
) {
1284 if (oper
->literal
[0])
1290 else if (oper
->type
== SLANG_OPER_EXPRESSION
&&
1291 oper
->num_children
== 1) {
1292 return _slang_is_constant_cond(&oper
->children
[0], value
);
1300 * Generate loop code using high-level IR_LOOP instruction
1302 static slang_ir_node
*
1303 _slang_gen_while(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1307 * BREAK if !expr (child[0])
1308 * body code (child[1])
1310 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
;
1311 GLboolean isConst
, constTrue
;
1313 /* Check if loop condition is a constant */
1314 isConst
= _slang_is_constant_cond(&oper
->children
[0], &constTrue
);
1316 if (isConst
&& !constTrue
) {
1317 /* loop is never executed! */
1318 return new_node0(IR_NOP
);
1321 loop
= new_loop(NULL
);
1323 /* save old, push new loop */
1324 prevLoop
= A
->CurLoop
;
1327 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[0]));
1328 if (isConst
&& constTrue
) {
1329 /* while(nonzero constant), no conditional break */
1333 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1335 body
= _slang_gen_operation(A
, &oper
->children
[1]);
1336 loop
->Children
[0] = new_seq(breakIf
, body
);
1338 /* Do infinite loop detection */
1339 /* loop->List is head of linked list of break/continue nodes */
1340 if (!loop
->List
&& isConst
&& constTrue
) {
1341 /* infinite loop detected */
1342 A
->CurLoop
= prevLoop
; /* clean-up */
1343 slang_info_log_error(A
->log
, "Infinite loop detected!");
1347 /* pop loop, restore prev */
1348 A
->CurLoop
= prevLoop
;
1355 * Generate IR tree for a do-while loop using high-level LOOP, IF instructions.
1357 static slang_ir_node
*
1358 _slang_gen_do(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1362 * body code (child[0])
1364 * BREAK if !expr (child[1])
1366 slang_ir_node
*prevLoop
, *loop
, *cond
;
1367 GLboolean isConst
, constTrue
;
1369 loop
= new_loop(NULL
);
1371 /* save old, push new loop */
1372 prevLoop
= A
->CurLoop
;
1376 loop
->Children
[0] = _slang_gen_operation(A
, &oper
->children
[0]);
1378 /* Check if loop condition is a constant */
1379 isConst
= _slang_is_constant_cond(&oper
->children
[1], &constTrue
);
1380 if (isConst
&& constTrue
) {
1381 /* do { } while(1) ==> no conditional break */
1382 loop
->Children
[1] = NULL
; /* no tail code */
1385 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[1]));
1386 loop
->Children
[1] = new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1389 /* XXX we should do infinite loop detection, as above */
1391 /* pop loop, restore prev */
1392 A
->CurLoop
= prevLoop
;
1399 * Generate for-loop using high-level IR_LOOP instruction.
1401 static slang_ir_node
*
1402 _slang_gen_for(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1405 * init code (child[0])
1407 * BREAK if !expr (child[1])
1408 * body code (child[3])
1410 * incr code (child[2]) // XXX continue here
1412 slang_ir_node
*prevLoop
, *loop
, *cond
, *breakIf
, *body
, *init
, *incr
;
1414 init
= _slang_gen_operation(A
, &oper
->children
[0]);
1415 loop
= new_loop(NULL
);
1417 /* save old, push new loop */
1418 prevLoop
= A
->CurLoop
;
1421 cond
= new_cond(_slang_gen_operation(A
, &oper
->children
[1]));
1422 breakIf
= new_break_if(A
->CurLoop
, cond
, GL_FALSE
);
1423 body
= _slang_gen_operation(A
, &oper
->children
[3]);
1424 incr
= _slang_gen_operation(A
, &oper
->children
[2]);
1426 loop
->Children
[0] = new_seq(breakIf
, body
);
1427 loop
->Children
[1] = incr
; /* tail code */
1429 /* pop loop, restore prev */
1430 A
->CurLoop
= prevLoop
;
1432 return new_seq(init
, loop
);
1436 static slang_ir_node
*
1437 _slang_gen_continue(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1439 slang_ir_node
*n
, *loopNode
;
1440 assert(oper
->type
== SLANG_OPER_CONTINUE
);
1441 loopNode
= A
->CurLoop
;
1443 assert(loopNode
->Opcode
== IR_LOOP
);
1444 n
= new_node0(IR_CONT
);
1446 n
->Parent
= loopNode
;
1447 /* insert this node at head of linked list */
1448 n
->List
= loopNode
->List
;
1456 * Determine if the given operation is of a specific type.
1459 is_operation_type(const const slang_operation
*oper
, slang_operation_type type
)
1461 if (oper
->type
== type
)
1463 else if ((oper
->type
== SLANG_OPER_BLOCK_NEW_SCOPE
||
1464 oper
->type
== SLANG_OPER_BLOCK_NO_NEW_SCOPE
) &&
1465 oper
->num_children
== 1)
1466 return is_operation_type(&oper
->children
[0], type
);
1473 * Generate IR tree for an if/then/else conditional using high-level
1474 * IR_IF instruction.
1476 static slang_ir_node
*
1477 _slang_gen_if(slang_assemble_ctx
* A
, const slang_operation
*oper
)
1480 * eval expr (child[0])
1487 const GLboolean haveElseClause
= !_slang_is_noop(&oper
->children
[2]);
1488 slang_ir_node
*ifNode
, *cond
, *ifBody
, *elseBody
;
1490 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1491 cond
= new_cond(cond
);
1493 if (is_operation_type(&oper
->children
[1], SLANG_OPER_BREAK
)) {
1494 /* Special case: generate a conditional break */
1495 ifBody
= new_break_if(A
->CurLoop
, cond
, GL_TRUE
);
1496 if (haveElseClause
) {
1497 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1498 return new_seq(ifBody
, elseBody
);
1502 else if (is_operation_type(&oper
->children
[1], SLANG_OPER_CONTINUE
)) {
1503 /* Special case: generate a conditional break */
1504 ifBody
= new_cont_if(A
->CurLoop
, cond
, GL_TRUE
);
1505 if (haveElseClause
) {
1506 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1507 return new_seq(ifBody
, elseBody
);
1513 ifBody
= _slang_gen_operation(A
, &oper
->children
[1]);
1515 elseBody
= _slang_gen_operation(A
, &oper
->children
[2]);
1518 ifNode
= new_if(cond
, ifBody
, elseBody
);
1526 * Generate IR node for storage of a temporary of given size.
1528 static slang_ir_node
*
1529 _slang_gen_temporary(GLint size
)
1531 slang_ir_storage
*store
;
1534 store
= _slang_new_ir_storage(PROGRAM_TEMPORARY
, -1, size
);
1536 n
= new_node0(IR_VAR_DECL
);
1549 * Generate IR node for allocating/declaring a variable.
1551 static slang_ir_node
*
1552 _slang_gen_var_decl(slang_assemble_ctx
*A
, slang_variable
*var
)
1555 assert(!is_sampler_type(&var
->type
));
1556 n
= new_node0(IR_VAR_DECL
);
1558 _slang_attach_storage(n
, var
);
1561 assert(n
->Store
== var
->aux
);
1563 assert(n
->Store
->Index
< 0);
1565 n
->Store
->File
= PROGRAM_TEMPORARY
;
1566 n
->Store
->Size
= _slang_sizeof_type_specifier(&n
->Var
->type
.specifier
);
1567 assert(n
->Store
->Size
> 0);
1574 * Generate code for a selection expression: b ? x : y
1575 * XXX In some cases we could implement a selection expression
1576 * with an LRP instruction (use the boolean as the interpolant).
1577 * Otherwise, we use an IF/ELSE/ENDIF construct.
1579 static slang_ir_node
*
1580 _slang_gen_select(slang_assemble_ctx
*A
, slang_operation
*oper
)
1582 slang_ir_node
*cond
, *ifNode
, *trueExpr
, *falseExpr
, *trueNode
, *falseNode
;
1583 slang_ir_node
*tmpDecl
, *tmpVar
, *tree
;
1584 slang_typeinfo type
;
1587 assert(oper
->type
== SLANG_OPER_SELECT
);
1588 assert(oper
->num_children
== 3);
1590 /* size of x or y's type */
1591 slang_typeinfo_construct(&type
);
1592 _slang_typeof_operation(A
, &oper
->children
[1], &type
);
1593 size
= _slang_sizeof_type_specifier(&type
.spec
);
1597 tmpDecl
= _slang_gen_temporary(size
);
1599 /* the condition (child 0) */
1600 cond
= _slang_gen_operation(A
, &oper
->children
[0]);
1601 cond
= new_cond(cond
);
1603 /* if-true body (child 1) */
1604 tmpVar
= new_node0(IR_VAR
);
1605 tmpVar
->Store
= tmpDecl
->Store
;
1606 trueExpr
= _slang_gen_operation(A
, &oper
->children
[1]);
1607 trueNode
= new_node2(IR_MOVE
, tmpVar
, trueExpr
);
1609 /* if-false body (child 2) */
1610 tmpVar
= new_node0(IR_VAR
);
1611 tmpVar
->Store
= tmpDecl
->Store
;
1612 falseExpr
= _slang_gen_operation(A
, &oper
->children
[2]);
1613 falseNode
= new_node2(IR_MOVE
, tmpVar
, falseExpr
);
1615 ifNode
= new_if(cond
, trueNode
, falseNode
);
1618 tmpVar
= new_node0(IR_VAR
);
1619 tmpVar
->Store
= tmpDecl
->Store
;
1621 tree
= new_seq(ifNode
, tmpVar
);
1622 tree
= new_seq(tmpDecl
, tree
);
1624 /*_slang_print_ir_tree(tree, 10);*/
1630 * Generate code for &&.
1632 static slang_ir_node
*
1633 _slang_gen_logical_and(slang_assemble_ctx
*A
, slang_operation
*oper
)
1635 /* rewrite "a && b" as "a ? b : false" */
1636 slang_operation
*select
;
1639 select
= slang_operation_new(1);
1640 select
->type
= SLANG_OPER_SELECT
;
1641 select
->num_children
= 3;
1642 select
->children
= slang_operation_new(3);
1644 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1645 slang_operation_copy(&select
->children
[1], &oper
->children
[1]);
1646 select
->children
[2].type
= SLANG_OPER_LITERAL_BOOL
;
1647 ASSIGN_4V(select
->children
[2].literal
, 0, 0, 0, 0); /* false */
1648 select
->children
[2].literal_size
= 1;
1650 n
= _slang_gen_select(A
, select
);
1653 free(select
->children
);
1661 * Generate code for ||.
1663 static slang_ir_node
*
1664 _slang_gen_logical_or(slang_assemble_ctx
*A
, slang_operation
*oper
)
1666 /* rewrite "a || b" as "a ? true : b" */
1667 slang_operation
*select
;
1670 select
= slang_operation_new(1);
1671 select
->type
= SLANG_OPER_SELECT
;
1672 select
->num_children
= 3;
1673 select
->children
= slang_operation_new(3);
1675 slang_operation_copy(&select
->children
[0], &oper
->children
[0]);
1676 select
->children
[1].type
= SLANG_OPER_LITERAL_BOOL
;
1677 ASSIGN_4V(select
->children
[1].literal
, 1, 1, 1, 1); /* true */
1678 select
->children
[1].literal_size
= 1;
1679 slang_operation_copy(&select
->children
[2], &oper
->children
[1]);
1681 n
= _slang_gen_select(A
, select
);
1684 free(select
->children
);
1692 * Generate IR tree for a return statement.
1694 static slang_ir_node
*
1695 _slang_gen_return(slang_assemble_ctx
* A
, slang_operation
*oper
)
1697 if (oper
->num_children
== 0 ||
1698 (oper
->num_children
== 1 &&
1699 oper
->children
[0].type
== SLANG_OPER_VOID
)) {
1703 * goto __endOfFunction;
1706 slang_operation gotoOp
;
1707 slang_operation_construct(&gotoOp
);
1708 gotoOp
.type
= SLANG_OPER_GOTO
;
1709 gotoOp
.label
= A
->curFuncEndLabel
;
1710 assert(gotoOp
.label
);
1712 /* assemble the new code */
1713 n
= _slang_gen_operation(A
, &gotoOp
);
1714 /* destroy temp code */
1715 slang_operation_destruct(&gotoOp
);
1724 * goto __endOfFunction;
1726 slang_operation
*block
, *assign
, *jump
;
1727 slang_atom a_retVal
;
1730 a_retVal
= slang_atom_pool_atom(A
->atoms
, "__retVal");
1736 = _slang_locate_variable(oper
->locals
, a_retVal
, GL_TRUE
);
1741 block
= slang_operation_new(1);
1742 block
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
;
1743 assert(block
->locals
);
1744 block
->locals
->outer_scope
= oper
->locals
->outer_scope
;
1745 block
->num_children
= 2;
1746 block
->children
= slang_operation_new(2);
1748 /* child[0]: __retVal = expr; */
1749 assign
= &block
->children
[0];
1750 assign
->type
= SLANG_OPER_ASSIGN
;
1751 assign
->locals
->outer_scope
= block
->locals
;
1752 assign
->num_children
= 2;
1753 assign
->children
= slang_operation_new(2);
1754 /* lhs (__retVal) */
1755 assign
->children
[0].type
= SLANG_OPER_IDENTIFIER
;
1756 assign
->children
[0].a_id
= a_retVal
;
1757 assign
->children
[0].locals
->outer_scope
= assign
->locals
;
1759 /* XXX we might be able to avoid this copy someday */
1760 slang_operation_copy(&assign
->children
[1], &oper
->children
[0]);
1762 /* child[1]: goto __endOfFunction */
1763 jump
= &block
->children
[1];
1764 jump
->type
= SLANG_OPER_GOTO
;
1765 assert(A
->curFuncEndLabel
);
1766 /* XXX don't call function? */
1767 jump
->label
= A
->curFuncEndLabel
;
1768 assert(jump
->label
);
1771 printf("NEW RETURN:\n");
1772 slang_print_tree(block
, 0);
1775 /* assemble the new code */
1776 n
= _slang_gen_operation(A
, block
);
1777 slang_operation_delete(block
);
1784 * Generate IR tree for a variable declaration.
1786 static slang_ir_node
*
1787 _slang_gen_declaration(slang_assemble_ctx
*A
, slang_operation
*oper
)
1790 slang_ir_node
*varDecl
;
1792 const char *varName
= (char *) oper
->a_id
;
1794 assert(oper
->num_children
== 0 || oper
->num_children
== 1);
1796 v
= _slang_locate_variable(oper
->locals
, oper
->a_id
, GL_TRUE
);
1799 varDecl
= _slang_gen_var_decl(A
, v
);
1801 if (oper
->num_children
> 0) {
1802 /* child is initializer */
1803 slang_ir_node
*var
, *init
, *rhs
;
1804 assert(oper
->num_children
== 1);
1805 var
= new_var(A
, oper
, oper
->a_id
);
1807 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
1810 /* XXX make copy of this initializer? */
1811 rhs
= _slang_gen_operation(A
, &oper
->children
[0]);
1813 init
= new_node2(IR_MOVE
, var
, rhs
);
1814 /*assert(rhs->Opcode != IR_SEQ);*/
1815 n
= new_seq(varDecl
, init
);
1817 else if (v
->initializer
) {
1818 slang_ir_node
*var
, *init
, *rhs
;
1819 var
= new_var(A
, oper
, oper
->a_id
);
1821 slang_info_log_error(A
->log
, "undefined variable '%s'", varName
);
1825 /* XXX make copy of this initializer? */
1827 slang_operation dup
;
1828 slang_operation_construct(&dup
);
1829 slang_operation_copy(&dup
, v
->initializer
);
1830 _slang_simplify(&dup
, &A
->space
, A
->atoms
);
1831 rhs
= _slang_gen_operation(A
, &dup
);
1834 _slang_simplify(v
->initializer
, &A
->space
, A
->atoms
);
1835 rhs
= _slang_gen_operation(A
, v
->initializer
);
1838 init
= new_node2(IR_MOVE
, var
, rhs
);
1840 assert(rhs->Opcode != IR_SEQ);
1842 n
= new_seq(varDecl
, init
);
1852 * Generate IR tree for a variable (such as in an expression).
1854 static slang_ir_node
*
1855 _slang_gen_variable(slang_assemble_ctx
* A
, slang_operation
*oper
)
1857 /* If there's a variable associated with this oper (from inlining)
1858 * use it. Otherwise, use the oper's var id.
1860 slang_atom aVar
= oper
->var
? oper
->var
->a_name
: oper
->a_id
;
1861 slang_ir_node
*n
= new_var(A
, oper
, aVar
);
1863 slang_info_log_error(A
->log
, "undefined variable '%s'", (char *) aVar
);
1871 * Some write-masked assignments are simple, but others are hard.
1874 * v.xy = vec2(a, b);
1877 * v.zy = vec2(a, b);
1878 * this gets transformed/swizzled into:
1879 * v.zy = vec2(a, b).*yx* (* = don't care)
1880 * This function helps to determine simple vs. non-simple.
1883 _slang_simple_writemask(GLuint writemask
, GLuint swizzle
)
1885 switch (writemask
) {
1887 return GET_SWZ(swizzle
, 0) == SWIZZLE_X
;
1889 return GET_SWZ(swizzle
, 1) == SWIZZLE_Y
;
1891 return GET_SWZ(swizzle
, 2) == SWIZZLE_Z
;
1893 return GET_SWZ(swizzle
, 3) == SWIZZLE_W
;
1895 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1896 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
);
1898 return (GET_SWZ(swizzle
, 0) == SWIZZLE_X
)
1899 && (GET_SWZ(swizzle
, 1) == SWIZZLE_Y
)
1900 && (GET_SWZ(swizzle
, 2) == SWIZZLE_Z
);
1901 case WRITEMASK_XYZW
:
1902 return swizzle
== SWIZZLE_NOOP
;
1910 * Convert the given swizzle into a writemask. In some cases this
1911 * is trivial, in other cases, we'll need to also swizzle the right
1912 * hand side to put components in the right places.
1913 * \param swizzle the incoming swizzle
1914 * \param writemaskOut returns the writemask
1915 * \param swizzleOut swizzle to apply to the right-hand-side
1916 * \return GL_FALSE for simple writemasks, GL_TRUE for non-simple
1919 swizzle_to_writemask(GLuint swizzle
,
1920 GLuint
*writemaskOut
, GLuint
*swizzleOut
)
1922 GLuint mask
= 0x0, newSwizzle
[4];
1925 /* make new dst writemask, compute size */
1926 for (i
= 0; i
< 4; i
++) {
1927 const GLuint swz
= GET_SWZ(swizzle
, i
);
1928 if (swz
== SWIZZLE_NIL
) {
1932 assert(swz
>= 0 && swz
<= 3);
1935 assert(mask
<= 0xf);
1936 size
= i
; /* number of components in mask/swizzle */
1938 *writemaskOut
= mask
;
1940 /* make new src swizzle, by inversion */
1941 for (i
= 0; i
< 4; i
++) {
1942 newSwizzle
[i
] = i
; /*identity*/
1944 for (i
= 0; i
< size
; i
++) {
1945 const GLuint swz
= GET_SWZ(swizzle
, i
);
1946 newSwizzle
[swz
] = i
;
1948 *swizzleOut
= MAKE_SWIZZLE4(newSwizzle
[0],
1953 if (_slang_simple_writemask(mask
, *swizzleOut
)) {
1955 assert(GET_SWZ(*swizzleOut
, 0) == SWIZZLE_X
);
1957 assert(GET_SWZ(*swizzleOut
, 1) == SWIZZLE_Y
);
1959 assert(GET_SWZ(*swizzleOut
, 2) == SWIZZLE_Z
);
1961 assert(GET_SWZ(*swizzleOut
, 3) == SWIZZLE_W
);
1969 static slang_ir_node
*
1970 _slang_gen_swizzle(slang_ir_node
*child
, GLuint swizzle
)
1972 slang_ir_node
*n
= new_node1(IR_SWIZZLE
, child
);
1975 n
->Store
= _slang_new_ir_storage(PROGRAM_UNDEFINED
, -1, -1);
1976 n
->Store
->Swizzle
= swizzle
;
1983 * Generate IR tree for an assignment (=).
1985 static slang_ir_node
*
1986 _slang_gen_assignment(slang_assemble_ctx
* A
, slang_operation
*oper
)
1988 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
&&
1989 oper
->children
[1].type
== SLANG_OPER_CALL
) {
1990 /* Special case of: x = f(a, b)
1991 * Replace with f(a, b, x) (where x == hidden __retVal out param)
1993 * XXX this could be even more effective if we could accomodate
1994 * cases such as "v.x = f();" - would help with typical vertex
1998 n
= _slang_gen_function_call_name(A
,
1999 (const char *) oper
->children
[1].a_id
,
2000 &oper
->children
[1], &oper
->children
[0]);
2004 slang_ir_node
*n
, *lhs
, *rhs
;
2005 lhs
= _slang_gen_operation(A
, &oper
->children
[0]);
2008 if (lhs
->Store
->File
!= PROGRAM_OUTPUT
&&
2009 lhs
->Store
->File
!= PROGRAM_TEMPORARY
&&
2010 lhs
->Store
->File
!= PROGRAM_VARYING
&&
2011 lhs
->Store
->File
!= PROGRAM_UNDEFINED
) {
2012 slang_info_log_error(A
->log
, "Assignment to read-only variable");
2017 rhs
= _slang_gen_operation(A
, &oper
->children
[1]);
2019 /* convert lhs swizzle into writemask */
2020 GLuint writemask
, newSwizzle
;
2021 if (!swizzle_to_writemask(lhs
->Store
->Swizzle
,
2022 &writemask
, &newSwizzle
)) {
2023 /* Non-simple writemask, need to swizzle right hand side in
2024 * order to put components into the right place.
2026 rhs
= _slang_gen_swizzle(rhs
, newSwizzle
);
2028 n
= new_node2(IR_MOVE
, lhs
, rhs
);
2029 n
->Writemask
= writemask
;
2040 * Generate IR tree for referencing a field in a struct (or basic vector type)
2042 static slang_ir_node
*
2043 _slang_gen_field(slang_assemble_ctx
* A
, slang_operation
*oper
)
2047 /* type of struct */
2048 slang_typeinfo_construct(&ti
);
2049 _slang_typeof_operation(A
, &oper
->children
[0], &ti
);
2051 if (_slang_type_is_vector(ti
.spec
.type
)) {
2052 /* the field should be a swizzle */
2053 const GLuint rows
= _slang_type_dim(ti
.spec
.type
);
2057 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2058 slang_info_log_error(A
->log
, "Bad swizzle");
2060 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2065 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2066 /* create new parent node with swizzle */
2068 n
= _slang_gen_swizzle(n
, swizzle
);
2071 else if (ti
.spec
.type
== SLANG_SPEC_FLOAT
) {
2072 const GLuint rows
= 1;
2076 if (!_slang_is_swizzle((char *) oper
->a_id
, rows
, &swz
)) {
2077 slang_info_log_error(A
->log
, "Bad swizzle");
2079 swizzle
= MAKE_SWIZZLE4(swz
.swizzle
[0],
2083 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2084 /* create new parent node with swizzle */
2085 n
= _slang_gen_swizzle(n
, swizzle
);
2089 /* the field is a structure member (base.field) */
2090 /* oper->children[0] is the base */
2091 /* oper->a_id is the field name */
2092 slang_ir_node
*base
, *n
;
2093 slang_typeinfo field_ti
;
2094 GLint fieldSize
, fieldOffset
;
2096 slang_typeinfo_construct(&field_ti
);
2097 _slang_typeof_operation(A
, oper
, &field_ti
);
2099 fieldSize
= _slang_sizeof_type_specifier(&field_ti
.spec
);
2100 fieldOffset
= _slang_field_offset(&ti
.spec
, oper
->a_id
);
2102 if (fieldOffset
< 0) {
2103 slang_info_log_error(A
->log
,
2104 "\"%s\" is not a member of struct \"%s\"",
2105 (char *) oper
->a_id
,
2106 (char *) ti
.spec
._struct
->a_name
);
2109 assert(fieldSize
>= 0);
2111 base
= _slang_gen_operation(A
, &oper
->children
[0]);
2113 /* error msg should have already been logged */
2117 n
= new_node1(IR_FIELD
, base
);
2119 n
->Field
= (char *) oper
->a_id
;
2120 n
->FieldOffset
= fieldOffset
;
2121 assert(n
->FieldOffset
>= 0);
2122 n
->Store
= _slang_new_ir_storage(base
->Store
->File
,
2129 _mesa_problem(NULL
, "glsl structs/fields not supported yet");
2137 * Gen code for array indexing.
2139 static slang_ir_node
*
2140 _slang_gen_subscript(slang_assemble_ctx
* A
, slang_operation
*oper
)
2142 slang_typeinfo array_ti
;
2144 /* get array's type info */
2145 slang_typeinfo_construct(&array_ti
);
2146 _slang_typeof_operation(A
, &oper
->children
[0], &array_ti
);
2148 if (_slang_type_is_vector(array_ti
.spec
.type
)) {
2149 /* indexing a simple vector type: "vec4 v; v[0]=p;" */
2150 /* translate the index into a swizzle/writemask: "v.x=p" */
2151 const GLuint max
= _slang_type_dim(array_ti
.spec
.type
);
2155 index
= (GLint
) oper
->children
[1].literal
[0];
2156 if (oper
->children
[1].type
!= SLANG_OPER_LITERAL_INT
||
2158 slang_info_log_error(A
->log
, "Invalid array index for vector type");
2162 n
= _slang_gen_operation(A
, &oper
->children
[0]);
2164 /* use swizzle to access the element */
2165 GLuint swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
+ index
,
2169 n
= _slang_gen_swizzle(n
, swizzle
);
2170 /*n->Store = _slang_clone_ir_storage_swz(n->Store, */
2171 n
->Writemask
= WRITEMASK_X
<< index
;
2176 /* conventional array */
2177 slang_typeinfo elem_ti
;
2178 slang_ir_node
*elem
, *array
, *index
;
2179 GLint elemSize
, arrayLen
;
2181 /* size of array element */
2182 slang_typeinfo_construct(&elem_ti
);
2183 _slang_typeof_operation(A
, oper
, &elem_ti
);
2184 elemSize
= _slang_sizeof_type_specifier(&elem_ti
.spec
);
2186 if (_slang_type_is_matrix(array_ti
.spec
.type
))
2187 arrayLen
= _slang_type_dim(array_ti
.spec
.type
);
2189 arrayLen
= array_ti
.array_len
;
2191 slang_typeinfo_destruct(&array_ti
);
2192 slang_typeinfo_destruct(&elem_ti
);
2194 if (elemSize
<= 0) {
2195 /* unknown var or type */
2196 slang_info_log_error(A
->log
, "Undefined variable or type");
2200 array
= _slang_gen_operation(A
, &oper
->children
[0]);
2201 index
= _slang_gen_operation(A
, &oper
->children
[1]);
2202 if (array
&& index
) {
2204 if (index
->Opcode
== IR_FLOAT
&&
2205 ((int) index
->Value
[0] < 0 ||
2206 (int) index
->Value
[0] >= arrayLen
)) {
2207 slang_info_log_error(A
->log
,
2208 "Array index out of bounds (index=%d size=%d)",
2209 (int) index
->Value
[0], arrayLen
);
2210 _slang_free_ir_tree(array
);
2211 _slang_free_ir_tree(index
);
2215 elem
= new_node2(IR_ELEMENT
, array
, index
);
2216 elem
->Store
= _slang_new_ir_storage(array
->Store
->File
,
2217 array
->Store
->Index
,
2219 /* XXX try to do some array bounds checking here */
2223 _slang_free_ir_tree(array
);
2224 _slang_free_ir_tree(index
);
2232 * Look for expressions such as: gl_ModelviewMatrix * gl_Vertex
2233 * and replace with this: gl_Vertex * gl_ModelviewMatrixTranpose
2234 * Since matrices are stored in column-major order, the second form of
2235 * multiplication is much more efficient (just 4 dot products).
2238 _slang_check_matmul_optimization(slang_assemble_ctx
*A
, slang_operation
*oper
)
2240 static const struct {
2242 const char *tranpose
;
2244 {"gl_ModelViewMatrix", "gl_ModelViewMatrixTranspose"},
2245 {"gl_ProjectionMatrix", "gl_ProjectionMatrixTranspose"},
2246 {"gl_ModelViewProjectionMatrix", "gl_ModelViewProjectionMatrixTranspose"},
2247 {"gl_TextureMatrix", "gl_TextureMatrixTranspose"},
2248 {"gl_NormalMatrix", "__NormalMatrixTranspose"},
2252 assert(oper
->type
== SLANG_OPER_MULTIPLY
);
2253 if (oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
) {
2255 for (i
= 0; matrices
[i
].orig
; i
++) {
2256 if (oper
->children
[0].a_id
2257 == slang_atom_pool_atom(A
->atoms
, matrices
[i
].orig
)) {
2259 _mesa_printf("Replace %s with %s\n",
2260 matrices[i].orig, matrices[i].tranpose);
2262 assert(oper
->children
[0].type
== SLANG_OPER_IDENTIFIER
);
2263 oper
->children
[0].a_id
2264 = slang_atom_pool_atom(A
->atoms
, matrices
[i
].tranpose
);
2265 /* finally, swap the operands */
2266 _slang_operation_swap(&oper
->children
[0], &oper
->children
[1]);
2275 * Generate IR tree for a slang_operation (AST node)
2277 static slang_ir_node
*
2278 _slang_gen_operation(slang_assemble_ctx
* A
, slang_operation
*oper
)
2280 switch (oper
->type
) {
2281 case SLANG_OPER_BLOCK_NEW_SCOPE
:
2285 _slang_push_var_table(A
->vartable
);
2287 oper
->type
= SLANG_OPER_BLOCK_NO_NEW_SCOPE
; /* temp change */
2288 n
= _slang_gen_operation(A
, oper
);
2289 oper
->type
= SLANG_OPER_BLOCK_NEW_SCOPE
; /* restore */
2291 _slang_pop_var_table(A
->vartable
);
2294 n
= new_node1(IR_SCOPE
, n
);
2299 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
2300 /* list of operations */
2301 if (oper
->num_children
> 0)
2303 slang_ir_node
*n
, *tree
= NULL
;
2306 for (i
= 0; i
< oper
->num_children
; i
++) {
2307 n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2309 _slang_free_ir_tree(tree
);
2310 return NULL
; /* error must have occured */
2312 tree
= tree
? new_seq(tree
, n
) : n
;
2316 if (oper
->locals
->num_variables
> 0) {
2319 printf("\n****** Deallocate vars in scope!\n");
2321 for (i
= 0; i
< oper
->locals
->num_variables
; i
++) {
2322 slang_variable
*v
= oper
->locals
->variables
+ i
;
2324 slang_ir_storage
*store
= (slang_ir_storage
*) v
->aux
;
2326 printf(" Deallocate var %s\n", (char*) v->a_name);
2328 assert(store
->File
== PROGRAM_TEMPORARY
);
2329 assert(store
->Index
>= 0);
2330 _slang_free_temp(A
->vartable
, store
->Index
, store
->Size
);
2338 return new_node0(IR_NOP
);
2341 case SLANG_OPER_EXPRESSION
:
2342 return _slang_gen_operation(A
, &oper
->children
[0]);
2344 case SLANG_OPER_FOR
:
2345 return _slang_gen_for(A
, oper
);
2347 return _slang_gen_do(A
, oper
);
2348 case SLANG_OPER_WHILE
:
2349 return _slang_gen_while(A
, oper
);
2350 case SLANG_OPER_BREAK
:
2352 slang_info_log_error(A
->log
, "'break' not in loop");
2354 return new_break(A
->CurLoop
);
2355 case SLANG_OPER_CONTINUE
:
2357 slang_info_log_error(A
->log
, "'continue' not in loop");
2359 return _slang_gen_continue(A
, oper
);
2360 case SLANG_OPER_DISCARD
:
2361 return new_node0(IR_KILL
);
2363 case SLANG_OPER_EQUAL
:
2364 return new_node2(IR_EQUAL
,
2365 _slang_gen_operation(A
, &oper
->children
[0]),
2366 _slang_gen_operation(A
, &oper
->children
[1]));
2367 case SLANG_OPER_NOTEQUAL
:
2368 return new_node2(IR_NOTEQUAL
,
2369 _slang_gen_operation(A
, &oper
->children
[0]),
2370 _slang_gen_operation(A
, &oper
->children
[1]));
2371 case SLANG_OPER_GREATER
:
2372 return new_node2(IR_SGT
,
2373 _slang_gen_operation(A
, &oper
->children
[0]),
2374 _slang_gen_operation(A
, &oper
->children
[1]));
2375 case SLANG_OPER_LESS
:
2376 return new_node2(IR_SLT
,
2377 _slang_gen_operation(A
, &oper
->children
[0]),
2378 _slang_gen_operation(A
, &oper
->children
[1]));
2379 case SLANG_OPER_GREATEREQUAL
:
2380 return new_node2(IR_SGE
,
2381 _slang_gen_operation(A
, &oper
->children
[0]),
2382 _slang_gen_operation(A
, &oper
->children
[1]));
2383 case SLANG_OPER_LESSEQUAL
:
2384 return new_node2(IR_SLE
,
2385 _slang_gen_operation(A
, &oper
->children
[0]),
2386 _slang_gen_operation(A
, &oper
->children
[1]));
2387 case SLANG_OPER_ADD
:
2390 assert(oper
->num_children
== 2);
2391 n
= _slang_gen_function_call_name(A
, "+", oper
, NULL
);
2394 case SLANG_OPER_SUBTRACT
:
2397 assert(oper
->num_children
== 2);
2398 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2401 case SLANG_OPER_MULTIPLY
:
2404 assert(oper
->num_children
== 2);
2405 _slang_check_matmul_optimization(A
, oper
);
2406 n
= _slang_gen_function_call_name(A
, "*", oper
, NULL
);
2409 case SLANG_OPER_DIVIDE
:
2412 assert(oper
->num_children
== 2);
2413 n
= _slang_gen_function_call_name(A
, "/", oper
, NULL
);
2416 case SLANG_OPER_MINUS
:
2419 assert(oper
->num_children
== 1);
2420 n
= _slang_gen_function_call_name(A
, "-", oper
, NULL
);
2423 case SLANG_OPER_PLUS
:
2424 /* +expr --> do nothing */
2425 return _slang_gen_operation(A
, &oper
->children
[0]);
2426 case SLANG_OPER_VARIABLE_DECL
:
2427 return _slang_gen_declaration(A
, oper
);
2428 case SLANG_OPER_ASSIGN
:
2429 return _slang_gen_assignment(A
, oper
);
2430 case SLANG_OPER_ADDASSIGN
:
2433 assert(oper
->num_children
== 2);
2434 n
= _slang_gen_function_call_name(A
, "+=", oper
, &oper
->children
[0]);
2437 case SLANG_OPER_SUBASSIGN
:
2440 assert(oper
->num_children
== 2);
2441 n
= _slang_gen_function_call_name(A
, "-=", oper
, &oper
->children
[0]);
2445 case SLANG_OPER_MULASSIGN
:
2448 assert(oper
->num_children
== 2);
2449 n
= _slang_gen_function_call_name(A
, "*=", oper
, &oper
->children
[0]);
2452 case SLANG_OPER_DIVASSIGN
:
2455 assert(oper
->num_children
== 2);
2456 n
= _slang_gen_function_call_name(A
, "/=", oper
, &oper
->children
[0]);
2459 case SLANG_OPER_LOGICALAND
:
2462 assert(oper
->num_children
== 2);
2463 n
= _slang_gen_logical_and(A
, oper
);
2466 case SLANG_OPER_LOGICALOR
:
2469 assert(oper
->num_children
== 2);
2470 n
= _slang_gen_logical_or(A
, oper
);
2473 case SLANG_OPER_LOGICALXOR
:
2476 assert(oper
->num_children
== 2);
2477 n
= _slang_gen_function_call_name(A
, "__logicalXor", oper
, NULL
);
2480 case SLANG_OPER_NOT
:
2483 assert(oper
->num_children
== 1);
2484 n
= _slang_gen_function_call_name(A
, "__logicalNot", oper
, NULL
);
2488 case SLANG_OPER_SELECT
: /* b ? x : y */
2491 assert(oper
->num_children
== 3);
2492 n
= _slang_gen_select(A
, oper
);
2496 case SLANG_OPER_ASM
:
2497 return _slang_gen_asm(A
, oper
, NULL
);
2498 case SLANG_OPER_CALL
:
2499 return _slang_gen_function_call_name(A
, (const char *) oper
->a_id
,
2501 case SLANG_OPER_RETURN
:
2502 return _slang_gen_return(A
, oper
);
2503 case SLANG_OPER_GOTO
:
2504 return new_jump(oper
->label
);
2505 case SLANG_OPER_LABEL
:
2506 return new_label(oper
->label
);
2507 case SLANG_OPER_IDENTIFIER
:
2508 return _slang_gen_variable(A
, oper
);
2510 return _slang_gen_if(A
, oper
);
2511 case SLANG_OPER_FIELD
:
2512 return _slang_gen_field(A
, oper
);
2513 case SLANG_OPER_SUBSCRIPT
:
2514 return _slang_gen_subscript(A
, oper
);
2515 case SLANG_OPER_LITERAL_FLOAT
:
2517 case SLANG_OPER_LITERAL_INT
:
2519 case SLANG_OPER_LITERAL_BOOL
:
2520 return new_float_literal(oper
->literal
);
2522 case SLANG_OPER_POSTINCREMENT
: /* var++ */
2525 assert(oper
->num_children
== 1);
2526 n
= _slang_gen_function_call_name(A
, "__postIncr", oper
, NULL
);
2529 case SLANG_OPER_POSTDECREMENT
: /* var-- */
2532 assert(oper
->num_children
== 1);
2533 n
= _slang_gen_function_call_name(A
, "__postDecr", oper
, NULL
);
2536 case SLANG_OPER_PREINCREMENT
: /* ++var */
2539 assert(oper
->num_children
== 1);
2540 n
= _slang_gen_function_call_name(A
, "++", oper
, NULL
);
2543 case SLANG_OPER_PREDECREMENT
: /* --var */
2546 assert(oper
->num_children
== 1);
2547 n
= _slang_gen_function_call_name(A
, "--", oper
, NULL
);
2551 case SLANG_OPER_SEQUENCE
:
2553 slang_ir_node
*tree
= NULL
;
2555 for (i
= 0; i
< oper
->num_children
; i
++) {
2556 slang_ir_node
*n
= _slang_gen_operation(A
, &oper
->children
[i
]);
2557 tree
= tree
? new_seq(tree
, n
) : n
;
2562 case SLANG_OPER_NONE
:
2563 case SLANG_OPER_VOID
:
2564 /* returning NULL here would generate an error */
2565 return new_node0(IR_NOP
);
2568 printf("Unhandled node type %d\n", oper
->type
);
2570 return new_node0(IR_NOP
);
2579 * Called by compiler when a global variable has been parsed/compiled.
2580 * Here we examine the variable's type to determine what kind of register
2581 * storage will be used.
2583 * A uniform such as "gl_Position" will become the register specification
2584 * (PROGRAM_OUTPUT, VERT_RESULT_HPOS). Or, uniform "gl_FogFragCoord"
2585 * will be (PROGRAM_INPUT, FRAG_ATTRIB_FOGC).
2587 * Samplers are interesting. For "uniform sampler2D tex;" we'll specify
2588 * (PROGRAM_SAMPLER, index) where index is resolved at link-time to an
2589 * actual texture unit (as specified by the user calling glUniform1i()).
2592 _slang_codegen_global_variable(slang_assemble_ctx
*A
, slang_variable
*var
,
2593 slang_unit_type type
)
2595 struct gl_program
*prog
= A
->program
;
2596 const char *varName
= (char *) var
->a_name
;
2597 GLboolean success
= GL_TRUE
;
2599 slang_ir_storage
*store
= NULL
;
2602 texIndex
= sampler_to_texture_index(var
->type
.specifier
.type
);
2604 if (texIndex
!= -1) {
2606 * store->File = PROGRAM_SAMPLER
2607 * store->Index = sampler uniform location
2608 * store->Size = texture type index (1D, 2D, 3D, cube, etc)
2610 GLint samplerUniform
= _mesa_add_sampler(prog
->Parameters
, varName
);
2611 store
= _slang_new_ir_storage(PROGRAM_SAMPLER
, samplerUniform
, texIndex
);
2612 if (dbg
) printf("SAMPLER ");
2614 else if (var
->type
.qualifier
== SLANG_QUAL_UNIFORM
) {
2615 /* Uniform variable */
2616 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
)
2617 * MAX2(var
->array_len
, 1);
2619 /* user-defined uniform */
2620 GLint uniformLoc
= _mesa_add_uniform(prog
->Parameters
, varName
, size
);
2621 store
= _slang_new_ir_storage(PROGRAM_UNIFORM
, uniformLoc
, size
);
2624 /* pre-defined uniform, like gl_ModelviewMatrix */
2625 /* We know it's a uniform, but don't allocate storage unless
2628 store
= _slang_new_ir_storage(PROGRAM_STATE_VAR
, -1, size
);
2630 if (dbg
) printf("UNIFORM (sz %d) ", size
);
2632 else if (var
->type
.qualifier
== SLANG_QUAL_VARYING
) {
2633 const GLint size
= 4; /* XXX fix */
2635 /* user-defined varying */
2636 GLint varyingLoc
= _mesa_add_varying(prog
->Varying
, varName
, size
);
2637 store
= _slang_new_ir_storage(PROGRAM_VARYING
, varyingLoc
, size
);
2640 /* pre-defined varying, like gl_Color or gl_TexCoord */
2641 if (type
== SLANG_UNIT_FRAGMENT_BUILTIN
) {
2643 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
2646 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2647 store
->Swizzle
= swizzle
;
2648 assert(index
< FRAG_ATTRIB_MAX
);
2651 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2653 assert(type
== SLANG_UNIT_VERTEX_BUILTIN
);
2654 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2655 assert(index
< VERT_RESULT_MAX
);
2657 if (dbg
) printf("V/F ");
2659 if (dbg
) printf("VARYING ");
2661 else if (var
->type
.qualifier
== SLANG_QUAL_ATTRIBUTE
) {
2663 /* user-defined vertex attribute */
2664 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2665 const GLint attr
= -1; /* unknown */
2666 GLint index
= _mesa_add_attribute(prog
->Attributes
, varName
,
2669 store
= _slang_new_ir_storage(PROGRAM_INPUT
,
2670 VERT_ATTRIB_GENERIC0
+ index
, size
);
2673 /* pre-defined vertex attrib */
2675 GLint index
= _slang_input_index(varName
, GL_VERTEX_PROGRAM_ARB
,
2677 GLint size
= 4; /* XXX? */
2679 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2680 store
->Swizzle
= swizzle
;
2682 if (dbg
) printf("ATTRIB ");
2684 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDINPUT
) {
2686 GLint index
= _slang_input_index(varName
, GL_FRAGMENT_PROGRAM_ARB
,
2688 GLint size
= 4; /* XXX? */
2689 store
= _slang_new_ir_storage(PROGRAM_INPUT
, index
, size
);
2690 store
->Swizzle
= swizzle
;
2691 if (dbg
) printf("INPUT ");
2693 else if (var
->type
.qualifier
== SLANG_QUAL_FIXEDOUTPUT
) {
2694 if (type
== SLANG_UNIT_VERTEX_BUILTIN
) {
2695 GLint index
= _slang_output_index(varName
, GL_VERTEX_PROGRAM_ARB
);
2696 GLint size
= 4; /* XXX? */
2697 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2700 assert(type
== SLANG_UNIT_FRAGMENT_BUILTIN
);
2701 GLint index
= _slang_output_index(varName
, GL_FRAGMENT_PROGRAM_ARB
);
2702 GLint size
= 4; /* XXX? */
2703 store
= _slang_new_ir_storage(PROGRAM_OUTPUT
, index
, size
);
2705 if (dbg
) printf("OUTPUT ");
2707 else if (var
->type
.qualifier
== SLANG_QUAL_CONST
&& !prog
) {
2708 /* pre-defined global constant, like gl_MaxLights */
2709 const GLint size
= _slang_sizeof_type_specifier(&var
->type
.specifier
);
2710 store
= _slang_new_ir_storage(PROGRAM_CONSTANT
, -1, size
);
2711 if (dbg
) printf("CONST ");
2714 /* ordinary variable (may be const) */
2717 /* IR node to declare the variable */
2718 n
= _slang_gen_var_decl(A
, var
);
2720 /* IR code for the var's initializer, if present */
2721 if (var
->initializer
) {
2722 slang_ir_node
*lhs
, *rhs
, *init
;
2724 /* Generate IR_MOVE instruction to initialize the variable */
2725 lhs
= new_node0(IR_VAR
);
2727 lhs
->Store
= n
->Store
;
2729 /* constant folding, etc */
2730 _slang_simplify(var
->initializer
, &A
->space
, A
->atoms
);
2732 rhs
= _slang_gen_operation(A
, var
->initializer
);
2734 init
= new_node2(IR_MOVE
, lhs
, rhs
);
2735 n
= new_seq(n
, init
);
2738 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_FALSE
, A
->log
);
2740 _slang_free_ir_tree(n
);
2743 if (dbg
) printf("GLOBAL VAR %s idx %d\n", (char*) var
->a_name
,
2744 store
? store
->Index
: -2);
2747 var
->aux
= store
; /* save var's storage info */
2754 * Produce an IR tree from a function AST (fun->body).
2755 * Then call the code emitter to convert the IR tree into gl_program
2759 _slang_codegen_function(slang_assemble_ctx
* A
, slang_function
* fun
)
2762 GLboolean success
= GL_TRUE
;
2764 if (_mesa_strcmp((char *) fun
->header
.a_name
, "main") != 0) {
2765 /* we only really generate code for main, all other functions get
2768 return GL_TRUE
; /* not an error */
2772 printf("\n*********** codegen_function %s\n", (char *) fun
->header
.a_name
);
2773 slang_print_function(fun
, 1);
2776 /* should have been allocated earlier: */
2777 assert(A
->program
->Parameters
);
2778 assert(A
->program
->Varying
);
2779 assert(A
->vartable
);
2781 /* fold constant expressions, etc. */
2782 _slang_simplify(fun
->body
, &A
->space
, A
->atoms
);
2785 printf("\n*********** simplified %s\n", (char *) fun
->header
.a_name
);
2786 slang_print_function(fun
, 1);
2789 /* Create an end-of-function label */
2790 A
->curFuncEndLabel
= _slang_label_new("__endOfFunc__main");
2792 /* push new vartable scope */
2793 _slang_push_var_table(A
->vartable
);
2795 /* Generate IR tree for the function body code */
2796 n
= _slang_gen_operation(A
, fun
->body
);
2798 n
= new_node1(IR_SCOPE
, n
);
2800 /* pop vartable, restore previous */
2801 _slang_pop_var_table(A
->vartable
);
2804 /* XXX record error */
2808 /* append an end-of-function-label to IR tree */
2809 n
= new_seq(n
, new_label(A
->curFuncEndLabel
));
2811 /*_slang_label_delete(A->curFuncEndLabel);*/
2812 A
->curFuncEndLabel
= NULL
;
2815 printf("************* New AST for %s *****\n", (char*)fun
->header
.a_name
);
2816 slang_print_function(fun
, 1);
2819 printf("************* IR for %s *******\n", (char*)fun
->header
.a_name
);
2820 _slang_print_ir_tree(n
, 0);
2823 printf("************* End codegen function ************\n\n");
2826 /* Emit program instructions */
2827 success
= _slang_emit_code(n
, A
->vartable
, A
->program
, GL_TRUE
, A
->log
);
2828 _slang_free_ir_tree(n
);
2830 /* free codegen context */
2832 _mesa_free(A->codegen);