Revert "util/bincfg: code cleanup: convert sym_table to a local variable"

This reverts commit 48c24ce5ee.

Reason for revert: Commit broke bincfg, and sym_table as a global
variable is less bad than passing it around in function calls.

Change-Id: Ib8d64a1dc201d17a4e278ab0114958b6807a45ac
Signed-off-by: Angel Pons <th3fanbus@gmail.com>
Reviewed-on: https://review.coreboot.org/c/31105
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
This commit is contained in:
Angel Pons 2019-01-26 10:09:13 +00:00 committed by Patrick Georgi
parent 3f7fd58823
commit 175778f059
2 changed files with 41 additions and 61 deletions

View File

@ -37,12 +37,13 @@ struct blob {
unsigned int lenactualblob; unsigned int lenactualblob;
}; };
static struct field *putsym(field_ptr_t, char const *, unsigned int); static struct field *putsym (char const *, unsigned int);
static struct field *getsym(field_ptr_t, char const *); static struct field *getsym (char const *);
static void yyerror(FILE *, field_ptr_t, char const *); static void yyerror (FILE* fp, char const *);
int yylex(void); int yylex (void);
static struct blob *binary; static struct blob *binary;
static struct field *sym_table;
static struct field *sym_table_tail; static struct field *sym_table_tail;
#endif /* __BINCFG_H */ #endif /* __BINCFG_H */

View File

