]> granicus.if.org Git - yasm/commitdiff
Build section linked list, and support the section directive, including
authorPeter Johnson <peter@tortall.net>
Wed, 19 Sep 2001 07:20:02 +0000 (07:20 -0000)
committerPeter Johnson <peter@tortall.net>
Wed, 19 Sep 2001 07:20:02 +0000 (07:20 -0000)
asking the objfmt module.  Add some utility functions for both bytecode
and section to better modularize their data structure usage and common
code.  Rename doparse to do_parse to be consistent in naming.

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

27 files changed:
frontends/yasm/yasm.c
libyasm/bytecode.c
libyasm/bytecode.h
libyasm/objfmt.h
libyasm/parser.h
libyasm/section.c [new file with mode: 0644]
libyasm/section.h
modules/objfmts/dbg/dbg-objfmt.c
modules/objfmts/dbg/objfmt.c
modules/parsers/nasm/bison.y.in
modules/parsers/nasm/nasm-bison.y
modules/parsers/nasm/nasm-parser.c
modules/parsers/nasm/parser.c
src/Makefile.am
src/bytecode.c
src/bytecode.h
src/main.c
src/objfmt.h
src/objfmts/dbg/dbg-objfmt.c
src/objfmts/dbg/objfmt.c
src/parser.h
src/parsers/nasm/bison.y.in
src/parsers/nasm/nasm-bison.y
src/parsers/nasm/nasm-parser.c
src/parsers/nasm/parser.c
src/section.c [new file with mode: 0644]
src/section.h

index 440f977ee9142e6687e54db984d14926356b461d..0b55dbbe739dc57a305689a3fcea000fea3584b5 100644 (file)
@@ -59,7 +59,7 @@ main(int argc, char *argv[])
        filename = strdup("<STDIN>");
     }
 
-    nasm_parser.doparse(&nasm_parser, &dbg_objfmt, in);
+    nasm_parser.do_parse(&nasm_parser, &dbg_objfmt, in);
 
     if (filename)
        free(filename);
index e9eb0283fa3cf5c77f8bfdb27c25bbe5cd42c6b1..a9854b8729ae7fb35b1351ec004f6f3323db5579 100644 (file)
@@ -548,6 +548,17 @@ bytecode_print(bytecode *bc)
     printf("Offset=%lx BITS=%u\n", bc->offset, bc->mode_bits);
 }
 
