view src/lwasm.h @ 0:57495da01900

Initial checking of LWASM
author lost
date Fri, 03 Oct 2008 02:44:20 +0000
parents
children 34568fab6058
line wrap: on
line source

/*
 * lwasm.h
 *
 * main defs used by the assembler
 *
 */


#ifndef __lwasm_h_seen__
#define __lwasm_h_seen__

#define MAX_OP_LEN 32
#define SYMCHAR_START "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define SYMCHAR "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$."

#define MACROCHAR_START "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define MACROCHAR "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"


#define OPER_INH	0
#define OPER_RTOR	1
#define OPER_RLIST	2
#define OPER_TFM	3
#define OPER_IMM8	4
#define OPER_IMM16	5
#define OPER_IMM32	6
#define OPER_DIR	7
#define OPER_EXT	8
#define OPER_INDEX	9	// indexed with no offset
#define OPER_DIREXT	10	// used as a placeholder when direct/extended unknown
#define OPER_BITBIT	11	// bit to bit direct page opers
#define OPER_REL8	12
#define OPER_REL16	13
#define OPER_INDEX5	14	// 5 bit offset indexed
#define OPER_INDEX8	15	// 8 bit offset indexed
#define OPER_INDEX16 16	// 16 bit offset indexed
#define OPER_INDEXV	17	// unknown size offset indexed
#define OPER_EXTIND	18	// extended indirect
#define OPER_LOGICD	19	// logic on mem, direct mode
#define OPER_LOGICE 20	// logic on mem, extended mode
#define OPER_LOGICEI 21	// logic on mem, extended indir mode
#define OPER_LOGICI	22	// logic on mem, indexed no offset
#define OPER_LOGICI5 23	// logic on mem, 5 bit indexed offset
#define OPER_LOGICI8 24	// logic on mem, 8 bit indexed offset
#define OPER_LOGICI16 25 // logic on mem, 16 bit indexed offset
#define OPER_LOGICIV 26	// logic on mem, unknown size offset

#define OUTPUT_DECB		0	// DECB multirecord format
#define OUTPUT_RAW		1	// raw sequence of bytes
#define OUTPUT_RAWREL	2	// raw but with ORG as a relative offset

enum
{
	ERR_NONE,		// no error
	ERR_BADOP,		// bad opcode
	ERR_BADSYM,		// illegal symbol
	ERR_DUPSYM,		// duplicate symbol definition
	ERR_NOSYM,		// symbol required but not present
	ERR_FORWARD,	// forward references not permitted here
	ERR_OVERFLOW,	// byte overflow
	ERR_PHASE,		// phasing error
	ERR_BADOPER,	// bad operand
	ERR_SYM,		// symbol present where not permitted
	ERR_UNDEF,		// undefined symbol
	ERR_OVERFLOW3,	// bit number out of range
	ERR_BADEXPR,	// invalid expression
	ERR_BADREG,		// invalid register
	ERR_BADFN,		// bad file name
	ERR_ENDM,		// end macro without macro
	ERR_MACRO,		// redefined macro
	ERR_NESTNAME,	// nested namespace
	ERR_BADCOND,	// bad condition
	ERR_USER,		// user error
	ERR_PRAGMA,		// bad pragma
	
	ERR_MAXERR		// last error in the list
};

typedef struct sourceline_s sourceline_t;

// structure to keep track of errors
typedef struct errortab_s errortab_t;
struct errortab_s {
	int errnum;
	sourceline_t *line;
	errortab_t *next;
};

typedef struct macroline_s macroline_t;
struct macroline_s
{
	char *linetext;
	macroline_t *next;
};

typedef struct macrotab_s macrotab_t;
struct macrotab_s
{
	char *name;				// name of the macro
	macroline_t *linehead;	// start of the macro lines
	macroline_t *linetail;	// last line of macro
	macrotab_t *next;
};


