changeset 353:faa97115952e

Added SECTION/ENDSECTION
author lost@starbug
date Tue, 30 Mar 2010 21:48:49 -0600
parents f5b77989f675
children 60568b123281
files lwasm/Makefile.am lwasm/instab.c lwasm/lwasm.h lwasm/section.c
diffstat 4 files changed, 189 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/lwasm/Makefile.am	Tue Mar 30 20:56:54 2010 -0600
+++ b/lwasm/Makefile.am	Tue Mar 30 21:48:49 2010 -0600
@@ -3,6 +3,6 @@
 lwasm_SOURCES = main.c pragma.c input.c pass1.c lwasm.c \
 	instab.c symbol.c macro.c \
 	insn_inh.c \
-	pseudo.c
+	pseudo.c section.c
 lwasm_LDADD = -L$(top_builddir)/lib -L$(top_srcdir)/lib -L$(top_builddir)/lwlib -L$(top_srcdir)/lwlib -lgnu -llw
 EXTRA_DIST =  lwasm.h input.h instab.h
--- a/lwasm/instab.c	Tue Mar 30 20:56:54 2010 -0600
+++ b/lwasm/instab.c	Tue Mar 30 21:48:49 2010 -0600
@@ -169,6 +169,13 @@
 #define pseudo_resolve_starpragma NULL
 #define pseudo_emit_starpragma NULL
 
+extern PARSEFUNC(pseudo_parse_section);
+#define pseudo_resolve_section NULL
+#define pseudo_emit_section NULL
+
+extern PARSEFUNC(pseudo_parse_endsection);
+#define pseudo_resolve_endsection NULL
+#define pseudo_emit_endsection NULL
 
 instab_t instab[] =
 {
@@ -487,7 +494,7 @@
 
 	{ "setdp", 		{	-1, 	-1, 	-1, 	-1},	pseudo_parse_setdp,		pseudo_resolve_setdp,			pseudo_emit_setdp,			lwasm_insn_normal},
 	{ "set",		{	-1, 	-1, 	-1, 	-1},	pseudo_parse_set,		pseudo_resolve_set,				pseudo_emit_set,			lwasm_insn_setsym},
-/*
+
 
 	{ "section",	{	-1, 	-1, 	-1, 	-1},	pseudo_parse_section,	pseudo_resolve_section,			pseudo_emit_section,		lwasm_insn_normal},
 	{ "sect",		{	-1, 	-1, 	-1, 	-1},	pseudo_parse_section,	pseudo_resolve_section,			pseudo_emit_section,		lwasm_insn_normal},
@@ -495,7 +502,7 @@
 	{ "endsect",	{	-1, 	-1, 	-1, 	-1},	pseudo_parse_endsection,pseudo_resolve_endsection,		pseudo_emit_endsection,		lwasm_insn_normal},
 	{ "endsection",	{	-1,		-1, 	-1, 	-1},	pseudo_parse_endsection,pseudo_resolve_endsection,		pseudo_emit_endsection,		lwasm_insn_normal},
 	
-*/
+
 	{ "pragma",		{	-1, 	-1, 	-1, 	-1},	pseudo_parse_pragma,	pseudo_resolve_pragma,			pseudo_emit_pragma,			lwasm_insn_normal},
 	{ "*pragma",	{	-1, 	-1, 	-1, 	-1},	pseudo_parse_starpragma,pseudo_resolve_starpragma,		pseudo_emit_starpragma,		lwasm_insn_normal},
 	
--- a/lwasm/lwasm.h	Tue Mar 30 20:56:54 2010 -0600
+++ b/lwasm/lwasm.h	Tue Mar 30 21:48:49 2010 -0600
@@ -140,6 +140,21 @@
 	macrotab_t *next;					// next macro in list
 };
 
