view src/list.c @ 4:34568fab6058

Fixed package to include all required files; also added copyright preamble to all source files
author lost
date Sat, 04 Oct 2008 03:14:20 +0000
parents 57495da01900
children 39d750ee8d34
line wrap: on
line source

/*
list.c
Copyright © 2008 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 listing in lwasm
*/

//#include <ctype.h>
#include <errno.h>
#include <stdio.h>
//#include <stdlib.h>
#include <string.h>
#define __list_c_seen__
//#include "instab.h"
#include "lwasm.h"

const char *errlist[] =
{
	"No error",
	"Bad opcode",
	"Illegal Symbol",
	"Multiply defined symbol",
	"Symbol required but not present",
	"Forward references not permitted",
	"Byte overflow",
	"Phase error",
	"Bad operand",
	"Symbol not permitted here",
	"Undefined symbol",
	"Bit number out of range",
	"Invalid expression",
	"Invalid register",
	"Bad file name",
	"ENDM without MACRO",
	"Redefined macro",
	"Nested namespace",
	"Bad condition",
	"User error",
	"Bad pragma",
	""
};

void list_code(asmstate_t *as)
{
	FILE *lf;
	sourceline_t *cl;
	int bn;
	int c;
	char *t;
	
	if (as -> listfile && strcmp(as -> listfile, "-"))
	{
		lf = fopen(as -> listfile, "w");
		if (!lf)
		{
			perror("Cannot open list file");
			return;
		}
	}
	else
	{
		lf = stdout;
	}
	
	for (cl = as -> source_head; cl; cl = cl -> next)
	{
		bn = 0;
		if (cl -> errors)
		{
			errortab_t *e;
			for (e = cl -> errors; e; e = e -> next)
			{
				if (e -> errnum < ERR_MAXERR && e -> errnum != ERR_USER)
					fprintf(lf, "*****ERROR: %s\n", errlist[e -> errnum]);
			}
			if (cl -> user_error)
			{
				fprintf(lf, "*****ERROR: %s\n", cl -> user_error);
			}
		}
		if (cl -> skipped)
		{
			fprintf(lf, "%-15.15s", "<skipped>");
		}
		else if (cl -> macrodef)
		{
			fprintf(lf, "%-15.15s", "<macrodef>");
		}
		else if (cl -> opcode >= 0 && cl -> numcodebytes > 0)
		{
			fprintf(lf, "%04X ", cl -> addr);
			while (bn < 5 && bn < cl -> numcodebytes)
			{
				fprintf(lf, "%02X", cl -> codebytes[bn]);
				bn++;
			}
			while (bn < 5)
			{
				fprintf(lf, "  ");
				bn++;
			}
		}
		else if (cl -> addrset || (cl -> len && cl -> numcodebytes == 0))
		{
			fprintf(lf, "%04X %10s", cl -> addr, "");
		}
		else if (cl -> isequ)
		{
			fprintf(lf, "     %04X      ", cl -> symaddr);
		}
		else if (cl -> issetdp)
		{
			fprintf(lf, "     %02X        ", cl -> dpval);
		}
		else
			fprintf(lf, "               ");
		fprintf(lf, " %15.15s:%06d ", cl -> sourcefile, cl -> lineno);
		// actually display the line from the file
		for (c = 0, t = cl -> line; *t; t++)
		{
			if (*t == '\n' || *t == '\r')
				break;
			if (*t == '\t')
			{
				do
				{
					fprintf(lf, " ");
					c++;
				} while (c % 8);
			}
			else
			{
				c++;
				fprintf(lf, "%c", *t);
			}
		}
//		fprintf(lf, "\n");

		while (bn < cl -> numcodebytes)
		{
			if (bn % 5 == 0)
				fprintf(lf, "\n%04X ", (cl -> addr + bn) & 0xFFFF);
			fprintf(lf, "%02X", cl -> codebytes[bn]);
			bn++;
		}
		fprintf(lf, "\n");
	}

	fprintf(lf, "\n");
	list_symbols(as, lf);
	
	if (lf != stdout)
		fclose(lf);
}