[PATCH vkd3d 3/6] vkd3d-shader/hlsl: Avoid hardcoding the size of the operands array.

Zebediah Figura zfigura at codeweavers.com
Mon Sep 27 20:51:44 CDT 2021


Signed-off-by: Zebediah Figura <zfigura at codeweavers.com>
---
 libs/vkd3d-shader/hlsl.c |  2 +-
 libs/vkd3d-shader/hlsl.h |  4 +++-
 libs/vkd3d-shader/hlsl.y | 26 +++++++++++++-------------
 3 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c
index 8a2286c45..c546bf06f 100644
--- a/libs/vkd3d-shader/hlsl.c
+++ b/libs/vkd3d-shader/hlsl.c
@@ -1108,7 +1108,7 @@ static void dump_ir_expr(struct vkd3d_string_buffer *buffer, const struct hlsl_i
     unsigned int i;
 
     vkd3d_string_buffer_printf(buffer, "%s (", debug_hlsl_expr_op(expr->op));
-    for (i = 0; i < 3 && expr->operands[i].node; ++i)
+    for (i = 0; i < HLSL_MAX_OPERANDS && expr->operands[i].node; ++i)
     {
         dump_src(buffer, &expr->operands[i]);
         vkd3d_string_buffer_printf(buffer, " ");
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h
index abc870f4f..7e6283921 100644
--- a/libs/vkd3d-shader/hlsl.h
+++ b/libs/vkd3d-shader/hlsl.h
@@ -323,11 +323,13 @@ enum hlsl_ir_expr_op
     HLSL_OP3_LERP,
 };
 
+#define HLSL_MAX_OPERANDS 3
+
 struct hlsl_ir_expr
 {
     struct hlsl_ir_node node;
     enum hlsl_ir_expr_op op;
-    struct hlsl_src operands[3];
+    struct hlsl_src operands[HLSL_MAX_OPERANDS];
 };
 
 enum hlsl_ir_jump_type
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y
index 8ad892f9e..21e1cb455 100644
--- a/libs/vkd3d-shader/hlsl.y
+++ b/libs/vkd3d-shader/hlsl.y
@@ -1022,15 +1022,15 @@ static struct hlsl_type *expr_common_type(struct hlsl_ctx *ctx, struct hlsl_type
     return hlsl_new_type(ctx, NULL, type, base, dimx, dimy);
 }
 
-static struct hlsl_ir_expr *add_expr(struct hlsl_ctx *ctx, struct list *instrs,
-        enum hlsl_ir_expr_op op, struct hlsl_ir_node *operands[3], struct vkd3d_shader_location *loc)
+static struct hlsl_ir_expr *add_expr(struct hlsl_ctx *ctx, struct list *instrs, enum hlsl_ir_expr_op op,
+        struct hlsl_ir_node *operands[HLSL_MAX_OPERANDS], struct vkd3d_shader_location *loc)
 {
     struct hlsl_ir_expr *expr;
     struct hlsl_type *type;
     unsigned int i;
 
     type = operands[0]->data_type;
-    for (i = 1; i <= 2; ++i)
+    for (i = 1; i < HLSL_MAX_OPERANDS; ++i)
     {
         if (!operands[i])
             break;
@@ -1038,7 +1038,7 @@ static struct hlsl_ir_expr *add_expr(struct hlsl_ctx *ctx, struct list *instrs,
         if (!type)
             return NULL;
     }
-    for (i = 0; i <= 2; ++i)
+    for (i = 0; i < HLSL_MAX_OPERANDS; ++i)
     {
         struct hlsl_ir_expr *cast;
 
@@ -1062,7 +1062,7 @@ static struct hlsl_ir_expr *add_expr(struct hlsl_ctx *ctx, struct list *instrs,
         return NULL;
     init_node(&expr->node, HLSL_IR_EXPR, type, *loc);
     expr->op = op;
-    for (i = 0; i <= 2; ++i)
+    for (i = 0; i < HLSL_MAX_OPERANDS; ++i)
         hlsl_src_from_node(&expr->operands[i], operands[i]);
     list_add_tail(instrs, &expr->node.entry);
 
@@ -1083,7 +1083,7 @@ static struct list *add_unary_expr(struct hlsl_ctx *ctx, struct list *instrs,
 static struct list *add_binary_expr(struct hlsl_ctx *ctx, struct list *list1, struct list *list2,
         enum hlsl_ir_expr_op op, struct vkd3d_shader_location loc)
 {
-    struct hlsl_ir_node *args[3] = {node_from_list(list1), node_from_list(list2)};
+    struct hlsl_ir_node *args[HLSL_MAX_OPERANDS] = {node_from_list(list1), node_from_list(list2)};
 
     list_move_tail(list1, list2);
     vkd3d_free(list2);
@@ -1157,7 +1157,7 @@ static struct hlsl_ir_node *add_assignment(struct hlsl_ctx *ctx, struct list *in
 
     if (assign_op == ASSIGN_OP_SUB)
     {
-        struct hlsl_ir_node *args[3] = {rhs};
+        struct hlsl_ir_node *args[HLSL_MAX_OPERANDS] = {rhs};
         struct hlsl_ir_expr *expr;
 
         if (!(expr = add_expr(ctx, instrs, HLSL_OP1_NEG, args, &rhs->loc)))
@@ -1167,8 +1167,8 @@ static struct hlsl_ir_node *add_assignment(struct hlsl_ctx *ctx, struct list *in
     }
     if (assign_op != ASSIGN_OP_ASSIGN)
     {
+        struct hlsl_ir_node *args[HLSL_MAX_OPERANDS] = {lhs, rhs};
         enum hlsl_ir_expr_op op = op_from_assignment(assign_op);
-        struct hlsl_ir_node *args[3] = {lhs, rhs};
         struct hlsl_ir_expr *expr;
 
         assert(op);
@@ -1556,7 +1556,7 @@ static const struct hlsl_ir_function_decl *find_function_call(struct hlsl_ctx *c
 static bool intrinsic_abs(struct hlsl_ctx *ctx,
         const struct parse_initializer *params, struct vkd3d_shader_location loc)
 {
-    struct hlsl_ir_node *args[3] = {params->args[0]};
+    struct hlsl_ir_node *args[HLSL_MAX_OPERANDS] = {params->args[0]};
 
     return !!add_expr(ctx, params->instrs, HLSL_OP1_ABS, args, &loc);
 }
@@ -1564,7 +1564,7 @@ static bool intrinsic_abs(struct hlsl_ctx *ctx,
 static bool intrinsic_clamp(struct hlsl_ctx *ctx,
         const struct parse_initializer *params, struct vkd3d_shader_location loc)
 {
-    struct hlsl_ir_node *args[3] = {params->args[0], params->args[1]};
+    struct hlsl_ir_node *args[HLSL_MAX_OPERANDS] = {params->args[0], params->args[1]};
     struct hlsl_ir_expr *max;
 
     if (!(max = add_expr(ctx, params->instrs, HLSL_OP2_MAX, args, &loc)))
@@ -1578,7 +1578,7 @@ static bool intrinsic_clamp(struct hlsl_ctx *ctx,
 static bool intrinsic_max(struct hlsl_ctx *ctx,
         const struct parse_initializer *params, struct vkd3d_shader_location loc)
 {
-    struct hlsl_ir_node *args[3] = {params->args[0], params->args[1]};
+    struct hlsl_ir_node *args[HLSL_MAX_OPERANDS] = {params->args[0], params->args[1]};
 
     return !!add_expr(ctx, params->instrs, HLSL_OP2_MAX, args, &loc);
 }
@@ -1586,7 +1586,7 @@ static bool intrinsic_max(struct hlsl_ctx *ctx,
 static bool intrinsic_pow(struct hlsl_ctx *ctx,
         const struct parse_initializer *params, struct vkd3d_shader_location loc)
 {
-    struct hlsl_ir_node *args[3] = {0};
+    struct hlsl_ir_node *args[HLSL_MAX_OPERANDS] = {0};
     struct hlsl_ir_node *log, *exp;
     struct hlsl_ir_expr *mul;
 
@@ -1608,7 +1608,7 @@ static bool intrinsic_pow(struct hlsl_ctx *ctx,
 static bool intrinsic_saturate(struct hlsl_ctx *ctx,
         const struct parse_initializer *params, struct vkd3d_shader_location loc)
 {
-    struct hlsl_ir_node *args[3] = {params->args[0]};
+    struct hlsl_ir_node *args[HLSL_MAX_OPERANDS] = {params->args[0]};
 
     return !!add_expr(ctx, params->instrs, HLSL_OP1_SAT, args, &loc);
 }
-- 
2.33.0




More information about the wine-devel mailing list