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