1 /* 2 the DASM macro assembler (aka small systems cross assembler) 3 4 Copyright (c) 1988-2002 by Matthew Dillon. 5 Copyright (c) 1995 by Olaf "Rhialto" Seibert. 6 Copyright (c) 2003-2008 by Andrew Davie. 7 Copyright (c) 2008 by Peter H. Froehlich. 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 2 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License along 20 with this program; if not, write to the Free Software Foundation, Inc., 21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 22 */ 23 24 /* 25 * ASM65.H 26 * 27 * Structures and definitions 28 */ 29 30 #include <stdio.h> 31 #include <string.h> 32 #include <stdlib.h> 33 #include <stdbool.h> 34 #include <stdint.h> 35 36 /* for -T option [phf] */ 37 typedef enum 38 { 39 SORTMODE_DEFAULT, 40 SORTMODE_ALPHA = SORTMODE_DEFAULT, 41 SORTMODE_ADDRESS, 42 SORTMODE_MAX 43 } sortmode_t; 44 45 /* for -E option [phf] */ 46 typedef enum 47 { 48 ERRORFORMAT_DEFAULT, 49 ERRORFORMAT_WOE = ERRORFORMAT_DEFAULT, 50 ERRORFORMAT_DILLON, 51 ERRORFORMAT_GNU, 52 ERRORFORMAT_MAX 53 } errorformat_t; 54 55 #define DAD 56 57 #ifdef DAD 58 59 enum FORMAT 60 { 61 FORMAT_DEFAULT = 1, 62 FORMAT_RAS, 63 FORMAT_RAW, 64 FORMAT_MAX 65 }; 66 67 #define MAXLINE 1024 68 #define MAX_SYM_LEN 1024 69 70 enum ASM_ERROR_EQUATES 71 { 72 ERROR_NONE = 0, 73 ERROR_COMMAND_LINE, /* Check format of command-line */ 74 ERROR_FILE_ERROR, /* Unable to open file */ 75 ERROR_NOT_RESOLVABLE, /* Source is not resolvable */ 76 ERROR_TOO_MANY_PASSES, /* Too many passes - something wrong */ 77 ERROR_NON_ABORT, /* one or more non-abort errors occured */ 78 79 ERROR_SYNTAX_ERROR, /* 0 */ 80 ERROR_EXPRESSION_TABLE_OVERFLOW, /* 1 */ 81 ERROR_UNBALANCED_BRACES, /* 2 */ 82 ERROR_DIVISION_BY_0, /* 3 */ 83 ERROR_UNKNOWN_MNEMONIC, /* 4 */ 84 ERROR_ILLEGAL_ADDRESSING_MODE, /* 5 */ 85 ERROR_ILLEGAL_FORCED_ADDRESSING_MODE, /* 6 */ 86 ERROR_NOT_ENOUGH_ARGUMENTS_PASSED_TO_MACRO, /* 7 */ 87 ERROR_PREMATURE_EOF, /* 8 */ 88 ERROR_ILLEGAL_CHARACTER, /* 9 */ 89 ERROR_BRANCH_OUT_OF_RANGE, /* 10 */ 90 ERROR_ERR_PSEUDO_OP_ENCOUNTERED, /* 11 */ 91 ERROR_ORIGIN_REVERSE_INDEXED, /* 12 */ 92 ERROR_EQU_VALUE_MISMATCH, /* 13 */ 93 ERROR_ADDRESS_MUST_BE_LT_100, /* 14 */ 94 ERROR_ADDRESS_MUST_BE_LT_10000, /* 15 */ 95 ERROR_ILLEGAL_BIT_SPECIFICATION, /* 16 */ 96 ERROR_NOT_ENOUGH_ARGS, /* 17 */ 97 ERROR_LABEL_MISMATCH, /* 18 */ 98 ERROR_MACRO_REPEATED, /* 19 */ 99 ERROR_VALUE_UNDEFINED, /* 20 */ 100 ERROR_PROCESSOR_NOT_SUPPORTED, /* 21 */ 101 ERROR_REPEAT_NEGATIVE, /* 22 */ 102 ERROR_BADERROR, /* 23 */ 103 ERROR_ONLY_ONE_PROCESSOR_SUPPORTED, /* Only allow one type of processor */ 104 ERROR_BAD_FORMAT, /* Bad format specifier */ 105 106 /* F8 support... */ 107 108 ERROR_VALUE_MUST_BE_1_OR_4, /* 26 */ 109 ERROR_VALUE_MUST_BE_LT_10, /* 27 */ 110 ERROR_VALUE_MUST_BE_LT_8, /* 28 */ 111 ERROR_VALUE_MUST_BE_LT_F, /* 29 */ 112 ERROR_VALUE_MUST_BE_LT_10000, /* 30 */ 113 ERROR_ILLEGAL_OPERAND_COMBINATION, /* 31 */ 114 115 ERROR_RECURSION_TOO_DEEP, /* 32 */ 116 ERROR_AVOID_SEGFAULT, /* 33 */ 117 ERROR_MISSING_ENDM, /* 34 */ 118 ERROR_MISSING_COMMENT_END, /* 35 */ 119 ERROR_SPURIOUS_COMMENT_CLOSE /* 36 */ 120 }; 121 122 typedef struct ERRORSTRUCT 123 { 124 int nErrorType; /* ASM_ERROR_EQUATES value */ 125 bool bFatal; /* 0 = OK, non-zero = cannot continue compilation */ 126 const char *sDescription; /* Error message */ 127 128 } ERROR_DEFINITION; 129 130 131 enum REASON_CODES 132 { 133 REASON_MNEMONIC_NOT_RESOLVED = 1 << 0, 134 REASON_OBSCURE = 1 << 1, /* fix this! */ 135 REASON_DC_NOT_RESOVED = 1 << 2, 136 REASON_DV_NOT_RESOLVED_PROBABLY = 1 << 3, 137 REASON_DV_NOT_RESOLVED_COULD = 1 << 4, 138 REASON_DS_NOT_RESOLVED = 1 << 5, 139 REASON_ALIGN_NOT_RESOLVED = 1 << 6, 140 REASON_ALIGN_RELOCATABLE_ORIGIN_NOT_KNOWN = 1 << 7, 141 REASON_ALIGN_NORMAL_ORIGIN_NOT_KNOWN = 1 << 8, 142 REASON_EQU_NOT_RESOLVED = 1 << 9, 143 REASON_EQU_VALUE_MISMATCH = 1 << 10, 144 REASON_IF_NOT_RESOLVED = 1 << 11, 145 REASON_REPEAT_NOT_RESOLVED = 1 << 12, 146 REASON_FORWARD_REFERENCE = 1 << 13, 147 REASON_PHASE_ERROR = 1 << 14, 148 REASON_BRANCH_OUT_OF_RANGE = 1 << 15 149 }; 150 151 152 #endif 153 154 #define MNEMONIC struct _MNE 155 #define MACRO struct _MACRO 156 #define INCFILE struct _INCFILE 157 #define REPLOOP struct _REPLOOP 158 #define IFSTACK struct _IFSTACK 159 #define SEGMENT struct _SEGMENT 160 #define SYMBOL struct _SYMBOL 161 #define STRLIST struct _STRLIST 162 163 #define DEFORGFILL 255 164 #define N_HASH_BITS 12 // 10 was in original implementation 165 #define SHASHSIZE (1 << N_HASH_BITS) 166 #define MHASHSIZE (1 << N_HASH_BITS) 167 #define SHASHAND (SHASHSIZE -1) 168 #define MHASHAND (MHASHSIZE -1) 169 #define ALLOCSIZE 16384 170 #define MAXMACLEVEL 32 171 #define TAB 9 172 173 174 enum ADDRESS_MODES { 175 AM_IMP, /* implied */ 176 AM_IMM8, /* immediate 8 bits */ 177 AM_IMM16, /* immediate 16 bits */ 178 AM_BYTEADR, /* address 8 bits */ 179 AM_BYTEADRX, /* index x + 8 bit offset */ 180 AM_BYTEADRY, /* index y + 8 bit offset */ 181 AM_WORDADR, /* extended addr */ 182 AM_WORDADRX, /* index x + 16 bit offset */ 183 AM_WORDADRY, /* index y + 16 bit offset */ 184 AM_REL, /* relative 8 bits */ 185 AM_INDBYTEX, /* indirect x */ 186 AM_INDBYTEY, /* indirect y */ 187 AM_INDWORD, /* indirect immediate */ 188 AM_0X, /* index x 0 bits */ 189 AM_0Y, /* index y 0 bits */ 190 AM_BITMOD, /* spec. bit modifcation */ 191 AM_BITBRAMOD, /* spec. bit-test rel. branch */ 192 AM_BYTEADR_SP, /* index SP +8 bits */ 193 AM_WORDADR_SP, /* index SP +16 bits */ 194 195 196 AM_SYMBOL, 197 AM_EXPLIST, 198 AM_LONG, 199 AM_BSS, 200 201 AM_OTHER_ENDIAN, /* force little endian to DC on big endian machines and the other way round */ 202 203 NUMOC 204 }; 205 206 #define AF_IMP ( 1L << AM_IMP ) 207 #define AF_IMM8 ( 1L << AM_IMM8 ) 208 #define AF_IMM16 ( 1L << AM_IMM16 ) 209 #define AF_BYTEADR ( 1L << AM_BYTEADR ) 210 #define AF_BYTEADRX ( 1L << AM_BYTEADRX ) 211 #define AF_BYTEADRY ( 1L << AM_BYTEADRY ) 212 #define AF_WORDADR ( 1L << AM_WORDADR ) 213 #define AF_WORDADRX ( 1L << AM_WORDADRX ) 214 #define AF_WORDADRY ( 1L << AM_WORDADRY ) 215 #define AF_REL ( 1L << AM_REL ) 216 #define AF_INDBYTEX ( 1L << AM_INDBYTEX ) 217 #define AF_INDBYTEY ( 1L << AM_INDBYTEY ) 218 #define AF_INDWORD ( 1L << AM_INDWORD ) 219 #define AF_0X ( 1L << AM_0X ) 220 #define AF_0Y ( 1L << AM_0Y ) 221 #define AF_BITMOD ( 1L << AM_BITMOD ) 222 #define AF_BITBRAMOD ( 1L << AM_BITBRAMOD ) 223 #define AF_BYTEADR_SP ( 1L << AM_BYTEADR_SP) 224 #define AF_WORDADR_SP ( 1L << AM_WORDADR_SP) 225 226 #define AM_BYTE AM_BYTEADR 227 #define AM_WORD AM_WORDADR 228 229 230 231 STRLIST { 232 STRLIST *next; 233 char buf[4]; 234 }; 235 236 #define STRLISTSIZE sizeof(STRLIST *) 237 238 #define MF_BEGM 0x02 239 #define MF_IF 0x04 240 #define MF_MACRO 0x08 241 #define MF_MASK 0x10 /* has mask argument (byte) */ 242 #define MF_REL 0x20 /* has rel. address (byte) */ 243 #define MF_IMOD 0x40 /* instruction byte mod. */ 244 #define MF_ENDM 0x80 /* is v_endm */ 245 246 MNEMONIC { 247 MNEMONIC *next; /* hash */ 248 void (*vect)(char *, MNEMONIC *); /* dispatch */ 249 const char *name; /* actual name */ 250 unsigned char flags; /* special flags */ 251 unsigned long okmask; 252 unsigned int opcode[NUMOC]; /* hex codes, byte or word (>xFF) opcodes */ 253 }; 254 255 /* MNEMONIC with all fields 0, used as end-of-table marker. */ 256 #define MNEMONIC_NULL {NULL, NULL, NULL, 0, 0, {0,}} 257 258 MACRO { 259 MACRO *next; 260 void (*vect)(char *, MACRO *); 261 char *name; 262 unsigned char flags; 263 STRLIST *strlist; 264 int defpass; 265 }; 266 267 #define INF_MACRO 0x01 268 #define INF_NOLIST 0x02 269 270 INCFILE { 271 INCFILE *next; /* previously pushed context */ 272 char *name; /* file name */ 273 FILE *fi; /* file handle */ 274 unsigned long lineno; /* line number in file */ 275 unsigned char flags; /* flags (macro) */ 276 277 /* Only if Macro */ 278 279 STRLIST *args; /* arguments to macro */ 280 STRLIST *strlist; /* current string list */ 281 unsigned long saveidx; /* save localindex */ 282 unsigned long savedolidx; /* save localdollarindex */ 283 284 }; 285 286 #define RPF_UNKNOWN 0x01 /* value unknown */ 287 288 REPLOOP { 289 REPLOOP *next; /* previously pushed context */ 290 unsigned long count; /* repeat count */ 291 unsigned long seek; /* seek to top of repeat */ 292 unsigned long lineno; /* line number of line before */ 293 INCFILE *file; /* which include file are we in*/ 294 unsigned char flags; 295 }; 296 297 #define IFF_UNKNOWN 0x01 /* value unknown */ 298 #define IFF_BASE 0x04 299 300 IFSTACK { 301 IFSTACK *next; /* previous IF */ 302 INCFILE *file; /* which include file are we in*/ 303 unsigned char flags; 304 unsigned char xtrue; /* 1 if true, 0 if false */ 305 unsigned char acctrue;/* accumulatively true (not incl this one) */ 306 }; 307 308 #define SF_UNKNOWN 0x01 /* ORG unknown */ 309 #define SF_REF 0x04 /* ORG referenced */ 310 #define SF_BSS 0x10 /* uninitialized area (U flag) */ 311 #define SF_RORG 0x20 /* relocatable origin active */ 312 313 SEGMENT { 314 SEGMENT *next; /* next segment in segment list */ 315 char *name; /* name of segment */ 316 unsigned char flags; /* for ORG */ 317 unsigned char rflags; /* for RORG */ 318 unsigned long org; /* current org */ 319 unsigned long rorg; /* current rorg */ 320 unsigned long initorg; 321 unsigned long initrorg; 322 unsigned char initflags; 323 unsigned char initrflags; 324 }; 325 326 #define SYM_UNKNOWN 0x01 /* value unknown */ 327 #define SYM_REF 0x04 /* referenced */ 328 #define SYM_STRING 0x08 /* result is a string */ 329 #define SYM_SET 0x10 /* SET instruction used */ 330 #define SYM_MACRO 0x20 /* symbol is a macro */ 331 #define SYM_MASREF 0x40 /* master reference */ 332 333 SYMBOL { 334 SYMBOL *next; /* next symbol in hash list */ 335 char *name; /* symbol name or string if expr. */ 336 char *string; /* if symbol is actually a string */ 337 unsigned char flags; /* flags */ 338 unsigned char addrmode; /* addressing mode (expressions) */ 339 long value; /* current value, never EVER change this to unsigned! */ 340 unsigned int namelen; /* name length */ 341 }; 342 343 extern SYMBOL *SHash[]; 344 extern MNEMONIC *MHash[]; 345 extern INCFILE *pIncfile; 346 extern REPLOOP *Reploop; 347 extern SEGMENT *Seglist; 348 extern IFSTACK *Ifstack; 349 350 extern SEGMENT *Csegment; /* current segment */ 351 extern char *Av[]; 352 extern char Avbuf[]; 353 /*extern unsigned int Adrbytes[];*/ 354 extern unsigned int Cvt[]; 355 extern MNEMONIC Ops[]; 356 extern unsigned int Opsize[]; 357 extern int Mnext; /* mnemonic extension */ 358 extern unsigned int Mlevel; 359 360 extern bool bTrace; 361 extern bool Xdebug; 362 extern bool bStrictMode; 363 extern unsigned char MsbOrder; 364 extern unsigned char Outputformat; 365 extern unsigned long Redo_why; 366 367 extern unsigned long maxFileSize; 368 369 extern int Redo; 370 extern int Redo_eval; 371 372 extern unsigned long Redo_if; 373 extern unsigned long Localindex, Lastlocalindex; 374 extern unsigned long Localdollarindex, Lastlocaldollarindex; 375 extern int F_format; 376 extern sortmode_t F_sortmode; /* -T option [phf] */ 377 extern errorformat_t F_errorformat; /* -E option [phf] */ 378 extern unsigned char F_verbose; 379 extern const char *F_outfile; 380 extern char *F_listfile; 381 extern char *F_symfile; 382 extern FILE *FI_listfile; 383 extern FILE *FI_temp; 384 extern unsigned char Fisclear; 385 extern unsigned long Plab, Pflags; 386 extern char Inclevel; 387 extern char ListMode; 388 extern unsigned long Processor; 389 390 /*extern unsigned int _fmode;*/ 391 extern unsigned long CheckSum; 392 393 extern int nMacroDeclarations; 394 extern int nMacroClosings; 395 396 /* main.c */ 397 /*extern unsigned char Listing;*/ 398 void findext(char *str); 399 int asmerr(int err, bool bAbort, const char *sText); 400 char *sftos(long val, int flags); 401 void rmnode(void **base, int bytes); 402 void addhashtable(MNEMONIC *mne); 403 void pushinclude(char *str); 404 char *permalloc(int bytes); 405 char *zmalloc(int bytes); 406 char *ckmalloc(int bytes); 407 char *strlower(char *str); 408 void addmsg(char *message); 409 410 /* symbols.c */ 411 void setspecial(int value, int flags); 412 SYMBOL *allocsymbol(void); 413 SYMBOL *findsymbol(const char *str, int len); 414 SYMBOL *CreateSymbol( const char *str, int len ); 415 void FreeSymbolList(SYMBOL *sym); 416 void programlabel(void); 417 418 /* ops.c */ 419 extern unsigned char Gen[]; 420 extern int Glen; 421 void v_set(char *str, MNEMONIC *); 422 void v_setstr(char *str, MNEMONIC *); 423 void v_mexit(char *str, MNEMONIC *); 424 void closegenerate(void); 425 void generate(void); 426 427 void v_list(char *, MNEMONIC *); 428 void v_include(char *, MNEMONIC *); 429 void v_seg(char *, MNEMONIC *); 430 void v_dc(char *, MNEMONIC *); 431 void v_ds(char *, MNEMONIC *); 432 void v_org(char *, MNEMONIC *); 433 void v_rorg(char *, MNEMONIC *); 434 void v_rend(char *, MNEMONIC *); 435 void v_align(char *, MNEMONIC *); 436 void v_subroutine(char *, MNEMONIC *); 437 void v_equ(char *, MNEMONIC *); 438 void v_eqm(char *, MNEMONIC *); 439 void v_set(char *, MNEMONIC *); 440 void v_macro(char *, MNEMONIC *); 441 void v_endm(char *, MNEMONIC *); 442 void v_mexit(char *, MNEMONIC *); 443 void v_ifconst(char *, MNEMONIC *); 444 void v_ifnconst(char *, MNEMONIC *); 445 void v_if(char *, MNEMONIC *); 446 void v_else(char *, MNEMONIC *); 447 void v_endif(char *, MNEMONIC *); 448 void v_repeat(char *, MNEMONIC *); 449 void v_repend(char *, MNEMONIC *); 450 void v_err(char *, MNEMONIC *); 451 void v_hex(char *, MNEMONIC *); 452 void v_trace(char *, MNEMONIC *); 453 void v_end(char *, MNEMONIC *); 454 void v_echo(char *, MNEMONIC *); 455 void v_processor(char *, MNEMONIC *); 456 void v_incbin(char *, MNEMONIC *); 457 void v_incdir(char *, MNEMONIC *); 458 void v_execmac(char *str, MACRO *mac); 459 void v_mnemonic(char *str, MNEMONIC *mne); 460 461 FILE *pfopen(const char *, const char *); 462 463 464 /* exp.c */ 465 SYMBOL *eval(const char *str, int wantmode); 466 467 468 469 470 471 /* end of asm.h */ 472