[PATCH vkd3d 1/5] vkd3d-shader: Implement macro body expansion.

Zebediah Figura zfigura at codeweavers.com
Tue Jan 12 16:14:17 CST 2021


Signed-off-by: Zebediah Figura <zfigura at codeweavers.com>
---
 Makefile.am                 |  1 -
 libs/vkd3d-shader/preproc.h | 18 ++++++++
 libs/vkd3d-shader/preproc.l | 83 +++++++++++++++++++++++++++++++------
 libs/vkd3d-shader/preproc.y | 24 +++++++++--
 tests/hlsl_d3d12.c          |  9 ++--
 5 files changed, 113 insertions(+), 22 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index bf1d7bfa..514f0506 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -243,7 +243,6 @@ XFAIL_TESTS = \
 	tests/hlsl-vector-indexing.shader_test \
 	tests/hlsl-vector-indexing-uniform.shader_test \
 	tests/math.shader_test \
-	tests/preproc-if.shader_test \
 	tests/preproc-ifdef.shader_test \
 	tests/preproc-if-expr.shader_test \
 	tests/preproc-invalid.shader_test \
diff --git a/libs/vkd3d-shader/preproc.h b/libs/vkd3d-shader/preproc.h
index 16b579e0..7d6bda2a 100644
--- a/libs/vkd3d-shader/preproc.h
+++ b/libs/vkd3d-shader/preproc.h
@@ -50,10 +50,24 @@ struct preproc_file
     size_t if_count, if_stack_size;
 };
 
+struct preproc_text
+{
+    struct vkd3d_string_buffer text;
+    struct vkd3d_shader_location location;
+};
+
+struct preproc_expansion
+{
+    struct preproc_buffer buffer;
+    const struct preproc_text *text;
+};
+
 struct preproc_macro
 {
     struct rb_entry entry;
     char *name;
+
+    struct preproc_text body;
 };
 
 struct preproc_ctx
@@ -67,6 +81,9 @@ struct preproc_ctx
     struct preproc_file *file_stack;
     size_t file_count, file_stack_size;
 
+    struct preproc_expansion *expansion_stack;
+    size_t expansion_count, expansion_stack_size;
+
     struct rb_tree macros;
 
     int current_directive;
@@ -78,6 +95,7 @@ struct preproc_ctx
 };
 
 void preproc_close_include(struct preproc_ctx *ctx, const struct vkd3d_shader_code *code) DECLSPEC_HIDDEN;
+struct preproc_macro *preproc_find_macro(struct preproc_ctx *ctx, const char *name) DECLSPEC_HIDDEN;
 void preproc_free_macro(struct preproc_macro *macro) DECLSPEC_HIDDEN;
 bool preproc_push_include(struct preproc_ctx *ctx, char *filename, const struct vkd3d_shader_code *code) DECLSPEC_HIDDEN;
 void preproc_warning(struct preproc_ctx *ctx, const struct vkd3d_shader_location *loc,
diff --git a/libs/vkd3d-shader/preproc.l b/libs/vkd3d-shader/preproc.l
index 476c1e59..86ca4733 100644
--- a/libs/vkd3d-shader/preproc.l
+++ b/libs/vkd3d-shader/preproc.l
@@ -169,27 +169,41 @@ static void update_location(struct preproc_ctx *ctx)
 
 static void preproc_pop_buffer(struct preproc_ctx *ctx)
 {
-    struct preproc_file *file = preproc_get_top_file(ctx);
+    if (ctx->expansion_count)
+    {
+        struct preproc_expansion *exp = &ctx->expansion_stack[ctx->expansion_count - 1];
 
-    if (ctx->file_count > 1)
-        preproc_close_include(ctx, &file->code);
+        yy_delete_buffer(exp->buffer.lexer_buffer, ctx->scanner);
 
-    if (file->if_count)
+        --ctx->expansion_count;
+        TRACE("Expansion stack size is now %zu.\n", ctx->expansion_count);
+    }
+    else
     {
-        const struct vkd3d_shader_location loc = {.source_name = file->filename};
+        struct preproc_file *file = preproc_get_top_file(ctx);
 
-        preproc_warning(ctx, &loc, VKD3D_SHADER_WARNING_PP_UNTERMINATED_IF, "Unterminated #if block.");
-    }
-    vkd3d_free(file->if_stack);
+        if (ctx->file_count > 1)
+            preproc_close_include(ctx, &file->code);
+
+        if (file->if_count)
+        {
+            const struct vkd3d_shader_location loc = {.source_name = file->filename};
 
-    vkd3d_free(file->filename);
+            preproc_warning(ctx, &loc, VKD3D_SHADER_WARNING_PP_UNTERMINATED_IF, "Unterminated #if block.");
+        }
+        vkd3d_free(file->if_stack);
 
-    yy_delete_buffer(file->buffer.lexer_buffer, ctx->scanner);
+        vkd3d_free(file->filename);
 
-    --ctx->file_count;
-    TRACE("File stack size is now %zu.\n", ctx->file_count);
+        yy_delete_buffer(file->buffer.lexer_buffer, ctx->scanner);
 
-    if (ctx->file_count)
+        --ctx->file_count;
+        TRACE("File stack size is now %zu.\n", ctx->file_count);
+    }
+
+    if (ctx->expansion_count)
+        yy_switch_to_buffer(ctx->expansion_stack[ctx->expansion_count - 1].buffer.lexer_buffer, ctx->scanner);
+    else if (ctx->file_count)
         yy_switch_to_buffer(ctx->file_stack[ctx->file_count - 1].buffer.lexer_buffer, ctx->scanner);
 }
 