@ -59,12 +59,10 @@ static void append_field_to_blob (unsigned char b[], unsigned int w)
binary->bloblen += w; binary->bloblen += w;
} }
static void set_bitfield(struct field **sym_table_ptr, char *name, static void set_bitfield(char *name, unsigned int value)
unsigned int value)
{ {
unsigned long long i; unsigned long long i;
struct field *bf = getsym (name);
struct field *bf = getsym(sym_table_ptr, name);
if (bf) { if (bf) {
bf->value = value & 0xffffffff; bf->value = value & 0xffffffff;
i = (1 << bf->width) - 1; i = (1 << bf->width) - 1;
@ -80,30 +78,28 @@ static void set_bitfield(struct field **sym_table_ptr, char *name,
} }
} }
static void set_bitfield_array(struct field **sym_table_ptr, char *name, static void set_bitfield_array(char *name, unsigned int n, unsigned int value)
unsigned int n, unsigned int value)
{ {
unsigned int i; unsigned int i;
unsigned long len = strlen (name); unsigned long len = strlen (name);
char *namen = (char *) malloc ((len + 9) * sizeof (char)); char *namen = (char *) malloc ((len + 9) * sizeof (char));
check_pointer(namen); check_pointer(namen);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
snprintf (namen, len + 8, "%s%x", name, i); snprintf (namen, len + 8, "%s%x", name, i);
set_bitfield (sym_table_ptr, namen, value); set_bitfield (namen, value);
} }
free(namen); free(namen);
} }
static void create_new_bitfield(struct field **sym_table_ptr, char *name, unsigned int width) static void create_new_bitfield(char *name, unsigned int width)
{ {
struct field *bf; struct field *bf;
if (!(bf = putsym (sym_table_ptr, name, width))) return; if (!(bf = putsym (name, width))) return;
//fprintf(stderr, "Added bitfield `%s` : %d\n", bf->name, width); //fprintf(stderr, "Added bitfield `%s` : %d\n", bf->name, width);
} }
static void create_new_bitfields(struct field **sym_table_ptr, char *name, unsigned int n, unsigned int width) static void create_new_bitfields(char *name, unsigned int n, unsigned int width)
{ {
unsigned int i; unsigned int i;
unsigned long len = strlen (name); unsigned long len = strlen (name);
@ -111,41 +107,38 @@ static void create_new_bitfields(struct field **sym_table_ptr, char *name, unsig
check_pointer(namen); check_pointer(namen);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
snprintf (namen, len + 8, "%s%x", name, i); snprintf (namen, len + 8, "%s%x", name, i);
create_new_bitfield (sym_table_ptr, namen, width); create_new_bitfield (namen, width);
} }
free(namen); free(namen);
} }
static struct field *putsym (struct field **sym_table_ptr, char const *sym_name, static struct field *putsym (char const *sym_name, unsigned int w)
unsigned int w)
{ {
if (getsym(sym_table_ptr, sym_name)) { if (getsym(sym_name)) {
fprintf(stderr, "Cannot add duplicate named bitfield `%s`\n", fprintf(stderr, "Cannot add duplicate named bitfield `%s`\n",
sym_name); sym_name);
return 0; return 0;
} }
struct field *ptr = (field_t ) malloc (sizeof (struct field)); struct field *ptr = (struct field *) malloc (sizeof (struct field));
check_pointer(ptr); check_pointer(ptr);
ptr->name = (char *) malloc (strlen (sym_name) + 1); ptr->name = (char *) malloc (strlen (sym_name) + 1);
check_pointer(ptr->name); check_pointer(ptr->name);
strcpy (ptr->name, sym_name); strcpy (ptr->name, sym_name);
ptr->width = w; ptr->width = w;
ptr->value = 0; ptr->value = 0;
ptr->next = (field_t)0; ptr->next = (struct field *)0;
if (sym_table_tail) { if (sym_table_tail) {
sym_table_tail->next = ptr; sym_table_tail->next = ptr;
} else { } else {
*sym_table_ptr = ptr; sym_table = ptr;
} }
sym_table_tail = ptr; sym_table_tail = ptr;
return ptr; return ptr;
} }
static struct field *getsym (struct field **sym_table_ptr, char const *sym_name) static struct field *getsym (char const *sym_name)
{ {
struct field *ptr; struct field *ptr;
struct field *sym_table = *sym_table_ptr;
for (ptr = sym_table; ptr != (struct field *) 0; for (ptr = sym_table; ptr != (struct field *) 0;
ptr = (struct field *)ptr->next) { ptr = (struct field *)ptr->next) {
if (strcmp (ptr->name, sym_name) == 0) if (strcmp (ptr->name, sym_name) == 0)
@ -154,11 +147,9 @@ static struct field *getsym (struct field **sym_table_ptr, char const *sym_name)
return 0; return 0;
} }
static void dump_all_values (struct field **sym_table_ptr) static void dump_all_values (void)
{ {
struct field *ptr; struct field *ptr;
struct field *sym_table = *sym_table_ptr;
for (ptr = sym_table; ptr != (struct field *) 0; for (ptr = sym_table; ptr != (struct field *) 0;
ptr = (struct field *)ptr->next) { ptr = (struct field *)ptr->next) {
fprintf(stderr, "%s = %d (%d bits)\n", fprintf(stderr, "%s = %d (%d bits)\n",
@ -168,13 +159,11 @@ static void dump_all_values (struct field **sym_table_ptr)
} }
} }
static void empty_field_table(struct field **sym_table_ptr) static void empty_field_table(void)
{ {
struct field *ptr; struct field *ptr;
struct field *ptrnext; struct field *ptrnext;
struct field *sym_table = *sym_table_ptr;
for (ptr = sym_table; ptr != (struct field *) 0; ptr = ptrnext) { for (ptr = sym_table; ptr != (struct field *) 0; ptr = ptrnext) {
if (ptr) { if (ptr) {
ptrnext = ptr->next; ptrnext = ptr->next;
@ -219,12 +208,10 @@ static void interpret_next_blob_value (struct field *f)
} }
/* {}%BIN -> {} */ /* {}%BIN -> {} */
static void generate_setter_bitfields(FILE* fp, struct field **sym_table_ptr, static void generate_setter_bitfields(FILE* fp, unsigned char *bin)
unsigned char *bin)
{ {
unsigned int i; unsigned int i;
struct field *ptr; struct field *ptr;
struct field *sym_table = *sym_table_ptr;
/* Convert bytes to bit array */ /* Convert bytes to bit array */
for (i = 0; i < binary->lenactualblob; i++) { for (i = 0; i < binary->lenactualblob; i++) {
@ -246,14 +233,11 @@ static void generate_setter_bitfields(FILE* fp, struct field **sym_table_ptr,
fprintf (fp, "\n}\n"); fprintf (fp, "\n}\n");
} }
static void generate_binary_with_gbe_checksum(FILE* fp, static void generate_binary_with_gbe_checksum(FILE* fp)
struct field **sym_table_ptr)
{ {
int i; int i;
unsigned short checksum; unsigned short checksum;
struct field *sym_table = *sym_table_ptr;
/* traverse spec, push to blob and add up for checksum */ /* traverse spec, push to blob and add up for checksum */
struct field *ptr; struct field *ptr;
unsigned int uptochksum = 0; unsigned int uptochksum = 0;
@ -293,7 +277,7 @@ static void generate_binary_with_gbe_checksum(FILE* fp,
checksum = (0xbaba - binary->checksum) & 0xffff; checksum = (0xbaba - binary->checksum) & 0xffff;
/* Now write checksum */ /* Now write checksum */
set_bitfield (sym_table_ptr, "checksum_gbe", checksum); set_bitfield ("checksum_gbe", checksum);
fprintf(fp, "%c", checksum & 0xff); fprintf(fp, "%c", checksum & 0xff);
fprintf(fp, "%c", (checksum & 0xff00) >> 8); fprintf(fp, "%c", (checksum & 0xff00) >> 8);
@ -321,11 +305,10 @@ static void generate_binary_with_gbe_checksum(FILE* fp,
} }
/* {}{} -> BIN */ /* {}{} -> BIN */
static void generate_binary(FILE* fp, struct field **sym_table_ptr) static void generate_binary(FILE* fp)
{ {
unsigned int i; unsigned int i;
struct field *ptr; struct field *ptr;
struct field *sym_table = *sym_table_ptr;
if (binary->bloblen % 8) { if (binary->bloblen % 8) {
fprintf (stderr, fprintf (stderr,
@ -333,8 +316,8 @@ static void generate_binary(FILE* fp, struct field **sym_table_ptr)
exit (1); exit (1);
} }
if (getsym (sym_table_ptr, "checksum_gbe")) { if (getsym ("checksum_gbe")) {
generate_binary_with_gbe_checksum(fp, sym_table_ptr); generate_binary_with_gbe_checksum(fp);
return; return;
} }
@ -370,7 +353,7 @@ static void generate_binary(FILE* fp, struct field **sym_table_ptr)
unsigned char u8; unsigned char u8;
unsigned char *u8array; unsigned char *u8array;
} }
%parse-param {FILE* fp} {struct field** sym_table_ptr} %parse-param {FILE* fp}
%token <str> name %token <str> name
%token <u32> val %token <u32> val
@ -389,13 +372,13 @@ static void generate_binary(FILE* fp, struct field **sym_table_ptr)
input: input:
/* empty */ /* empty */
| input spec setter eof { empty_field_table(sym_table_ptr); YYACCEPT;} | input spec setter eof { empty_field_table(); YYACCEPT;}
| input spec blob { fprintf (stderr, "Parsed all bytes\n"); | input spec blob { fprintf (stderr, "Parsed all bytes\n");
empty_field_table(sym_table_ptr); YYACCEPT;} empty_field_table(); YYACCEPT;}
; ;
blob: blob:
'%' eof { generate_setter_bitfields(fp, sym_table_ptr, '%' eof { generate_setter_bitfields(fp,
binary->actualblob); } binary->actualblob); }
; ;
@ -411,15 +394,14 @@ specmembers:
; ;
specpair: specpair:
name ':' val { create_new_bitfield(sym_table_ptr, $1, $3); } name ':' val { create_new_bitfield($1, $3); }
| name '[' val ']' ':' val { create_new_bitfields(sym_table_ptr, $1, $3, | name '[' val ']' ':' val { create_new_bitfields($1, $3, $6); }
$6); }
; ;
setter: setter:
'{' '}' { fprintf (stderr, "No values\n"); } '{' '}' { fprintf (stderr, "No values\n"); }
| '{' valuemembers '}' { fprintf (stderr, "Parsed all values\n"); | '{' valuemembers '}' { fprintf (stderr, "Parsed all values\n");
generate_binary(fp, sym_table_ptr); } generate_binary(fp); }
; ;
valuemembers: valuemembers:
@ -428,15 +410,14 @@ valuemembers:
; ;
setpair: setpair:
name '=' val { set_bitfield(sym_table_ptr, $1, $3); } name '=' val { set_bitfield($1, $3); }
| name '[' val ']' '=' val { set_bitfield_array(sym_table_ptr, $1, $3, | name '[' val ']' '=' val { set_bitfield_array($1, $3, $6); }
$6); }
; ;
%% %%
/* Called by yyparse on error. */ /* Called by yyparse on error. */
static void yyerror (FILE* fp, struct field **sym_table_ptr, char const *s) static void yyerror (FILE* fp, char const *s)
{ {
fprintf (stderr, "yyerror: %s\n", s); fprintf (stderr, "yyerror: %s\n", s);
} }
@ -445,10 +426,9 @@ static void yyerror (FILE* fp, struct field **sym_table_ptr, char const *s)
void set_input_string(char* in); void set_input_string(char* in);
/* This function parses a string */ /* This function parses a string */
static int parse_string(FILE* fp, struct field **sym_table_ptr, unsigned char* in) static int parse_string(FILE* fp, unsigned char* in) {
{
set_input_string ((char *)in); set_input_string ((char *)in);
return yyparse (fp, sym_table_ptr); return yyparse (fp);
} }
static unsigned int loadfile (FILE* fp, char *file, char *filetype, static unsigned int loadfile (FILE* fp, char *file, char *filetype,
@ -484,7 +464,6 @@ int main (int argc, char *argv[])
unsigned int pos = 0; unsigned int pos = 0;
int ret = 0; int ret = 0;
FILE* fp; FILE* fp;
struct field *sym_table;
#if YYDEBUG == 1 #if YYDEBUG == 1
yydebug = 1; yydebug = 1;
@ -505,7 +484,7 @@ int main (int argc, char *argv[])
argv[3]); argv[3]);
exit(1); exit(1);
} }
ret = parse_string(fp, &sym_table, parsestring); ret = parse_string(fp, parsestring);
free(parsestring); free(parsestring);
} else if (argc == 5 && strcmp (argv[1], "-d") == 0) { } else if (argc == 5 && strcmp (argv[1], "-d") == 0) {
/* Decompile mode */ /* Decompile mode */
@ -537,7 +516,7 @@ int main (int argc, char *argv[])
argv[4]); argv[4]);
exit(1); exit(1);
} }
ret = parse_string(fp, &sym_table, parsestring); ret = parse_string(fp, parsestring);
free(parsestring); free(parsestring);
free(binary->actualblob); free(binary->actualblob);
fclose(fp); fclose(fp);