Source file: /~heha/hs/gputils64-210929.zip/gpasm/gpasm.h

/* Common definitions for gpasm
   Copyright 1998-2005	James Bowman, Craig Franklin
*/

#pragma once
#include "stdhdr.h"
#include "gpRanges.h"

#define GPASM_VERSION_STRING        ("gpasm-" VERSION " #" REVISION " (" __DATE__ ")")

/* This symbol will get placed into the symbol table for the 16bit cores
   and thus allow compile-time selection of the proper macro set. */
#define __16bit_core_               "__16bit_core_"

#define __ACTIVE_BANK_ADDR          "__ACTIVE_BANK_ADDR"
#define __BANK_INV                  -1

#define __ASSUMED_BANK_ADDR         "__ASSUMED_BANK_ADDR"

#define __ACTIVE_PAGE_ADDR          "__ACTIVE_PAGE_ADDR"
#define __PAGE_INV                  -1

#define WHILE_LOOP_COUNT_MAX        255

#define STRCMP(s1, s2)              ((state.case_insensitive) ? strcasecmp((s1), (s2)) : strcmp((s1), (s2)))

#define MAX_PATHS                   100

#define IN_MACRO_WHILE_DEFINITION   state.mac_prev != NULL
#define IN_WHILE_DEFINITION         IN_MACRO_WHILE_DEFINITION && (state.while_head)
#define IN_MACRO_EXPANSION          state.src_list.last->type == SRC_MACRO
#define IN_WHILE_EXPANSION          state.src_list.last->type == SRC_WHILE
#define IN_FILE_EXPANSION           state.src_list.last->type == SRC_FILE

enum gpasmValTypes {
  VAL_CONSTANT,
  VAL_VARIABLE,
  VAL_EXTERNAL,
  VAL_GLOBAL,
  VAL_CBLOCK,
  VAL_STATIC,
  VAL_ADDRESS,
  VAL_ABSOLUTE,
  VAL_DEBUG
};

enum state_types {
  STATE_NOCHANGE,
  STATE_EXITMACRO,
  STATE_INCLUDE,
  STATE_MACRO,
  STATE_SECTION,
  STATE_SUBSTITUTION,
  STATE_WHILE
};

enum out_file {
  OUT_NORMAL,
  OUT_SUPPRESS,
  OUT_NAMED
};

enum file_types {
  FT_SRC,
  FT_OTHER
};

enum gpasm_modes {
  MODE_ABSOLUTE,
  MODE_RELOCATABLE
};

/************************************************************************/

typedef struct src_line {
  char   *line;                         /* Source line. */
  size_t  size;                         /* Source line allocated size. */
} src_line_t;

typedef struct conf_mem_block {
  int                    addr;
  MemBlock_t            *m;
  bool             new_config;
  gp_symbol_t           *file_symbol;
  unsigned           line_number;
  struct conf_mem_block *next;
} conf_mem_block_t;

/************************************************************************/

/* file_context: A structure to keep track of all files that have been opened.
                 Used to create the list of project files that can be found
                 in the .cod file. */

typedef struct file_context {
  /* This always should be the first item! (libgputils/gplist.c) */
  GPNodeHeader(struct file_context);

  char                *name;            /* file name */
  unsigned         id;              /* Unique identifier. */
  enum file_types      ft;              /* allowed file types */
} file_context_t;

typedef struct file_context_list {
  /* head of file contexts
   * tail of file contexts
   * number of file contexts */
  GPListHeader(file_context_t);
} file_context_list_t;

typedef struct macro_body {
  char              *src_line;          /* Original source line - for listing. */
  struct macro_body *next;              /* Next line in listing. */
} macro_body_t;

typedef struct macro_head {
  int           pass;                   /* Pass in which macro was defined: 1 or 2 */
  pnode_t      *parms;
  macro_body_t *body;
  bool    defined;                /* 1 macro has been defined so calls are valid */
  char         *src_name;
  unsigned  line_number;
  gp_symbol_t  *file_symbol;
} macro_head_t;

typedef struct amode {
  enum {
    IN_THEN,
    IN_ELIF,
    IN_ELSE
  } mode;

  bool    enabled;                /* Are we currently enabled? */
  bool    before_else_enabled;    /* This true if before else branch was an another enabled branch. */
  bool    upper_enabled;
  struct amode *upper;
} amode_t;