+void
+bytecodes_append(bytecodehead *headp, bytecode *bc)
+{
+    if (bc) {
+       if (bc->type != BC_EMPTY)
+           STAILQ_INSERT_TAIL(headp, bc, link);
+       else
+           free(bc);
+    }
+}
+
 dataval *
 dataval_new_expr(expr *exp)
 {
index 5f1e45b3fbf43412877ad17a3089efe91856861d..9b45b03d18e7cb0fc11a8df29b8f6c8197e25b33 100644 (file)
@@ -183,6 +183,16 @@ bytecode *bytecode_new_reserve(struct expr_s *numitems,
 
 void bytecode_print(bytecode *bc);
 
+/* void bytecodes_initialize(bytecodehead *headp); */
+#define        bytecodes_initialize(headp)     STAILQ_INIT(headp)
+
+/* Adds bc to the list of bytecodes headp.
+ * NOTE: Does not make a copy of bc; so don't pass this function
+ * static or local variables, and discard the bc pointer after calling
+ * this function.
+ */
+void bytecodes_append(bytecodehead *headp, bytecode *bc);
+
 dataval *dataval_new_expr(struct expr_s *exp);
 dataval *dataval_new_float(double float_val);
 dataval *dataval_new_string(char *str_val);
index 24955b36846da2f7f15dbb27719a182c15b9d53a..813fc94acbe3b4d01d8573c74de118932cce82cd 100644 (file)
@@ -39,6 +39,14 @@ typedef struct objfmt_s {
      * use)
      */
 /*    struct debugfmt_s *default_df;*/
+
+    /* Get the default (starting) section name. */
+    const char *(*get_default_section_name) (void);
+
+    /* Is the specified section name valid?
+     * Return is a boolean value.
+     */
+    int (*is_valid_section) (const char *name);
 } objfmt;
 
 /* Available object formats */
index fb730830d89a0ec08e1c436ac52379dfa00db7f0..e021bdc2c16d1575d7817b31057f514b0b0fd926 100644 (file)
@@ -52,7 +52,7 @@ typedef struct parser_s {
      * This function returns the starting section of a linked list of sections
      * (whatever was in the file).
      */
-    sectionhead *(*doparse) (struct parser_s *p, objfmt *of, FILE *f);
+    sectionhead *(*do_parse) (struct parser_s *p, objfmt *of, FILE *f);
 } parser;
 
 /* Generic functions for all parsers - implemented in src/parser.c */
diff --git a/libyasm/section.c b/libyasm/section.c
new file mode 100644 (file)
index 0000000..ad760bb
--- /dev/null
@@ -0,0 +1,113 @@
+/* $IdPath$
+ * Section utility functions
+ *
+ *  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 <libintl.h>
+#define        _(String)       gettext(String)
+#ifdef gettext_noop
+#define        N_(String)      gettext_noop(String)
+#else
+#define        N_(String)      (String)
+#endif
+
+#include "globals.h"
+#include "errwarn.h"
+#include "expr.h"
+
+#include "bytecode.h"
+#include "section.h"
+#include "objfmt.h"
+
+RCSID("$IdPath$");
+
+section *
+sections_initialize(sectionhead *headp, objfmt *of)
+{
+    section *s;
+
+    /* Initialize linked list */
+    STAILQ_INIT(headp);
+
+    /* Add an initial "default" section to the list */
+    s = calloc(1, sizeof(section));
+    if (!s)
+       Fatal(FATAL_NOMEM);
+    STAILQ_INSERT_TAIL(headp, s, link);
+
+    /* Initialize default section */
+    s->type = SECTION_GENERAL;
+    s->name = strdup(of->get_default_section_name());
+    bytecodes_initialize(&s->bc);
+
+    return s;
+}
+
+section *
+sections_switch(sectionhead *headp, objfmt *of, const char *name)
+{
+    section *s, *sp;
+
+    /* Search through current sections to see if we already have one with
+     * that name.
+     */
+    s = (section *)NULL;
+    STAILQ_FOREACH(sp, headp, link) {
+       if (strcmp(sp->name, name) == 0)
+           s = sp;
+    }
+
+    if (s)
+       return s;
+
+    /* No: we have to allocate and create a new one. */
+
+    /* But first check with objfmt to see if the name is valid.
+     * If it isn't, error and just return the default (first) section.
+     */
+    if (!of->is_valid_section(name)) {
+       Error(_("Invalid section name: %s"), name);
+       return STAILQ_FIRST(headp);
+    }
+
+    /* Okay, the name is valid; now allocate and initialize */
+    s = calloc(1, sizeof(section));
+    if (!s)
+       Fatal(FATAL_NOMEM);
+    STAILQ_INSERT_TAIL(headp, s, link);
+
+    s->type = SECTION_GENERAL;
+    s->name = strdup(name);
+    bytecodes_initialize(&s->bc);
+
+    return s;
+}
index 592fb67b6d4e78d7601e171498df4e058d645f6b..71dd50b277152e229b3ded79544cb90e10f0498e 100644 (file)
 #ifndef YASM_SECTION_H
 #define YASM_SECTION_H
 
