brw: Create a struct to hold parser state

Hold most of the parser data.  Remaining will be moved
in follow-up patches.  The struct itself is still a
global for now.

Reviewed-by: Sagar Ghuge <sagar.ghuge@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/39363>
This commit is contained in:
Caio Oliveira 2026-01-15 14:15:09 -08:00 committed by Marge Bot
parent 97930015d8
commit a5fac4e084
4 changed files with 40 additions and 38 deletions

View file

@ -13,15 +13,8 @@
extern FILE *yyin;
struct brw_asm_parser *parser;
struct brw_codegen *p;
const char *input_filename;
int errors;
bool compaction_warning_given;
/*
* Label tracking.
*/
static struct hash_table *brw_asm_labels;
typedef struct {
char *name;
@ -35,15 +28,15 @@ brw_asm_label_lookup(const char *name)
{
uint32_t h = _mesa_hash_string(name);
struct hash_entry *entry =
_mesa_hash_table_search_pre_hashed(brw_asm_labels, h, name);
_mesa_hash_table_search_pre_hashed(parser->labels, h, name);
if (!entry) {
void *mem_ctx = brw_asm_labels;
void *mem_ctx = parser->labels;
brw_asm_label *label = rzalloc(mem_ctx, brw_asm_label);
label->name = ralloc_strdup(mem_ctx, name);
label->offset = -1;
util_dynarray_init(&label->jip_uses, mem_ctx);
util_dynarray_init(&label->uip_uses, mem_ctx);
entry = _mesa_hash_table_insert_pre_hashed(brw_asm_labels,
entry = _mesa_hash_table_insert_pre_hashed(parser->labels,
h, name, label);
}
assert(entry);
@ -83,7 +76,7 @@ brw_postprocess_labels()
unsigned unknown = 0;
void *store = p->store;
hash_table_foreach(brw_asm_labels, entry) {
hash_table_foreach(parser->labels, entry) {
brw_asm_label *label = entry->data;
if (label->offset == -1) {
@ -114,8 +107,10 @@ brw_assemble(void *mem_ctx, const struct intel_device_info *devinfo,
{
brw_assemble_result result = {0};
void *tmp_ctx = ralloc_context(mem_ctx);
brw_asm_labels = _mesa_string_hash_table_create(tmp_ctx);
assert(parser == NULL);
parser = rzalloc(mem_ctx, brw_asm_parser);
parser->devinfo = devinfo;
parser->labels = _mesa_string_hash_table_create(parser);
struct brw_isa_info isa;
brw_init_isa_info(&isa, devinfo);
@ -124,11 +119,11 @@ brw_assemble(void *mem_ctx, const struct intel_device_info *devinfo,
brw_init_codegen(&isa, p, p);
yyin = f;
input_filename = filename;
parser->input_filename = filename;
compaction_warning_given = false;
parser->compaction_warning_given = false;
int err = yyparse();
if (err || errors)
if (err || parser->errors)
goto end;
if (!brw_postprocess_labels())
@ -167,11 +162,10 @@ brw_assemble(void *mem_ctx, const struct intel_device_info *devinfo,
end:
/* Reset internal state. */
yyin = NULL;
input_filename = NULL;
p = NULL;
brw_asm_labels = NULL;
ralloc_free(tmp_ctx);
ralloc_free(parser);
parser = NULL;
return result;
}

View file

@ -22,14 +22,24 @@
/* glibc < 2.27 defines OVERFLOW in /usr/include/math.h. */
#undef OVERFLOW
typedef struct brw_asm_parser {
const struct intel_device_info *devinfo;
const char *input_filename;
int errors;
bool compaction_warning_given;
struct hash_table *labels;
/* Lexer state. */
int yycolumn;
int saved_state;
} brw_asm_parser;
int yyparse(void);
int yylex(void);
char *lex_text(void);
extern struct brw_asm_parser *parser;
extern struct brw_codegen *p;
extern int errors;
extern bool compaction_warning_given;
extern const char *input_filename;
struct condition {
unsigned cond_modifier:4;

View file

@ -62,11 +62,11 @@ message(enum message_level level, YYLTYPE *location,
va_list args;
if (location)
fprintf(stderr, "%s:%d:%d: %s: ", input_filename,
fprintf(stderr, "%s:%d:%d: %s: ", parser->input_filename,
location->first_line,
location->first_column, level_str[level]);
else
fprintf(stderr, "%s:%s: ", input_filename, level_str[level]);
fprintf(stderr, "%s:%s: ", parser->input_filename, level_str[level]);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
@ -606,11 +606,11 @@ add_instruction_option(struct options *options, struct instoption opt)
* assemble things later will set the flag if it decides to
* compact instructions.
*/
if (!compaction_warning_given) {
compaction_warning_given = true;
if (!parser->compaction_warning_given) {
parser->compaction_warning_given = true;
fprintf(stderr, "%s: ignoring 'compacted' "
"annotations for text assembly "
"instructions\n", input_filename);
"instructions\n", parser->input_filename);
}
break;
case ACCWREN:
@ -2228,6 +2228,6 @@ yyerror(char *msg)
#endif
{
fprintf(stderr, "%s: %d: %s at \"%s\"\n",
input_filename, yylineno, msg, lex_text());
++errors;
parser->input_filename, yylineno, msg, lex_text());
++parser->errors;
}

View file

@ -6,18 +6,16 @@
#undef ALIGN16
#include "brw_gram.tab.h"
/* Locations */
int yycolumn = 1;
int saved_state = 0;
extern const char *input_filename;
#define YY_NO_INPUT
#define YY_USER_ACTION \
yylloc.first_line = yylloc.last_line = yylineno; \
yylloc.first_column = yycolumn; \
yylloc.last_column = yycolumn + yyleng - 1; \
yycolumn += yyleng;
%}
%x BLOCK_COMMENT
@ -33,9 +31,9 @@ extern const char *input_filename;
\/\/.*[\r\n] { yycolumn = 1; }
/* eat up multiline comment */
\/\* { saved_state = YYSTATE; BEGIN(BLOCK_COMMENT); }
\/\* { parser->saved_state = YYSTATE; BEGIN(BLOCK_COMMENT); }
<BLOCK_COMMENT>\*\/ { BEGIN(saved_state); }
<BLOCK_COMMENT>\*\/ { BEGIN(parser->saved_state); }
<BLOCK_COMMENT>. { }
<BLOCK_COMMENT>[\r\n] { }
@ -44,7 +42,7 @@ extern const char *input_filename;
char *name = malloc(yyleng - 1);
memmove(name, yytext + 1, yyleng - 2);
name[yyleng-1] = '\0';
input_filename = name;
parser->input_filename = name;
}
/* null register */
@ -435,7 +433,7 @@ sr[0-9]+ { yylval.integer = atoi(yytext + 2); return STATEREG; }
. {
fprintf(stderr, "%s: %d: %s: at \"%s\"\n",
input_filename, yylineno,
parser->input_filename, yylineno,
"unexpected token", lex_text());
}
%%