enum src_types {
  SRC_UNKNOWN,
  SRC_FILE,
  SRC_MACRO,
  SRC_WHILE
};

typedef struct source_context {
  /* This always should be the first item! (libgputils/gplist.c) */
  GPNodeHeader(struct source_context);

  char                   *name;
  enum src_types          type;
  FILE                   *f;
  struct macro_head      *mac_head;
  struct macro_body      *mac_body;         /* Macro line to parse. */
  struct yy_buffer_state *yybuf;
  unsigned            line_number;
  unsigned            loop_number;      /* Loop number for while loops. */
  gp_symbol_t            *file_symbol;
  file_context_t         *fc;               /* Position in the file context stack. */
  struct amode           *astack;           /* Stack of amodes when a macro was called. */
  bool              last_char_is_nl;  /* If the last read character is a newline. */
  src_line_t              curr_src_line;    /* Current source line. */
} source_context_t;

typedef struct source_context_list {
  /* head of source contexts
   * tail of source contexts
   * number of source contexts */
  GPListHeader(source_context_t);
} source_context_list_t;

/************************************************************************/

extern struct gpasm_state {
  enum gpasm_modes  mode;
  bool        mpasm_compatible;   /* MPASMX compatibility mode. */
  bool        extended_pic16e;
  int               radix;
  enum formats      hex_format;
  bool        case_insensitive;
  bool        show_full_addr;
  bool        quiet;
  bool        use_absolute_path;
  bool        debug_info;         /* Use debug directives for coff outputs. */
  int               error_level;        /* 0, 1, 2 */
  int               strict_level;       /* 0, 1, 2 */
  int               num_paths;          /* Number of paths in the list. */
  char             *paths[MAX_PATHS];   /* The list of include paths. */

  struct {                              /* Command line override flags. */
    bool radix;                   /* Values is specified by the command line. */
    bool hex_format;
    bool error_level;
    bool strict_level;
    bool macro_expand;
    bool processor;
    bool lst_force;
  } cmd_line;

  int pass;                             /* 1 or 2 */
  unsigned   byte_addr;             /* Current code-generation point. */
  bool     dos_newlines;          /* Use DOS newlines in hex file. */
  bool     memory_dump;           /* Dump instruction memory to standard output. */
  bool     found_config;          /* Config directive in source code. */
  bool     found_devid;           /* Config directive in source code. */
  bool     found_idlocs;          /* Idlocs directive in source code. */
  bool     found_end;             /* End directive in source code. */
  int            maxram;                /* Highest legal data memory location. */
  int            maxrom;                /* Highest legal program memory location. */

  enum out_file
       cod_file,                        /* Symbol output file control. */
       dep_file,                        /* Dependency output file control. */
       err_file,                        /* Error output file control. */
       hex_file,                        /* Hex output file control. */
       lst_file,                        /* List output file control. */
       obj_file;                        /* Relocatable object file control. */

  struct {                              /* Totals for errors, warnings, messages. */
    int errors;
    int warnings;
    int messages;
    int warnings_suppressed;
    int messages_suppressed;
  } num;

  pic_processor_t processor;
  bool      processor_chosen;     /* Nonzero after processor-specific init. */

  struct {                              /* Processor data. */
    proc_class_t pclass;                 /* Processor pclass. */
    int          id_location;           /* address of last __idlocs */
    int          bsr_boundary;          /* 18xx bsr boundary location */
  } device;

  symbol_table_t
    *stBuiltin,                         /* Built-ins: instructions, pseudo-ops */
    *stDirective,                       /* bottom half of Builtin with directives */
    *stGlobal,                          /* Global symbols. */
    *stTop,                             /* Top of locals stack (stGlobal is base). */
    *stDefines,                         /* Preprocessor #defines */
    *stMacroParams,                     /* Macro #defines (stDefines is base). */
    *stMacros;                          /* Macros */

  MemBlock_t       *i_memory;           /* Instruction memory linked list. */
  MemBlock_t       *c_memory;           /* Configuration memory linked list. */
  conf_mem_block_t *conf_sec_mem;       /* Head of configuration section memory linked list. */
  conf_mem_block_t *conf_sec_mem_last;  /* Tail of configuration section memory linked list. */

  const char*src_file_name;	// Source (.asm) file name
  char base_file_name[BUFSIZ],         /* Basename for generating hex,list,symbol filenames. */
        cod_file_name[BUFSIZ],          /* Symbol (.cod) file name. */
        dep_file_name[BUFSIZ],          /* Dependency (.d) file name. */
        err_file_name[BUFSIZ],          /* Error - messages - (.err) file name. */
        lst_file_name[BUFSIZ],          /* List (.lst) file name. */
        obj_file_name[BUFSIZ];          /* Object (.o) file name. */

  struct {                              /* Symbol file state: */
    FILE         *f;                    /*   Symbol file output. */
    bool    enabled;              /*   True if symbol file is enabled. */
    unsigned  emitting;             /*   Flag indicating when an opcode is emitted. */
  } cod;

  struct {                              /* Dep file state: */
    FILE       *f;                      /*   Dep file output. */
    bool  enabled;                /*   True if dep file is enabled. */
  } dep;

  struct {                              /* Error file state: */
    FILE       *f;                      /*   Error file output. */
    bool  enabled;                /*   True if err file is enabled. */
  } err;

  struct Lst{                              /* List file state: */
    FILE *f;                            /*   List file output. */
    unsigned
      line_of_page,                     /*   What line are we at within the page. */
      page,                             /*   What page are we at. */
      lines_per_page,                   /*   Lines per page. */
      line_number;                      /*   What line are we at within the file. */

    bool
      memory_map,                       /*   Memory Map dump enabled. */
      symbol_table;                     /*   Symbol table dump enabled. */

    enum{
      LST_IN_NONE,
      LST_IN_MEM,
      LST_IN_SYMTAB,
      LST_IN_MAP
    } lst_state;                        /*   Listing state. */

    struct Line{
      unsigned was_byte_addr;       /*   Value of state.byte_addr at start of line. */
                                        /*   What kind of line was it? */
      enum {
        LTY_NONE,                       /*     Nothing - blank line */
        LTY_ORG,                        /*     ORG pseudo-op */
        LTY_DIR,                        /*     Directive, non-code generating. */
        LTY_IDLOCS,                     /*     ID locations for 12 and 14 bit cores. */
        LTY_INSN,                       /*     Some other instruction or pseudo. */
        LTY_EQU,                        /*     An equate. */
        LTY_DATA,                       /*     Data. */
        LTY_RES,                        /*     Reserve memory. */
        LTY_SEC,                        /*     pnew coff section */
        LTY_SET,                        /*     A SET or '=' */
        LTY_SET4,                       /*     A 2 byte variable, constant or local. */
        LTY_CONFIG,                     /*     A __config line. */
        LTY_NOLIST_DIR,                 /*     Don't list the directive (ENDW). */
        LTY_DOLIST_DIR                  /*     Force list the directive (WHILE). */
      } linetype;
    } line;

    char       start_date[80];          /*   When assembly started. */
    bool enabled;                 /*   listing is enabled */
    bool expand;                  /*   macro listings are expanded */
    bool force;                   /*   ignoring nolist directives */
    int        config_address;          /*   list config address for 16 bit devices */
    char       title_name[80];          /*   given in TITLE directive */
    char       subtitle_name[80];       /*   given in SUBTITLE directive */
    int        tabstop;                 /*   tab-stop distance */
    int        line_width;              /*   listing line width - list c=xxx option */
    gpasmVal   cblock_lst;              /*   cblock constant for listing */
  } lst;

  struct {                              /* Preprocessor emit state: */
    const char *preproc_file_name;      /*   preprocessor output file name */
    FILE       *f;                      /*   preprocessor file pointer */
    bool  do_emit;                /*   emit current preprocessed asm line */
    src_line_t  curr_src_line;          /*  current preprocessed source line */
  } preproc;

  struct {                              /* Object file state: */
    gp_object_t  *object;               /*   Object file. */
    gp_section_t *section;              /*   Current section. */
    int           section_num;          /*   Current section number. */
    bool    enabled;              /*   True if object file is enabled. */
    char          new_sect_name[80];    /*   pnew section name */
    unsigned  new_sect_addr;        /*   pnew section adress */
    unsigned  new_sect_flags;       /*   pnew section flags */
    unsigned  symbol_num;           /*   Current symbol number. */
    unsigned  flags;                /*   Current section flags. */
    gp_symbol_t  *debug_file;           /*   Debug information for high level langs. */
    unsigned  debug_line;
    bool    newcoff;
  } obj;

  source_context_list_t  src_list;      /* The stack of source files. */

  file_context_list_t    file_list;     /* The stack of all files. */

  struct amode          *astack;        /* Stack of amodes (macros, etc). */
  gpasmVal               cblock;        /* cblock constant */
  bool             cblock_defined;
  struct macro_head     *mac_head;      /* Starting a macro... */
  struct macro_body    **mac_prev;      /* Stitching ptr. */
  struct macro_body     *mac_body;      /* While we're building a macro. */
  struct macro_head     *while_head;    /* WHILEs work a lot like macros... */
  unsigned           while_depth;   /* WHILE nesting depth, used in WHILE definition. */
  enum state_types       next_state;
  bool             skipped_inst;  /* Instruction execution depends on the previous one (after btfsc etc.). */
  bool             macro_dereference;  /* Use the source from where the macro was invoked for errors. */

  union {
    char              *file;
    struct macro_head *macro;
  } next_buffer;

  gp_Ranges badrom;	// region list of __badrom directive
  gp_Ranges badram;	// region list of __badram directive
} state;