+struct objfmt_s;
+
 typedef STAILQ_HEAD(sectionhead_s, section_s) sectionhead;
 
 typedef struct section_s {
     STAILQ_ENTRY(section_s) link;
 
-    enum { SECTION_DEFAULT, SECTION_GENERAL, SECTION_ABSOLUTE } type;
+    enum { SECTION_GENERAL, SECTION_ABSOLUTE } type;
 
-    char *name;
-    unsigned int id;
+    char *name;                        /* strdup()'ed name (given by user) */
 
     union {
-       /* SECTION_DEFAULT data */
        /* SECTION_GENERAL data */
        /* SECTION_ABSOLUTE data */
        unsigned long start;
@@ -42,4 +42,9 @@ typedef struct section_s {
     bytecodehead bc;           /* the bytecodes for the section's contents */
 } section;
 
+section *sections_initialize(sectionhead *headp, struct objfmt_s *of);
+
+section *sections_switch(sectionhead *headp, struct objfmt_s *of,
+                        const char *name);
+
 #endif
index 57ffcef29ffffa5fe18419ba42d419e0335409dd..7b02785590ff3fd63acf834d4314eb6f4ffd4859 100644 (file)
 
 #include "util.h"
 
+#include <stdio.h>
+
 #include "objfmt.h"
 
 RCSID("$IdPath$");
 
+static const char *
+dbg_objfmt_get_default_section_name(void)
+{
+    fprintf(stderr, "-dbg_objfmt_get_default_section_name()\n");
+    return ".text";
+}
+
+static int
+dbg_objfmt_is_valid_section(const char *name)
+{
+    fprintf(stderr, "-dbg_objfmt_is_valid_section(\"%s\")\n", name);
+    return 1;
+}
+
 /* Define objfmt structure -- see objfmt.h for details */
 objfmt dbg_objfmt = {
     "Trace of all info passed to object format module",
-    "dbg"
+    "dbg",
+    dbg_objfmt_get_default_section_name,
+    dbg_objfmt_is_valid_section
 };
index 57ffcef29ffffa5fe18419ba42d419e0335409dd..7b02785590ff3fd63acf834d4314eb6f4ffd4859 100644 (file)
 
 #include "util.h"
 
+#include <stdio.h>
+
 #include "objfmt.h"
 
 RCSID("$IdPath$");
 
+static const char *
+dbg_objfmt_get_default_section_name(void)
+{
+    fprintf(stderr, "-dbg_objfmt_get_default_section_name()\n");
+    return ".text";
+}
+
+static int
+dbg_objfmt_is_valid_section(const char *name)
+{
+    fprintf(stderr, "-dbg_objfmt_is_valid_section(\"%s\")\n", name);
+    return 1;
+}
+
 /* Define objfmt structure -- see objfmt.h for details */
 objfmt dbg_objfmt = {
     "Trace of all info passed to object format module",
-    "dbg"
+    "dbg",
+    dbg_objfmt_get_default_section_name,
+    dbg_objfmt_is_valid_section
 };
index e13c54c7ab8d8b6bc38c806174057097553c61b9..156b56bd9d0166c5e62df056f5bfde55af52367a 100644 (file)
@@ -28,6 +28,7 @@
 
 #ifdef STDC_HEADERS
 # include <stdlib.h>
+# include <string.h>
 # include <math.h>
 #endif
 
@@ -41,6 +42,7 @@
 
 #include "bytecode.h"
 #include "section.h"
+#include "objfmt.h"
 
 RCSID("$IdPath$");
 
@@ -51,6 +53,8 @@ extern int nasm_parser_lex(void);
 static unsigned long ConvertCharConstToInt(char *);
 void nasm_parser_error(char *);
 
+extern objfmt *nasm_parser_objfmt;
+extern sectionhead nasm_parser_sections;
 extern section *nasm_parser_cur_section;
 
 %}
@@ -127,13 +131,7 @@ input: /* empty */
     | input line    {
        OutputError();
        OutputWarning();
-       if ($2) {
-           if ($2->type != BC_EMPTY) {
-               STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, $2, link);
-           } else {
-               free($2);
-           }
-       }
+       bytecodes_append(&nasm_parser_cur_section->bc, $2);
        line_number++;
     }
 ;
@@ -185,7 +183,11 @@ label_id: ID           { $$ = locallabel_base = sym_def_get($1.name, SYM_LABEL); }
 
 /* directives */
 directive: '[' DIRECTIVE_NAME DIRECTIVE_VAL ']'        {
-       printf("Directive: Name='%s' Value='%s'\n", $2, $3);
+       if (strcasecmp($2, "section") == 0)
+           nasm_parser_cur_section = sections_switch(&nasm_parser_sections,
+                                                     nasm_parser_objfmt, $3);
+       else
+           printf("Directive: Name='%s' Value='%s'\n", $2, $3);
     }
     | '[' DIRECTIVE_NAME DIRECTIVE_VAL error   {
        Error(_("missing `%c'"), ']');
index e13c54c7ab8d8b6bc38c806174057097553c61b9..156b56bd9d0166c5e62df056f5bfde55af52367a 100644 (file)
@@ -28,6 +28,7 @@
 
 #ifdef STDC_HEADERS
 # include <stdlib.h>
+# include <string.h>
 # include <math.h>
 #endif
 
@@ -41,6 +42,7 @@
 
 #include "bytecode.h"
 #include "section.h"
+#include "objfmt.h"
 
 RCSID("$IdPath$");
 
@@ -51,6 +53,8 @@ extern int nasm_parser_lex(void);
 static unsigned long ConvertCharConstToInt(char *);
 void nasm_parser_error(char *);
 
+extern objfmt *nasm_parser_objfmt;
+extern sectionhead nasm_parser_sections;
 extern section *nasm_parser_cur_section;
 
 %}
