Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /***********************************************************************
  2.  *
  3.  *  avra - Assembler for the Atmel AVR microcontroller series
  4.  *
  5.  *  Copyright (C) 1998-2006 Jon Anders Haugum, Tobias Weber
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; see the file COPYING.  If not, write to
  19.  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20.  *  Boston, MA 02111-1307, USA.
  21.  *
  22.  *
  23.  *  Authors of avra can be reached at:
  24.  *     email: jonah@omegav.ntnu.no, tobiw@suprafluid.com
  25.  *     www: http://sourceforge.net/projects/avra
  26.  */
  27.  
  28. #ifndef _AVRA_H_ /* avoid multiple inclusion */
  29. #define _AVRA_H_
  30.  
  31. #include <stdio.h>
  32. #include <time.h>
  33.  
  34. #ifndef VER_MAJOR
  35. #  define VER_MAJOR 1
  36. #endif
  37. #ifndef VER_MINOR
  38. #  define VER_MINOR 3
  39. #endif
  40. #ifndef VER_RELEASE
  41. #  define VER_RELEASE 0
  42. #endif
  43. #ifndef VER_BUILD
  44. #  define VER_BUILD 1
  45. #endif
  46. #ifndef VER_DATE
  47. #  define VER_DATE    "8 May 2010"
  48. #endif
  49.  
  50. #define IS_HOR_SPACE(x) ((x == ' ') || (x == 9))
  51. #define IS_LABEL(x)     (isalnum(x) || (x == '%') || (x == '_'))
  52. #define IS_END_OR_COMMENT(x)    ((x == ';') || (x == 10) || (x == 13) || (x == '\0') || (x == 12))
  53. #define IS_ENDLINE(x)   ((x == 10) || (x == 13) || (x == '\0') || (x == 12))
  54. #define IS_SEPARATOR(x) ((x == ' ') || (x == ',') || (x == '[') || (x == ']'))
  55.  
  56. #define LINEBUFFER_LENGTH 256
  57. #define MAX_NESTED_MACROLOOPS 256
  58.  
  59. #define MAX_MACRO_ARGS 10
  60.  
  61. /* warning switches */
  62.  
  63. /* Option enumeration */
  64. enum {
  65.         ARG_DEFINE = 0,         /* --define, -D            */
  66.         ARG_INCLUDEPATH,        /* --includedir, -I        */
  67.         ARG_LISTMAC,            /* --listmac               */
  68.         ARG_MAX_ERRORS,         /* --max_errors            */
  69.         ARG_COFF,               /* --coff                  */
  70.         ARG_DEVICES,            /* --devices               */
  71.         ARG_VER,                /* --version               */
  72.         ARG_HELP,               /* --help, -h              */
  73.         ARG_WRAP,               /* --wrap                  */
  74.         ARG_WARNINGS,           /* --warn, -W              */
  75.         ARG_FILEFORMAT,         /* --filetype              */
  76.         ARG_LISTFILE,           /* --listfile              */
  77.         ARG_OUTFILE,            /* --outfile   */
  78.         ARG_MAPFILE,            /* --mapfile   */
  79.         ARG_DEBUGFILE,          /* --debugfile */
  80.         ARG_EEPFILE,            /* --eepfile   */
  81.         ARG_COUNT
  82. };
  83.  
  84. enum {
  85.         MSGTYPE_ERROR = 0,
  86.         MSGTYPE_WARNING,
  87.         MSGTYPE_MESSAGE,
  88.         MSGTYPE_OUT_OF_MEM,
  89.         MSGTYPE_MESSAGE_NO_LF,          /* B.A. : Like MSGTYPE_MESSAGE, but without /n */
  90.         MSGTYPE_APPEND                  /* B.A. : Print Message without any header and without /n. To append messages */
  91.         /*      MSGTYPE_INCLUDE         B.A. Removed. Was not in used */
  92. };
  93.  
  94. enum {
  95.         PASS_1 = 0,
  96.         PASS_2
  97. };
  98.  
  99. enum {
  100.         SEGMENT_CODE = 0,
  101.         SEGMENT_DATA,
  102.         SEGMENT_EEPROM
  103. };
  104.  
  105. enum {
  106.         TERM_END = 0,
  107.         TERM_SPACE,
  108.         TERM_COMMA,
  109.         TERM_EQUAL,
  110.         TERM_DASH,
  111.         TERM_DOUBLEQUOTE,
  112.         TERM_COLON
  113. };
  114.  
  115. /* Structures */
  116.  
  117. struct prog_info
  118. {
  119.         struct args *args;
  120.         struct device *device;
  121.         struct file_info *fi;
  122.         struct macro_call *macro_call;
  123.         struct macro_line *macro_line;
  124.         FILE *list_file;
  125.         int list_on;
  126.         int map_on;
  127.         char *list_line;
  128.         char *root_path;
  129.         FILE *obj_file;
  130.         struct hex_file_info *hfi;
  131.         struct hex_file_info *eep_hfi;
  132.         int segment;
  133.         int cseg_addr;
  134.         int dseg_addr;
  135.         int eseg_addr;
  136.         int cseg_count;
  137.         int dseg_count;
  138.         int eseg_count;
  139.         int error_count;
  140.         int max_errors;
  141.         int warning_count;
  142.         struct include_file *last_include_file;
  143.         struct include_file *first_include_file;
  144.         struct def *first_def;
  145.         struct def *last_def;
  146.         struct label *first_label;
  147.         struct label *last_label;
  148.         struct label *first_constant;
  149.         struct label *last_constant;
  150.         struct label *first_variable;
  151.         struct label *last_variable;
  152.         struct label *first_blacklist;  /* B.A. : List for undefined symbols. Needed to make forward references safe */
  153.         struct label *last_blacklist;
  154.         struct macro *first_macro;
  155.         struct macro *last_macro;
  156.         struct macro_call *first_macro_call;
  157.         struct macro_call *last_macro_call;
  158.         struct orglist *first_orglist;  /* B.A. : List of used memory segments. Needed for overlap-check */
  159.         struct orglist *last_orglist;
  160.         int conditional_depth;
  161.         time_t time;                    /* B.A. : Use a global timestamp for listing header and %hour% ... tags */
  162.         /* coff additions */
  163.         FILE *coff_file;
  164.         /* Warning additions */
  165.         int NoRegDef;
  166.         int pass;
  167.        
  168. };
  169.  
  170. struct file_info
  171. {
  172.         FILE *fp;
  173.         struct include_file *include_file;
  174.         char buff[LINEBUFFER_LENGTH];
  175.         char scratch[LINEBUFFER_LENGTH];
  176.         int line_number;
  177.         int exit_file;
  178.         struct label *label;
  179. };
  180.  
  181. struct hex_file_info
  182. {
  183.         FILE *fp;
  184.         int count;
  185.         int linestart_addr;
  186.         int segment;
  187.         unsigned char hex_line[16];
  188. };
  189.  
  190. struct include_file
  191. {
  192.         struct include_file *next;
  193.         char *name;
  194.         int num;
  195. };
  196.  
  197. struct def
  198. {
  199.         struct def *next;
  200.         char *name;
  201.         int reg;
  202. };
  203.  
  204. struct label
  205. {
  206.         struct label *next;
  207.         char *name;
  208.         int value;
  209. };
  210.  
  211. struct macro
  212. {
  213.         struct macro *next;
  214.         char *name;
  215.         struct include_file *include_file;
  216.         int first_line_number;
  217.         struct macro_line *first_macro_line;
  218.         struct macro_label *first_label;
  219. };
  220.  
  221. struct macro_label
  222. {
  223.         char *label;
  224.         struct macro_label *next;
  225.         int running_number;
  226. };
  227.  
  228. struct macro_line
  229. {
  230.         struct macro_line *next;
  231.         char *line;
  232. };
  233.  
  234. struct macro_call
  235. {
  236.         struct macro_call *next;
  237.         int line_number;
  238.         struct include_file *include_file;
  239.         struct macro_call *prev_on_stack;
  240.         struct macro *macro;
  241.         int line_index;
  242.         int prev_line_index;
  243.         int nest_level;
  244.         struct label *first_label;
  245.         struct label *last_label;
  246. };
  247.  
  248. struct orglist
  249. {
  250.         struct orglist *next;
  251.         int segment;
  252.         int start;
  253.         int length;
  254. };
  255.  
  256. /* Prototypes */
  257. /* avra.c */
  258. int assemble(struct prog_info *pi);
  259. int load_arg_defines(struct prog_info *pi);
  260. struct prog_info *get_pi(struct args *args);
  261. void free_pi(struct prog_info *pi);
  262. void prepare_second_pass(struct prog_info *pi);
  263. void print_msg(struct prog_info *pi, int type, char *fmt, ... );
  264. void get_rootpath(struct prog_info *pi, struct args *args);
  265.  
  266. int def_const(struct prog_info *pi, const char *name, int value);
  267. int def_var(struct prog_info *pi, char *name, int value);
  268. int def_blacklist(struct prog_info *pi, const char *name);
  269. int def_orglist(struct prog_info *pi);                                  /* B.A. : Test for overlapping segments */
  270. int fix_orglist(struct prog_info *pi);
  271. void print_orglist(struct prog_info *pi);
  272. int test_orglist(struct prog_info *pi);
  273. int get_label(struct prog_info *pi,char *name,int *value);
  274. int get_constant(struct prog_info *pi,char *name,int *value);
  275. int get_variable(struct prog_info *pi,char *name,int *value);
  276. struct label *test_label(struct prog_info *pi,char *name,char *message);
  277. struct label *test_constant(struct prog_info *pi,char *name,char *message);
  278. struct label *test_variable(struct prog_info *pi,char *name,char *message);
  279. struct label *test_blacklist(struct prog_info *pi,char *name,char *message);
  280. struct label *search_symbol(struct prog_info *pi,struct label *first,char *name,char *message);
  281. void free_defs(struct prog_info *pi);
  282. void free_labels(struct prog_info *pi);
  283. void free_constants(struct prog_info *pi);
  284. void free_blacklist(struct prog_info *pi);
  285. void free_variables(struct prog_info *pi);
  286. void free_orglist(struct prog_info *pi);
  287.  
  288.  
  289. /* parser.c */
  290. int parse_file(struct prog_info *pi, char *filename);
  291. int parse_line(struct prog_info *pi, char *line);
  292. char *get_next_token(char *scratch, int term);
  293. char *fgets_new(struct prog_info *pi, char *s, int size, FILE *stream);
  294.  
  295. /* expr.c */
  296. int get_expr(struct prog_info *pi, char *data, int *value);
  297. //int get_operator(char *op);
  298. //int test_operator_at_precedence(int operator, int precedence);
  299. //int calc(struct prog_info *pi, int left, int operator, int right);
  300. //int get_function(char *function);
  301. //int do_function(int function, int value);
  302. //int log2(int value);
  303. int get_symbol(struct prog_info *pi, char *label_name, int *data);
  304. int par_length(char *data);
  305.  
  306. /* mnemonic.c */
  307. int parse_mnemonic(struct prog_info *pi);
  308. int get_mnemonic_type(char *mnemonic);
  309. int get_register(struct prog_info *pi, char *data);
  310. int get_bitnum(struct prog_info *pi, char *data, int *ret);
  311. int get_indirect(struct prog_info *pi, char *operand);
  312. int is_supported(struct prog_info *pi, char *name);
  313. int count_supported_instructions(int flags);
  314.  
  315. /* directiv.c */
  316. int parse_directive(struct prog_info *pi);
  317. int get_directive_type(char *directive);
  318. char *term_string(struct prog_info *pi, char *string);
  319. int parse_db(struct prog_info *pi, char *next);
  320. void write_db(struct prog_info *pi, char byte, char *prev, int count);
  321. int spool_conditional(struct prog_info *pi, int only_endif);
  322. int check_conditional(struct prog_info *pi, char *buff, int *current_depth, int *do_next, int only_endif);
  323. int test_include(const char *filename);
  324.  
  325. /* macro.c */
  326. int read_macro(struct prog_info *pi, char *name);
  327. struct macro *get_macro(struct prog_info *pi, char *name);
  328. struct macro_label *get_macro_label(char *line, struct macro *macro);
  329. int expand_macro(struct prog_info *pi, struct macro *macro, char *rest_line);
  330.  
  331.  
  332. /* file.c */
  333. int open_out_files(struct prog_info *pi, char *filename);
  334. void close_out_files(struct prog_info *pi);
  335. struct hex_file_info *open_hex_file(char *filename);
  336. void close_hex_file(struct hex_file_info *hfi);
  337. void write_ee_byte(struct prog_info *pi, int address, unsigned char data);
  338. void write_prog_word(struct prog_info *pi, int address, int data);
  339. void do_hex_line(struct hex_file_info *hfi);
  340. FILE *open_obj_file(struct prog_info *pi, char *filename);
  341. void close_obj_file(struct prog_info *pi, FILE *fp);
  342. void write_obj_record(struct prog_info *pi, int address, int data);
  343. void unlink_out_files(struct prog_info *pi, char *filename);
  344.  
  345. /* map.c */
  346. void write_map_file(struct prog_info *pi);
  347. char *Space(char *n);
  348.  
  349. /* stdextra.c */
  350. char *nocase_strcmp(char *s, char *t);
  351. char *nocase_strncmp(char *s, char *t, int n);
  352. char *nocase_strstr(char *s, char *t);
  353. int atox(char *s);
  354. int atoi_n(char *s, int n);
  355. int atox_n(char *s, int n);
  356. char *my_strlwr(char *in);
  357. char *my_strupr(char *in);
  358.  
  359. /* coff.c */
  360. FILE *open_coff_file(struct prog_info *pi, char *filename);
  361. void write_coff_file(struct prog_info *pi);
  362. void write_coff_eeprom( struct prog_info *pi, int address, unsigned char data);
  363. void write_coff_program( struct prog_info *pi, int address, unsigned int data);
  364. void close_coff_file(struct prog_info *pi, FILE *fp);
  365. int parse_stabs( struct prog_info *pi, char *p );
  366. int parse_stabn( struct prog_info *pi, char *p );
  367.  
  368. #endif /* end of avra.h */
  369.  
  370.  
  371.