1 /* gasp.c - Gnu assembler preprocessor main program.
2 Copyright (C) 1994, 1995 Free Software Foundation, Inc.
4 Written by Steve and Judy Chamberlain of Cygnus Support,
7 This file is part of GASP, the GNU Assembler Preprocessor.
9 GASP is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GASP is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GASP; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 This program translates the input macros and stuff into a form
27 suitable for gas to consume.
30 gasp [-sdhau] [-c char] [-o <outfile>] <infile>*
32 -s copy source to output
33 -c <char> comments are started with <char> instead of !
34 -u allow unreasonable stuff
36 -d print debugging stats
37 -s semi colons start comments
38 -a use alternate syntax
39 Pseudo ops can start with or without a .
40 Labels have to be in first column.
41 -I specify include dir
42 Macro arg parameters subsituted by name, don't need the &.
43 String can start with ' too.
44 Strings can be surrounded by <..>
45 A %<exp> in a string evaluates the expression
46 Literal char in a string with !
62 #ifdef NEED_MALLOC_DECLARATION
63 extern char *malloc ();
67 #include "libiberty.h"
69 char *program_version = "1.2";
71 #define MAX_INCLUDES 30 /* Maximum include depth */
72 #define MAX_REASONABLE 1000 /* Maximum number of expansions */
74 int unreasonable; /* -u on command line */
75 int stats; /* -d on command line */
76 int print_line_number; /* -p flag on command line */
77 int copysource; /* -c flag on command line */
78 int warnings; /* Number of WARNINGs generated so far. */
79 int errors; /* Number of ERRORs generated so far. */
80 int fatals; /* Number of fatal ERRORs generated so far (either 0 or 1). */
81 int alternate = 0; /* -a on command line */
82 int mri = 0; /* -M on command line */
83 char comment_char = '!';
84 int radix = 10; /* Default radix */
86 int had_end; /* Seen .END */
88 /* The output stream */
93 I had a couple of choices when deciding upon this data structure.
94 gas uses null terminated strings for all its internal work. This
95 often means that parts of the program that want to examine
96 substrings have to manipulate the data in the string to do the
97 right thing (a common operation is to single out a bit of text by
98 saving away the character after it, nulling it out, operating on
99 the substring and then replacing the character which was under the
100 null). This is a pain and I remember a load of problems that I had with
101 code in gas which almost got this right. Also, it's harder to grow and
102 allocate null terminated strings efficiently.
104 Obstacks provide all the functionality needed, but are too
105 complicated, hence the sb.
107 An sb is allocated by the caller, and is initialzed to point to an
108 sb_element. sb_elements are kept on a free lists, and used when
109 needed, replaced onto the free list when unused.
112 #define max_power_two 30 /* don't allow strings more than
113 2^max_power_two long */
114 /* structure of an sb */
117 char *ptr; /* points to the current block. */
118 int len; /* how much is used. */
119 int pot; /* the maximum length is 1<<pot */
124 /* Structure of the free list object of an sb */
136 sb_element *size[max_power_two];
139 sb_list_vector free_list;
141 int string_count[max_power_two];
143 /* the attributes of each character are stored as a bit pattern
144 chartype, which gives us quick tests. */
151 #define COMMENTBIT 16
153 #define ISCOMMENTCHAR(x) (chartype[(unsigned)(x)] & COMMENTBIT)
154 #define ISFIRSTCHAR(x) (chartype[(unsigned)(x)] & FIRSTBIT)
155 #define ISNEXTCHAR(x) (chartype[(unsigned)(x)] & NEXTBIT)
156 #define ISSEP(x) (chartype[(unsigned)(x)] & SEPBIT)
157 #define ISWHITE(x) (chartype[(unsigned)(x)] & WHITEBIT)
158 #define ISBASE(x) (chartype[(unsigned)(x)] & BASEBIT)
159 static char chartype[256];
162 /* Conditional assembly uses the `ifstack'. Each aif pushes another
163 entry onto the stack, and sets the on flag if it should. The aelse
164 sets hadelse, and toggles on. An aend pops a level. We limit to
165 100 levels of nesting, not because we're facists pigs with read
166 only minds, but because more than 100 levels of nesting is probably
167 a bug in the user's macro structure. */
169 #define IFNESTING 100
172 int on; /* is the level being output */
173 int hadelse; /* has an aelse been seen */
178 /* The final and intermediate results of expression evaluation are kept in
179 exp_t's. Note that a symbol is not an sb, but a pointer into the input
180 line. It must be coped somewhere safe before the next line is read in. */
191 int value; /* constant part */
192 symbol add_symbol; /* name part */
193 symbol sub_symbol; /* name part */
198 /* Hashing is done in a pretty standard way. A hash_table has a
199 pointer to a vector of pointers to hash_entrys, and the size of the
200 vector. A hash_entry contains a union of all the info we like to
201 store in hash table. If there is a hash collision, hash_entries
202 with the same hash are kept in a chain. */
204 /* What the data in a hash_entry means */
207 hash_integer, /* name->integer mapping */
208 hash_string, /* name->string mapping */
209 hash_macro, /* name is a macro */
210 hash_formal /* name is a formal argument */
215 sb key; /* symbol name */
216 hash_type type; /* symbol meaning */
221 struct macro_struct *m;
222 struct formal_struct *f;
224 struct hs *next; /* next hash_entry with same hash key */
234 /* Structures used to store macros.
236 Each macro knows its name and included text. It gets built with a
237 list of formal arguments, and also keeps a hash table which points
238 into the list to speed up formal search. Each formal knows its
239 name and its default value. Each time the macro is expanded, the
240 formals get the actual values attatched to them. */
242 /* describe the formal arguments to a macro */
244 typedef struct formal_struct
246 struct formal_struct *next; /* next formal in list */
247 sb name; /* name of the formal */
248 sb def; /* the default value */
249 sb actual; /* the actual argument (changed on each expansion) */
250 int index; /* the index of the formal 0..formal_count-1 */
254 /* describe the macro. */
256 typedef struct macro_struct
258 sb sub; /* substitution text. */
259 int formal_count; /* number of formal args. */
260 formal_entry *formals; /* pointer to list of formal_structs */
261 hash_table formal_hash; /* hash table of formals. */
265 /* how we nest files and expand macros etc.
267 we keep a stack of of include_stack structs. each include file
268 pushes a new level onto the stack. we keep an sb with a pushback
269 too. unget chars are pushed onto the pushback sb, getchars first
270 checks the pushback sb before reading from the input stream.
272 small things are expanded by adding the text of the item onto the
273 pushback sb. larger items are grown by pushing a new level and
274 allocating the entire pushback buf for the item. each time
275 something like a macro is expanded, the stack index is changed. we
276 can then perform an exitm by popping all entries off the stack with
277 the same stack index. if we're being reasonable, we can detect
278 recusive expansion by checking the index is reasonably small.
283 include_file, include_repeat, include_while, include_macro
288 sb pushback; /* current pushback stream */
289 int pushback_index; /* next char to read from stream */
290 FILE *handle; /* open file */
291 sb name; /* name of file */
292 int linecount; /* number of lines read so far */
294 int index; /* index of this layer */
296 include_stack[MAX_INCLUDES];
298 struct include_stack *sp;
299 #define isp (sp - include_stack)
305 /* Include file list */
307 typedef struct include_path
309 struct include_path *next;
313 include_path *paths_head;
314 include_path *paths_tail;
317 static void quit PARAMS ((void));
318 static void sb_build PARAMS ((sb *, int));
319 static void sb_new PARAMS ((sb *));
320 static void sb_kill PARAMS ((sb *));
321 static void sb_add_sb PARAMS ((sb *, sb *));
322 static void sb_check PARAMS ((sb *, int));
323 static void sb_reset PARAMS ((sb *));
324 static void sb_add_char PARAMS ((sb *, int));
325 static void sb_add_string PARAMS ((sb *, const char *));
326 static void sb_add_buffer PARAMS ((sb *, const char *, int));
327 static void sb_print PARAMS ((sb *));
328 static void sb_print_at PARAMS ((int, sb *));
329 static char *sb_name PARAMS ((sb *));
330 static int sb_skip_white PARAMS ((int, sb *));
331 static int sb_skip_comma PARAMS ((int, sb *));
332 static void hash_new_table PARAMS ((int, hash_table *));
333 static int hash PARAMS ((sb *));
334 static hash_entry *hash_create PARAMS ((hash_table *, sb *));
335 static void hash_add_to_string_table PARAMS ((hash_table *, sb *, sb *, int));
336 static void hash_add_to_int_table PARAMS ((hash_table *, sb *, int));
337 static hash_entry *hash_lookup PARAMS ((hash_table *, sb *));
338 static void checkconst PARAMS ((int, exp_t *));
339 static int sb_strtol PARAMS ((int, sb *, int, int *));
340 static int level_0 PARAMS ((int, sb *, exp_t *));
341 static int level_1 PARAMS ((int, sb *, exp_t *));
342 static int level_2 PARAMS ((int, sb *, exp_t *));
343 static int level_3 PARAMS ((int, sb *, exp_t *));
344 static int level_4 PARAMS ((int, sb *, exp_t *));
345 static int level_5 PARAMS ((int, sb *, exp_t *));
346 static int exp_parse PARAMS ((int, sb *, exp_t *));
347 static void exp_string PARAMS ((exp_t *, sb *));
348 static int exp_get_abs PARAMS ((const char *, int, sb *, int *));
350 static void strip_comments PARAMS ((sb *));
352 static void unget PARAMS ((int));
353 static void include_buf PARAMS ((sb *, sb *, include_type, int));
354 static void include_print_where_line PARAMS ((FILE *));
355 static void include_print_line PARAMS ((FILE *));
356 static int get_line PARAMS ((sb *));
357 static int grab_label PARAMS ((sb *, sb *));
358 static void change_base PARAMS ((int, sb *, sb *));
359 static void do_end PARAMS ((sb *));
360 static void do_assign PARAMS ((int, int, sb *));
361 static void do_radix PARAMS ((sb *));
362 static int get_opsize PARAMS ((int, sb *, int *));
363 static int eol PARAMS ((int, sb *));
364 static void do_data PARAMS ((int, sb *, int));
365 static void do_datab PARAMS ((int, sb *));
366 static void do_align PARAMS ((int, sb *));
367 static void do_res PARAMS ((int, sb *, int));
368 static void do_export PARAMS ((sb *));
369 static void do_print PARAMS ((int, sb *));
370 static void do_heading PARAMS ((int, sb *));
371 static void do_page PARAMS ((void));
372 static void do_form PARAMS ((int, sb *));
373 static int get_any_string PARAMS ((int, sb *, sb *, int, int));
374 static int skip_openp PARAMS ((int, sb *));
375 static int skip_closep PARAMS ((int, sb *));
376 static int dolen PARAMS ((int, sb *, sb *));
377 static int doinstr PARAMS ((int, sb *, sb *));
378 static int dosubstr PARAMS ((int, sb *, sb *));
379 static void process_assigns PARAMS ((int, sb *, sb *));
380 static int get_and_process PARAMS ((int, sb *, sb *));
381 static void process_file PARAMS ((void));
382 static void free_old_entry PARAMS ((hash_entry *));
383 static void do_assigna PARAMS ((int, sb *));
384 static void do_assignc PARAMS ((int, sb *));
385 static void do_reg PARAMS ((int, sb *));
386 static int condass_lookup_name PARAMS ((sb *, int, sb *, int));
387 static int whatcond PARAMS ((int, sb *, int *));
388 static int istrue PARAMS ((int, sb *));
389 static void do_aif PARAMS ((int, sb *));
390 static void do_aelse PARAMS ((void));
391 static void do_aendi PARAMS ((void));
392 static int condass_on PARAMS ((void));
393 static void do_if PARAMS ((int, sb *, int));
394 static int get_mri_string PARAMS ((int, sb *, sb *, int));
395 static void do_ifc PARAMS ((int, sb *, int));
396 static void buffer_and_nest PARAMS ((const char *, const char *, sb *));
397 static void do_aendr PARAMS ((void));
398 static void do_awhile PARAMS ((int, sb *));
399 static void do_aendw PARAMS ((void));
400 static void do_exitm PARAMS ((void));
401 static void do_arepeat PARAMS ((int, sb *));
402 static void do_endm PARAMS ((void));
403 static void do_irp PARAMS ((int, sb *, int));
404 static int do_formals PARAMS ((macro_entry *, int, sb *));
405 static void do_local PARAMS ((int, sb *));
406 static void do_macro PARAMS ((int, sb *));
407 static int get_token PARAMS ((int, sb *, sb *));
408 static int get_apost_token PARAMS ((int, sb *, sb *, int));
409 static int sub_actual
410 PARAMS ((int, sb *, sb *, hash_table *, int, sb *, int));
411 static void macro_expand_body
412 PARAMS ((sb *, sb *, sb *, formal_entry *, hash_table *));
413 static void macro_expand PARAMS ((sb *, int, sb *, macro_entry *));
414 static int macro_op PARAMS ((int, sb *));
415 static int getstring PARAMS ((int, sb *, sb *));
416 static void do_sdata PARAMS ((int, sb *, int));
417 static void do_sdatab PARAMS ((int, sb *));
418 static int new_file PARAMS ((const char *));
419 static void do_include PARAMS ((int, sb *));
420 static void include_pop PARAMS ((void));
421 static int get PARAMS ((void));
422 static int linecount PARAMS ((void));
423 static int include_next_index PARAMS ((void));
424 static void chartype_init PARAMS ((void));
425 static int process_pseudo_op PARAMS ((int, sb *, sb *));
426 static void add_keyword PARAMS ((const char *, int));
427 static void process_init PARAMS ((void));
428 static void do_define PARAMS ((const char *));
429 static void show_usage PARAMS ((FILE *, int));
430 static void show_help PARAMS ((void));
433 do { include_print_where_line (stderr); fprintf x ; fatals++; quit(); } while(0)
435 do { include_print_where_line (stderr); fprintf x; errors++; } while(0)
437 do { include_print_where_line (stderr); fprintf x; warnings++;} while(0)
441 /* exit the program and return the right ERROR code. */
454 for (i = 0; i < max_power_two; i++)
456 fprintf (stderr, "strings size %8d : %d\n", 1<<i, string_count[i]);
463 /* this program is about manipulating strings.
464 they are managed in things called `sb's which is an abbreviation
465 for string buffers. an sb has to be created, things can be glued
466 on to it, and at the end of it's life it should be freed. the
467 contents should never be pointed at whilst it is still growing,
468 since it could be moved at any time
472 sb_grow... (&foo,...);
478 /* initializes an sb. */
485 /* see if we can find one to allocate */
488 if (size > max_power_two)
490 FATAL ((stderr, "string longer than %d bytes requested.\n",
491 1 << max_power_two));
493 e = free_list.size[size];
496 /* nothing there, allocate one and stick into the free list */
497 e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size));
498 e->next = free_list.size[size];
500 free_list.size[size] = e;
501 string_count[size]++;
504 /* remove from free list */
506 free_list.size[size] = e->next;
508 /* copy into callers world */
520 sb_build (ptr, dsize);
523 /* deallocate the sb at ptr */
530 /* return item to free list */
531 ptr->item->next = free_list.size[ptr->pot];
532 free_list.size[ptr->pot] = ptr->item;
535 /* add the sb at s to the end of the sb at ptr */
537 static void sb_check ();
545 sb_check (ptr, s->len);
546 memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
550 /* make sure that the sb at ptr has room for another len characters,
551 and grow it if it doesn't. */
558 if (ptr->len + len >= 1 << ptr->pot)
562 while (ptr->len + len >= 1 << pot)
564 sb_build (&tmp, pot);
565 sb_add_sb (&tmp, ptr);
571 /* make the sb at ptr point back to the beginning. */
580 /* add character c to the end of the sb at ptr. */
588 ptr->ptr[ptr->len++] = c;
591 /* add null terminated string s to the end of sb at ptr. */
594 sb_add_string (ptr, s)
598 int len = strlen (s);
600 memcpy (ptr->ptr + ptr->len, s, len);
604 /* add string at s of length len to sb at ptr */
607 sb_add_buffer (ptr, s, len)
613 memcpy (ptr->ptr + ptr->len, s, len);
618 /* print the sb at ptr to the output file */
628 for (i = 0; i < ptr->len; i++)
632 fprintf (outfile, ",");
634 fprintf (outfile, "%d", ptr->ptr[i]);
640 sb_print_at (idx, ptr)
645 for (i = idx; i < ptr->len; i++)
646 putc (ptr->ptr[i], outfile);
648 /* put a null at the end of the sb at in and return the start of the
649 string, so that it can be used as an arg to printf %s. */
656 /* stick a null on the end of the string */
661 /* start at the index idx into the string in sb at ptr and skip
662 whitespace. return the index of the first non whitespace character */
665 sb_skip_white (idx, ptr)
669 while (idx < ptr->len && ISWHITE (ptr->ptr[idx]))
674 /* start at the index idx into the sb at ptr. skips whitespace,
675 a comma and any following whitespace. returnes the index of the
679 sb_skip_comma (idx, ptr)
683 while (idx < ptr->len && ISWHITE (ptr->ptr[idx]))
687 && ptr->ptr[idx] == ',')
690 while (idx < ptr->len && ISWHITE (ptr->ptr[idx]))
697 /* hash table maintenance. */
699 /* build a new hash table with size buckets, and fill in the info at ptr. */
702 hash_new_table (size, ptr)
708 ptr->table = (hash_entry **) xmalloc (size * (sizeof (hash_entry *)));
709 /* Fill with null-pointer, not zero-bit-pattern. */
710 for (i = 0; i < size; i++)
714 /* calculate and return the hash value of the sb at key. */
723 for (i = 0; i < key->len; i++)
731 /* lookup key in hash_table tab, if present, then return it, otherwise
732 build a new one and fill it with hash_integer. */
736 hash_create (tab, key)
740 int k = hash (key) % tab->size;
742 hash_entry **table = tab->table;
750 hash_entry *n = (hash_entry *) xmalloc (sizeof (hash_entry));
753 sb_add_sb (&n->key, key);
755 n->type = hash_integer;
758 if (strncmp (table[k]->key.ptr, key->ptr, key->len) == 0)
766 /* add sb name with key into hash_table tab. if replacing old value
767 and again, then ERROR. */
771 hash_add_to_string_table (tab, key, name, again)
777 hash_entry *ptr = hash_create (tab, key);
778 if (ptr->type == hash_integer)
780 sb_new (&ptr->value.s);
782 if (ptr->value.s.len)
785 ERROR ((stderr, "redefintion not allowed"));
788 ptr->type = hash_string;
789 sb_reset (&ptr->value.s);
791 sb_add_sb (&ptr->value.s, name);
794 /* add integer name to hash_table tab with sb key. */
798 hash_add_to_int_table (tab, key, name)
803 hash_entry *ptr = hash_create (tab, key);
807 /* lookup sb key in hash_table tab. if found return hash_entry result,
812 hash_lookup (tab, key)
816 int k = hash (key) % tab->size;
817 hash_entry **table = tab->table;
818 hash_entry *p = table[k];
821 if (p->key.len == key->len
822 && strncmp (p->key.ptr, key->ptr, key->len) == 0)
832 are handled in a really simple recursive decent way. each bit of
833 the machine takes an index into an sb and a pointer to an exp_t,
834 modifies the *exp_t and returns the index of the first character
835 past the part of the expression parsed.
837 expression precedence:
848 /* make sure that the exp_t at term is constant, if not the give the op ERROR. */
852 checkconst (op, term)
856 if (term->add_symbol.len
857 || term->sub_symbol.len)
859 ERROR ((stderr, "the %c operator cannot take non-absolute arguments.\n", op));
863 /* turn the number in string at idx into a number of base,
864 fill in ptr and return the index of the first character not in the
869 sb_strtol (idx, string, base, ptr)
876 idx = sb_skip_white (idx, string);
878 while (idx < string->len)
880 int ch = string->ptr[idx];
884 else if (ch >= 'a' && ch <= 'f')
886 else if (ch >= 'A' && ch <= 'F')
894 value = value * base + dig;
902 level_0 (idx, string, lhs)
907 lhs->add_symbol.len = 0;
908 lhs->add_symbol.name = 0;
910 lhs->sub_symbol.len = 0;
911 lhs->sub_symbol.name = 0;
913 idx = sb_skip_white (idx, string);
917 if (isdigit (string->ptr[idx]))
919 idx = sb_strtol (idx, string, 10, &lhs->value);
921 else if (ISFIRSTCHAR (string->ptr[idx]))
924 lhs->add_symbol.name = string->ptr + idx;
925 while (idx < string->len && ISNEXTCHAR (string->ptr[idx]))
930 lhs->add_symbol.len = len;
932 else if (string->ptr[idx] == '"')
936 ERROR ((stderr, "string where expression expected.\n"));
937 idx = getstring (idx, string, &acc);
942 ERROR ((stderr, "can't find primary in expression.\n"));
945 return sb_skip_white (idx, string);
951 level_1 (idx, string, lhs)
956 idx = sb_skip_white (idx, string);
958 switch (string->ptr[idx])
961 idx = level_1 (idx + 1, string, lhs);
964 idx = level_1 (idx + 1, string, lhs);
965 checkconst ('~', lhs);
966 lhs->value = ~lhs->value;
971 idx = level_1 (idx + 1, string, lhs);
972 lhs->value = -lhs->value;
974 lhs->add_symbol = lhs->sub_symbol;
980 idx = level_5 (sb_skip_white (idx, string), string, lhs);
981 if (string->ptr[idx] != ')')
982 ERROR ((stderr, "misplaced closing parens.\n"));
987 idx = level_0 (idx, string, lhs);
990 return sb_skip_white (idx, string);
994 level_2 (idx, string, lhs)
1001 idx = level_1 (idx, string, lhs);
1003 while (idx < string->len && (string->ptr[idx] == '*'
1004 || string->ptr[idx] == '/'))
1006 char op = string->ptr[idx++];
1007 idx = level_1 (idx, string, &rhs);
1011 checkconst ('*', lhs);
1012 checkconst ('*', &rhs);
1013 lhs->value *= rhs.value;
1016 checkconst ('/', lhs);
1017 checkconst ('/', &rhs);
1019 ERROR ((stderr, "attempt to divide by zero.\n"));
1021 lhs->value /= rhs.value;
1025 return sb_skip_white (idx, string);
1030 level_3 (idx, string, lhs)
1037 idx = level_2 (idx, string, lhs);
1039 while (idx < string->len
1040 && (string->ptr[idx] == '+'
1041 || string->ptr[idx] == '-'))
1043 char op = string->ptr[idx++];
1044 idx = level_2 (idx, string, &rhs);
1048 lhs->value += rhs.value;
1049 if (lhs->add_symbol.name && rhs.add_symbol.name)
1051 ERROR ((stderr, "can't add two relocatable expressions\n"));
1053 /* change nn+symbol to symbol + nn */
1054 if (rhs.add_symbol.name)
1056 lhs->add_symbol = rhs.add_symbol;
1060 lhs->value -= rhs.value;
1061 lhs->sub_symbol = rhs.add_symbol;
1065 return sb_skip_white (idx, string);
1069 level_4 (idx, string, lhs)
1076 idx = level_3 (idx, string, lhs);
1078 while (idx < string->len &&
1079 string->ptr[idx] == '&')
1081 char op = string->ptr[idx++];
1082 idx = level_3 (idx, string, &rhs);
1086 checkconst ('&', lhs);
1087 checkconst ('&', &rhs);
1088 lhs->value &= rhs.value;
1092 return sb_skip_white (idx, string);
1096 level_5 (idx, string, lhs)
1103 idx = level_4 (idx, string, lhs);
1105 while (idx < string->len
1106 && (string->ptr[idx] == '|' || string->ptr[idx] == '~'))
1108 char op = string->ptr[idx++];
1109 idx = level_4 (idx, string, &rhs);
1113 checkconst ('|', lhs);
1114 checkconst ('|', &rhs);
1115 lhs->value |= rhs.value;
1118 checkconst ('~', lhs);
1119 checkconst ('~', &rhs);
1120 lhs->value ^= rhs.value;
1124 return sb_skip_white (idx, string);
1128 /* parse the expression at offset idx into string, fill up res with
1129 the result. return the index of the first char past the expression.
1133 exp_parse (idx, string, res)
1138 return level_5 (sb_skip_white (idx, string), string, res);
1142 /* turn the expression at exp into text and glue it onto the end of
1146 exp_string (exp, string)
1154 if (exp->add_symbol.len)
1156 sb_add_buffer (string, exp->add_symbol.name, exp->add_symbol.len);
1164 sb_add_char (string, '+');
1165 sprintf (buf, "%d", exp->value);
1166 sb_add_string (string, buf);
1170 if (exp->sub_symbol.len)
1172 sb_add_char (string, '-');
1173 sb_add_buffer (string, exp->add_symbol.name, exp->add_symbol.len);
1179 sb_add_char (string, '0');
1183 /* parse the expression at offset idx into sb in, return the value in val.
1184 if the expression is not constant, give ERROR emsg. returns the index
1185 of the first character past the end of the expression. */
1188 exp_get_abs (emsg, idx, in, val)
1195 idx = exp_parse (idx, in, &res);
1196 if (res.add_symbol.len || res.sub_symbol.len)
1197 ERROR ((stderr, emsg));
1203 sb label; /* current label parsed from line */
1204 hash_table assign_hash_table; /* hash table for all assigned variables */
1205 hash_table keyword_hash_table; /* hash table for keyword */
1206 hash_table vars; /* hash table for eq variables */
1208 #define in_comment ';'
1212 strip_comments (out)
1217 for (i = 0; i < out->len; i++)
1219 if (ISCOMMENTCHAR(s[i]))
1228 /* push back character ch so that it can be read again. */
1238 if (sp->pushback_index)
1239 sp->pushback_index--;
1241 sb_add_char (&sp->pushback, ch);
1244 /* push the sb ptr onto the include stack, with the given name, type and index. */
1248 include_buf (name, ptr, type, index)
1255 if (sp - include_stack >= MAX_INCLUDES)
1256 FATAL ((stderr, "unreasonable nesting.\n"));
1258 sb_add_sb (&sp->name, name);
1261 sp->pushback_index = 0;
1264 sb_new (&sp->pushback);
1265 sb_add_sb (&sp->pushback, ptr);
1269 /* used in ERROR messages, print info on where the include stack is onto file. */
1272 include_print_where_line (file)
1275 struct include_stack *p = include_stack + 1;
1279 fprintf (file, "%s:%d ", sb_name (&p->name), p->linecount - 1);
1284 /* used in listings, print the line number onto file. */
1286 include_print_line (file)
1290 struct include_stack *p = include_stack + 1;
1292 n = fprintf (file, "%4d", p->linecount);
1296 n += fprintf (file, ".%d", p->linecount);
1301 fprintf (file, " ");
1307 /* read a line from the top of the include stack into sb in. */
1318 putc (comment_char, outfile);
1319 if (print_line_number)
1320 include_print_line (outfile);
1334 WARNING ((stderr, "End of file not at start of line.\n"));
1336 putc ('\n', outfile);
1355 /* continued line */
1358 putc (comment_char, outfile);
1359 putc ('+', outfile);
1372 sb_add_char (in, ch);
1380 /* find a label from sb in and put it in out. */
1383 grab_label (in, out)
1389 if (ISFIRSTCHAR (in->ptr[i]))
1391 sb_add_char (out, in->ptr[i]);
1393 while ((ISNEXTCHAR (in->ptr[i])
1394 || in->ptr[i] == '\\'
1395 || in->ptr[i] == '&')
1398 sb_add_char (out, in->ptr[i]);
1405 /* find all strange base stuff and turn into decimal. also
1406 find all the other numbers and convert them from the default radix */
1409 change_base (idx, in, out)
1416 while (idx < in->len)
1418 if (idx < in->len - 1 && in->ptr[idx + 1] == '\'' && ! mri)
1422 switch (in->ptr[idx])
1441 ERROR ((stderr, "Illegal base character %c.\n", in->ptr[idx]));
1446 idx = sb_strtol (idx + 2, in, base, &value);
1447 sprintf (buffer, "%d", value);
1448 sb_add_string (out, buffer);
1450 else if (ISFIRSTCHAR (in->ptr[idx]))
1452 /* copy entire names through quickly */
1453 sb_add_char (out, in->ptr[idx]);
1455 while (idx < in->len && ISNEXTCHAR (in->ptr[idx]))
1457 sb_add_char (out, in->ptr[idx]);
1461 else if (isdigit (in->ptr[idx]))
1464 /* all numbers must start with a digit, let's chew it and
1466 idx = sb_strtol (idx, in, radix, &value);
1467 sprintf (buffer, "%d", value);
1468 sb_add_string (out, buffer);
1470 /* skip all undigsested letters */
1471 while (idx < in->len && ISNEXTCHAR (in->ptr[idx]))
1473 sb_add_char (out, in->ptr[idx]);
1477 else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
1479 char tchar = in->ptr[idx];
1480 /* copy entire names through quickly */
1481 sb_add_char (out, in->ptr[idx]);
1483 while (idx < in->len && in->ptr[idx] != tchar)
1485 sb_add_char (out, in->ptr[idx]);
1491 /* nothing special, just pass it through */
1492 sb_add_char (out, in->ptr[idx]);
1506 fprintf (outfile, "%s\n", sb_name (in));
1512 do_assign (again, idx, in)
1517 /* stick label in symbol table with following value */
1522 idx = exp_parse (idx, in, &e);
1523 exp_string (&e, &acc);
1524 hash_add_to_string_table (&assign_hash_table, &label, &acc, again);
1529 /* .radix [b|q|d|h] */
1536 int idx = sb_skip_white (0, ptr);
1537 switch (ptr->ptr[idx])
1556 ERROR ((stderr, "radix is %c must be one of b, q, d or h", radix));
1561 /* Parse off a .b, .w or .l */
1564 get_opsize (idx, in, size)
1570 if (in->ptr[idx] == '.')
1574 switch (in->ptr[idx])
1592 ERROR ((stderr, "size must be one of b, w or l, is %c.\n", in->ptr[idx]));
1605 idx = sb_skip_white (idx, line);
1607 && ISCOMMENTCHAR(line->ptr[idx]))
1609 if (idx >= line->len)
1614 /* .data [.b|.w|.l] <data>*
1615 or d[bwl] <data>* */
1618 do_data (idx, in, size)
1624 char *opname = ".yikes!";
1630 idx = get_opsize (idx, in, &opsize);
1649 fprintf (outfile, "%s\t", opname);
1651 idx = sb_skip_white (idx, in);
1655 && in->ptr[idx] == '"')
1658 idx = getstring (idx, in, &acc);
1659 for (i = 0; i < acc.len; i++)
1662 fprintf(outfile,",");
1663 fprintf (outfile, "%d", acc.ptr[i]);
1668 while (!eol (idx, in))
1671 idx = exp_parse (idx, in, &e);
1672 exp_string (&e, &acc);
1673 sb_add_char (&acc, 0);
1674 fprintf (outfile, acc.ptr);
1675 if (idx < in->len && in->ptr[idx] == ',')
1677 fprintf (outfile, ",");
1683 sb_print_at (idx, in);
1684 fprintf (outfile, "\n");
1687 /* .datab [.b|.w|.l] <repeat>,<fill> */
1698 idx = get_opsize (idx, in, &opsize);
1700 idx = exp_get_abs ("datab repeat must be constant.\n", idx, in, &repeat);
1701 idx = sb_skip_comma (idx, in);
1702 idx = exp_get_abs ("datab data must be absolute.\n", idx, in, &fill);
1704 fprintf (outfile, ".fill\t%d,%d,%d\n", repeat, opsize, fill);
1715 idx = exp_get_abs ("align needs absolute expression.\n", idx, in, &al);
1720 WARNING ((stderr, "alignment must be one of 1, 2 or 4.\n"));
1722 fprintf (outfile, ".align %d\n", al);
1725 /* .res[.b|.w|.l] <size> */
1728 do_res (idx, in, type)
1736 idx = get_opsize (idx, in, &size);
1737 while (!eol(idx, in))
1739 idx = sb_skip_white (idx, in);
1740 if (in->ptr[idx] == ',')
1742 idx = exp_get_abs ("res needs absolute expression for fill count.\n", idx, in, &count);
1744 if (type == 'c' || type == 'z')
1747 fprintf (outfile, ".space %d\n", count * size);
1758 fprintf (outfile, ".global %s\n", sb_name (in));
1761 /* .print [list] [nolist] */
1768 idx = sb_skip_white (idx, in);
1769 while (idx < in->len)
1771 if (strncasecmp (in->ptr + idx, "LIST", 4) == 0)
1773 fprintf (outfile, ".list\n");
1776 else if (strncasecmp (in->ptr + idx, "NOLIST", 6) == 0)
1778 fprintf (outfile, ".nolist\n");
1787 do_heading (idx, in)
1793 idx = getstring (idx, in, &head);
1794 fprintf (outfile, ".title \"%s\"\n", sb_name (&head));
1803 fprintf (outfile, ".eject\n");
1806 /* .form [lin=<value>] [col=<value>] */
1814 idx = sb_skip_white (idx, in);
1816 while (idx < in->len)
1819 if (strncasecmp (in->ptr + idx, "LIN=", 4) == 0)
1822 idx = exp_get_abs ("form LIN= needs absolute expresssion.\n", idx, in, &lines);
1825 if (strncasecmp (in->ptr + idx, "COL=", 4) == 0)
1828 idx = exp_get_abs ("form COL= needs absolute expresssion.\n", idx, in, &columns);
1833 fprintf (outfile, ".psize %d,%d\n", lines, columns);
1838 /* Fetch string from the input stream,
1840 'Bxyx<whitespace> -> return 'Bxyza
1841 %<char> -> return string of decimal value of x
1842 "<string>" -> return string
1843 xyx<whitespace> -> return xyz
1846 get_any_string (idx, in, out, expand, pretend_quoted)
1854 idx = sb_skip_white (idx, in);
1858 if (in->len > 2 && in->ptr[idx+1] == '\'' && ISBASE (in->ptr[idx]))
1860 while (!ISSEP (in->ptr[idx]))
1861 sb_add_char (out, in->ptr[idx++]);
1863 else if (in->ptr[idx] == '%'
1869 /* Turns the next expression into a string */
1870 idx = exp_get_abs ("% operator needs absolute expression",
1874 sprintf(buf, "%d", val);
1875 sb_add_string (out, buf);
1877 else if (in->ptr[idx] == '"'
1878 || in->ptr[idx] == '<'
1879 || (alternate && in->ptr[idx] == '\''))
1881 if (alternate && expand)
1883 /* Keep the quotes */
1884 sb_add_char (out, '\"');
1886 idx = getstring (idx, in, out);
1887 sb_add_char (out, '\"');
1891 idx = getstring (idx, in, out);
1896 while (idx < in->len
1897 && (in->ptr[idx] == '"'
1898 || in->ptr[idx] == '\''
1900 || !ISSEP (in->ptr[idx])))
1902 if (in->ptr[idx] == '"'
1903 || in->ptr[idx] == '\'')
1905 char tchar = in->ptr[idx];
1906 sb_add_char (out, in->ptr[idx++]);
1907 while (idx < in->len
1908 && in->ptr[idx] != tchar)
1909 sb_add_char (out, in->ptr[idx++]);
1913 sb_add_char (out, in->ptr[idx++]);
1922 /* skip along sb in starting at idx, suck off whitespace a ( and more
1923 whitespace. return the idx of the next char */
1926 skip_openp (idx, in)
1930 idx = sb_skip_white (idx, in);
1931 if (in->ptr[idx] != '(')
1932 ERROR ((stderr, "misplaced ( .\n"));
1933 idx = sb_skip_white (idx + 1, in);
1937 /* skip along sb in starting at idx, suck off whitespace a ) and more
1938 whitespace. return the idx of the next char */
1941 skip_closep (idx, in)
1945 idx = sb_skip_white (idx, in);
1946 if (in->ptr[idx] != ')')
1947 ERROR ((stderr, "misplaced ).\n"));
1948 idx = sb_skip_white (idx + 1, in);
1955 dolen (idx, in, out)
1964 sb_new (&stringout);
1965 idx = skip_openp (idx, in);
1966 idx = get_and_process (idx, in, &stringout);
1967 idx = skip_closep (idx, in);
1968 sprintf (buffer, "%d", stringout.len);
1969 sb_add_string (out, buffer);
1971 sb_kill (&stringout);
1980 doinstr (idx, in, out)
1994 idx = skip_openp (idx, in);
1995 idx = get_and_process (idx, in, &string);
1996 idx = sb_skip_comma (idx, in);
1997 idx = get_and_process (idx, in, &search);
1998 idx = sb_skip_comma (idx, in);
1999 if (isdigit (in->ptr[idx]))
2001 idx = exp_get_abs (".instr needs absolute expresson.\n", idx, in, &start);
2007 idx = skip_closep (idx, in);
2009 for (i = start; i < string.len; i++)
2011 if (strncmp (string.ptr + i, search.ptr, search.len) == 0)
2017 sprintf (buffer, "%d", res);
2018 sb_add_string (out, buffer);
2026 dosubstr (idx, in, out)
2036 idx = skip_openp (idx, in);
2037 idx = get_and_process (idx, in, &string);
2038 idx = sb_skip_comma (idx, in);
2039 idx = exp_get_abs ("need absolute position.\n", idx, in, &pos);
2040 idx = sb_skip_comma (idx, in);
2041 idx = exp_get_abs ("need absolute length.\n", idx, in, &len);
2042 idx = skip_closep (idx, in);
2045 if (len < 0 || pos < 0 ||
2047 || pos + len > string.len)
2049 sb_add_string (out, " ");
2053 sb_add_char (out, '"');
2056 sb_add_char (out, string.ptr[pos++]);
2059 sb_add_char (out, '"');
2065 /* scan line, change tokens in the hash table to their replacements */
2067 process_assigns (idx, in, buf)
2072 while (idx < in->len)
2075 if (in->ptr[idx] == '\\'
2076 && in->ptr[idx + 1] == '&')
2078 idx = condass_lookup_name (in, idx + 2, buf, 1);
2080 else if (in->ptr[idx] == '\\'
2081 && in->ptr[idx + 1] == '$')
2083 idx = condass_lookup_name (in, idx + 2, buf, 0);
2085 else if (idx + 3 < in->len
2086 && in->ptr[idx] == '.'
2087 && toupper ((unsigned char) in->ptr[idx + 1]) == 'L'
2088 && toupper ((unsigned char) in->ptr[idx + 2]) == 'E'
2089 && toupper ((unsigned char) in->ptr[idx + 3]) == 'N')
2090 idx = dolen (idx + 4, in, buf);
2091 else if (idx + 6 < in->len
2092 && in->ptr[idx] == '.'
2093 && toupper ((unsigned char) in->ptr[idx + 1]) == 'I'
2094 && toupper ((unsigned char) in->ptr[idx + 2]) == 'N'
2095 && toupper ((unsigned char) in->ptr[idx + 3]) == 'S'
2096 && toupper ((unsigned char) in->ptr[idx + 4]) == 'T'
2097 && toupper ((unsigned char) in->ptr[idx + 5]) == 'R')
2098 idx = doinstr (idx + 6, in, buf);
2099 else if (idx + 7 < in->len
2100 && in->ptr[idx] == '.'
2101 && toupper ((unsigned char) in->ptr[idx + 1]) == 'S'
2102 && toupper ((unsigned char) in->ptr[idx + 2]) == 'U'
2103 && toupper ((unsigned char) in->ptr[idx + 3]) == 'B'
2104 && toupper ((unsigned char) in->ptr[idx + 4]) == 'S'
2105 && toupper ((unsigned char) in->ptr[idx + 5]) == 'T'
2106 && toupper ((unsigned char) in->ptr[idx + 6]) == 'R')
2107 idx = dosubstr (idx + 7, in, buf);
2108 else if (ISFIRSTCHAR (in->ptr[idx]))
2110 /* may be a simple name subsitution, see if we have a word */
2113 while (cur < in->len
2114 && (ISNEXTCHAR (in->ptr[cur])))
2118 sb_add_buffer (&acc, in->ptr + idx, cur - idx);
2119 ptr = hash_lookup (&assign_hash_table, &acc);
2122 /* Found a definition for it */
2123 sb_add_sb (buf, &ptr->value.s);
2127 /* No definition, just copy the word */
2128 sb_add_sb (buf, &acc);
2135 sb_add_char (buf, in->ptr[idx++]);
2141 get_and_process (idx, in, out)
2148 idx = get_any_string (idx, in, &t, 1, 0);
2149 process_assigns (0, &t, out);
2170 more = get_line (&line);
2173 /* Find any label and pseudo op that we're intested in */
2178 fprintf (outfile, "\n");
2181 && (line.ptr[0] == '*'
2182 || line.ptr[0] == '!'))
2184 /* MRI line comment. */
2185 fprintf (outfile, sb_name (&line));
2189 l = grab_label (&line, &label_in);
2193 /* Munge any label */
2196 process_assigns (0, &label_in, &label);
2199 if (line.ptr[l] == ':')
2201 while (ISWHITE (line.ptr[l]) && l < line.len)
2206 if (process_pseudo_op (l, &line, &acc))
2212 else if (condass_on ())
2214 if (macro_op (l, &line))
2224 fprintf (outfile, "%s:\t", sb_name (&label));
2227 fprintf (outfile, "\t");
2229 process_assigns (l, &line, &t1);
2231 change_base (0, &t1, &t2);
2232 fprintf (outfile, "%s\n", sb_name (&t2));
2238 /* Only a label on this line */
2239 if (label.len && condass_on())
2241 fprintf (outfile, "%s:\n", sb_name (&label));
2249 more = get_line (&line);
2252 if (!had_end && !mri)
2253 WARNING ((stderr, "END missing from end of file.\n"));
2261 free_old_entry (ptr)
2266 if (ptr->type == hash_string)
2267 sb_kill(&ptr->value.s);
2271 /* name: .ASSIGNA <value> */
2274 do_assigna (idx, in)
2282 process_assigns (idx, in, &tmp);
2283 idx = exp_get_abs (".ASSIGNA needs constant expression argument.\n", 0, &tmp, &val);
2287 ERROR ((stderr, ".ASSIGNA without label.\n"));
2291 hash_entry *ptr = hash_create (&vars, &label);
2292 free_old_entry (ptr);
2293 ptr->type = hash_integer;
2299 /* name: .ASSIGNC <string> */
2302 do_assignc (idx, in)
2308 idx = getstring (idx, in, &acc);
2312 ERROR ((stderr, ".ASSIGNS without label.\n"));
2316 hash_entry *ptr = hash_create (&vars, &label);
2317 free_old_entry (ptr);
2318 ptr->type = hash_string;
2319 sb_new (&ptr->value.s);
2320 sb_add_sb (&ptr->value.s, &acc);
2326 /* name: .REG (reg) */
2333 /* remove reg stuff from inside parens */
2336 idx = skip_openp (idx, in);
2338 idx = sb_skip_white (idx, in);
2340 while (idx < in->len
2343 : in->ptr[idx] != ')'))
2345 sb_add_char (&what, in->ptr[idx]);
2348 hash_add_to_string_table (&assign_hash_table, &label, &what, 1);
2354 condass_lookup_name (inbuf, idx, out, warn)
2362 sb_new (&condass_acc);
2364 while (idx < inbuf->len
2365 && ISNEXTCHAR (inbuf->ptr[idx]))
2367 sb_add_char (&condass_acc, inbuf->ptr[idx++]);
2370 if (inbuf->ptr[idx] == '\'')
2372 ptr = hash_lookup (&vars, &condass_acc);
2379 WARNING ((stderr, "Can't find preprocessor variable %s.\n", sb_name (&condass_acc)));
2383 sb_add_string (out, "0");
2388 if (ptr->type == hash_integer)
2391 sprintf (buffer, "%d", ptr->value.i);
2392 sb_add_string (out, buffer);
2396 sb_add_sb (out, &ptr->value.s);
2399 sb_kill (&condass_acc);
2412 whatcond (idx, in, val)
2419 idx = sb_skip_white (idx, in);
2421 if (idx + 1 < in->len)
2427 a = toupper ((unsigned char) p[0]);
2428 b = toupper ((unsigned char) p[1]);
2429 if (a == 'E' && b == 'Q')
2431 else if (a == 'N' && b == 'E')
2433 else if (a == 'L' && b == 'T')
2435 else if (a == 'L' && b == 'E')
2437 else if (a == 'G' && b == 'T')
2439 else if (a == 'G' && b == 'E')
2444 ERROR ((stderr, "Comparison operator must be one of EQ, NE, LT, LE, GT or GE.\n"));
2447 idx = sb_skip_white (idx + 2, in);
2464 idx = sb_skip_white (idx, in);
2466 if (in->ptr[idx] == '"')
2470 /* This is a string comparision */
2471 idx = getstring (idx, in, &acc_a);
2472 idx = whatcond (idx, in, &cond);
2473 idx = getstring (idx, in, &acc_b);
2474 same = acc_a.len == acc_b.len && (strncmp (acc_a.ptr, acc_b.ptr, acc_a.len) == 0);
2476 if (cond != EQ && cond != NE)
2478 ERROR ((stderr, "Comparison operator for strings must be EQ or NE\n"));
2482 res = (cond != EQ) ^ same;
2485 /* This is a numeric expression */
2490 idx = exp_get_abs ("Conditional operator must have absolute operands.\n", idx, in, &vala);
2491 idx = whatcond (idx, in, &cond);
2492 idx = sb_skip_white (idx, in);
2493 if (in->ptr[idx] == '"')
2495 WARNING ((stderr, "String compared against expression.\n"));
2500 idx = exp_get_abs ("Conditional operator must have absolute operands.\n", idx, in, &valb);
2543 if (ifi >= IFNESTING)
2545 FATAL ((stderr, "AIF nesting unreasonable.\n"));
2548 ifstack[ifi].on = ifstack[ifi-1].on ? istrue (idx, in) : 0;
2549 ifstack[ifi].hadelse = 0;
2557 ifstack[ifi].on = ifstack[ifi-1].on ? !ifstack[ifi].on : 0;
2558 if (ifstack[ifi].hadelse)
2560 ERROR ((stderr, "Multiple AELSEs in AIF.\n"));
2562 ifstack[ifi].hadelse = 1;
2576 ERROR ((stderr, "AENDI without AIF.\n"));
2583 return ifstack[ifi].on;
2586 /* MRI IFEQ, IFNE, IFLT, IFLE, IFGE, IFGT. */
2589 do_if (idx, in, cond)
2597 if (ifi >= IFNESTING)
2599 FATAL ((stderr, "IF nesting unreasonable.\n"));
2602 idx = exp_get_abs ("Conditional operator must have absolute operands.\n",
2607 case EQ: res = val == 0; break;
2608 case NE: res = val != 0; break;
2609 case LT: res = val < 0; break;
2610 case LE: res = val <= 0; break;
2611 case GE: res = val >= 0; break;
2612 case GT: res = val > 0; break;
2616 ifstack[ifi].on = ifstack[ifi-1].on ? res: 0;
2617 ifstack[ifi].hadelse = 0;
2620 /* Get a string for the MRI IFC or IFNC pseudo-ops. */
2623 get_mri_string (idx, in, val, terminator)
2629 idx = sb_skip_white (idx, in);
2632 && in->ptr[idx] == '\'')
2634 sb_add_char (val, '\'');
2635 for (++idx; idx < in->len; ++idx)
2637 sb_add_char (val, in->ptr[idx]);
2638 if (in->ptr[idx] == '\'')
2642 || in->ptr[idx] != '\'')
2646 idx = sb_skip_white (idx, in);
2652 while (idx < in->len
2653 && in->ptr[idx] != terminator)
2655 sb_add_char (val, in->ptr[idx]);
2659 while (i >= 0 && ISWHITE (val->ptr[i]))
2667 /* MRI IFC, IFNC. */
2670 do_ifc (idx, in, ifnc)
2679 if (ifi >= IFNESTING)
2681 FATAL ((stderr, "IF nesting unreasonable.\n"));
2687 idx = get_mri_string (idx, in, &first, ',');
2689 if (idx >= in->len || in->ptr[idx] != ',')
2691 ERROR ((stderr, "Bad format for IF or IFNC.\n"));
2695 idx = get_mri_string (idx + 1, in, &second, ';');
2697 res = (first.len == second.len
2698 && strncmp (first.ptr, second.ptr, first.len) == 0);
2702 ifstack[ifi].on = ifstack[ifi-1].on ? res : 0;
2703 ifstack[ifi].hadelse = 0;
2706 /* Read input lines till we get to a TO string.
2707 Increase nesting depth if we geta FROM string.
2708 Put the results into sb at PTR. */
2711 buffer_and_nest (from, to, ptr)
2716 int from_len = strlen (from);
2717 int to_len = strlen (to);
2719 int line_start = ptr->len;
2720 int line = linecount ();
2722 int more = get_line (ptr);
2726 /* Try and find the first pseudo op on the line */
2729 if (!alternate && !mri)
2731 /* With normal syntax we can suck what we want till we get
2732 to the dot. With the alternate, labels have to start in
2733 the first column, since we cant tell what's a label and
2736 /* Skip leading whitespace */
2738 && ISWHITE (ptr->ptr[i]))
2741 /* Skip over a label */
2743 && ISNEXTCHAR (ptr->ptr[i]))
2748 && ptr->ptr[i] == ':')
2752 /* Skip trailing whitespace */
2754 && ISWHITE (ptr->ptr[i]))
2757 if (i < ptr->len && (ptr->ptr[i] == '.'
2761 if (ptr->ptr[i] == '.')
2763 if (strncasecmp (ptr->ptr + i, from, from_len) == 0)
2765 if (strncasecmp (ptr->ptr + i, to, to_len) == 0)
2770 /* Reset the string to not include the ending rune */
2771 ptr->len = line_start;
2777 /* Add a CR to the end and keep running */
2778 sb_add_char (ptr, '\n');
2779 line_start = ptr->len;
2780 more = get_line (ptr);
2785 FATAL ((stderr, "End of file whilst inside %s, started on line %d.\n", from, line));
2794 ERROR ((stderr, "AENDR without a AREPEAT.\n"));
2796 ERROR ((stderr, "ENDR without a REPT.\n"));
2815 process_assigns (idx, in, &exp);
2816 doit = istrue (0, &exp);
2818 buffer_and_nest ("AWHILE", "AENDW", &sub);
2833 int index = include_next_index ();
2837 sb_add_sb (©, &sub);
2838 sb_add_sb (©, in);
2839 sb_add_string (©, "\n");
2840 sb_add_sb (©, &sub);
2841 sb_add_string (©, "\t.AENDW\n");
2842 /* Push another WHILE */
2843 include_buf (&exp, ©, include_while, index);
2856 ERROR ((stderr, "AENDW without a AENDW.\n"));
2862 Pop things off the include stack until the type and index changes */
2867 include_type type = sp->type;
2868 if (type == include_repeat
2869 || type == include_while
2870 || type == include_macro)
2872 int index = sp->index;
2874 while (sp->index == index
2875 && sp->type == type)
2885 do_arepeat (idx, in)
2889 sb exp; /* buffer with expression in it */
2890 sb copy; /* expanded repeat block */
2891 sb sub; /* contents of AREPEAT */
2897 process_assigns (idx, in, &exp);
2898 idx = exp_get_abs ("AREPEAT must have absolute operand.\n", 0, &exp, &rc);
2900 buffer_and_nest ("AREPEAT", "AENDR", &sub);
2902 buffer_and_nest ("REPT", "ENDR", &sub);
2905 /* Push back the text following the repeat, and another repeat block
2916 int index = include_next_index ();
2917 sb_add_sb (©, &sub);
2921 sprintf (buffer, "\t.AREPEAT %d\n", rc - 1);
2923 sprintf (buffer, "\tREPT %d\n", rc - 1);
2924 sb_add_string (©, buffer);
2925 sb_add_sb (©, &sub);
2927 sb_add_string (©, " .AENDR\n");
2929 sb_add_string (©, " ENDR\n");
2932 include_buf (&exp, ©, include_repeat, index);
2944 ERROR ((stderr, ".ENDM without a matching .MACRO.\n"));
2947 /* MRI IRP pseudo-op. */
2950 do_irp (idx, in, irpc)
2968 idx = sb_skip_white (idx, in);
2971 buffer_and_nest (mn, "ENDR", &sub);
2977 idx = get_token (idx, in, &f.name);
2978 if (f.name.len == 0)
2980 ERROR ((stderr, "Missing model parameter in %s", mn));
2984 hash_new_table (1, &h);
2985 p = hash_create (&h, &f.name);
2986 p->type = hash_formal;
2993 sb_add_string (&name, mn);
2997 idx = sb_skip_comma (idx, in);
3000 /* Expand once with a null string. */
3001 macro_expand_body (&name, &sub, &out, &f, &h);
3002 fprintf (outfile, "%s", sb_name (&out));
3006 while (!eol (idx, in))
3009 idx = get_any_string (idx, in, &f.actual, 1, 0);
3012 sb_reset (&f.actual);
3013 sb_add_char (&f.actual, in->ptr[idx]);
3017 macro_expand_body (&name, &sub, &out, &f, &h);
3018 fprintf (outfile, "%s", sb_name (&out));
3020 idx = sb_skip_comma (idx, in);
3022 idx = sb_skip_white (idx, in);
3031 /* MACRO PROCESSING */
3034 hash_table macro_table;
3044 do_formals (macro, idx, in)
3049 formal_entry **p = ¯o->formals;
3050 macro->formal_count = 0;
3051 hash_new_table (5, ¯o->formal_hash);
3052 while (idx < in->len)
3054 formal_entry *formal;
3056 formal = (formal_entry *) xmalloc (sizeof (formal_entry));
3058 sb_new (&formal->name);
3059 sb_new (&formal->def);
3060 sb_new (&formal->actual);
3062 idx = sb_skip_white (idx, in);
3063 idx = get_token (idx, in, &formal->name);
3064 if (formal->name.len == 0)
3066 idx = sb_skip_white (idx, in);
3067 if (formal->name.len)
3069 /* This is a formal */
3070 if (idx < in->len && in->ptr[idx] == '=')
3073 idx = get_any_string (idx + 1, in, &formal->def, 1, 0);
3078 /* Add to macro's hash table */
3080 hash_entry *p = hash_create (¯o->formal_hash, &formal->name);
3081 p->type = hash_formal;
3082 p->value.f = formal;
3085 formal->index = macro->formal_count;
3086 idx = sb_skip_comma (idx, in);
3087 macro->formal_count++;
3095 formal_entry *formal;
3097 /* Add a special NARG formal, which macro_expand will set to the
3098 number of arguments. */
3099 formal = (formal_entry *) xmalloc (sizeof (formal_entry));
3101 sb_new (&formal->name);
3102 sb_new (&formal->def);
3103 sb_new (&formal->actual);
3105 sb_add_string (&formal->name, "NARG");
3108 /* Add to macro's hash table */
3110 hash_entry *p = hash_create (¯o->formal_hash, &formal->name);
3111 p->type = hash_formal;
3112 p->value.f = formal;
3117 formal->next = NULL;
3123 /* Parse off LOCAL n1, n2,... Invent a label name for it */
3126 do_local (idx, line)
3136 idx = sb_skip_white (idx, line);
3137 while (!eol(idx, line))
3142 sprintf(subs, "LL%04x", ln);
3143 idx = get_token(idx, line, &acc);
3144 sb_add_string (&sub, subs);
3145 hash_add_to_string_table (&assign_hash_table, &acc, &sub, 1);
3146 idx = sb_skip_comma (idx, line);
3161 macro = (macro_entry *) xmalloc (sizeof (macro_entry));
3162 sb_new (¯o->sub);
3165 macro->formal_count = 0;
3168 idx = sb_skip_white (idx, in);
3169 buffer_and_nest ("MACRO", "ENDM", ¯o->sub);
3173 sb_add_sb (&name, &label);
3174 if (in->ptr[idx] == '(')
3176 /* It's the label: MACRO (formals,...) sort */
3177 idx = do_formals (macro, idx + 1, in);
3178 if (in->ptr[idx] != ')')
3179 ERROR ((stderr, "Missing ) after formals.\n"));
3182 /* It's the label: MACRO formals,... sort */
3183 idx = do_formals (macro, idx, in);
3188 idx = get_token (idx, in, &name);
3189 idx = sb_skip_white (idx, in);
3190 idx = do_formals (macro, idx, in);
3193 /* and stick it in the macro hash table */
3194 hash_create (¯o_table, &name)->value.m = macro;
3199 get_token (idx, in, name)
3205 && ISFIRSTCHAR (in->ptr[idx]))
3207 sb_add_char (name, in->ptr[idx++]);
3208 while (idx < in->len
3209 && ISNEXTCHAR (in->ptr[idx]))
3211 sb_add_char (name, in->ptr[idx++]);
3214 /* Ignore trailing & */
3215 if (alternate && idx < in->len && in->ptr[idx] == '&')
3220 /* Scan a token, but stop if a ' is seen */
3222 get_apost_token (idx, in, name, kind)
3228 idx = get_token (idx, in, name);
3229 if (idx < in->len && in->ptr[idx] == kind)
3235 sub_actual (src, in, t, formal_hash, kind, out, copyifnotthere)
3239 hash_table *formal_hash;
3244 /* This is something to take care of */
3246 src = get_apost_token (src, in, t, kind);
3247 /* See if it's in the macro's hash table */
3248 ptr = hash_lookup (formal_hash, t);
3251 if (ptr->value.f->actual.len)
3253 sb_add_sb (out, &ptr->value.f->actual);
3257 sb_add_sb (out, &ptr->value.f->def);
3260 else if (copyifnotthere)
3266 sb_add_char (out, '\\');
3272 /* Copy the body from the macro buffer into a safe place and
3273 substitute any args. */
3276 macro_expand_body (name, in, out, formals, formal_hash)
3280 formal_entry *formals;
3281 hash_table *formal_hash;
3289 while (src < in->len)
3291 if (in->ptr[src] == '&')
3294 if (mri && src + 1 < in->len && in->ptr[src + 1] == '&')
3296 src = sub_actual (src + 2, in, &t, formal_hash, '\'', out, 1);
3300 src = sub_actual (src + 1, in, &t, formal_hash, '&', out, 0);
3303 else if (in->ptr[src] == '\\')
3306 if (in->ptr[src] == comment_char)
3308 /* This is a comment, just drop the rest of the line */
3309 while (src < in->len
3310 && in->ptr[src] != '\n')
3314 else if (in->ptr[src] == '(')
3316 /* Sub in till the next ')' literally */
3318 while (src < in->len && in->ptr[src] != ')')
3320 sb_add_char (out, in->ptr[src++]);
3322 if (in->ptr[src] == ')')
3325 ERROR ((stderr, "Missplaced ).\n"));
3327 else if (in->ptr[src] == '@')
3329 /* Sub in the macro invocation number */
3333 sprintf (buffer, "%05d", number);
3334 sb_add_string (out, buffer);
3336 else if (in->ptr[src] == '&')
3338 /* This is a preprocessor variable name, we don't do them
3340 sb_add_char (out, '\\');
3341 sb_add_char (out, '&');
3345 && isalnum ((unsigned char) in->ptr[src]))
3350 if (isdigit ((unsigned char) in->ptr[src]))
3351 ind = in->ptr[src] - '0';
3352 else if (isupper ((unsigned char) in->ptr[src]))
3353 ind = in->ptr[src] - 'A' + 10;
3355 ind = in->ptr[src] - 'a' + 10;
3357 for (f = formals; f != NULL; f = f->next)
3359 if (f->index == ind - 1)
3361 if (f->actual.len != 0)
3362 sb_add_sb (out, &f->actual);
3364 sb_add_sb (out, &f->def);
3372 src = sub_actual (src, in, &t, formal_hash, '\'', out, 0);
3375 else if (ISFIRSTCHAR (in->ptr[src]) && (alternate || mri))
3378 src = sub_actual (src, in, &t, formal_hash, '\'', out, 1);
3380 else if (ISCOMMENTCHAR (in->ptr[src])
3381 && src + 1 < in->len
3382 && ISCOMMENTCHAR (in->ptr[src+1])
3385 /* Two comment chars in a row cause the rest of the line to
3387 while (src < in->len && in->ptr[src] != '\n')
3390 else if (in->ptr[src] == '"'
3391 || (mri && in->ptr[src] == '\''))
3394 sb_add_char (out, in->ptr[src++]);
3397 && in->ptr[src] == '='
3398 && src + 1 < in->len
3399 && in->ptr[src + 1] == '=')
3404 src = get_token (src + 2, in, &t);
3405 ptr = hash_lookup (formal_hash, &t);
3408 ERROR ((stderr, "MACRO formal argument %s does not exist.\n",
3413 if (ptr->value.f->actual.len)
3415 sb_add_string (out, "-1");
3419 sb_add_char (out, '0');
3425 sb_add_char (out, in->ptr[src++]);
3433 macro_expand (name, idx, in, m)
3443 int is_positional = 0;
3450 /* Reset any old value the actuals may have */
3451 for (f = m->formals; f; f = f->next)
3452 sb_reset (&f->actual);
3457 /* The macro may be called with an optional qualifier, which may
3458 be referred to in the macro body as \0. */
3459 if (idx < in->len && in->ptr[idx] == '.')
3463 n = (formal_entry *) xmalloc (sizeof (formal_entry));
3466 sb_new (&n->actual);
3469 n->next = m->formals;
3472 idx = get_any_string (idx + 1, in, &n->actual, 1, 0);
3476 /* Peel off the actuals and store them away in the hash tables' actuals */
3477 while (!eol(idx, in))
3480 idx = sb_skip_white (idx, in);
3481 /* Look and see if it's a positional or keyword arg */
3483 while (scan < in->len
3484 && !ISSEP (in->ptr[scan])
3485 && (!alternate && in->ptr[scan] != '='))
3487 if (scan < in->len && (!alternate) && in->ptr[scan] == '=')
3492 ERROR ((stderr, "Can't mix positional and keyword arguments.\n"));
3495 /* This is a keyword arg, fetch the formal name and
3496 then the actual stuff */
3498 idx = get_token (idx, in, &t);
3499 if (in->ptr[idx] != '=')
3500 ERROR ((stderr, "confused about formal params.\n"));
3502 /* Lookup the formal in the macro's list */
3503 ptr = hash_lookup (&m->formal_hash, &t);
3506 ERROR ((stderr, "MACRO formal argument %s does not exist.\n", sb_name (&t)));
3511 /* Insert this value into the right place */
3512 sb_reset (&ptr->value.f->actual);
3513 idx = get_any_string (idx + 1, in, &ptr->value.f->actual, 0, 0);
3514 if (ptr->value.f->actual.len > 0)
3520 /* This is a positional arg */
3524 ERROR ((stderr, "Can't mix positional and keyword arguments.\n"));
3534 ERROR ((stderr, "Too many positional arguments.\n"));
3537 f = (formal_entry *) xmalloc (sizeof (formal_entry));
3540 sb_new (&f->actual);
3544 for (pf = &m->formals; *pf != NULL; pf = &(*pf)->next)
3545 if ((*pf)->index >= c)
3546 c = (*pf)->index + 1;
3551 sb_reset (&f->actual);
3552 idx = get_any_string (idx, in, &f->actual, 1, 0);
3553 if (f->actual.len > 0)
3559 while (f != NULL && f->index < 0);
3562 idx = sb_skip_comma (idx, in);
3570 sb_add_string (&t, "NARG");
3571 ptr = hash_lookup (&m->formal_hash, &t);
3572 sb_reset (&ptr->value.f->actual);
3573 sprintf (buffer, "%d", narg);
3574 sb_add_string (&ptr->value.f->actual, buffer);
3577 macro_expand_body (name, &m->sub, &out, m->formals, &m->formal_hash);
3579 include_buf (name, &out, include_macro, include_next_index ());
3585 /* Discard any unnamed formal arguments. */
3589 if ((*pf)->name.len != 0)
3593 sb_kill (&(*pf)->name);
3594 sb_kill (&(*pf)->def);
3595 sb_kill (&(*pf)->actual);
3614 /* The macro name must be the first thing on the line */
3620 idx = get_token (idx, in, &name);
3624 /* Got a name, look it up */
3626 ptr = hash_lookup (¯o_table, &name);
3630 /* It's in the table, copy out the stuff and convert any macro args */
3631 macro_expand (&name, idx, in, ptr->value.m);
3643 /* STRING HANDLING */
3646 getstring (idx, in, acc)
3651 idx = sb_skip_white (idx, in);
3653 while (idx < in->len
3654 && (in->ptr[idx] == '"'
3655 || in->ptr[idx] == '<'
3656 || (in->ptr[idx] == '\'' && alternate)))
3658 if (in->ptr[idx] == '<')
3660 if (alternate || mri)
3664 while ((in->ptr[idx] != '>' || nest)
3667 if (in->ptr[idx] == '!')
3670 sb_add_char (acc, in->ptr[idx++]);
3673 if (in->ptr[idx] == '>')
3675 if (in->ptr[idx] == '<')
3677 sb_add_char (acc, in->ptr[idx++]);
3685 idx = exp_get_abs ("Character code in string must be absolute expression.\n",
3687 sb_add_char (acc, code);
3689 if (in->ptr[idx] != '>')
3690 ERROR ((stderr, "Missing > for character code.\n"));
3694 else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
3696 char tchar = in->ptr[idx];
3698 while (idx < in->len)
3700 if (alternate && in->ptr[idx] == '!')
3703 sb_add_char (acc, in->ptr[idx++]);
3706 if (in->ptr[idx] == tchar)
3709 if (idx >= in->len || in->ptr[idx] != tchar)
3712 sb_add_char (acc, in->ptr[idx]);
3722 /* .SDATA[C|Z] <string> */
3726 do_sdata (idx, in, type)
3735 fprintf (outfile, ".byte\t");
3737 while (!eol (idx, in))
3741 idx = sb_skip_white (idx, in);
3742 while (!eol (idx, in))
3744 pidx = idx = get_any_string (idx, in, &acc, 0, 1);
3749 ERROR ((stderr, "string for SDATAC longer than 255 characters (%d).\n", acc.len));
3751 fprintf (outfile, "%d", acc.len);
3755 for (i = 0; i < acc.len; i++)
3759 fprintf (outfile, ",");
3761 fprintf (outfile, "%d", acc.ptr[i]);
3768 fprintf (outfile, ",");
3769 fprintf (outfile, "0");
3771 idx = sb_skip_comma (idx, in);
3772 if (idx == pidx) break;
3774 if (!alternate && in->ptr[idx] != ',' && idx != in->len)
3776 fprintf (outfile, "\n");
3777 ERROR ((stderr, "illegal character in SDATA line (0x%x).\n", in->ptr[idx]));
3783 fprintf (outfile, "\n");
3786 /* .SDATAB <count> <string> */
3798 idx = exp_get_abs ("Must have absolute SDATAB repeat count.\n", idx, in, &repeat);
3801 ERROR ((stderr, "Must have positive SDATAB repeat count (%d).\n", repeat));
3805 idx = sb_skip_comma (idx, in);
3806 idx = getstring (idx, in, &acc);
3808 for (i = 0; i < repeat; i++)
3811 fprintf (outfile, "\t");
3812 fprintf (outfile, ".byte\t");
3814 fprintf (outfile, "\n");
3824 FILE *newone = fopen (name, "r");
3828 if (isp == MAX_INCLUDES)
3829 FATAL ((stderr, "Unreasonable include depth (%ld).\n", (long) isp));
3832 sp->handle = newone;
3835 sb_add_string (&sp->name, name);
3838 sp->pushback_index = 0;
3839 sp->type = include_file;
3841 sb_new (&sp->pushback);
3846 do_include (idx, in)
3852 include_path *includes;
3858 idx = getstring (idx, in, &t);
3861 idx = sb_skip_white (idx, in);
3862 while (idx < in->len && ! ISWHITE (in->ptr[idx]))
3864 sb_add_char (&t, in->ptr[idx]);
3869 for (includes = paths_head; includes; includes = includes->next)
3872 sb_add_sb (&cat, &includes->path);
3873 sb_add_char (&cat, '/');
3874 sb_add_sb (&cat, &t);
3875 if (new_file (sb_name (&cat)))
3882 if (! new_file (sb_name (&t)))
3883 FATAL ((stderr, "Can't open include file `%s'.\n", sb_name (&t)));
3892 if (sp != include_stack)
3895 fclose (sp->handle);
3900 /* Get the next character from the include stack. If there's anything
3901 in the pushback buffer, take that first. If we're at eof, pop from
3902 the stack and try again. Keep the linecount up to date. */
3909 if (sp->pushback.len != sp->pushback_index)
3911 r = (char) (sp->pushback.ptr[sp->pushback_index++]);
3912 /* When they've all gone, reset the pointer */
3913 if (sp->pushback_index == sp->pushback.len)
3915 sp->pushback.len = 0;
3916 sp->pushback_index = 0;
3919 else if (sp->handle)
3921 r = getc (sp->handle);
3926 if (r == EOF && isp)
3930 while (r == EOF && isp)
3948 return sp->linecount;
3952 include_next_index ()
3956 && index > MAX_REASONABLE)
3957 FATAL ((stderr, "Unreasonable expansion (-u turns off check).\n"));
3962 /* Initialize the chartype vector. */
3968 for (x = 0; x < 256; x++)
3970 if (isalpha (x) || x == '_' || x == '$')
3971 chartype[x] |= FIRSTBIT;
3973 if (mri && x == '.')
3974 chartype[x] |= FIRSTBIT;
3976 if (isdigit (x) || isalpha (x) || x == '_' || x == '$')
3977 chartype[x] |= NEXTBIT;
3979 if (x == ' ' || x == '\t' || x == ',' || x == '"' || x == ';'
3980 || x == '"' || x == '<' || x == '>' || x == ')' || x == '(')
3981 chartype[x] |= SEPBIT;
3983 if (x == 'b' || x == 'B'
3984 || x == 'q' || x == 'Q'
3985 || x == 'h' || x == 'H'
3986 || x == 'd' || x == 'D')
3987 chartype [x] |= BASEBIT;
3989 if (x == ' ' || x == '\t')
3990 chartype[x] |= WHITEBIT;
3992 if (x == comment_char)
3993 chartype[x] |= COMMENTBIT;
3999 /* What to do with all the keywords */
4000 #define PROCESS 0x1000 /* Run substitution over the line */
4001 #define LAB 0x2000 /* Spit out the label */
4003 #define K_EQU (PROCESS|1)
4004 #define K_ASSIGN (PROCESS|2)
4005 #define K_REG (PROCESS|3)
4006 #define K_ORG (PROCESS|4)
4007 #define K_RADIX (PROCESS|5)
4008 #define K_DATA (LAB|PROCESS|6)
4009 #define K_DATAB (LAB|PROCESS|7)
4010 #define K_SDATA (LAB|PROCESS|8)
4011 #define K_SDATAB (LAB|PROCESS|9)
4012 #define K_SDATAC (LAB|PROCESS|10)
4013 #define K_SDATAZ (LAB|PROCESS|11)
4014 #define K_RES (LAB|PROCESS|12)
4015 #define K_SRES (LAB|PROCESS|13)
4016 #define K_SRESC (LAB|PROCESS|14)
4017 #define K_SRESZ (LAB|PROCESS|15)
4018 #define K_EXPORT (LAB|PROCESS|16)
4019 #define K_GLOBAL (LAB|PROCESS|17)
4020 #define K_PRINT (LAB|PROCESS|19)
4021 #define K_FORM (LAB|PROCESS|20)
4022 #define K_HEADING (LAB|PROCESS|21)
4023 #define K_PAGE (LAB|PROCESS|22)
4024 #define K_IMPORT (LAB|PROCESS|23)
4025 #define K_PROGRAM (LAB|PROCESS|24)
4026 #define K_END (PROCESS|25)
4027 #define K_INCLUDE (PROCESS|26)
4028 #define K_IGNORED (PROCESS|27)
4029 #define K_ASSIGNA (PROCESS|28)
4030 #define K_ASSIGNC (29)
4031 #define K_AIF (PROCESS|30)
4032 #define K_AELSE (PROCESS|31)
4033 #define K_AENDI (PROCESS|32)
4034 #define K_AREPEAT (PROCESS|33)
4035 #define K_AENDR (PROCESS|34)
4036 #define K_AWHILE (35)
4037 #define K_AENDW (PROCESS|36)
4038 #define K_EXITM (37)
4039 #define K_MACRO (PROCESS|38)
4041 #define K_ALIGN (PROCESS|LAB|40)
4042 #define K_ALTERNATE (41)
4043 #define K_DB (LAB|PROCESS|42)
4044 #define K_DW (LAB|PROCESS|43)
4045 #define K_DL (LAB|PROCESS|44)
4046 #define K_LOCAL (45)
4047 #define K_IFEQ (PROCESS|46)
4048 #define K_IFNE (PROCESS|47)
4049 #define K_IFLT (PROCESS|48)
4050 #define K_IFLE (PROCESS|49)
4051 #define K_IFGE (PROCESS|50)
4052 #define K_IFGT (PROCESS|51)
4053 #define K_IFC (PROCESS|52)
4054 #define K_IFNC (PROCESS|53)
4055 #define K_IRP (PROCESS|54)
4056 #define K_IRPC (PROCESS|55)
4066 static struct keyword kinfo[] =
4068 { "EQU", K_EQU, 0 },
4069 { "ALTERNATE", K_ALTERNATE, 0 },
4070 { "ASSIGN", K_ASSIGN, 0 },
4071 { "REG", K_REG, 0 },
4072 { "ORG", K_ORG, 0 },
4073 { "RADIX", K_RADIX, 0 },
4074 { "DATA", K_DATA, 0 },
4078 { "DATAB", K_DATAB, 0 },
4079 { "SDATA", K_SDATA, 0 },
4080 { "SDATAB", K_SDATAB, 0 },
4081 { "SDATAZ", K_SDATAZ, 0 },
4082 { "SDATAC", K_SDATAC, 0 },
4083 { "RES", K_RES, 0 },
4084 { "SRES", K_SRES, 0 },
4085 { "SRESC", K_SRESC, 0 },
4086 { "SRESZ", K_SRESZ, 0 },
4087 { "EXPORT", K_EXPORT, 0 },
4088 { "GLOBAL", K_GLOBAL, 0 },
4089 { "PRINT", K_PRINT, 0 },
4090 { "FORM", K_FORM, 0 },
4091 { "HEADING", K_HEADING, 0 },
4092 { "PAGE", K_PAGE, 0 },
4093 { "PROGRAM", K_IGNORED, 0 },
4094 { "END", K_END, 0 },
4095 { "INCLUDE", K_INCLUDE, 0 },
4096 { "ASSIGNA", K_ASSIGNA, 0 },
4097 { "ASSIGNC", K_ASSIGNC, 0 },
4098 { "AIF", K_AIF, 0 },
4099 { "AELSE", K_AELSE, 0 },
4100 { "AENDI", K_AENDI, 0 },
4101 { "AREPEAT", K_AREPEAT, 0 },
4102 { "AENDR", K_AENDR, 0 },
4103 { "EXITM", K_EXITM, 0 },
4104 { "MACRO", K_MACRO, 0 },
4105 { "ENDM", K_ENDM, 0 },
4106 { "AWHILE", K_AWHILE, 0 },
4107 { "ALIGN", K_ALIGN, 0 },
4108 { "AENDW", K_AENDW, 0 },
4109 { "ALTERNATE", K_ALTERNATE, 0 },
4110 { "LOCAL", K_LOCAL, 0 },
4114 /* Although the conditional operators are handled by gas, we need to
4115 handle them here as well, in case they are used in a recursive
4116 macro to end the recursion. */
4118 static struct keyword mrikinfo[] =
4120 { "IFEQ", K_IFEQ, 0 },
4121 { "IFNE", K_IFNE, 0 },
4122 { "IFLT", K_IFLT, 0 },
4123 { "IFLE", K_IFLE, 0 },
4124 { "IFGE", K_IFGE, 0 },
4125 { "IFGT", K_IFGT, 0 },
4126 { "IFC", K_IFC, 0 },
4127 { "IFNC", K_IFNC, 0 },
4128 { "ELSEC", K_AELSE, 0 },
4129 { "ENDC", K_AENDI, 0 },
4130 { "MEXIT", K_EXITM, 0 },
4131 { "REPT", K_AREPEAT, 0 },
4132 { "IRP", K_IRP, 0 },
4133 { "IRPC", K_IRPC, 0 },
4134 { "ENDR", K_AENDR, 0 },
4138 /* Look for a pseudo op on the line. If one's there then call
4142 process_pseudo_op (idx, line, acc)
4149 if (line->ptr[idx] == '.' || alternate || mri)
4151 /* Scan forward and find pseudo name */
4157 if (line->ptr[idx] == '.')
4159 in = line->ptr + idx;
4164 while (idx < line->len && *e && ISFIRSTCHAR (*e))
4166 sb_add_char (acc, *e);
4171 ptr = hash_lookup (&keyword_hash_table, acc);
4176 /* This one causes lots of pain when trying to preprocess
4178 WARNING ((stderr, "Unrecognised pseudo op `%s'.\n", sb_name (acc)));
4182 if (ptr->value.i & LAB)
4183 { /* output the label */
4186 fprintf (outfile, "%s:\t", sb_name (&label));
4189 fprintf (outfile, "\t");
4192 if (mri && ptr->value.i == K_END)
4197 sb_add_buffer (&t, line->ptr + oidx, idx - oidx);
4198 fprintf (outfile, "\t%s", sb_name (&t));
4202 if (ptr->value.i & PROCESS)
4204 /* Polish the rest of the line before handling the pseudo op */
4206 strip_comments(line);
4209 process_assigns (idx, line, acc);
4211 change_base (0, acc, line);
4216 switch (ptr->value.i)
4232 switch (ptr->value.i)
4244 ERROR ((stderr, "ORG command not allowed.\n"));
4250 do_data (idx, line, 1);
4253 do_data (idx, line, 2);
4256 do_data (idx, line, 4);
4259 do_data (idx, line, 0);
4262 do_datab (idx, line);
4265 do_sdata (idx, line, 0);
4268 do_sdatab (idx, line);
4271 do_sdata (idx, line, 'c');
4274 do_sdata (idx, line, 'z');
4277 do_assign (1, 0, line);
4283 do_arepeat (idx, line);
4289 do_awhile (idx, line);
4295 do_assign (0, idx, line);
4298 do_align (idx, line);
4301 do_res (idx, line, 0);
4304 do_res (idx, line, 's');
4307 do_include (idx, line);
4310 do_local (idx, line);
4313 do_macro (idx, line);
4319 do_res (idx, line, 'c');
4322 do_print (idx, line);
4325 do_form (idx, line);
4328 do_heading (idx, line);
4340 do_res (idx, line, 'z');
4348 do_assigna (idx, line);
4351 do_assignc (idx, line);
4360 do_if (idx, line, EQ);
4363 do_if (idx, line, NE);
4366 do_if (idx, line, LT);
4369 do_if (idx, line, LE);
4372 do_if (idx, line, GE);
4375 do_if (idx, line, GT);
4378 do_ifc (idx, line, 0);
4381 do_ifc (idx, line, 1);
4384 do_irp (idx, line, 0);
4387 do_irp (idx, line, 1);
4397 /* Add a keyword to the hash table. */
4400 add_keyword (name, code)
4408 sb_add_string (&label, name);
4410 hash_add_to_int_table (&keyword_hash_table, &label, code);
4413 for (j = 0; name[j]; j++)
4414 sb_add_char (&label, name[j] - 'A' + 'a');
4415 hash_add_to_int_table (&keyword_hash_table, &label, code);
4420 /* Build the keyword hash table - put each keyword in the table twice,
4421 once upper and once lower case.*/
4428 for (i = 0; kinfo[i].name; i++)
4429 add_keyword (kinfo[i].name, kinfo[i].code);
4433 for (i = 0; mrikinfo[i].name; i++)
4434 add_keyword (mrikinfo[i].name, mrikinfo[i].code);
4458 sb_add_char (&value, *string);
4461 exp_get_abs ("Invalid expression on command line.\n", 0, &value, &res);
4465 sb_add_char (&label, *string);
4470 ptr = hash_create (&vars, &label);
4471 free_old_entry (ptr);
4472 ptr->type = hash_integer;
4478 /* The list of long options. */
4479 static struct option long_options[] =
4481 { "alternate", no_argument, 0, 'a' },
4482 { "include", required_argument, 0, 'I' },
4483 { "commentchar", required_argument, 0, 'c' },
4484 { "copysource", no_argument, 0, 's' },
4485 { "debug", no_argument, 0, 'd' },
4486 { "help", no_argument, 0, 'h' },
4487 { "mri", no_argument, 0, 'M' },
4488 { "output", required_argument, 0, 'o' },
4489 { "print", no_argument, 0, 'p' },
4490 { "unreasonable", no_argument, 0, 'u' },
4491 { "version", no_argument, 0, 'v' },
4492 { "define", required_argument, 0, 'd' },
4493 { NULL, no_argument, 0, 0 }
4496 /* Show a usage message and exit. */
4498 show_usage (file, status)
4504 [-a] [--alternate] enter alternate macro mode\n\
4505 [-c char] [--commentchar char] change the comment character from !\n\
4506 [-d] [--debug] print some debugging info\n\
4507 [-h] [--help] print this message\n\
4508 [-M] [--mri] enter MRI compatibility mode\n\
4509 [-o out] [--output out] set the output file\n\
4510 [-p] [--print] print line numbers\n\
4511 [-s] [--copysource] copy source through as comments \n\
4512 [-u] [--unreasonable] allow unreasonable nesting\n\
4513 [-v] [--version] print the program version\n\
4514 [-Dname=value] create preprocessor variable called name, with value\n\
4515 [-Ipath] add to include path list\n\
4516 [in-file]\n", program_name);
4520 /* Display a help message and exit. */
4524 printf ("%s: Gnu Assembler Macro Preprocessor\n",
4526 show_usage (stdout, 0);
4543 program_name = argv[0];
4544 xmalloc_set_program_name (program_name);
4546 hash_new_table (101, ¯o_table);
4547 hash_new_table (101, &keyword_hash_table);
4548 hash_new_table (101, &assign_hash_table);
4549 hash_new_table (101, &vars);
4553 while ((opt = getopt_long (argc, argv, "I:sdhavc:upo:D:M", long_options,
4567 include_path *p = (include_path *) xmalloc (sizeof (include_path));
4569 sb_add_string (&p->path, optarg);
4571 paths_tail->next = p;
4578 print_line_number = 1;
4581 comment_char = optarg[0];
4603 printf ("GNU %s version %s\n", program_name, program_version);
4609 show_usage (stderr, 1);
4617 outfile = fopen (out_name, "w");
4620 fprintf (stderr, "%s: Can't open output file `%s'.\n",
4621 program_name, out_name);
4633 /* Process all the input files */
4635 while (optind < argc)
4637 if (new_file (argv[optind]))
4643 fprintf (stderr, "%s: Can't open input file `%s'.\n",
4644 program_name, argv[optind]);