forked from shattered/macro11
-
Notifications
You must be signed in to change notification settings - Fork 5
/
assemble.c
1878 lines (1601 loc) · 75.5 KB
/
assemble.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define ASSEMBLE__C
#include <stdlib.h>
#include <string.h>
#include "assemble.h" /* my own definitions */
#include "assemble_globals.h"
#include "assemble_aux.h"
#include "util.h"
#include "mlb.h"
#include "object.h"
#include "listing.h"
#include "parse.h"
#include "symbols.h"
#include "extree.h"
#include "macros.h"
#include "rept_irpc.h"
#include "rad50.h"
#define CHECK_EOL check_eol(stack, cp)
/* assemble a rad50 value (some number of words). */
static unsigned * assemble_rad50 (
char *cp,
int max,
int *count,
STACK *stack)
{
char *radstr;
unsigned *ret;
int i, len, wcnt;
/*
* Allocate storage sufficient for the rest of
* the line.
*/
radstr = memcheck(malloc(strlen(cp)));
len = 0;
do {
cp = skipwhite(cp);
if (*cp == '<') {
EX_TREE *value;
/* A byte value */
value = parse_unary_expr(cp, 0);
cp = value->cp;
if (value->type != EX_LIT) {
report(stack->top, "expression must be constant\n");
radstr[len++] = 0;
} else if (value->data.lit >= 050) {
report(stack->top, "invalid character value %o\n", value->data.lit);
radstr[len++] = 0;
} else {
radstr[len++] = value->data.lit;
}
free_tree(value);
} else {
char quote = *cp++;
while (*cp && *cp != '\n' && *cp != quote) {
int ch = ascii2rad50(*cp++);
if (ch == -1) {
report(stack->top, "invalid character '%c'\n", cp[-1]);
radstr[len++] = 0;
} else {
radstr[len++] = ch;
}
}
cp++; /* Skip closing quote */
}
cp = skipwhite(cp);
} while (!EOL(*cp));
wcnt = (len + 2) / 3;
/* Return at most "max" words, if specified */
if (max && max < wcnt)
wcnt = max;
if (count != NULL)
*count = wcnt;
/* Allocate space for actual or max words, whichever is larger */
ret = memcheck (malloc (((wcnt < max) ? max : wcnt) * sizeof (int)));
for (i = 0; i < wcnt; i++) {
int word = packrad50word(radstr + i * 3, len - (i * 3));
ret[i] = word;
}
/* If max is specified, zero fill */
for (; i < max; i++)
ret[i] = 0;
free(radstr);
return ret;
}
/* assemble - read a line from the input stack, assemble it. */
/* This function is way way too large, because I just coded most of
the operation code and pseudo-op handling right in line. */
static int assemble(
STACK *stack,
TEXT_RLD *tr)
{
char *cp; /* Parse character pointer */
char *opcp; /* Points to operation mnemonic text */
char *ncp; /* "next" cp */
char *label; /* A label */
char *line; /* The whole line */
SYMBOL *op; /* The operation SYMBOL */
int local; /* Whether a label is a local label or
not */
line = stack_getline(stack);
if (line == NULL)
return -1; /* Return code for EOF. */
if (!enabl_lc) { /* If lower case disabled, */
upcase(line); /* turn it into upper case. */
}
cp = line;
/* Frankly, I don't need to keep "line." But I found it quite
handy during debugging, to see what the whole operation was,
when I'm down to parsing the second operand and things aren't
going right. */
stmtno++; /* Increment statement number */
list_source(stack->top, line); /* List source */
if (suppressed) {
/* Assembly is suppressed by unsatisfied conditional. Look
for ending and enabling statements. */
op = get_op(cp, &cp); /* Look at operation code */
/* FIXME: this code will blindly look into .REM commentary and
find operation codes. Incidentally, so will read_body().
That doesn't really matter, though, since the original also
did that (line 72 ends the suppressed conditional block):
69 .if NE,0
70 .rem &
71 junk
72 .endc
A 73 000144 000000G 000000G more junk
A 74 000150 000000G 000000G 000000G line that ends the comments with &
000156 000000G 000000G 000000C
O 75 .endc
*/
if (op == NULL)
return 1; /* Not found. Don't care. */
if (op->section->type != SECTION_PSEUDO)
return 1; /* Not a pseudo-op. */
switch (op->value) {
case P_IF:
case P_IFDF:
suppressed++; /* Nested. Suppressed. */
break;
case P_IFTF:
if (suppressed == 1) /* Reduce suppression from 1 to 0. */
suppressed = 0;
break;
case P_IFF:
if (suppressed == 1) { /* Can reduce suppression from 1 to 0. */
if (!conds[last_cond].ok)
suppressed = 0;
}
break;
case P_IFT:
if (suppressed == 1) { /* Can reduce suppression from 1 to 0. */
if (conds[last_cond].ok)
suppressed = 0;
}
break;
case P_ENDC:
suppressed--; /* Un-nested. */
if (suppressed == 0)
pop_cond(last_cond - 1); /* Re-enabled. */
break;
}
return 1;
}
/* The line may begin with "label<ws>:[:]" */
/* PSEUDO P_IIF jumps here. */
reassemble:
opcp = cp;
if ((label = get_symbol(cp, &ncp, &local)) != NULL) {
int flag = SYMBOLFLAG_PERMANENT | SYMBOLFLAG_DEFINITION | local;
SYMBOL *sym;
ncp = skipwhite(ncp);
if (*ncp == ':') { /* Colon, for symbol definition? */
ncp++;
/* maybe it's a global definition */
if (*ncp == ':') {
flag |= SYMBOLFLAG_GLOBAL; /* Yes, include global flag */
ncp++;
}
sym = add_sym(label, DOT, flag, current_pc->section, &symbol_st);
cp = ncp;
if (sym == NULL)
report(stack->top, "Illegal symbol definition %s\n", label);
free(label);
/* See if local symbol block should be incremented */
if (!enabl_lsb && !local) {
lsb = get_next_lsb();
}
cp = skipwhite(ncp);
opcp = cp;
label = get_symbol(cp, &ncp, NULL); /* Now, get what follows */
}
}
cp = skipwhite(cp);
if (EOL(*cp))
return 1; /* It's commentary. All done. */
if (label) { /* Something looks like a label. */
/* detect assignment */
ncp = skipwhite(ncp); /* The pointer to the text that
follows the symbol */
if (*ncp == '=') {
unsigned flags;
EX_TREE *value;
SYMBOL *sym;
cp = ncp;
/* Symbol assignment. */
flags = SYMBOLFLAG_DEFINITION | local;
cp++;
if (*cp == '=') {
flags |= SYMBOLFLAG_GLOBAL; /* Global definition */
cp++;
}
if (*cp == ':') {
flags |= SYMBOLFLAG_PERMANENT;
cp++;
}
cp = skipwhite(cp);
value = parse_expr(cp, 0);
cp = value->cp;
/* Special code: if the symbol is the program counter,
this is harder. */
if (strcmp(label, ".") == 0) {
if (current_pc->section->flags & PSECT_REL) {
SYMBOL *symb;
unsigned offset;
/* Express the given expression as a symbol and an
offset. The symbol must not be global, the
section must = current. */
if (!express_sym_offset(value, &symb, &offset)) {
report(stack->top, "Illegal ORG (for relocatable section)\n");
} else if (SYM_IS_IMPORTED(symb)) {
report(stack->top, "Can't ORG to external location\n");
} else if (symb->flags & SYMBOLFLAG_UNDEFINED) {
report(stack->top, "Can't ORG to undefined sym\n");
} else if (symb->section != current_pc->section) {
report(stack->top, "Can't ORG to alternate section (use PSECT)\n");
} else {
DOT = symb->value + offset;
list_value(stack->top, DOT);
change_dot(tr, 0);
}
} else {
/* If the current section is absolute, the value
must be a literal */
if (value->type != EX_LIT) {
report(stack->top, "Can't ORG to non-absolute location\n");
free_tree(value);
free(label);
return 0;
}
DOT = value->data.lit;
list_value(stack->top, DOT);
change_dot(tr, 0);
}
free_tree(value);
free(label);
return CHECK_EOL;
}
/* regular symbols */
if (value->type == EX_LIT) {
sym = add_sym(label, value->data.lit, flags, &absolute_section, &symbol_st);
} else if (value->type == EX_SYM || value->type == EX_TEMP_SYM) {
sym = add_sym(label, value->data.symbol->value, flags, value->data.symbol->section, &symbol_st);
} else {
report(stack->top, "Complex expression cannot be assigned to a symbol\n");
if (!pass) {
/* This may work better in pass 2 - something in
RT-11 monitor needs the symbol to apear to be
defined even if I can't resolve its value. */
sym = add_sym(label, 0, SYMBOLFLAG_UNDEFINED, &absolute_section, &symbol_st);
} else
sym = NULL;
}
if (sym != NULL)
list_value(stack->top, sym->value);
free_tree(value);
free(label);
return sym != NULL && CHECK_EOL;
}
/* Try to resolve macro */
do_mcalled_macro:
op = lookup_sym(label, ¯o_st);
if (op /*&& op->stmtno < stmtno*/) {
STREAM *macstr;
free(label);
list_location(stack->top, DOT);
macstr = expandmacro(stack->top, (MACRO *) op, ncp);
if (macstr == NULL) {
/* Error in expanding the macro, stop now. */
return 0;
}
stack_push(stack, macstr); /* Push macro expansion
onto input stream */
return 1;
}
/* Try to resolve instruction or pseudo */
op = lookup_sym(label, &system_st);
if (op) {
cp = ncp;
free(label); /* Don't need this hanging around anymore */
switch (op->section->type) {
case SECTION_PSEUDO:
switch (op->value) {
case P_PAGE:
case P_PRINT:
case P_SBTTL:
case P_CROSS:
case P_NOCROSS:
return 1; /* Accepted, ignored. (An obvious
need: get assembly listing
controls working fully. ) */
case P_LIST:
if (pass > 0) {
cp = skipwhite(cp);
if (EOL(*cp))
list_level++;
}
return 1;
case P_NLIST:
if (pass > 0) {
cp = skipwhite(cp);
if (EOL(*cp))
list_level--;
}
return 1;
case P_IDENT:
if (ident) /* An existing ident? */
free(ident); /* Discard it. */
ident = assemble_rad50 (cp, 2, NULL, stack);
return 1;
case P_RADIX:
{
int old_radix = radix;
EX_TREE *value;
int ok = 1;
cp = skipwhite(cp);
if (EOL(*cp)) {
/* If no argument, assume 8 */
radix = 8;
return 1;
}
/* Parse the argument in decimal radix */
radix = 10;
value = parse_expr(cp, 0);
cp = value->cp;
if (value->type != EX_LIT) {
report(stack->top, "Argument to .RADIX must be constant\n");
radix = old_radix;
ok = 0;
} else {
radix = value->data.lit;
list_value(stack->top, radix);
if (radix != 8 && radix != 10 &&
radix != 2 && radix != 16) {
radix = old_radix;
report(stack->top, "Argument to .RADIX must be 2, 8, 10, or 16\n");
ok = 0;
}
}
free_tree(value);
return ok && CHECK_EOL;
}
case P_FLT4:
case P_FLT2:
{
int ok = 1;
while (ok && !EOL(*cp)) {
unsigned flt[4];
if (parse_float(cp, &cp, (op->value == P_FLT4 ? 4 : 2), flt)) {
/* All is well */
} else {
report(stack->top, "Bad floating point format\n");
ok = 0; /* Don't try to parse the rest of the line */
flt[0] = flt[1] /* Store zeroes */
= flt[2]
= flt[3] = 0;
}
/* Store the word values */
store_word(stack->top, tr, 2, flt[0]);
store_word(stack->top, tr, 2, flt[1]);
if (op->value == P_FLT4) {
store_word(stack->top, tr, 2, flt[2]);
store_word(stack->top, tr, 2, flt[3]);
}
cp = skipdelim(cp);
}
return ok && CHECK_EOL;
}
case P_ERROR:
report(stack->top, "%.*s\n", strcspn(cp, "\n"), cp);
return 0;
case P_SAVE:
if (sect_sp >= SECT_STACK_SIZE - 1) {
report(stack->top, "Too many saved sections for .SAVE\n");
return 0;
}
sect_sp++;
sect_stack[sect_sp] = current_pc->section;
dot_stack[sect_sp] = DOT;
return CHECK_EOL;
case P_RESTORE:
if (sect_sp < 0) {
report(stack->top, "No saved section for .RESTORE\n");
return 0;
} else {
go_section(tr, sect_stack[sect_sp]);
DOT = dot_stack[sect_sp];
list_location(stack->top, DOT);
if (!enabl_lsb) {
lsb = get_next_lsb();
}
sect_sp--;
}
return CHECK_EOL;
case P_NARG:
{
STREAM *str;
MACRO_STREAM *mstr;
int islocal;
label = get_symbol(cp, &cp, &islocal);
if (label == NULL) {
report(stack->top, "Bad .NARG syntax\n");
return 0;
}
/* Walk up the stream stack to find the
topmost macro stream */
for (str = stack->top; str != NULL && str->vtbl != ¯o_stream_vtbl;
str = str->next) ;
if (!str) {
report(str, ".NARG not within macro expansion\n");
free(label);
return 0;
}
mstr = (MACRO_STREAM *) str;
add_sym(label, mstr->nargs, SYMBOLFLAG_DEFINITION | islocal, &absolute_section,
&symbol_st);
free(label);
list_value(stack->top, mstr->nargs);
return CHECK_EOL;
}
case P_NCHR:
{
char *string;
int islocal;
label = get_symbol(cp, &cp, &islocal);
if (label == NULL) {
report(stack->top, "Bad .NCHR syntax\n");
return 0;
}
cp = skipdelim(cp);
string = getstring(cp, &cp);
add_sym(label, strlen(string), SYMBOLFLAG_DEFINITION | islocal, &absolute_section,
&symbol_st);
free(label);
free(string);
return CHECK_EOL;
}
case P_NTYPE:
{
ADDR_MODE mode;
int islocal;
char *error;
label = get_symbol(cp, &cp, &islocal);
if (label == NULL) {
report(stack->top, "Bad .NTYPE syntax\n");
return 0;
}
cp = skipdelim(cp);
if (!get_mode(cp, &cp, &mode, &error)) {
report(stack->top,
"Bad .NTYPE addressing mode (%s)\n", error);
free(label);
return 0;
}
add_sym(label, mode.type, SYMBOLFLAG_DEFINITION | islocal, &absolute_section, &symbol_st);
free_addr_mode(&mode);
free(label);
return CHECK_EOL;
}
case P_INCLUDE:
{
char *name = getstring_fn(cp, &cp);
STREAM *incl;
char hitfile[FILENAME_MAX];
if (name == NULL) {
report(stack->top, "Bad .INCLUDE file name\n");
return 0;
}
name = defext (name, "MAC");
my_searchenv(name, "INCLUDE", hitfile, sizeof(hitfile));
if (hitfile[0] == '\0') {
report(stack->top, "Unable to find .INCLUDE file \"%s\"\n", name);
free(name);
return 0;
}
free(name);
incl = new_file_stream(hitfile);
if (incl == NULL) {
report(stack->top, "Unable to open .INCLUDE file \"%s\"\n", hitfile);
return 0;
}
stack_push(stack, incl);
return CHECK_EOL;
}
case P_REM:
{
char quote[4];
/* Read and discard lines until one with a
closing quote */
cp = skipwhite(cp);
quote[0] = *cp++;
quote[1] = '\n';
quote[2] = 0;
for (;;) {
cp += strcspn(cp, quote);
if (*cp == quote[0])
break; /* Found closing quote */
cp = stack_getline(stack); /* Read next input line */
if (cp == NULL)
break; /* EOF */
}
}
return 1;
case P_IRP:
{
STREAM *str = expand_irp(stack, cp);
if (str)
stack_push(stack, str);
return str != NULL;
}
case P_IRPC:
{
STREAM *str = expand_irpc(stack, cp);
if (str)
stack_push(stack, str);
return str != NULL;
}
case P_LIBRARY:
{
char hitfile[FILENAME_MAX];
char *name = getstring_fn(cp, &cp);
name = defext (name, "MLB");
my_searchenv(name, "MCALL", hitfile, sizeof(hitfile));
if (hitfile[0]) {
mlbs[nr_mlbs] = mlb_open(hitfile, 0);
if (mlbs[nr_mlbs] == NULL) {
report(stack->top, "Unable to register macro library \"%s\"\n", hitfile);
} else {
nr_mlbs++;
}
} else {
report(stack->top, "Unable to locate macro library \"%s\"\n", name);
}
free(name);
}
return CHECK_EOL;
case P_MCALL:
{
for (;;) {
cp = skipdelim(cp);
if (EOL(*cp))
return 1;
/* (lib)macro syntax. Ignore (lib) for now. */
if (*cp == '(') {
char *close = strchr(cp + 1, ')');
if (close != NULL) {
char *libname = cp + 1;
(void)libname;
*close = '\0';
cp = close + 1;
}
}
label = get_symbol(cp, &cp, NULL);
if (!label) {
report(stack->top, "Illegal .MCALL format\n");
return 0;
}
/* See if that macro's already defined */
if (lookup_sym(label, ¯o_st)) {
free(label); /* Macro already
registered. No
prob. */
cp = skipdelim(cp);
continue;
}
/* Do the actual macro library search */
if (!do_mcall (label, stack))
report(stack->top, "MACRO %s not found\n", label);
free(label);
}
/* NOTREACHED */
}
return 1;
case P_MACRO:
{
MACRO *mac = defmacro(cp, stack, CALLED_NORMAL);
return mac != NULL;
}
case P_MDELETE:
return 1; /* TODO: or should it just be a NOP? */
case P_MEXIT:
{
STREAM *macstr;
/* Pop a stream from the input. */
/* It must be the first stream, and it must be */
/* a macro, rept, irp, or irpc. */
macstr = stack->top;
if (macstr->vtbl != ¯o_stream_vtbl && macstr->vtbl != &rept_stream_vtbl
&& macstr->vtbl != &irp_stream_vtbl && macstr->vtbl != &irpc_stream_vtbl) {
report(stack->top, ".MEXIT not within a macro\n");
return 0;
}
/* and finally, pop the macro */
stack_pop(stack);
return CHECK_EOL;
}
case P_REPT:
{
STREAM *reptstr = expand_rept(stack, cp);
if (reptstr)
stack_push(stack, reptstr);
return reptstr != NULL;
}
case P_ENABL:
/* FIXME - add all the rest of the options. */
while (!EOL(*cp)) {
label = get_symbol(cp, &cp, NULL);
if (strcmp(label, "AMA") == 0)
enabl_ama = 1;
else if (strcmp(label, "LSB") == 0) {
enabl_lsb = 1;
lsb = get_next_lsb();
} else if (strcmp(label, "GBL") == 0) {
enabl_gbl = 1;
} else if (strcmp(label, "LC") == 0) {
enabl_lc = 1;
} else if (strcmp(label, "LCM") == 0) {
enabl_lcm = 1;
} else if (strcmp(label, "MCL") == 0) {
enabl_mcl = 1;
}
free(label);
cp = skipdelim(cp);
}
return 1;
case P_DSABL:
/* FIXME Ditto as for .ENABL */
while (!EOL(*cp)) {
label = get_symbol(cp, &cp, NULL);
if (strcmp(label, "AMA") == 0)
enabl_ama = 0;
else if (strcmp(label, "LSB") == 0) {
lsb = get_next_lsb();
enabl_lsb = 0;
} else if (strcmp(label, "GBL") == 0) {
enabl_gbl = 0;
} else if (strcmp(label, "LC") == 0) {
enabl_lc = 0;
} else if (strcmp(label, "LCM") == 0) {
enabl_lcm = 0;
} else if (strcmp(label, "MCL") == 0) {
enabl_mcl = 0;
}
free(label);
cp = skipdelim(cp);
}
return 1;
case P_LIMIT:
store_limits(stack->top, tr);
return CHECK_EOL;
case P_TITLE:
/* accquire module name */
if (module_name != NULL) {
free(module_name);
}
module_name = get_symbol(cp, &cp, NULL);
return 1;
case P_END:
/* Accquire transfer address */
cp = skipwhite(cp);
if (!EOL(*cp)) {
if (xfer_address)
free_tree(xfer_address);
xfer_address = parse_expr(cp, 0);
cp = xfer_address->cp;
}
return CHECK_EOL;
case P_IFDF:
opcp = skipwhite(opcp);
cp = opcp + 3; /* Point cp at the "DF" or
"NDF" part */
/* FALLS THROUGH */
case P_IIF:
case P_IF:
{
EX_TREE *value;
int ok = FALSE;
label = get_symbol(cp, &cp, NULL); /* Get condition */
cp = skipdelim(cp);
if (!label) {
report(stack->top, "Missing .(I)IF condition\n");
} else if (strcmp(label, "DF") == 0) {
value = parse_expr(cp, EVALUATE_DEFINEDNESS);
cp = value->cp;
ok = eval_defined(value);
free_tree(value);
} else if (strcmp(label, "NDF") == 0) {
value = parse_expr(cp, EVALUATE_DEFINEDNESS);
cp = value->cp;
ok = eval_undefined(value);
free_tree(value);
} else if (strcmp(label, "B") == 0 ||
strcmp(label, "NB") == 0) {
/*
* Page 6-46 footnote 1 says
* "A macro argument (a form of symbolic argument)
* is enclosed within angle brackets or delimited
* by the circumflex construction, as described in
* section 7.3. For example,
* <A,B,C>
* ^/124/"
* but we don't enforce that here (yet) by using
* simply getstring().
*/
cp = skipwhite(cp);
if (EOL(*cp)) {
ok = 1;
} else {
char *thing, *end;
thing = getstring(cp, &cp);
end = skipwhite(thing);
ok = (*end == 0);
free(thing);
}
if (label[0] == 'N') {
ok = !ok;
}
} else if (strcmp(label, "IDN") == 0 ||
strcmp(label, "DIF") == 0) {
char *thing1,
*thing2;
thing1 = getstring(cp, &cp);
cp = skipdelim(cp);
if (!EOL(*cp))
thing2 = getstring(cp, &cp);
else
thing2 = memcheck(strdup(""));
if (!enabl_lcm) {
upcase(thing1);
upcase(thing2);
}
ok = (strcmp(thing1, thing2) == 0);
if (label[0] == 'D') {
ok = !ok;
}
free(thing1);
free(thing2);
} else if (strcmp(label, "P1") == 0) {
ok = (pass == 0);
} else if (strcmp(label, "P2") == 0) {
ok = (pass == 1);
} else {
int sword;
unsigned uword;
EX_TREE *tvalue = parse_expr(cp, 0);
cp = tvalue->cp;
if (tvalue->type != EX_LIT) {
report(stack->top, "Bad .IF expression\n");
list_value(stack->top, 0);
free_tree(tvalue);
ok = TRUE; /* Pick something. */
} else {
unsigned word;
/* Convert to signed and unsigned words */
sword = tvalue->data.lit & 0x7fff;
/* FIXME I don't know if the following
is portable enough. */
if (tvalue->data.lit & 0x8000)
sword |= ~0x7FFF; /* Render negative */
/* Reduce unsigned value to 16 bits */
uword = tvalue->data.lit & 0xffff;
if (strcmp(label, "EQ") == 0 || strcmp(label, "Z") == 0)
ok = (uword == 0), word = uword;
else if (strcmp(label, "NE") == 0 || strcmp(label, "NZ") == 0)
ok = (uword != 0), word = uword;
else if (strcmp(label, "GT") == 0 || strcmp(label, "G") == 0)
ok = (sword > 0), word = sword;
else if (strcmp(label, "GE") == 0)
ok = (sword >= 0), word = sword;
else if (strcmp(label, "LT") == 0 || strcmp(label, "L") == 0)
ok = (sword < 0), word = sword;
else if (strcmp(label, "LE") == 0)
ok = (sword <= 0), word = sword;
else
ok = 0, word = 0;
list_value(stack->top, word);
free_tree(tvalue);
}
}
free(label);
if (op->value == P_IIF) {
stmtno++; /* the second half is a
separate statement */
if (ok) {
/* The "immediate if" */
/* Only slightly tricky. */
cp = skipdelim(cp);
goto reassemble;
}
return 1; /* Ignore rest of line if
condition is false */
}
push_cond(ok, stack->top);
if (!ok)
suppressed++; /* Assembly
suppressed
until .ENDC */
}
return CHECK_EOL;
case P_IFF:
if (last_cond < 0) {
report(stack->top, "No conditional block active\n");
return 0;
}
if (conds[last_cond].ok) /* Suppress if last cond
is true */
suppressed++;
return CHECK_EOL;
case P_IFT:
if (last_cond < 0) {
report(stack->top, "No conditional block active\n");
return 0;
}
if (!conds[last_cond].ok) /* Suppress if last cond
is false */
suppressed++;
return CHECK_EOL;