From 4c5fd9c7b96e45a769bd21f1b6adcf2469083431 Mon Sep 17 00:00:00 2001
From: Zebediah Figura <zfigura@codeweavers.com>
Date: Mon, 26 Sep 2022 18:50:04 -0500
Subject: [PATCH 1/4] vkd3d-shader/hlsl: Introduce a hlsl_new_float_constant()
 helper.

---
 libs/vkd3d-shader/hlsl.c | 11 +++++++++++
 libs/vkd3d-shader/hlsl.h |  2 ++
 libs/vkd3d-shader/hlsl.y |  4 +---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c
index c484863291..cb1fd14a32 100644
--- a/libs/vkd3d-shader/hlsl.c
+++ b/libs/vkd3d-shader/hlsl.c
@@ -915,6 +915,17 @@ struct hlsl_ir_constant *hlsl_new_constant(struct hlsl_ctx *ctx, struct hlsl_typ
     return c;
 }
 
+struct hlsl_ir_constant *hlsl_new_float_constant(struct hlsl_ctx *ctx, float f,
+        const struct vkd3d_shader_location *loc)
+{
+    struct hlsl_ir_constant *c;
+
+    if ((c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_FLOAT), loc)))
+        c->value[0].f = f;
+
+    return c;
+}
+
 struct hlsl_ir_constant *hlsl_new_int_constant(struct hlsl_ctx *ctx, int n,
         const struct vkd3d_shader_location *loc)
 {
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h
index b9dfc9cd3c..c5bd9b79fb 100644
--- a/libs/vkd3d-shader/hlsl.h
+++ b/libs/vkd3d-shader/hlsl.h
@@ -758,6 +758,8 @@ struct hlsl_ir_expr *hlsl_new_cast(struct hlsl_ctx *ctx, struct hlsl_ir_node *no
 struct hlsl_ir_constant *hlsl_new_constant(struct hlsl_ctx *ctx, struct hlsl_type *type,
         const struct vkd3d_shader_location *loc);
 struct hlsl_ir_expr *hlsl_new_copy(struct hlsl_ctx *ctx, struct hlsl_ir_node *node);
+struct hlsl_ir_constant *hlsl_new_float_constant(struct hlsl_ctx *ctx,
+        float f, const struct vkd3d_shader_location *loc);
 struct hlsl_ir_function_decl *hlsl_new_func_decl(struct hlsl_ctx *ctx, struct hlsl_type *return_type,
         struct list *parameters, const struct hlsl_semantic *semantic, struct vkd3d_shader_location loc);
 struct hlsl_ir_if *hlsl_new_if(struct hlsl_ctx *ctx, struct hlsl_ir_node *condition, struct vkd3d_shader_location loc);
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y
index 4c9b912ceb..565a551d4a 100644
--- a/libs/vkd3d-shader/hlsl.y
+++ b/libs/vkd3d-shader/hlsl.y
@@ -3981,10 +3981,8 @@ primary_expr:
         {
             struct hlsl_ir_constant *c;
 
-            if (!(c = hlsl_alloc(ctx, sizeof(*c))))
+            if (!(c = hlsl_new_float_constant(ctx, $1, &@1)))
                 YYABORT;
-            init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_FLOAT), @1);
-            c->value[0].f = $1;
             if (!($$ = make_list(ctx, &c->node)))
                 YYABORT;
         }
-- 
GitLab


From 3fc2fdc37f2e6c3b9c3560213a8fdd36577b6e80 Mon Sep 17 00:00:00 2001
From: Zebediah Figura <zfigura@codeweavers.com>
Date: Mon, 26 Sep 2022 18:54:03 -0500
Subject: [PATCH 2/4] vkd3d-shader/hlsl: Introduce a hlsl_new_bool_constant()
 helper.

---
 libs/vkd3d-shader/hlsl.c | 10 ++++++++++
 libs/vkd3d-shader/hlsl.h |  1 +
 libs/vkd3d-shader/hlsl.y | 13 +++++++------
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl.c b/libs/vkd3d-shader/hlsl.c
index cb1fd14a32..c1cd065a50 100644
--- a/libs/vkd3d-shader/hlsl.c
+++ b/libs/vkd3d-shader/hlsl.c
@@ -915,6 +915,16 @@ struct hlsl_ir_constant *hlsl_new_constant(struct hlsl_ctx *ctx, struct hlsl_typ
     return c;
 }
 
