2020-12-07 10:56:30 -08:00
|
|
|
/*
|
|
|
|
* HLSL preprocessor
|
|
|
|
*
|
|
|
|
* Copyright 2020 Zebediah Figura for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
%{
|
|
|
|
|
|
|
|
#include "preproc.tab.h"
|
|
|
|
|
|
|
|
#define YYSTYPE PREPROC_YYSTYPE
|
|
|
|
#define YYLTYPE PREPROC_YYLTYPE
|
|
|
|
|
|
|
|
#define YY_DECL static int preproc_lexer_lex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param, yyscan_t yyscanner)
|
|
|
|
|
2020-12-15 15:13:20 -08:00
|
|
|
static void update_location(struct preproc_ctx *ctx);
|
|
|
|
|
|
|
|
#define YY_USER_ACTION update_location(yyget_extra(yyscanner));
|
|
|
|
|
2020-12-07 10:56:30 -08:00
|
|
|
%}
|
|
|
|
|
|
|
|
%option 8bit
|
|
|
|
%option bison-bridge
|
|
|
|
%option bison-locations
|
|
|
|
%option extra-type="struct preproc_ctx *"
|
|
|
|
%option never-interactive
|
|
|
|
%option noinput
|
|
|
|
%option nounput
|
2020-12-07 10:56:31 -08:00
|
|
|
%option noyy_top_state
|
2020-12-07 10:56:30 -08:00
|
|
|
%option noyywrap
|
|
|
|
%option prefix="preproc_yy"
|
|
|
|
%option reentrant
|
2020-12-07 10:56:31 -08:00
|
|
|
%option stack
|
|
|
|
|
|
|
|
/* Because these can both be terminated by EOF, we need states for them. */
|
|
|
|
%s C_COMMENT
|
|
|
|
%s CXX_COMMENT
|
2020-12-07 10:56:30 -08:00
|
|
|
|
2020-12-21 12:37:06 -08:00
|
|
|
NEWLINE \r?\n
|
2020-12-07 10:56:30 -08:00
|
|
|
WS [ \t]
|
2020-12-07 10:56:32 -08:00
|
|
|
IDENTIFIER [A-Za-z_][A-Za-z0-9_]*
|
2020-12-07 10:56:30 -08:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
2020-12-07 10:56:31 -08:00
|
|
|
<INITIAL>"//" {yy_push_state(CXX_COMMENT, yyscanner);}
|
|
|
|
<INITIAL>"/*" {yy_push_state(C_COMMENT, yyscanner);}
|
2020-12-21 12:37:06 -08:00
|
|
|
<CXX_COMMENT>\\{NEWLINE} {}
|
2020-12-07 10:56:31 -08:00
|
|
|
<CXX_COMMENT>\n {
|
|
|
|
yy_pop_state(yyscanner);
|
2020-12-21 12:37:06 -08:00
|
|
|
return T_NEWLINE;
|
2020-12-07 10:56:31 -08:00
|
|
|
}
|
|
|
|
<C_COMMENT>"*/" {yy_pop_state(yyscanner);}
|
|
|
|
<C_COMMENT,CXX_COMMENT><<EOF>> {yy_pop_state(yyscanner);}
|
|
|
|
<C_COMMENT,CXX_COMMENT>. {}
|
|
|
|
|
2021-01-07 09:48:06 -08:00
|
|
|
<INITIAL>{IDENTIFIER} {return T_IDENTIFIER;}
|
2020-12-07 10:56:32 -08:00
|
|
|
|
2020-12-21 12:37:06 -08:00
|
|
|
/* We have no use for floats, but shouldn't parse them as integers. */
|
|
|
|
|
2020-12-07 10:56:32 -08:00
|
|
|
<INITIAL>[0-9]*\.[0-9]+([eE][+-]?[0-9]+)?[hHfF]? {return T_TEXT;}
|
|
|
|
<INITIAL>[0-9]+\.([eE][+-]?[0-9]+)?[hHfF]? {return T_TEXT;}
|
|
|
|
<INITIAL>[0-9]+([eE][+-]?[0-9]+)?[hHfF] {return T_TEXT;}
|
|
|
|
<INITIAL>[0-9]+[eE][+-]?[0-9]+ {return T_TEXT;}
|
2020-12-21 12:37:06 -08:00
|
|
|
<INITIAL>0[xX][0-9a-fA-f]+[ul]{0,2} {return T_INTEGER;}
|
|
|
|
<INITIAL>0[0-7]*[ul]{0,2} {return T_INTEGER;}
|
|
|
|
<INITIAL>[1-9][0-9]*[ul]{0,2} {return T_INTEGER;}
|
2020-12-07 10:56:32 -08:00
|
|
|
|
|
|
|
<INITIAL>"&&" {return T_TEXT;}
|
|
|
|
<INITIAL>"||" {return T_TEXT;}
|
|
|
|
<INITIAL>"++" {return T_TEXT;}
|
|
|
|
<INITIAL>"--" {return T_TEXT;}
|
|
|
|
<INITIAL>"<<"=? {return T_TEXT;}
|
|
|
|
<INITIAL>">>"=? {return T_TEXT;}
|
|
|
|
<INITIAL>[-+*/%&|^=><!]= {return T_TEXT;}
|
|
|
|
|
|
|
|
/* C strings (including escaped quotes). */
|
|
|
|
<INITIAL>\"([^"\\]|\\.)*\" {return T_TEXT;}
|
|
|
|
|
2020-12-21 12:37:06 -08:00
|
|
|
<INITIAL>#{WS}*{IDENTIFIER} {
|
|
|
|
struct preproc_ctx *ctx = yyget_extra(yyscanner);
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (!ctx->last_was_newline)
|
|
|
|
return T_TEXT;
|
|
|
|
|
|
|
|
for (p = yytext + 1; strchr(" \t", *p); ++p)
|
|
|
|
;
|
|
|
|
|
2021-01-07 09:48:06 -08:00
|
|
|
if (!strcmp(p, "define"))
|
|
|
|
return T_DEFINE;
|
2020-12-15 15:13:23 -08:00
|
|
|
if (!strcmp(p, "elif"))
|
|
|
|
return T_ELIF;
|
2020-12-15 15:13:22 -08:00
|
|
|
if (!strcmp(p, "else"))
|
|
|
|
return T_ELSE;
|
2020-12-21 12:37:06 -08:00
|
|
|
if (!strcmp(p, "endif"))
|
|
|
|
return T_ENDIF;
|
|
|
|
if (!strcmp(p, "if"))
|
|
|
|
return T_IF;
|
2021-01-07 09:48:07 -08:00
|
|
|
if (!strcmp(p, "ifdef"))
|
|
|
|
return T_IFDEF;
|
2021-01-07 09:48:08 -08:00
|
|
|
if (!strcmp(p, "ifndef"))
|
|
|
|
return T_IFNDEF;
|
2020-12-21 12:37:06 -08:00
|
|
|
|
|
|
|
preproc_warning(ctx, yyget_lloc(yyscanner), VKD3D_SHADER_WARNING_PP_UNKNOWN_DIRECTIVE,
|
|
|
|
"Ignoring unknown directive \"%s\".", yytext);
|
|
|
|
return T_TEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
<INITIAL>\\{NEWLINE} {}
|
|
|
|
<INITIAL>{NEWLINE} {return T_NEWLINE;}
|
|
|
|
|
2020-12-07 10:56:31 -08:00
|
|
|
<INITIAL>{WS}+ {}
|
|
|
|
<INITIAL>. {return T_TEXT;}
|
2020-12-07 10:56:30 -08:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
2020-12-15 15:13:20 -08:00
|
|
|
static void update_location(struct preproc_ctx *ctx)
|
|
|
|
{
|
|
|
|
unsigned int i, leng = yyget_leng(ctx->scanner);
|
|
|
|
const char *text = yyget_text(ctx->scanner);
|
|
|
|
|
|
|
|
/* We want to do this here, rather than before calling yylex(), because
|
|
|
|
* some tokens are skipped by the lexer. */
|
|
|
|
|
|
|
|
*yyget_lloc(ctx->scanner) = ctx->location;
|
|
|
|
|
|
|
|
for (i = 0; i < leng; ++i)
|
|
|
|
{
|
|
|
|
++ctx->location.column;
|
|
|
|
if (text[i] == '\n')
|
|
|
|
{
|
|
|
|
ctx->location.column = 1;
|
|
|
|
++ctx->location.line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 12:37:06 -08:00
|
|
|
static bool preproc_is_writing(struct preproc_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (!ctx->if_count)
|
|
|
|
return true;
|
|
|
|
return ctx->if_stack[ctx->if_count - 1].current_true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int return_token(int token, YYSTYPE *lval, const char *text)
|
|
|
|
{
|
|
|
|
switch (token)
|
|
|
|
{
|
2021-01-07 09:48:06 -08:00
|
|
|
case T_IDENTIFIER:
|
2020-12-21 12:37:06 -08:00
|
|
|
case T_INTEGER:
|
|
|
|
case T_TEXT:
|
|
|
|
if (!(lval->string = vkd3d_strdup(text)))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2020-12-07 10:56:30 -08:00
|
|
|
int yylex(YYSTYPE *lval, YYLTYPE *lloc, yyscan_t scanner)
|
|
|
|
{
|
|
|
|
struct preproc_ctx *ctx = yyget_extra(scanner);
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
const char *text;
|
|
|
|
int token;
|
|
|
|
|
2020-12-21 12:37:06 -08:00
|
|
|
if (ctx->last_was_eof)
|
2020-12-07 10:56:30 -08:00
|
|
|
return 0;
|
|
|
|
|
2020-12-21 12:37:06 -08:00
|
|
|
if (!(token = preproc_lexer_lex(lval, lloc, scanner)))
|
|
|
|
{
|
|
|
|
ctx->last_was_eof = true;
|
|
|
|
token = T_NEWLINE;
|
|
|
|
text = "\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
text = yyget_text(scanner);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->last_was_newline)
|
|
|
|
{
|
|
|
|
switch (token)
|
|
|
|
{
|
2021-01-07 09:48:06 -08:00
|
|
|
case T_DEFINE:
|
2020-12-15 15:13:23 -08:00
|
|
|
case T_ELIF:
|
2020-12-15 15:13:22 -08:00
|
|
|
case T_ELSE:
|
2020-12-21 12:37:06 -08:00
|
|
|
case T_ENDIF:
|
|
|
|
case T_IF:
|
2021-01-07 09:48:07 -08:00
|
|
|
case T_IFDEF:
|
2021-01-07 09:48:08 -08:00
|
|
|
case T_IFNDEF:
|
2020-12-21 12:37:06 -08:00
|
|
|
ctx->current_directive = token;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ctx->current_directive = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->last_was_newline = (token == T_NEWLINE);
|
|
|
|
|
|
|
|
TRACE("Parsing token %d, line %d, in directive %d, string %s.\n",
|
|
|
|
token, lloc->line, ctx->current_directive, debugstr_a(text));
|
|
|
|
|
2021-01-07 09:48:06 -08:00
|
|
|
switch (ctx->current_directive)
|
|
|
|
{
|
|
|
|
case T_ELIF:
|
|
|
|
case T_ELSE:
|
|
|
|
case T_ENDIF:
|
|
|
|
case T_IF:
|
2021-01-07 09:48:07 -08:00
|
|
|
case T_IFDEF:
|
2021-01-07 09:48:08 -08:00
|
|
|
case T_IFNDEF:
|
2021-01-07 09:48:06 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (!preproc_is_writing(ctx))
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-21 12:37:06 -08:00
|
|
|
|
|
|
|
if (ctx->current_directive)
|
|
|
|
return return_token(token, lval, text);
|
2020-12-07 10:56:30 -08:00
|
|
|
|
|
|
|
vkd3d_string_buffer_printf(&ctx->buffer, "%s ", text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 09:48:06 -08:00
|
|
|
static int preproc_macro_compare(const void *key, const struct rb_entry *entry)
|
|
|
|
{
|
|
|
|
const struct preproc_macro *macro = RB_ENTRY_VALUE(entry, struct preproc_macro, entry);
|
|
|
|
const char *name = key;
|
|
|
|
|
|
|
|
return strcmp(name, macro->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void preproc_macro_rb_free(struct rb_entry *entry, void *ctx)
|
|
|
|
{
|
|
|
|
preproc_free_macro(RB_ENTRY_VALUE(entry, struct preproc_macro, entry));
|
|
|
|
}
|
|
|
|
|
2020-12-07 10:56:30 -08:00
|
|
|
int preproc_lexer_parse(const struct vkd3d_shader_compile_info *compile_info,
|
|
|
|
struct vkd3d_shader_code *out, struct vkd3d_shader_message_context *message_context)
|
|
|
|
{
|
|
|
|
struct preproc_ctx ctx = {0};
|
|
|
|
YY_BUFFER_STATE top_buffer;
|
|
|
|
void *output_code;
|
|
|
|
|
|
|
|
vkd3d_string_buffer_init(&ctx.buffer);
|
2021-01-07 09:48:06 -08:00
|
|
|
rb_init(&ctx.macros, preproc_macro_compare);
|
2020-12-15 15:13:20 -08:00
|
|
|
ctx.message_context = message_context;
|
|
|
|
ctx.location.source_name = compile_info->source_name;
|
|
|
|
ctx.location.line = 1;
|
|
|
|
ctx.location.column = 1;
|
2020-12-07 10:56:30 -08:00
|
|
|
|
|
|
|
yylex_init_extra(&ctx, &ctx.scanner);
|
|
|
|
top_buffer = yy_scan_bytes(compile_info->source.code, compile_info->source.size, ctx.scanner);
|
2020-12-21 12:37:06 -08:00
|
|
|
ctx.last_was_newline = true;
|
2020-12-07 10:56:30 -08:00
|
|
|
|
|
|
|
preproc_yyparse(ctx.scanner, &ctx);
|
|
|
|
|
|
|
|
yy_delete_buffer(top_buffer, ctx.scanner);
|
|
|
|
yylex_destroy(ctx.scanner);
|
|
|
|
|
2020-12-21 12:37:06 -08:00
|
|
|
if (ctx.if_count)
|
|
|
|
{
|
|
|
|
const struct vkd3d_shader_location loc = {.source_name = ctx.location.source_name};
|
|
|
|
|
|
|
|
preproc_warning(&ctx, &loc, VKD3D_SHADER_WARNING_PP_UNTERMINATED_IF, "Unterminated #if block.");
|
|
|
|
}
|
|
|
|
|
|
|
|
vkd3d_free(ctx.if_stack);
|
2021-01-07 09:48:06 -08:00
|
|
|
rb_destroy(&ctx.macros, preproc_macro_rb_free, NULL);
|
2020-12-21 12:37:06 -08:00
|
|
|
|
2020-12-15 15:13:20 -08:00
|
|
|
if (ctx.error)
|
|
|
|
{
|
|
|
|
WARN("Failed to preprocess.\n");
|
|
|
|
vkd3d_string_buffer_cleanup(&ctx.buffer);
|
|
|
|
return VKD3D_ERROR_INVALID_SHADER;
|
|
|
|
}
|
|
|
|
|
2020-12-07 10:56:30 -08:00
|
|
|
if (!(output_code = vkd3d_malloc(ctx.buffer.content_size)))
|
|
|
|
{
|
|
|
|
vkd3d_string_buffer_cleanup(&ctx.buffer);
|
|
|
|
return VKD3D_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(output_code, ctx.buffer.buffer, ctx.buffer.content_size);
|
|
|
|
out->size = ctx.buffer.content_size;
|
|
|
|
out->code = output_code;
|
|
|
|
vkd3d_string_buffer_trace(&ctx.buffer);
|
|
|
|
vkd3d_string_buffer_cleanup(&ctx.buffer);
|
|
|
|
return VKD3D_OK;
|
|
|
|
}
|