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