+struct hlsl_ir_constant *hlsl_new_bool_constant(struct hlsl_ctx *ctx, bool b, const struct vkd3d_shader_location *loc)
+{
+    struct hlsl_ir_constant *c;
+
+    if ((c = hlsl_new_constant(ctx, hlsl_get_scalar_type(ctx, HLSL_TYPE_BOOL), loc)))
+        c->value[0].u = b ? ~0u : 0;
+
+    return c;
+}
+
 struct hlsl_ir_constant *hlsl_new_float_constant(struct hlsl_ctx *ctx, float f,
         const struct vkd3d_shader_location *loc)
 {
diff --git a/libs/vkd3d-shader/hlsl.h b/libs/vkd3d-shader/hlsl.h
index c5bd9b79fb..770a4032e9 100644
--- a/libs/vkd3d-shader/hlsl.h
+++ b/libs/vkd3d-shader/hlsl.h
@@ -751,6 +751,7 @@ struct hlsl_type *hlsl_get_element_type_from_path_index(struct hlsl_ctx *ctx, co
 struct hlsl_type *hlsl_new_array_type(struct hlsl_ctx *ctx, struct hlsl_type *basic_type, unsigned int array_size);
 struct hlsl_ir_node *hlsl_new_binary_expr(struct hlsl_ctx *ctx, enum hlsl_ir_expr_op op, struct hlsl_ir_node *arg1,
         struct hlsl_ir_node *arg2);
+struct hlsl_ir_constant *hlsl_new_bool_constant(struct hlsl_ctx *ctx, bool b, const struct vkd3d_shader_location *loc);
 struct hlsl_buffer *hlsl_new_buffer(struct hlsl_ctx *ctx, enum hlsl_buffer_type type, const char *name,
         const struct hlsl_reg_reservation *reservation, struct vkd3d_shader_location loc);
 struct hlsl_ir_expr *hlsl_new_cast(struct hlsl_ctx *ctx, struct hlsl_ir_node *node, struct hlsl_type *type,
diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y
index 565a551d4a..50a3a09445 100644
--- a/libs/vkd3d-shader/hlsl.y
+++ b/libs/vkd3d-shader/hlsl.y
@@ -2800,7 +2800,7 @@ static bool add_method_call(struct hlsl_ctx *ctx, struct list *instrs, struct hl
     struct hlsl_type *type;
     INT intval;
     FLOAT floatval;
-    BOOL boolval;
+    bool boolval;
     char *name;
     DWORD modifiers;
     struct hlsl_ir_node *instr;
@@ -3857,11 +3857,11 @@ initializer_expr_list:
 boolean:
       KW_TRUE
         {
-            $$ = TRUE;
+            $$ = true;
         }
     | KW_FALSE
         {
-            $$ = FALSE;
+            $$ = false;
         }
 
 statement_list:
@@ -4001,12 +4001,13 @@ primary_expr:
         {
             struct hlsl_ir_constant *c;
 
-            if (!(c = hlsl_alloc(ctx, sizeof(*c))))
+            if (!(c = hlsl_new_bool_constant(ctx, $1, &@1)))
                 YYABORT;
-            init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_BOOL), @1);
-            c->value[0].u = $1 ? ~0u : 0;
             if (!($$ = make_list(ctx, &c->node)))
+            {
+                hlsl_free_instr(&c->node);
                 YYABORT;
+            }
         }
     | VAR_IDENTIFIER
         {
-- 
GitLab


From f8da1000524388bfbd19243c72e8d57956e0bd7b Mon Sep 17 00:00:00 2001
From: Zebediah Figura <zfigura@codeweavers.com>
Date: Mon, 26 Sep 2022 18:54:09 -0500
Subject: [PATCH 3/4] vkd3d-shader/hlsl: Use hlsl_new_int_constant() in the
 "primary_expr" rule.

---
 libs/vkd3d-shader/hlsl.y | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl.y b/libs/vkd3d-shader/hlsl.y
index 50a3a09445..fc3b31a42e 100644
--- a/libs/vkd3d-shader/hlsl.y
+++ b/libs/vkd3d-shader/hlsl.y
@@ -3990,10 +3990,8 @@ primary_expr:
         {
             struct hlsl_ir_constant *c;
 
-            if (!(c = hlsl_alloc(ctx, sizeof(*c))))
+            if (!(c = hlsl_new_int_constant(ctx, $1, &@1)))
                 YYABORT;
-            init_node(&c->node, HLSL_IR_CONSTANT, hlsl_get_scalar_type(ctx, HLSL_TYPE_INT), @1);
-            c->value[0].i = $1;
             if (!($$ = make_list(ctx, &c->node)))
                 YYABORT;
         }
-- 
GitLab


From b74a5460341090a9c485e7d73a75596326e25d54 Mon Sep 17 00:00:00 2001
From: Zebediah Figura <zfigura@codeweavers.com>
Date: Mon, 26 Sep 2022 19:13:29 -0500
Subject: [PATCH 4/4] vkd3d-shader/hlsl: Use hlsl_new_constant() in more
 places.

---
 libs/vkd3d-shader/hlsl_constant_ops.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl_constant_ops.c b/libs/vkd3d-shader/hlsl_constant_ops.c
index 7ca63a3e7e..858f020ce0 100644
--- a/libs/vkd3d-shader/hlsl_constant_ops.c
+++ b/libs/vkd3d-shader/hlsl_constant_ops.c
@@ -526,9 +526,8 @@ bool hlsl_fold_constant_exprs(struct hlsl_ctx *ctx, struct hlsl_ir_node *instr,
     if (expr->operands[1].node)
         arg2 = hlsl_ir_constant(expr->operands[1].node);
 
-    if (!(res = hlsl_alloc(ctx, sizeof(*res))))
+    if (!(res = hlsl_new_constant(ctx, instr->data_type, &instr->loc)))
         return false;
-    init_node(&res->node, HLSL_IR_CONSTANT, instr->data_type, instr->loc);
 
     switch (expr->op)
     {
@@ -611,9 +610,8 @@ bool hlsl_fold_constant_swizzles(struct hlsl_ctx *ctx, struct hlsl_ir_node *inst
         return false;
     value = hlsl_ir_constant(swizzle->val.node);
 
-    if (!(res = hlsl_alloc(ctx, sizeof(*res))))
+    if (!(res = hlsl_new_constant(ctx, instr->data_type, &instr->loc)))
         return false;
-    init_node(&res->node, HLSL_IR_CONSTANT, instr->data_type, instr->loc);
 
     swizzle_bits = swizzle->swizzle;
     for (i = 0; i < swizzle->node.data_type->dimx; ++i)
-- 
GitLab