+enum
+{
+	section_flag_bss = 1,				// BSS section
+	section_flag_none = 0				// no flags
+};
+
+typedef struct sectiontab_s sectiontab_t;
+struct sectiontab_s
+{
+	char *name;							// section name
+	int flags;							// section flags;
+	lw_expr_t offset;					// offset for next instance
+	sectiontab_t *next;
+};
+
 struct asmstate_s
 {
 	int output_format;					// output format
@@ -160,11 +175,14 @@
 
 	line_t *cl;							// current line pointer
 	
+	sectiontab_t *csect;				// current section
+	
 	int context;						// the current "context"
 	int nextcontext;					// the next available context
 	
 	symtab_t symtab;					// meta data for the symbol table
 	macrotab_t *macros;					// macro table
+	sectiontab_t *sections;				// section table
 
 	char *list_file;					// name of file to list to
 	char *output_file;					// output file name	
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwasm/section.c	Tue Mar 30 21:48:49 2010 -0600
@@ -0,0 +1,161 @@
+/*
+section.c
+
+Copyright © 2010 William Astle
+
+This file is part of LWTOOLS.
+
+LWTOOLS 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 3 of the License, or (at your option) any later
+version.
+
+This program 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <config.h>
+
+#include <string.h>
+
+#include <lw_string.h>
+#include <lw_alloc.h>
+
+#include "lwasm.h"
+#include "instab.h"
+
+PARSEFUNC(pseudo_parse_section)
+{
+	char *p2;
+	char *sn;
+	char *opts = NULL;
+	sectiontab_t *s;
+
+	if (as -> output_format != OUTPUT_OBJ)
+	{
+		lwasm_register_error(as, l, "Cannot use sections unless using the object target");
+		return;
+	}
+	
+	if (!**p)
+	{
+		lwasm_register_error(as, l, "Need section name");
+		return;
+	}
+
+	if (as -> csect)
+	{
+		lw_expr_destroy(as -> csect -> offset);
+		as -> csect -> offset = l -> addr;
+		as -> csect = NULL;
+	}
+	
+	for (p2 = *p; *p2 && *p2 != ',' && !isspace(*p2); p2++)
+		/* do nothing */ ;
+	
+	sn = lw_strndup(*p, p2 - *p);
+	*p = p2;
+	
+	if (**p == ',')
+	{
+		// have opts
+		(*p)++;
+		
+		for (p2 = *p; *p2 && !isspace(*p2); p2++)
+			/* do nothing */ ;
+		
+		opts = lw_strndup(*p, p2 - *p);
+		*p = p2;
+	}
+
+	for (s = as -> sections; s; s = s -> next)
+	{
+		if (!strcmp(s -> name, sn))
+			break;
+	}
+	if (s && opts)
+	{
+		lwasm_register_warning(as, l, "Section flags can only be specified the first time; ignoring duplicate definition");
+	}
+	if (!s)
+	{
+		// create section data structure
+		s = lw_alloc(sizeof(sectiontab_t));
+		s -> name = lw_strdup(sn);
+		s -> offset = lw_expr_build(lw_expr_type_int, 0);
+		s -> flags = section_flag_none;
+		if (!strcasecmp(sn, "bss") || !strcasecmp(sn, ".bss"))
+		{
+			s -> flags |= section_flag_bss;
+		}
+		// parse options
+		if (opts)
+		{
+			// only one option ("bss" or "!bss")
+			if (!strcasecmp(opts, "bss"))
+			{
+				s -> flags |= section_flag_bss;
+			}
+			else if (!strcasecmp(opts, "!bss"))
+			{
+				s -> flags &= ~section_flag_bss;
+			}
+			else
+			{
+				lwasm_register_error(as, l, "Unrecognized section flag");
+				lw_free(sn);
+				lw_free(opts);
+				lw_free(s -> name);
+				lw_expr_destroy(s -> offset);
+				lw_free(s);
+				return;
+			}
+		}
+		s -> next = as -> sections;
+		as -> sections = s;
+	}
+	
+	lw_expr_destroy(l -> addr);
+	l -> addr = lw_expr_copy(s -> offset);
+	
+	as -> csect = s;
+	as -> context = lwasm_next_context(as);
+
+	l -> len = 0;
+	
+	lw_free(opts);
+	lw_free(sn);
+}
+
+PARSEFUNC(pseudo_parse_endsection)
+{
+	if (as -> output_format != OUTPUT_OBJ)
+	{
+		lwasm_register_error(as, l, "Cannot use sections unless using the object target");
+		return;
+	}
+
+	if (!(as -> csect))
+	{
+		lwasm_register_error(as, l, "ENDSECTION without SECTION");
+		return;
+	}
+
+	// save offset in case another instance of the section appears	
+	lw_expr_destroy(as -> csect -> offset);
+	as -> csect -> offset = l -> addr;
+	
+	// reset address to 0
+	l -> addr = lw_expr_build(lw_expr_type_int, 0);
+	as -> csect = NULL;
+	
+	// end of section is a context break
+	as -> context = lwasm_next_context(as);
+
+	skip_operand(p);
+}