101e04c3fSmrg/* 201e04c3fSmrg * Copyright © 2012 Intel Corporation 301e04c3fSmrg * 401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 501e04c3fSmrg * copy of this software and associated documentation files (the "Software"), 601e04c3fSmrg * to deal in the Software without restriction, including without limitation 701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the 901e04c3fSmrg * Software is furnished to do so, subject to the following conditions: 1001e04c3fSmrg * 1101e04c3fSmrg * The above copyright notice and this permission notice (including the next 1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the 1301e04c3fSmrg * Software. 1401e04c3fSmrg * 1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 2101e04c3fSmrg * IN THE SOFTWARE. 2201e04c3fSmrg */ 2301e04c3fSmrg 2401e04c3fSmrg#ifndef IR_BUILDER_H 2501e04c3fSmrg#define IR_BUILDER_H 2601e04c3fSmrg 2701e04c3fSmrg#include "ir.h" 2801e04c3fSmrg 2901e04c3fSmrgnamespace ir_builder { 3001e04c3fSmrg 3101e04c3fSmrg#ifndef WRITEMASK_X 3201e04c3fSmrgenum writemask { 3301e04c3fSmrg WRITEMASK_X = 0x1, 3401e04c3fSmrg WRITEMASK_Y = 0x2, 3501e04c3fSmrg WRITEMASK_Z = 0x4, 3601e04c3fSmrg WRITEMASK_W = 0x8, 3701e04c3fSmrg}; 3801e04c3fSmrg#endif 3901e04c3fSmrg 4001e04c3fSmrg/** 4101e04c3fSmrg * This little class exists to let the helper expression generators 4201e04c3fSmrg * take either an ir_rvalue * or an ir_variable * to be automatically 4301e04c3fSmrg * dereferenced, while still providing compile-time type checking. 4401e04c3fSmrg * 4501e04c3fSmrg * You don't have to explicitly call the constructor -- C++ will see 4601e04c3fSmrg * that you passed an ir_variable, and silently call the 4701e04c3fSmrg * operand(ir_variable *var) constructor behind your back. 4801e04c3fSmrg */ 4901e04c3fSmrgclass operand { 5001e04c3fSmrgpublic: 5101e04c3fSmrg operand(ir_rvalue *val) 5201e04c3fSmrg : val(val) 5301e04c3fSmrg { 5401e04c3fSmrg } 5501e04c3fSmrg 5601e04c3fSmrg operand(ir_variable *var) 5701e04c3fSmrg { 5801e04c3fSmrg void *mem_ctx = ralloc_parent(var); 5901e04c3fSmrg val = new(mem_ctx) ir_dereference_variable(var); 6001e04c3fSmrg } 6101e04c3fSmrg 6201e04c3fSmrg ir_rvalue *val; 6301e04c3fSmrg}; 6401e04c3fSmrg 6501e04c3fSmrg/** Automatic generator for ir_dereference_variable on assignment LHS. 6601e04c3fSmrg * 6701e04c3fSmrg * \sa operand 6801e04c3fSmrg */ 6901e04c3fSmrgclass deref { 7001e04c3fSmrgpublic: 7101e04c3fSmrg deref(ir_dereference *val) 7201e04c3fSmrg : val(val) 7301e04c3fSmrg { 7401e04c3fSmrg } 7501e04c3fSmrg 7601e04c3fSmrg deref(ir_variable *var) 7701e04c3fSmrg { 7801e04c3fSmrg void *mem_ctx = ralloc_parent(var); 7901e04c3fSmrg val = new(mem_ctx) ir_dereference_variable(var); 8001e04c3fSmrg } 8101e04c3fSmrg 8201e04c3fSmrg 8301e04c3fSmrg ir_dereference *val; 8401e04c3fSmrg}; 8501e04c3fSmrg 8601e04c3fSmrgclass ir_factory { 8701e04c3fSmrgpublic: 8801e04c3fSmrg ir_factory(exec_list *instructions = NULL, void *mem_ctx = NULL) 8901e04c3fSmrg : instructions(instructions), 9001e04c3fSmrg mem_ctx(mem_ctx) 9101e04c3fSmrg { 9201e04c3fSmrg return; 9301e04c3fSmrg } 9401e04c3fSmrg 9501e04c3fSmrg void emit(ir_instruction *ir); 9601e04c3fSmrg ir_variable *make_temp(const glsl_type *type, const char *name); 9701e04c3fSmrg 9801e04c3fSmrg ir_constant* 9901e04c3fSmrg constant(float f) 10001e04c3fSmrg { 10101e04c3fSmrg return new(mem_ctx) ir_constant(f); 10201e04c3fSmrg } 10301e04c3fSmrg 10401e04c3fSmrg ir_constant* 10501e04c3fSmrg constant(int i) 10601e04c3fSmrg { 10701e04c3fSmrg return new(mem_ctx) ir_constant(i); 10801e04c3fSmrg } 10901e04c3fSmrg 11001e04c3fSmrg ir_constant* 11101e04c3fSmrg constant(unsigned u) 11201e04c3fSmrg { 11301e04c3fSmrg return new(mem_ctx) ir_constant(u); 11401e04c3fSmrg } 11501e04c3fSmrg 11601e04c3fSmrg ir_constant* 11701e04c3fSmrg constant(bool b) 11801e04c3fSmrg { 11901e04c3fSmrg return new(mem_ctx) ir_constant(b); 12001e04c3fSmrg } 12101e04c3fSmrg 12201e04c3fSmrg exec_list *instructions; 12301e04c3fSmrg void *mem_ctx; 12401e04c3fSmrg}; 12501e04c3fSmrg 12601e04c3fSmrgir_assignment *assign(deref lhs, operand rhs); 12701e04c3fSmrgir_assignment *assign(deref lhs, operand rhs, int writemask); 12801e04c3fSmrgir_assignment *assign(deref lhs, operand rhs, operand condition); 12901e04c3fSmrgir_assignment *assign(deref lhs, operand rhs, operand condition, int writemask); 13001e04c3fSmrg 13101e04c3fSmrgir_return *ret(operand retval); 13201e04c3fSmrg 13301e04c3fSmrgir_expression *expr(ir_expression_operation op, operand a); 13401e04c3fSmrgir_expression *expr(ir_expression_operation op, operand a, operand b); 13501e04c3fSmrgir_expression *expr(ir_expression_operation op, operand a, operand b, operand c); 13601e04c3fSmrgir_expression *add(operand a, operand b); 13701e04c3fSmrgir_expression *sub(operand a, operand b); 13801e04c3fSmrgir_expression *mul(operand a, operand b); 13901e04c3fSmrgir_expression *imul_high(operand a, operand b); 14001e04c3fSmrgir_expression *div(operand a, operand b); 14101e04c3fSmrgir_expression *carry(operand a, operand b); 14201e04c3fSmrgir_expression *borrow(operand a, operand b); 14301e04c3fSmrgir_expression *trunc(operand a); 14401e04c3fSmrgir_expression *round_even(operand a); 14501e04c3fSmrgir_expression *fract(operand a); 14601e04c3fSmrgir_expression *dot(operand a, operand b); 14701e04c3fSmrgir_expression *clamp(operand a, operand b, operand c); 14801e04c3fSmrgir_expression *saturate(operand a); 14901e04c3fSmrgir_expression *abs(operand a); 15001e04c3fSmrgir_expression *neg(operand a); 15101e04c3fSmrgir_expression *sin(operand a); 15201e04c3fSmrgir_expression *cos(operand a); 15301e04c3fSmrgir_expression *exp(operand a); 15401e04c3fSmrgir_expression *rcp(operand a); 15501e04c3fSmrgir_expression *rsq(operand a); 15601e04c3fSmrgir_expression *sqrt(operand a); 15701e04c3fSmrgir_expression *log(operand a); 15801e04c3fSmrgir_expression *sign(operand a); 15901e04c3fSmrg 16001e04c3fSmrgir_expression *subr_to_int(operand a); 16101e04c3fSmrgir_expression *equal(operand a, operand b); 16201e04c3fSmrgir_expression *nequal(operand a, operand b); 16301e04c3fSmrgir_expression *less(operand a, operand b); 16401e04c3fSmrgir_expression *greater(operand a, operand b); 16501e04c3fSmrgir_expression *lequal(operand a, operand b); 16601e04c3fSmrgir_expression *gequal(operand a, operand b); 16701e04c3fSmrg 16801e04c3fSmrgir_expression *logic_not(operand a); 16901e04c3fSmrgir_expression *logic_and(operand a, operand b); 17001e04c3fSmrgir_expression *logic_or(operand a, operand b); 17101e04c3fSmrg 17201e04c3fSmrgir_expression *bit_not(operand a); 17301e04c3fSmrgir_expression *bit_or(operand a, operand b); 17401e04c3fSmrgir_expression *bit_and(operand a, operand b); 17501e04c3fSmrgir_expression *bit_xor(operand a, operand b); 17601e04c3fSmrgir_expression *lshift(operand a, operand b); 17701e04c3fSmrgir_expression *rshift(operand a, operand b); 17801e04c3fSmrg 17901e04c3fSmrgir_expression *f2i(operand a); 18001e04c3fSmrgir_expression *bitcast_f2i(operand a); 18101e04c3fSmrgir_expression *i2f(operand a); 18201e04c3fSmrgir_expression *bitcast_i2f(operand a); 18301e04c3fSmrgir_expression *f2u(operand a); 18401e04c3fSmrgir_expression *bitcast_f2u(operand a); 18501e04c3fSmrgir_expression *u2f(operand a); 18601e04c3fSmrgir_expression *bitcast_u2f(operand a); 18701e04c3fSmrgir_expression *i2u(operand a); 18801e04c3fSmrgir_expression *u2i(operand a); 18901e04c3fSmrgir_expression *b2i(operand a); 19001e04c3fSmrgir_expression *i2b(operand a); 19101e04c3fSmrgir_expression *f2b(operand a); 19201e04c3fSmrgir_expression *b2f(operand a); 19301e04c3fSmrg 19401e04c3fSmrgir_expression *f2d(operand a); 19501e04c3fSmrgir_expression *i2d(operand a); 19601e04c3fSmrgir_expression *u2d(operand a); 19701e04c3fSmrg 19801e04c3fSmrgir_expression *bitcast_d2i64(operand a); 19901e04c3fSmrgir_expression *bitcast_d2u64(operand a); 20001e04c3fSmrg 20101e04c3fSmrgir_expression *bitcast_i642d(operand a); 20201e04c3fSmrgir_expression *bitcast_u642d(operand a); 20301e04c3fSmrg 20401e04c3fSmrgir_expression *min2(operand a, operand b); 20501e04c3fSmrgir_expression *max2(operand a, operand b); 20601e04c3fSmrg 20701e04c3fSmrgir_expression *interpolate_at_centroid(operand a); 20801e04c3fSmrgir_expression *interpolate_at_offset(operand a, operand b); 20901e04c3fSmrgir_expression *interpolate_at_sample(operand a, operand b); 21001e04c3fSmrg 21101e04c3fSmrgir_expression *fma(operand a, operand b, operand c); 21201e04c3fSmrgir_expression *lrp(operand x, operand y, operand a); 21301e04c3fSmrgir_expression *csel(operand a, operand b, operand c); 21401e04c3fSmrgir_expression *bitfield_extract(operand a, operand b, operand c); 21501e04c3fSmrgir_expression *bitfield_insert(operand a, operand b, operand c, operand d); 21601e04c3fSmrg 21701e04c3fSmrgir_swizzle *swizzle(operand a, int swizzle, int components); 21801e04c3fSmrg/** 21901e04c3fSmrg * Swizzle away later components, but preserve the ordering. 22001e04c3fSmrg */ 22101e04c3fSmrgir_swizzle *swizzle_for_size(operand a, unsigned components); 22201e04c3fSmrg 22301e04c3fSmrgir_swizzle *swizzle_xxxx(operand a); 22401e04c3fSmrgir_swizzle *swizzle_yyyy(operand a); 22501e04c3fSmrgir_swizzle *swizzle_zzzz(operand a); 22601e04c3fSmrgir_swizzle *swizzle_wwww(operand a); 22701e04c3fSmrgir_swizzle *swizzle_x(operand a); 22801e04c3fSmrgir_swizzle *swizzle_y(operand a); 22901e04c3fSmrgir_swizzle *swizzle_z(operand a); 23001e04c3fSmrgir_swizzle *swizzle_w(operand a); 23101e04c3fSmrgir_swizzle *swizzle_xy(operand a); 23201e04c3fSmrgir_swizzle *swizzle_xyz(operand a); 23301e04c3fSmrgir_swizzle *swizzle_xyzw(operand a); 23401e04c3fSmrg 23501e04c3fSmrgir_if *if_tree(operand condition, 23601e04c3fSmrg ir_instruction *then_branch); 23701e04c3fSmrgir_if *if_tree(operand condition, 23801e04c3fSmrg ir_instruction *then_branch, 23901e04c3fSmrg ir_instruction *else_branch); 24001e04c3fSmrg 24101e04c3fSmrg} /* namespace ir_builder */ 24201e04c3fSmrg 24301e04c3fSmrg#endif 244