1 /* asdata.c */
2 
3 /*
4  *  Copyright (C) 1989-2010  Alan R. Baldwin
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any 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  * Alan R. Baldwin
21  * 721 Berkeley St.
22  * Kent, Ohio  44240
23  *
24  *   With enhancements from
25  *
26  *      John L. Hartman (JLH)
27  *      jhartman@compuserve.com
28  *
29  *      Bill McKinnon (BM)
30  *      w_mckinnon@conknet.com
31  *
32  *      Mike McCarty
33  *      mike dot mccarty at sbcglobal dot net
34  */
35 
36 #include "asxxxx.h"
37 
38 /*)Module       asdata.c
39  *
40  *      The module asdata.c contains the global constants,
41  *      structures, and variables used in the assembler.
42  */
43 
44 int     aserr;          /*      ASxxxx error counter
45                          */
46 jmp_buf jump_env;       /*      compiler dependent structure
47                          *      used by setjmp() and longjmp()
48                          */
49 
50 /*
51  *      The asmf structure contains the information
52  *      pertaining to an assembler source file/macro.
53  *
54  * The Parameters:
55  *      next    is a pointer to the next object in the linked list
56  *      objtyp  specifies the object type - T_ASM, T_INCL, T_MACRO
57  *      line    is the saved line number of the parent object
58  *      fp      is the source FILE handle
59  *      afp     is the file path length (excludes the files name.ext)
60  *      afn[]   is the assembler/include file path/name.ext
61  *
62  *      struct  asmf
63  *      {
64  *              struct  asmf *next;     Link to Next Object
65  *              int     objtyp;         Object Type
66  *              int     line;           Saved Line Counter
67  *              int     flevel;         saved flevel
68  *              int     tlevel;         saved tlevel
69  *              int     lnlist;         saved lnlist
70  *              FILE *  fp;             FILE Handle
71  *              int     afp;            File Path Length
72  *              char    afn[FILSPC];    File Name
73  *      };
74  */
75 struct  asmf    *asmp;  /*      The pointer to the first assembler
76                          *      source file structure of a linked list
77                          */
78 struct  asmf    *asmc;  /*      Pointer to the current
79                          *      source input structure
80                          */
81 struct  asmf    *asmi;  /*      Queued pointer to an include file
82                          *      source input structure
83                          */
84 struct  asmf    *asmq;  /*      Queued pointer to a macro
85                          *      source input structure
86                          */
87 
88 /*
89  *      The mcrdef structure contains the
90  *      information about a macro definition.
91  *
92  *      When the macro is defined the definition
93  *      arguments are packed into a linked list of
94  *      strings beginning with bgnarg and ending with
95  *      endarg. The number of args is placed in narg.
96  *
97  *      When the macro is invoked the expansion
98  *      argument strings are placed into a linked
99  *      list of strings beginning with bgnxrg and
100  *      ending with endxrg. The number of expansion
101  *      arguments is placed in xarg.
102  *
103  * The Parameters:
104  *      next    is a pointer to the next macro definition structure
105  *      name    is a pointer to the macro name string
106  *      bgnlst  is a pointer to the first text line of the macro
107  *      endlst  is a pointer to the last  text line of the macro
108  *      type    is the macro type - .macro, .irp, .irpc, or .rept
109  *      rptcnt  is the repeat count for the macro
110  *      nest    is the macro nesting counter
111  *      narg    is the number of macro definition arguments
112  *      bgnarg  is a pointer to the first definition argument string
113  *      endarg  is a pointer to the last  definition argument string
114  *      xarg    is the number of expansion arguments at macro invocation
115  *      bgnxrg  is a pointer to the first expansion argument string
116  *      endxrg  is a pointer to the last  expansion argument string
117  *
118  *      struct  mcrdef {
119  *              struct mcrdef * next;           link to next macro definition
120  *              char *          name;           pointer to the macro name
121  *              struct strlst * bgnlst;         link to first text line of macro
122  *              struct strlst * endlst;         link to last text line of macro
123  *              int             type;           macro type
124  *              int             rptcnt;         repeat counter
125  *              int             nest;           macro nesting counter
126  *              int             narg;           number of macro defintion arguments
127  *              struct strlst * bgnarg;         link to first macro defintion argument
128  *              struct strlst * endarg;         link to last macro definition argument
129  *              int             xarg;           number of macro expansion arguments
130  *              struct strlst * bgnxrg;         link to first macro expansion argument
131  *              struct strlst * endxrg;         link to last macro xpansion argument
132  *      };
133  */
134 struct mcrdef * mcrlst; /*      link to list of defined macros
135                          */
136 struct mcrdef * mcrp;   /*      link to list of defined macros
137                          */
138 
139 /*
140  *      The memlnk structure is a linked list
141  *      of memory allocations.
142  *
143  *      The function new() uses the memlnk structure
144  *      to create a linked list of allocated memory
145  *      that can be traversed by asfree() to release
146  *      the allocated memory.
147  *
148  *      The function mhunk() uses the memlnk structure
149  *      to create a linked list of allocated memory
150  *      that can be reused.
151  *
152  * The Parameters:
153  *      next    is a pointer to the next memlnk structure.
154  *      ptr     is a pointer to the allocated memory.
155  *
156  *      struct  memlnk {
157  *              struct memlnk * next;           link to next memlnk
158  *              VOID *          ptr;            pointer to allocated memory
159  *      };
160  */
161 struct memlnk * pmcrmem;/*      First Macro Memory Allocation Structure
162                          */
163 struct memlnk * mcrmem; /*      Macro Memory Allocation Structure
164                          */
165 int     mcrblk;         /*      new data blocks allocated
166                          */
167 int     incfil;         /*      include file nesting counter
168                          */
169 int     maxinc;         /*      maximum include file nesting encountered
170                          */
171 int     mcrfil;         /*      macro nesting counter
172                          */
173 int     maxmcr;         /*      maximum macro nesting encountered
174                          */
175 int     flevel;         /*      IF-ELSE-ENDIF flag will be non
176                          *      zero for false conditional case
177                          */
178 int     ftflevel;       /*      IIFF-IIFT-IIFTF FLAG
179                          */
180 int     tlevel;         /*      current conditional level
181                          */
182 int     lnlist;         /*      LIST-NLIST options
183                          */
184 int     ifcnd[MAXIF+1]; /*      array of IF statement condition
185                          *      values (0 = FALSE) indexed by tlevel
186                          */
187 int     iflvl[MAXIF+1]; /*      array of IF-ELSE-ENDIF flevel
188                          *      values indexed by tlevel
189                          */
190 char    afn[FILSPC];    /*      current input file specification
191                          */
192 int     afp;            /*      current input file path length
193                          */
194 char    afntmp[FILSPC]; /*      temporary input file specification
195                          */
196 int     afptmp;         /*      temporary input file path length
197                          */
198 int     srcline;        /*      current source line number
199                          */
200 int     asmline;        /*      current assembler file line number
201                          */
202 int     incline;        /*      current include file line number
203                          */
204 int     mcrline;        /*      current macro line number
205                          */
206 int     radix;          /*      current number conversion radix:
207                          *      2 (binary), 8 (octal), 10 (decimal),
208                          *      16 (hexadecimal)
209                          */
210 int     line;           /*      current assembler source
211                          *      line number
212                          */
213 int     page;           /*      current page number
214                          */
215 int     lop;            /*      current line number on page
216                          */
217 int     pass;           /*      assembler pass number
218                          */
219 int     aflag;          /*      -a, make all symbols global flag
220                          */
221 int     bflag;          /*      -b(b), listing modes flag
222                          */
223 int     cflag;          /*      -c, disable cycle counts in listing flag
224                          */
225 int     fflag;          /*      -f(f), relocations flagged flag
226                          */
227 int     gflag;          /*      -g, make undefined symbols global flag
228                          */
229 int     jflag;          /*      -j, generate debug information flag
230                          */
231 int     lflag;          /*      -l, generate listing flag
232                          */
233 int     oflag;          /*      -o, generate relocatable output flag
234                          */
235 int     pflag;          /*      -p, disable listing pagination
236                          */
237 int     sflag;          /*      -s, generate symbol table flag
238                          */
239 int     uflag;          /*      -u, disable .list/.nlist processing flag
240                          */
241 int     wflag;          /*      -w, enable wide listing format
242                          */
243 int     xflag;          /*      -x, listing radix flag
244                          */
245 int     yflag;          /*      -y, enable SDCC Debug Symbols
246                          */
247 int     zflag;          /*      -z, disable symbol case sensitivity
248                          */
249 int     waddrmode;      /*      WORD Address mode flag
250                          */
251 int     a_bytes;        /*      REL file T Line address length
252                          */
253 a_uint  a_mask;         /*      Address Mask
254                          */
255 a_uint  s_mask;         /*      Sign Mask
256                          */
257 a_uint  v_mask;         /*      Value Mask
258                          */
259 a_uint  laddr;          /*      address of current assembler line
260                          *      or value of .if argument
261                          */
262 a_uint  fuzz;           /*      tracks pass to pass changes in the
263                          *      address of symbols caused by
264                          *      variable length instruction formats
265                          */
266 int     lmode;          /*      listing mode
267                          */
268 char    *ep;            /*      pointer into error list
269                          *      array eb[NERR]
270                          */
271 char    eb[NERR];       /*      array of generated error codes
272                          */
273 char    *ip;            /*      pointer into the assembler-source
274                          *      text line in ib[]
275                          */
276 char    *ib;            /*      assembler-source text line for processing
277                          */
278 char    *ic;            /*      assembler-source text line for listing
279                          */
280 char    *cp;            /*      pointer to assembler output
281                          *      array cb[]
282                          */
283 char    cb[NCODE];      /*      array of assembler output values
284                          */
285 int     *cpt;           /*      pointer to assembler relocation type
286                          *      output array cbt[]
287                          */
288 int     cbt[NCODE];     /*      array of assembler relocation types
289                          *      describing the data in cb[]
290                          */
291 int     opcycles;       /*      opcode execution cycles
292                          */
293 char    tb[NTITL];      /*      Title string buffer
294                          */
295 char    stb[NSBTL];     /*      Subtitle string buffer
296                          */
297 char    erb[NINPUT+4];  /*      Error string buffer
298                          */
299 
300 char    symtbl[] = { "Symbol Table" };
301 char    aretbl[] = { "Area Table" };
302 
303 char    module[NCPS+2]; /*      module name string
304                          */
305 /* sdas specific */
306 int     org_cnt;        /*      .org directive counter
307                          */
308 char    *optsdcc;       /*      sdcc compile options
309                          */
310 /* end sdas specific */
311 
312 /*
313  *      The mne structure is a linked list of the assembler
314  *      mnemonics and directives.  The list of mnemonics and
315  *      directives contained in the device dependent file
316  *      xxxpst.c are hashed and linked into NHASH lists in
317  *      module assym.c by syminit().  The structure contains
318  *      the mnemonic/directive name, a subtype which directs
319  *      the evaluation of this mnemonic/directive, a flag which
320  *      is used to detect the end of the mnemonic/directive
321  *      list in xxxpst.c, and a value which is normally
322  *      associated with the assembler mnemonic base instruction
323  *      value.
324  *
325  *      struct  mne
326  *      {
327  *              struct  mne *m_mp;      Hash link
328  *              char *  m_id;           Mnemonic (JLH)
329  *              char    m_type;         Mnemonic subtype
330  *              char    m_flag;         Mnemonic flags
331  *              a_uint  m_valu;         Value
332  *      };
333  */
334 struct  mne     *mnehash[NHASH];
335 
336 /*
337  *      The sym structure is a linked list of symbols defined
338  *      in the assembler source files.  The first symbol is "."
339  *      defined here.  The entry 'struct tsym *s_tsym'
340  *      links any temporary symbols following this symbol and
341  *      preceeding the next normal symbol.  The structure also
342  *      contains the symbol's name, type (USER or NEW), flag
343  *      (global, assigned, and multiply defined), a pointer
344  *      to the area structure defining where the symbol is
345  *      located, a reference number assigned by outgsd() in
346  *      asout.c, and the symbols address relative to the base
347  *      address of the area where the symbol is located.
348  *
349  *      struct  sym
350  *      {
351  *              struct  sym  *s_sp;     Hash link
352  *              struct  tsym *s_tsym;   Temporary symbol link
353  *              char    *s_id;          Symbol (JLH)
354  *              char    s_type;         Symbol subtype
355  *              char    s_flag;         Symbol flags
356  *              struct  area *s_area;   Area line, 0 if absolute
357  *              int     s_ref;          Ref. number
358  *              a_uint  s_addr;         Address
359  * sdas specific
360  *              a_uint  s_org;          Start Address if absolute
361  * end sdas specific
362  *      };
363  */
364 struct  sym     sym[] = {
365     {   NULL,   NULL,   ".",        S_USER, 0,                  NULL,   0,  0,  0 },
366     {   NULL,   NULL,   ".__.ABS.", S_USER, S_ASG|S_GBL,        NULL,   0,  0,  0 },
367     {   NULL,   NULL,   ".__.CPU.", S_USER, S_ASG|S_LCL,        NULL,   0,  0,  0 },
368     {   NULL,   NULL,   ".__.H$L.", S_USER, S_ASG|S_LCL,        NULL,   0,  0,  0 },
369     {   NULL,   NULL,   ".__.$$$.", S_USER, S_ASG|S_LCL|S_EOL,  NULL,   0,  0,  0 }
370 };
371 
372 struct  sym     *symp;          /*      pointer to a symbol structure
373                                  */
374 struct  sym *symhash[NHASH];    /*      array of pointers to NHASH
375                                  *      linked symbol lists
376                                  */
377 
378 /*
379  *      The area structure contains the parameter values for a
380  *      specific program or data section.  The area structure
381  *      is a linked list of areas.  The initial default area
382  *      is "_CODE" defined here, the next area structure
383  *      will be linked to this structure through the structure
384  *      element 'struct area *a_ap'.  The structure contains the
385  *      area name, area reference number ("_CODE" is 0) determined
386  *      by the order of .area directives, area size determined
387  *      from the total code and/or data in an area, area fuzz is
388  *      a variable used to track pass to pass changes in the
389  *      area size caused by variable length instruction formats,
390  *      and area flags which specify the area's relocation type.
391  *
392  *      struct  area
393  *      {
394  *              struct  area *a_ap;     Area link
395  *              char *  a_id;           Area Name
396  *              int     a_ref;          Reference number
397  *              a_uint  a_size;         Area size
398  *              a_uint  a_fuzz;         Area fuzz
399  *              int     a_flag;         Area flags
400  *      };
401  */
402 struct  area    area[] = {
403     {NULL,      "_CODE",        0,      0,      0,      A_CON|A_REL}
404 };
405 
406 struct  area    *areap; /*      pointer to an area structure
407                          */
408 
409 FILE    *lfp;           /*      list output file handle
410                          */
411 FILE    *ofp;           /*      relocation output file handle
412                          */
413 FILE    *tfp;           /*      symbol table output file handle
414                          */
415 char    txt[NTXT];      /*      T Line Values
416                          */
417 char    rel[NREL];      /*      R Line Values
418                          */
419 char    *txtp = &txt[0];/*      Pointer to T Line Values
420                          */
421 char    *relp = &rel[0];/*      Pointer to R Line Values
422                          */
423 
424 /*
425  *      an array of character types,
426  *      one per ASCII character
427  */
428 unsigned char   ctype[256] = {
429 /*NUL*/ ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,
430 /*BS*/  ILL,    SPACE,  ILL,    ILL,    SPACE,  ILL,    ILL,    ILL,
431 /*DLE*/ ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,
432 /*CAN*/ ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,    ILL,
433 /*SPC*/ SPACE,  ETC,    ETC,    ETC,    LETTER, BINOP,  BINOP,  ETC,
434 /*(*/   ETC,    ETC,    BINOP,  BINOP,  ETC,    BINOP,  LETTER, BINOP,
435 /*0*/   DGT2,   DGT2,   DGT8,   DGT8,   DGT8,   DGT8,   DGT8,   DGT8,
436 /*8*/   DGT10,  DGT10,  ETC,    ETC,    BINOP,  ETC,    BINOP,  ETC,
437 /*@*/   ETC,    LTR16,  LTR16,  LTR16,  LTR16,  LTR16,  LTR16,  LETTER,
438 /*H*/   LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
439 /*P*/   LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
440 /*X*/   LETTER, LETTER, LETTER, BINOP,  ETC,    ETC,    BINOP,  LETTER,
441 /*`*/   ETC,    LTR16,  LTR16,  LTR16,  LTR16,  LTR16,  LTR16,  LETTER,
442 /*h*/   LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
443 /*p*/   LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
444 /*x*/   LETTER, LETTER, LETTER, ETC,    BINOP,  ETC,    ETC,    ETC,
445         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
446         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
447         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
448         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
449         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
450         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
451         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
452         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
453         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
454         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
455         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
456         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
457         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
458         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
459         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER,
460         LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER, LETTER
461 };
462 
463 /*
464  *      an array of characters which
465  *      perform the case translation function
466  */
467 char    ccase[256] = {
468 /*NUL*/ '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
469 /*BS*/  '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
470 /*DLE*/ '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
471 /*CAN*/ '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
472 /*SPC*/ '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
473 /*(*/   '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
474 /*0*/   '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
475 /*8*/   '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
476 /*@*/   '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
477 /*H*/   '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
478 /*P*/   '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
479 /*X*/   '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
480 /*`*/   '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
481 /*h*/   '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
482 /*p*/   '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
483 /*x*/   '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
484         '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
485         '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
486         '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
487         '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
488         '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
489         '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
490         '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
491         '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
492         '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
493         '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
494         '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
495         '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
496         '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
497         '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
498         '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
499         '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377'
500 };
501