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