@@ -218,6 +232,21 @@ static int return_token(int token, YYSTYPE *lval, const char *text)
     return token;
 }
 
+static bool preproc_push_expansion(struct preproc_ctx *ctx, const struct preproc_text *text)
+{
+    struct preproc_expansion *exp;
+
+    if (!vkd3d_array_reserve((void **)&ctx->expansion_stack, &ctx->expansion_stack_size,
+            ctx->expansion_count + 1, sizeof(*ctx->expansion_stack)))
+        return false;
+    exp = &ctx->expansion_stack[ctx->expansion_count++];
+    exp->text = text;
+    exp->buffer.lexer_buffer = yy_scan_bytes(text->text.buffer, text->text.content_size, ctx->scanner);
+    exp->buffer.location = text->location;
+    TRACE("Expansion stack size is now %zu.\n", ctx->expansion_count);
+    return true;
+}
+
 int yylex(YYSTYPE *lval, YYLTYPE *lloc, yyscan_t scanner)
 {
     struct preproc_ctx *ctx = yyget_extra(scanner);
@@ -239,6 +268,10 @@ int yylex(YYSTYPE *lval, YYLTYPE *lloc, yyscan_t scanner)
         if (!(token = preproc_lexer_lex(lval, lloc, scanner)))
         {
             ctx->last_was_eof = true;
+
+            /* If we have reached the end of an included file, inject a newline. */
+            if (ctx->expansion_count)
+                continue;
             token = T_NEWLINE;
             text = "\n";
         }
@@ -288,6 +321,29 @@ int yylex(YYSTYPE *lval, YYLTYPE *lloc, yyscan_t scanner)
                     continue;
         }
 
+        if (token == T_IDENTIFIER)
+        {
+            struct preproc_macro *macro;
+
+            switch (ctx->current_directive)
+            {
+                case T_DEFINE:
+                case T_IFDEF:
+                case T_IFNDEF:
+                case T_UNDEF:
+                    /* Return identifiers verbatim. */
+                    return return_token(token, lval, text);
+            }
+
+            /* Otherwise, expand a macro if there is one. */
+
+            if ((macro = preproc_find_macro(ctx, text)))
+            {
+                preproc_push_expansion(ctx, &macro->body);
+                continue;
+            }
+        }
+
         if (ctx->current_directive)
             return return_token(token, lval, text);
 
