view lwasm/section.c @ 353:faa97115952e

Added SECTION/ENDSECTION
author lost@starbug
date Tue, 30 Mar 2010 21:48:49 -0600
parents
children 7166254491ed
line wrap: on
line source

/*
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);
}