#define VATRR_PROC_DEPENDENT    (1 << 0)  /* Depend on the type of processor. */
#define VATRR_HAS_NO_VALUE      (1 << 1)  /* The variable has no value. */

typedef struct variable {
  gpasmVal            value;
  enum gpasmValTypes  type;
  enum gpasmValTypes  previous_type;    /* Can change from static to global. */
  unsigned        coff_section_num;
  unsigned        coff_section_flags;
  unsigned        coff_symbol_num;
  unsigned        flags;            /* VATRR_... */
} variable_t;

/************************************************************************/

void yyerror(const char *String);

/* gpasm.cpp */
void add_path(const char *Path);

/* util.c */
typedef enum numstring_types {
  NUM_STR_UNKNOWN = 0,
  NUM_STR_BIN = 2,
  NUM_STR_OCT = 8,
  NUM_STR_DEC = 10,
  NUM_STR_HEX = 16,
} numstring_t;

int string_to_int(const char *String, int Radix);
long gp_strtol(const char *String, numstring_t *Type);
bool find_hv_macro(const char *String, const char **Start, const char **End);
int gpasm_magic(const char *);
const char *convert_escape_chars(const char *Ps, int *Value, bool utf8conv=false);
char *convert_escaped_char(char *Str, char Ch);
void coerce_str1(pnode_t *Exp);
gpasmVal do_or_append_insn(const char *Op, pnode_t *Parms);

