* as/z80/asm.h: made CASE_SENSITIVE 1
[fw/sdcc] / link / z80 / aslink.h
1 /* aslink.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
12 /*
13  * Extensions: P. Felber
14  */
15 #define VERSION "V01.75"
16
17 /*
18  * Case Sensitivity Flag
19  */
20 #define CASE_SENSITIVE  1
21
22 /*)Module       asmlnk.h
23  *
24  *      The module asmlnk.h contains the definitions for constants,
25  *      structures, global variables, and LKxxxx functions
26  *      contained in the LKxxxx.c files.
27  */
28
29 /*)BUILD
30         $(PROGRAM) =    ASLINK
31         $(INCLUDE) =    ASLINK.H
32         $(FILES) = {
33                 LKMAIN.C
34                 LKLEX.C
35                 LKAREA.C
36                 LKHEAD.C
37                 LKSYM.C
38                 LKEVAL.C
39                 LKDATA.C
40                 LKLIST.C
41                 LKRLOC.C
42                 LKLIBR.C
43                 LKS19.C
44                 LKIHX.C
45         }
46         $(STACK) = 2000
47 */
48
49 /* DECUS C void definition */
50 /* File/extension seperator */
51
52 #ifdef  decus
53 #define VOID    char
54 #define FSEPX   '.'
55 #endif
56
57 /* PDOS C void definition */
58 /* File/extension seperator */
59
60 #ifdef  PDOS
61 #define VOID    char
62 #define FSEPX   ':'
63 #endif
64
65 /* Default void definition */
66 /* File/extension seperator */
67
68 #ifndef VOID
69 #define VOID    void
70 #define FSEPX   '.'
71 #define OTHERSYSTEM
72 #endif
73
74 /*
75  * PATH_MAX
76  */
77 #include <limits.h>
78 #ifndef PATH_MAX                /* POSIX, but not required   */
79  #if defined(__BORLANDC__) || defined(_MSC_VER)
80   #include <stdlib.h>
81   #define PATH_MAX      _MAX_PATH
82  #else
83   #define PATH_MAX      255     /* define a reasonable value */
84  #endif
85 #endif
86
87 /*
88  * This file defines the format of the
89  * relocatable binary file.
90  */
91
92 #ifdef SDK
93 #define NCPS    80              /* characters per symbol.  Used to be 32... */
94 #else /* SDK */
95 #define NCPS    8               /* characters per symbol */
96 #endif /* SDK */
97 /* #define      NCPS    32 */   /* characters per symbol */
98 #define NDATA   16              /* actual data */
99 #define NINPUT  PATH_MAX        /* Input 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 NTXT    16              /* T values */
104 #define FILSPC  PATH_MAX        /* File spec length */
105
106 /*
107  *      The "R_" relocation constants define values used in
108  *      generating the assembler relocation output data for
109  *      areas, symbols, and code.
110  *
111  *
112  *      Relocation types.
113  *
114  *             7     6     5     4     3     2     1     0
115  *          +-----+-----+-----+-----+-----+-----+-----+-----+
116  *          | MSB | PAGn| PAG0| USGN| BYT2| PCR | SYM | BYT |
117  *          +-----+-----+-----+-----+-----+-----+-----+-----+
118  */
119
120 #define R_WORD  0x00            /* 16 bit */
121 #define R_BYTE  0x01            /*  8 bit */
122
123 #define R_AREA  0x00            /* Base type */
124 #define R_SYM   0x02
125
126 #define R_NORM  0x00            /* PC adjust */
127 #define R_PCR   0x04
128
129 #define R_BYT1  0x00            /* Byte count for R_BYTE = 1 */
130 #define R_BYT2  0x08            /* Byte count for R_BYTE = 2 */
131
132 #define R_SGND  0x00            /* Signed value */
133 #define R_USGN  0x10            /* Unsigned value */
134
135 #define R_NOPAG 0x00            /* Page Mode */
136 #define R_PAG0  0x20            /* Page '0' */
137 #define R_PAG   0x40            /* Page 'nnn' */
138
139 /*
140  * Valid for R_BYT2:
141  */
142 #define R_LSB   0x00            /* output low byte */
143 #define R_MSB   0x80            /* output high byte */
144
145 /*
146  * Global symbol types.
147  */
148 #define S_REF   1               /* referenced */
149 #define S_DEF   2               /* defined */
150
151 /*
152  * Area types
153  */
154 #define A_CON   0000            /* concatenate */
155 #define A_OVR   0004            /* overlay */
156 #define A_REL   0000            /* relocatable */
157 #define A_ABS   0010            /* absolute */
158 #define A_NOPAG 0000            /* non-paged */
159 #define A_PAG   0020            /* paged */
160
161 /*
162  * File types
163  */
164 #define F_INV   0               /* invalid */
165 #define F_STD   1               /* stdin */
166 #define F_LNK   2               /* File.lnk */
167 #define F_REL   3               /* File.rel */
168 #ifdef SDK
169 #define F_CMD   4               /* Command line */
170 #endif /* SDK */
171
172 #ifdef GAMEBOY
173 /*
174  * Multiple banks support
175  */
176 extern int nb_rom_banks;
177 extern int nb_ram_banks;
178 extern int current_rom_bank;
179 extern int mbc_type;
180 extern char cart_name[];
181 /*
182  * ROM patching support
183  */
184 typedef struct _patch {
185   unsigned int addr;
186   unsigned char value;
187   struct _patch *next;
188 } patch;
189 extern patch* patches;
190 #endif /* GAMEBOY */
191 /*
192  *      General assembler address type
193  */
194 typedef unsigned int Addr_T;
195
196 /*
197  *      The structures of head, area, areax, and sym are created
198  *      as the REL files are read during the first pass of the
199  *      linker.  The struct head is created upon encountering a
200  *      H directive in the REL file.  The structure contains a
201  *      link to a link file structure (struct lfile) which describes
202  *      the file containing the H directive, the number of data/code
203  *      areas contained in this header segment, the number of
204  *      symbols referenced/defined in this header segment, a pointer
205  *      to an array of pointers to areax structures (struct areax)
206  *      created as each A directive is read, and a pointer to an
207  *      array of pointers to symbol structures (struct sym) for
208  *      all referenced/defined symbols.  As H directives are read
209  *      from the REL files a linked list of head structures is
210  *      created by placing a link to the new head structure
211  *      in the previous head structure.
212  */
213 struct  head
214 {
215         struct  head   *h_hp;   /* Header link */
216         struct  lfile  *h_lfile;/* Associated file */
217         int     h_narea;        /* # of areas */
218         struct  areax **a_list; /* Area list */
219         int     h_nglob;        /* # of global symbols */
220         struct  sym   **s_list; /* Globle symbol list */
221         char    m_id[NCPS];     /* Module name */
222 };
223
224 /*
225  *      A structure area is created for each 'unique' data/code
226  *      area definition found as the REL files are read.  The
227  *      struct area contains the name of the area, a flag byte
228  *      which contains the area attributes (REL/CON/OVR/ABS),
229  *      an area subtype (not used in this assembler), and the
230  *      area base address and total size which will be filled
231  *      in at the end of the first pass through the REL files.
232  *      As A directives are read from the REL files a linked
233  *      list of unique area structures is created by placing a
234  *      link to the new area structure in the previous area structure.
235  */
236 struct  area
237 {
238         struct  area    *a_ap;  /* Area link */
239         struct  areax   *a_axp; /* Area extension link */
240         Addr_T  a_addr;         /* Beginning address of area */
241         Addr_T  a_size;         /* Total size of the area */
242         char    a_type;         /* Area subtype */
243         char    a_flag;         /* Flag byte */
244         char    a_id[NCPS];     /* Name */
245 };
246
247 /*
248  *      An areax structure is created for every A directive found
249  *      while reading the REL files.  The struct areax contains a
250  *      link to the 'unique' area structure referenced by the A
251  *      directive and to the head structure this area segment is
252  *      a part of.  The size of this area segment as read from the
253  *      A directive is placed in the areax structure.  The beginning
254  *      address of this segment will be filled in at the end of the
255  *      first pass through the REL files.  As A directives are read
256  *      from the REL files a linked list of areax structures is
257  *      created for each unique area.  The final areax linked
258  *      list has at its head the 'unique' area structure linked
259  *      to the linked areax structures (one areax structure for
260  *      each A directive for this area).
261  */
262 struct  areax
263 {
264         struct  areax   *a_axp; /* Area extension link */
265         struct  area    *a_bap; /* Base area link */
266         struct  head    *a_bhp; /* Base header link */
267         Addr_T  a_addr;         /* Beginning address of section */
268         Addr_T  a_size;         /* Size of the area in section */
269 };
270
271 /*
272  *      A sym structure is created for every unique symbol
273  *      referenced/defined while reading the REL files.  The
274  *      struct sym contains the symbol's name, a flag value
275  *      (not used in this linker), a symbol type denoting
276  *      referenced/defined, and an address which is loaded
277  *      with the relative address within the area in which
278  *      the symbol was defined.  The sym structure also
279  *      contains a link to the area where the symbol was defined.
280  *      The sym structures are linked into linked lists using
281  *      the symbol link element.
282  */
283 struct  sym
284 {
285         struct  sym     *s_sp;  /* Symbol link */
286         struct  areax   *s_axp; /* Symbol area link */
287         char    s_type;         /* Symbol subtype */
288         char    s_flag;         /* Flag byte */
289         Addr_T  s_addr;         /* Address */
290         char    s_id[NCPS];     /* Name */
291 };
292
293 /*
294  *      The structure lfile contains a pointer to a
295  *      file specification string, the file type, and
296  *      a link to the next lfile structure.
297  */
298 struct  lfile
299 {
300         struct  lfile   *f_flp; /* lfile link */
301         int     f_type;         /* File type */
302         char    *f_idp;         /* Pointer to file spec */
303 };
304
305 /*
306  *      The struct base contains a pointer to a
307  *      base definition string and a link to the next
308  *      base structure.
309  */
310 struct  base
311 {
312         struct  base  *b_base;  /* Base link */
313         char          *b_strp;  /* String pointer */
314 };
315
316 /*
317  *      The struct globl contains a pointer to a
318  *      global definition string and a link to the next
319  *      global structure.
320  */
321 struct  globl
322 {
323         struct  globl *g_globl; /* Global link */
324         char          *g_strp;  /* String pointer */
325 };
326
327 /*
328  *      A structure sdp is created for each 'unique' paged
329  *      area definition found as the REL files are read.
330  *      As P directives are read from the REL files a linked
331  *      list of unique sdp structures is created by placing a
332  *      link to the new sdp structure in the previous area structure.
333  */
334 struct  sdp
335 {
336         struct  area  *s_area;  /* Paged Area link */
337         struct  areax *s_areax; /* Paged Area Extension Link */
338         Addr_T  s_addr;         /* Page address offset */
339 };
340
341 /*
342  *      The structure rerr is loaded with the information
343  *      required to report an error during the linking
344  *      process.  The structure contains an index value
345  *      which selects the areax structure from the header
346  *      areax structure list, a mode value which selects
347  *      symbol or area relocation, the base address in the
348  *      area section, an area/symbol list index value, and
349  *      an area/symbol offset value.
350  */
351 struct  rerr
352 {
353         int     aindex;         /* Linking area */
354         int     mode;           /* Relocation mode */
355         Addr_T  rtbase;         /* Base address in section */
356         int     rindex;         /* Area/Symbol reloaction index */
357         Addr_T  rval;           /* Area/Symbol offset value */
358 };
359
360 /*
361  *      The structure lbpath is created for each library
362  *      path specification input by the -k option.  The
363  *      lbpath structures are linked into a list using
364  *      the next link element.
365  */
366 struct lbpath {
367         struct  lbpath  *next;
368         char            *path;
369 };
370
371 /*
372  *      The structure lbname is created for all combinations of the
373  *      library path specifications (input by the -k option) and the
374  *      library file specifications (input by the -l option) that
375  *      lead to an existing file.  The element path points to
376  *      the path string, element libfil points to the library
377  *      file string, and the element libspc is the concatenation
378  *      of the valid path and libfil strings.
379  *
380  *      The lbpath structures are linked into a list
381  *      using the next link element.
382  *
383  *      Each library file contains a list of object files
384  *      that are contained in the particular library. e.g.:
385  *
386  *              \iolib\termio
387  *              \inilib\termio
388  *
389  *      Only one specification per line is allowed.
390  */
391 struct lbname {
392         struct  lbname  *next;
393         char            *path;
394         char            *libfil;
395         char            *libspc;
396 };
397
398 /*
399  *      The function fndsym() searches through all combinations of the
400  *      library path specifications (input by the -k option) and the
401  *      library file specifications (input by the -l option) that
402  *      lead to an existing file for a symbol definition.
403  *
404  *      The structure lbfile is created for the first library
405  *      object file which contains the definition for the
406  *      specified undefined symbol.
407  *
408  *      The element libspc points to the library file path specification
409  *      and element relfil points to the object file specification string.
410  *      The element filspc is the complete path/file specification for
411  *      the library file to be imported into the linker.  The
412  *      file specicifation may be formed in one of two ways:
413  *
414  *      (1)     If the library file contained an absolute
415  *              path/file specification then this becomes filspc.
416  *              (i.e. C:\...)
417  *
418  *      (2)     If the library file contains a relative path/file
419  *              specification then the concatenation of the path
420  *              and this file specification becomes filspc.
421  *              (i.e. \...)
422  *
423  *      The lbpath structures are linked into a list
424  *      using the next link element.
425  */
426 struct lbfile {
427         struct  lbfile  *next;
428         char            *libspc;
429         char            *relfil;
430         char            *filspc;
431     long                offset; /*>=0 if rel file is embedded in a lib file at this offset*/
432 };
433
434 /*
435  *      External Definitions for all Global Variables
436  */
437
438 extern  char    *_abs_;         /*      = { ".  .ABS." };
439                                  */
440 extern  int     lkerr;          /*      ASLink error flag
441                                  */
442 extern  char    *ip;            /*      pointer into the REL file
443                                  *      text line in ib[]
444                                  */
445 extern  char    ib[NINPUT];     /*      REL file text line
446                                  */
447 extern  char    *rp;            /*      pointer into the LST file
448                                  *      text line in rb[]
449                                  */
450 extern  char    rb[NINPUT];     /*      LST file text line being
451                                  *      address relocated
452                                  */
453 extern  unsigned char   ctype[];        /*      array of character types, one per
454                                  *      ASCII character
455                                  */
456
457 extern char sdccopt[NINPUT];
458 extern char sdccopt_module[NINPUT];
459 extern char curr_module[NINPUT];
460
461 /*
462  *      Character Type Definitions
463  */
464 #define SPACE   0000
465 #define ETC     0000
466 #define LETTER  0001
467 #define DIGIT   0002
468 #define BINOP   0004
469 #define RAD2    0010
470 #define RAD8    0020
471 #define RAD10   0040
472 #define RAD16   0100
473 #define ILL     0200
474
475 #define DGT2    DIGIT|RAD16|RAD10|RAD8|RAD2
476 #define DGT8    DIGIT|RAD16|RAD10|RAD8
477 #define DGT10   DIGIT|RAD16|RAD10
478 #define LTR16   LETTER|RAD16
479
480 #if     CASE_SENSITIVE
481 #else
482 extern  char    ccase[];        /*      an array of characters which
483                                  *      perform the case translation function
484                                  */
485 #endif
486
487 extern  struct  lfile   *filep; /*      The pointers (lfile *) filep,
488                                  *      (lfile *) cfp, and (FILE *) sfp
489                                  *      are used in conjunction with
490                                  *      the routine lk_getline() to read
491                                  *      asmlnk commands from
492                                  *      (1) the standard input or
493                                  *      (2) or a command file
494                                  *      and to read the REL files
495                                  *      sequentially as defined by the
496                                  *      asmlnk input commands.
497                                  *
498                                  *      The pointer *filep points to the
499                                  *      beginning of a linked list of
500                                  *      lfile structures.
501                                  */
502 extern  struct  lfile   *cfp;   /*      The pointer *cfp points to the
503                                  *      current lfile structure
504                                  */
505 extern  struct  lfile   *startp;/*      asmlnk startup file structure
506                                  */
507 extern  struct  lfile   *linkp; /*      pointer to first lfile structure
508                                  *      containing an input REL file
509                                  *      specification
510                                  */
511 extern  struct  lfile   *lfp;   /*      pointer to current lfile structure
512                                  *      being processed by parse()
513                                  */
514 extern  struct  head    *headp; /*      The pointer to the first
515                                  *      head structure of a linked list
516                                  */
517 extern  struct  head    *hp;    /*      Pointer to the current
518                                  *      head structure
519                                  */
520 extern  struct  area    *areap; /*      The pointer to the first
521                                  *      area structure of a linked list
522                                  */
523 extern  struct  area    *ap;    /*      Pointer to the current
524                                  *      area structure
525                                  */
526 extern  struct  areax   *axp;   /*      Pointer to the current
527                                  *      areax structure
528                                  */
529 extern  struct  sym *symhash[NHASH]; /* array of pointers to NHASH
530                                       * linked symbol lists
531                                       */
532 extern  struct  base    *basep; /*      The pointer to the first
533                                  *      base structure
534                                  */
535 extern  struct  base    *bsp;   /*      Pointer to the current
536                                  *      base structure
537                                  */
538 extern  struct  globl   *globlp;/*      The pointer to the first
539                                  *      globl structure
540                                  */
541 extern  struct  globl   *gsp;   /*      Pointer to the current
542                                  *      globl structure
543                                  */
544 extern  struct  sdp     sdp;    /*      Base Paged structure
545                                  */
546 extern  struct  rerr    rerr;   /*      Structure containing the
547                                  *      linker error information
548                                  */
549 extern  FILE    *ofp;           /*      Linker Output file handle
550                                  */
551 extern  FILE    *mfp;           /*      Map output file handle
552                                  */
553 extern  FILE    *rfp;           /*      File handle for output
554                                  *      address relocated ASxxxx
555                                  *      listing file
556                                  */
557 extern  FILE    *sfp;           /*      The file handle sfp points to the
558                                  *      currently open file
559                                  */
560 extern  FILE    *tfp;           /*      File handle for input
561                                  *      ASxxxx listing file
562                                  */
563 extern  int     oflag;          /*      Output file type flag
564                                  */
565 extern  int     mflag;          /*      Map output flag
566                                  */
567 #ifdef SDK
568 extern  int     symflag;        /*      no$gmb .sym output flag
569                                  */
570 #endif
571 extern  int     xflag;          /*      Map file radix type flag
572                                  */
573 extern  int     pflag;          /*      print linker command file flag
574                                  */
575 extern  int     uflag;          /*      Listing relocation flag
576                                  */
577 extern  int     radix;          /*      current number conversion radix:
578                                  *      2 (binary), 8 (octal), 10 (decimal),
579                                  *      16 (hexadecimal)
580                                  */
581 extern  int     line;           /*      current line number
582                                  */
583 extern  int     page;           /*      current page number
584                                  */
585 extern  int     lop;            /*      current line number on page
586                                  */
587 extern  int     pass;           /*      linker pass number
588                                  */
589 extern  int     rtcnt;          /*      count of elements in the
590                                  *      rtval[] and rtflg[] arrays
591                                  */
592 extern  Addr_T  rtval[];        /*      data associated with relocation
593                                  */
594 extern  int     rtflg[];        /*      indicates if rtval[] value is
595                                  *      to be sent to the output file.
596                                  *      (always set in this linker)
597                                  */
598 extern  int     hilo;           /*      REL file byte ordering
599                                  */
600 extern  int     gline;          /*      LST file relocation active
601                                  *      for current line
602                                  */
603 extern  int     gcntr;          /*      LST file relocation active
604                                  *      counter
605                                  */
606 extern  struct lbpath *lbphead; /*      pointer to the first
607                                  *      library path structure
608                                  */
609 extern  struct lbname *lbnhead; /*      pointer to the first
610                                  *      library name structure
611                                  */
612 extern  struct lbfile *lbfhead; /*      pointer to the first
613                                  *      library file structure
614                                  */
615
616 /* C Library function definitions */
617 /* for reference only
618 extern  VOID            exit();
619 extern  int             fclose();
620 extern  char *          fgets();
621 extern  FILE *          fopen();
622 extern  int             fprintf();
623 extern  VOID            free();
624 extern  VOID *          malloc();
625 extern  char            putc();
626 extern  char *          strcpy();
627 extern  int             strlen();
628 extern  char *          strncpy();
629 */
630
631 /* Program function definitions */
632
633 /* lkmain.c */
634 extern  FILE *          afile();
635 extern  VOID            bassav();
636 extern  VOID            gblsav();
637 extern  VOID            link();
638 extern  VOID            lkexit();
639 extern  int             main();
640 extern  VOID            map();
641 #ifdef SDK
642 extern  VOID            sym();
643 #endif
644 extern  int             parse();
645 extern  VOID            setbas();
646 extern  VOID            setgbl();
647 extern  VOID            usage();
648
649 /* lklex.c */
650 extern  char            endline();
651 extern  char            get();
652 extern  VOID            getfid();
653 extern  VOID            getid();
654 extern  int             lk_getline();
655 extern  int             getmap();
656 extern  char            getnb();
657 extern  int             more();
658 extern  VOID            skip();
659 extern  VOID            unget();
660 extern  VOID            chop_crlf();
661
662 /* lkarea.c */
663 extern  VOID            lkparea();
664 extern  VOID            lnkarea();
665 extern  VOID            lnksect();
666 extern  VOID            newarea();
667
668 /* lkhead.c */
669 extern  VOID            module();
670 extern  VOID            newhead();
671
672 /* lksym.c */
673 extern  int             hash();
674 extern  struct  sym *   lkpsym();
675 extern  VOID *          new();
676 extern  struct  sym *   newsym();
677 extern  VOID            symdef();
678 extern  int             symeq();
679 extern  VOID            syminit();
680 extern  VOID            symmod();
681 extern  Addr_T          symval();
682
683 /* lkeval.c */
684 extern  int             digit();
685 extern  Addr_T          eval();
686 extern  Addr_T          expr();
687 extern  int             oprio();
688 extern  Addr_T          term();
689
690 /* lklist.c */
691 extern  int             dgt();
692 extern  VOID            lkulist();
693 extern  VOID            lkalist();
694 extern  VOID            lkglist();
695 extern  VOID            lstarea();
696 extern  VOID            newpag();
697 extern  VOID            slew();
698
699 /* lkrloc.c */
700 extern  Addr_T          adb_b();
701 extern  Addr_T          adb_hi();
702 extern  Addr_T          adb_lo();
703 extern  Addr_T          adw_w();
704 extern  Addr_T          adw_hi();
705 extern  Addr_T          adw_lo();
706 extern  Addr_T          evword();
707 extern  VOID            rele();
708 extern  VOID            reloc();
709 extern  VOID            relt();
710 extern  VOID            relr();
711 extern  VOID            relp();
712 extern  VOID            relerr();
713 extern  char *          errmsg[];
714 extern  VOID            errdmp();
715 extern  VOID            relerp();
716 extern  VOID            erpdmp();
717 extern  VOID            prntval();
718
719 /* lklibr.c */
720 extern  int             addfile();
721 extern  VOID            addlib();
722 extern  VOID            addpath();
723 extern  int             fndsym();
724 extern  VOID            library();
725 extern  VOID            loadfile();
726 extern  VOID            search();
727
728 /* lks19.c */
729 extern  VOID            s19();
730
731 /* lkihx.c */
732 extern  VOID            ihx();
733
734 /* lkgb.h */
735 VOID gb(int in);
736 VOID gg(int in);
737