1/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "ir.h"
25
26namespace ir_builder {
27
28#ifndef WRITEMASK_X
29enum writemask {
30   WRITEMASK_X = 0x1,
31   WRITEMASK_Y = 0x2,
32   WRITEMASK_Z = 0x4,
33   WRITEMASK_W = 0x8,
34};
35#endif
36
37/**
38 * This little class exists to let the helper expression generators
39 * take either an ir_rvalue * or an ir_variable * to be automatically
40 * dereferenced, while still providing compile-time type checking.
41 *
42 * You don't have to explicitly call the constructor -- C++ will see
43 * that you passed an ir_variable, and silently call the
44 * operand(ir_variable *var) constructor behind your back.
45 */
46class operand {
47public:
48   operand(ir_rvalue *val)
49      : val(val)
50   {
51   }
52
53   operand(ir_variable *var)
54   {
55      void *mem_ctx = ralloc_parent(var);
56      val = new(mem_ctx) ir_dereference_variable(var);
57   }
58
59   ir_rvalue *val;
60};
61
62/** Automatic generator for ir_dereference_variable on assignment LHS.
63 *
64 * \sa operand
65 */
66class deref {
67public:
68   deref(ir_dereference *val)
69      : val(val)
70   {
71   }
72
73   deref(ir_variable *var)
74   {
75      void *mem_ctx = ralloc_parent(var);
76      val = new(mem_ctx) ir_dereference_variable(var);
77   }
78
79
80   ir_dereference *val;
81};
82
83class ir_factory {
84public:
85   ir_factory(exec_list *instructions = NULL, void *mem_ctx = NULL)
86      : instructions(instructions),
87        mem_ctx(mem_ctx)
88   {
89      return;
90   }
91
92   void emit(ir_instruction *ir);
93   ir_variable *make_temp(const glsl_type *type, const char *name);
94
95   ir_constant*
96   constant(float f)
97   {
98      return new(mem_ctx) ir_constant(f);
99   }
100
101   ir_constant*
102   constant(int i)
103   {
104      return new(mem_ctx) ir_constant(i);
105   }
106
107   ir_constant*
108   constant(unsigned u)
109   {
110      return new(mem_ctx) ir_constant(u);
111   }
112
113   ir_constant*
114   constant(bool b)
115   {
116      return new(mem_ctx) ir_constant(b);
117   }
118
119   exec_list *instructions;
120   void *mem_ctx;
121};
122
123ir_assignment *assign(deref lhs, operand rhs);
124ir_assignment *assign(deref lhs, operand rhs, int writemask);
125ir_assignment *assign(deref lhs, operand rhs, operand condition);
126ir_assignment *assign(deref lhs, operand rhs, operand condition, int writemask);
127
128ir_return *ret(operand retval);
129
130ir_expression *expr(ir_expression_operation op, operand a);
131ir_expression *expr(ir_expression_operation op, operand a, operand b);
132ir_expression *expr(ir_expression_operation op, operand a, operand b, operand c);
133ir_expression *add(operand a, operand b);
134ir_expression *sub(operand a, operand b);
135ir_expression *mul(operand a, operand b);
136ir_expression *imul_high(operand a, operand b);
137ir_expression *div(operand a, operand b);
138ir_expression *carry(operand a, operand b);
139ir_expression *borrow(operand a, operand b);
140ir_expression *trunc(operand a);
141ir_expression *round_even(operand a);
142ir_expression *fract(operand a);
143ir_expression *dot(operand a, operand b);
144ir_expression *clamp(operand a, operand b, operand c);
145ir_expression *saturate(operand a);
146ir_expression *abs(operand a);
147ir_expression *neg(operand a);
148ir_expression *sin(operand a);
149ir_expression *cos(operand a);
150ir_expression *exp(operand a);
151ir_expression *rsq(operand a);
152ir_expression *sqrt(operand a);
153ir_expression *log(operand a);
154ir_expression *sign(operand a);
155
156ir_expression *subr_to_int(operand a);
157ir_expression *equal(operand a, operand b);
158ir_expression *nequal(operand a, operand b);
159ir_expression *less(operand a, operand b);
160ir_expression *greater(operand a, operand b);
161ir_expression *lequal(operand a, operand b);
162ir_expression *gequal(operand a, operand b);
163
164ir_expression *logic_not(operand a);
165ir_expression *logic_and(operand a, operand b);
166ir_expression *logic_or(operand a, operand b);
167
168ir_expression *bit_not(operand a);
169ir_expression *bit_or(operand a, operand b);
170ir_expression *bit_and(operand a, operand b);
171ir_expression *bit_xor(operand a, operand b);
172ir_expression *lshift(operand a, operand b);
173ir_expression *rshift(operand a, operand b);
174
175ir_expression *f2i(operand a);
176ir_expression *bitcast_f2i(operand a);
177ir_expression *i2f(operand a);
178ir_expression *bitcast_i2f(operand a);
179ir_expression *f2u(operand a);
180ir_expression *bitcast_f2u(operand a);
181ir_expression *u2f(operand a);
182ir_expression *bitcast_u2f(operand a);
183ir_expression *i2u(operand a);
184ir_expression *u2i(operand a);
185ir_expression *b2i(operand a);
186ir_expression *i2b(operand a);
187ir_expression *f2b(operand a);
188ir_expression *b2f(operand a);
189
190ir_expression *f2d(operand a);
191ir_expression *i2d(operand a);
192ir_expression *u2d(operand a);
193
194ir_expression *min2(operand a, operand b);
195ir_expression *max2(operand a, operand b);
196
197ir_expression *interpolate_at_centroid(operand a);
198ir_expression *interpolate_at_offset(operand a, operand b);
199ir_expression *interpolate_at_sample(operand a, operand b);
200
201ir_expression *fma(operand a, operand b, operand c);
202ir_expression *lrp(operand x, operand y, operand a);
203ir_expression *csel(operand a, operand b, operand c);
204ir_expression *bitfield_extract(operand a, operand b, operand c);
205ir_expression *bitfield_insert(operand a, operand b, operand c, operand d);
206
207ir_swizzle *swizzle(operand a, int swizzle, int components);
208/**
209 * Swizzle away later components, but preserve the ordering.
210 */
211ir_swizzle *swizzle_for_size(operand a, unsigned components);
212
213ir_swizzle *swizzle_xxxx(operand a);
214ir_swizzle *swizzle_yyyy(operand a);
215ir_swizzle *swizzle_zzzz(operand a);
216ir_swizzle *swizzle_wwww(operand a);
217ir_swizzle *swizzle_x(operand a);
218ir_swizzle *swizzle_y(operand a);
219ir_swizzle *swizzle_z(operand a);
220ir_swizzle *swizzle_w(operand a);
221ir_swizzle *swizzle_xy(operand a);
222ir_swizzle *swizzle_xyz(operand a);
223ir_swizzle *swizzle_xyzw(operand a);
224
225ir_if *if_tree(operand condition,
226               ir_instruction *then_branch);
227ir_if *if_tree(operand condition,
228               ir_instruction *then_branch,
229               ir_instruction *else_branch);
230
231} /* namespace ir_builder */
232