From 013530034ed7055a2ceb27c8861ad5532c403f45 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 16 Sep 2001 18:53:47 +0000 Subject: [PATCH] Start building of sections and bytecodes in parser. Still need to add section switching (currently never switches away from default section). svn path=/trunk/yasm/; revision=163 --- libyasm/section.h | 9 +++++---- modules/parsers/nasm/bison.y.in | 24 ++++++++++++++++++++---- modules/parsers/nasm/nasm-bison.y | 24 ++++++++++++++++++++---- modules/parsers/nasm/nasm-parser.c | 30 ++++++++++++++++++++++++++---- modules/parsers/nasm/parser.c | 30 ++++++++++++++++++++++++++---- src/parsers/nasm/bison.y.in | 24 ++++++++++++++++++++---- src/parsers/nasm/nasm-bison.y | 24 ++++++++++++++++++++---- src/parsers/nasm/nasm-parser.c | 30 ++++++++++++++++++++++++++---- src/parsers/nasm/parser.c | 30 ++++++++++++++++++++++++++---- src/section.h | 9 +++++---- 10 files changed, 194 insertions(+), 40 deletions(-) diff --git a/libyasm/section.h b/libyasm/section.h index 43efd819..0dd0b45b 100644 --- a/libyasm/section.h +++ b/libyasm/section.h @@ -1,4 +1,4 @@ -/* $Id: section.h,v 1.6 2001/09/16 04:49:46 peter Exp $ +/* $Id: section.h,v 1.7 2001/09/16 18:53:47 peter Exp $ * Section header file * * Copyright (C) 2001 Peter Johnson @@ -27,14 +27,15 @@ typedef STAILQ_HEAD(sectionhead_s, section_s) sectionhead; typedef struct section_s { STAILQ_ENTRY(section_s) link; - enum { SECTION, ABSOLUTE } type; + enum { SECTION_DEFAULT, SECTION_GENERAL, SECTION_ABSOLUTE } type; char *name; unsigned int id; union { - /* SECTION data */ - /* ABSOLUTE data */ + /* SECTION_DEFAULT data */ + /* SECTION_GENERAL data */ + /* SECTION_ABSOLUTE data */ unsigned long start; } data; diff --git a/modules/parsers/nasm/bison.y.in b/modules/parsers/nasm/bison.y.in index c1cab314..641b1913 100644 --- a/modules/parsers/nasm/bison.y.in +++ b/modules/parsers/nasm/bison.y.in @@ -1,4 +1,4 @@ -/* $Id: bison.y.in,v 1.28 2001/08/30 03:45:26 peter Exp $ +/* $Id: bison.y.in,v 1.29 2001/09/16 18:53:47 peter Exp $ * Main bison parser * * Copyright (C) 2001 Peter Johnson, Michael Urman @@ -40,8 +40,9 @@ #include "symrec.h" #include "bytecode.h" +#include "section.h" -RCSID("$Id: bison.y.in,v 1.28 2001/08/30 03:45:26 peter Exp $"); +RCSID("$Id: bison.y.in,v 1.29 2001/09/16 18:53:47 peter Exp $"); #define YYDEBUG 1 @@ -50,6 +51,10 @@ extern int nasm_parser_lex(void); static unsigned long ConvertCharConstToInt(char *); void nasm_parser_error(char *); +extern section *nasm_parser_cur_section; + +static bytecode *new_bc; + %} %union { @@ -121,11 +126,22 @@ void nasm_parser_error(char *); %% input: /* empty */ - | input line { OutputError(); OutputWarning(); line_number++; } + | input line { + OutputError(); + OutputWarning(); + if ($2.type != BC_EMPTY) { + new_bc = malloc(sizeof(bytecode)); + if(!new_bc) + Fatal(FATAL_NOMEM); + memcpy(new_bc, &$2, sizeof(bytecode)); + STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, new_bc, link); + } + line_number++; + } ; line: '\n' { $$.type = BC_EMPTY; } - | exp '\n' { DebugPrintBC(&$1); $$ = $1; } + | exp '\n' | directive '\n' { $$.type = BC_EMPTY; } | error '\n' { Error(_("label or instruction expected at start of line")); diff --git a/modules/parsers/nasm/nasm-bison.y b/modules/parsers/nasm/nasm-bison.y index 84720ffb..655924e0 100644 --- a/modules/parsers/nasm/nasm-bison.y +++ b/modules/parsers/nasm/nasm-bison.y @@ -1,4 +1,4 @@ -/* $Id: nasm-bison.y,v 1.28 2001/08/30 03:45:26 peter Exp $ +/* $Id: nasm-bison.y,v 1.29 2001/09/16 18:53:47 peter Exp $ * Main bison parser * * Copyright (C) 2001 Peter Johnson, Michael Urman @@ -40,8 +40,9 @@ #include "symrec.h" #include "bytecode.h" +#include "section.h" -RCSID("$Id: nasm-bison.y,v 1.28 2001/08/30 03:45:26 peter Exp $"); +RCSID("$Id: nasm-bison.y,v 1.29 2001/09/16 18:53:47 peter Exp $"); #define YYDEBUG 1 @@ -50,6 +51,10 @@ extern int nasm_parser_lex(void); static unsigned long ConvertCharConstToInt(char *); void nasm_parser_error(char *); +extern section *nasm_parser_cur_section; + +static bytecode *new_bc; + %} %union { @@ -121,11 +126,22 @@ void nasm_parser_error(char *); %% input: /* empty */ - | input line { OutputError(); OutputWarning(); line_number++; } + | input line { + OutputError(); + OutputWarning(); + if ($2.type != BC_EMPTY) { + new_bc = malloc(sizeof(bytecode)); + if(!new_bc) + Fatal(FATAL_NOMEM); + memcpy(new_bc, &$2, sizeof(bytecode)); + STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, new_bc, link); + } + line_number++; + } ; line: '\n' { $$.type = BC_EMPTY; } - | exp '\n' { DebugPrintBC(&$1); $$ = $1; } + | exp '\n' | directive '\n' { $$.type = BC_EMPTY; } | error '\n' { Error(_("label or instruction expected at start of line")); diff --git a/modules/parsers/nasm/nasm-parser.c b/modules/parsers/nasm/nasm-parser.c index 9981e046..836965ec 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.8 2001/09/16 04:49:46 peter Exp $ +/* $Id: nasm-parser.c,v 1.9 2001/09/16 18:53:47 peter Exp $ * NASM-compatible parser * * Copyright (C) 2001 Peter Johnson @@ -27,13 +27,19 @@ #include +#ifdef STDC_HEADERS +# include +#endif + +#include "errwarn.h" + #include "bytecode.h" #include "section.h" #include "objfmt.h" #include "preproc.h" #include "parser.h" -RCSID("$Id: nasm-parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $"); +RCSID("$Id: nasm-parser.c,v 1.9 2001/09/16 18:53:47 peter Exp $"); extern FILE *nasm_parser_in; extern int nasm_parser_debug; @@ -42,6 +48,9 @@ extern int nasm_parser_parse(void); int (*nasm_parser_yyinput) (char *buf, int max_size); +sectionhead nasm_parser_sections; +section *nasm_parser_cur_section; + static sectionhead * nasm_parser_doparse(parser *p, objfmt *of, FILE *f) { @@ -49,12 +58,25 @@ nasm_parser_doparse(parser *p, objfmt *of, FILE *f) nasm_parser_in = f; nasm_parser_yyinput = p->current_pp->input; + /* Initialize linked list */ + STAILQ_INIT(&nasm_parser_sections); + + /* 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); + /* only temporary */ - nasm_parser_debug = 1; + nasm_parser_debug = 0; nasm_parser_parse(); - return NULL; + return &nasm_parser_sections; } /* Define valid preprocessors to use with this parser */ diff --git a/modules/parsers/nasm/parser.c b/modules/parsers/nasm/parser.c index 449279c6..b2edcb8c 100644 --- a/modules/parsers/nasm/parser.c +++ b/modules/parsers/nasm/parser.c @@ -1,4 +1,4 @@ -/* $Id: parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $ +/* $Id: parser.c,v 1.9 2001/09/16 18:53:47 peter Exp $ * NASM-compatible parser * * Copyright (C) 2001 Peter Johnson @@ -27,13 +27,19 @@ #include +#ifdef STDC_HEADERS +# include +#endif + +#include "errwarn.h" + #include "bytecode.h" #include "section.h" #include "objfmt.h" #include "preproc.h" #include "parser.h" -RCSID("$Id: parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $"); +RCSID("$Id: parser.c,v 1.9 2001/09/16 18:53:47 peter Exp $"); extern FILE *nasm_parser_in; extern int nasm_parser_debug; @@ -42,6 +48,9 @@ extern int nasm_parser_parse(void); int (*nasm_parser_yyinput) (char *buf, int max_size); +sectionhead nasm_parser_sections; +section *nasm_parser_cur_section; + static sectionhead * nasm_parser_doparse(parser *p, objfmt *of, FILE *f) { @@ -49,12 +58,25 @@ nasm_parser_doparse(parser *p, objfmt *of, FILE *f) nasm_parser_in = f; nasm_parser_yyinput = p->current_pp->input; + /* Initialize linked list */ + STAILQ_INIT(&nasm_parser_sections); + + /* 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); + /* only temporary */ - nasm_parser_debug = 1; + nasm_parser_debug = 0; nasm_parser_parse(); - return NULL; + return &nasm_parser_sections; } /* Define valid preprocessors to use with this parser */ diff --git a/src/parsers/nasm/bison.y.in b/src/parsers/nasm/bison.y.in index c1cab314..641b1913 100644 --- a/src/parsers/nasm/bison.y.in +++ b/src/parsers/nasm/bison.y.in @@ -1,4 +1,4 @@ -/* $Id: bison.y.in,v 1.28 2001/08/30 03:45:26 peter Exp $ +/* $Id: bison.y.in,v 1.29 2001/09/16 18:53:47 peter Exp $ * Main bison parser * * Copyright (C) 2001 Peter Johnson, Michael Urman @@ -40,8 +40,9 @@ #include "symrec.h" #include "bytecode.h" +#include "section.h" -RCSID("$Id: bison.y.in,v 1.28 2001/08/30 03:45:26 peter Exp $"); +RCSID("$Id: bison.y.in,v 1.29 2001/09/16 18:53:47 peter Exp $"); #define YYDEBUG 1 @@ -50,6 +51,10 @@ extern int nasm_parser_lex(void); static unsigned long ConvertCharConstToInt(char *); void nasm_parser_error(char *); +extern section *nasm_parser_cur_section; + +static bytecode *new_bc; + %} %union { @@ -121,11 +126,22 @@ void nasm_parser_error(char *); %% input: /* empty */ - | input line { OutputError(); OutputWarning(); line_number++; } + | input line { + OutputError(); + OutputWarning(); + if ($2.type != BC_EMPTY) { + new_bc = malloc(sizeof(bytecode)); + if(!new_bc) + Fatal(FATAL_NOMEM); + memcpy(new_bc, &$2, sizeof(bytecode)); + STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, new_bc, link); + } + line_number++; + } ; line: '\n' { $$.type = BC_EMPTY; } - | exp '\n' { DebugPrintBC(&$1); $$ = $1; } + | exp '\n' | directive '\n' { $$.type = BC_EMPTY; } | error '\n' { Error(_("label or instruction expected at start of line")); diff --git a/src/parsers/nasm/nasm-bison.y b/src/parsers/nasm/nasm-bison.y index 84720ffb..655924e0 100644 --- a/src/parsers/nasm/nasm-bison.y +++ b/src/parsers/nasm/nasm-bison.y @@ -1,4 +1,4 @@ -/* $Id: nasm-bison.y,v 1.28 2001/08/30 03:45:26 peter Exp $ +/* $Id: nasm-bison.y,v 1.29 2001/09/16 18:53:47 peter Exp $ * Main bison parser * * Copyright (C) 2001 Peter Johnson, Michael Urman @@ -40,8 +40,9 @@ #include "symrec.h" #include "bytecode.h" +#include "section.h" -RCSID("$Id: nasm-bison.y,v 1.28 2001/08/30 03:45:26 peter Exp $"); +RCSID("$Id: nasm-bison.y,v 1.29 2001/09/16 18:53:47 peter Exp $"); #define YYDEBUG 1 @@ -50,6 +51,10 @@ extern int nasm_parser_lex(void); static unsigned long ConvertCharConstToInt(char *); void nasm_parser_error(char *); +extern section *nasm_parser_cur_section; + +static bytecode *new_bc; + %} %union { @@ -121,11 +126,22 @@ void nasm_parser_error(char *); %% input: /* empty */ - | input line { OutputError(); OutputWarning(); line_number++; } + | input line { + OutputError(); + OutputWarning(); + if ($2.type != BC_EMPTY) { + new_bc = malloc(sizeof(bytecode)); + if(!new_bc) + Fatal(FATAL_NOMEM); + memcpy(new_bc, &$2, sizeof(bytecode)); + STAILQ_INSERT_TAIL(&nasm_parser_cur_section->bc, new_bc, link); + } + line_number++; + } ; line: '\n' { $$.type = BC_EMPTY; } - | exp '\n' { DebugPrintBC(&$1); $$ = $1; } + | exp '\n' | directive '\n' { $$.type = BC_EMPTY; } | error '\n' { Error(_("label or instruction expected at start of line")); diff --git a/src/parsers/nasm/nasm-parser.c b/src/parsers/nasm/nasm-parser.c index 9981e046..836965ec 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.8 2001/09/16 04:49:46 peter Exp $ +/* $Id: nasm-parser.c,v 1.9 2001/09/16 18:53:47 peter Exp $ * NASM-compatible parser * * Copyright (C) 2001 Peter Johnson @@ -27,13 +27,19 @@ #include +#ifdef STDC_HEADERS +# include +#endif + +#include "errwarn.h" + #include "bytecode.h" #include "section.h" #include "objfmt.h" #include "preproc.h" #include "parser.h" -RCSID("$Id: nasm-parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $"); +RCSID("$Id: nasm-parser.c,v 1.9 2001/09/16 18:53:47 peter Exp $"); extern FILE *nasm_parser_in; extern int nasm_parser_debug; @@ -42,6 +48,9 @@ extern int nasm_parser_parse(void); int (*nasm_parser_yyinput) (char *buf, int max_size); +sectionhead nasm_parser_sections; +section *nasm_parser_cur_section; + static sectionhead * nasm_parser_doparse(parser *p, objfmt *of, FILE *f) { @@ -49,12 +58,25 @@ nasm_parser_doparse(parser *p, objfmt *of, FILE *f) nasm_parser_in = f; nasm_parser_yyinput = p->current_pp->input; + /* Initialize linked list */ + STAILQ_INIT(&nasm_parser_sections); + + /* 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); + /* only temporary */ - nasm_parser_debug = 1; + nasm_parser_debug = 0; nasm_parser_parse(); - return NULL; + return &nasm_parser_sections; } /* Define valid preprocessors to use with this parser */ diff --git a/src/parsers/nasm/parser.c b/src/parsers/nasm/parser.c index 449279c6..b2edcb8c 100644 --- a/src/parsers/nasm/parser.c +++ b/src/parsers/nasm/parser.c @@ -1,4 +1,4 @@ -/* $Id: parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $ +/* $Id: parser.c,v 1.9 2001/09/16 18:53:47 peter Exp $ * NASM-compatible parser * * Copyright (C) 2001 Peter Johnson @@ -27,13 +27,19 @@ #include +#ifdef STDC_HEADERS +# include +#endif + +#include "errwarn.h" + #include "bytecode.h" #include "section.h" #include "objfmt.h" #include "preproc.h" #include "parser.h" -RCSID("$Id: parser.c,v 1.8 2001/09/16 04:49:46 peter Exp $"); +RCSID("$Id: parser.c,v 1.9 2001/09/16 18:53:47 peter Exp $"); extern FILE *nasm_parser_in; extern int nasm_parser_debug; @@ -42,6 +48,9 @@ extern int nasm_parser_parse(void); int (*nasm_parser_yyinput) (char *buf, int max_size); +sectionhead nasm_parser_sections; +section *nasm_parser_cur_section; + static sectionhead * nasm_parser_doparse(parser *p, objfmt *of, FILE *f) { @@ -49,12 +58,25 @@ nasm_parser_doparse(parser *p, objfmt *of, FILE *f) nasm_parser_in = f; nasm_parser_yyinput = p->current_pp->input; + /* Initialize linked list */ + STAILQ_INIT(&nasm_parser_sections); + + /* 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); + /* only temporary */ - nasm_parser_debug = 1; + nasm_parser_debug = 0; nasm_parser_parse(); - return NULL; + return &nasm_parser_sections; } /* Define valid preprocessors to use with this parser */ diff --git a/src/section.h b/src/section.h index 43efd819..0dd0b45b 100644 --- a/src/section.h +++ b/src/section.h @@ -1,4 +1,4 @@ -/* $Id: section.h,v 1.6 2001/09/16 04:49:46 peter Exp $ +/* $Id: section.h,v 1.7 2001/09/16 18:53:47 peter Exp $ * Section header file * * Copyright (C) 2001 Peter Johnson @@ -27,14 +27,15 @@ typedef STAILQ_HEAD(sectionhead_s, section_s) sectionhead; typedef struct section_s { STAILQ_ENTRY(section_s) link; - enum { SECTION, ABSOLUTE } type; + enum { SECTION_DEFAULT, SECTION_GENERAL, SECTION_ABSOLUTE } type; char *name; unsigned int id; union { - /* SECTION data */ - /* ABSOLUTE data */ + /* SECTION_DEFAULT data */ + /* SECTION_GENERAL data */ + /* SECTION_ABSOLUTE data */ unsigned long start; } data; -- 2.40.0