-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmacros.c
More file actions
1447 lines (1411 loc) · 50.1 KB
/
Copy pathmacros.c
File metadata and controls
1447 lines (1411 loc) · 50.1 KB
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
/*
macros.c - Part of macxx, a cross assembler family for various micro-processors
Copyright (C) 2008 David Shepperd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/******************************************************************************
Change Log
02-06-2022 - Changed added support for HLLxxF - Tim Giddens
******************************************************************************/
#include "token.h"
#include "pst_tokens.h"
#include "listctrl.h"
#include "memmgt.h"
#include "exproper.h"
#include "utils.h"
uint8_t *macro_pool; /* pointer to free space for macro expansion */
int macro_pool_size; /* amount of space left in macro pool */
uint32_t macro_pool_used,peak_macro_pool_used;
int macro_level; /* nest level of macro calls */
int macro_nesting; /* .ne. if macro called a .INCLUDE file */
Mcall_struct *marg_head;
#ifdef DUMP_MACRO
void dump_macro( Macargs *ma, Opcode *mac )
{
int c,i;
char *d,*s,**sp;
if (mac != 0)
{
sprintf(emsg,"\tDump of macro %s\n",mac->op_name);
puts_lis(emsg,1);
sprintf(emsg,"\t\t Opcode entry (%08X):\n\t\t\top_margs: %08X\n\t\t\top_class: %04X\n",
(int32_t)mac,(int32_t)mac->op_margs,mac->op_class);
puts_lis(emsg,3);
}
else
{
puts_lis("\tDump of .IRP macro\n",1);
}
sprintf(emsg,"\t\t Dummy argument list (%08X) %d arguments:\n\t\t\tMacro body at %08X\n",
(int32_t)ma,(int32_t)ma->mac_numargs, (int32_t)ma->mac_body);
puts_lis(emsg,2);
for (i=0;i<ma->mac_numargs;++i)
{
sp = ma->mac_keywrd+i;
s = *sp;
sprintf(emsg,"\t\t\tArg %d (%08X)->(%08X)-> %s\n",
i,(int32_t)sp,(int32_t)s,s);
puts_lis(emsg,1);
}
if (ma->mac_body != 0)
{
puts_lis("\t\tMacro body:\n",1);
s = ma->mac_body;
while (1)
{
d = emsg;
*d++ = '\t';
*d++ = '\t';
*d++ = '\t';
while (c = (uint8_t)*s++)
{
if ((c & 0x80) != 0)
{
if (c == MAC_LINK)
{
ADJ_ALIGN(s)
s = *((char **)s);
sprintf(d,"{LINK to %08X}", (int32_t)s);
}
else if (c == MAC_EOM)
{
strcpy(d,"{EOM}\n");
d += strlen(d);
break;
}
else
{
sprintf(d,"{%d}",c&0x7F);
}
d += strlen(d);
}
else
{
*d++ = c;
}
}
*d = 0; /* terminate the string */
puts_lis(emsg,1);
if (c == MAC_EOM) break;
}
}
return;
}
#endif
void free_macbody( uint8_t *link )
{
#if !defined(SUN)
uint8_t c,*s;
int *ip;
s = (uint8_t *)link;
ip = (int *)s;
--ip;
while (1)
{
c = *s++;
if (c == MAC_LINK)
{
ADJ_ALIGN(s)
free_macbody(*((uint8_t **)s));
break;
}
if (c == MAC_EOM) break;
}
macro_pool_used -= *ip;
MEM_free(link);
#endif
return;
}
static int scan_margs( int numarg, char **args, char *aptr)
{
int num = 0;
for (num = 0; num < numarg; ++num)
{
if (strcmp(aptr,*args++) == 0) return(num|-128);
}
return 0;
}
int macro_to_mem( Macargs *ma, char *name)
{
int nest = 0,tkncnt;
char *src;
uint8_t *dst;
list_stats.include_level = include_level;
while ( 1 )
{ /* until .ENDM/.ENDR found */
int mpsize;
if (get_text() == EOF) return EOF;
list_stats.line_no = current_fnd->fn_line;
src = inp_str; /* point to beginning of string */
mpsize = inp_len+1+1+ALIGNMENT+sizeof(char *);
if (macro_pool_size < mpsize)
{
uint8_t **nmp;
if (macro_pool != 0)
{
*macro_pool++ = MAC_LINK;
ADJ_ALIGN(macro_pool)
}
nmp = (uint8_t **)macro_pool;
macro_pool_size = 128;
if (macro_pool_size < mpsize) macro_pool_size = mpsize;
macro_pool = (uint8_t *)MEM_alloc(macro_pool_size);
if (nmp != 0)
*nmp = macro_pool;
if (ma->mac_body == 0)
ma->mac_body = macro_pool;
macro_pool_used += macro_pool_size;
if (macro_pool_used > peak_macro_pool_used)
{
peak_macro_pool_used = macro_pool_used;
}
}
dst = macro_pool;
tkncnt = 0;
while (1)
{ /* for each token on a line */
uint8_t *d, *aptr;
int c;
int argnum;
while ((cttbl[(int)*src]&CT_WS) != 0)
*dst++ = *src++;
aptr = d = dst; /* remember start of token */
c = *dst++ = *src++; /* pass the first char of string */
if (cttbl[c]&CT_EOL)
{
--src; /* stop on EOL character */
break; /* and quit */
}
if (c == '\'')
{ /* start with a "'"? */
aptr = dst;
c = *dst++ = *src++; /* pickup the next char */
if (c == '\'')
{ /* followed by another one? */
d = aptr; /* remember it */
aptr = dst; /* remember start of token */
c = *dst++ = *src++;
}
}
if (cttbl[c]&CT_ALP)
{ /* first char an ALP? */
char *tpp;
tpp = token_pool;
if ((edmask&ED_LC) != 0)
{ /* if case sensitive */
*tpp++ = c; /* don't upcase it */
while (cttbl[c = *src]&(CT_ALP|CT_NUM))
{
++src; /* pass remaining ALP|NUM's */
*dst++ = c;
*tpp++ = c;
}
}
else
{ /* else not case sensitive */
*tpp++ = _toupper(c); /* upcase it */
while (cttbl[c = *src]&(CT_ALP|CT_NUM))
{
++src; /* yep, pass remaining ALP|NUM's */
*dst++ = c;
*tpp++ = _toupper(c);
}
}
*tpp = *dst = 0; /* terminate the strings */
if (tkncnt == 0 && (cttbl[c]&(CT_WS|CT_EOL|CT_SMC)) != 0 &&
aptr == d)
{
Opcode *mptr; /* yep */
*(token_pool+max_opcode_length) = 0;
mptr = opcode_lookup(token_pool,0);
if ( (mptr != 0) &&
((mptr->op_class&(OP_CLASS_OPC|OP_CLASS_MAC)) == 0) &&
(mptr->op_class&DFLMAC))
{
if (mptr->op_class&DFLMEND)
{
--nest;
if (nest < 0)
{
while ((cttbl[(int)*src]&CT_WS) != 0) ++src;
tpp = token_pool;
while ((cttbl[c = *src]&(CT_ALP|CT_NUM)) != 0)
{
++src;
*tpp++ = _toupper(c);
}
*tpp = 0;
if (tpp-token_pool > 0)
{
if (name != 0 && strcmp(token_pool,name) != 0)
{
sprintf(emsg,"Name on .ENDM (%s) doesn't match macro name (%s)"
,token_pool,name);
bad_token((char *)0,emsg);
}
} /* -- name on .ENDM */
inp_ptr = src;
dst = aptr;
for (d=dst-1; d >= macro_pool && (cttbl[c = *d]&CT_WS) != 0;--d);
if (d >= macro_pool)
{
dst = d+1;
if (c != 0 && c != '\n')
{
*dst++ = '\n';
*dst++ = 0;
}
}
else
{
dst = macro_pool;
}
ma->mac_end = dst;
*dst++ = MAC_EOM;
*dst++ = 0;
return EOL;
} /* -- nest level < 0 */
}
else
{ /* +- MACRO end directive */
++nest; /* bump nest level */
} /* -- MACRO end directive */
} /* -- MACRO directive */
} /* -- arg 0 terminated on WS */
if (ma->mac_numargs > 0)
{
argnum = scan_margs(ma->mac_numargs,ma->mac_keywrd,token_pool);
if (argnum < 0)
{
*d++ = argnum; /* stuff in the argument number */
dst = d; /* backup the pointer */
if (*src == '\'')
{ /* end with a "'"? */
++src; /* yep, then eat it */
}
}
}
}
else
{ /* +- First char is ALP */
uint16_t oct,ct; /* first char is NOT ALP */
oct = cttbl[c]; /* get the flags of the first char */
while (1)
{ /* pass all chars until next ALP or EOL */
ct = cttbl[c = *src];
if (c == '\'') break; /* stop on tics */
if ((ct&CT_EOL) != 0) break; /* stop at EOL */
if ((ct&CT_ALP) != 0)
{ /* found an ALP */
if ((oct&(CT_ALP|CT_NUM)) == 0)
{ /* if previous char not a NUM */
break;
}
}
else
{
oct = ct; /* remember previous char */
}
*dst++ = c; /* pass current char */
++src; /* and advance */
}
} /* -- First char is ALP */
c = *src; /* pickup the next source char */
if (tkncnt == 0)
{
if (c == ':')
{
*dst++ = c;
++src; /* skip over the : */
if (*src == ':')
{ /* a global? */
*dst++ = c;
++src; /* skip over the : */
}
--tkncnt; /* doesn't count if label */
} /* -- char is ':' */
} /* -- tkncnt == 0 */
++tkncnt; /* count the token */
} /* -- while all arguments */
inp_ptr = src; /* move the pointer */
if (show_line != 0)
line_to_listing();
*dst++ = 0; /* insert a terminator */
macro_pool_size -= dst-macro_pool;
macro_pool = dst;
} /* -- for all lines until .ENDM */
}
int op_macro( void ) /* define a macro */
{
char **key_pool,**def_pool;
uint8_t *gs_flag;
uint8_t *mac_pool;
Opcode *mac;
int size,tt;
uint32_t old_edmask;
Macargs *ma;
comma_expected = 2; /* comma is optional */
old_edmask = edmask;
edmask &= ~(ED_LC|ED_DOL);
tt = get_token();
edmask = old_edmask;
if (tt != TOKEN_strng)
{
bad_token(tkn_ptr,"Macro name must be a string beginning with an alpha");
f1_eatit();
return 0;
}
*(token_pool + max_opcode_length) = 0;
mac = opcode_lookup(token_pool,2);
if (mac == 0)
{
bad_token(tkn_ptr,"Unable to add macro name to PST");
f1_eatit();
return 0;
}
if (new_opcode != 0)
{
if (mac->op_name == token_pool)
{
token_pool += ++token_value; /* save the macro name if new */
token_pool_size -= token_value;
}
}
if (mac->op_class == OP_CLASS_MAC)
{
ma = mac->op_margs;
if (ma != 0)
{ /* discard any old definitions */
if (ma->mac_body != 0)
free_macbody(ma->mac_body);
MEM_free(ma);
}
}
mac->op_class = OP_CLASS_MAC; /* signal it's a macro now */
size = (inp_len-(inp_ptr-inp_str)+3)&-4/2; /* get length of remaining input */
if (size > 126) size = 126; /* maximum of 126 args */
tt = size*(2+ /* room for keywrds and defaults */
2*sizeof(char *)+ /* room for ptrs to keywrds */
/* and defaults */
1)+ /* room for gsflags */
sizeof(Macargs); /* room for macargs struct */
mac_pool = (uint8_t *)MEM_alloc(tt); /* pickup some memory */
ma = mac->op_margs = (Macargs *)mac_pool;
mac_pool += sizeof(Macargs);
key_pool = (char **)mac_pool; /* get space for keyword arg */
def_pool = key_pool+size; /* get space for default arg */
gs_flag = (uint8_t *)(def_pool+size); /* point to GS flag area */
mac_pool = gs_flag+size; /* point to area to copy args */
ma->mac_keywrd = key_pool;
ma->mac_default = def_pool;
ma->mac_gsflag = gs_flag;
ma->mac_numargs = 0; /* assume 0 arguments */
while (1)
{ /* first get the arguments */
int flg;
int c;
flg = 0;
while ((cttbl[(int)*inp_ptr]&CT_WS) != 0) ++inp_ptr; /* skip white space */
if (*inp_ptr == ',')
{
++inp_ptr; /* eat intervening commas */
while ((cttbl[(int)*inp_ptr]&CT_WS) != 0) ++inp_ptr; /* skip white space */
}
if (*inp_ptr == macro_arg_gensym)
{ /* if first char is a '?', gensym */
flg = 1;
++inp_ptr; /* and eat it */
}
if ((tt=get_token()) == EOL) break; /* pickup rest of arg */
if (tt != TOKEN_strng)
{
bad_token(tkn_ptr,"Illegal macro argument");
continue;
}
*gs_flag++ = flg;
strcpy((char *)mac_pool,token_pool);
*key_pool++ = (char *)mac_pool;
mac_pool += token_value+1;
if (*inp_ptr == '=')
{
int nest = 0;
char beg = macro_arg_open,end = macro_arg_close;
++inp_ptr; /* eat the '=' */
while ((cttbl[(int)*inp_ptr]&CT_WS) != 0) ++inp_ptr; /* skip over white space */
*def_pool++ = (char *)mac_pool;
if (*inp_ptr == macro_arg_escape) beg = end = *++inp_ptr;
if (*inp_ptr == beg)
{ /* delimited string? */
++inp_ptr; /* yep, eat the beginning char */
while (1)
{
if ((cttbl[c = *inp_ptr]&CT_EOL) != 0) break;
if (c == end)
{
--nest;
if (nest < 0) break;
}
else if (c == beg)
{
++nest;
}
*mac_pool++ = c;
++inp_ptr;
}
}
else
{
while (1)
{
c = *inp_ptr;
if (cttbl[c] & (CT_EOL|CT_SMC|CT_COM|CT_WS)) break;
*mac_pool++ = c;
++inp_ptr;
}
}
*mac_pool++ = 0;
}
else
{
*def_pool++ = 0; /* no default for this argument */
}
ma->mac_numargs += 1;
continue; /* next argument */
} /* -- while all arguments */
if (show_line != 0)
line_to_listing();
macro_pool_size = 0;
macro_pool = 0;
#ifdef DUMP_MACRO
puts_lis("\t\tBefore the macro_to_mem() call\n",1);
dump_macro(ma,mac);
#endif
if (macro_to_mem(ma,mac->op_name) == EOF)
{ /* process the macro body */
inp_str[0] = '\n'; /* display a blank line */
inp_str[1] = 0;
sprintf(emsg,"No terminating .ENDM for macro definition: %s",mac->op_name);
bad_token((char *)0,emsg);
return EOF;
}
#ifdef DUMP_MACRO
puts_lis("\t\tAfter the macro_to_mem() call\n",1);
dump_macro(ma,mac); /* display the macro */
#endif
return 0;
}
#ifndef MAC_PP
static void setup_mebstats( void )
{
meb_stats.getting_stuff = 1;
meb_stats.list_ptr = LLIST_OPC;
meb_stats.f1_flag = 0;
meb_stats.f2_flag = 0;
meb_stats.has_stuff = 1;
meb_stats.line_no = list_stats.line_no;
meb_stats.include_level = list_stats.include_level;
meb_stats.pc_flag = 0;
list_source.optTextBuf[0] = 0;
list_source.reqNewLine = 0;
list_source.srcPosition = limitSrcPosition(list_source.srcPositionQued);
/* detab the whole line */
deTab(inp_str, 8, 0, 0, meb_stats.listBuffer + list_source.srcPosition, sizeof(meb_stats.listBuffer) - list_source.srcPosition);
return;
}
#endif
int macro_call(Opcode *opc)
{
Macargs *ma;
char **keywrd,**defalt,**argptr,*args;
uint8_t *gensym;
Mcall_struct *mac_pool;
int argcnt,arglen; /* assume no args */
char **args_area;
if ( (ma = opc->op_margs) == 0 )
{
bad_token(tkn_ptr,"Undefined macro call. Fatal internal error.");
return 0;
}
if (macro_level == 0)
{
show_line = list_mc;
#ifndef MAC_PP
if (list_meb && !list_mes && !list_me)
setup_mebstats();
#endif
}
else
{
show_line = list_me & list_mc;
}
++macro_level; /* bump macro level */
++macro_nesting; /* keep 2 copies */
arglen = inp_len-(tkn_ptr-inp_str)+1; /* length of input args */
argcnt = ma->mac_numargs;
mac_pool = (Mcall_struct *)MEM_alloc((int)sizeof(Mcall_struct)+
arglen+ /* + size of args */
argcnt*((int)sizeof(char *)+ /* + room for ptrs to args */
7)); /* plus room for generated symbols */
mac_pool->marg_next = marg_head; /* save ptr to previous list head */
marg_head = mac_pool; /* this one becomes the top */
marg_head->marg_ptr = ma->mac_body; /* point to beginning of macro text */
marg_head->marg_top = ma->mac_body; /* point to beginning of macro text */
marg_head->marg_end = ma->mac_end; /* point to EOM */
keywrd = (char **)(marg_head+1); /* point to start of keyword args */
marg_head->marg_flag = 0; /* ordinary macro */
marg_head->marg_count = 0; /* loop count is 0 on ordinary macro */
marg_head->marg_icount = 0; /* loop count is 0 on ordinary macro */
marg_head->marg_args = keywrd; /* ptr to arguments */
marg_head->marg_cndlvl = condit_level;
marg_head->marg_cndnst = condit_nest;
marg_head->marg_cndwrd = condit_word;
marg_head->marg_cndpol = condit_polarity;
condit_level = 0;
condit_nest = 0;
condit_word = 0;
condit_polarity = 0;
args = (char *)(keywrd+ma->mac_numargs); /* point to argument area */
args_area = argptr = (char **)MEM_alloc(argcnt*(int)sizeof(char *));
for (argcnt = 0; argcnt < ma->mac_numargs; ++argcnt)
{
int c,beg, end, altBeg;
beg = macro_arg_open;
end = macro_arg_close;
altBeg = 0;
*argptr = 0; /* assume no argument */
while (myIsspace((int)*inp_ptr))
++inp_ptr; /* skip over white space */
c = *inp_ptr; /* pick up character */
if ((cttbl[c]&(CT_EOL|CT_COM|CT_SMC)) != 0)
{
goto term_arg; /* go terminate the argument (eats the comma if there is one) */
}
*argptr = args; /* point to argument */
if (c == macro_arg_genval)
{ /* special value to ascii? */
char *ep;
int unpack_radix;
++inp_ptr; /* yep, eat the escape char */
unpack_radix = 0;
if (*inp_ptr == macro_arg_genval)
{
c = *(inp_ptr+1); /* get requested radix */
c = _toupper(c); /* upcase the char */
if (c == 'O') unpack_radix = 8;
if (c == 'D') unpack_radix = 10;
if (c == 'H') unpack_radix = 16;
if (c == 'X') unpack_radix = 16;
if (unpack_radix != 0)
{
inp_ptr += 2; /* eat both items */
}
}
if (unpack_radix == 0) unpack_radix = current_radix;
get_token(); /* setup the variables */
ep = tkn_ptr; /* start at current input */
while ((cttbl[(int)*ep]&(CT_EOL|CT_WS|CT_COM)) == 0) ++ep; /* find end */
if ((cttbl[(int)*ep]&CT_WS) != 0)
{ /* if white space then */
char sv1,sv2;
sv1 = *ep; /* save the ws char */
*ep++ = '\n'; /* replace with nl */
sv2 = *ep; /* save the following char too */
*ep = 0; /* and replace it with a 0 */
exprs(0,&EXP0); /* pickup an absolute expression */
*ep = sv2; /* restore chars */
*--ep = sv1;
}
else
{
exprs(0,&EXP0); /* pickup an absolute expression */
}
if (unpack_radix < 10)
{
sprintf(args,"%o",EXP0SP->expr_value);
}
else if (unpack_radix < 16)
{
sprintf(args,"%d",EXP0SP->expr_value);
}
else
{
uint32_t tv;
tv = EXP0SP->expr_value;
while (tv > 15) tv >>= 4;
if (tv > 9)
{
sprintf(args,"0%X",EXP0SP->expr_value);
}
else
{
sprintf(args,"%X",EXP0SP->expr_value);
}
}
args += strlen(args); /* position to end of string */
goto term_arg; /* and terminate the arg */
}
if (c == macro_arg_escape)
{ /* argument escape? */
++inp_ptr; /* yep, eat it */
c = end = beg = *inp_ptr; /* and set the new delimiters */
altBeg = 0;
}
if ( c == '\'' && (edmask & ED_Q_MARG) )
{
altBeg = '\'';
beg = altBeg;
end = beg;
}
if ( c == beg )
{ /* special delimiter character */
int nest = 0;
++inp_ptr; /* eat it */
while (1)
{ /* copy string */
c = *inp_ptr; /* pickup next char */
if ((cttbl[c]&CT_EOL) != 0) goto term_arg;
if (c == end)
{ /* match end delimiter? */
--nest; /* yep, de-nest */
if (nest < 0)
{ /* end of term? */
++inp_ptr; /* yep, eat the terminator character */
goto term_arg; /* and terminate the argument */
}
}
else if ( c == beg)
{
++nest; /* bump the nest level */
}
*args++ = c; /* pass the character */
++inp_ptr;
}
}
else
{ /* not escaped */
int not_kw = 1;
char *tpp;
tpp = token_pool; /* point to temp space for upcased token */
while (1)
{ /* copy string */
c = *inp_ptr; /* pickup next char */
if ((cttbl[c]&(CT_EOL|CT_WS|CT_COM|CT_SMC)) != 0)
goto term_arg;
*tpp++ = _toupper(c); /* upcase the keyword */
if (not_kw && c == '=')
{ /* asking for keyword parameter? */
not_kw = 0; /* signal already through here */
*(tpp-1) = 0; /* null terminate the string */
c = scan_margs(ma->mac_numargs,ma->mac_keywrd,token_pool);
if ((c&0x80) != 0)
{
++inp_ptr; /* eat the = */
args = *argptr; /* reset the pointers */
tpp = token_pool;
*(keywrd+(c&0x7F)) = args;
*argptr = 0; /* no regular argument */
continue; /* and keep going */
}
*tpp = '='; /* put the char back */
c = *inp_ptr; /* and restore c */
}
*args++ = c; /* pass the character */
++inp_ptr;
}
}
term_arg:
*args++ = 0; /* null terminate the string */
++argptr; /* skip to next arg */
while ((cttbl[c = *inp_ptr]&CT_WS) != 0)
++inp_ptr; /* skip over trailing white space */
if (c == ',')
++inp_ptr; /* eat trailing comma */
if ((cttbl[c]&CT_EOL) != 0)
break;
}
argptr = args_area; /* backup to beginning of dummy arg area */
keywrd = (char **)(marg_head+1); /* point to start of keyword args */
defalt = ma->mac_default; /* point to default list */
gensym = ma->mac_gsflag; /* point to generated symbol flag */
for (argcnt = 0; argcnt < ma->mac_numargs; ++argcnt)
{
if (*keywrd == 0)
{ /* user pass a keyword argument? */
if (*argptr == 0)
{ /* nope, user pass positional argument? */
if (*gensym != 0)
{ /* generated symbol */
*keywrd = args; /* point to argument */
sprintf(args,"%d",autogen_lsb);
++autogen_lsb; /* bump generated symbol number */
args += strlen(args);
*args++ = '$'; /* make it a local symbol */
*args++ = 0; /* null terminate it */
}
else
{
if (*defalt != 0)
{
*keywrd = *defalt; /* nope, use the default */
}
else
{
*keywrd = ""; /* else nothing */
}
}
}
else
{
*keywrd = *argptr; /* else use the next positional argument */
}
}
if (strlen(*keywrd) != 0) marg_head->marg_icount += 1;
++argptr; /* bump to next argumnet pointer */
++keywrd; /* advance to next argument */
++defalt; /* and its cooresponding default */
++gensym; /* next generated symbol flag */
}
MEM_free((char *)args_area); /* free the temp arg list */
return 1;
}
int op_irp( void ) /* .IRP macro */
{
char **key_pool,**def_pool,*dst;
uint8_t *gs_flag;
char *macro_name;
uint8_t *mac_pool;
int size,tt;
Mcall_struct *marg;
Macargs *ma;
tt = get_token();
if (tt != TOKEN_strng)
{
bad_token(tkn_ptr,"IRP dummy argument must be a string beginning with an alpha");
}
size = (inp_len-(inp_ptr-inp_str)+3)&-4/2; /* get length of remaining input */
if (size > 126) size = 126; /* maximum of 126 args */
#define IRP_NAME ".IRP %s,<...>"
tt = size*(2+ /* room for keywrds and defaults */
2*sizeof(char *)+ /* room for ptrs to keywrds */
/* and defaults */
1)+ /* room for gsflags */
sizeof(Mcall_struct)+ /* room for mcall struct */
sizeof(Macargs)+ /* room for macargs struct */
2*token_value+sizeof(IRP_NAME); /* room for macro name and argument name */
mac_pool = MEM_alloc(tt); /* pickup some memory */
marg = (Mcall_struct *)mac_pool; /* point to argument area */
ma = (Macargs *)(marg+1); /* pointer to dummy macargs */
key_pool = (char **)(ma+1); /* get space for arg ptrs */
def_pool = key_pool+size; /* get space for default arg */
gs_flag = (uint8_t *)(def_pool+size); /* point to GS flag area */
mac_pool = gs_flag+size; /* point to next free area */
macro_name = (char *)mac_pool; /* point to area for macro name */
sprintf(macro_name,IRP_NAME,token_pool); /* create macro name */
mac_pool += strlen(macro_name)+1;
ma->mac_keywrd = key_pool;
ma->mac_default = def_pool;
ma->mac_gsflag = gs_flag;
ma->mac_numargs = 1; /* .IRP has only 1 replaceable argument */
*key_pool++ = (char *)mac_pool; /* pointer to dummy argument */
strcpy((char *)mac_pool,token_pool); /* copy in dummy argument name */
mac_pool += token_value+1; /* move pointer */
marg->marg_args = key_pool; /* pointer to array of ptrs to args */
while (myIsspace(*inp_ptr)) ++inp_ptr; /* skip over white space */
if (*inp_ptr != ',')
{ /* next thing must be a comma */
show_bad_token(inp_ptr,"Expected a comma here. One is assumed", MSG_WARN);
}
else
{
++inp_ptr; /* eat the comma */
while (myIsspace(*inp_ptr)) ++inp_ptr; /* skip over white space */
}
if (*inp_ptr != macro_arg_open)
{ /* next thing must be open brace */
show_bad_token(inp_ptr,"Expected a macro_arg_open here. One is assumed", MSG_WARN);
}
else
{
++inp_ptr; /* eat opening bracket */
}
dst = (char *)mac_pool; /* point to start of free space */
*key_pool++ = dst; /* space for replacement argument ptr */
while (1)
{ /* for all arguments */
int nest;
int c,beg,end;
c = *inp_ptr;
if (c == '\n' || c == 0)
{
bad_token(inp_ptr,"Expected a macro_arg_close here. One is assumed.");
break;
}
beg = macro_arg_open;
end = macro_arg_close;
while (myIsspace(*inp_ptr)) ++inp_ptr; /* skip over white space */
if (*inp_ptr == '^')
{ /* arg delim escape? */
c = *++inp_ptr; /* yep, eat it and get the next*/
if (c == '\n' || c == 0)
{
--inp_ptr;
bad_token(inp_ptr,"Premature end of line");
break;
}
beg = end = c; /* pass the delimiter */
}
*key_pool++ = dst; /* point to place to deposit string */
nest = 0; /* nest the search */
if (*inp_ptr == beg)
{ /* starting with a bracket */
++inp_ptr; /* eat the starting char */
while (1)
{ /* for all chars in argument */
c = *inp_ptr; /* pickup next char */
if (cttbl[c]&CT_EOL) break; /* break if EOL */
++inp_ptr; /* eat the char */
if (c == end)
{ /* find an end? */
--nest; /* yep */
if (nest < 0)
{ /* all of them? */
break; /* yep, we're done then */
}
}
else if (c == beg)
{
++nest; /* bump nest level */
}
*dst++ = c; /* pass the char */
}
}
else
{
while (1)
{ /* for all chars in argument */
c = *inp_ptr; /* pickup next char */
if ((cttbl[c]&(CT_EOL|CT_WS|CT_COM)) != 0) break; /* break if EOL */
if (c == macro_arg_close)
{ /* trailing bracket */
--nest;
if (nest < 0) break; /* found end of string */
}
else
{
if (c == macro_arg_open) ++nest; /* count inner brackets */
}
inp_ptr++; /* its ok, so eat it */
*dst++ = c; /* pass it */
}
}
*dst++ = 0; /* terminate the string */
while (myIsspace(*inp_ptr)) ++inp_ptr; /* skip over white space */
if (*inp_ptr == macro_arg_close)
{ /* terminate on closing brace? */
++inp_ptr; /* yep, eat it */
break; /* and we're done */
}
if (*inp_ptr == ',') ++inp_ptr; /* skip optional comma */
} /* -- while all arguments */
marg->marg_icount = key_pool-marg->marg_args-1;
f1_eol(); /* should be a EOL here */
if (show_line != 0)
line_to_listing(); /* display it */
macro_pool = 0;
macro_pool_size = 0; /* start with fresh memory */
#ifdef DUMP_MACRO
puts_lis("\t\tBefore the .irp macro_to_mem() call\n",1);
dump_macro(ma,(Opcode *)0);
#endif
if (macro_to_mem(ma,macro_name) == EOF)
{ /* process the macro body */
inp_str[0] = '\n'; /* display a blank line */
inp_str[1] = 0;
sprintf(emsg,"No terminating .ENDR for directive: %s",macro_name);
bad_token((char *)0,emsg);
return EOF;
}
++macro_level; /* bump macro level */
++macro_nesting; /* keep 2 copies */
marg->marg_next = marg_head; /* save ptr to previous list head */
marg_head = marg; /* this one becomes the top */
marg_head->marg_ptr = ma->mac_body; /* point to beginning of macro text */
marg_head->marg_top = ma->mac_body; /* point to beginning of macro text */
marg_head->marg_end = ma->mac_end; /* point to EOM */
marg_head->marg_flag = 2; /* .IRP type macro */
marg_head->marg_count = 0; /* loop count is 0 for first argument */
marg_head->marg_cndlvl = condit_level;
marg_head->marg_cndnst = condit_nest;
marg_head->marg_cndwrd = condit_word;
marg_head->marg_cndpol = condit_polarity;
condit_level = 0;
condit_nest = 0;
condit_word = 0;
condit_polarity = 0;
#ifdef DUMP_MACRO
puts_lis("\t\tAfter the .irp macro_to_mem() call\n",1);
dump_macro(ma,(Opcode *)0); /* display the macro */
#endif
if (macro_level == 1)
{
#ifndef MAC_PP
if (list_meb && !list_mes) setup_mebstats();
#endif
/* list_mes = 1; */ /* make sure the .ENDR shows up */
}
return 0; /* continue with macro next */
}
int op_irpc( void ) /* .IRPC macro */
{
char **key_pool,**def_pool,*dst,*first_targ;
uint8_t *gs_flag;
char *macro_name;
uint8_t *mac_pool;
int c,size,tt,nest;
Mcall_struct *marg;
Macargs *ma;