1/* Common definitions for gpasm
2   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3   James Bowman, Craig Franklin
4
5This file is part of gputils.
6
7gputils is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12gputils is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with gputils; see the file COPYING.  If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA.  */
21
22#ifndef __GPASM_H__
23#define __GPASM_H__
24
25#include "symbol_list.h"
26
27#define GPASM_VERSION_STRING        ("gpasm-" VERSION " #" @REVISION@ " (" __DATE__ ")")
28
29/* This symbol will get placed into the symbol table for the 16bit cores
30   and thus allow compile-time selection of the proper macro set. */
31#define __16bit_core_               "__16bit_core_"
32
33#define __ACTIVE_BANK_ADDR          "__ACTIVE_BANK_ADDR"
34#define __BANK_INV                  -1
35
36#define __ASSUMED_BANK_ADDR         "__ASSUMED_BANK_ADDR"
37
38#define __ACTIVE_PAGE_ADDR          "__ACTIVE_PAGE_ADDR"
39#define __PAGE_INV                  -1
40
41#define WHILE_LOOP_COUNT_MAX        255
42
43#define STRCMP(s1, s2)              ((state.case_insensitive) ? strcasecmp((s1), (s2)) : strcmp((s1), (s2)))
44
45#define MAX_PATHS                   100
46
47#define IN_MACRO_WHILE_DEFINITION   state.mac_prev != NULL
48#define IN_WHILE_DEFINITION         IN_MACRO_WHILE_DEFINITION && (state.while_head != NULL)
49#define IN_MACRO_EXPANSION          state.src_list.last->type == SRC_MACRO
50#define IN_WHILE_EXPANSION          state.src_list.last->type == SRC_WHILE
51#define IN_FILE_EXPANSION           state.src_list.last->type == SRC_FILE
52
53enum gpasmValTypes {
54  VAL_CONSTANT,
55  VAL_VARIABLE,
56  VAL_EXTERNAL,
57  VAL_GLOBAL,
58  VAL_CBLOCK,
59  VAL_STATIC,
60  VAL_ADDRESS,
61  VAL_ABSOLUTE,
62  VAL_DEBUG
63};
64
65enum state_types {
66  STATE_NOCHANGE,
67  STATE_EXITMACRO,
68  STATE_INCLUDE,
69  STATE_MACRO,
70  STATE_SECTION,
71  STATE_SUBSTITUTION,
72  STATE_WHILE
73};
74
75enum out_file {
76  OUT_NORMAL,
77  OUT_SUPPRESS,
78  OUT_NAMED
79};
80
81enum file_types {
82  FT_SRC,
83  FT_OTHER
84};
85
86enum gpasm_modes {
87  MODE_ABSOLUTE,
88  MODE_RELOCATABLE
89};
90
91/************************************************************************/
92
93typedef struct src_line {
94  char   *line;                         /* Source line. */
95  size_t  size;                         /* Source line allocated size. */
96} src_line_t;
97
98typedef struct conf_mem_block {
99  int                    addr;
100  MemBlock_t            *m;
101  gp_boolean             new_config;
102  gp_symbol_t           *file_symbol;
103  unsigned int           line_number;
104  struct conf_mem_block *next;
105} conf_mem_block_t;
106
107/************************************************************************/
108
109/* file_context: A structure to keep track of all files that have been opened.
110                 Used to create the list of project files that can be found
111                 in the .cod file. */
112
113typedef struct file_context {
114  /* This always should be the first item! (libgputils/gplist.c) */
115  GPNodeHeader(struct file_context);
116
117  char                *name;            /* file name */
118  unsigned int         id;              /* Unique identifier. */
119  enum file_types      ft;              /* allowed file types */
120} file_context_t;
121
122typedef struct file_context_list {
123  /* head of file contexts
124   * tail of file contexts
125   * number of file contexts */
126  GPListHeader(file_context_t);
127} file_context_list_t;
128
129typedef struct macro_body {
130  char              *src_line;          /* Original source line - for listing. */
131  struct macro_body *next;              /* Next line in listing. */
132} macro_body_t;
133
134typedef struct macro_head {
135  int           pass;                   /* Pass in which macro was defined: 1 or 2 */
136  pnode_t      *parms;
137  macro_body_t *body;
138  gp_boolean    defined;                /* 1 macro has been defined so calls are valid */
139  char         *src_name;
140  unsigned int  line_number;
141  gp_symbol_t  *file_symbol;
142} macro_head_t;
143
144typedef struct amode {
145  enum {
146    IN_THEN,
147    IN_ELIF,
148    IN_ELSE
149  } mode;
150
151  gp_boolean    enabled;                /* Are we currently enabled? */
152  gp_boolean    before_else_enabled;    /* This true if before else branch was an another enabled branch. */
153  gp_boolean    upper_enabled;
154  struct amode *upper;
155} amode_t;
156
157enum src_types {
158  SRC_UNKNOWN,
159  SRC_FILE,
160  SRC_MACRO,
161  SRC_WHILE
162};
163
164typedef struct source_context {
165  /* This always should be the first item! (libgputils/gplist.c) */
166  GPNodeHeader(struct source_context);
167
168  char                   *name;
169  enum src_types          type;
170  FILE                   *f;
171  struct macro_head      *mac_head;
172  struct macro_body      *mac_body;         /* Macro line to parse. */
173  struct yy_buffer_state *yybuf;
174  unsigned int            line_number;
175  unsigned int            loop_number;      /* Loop number for while loops. */
176  gp_symbol_t            *file_symbol;
177  file_context_t         *fc;               /* Position in the file context stack. */
178  struct amode           *astack;           /* Stack of amodes when a macro was called. */
179  gp_boolean              last_char_is_nl;  /* If the last read character is a newline. */
180  src_line_t              curr_src_line;    /* Current source line. */
181} source_context_t;
182
183typedef struct source_context_list {
184  /* head of source contexts
185   * tail of source contexts
186   * number of source contexts */
187  GPListHeader(source_context_t);
188} source_context_list_t;
189
190/************************************************************************/
191
192extern struct gpasm_state {
193  enum gpasm_modes  mode;
194  gp_boolean        mpasm_compatible;   /* MPASMX compatibility mode. */
195  gp_boolean        extended_pic16e;
196  int               radix;
197  enum formats      hex_format;
198  gp_boolean        case_insensitive;
199  gp_boolean        show_full_addr;
200  gp_boolean        quiet;
201  gp_boolean        use_absolute_path;
202  gp_boolean        debug_info;         /* Use debug directives for coff outputs. */
203  int               error_level;        /* 0, 1, 2 */
204  int               strict_level;       /* 0, 1, 2 */
205  int               num_paths;          /* Number of paths in the list. */
206  char             *paths[MAX_PATHS];   /* The list of include paths. */
207
208  struct {                              /* Command line override flags. */
209    gp_boolean radix;                   /* Values is specified by the command line. */
210    gp_boolean hex_format;
211    gp_boolean error_level;
212    gp_boolean strict_level;
213    gp_boolean macro_expand;
214    gp_boolean processor;
215    gp_boolean lst_force;
216  } cmd_line;
217
218  int pass;                             /* 1 or 2 */
219  unsigned int   byte_addr;             /* Current code-generation point. */
220  gp_boolean     dos_newlines;          /* Use DOS newlines in hex file. */
221  gp_boolean     memory_dump;           /* Dump instruction memory to standard output. */
222  gp_boolean     found_config;          /* Config directive in source code. */
223  gp_boolean     found_devid;           /* Config directive in source code. */
224  gp_boolean     found_idlocs;          /* Idlocs directive in source code. */
225  gp_boolean     found_end;             /* End directive in source code. */
226  int            maxram;                /* Highest legal data memory location. */
227  int            maxrom;                /* Highest legal program memory location. */
228  gp_bit_array_t badrom;                /* Nonzero indicates illegal memory. */
229
230  enum out_file
231       cod_file,                        /* Symbol output file control. */
232       dep_file,                        /* Dependency output file control. */
233       err_file,                        /* Error output file control. */
234       hex_file,                        /* Hex output file control. */
235       lst_file,                        /* List output file control. */
236       obj_file;                        /* Relocatable object file control. */
237
238  struct {                              /* Totals for errors, warnings, messages. */
239    int errors;
240    int warnings;
241    int messages;
242    int warnings_suppressed;
243    int messages_suppressed;
244  } num;
245
246  pic_processor_t processor;
247  gp_boolean      processor_chosen;     /* Nonzero after processor-specific init. */
248
249  struct {                              /* Processor data. */
250    proc_class_t class;                 /* Processor class. */
251    int          id_location;           /* address of last __idlocs */
252    int          bsr_boundary;          /* 18xx bsr boundary location */
253  } device;
254
255  uint8_t badram[MAX_RAM];              /* Nonzero indicates illegal memory. */
256  symbol_table_t
257    *stBuiltin,                         /* Built-ins: instructions, pseudo-ops */
258    *stDirective,                       /* bottom half of Builtin with directives */
259    *stGlobal,                          /* Global symbols. */
260    *stTop,                             /* Top of locals stack (stGlobal is base). */
261    *stDefines,                         /* Preprocessor #defines */
262    *stMacroParams,                     /* Macro #defines (stDefines is base). */
263    *stMacros;                          /* Macros */
264
265  MemBlock_t       *i_memory;           /* Instruction memory linked list. */
266  MemBlock_t       *c_memory;           /* Configuration memory linked list. */
267  conf_mem_block_t *conf_sec_mem;       /* Head of configuration section memory linked list. */
268  conf_mem_block_t *conf_sec_mem_last;  /* Tail of configuration section memory linked list. */
269
270  char *src_file_name,                  /* Source (.asm) file name. */
271        base_file_name[BUFSIZ],         /* Basename for generating hex,list,symbol filenames. */
272        cod_file_name[BUFSIZ],          /* Symbol (.cod) file name. */
273        dep_file_name[BUFSIZ],          /* Dependency (.d) file name. */
274        err_file_name[BUFSIZ],          /* Error - messages - (.err) file name. */
275        lst_file_name[BUFSIZ],          /* List (.lst) file name. */
276        obj_file_name[BUFSIZ];          /* Object (.o) file name. */
277
278  struct {                              /* Symbol file state: */
279    FILE         *f;                    /*   Symbol file output. */
280    gp_boolean    enabled;              /*   True if symbol file is enabled. */
281    unsigned int  emitting;             /*   Flag indicating when an opcode is emitted. */
282  } cod;
283
284  struct {                              /* Dep file state: */
285    FILE       *f;                      /*   Dep file output. */
286    gp_boolean  enabled;                /*   True if dep file is enabled. */
287  } dep;
288
289  struct {                              /* Error file state: */
290    FILE       *f;                      /*   Error file output. */
291    gp_boolean  enabled;                /*   True if err file is enabled. */
292  } err;
293
294  struct {                              /* List file state: */
295    FILE *f;                            /*   List file output. */
296    unsigned int
297      line_of_page,                     /*   What line are we at within the page. */
298      page,                             /*   What page are we at. */
299      lines_per_page,                   /*   Lines per page. */
300      line_number;                      /*   What line are we at within the file. */
301
302    gp_boolean
303      memory_map,                       /*   Memory Map dump enabled. */
304      symbol_table;                     /*   Symbol table dump enabled. */
305
306    enum {
307      LST_IN_NONE,
308      LST_IN_MEM,
309      LST_IN_SYMTAB,
310      LST_IN_MAP
311    } lst_state;                        /*   Listing state. */
312
313    struct {
314      unsigned int was_byte_addr;       /*   Value of state.byte_addr at start of line. */
315                                        /*   What kind of line was it? */
316      enum {
317        LTY_NONE,                       /*     Nothing - blank line */
318        LTY_ORG,                        /*     ORG pseudo-op */
319        LTY_DIR,                        /*     Directive, non-code generating. */
320        LTY_IDLOCS,                     /*     ID locations for 12 and 14 bit cores. */
321        LTY_INSN,                       /*     Some other instruction or pseudo. */
322        LTY_EQU,                        /*     An equate. */
323        LTY_DATA,                       /*     Data. */
324        LTY_RES,                        /*     Reserve memory. */
325        LTY_SEC,                        /*     new coff section */
326        LTY_SET,                        /*     A SET or '=' */
327        LTY_SET4,                       /*     A 2 byte variable, constant or local. */
328        LTY_CONFIG,                     /*     A __config line. */
329        LTY_NOLIST_DIR,                 /*     Don't list the directive (ENDW). */
330        LTY_DOLIST_DIR                  /*     Force list the directive (WHILE). */
331      } linetype;
332    } line;
333
334    char       start_date[80];          /*   When assembly started. */
335    gp_boolean enabled;                 /*   listing is enabled */
336    gp_boolean expand;                  /*   macro listings are expanded */
337    gp_boolean force;                   /*   ignoring nolist directives */
338    int        config_address;          /*   list config address for 16 bit devices */
339    char       title_name[80];          /*   given in TITLE directive */
340    char       subtitle_name[80];       /*   given in SUBTITLE directive */
341    int        tabstop;                 /*   tab-stop distance */
342    int        line_width;              /*   listing line width - list c=xxx option */
343    gpasmVal   cblock_lst;              /*   cblock constant for listing */
344  } lst;
345
346  struct {                              /* Preprocessor emit state: */
347    char       *preproc_file_name;      /*   preprocessor output file name */
348    FILE       *f;                      /*   preprocessor file pointer */
349    gp_boolean  do_emit;                /*   emit current preprocessed asm line */
350    src_line_t  curr_src_line;          /*  current preprocessed source line */
351  } preproc;
352
353  struct {                              /* Object file state: */
354    gp_object_t  *object;               /*   Object file. */
355    gp_section_t *section;              /*   Current section. */
356    int           section_num;          /*   Current section number. */
357    gp_boolean    enabled;              /*   True if object file is enabled. */
358    char          new_sect_name[80];    /*   new section name */
359    unsigned int  new_sect_addr;        /*   new section adress */
360    unsigned int  new_sect_flags;       /*   new section flags */
361    unsigned int  symbol_num;           /*   Current symbol number. */
362    unsigned int  flags;                /*   Current section flags. */
363    gp_symbol_t  *debug_file;           /*   Debug information for high level langs. */
364    unsigned int  debug_line;
365    gp_boolean    newcoff;
366    symbol_list_t symbol_fifo;
367  } obj;
368
369  source_context_list_t  src_list;      /* The stack of source files. */
370
371  file_context_list_t    file_list;     /* The stack of all files. */
372
373  struct amode          *astack;        /* Stack of amodes (macros, etc). */
374  gpasmVal               cblock;        /* cblock constant */
375  gp_boolean             cblock_defined;
376  struct macro_head     *mac_head;      /* Starting a macro... */
377  struct macro_body    **mac_prev;      /* Stitching ptr. */
378  struct macro_body     *mac_body;      /* While we're building a macro. */
379  struct macro_head     *while_head;    /* WHILEs work a lot like macros... */
380  unsigned int           while_depth;   /* WHILE nesting depth, used in WHILE definition. */
381  enum state_types       next_state;
382  gp_boolean             skipped_inst;  /* Instruction execution depends on the previous one (after btfsc etc.). */
383  gp_boolean             macro_dereference;  /* Use the source from where the macro was invoked for errors. */
384
385  union {
386    char              *file;
387    struct macro_head *macro;
388  } next_buffer;
389} state;
390
391#define VATRR_PROC_DEPENDENT    (1 << 0)  /* Depend on the type of processor. */
392#define VATRR_HAS_NO_VALUE      (1 << 1)  /* The variable has no value. */
393
394typedef struct variable {
395  gpasmVal            value;
396  enum gpasmValTypes  type;
397  enum gpasmValTypes  previous_type;    /* Can change from static to global. */
398  unsigned int        coff_section_num;
399  unsigned int        coff_section_flags;
400  unsigned int        coff_symbol_num;
401  unsigned int        flags;            /* VATRR_... */
402} variable_t;
403
404/************************************************************************/
405
406extern void yyerror(const char *String);
407
408/* gpasm.c */
409extern void add_path(const char *Path);
410
411/* util.c */
412typedef enum numstring_types {
413  NUM_STR_UNKNOWN = 0,
414  NUM_STR_BIN,
415  NUM_STR_OCT,
416  NUM_STR_DEC,
417  NUM_STR_HEX
418} numstring_t;
419
420extern int string_to_int(const char *String, int Radix);
421extern long gp_strtol(const char *String, numstring_t *Type);
422extern gp_boolean find_hv_macro(const char *String, const char **Start, const char **End);
423extern int gpasm_magic(const char *);
424extern const char *convert_escape_chars(const char *Ps, int *Value);
425extern char *convert_escaped_char(char *Str, char Ch);
426extern void coerce_str1(pnode_t *Exp);
427extern gpasmVal do_or_append_insn(const char *Op, pnode_t *Parms);
428
429extern gp_boolean set_symbol_attr(int *Section_number, unsigned int *Class, enum gpasmValTypes Type);
430
431extern void set_global(const char *Name, gpasmVal Value, enum gpasmValTypes Type,
432                       gp_boolean Proc_dependent, gp_boolean Has_no_value);
433
434extern variable_t *get_global_constant(const char *Name);
435extern void delete_variable_symbols(symbol_table_t *Table);
436extern void delete_processor_variable_symbols(symbol_table_t *Table);
437extern void select_error_level(int Level);
438extern void select_strict_level(int Level);
439extern void select_expand(const char *Expand);
440extern void select_hex_format(const char *Format_name);
441extern void select_radix(const char *Name);
442extern char *macro_params_to_string(char *String, size_t String_max_length, size_t *Length, const pnode_t *Macro_params);
443extern const char *variable_type_to_str(enum gpasmValTypes Type);
444extern const char *value_type_to_str(const variable_t *Variable, gp_boolean Previous);
445extern const char *pnode_symbol_name(const pnode_t *Pnode);
446extern gpasmVal pnode_symbol_value(const pnode_t *Pnode);
447extern const char *pnode_string(const pnode_t *Pnode);
448extern void msg_has_no_value(const char *Optional_text, const char *Symbol_name);
449extern void macro_append(void);
450extern void hex_create(void);
451
452/* parse.y */
453extern pnode_t *mk_constant(int Value);
454extern pnode_t *mk_offset(pnode_t *Pnode);
455extern pnode_t *mk_symbol(const char *String);
456extern pnode_t *mk_string(char *String);
457extern pnode_t *mk_list(pnode_t *Head, pnode_t *Tail);
458extern pnode_t *mk_2op(int Op, pnode_t *Pnode0, pnode_t *Pnode1);
459extern pnode_t *mk_1op(int Op, pnode_t *Pnode);
460
461#endif
462