1 /* gasp.c - Gnu assembler preprocessor main program.
2 Copyright (C) 1994 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
21 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 This program translates the input macros and stuff into a form
26 suitable for gas to consume.
29 gasp [-sdhau] [-c char] [-o <outfile>] <infile>*
31 -s copy source to output
32 -c <char> comments are started with <char> instead of !
33 -u allow unreasonable stuff
35 -d print debugging stats
36 -s semi colons start comments
37 -a use alternate syntax
38 Pseudo ops can start with or without a .
39 Labels have to be in first column.
40 Macro arg parameters subsituted by name, don't need the &.
41 String can start with ' too.
42 Strings can be surrounded by <..>
43 A %<exp> in a string evaluates the expression
44 Literal char in a string with !
60 #ifdef NEED_MALLOC_DECLARATION
61 extern char *malloc ();
64 #include "libiberty.h"
66 char *program_version = "1.2";
68 #define MAX_INCLUDES 30 /* Maximum include depth */
69 #define MAX_REASONABLE 1000 /* Maximum number of expansions */
71 int unreasonable; /* -u on command line */
72 int stats; /* -d on command line */
73 int print_line_number; /* -p flag on command line */
74 int copysource; /* -c flag on command line */
75 int warnings; /* Number of WARNINGs generated so far. */
76 int errors; /* Number of ERRORs generated so far. */
77 int fatals; /* Number of fatal ERRORs generated so far (either 0 or 1). */
78 int alternate = 0; /* -a on command line */
79 char comment_char = '!';
80 int radix = 10; /* Default radix */
82 int had_end; /* Seen .END */
84 /* The output stream */
88 /* Forward declarations. */
89 static int condass_lookup_name();
90 static int condass_on();
92 static int get_and_process();
93 static int get_token();
94 static int getstring();
95 static int include_next_index();
96 static int macro_op();
97 static int linecount();
98 static int process_pseudo_op();
99 static void include_pop();
100 static void include_print_where_line();
103 I had a couple of choices when deciding upon this data structure.
104 gas uses null terminated strings for all its internal work. This
105 often means that parts of the program that want to examine
106 substrings have to manipulate the data in the string to do the
107 right thing (a common operation is to single out a bit of text by
108 saving away the character after it, nulling it out, operating on
109 the substring and then replacing the character which was under the
110 null). This is a pain and I remember a load of problems that I had with
111 code in gas which almost got this right. Also, it's harder to grow and
112 allocate null terminated strings efficiently.
114 Obstacks provide all the functionality needed, but are too
115 complicated, hence the sb.
117 An sb is allocated by the caller, and is initialzed to point to an
118 sb_element. sb_elements are kept on a free lists, and used when
119 needed, replaced onto the free list when unused.
122 #define max_power_two 30 /* don't allow strings more than
123 2^max_power_two long */
124 /* structure of an sb */
127 char *ptr; /* points to the current block. */
128 int len; /* how much is used. */
129 int pot; /* the maximum length is 1<<pot */
134 /* Structure of the free list object of an sb */
146 sb_element *size[max_power_two];
149 sb_list_vector free_list;
151 int string_count[max_power_two];
153 /* the attributes of each character are stored as a bit pattern
154 chartype, which gives us quick tests. */
161 #define COMMENTBIT 16
163 #define ISCOMMENTCHAR(x) (chartype[(unsigned)(x)] & COMMENTBIT)
164 #define ISFIRSTCHAR(x) (chartype[(unsigned)(x)] & FIRSTBIT)
165 #define ISNEXTCHAR(x) (chartype[(unsigned)(x)] & NEXTBIT)
166 #define ISSEP(x) (chartype[(unsigned)(x)] & SEPBIT)
167 #define ISWHITE(x) (chartype[(unsigned)(x)] & WHITEBIT)
168 #define ISBASE(x) (chartype[(unsigned)(x)] & BASEBIT)
169 static char chartype[256];
172 /* Conditional assembly uses the `ifstack'. Each aif pushes another
173 entry onto the stack, and sets the on flag if it should. The aelse
174 sets hadelse, and toggles on. An aend pops a level. We limit to
175 100 levels of nesting, not because we're facists pigs with read
176 only minds, but because more than 100 levels of nesting is probably
177 a bug in the user's macro structure. */
179 #define IFNESTING 100
182 int on; /* is the level being output */
183 int hadelse; /* has an aelse been seen */
188 /* The final and intermediate results of expression evaluation are kept in
189 exp_t's. Note that a symbol is not an sb, but a pointer into the input
190 line. It must be coped somewhere safe before the next line is read in. */
201 int value; /* constant part */
202 symbol add_symbol; /* name part */
203 symbol sub_symbol; /* name part */
208 /* Hashing is done in a pretty standard way. A hash_table has a
209 pointer to a vector of pointers to hash_entrys, and the size of the
210 vector. A hash_entry contains a union of all the info we like to
211 store in hash table. If there is a hash collision, hash_entries
212 with the same hash are kept in a chain. */
214 /* What the data in a hash_entry means */
217 hash_integer, /* name->integer mapping */
218 hash_string, /* name->string mapping */
219 hash_macro, /* name is a macro */
220 hash_formal /* name is a formal argument */
225 sb key; /* symbol name */
226 hash_type type; /* symbol meaning */
231 struct macro_struct *m;
232 struct formal_struct *f;
234 struct hs *next; /* next hash_entry with same hash key */
244 /* Structures used to store macros.
246 Each macro knows its name and included text. It gets built with a
247 list of formal arguments, and also keeps a hash table which points
248 into the list to speed up formal search. Each formal knows its
249 name and its default value. Each time the macro is expanded, the
250 formals get the actual values attatched to them. */
252 /* describe the formal arguments to a macro */
254 typedef struct formal_struct
256 struct formal_struct *next; /* next formal in list */
257 sb name; /* name of the formal */
258 sb def; /* the default value */
259 sb actual; /* the actual argument (changed on each expansion) */
260 int index; /* the index of the formal 0..formal_count-1 */
264 /* describe the macro. */
266 typedef struct macro_struct
268 sb sub; /* substitution text. */
269 int formal_count; /* number of formal args. */
270 formal_entry *formals; /* pointer to list of formal_structs */
271 hash_table formal_hash; /* hash table of formals. */
275 /* how we nest files and expand macros etc.
277 we keep a stack of of include_stack structs. each include file
278 pushes a new level onto the stack. we keep an sb with a pushback
279 too. unget chars are pushed onto the pushback sb, getchars first
280 checks the pushback sb before reading from the input stream.
282 small things are expanded by adding the text of the item onto the
283 pushback sb. larger items are grown by pushing a new level and
284 allocating the entire pushback buf for the item. each time
285 something like a macro is expanded, the stack index is changed. we
286 can then perform an exitm by popping all entries off the stack with
287 the same stack index. if we're being reasonable, we can detect
288 recusive expansion by checking the index is reasonably small.
293 include_file, include_repeat, include_while, include_macro
298 sb pushback; /* current pushback stream */
299 int pushback_index; /* next char to read from stream */
300 FILE *handle; /* open file */
301 sb name; /* name of file */
302 int linecount; /* number of lines read so far */
304 int index; /* index of this layer */
306 include_stack[MAX_INCLUDES];
308 struct include_stack *sp;
309 #define isp (sp - include_stack)
314 void include_print_where_line ();
318 do { include_print_where_line (stderr); fprintf x ; fatals++; quit(); } while(0)
320 do { include_print_where_line (stderr); fprintf x; errors++; } while(0)
322 do { include_print_where_line (stderr); fprintf x; warnings++;} while(0)
326 /* exit the program and return the right ERROR code. */
339 for (i = 0; i < max_power_two; i++)
341 fprintf (stderr, "strings size %8d : %d\n", 1<<i, string_count[i]);
348 /* this program is about manipulating strings.
349 they are managed in things called `sb's which is an abbreviation
350 for string buffers. an sb has to be created, things can be glued
351 on to it, and at the end of it's life it should be freed. the
352 contents should never be pointed at whilst it is still growing,
353 since it could be moved at any time
357 sb_grow... (&foo,...);
363 /* initializes an sb. */
370 /* see if we can find one to allocate */
373 if (size > max_power_two)
375 FATAL ((stderr, "string longer than %d bytes requested.\n",
376 1 << max_power_two));
378 e = free_list.size[size];
381 /* nothing there, allocate one and stick into the free list */
382 e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size));
383 e->next = free_list.size[size];
385 free_list.size[size] = e;
386 string_count[size]++;
389 /* remove from free list */
391 free_list.size[size] = e->next;
393 /* copy into callers world */
405 sb_build (ptr, dsize);
408 /* deallocate the sb at ptr */
415 /* return item to free list */
416 ptr->item->next = free_list.size[ptr->pot];
417 free_list.size[ptr->pot] = ptr->item;
420 /* add the sb at s to the end of the sb at ptr */
422 static void sb_check ();
430 sb_check (ptr, s->len);
431 memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
435 /* make sure that the sb at ptr has room for another len characters,
436 and grow it if it doesn't. */
443 if (ptr->len + len >= 1 << ptr->pot)
447 while (ptr->len + len >= 1 << pot)
449 sb_build (&tmp, pot);
450 sb_add_sb (&tmp, ptr);
456 /* make the sb at ptr point back to the beginning. */
465 /* add character c to the end of the sb at ptr. */
473 ptr->ptr[ptr->len++] = c;
476 /* add null terminated string s to the end of sb at ptr. */
479 sb_add_string (ptr, s)
483 int len = strlen (s);
485 memcpy (ptr->ptr + ptr->len, s, len);
489 /* add string at s of length len to sb at ptr */
492 sb_add_buffer (ptr, s, len)
498 memcpy (ptr->ptr + ptr->len, s, len);
503 /* print the sb at ptr to the output file */
513 for (i = 0; i < ptr->len; i++)
517 fprintf (outfile, ",");
519 fprintf (outfile, "%d", ptr->ptr[i]);
526 sb_print_at (idx, ptr)
531 for (i = idx; i < ptr->len; i++)
532 putc (ptr->ptr[i], outfile);
534 /* put a null at the end of the sb at in and return the start of the
535 string, so that it can be used as an arg to printf %s. */
542 /* stick a null on the end of the string */
547 /* start at the index idx into the string in sb at ptr and skip
548 whitespace. return the index of the first non whitespace character */
551 sb_skip_white (idx, ptr)
555 while (idx < ptr->len && ISWHITE (ptr->ptr[idx]))
560 /* start at the index idx into the sb at ptr. skips whitespace,
561 a comma and any following whitespace. returnes the index of the
565 sb_skip_comma (idx, ptr)
569 while (idx < ptr->len && ISWHITE (ptr->ptr[idx]))
573 && ptr->ptr[idx] == ',')
576 while (idx < ptr->len && ISWHITE (ptr->ptr[idx]))
583 /* hash table maintenance. */
585 /* build a new hash table with size buckets, and fill in the info at ptr. */
588 hash_new_table (size, ptr)
594 ptr->table = (hash_entry **) xmalloc (size * (sizeof (hash_entry *)));
595 /* Fill with null-pointer, not zero-bit-pattern. */
596 for (i = 0; i < size; i++)
600 /* calculate and return the hash value of the sb at key. */
609 for (i = 0; i < key->len; i++)
617 /* lookup key in hash_table tab, if present, then return it, otherwise
618 build a new one and fill it with hash_integer. */
622 hash_create (tab, key)
626 int k = hash (key) % tab->size;
628 hash_entry **table = tab->table;
636 hash_entry *n = (hash_entry *) xmalloc (sizeof (hash_entry));
639 sb_add_sb (&n->key, key);
641 n->type = hash_integer;
644 if (strncmp (table[k]->key.ptr, key->ptr, key->len) == 0)
652 /* add sb name with key into hash_table tab. if replacing old value
653 and again, then ERROR. */
657 hash_add_to_string_table (tab, key, name, again)
663 hash_entry *ptr = hash_create (tab, key);
664 if (ptr->type == hash_integer)
666 sb_new (&ptr->value.s);
668 if (ptr->value.s.len)
671 ERROR ((stderr, "redefintion not allowed"));
674 ptr->type = hash_string;
675 sb_reset (&ptr->value.s);
677 sb_add_sb (&ptr->value.s, name);
680 /* add integer name to hash_table tab with sb key. */
684 hash_add_to_int_table (tab, key, name)
689 hash_entry *ptr = hash_create (tab, key);
693 /* lookup sb key in hash_table tab. if found return hash_entry result,
698 hash_lookup (tab, key)
702 int k = hash (key) % tab->size;
703 hash_entry **table = tab->table;
704 hash_entry *p = table[k];
707 if (p->key.len == key->len
708 && strncmp (p->key.ptr, key->ptr, key->len) == 0)
718 are handled in a really simple recursive decent way. each bit of
719 the machine takes an index into an sb and a pointer to an exp_t,
720 modifies the *exp_t and returns the index of the first character
721 past the part of the expression parsed.
723 expression precedence:
734 /* make sure that the exp_t at term is constant, if not the give the op ERROR. */
738 checkconst (op, term)
742 if (term->add_symbol.len
743 || term->sub_symbol.len)
745 ERROR ((stderr, "the %c operator cannot take non-absolute arguments.\n", op));
749 /* turn the number in string at idx into a number of base,
750 fill in ptr and return the index of the first character not in the
755 sb_strtol (idx, string, base, ptr)
762 idx = sb_skip_white (idx, string);
764 while (idx < string->len)
766 int ch = string->ptr[idx];
770 else if (ch >= 'a' && ch <= 'f')
772 else if (ch >= 'A' && ch <= 'F')
780 value = value * base + dig;
787 static int level_5 ();
790 level_0 (idx, string, lhs)
795 lhs->add_symbol.len = 0;
796 lhs->add_symbol.name = 0;
798 lhs->sub_symbol.len = 0;
799 lhs->sub_symbol.name = 0;
801 idx = sb_skip_white (idx, string);
805 if (isdigit (string->ptr[idx]))
807 idx = sb_strtol (idx, string, 10, &lhs->value);
809 else if (ISFIRSTCHAR (string->ptr[idx]))
812 lhs->add_symbol.name = string->ptr + idx;
813 while (idx < string->len && ISNEXTCHAR (string->ptr[idx]))
818 lhs->add_symbol.len = len;
820 else if (string->ptr[idx] == '"')
824 ERROR ((stderr, "string where expression expected.\n"));
825 idx = getstring (idx, string, &acc);
830 ERROR ((stderr, "can't find primary in expression.\n"));
833 return sb_skip_white (idx, string);
839 level_1 (idx, string, lhs)
844 idx = sb_skip_white (idx, string);
846 switch (string->ptr[idx])
849 idx = level_1 (idx + 1, string, lhs);
852 idx = level_1 (idx + 1, string, lhs);
853 checkconst ('~', lhs);
854 lhs->value = ~lhs->value;
859 idx = level_1 (idx + 1, string, lhs);
860 lhs->value = -lhs->value;
862 lhs->add_symbol = lhs->sub_symbol;
868 idx = level_5 (sb_skip_white (idx, string), string, lhs);
869 if (string->ptr[idx] != ')')
870 ERROR ((stderr, "misplaced closing parens.\n"));
875 idx = level_0 (idx, string, lhs);
878 return sb_skip_white (idx, string);
882 level_2 (idx, string, lhs)
889 idx = level_1 (idx, string, lhs);
891 while (idx < string->len && (string->ptr[idx] == '*'
892 || string->ptr[idx] == '/'))
894 char op = string->ptr[idx++];
895 idx = level_1 (idx, string, &rhs);
899 checkconst ('*', lhs);
900 checkconst ('*', &rhs);
901 lhs->value *= rhs.value;
904 checkconst ('/', lhs);
905 checkconst ('/', &rhs);
907 ERROR ((stderr, "attempt to divide by zero.\n"));
909 lhs->value /= rhs.value;
913 return sb_skip_white (idx, string);
918 level_3 (idx, string, lhs)
925 idx = level_2 (idx, string, lhs);
927 while (idx < string->len
928 && (string->ptr[idx] == '+'
929 || string->ptr[idx] == '-'))
931 char op = string->ptr[idx++];
932 idx = level_2 (idx, string, &rhs);
936 lhs->value += rhs.value;
937 if (lhs->add_symbol.name && rhs.add_symbol.name)
939 ERROR ((stderr, "can't add two relocatable expressions\n"));
941 /* change nn+symbol to symbol + nn */
942 if (rhs.add_symbol.name)
944 lhs->add_symbol = rhs.add_symbol;
948 lhs->value -= rhs.value;
949 lhs->sub_symbol = rhs.add_symbol;
953 return sb_skip_white (idx, string);
957 level_4 (idx, string, lhs)
964 idx = level_3 (idx, string, lhs);
966 while (idx < string->len &&
967 string->ptr[idx] == '&')
969 char op = string->ptr[idx++];
970 idx = level_3 (idx, string, &rhs);
974 checkconst ('&', lhs);
975 checkconst ('&', &rhs);
976 lhs->value &= rhs.value;
980 return sb_skip_white (idx, string);
984 level_5 (idx, string, lhs)
991 idx = level_4 (idx, string, lhs);
993 while (idx < string->len
994 && (string->ptr[idx] == '|' || string->ptr[idx] == '~'))
996 char op = string->ptr[idx++];
997 idx = level_4 (idx, string, &rhs);
1001 checkconst ('|', lhs);
1002 checkconst ('|', &rhs);
1003 lhs->value |= rhs.value;
1006 checkconst ('~', lhs);
1007 checkconst ('~', &rhs);
1008 lhs->value ^= rhs.value;
1012 return sb_skip_white (idx, string);
1016 /* parse the expression at offset idx into string, fill up res with
1017 the result. return the index of the first char past the expression.
1021 exp_parse (idx, string, res)
1026 return level_5 (sb_skip_white (idx, string), string, res);
1030 /* turn the expression at exp into text and glue it onto the end of
1034 exp_string (exp, string)
1042 if (exp->add_symbol.len)
1044 sb_add_buffer (string, exp->add_symbol.name, exp->add_symbol.len);
1052 sb_add_char (string, '+');
1053 sprintf (buf, "%d", exp->value);
1054 sb_add_string (string, buf);
1058 if (exp->sub_symbol.len)
1060 sb_add_char (string, '-');
1061 sb_add_buffer (string, exp->add_symbol.name, exp->add_symbol.len);
1067 sb_add_char (string, '0');
1071 /* parse the expression at offset idx into sb in, return the value in val.
1072 if the expression is not constant, give ERROR emsg. returns the index
1073 of the first character past the end of the expression. */
1076 exp_get_abs (emsg, idx, in, val)
1083 idx = exp_parse (idx, in, &res);
1084 if (res.add_symbol.len || res.sub_symbol.len)
1085 ERROR ((stderr, emsg));
1091 sb label; /* current label parsed from line */
1092 hash_table assign_hash_table; /* hash table for all assigned variables */
1093 hash_table keyword_hash_table; /* hash table for keyword */
1094 hash_table vars; /* hash table for eq variables */
1096 #define in_comment ';'
1100 strip_comments (out)
1105 for (i = 0; i < out->len; i++)
1107 if (ISCOMMENTCHAR(s[i]))
1116 /* push back character ch so that it can be read again. */
1126 if (sp->pushback_index)
1127 sp->pushback_index--;
1129 sb_add_char (&sp->pushback, ch);
1132 /* push the sb ptr onto the include stack, with the given name, type and index. */
1136 include_buf (name, ptr, type, index)
1143 if (sp - include_stack >= MAX_INCLUDES)
1144 FATAL ((stderr, "unreasonable nesting.\n"));
1146 sb_add_sb (&sp->name, name);
1149 sp->pushback_index = 0;
1152 sb_new (&sp->pushback);
1153 sb_add_sb (&sp->pushback, ptr);
1157 /* used in ERROR messages, print info on where the include stack is onto file. */
1160 include_print_where_line (file)
1163 struct include_stack *p = include_stack + 1;
1167 fprintf (file, "%s:%d ", sb_name (&p->name), p->linecount - ((p == sp) ? 1 : 0));
1172 /* used in listings, print the line number onto file. */
1174 include_print_line (file)
1178 struct include_stack *p = include_stack + 1;
1180 n = fprintf (file, "%4d", p->linecount);
1184 n += fprintf (file, ".%d", p->linecount);
1189 fprintf (file, " ");
1195 /* read a line from the top of the include stack into sb in. */
1206 putc (comment_char, outfile);
1207 if (print_line_number)
1208 include_print_line (outfile);
1222 WARNING ((stderr, "End of file not at start of line.\n"));
1224 putc ('\n', outfile);
1243 /* continued line */
1246 putc (comment_char, outfile);
1247 putc ('+', outfile);
1260 sb_add_char (in, ch);
1268 /* find a label from sb in and put it in out. */
1271 grab_label (in, out)
1277 if (ISFIRSTCHAR (in->ptr[i]))
1279 sb_add_char (out, in->ptr[i]);
1281 while ((ISNEXTCHAR (in->ptr[i])
1282 || in->ptr[i] == '\\'
1283 || in->ptr[i] == '&')
1286 sb_add_char (out, in->ptr[i]);
1293 /* find all strange base stuff and turn into decimal. also
1294 find all the other numbers and convert them from the default radix */
1297 change_base (idx, in, out)
1304 while (idx < in->len)
1306 if (idx < in->len - 1 && in->ptr[idx + 1] == '\'')
1310 switch (in->ptr[idx])
1329 ERROR ((stderr, "Illegal base character %c.\n", in->ptr[idx]));
1334 idx = sb_strtol (idx + 2, in, base, &value);
1335 sprintf (buffer, "%d", value);
1336 sb_add_string (out, buffer);
1338 else if (ISFIRSTCHAR (in->ptr[idx]))
1340 /* copy entire names through quickly */
1341 sb_add_char (out, in->ptr[idx]);
1343 while (idx < in->len && ISNEXTCHAR (in->ptr[idx]))
1345 sb_add_char (out, in->ptr[idx]);
1349 else if (isdigit (in->ptr[idx]))
1352 /* all numbers must start with a digit, let's chew it and
1354 idx = sb_strtol (idx, in, radix, &value);
1355 sprintf (buffer, "%d", value);
1356 sb_add_string (out, buffer);
1358 /* skip all undigsested letters */
1359 while (idx < in->len && ISNEXTCHAR (in->ptr[idx]))
1361 sb_add_char (out, in->ptr[idx]);
1367 /* nothing special, just pass it through */
1368 sb_add_char (out, in->ptr[idx]);
1385 do_assign (again, idx, in)
1390 /* stick label in symbol table with following value */
1395 idx = exp_parse (idx, in, &e);
1396 exp_string (&e, &acc);
1397 hash_add_to_string_table (&assign_hash_table, &label, &acc, again);
1402 /* .radix [b|q|d|h] */
1409 int idx = sb_skip_white (0, ptr);
1410 switch (ptr->ptr[idx])
1429 ERROR ((stderr, "radix is %c must be one of b, q, d or h", radix));
1434 /* Parse off a .b, .w or .l */
1437 get_opsize (idx, in, size)
1443 if (in->ptr[idx] == '.')
1447 switch (in->ptr[idx])
1465 ERROR ((stderr, "size must be one of b, w or l, is %c.\n", in->ptr[idx]));
1478 idx = sb_skip_white (idx, line);
1480 && ISCOMMENTCHAR(line->ptr[idx]))
1482 if (idx >= line->len)
1487 /* .data [.b|.w|.l] <data>*
1488 or d[bwl] <data>* */
1491 do_data (idx, in, size)
1497 char *opname = ".yikes!";
1503 idx = get_opsize (idx, in, &opsize);
1522 fprintf (outfile, "%s\t", opname);
1524 idx = sb_skip_white (idx, in);
1528 && in->ptr[idx] == '"')
1531 idx = getstring (idx, in, &acc);
1532 for (i = 0; i < acc.len; i++)
1535 fprintf(outfile,",");
1536 fprintf (outfile, "%d", acc.ptr[i]);
1541 while (!eol (idx, in))
1544 idx = exp_parse (idx, in, &e);
1545 exp_string (&e, &acc);
1546 sb_add_char (&acc, 0);
1547 fprintf (outfile, acc.ptr);
1548 if (idx < in->len && in->ptr[idx] == ',')
1550 fprintf (outfile, ",");
1556 sb_print_at (idx, in);
1557 fprintf (outfile, "\n");
1560 /* .datab [.b|.w|.l] <repeat>,<fill> */
1571 idx = get_opsize (idx, in, &opsize);
1573 idx = exp_get_abs ("datab repeat must be constant.\n", idx, in, &repeat);
1574 idx = sb_skip_comma (idx, in);
1575 idx = exp_get_abs ("datab data must be absolute.\n", idx, in, &fill);
1577 fprintf (outfile, ".fill\t%d,%d,%d\n", repeat, opsize, fill);
1588 idx = exp_get_abs ("align needs absolute expression.\n", idx, in, &al);
1593 WARNING ((stderr, "alignment must be one of 1, 2 or 4.\n"));
1595 fprintf (outfile, ".align %d\n", al);
1598 /* .res[.b|.w|.l] <size> */
1601 do_res (idx, in, type)
1609 idx = get_opsize (idx, in, &size);
1610 while (!eol(idx, in))
1612 idx = sb_skip_white (idx, in);
1613 if (in->ptr[idx] == ',')
1615 idx = exp_get_abs ("res needs absolute expression for fill count.\n", idx, in, &count);
1617 if (type == 'c' || type == 'z')
1620 fprintf (outfile, ".space %d\n", count * size);
1631 fprintf (outfile, ".global %s\n", sb_name (in));
1634 /* .print [list] [nolist] */
1641 idx = sb_skip_white (idx, in);
1642 while (idx < in->len)
1644 if (strncasecmp (in->ptr + idx, "LIST", 4) == 0)
1646 fprintf (outfile, ".list\n");
1649 else if (strncasecmp (in->ptr + idx, "NOLIST", 6) == 0)
1651 fprintf (outfile, ".nolist\n");
1660 do_heading (idx, in)
1666 idx = getstring (idx, in, &head);
1667 fprintf (outfile, ".title \"%s\"\n", sb_name (&head));
1676 fprintf (outfile, ".eject\n");
1679 /* .form [lin=<value>] [col=<value>] */
1687 idx = sb_skip_white (idx, in);
1689 while (idx < in->len)
1692 if (strncasecmp (in->ptr + idx, "LIN=", 4) == 0)
1695 idx = exp_get_abs ("form LIN= needs absolute expresssion.\n", idx, in, &lines);
1698 if (strncasecmp (in->ptr + idx, "COL=", 4) == 0)
1701 idx = exp_get_abs ("form COL= needs absolute expresssion.\n", idx, in, &columns);
1706 fprintf (outfile, ".psize %d,%d\n", lines, columns);
1711 /* Fetch string from the input stream,
1713 'Bxyx<whitespace> -> return 'Bxyza
1714 %<char> -> return string of decimal value of x
1715 "<string>" -> return string
1716 xyx<whitespace> -> return xyz
1719 get_any_string (idx, in, out, expand, pretend_quoted)
1727 idx = sb_skip_white (idx, in);
1731 if (in->len > 2 && in->ptr[idx+1] == '\'' && ISBASE (in->ptr[idx]))
1733 while (!ISSEP (in->ptr[idx]))
1734 sb_add_char (out, in->ptr[idx++]);
1736 else if (in->ptr[idx] == '%'
1742 /* Turns the next expression into a string */
1743 idx = exp_get_abs ("% operator needs absolute expression",
1747 sprintf(buf, "%d", val);
1748 sb_add_string (out, buf);
1750 else if (in->ptr[idx] == '"'
1751 || in->ptr[idx] == '<'
1752 || (alternate && in->ptr[idx] == '\''))
1754 if (alternate && expand)
1756 /* Keep the quotes */
1757 sb_add_char (out, '\"');
1759 idx = getstring (idx, in, out);
1760 sb_add_char (out, '\"');
1764 idx = getstring (idx, in, out);
1769 while (idx < in->len
1770 && (in->ptr[idx] == '"'
1771 || in->ptr[idx] == '\''
1773 || !ISSEP (in->ptr[idx])))
1775 if (in->ptr[idx] == '"'
1776 || in->ptr[idx] == '\'')
1778 char tchar = in->ptr[idx];
1779 sb_add_char (out, in->ptr[idx++]);
1780 while (idx < in->len
1781 && in->ptr[idx] != tchar)
1782 sb_add_char (out, in->ptr[idx++]);
1786 sb_add_char (out, in->ptr[idx++]);
1795 /* skip along sb in starting at idx, suck off whitespace a ( and more
1796 whitespace. return the idx of the next char */
1799 skip_openp (idx, in)
1803 idx = sb_skip_white (idx, in);
1804 if (in->ptr[idx] != '(')
1805 ERROR ((stderr, "misplaced ( .\n"));
1806 idx = sb_skip_white (idx + 1, in);
1810 /* skip along sb in starting at idx, suck off whitespace a ) and more
1811 whitespace. return the idx of the next char */
1814 skip_closep (idx, in)
1818 idx = sb_skip_white (idx, in);
1819 if (in->ptr[idx] != ')')
1820 ERROR ((stderr, "misplaced ).\n"));
1821 idx = sb_skip_white (idx + 1, in);
1828 dolen (idx, in, out)
1837 sb_new (&stringout);
1838 idx = skip_openp (idx, in);
1839 idx = get_and_process (idx, in, &stringout);
1840 idx = skip_closep (idx, in);
1841 sprintf (buffer, "%d", stringout.len);
1842 sb_add_string (out, buffer);
1844 sb_kill (&stringout);
1853 doinstr (idx, in, out)
1867 idx = skip_openp (idx, in);
1868 idx = get_and_process (idx, in, &string);
1869 idx = sb_skip_comma (idx, in);
1870 idx = get_and_process (idx, in, &search);
1871 idx = sb_skip_comma (idx, in);
1872 if (isdigit (in->ptr[idx]))
1874 idx = exp_get_abs (".instr needs absolute expresson.\n", idx, in, &start);
1880 idx = skip_closep (idx, in);
1882 for (i = start; i < string.len; i++)
1884 if (strncmp (string.ptr + i, search.ptr, search.len) == 0)
1890 sprintf (buffer, "%d", res);
1891 sb_add_string (out, buffer);
1899 dosubstr (idx, in, out)
1909 idx = skip_openp (idx, in);
1910 idx = get_and_process (idx, in, &string);
1911 idx = sb_skip_comma (idx, in);
1912 idx = exp_get_abs ("need absolute position.\n", idx, in, &pos);
1913 idx = sb_skip_comma (idx, in);
1914 idx = exp_get_abs ("need absolute length.\n", idx, in, &len);
1915 idx = skip_closep (idx, in);
1918 if (len < 0 || pos < 0 ||
1920 || pos + len > string.len)
1922 sb_add_string (out, " ");
1926 sb_add_char (out, '"');
1929 sb_add_char (out, string.ptr[pos++]);
1932 sb_add_char (out, '"');
1938 /* scan line, change tokens in the hash table to their replacements */
1940 process_assigns (idx, in, buf)
1945 while (idx < in->len)
1948 if (in->ptr[idx] == '\\'
1949 && in->ptr[idx + 1] == '&')
1951 idx = condass_lookup_name (in, idx + 2, buf, 1);
1953 else if (in->ptr[idx] == '\\'
1954 && in->ptr[idx + 1] == '$')
1956 idx = condass_lookup_name (in, idx + 2, buf, 0);
1958 else if (idx + 3 < in->len
1959 && in->ptr[idx] == '.'
1960 && in->ptr[idx + 1] == 'L'
1961 && in->ptr[idx + 2] == 'E'
1962 && in->ptr[idx + 3] == 'N')
1963 idx = dolen (idx + 4, in, buf);
1964 else if (idx + 6 < in->len
1965 && in->ptr[idx] == '.'
1966 && in->ptr[idx + 1] == 'I'
1967 && in->ptr[idx + 2] == 'N'
1968 && in->ptr[idx + 3] == 'S'
1969 && in->ptr[idx + 4] == 'T'
1970 && in->ptr[idx + 5] == 'R')
1971 idx = doinstr (idx + 6, in, buf);
1972 else if (idx + 7 < in->len
1973 && in->ptr[idx] == '.'
1974 && in->ptr[idx + 1] == 'S'
1975 && in->ptr[idx + 2] == 'U'
1976 && in->ptr[idx + 3] == 'B'
1977 && in->ptr[idx + 4] == 'S'
1978 && in->ptr[idx + 5] == 'T'
1979 && in->ptr[idx + 6] == 'R')
1980 idx = dosubstr (idx + 7, in, buf);
1981 else if (ISFIRSTCHAR (in->ptr[idx]))
1983 /* may be a simple name subsitution, see if we have a word */
1986 while (cur < in->len
1987 && (ISNEXTCHAR (in->ptr[cur])))
1991 sb_add_buffer (&acc, in->ptr + idx, cur - idx);
1992 ptr = hash_lookup (&assign_hash_table, &acc);
1995 /* Found a definition for it */
1996 sb_add_sb (buf, &ptr->value.s);
2000 /* No definition, just copy the word */
2001 sb_add_sb (buf, &acc);
2008 sb_add_char (buf, in->ptr[idx++]);
2014 get_and_process (idx, in, out)
2021 idx = get_any_string (idx, in, &t, 1, 0);
2022 process_assigns (0, &t, out);
2043 more = get_line (&line);
2046 /* Find any label and pseudo op that we're intested in */
2051 fprintf (outfile, "\n");
2055 l = grab_label (&line, &label_in);
2059 /* Munge any label */
2062 process_assigns (0, &label_in, &label);
2065 if (line.ptr[l] == ':')
2067 while (ISWHITE (line.ptr[l]) && l < line.len)
2072 if (process_pseudo_op (l, &line, &acc))
2078 else if (condass_on ())
2080 if (macro_op (l, &line))
2090 fprintf (outfile, "%s:\t", sb_name (&label));
2093 fprintf (outfile, "\t");
2095 process_assigns (l, &line, &t1);
2097 change_base (0, &t1, &t2);
2098 fprintf (outfile, "%s\n", sb_name (&t2));
2104 /* Only a label on this line */
2105 if (label.len && condass_on())
2107 fprintf (outfile, "%s:\n", sb_name (&label));
2115 more = get_line (&line);
2119 WARNING ((stderr, "END missing from end of file.\n"));
2127 free_old_entry (ptr)
2132 if (ptr->type == hash_string)
2133 sb_kill(&ptr->value.s);
2137 /* name: .ASSIGNA <value> */
2140 do_assigna (idx, in)
2148 process_assigns (idx, in, &tmp);
2149 idx = exp_get_abs (".ASSIGNA needs constant expression argument.\n", 0, &tmp, &val);
2153 ERROR ((stderr, ".ASSIGNA without label.\n"));
2157 hash_entry *ptr = hash_create (&vars, &label);
2158 free_old_entry (ptr);
2159 ptr->type = hash_integer;
2165 /* name: .ASSIGNC <string> */
2168 do_assignc (idx, in)
2174 idx = getstring (idx, in, &acc);
2178 ERROR ((stderr, ".ASSIGNS without label.\n"));
2182 hash_entry *ptr = hash_create (&vars, &label);
2183 free_old_entry (ptr);
2184 ptr->type = hash_string;
2185 sb_new (&ptr->value.s);
2186 sb_add_sb (&ptr->value.s, &acc);
2192 /* name: .REG (reg) */
2199 /* remove reg stuff from inside parens */
2201 idx = skip_openp (idx, in);
2203 while (idx < in->len && in->ptr[idx] != ')')
2205 sb_add_char (&what, in->ptr[idx]);
2208 hash_add_to_string_table (&assign_hash_table, &label, &what, 1);
2214 condass_lookup_name (inbuf, idx, out, warn)
2222 sb_new (&condass_acc);
2224 while (idx < inbuf->len
2225 && ISNEXTCHAR (inbuf->ptr[idx]))
2227 sb_add_char (&condass_acc, inbuf->ptr[idx++]);
2230 if (inbuf->ptr[idx] == '\'')
2232 ptr = hash_lookup (&vars, &condass_acc);
2239 WARNING ((stderr, "Can't find preprocessor variable %s.\n", sb_name (&condass_acc)));
2243 sb_add_string (out, "0");
2248 if (ptr->type == hash_integer)
2251 sprintf (buffer, "%d", ptr->value.i);
2252 sb_add_string (out, buffer);
2256 sb_add_sb (out, &ptr->value.s);
2259 sb_kill (&condass_acc);
2272 whatcond (idx, in, val)
2279 idx = sb_skip_white (idx, in);
2281 if (p[0] == 'E' && p[1] == 'Q')
2283 else if (p[0] == 'N' && p[1] == 'E')
2285 else if (p[0] == 'L' && p[1] == 'T')
2287 else if (p[0] == 'L' && p[1] == 'E')
2289 else if (p[0] == 'G' && p[1] == 'T')
2291 else if (p[0] == 'G' && p[1] == 'E')
2295 ERROR ((stderr, "Comparison operator must be one of EQ, NE, LT, LE, GT or GE.\n"));
2298 idx = sb_skip_white (idx + 2, in);
2315 idx = sb_skip_white (idx, in);
2317 if (in->ptr[idx] == '"')
2321 /* This is a string comparision */
2322 idx = getstring (idx, in, &acc_a);
2323 idx = whatcond (idx, in, &cond);
2324 idx = getstring (idx, in, &acc_b);
2325 same = acc_a.len == acc_b.len && (strncmp (acc_a.ptr, acc_b.ptr, acc_a.len) == 0);
2327 if (cond != EQ && cond != NE)
2329 ERROR ((stderr, "Comparison operator for strings must be EQ or NE\n"));
2333 res = (cond != EQ) ^ same;
2336 /* This is a numeric expression */
2341 idx = exp_get_abs ("Conditional operator must have absolute operands.\n", idx, in, &vala);
2342 idx = whatcond (idx, in, &cond);
2343 idx = sb_skip_white (idx, in);
2344 if (in->ptr[idx] == '"')
2346 WARNING ((stderr, "String compared against expression.\n"));
2351 idx = exp_get_abs ("Conditional operator must have absolute operands.\n", idx, in, &valb);
2394 if (ifi >= IFNESTING)
2396 FATAL ((stderr, "AIF nesting unreasonable.\n"));
2399 ifstack[ifi].on = ifstack[ifi-1].on ? istrue (idx, in) : 0;
2400 ifstack[ifi].hadelse = 0;
2408 ifstack[ifi].on = ifstack[ifi-1].on ? !ifstack[ifi].on : 0;
2409 if (ifstack[ifi].hadelse)
2411 ERROR ((stderr, "Multiple AELSEs in AIF.\n"));
2413 ifstack[ifi].hadelse = 1;
2427 ERROR ((stderr, "AENDI without AIF.\n"));
2434 return ifstack[ifi].on;
2438 /* Read input lines till we get to a TO string.
2439 Increase nesting depth if we geta FROM string.
2440 Put the results into sb at PTR. */
2443 buffer_and_nest (from, to, ptr)
2448 int from_len = strlen (from);
2449 int to_len = strlen (to);
2451 int line_start = ptr->len;
2452 int line = linecount ();
2454 int more = get_line (ptr);
2458 /* Try and find the first pseudo op on the line */
2463 /* With normal syntax we can suck what we want till we get to the dot.
2464 With the alternate, labels have to start in the first column, since
2465 we cant tell what's a label and whats a pseudoop */
2467 /* Skip leading whitespace */
2469 && ISWHITE (ptr->ptr[i]))
2472 /* Skip over a label */
2474 && ISNEXTCHAR (ptr->ptr[i]))
2479 && ptr->ptr[i] == ':')
2483 /* Skip trailing whitespace */
2485 && ISWHITE (ptr->ptr[i]))
2488 if (i < ptr->len && (ptr->ptr[i] == '.'
2491 if (ptr->ptr[i] == '.')
2493 if (strncasecmp (ptr->ptr + i, from, from_len) == 0)
2495 if (strncasecmp (ptr->ptr + i, to, to_len) == 0)
2500 /* Reset the string to not include the ending rune */
2501 ptr->len = line_start;
2507 /* Add a CR to the end and keep running */
2508 sb_add_char (ptr, '\n');
2509 line_start = ptr->len;
2510 more = get_line (ptr);
2515 FATAL ((stderr, "End of file whilst inside %s, started on line %d.\n", from, line));
2523 ERROR ((stderr, "AENDR without a AREPEAT.\n"));
2542 process_assigns (idx, in, &exp);
2543 doit = istrue (0, &exp);
2545 buffer_and_nest ("AWHILE", "AENDW", &sub);
2560 int index = include_next_index ();
2564 sb_add_sb (©, &sub);
2565 sb_add_sb (©, in);
2566 sb_add_string (©, "\n");
2567 sb_add_sb (©, &sub);
2568 sb_add_string (©, "\t.AENDW\n");
2569 /* Push another WHILE */
2570 include_buf (&exp, ©, include_while, index);
2583 ERROR ((stderr, "AENDW without a AENDW.\n"));
2589 Pop things off the include stack until the type and index changes */
2594 include_type type = sp->type;
2595 if (type == include_repeat
2596 || type == include_while
2597 || type == include_macro)
2599 int index = sp->index;
2601 while (sp->index == index
2602 && sp->type == type)
2612 do_arepeat (idx, in)
2616 sb exp; /* buffer with expression in it */
2617 sb copy; /* expanded repeat block */
2618 sb sub; /* contents of AREPEAT */
2624 process_assigns (idx, in, &exp);
2625 idx = exp_get_abs ("AREPEAT must have absolute operand.\n", 0, &exp, &rc);
2626 buffer_and_nest ("AREPEAT", "AENDR", &sub);
2629 /* Push back the text following the repeat, and another repeat block
2640 int index = include_next_index ();
2641 sb_add_sb (©, &sub);
2644 sprintf (buffer, "\t.AREPEAT %d\n", rc - 1);
2645 sb_add_string (©, buffer);
2646 sb_add_sb (©, &sub);
2647 sb_add_string (©, " .AENDR\n");
2650 include_buf (&exp, ©, include_repeat, index);
2662 ERROR ((stderr, ".ENDM without a matching .MACRO.\n"));
2666 /* MARRO PROCESSING */
2669 hash_table macro_table;
2679 do_formals (macro, idx, in)
2684 formal_entry **p = ¯o->formals;
2685 macro->formal_count = 0;
2686 hash_new_table (5, ¯o->formal_hash);
2687 while (idx < in->len)
2689 formal_entry *formal;
2691 formal = (formal_entry *) xmalloc (sizeof (formal_entry));
2693 sb_new (&formal->name);
2694 sb_new (&formal->def);
2695 sb_new (&formal->actual);
2697 idx = sb_skip_white (idx, in);
2698 idx = get_token (idx, in, &formal->name);
2699 if (formal->name.len == 0)
2701 idx = sb_skip_white (idx, in);
2702 if (formal->name.len)
2704 /* This is a formal */
2705 if (idx < in->len && in->ptr[idx] == '=')
2708 idx = get_any_string (idx + 1, in, &formal->def, 1, 0);
2713 /* Add to macro's hash table */
2715 hash_entry *p = hash_create (¯o->formal_hash, &formal->name);
2716 p->type = hash_formal;
2717 p->value.f = formal;
2720 formal->index = macro->formal_count;
2721 idx = sb_skip_comma (idx, in);
2722 macro->formal_count++;
2729 /* Parse off LOCAL n1, n2,... Invent a label name for it */
2732 do_local (idx, line)
2742 idx = sb_skip_white (idx, line);
2743 while (!eol(idx, line))
2748 sprintf(subs, "LL%04x", ln);
2749 idx = get_token(idx, line, &acc);
2750 sb_add_string (&sub, subs);
2751 hash_add_to_string_table (&assign_hash_table, &acc, &sub, 1);
2752 idx = sb_skip_comma (idx, line);
2767 macro = (macro_entry *) xmalloc (sizeof (macro_entry));
2768 sb_new (¯o->sub);
2771 macro->formal_count = 0;
2774 idx = sb_skip_white (idx, in);
2775 buffer_and_nest ("MACRO", "ENDM", ¯o->sub);
2779 sb_add_sb (&name, &label);
2780 if (in->ptr[idx] == '(')
2782 /* It's the label: MACRO (formals,...) sort */
2783 idx = do_formals (macro, idx + 1, in);
2784 if (in->ptr[idx] != ')')
2785 ERROR ((stderr, "Missing ) after formals.\n"));
2788 /* It's the label: MACRO formals,... sort */
2789 idx = do_formals (macro, idx, in);
2794 idx = get_token (idx, in, &name);
2795 idx = sb_skip_white (idx, in);
2796 idx = do_formals (macro, idx, in);
2799 /* and stick it in the macro hash table */
2800 hash_create (¯o_table, &name)->value.m = macro;
2805 get_token (idx, in, name)
2811 && ISFIRSTCHAR (in->ptr[idx]))
2813 sb_add_char (name, in->ptr[idx++]);
2814 while (idx < in->len
2815 && ISNEXTCHAR (in->ptr[idx]))
2817 sb_add_char (name, in->ptr[idx++]);
2820 /* Ignore trailing & */
2821 if (alternate && idx < in->len && in->ptr[idx] == '&')
2826 /* Scan a token, but stop if a ' is seen */
2828 get_apost_token (idx, in, name, kind)
2834 idx = get_token (idx, in, name);
2835 if (idx < in->len && in->ptr[idx] == kind)
2841 sub_actual (src, in, t, m, kind, out, copyifnotthere)
2850 /* This is something to take care of */
2852 src = get_apost_token (src, in, t, kind);
2853 /* See if it's in the macro's hash table */
2854 ptr = hash_lookup (&m->formal_hash, t);
2857 if (ptr->value.f->actual.len)
2859 sb_add_sb (out, &ptr->value.f->actual);
2863 sb_add_sb (out, &ptr->value.f->def);
2866 else if (copyifnotthere)
2872 sb_add_char (out, '\\');
2880 macro_expand (name, idx, in, m)
2890 int is_positional = 0;
2896 /* Reset any old value the actuals may have */
2897 for (f = m->formals; f; f = f->next)
2898 sb_reset (&f->actual);
2900 /* Peel off the actuals and store them away in the hash tables' actuals */
2901 while (!eol(idx, in))
2904 idx = sb_skip_white (idx, in);
2905 /* Look and see if it's a positional or keyword arg */
2907 while (scan < in->len
2908 && !ISSEP (in->ptr[scan])
2909 && (!alternate && in->ptr[scan] != '='))
2911 if (scan < in->len && (!alternate) && in->ptr[scan] == '=')
2916 ERROR ((stderr, "Can't mix positional and keyword arguments.\n"));
2919 /* This is a keyword arg, fetch the formal name and
2920 then the actual stuff */
2922 idx = get_token (idx, in, &t);
2923 if (in->ptr[idx] != '=')
2924 ERROR ((stderr, "confused about formal params.\n"));
2926 /* Lookup the formal in the macro's list */
2927 ptr = hash_lookup (&m->formal_hash, &t);
2930 ERROR ((stderr, "MACRO formal argument %s does not exist.\n", sb_name (&t)));
2935 /* Insert this value into the right place */
2936 sb_reset (&ptr->value.f->actual);
2937 idx = get_any_string (idx + 1, in, &ptr->value.f->actual, 0, 0);
2942 /* This is a positional arg */
2946 ERROR ((stderr, "Can't mix positional and keyword arguments.\n"));
2951 ERROR ((stderr, "Too many positional arguments.\n"));
2955 sb_reset (&f->actual);
2956 idx = get_any_string (idx, in, &f->actual, 1, 0);
2959 idx = sb_skip_comma (idx, in);
2962 /* Copy the stuff from the macro buffer into a safe place and substitute any args */
2970 while (src < in->len)
2972 if (in->ptr[src] == '&')
2975 src = sub_actual (src + 1, in, &t, m, '&', &out, 0);
2977 else if (in->ptr[src] == '\\')
2980 if (in->ptr[src] == comment_char)
2982 /* This is a comment, just drop the rest of the line */
2983 while (src < in->len
2984 && in->ptr[src] != '\n')
2988 else if (in->ptr[src] == '(')
2990 /* Sub in till the next ')' literally */
2992 while (src < in->len && in->ptr[src] != ')')
2994 sb_add_char (&out, in->ptr[src++]);
2996 if (in->ptr[src] == ')')
2999 ERROR ((stderr, "Missplaced ).\n"));
3001 else if (in->ptr[src] == '@')
3003 /* Sub in the macro invocation number */
3007 sprintf (buffer, "%05d", number);
3008 sb_add_string (&out, buffer);
3010 else if (in->ptr[src] == '&')
3012 /* This is a preprocessor variable name, we don't do them
3014 sb_add_char (&out, '\\');
3015 sb_add_char (&out, '&');
3021 src = sub_actual (src, in, &t, m, '\'', &out, 0);
3024 else if (ISFIRSTCHAR (in->ptr[src]) && alternate)
3027 src = sub_actual (src, in, &t, m, '\'', &out, 1);
3029 else if (ISCOMMENTCHAR (in->ptr[src])
3030 && src + 1 < in->len
3031 && ISCOMMENTCHAR (in->ptr[src+1])
3034 /* Two comment chars in a row cause the rest of the line to be dropped */
3035 while (src < in->len && in->ptr[src] != '\n')
3038 else if (in->ptr[src] == '"')
3041 sb_add_char (&out, in->ptr[src++]);
3045 sb_add_char (&out, in->ptr[src++]);
3048 include_buf (name, &out, include_macro, include_next_index ());
3061 /* The macro name must be the first thing on the line */
3067 idx = get_token (idx, in, &name);
3071 /* Got a name, look it up */
3073 ptr = hash_lookup (¯o_table, &name);
3077 /* It's in the table, copy out the stuff and convert any macro args */
3078 macro_expand (&name, idx, in, ptr->value.m);
3090 /* STRING HANDLING */
3093 getstring (idx, in, acc)
3098 idx = sb_skip_white (idx, in);
3100 while (idx < in->len
3101 && (in->ptr[idx] == '"'
3102 || in->ptr[idx] == '<'
3103 || (in->ptr[idx] == '\'' && alternate)))
3105 if (in->ptr[idx] == '<')
3111 while ((in->ptr[idx] != '>' || nest)
3114 if (in->ptr[idx] == '!')
3117 sb_add_char (acc, in->ptr[idx++]);
3120 if (in->ptr[idx] == '>')
3122 if (in->ptr[idx] == '<')
3124 sb_add_char (acc, in->ptr[idx++]);
3132 idx = exp_get_abs ("Character code in string must be absolute expression.\n",
3134 sb_add_char (acc, code);
3136 if (in->ptr[idx] != '>')
3137 ERROR ((stderr, "Missing > for character code.\n"));
3141 else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
3143 char tchar = in->ptr[idx];
3145 while (idx < in->len)
3147 if (alternate && in->ptr[idx] == '!')
3150 sb_add_char (acc, in->ptr[idx++]);
3153 if (in->ptr[idx] == tchar)
3156 if (idx >= in->len || in->ptr[idx] != tchar)
3159 sb_add_char (acc, in->ptr[idx]);
3169 /* .SDATA[C|Z] <string> */
3173 do_sdata (idx, in, type)
3182 fprintf (outfile, ".byte\t");
3184 while (!eol (idx, in))
3188 idx = sb_skip_white (idx, in);
3189 while (!eol (idx, in))
3191 pidx = idx = get_any_string (idx, in, &acc, 0, 1);
3196 ERROR ((stderr, "string for SDATAC longer than 255 characters (%d).\n", acc.len));
3198 fprintf (outfile, "%d", acc.len);
3202 for (i = 0; i < acc.len; i++)
3206 fprintf (outfile, ",");
3208 fprintf (outfile, "%d", acc.ptr[i]);
3215 fprintf (outfile, ",");
3216 fprintf (outfile, "0");
3218 idx = sb_skip_comma (idx, in);
3219 if (idx == pidx) break;
3221 if (!alternate && in->ptr[idx] != ',' && idx != in->len)
3223 fprintf (outfile, "\n");
3224 ERROR ((stderr, "illegal character in SDATA line (0x%x).\n", in->ptr[idx]));
3230 fprintf (outfile, "\n");
3233 /* .SDATAB <count> <string> */
3245 idx = exp_get_abs ("Must have absolute SDATAB repeat count.\n", idx, in, &repeat);
3248 ERROR ((stderr, "Must have positive SDATAB repeat count (%d).\n", repeat));
3252 idx = sb_skip_comma (idx, in);
3253 idx = getstring (idx, in, &acc);
3255 for (i = 0; i < repeat; i++)
3258 fprintf (outfile, "\t");
3259 fprintf (outfile, ".byte\t");
3261 fprintf (outfile, "\n");
3271 FILE *newone = fopen (name, "r");
3275 if (isp == MAX_INCLUDES)
3276 FATAL ((stderr, "Unreasonable include depth (%ld).\n", (long) isp));
3279 sp->handle = newone;
3282 sb_add_string (&sp->name, name);
3285 sp->pushback_index = 0;
3286 sp->type = include_file;
3288 sb_new (&sp->pushback);
3293 do_include (idx, in)
3300 idx = getstring (idx, in, &t);
3301 text = sb_name (&t);
3302 if (!new_file (text))
3304 FATAL ((stderr, "Can't open include file `%s'.\n", text));
3312 if (sp != include_stack)
3315 fclose (sp->handle);
3320 /* Get the next character from the include stack. If there's anything
3321 in the pushback buffer, take that first. If we're at eof, pop from
3322 the stack and try again. Keep the linecount up to date. */
3329 if (sp->pushback.len != sp->pushback_index)
3331 r = (char) (sp->pushback.ptr[sp->pushback_index++]);
3332 /* When they've all gone, reset the pointer */
3333 if (sp->pushback_index == sp->pushback.len)
3335 sp->pushback.len = 0;
3336 sp->pushback_index = 0;
3339 else if (sp->handle)
3341 r = getc (sp->handle);
3346 if (r == EOF && isp)
3350 while (r == EOF && isp)
3368 return sp->linecount;
3372 include_next_index ()
3376 && index > MAX_REASONABLE)
3377 FATAL ((stderr, "Unreasonable expansion (-u turns off check).\n"));
3382 /* Initialize the chartype vector. */
3388 for (x = 0; x < 256; x++)
3390 if (isalpha (x) || x == '_' || x == '$')
3391 chartype[x] |= FIRSTBIT;
3393 if (isdigit (x) || isalpha (x) || x == '_' || x == '$')
3394 chartype[x] |= NEXTBIT;
3396 if (x == ' ' || x == '\t' || x == ',' || x == '"' || x == ';'
3397 || x == '"' || x == '<' || x == '>' || x == ')' || x == '(')
3398 chartype[x] |= SEPBIT;
3400 if (x == 'b' || x == 'B'
3401 || x == 'q' || x == 'Q'
3402 || x == 'h' || x == 'H'
3403 || x == 'd' || x == 'D')
3404 chartype [x] |= BASEBIT;
3406 if (x == ' ' || x == '\t')
3407 chartype[x] |= WHITEBIT;
3409 if (x == comment_char)
3410 chartype[x] |= COMMENTBIT;
3416 /* What to do with all the keywords */
3417 #define PROCESS 0x1000 /* Run substitution over the line */
3418 #define LAB 0x2000 /* Spit out the label */
3420 #define K_EQU PROCESS|1
3421 #define K_ASSIGN PROCESS|2
3422 #define K_REG PROCESS|3
3423 #define K_ORG PROCESS|4
3424 #define K_RADIX PROCESS|5
3425 #define K_DATA LAB|PROCESS|6
3426 #define K_DATAB LAB|PROCESS|7
3427 #define K_SDATA LAB|PROCESS|8
3428 #define K_SDATAB LAB|PROCESS|9
3429 #define K_SDATAC LAB|PROCESS|10
3430 #define K_SDATAZ LAB|PROCESS|11
3431 #define K_RES LAB|PROCESS|12
3432 #define K_SRES LAB|PROCESS|13
3433 #define K_SRESC LAB|PROCESS|14
3434 #define K_SRESZ LAB|PROCESS|15
3435 #define K_EXPORT LAB|PROCESS|16
3436 #define K_GLOBAL LAB|PROCESS|17
3437 #define K_PRINT LAB|PROCESS|19
3438 #define K_FORM LAB|PROCESS|20
3439 #define K_HEADING LAB|PROCESS|21
3440 #define K_PAGE LAB|PROCESS|22
3441 #define K_IMPORT LAB|PROCESS|23
3442 #define K_PROGRAM LAB|PROCESS|24
3443 #define K_END PROCESS|25
3444 #define K_INCLUDE PROCESS|26
3445 #define K_IGNORED PROCESS|27
3446 #define K_ASSIGNA PROCESS|28
3447 #define K_ASSIGNC 29
3448 #define K_AIF PROCESS|30
3449 #define K_AELSE PROCESS|31
3450 #define K_AENDI PROCESS|32
3451 #define K_AREPEAT PROCESS|33
3452 #define K_AENDR PROCESS|34
3454 #define K_AENDW PROCESS|36
3456 #define K_MACRO PROCESS|38
3458 #define K_ALIGN PROCESS|LAB|40
3459 #define K_ALTERNATE 41
3460 #define K_DB LAB|PROCESS|42
3461 #define K_DW LAB|PROCESS|43
3462 #define K_DL LAB|PROCESS|44
3474 { "EQU", K_EQU, 0 },
3475 { "ALTERNATE", K_ALTERNATE, 0 },
3476 { "ASSIGN", K_ASSIGN, 0 },
3477 { "REG", K_REG, 0 },
3478 { "ORG", K_ORG, 0 },
3479 { "RADIX", K_RADIX, 0 },
3480 { "DATA", K_DATA, 0 },
3484 { "DATAB", K_DATAB, 0 },
3485 { "SDATA", K_SDATA, 0 },
3486 { "SDATAB", K_SDATAB, 0 },
3487 { "SDATAZ", K_SDATAZ, 0 },
3488 { "SDATAC", K_SDATAC, 0 },
3489 { "RES", K_RES, 0 },
3490 { "SRES", K_SRES, 0 },
3491 { "SRESC", K_SRESC, 0 },
3492 { "SRESZ", K_SRESZ, 0 },
3493 { "EXPORT", K_EXPORT, 0 },
3494 { "GLOBAL", K_GLOBAL, 0 },
3495 { "PRINT", K_PRINT, 0 },
3496 { "FORM", K_FORM, 0 },
3497 { "HEADING", K_HEADING, 0 },
3498 { "PAGE", K_PAGE, 0 },
3499 { "PROGRAM", K_IGNORED, 0 },
3500 { "END", K_END, 0 },
3501 { "INCLUDE", K_INCLUDE, 0 },
3502 { "ASSIGNA", K_ASSIGNA, 0 },
3503 { "ASSIGNC", K_ASSIGNC, 0 },
3504 { "AIF", K_AIF, 0 },
3505 { "AELSE", K_AELSE, 0 },
3506 { "AENDI", K_AENDI, 0 },
3507 { "AREPEAT", K_AREPEAT, 0 },
3508 { "AENDR", K_AENDR, 0 },
3509 { "EXITM", K_EXITM, 0 },
3510 { "MACRO", K_MACRO, 0 },
3511 { "ENDM", K_ENDM, 0 },
3512 { "AWHILE", K_AWHILE, 0 },
3513 { "ALIGN", K_ALIGN, 0 },
3514 { "AENDW", K_AENDW, 0 },
3515 { "ALTERNATE", K_ALTERNATE, 0 },
3516 { "LOCAL", K_LOCAL, 0 },
3520 /* Look for a pseudo op on the line. If one's there then call
3524 process_pseudo_op (idx, line, acc)
3531 if (line->ptr[idx] == '.' || alternate)
3533 /* Scan forward and find pseudo name */
3539 if (line->ptr[idx] == '.')
3541 in = line->ptr + idx;
3546 while (idx < line->len && *e && ISFIRSTCHAR (*e))
3548 sb_add_char (acc, *e);
3553 ptr = hash_lookup (&keyword_hash_table, acc);
3558 /* This one causes lots of pain when trying to preprocess
3560 WARNING ((stderr, "Unrecognised pseudo op `%s'.\n", sb_name (acc)));
3564 if (ptr->value.i & LAB)
3565 { /* output the label */
3568 fprintf (outfile, "%s:\t", sb_name (&label));
3571 fprintf (outfile, "\t");
3574 if (ptr->value.i & PROCESS)
3576 /* Polish the rest of the line before handling the pseudo op */
3578 strip_comments(line);
3581 process_assigns (idx, line, acc);
3583 change_base (0, acc, line);
3588 switch (ptr->value.i)
3604 switch (ptr->value.i)
3616 ERROR ((stderr, "ORG command not allowed.\n"));
3622 do_data (idx, line, 1);
3625 do_data (idx, line, 2);
3628 do_data (idx, line, 4);
3631 do_data (idx, line, 0);
3634 do_datab (idx, line);
3637 do_sdata (idx, line, 0);
3640 do_sdatab (idx, line);
3643 do_sdata (idx, line, 'c');
3646 do_sdata (idx, line, 'z');
3649 do_assign (1, 0, line);
3655 do_arepeat (idx, line);
3661 do_awhile (idx, line);
3667 do_assign (0, idx, line);
3670 do_align (idx, line);
3673 do_res (idx, line, 0);
3676 do_res (idx, line, 's');
3679 do_include (idx, line);
3682 do_local (idx, line);
3685 do_macro (idx, line);
3691 do_res (idx, line, 'c');
3694 do_print (idx, line);
3697 do_form (idx, line);
3700 do_heading (idx, line);
3712 do_res (idx, line, 'z');
3720 do_assigna (idx, line);
3723 do_assignc (idx, line);
3739 /* Build the keyword hash table - put each keyword in the table twice,
3740 once upper and once lower case.*/
3747 for (i = 0; kinfo[i].name; i++)
3752 sb_add_string (&label, kinfo[i].name);
3754 hash_add_to_int_table (&keyword_hash_table, &label, kinfo[i].code);
3757 for (j = 0; kinfo[i].name[j]; j++)
3758 sb_add_char (&label, kinfo[i].name[j] - 'A' + 'a');
3759 hash_add_to_int_table (&keyword_hash_table, &label, kinfo[i].code);
3785 sb_add_char (&value, *string);
3788 exp_get_abs ("Invalid expression on command line.\n", 0, &value, &res);
3792 sb_add_char (&label, *string);
3797 ptr = hash_create (&vars, &label);
3798 free_old_entry (ptr);
3799 ptr->type = hash_integer;
3805 /* The list of long options. */
3806 static struct option long_options[] =
3808 { "alternate", no_argument, 0, 'a' },
3809 { "commentchar", required_argument, 0, 'c' },
3810 { "copysource", no_argument, 0, 's' },
3811 { "debug", no_argument, 0, 'd' },
3812 { "help", no_argument, 0, 'h' },
3813 { "output", required_argument, 0, 'o' },
3814 { "print", no_argument, 0, 'p' },
3815 { "unreasonable", no_argument, 0, 'u' },
3816 { "version", no_argument, 0, 'v' },
3817 { "define", required_argument, 0, 'd' },
3818 { NULL, no_argument, 0, 0 }
3821 /* Show a usage message and exit. */
3823 show_usage (file, status)
3829 [-a] [--alternate] enter alternate macro mode\n\
3830 [-c char] [--commentchar char] change the comment character from !\n\
3831 [-d] [--debug] print some debugging info\n\
3832 [-h] [--help] print this message\n\
3833 [-o out] [--output out] set the output file\n\
3834 [-p] [--print] print line numbers\n\
3835 [-s] [--copysource] copy source through as comments \n\
3836 [-u] [--unreasonable] allow unreasonable nesting\n\
3837 [-v] [--version] print the program version\n\
3838 [-Dname=value] create preprocessor variable called name, with value\n\
3839 [in-file]\n", program_name);
3843 /* Display a help message and exit. */
3847 printf ("%s: Gnu Assembler Macro Preprocessor\n",
3849 show_usage (stdout, 0);
3866 program_name = argv[0];
3867 xmalloc_set_program_name (program_name);
3869 hash_new_table (101, ¯o_table);
3870 hash_new_table (101, &keyword_hash_table);
3871 hash_new_table (101, &assign_hash_table);
3872 hash_new_table (101, &vars);
3877 while ((opt = getopt_long (argc, argv, "sdhavc:upo:D:", long_options,
3890 print_line_number = 1;
3893 comment_char = optarg[0];
3911 printf ("GNU %s version %s\n", program_name, program_version);
3917 show_usage (stderr, 1);
3924 outfile = fopen (out_name, "w");
3927 fprintf (stderr, "%s: Can't open output file `%s'.\n",
3928 program_name, out_name);
3940 /* Process all the input files */
3942 while (optind < argc)
3944 if (new_file (argv[optind]))
3950 fprintf (stderr, "%s: Can't open input file `%s'.\n",
3951 program_name, argv[optind]);