]> granicus.if.org Git - yasm/commitdiff
Use queue.h macros in section.h, and reference bytecode using bytecodehead.
authorPeter Johnson <peter@tortall.net>
Sun, 16 Sep 2001 04:49:46 +0000 (04:49 -0000)
committerPeter Johnson <peter@tortall.net>
Sun, 16 Sep 2001 04:49:46 +0000 (04:49 -0000)
Remove old "append" function from bytecode.h
Add generic parser functions, clean up parser struct.

svn path=/trunk/yasm/; revision=157

14 files changed:
frontends/yasm/yasm.c
libyasm/bytecode.h
libyasm/parser.h
libyasm/section.h
modules/parsers/nasm/nasm-parser.c
modules/parsers/nasm/parser.c
src/Makefile.am
src/bytecode.h
src/main.c
src/parser.c [new file with mode: 0644]
src/parser.h
src/parsers/nasm/nasm-parser.c
src/parsers/nasm/parser.c
src/section.h

index 0ecbe5b16b1a42e9ada850dc750c76cbab643037..25ea8f32b815a692f46dde977369294f4c95b000 100644 (file)
@@ -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("<STDIN>");
     }
 
-    nasm_parser.doparse(&raw_preproc, &dbg_objfmt, in);
+    nasm_parser.doparse(&nasm_parser, &dbg_objfmt, in);
 
     if (filename)
        free(filename);
index bb69f8db5e59d1eb8f6801809c349504f5d4ad49..95af9787edc52e5231645136324f998bd4b54240 100644 (file)
@@ -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
index affff6fd3a512539b9b316e40af0b91d56c4c95e..de2f75f0153d8b8a1c7eb0a16e88e14cebf3bfaf 100644 (file)
@@ -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;
 
index fcfb1f13e38eec110a4a1e26e911f6b497d420f7..43efd819ffabc7ff0fef344b9ec98bbd87214daf 100644 (file)
@@ -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
 #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
index 8f4c43707f6d616997997addcfb441eb1417d442..9981e046cfff3c5aeacd014d327f3e72ed99675b 100644 (file)
@@ -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
 };
index 23da8f2be9d5208f3c8a1c92ec33e4e257e20e1b..449279c65994f34ed5de0f471d3cdb99570cc9c4 100644 (file)
@@ -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
 };
index 004f162a35349babc319d1348ed77a2b443e50e5..fbb3381683f8ccd7dc6095e6a29077e847072072 100644 (file)
@@ -19,6 +19,7 @@ yasm_SOURCES = \
        section.h               \
        objfmt.h                \
        preproc.h               \
+       parser.c                \
        parser.h
 
 yasm_LDADD = \
index bb69f8db5e59d1eb8f6801809c349504f5d4ad49..95af9787edc52e5231645136324f998bd4b54240 100644 (file)
@@ -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
index b333aa88351cf1f79535e855e34f386fd2153f17..46526fab03b5c9b0035c6aee3bd752003d3cd572 100644 (file)
@@ -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("<STDIN>");
     }
 
-    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 (file)
index 0000000..ed6a212
--- /dev/null
@@ -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 <stdio.h>
+
+#ifdef STDC_HEADERS
+# include <stdlib.h>
+# include <string.h>
+#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);
+    }
+}
index affff6fd3a512539b9b316e40af0b91d56c4c95e..de2f75f0153d8b8a1c7eb0a16e88e14cebf3bfaf 100644 (file)
@@ -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;
 
index 8f4c43707f6d616997997addcfb441eb1417d442..9981e046cfff3c5aeacd014d327f3e72ed99675b 100644 (file)
@@ -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
 };
index 23da8f2be9d5208f3c8a1c92ec33e4e257e20e1b..449279c65994f34ed5de0f471d3cdb99570cc9c4 100644 (file)
@@ -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
 };
index fcfb1f13e38eec110a4a1e26e911f6b497d420f7..43efd819ffabc7ff0fef344b9ec98bbd87214daf 100644 (file)
@@ -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
 #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