// structure to keep track of each line of the source
// this also keeps various bits of state about a line
struct sourceline_s {
	char *sourcefile;		// name of the source file
	int lineno;				// line number in the source file
	char *symstr;			// symbol on this line (if any)
	int opcode;				// index to instab for this line
	int opbytes;			// actual bytes of opcode
	int postbyte;			// post byte for the instruction
	int numcodebytes;		// how many code bytes for this line
	unsigned char *codebytes; // actual code for this line
	int codesize;			// size of codebytes
// if minaddr and maxaddr are the same, we know exactly which address
// we are at so there can be no possible phase error with symbols defined
// here; these are *code* addresses
	int len;				// length of this insn
	int in_macro;			// set if it's a macro expansion line
	int code_symloc;
	int addrmode;			// addressing mode of this insn
	int addr;				// the lowest possible address of this line
	int dpval;				// dp value at this point in the code
	int hassym;				// is there a symbol on this line
	int addrset;			// is this insn a point where the address is *set* (org)
	int isequ;				// does this insn set a symbol address (equ)?
	int isset;				// is the address setting from a "set"?
	int issetdp;			// this insn sets DP
	int symaddr;			// address for the symbol (if we're setting one)
							// also used for "setdp"
	int undef;				// set if the symbol is undefined
	int p1f16;				// if we forced 16 bit on pass 1 so we can force it on pass 2
	int nocode;				// if set, there is no code to output (rmb)
	int notinsn;			// if this is not a regular insn (used for formatting output)
	char *line;				// the text of the line
	char *opcstr;			// opcode string
	char *operstr;			// operand string
	char *remainder;		// remainder string (whole line if no opcode/operand
	errortab_t *errors;		// errors on this line
	sourceline_t *next;		// next line
	sourceline_t *prev;		// previous line
	macrotab_t *macro;		// pointer to macro table entry if this is a macro
	int skipped;			// set if the line was skipped
	char *user_error;		// user error message
	int macrodef;			// set if it's a macro definition line
};

// structure to keep the symbol table
typedef struct symtab_s symtab_t;
struct symtab_s {
	char *symbol; // symbol name
	sourceline_t *line;			// pointer to the line where symbol is defined
	int addr;					// address of symbol (-1 for unknown)
	int flags;					// flags for symbol
	symtab_t *next;				// next symbol
};


// keep track of current assembler state
typedef struct {
	int dpval;			// current dp value (setdp)
	int addr;		// current address
	symtab_t *symbol_table;
	sourceline_t *source_head;
	sourceline_t *source_tail;
	int errorcount;		// error count
	int passnum;		// which pass are we on?
	const char *infile;	// input file
	const char *outfile;// output file
	const char *listfile;	// output listing file
	int debug;			// debug mode
	int outformat;		// output format type: 0 = decb, 1 = raw
	int execaddr;		// execution address for the program (END ....)
	macrotab_t *macros;	// pointer to macros
	int inmacro;		// are we in a macro?
	char *cur_namespace;	// current namespace prefix
	int skipcond;		// are we skipping a condition?
	int skipcount;		// number of endc we need before we stop skipping
	int skipmacro;		// are we skipping a macro definition?
	int noelse;			// skipped an else?
	int pragmas;		// what pragmas are in effect?
} asmstate_t;

#define PRAGMA_NOINDEX0TONONE	1


#ifndef __lwasm_c_seen__
extern int eval_expr(asmstate_t *as, sourceline_t *cl, char **optr, int *val);
extern void register_error(asmstate_t *as, sourceline_t *cl, int errcode);
extern int lookupreg3(const char *rlist, char **str);
extern int lookupreg(const char *reglist, char **str);
extern void lwasm_read_file(asmstate_t *as, char *fname);
extern void addcodebyte(asmstate_t *as, sourceline_t *cl, int cb);
#endif

#define SYMFLAG_NONE	0
#define SYMFLAG_SET		1

#ifndef __symtab_c_seen__
#include <stdio.h>
extern void register_symbol(asmstate_t *as, sourceline_t *cl, char *symstr, int val, int flags);
extern int lookup_symbol(asmstate_t *as, char *symstr);
extern void list_symbols(asmstate_t *as, FILE *f);
#endif


#ifndef __index_c_seen__
extern int parse_index_expr(asmstate_t *as, sourceline_t *cl, char **optr, int *postbyte, int *opmode, int *v1);
#endif

#define emit(cb)	do { addcodebyte(as, cl, (cb)); } while (0)
#define emitop(cb)	do { if ((cb) > 0xff) addcodebyte(as, cl, (cb)>>8); addcodebyte(as, cl, (cb) & 0xff); } while (0)
#define errorp1(e)	do { if (as -> passnum == 1) register_error(as, cl, (e)); } while (0)
#define errorp2(e)	do { if (as -> passnum == 2) register_error(as, cl, (e)); } while (0)

#endif //__lwasm_h_seen__