@@ -365,6 +421,7 @@ int preproc_lexer_parse(const struct vkd3d_shader_compile_info *compile_info,
 
     rb_destroy(&ctx.macros, preproc_macro_rb_free, NULL);
     vkd3d_free(ctx.file_stack);
+    vkd3d_free(ctx.expansion_stack);
 
     if (ctx.error)
     {
diff --git a/libs/vkd3d-shader/preproc.y b/libs/vkd3d-shader/preproc.y
index 53ce3e57..17bc6499 100644
--- a/libs/vkd3d-shader/preproc.y
+++ b/libs/vkd3d-shader/preproc.y
@@ -68,7 +68,7 @@ static void yyerror(const YYLTYPE *loc, void *scanner, struct preproc_ctx *ctx,
     preproc_error(ctx, loc, VKD3D_SHADER_ERROR_PP_INVALID_SYNTAX, "%s", string);
 }
 
-static struct preproc_macro *preproc_find_macro(struct preproc_ctx *ctx, const char *name)
+struct preproc_macro *preproc_find_macro(struct preproc_ctx *ctx, const char *name)
 {
     struct rb_entry *entry;
 
@@ -77,7 +77,8 @@ static struct preproc_macro *preproc_find_macro(struct preproc_ctx *ctx, const c
     return NULL;
 }
 
-static bool preproc_add_macro(struct preproc_ctx *ctx, const struct vkd3d_shader_location *loc, char *name)
+static bool preproc_add_macro(struct preproc_ctx *ctx, const struct vkd3d_shader_location *loc, char *name,
+        const struct vkd3d_shader_location *body_loc, struct vkd3d_string_buffer *body)
 {
     struct preproc_macro *macro;
     int ret;
@@ -94,6 +95,8 @@ static bool preproc_add_macro(struct preproc_ctx *ctx, const struct vkd3d_shader
     if (!(macro = vkd3d_malloc(sizeof(*macro))))
         return false;
     macro->name = name;
+    macro->body.text = *body;
+    macro->body.location = *body_loc;
     ret = rb_put(&ctx->macros, name, &macro->entry);
     assert(!ret);
     return true;
@@ -102,6 +105,7 @@ static bool preproc_add_macro(struct preproc_ctx *ctx, const struct vkd3d_shader
 void preproc_free_macro(struct preproc_macro *macro)
 {
     vkd3d_free(macro->name);
+    vkd3d_string_buffer_cleanup(&macro->body.text);
     vkd3d_free(macro);
 }
 
@@ -265,6 +269,7 @@ static const void *get_parent_data(struct preproc_ctx *ctx)
 {
     char *string;
     uint32_t integer;
+    struct vkd3d_string_buffer string_buffer;
 }
 
 %token <string> T_IDENTIFIER
@@ -286,6 +291,7 @@ static const void *get_parent_data(struct preproc_ctx *ctx)
 
 %type <integer> expr
 %type <string> body_token
+%type <string_buffer> body_text
 
 %%
 
@@ -298,8 +304,16 @@ shader_text
 
 body_text
     : %empty
+        {
+            vkd3d_string_buffer_init(&$$);
+        }
     | body_text body_token
         {
+            if (vkd3d_string_buffer_printf(&$$, "%s ", $2) < 0)
+            {
+                vkd3d_free($2);
+                YYABORT;
+            }
             vkd3d_free($2);
         }
 
@@ -311,8 +325,12 @@ body_token
 directive
     : T_DEFINE T_IDENTIFIER body_text T_NEWLINE
         {
-            if (!preproc_add_macro(ctx, &@$, $2))
+            if (!preproc_add_macro(ctx, &@$, $2, &@3, &$3))
+            {
+                vkd3d_free($2);
+                vkd3d_string_buffer_cleanup(&$3);
                 YYABORT;
+            }
         }
     | T_UNDEF T_IDENTIFIER T_NEWLINE
         {
diff --git a/tests/hlsl_d3d12.c b/tests/hlsl_d3d12.c
index 54ab94de..ac616c23 100644
--- a/tests/hlsl_d3d12.c
+++ b/tests/hlsl_d3d12.c
@@ -349,10 +349,10 @@ static void test_preprocess(void)
 
     for (i = 0; i < ARRAY_SIZE(tests); ++i)
     {
-        if (i == 6 || i == 10 || i == 11)
+        if (i == 10 || i == 11)
             continue;
         vkd3d_test_set_context("Source \"%s\"", tests[i].source);
-        todo_if (i <= 4 || (i >= 9 && i <= 14))
+        todo_if (i <= 4 || (i >= 9 && i <= 14) || i == 43)
             check_preprocess(tests[i].source, NULL, NULL, tests[i].present, tests[i].absent);
     }
     vkd3d_test_set_context(NULL);
@@ -400,12 +400,11 @@ static void test_preprocess(void)
     macros[1].Definition = "KEY2";
     todo check_preprocess("KEY", macros, NULL, "value", NULL);
 
-    if (0)
-        todo check_preprocess(test_include_top, NULL, &test_include, "pass", "fail");
+    check_preprocess(test_include_top, NULL, &test_include, "pass", "fail");
     ok(!refcount_file1, "Got %d references to file1.\n", refcount_file1);
     ok(!refcount_file2, "Got %d references to file1.\n", refcount_file2);
     ok(!refcount_file3, "Got %d references to file1.\n", refcount_file3);
-    todo ok(include_count_file2 == 2, "file2 was included %u times.\n", include_count_file2);
+    ok(include_count_file2 == 2, "file2 was included %u times.\n", include_count_file2);
 
     /* Macro invocation spread across multiple files. */
     if (0)
-- 
2.30.0




More information about the wine-devel mailing list