@@ -127,13 +131,7 @@ input: /* empty */
     | input line    {
        OutputError();
        OutputWarning();
-       if ($2) {
-           if ($2->type != BC_EMPTY) {
-               STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, $2, link);
-           } else {
-               free($2);
-           }
-       }
+       bytecodes_append(&nasm_parser_cur_section->bc, $2);
        line_number++;
     }
 ;
@@ -185,7 +183,11 @@ label_id: ID           { $$ = locallabel_base = sym_def_get($1.name, SYM_LABEL); }
 
 /* directives */
 directive: '[' DIRECTIVE_NAME DIRECTIVE_VAL ']'        {
-       printf("Directive: Name='%s' Value='%s'\n", $2, $3);
+       if (strcasecmp($2, "section") == 0)
+           nasm_parser_cur_section = sections_switch(&nasm_parser_sections,
+                                                     nasm_parser_objfmt, $3);
+       else
+           printf("Directive: Name='%s' Value='%s'\n", $2, $3);
     }
     | '[' DIRECTIVE_NAME DIRECTIVE_VAL error   {
        Error(_("missing `%c'"), ']');
index 05dd10759510fc5ae387bf7623aefa2b2dfd751b..2f43d0c425e6c612025e5d9402158aac8d415d2a 100644 (file)
@@ -48,28 +48,21 @@ extern int nasm_parser_parse(void);
 
 int (*nasm_parser_yyinput) (char *buf, int max_size);
 
+objfmt *nasm_parser_objfmt;
 sectionhead nasm_parser_sections;
 section *nasm_parser_cur_section;
 
 static sectionhead *
-nasm_parser_doparse(parser *p, objfmt *of, FILE *f)
+nasm_parser_do_parse(parser *p, objfmt *of, FILE *f)
 {
     p->current_pp->initialize(of, f);
     nasm_parser_in = f;
     nasm_parser_yyinput = p->current_pp->input;
 
-    /* Initialize linked list */
-    STAILQ_INIT(&nasm_parser_sections);
+    nasm_parser_objfmt = of;
 
-    /* Add an initial "default" section to the list */
-    nasm_parser_cur_section = calloc(1, sizeof(section));
-    if (!nasm_parser_cur_section)
-       Fatal(FATAL_NOMEM);
-    STAILQ_INSERT_TAIL(&nasm_parser_sections, nasm_parser_cur_section, link);
-
-    /* Initialize default section */
-    nasm_parser_cur_section->type = SECTION_DEFAULT;
-    STAILQ_INIT(&nasm_parser_cur_section->bc);
+    /* Initialize section list */
+    nasm_parser_cur_section = sections_initialize(&nasm_parser_sections, of);
 
     /* only temporary */
     nasm_parser_debug = 0;
@@ -91,5 +84,5 @@ parser nasm_parser = {
     "nasm",
     nasm_parser_preprocs,
     &raw_preproc,
-    nasm_parser_doparse
+    nasm_parser_do_parse
 };
index 05dd10759510fc5ae387bf7623aefa2b2dfd751b..2f43d0c425e6c612025e5d9402158aac8d415d2a 100644 (file)
@@ -48,28 +48,21 @@ extern int nasm_parser_parse(void);
 
 int (*nasm_parser_yyinput) (char *buf, int max_size);
 
+objfmt *nasm_parser_objfmt;
 sectionhead nasm_parser_sections;
 section *nasm_parser_cur_section;
 
 static sectionhead *
-nasm_parser_doparse(parser *p, objfmt *of, FILE *f)
+nasm_parser_do_parse(parser *p, objfmt *of, FILE *f)
 {
     p->current_pp->initialize(of, f);
     nasm_parser_in = f;
     nasm_parser_yyinput = p->current_pp->input;
 
-    /* Initialize linked list */
-    STAILQ_INIT(&nasm_parser_sections);
+    nasm_parser_objfmt = of;
 
-    /* Add an initial "default" section to the list */
-    nasm_parser_cur_section = calloc(1, sizeof(section));
-    if (!nasm_parser_cur_section)
-       Fatal(FATAL_NOMEM);
-    STAILQ_INSERT_TAIL(&nasm_parser_sections, nasm_parser_cur_section, link);
-
-    /* Initialize default section */
-    nasm_parser_cur_section->type = SECTION_DEFAULT;
-    STAILQ_INIT(&nasm_parser_cur_section->bc);
+    /* Initialize section list */
+    nasm_parser_cur_section = sections_initialize(&nasm_parser_sections, of);
 
     /* only temporary */
     nasm_parser_debug = 0;
@@ -91,5 +84,5 @@ parser nasm_parser = {
     "nasm",
     nasm_parser_preprocs,
     &raw_preproc,
-    nasm_parser_doparse
+    nasm_parser_do_parse
 };
index 36de584df8e648fc9f2057d5658dc2391a2f04e6..1c313078f027fe62cd95088407d88ca2d70a5c8d 100644 (file)
@@ -30,6 +30,7 @@ libyasm_a_SOURCES = \
        globals.c               \
        globals.h               \
        util.h                  \
+       section.c               \
        section.h               \
        objfmt.h                \
        preproc.h               \
index e9eb0283fa3cf5c77f8bfdb27c25bbe5cd42c6b1..a9854b8729ae7fb35b1351ec004f6f3323db5579 100644 (file)
@@ -548,6 +548,17 @@ bytecode_print(bytecode *bc)
     printf("Offset=%lx BITS=%u\n", bc->offset, bc->mode_bits);
 }
 
+void
+bytecodes_append(bytecodehead *headp, bytecode *bc)
+{
+    if (bc) {
+       if (bc->type != BC_EMPTY)
+           STAILQ_INSERT_TAIL(headp, bc, link);
+       else
+           free(bc);
+    }
+}
+
 dataval *
 dataval_new_expr(expr *exp)
 {
index 5f1e45b3fbf43412877ad17a3089efe91856861d..9b45b03d18e7cb0fc11a8df29b8f6c8197e25b33 100644 (file)
@@ -183,6 +183,16 @@ bytecode *bytecode_new_reserve(struct expr_s *numitems,
 
 void bytecode_print(bytecode *bc);
 
+/* void bytecodes_initialize(bytecodehead *headp); */
+#define        bytecodes_initialize(headp)     STAILQ_INIT(headp)
+
+/* Adds bc to the list of bytecodes headp.
+ * NOTE: Does not make a copy of bc; so don't pass this function
+ * static or local variables, and discard the bc pointer after calling
+ * this function.
+ */
+void bytecodes_append(bytecodehead *headp, bytecode *bc);
+
 dataval *dataval_new_expr(struct expr_s *exp);
 dataval *dataval_new_float(double float_val);
 dataval *dataval_new_string(char *str_val);
index 440f977ee9142e6687e54db984d14926356b461d..0b55dbbe739dc57a305689a3fcea000fea3584b5 100644 (file)
@@ -59,7 +59,7 @@ main(int argc, char *argv[])
        filename = strdup("<STDIN>");
     }
 
-    nasm_parser.doparse(&nasm_parser, &dbg_objfmt, in);
+    nasm_parser.do_parse(&nasm_parser, &dbg_objfmt, in);
 
     if (filename)
        free(filename);
index 24955b36846da2f7f15dbb27719a182c15b9d53a..813fc94acbe3b4d01d8573c74de118932cce82cd 100644 (file)
@@ -39,6 +39,14 @@ typedef struct objfmt_s {
      * use)
      */
 /*    struct debugfmt_s *default_df;*/
+
+    /* Get the default (starting) section name. */
+    const char *(*get_default_section_name) (void);
+
+    /* Is the specified section name valid?
+     * Return is a boolean value.
+     */
+    int (*is_valid_section) (const char *name);
 } objfmt;
 
 /* Available object formats */
index 57ffcef29ffffa5fe18419ba42d419e0335409dd..7b02785590ff3fd63acf834d4314eb6f4ffd4859 100644 (file)
 
 #include "util.h"
 
+#include <stdio.h>
+
 #include "objfmt.h"
 
 RCSID("$IdPath$");
 
+static const char *
+dbg_objfmt_get_default_section_name(void)
+{
+    fprintf(stderr, "-dbg_objfmt_get_default_section_name()\n");
+    return ".text";
+}
+
+static int
+dbg_objfmt_is_valid_section(const char *name)
+{
+    fprintf(stderr, "-dbg_objfmt_is_valid_section(\"%s\")\n", name);
+    return 1;
+}
+
 /* Define objfmt structure -- see objfmt.h for details */
 objfmt dbg_objfmt = {
     "Trace of all info passed to object format module",
-    "dbg"
+    "dbg",
+    dbg_objfmt_get_default_section_name,
+    dbg_objfmt_is_valid_section
 };
index 57ffcef29ffffa5fe18419ba42d419e0335409dd..7b02785590ff3fd63acf834d4314eb6f4ffd4859 100644 (file)
 
 #include "util.h"
 
+#include <stdio.h>
+
 #include "objfmt.h"
 
 RCSID("$IdPath$");
 
+static const char *
+dbg_objfmt_get_default_section_name(void)
+{
+    fprintf(stderr, "-dbg_objfmt_get_default_section_name()\n");
+    return ".text";
+}
+
+static int
+dbg_objfmt_is_valid_section(const char *name)
+{
+    fprintf(stderr, "-dbg_objfmt_is_valid_section(\"%s\")\n", name);
+    return 1;
+}
+
 /* Define objfmt structure -- see objfmt.h for details */
 objfmt dbg_objfmt = {
     "Trace of all info passed to object format module",
-    "dbg"
+    "dbg",
+    dbg_objfmt_get_default_section_name,
+    dbg_objfmt_is_valid_section
 };
index fb730830d89a0ec08e1c436ac52379dfa00db7f0..e021bdc2c16d1575d7817b31057f514b0b0fd926 100644 (file)
@@ -52,7 +52,7 @@ typedef struct parser_s {
      * This function returns the starting section of a linked list of sections
      * (whatever was in the file).
      */
-    sectionhead *(*doparse) (struct parser_s *p, objfmt *of, FILE *f);
+    sectionhead *(*do_parse) (struct parser_s *p, objfmt *of, FILE *f);
 } parser;
 
 /* Generic functions for all parsers - implemented in src/parser.c */
index e13c54c7ab8d8b6bc38c806174057097553c61b9..156b56bd9d0166c5e62df056f5bfde55af52367a 100644 (file)
@@ -28,6 +28,7 @@
 
 #ifdef STDC_HEADERS
 # include <stdlib.h>
+# include <string.h>
 # include <math.h>
 #endif
 
@@ -41,6 +42,7 @@
 
 #include "bytecode.h"
 #include "section.h"
+#include "objfmt.h"
 
 RCSID("$IdPath$");
 
@@ -51,6 +53,8 @@ extern int nasm_parser_lex(void);
 static unsigned long ConvertCharConstToInt(char *);
 void nasm_parser_error(char *);
 
+extern objfmt *nasm_parser_objfmt;
+extern sectionhead nasm_parser_sections;
 extern section *nasm_parser_cur_section;
 
 %}
@@ -127,13 +131,7 @@ input: /* empty */
     | input line    {
        OutputError();
        OutputWarning();
-       if ($2) {
-           if ($2->type != BC_EMPTY) {
-               STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, $2, link);
-           } else {
-               free($2);
-           }
-       }
+       bytecodes_append(&nasm_parser_cur_section->bc, $2);
        line_number++;
     }
 ;
