* asranlib/asranlib.c, link/lkar.h, link/lkar.c:
[fw/sdcc] / as / hc08 / asdata.c
1 /* asdata.c
2
3    Copyright (C) 1989-1995 Alan R. Baldwin
4    721 Berkeley St., Kent, Ohio 44240
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19 /*
20  * 28-Oct-97 JLH:
21  *           - change s_id from [NCPS] to pointer (comment)
22  *  2-Nov-97 JLH:
23  *           - add jflag for debug control
24  */
25
26 #include <stdio.h>
27 #include <setjmp.h>
28 #include <string.h>
29 #include "asm.h"
30
31 /*)Module       asdata.c
32  *
33  *      The module asdata.c contains the global constants,
34  *      structures, and variables used in the assembler.
35  */
36
37 int     aserr;                  /*      ASxxxx error counter
38                                  */
39 jmp_buf jump_env;               /*      compiler dependent structure
40                                  *      used by setjmp() and longjmp()
41                                  */
42 int     inpfil;                 /*      count of assembler
43                                  *      input files specified
44                                  */
45 int     incfil;                 /*      current file handle index
46                                  *      for include files
47                                  */
48 int     cfile;                  /*      current file handle index
49                                  *      of input assembly files
50                                  */
51 int     flevel;                 /*      IF-ELSE-ENDIF flag will be non
52                                  *      zero for false conditional case
53                                  */
54 int     tlevel;                 /*      current conditional level
55                                  */
56 int     ifcnd[MAXIF+1];         /*      array of IF statement condition
57                                  *      values (0 = FALSE) indexed by tlevel
58                                  */
59 int     iflvl[MAXIF+1];         /*      array of IF-ELSE-ENDIF flevel
60                                  *      values indexed by tlevel
61                                  */
62
63 char    afn[PATH_MAX];          /*      afile temporary file name
64                                  */
65 char    srcfn[MAXFIL][PATH_MAX];/*      array of source file names
66                                  */
67 int     srcline[MAXFIL];        /*      source line number
68                                  */
69 char    incfn[MAXINC][PATH_MAX];/*      array of include file names
70                                  */
71 int     incline[MAXINC];        /*      include line number
72                                  */
73
74 int     radix;                  /*      current number conversion radix:
75                                  *      2 (binary), 8 (octal), 10 (decimal),
76                                  *      16 (hexadecimal)
77                                  */
78 int     line;                   /*      current assembler source
79                                  *      line number
80                                  */
81 int     page;                   /*      current page number
82                                  */
83 int     lop;                    /*      current line number on page
84                                  */
85 int     pass;                   /*      assembler pass number
86                                  */
87 int     org_cnt;                /*      .org directive counter
88                                  */
89 int     lflag;                  /*      -l, generate listing flag
90                                  */
91 int     cflag;                  /*      -lc, generate sdcdb debug info
92                                  */
93 int     gflag;                  /*      -g, make undefined symbols global flag
94                                  */
95 int     aflag;                  /*      -a, make all symbols global flag
96                                  */
97 int     jflag;                  /*      -j, generate debug information flag
98                                  */
99 int     oflag;                  /*      -o, generate relocatable output flag
100                                  */
101 int     sflag;                  /*      -s, generate symbol table flag
102                                  */
103 int     pflag;                  /*      -p, enable listing pagination
104                                  */
105 int     xflag;                  /*      -x, listing radix flag
106                                  */
107 int     fflag;                  /*      -f(f), relocations flagged flag
108                                  */
109 Addr_T  laddr;                  /*      address of current assembler line
110                                  *      or value of .if argument
111                                  */
112 Addr_T  fuzz;                   /*      tracks pass to pass changes in the
113                                  *      address of symbols caused by
114                                  *      variable length instruction formats
115                                  */
116 int     lmode;                  /*      listing mode
117                                  */
118 char    *ep;                    /*      pointer into error list
119                                  *      array eb[NERR]
120                                  */
121 char    eb[NERR];               /*      array of generated error codes
122                                  */
123 const char *ip;                 /*      pointer into the assembler-source
124                                  *      text line in ib[]
125                                  */
126 const char *ib;                 /*      assembler-source text line
127                                  */
128 char    *cp;                    /*      pointer to assembler output
129                                  *      array cb[]
130                                  */
131 char    cb[NCODE];              /*      array of assembler output values
132                                  */
133 int     *cpt;                   /*      pointer to assembler relocation type
134                                  *      output array cbt[]
135                                  */
136 int     cbt[NCODE];             /*      array of assembler relocation types
137                                  *      describing the data in cb[]
138                                  */
139 char    tb[NTITL];              /*      Title string buffer
140                                  */
141 char    stb[NSBTL];             /*      Subtitle string buffer
142                                  */
143 char    optsdcc[NINPUT];        /*      sdcc compile options
144                                  */
145 int     flat24Mode;             /*      non-zero if we are using DS390 24 bit
146                                  *      flat mode (via .flat24 directive).
147                                  */
148
149 char    symtbl[] = { "Symbol Table" };
150 char    aretbl[] = { "Area Table" };
151
152 char    module[NCPS];           /*      module name string
153                                  */
154
155 /*
156  *      The mne structure is a linked list of the assembler
157  *      mnemonics and directives.  The list of mnemonics and
158  *      directives contained in the device dependent file
159  *      xxxpst.c are hashed and linked into NHASH lists in
160  *      module assym.c by syminit().  The structure contains
161  *      the mnemonic/directive name, a subtype which directs
162  *      the evaluation of this mnemonic/directive, a flag which
163  *      is used to detect the end of the mnemonic/directive
164  *      list in xxxpst.c, and a value which is normally
165  *      associated with the assembler mnemonic base instruction
166  *      value.
167  *
168  *      struct  mne
169  *      {
170  *              struct  mne *m_mp;      Hash link
171  *              char    m_id[NCPS];     Mnemonic
172  *              char    m_type;         Mnemonic subtype
173  *              char    m_flag;         Mnemonic flags
174  *              Addr_T  m_valu;         Value
175  *      };
176  */
177 struct  mne     *mnehash[NHASH];
178
179 /*
180  *      The sym structure is a linked list of symbols defined
181  *      in the assembler source files.  The first symbol is "."
182  *      defined here.  The entry 'struct tsym *s_tsym'
183  *      links any temporary symbols following this symbol and
184  *      preceeding the next normal symbol.  The structure also
185  *      contains the symbol's name, type (USER or NEW), flag
186  *      (global, assigned, and multiply defined), a pointer
187  *      to the area structure defining where the symbol is
188  *      located, a reference number assigned by outgsd() in
189  *      asout.c, and the symbols address relative to the base
190  *      address of the area where the symbol is located.
191  *
192  *      struct  sym
193  *      {
194  *              struct  sym  *s_sp;     Hash link
195  *              struct  tsym *s_tsym;   Temporary symbol link
196  *              char    *s_id;          Symbol (JLH)
197  *              char    s_type;         Symbol subtype
198  *              char    s_flag;         Symbol flags
199  *              struct  area *s_area;   Area line, 0 if absolute
200  *              int     s_ref;          Ref. number
201  *              Addr_T  s_addr;         Address
202  *      };
203  */
204 struct  sym     sym[] = {
205     {NULL,      NULL,   ".",    S_USER, S_END,  NULL,   0,      0}
206 };
207
208 struct  sym     *symp;          /*      pointer to a symbol structure
209                                  */
210 struct  sym *symhash[NHASH];    /*      array of pointers to NHASH
211                                  *      linked symbol lists
212                                  */
213
214 /*
215  *      The area structure contains the parameter values for a
216  *      specific program or data section.  The area structure
217  *      is a linked list of areas.  The initial default area
218  *      is "_CODE" defined here, the next area structure
219  *      will be linked to this structure through the structure
220  *      element 'struct area *a_ep'.  The structure contains the
221  *      area name, area reference number ("_CODE" is 0) determined
222  *      by the order of .area directives, area size determined
223  *      from the total code and/or data in an area, area fuzz is
224  *      an variable used to track pass to pass changes in the
225  *      area size caused by variable length instruction formats,
226  *      and area flags which specify the area's relocation type.
227  *
228  *      struct  area
229  *      {
230  *              struct  area *a_ap;     Area link
231  *              char    a_id[NCPS];     Area Name
232  *              int     a_ref;          Reference number
233  *              Addr_T  a_size;         Area size
234  *              Addr_T  a_fuzz;         Area fuzz
235  *              int     a_flag;         Area flags
236  *      };
237  */
238 struct  area    area[] = {
239     {NULL,      "_CODE",        0,      0,      0,      A_CON|A_REL}
240 };
241
242 struct  area    *areap;         /*      pointer to an area structure
243                                  */
244
245 FILE    *lfp;                   /*      list output file handle
246                                  */
247 FILE    *ofp;                   /*      relocation output file handle
248                                  */
249 FILE    *tfp;                   /*      symbol table output file handle
250                                  */
251 FILE    *sfp[MAXFIL];           /*      array of assembler-source file handles
252                                  */
253 FILE    *ifp[MAXINC];           /*      array of include-file file handles
254                                  */
255
256 /*
257  *      array of character types, one per
258  *      ASCII character
259  */
260 unsigned char   ctype[128] = {
261 /*NUL*/ ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,
262 /*BS*/  ILL,    SPACE,  ILL,    ILL,    SPACE,  ILL,    ILL,    ILL,
263 /*DLE*/ ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,
264 /*CAN*/ ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,
265 /*SPC*/ SPACE,  ETC,    ETC,    ETC,    LETTER, BINOP,  BINOP,  ETC,
266 /*(*/   ETC,    ETC,    BINOP,  BINOP,  ETC,    BINOP,  LETTER, BINOP,
267 /*0*/   DGT2,   DGT2,   DGT8,   DGT8,   DGT8,   DGT8,   DGT8,   DGT8,
268 /*8*/   DGT10,  DGT10,  ETC,    ETC,    BINOP,  ETC,    BINOP,  ETC,
269 /*@*/   ETC,    LTR16,  LTR16,  LTR16,  LTR16,  LTR16,  LTR16,  LETTER,
270 /*H*/   LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
271 /*P*/   LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
272 /*X*/   LETTER, LETTER, LETTER, ETC,    ETC,    ETC,    BINOP,  LETTER,
273 /*`*/   ETC,    LTR16,  LTR16,  LTR16,  LTR16,  LTR16,  LTR16,  LETTER,
274 /*h*/   LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
275 /*p*/   LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
276 /*x*/   LETTER, LETTER, LETTER, ETC,    BINOP,  ETC,    ETC,    ETC
277 };
278
279 /*
280  *      an array of characters which
281  *      perform the case translation function
282  */
283 char    ccase[128] = {
284 /*NUL*/ '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
285 /*BS*/  '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
286 /*DLE*/ '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
287 /*CAN*/ '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
288 /*SPC*/ '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
289 /*(*/   '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
290 /*0*/   '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
291 /*8*/   '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
292 /*@*/   '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
293 /*H*/   '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
294 /*P*/   '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
295 /*X*/   '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
296 /*`*/   '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
297 /*h*/   '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
298 /*p*/   '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
299 /*x*/   '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177'
300 };