From: Peter Johnson Date: Sun, 16 Sep 2001 04:49:46 +0000 (-0000) Subject: Use queue.h macros in section.h, and reference bytecode using bytecodehead. X-Git-Tag: v0.1.0~355 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=84e0411c490186e81d7e8114595ea02d0f92da35;p=yasm Use queue.h macros in section.h, and reference bytecode using bytecodehead. Remove old "append" function from bytecode.h Add generic parser functions, clean up parser struct. svn path=/trunk/yasm/; revision=157 --- diff --git a/frontends/yasm/yasm.c b/frontends/yasm/yasm.c index 0ecbe5b1..25ea8f32 100644 --- a/frontends/yasm/yasm.c +++ b/frontends/yasm/yasm.c @@ -1,4 +1,4 @@ -/* $Id: yasm.c,v 1.11 2001/09/15 07:16:59 peter Exp $ +/* $Id: yasm.c,v 1.12 2001/09/16 04:49:46 peter Exp $ * Program entry point, command line parsing * * Copyright (C) 2001 Peter Johnson @@ -38,7 +38,7 @@ #include "preproc.h" #include "parser.h" -RCSID("$Id: yasm.c,v 1.11 2001/09/15 07:16:59 peter Exp $"); +RCSID("$Id: yasm.c,v 1.12 2001/09/16 04:49:46 peter Exp $"); char *filename = (char *)NULL; unsigned int line_number = 1; @@ -61,7 +61,7 @@ main(int argc, char *argv[]) filename = strdup(""); } - nasm_parser.doparse(&raw_preproc, &dbg_objfmt, in); + nasm_parser.doparse(&nasm_parser, &dbg_objfmt, in); if (filename) free(filename); diff --git a/libyasm/bytecode.h b/libyasm/bytecode.h index bb69f8db..95af9787 100644 --- a/libyasm/bytecode.h +++ b/libyasm/bytecode.h @@ -1,4 +1,4 @@ -/* $Id: bytecode.h,v 1.17 2001/08/19 05:41:01 peter Exp $ +/* $Id: bytecode.h,v 1.18 2001/09/16 04:49:46 peter Exp $ * Bytecode utility functions header file * * Copyright (C) 2001 Peter Johnson @@ -75,6 +75,8 @@ typedef struct targetval_s { jmprel_opcode_sel op_sel; } targetval; +typedef STAILQ_HEAD(bytecodehead_s, bytecode_s) bytecodehead; + typedef struct bytecode_s { STAILQ_ENTRY(bytecode_s) link; @@ -188,8 +190,6 @@ dataval *dataval_new_expr(struct expr_s *exp); dataval *dataval_new_float(double float_val); dataval *dataval_new_string(char *str_val); -dataval *dataval_append(dataval *list, dataval *item); - void dataval_print(datavalhead *head); #endif diff --git a/libyasm/parser.h b/libyasm/parser.h index affff6fd..de2f75f0 100644 --- a/libyasm/parser.h +++ b/libyasm/parser.h @@ -1,4 +1,4 @@ -/* $Id: parser.h,v 1.3 2001/09/15 07:16:59 peter Exp $ +/* $Id: parser.h,v 1.4 2001/09/16 04:49:46 peter Exp $ * Parser module interface header file * * Copyright (C) 2001 Peter Johnson @@ -36,14 +36,14 @@ typedef struct parser_s { */ preproc **preprocs; - /* Default preprocessor (set even if there's only one available to use) */ - preproc *default_pp; + /* Current preprocessor (set to the default at compile time) */ + preproc *current_pp; /* Main entrance point for the parser. * * The parser needs access to both the object format module (for format- - * specific directives and segment names), and the selected preprocessor - * (which should naturally be in the preprocs list above). + * specific directives and segment names), and the preprocessor + * (it uses current_pp as set either at compile-time or by parser_setpp). * * This function also takes the FILE * to the initial starting file, but * not the filename (which is in a global variable and is not @@ -51,13 +51,32 @@ typedef struct parser_s { * * This function returns the starting section of a linked list of sections * (whatever was in the file). - * - * Note that calling this has many side effects in the output format - * module: sections and variables are declared, etc. */ - section *(*doparse) (preproc *pp, objfmt *of, FILE *f); + sectionhead *(*doparse) (struct parser_s *p, objfmt *of, FILE *f); } parser; +/* Generic functions for all parsers - implemented in src/parser.c */ + +/* Sets current_pp within p by searching the preprocs list for a preproc + * matching pp_keyword. Returns nonzero if no match was found. + */ +int parser_setpp(parser *p, const char *pp_keyword); + +/* Lists preprocessors available for p. Calls printfunc with the name + * and keyword of each available preprocessor. + */ +void parser_listpp(parser *p, + void (*printfunc) (const char *name, const char *keyword)); + +/* Finds a parser based on its keyword. Returns NULL if no match was found. + */ +parser *find_parser(const char *keyword); + +/* Lists all available parsers. Calls printfunc with the name and keyword + * of each available parser. + */ +void list_parsers(void (*printfunc) (const char *name, const char *keyword)); + /* Available parsers */ extern parser nasm_parser; diff --git a/libyasm/section.h b/libyasm/section.h index fcfb1f13..43efd819 100644 --- a/libyasm/section.h +++ b/libyasm/section.h @@ -1,4 +1,4 @@ -/* $Id: section.h,v 1.5 2001/08/19 03:52:58 peter Exp $ +/* $Id: section.h,v 1.6 2001/09/16 04:49:46 peter Exp $ * Section header file * * Copyright (C) 2001 Peter Johnson @@ -22,8 +22,10 @@ #ifndef YASM_SECTION_H #define YASM_SECTION_H +typedef STAILQ_HEAD(sectionhead_s, section_s) sectionhead; + typedef struct section_s { - struct section_s *next; + STAILQ_ENTRY(section_s) link; enum { SECTION, ABSOLUTE } type; @@ -36,7 +38,7 @@ typedef struct section_s { unsigned long start; } data; - bytecode *bc; /* the bytecodes for the section's contents */ + bytecodehead bc; /* the bytecodes for the section's contents */ } section; #endif diff --git a/modules/parsers/nasm/nasm-parser.c b/modules/parsers/nasm/nasm-parser.c index 8f4c4370..9981e046 100644 --- a/modules/parsers/nasm/nasm-parser.c +++ b/modules/parsers/nasm/nasm-parser.c @@ -1,4 +1,4 @@ -/* $Id: nasm-parser.c,v 1.7 2001/09/15 07:16:59 peter Exp $ +/* $Id: nasm-parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $ * NASM-compatible parser * * Copyright (C) 2001 Peter Johnson @@ -33,7 +33,7 @@ #include "preproc.h" #include "parser.h" -RCSID("$Id: nasm-parser.c,v 1.7 2001/09/15 07:16:59 peter Exp $"); +RCSID("$Id: nasm-parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $"); extern FILE *nasm_parser_in; extern int nasm_parser_debug; @@ -42,12 +42,12 @@ extern int nasm_parser_parse(void); int (*nasm_parser_yyinput) (char *buf, int max_size); -static section * -doparse(preproc *pp, objfmt *of, FILE *f) +static sectionhead * +nasm_parser_doparse(parser *p, objfmt *of, FILE *f) { - pp->initialize(of, f); + p->current_pp->initialize(of, f); nasm_parser_in = f; - nasm_parser_yyinput = pp->input; + nasm_parser_yyinput = p->current_pp->input; /* only temporary */ nasm_parser_debug = 1; @@ -58,7 +58,7 @@ doparse(preproc *pp, objfmt *of, FILE *f) } /* Define valid preprocessors to use with this parser */ -static preproc *preprocs[] = { +static preproc *nasm_parser_preprocs[] = { &raw_preproc, NULL }; @@ -67,7 +67,7 @@ static preproc *preprocs[] = { parser nasm_parser = { "NASM-compatible parser", "nasm", - preprocs, + nasm_parser_preprocs, &raw_preproc, - doparse + nasm_parser_doparse }; diff --git a/modules/parsers/nasm/parser.c b/modules/parsers/nasm/parser.c index 23da8f2b..449279c6 100644 --- a/modules/parsers/nasm/parser.c +++ b/modules/parsers/nasm/parser.c @@ -1,4 +1,4 @@ -/* $Id: parser.c,v 1.7 2001/09/15 07:16:59 peter Exp $ +/* $Id: parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $ * NASM-compatible parser * * Copyright (C) 2001 Peter Johnson @@ -33,7 +33,7 @@ #include "preproc.h" #include "parser.h" -RCSID("$Id: parser.c,v 1.7 2001/09/15 07:16:59 peter Exp $"); +RCSID("$Id: parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $"); extern FILE *nasm_parser_in; extern int nasm_parser_debug; @@ -42,12 +42,12 @@ extern int nasm_parser_parse(void); int (*nasm_parser_yyinput) (char *buf, int max_size); -static section * -doparse(preproc *pp, objfmt *of, FILE *f) +static sectionhead * +nasm_parser_doparse(parser *p, objfmt *of, FILE *f) { - pp->initialize(of, f); + p->current_pp->initialize(of, f); nasm_parser_in = f; - nasm_parser_yyinput = pp->input; + nasm_parser_yyinput = p->current_pp->input; /* only temporary */ nasm_parser_debug = 1; @@ -58,7 +58,7 @@ doparse(preproc *pp, objfmt *of, FILE *f) } /* Define valid preprocessors to use with this parser */ -static preproc *preprocs[] = { +static preproc *nasm_parser_preprocs[] = { &raw_preproc, NULL }; @@ -67,7 +67,7 @@ static preproc *preprocs[] = { parser nasm_parser = { "NASM-compatible parser", "nasm", - preprocs, + nasm_parser_preprocs, &raw_preproc, - doparse + nasm_parser_doparse }; diff --git a/src/Makefile.am b/src/Makefile.am index 004f162a..fbb33816 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -19,6 +19,7 @@ yasm_SOURCES = \ section.h \ objfmt.h \ preproc.h \ + parser.c \ parser.h yasm_LDADD = \ diff --git a/src/bytecode.h b/src/bytecode.h index bb69f8db..95af9787 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -1,4 +1,4 @@ -/* $Id: bytecode.h,v 1.17 2001/08/19 05:41:01 peter Exp $ +/* $Id: bytecode.h,v 1.18 2001/09/16 04:49:46 peter Exp $ * Bytecode utility functions header file * * Copyright (C) 2001 Peter Johnson @@ -75,6 +75,8 @@ typedef struct targetval_s { jmprel_opcode_sel op_sel; } targetval; +typedef STAILQ_HEAD(bytecodehead_s, bytecode_s) bytecodehead; + typedef struct bytecode_s { STAILQ_ENTRY(bytecode_s) link; @@ -188,8 +190,6 @@ dataval *dataval_new_expr(struct expr_s *exp); dataval *dataval_new_float(double float_val); dataval *dataval_new_string(char *str_val); -dataval *dataval_append(dataval *list, dataval *item); - void dataval_print(datavalhead *head); #endif diff --git a/src/main.c b/src/main.c index b333aa88..46526fab 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.11 2001/09/15 07:16:59 peter Exp $ +/* $Id: main.c,v 1.12 2001/09/16 04:49:46 peter Exp $ * Program entry point, command line parsing * * Copyright (C) 2001 Peter Johnson @@ -38,7 +38,7 @@ #include "preproc.h" #include "parser.h" -RCSID("$Id: main.c,v 1.11 2001/09/15 07:16:59 peter Exp $"); +RCSID("$Id: main.c,v 1.12 2001/09/16 04:49:46 peter Exp $"); char *filename = (char *)NULL; unsigned int line_number = 1; @@ -61,7 +61,7 @@ main(int argc, char *argv[]) filename = strdup(""); } - nasm_parser.doparse(&raw_preproc, &dbg_objfmt, in); + nasm_parser.doparse(&nasm_parser, &dbg_objfmt, in); if (filename) free(filename); diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 00000000..ed6a2122 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,107 @@ +/* $Id: parser.c,v 1.1 2001/09/16 04:49:46 peter Exp $ + * Generic functions for all parsers + * + * Copyright (C) 2001 Peter Johnson + * + * This file is part of YASM. + * + * YASM is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * YASM 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "util.h" + +#include + +#ifdef STDC_HEADERS +# include +# include +#endif + +#include "globals.h" +#include "errwarn.h" +#include "expr.h" + +#include "bytecode.h" +#include "section.h" +#include "objfmt.h" +#include "preproc.h" +#include "parser.h" + +RCSID("$Id: parser.c,v 1.1 2001/09/16 04:49:46 peter Exp $"); + +/* NULL-terminated list of all available parsers. + * Someday change this if we dynamically load parsers at runtime. + * Could improve this a little by generating automatically at build-time. + */ +static parser *parsers[] = { + &nasm_parser, + NULL +}; + +int +parser_setpp(parser *p, const char *pp_keyword) +{ + int i; + + /* We're just doing a linear search, as preprocs should be short */ + for (i = 0; p->preprocs[i]; i++) { + if (strcmp(p->preprocs[i]->keyword, pp_keyword) == 0) { + p->current_pp = p->preprocs[i]; + return 0; + } + } + + /* no match found */ + return 1; +} + +void +parser_listpp(parser *p, + void (*printfunc) (const char *name, const char *keyword)) +{ + int i; + + for (i = 0; p->preprocs[i]; i++) { + printfunc(p->preprocs[i]->name, p->preprocs[i]->keyword); + } +} + +parser * +find_parser(const char *keyword) +{ + int i; + + /* We're just doing a linear search, as there aren't many parsers */ + for (i = 0; parsers[i]; i++) { + if (strcmp(parsers[i]->keyword, keyword) == 0) + return parsers[i]; + } + + /* no match found */ + return NULL; +} + +void +list_parsers(void (*printfunc) (const char *name, const char *keyword)) +{ + int i; + + for (i = 0; parsers[i]; i++) { + printfunc(parsers[i]->name, parsers[i]->keyword); + } +} diff --git a/src/parser.h b/src/parser.h index affff6fd..de2f75f0 100644 --- a/src/parser.h +++ b/src/parser.h @@ -1,4 +1,4 @@ -/* $Id: parser.h,v 1.3 2001/09/15 07:16:59 peter Exp $ +/* $Id: parser.h,v 1.4 2001/09/16 04:49:46 peter Exp $ * Parser module interface header file * * Copyright (C) 2001 Peter Johnson @@ -36,14 +36,14 @@ typedef struct parser_s { */ preproc **preprocs; - /* Default preprocessor (set even if there's only one available to use) */ - preproc *default_pp; + /* Current preprocessor (set to the default at compile time) */ + preproc *current_pp; /* Main entrance point for the parser. * * The parser needs access to both the object format module (for format- - * specific directives and segment names), and the selected preprocessor - * (which should naturally be in the preprocs list above). + * specific directives and segment names), and the preprocessor + * (it uses current_pp as set either at compile-time or by parser_setpp). * * This function also takes the FILE * to the initial starting file, but * not the filename (which is in a global variable and is not @@ -51,13 +51,32 @@ typedef struct parser_s { * * This function returns the starting section of a linked list of sections * (whatever was in the file). - * - * Note that calling this has many side effects in the output format - * module: sections and variables are declared, etc. */ - section *(*doparse) (preproc *pp, objfmt *of, FILE *f); + sectionhead *(*doparse) (struct parser_s *p, objfmt *of, FILE *f); } parser; +/* Generic functions for all parsers - implemented in src/parser.c */ + +/* Sets current_pp within p by searching the preprocs list for a preproc + * matching pp_keyword. Returns nonzero if no match was found. + */ +int parser_setpp(parser *p, const char *pp_keyword); + +/* Lists preprocessors available for p. Calls printfunc with the name + * and keyword of each available preprocessor. + */ +void parser_listpp(parser *p, + void (*printfunc) (const char *name, const char *keyword)); + +/* Finds a parser based on its keyword. Returns NULL if no match was found. + */ +parser *find_parser(const char *keyword); + +/* Lists all available parsers. Calls printfunc with the name and keyword + * of each available parser. + */ +void list_parsers(void (*printfunc) (const char *name, const char *keyword)); + /* Available parsers */ extern parser nasm_parser; diff --git a/src/parsers/nasm/nasm-parser.c b/src/parsers/nasm/nasm-parser.c index 8f4c4370..9981e046 100644 --- a/src/parsers/nasm/nasm-parser.c +++ b/src/parsers/nasm/nasm-parser.c @@ -1,4 +1,4 @@ -/* $Id: nasm-parser.c,v 1.7 2001/09/15 07:16:59 peter Exp $ +/* $Id: nasm-parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $ * NASM-compatible parser * * Copyright (C) 2001 Peter Johnson @@ -33,7 +33,7 @@ #include "preproc.h" #include "parser.h" -RCSID("$Id: nasm-parser.c,v 1.7 2001/09/15 07:16:59 peter Exp $"); +RCSID("$Id: nasm-parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $"); extern FILE *nasm_parser_in; extern int nasm_parser_debug; @@ -42,12 +42,12 @@ extern int nasm_parser_parse(void); int (*nasm_parser_yyinput) (char *buf, int max_size); -static section * -doparse(preproc *pp, objfmt *of, FILE *f) +static sectionhead * +nasm_parser_doparse(parser *p, objfmt *of, FILE *f) { - pp->initialize(of, f); + p->current_pp->initialize(of, f); nasm_parser_in = f; - nasm_parser_yyinput = pp->input; + nasm_parser_yyinput = p->current_pp->input; /* only temporary */ nasm_parser_debug = 1; @@ -58,7 +58,7 @@ doparse(preproc *pp, objfmt *of, FILE *f) } /* Define valid preprocessors to use with this parser */ -static preproc *preprocs[] = { +static preproc *nasm_parser_preprocs[] = { &raw_preproc, NULL }; @@ -67,7 +67,7 @@ static preproc *preprocs[] = { parser nasm_parser = { "NASM-compatible parser", "nasm", - preprocs, + nasm_parser_preprocs, &raw_preproc, - doparse + nasm_parser_doparse }; diff --git a/src/parsers/nasm/parser.c b/src/parsers/nasm/parser.c index 23da8f2b..449279c6 100644 --- a/src/parsers/nasm/parser.c +++ b/src/parsers/nasm/parser.c @@ -1,4 +1,4 @@ -/* $Id: parser.c,v 1.7 2001/09/15 07:16:59 peter Exp $ +/* $Id: parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $ * NASM-compatible parser * * Copyright (C) 2001 Peter Johnson @@ -33,7 +33,7 @@ #include "preproc.h" #include "parser.h" -RCSID("$Id: parser.c,v 1.7 2001/09/15 07:16:59 peter Exp $"); +RCSID("$Id: parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $"); extern FILE *nasm_parser_in; extern int nasm_parser_debug; @@ -42,12 +42,12 @@ extern int nasm_parser_parse(void); int (*nasm_parser_yyinput) (char *buf, int max_size); -static section * -doparse(preproc *pp, objfmt *of, FILE *f) +static sectionhead * +nasm_parser_doparse(parser *p, objfmt *of, FILE *f) { - pp->initialize(of, f); + p->current_pp->initialize(of, f); nasm_parser_in = f; - nasm_parser_yyinput = pp->input; + nasm_parser_yyinput = p->current_pp->input; /* only temporary */ nasm_parser_debug = 1; @@ -58,7 +58,7 @@ doparse(preproc *pp, objfmt *of, FILE *f) } /* Define valid preprocessors to use with this parser */ -static preproc *preprocs[] = { +static preproc *nasm_parser_preprocs[] = { &raw_preproc, NULL }; @@ -67,7 +67,7 @@ static preproc *preprocs[] = { parser nasm_parser = { "NASM-compatible parser", "nasm", - preprocs, + nasm_parser_preprocs, &raw_preproc, - doparse + nasm_parser_doparse }; diff --git a/src/section.h b/src/section.h index fcfb1f13..43efd819 100644 --- a/src/section.h +++ b/src/section.h @@ -1,4 +1,4 @@ -/* $Id: section.h,v 1.5 2001/08/19 03:52:58 peter Exp $ +/* $Id: section.h,v 1.6 2001/09/16 04:49:46 peter Exp $ * Section header file * * Copyright (C) 2001 Peter Johnson @@ -22,8 +22,10 @@ #ifndef YASM_SECTION_H #define YASM_SECTION_H +typedef STAILQ_HEAD(sectionhead_s, section_s) sectionhead; + typedef struct section_s { - struct section_s *next; + STAILQ_ENTRY(section_s) link; enum { SECTION, ABSOLUTE } type; @@ -36,7 +38,7 @@ typedef struct section_s { unsigned long start; } data; - bytecode *bc; /* the bytecodes for the section's contents */ + bytecodehead bc; /* the bytecodes for the section's contents */ } section; #endif