@@ -185,7 +183,11 @@ label_id: ID           { $$ = locallabel_base = sym_def_get($1.name, SYM_LABEL); }
 
 /* directives */
 directive: '[' DIRECTIVE_NAME DIRECTIVE_VAL ']'        {
-       printf("Directive: Name='%s' Value='%s'\n", $2, $3);
+       if (strcasecmp($2, "section") == 0)
+           nasm_parser_cur_section = sections_switch(&nasm_parser_sections,
+                                                     nasm_parser_objfmt, $3);
+       else
+           printf("Directive: Name='%s' Value='%s'\n", $2, $3);
     }
     | '[' DIRECTIVE_NAME DIRECTIVE_VAL error   {
        Error(_("missing `%c'"), ']');
index e13c54c7ab8d8b6bc38c806174057097553c61b9..156b56bd9d0166c5e62df056f5bfde55af52367a 100644 (file)
@@ -28,6 +28,7 @@
 
 #ifdef STDC_HEADERS
 # include <stdlib.h>
+# include <string.h>
 # include <math.h>
 #endif
 
@@ -41,6 +42,7 @@
 
 #include "bytecode.h"
 #include "section.h"
+#include "objfmt.h"
 
 RCSID("$IdPath$");
 
@@ -51,6 +53,8 @@ extern int nasm_parser_lex(void);
 static unsigned long ConvertCharConstToInt(char *);
 void nasm_parser_error(char *);
 
+extern objfmt *nasm_parser_objfmt;
+extern sectionhead nasm_parser_sections;
 extern section *nasm_parser_cur_section;
 
 %}
