61ff649e5cce7bdb49ed84070bbfd24601cb7565
[mesa.git] / src / mesa / shader / slang / slang_ir.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25 /**
26 * \file slang_ir.h
27 * Mesa GLSL Intermediate Representation tree types and constants.
28 * \author Brian Paul
29 */
30
31
32 #ifndef SLANG_IR_H
33 #define SLANG_IR_H
34
35
36 #include "imports.h"
37 #include "slang_compile.h"
38 #include "slang_label.h"
39 #include "mtypes.h"
40
41
42 /**
43 * Intermediate Representation opcodes
44 */
45 typedef enum
46 {
47 IR_NOP = 0,
48 IR_SEQ, /* sequence (eval left, then right) */
49 IR_SCOPE, /* new variable scope (one child) */
50
51 IR_LABEL, /* target of a jump or cjump */
52
53 IR_COND, /* conditional expression/predicate */
54
55 IR_IF, /* high-level IF/then/else */
56 /* Children[0] = conditional expression */
57 /* Children[1] = if-true part */
58 /* Children[2] = if-else part, or NULL */
59
60 IR_BEGIN_SUB, /* begin subroutine */
61 IR_END_SUB, /* end subroutine */
62 IR_RETURN, /* return from subroutine */
63 IR_CALL, /* call subroutine */
64
65 IR_LOOP, /* high-level loop-begin / loop-end */
66 /* Children[0] = loop body */
67 /* Children[1] = loop tail code, or NULL */
68
69 IR_CONT, /* continue loop */
70 /* n->Parent = ptr to parent IR_LOOP Node */
71 IR_BREAK, /* break loop */
72
73 IR_BREAK_IF_TRUE,
74 IR_CONT_IF_TRUE,
75 /* Children[0] = the condition expression */
76
77 IR_MOVE,
78
79 /* vector ops: */
80 IR_ADD,
81 IR_SUB,
82 IR_MUL,
83 IR_DIV,
84 IR_DOT4,
85 IR_DOT3,
86 IR_CROSS, /* vec3 cross product */
87 IR_LRP,
88 IR_CLAMP,
89 IR_MIN,
90 IR_MAX,
91 IR_SEQUAL, /* Set if args are equal (vector) */
92 IR_SNEQUAL, /* Set if args are not equal (vector) */
93 IR_SGE, /* Set if greater or equal (vector) */
94 IR_SGT, /* Set if greater than (vector) */
95 IR_SLE, /* Set if less or equal (vector) */
96 IR_SLT, /* Set if less than (vector) */
97 IR_POW, /* x^y */
98 IR_EXP, /* e^x */
99 IR_EXP2, /* 2^x */
100 IR_LOG2, /* log base 2 */
101 IR_RSQ, /* 1/sqrt() */
102 IR_RCP, /* reciprocol */
103 IR_FLOOR,
104 IR_FRAC,
105 IR_ABS, /* absolute value */
106 IR_NEG, /* negate */
107 IR_DDX, /* derivative w.r.t. X */
108 IR_DDY, /* derivative w.r.t. Y */
109 IR_SIN, /* sine */
110 IR_COS, /* cosine */
111 IR_NOISE1, /* noise(x) */
112 IR_NOISE2, /* noise(x, y) */
113 IR_NOISE3, /* noise(x, y, z) */
114 IR_NOISE4, /* noise(x, y, z, w) */
115
116 IR_EQUAL, /* boolean equality */
117 IR_NOTEQUAL,/* boolean inequality */
118 IR_NOT, /* boolean not */
119
120 IR_VAR, /* variable reference */
121 IR_VAR_DECL,/* var declaration */
122
123 IR_ELEMENT, /* array element */
124 IR_FIELD, /* struct field */
125 IR_SWIZZLE, /* swizzled storage access */
126
127 IR_TEX, /* texture lookup */
128 IR_TEXB, /* texture lookup with LOD bias */
129 IR_TEXP, /* texture lookup with projection */
130
131 IR_FLOAT,
132 IR_I_TO_F, /* int[4] to float[4] conversion */
133 IR_F_TO_I, /* float[4] to int[4] conversion */
134
135 IR_KILL /* fragment kill/discard */
136 } slang_ir_opcode;
137
138
139 /**
140 * Describes where data storage is allocated.
141 */
142 struct _slang_ir_storage
143 {
144 enum register_file File; /**< PROGRAM_TEMPORARY, PROGRAM_INPUT, etc */
145 GLint Index; /**< -1 means unallocated */
146 GLint Size; /**< number of floats */
147 GLuint Swizzle;
148 GLint RefCount; /**< Used during IR tree delete */
149 GLboolean RelAddr;
150
151 /** If Parent is non-null, Index is relative to parent.
152 * The other fields are ignored.
153 */
154 struct _slang_ir_storage *Parent;
155 };
156
157 typedef struct _slang_ir_storage slang_ir_storage;
158
159
160 /**
161 * Intermediate Representation (IR) tree node
162 * Basically a binary tree, but IR_LRP and IR_CLAMP have three children.
163 */
164 typedef struct slang_ir_node_
165 {
166 slang_ir_opcode Opcode;
167 struct slang_ir_node_ *Children[3];
168 slang_ir_storage *Store; /**< location of result of this operation */
169 GLint InstLocation; /**< Location of instruction emitted for this node */
170
171 /** special fields depending on Opcode: */
172 const char *Field; /**< If Opcode == IR_FIELD */
173 GLuint Writemask; /**< If Opcode == IR_MOVE */
174 GLfloat Value[4]; /**< If Opcode == IR_FLOAT */
175 slang_variable *Var; /**< If Opcode == IR_VAR or IR_VAR_DECL */
176 struct slang_ir_node_ *List; /**< For various linked lists */
177 struct slang_ir_node_ *Parent; /**< Pointer to logical parent (ie. loop) */
178 slang_label *Label; /**< Used for branches */
179 } slang_ir_node;
180
181
182
183 /**
184 * Assembly and IR info
185 */
186 typedef struct
187 {
188 slang_ir_opcode IrOpcode;
189 const char *IrName;
190 gl_inst_opcode InstOpcode;
191 GLuint ResultSize, NumParams;
192 } slang_ir_info;
193
194
195
196 extern const slang_ir_info *
197 _slang_ir_info(slang_ir_opcode opcode);
198
199
200 extern slang_ir_storage *
201 _slang_new_ir_storage(enum register_file file, GLint index, GLint size);
202
203
204 extern slang_ir_storage *
205 _slang_new_ir_storage_swz(enum register_file file, GLint index, GLint size,
206 GLuint swizzle);
207
208 extern slang_ir_storage *
209 _slang_new_ir_storage_relative(GLint index, GLint size,
210 slang_ir_storage *parent);
211
212
213
214 extern void
215 _slang_free_ir_tree(slang_ir_node *n);
216
217
218 extern void
219 _slang_print_ir_tree(const slang_ir_node *n, int indent);
220
221
222 #endif /* SLANG_IR_H */