Debian Codegraph @

src/pseudoopcodes.c

raw symbols SCIP
1 // ACME - a crossassembler for producing 6502/65c02/65816/65ce02 code.
2 // Copyright (C) 1998-2025 Marco Baye
3 // Have a look at "acme.c" for further info
4 //
5 // pseudo opcode stuff
6 #include "pseudoopcodes.h"
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h> // for memcpy()
10 #include "config.h"
11 #include "cpu.h"
12 #include "alu.h"
13 #include "dynabuf.h"
14 #include "encoding.h"
15 #include "flow.h"
16 #include "input.h"
17 #include "macro.h"
18 #include "global.h"
19 #include "output.h"
20 #include "section.h"
21 #include "symbol.h"
22 #include "tree.h"
23 #include "typesystem.h"
24
25
26 // TODO - make new, keyword-based argument parser for !bin, !fill and !align
27 // helper function to support switching from positional arguments to keyword arguments
28 static boolean line_uses_keyword_arguments(void)
29 {
30 // to be on the safe side, return FALSE if dialect < 0.98!
31 // if (config.dialect < V0_98__PATHS_AND_SYMBOLCHANGE)
32 // return FALSE;
33 //
34 //
35 // read line to buffer, check if it begins with '[a-zA-Z]+='
36 // change input to RAM, return result
37 // ...and the caller needs to pass us some struct so it can change input back later on!
38 return FALSE;
39 }
40
41
42 // different ways to handle end-of-statement:
43 enum eos {
44 SKIP_REMAINDER, // skip remainder of line - (after errors)
45 ENSURE_EOS, // make sure there's nothing left in statement
46 PARSE_REMAINDER, // parse what's left
47 AT_EOS_ANYWAY // actually, same as PARSE_REMAINDER
48 };
49
50 // constants
51 static const char exception_want_IN_or_comma[] = "Expected ',' or IN keyword after loop variable.";
52
53
54 // helper function, just for old, deprecated, obsolete, stupid "realpc":
55 static void end_all_pseudopc(void)
56 {
57 while (pseudopc_isactive())
58 pseudopc_end();
59 }
60
61
62 // "configuration" pseudo opcodes (can be overridden by cli arguments):
63
64
65 // define default value for empty memory ("!initmem" pseudo opcode)
66 static enum eos po_initmem(void)
67 {
68 struct number intresult;
69
70 // ignore in later passes
71 if (pass.number != 1)
72 return SKIP_REMAINDER;
73
74 // the "--initmem" cli arg and earlier calls have priority
75 if (config.mem_init_value != NO_VALUE_GIVEN) {
76 throw_warning("Memory already initialised.");
77 return SKIP_REMAINDER;
78 }
79
80 // read value
81 // (allowing undefined values in future versions does not make sense,
82 // because all "configuration pseudo opcodes" should be skipped after
83 // first pass)
84 ALU_defined_int(&intresult);
85 if ((intresult.val.intval > 255) || (intresult.val.intval < -128))
86 throw_error(exception_number_out_of_8b_range);
87
88 // remember value
89 config.mem_init_value = intresult.val.intval & 0xff;
90
91 return ENSURE_EOS;
92 }
93
94 // select output file name and format ("!to" pseudo opcode)
95 static enum eos po_to(void)
96 {
97 enum outfile_format format;
98
99 // ignore in later passes
100 if (pass.number != 1)
101 return SKIP_REMAINDER;
102
103 // the "--outfile" cli arg and earlier calls have priority
104 if (config.output_filename) {
105 throw_warning("Output file name already chosen.");
106 return SKIP_REMAINDER;
107 }
108
109 // read filename to global dynamic buffer
110 // if no file name given, exit (complaining will have been done)
111 if (input_read_output_filename())
112 return SKIP_REMAINDER;
113
114 // get malloc'd copy of filename
115 config.output_filename = dynabuf_get_copy(GlobalDynaBuf);
116
117 // select output format
118 if (parser_accept_comma()) {
119 // parse output format name
120 // if no keyword given, give up
121 if (parser_read_and_lower_keyword() == 0)
122 return SKIP_REMAINDER; // "Expected keyword" error has already been thrown
123
124 format = outputformat_find();
125 if (format == OUTFILE_FORMAT_UNSPECIFIED) {
126 throw_error("Unknown output format.");
127 return SKIP_REMAINDER;
128 }
129 } else {
130 // no comma: complain unless user requested really old behaviour
131 if (config.dialect >= V0_86__DEPRECATE_REALPC) {
132 throw_warning("Used \"!to\" without file format indicator.");
133 } else {
134 // older versions did not complain
135 }
136 // default to cbm
137 format = OUTFILE_FORMAT_CBM;
138 }
139
140 // the "--format" cli arg has priority
141 if (config.outfile_format != OUTFILE_FORMAT_UNSPECIFIED) {
142 throw_warning("Output file format already chosen.");
143 } else {
144 config.outfile_format = format;
145 }
146
147 return ENSURE_EOS;
148 }
149
150 // set file name for symbol list
151 static enum eos po_symbollist(void)
152 {
153 // ignore in later passes
154 if (pass.number != 1)
155 return SKIP_REMAINDER;
156
157 // cli arg and earlier calls have priority
158 if (config.symbollist_filename) {
159 throw_warning("Symbol list file name already chosen.");
160 return SKIP_REMAINDER;
161 }
162
163 // read filename to global dynamic buffer
164 // if no file name given, exit (complaining will have been done)
165 if (input_read_output_filename())
166 return SKIP_REMAINDER;
167
168 // get malloc'd copy of filename
169 config.symbollist_filename = dynabuf_get_copy(GlobalDynaBuf);
170
171 return ENSURE_EOS;
172 }
173
174
175 // set start and end (can also be overwritten by cli args):
176
177
178 // use current outbuf index as "first byte of output file"
179 static enum eos po_outfilestart(void)
180 {
181 static int last_pass_number = -1;
182
183 if ((config.outfile_start != NO_VALUE_GIVEN)
184 || (last_pass_number == pass.number)) {
185 throw_pass1_warning("Start of output file already chosen.");
186 } else {
187 last_pass_number = pass.number;
188 outbuf_set_outfile_start();
189 }
190 return ENSURE_EOS;
191 }
192
193 // use current outbuf index as "end+1 of output file"
194 static enum eos po_outfilelimit(void)
195 {
196 static int last_pass_number = -1;
197
198 if ((config.outfile_limit != NO_VALUE_GIVEN)
199 || (last_pass_number == pass.number)) {
200 throw_pass1_warning("End of output file already chosen.");
201 } else {
202 last_pass_number = pass.number;
203 outbuf_set_outfile_limit();
204 }
205 return ENSURE_EOS;
206 }
207
208
209 // "real" pseudo opcodes:
210
211
212 // change output "encryption" ("!xor" pseudo opcode)
213 // (allows for block, so must be reentrant)
214 static enum eos po_xor(void)
215 {
216 char old_value;
217 intval_t change;
218
219 old_value = output_get_xor();
220 ALU_any_int(&change);
221 if ((change > 255) || (change < -128)) {
222 throw_error(exception_number_out_of_8b_range);
223 change = 0;
224 }
225 output_set_xor(old_value ^ change);
226 // if there's a block, parse that and then restore old value!
227 if (parse_optional_block())
228 output_set_xor(old_value);
229 return ENSURE_EOS;
230 }
231
232
233 // helper function for !8, !16, !24 and !32 pseudo opcodes
234 static enum eos iterate(void (*fn)(intval_t))
235 {
236 struct iter_context iter;
237 struct object object;
238
239 iter.fn = fn;
240 iter.accept_long_strings = FALSE;
241 iter.stringxor = 0;
242 do {
243 ALU_any_result(&object);
244 output_object(&object, &iter);
245 } while (parser_accept_comma());
246 return ENSURE_EOS;
247 }
248
249
250 // insert 8-bit values ("!8" / "!08" / "!by" / "!byte" pseudo opcode)
251 static enum eos po_byte(void)
252 {
253 return iterate(output_8);
254 }
255
256
257 // Insert 16-bit values ("!16" / "!wo" / "!word" pseudo opcode)
258 static enum eos po_16(void)
259 {
260 return iterate((cpu_current_type->flags & CPUFLAG_ISBIGENDIAN) ? output_be16 : output_le16);
261 }
262 // Insert 16-bit values big-endian ("!be16" pseudo opcode)
263 static enum eos po_be16(void)
264 {
265 return iterate(output_be16);
266 }
267 // Insert 16-bit values little-endian ("!le16" pseudo opcode)
268 static enum eos po_le16(void)
269 {
270 return iterate(output_le16);
271 }
272
273
274 // Insert 24-bit values ("!24" pseudo opcode)
275 static enum eos po_24(void)
276 {
277 return iterate((cpu_current_type->flags & CPUFLAG_ISBIGENDIAN) ? output_be24 : output_le24);
278 }
279 // Insert 24-bit values big-endian ("!be24" pseudo opcode)
280 static enum eos po_be24(void)
281 {
282 return iterate(output_be24);
283 }
284 // Insert 24-bit values little-endian ("!le24" pseudo opcode)
285 static enum eos po_le24(void)
286 {
287 return iterate(output_le24);
288 }
289
290
291 // Insert 32-bit values ("!32" pseudo opcode)
292 static enum eos po_32(void)
293 {
294 return iterate((cpu_current_type->flags & CPUFLAG_ISBIGENDIAN) ? output_be32 : output_le32);
295 }
296 // Insert 32-bit values big-endian ("!be32" pseudo opcode)
297 static enum eos po_be32(void)
298 {
299 return iterate(output_be32);
300 }
301 // Insert 32-bit values little-endian ("!le32" pseudo opcode)
302 static enum eos po_le32(void)
303 {
304 return iterate(output_le32);
305 }
306
307
308 // Insert bytes given as pairs of hex digits (helper for source code generators)
309 static enum eos po_hex(void) // now GotByte = illegal char
310 {
311 int digits = 0;
312 unsigned char byte = 0;
313
314 for (;;) {
315 if (digits == 2) {
316 output_byte(byte);
317 digits = 0;
318 byte = 0;
319 }
320 if (GotByte >= '0' && GotByte <= '9') {
321 byte = (byte << 4) | (GotByte - '0');
322 ++digits;
323 GetByte();
324 continue;
325 }
326 if (GotByte >= 'a' && GotByte <= 'f') {
327 byte = (byte << 4) | (GotByte - 'a' + 10);
328 ++digits;
329 GetByte();
330 continue;
331 }
332 if (GotByte >= 'A' && GotByte <= 'F') {
333 byte = (byte << 4) | (GotByte - 'A' + 10);
334 ++digits;
335 GetByte();
336 continue;
337 }
338 // if we're here, the current character is not a hex digit,
339 // which is only allowed outside of pairs:
340 if (digits == 1) {
341 throw_error("Hex digits are not given in pairs.");
342 return SKIP_REMAINDER; // error exit
343 }
344 switch (GotByte) {
345 case ' ':
346 case '\t':
347 GetByte(); // spaces and tabs are ignored (maybe add commas, too?)
348 continue;
349 case CHAR_EOS:
350 return AT_EOS_ANYWAY; // normal exit
351 default:
352 throw_error(exception_syntax); // FIXME - include character in error message!
353 return SKIP_REMAINDER; // error exit
354 }
355 }
356 }
357
358
359 // "!cbm" pseudo opcode (now obsolete)
360 static enum eos po_cbm(void)
361 {
362 if (config.dialect >= V0_94_8__DISABLED_OBSOLETE) {
363 throw_error("\"!cbm\" is obsolete; use \"!ct pet\" instead.");
364 } else {
365 encoder_current = &encoder_pet;
366 throw_finalpass_warning("\"!cbm\" is deprecated; use \"!ct pet\" instead.");
367 }
368 return ENSURE_EOS;
369 }
370
371 // read encoding table from file
372 // (allows for block, so must be reentrant)
373 static enum eos use_encoding_from_file(void)
374 {
375 struct filespecflags flags;
376 FILE *stream;
377 unsigned char local_table[256],
378 *buffered_table;
379 const struct encoder *buffered_encoder;
380
381 // read file name and convert from UNIX style to platform style
382 if (input_read_input_filename(&flags))
383 return SKIP_REMAINDER; // if missing or unterminated, give up
384
385 // read from file
386 stream = includepaths_open_ro(&flags);
387 if (stream) {
388 // try to load encoding table from given file
389 if (fread(local_table, sizeof(char), 256, stream) != 256)
390 throw_error("Conversion table incomplete.");
391 fclose(stream);
392 }
393
394 // now switch encoding
395 buffered_encoder = encoder_current;
396 encoder_current = &encoder_file;
397 buffered_table = encoding_loaded_table;
398 encoding_loaded_table = local_table;
399 // if there's a block, parse that and then restore old values
400 if (parse_optional_block()) {
401 encoder_current = buffered_encoder;
402 } else {
403 // if there's *no* block, the table must be used from now on.
404 // copy the local table to the "outer" table
405 memcpy(buffered_table, local_table, 256);
406 }
407 // re-activate "outer" table (it might have been changed by memcpy())
408 encoding_loaded_table = buffered_table;
409 return ENSURE_EOS;
410 }
411
412 // use one of the pre-defined encodings (raw, pet, scr)
413 // (allows for block, so must be reentrant)
414 static enum eos use_predefined_encoding(const struct encoder *new_encoder)
415 {
416 unsigned char local_table[256],
417 *buffered_table;
418 const struct encoder *buffered_encoder;
419
420 // switch encoding
421 buffered_encoder = encoder_current;
422 encoder_current = new_encoder;
423 buffered_table = encoding_loaded_table;
424 encoding_loaded_table = local_table;
425 // if there's a block, parse that and then restore old values
426 if (parse_optional_block())
427 encoder_current = buffered_encoder;
428 // re-activate "outer" table
429 encoding_loaded_table = buffered_table;
430 return ENSURE_EOS;
431 }
432
433 // set current encoding ("!convtab" pseudo opcode)
434 // (allows for block, so must be reentrant)
435 static enum eos po_convtab(void)
436 {
437 // is file name given old-style, without "file" keyword?
438 if ((GotByte == '<') || (GotByte == '"'))
439 return use_encoding_from_file();
440
441 // expect keyword: either one of the pre-defined encodings or
442 // "file" with a filename argument:
443 if (parser_read_and_lower_keyword() == 0)
444 return SKIP_REMAINDER; // "Expected keyword" error has already been thrown
445
446 // now check for known keywords:
447 if (strcmp(GlobalDynaBuf->buffer, "pet") == 0)
448 return use_predefined_encoding(&encoder_pet);
449
450 if (strcmp(GlobalDynaBuf->buffer, "raw") == 0)
451 return use_predefined_encoding(&encoder_raw);
452
453 if (strcmp(GlobalDynaBuf->buffer, "scr") == 0)
454 return use_predefined_encoding(&encoder_scr);
455
456 if (strcmp(GlobalDynaBuf->buffer, "file") == 0) {
457 SKIPSPACE();
458 if (!parser_expect('=')) {
459 return SKIP_REMAINDER;
460 }
461 return use_encoding_from_file();
462 }
463
464 throw_error("Unknown encoding.");
465 return use_predefined_encoding(encoder_current); // keep going
466 }
467 // insert string(s)
468 static enum eos encode_string(const struct encoder *inner_encoder, unsigned char xor)
469 {
470 const struct encoder *outer_encoder = encoder_current; // buffer encoder
471 struct iter_context iter;
472 struct object object;
473 int offset;
474
475 iter.fn = output_8;
476 iter.accept_long_strings = TRUE;
477 iter.stringxor = xor;
478 // make given encoder the current one (for ALU-parsed values of character codes)
479 encoder_current = inner_encoder;
480 // algo depends on dialect:
481 if (config.dialect >= V0_97__BACKSLASH_ESCAPING) {
482 // since v0.97 the expression parser supports strings, so we pass all args to it:
483 do {
484 ALU_any_result(&object);
485 output_object(&object, &iter);
486 } while (parser_accept_comma());
487 } else {
488 // in older versions the expression parser complained about
489 // strings longer than one character, so we assumed everything
490 // starting with a doublequote is a string literal and handled
491 // those separately:
492 do {
493 if (GotByte == '"') {
494 // string literal
495 if (input_read_string_literal('"'))
496 break; // unterminated
497 // send characters
498 for (offset = 0; offset < GlobalDynaBuf->size; ++offset)
499 output_8(xor ^ encoding_encode_char(GLOBALDYNABUF_CURRENT[offset]));
500 } else {
501 // everything else is passed to expression parser:
502 ALU_any_result(&object);
503 output_object(&object, &iter);
504 }
505 } while (parser_accept_comma());
506 }
507 // reactivate buffered encoder:
508 encoder_current = outer_encoder;
509 return ENSURE_EOS;
510 }
511 // insert text string (default format)
512 static enum eos po_text(void)
513 {
514 return encode_string(encoder_current, 0);
515 }
516 // insert raw string
517 static enum eos po_raw(void)
518 {
519 return encode_string(&encoder_raw, 0);
520 }
521 // insert PetSCII string
522 static enum eos po_pet(void)
523 {
524 return encode_string(&encoder_pet, 0);
525 }
526 // insert screencode string
527 static enum eos po_scr(void)
528 {
529 return encode_string(&encoder_scr, 0);
530 }
531 // insert screencode string, XOR'd
532 static enum eos po_scrxor(void)
533 {
534 intval_t xor;
535
536 ALU_any_int(&xor);
537 if (parser_expect(',')) {
538 return encode_string(&encoder_scr, xor);
539 }
540 return SKIP_REMAINDER;
541 }
542
543 // "backend" function for "!binary"
544 static void include_file(FILE *stream, intval_t size, intval_t offset)
545 {
546 int byte,
547 amount;
548
549 // check whether including is a waste of time
550 // FIXME - future changes ("several-projects-at-once")
551 // may be incompatible with this!
552 if ((size >= 0) && (!pass.flags.generate_output)) {
553 // no need to read actual data from file, so use zeroes instead.
554 // do not call output_skip()! we want this area to be included
555 // when calculating outbuf size, even if at start/end!
556 while (size != 0) {
557 output_byte(0);
558 --size;
559 }
560 return;
561 }
562
563 // really insert file
564 fseek(stream, offset, SEEK_SET); // set read pointer
565 // read "size" bytes. if size is "NO_VALUE_GIVEN" (which is -1),
566 // decrementing it won't give zero, so we'll read until EOF,
567 // which is exactly what we want if no size was given:
568 while (size != 0) {
569 byte = getc(stream);
570 if (byte == EOF)
571 break;
572 output_byte(byte);
573 --size;
574 }
575 // if more should have been read, warn and add padding
576 if (size > 0) {
577 throw_warning("Padding with zeroes.");
578 do {
579 output_byte(0);
580 } while (--size);
581 }
582 // if verbose, produce some output
583 if (config.process_verbosity >= 2) {
584 amount = output_get_statement_size();
585 printf("Loaded %d (0x%04x) bytes from file offset %d (0x%04x).\n",
586 amount, amount, offset, offset);
587 }
588 }
589 // include binary file ("!binary" pseudo opcode)
590 static enum eos po_binary(void)
591 {
592 struct filespecflags flags;
593 FILE *stream;
594 struct number arg;
595 intval_t size = NO_VALUE_GIVEN, // means "until EOF"
596 skip = 0;
597
598 // read file name and convert from UNIX style to platform style
599 if (input_read_input_filename(&flags))
600 return SKIP_REMAINDER; // if missing or unterminated, give up
601
602 // try to open file
603 stream = includepaths_open_ro(&flags);
604 if (stream == NULL)
605 return SKIP_REMAINDER;
606
607 // any more arguments?
608 if (parser_accept_comma()) {
609 if (line_uses_keyword_arguments()) {
610 // read keyword arguments
611 // TODO!
612 BUG("KeywordArgsNotYet", 0);
613 } else {
614 // read old-style, positional arguments:
615 if ((GotByte != ',') && (GotByte != CHAR_EOS)) {
616 // first arg is "size"
617 ALU_defined_int(&arg);
618 size = arg.val.intval;
619 // CAUTION: do not move this check elsewhere,
620 // because the default value -1 is ok!
621 if (size < 0)
622 throw_serious_error(exception_negative_size);
623 }
624 // more?
625 if (parser_accept_comma()) {
626 // second arg is "skip"
627 if (GotByte != CHAR_EOS) {
628 // then parse it
629 ALU_defined_int(&arg);
630 skip = arg.val.intval;
631 }
632 }
633 }
634 }
635 // FIXME - add check for "skip" and complain if negative!
636 include_file(stream, size, skip);
637 fclose(stream);
638 return ENSURE_EOS;
639 }
640
641
642 // reserve space by sending bytes of given value ("!fi" / "!fill" pseudo opcode)
643 static enum eos po_fill(void)
644 {
645 struct number sizeresult;
646 intval_t fill = FILLVALUE_FILL;
647
648 if (line_uses_keyword_arguments()) {
649 // read keyword arguments
650 // TODO!
651 BUG("KeywordArgsNotYet", 1);
652 } else {
653 // read old-style, positional arguments:
654 ALU_defined_int(&sizeresult); // FIXME - forbid addresses!
655 if (parser_accept_comma())
656 ALU_any_int(&fill); // FIXME - forbid addresses!
657 }
658
659 while (sizeresult.val.intval--)
660 output_8(fill);
661 return ENSURE_EOS;
662 }
663
664
665 // skip over some bytes in output without starting a new segment.
666 // in contrast to "*=*+AMOUNT", "!skip AMOUNT" does not start a new segment.
667 static enum eos po_skip(void) // now GotByte = illegal char
668 {
669 struct number amount;
670
671 ALU_defined_int(&amount); // FIXME - forbid addresses!
672 if (amount.val.intval < 0)
673 throw_serious_error(exception_negative_size); // TODO - allow this?
674 else
675 output_skip(amount.val.intval);
676 return ENSURE_EOS;
677 }
678
679
680 // insert byte until PC fits condition
681 // TODO:
682 // now: "!align ANDVALUE, EQUALVALUE [,FILLVALUE]"
683 // in future: use keyword arguments so "!align fillvalue=17, blocksize=64" is
684 // possible (where blocksize must be a power of two)
685 static enum eos po_align(void)
686 {
687 struct number arg;
688 intval_t andresult,
689 equalresult,
690 fill = cpu_current_type->default_align_value;
691 struct number pc;
692
693 if (line_uses_keyword_arguments()) {
694 // read keyword arguments
695 // TODO!
696 BUG("KeywordArgsNotYet", 2);
697 } else {
698 // read old-style, positional arguments:
699 ALU_defined_int(&arg); // FIXME - forbid addresses!
700 andresult = arg.val.intval;
701 if (parser_expect(',')) {
702 // fn already does everything for us
703 }
704 ALU_defined_int(&arg); // ...allow addresses (unlikely, but possible)
705 equalresult = arg.val.intval;
706 if (parser_accept_comma())
707 ALU_any_int(&fill);
708 }
709
710 // make sure PC is defined
711 programcounter_read_pc(&pc);
712 if (pc.ntype == NUMTYPE_UNDEFINED) {
713 throw_error(exception_pc_undefined);
714 return SKIP_REMAINDER;
715 }
716
717 while ((pc.val.intval++ & andresult) != equalresult)
718 output_8(fill);
719 return ENSURE_EOS;
720 }
721
722
723 // not using a block is no longer allowed
724 static void old_offset_assembly(void)
725 {
726 if (config.dialect >= V0_94_8__DISABLED_OBSOLETE) {
727 // now it's obsolete
728 throw_error("\"!pseudopc/!realpc\" are obsolete; use \"!pseudopc {}\" instead."); // FIXME - amend msg, tell user how to use old behaviour!
729 } else if (config.dialect >= V0_86__DEPRECATE_REALPC) {
730 // earlier it was deprecated
731 throw_finalpass_warning("\"!pseudopc/!realpc\" are deprecated; use \"!pseudopc {}\" instead.");
732 } else {
733 // really old versions allowed it
734 }
735 }
736
737 // start offset assembly
738 // (allows for block, so must be reentrant)
739 static enum eos po_pseudopc(void)
740 {
741 struct number new_pc;
742
743 // get new value
744 ALU_defined_int(&new_pc); // complaining about non-addresses would be logical, but annoying
745 /* TODO - add this. check if code can be shared with "*="!
746 // check for modifiers
747 while (parser_accept_comma()) {
748 // parse modifier. if no keyword given, give up
749 if (parser_read_and_lower_keyword() == 0)
750 return SKIP_REMAINDER; // "Expected keyword" error has already been thrown
751
752 if (strcmp(GlobalDynaBuf->buffer, "limit") == 0) {
753 skip '='
754 read memory limit
755 } else if (strcmp(GlobalDynaBuf->buffer, "name") == 0) {
756 skip '='
757 read segment name (quoted string!)
758 } else {
759 throw_error("Unknown !pseudopc segment modifier.");
760 return SKIP_REMAINDER;
761 }
762 }
763 */
764 if (new_pc.val.intval < 0) {
765 throw_error("Program counter cannot be negative.");
766 new_pc.val.intval = cpu_current_type->dummy_pc;
767 }
768 pseudopc_start(&new_pc);
769 // if there's a block, parse that and then restore old value!
770 if (parse_optional_block()) {
771 // restore old state
772 if (pseudopc_isactive()) {
773 pseudopc_end();
774 } else {
775 // calling pseudopc_end() here would create a segfault.
776 // the only way this point can be reached is when the user
777 // a) asked for an older dialect where "*=" disabled "!pseudopc"
778 // and
779 // b) did exactly that in the source.
780 //
781 // ...*or* it is a bug! maybe set a flag in the case above which
782 // can then be checked here, and if it isn't set, call BUG()?
783 }
784 } else {
785 old_offset_assembly();
786 }
787 return ENSURE_EOS;
788 }
789
790
791 // "!realpc" pseudo opcode (now obsolete)
792 static enum eos po_realpc(void)
793 {
794 old_offset_assembly();
795 end_all_pseudopc(); // restore outermost state
796 return ENSURE_EOS;
797 }
798
799
800 // select CPU ("!cpu" pseudo opcode)
801 // (allows for block, so must be reentrant)
802 static enum eos po_cpu(void)
803 {
804 const struct cpu_type *cpu_buffer = cpu_current_type; // remember
805 const struct cpu_type *new_cpu_type;
806
807 if (parser_read_and_lower_keyword()) {
808 new_cpu_type = cputype_find();
809 if (new_cpu_type)
810 cpu_current_type = new_cpu_type; // activate new cpu type
811 else
812 throw_error("Unknown processor.");
813 }
814 // if there's a block, parse that and then restore old value
815 if (parse_optional_block())
816 cpu_current_type = cpu_buffer;
817 return ENSURE_EOS;
818 }
819
820
821 // set register length, block-wise if needed.
822 // (allows for block, so must be reentrant)
823 static enum eos set_register_length(boolean *var, boolean make_long)
824 {
825 boolean long_before = *var;
826
827 // set new register length (or complain - whichever is more fitting)
828 vcpu_check_and_set_reg_length(var, make_long);
829 // if there's a block, parse that and then restore old value!
830 if (parse_optional_block())
831 vcpu_check_and_set_reg_length(var, long_before); // restore old length
832 return ENSURE_EOS;
833 }
834 // switch to long accumulator ("!al" pseudo opcode)
835 static enum eos po_al(void)
836 {
837 return set_register_length(&cpu_a_is_long, TRUE);
838 }
839 // switch to short accumulator ("!as" pseudo opcode)
840 static enum eos po_as(void)
841 {
842 return set_register_length(&cpu_a_is_long, FALSE);
843 }
844 // switch to long index registers ("!rl" pseudo opcode)
845 static enum eos po_rl(void)
846 {
847 return set_register_length(&cpu_xy_are_long, TRUE);
848 }
849 // switch to short index registers ("!rs" pseudo opcode)
850 static enum eos po_rs(void)
851 {
852 return set_register_length(&cpu_xy_are_long, FALSE);
853 }
854
855
856 #if 0
857 // enumerate constants ("!enum")
858 static enum eos po_enum(void) // now GotByte = illegal char
859 {
860 struct number step;
861
862 step.val.intval = 1;
863 ALU_defined_int(&step);
864 throw_serious_error("Not yet"); // FIXME
865 return ENSURE_EOS;
866 }
867 #endif
868
869
870 // (re)set symbol
871 static enum eos po_set(void) // now GotByte = illegal char
872 {
873 scope_t scope;
874 int force_bit;
875
876 if (input_read_scope_and_symbol_name(&scope)) // skips spaces before
877 return SKIP_REMAINDER; // zero length
878
879 force_bit = parser_get_force_bit(); // skips spaces after
880 if (!parser_expect('='))
881 return SKIP_REMAINDER;
882
883 // TODO: in versions before 0.97, force bit handling was broken in both
884 // "!set" and "!for":
885 // trying to change a force bit raised an error (which is correct), but
886 // in any case, ALL FORCE BITS WERE CLEARED in symbol. only cases like
887 // !set N=N+1 worked, because the force bit was taken from result.
888 // maybe support this behaviour via --dialect? I'd rather not...
889 parse_assignment(scope, force_bit, POWER_CHANGE_VALUE | POWER_CHANGE_OBJTYPE);
890 return ENSURE_EOS;
891 }
892
893
894 // switch to new zone ("!zone" or "!zn"). has to be re-entrant.
895 // (allows for block, so must be reentrant)
896 static enum eos po_zone(void)
897 {
898 struct section entry_values; // buffer for outer zone
899 char *new_title;
900 int allocated;
901
902 // remember everything about current structure
903 entry_values = *section_now;
904 // set default values in case there is no valid title
905 new_title = s_untitled;
906 allocated = FALSE;
907 // check whether a zone title is given. if yes and it can be read,
908 // get copy, remember pointer and remember to free it later on.
909 if (BYTE_CONTINUES_KEYWORD(GotByte)) {
910 // because we know of one character for sure,
911 // there's no need to check the return value.
912 parser_read_keyword();
913 new_title = dynabuf_get_copy(GlobalDynaBuf);
914 allocated = TRUE;
915 }
916 // setup new section
917 // section type is "subzone", just in case a block follows
918 section_new(section_now, "Subzone", new_title, allocated);
919 if (parse_optional_block()) {
920 // block has been parsed, so it was a SUBzone.
921 section_finalize(section_now); // end inner zone
922 *section_now = entry_values; // restore entry values
923 } else {
924 // no block found, so it's a normal zone change
925 section_finalize(&entry_values); // end outer zone
926 section_now->type = "Zone"; // fix type
927 }
928 return ENSURE_EOS;
929 }
930
931 // "!subzone" or "!sz" pseudo opcode (now obsolete)
932 static enum eos po_subzone(void)
933 {
934 if (config.dialect >= V0_94_8__DISABLED_OBSOLETE) {
935 throw_error("\"!subzone {}\" is obsolete; use \"!zone {}\" instead.");
936 } else {
937 throw_finalpass_warning("\"!subzone {}\" is deprecated; use \"!zone {}\" instead.");
938 }
939 // call "!zone" instead
940 return po_zone();
941 }
942
943 // include source file ("!source" or "!src"). has to be re-entrant.
944 static enum eos po_source(void) // now GotByte = illegal char
945 {
946 struct filespecflags flags;
947 FILE *stream;
948 const char *eternal_plat_filename;
949
950 // read file name and convert from UNIX style to platform style
951 if (input_read_input_filename(&flags))
952 return SKIP_REMAINDER; // if missing or unterminated, give up
953
954 // enter new nesting level
955 // quit program if recursion too deep
956 if (--sanity.source_recursions_left < 0)
957 throw_serious_error("Too deeply nested. Recursive \"!source\"?");
958 // if file could be opened, parse it. otherwise, complain
959 stream = includepaths_open_ro(&flags);
960 if (stream) {
961 eternal_plat_filename = dynabuf_get_copy(GlobalDynaBuf);
962 parse_source_code_file(stream, eternal_plat_filename);
963 fclose(stream);
964 }
965 // leave nesting level
966 ++sanity.source_recursions_left;
967 return ENSURE_EOS;
968 }
969
970 // if/ifdef/ifndef/else
971 enum ifmode {
972 IFMODE_IF, // parse expression, then block
973 IFMODE_IFDEF, // check symbol, then parse block or line
974 IFMODE_IFNDEF, // check symbol, then parse block or line
975 IFMODE_ELSE // unconditional last block
976 };
977 // has to be re-entrant
978 static enum eos ifelse(enum ifmode mode)
979 {
980 boolean skip_the_rest = FALSE; // once a block gets executed, this becomes TRUE, so all others will be skipped even if condition met
981 boolean condition_met; // condition result for next block
982 struct number ifresult;
983
984 for (;;) {
985 // check condition according to mode
986 switch (mode) {
987 case IFMODE_IF:
988 if (skip_the_rest) {
989 // parsing the condition might have side effects
990 // we do not want, so just skip it:
991 input_read_statement(CHAR_SOB);
992 //condition_met = does not matter because of "skip_the_rest"
993 } else {
994 // we need to parse the condition, and we need a
995 // defined result to know how to go on:
996 ALU_defined_int(&ifresult);
997 condition_met = !!ifresult.val.intval;
998 }
999 if (GotByte != CHAR_SOB)
1000 throw_serious_error(exception_no_left_brace);
1001 break;
1002 case IFMODE_IFDEF:
1003 condition_met = check_ifdef_condition();
1004 break;
1005 case IFMODE_IFNDEF:
1006 condition_met = !check_ifdef_condition();
1007 break;
1008 case IFMODE_ELSE:
1009 condition_met = TRUE;
1010 break;
1011 default:
1012 BUG("IllegalIfMode", mode);
1013 condition_met = TRUE; // inhibit compiler warning ;)
1014 }
1015 SKIPSPACE();
1016 // execute this block?
1017 if (condition_met && !skip_the_rest) {
1018 skip_the_rest = TRUE; // we execute this block, so skip all others
1019 if (GotByte == CHAR_SOB) {
1020 parse_until_eob_or_eof(); // parse block
1021 // if block isn't correctly terminated, complain and exit
1022 if (GotByte != CHAR_EOB)
1023 throw_serious_error(exception_no_right_brace);
1024 } else {
1025 return PARSE_REMAINDER; // parse line (only for ifdef/ifndef)
1026 }
1027 } else {
1028 if (GotByte == CHAR_SOB) {
1029 input_block_skip(); // skip block
1030 } else {
1031 return SKIP_REMAINDER; // skip line (only for ifdef/ifndef)
1032 }
1033 }
1034 // now GotByte = '}'
1035 NEXTANDSKIPSPACE();
1036 // after ELSE {} it's all over. it must be.
1037 if (mode == IFMODE_ELSE) {
1038 // we could just return ENSURE_EOS, but checking here allows for better error message
1039 if (GotByte != CHAR_EOS)
1040 throw_error("Expected end-of-statement after ELSE block.");
1041 return SKIP_REMAINDER; // normal exit after ELSE {...}
1042 }
1043
1044 // anything more?
1045 if (GotByte == CHAR_EOS)
1046 return AT_EOS_ANYWAY; // normal exit if there is no ELSE {...} block
1047
1048 // read keyword (expected to be "elif" or "else")
1049 if (parser_read_and_lower_keyword() == 0)
1050 return SKIP_REMAINDER; // "Expected keyword" error -> ignore rest of line
1051
1052 // if it's "elif", we can just do another iteration:
1053 if (strcmp(GlobalDynaBuf->buffer, "elif") == 0) {
1054 mode = IFMODE_IF;
1055 continue;
1056 }
1057
1058 // make sure it's "else"
1059 if (strcmp(GlobalDynaBuf->buffer, "else")) {
1060 throw_error("Expected end-of-statement or ELIF/ELSE keyword after '}'.");
1061 return SKIP_REMAINDER; // an error has been reported, so ignore rest of line
1062 }
1063 // anything more?
1064 SKIPSPACE();
1065 if (GotByte == CHAR_SOB) {
1066 // ELSE {...} -> one last round
1067 mode = IFMODE_ELSE;
1068 continue;
1069 }
1070
1071 // read keyword after else (expected to be if/ifdef/ifndef)
1072 if (parser_read_and_lower_keyword() == 0)
1073 return SKIP_REMAINDER; // "Expected keyword" error -> ignore rest of line
1074
1075 // which one is it?
1076 if (strcmp(GlobalDynaBuf->buffer, "if") == 0) {
1077 mode = IFMODE_IF;
1078 } else if (strcmp(GlobalDynaBuf->buffer, "ifdef") == 0) {
1079 mode = IFMODE_IFDEF;
1080 } else if (strcmp(GlobalDynaBuf->buffer, "ifndef") == 0) {
1081 mode = IFMODE_IFNDEF;
1082 } else {
1083 throw_error("Expected '{' or IF/IFDEF/IFNDEF keyword after ELSE keyword.");
1084 return SKIP_REMAINDER; // an error has been reported, so ignore rest of line
1085 }
1086 }
1087 }
1088
1089 // conditional assembly ("!if"). has to be re-entrant.
1090 static enum eos po_if(void) // now GotByte = illegal char
1091 {
1092 return ifelse(IFMODE_IF);
1093 }
1094
1095
1096 // conditional assembly ("!ifdef"). has to be re-entrant.
1097 static enum eos po_ifdef(void) // now GotByte = illegal char
1098 {
1099 return ifelse(IFMODE_IFDEF);
1100 }
1101
1102
1103 // conditional assembly ("!ifndef"). has to be re-entrant.
1104 static enum eos po_ifndef(void) // now GotByte = illegal char
1105 {
1106 return ifelse(IFMODE_IFNDEF);
1107 }
1108
1109
1110 // looping assembly ("!for"). has to be re-entrant.
1111 // old counter syntax: !for VAR, END { BLOCK } VAR counts from 1 to END
1112 // new counter syntax: !for VAR, START, END { BLOCK } VAR counts from START to END
1113 // iterating syntax: !for VAR in ITERABLE { BLOCK } VAR iterates over string/list contents
1114 static enum eos po_for(void) // now GotByte = illegal char
1115 {
1116 scope_t scope;
1117 bits force_bit;
1118 struct for_loop loop;
1119 struct number intresult;
1120
1121 if (input_read_scope_and_symbol_name(&scope)) // skips spaces before
1122 return SKIP_REMAINDER; // zero length
1123
1124 // now GotByte = illegal char
1125 force_bit = parser_get_force_bit(); // skips spaces after
1126 loop.symbol = symbol_find(scope); // if not number, error will be reported on first assignment
1127 if (parser_accept_comma()) {
1128 // counter syntax (old or new)
1129 loop.u.counter.force_bit = force_bit;
1130 ALU_defined_int(&intresult); // read first argument
1131 loop.u.counter.addr_refs = intresult.addr_refs;
1132 if (parser_accept_comma()) {
1133 // new counter syntax
1134 loop.algorithm = FORALGO_NEWCOUNT;
1135 if (config.dialect >= V0_94_12__NEW_FOR_SYNTAX) {
1136 // version 0.94.12 introduced the new syntax,
1137 } else {
1138 // so if user chooses an older dialect, warn about the new syntax:
1139 throw_finalpass_warning("Found new \"!for\" syntax.");
1140 }
1141 loop.u.counter.first = intresult.val.intval; // use first argument
1142 ALU_defined_int(&intresult); // read second argument
1143 // compare addr_ref counts and complain if not equal!
1144 if (config.warn_on_type_mismatch
1145 && (intresult.addr_refs != loop.u.counter.addr_refs)) {
1146 throw_finalpass_warning("Wrong type for loop's END value - must match type of START value.");
1147 }
1148 // setup direction and total
1149 if (loop.u.counter.first <= intresult.val.intval) {
1150 // count up
1151 loop.iterations_left = 1 + intresult.val.intval - loop.u.counter.first;
1152 loop.u.counter.increment = 1;
1153 } else {
1154 // count down
1155 loop.iterations_left = 1 + loop.u.counter.first - intresult.val.intval;
1156 loop.u.counter.increment = -1;
1157 }
1158 } else {
1159 // old counter syntax
1160 loop.algorithm = FORALGO_OLDCOUNT;
1161 if (config.dialect >= V0_94_12__NEW_FOR_SYNTAX) {
1162 // version 0.94.12 introduced the new syntax, so warn when encountering the old one:
1163 throw_finalpass_warning("Found old \"!for\" syntax.");
1164 }
1165 if (intresult.val.intval < 0) {
1166 throw_serious_error("Loop count is negative.");
1167 }
1168 // count up
1169 loop.u.counter.first = 1;
1170 loop.iterations_left = intresult.val.intval; // use given argument
1171 loop.u.counter.increment = 1;
1172 }
1173 } else {
1174 // iterator syntax
1175 loop.algorithm = FORALGO_ITERATE;
1176 // check for "in" keyword
1177 if ((GotByte != 'i') && (GotByte != 'I')) {
1178 throw_error(exception_want_IN_or_comma);
1179 return SKIP_REMAINDER; // FIXME - this ignores '{' and will then complain about '}'
1180 }
1181 /* checking for the first character explicitly here looks dumb, but actually
1182 serves a purpose: we're here because the check for comma failed, but maybe that
1183 was just a typo. if the current byte is '.' or '-' or whatever, then trying to
1184 read a keyword will result in "Expected keyword" - which is confusing for the
1185 user if they did not even want to put a keyword there.
1186 so if the current byte is not the start of "in" we just throw a syntax error.
1187 knowing there is an "i" also makes sure that parser_read_and_lower_keyword()
1188 does not fail. */
1189 parser_read_and_lower_keyword();
1190 if (strcmp(GlobalDynaBuf->buffer, "in") != 0) {
1191 throw_error(exception_want_IN_or_comma);
1192 return SKIP_REMAINDER; // FIXME - this ignores '{' and will then complain about '}'
1193 }
1194 if (force_bit) {
1195 throw_error("Force bits can only be given to counters, not when iterating over string/list contents.");
1196 return SKIP_REMAINDER; // FIXME - this ignores '{' and will then complain about '}'
1197 }
1198 ALU_any_result(&loop.u.iter.obj); // get iterable
1199 loop.iterations_left = loop.u.iter.obj.type->length(&loop.u.iter.obj);
1200 if (loop.iterations_left < 0) {
1201 throw_error("Given object is not iterable.");
1202 return SKIP_REMAINDER; // FIXME - this ignores '{' and will then complain about '}'
1203 }
1204 }
1205
1206 if (GotByte != CHAR_SOB)
1207 throw_serious_error(exception_no_left_brace);
1208
1209 input_block_getcopy(&loop.block); // "body" must be freed afterward...
1210 flow_forloop(&loop);
1211 free(loop.block.body); // ...so free it
1212
1213 // GotByte is '}'
1214 GetByte(); // fetch next byte
1215 return ENSURE_EOS;
1216 }
1217
1218
1219 // looping assembly ("!do"). has to be re-entrant.
1220 static enum eos po_do(void) // now GotByte = illegal char
1221 {
1222 struct do_while loop;
1223
1224 // read head condition to buffer
1225 SKIPSPACE();
1226 flow_store_doloop_condition(&loop.head_cond, CHAR_SOB); // must be freed!
1227 if (GotByte != CHAR_SOB)
1228 throw_serious_error(exception_no_left_brace);
1229
1230 input_block_getcopy(&loop.block); // "body" must be freed!
1231 // now GotByte = '}'
1232 NEXTANDSKIPSPACE(); // now GotByte = first non-blank char after block
1233 // read tail condition to buffer
1234 flow_store_doloop_condition(&loop.tail_cond, CHAR_EOS); // must be freed!
1235 // now GotByte = CHAR_EOS
1236 flow_do_while(&loop);
1237 // free memory
1238 free(loop.tail_cond.block.body);
1239 free(loop.block.body);
1240 free(loop.head_cond.block.body);
1241 return AT_EOS_ANYWAY;
1242 }
1243
1244
1245 // looping assembly ("!while", alternative for people used to c-style loops). has to be re-entrant.
1246 static enum eos po_while(void) // now GotByte = illegal char
1247 {
1248 struct do_while loop;
1249
1250 // read condition to buffer
1251 SKIPSPACE();
1252 flow_store_while_condition(&loop.head_cond); // "body" must be freed!
1253 if (GotByte != CHAR_SOB)
1254 throw_serious_error(exception_no_left_brace);
1255 // read block
1256 input_block_getcopy(&loop.block); // "body" must be freed!
1257 // clear tail condition
1258 loop.tail_cond.block.body = NULL;
1259 flow_do_while(&loop);
1260 // free memory
1261 free(loop.block.body);
1262 free(loop.head_cond.block.body);
1263 GetByte(); // fetch next byte (last byte read was '}')
1264 return ENSURE_EOS;
1265 }
1266
1267
1268 // macro definition ("!macro").
1269 static enum eos po_macro(void) // now GotByte = illegal char
1270 {
1271 // in first pass, parse. In all other passes, skip.
1272 if (pass.number == 1) {
1273 macro_parse_definition(); // now GotByte = '}'
1274 } else {
1275 // skip until CHAR_SOB ('{') is found.
1276 // no need to check for end-of-statement, because such an
1277 // error would already have been detected in first pass.
1278 // for the same reason, there is no need to check for quotes.
1279 while (GotByte != CHAR_SOB)
1280 GetByte();
1281 input_block_skip(); // now GotByte = '}'
1282 }
1283 GetByte(); // Proceed with next character
1284 return ENSURE_EOS;
1285 }
1286
1287 /*
1288 // trace/watch
1289 #define TRACEWATCH_LOAD (1u << 0)
1290 #define TRACEWATCH_STORE (1u << 1)
1291 #define TRACEWATCH_EXEC (1u << 2)
1292 #define TRACEWATCH_DEFAULT (TRACEWATCH_LOAD | TRACEWATCH_STORE | TRACEWATCH_EXEC)
1293 #define TRACEWATCH_BREAK (1u << 3)
1294 static enum eos tracewatch(boolean enter_monitor)
1295 {
1296 struct number pc;
1297 bits flags = 0;
1298
1299 programcounter_read(&pc);
1300 SKIPSPACE();
1301 // check for flags
1302 if (GotByte != CHAR_EOS) {
1303 do {
1304 // parse flag. if no keyword given, give up
1305 if (parser_read_and_lower_keyword() == 0)
1306 return SKIP_REMAINDER; // "Expected keyword" error has already been thrown
1307
1308 if (strcmp(GlobalDynaBuf->buffer, "load") == 0) {
1309 flags |= TRACEWATCH_LOAD;
1310 } else if (strcmp(GlobalDynaBuf->buffer, "store") == 0) {
1311 flags |= TRACEWATCH_STORE;
1312 } else if (strcmp(GlobalDynaBuf->buffer, "exec") == 0) {
1313 flags |= TRACEWATCH_EXEC;
1314 } else {
1315 throw_error("Unknown flag (known are: load, store, exec)."); // FIXME - add to docs!
1316 return SKIP_REMAINDER;
1317 }
1318 } while (parser_accept_comma());
1319 }
1320 // shortcut: no flags at all -> set all flags!
1321 if (!flags)
1322 flags = TRACEWATCH_DEFAULT;
1323 if (enter_monitor)
1324 flags |= TRACEWATCH_BREAK;
1325 if (pc.ntype != NUMTYPE_UNDEFINED) {
1326 //FIXME - store pc and flags!
1327 }
1328 return ENSURE_EOS;
1329 }
1330 // make next byte a trace point (for VICE debugging)
1331 static enum eos po_trace(void)
1332 {
1333 return tracewatch(FALSE); // do not enter monitor, just output
1334 }
1335 // make next byte a watch point (for VICE debugging)
1336 static enum eos po_watch(void)
1337 {
1338 return tracewatch(TRUE); // break into monitor
1339 }
1340 */
1341
1342 // force explicit symbol definitions to set "address" flag.
1343 // if this is used without a block, it is only a prefix for a single statement.
1344 // has to be re-entrant.
1345 static enum eos po_address(void) // now GotByte = illegal char
1346 {
1347 boolean buf;
1348
1349 buf = parser_change_addr_block_flag(TRUE);
1350 if (parse_optional_block()) {
1351 // when used with a block, is used throughout the block
1352 parser_change_addr_block_flag(buf); // restore after block
1353 return ENSURE_EOS;
1354 } else {
1355 // without a block, it's only a "prefix" for the current statement
1356 parser_change_addr_block_flag(buf); // there was no block, so restore...
1357 parser_set_addr_prefix(); // ...and set flag...
1358 return PARSE_REMAINDER; // ...just for this statement.
1359 }
1360 }
1361
1362
1363 // disable warnings.
1364 // if this is used without a block, it is only a prefix for a single statement.
1365 // has to be re-entrant.
1366 static enum eos po_nowarn(void) // now GotByte = illegal char
1367 {
1368 boolean buf;
1369
1370 buf = parser_change_nowarn_block_flag(TRUE);
1371 if (parse_optional_block()) {
1372 // when used with a block, is used throughout the block
1373 parser_change_nowarn_block_flag(buf); // restore after block
1374 return ENSURE_EOS;
1375 } else {
1376 // without a block, it's only a "prefix" for the current statement
1377 parser_change_nowarn_block_flag(buf); // there was no block, so restore...
1378 parser_set_nowarn_prefix(); // ...and set flag...
1379 return PARSE_REMAINDER; // ...just for this statement.
1380 }
1381 }
1382
1383
1384 // exit innermost loop
1385 static enum eos po_break(void) // now GotByte = illegal char
1386 {
1387 boolean allowed;
1388
1389 allowed = parser_allow_break_cont(FALSE);
1390 parser_allow_break_cont(allowed);
1391 if (allowed)
1392 parser_set_shortcut(SHORTCUT_BREAK);
1393 else
1394 throw_error("!break not within a loop.");
1395 return ENSURE_EOS;
1396 }
1397
1398 // end innermost loop iteration
1399 static enum eos po_continue(void) // now GotByte = illegal char
1400 {
1401 boolean allowed;
1402
1403 allowed = parser_allow_break_cont(FALSE);
1404 parser_allow_break_cont(allowed);
1405 if (allowed)
1406 parser_set_shortcut(SHORTCUT_CONT);
1407 else
1408 throw_error("!continue not within a loop.");
1409 return ENSURE_EOS;
1410 }
1411
1412 // return from innermost macro
1413 static enum eos po_return(void) // now GotByte = illegal char
1414 {
1415 boolean allowed;
1416
1417 // if ACME ever gets real functions, this is the place to parse the
1418 // return value and store it via some innermost_function->result
1419 // pointer.
1420 allowed = parser_allow_return(FALSE);
1421 parser_allow_return(allowed);
1422 if (allowed)
1423 parser_set_shortcut(SHORTCUT_RETURN);
1424 else
1425 throw_error("!return not within a macro.");
1426 return ENSURE_EOS;
1427 }
1428
1429
1430 // variables
1431 static STRUCT_DYNABUF_REF(user_message, 80); // for !debug/info/warn/error/serious
1432
1433
1434 // helper function to show user-defined messages
1435 static enum eos throw_src_string(enum debuglevel level, const char prefix[])
1436 {
1437 struct object object;
1438
1439 dynabuf_clear(user_message);
1440 dynabuf_add_string(user_message, prefix);
1441 // algo depends on dialect:
1442 if (config.dialect >= V0_97__BACKSLASH_ESCAPING) {
1443 // since v0.97 the expression parser supports strings, so we pass all args to it:
1444 do {
1445 ALU_any_result(&object);
1446 object.type->print(&object, user_message);
1447 } while (parser_accept_comma());
1448 } else {
1449 // in older versions the expression parser complained about
1450 // strings longer than one character, so we assumed everything
1451 // starting with a doublequote is a string literal and handled
1452 // those separately:
1453 do {
1454 if (GotByte == '"') {
1455 // string literal
1456 if (input_read_string_literal('"'))
1457 break; // unterminated
1458 dynabuf_append(GlobalDynaBuf, '\0'); // terminate string
1459 dynabuf_add_string(user_message, GLOBALDYNABUF_CURRENT); // add to message
1460 } else {
1461 // everything else is passed to expression parser:
1462 ALU_any_result(&object);
1463 object.type->print(&object, user_message);
1464 }
1465 } while (parser_accept_comma());
1466 }
1467 dynabuf_append(user_message, '\0');
1468 throw_message(level, user_message->buffer, NULL);
1469 return ENSURE_EOS;
1470 }
1471
1472 // show debug data as given in source code
1473 static enum eos po_debug(void)
1474 {
1475 struct number debuglevel;
1476
1477 ALU_defined_int(&debuglevel);
1478 if (!parser_expect(','))
1479 return SKIP_REMAINDER;
1480
1481 // drop this one?
1482 if (debuglevel.val.intval > config.debuglevel)
1483 return SKIP_REMAINDER;
1484
1485 return throw_src_string(debuglevel.val.intval, "!debug: ");
1486 }
1487
1488 // show info as given in source code
1489 static enum eos po_info(void)
1490 {
1491 return throw_src_string(DEBUGLEVEL_INFO, "!info: ");
1492 }
1493
1494 // throw warning as given in source code
1495 static enum eos po_warn(void)
1496 {
1497 return throw_src_string(DEBUGLEVEL_WARNING, "!warn: ");
1498 }
1499
1500 // throw error as given in source code
1501 static enum eos po_error(void)
1502 {
1503 return throw_src_string(DEBUGLEVEL_ERROR, "!error: ");
1504 }
1505
1506 // throw serious error as given in source code
1507 static enum eos po_serious(void)
1508 {
1509 return throw_src_string(DEBUGLEVEL_SERIOUS, "!serious: ");
1510 }
1511
1512
1513
1514 // end of source file ("!endoffile" or "!eof")
1515 static enum eos po_endoffile(void)
1516 {
1517 parser_ensure_EOS(); // make sure there are no args
1518 input_force_eof(); // fake end of file
1519 return AT_EOS_ANYWAY;
1520 }
1521
1522 // pseudo opcode table
1523 static struct ronode pseudo_opcode_tree[] = {
1524 PREDEF_START,
1525 PREDEFNODE("initmem", po_initmem),
1526 PREDEFNODE("to", po_to),
1527 PREDEFNODE("sl", po_symbollist),
1528 PREDEFNODE("symbollist", po_symbollist),
1529 PREDEFNODE("outfilestart", po_outfilestart),
1530 PREDEFNODE("outfilestop", po_outfilelimit),
1531 PREDEFNODE("outfilelimit", po_outfilelimit),
1532 PREDEFNODE("xor", po_xor),
1533 PREDEFNODE("by", po_byte),
1534 PREDEFNODE("byt", po_byte),
1535 PREDEFNODE("byte", po_byte),
1536 PREDEFNODE("8", po_byte),
1537 PREDEFNODE("08", po_byte), // legacy alias, don't ask...
1538 PREDEFNODE("wo", po_16),
1539 PREDEFNODE("word", po_16),
1540 PREDEFNODE("16", po_16),
1541 PREDEFNODE("be16", po_be16),
1542 PREDEFNODE("le16", po_le16),
1543 PREDEFNODE("24", po_24),
1544 PREDEFNODE("be24", po_be24),
1545 PREDEFNODE("le24", po_le24),
1546 PREDEFNODE("32", po_32),
1547 PREDEFNODE("be32", po_be32),
1548 PREDEFNODE("le32", po_le32),
1549 PREDEFNODE("h", po_hex),
1550 PREDEFNODE("hex", po_hex),
1551 PREDEFNODE("cbm", po_cbm), // obsolete
1552 PREDEFNODE("ct", po_convtab),
1553 PREDEFNODE("convtab", po_convtab),
1554 PREDEFNODE("tx", po_text),
1555 PREDEFNODE("txt", po_text),
1556 PREDEFNODE("text", po_text),
1557 PREDEFNODE("raw", po_raw),
1558 PREDEFNODE("pet", po_pet),
1559 PREDEFNODE("scr", po_scr),
1560 PREDEFNODE("scrxor", po_scrxor),
1561 PREDEFNODE("bin", po_binary),
1562 PREDEFNODE("binary", po_binary),
1563 PREDEFNODE("fi", po_fill),
1564 PREDEFNODE("fill", po_fill),
1565 PREDEFNODE("skip", po_skip),
1566 PREDEFNODE("align", po_align),
1567 PREDEFNODE("pseudopc", po_pseudopc),
1568 PREDEFNODE("realpc", po_realpc), // obsolete
1569 PREDEFNODE("cpu", po_cpu),
1570 PREDEFNODE("al", po_al),
1571 PREDEFNODE("as", po_as),
1572 PREDEFNODE("rl", po_rl),
1573 PREDEFNODE("rs", po_rs),
1574 // PREDEFNODE("enum", po_enum),
1575 PREDEFNODE("set", po_set),
1576 PREDEFNODE("zn", po_zone),
1577 PREDEFNODE("zone", po_zone),
1578 PREDEFNODE("sz", po_subzone), // obsolete
1579 PREDEFNODE("subzone", po_subzone), // obsolete
1580 PREDEFNODE("src", po_source),
1581 PREDEFNODE("source", po_source),
1582 PREDEFNODE("if", po_if),
1583 PREDEFNODE("ifdef", po_ifdef),
1584 PREDEFNODE("ifndef", po_ifndef),
1585 PREDEFNODE("for", po_for),
1586 PREDEFNODE("do", po_do),
1587 PREDEFNODE("while", po_while),
1588 PREDEFNODE("macro", po_macro),
1589 /* PREDEFNODE("trace", po_trace),
1590 PREDEFNODE("watch", po_watch), */
1591 PREDEFNODE("addr", po_address),
1592 PREDEFNODE("address", po_address),
1593 PREDEFNODE("nowarn", po_nowarn),
1594 PREDEFNODE("break", po_break),
1595 PREDEFNODE("continue", po_continue),
1596 PREDEFNODE("return", po_return),
1597 PREDEFNODE("debug", po_debug),
1598 PREDEFNODE("info", po_info),
1599 PREDEFNODE("warn", po_warn),
1600 PREDEFNODE("error", po_error),
1601 PREDEFNODE("serious", po_serious),
1602 PREDEFNODE("eof", po_endoffile),
1603 PREDEF_END("endoffile", po_endoffile),
1604 // ^^^^ this marks the last element
1605 };
1606
1607
1608 // parse a pseudo opcode. has to be re-entrant.
1609 void pseudoopcode_parse(void) // now GotByte = '!' (or '.' in case of --fullstop)
1610 {
1611 void *node_body;
1612 enum eos (*fn)(void),
1613 then = SKIP_REMAINDER; // prepare for errors
1614
1615 GetByte(); // read next byte
1616 // on missing keyword, return (complaining will have been done)
1617 if (parser_read_and_lower_keyword()) {
1618 // search for tree item
1619 if ((tree_easy_scan(pseudo_opcode_tree, &node_body, GlobalDynaBuf))
1620 && node_body) {
1621 fn = (enum eos (*)(void)) node_body;
1622 SKIPSPACE();
1623 // call function
1624 then = fn();
1625 } else {
1626 throw_error("Unknown pseudo opcode.");
1627 }
1628 }
1629 if (then == SKIP_REMAINDER)
1630 parser_skip_remainder();
1631 else if (then == ENSURE_EOS)
1632 parser_ensure_EOS();
1633 // the other two possibilities (PARSE_REMAINDER and AT_EOS_ANYWAY)
1634 // will lead to the remainder of the line being parsed by the mainloop.
1635 }
1636
1637
1638 // this is not really a pseudo opcode, but similar enough to be put here:
1639 // called when "*= EXPRESSION" is parsed, to set the program counter
1640 void notreallypo_setpc(void) // GotByte is '*'
1641 {
1642 bits segment_flags = 0;
1643 boolean do_outfilestart = FALSE;
1644 struct number intresult;
1645
1646 // next non-space must be '='
1647 NEXTANDSKIPSPACE();
1648 if (!parser_expect('='))
1649 goto fail;
1650
1651 ALU_defined_int(&intresult); // read new address
1652 // check for modifiers
1653 while (parser_accept_comma()) {
1654 // parse modifier. if no keyword given, give up
1655 if (parser_read_and_lower_keyword() == 0)
1656 goto fail;
1657
1658 if (strcmp(GlobalDynaBuf->buffer, "overlay") == 0) {
1659 segment_flags |= SEGMENT_FLAG_OVERLAY;
1660 } else if (strcmp(GlobalDynaBuf->buffer, "invisible") == 0) {
1661 segment_flags |= SEGMENT_FLAG_INVISIBLE;
1662 } else if (strcmp(GlobalDynaBuf->buffer, "outfilestart") == 0) {
1663 do_outfilestart = TRUE;
1664 /*TODO } else if (strcmp(GlobalDynaBuf->buffer, "limit") == 0) {
1665 skip '='
1666 read memory limit
1667 } else if (strcmp(GlobalDynaBuf->buffer, "stay" or "same" or something like that) == 0) {
1668 mutually exclusive with all other arguments!
1669 this would mean to keep all previous segment data,
1670 so it could be used with "*=*-5" or "*=*+3"
1671 } else if (strcmp(GlobalDynaBuf->buffer, "name") == 0) {
1672 skip '='
1673 read segment name (quoted string!) */
1674 } else {
1675 throw_error("Unknown \"*=\" segment modifier.");
1676 goto fail;
1677 }
1678 }
1679
1680 // before actually setting pc,
1681 // support stupidly bad, old, ancient, deprecated, obsolete behaviour:
1682 if (pseudopc_isactive()) {
1683 if (config.dialect >= V0_94_8__DISABLED_OBSOLETE) {
1684 // current behaviour:
1685 // setting pc does not disable pseudopc
1686 } else if (config.dialect >= V0_93__SHORTER_SETPC_WARNING) {
1687 throw_warning("Offset assembly still active at end of segment.");
1688 end_all_pseudopc(); // warning did not say it would
1689 // disable pseudopc, but still did. nevertheless, there
1690 // is something different to older versions: when the
1691 // closing '}' or !realpc is encountered, _really_ weird
1692 // stuff happens! i see no reason to try to mimic that.
1693 } else {
1694 // prior to 0.93, setting pc disabled pseudopc with a warning:
1695 throw_warning("Offset assembly still active at end of segment. Switched it off.");
1696 end_all_pseudopc();
1697 }
1698 }
1699
1700 if (intresult.val.intval < 0) {
1701 throw_error("Program counter cannot be negative.");
1702 intresult.val.intval = cpu_current_type->dummy_pc;
1703 }
1704 programcounter_set(intresult.val.intval, segment_flags);
1705
1706 // if wanted, perform "!outfilestart":
1707 if (do_outfilestart)
1708 po_outfilestart();
1709
1710 parser_ensure_EOS();
1711 return;
1712
1713 fail:
1714 parser_skip_remainder();
1715 }