view lwasm/list.c @ 213:c40960370fd8

Added autogen.sh
author lost
date Fri, 24 Apr 2009 21:42:06 +0000
parents bae1e3ecdce1
children
line wrap: on
line source

/*
list.c
Copyright © 2009 William Astle

This file is part of LWASM.

LWASM 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/>.

Contains code for displaying a program listings, etc.
*/

#define __list_c_seen__
#include <config.h>

#include <stdio.h>
#include <stdlib.h>

#include "lwasm.h"

void lwasm_show_errors(asmstate_t *as)
{
	lwasm_line_t *l;
	lwasm_error_t *e;
	
	for (l = as -> lineshead; l; l = l -> next)
	{
		if (l -> err)
		{
			for (e = l -> err; e; e = e -> next)
			{
				fprintf(stderr, "ERROR: %s\n", e -> mess);
			}
			fprintf(stderr, "%s:%d: %s\n", l -> filename, l -> lineno, l -> text);
		}
	}
}

void lwasm_list(asmstate_t *as)
{
	FILE *lf;
	lwasm_line_t *l;
	int c, c3;
	char *p;
	
	if (!as -> listfile)
		goto showerr;
	if (as -> listfile[0] == '-' && as -> listfile[1] == '\0')
		lf = stdout;
	else
	{
		lf = fopen(as -> listfile, "w");
		if (!lf)
		{
			fprintf(stderr, "Unable to open list file '%s'. No listing will be generated: ", as -> listfile);
			perror("");
			goto showerr;
		}
	}

	for (l = as -> lineshead; l; l = l -> next)
	{
		if (l -> addrset == 1 || l -> codelen > 0 || l -> nocodelen > 0)
		{
			fprintf(lf, "%04X ", l -> codeaddr);
		}
		else
		{
			fprintf(lf, "     ");
		}
		
		if (l -> addrset == 2)
		{
			fprintf(lf, "%04X      ", l -> symaddr);
		}
		else
		{
			for (c = 0; c < l -> codelen && c < 5; c++)
			{
				fprintf(lf, "%02X", l -> bytes[c]);
			}
			while (c < 5)
			{
				fprintf(lf, "  ");
				c++;
			}
		}
		fprintf(lf, " %20.20s:%05d ", l -> filename, l -> lineno);
		
		// print line here
		for (c3 = 0, c = 0, p = l -> text; *p; c++, p++)
		{
			if (*p == '\t')
			{
				int c2;
				c2 = 8 - (c3 % 8);
				c3 += c2;
				while (c2--) fputc(' ', lf);
			}
			else
			{
				c3++;
				fputc(*p, lf);
			}
		}
		fputc('\n', lf);
		
		if (l -> codelen > 5)
		{
			fprintf(lf, "%04X ", (l -> codeaddr + 5) & 0xFFFF);
			for (c = 5; c < l -> codelen; c++)
			{
				if (!(c % 5) && c != 5)
				{
					fprintf(lf, "\n%04X ", (l -> codeaddr + c) & 0xFFFF);
				}
				fprintf(lf, "%02X", l -> bytes[c]);
			}
			fputc('\n', lf);
		}
	}
	
	lwasm_list_symbols(as, lf);
	
	if (lf != stdout)
		fclose(lf);

showerr:
	lwasm_show_errors(as);
}