view lwasm/symbol.c @ 360:7d91ab7ac7d6

Indexed stage 2; set line structure to track pragmas in effect for that line
author lost@starbug
date Thu, 01 Apr 2010 18:39:40 -0600
parents a82c55070624
children d96c30e60ddf
line wrap: on
line source

/*
symbol.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 <stdlib.h>
#include <string.h>

#include <lw_alloc.h>
#include <lw_expr.h>

#include "lwasm.h"

struct symtabe *register_symbol(asmstate_t *as, line_t *cl, char *sym, lw_expr_t val, int flags)
{
	struct symtabe *se;
	int islocal = 0;
	int context = -1;
	int version = -1;
	char *cp;

	if (*sym < 0x80 && !strchr(SSYMCHARS, *sym))
	{
		lwasm_register_error(as, cl, "Bad symbol (%s)", sym);
		return NULL;
	}

	if ((*sym == '$' || *sym == '@') && (sym[1] >= '0' && sym[1] <= '9'))
	{
		lwasm_register_error(as, cl, "Bad symbol (%s)", sym);
		return NULL;
	}

	for (cp = sym; *cp; cp++)
	{
		if (*cp == '@' || *cp == '?')
			islocal = 1;
		if (*cp == '$' && !(CURPRAGMA(cl, PRAGMA_DOLLARNOTLOCAL)))
			islocal = 1;
		
		// bad symbol
		if (*cp < 0x80 && !strchr(SYMCHARS, *cp))
		{
			lwasm_register_error(as, cl, "Bad symbol (%s)", sym);
			return NULL;
		}
	}

	if (islocal)
		context = as -> context;
	
	// first, look up symbol to see if it is already defined
	for (se = as -> symtab.head; se; se = se -> next)
	{
		if (!strcmp(sym, se -> symbol))
		{
			if (se -> context != context)
				continue;
			if ((flags & symbol_flag_set) && (se -> flags & symbol_flag_set))
			{
				if (version < se -> version)
					version = se -> version;
					continue;
			}
			break;
		}
	}
	if (se)
	{
		// multiply defined symbol
		lwasm_register_error(as, cl, "Multiply defined symbol (%s)", sym);
		return NULL;
	}

	if (flags & symbol_flag_set)
	{
		version++;
	}
	
	se = lw_alloc(sizeof(struct symtabe));
	se -> next = as -> symtab.head;
	as -> symtab.head = se;
	se -> context = context;
	se -> version = version;
	se -> flags = flags;
	se -> value = lw_expr_copy(val);
	return se;
}

struct symtabe * lookup_symbol(asmstate_t *as, line_t *cl, char *sym, int context, int version)
{
	return NULL;
}