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