-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.c
2626 lines (2306 loc) · 58.6 KB
/
format.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
/****************************************************************
Copyright 1990-1996, 1999-2001 by AT&T, Lucent Technologies and Bellcore.
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the names of AT&T, Bell Laboratories,
Lucent or Bellcore or any of their entities not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
AT&T, Lucent and Bellcore disclaim all warranties with regard to
this software, including all implied warranties of
merchantability and fitness. In no event shall AT&T, Lucent or
Bellcore be liable for any special, indirect or consequential
damages or any damages whatsoever resulting from loss of use,
data or profits, whether in an action of contract, negligence or
other tortious action, arising out of or in connection with the
use or performance of this software.
****************************************************************/
/* Format.c -- this file takes an intermediate file (generated by pass 1
of the translator) and some state information about the contents of that
file, and generates C program text. */
#include "defs.h"
#include "p1defs.h"
#include "format.h"
#include "output.h"
#include "names.h"
#include "iob.h"
int c_output_line_length = DEF_C_LINE_LENGTH;
int last_was_label; /* Boolean used to generate semicolons
when a label terminates a block */
static char this_proc_name[52]; /* Name of the current procedure. This is
probably too simplistic to handle
multiple entry points */
static tagptr do_format Argdcl((FILEP, FILEP));
static void do_p1_1while Argdcl((FILEP));
static void do_p1_2while Argdcl((FILEP, FILEP));
static tagptr do_p1_addr Argdcl((FILEP, FILEP));
static void do_p1_asgoto Argdcl((FILEP, FILEP));
static tagptr do_p1_charp Argdcl((FILEP));
static void do_p1_comment Argdcl((FILEP, FILEP));
static void do_p1_comp_goto Argdcl((FILEP, FILEP));
static tagptr do_p1_const Argdcl((FILEP));
static void do_p1_elif Argdcl((FILEP, FILEP));
static void do_p1_else Argdcl((FILEP));
static void do_p1_elseifstart Argdcl((FILEP));
static void do_p1_end_for Argdcl((FILEP));
static void do_p1_endelse Argdcl((FILEP));
static void do_p1_endif Argdcl((FILEP));
static tagptr do_p1_expr Argdcl((FILEP, FILEP));
static tagptr do_p1_extern Argdcl((FILEP));
static void do_p1_for Argdcl((FILEP, FILEP));
static void do_p1_fortran Argdcl((FILEP, FILEP));
static void do_p1_goto Argdcl((FILEP, FILEP));
static tagptr do_p1_head Argdcl((FILEP, FILEP));
static tagptr do_p1_ident Argdcl((FILEP));
static void do_p1_if Argdcl((FILEP, FILEP));
static void do_p1_label Argdcl((FILEP, FILEP));
static tagptr do_p1_list Argdcl((FILEP, FILEP));
static tagptr do_p1_literal Argdcl((FILEP));
static tagptr do_p1_name_pointer Argdcl((FILEP));
static void do_p1_set_line Argdcl((FILEP));
static void do_p1_subr_ret Argdcl((FILEP, FILEP));
static int get_p1_token Argdcl((FILEP));
static int p1get_const Argdcl((FILEP, int, Constp*));
static int p1geta Argdcl((FILEP, Addr*));
static int p1getd Argdcl((FILEP, long int*));
static int p1getf Argdcl((FILEP, char**));
static int p1getn Argdcl((FILEP, int, char**));
static int p1gets Argdcl((FILEP, char*, int));
static void proto Argdcl((FILEP, Argtypes*, char*));
extern chainp assigned_fmts;
char filename[P1_FILENAME_MAX];
extern int gflag, sharp_line, trapuv;
extern int typeconv[];
int gflag1;
extern char *parens;
void
start_formatting(Void)
{
FILE *infile;
static int wrote_one = 0;
extern int usedefsforcommon;
extern char *p1_file, *p1_bakfile;
this_proc_name[0] = '\0';
last_was_label = 0;
ei_next = ei_first;
wh_next = wh_first;
(void) fclose (pass1_file);
if ((infile = fopen (p1_file, binread)) == NULL)
Fatal("start_formatting: couldn't open the intermediate file\n");
if (wrote_one)
nice_printf (c_file, "\n");
while (!feof (infile)) {
expptr this_expr;
this_expr = do_format (infile, c_file);
if (this_expr) {
out_and_free_statement (c_file, this_expr);
} /* if this_expr */
} /* while !feof infile */
(void) fclose (infile);
if (last_was_label)
nice_printf (c_file, ";\n");
prev_tab (c_file);
gflag1 = sharp_line = 0;
if (this_proc_name[0])
nice_printf (c_file, "} /* %s */\n", this_proc_name);
/* Write the #undefs for common variable reference */
if (usedefsforcommon) {
Extsym *ext;
int did_one = 0;
for (ext = extsymtab; ext < nextext; ext++)
if (ext -> extstg == STGCOMMON && ext -> used_here) {
ext -> used_here = 0;
if (!did_one)
nice_printf (c_file, "\n");
wr_abbrevs(c_file, 0, ext->extp);
did_one = 1;
ext -> extp = CHNULL;
} /* if */
if (did_one)
nice_printf (c_file, "\n");
} /* if usedefsforcommon */
other_undefs(c_file);
wrote_one = 1;
/* For debugging only */
if (debugflag && (pass1_file = fopen (p1_bakfile, binwrite)))
if (infile = fopen (p1_file, binread)) {
ffilecopy (infile, pass1_file);
fclose (infile);
fclose (pass1_file);
} /* if infile */
/* End of "debugging only" */
scrub(p1_file); /* optionally unlink */
if ((pass1_file = fopen (p1_file, binwrite)) == NULL)
err ("start_formatting: couldn't reopen the pass1 file");
} /* start_formatting */
static void
#ifdef KR_headers
put_semi(outfile)
FILE *outfile;
#else
put_semi(FILE *outfile)
#endif
{
nice_printf (outfile, ";\n");
last_was_label = 0;
}
#define SEM_CHECK(x) if (last_was_label) put_semi(x)
/* do_format -- takes an input stream (a file in pass1 format) and writes
the appropriate C code to outfile when possible. When reading an
expression, the expression tree is returned instead. */
static expptr
#ifdef KR_headers
do_format(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_format(FILE *infile, FILE *outfile)
#endif
{
int token_type, was_c_token;
expptr retval = ENULL;
token_type = get_p1_token (infile);
was_c_token = 1;
switch (token_type) {
case P1_COMMENT:
do_p1_comment (infile, outfile);
was_c_token = 0;
break;
case P1_SET_LINE:
do_p1_set_line (infile);
was_c_token = 0;
break;
case P1_FILENAME:
p1gets(infile, filename, P1_FILENAME_MAX);
was_c_token = 0;
break;
case P1_NAME_POINTER:
retval = do_p1_name_pointer (infile);
break;
case P1_CONST:
retval = do_p1_const (infile);
break;
case P1_EXPR:
retval = do_p1_expr (infile, outfile);
break;
case P1_IDENT:
retval = do_p1_ident(infile);
break;
case P1_CHARP:
retval = do_p1_charp(infile);
break;
case P1_EXTERN:
retval = do_p1_extern (infile);
break;
case P1_HEAD:
gflag1 = sharp_line = 0;
retval = do_p1_head (infile, outfile);
gflag1 = sharp_line = gflag;
break;
case P1_LIST:
retval = do_p1_list (infile, outfile);
break;
case P1_LITERAL:
retval = do_p1_literal (infile);
break;
case P1_LABEL:
do_p1_label (infile, outfile);
/* last_was_label = 1; -- now set in do_p1_label */
was_c_token = 0;
break;
case P1_ASGOTO:
do_p1_asgoto (infile, outfile);
break;
case P1_GOTO:
do_p1_goto (infile, outfile);
break;
case P1_IF:
do_p1_if (infile, outfile);
break;
case P1_ELSE:
SEM_CHECK(outfile);
do_p1_else (outfile);
break;
case P1_ELIF:
SEM_CHECK(outfile);
do_p1_elif (infile, outfile);
break;
case P1_ENDIF:
SEM_CHECK(outfile);
do_p1_endif (outfile);
break;
case P1_ENDELSE:
SEM_CHECK(outfile);
do_p1_endelse (outfile);
break;
case P1_ADDR:
retval = do_p1_addr (infile, outfile);
break;
case P1_SUBR_RET:
do_p1_subr_ret (infile, outfile);
break;
case P1_COMP_GOTO:
do_p1_comp_goto (infile, outfile);
break;
case P1_FOR:
do_p1_for (infile, outfile);
break;
case P1_ENDFOR:
SEM_CHECK(outfile);
do_p1_end_for (outfile);
break;
case P1_WHILE1START:
do_p1_1while(outfile);
break;
case P1_WHILE2START:
do_p1_2while(infile, outfile);
break;
case P1_PROCODE:
procode(outfile);
break;
case P1_ELSEIFSTART:
SEM_CHECK(outfile);
do_p1_elseifstart(outfile);
break;
case P1_FORTRAN:
do_p1_fortran(infile, outfile);
/* no break; */
case P1_EOF:
was_c_token = 0;
break;
case P1_UNKNOWN:
Fatal("do_format: Unknown token type in intermediate file");
break;
default:
Fatal("do_format: Bad token type in intermediate file");
break;
} /* switch */
if (was_c_token)
last_was_label = 0;
return retval;
} /* do_format */
static void
#ifdef KR_headers
do_p1_comment(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_comment(FILE *infile, FILE *outfile)
#endif
{
extern int in_comment;
char storage[COMMENT_BUFFER_SIZE + 1];
int length;
if (!p1gets(infile, storage, COMMENT_BUFFER_SIZE + 1))
return;
length = strlen (storage);
gflag1 = sharp_line = 0;
in_comment = 1;
margin_printf(outfile, length ? "/* %s */\n" : "\n", storage);
in_comment = 0;
gflag1 = sharp_line = gflag;
} /* do_p1_comment */
static void
#ifdef KR_headers
do_p1_set_line(infile)
FILE *infile;
#else
do_p1_set_line(FILE *infile)
#endif
{
int status;
long new_line_number = -1;
status = p1getd (infile, &new_line_number);
if (status == EOF)
err ("do_p1_set_line: Missing line number at end of file\n");
else if (status == 0 || new_line_number == -1)
errl("do_p1_set_line: Illegal line number in intermediate file: %ld\n",
new_line_number);
else {
lineno = new_line_number;
}
} /* do_p1_set_line */
static expptr
#ifdef KR_headers
do_p1_name_pointer(infile)
FILE *infile;
#else
do_p1_name_pointer(FILE *infile)
#endif
{
Namep namep = (Namep) NULL;
int status;
status = p1geta (infile, (Addr *) &namep);
if (status == EOF)
err ("do_p1_name_pointer: Missing pointer at end of file\n");
else if (status == 0 || namep == (Namep) NULL)
erri ("do_p1_name_pointer: Illegal name pointer in p1 file: '#" Addrfmt "'\n",
(Addr) namep);
return (expptr) namep;
} /* do_p1_name_pointer */
static expptr
#ifdef KR_headers
do_p1_const(infile)
FILE *infile;
#else
do_p1_const(FILE *infile)
#endif
{
struct Constblock *c = (struct Constblock *) NULL;
long type = -1;
int status;
status = p1getd (infile, &type);
if (status == EOF)
err ("do_p1_const: Missing constant type at end of file\n");
else if (status == 0)
errl("do_p1_const: Illegal constant type in p1 file: %ld\n", type);
else {
status = p1get_const (infile, (int)type, &c);
if (status == EOF) {
err ("do_p1_const: Missing constant value at end of file\n");
c = (struct Constblock *) NULL;
} else if (status == 0) {
err ("do_p1_const: Illegal constant value in p1 file\n");
c = (struct Constblock *) NULL;
} /* else */
} /* else */
return (expptr) c;
} /* do_p1_const */
void
#ifdef KR_headers
addrlit(addrp)
Addrp addrp;
#else
addrlit(Addrp addrp)
#endif
{
long memno = addrp->memno;
struct Literal *litp, *lastlit;
lastlit = litpool + nliterals;
for (litp = litpool; litp < lastlit; litp++)
if (litp->litnum == memno) {
addrp->vtype = litp->littype;
*((union Constant *) &(addrp->user)) =
*((union Constant *) &(litp->litval));
addrp->vstg = STGMEMNO;
return;
}
err("addrlit failure!");
}
static expptr
#ifdef KR_headers
do_p1_literal(infile)
FILE *infile;
#else
do_p1_literal(FILE *infile)
#endif
{
int status;
long memno;
Addrp addrp;
status = p1getd (infile, &memno);
if (status == EOF)
err ("do_p1_literal: Missing memno at end of file");
else if (status == 0)
err ("do_p1_literal: Missing memno in p1 file");
else {
addrp = ALLOC (Addrblock);
addrp -> tag = TADDR;
addrp -> vtype = TYUNKNOWN;
addrp -> Field = NULL;
addrp -> memno = memno;
addrlit(addrp);
addrp -> uname_tag = UNAM_CONST;
} /* else */
return (expptr) addrp;
} /* do_p1_literal */
static void
#ifdef KR_headers
do_p1_label(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_label(FILE *infile, FILE *outfile)
#endif
{
int status;
ftnint stateno;
struct Labelblock *L;
char *fmt;
status = p1getd (infile, &stateno);
if (status == EOF)
err ("do_p1_label: Missing label at end of file");
else if (status == 0)
err ("do_p1_label: Missing label in p1 file ");
else if (stateno < 0) { /* entry */
margin_printf(outfile, "\n%s:\n", user_label(stateno));
last_was_label = 1;
}
else {
L = labeltab + stateno;
if (L->labused) {
fmt = "%s:\n";
last_was_label = 1;
}
else
fmt = "/* %s: */\n";
margin_printf(outfile, fmt, user_label(L->stateno));
} /* else */
} /* do_p1_label */
static void
#ifdef KR_headers
do_p1_asgoto(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_asgoto(FILE *infile, FILE *outfile)
#endif
{
expptr expr;
expr = do_format (infile, outfile);
out_asgoto (outfile, expr);
} /* do_p1_asgoto */
static void
#ifdef KR_headers
do_p1_goto(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_goto(FILE *infile, FILE *outfile)
#endif
{
int status;
long stateno;
status = p1getd (infile, &stateno);
if (status == EOF)
err ("do_p1_goto: Missing goto label at end of file");
else if (status == 0)
err ("do_p1_goto: Missing goto label in p1 file");
else {
nice_printf (outfile, "goto %s;\n", user_label (stateno));
} /* else */
} /* do_p1_goto */
static void
#ifdef KR_headers
do_p1_if(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_if(FILE *infile, FILE *outfile)
#endif
{
expptr cond;
do {
cond = do_format (infile, outfile);
} while (cond == ENULL);
out_if (outfile, cond);
} /* do_p1_if */
static void
#ifdef KR_headers
do_p1_else(outfile)
FILE *outfile;
#else
do_p1_else(FILE *outfile)
#endif
{
out_else (outfile);
} /* do_p1_else */
static void
#ifdef KR_headers
do_p1_elif(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_elif(FILE *infile, FILE *outfile)
#endif
{
expptr cond;
do {
cond = do_format (infile, outfile);
} while (cond == ENULL);
elif_out (outfile, cond);
} /* do_p1_elif */
static void
#ifdef KR_headers
do_p1_endif(outfile)
FILE *outfile;
#else
do_p1_endif(FILE *outfile)
#endif
{
endif_out (outfile);
} /* do_p1_endif */
static void
#ifdef KR_headers
do_p1_endelse(outfile)
FILE *outfile;
#else
do_p1_endelse(FILE *outfile)
#endif
{
end_else_out (outfile);
} /* do_p1_endelse */
static expptr
#ifdef KR_headers
do_p1_addr(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_addr(FILE *infile, FILE *outfile)
#endif
{
Addrp addrp = (Addrp) NULL;
int status;
status = p1getn (infile, (int)sizeof(struct Addrblock), (char **) &addrp);
if (status == EOF)
err ("do_p1_addr: Missing Addrp at end of file");
else if (status == 0)
err ("do_p1_addr: Missing Addrp in p1 file");
else if (addrp == (Addrp) NULL)
err ("do_p1_addr: Null addrp in p1 file");
else if (addrp -> tag != TADDR)
erri ("do_p1_addr: bad tag in p1 file '%d'", addrp -> tag);
else {
addrp -> vleng = do_format (infile, outfile);
addrp -> memoffset = do_format (infile, outfile);
}
return (expptr) addrp;
} /* do_p1_addr */
static void
#ifdef KR_headers
do_p1_subr_ret(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_subr_ret(FILE *infile, FILE *outfile)
#endif
{
expptr retval;
nice_printf (outfile, "return ");
retval = do_format (infile, outfile);
if (!multitype)
if (retval)
expr_out (outfile, retval);
nice_printf (outfile, ";\n");
} /* do_p1_subr_ret */
static void
#ifdef KR_headers
do_p1_comp_goto(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_comp_goto(FILE *infile, FILE *outfile)
#endif
{
expptr index;
expptr labels;
index = do_format (infile, outfile);
if (index == ENULL) {
err ("do_p1_comp_goto: no expression for computed goto");
return;
} /* if index == ENULL */
labels = do_format (infile, outfile);
if (labels && labels -> tag != TLIST)
erri ("do_p1_comp_goto: expected list, got tag '%d'", labels -> tag);
else
compgoto_out (outfile, index, labels);
} /* do_p1_comp_goto */
static void
#ifdef KR_headers
do_p1_for(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_for(FILE *infile, FILE *outfile)
#endif
{
expptr init, test, inc;
init = do_format (infile, outfile);
test = do_format (infile, outfile);
inc = do_format (infile, outfile);
out_for (outfile, init, test, inc);
} /* do_p1_for */
static void
#ifdef KR_headers
do_p1_end_for(outfile)
FILE *outfile;
#else
do_p1_end_for(FILE *outfile)
#endif
{
out_end_for (outfile);
} /* do_p1_end_for */
static void
#ifdef KR_headers
do_p1_fortran(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_fortran(FILE *infile, FILE *outfile)
#endif
{
char buf[P1_STMTBUFSIZE];
if (!p1gets(infile, buf, P1_STMTBUFSIZE))
return;
/* bypass nice_printf nonsense */
fprintf(outfile, "/*< %s >*/\n", buf+1); /* + 1 to skip by '$' */
}
static expptr
#ifdef KR_headers
do_p1_expr(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_expr(FILE *infile, FILE *outfile)
#endif
{
int status;
long opcode, type;
struct Exprblock *result = (struct Exprblock *) NULL;
status = p1getd (infile, &opcode);
if (status == EOF)
err ("do_p1_expr: Missing expr opcode at end of file");
else if (status == 0)
err ("do_p1_expr: Missing expr opcode in p1 file");
else {
status = p1getd (infile, &type);
if (status == EOF)
err ("do_p1_expr: Missing expr type at end of file");
else if (status == 0)
err ("do_p1_expr: Missing expr type in p1 file");
else if (opcode == 0)
return ENULL;
else {
result = ALLOC (Exprblock);
result -> tag = TEXPR;
result -> vtype = (field)type;
result -> opcode = (unsigned int)opcode;
result -> vleng = do_format (infile, outfile);
if (is_unary_op (opcode))
result -> leftp = do_format (infile, outfile);
else if (is_binary_op (opcode)) {
result -> leftp = do_format (infile, outfile);
result -> rightp = do_format (infile, outfile);
} else
errl("do_p1_expr: Illegal opcode %ld", opcode);
} /* else */
} /* else */
return (expptr) result;
} /* do_p1_expr */
static expptr
#ifdef KR_headers
do_p1_ident(infile)
FILE *infile;
#else
do_p1_ident(FILE *infile)
#endif
{
Addrp addrp;
int status;
long vtype, vstg;
addrp = ALLOC (Addrblock);
addrp -> tag = TADDR;
status = p1getd (infile, &vtype);
if (status == EOF)
err ("do_p1_ident: Missing identifier type at end of file\n");
else if (status == 0 || vtype < 0 || vtype >= NTYPES)
errl("do_p1_ident: Bad type in intermediate file: %ld\n", vtype);
else
addrp -> vtype = (field)vtype;
status = p1getd (infile, &vstg);
if (status == EOF)
err ("do_p1_ident: Missing identifier storage at end of file\n");
else if (status == 0 || vstg < 0 || vstg > STGNULL)
errl("do_p1_ident: Bad storage in intermediate file: %ld\n", vtype);
else
addrp -> vstg = (field)vstg;
status = p1gets(infile, addrp->user.ident, IDENT_LEN);
if (status == EOF)
err ("do_p1_ident: Missing ident string at end of file");
else if (status == 0)
err ("do_p1_ident: Missing ident string in intermediate file");
addrp->uname_tag = UNAM_IDENT;
return (expptr) addrp;
} /* do_p1_ident */
static expptr
#ifdef KR_headers
do_p1_charp(infile)
FILE *infile;
#else
do_p1_charp(FILE *infile)
#endif
{
Addrp addrp;
int status;
long vtype, vstg;
char buf[64];
addrp = ALLOC (Addrblock);
addrp -> tag = TADDR;
status = p1getd (infile, &vtype);
if (status == EOF)
err ("do_p1_ident: Missing identifier type at end of file\n");
else if (status == 0 || vtype < 0 || vtype >= NTYPES)
errl("do_p1_ident: Bad type in intermediate file: %ld\n", vtype);
else
addrp -> vtype = (field)vtype;
status = p1getd (infile, &vstg);
if (status == EOF)
err ("do_p1_ident: Missing identifier storage at end of file\n");
else if (status == 0 || vstg < 0 || vstg > STGNULL)
errl("do_p1_ident: Bad storage in intermediate file: %ld\n", vtype);
else
addrp -> vstg = (field)vstg;
status = p1gets(infile, buf, (int)sizeof(buf));
if (status == EOF)
err ("do_p1_ident: Missing charp ident string at end of file");
else if (status == 0)
err ("do_p1_ident: Missing charp ident string in intermediate file");
addrp->uname_tag = UNAM_CHARP;
addrp->user.Charp = strcpy(mem(strlen(buf)+1,0), buf);
return (expptr) addrp;
}
static expptr
#ifdef KR_headers
do_p1_extern(infile)
FILE *infile;
#else
do_p1_extern(FILE *infile)
#endif
{
Addrp addrp;
addrp = ALLOC (Addrblock);
if (addrp) {
int status;
addrp->tag = TADDR;
addrp->vstg = STGEXT;
addrp->uname_tag = UNAM_EXTERN;
status = p1getd (infile, &(addrp -> memno));
if (status == EOF)
err ("do_p1_extern: Missing memno at end of file");
else if (status == 0)
err ("do_p1_extern: Missing memno in intermediate file");
if (addrp->vtype = extsymtab[addrp->memno].extype)
addrp->vclass = CLPROC;
} /* if addrp */
return (expptr) addrp;
} /* do_p1_extern */
static expptr
#ifdef KR_headers
do_p1_head(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_head(FILE *infile, FILE *outfile)
#endif
{
int status;
int add_n_;
long Class;
char storage[256];
status = p1getd (infile, &Class);
if (status == EOF)
err ("do_p1_head: missing header class at end of file");
else if (status == 0)
err ("do_p1_head: missing header class in p1 file");
else {
status = p1gets (infile, storage, (int)sizeof(storage));
if (status == EOF || status == 0)
storage[0] = '\0';
} /* else */
if (Class == CLPROC || Class == CLMAIN) {
chainp lengths;
add_n_ = nentry > 1;
lengths = length_comp(entries, add_n_);
if (!add_n_ && protofile && Class != CLMAIN)
protowrite(protofile, proctype, storage, entries, lengths);
if (Class == CLMAIN)
nice_printf (outfile, "/* Main program */ int ");
else
nice_printf(outfile, "%s ", multitype ? "VOID"
: c_type_decl(proctype, 1));
nice_printf(outfile, add_n_ ? "%s0_" : "%s", storage);
if (!Ansi) {
listargs(outfile, entries, add_n_, lengths);
nice_printf (outfile, "\n");
}
list_arg_types (outfile, entries, lengths, add_n_, "\n");
nice_printf (outfile, "{\n");
frchain(&lengths);
next_tab (outfile);
strcpy(this_proc_name, storage);
list_decls (outfile);
} else if (Class == CLBLOCK)
next_tab (outfile);
else
errl("do_p1_head: got class %ld", Class);
return NULL;
} /* do_p1_head */
static expptr
#ifdef KR_headers
do_p1_list(infile, outfile)
FILE *infile;
FILE *outfile;
#else
do_p1_list(FILE *infile, FILE *outfile)