bool set_symbol_attr(int *Section_number, unsigned *Class, enum gpasmValTypes Type);

void set_global(const char *Name, gpasmVal Value, enum gpasmValTypes Type,
                       bool Proc_dependent, bool Has_no_value);

variable_t* get_global_constant(const char *Name);
void delete_variable_symbols(symbol_table_t *Table);
void delete_processor_variable_symbols(symbol_table_t *Table);
void select_error_level(int Level);
void select_strict_level(int Level);
void select_expand(const char *Expand);
void select_hex_format(const char *Format_name);
void select_radix(const char *Name);
char *macro_params_to_string(char *String, size_t String_max_length, size_t *Length, const pnode_t *Macro_params);
const char *variable_type_to_str(enum gpasmValTypes Type);
const char *value_type_to_str(const variable_t *Variable, bool Previous);
const char *pnode_symbol_name(const pnode_t *Pnode);
gpasmVal pnode_symbol_value(const pnode_t *Pnode);
const char *pnode_string(const pnode_t *Pnode);
void msg_has_no_value(const char *Optional_text, const char *Symbol_name);
void macro_append();
void hex_create();

/* parse.y */
pnode_t* mk_constant(int Value);
pnode_t* mk_offset(pnode_t *Pnode);
pnode_t* mk_symbol(const char *String);
pnode_t* mk_string(char *String);
pnode_t* mk_list(pnode_t *Head, pnode_t *Tail);
pnode_t* mk_2op(int Op, pnode_t *Pnode0, pnode_t *Pnode1);
pnode_t* mk_1op(int Op, pnode_t *Pnode);
Detected encoding: UTF-80