@@ -127,13 +131,7 @@ input: /* empty */
     | input line    {
        OutputError();
        OutputWarning();
-       if ($2) {
-           if ($2->type != BC_EMPTY) {
-               STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, $2, link);
-           } else {
-               free($2);
-           }
-       }
+       bytecodes_append(&nasm_parser_cur_section->bc, $2);
        line_number++;
     }
 ;
@@ -185,7 +183,11 @@ label_id: ID           { $$ = locallabel_base = sym_def_get($1.name, SYM_LABEL); }
 
 /* directives */
 directive: '[' DIRECTIVE_NAME DIRECTIVE_VAL ']'        {
-       printf("Directive: Name='%s' Value='%s'\n", $2, $3);
+       if (strcasecmp($2, "section") == 0)
+           nasm_parser_cur_section = sections_switch(&nasm_parser_sections,
+                                                     nasm_parser_objfmt, $3);
+       else
+           printf("Directive: Name='%s' Value='%s'\n", $2, $3);
     }
     | '[' DIRECTIVE_NAME DIRECTIVE_VAL error   {
        Error(_("missing `%c'"), ']');
index 05dd10759510fc5ae387bf7623aefa2b2dfd751b..2f43d0c425e6c612025e5d9402158aac8d415d2a 100644 (file)
@@ -48,28 +48,21 @@ extern int nasm_parser_parse(void);
 
 int (*nasm_parser_yyinput) (char *buf, int max_size);
 
+objfmt *nasm_parser_objfmt;
 sectionhead nasm_parser_sections;
 section *nasm_parser_cur_section;
 
 static sectionhead *
-nasm_parser_doparse(parser *p, objfmt *of, FILE *f)
+nasm_parser_do_parse(parser *p, objfmt *of, FILE *f)
 {
     p->current_pp->initialize(of, f);
     nasm_parser_in = f;
     nasm_parser_yyinput = p->current_pp->input;
 
-    /* Initialize linked list */
-    STAILQ_INIT(&nasm_parser_sections);
+    nasm_parser_objfmt = of;
 
-    /* Add an initial "default" section to the list */
-    nasm_parser_cur_section = calloc(1, sizeof(section));
-    if (!nasm_parser_cur_section)
-       Fatal(FATAL_NOMEM);
-    STAILQ_INSERT_TAIL(&nasm_parser_sections, nasm_parser_cur_section, link);
-
-    /* Initialize default section */
-    nasm_parser_cur_section->type = SECTION_DEFAULT;
-    STAILQ_INIT(&nasm_parser_cur_section->bc);
+    /* Initialize section list */
+    nasm_parser_cur_section = sections_initialize(&nasm_parser_sections, of);
 
     /* only temporary */
     nasm_parser_debug = 0;
@@ -91,5 +84,5 @@ parser nasm_parser = {
     "nasm",
     nasm_parser_preprocs,
     &raw_preproc,
-    nasm_parser_doparse
+    nasm_parser_do_parse
 };
index 05dd10759510fc5ae387bf7623aefa2b2dfd751b..2f43d0c425e6c612025e5d9402158aac8d415d2a 100644 (file)
@@ -48,28 +48,21 @@ extern int nasm_parser_parse(void);
 
 int (*nasm_parser_yyinput) (char *buf, int max_size);
 
+objfmt *nasm_parser_objfmt;
 sectionhead nasm_parser_sections;
 section *nasm_parser_cur_section;
 
 static sectionhead *
-nasm_parser_doparse(parser *p, objfmt *of, FILE *f)
+nasm_parser_do_parse(parser *p, objfmt *of, FILE *f)
 {
     p->current_pp->initialize(of, f);
     nasm_parser_in = f;
     nasm_parser_yyinput = p->current_pp->input;
 
-    /* Initialize linked list */
-    STAILQ_INIT(&nasm_parser_sections);
+    nasm_parser_objfmt = of;
 
-    /* Add an initial "default" section to the list */
-    nasm_parser_cur_section = calloc(1, sizeof(section));
-    if (!nasm_parser_cur_section)
-       Fatal(FATAL_NOMEM);
-    STAILQ_INSERT_TAIL(&nasm_parser_sections, nasm_parser_cur_section, link);
-
-    /* Initialize default section */
-    nasm_parser_cur_section->type = SECTION_DEFAULT;
-    STAILQ_INIT(&nasm_parser_cur_section->bc);
+    /* Initialize section list */
+    nasm_parser_cur_section = sections_initialize(&nasm_parser_sections, of);
 
     /* only temporary */
     nasm_parser_debug = 0;
@@ -91,5 +84,5 @@ parser nasm_parser = {
     "nasm",
     nasm_parser_preprocs,
     &raw_preproc,
-    nasm_parser_doparse
+    nasm_parser_do_parse
 };
diff --git a/src/section.c b/src/section.c
new file mode 100644 (file)
index 0000000..ad760bb
--- /dev/null
@@ -0,0 +1,113 @@
+/* $IdPath$
+ * Section utility functions
+ *
+ *  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 <libintl.h>
+#define        _(String)       gettext(String)
+#ifdef gettext_noop
+#define        N_(String)      gettext_noop(String)
+#else
+#define        N_(String)      (String)
+#endif
+
+#include "globals.h"
+#include "errwarn.h"
+#include "expr.h"
+
+#include "bytecode.h"
+#include "section.h"
+#include "objfmt.h"
+
+RCSID("$IdPath$");
+
+section *
+sections_initialize(sectionhead *headp, objfmt *of)
+{
+    section *s;
+
+    /* Initialize linked list */
+    STAILQ_INIT(headp);
+
+    /* Add an initial "default" section to the list */
+    s = calloc(1, sizeof(section));
+    if (!s)
+       Fatal(FATAL_NOMEM);
+    STAILQ_INSERT_TAIL(headp, s, link);
+
+    /* Initialize default section */
+    s->type = SECTION_GENERAL;
+    s->name = strdup(of->get_default_section_name());
+    bytecodes_initialize(&s->bc);
+
+    return s;
+}
+
+section *
+sections_switch(sectionhead *headp, objfmt *of, const char *name)
+{
+    section *s, *sp;
+
+    /* Search through current sections to see if we already have one with
+     * that name.
+     */
+    s = (section *)NULL;
+    STAILQ_FOREACH(sp, headp, link) {
+       if (strcmp(sp->name, name) == 0)
+           s = sp;
+    }
+
+    if (s)
+       return s;
+
+    /* No: we have to allocate and create a new one. */
+
+    /* But first check with objfmt to see if the name is valid.
+     * If it isn't, error and just return the default (first) section.
+     */
+    if (!of->is_valid_section(name)) {
+       Error(_("Invalid section name: %s"), name);
+       return STAILQ_FIRST(headp);
+    }
+
+    /* Okay, the name is valid; now allocate and initialize */
+    s = calloc(1, sizeof(section));
+    if (!s)
+       Fatal(FATAL_NOMEM);
+    STAILQ_INSERT_TAIL(headp, s, link);
+
+    s->type = SECTION_GENERAL;
+    s->name = strdup(name);
+    bytecodes_initialize(&s->bc);
+
+    return s;
+}
index 592fb67b6d4e78d7601e171498df4e058d645f6b..71dd50b277152e229b3ded79544cb90e10f0498e 100644 (file)
 #ifndef YASM_SECTION_H
 #define YASM_SECTION_H
 
+struct objfmt_s;
+
 typedef STAILQ_HEAD(sectionhead_s, section_s) sectionhead;
 
 typedef struct section_s {
     STAILQ_ENTRY(section_s) link;
 
-    enum { SECTION_DEFAULT, SECTION_GENERAL, SECTION_ABSOLUTE } type;
+    enum { SECTION_GENERAL, SECTION_ABSOLUTE } type;
 
-    char *name;
-    unsigned int id;
+    char *name;                        /* strdup()'ed name (given by user) */
 
     union {
-       /* SECTION_DEFAULT data */
        /* SECTION_GENERAL data */
        /* SECTION_ABSOLUTE data */
        unsigned long start;
@@ -42,4 +42,9 @@ typedef struct section_s {
     bytecodehead bc;           /* the bytecodes for the section's contents */
 } section;
 
+section *sections_initialize(sectionhead *headp, struct objfmt_s *of);
+
+section *sections_switch(sectionhead *headp, struct objfmt_s *of,
+                        const char *name);
+
 #endif