view lwdisasm/symbol.c @ 409:cba03436c720

Checkpoint disassembler
author lost@l-w.ca
date Mon, 02 Aug 2010 18:07:04 -0600
parents
children
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 <stdio.h>

#include <lw_alloc.h>
#include <lw_string.h>

#include "lwdisasm.h"

symbol_t *find_symbol(disasmstate_t *as, int addr, int section)
{
	symbol_t *s;
	
	for (s = as -> symbols; s; s = s -> next)
	{
		if (s -> address == addr && s -> section == section)
			return s;
	}
	return NULL;
}

symbol_t *register_symbol(disasmstate_t *as, int addr, int section, char *symbol)
{
	char symbuf[200];
	symbol_t *s;

	for (s = as -> symbols; s; s = s -> next)
	{
		if (s -> address == addr && s -> section == section)
			return s;
	}
	
	if (!symbol)
	{
		if (section)
		{
			sprintf(symbuf, "L%d_%04X", section, addr);
		}
		else
		{
			sprintf(symbuf, "L%04X", addr);
		}
		symbol = symbuf;
	}
	
	s = lw_alloc(sizeof(symbol_t));
	s -> symbol = lw_strdup(symbol);
	s -> address = addr;
	s -> section = section;
	s -> next = as -> symbols;
	as -> symbols = s;
	return s;
}

void attach_symbols(disasmstate_t *as)
{
	symbol_t *s;
	linedata_t *l;
	
	for (s = as -> symbols; s; s = s -> next)
	{
		for (l = as -> lhead; l; l = l -> next)
		{
			if (l -> isref == 0 && l -> address == s -> address && l -> sectionref == s -> section)
			{
				l -> symbol = s;
				l -> isref = 1;
				break;
			}
		}
	}
}