/
c80.c
2022 lines (1963 loc) · 43.8 KB
/
c80.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
/************************************************/
/* */
/* small-c compiler */
/* rev. 1.1 */
/* by Ron Cain */
/* */
/************************************************/
/* Define system dependent parameters */
/* Stand-alone definitions */
#define NULL 0
/* #define eol 13 */
/* UNIX definitions (if not stand-alone) */
/* #include <stdio.h> */
#define eol 10
/* Define the symbol table parameters */
#define symsiz 14
#define symtbsz 5040
#define numglbs 300
#define startglb symtab
#define endglb startglb+numglbs*symsiz
#define startloc endglb+symsiz
#define endloc symtab+symtbsz-symsiz
/* Define symbol table entry format */
#define name 0
#define ident 9
#define type 10
#define storage 11
#define offset 12
/* System wide name size (for symbols) */
#define namesize 9
#define namemax 8
/* Define possible entries for "ident" */
#define variable 1
#define array 2
#define pointer 3
#define function 4
/* Define possible entries for "type" */
#define cchar 1
#define cint 2
/* Define possible entries for "storage" */
#define statik 1
#define stkloc 2
/* Define the "while" statement queue */
#define wqtabsz 100
#define wqsiz 4
#define wqmax wq+wqtabsz-wqsiz
/* Define entry offsets in while queue */
#define wqsym 0
#define wqsp 1
#define wqloop 2
#define wqlab 3
/* Define the literal pool */
#define litabsz 2000
#define litmax litabsz-1
/* Define the input line */
#define linesize 80
#define linemax linesize-1
#define mpmax linemax
/* Define the macro (define) pool */
#define macqsize 1000
#define macmax macqsize-1
/* Define statement types (tokens) */
#define stif 1
#define stwhile 2
#define streturn 3
#define stbreak 4
#define stcont 5
#define stasm 6
#define stexp 7
/* Now reserve some storage words */
char symtab[symtbsz]; /* symbol table */
char *glbptr,*locptr; /* ptrs to next entries */
int wq[wqtabsz]; /* while queue */
int *wqptr; /* ptr to next entry */
char litq[litabsz]; /* literal pool */
int litptr; /* ptr to next entry */
char macq[macqsize]; /* macro string buffer */
int macptr; /* and its index */
char line[linesize]; /* parsing buffer */
char mline[linesize]; /* temp macro buffer */
int lptr,mptr; /* ptrs into each */
/* Misc storage */
int nxtlab, /* next avail label # */
litlab, /* label # assigned to literal pool */
sp, /* compiler relative stk ptr */
argstk, /* function arg sp */
ncmp, /* # open compound statements */
errcnt, /* # errors in compilation */
eof, /* set non-zero on final input eof */
input, /* iob # for input file */
output, /* iob # for output file (if any) */
input2, /* iob # for "include" file */
glbflag, /* non-zero if internal globals */
ctext, /* non-zero to intermix c-source */
cmode, /* non-zero while parsing c-code */
/* zero when passing assembly code */
lastst; /* last executed statement type */
char quote[2]; /* literal string for '"' */
char *cptr; /* work ptr to any char buffer */
int *iptr; /* work ptr to any int buffer */
/* >>>>> start cc1 <<<<<< */
/* */
/* Compiler begins execution here */
/* */
main()
{
glbptr=startglb; /* clear global symbols */
locptr=startloc; /* clear local symbols */
wqptr=wq; /* clear while queue */
macptr= /* clear the macro pool */
litptr= /* clear literal pool */
sp = /* stack ptr (relative) */
errcnt= /* no errors */
eof= /* not eof yet */
input= /* no input file */
input2= /* or include file */
output= /* no open units */
ncmp= /* no open compound states */
lastst= /* no last statement yet */
quote[1]=
0; /* ...all set to zero.... */
quote[0]='"'; /* fake a quote literal */
cmode=1; /* enable preprocessing */
/* */
/* compiler body */
/* */
ask(); /* get user options */
openout(); /* get an output file */
openin(); /* and initial input file */
header(); /* intro code */
parse(); /* process ALL input */
dumplits(); /* then dump literal pool */
dumpglbs(); /* and all static memory */
errorsummary(); /* summarize errors */
trailer(); /* follow-up code */
closeout(); /* close the output (if any) */
return; /* then exit to system */
}
/* */
/* Process all input text */
/* */
/* At this level, only static declarations, */
/* defines, includes, and function */
/* definitions are legal... */
parse()
{
while (eof==0) /* do until no more input */
{
if(amatch("char",4)){declglb(cchar);ns();}
else if(amatch("int",3)){declglb(cint);ns();}
else if(match("#asm"))doasm();
else if(match("#include"))doinclude();
else if(match("#define"))addmac();
else newfunc();
blanks(); /* force eof if pending */
}
}
/* */
/* Dump the literal pool */
/* */
dumplits()
{int j,k;
if (litptr==0) return; /* if nothing there, exit...*/
printlabel(litlab);col(); /* print literal label */
k=0; /* init an index... */
while (k < litptr) /* to loop with */
{defbyte(); /* pseudo-op to define byte */
j=10; /* max bytes per line */
while(j--)
{outdec((litq[k++]&127));
if ((j==0) | (k>=litptr))
{nl(); /* need <cr> */
break;
}
outbyte(','); /* separate bytes */
}
}
}
/* */
/* Dump all static variables */
/* */
dumpglbs()
{
int j;
if(glbflag==0)return; /* don't if user said no */
cptr=startglb;
while(cptr < glbptr)
{if(cptr[ident]!=function)
/* do if anything but function */
{outstr(cptr);col();
/* output name as label... */
defstorage(); /* define storage */
j=((cptr[offset]&255)+
((cptr[offset+1]&255)<<8));
/* calc # bytes */
if((cptr[type]==cint)|
(cptr[ident]==pointer))
j=j+1;
outdec(j); /* need that many */
nl();
}
cptr=cptr+symsiz;
}
}
/* */
/* Report errors for user */
/* */
errorsummary()
{
/* see if anything left hanging... */
if (ncmp) error("missing closing bracket");
/* open compound statement ... */
nl();
comment();
outdec(errcnt); /* total # errors */
outstr(" errors in compilation.");
nl();
}
/* */
/* Get options from user */
/* */
ask()
{
int k,num[1];
kill(); /* clear input line */
outbyte(12); /* clear the screen */
nl();nl();nl(); /* print banner */
pl(" * * * small-c compiler * * *");
nl();
pl(" by Ron Cain");
nl();nl();
/* see if user wants to interleave the c-text */
/* in form of comments (for clarity) */
pl("Do you want the c-text to appear? ");
gets(line); /* get answer */
ctext=0; /* assume no */
if((ch()=='Y')|(ch()=='y'))
ctext=1; /* user said yes */
/* see if user wants us to allocate static */
/* variables by name in this module */
/* (pseudo external capability) */
pl("Do you wish the globals to be defined? ");
gets(line);
glbflag=0;
if((ch()=='Y')|(ch()=='y'))
glbflag=1; /* user said yes */
/* get first allowable number for compiler-generated */
/* labels (in case user will append modules) */
while(1){
pl("Starting number for labels? ");
gets(line);
if(ch()==0){num[0]=0;break;}
if(k=number(num))break;
}
nxtlab=num[0];
litlab=getlabel(); /* first label=literal pool */
kill(); /* erase line */
}
/* */
/* Get output filename */
/* */
openout()
{
kill(); /* erase line */
output=0; /* start with none */
pl("Output filename? "); /* ask...*/
gets(line); /* get a filename */
if(ch()==0)return; /* none given... */
if((output=fopen(line,"w"))==NULL) /* if given, open */
{output=0; /* can't open */
error("Open failure!");
}
kill(); /* erase line */
}
/* */
/* Get (next) input file */
/* */
openin()
{
input=0; /* none to start with */
while(input==0){ /* any above 1 allowed */
kill(); /* clear line */
if(eof)break; /* if user said none */
pl("Input filename? ");
gets(line); /* get a name */
if(ch()==0)
{eof=1;break;} /* none given... */
if((input=fopen(line,"r"))==NULL)
{input=0; /* can't open it */
pl("Open failure");
}
}
kill(); /* erase line */
}
/* */
/* Open an include file */
/* */
doinclude()
{
blanks(); /* skip over to name */
if((input2=fopen(line+lptr,"r"))==NULL)
{input2=0;
error("Open failure on include file");
}
kill(); /* clear rest of line */
/* so next read will come from */
/* new file (if open */
}
/* */
/* Close the output file */
/* */
closeout()
{
if(output)fclose(output); /* if open, close it */
output=0; /* mark as closed */
}
/* */
/* Declare a static variable */
/* (i.e. define for use) */
/* */
/* makes an entry in the symbol table so subsequent */
/* references can call symbol by name */
declglb(typ) /* typ is cchar or cint */
int typ;
{ int k,j;char sname[namesize];
while(1)
{while(1)
{if(endst())return; /* do line */
k=1; /* assume 1 element */
if(match("*")) /* pointer ? */
j=pointer; /* yes */
else j=variable; /* no */
if (symname(sname)==0) /* name ok? */
illname(); /* no... */
if(findglb(sname)) /* already there? */
multidef(sname);
if (match("[")) /* array? */
{k=needsub(); /* get size */
if(k)j=array; /* !0=array */
else j=pointer; /* 0=ptr */
}
addglb(sname,j,typ,k); /* add symbol */
break;
}
if (match(",")==0) return; /* more? */
}
}
/* */
/* Declare local variables */
/* (i.e. define for use) */
/* */
/* works just like "declglb" but modifies machine stack */
/* and adds symbol table entry with appropriate */
/* stack offset to find it again */
declloc(typ) /* typ is cchar or cint */
int typ;
{
int k,j;char sname[namesize];
while(1)
{while(1)
{if(endst())return;
if(match("*"))
j=pointer;
else j=variable;
if (symname(sname)==0)
illname();
if(findloc(sname))
multidef(sname);
if (match("["))
{k=needsub();
if(k)
{j=array;
if(typ==cint)k=k+k;
}
else
{j=pointer;
k=2;
}
}
else
if((typ==cchar)
&(j!=pointer))
k=1;else k=2;
/* change machine stack */
sp=modstk(sp-k);
addloc(sname,j,typ,sp);
break;
}
if (match(",")==0) return;
}
}
/* >>>>>> start of cc2 <<<<<<<< */
/* */
/* Get required array size */
/* */
/* invoked when declared variable is followed by "[" */
/* this routine makes subscript the absolute */
/* size of the array. */
needsub()
{
int num[1];
if(match("]"))return 0; /* null size */
if (number(num)==0) /* go after a number */
{error("must be constant"); /* it isn't */
num[0]=1; /* so force one */
}
if (num[0] < 0)
{error("negative size illegal");
num[0]=(-num[0]);
}
needbrack("]"); /* force single dimension */
return num[0]; /* and return size */
}
/* */
/* Begin a function */
/* */
/* Called from "parse" this routine tries to make a function */
/* out of what follows. */
newfunc()
{
char n[namesize],*ptr;
if (symname(n)==0)
{error("illegal function or declaration");
kill(); /* invalidate line */
return;
}
if(ptr=findglb(n)) /* already in symbol table ? */
{if(ptr[ident]!=function)multidef(n);
/* already variable by that name */
else if(ptr[offset]==function)multidef(n);
/* already function by that name */
else ptr[offset]=function;
/* otherwise we have what was earlier*/
/* assumed to be a function */
}
/* if not in table, define as a function now */
else addglb(n,function,cint,function);
/* we had better see open paren for args... */
if(match("(")==0)error("missing open paren");
outstr(n);col();nl(); /* print function name */
argstk=0; /* init arg count */
while(match(")")==0) /* then count args */
/* any legal name bumps arg count */
{if(symname(n))argstk=argstk+2;
else{error("illegal argument name");junk();}
blanks();
/* if not closing paren, should be comma */
if(streq(line+lptr,")")==0)
{if(match(",")==0)
error("expected comma");
}
if(endst())break;
}
locptr=startloc; /* "clear" local symbol table */
sp=0; /* preset stack ptr */
while(argstk)
/* now let user declare what types of things */
/* those arguments were */
{if(amatch("char",4)){getarg(cchar);ns();}
else if(amatch("int",3)){getarg(cint);ns();}
else{error("wrong number args");break;}
}
if(statement()!=streturn) /* do a statement, but if */
/* it's a return, skip */
/* cleaning up the stack */
{modstk(0);
ret();
}
sp=0; /* reset stack ptr again */
locptr=startloc; /* deallocate all locals */
}
/* */
/* Declare argument types */
/* */
/* called from "newfunc" this routine adds an entry in the */
/* local symbol table for each named argument */
getarg(t) /* t = cchar or cint */
int t;
{
char n[namesize],c;int j;
while(1)
{if(argstk==0)return; /* no more args */
if(match("*"))j=pointer;
else j=variable;
if(symname(n)==0) illname();
if(findloc(n)) multidef(n);
if(match("[")) /* pointer ? */
/* it is a pointer, so skip all */
/* stuff between "[]" */
{while(inbyte()!=']')
if(endst())break;
j=pointer;
/* add entry as pointer */
}
addloc(n,j,t,argstk);
argstk=argstk-2; /* cnt down */
if(endst())return;
if(match(",")==0)error("expected comma");
}
}
/* */
/* Statement parser */
/* */
/* called whenever syntax requires */
/* a statement. */
/* this routine performs that statement */
/* and returns a number telling which one */
statement()
{
if ((ch()==0) & (eof)) return;
else if(amatch("char",4))
{declloc(cchar);ns();}
else if(amatch("int",3))
{declloc(cint);ns();}
else if(match("{"))compound();
else if(amatch("if",2))
{doif();lastst=stif;}
else if(amatch("while",5))
{dowhile();lastst=stwhile;}
else if(amatch("return",6))
{doreturn();ns();lastst=streturn;}
else if(amatch("break",5))
{dobreak();ns();lastst=stbreak;}
else if(amatch("continue",8))
{docont();ns();lastst=stcont;}
else if(match(";"));
else if(match("#asm"))
{doasm();lastst=stasm;}
/* if nothing else, assume it's an expression */
else{expression();ns();lastst=stexp;}
return lastst;
}
/* */
/* Semicolon enforcer */
/* */
/* called whenever syntax requires a semicolon */
ns() {if(match(";")==0)error("missing semicolon");}
/* */
/* Compound statement */
/* */
/* allow any number of statements to fall between "{}" */
compound()
{
++ncmp; /* new level open */
while (match("}")==0)
if(eof) return;
else statement();
--ncmp; /* close current level */
}
/* */
/* "if" statement */
/* */
doif()
{
int flev,fsp,flab1,flab2;
flev=locptr; /* record current local level */
fsp=sp; /* record current stk ptr */
flab1=getlabel(); /* get label for false branch */
test(flab1); /* get expression, and branch false */
statement(); /* if true, do a statement */
sp=modstk(fsp); /* then clean up the stack */
locptr=flev; /* and deallocate any locals */
if (amatch("else",4)==0) /* if...else ? */
/* simple "if"...print false label */
{printlabel(flab1);col();nl();
return; /* and exit */
}
/* an "if...else" statement. */
jump(flab2=getlabel()); /* jump around false code */
printlabel(flab1);col();nl(); /* print false label */
statement(); /* and do "else" clause */
sp=modstk(fsp); /* then clean up stk ptr */
locptr=flev; /* and deallocate locals */
printlabel(flab2);col();nl(); /* print true label */
}
/* */
/* "while" statement */
/* */
dowhile()
{
int wq[4]; /* allocate local queue */
wq[wqsym]=locptr; /* record local level */
wq[wqsp]=sp; /* and stk ptr */
wq[wqloop]=getlabel(); /* and looping label */
wq[wqlab]=getlabel(); /* and exit label */
addwhile(wq); /* add entry to queue */
/* (for "break" statement) */
printlabel(wq[wqloop]);col();nl(); /* loop label */
test(wq[wqlab]); /* see if true */
statement(); /* if so, do a statement */
jump(wq[wqloop]); /* loop to label */
printlabel(wq[wqlab]);col();nl(); /* exit label */
locptr=wq[wqsym]; /* deallocate locals */
sp=modstk(wq[wqsp]); /* clean up stk ptr */
delwhile(); /* delete queue entry */
}
/* */
/* "return" statement */
/* */
doreturn()
{
/* if not end of statement, get an expression */
if(endst()==0)expression();
modstk(0); /* clean up stk */
ret(); /* and exit function */
}
/* */
/* "break" statement */
/* */
dobreak()
{
int *ptr;
/* see if any "whiles" are open */
if ((ptr=readwhile())==0) return; /* no */
modstk((ptr[wqsp])); /* else clean up stk ptr */
jump(ptr[wqlab]); /* jump to exit label */
}
/* */
/* "continue" statement */
/* */
docont()
{
int *ptr;
/* see if any "whiles" are open */
if ((ptr=readwhile())==0) return; /* no */
modstk((ptr[wqsp])); /* else clean up stk ptr */
jump(ptr[wqloop]); /* jump to loop label */
}
/* */
/* "asm" pseudo-statement */
/* */
/* enters mode where assembly language statement are */
/* passed intact through parser */
doasm()
{
cmode=0; /* mark mode as "asm" */
while (1)
{inline2(); /* get and print lines */
if (match("#endasm")) break; /* until... */
if(eof)break;
outstr(line);
nl();
}
kill(); /* invalidate line */
cmode=1; /* then back to parse level */
}
/* >>>>> start of cc3 <<<<<<<<< */
/* */
/* Perform a function call */
/* */
/* called from heir11, this routine will either call */
/* the named function, or if the supplied ptr is */
/* zero, will call the contents of HL */
callfunction(ptr)
char *ptr; /* symbol table entry (or 0) */
{ int nargs;
nargs=0;
blanks(); /* already saw open paren */
if(ptr==0)push(); /* calling HL */
while(streq(line+lptr,")")==0)
{if(endst())break;
expression(); /* get an argument */
if(ptr==0)swapstk(); /* don't push addr */
push(); /* push argument */
nargs=nargs+2; /* count args*2 */
if (match(",")==0) break;
}
needbrack(")");
if(ptr)call(ptr);
else callstk();
sp=modstk(sp+nargs); /* clean up arguments */
}
junk()
{ if(an(inbyte()))
while(an(ch()))gch();
else while(an(ch())==0)
{if(ch()==0)break;
gch();
}
blanks();
}
endst()
{ blanks();
return ((streq(line+lptr,";")|(ch()==0)));
}
illname()
{ error("illegal symbol name");junk();}
multidef(sname)
char *sname;
{ error("already defined");
comment();
outstr(sname);nl();
}
needbrack(str)
char *str;
{ if (match(str)==0)
{error("missing bracket");
comment();outstr(str);nl();
}
}
needlval()
{ error("must be lvalue");
}
findglb(sname)
char *sname;
{ char *ptr;
ptr=startglb;
while(ptr!=glbptr)
{if(astreq(sname,ptr,namemax))return ptr;
ptr=ptr+symsiz;
}
return 0;
}
findloc(sname)
char *sname;
{ char *ptr;
ptr=startloc;
while(ptr!=locptr)
{if(astreq(sname,ptr,namemax))return ptr;
ptr=ptr+symsiz;
}
return 0;
}
addglb(sname,id,typ,value)
char *sname,id,typ;
int value;
{ char *ptr;
if(cptr=findglb(sname))return cptr;
if(glbptr>=endglb)
{error("global symbol table overflow");
return 0;
}
cptr=ptr=glbptr;
while(an(*ptr++ = *sname++)); /* copy name */
cptr[ident]=id;
cptr[type]=typ;
cptr[storage]=statik;
cptr[offset]=value;
cptr[offset+1]=value>>8;
glbptr=glbptr+symsiz;
return cptr;
}
addloc(sname,id,typ,value)
char *sname,id,typ;
int value;
{ char *ptr;
if(cptr=findloc(sname))return cptr;
if(locptr>=endloc)
{error("local symbol table overflow");
return 0;
}
cptr=ptr=locptr;
while(an(*ptr++ = *sname++)); /* copy name */
cptr[ident]=id;
cptr[type]=typ;
cptr[storage]=stkloc;
cptr[offset]=value;
cptr[offset+1]=value>>8;
locptr=locptr+symsiz;
return cptr;
}
/* Test if next input string is legal symbol name */
symname(sname)
char *sname;
{ int k;char c;
blanks();
if(alpha(ch())==0)return 0;
k=0;
while(an(ch()))sname[k++]=gch();
sname[k]=0;
return 1;
}
/* Return next avail internal label number */
getlabel()
{ return(++nxtlab);
}
/* Print specified number as label */
printlabel(label)
int label;
{ outstr("cc");
outdec(label);
}
/* Test if given character is alpha */
alpha(c)
char c;
{ c=c&127;
return(((c>='a')&(c<='z'))|
((c>='A')&(c<='Z'))|
(c=='_'));
}
/* Test if given character is numeric */
numeric(c)
char c;
{ c=c&127;
return((c>='0')&(c<='9'));
}
/* Test if given character is alphanumeric */
an(c)
char c;
{ return((alpha(c))|(numeric(c)));
}
/* Print a carriage return and a string only to console */
pl(str)
char *str;
{ int k;
k=0;
putchar(eol);
while(str[k])putchar(str[k++]);
}
addwhile(ptr)
int ptr[];
{
int k;
if (wqptr==wqmax)
{error("too many active whiles");return;}
k=0;
while (k < wqsiz)
{*wqptr++ = ptr[k++];}
}
delwhile()
{if(readwhile()) wqptr=wqptr-wqsiz;
}
readwhile()
{
if (wqptr==wq){error("no active whiles");return 0;}
else return (wqptr-wqsiz);
}
ch()
{ return(line[lptr]&127);
}
nch()
{ if(ch()==0)return 0;
else return(line[lptr+1]&127);
}
gch()
{ if(ch()==0)return 0;
else return(line[lptr++]&127);
}
kill()
{ lptr=0;
line[lptr]=0;
}
inbyte()
{
while(ch()==0)
{if (eof) return 0;
inline2();
preprocess();
}
return gch();
}
inchar()
{
if(ch()==0)inline2();
if(eof)return 0;
return(gch());
}
inline2()
{
int k,unit;
while(1)
{if (input==0)openin();
if(eof)return;
if((unit=input2)==0)unit=input;
kill();
while((k=getc(unit))>0)
{if((k==eol)|(lptr>=linemax))break;
line[lptr++]=k;
}
line[lptr]=0; /* append null */
if(k<=0)
{fclose(unit);
if(input2)input2=0;
else input=0;
}
if(lptr)
{if((ctext)&(cmode))
{comment();
outstr(line);
nl();
}
lptr=0;
return;
}
}
}
/* >>>>>> start of cc4 <<<<<<< */
keepch(c)
char c;
{ mline[mptr]=c;
if(mptr < mpmax)mptr++;
return c;
}
preprocess()
{ int k;
char c,sname[namesize];
if(cmode==0)return;
mptr=lptr=0;
while(ch())
{if((ch()==' ')|(ch()==9))
{keepch(' ');
while((ch()==' ')|
(ch()==9))
gch();
}
else if(ch()=='"')
{keepch(ch());
gch();
while(ch()!='"')
{if(ch()==0)
{error("missing quote");
break;
}
keepch(gch());
}
gch();
keepch('"');
}
else if(ch()==39)
{keepch(39);
gch();
while(ch()!=39)
{if(ch()==0)
{error("missing apostrophe");
break;
}
keepch(gch());
}
gch();
keepch(39);
}
else if((ch()=='/')&(nch()=='*'))
{inchar();inchar();
while(((ch()=='*')&
(nch()=='/'))==0)
{if(ch()==0)inline2();
else inchar();
if(eof)break;
}
inchar();inchar();
}
else if(an(ch()))
{k=0;
while(an(ch()))
{if(k < namemax)sname[k++]=ch();
gch();
}
sname[k]=0;
if(k=findmac(sname))
while(c=macq[k++])
keepch(c);
else
{k=0;
while(c=sname[k++])
keepch(c);
}