vkd3d-shader: Implement macro body expansion.

Signed-off-by: Zebediah Figura <zfigura@codeweavers.com>
Signed-off-by: Henri Verbeet <hverbeet@codeweavers.com>
Signed-off-by: Matteo Bruni <mbruni@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Zebediah Figura
2021-01-12 16:14:17 -06:00
committed by Alexandre Julliard
parent 73a937edd0
commit 668820f069
5 changed files with 114 additions and 23 deletions

View File

@@ -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,

View File

@@ -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->file_count > 1)
preproc_close_include(ctx, &file->code);
if (file->if_count)
if (ctx->expansion_count)
{
const struct vkd3d_shader_location loc = {.source_name = file->filename};
struct preproc_expansion *exp = &ctx->expansion_stack[ctx->expansion_count - 1];
preproc_warning(ctx, &loc, VKD3D_SHADER_WARNING_PP_UNTERMINATED_IF, "Unterminated #if block.");
yy_delete_buffer(exp->buffer.lexer_buffer, ctx->scanner);
--ctx->expansion_count;
TRACE("Expansion stack size is now %zu.\n", ctx->expansion_count);
}
vkd3d_free(file->if_stack);
else
{
struct preproc_file *file = preproc_get_top_file(ctx);
vkd3d_free(file->filename);
if (ctx->file_count > 1)
preproc_close_include(ctx, &file->code);
yy_delete_buffer(file->buffer.lexer_buffer, ctx->scanner);
if (file->if_count)
{
const struct vkd3d_shader_location loc = {.source_name = file->filename};
--ctx->file_count;
TRACE("File stack size is now %zu.\n", ctx->file_count);
preproc_warning(ctx, &loc, VKD3D_SHADER_WARNING_PP_UNTERMINATED_IF, "Unterminated #if block.");
}
vkd3d_free(file->if_stack);
if (ctx->file_count)
vkd3d_free(file->filename);
yy_delete_buffer(file->buffer.lexer_buffer, ctx->scanner);
--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)
{

View File

@@ -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
{