* asranlib/asranlib.c, link/lkar.h, link/lkar.c:
[fw/sdcc] / as / z80 / asm.h
1 /* asm.h
2
3    Copyright (C) 1989-1995 Alan R. Baldwin
4    721 Berkeley St., Kent, Ohio 44240
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19 /*
20  * 10-Nov-07 borutr:
21  *           - add proto for strsto
22  *           - change s_id from [NCPS] to pointer
23  *           - change m_id from [NCPS] to pointer
24  *           - change a_id from [NCPS] to pointer
25  *           - change NCPS to 80
26  *           - case sensitive
27  *           - always define "ccase"
28  */
29
30 /*
31  * Extensions: P. Felber
32  */
33
34 #define VERSION "V01.75 + SDCC mods"
35
36 /*
37  * Case Sensitivity Flag
38  */
39 #define CASE_SENSITIVE  1
40
41 /*)Module       asm.h
42  *
43  *      The module asm.h contains the definitions for constants,
44  *      structures, global variables, and ASxxxx functions
45  *      contained in the ASxxxx.c files.  The two functions
46  *      and three global variables from the machine dependent
47  *      files are also defined.
48  */
49
50 /*
51  *       compiler/operating system specific definitions
52  */
53
54 /* DECUS C void definition */
55 /* File/extension seperator */
56
57 #ifdef  decus
58 #define VOID    char
59 #define FSEPX   '.'
60 #endif
61
62 /* PDOS C void definition */
63 /* File/extension seperator */
64
65 #ifdef  PDOS
66 #define VOID    char
67 #define FSEPX   ':'
68 #endif
69
70 /* Default void definition */
71 /* File/extension seperator */
72
73 #ifndef VOID
74 #define VOID    void
75 #define FSEPX   '.'
76 #define OTHERSYSTEM
77 #endif
78
79 /*
80  * PATH_MAX
81  */
82 #include <limits.h>
83 #ifndef PATH_MAX                                /* POSIX, but not required   */
84 #if defined(_MSC_VER) || defined(__BORLANDC__)  /* Microsoft C or Borland C*/
85 #include <stdlib.h>
86 #define PATH_MAX        _MAX_PATH
87 #else
88 #define PATH_MAX                                /* define a reasonable value */
89 #endif
90 #endif
91
92 #ifdef _WIN32       /* WIN32 native */
93
94 #  define NATIVE_WIN32          1
95 #  ifdef __MINGW32__  /* GCC MINGW32 depends on configure */
96 #    include "../../sdccconf.h"
97 #  else
98 #    include "../../sdcc_vc.h"
99 #    define PATH_MAX  _MAX_PATH
100 #  endif
101
102 #else               /* Assume Un*x style system */
103 #  include "../../sdccconf.h"
104 #endif
105
106 /*
107  * Assembler definitions.
108  */
109 #define LFTERM  '('             /* Left expression delimeter */
110 #define RTTERM  ')'             /* Right expression delimeter */
111
112 #ifdef SDK
113 #define NCPS    80              /* characters per symbol */
114 #else /* SDK */
115 #define NCPS    8               /* Chars. per symbol */
116 #endif /* SDK */
117 /* #define      NCPS    32 */   /* Chars. per symbol */
118 #define HUGE    1000            /* A huge number */
119 #define NERR    3               /* Errors per line */
120 #define NINPUT  1024            /* Input buffer size */
121 #define NCODE   128             /* Listing code buffer size */
122 #define NTITL   64              /* Title buffer size */
123 #define NSBTL   64              /* SubTitle buffer size */
124 #define NHASH   64              /* Buckets in hash table */
125 #define HMASK   077             /* Hash mask */
126 #define NLPP    60              /* Lines per page */
127 #define MAXFIL  6               /* Maximum command line input files */
128 #define MAXINC  6               /* Maximum nesting of include files */
129 #define MAXIF   10              /* Maximum nesting of if/else/endif */
130 #define FILSPC  256             /* Chars. in filespec */
131
132 #define NLIST   0               /* No listing */
133 #define SLIST   1               /* Source only */
134 #define ALIST   2               /* Address only */
135 #define BLIST   3               /* Address only with allocation */
136 #define CLIST   4               /* Code */
137 #define ELIST   5               /* Equate only */
138
139 #define dot     sym[0]          /* Dot, current loc */
140 #define dca     area[0]         /* Dca, default code area */
141
142
143 typedef unsigned int Addr_T;
144
145 /*
146  *      The area structure contains the parameter values for a
147  *      specific program or data section.  The area structure
148  *      is a linked list of areas.  The initial default area
149  *      is "_CODE" defined in asdata.c, the next area structure
150  *      will be linked to this structure through the structure
151  *      element 'struct area *a_ap'.  The structure contains the
152  *      area name, area reference number ("_CODE" is 0) determined
153  *      by the order of .area directives, area size determined
154  *      from the total code and/or data in an area, area fuzz is
155  *      a variable used to track pass to pass changes in the
156  *      area size caused by variable length instruction formats,
157  *      and area flags which specify the area's relocation type.
158  */
159 struct  area
160 {
161         struct  area *a_ap;     /* Area link */
162         char    *a_id;          /* Area Name */
163         int     a_ref;          /* Ref. number */
164         Addr_T  a_size;         /* Area size */
165         Addr_T  a_fuzz;         /* Area fuzz */
166         int     a_flag;         /* Area flags */
167 };
168
169 /*
170  *      The "A_" area constants define values used in
171  *      generating the assembler area output data.
172  *
173  * Area flags
174  *
175  *         7     6      5    4     3     2     1     0
176  *      +-----+-----+-----+-----+-----+-----+-----+-----+
177  *      |     |     |     | PAG | ABS | OVR |     |     |
178  *      +-----+-----+-----+-----+-----+-----+-----+-----+
179  */
180
181 #define A_CON   000             /* Concatenating */
182 #define A_OVR   004             /* Overlaying */
183 #define A_REL   000             /* Relocatable */
184 #define A_ABS   010             /* absolute */
185 #define A_NOPAG 000             /* Non-Paged */
186 #define A_PAG   020             /* Paged */
187
188 /*
189  *      The "R_" relocation constants define values used in
190  *      generating the assembler relocation output data for
191  *      areas, symbols, and code.
192  *
193  * Relocation flags
194  *
195  *         7     6     5     4     3     2     1     0
196  *      +-----+-----+-----+-----+-----+-----+-----+-----+
197  *      | MSB | PAGn| PAG0| USGN| BYT2| PCR | SYM | BYT |
198  *      +-----+-----+-----+-----+-----+-----+-----+-----+
199  */
200
201 #define R_WORD  0000            /* 16 bit */
202 #define R_BYTE  0001            /*  8 bit */
203
204 #define R_AREA  0000            /* Base type */
205 #define R_SYM   0002
206
207 #define R_NORM  0000            /* PC adjust */
208 #define R_PCR   0004
209
210 #define R_BYT1  0000            /* Byte count for R_BYTE = 1 */
211 #define R_BYT2  0010            /* Byte count for R_BYTE = 2 */
212
213 #define R_SGND  0000            /* Signed Byte */
214 #define R_USGN  0020            /* Unsigned Byte */
215
216 #define R_NOPAG 0000            /* Page Mode */
217 #define R_PAG0  0040            /* Page '0' */
218 #define R_PAG   0100            /* Page 'nnn' */
219
220 #define R_LSB   0000            /* low byte */
221 #define R_MSB   0200            /* high byte */
222
223 /*
224  * Listing Control Flags
225  */
226
227 #define R_HIGH  0040000         /* High Byte */
228 #define R_RELOC 0100000         /* Relocation */
229
230 #define R_DEF   00              /* Global def. */
231 #define R_REF   01              /* Global ref. */
232 #define R_REL   00              /* Relocatable */
233 #define R_ABS   02              /* Absolute */
234 #define R_GBL   00              /* Global */
235 #define R_LCL   04              /* Local */
236
237 /*
238  *      The mne structure is a linked list of the assembler
239  *      mnemonics and directives.  The list of mnemonics and
240  *      directives contained in the device dependent file
241  *      xxxpst.c are hashed and linked into NHASH lists in
242  *      module assym.c by syminit().  The structure contains
243  *      the mnemonic/directive name, a subtype which directs
244  *      the evaluation of this mnemonic/directive, a flag which
245  *      is used to detect the end of the mnemonic/directive
246  *      list in xxxpst.c, and a value which is normally
247  *      associated with the assembler mnemonic base instruction
248  *      value.
249  */
250 struct  mne
251 {
252         struct  mne *m_mp;      /* Hash link */
253         char    *m_id;          /* Mnemonic */
254         char    m_type;         /* Mnemonic subtype */
255         char    m_flag;         /* Mnemonic flags */
256         Addr_T  m_valu;         /* Value */
257 };
258
259 /*
260  *      The sym structure is a linked list of symbols defined
261  *      in the assembler source files.  The first symbol is "."
262  *      defined in asdata.c.  The entry 'struct tsym *s_tsym'
263  *      links any temporary symbols following this symbol and
264  *      preceeding the next normal symbol.  The structure also
265  *      contains the symbol's name, type (USER or NEW), flag
266  *      (global, assigned, and multiply defined), a pointer
267  *      to the area structure defining where the symbol is
268  *      located, a reference number assigned by outgsd() in
269  *      asout.c, and the symbols address relative to the base
270  *      address of the area where the symbol is located.
271  */
272 struct  sym
273 {
274         struct  sym  *s_sp;     /* Hash link */
275         struct  tsym *s_tsym;   /* Temporary symbol link */
276         char    *s_id;          /* Symbol */
277         char    s_type;         /* Symbol subtype */
278         char    s_flag;         /* Symbol flags */
279         struct  area *s_area;   /* Area line, 0 if absolute */
280         int     s_ref;          /* Ref. number */
281         Addr_T  s_addr;         /* Address */
282 };
283
284 #define S_GBL           01      /* Global */
285 #define S_ASG           02      /* Assigned */
286 #define S_MDF           04      /* Mult. def */
287 #define S_END           010     /* End mark for pst. */
288
289 #define S_NEW           0       /* New name */
290 #define S_USER          1       /* User name */
291                                 /* unused slot */
292                                 /* unused slot */
293                                 /* unused slot */
294
295 #define S_BYTE          5       /* .byte */
296 #define S_WORD          6       /* .word */
297 #define S_ASCII         7       /* .ascii */
298 #define S_ASCIZ         8       /* .asciz */
299 #define S_BLK           9       /* .blkb or .blkw */
300 #define S_INCL          10      /* .include */
301 #define S_DAREA         11      /* .area */
302 #define S_ATYP          12      /* .area type */
303 #define S_AREA          13      /* .area name */
304 #define S_GLOBL         14      /* .globl */
305 #define S_PAGE          15      /* .page */
306 #define S_TITLE         16      /* .title */
307 #define S_SBTL          17      /* .sbttl */
308 #define S_IF            18      /* .if */
309 #define S_ELSE          19      /* .else */
310 #define S_ENDIF         20      /* .endif */
311 #define S_EVEN          21      /* .even */
312 #define S_ODD           22      /* .odd */
313 #define S_RADIX         23      /* .radix */
314 #define S_ORG           24      /* .org */
315 #define S_MODUL         25      /* .module */
316 #define S_ASCIS         26      /* .ascis */
317 #ifdef SDK
318 # define S_FLOAT        27      /* .df */
319 #endif
320 #define S_OPTSDCC       28  /* .optsdcc */
321
322 /*
323  *      The tsym structure is a linked list of temporary
324  *      symbols defined in the assembler source files following
325  *      a normal symbol.  The structure contains the temporary
326  *      symbols number, a flag (multiply defined), a pointer to the
327  *      area structure defining where the temporary structure
328  *      is located, and the temporary symbol's address relative
329  *      to the base address of the area where the symbol
330  *      is located.
331  */
332 struct  tsym
333 {
334     struct      tsym *t_lnk;    /* Link to next */
335     int t_num;          /* 0-lots$ */
336     char t_flg;         /* flags */
337     struct      area *t_area;   /* Area */
338     Addr_T      t_addr;         /* Address */
339 };
340
341 /*
342  *      External Definitions for all Global Variables
343  */
344
345 extern  int     aserr;          /*      ASxxxx error counter
346                                  */
347 extern  jmp_buf jump_env;       /*      compiler dependent structure
348                                  *      used by setjmp() and longjmp()
349                                  */
350 extern  int     inpfil;         /*      count of assembler
351                                  *      input files specified
352                                  */
353 extern  int     incfil;         /*      current file handle index
354                                  *      for include files
355                                  */
356 extern  int     cfile;          /*      current file handle index
357                                  *      of input assembly files
358                                  */
359 extern  int     flevel;         /*      IF-ELSE-ENDIF flag will be non
360                                  *      zero for false conditional case
361                                  */
362 extern  int     tlevel;         /*      current conditional level
363                                  */
364 extern  int     ifcnd[MAXIF+1]; /*      array of IF statement condition
365                                  *      values (0 = FALSE) indexed by tlevel
366                                  */
367 extern  int     iflvl[MAXIF+1]; /*      array of IF-ELSE-ENDIF flevel
368                                  *      values indexed by tlevel
369                                  */
370 extern  char
371         afn[FILSPC];            /*      afile() temporary filespec
372                                  */
373 extern  char
374         srcfn[MAXFIL][FILSPC];  /*      array of source file names
375                                  */
376 extern  int
377         srcline[MAXFIL];        /*      current source file line
378                                  */
379 extern  char
380         incfn[MAXINC][FILSPC];  /*      array of include file names
381                                  */
382 extern  int
383         incline[MAXINC];        /*      current include file line
384                                  */
385 extern  int     radix;          /*      current number conversion radix:
386                                  *      2 (binary), 8 (octal), 10 (decimal),
387                                  *      16 (hexadecimal)
388                                  */
389 extern  int     line;           /*      current assembler source
390                                  *      line number
391                                  */
392 extern  int     page;           /*      current page number
393                                  */
394 extern  int     lop;            /*      current line number on page
395                                  */
396 extern  int     pass;           /*      assembler pass number
397                                  */
398 extern  int     lflag;          /*      -l, generate listing flag
399                                  */
400 extern  int     cflag;          /*      -c, generate sdcdb debug information
401                                  */
402 extern  int     gflag;          /*      -g, make undefined symbols global flag
403                                  */
404 extern  int     aflag;          /*      -a, make all symbols global flag
405                                  */
406 extern  int     jflag;          /*      -j, generate debug information flag
407                                 */
408 extern  int     oflag;          /*      -o, generate relocatable output flag
409                                  */
410 extern  int     sflag;          /*      -s, generate symbol table flag
411                                  */
412 extern  int     pflag;          /*      -p, enable listing pagination
413                                  */
414 extern  int     xflag;          /*      -x, listing radix flag
415                                  */
416 extern  int     fflag;          /*      -f(f), relocations flagged flag
417                                  */
418 extern  Addr_T  laddr;          /*      address of current assembler line
419                                  *      or value of .if argument
420                                  */
421 extern  Addr_T  fuzz;           /*      tracks pass to pass changes in the
422                                  *      address of symbols caused by
423                                  *      variable length instruction formats
424                                  */
425 extern  int     lmode;          /*      listing mode
426                                  */
427 extern  struct  area    area[]; /*      array of 1 area
428                                  */
429 extern  struct  area *areap;    /*      pointer to an area structure
430                                  */
431 extern  struct  sym     sym[];  /*      array of 1 symbol
432                                  */
433 extern  struct  sym *symp;      /*      pointer to a symbol structure
434                                  */
435 extern  struct  sym *symhash[NHASH]; /* array of pointers to NHASH
436                                       * linked symbol lists
437                                       */
438 extern  struct  mne *mnehash[NHASH]; /* array of pointers to NHASH
439                                       * linked mnemonic/directive lists
440                                       */
441 extern  char    *ep;            /*      pointer into error list
442                                  *      array eb[NERR]
443                                  */
444 extern  char    eb[NERR];       /*      array of generated error codes
445                                  */
446 extern  const char *ip;         /*      pointer into the assembler-source
447                                  *      text line in ib[]
448                                  */
449 extern  const char *ib;         /*      assembler-source text line
450                                  */
451 extern  char    *cp;            /*      pointer to assembler output
452                                  *      array cb[]
453                                  */
454 extern  char    cb[NCODE];      /*      array of assembler output values
455                                  */
456 extern  int     *cpt;           /*      pointer to assembler relocation type
457                                  *      output array cbt[]
458                                  */
459 extern  int     cbt[NCODE];     /*      array of assembler relocation types
460                                  *      describing the data in cb[]
461                                  */
462 extern  char    tb[NTITL];      /*      Title string buffer
463                                  */
464 extern  char    stb[NSBTL];     /*      Subtitle string buffer
465                                  */
466 extern  char    optsdcc[NINPUT]; /*     sdcc compile options
467                                   */
468 extern  char    symtbl[];       /*      string "Symbol Table"
469                                  */
470 extern  char    aretbl[];       /*      string "Area Table"
471                                  */
472 extern  char    module[NCPS];   /*      module name string
473                                  */
474 extern  FILE    *lfp;           /*      list output file handle
475                                  */
476 extern  FILE    *ofp;           /*      relocation output file handle
477                                  */
478 extern  FILE    *tfp;           /*      symbol table output file handle
479                                  */
480 extern  FILE    *sfp[MAXFIL];   /*      array of assembler-source file handles
481                                  */
482 extern  FILE    *ifp[MAXINC];   /*      array of include-file file handles
483                                  */
484 extern  unsigned char ctype[128]; /*    array of character types, one per
485                                    *    ASCII character
486                                    */
487 extern  char    ccase[128];     /* an array of characters which
488                                  * perform the case translation function
489                                  */
490
491 /*
492  * Definitions for Character Types
493  */
494 #define SPACE   0000
495 #define ETC     0000
496 #define LETTER  0001
497 #define DIGIT   0002
498 #define BINOP   0004
499 #define RAD2    0010
500 #define RAD8    0020
501 #define RAD10   0040
502 #define RAD16   0100
503 #define ILL     0200
504
505 #define DGT2    DIGIT|RAD16|RAD10|RAD8|RAD2
506 #define DGT8    DIGIT|RAD16|RAD10|RAD8
507 #define DGT10   DIGIT|RAD16|RAD10
508 #define LTR16   LETTER|RAD16
509
510 /*
511  *      The exp structure is used to return the evaluation
512  *      of an expression.  The structure supports three valid
513  *      cases:
514  *      (1)     The expression evaluates to a constant,
515  *              mode = S_USER, flag = 0, addr contains the
516  *              constant, and base = NULL.
517  *      (2)     The expression evaluates to a defined symbol
518  *              plus or minus a constant, mode = S_USER,
519  *              flag = 0, addr contains the constant, and
520  *              base = pointer to area symbol.
521  *      (3)     The expression evaluates to a external
522  *              global symbol plus or minus a constant,
523  *              mode = S_NEW, flag = 1, addr contains the
524  *              constant, and base = pointer to symbol.
525  */
526 struct  expr
527 {
528         char    e_mode;         /* Address mode */
529         char    e_flag;         /* Symbol flag */
530         Addr_T  e_addr;         /* Address */
531         union   {
532                 struct area *e_ap;
533                 struct sym  *e_sp;
534         } e_base;               /* Rel. base */
535         char    e_rlcf;         /* Rel. flags */
536 };
537
538 /* C Library functions */
539 /* for reference only
540 extern  VOID            exit();
541 extern  int             fclose();
542 extern  char *          fgets();
543 extern  FILE *          fopen();
544 extern  int             fprintf();
545 extern  VOID            longjmp();
546 extern  VOID *          malloc();
547 extern  int             printf();
548 extern  char            putc();
549 extern  int             rewind();
550 extern  int             setjmp();
551 extern  int             strcmp();
552 extern  char *          strcpy();
553 extern  int             strlen();
554 extern  char *          strncpy();
555 */
556
557 /* Machine independent functions */
558
559 /* asmain.c */
560 extern  FILE *          afile();
561 extern  VOID            asexit();
562 extern  VOID            asmbl();
563 extern  int             main();
564 extern  VOID            newdot();
565 extern  VOID            phase();
566 extern  VOID            usage();
567
568 /* aslex.c */
569 extern  char            endline();
570 extern  char            get();
571 extern  VOID            getid();
572 extern  int             as_getline();
573 extern  int             getmap();
574 extern  char            getnb();
575 extern  VOID            getst();
576 extern  int             more();
577 extern  VOID            unget();
578
579 /* assym.c */
580 extern  struct  area *  alookup();
581 extern  struct  mne *   mlookup();
582 extern  int             hash();
583 extern  struct  sym *   lookup();
584 extern  VOID *          new();
585 extern  char *          strsto(char *str);
586 extern  int             symeq();
587 extern  VOID            syminit();
588 extern  VOID            symglob();
589 extern  VOID            allglob();
590
591 /* assubr.c */
592 extern  VOID            aerr();
593 extern  VOID            diag();
594 extern  VOID            err();
595 extern  char *          geterr();
596 extern  VOID            qerr();
597 extern  VOID            rerr();
598
599 /* asexpr.c */
600 extern  VOID            abscheck();
601 extern  Addr_T          absexpr();
602 extern  VOID            clrexpr();
603 extern  int             digit();
604 extern  int             is_abs();
605 extern  VOID            expr();
606 extern  int             oprio();
607 extern  VOID            term();
608
609 /* aslist.c */
610 extern  VOID            list();
611 extern  VOID            list1();
612 extern  VOID            list2();
613 extern  VOID            lstsym();
614 extern  VOID            slew();
615
616 /* asout.c */
617 extern  int             hibyte();
618 extern  int             lobyte();
619 extern  VOID            out();
620 extern  VOID            outab();
621 extern  VOID            outarea();
622 extern  VOID            outaw();
623 extern  VOID            outall();
624 extern  VOID            outdot();
625 extern  VOID            outbuf();
626 extern  VOID            outchk();
627 extern  VOID            outgsd();
628 extern  VOID            outrb();
629 extern  VOID            outrw();
630 extern  VOID            outsym();
631 extern  VOID            out_lb();
632 extern  VOID            out_lw();
633 extern  VOID            out_rw();
634 extern  VOID            out_tw();
635
636 /* asnoice.c */
637 extern void DefineNoICE_Line();
638 extern void DefineCDB_Line();
639
640
641 /* Machine dependent variables */
642
643 extern  char *          cpu;
644 extern  char *          dsft;
645 extern  int             hilo;
646 extern  struct  mne     mne[];
647
648 /* Machine dependent functions */
649
650 extern  VOID            minit();
651
652 /* strcmpi.c */
653 extern  int as_strcmpi(const char *s1, const char *s2);
654 extern  int as_strncmpi(const char *s1, const char *s2, size_t n);