-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcproc.c
1620 lines (1592 loc) · 44.1 KB
/
pcproc.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 (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if !defined(lint) && defined(sccs)
static char sccsid[] = "@(#)pcproc.c 8.1 (Berkeley) 6/6/93";
#endif /* not lint */
#include "whoami.h"
#ifdef PC
/*
* and to the end of the file
*/
#include "0.h"
#include "tree.h"
#include "objfmt.h"
#include "opcode.h"
#include "pc.h"
#include <pcc.h>
#include "tmps.h"
#include "tree_ty.h"
/*
* The constant EXPOSIZE specifies the number of digits in the exponent
* of real numbers.
*
* The constant REALSPC defines the amount of forced padding preceeding
* real numbers when they are printed. If REALSPC == 0, then no padding
* is added, REALSPC == 1 adds one extra blank irregardless of the width
* specified by the user.
*
* N.B. - Values greater than one require program mods.
*/
#define EXPOSIZE 2
#define REALSPC 0
/*
* The following array is used to determine which classes may be read
* from textfiles. It is indexed by the return value from classify.
*/
#define rdops(x) rdxxxx[(x)-(TFIRST)]
int rdxxxx[] = {
0, /* -7 file types */
0, /* -6 record types */
0, /* -5 array types */
O_READE, /* -4 scalar types */
0, /* -3 pointer types */
0, /* -2 set types */
0, /* -1 string types */
0, /* 0 nil, no type */
O_READE, /* 1 boolean */
O_READC, /* 2 character */
O_READ4, /* 3 integer */
O_READ8 /* 4 real */
};
/*
* Proc handles procedure calls.
* Non-builtin procedures are "buck-passed" to func (with a flag
* indicating that they are actually procedures.
* builtin procedures are handled here.
*/
void
pcproc(r)
struct tnode *r; /* T_PCALL */
{
register struct nl *p;
register struct tnode *alv, *al;
register op;
struct nl *filetype, *ap;
int argc, typ, fmtspec, strfmt;
struct tnode *argv, *file;
char fmt, format[20], *strptr, *cmd;
int prec, field, strnglen, fmtstart;
char *pu;
struct tnode *pua, *pui, *puz;
int i, j, k;
int itemwidth;
char *readname;
struct nl *tempnlp;
long readtype;
struct tmps soffset;
bool soffset_flag;
#define CONPREC 4
#define VARPREC 8
#define CONWIDTH 1
#define VARWIDTH 2
#define SKIP 16
/*
* Verify that the name is
* defined and is that of a
* procedure.
*/
p = lookup(r->pcall_node.proc_id);
if (p == NLNIL) {
rvlist(r->pcall_node.arg);
return;
}
if (p->class != PROC && p->class != FPROC) {
error("Can't call %s, its %s not a procedure", p->symbol, classes[p->class]);
rvlist(r->pcall_node.arg);
return;
}
argv = r->pcall_node.arg;
/*
* Call handles user defined
* procedures and functions.
*/
if (bn != 0) {
(void) call(p, argv, PROC, bn);
return;
}
/*
* Call to built-in procedure.
* Count the arguments.
*/
argc = 0;
for (al = argv; al != TR_NIL; al = al->list_node.next)
argc++;
/*
* Switch on the operator
* associated with the built-in
* procedure in the namelist
*/
op = p->value[0] &~ NSTAND;
if (opt('s') && (p->value[0] & NSTAND)) {
standard();
error("%s is a nonstandard procedure", p->symbol);
}
switch (op) {
case O_ABORT:
if (argc != 0)
error("null takes no arguments");
return;
case O_FLUSH:
if (argc == 0) {
putleaf( PCC_ICON , 0 , 0 , PCCT_INT , "_PFLUSH" );
putop( PCCOM_UNARY PCC_CALL , PCCT_INT );
putdot( filename , line );
return;
}
if (argc != 1) {
error("flush takes at most one argument");
return;
}
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_FLUSH" );
ap = stklval(argv->list_node.list, NOFLAGS);
if (ap == NLNIL)
return;
if (ap->class != FILET) {
error("flush's argument must be a file, not %s", nameof(ap));
return;
}
putop( PCC_CALL , PCCT_INT );
putdot( filename , line );
return;
case O_MESSAGE:
case O_WRITEF:
case O_WRITLN:
/*
* Set up default file "output"'s type
*/
file = NIL;
filetype = nl+T1CHAR;
/*
* Determine the file implied
* for the write and generate
* code to make it the active file.
*/
if (op == O_MESSAGE) {
/*
* For message, all that matters
* is that the filetype is
* a character file.
* Thus "output" will suit us fine.
*/
putleaf( PCC_ICON , 0 , 0 , PCCT_INT , "_PFLUSH" );
putop( PCCOM_UNARY PCC_CALL , PCCT_INT );
putdot( filename , line );
putRV( (char *) 0 , cbn , CURFILEOFFSET , NLOCAL ,
PCCTM_PTR|PCCT_STRTY );
putLV( "__err" , 0 , 0 , NGLOBAL , PCCTM_PTR|PCCT_STRTY );
putop( PCC_ASSIGN , PCCTM_PTR|PCCT_STRTY );
putdot( filename , line );
} else if (argv != TR_NIL && (al = argv->list_node.list)->tag !=
T_WEXP) {
/*
* If there is a first argument which has
* no write widths, then it is potentially
* a file name.
*/
codeoff();
ap = stkrval(argv->list_node.list, NLNIL, (long) RREQ );
codeon();
if (ap == NLNIL)
argv = argv->list_node.next;
if (ap != NIL && ap->class == FILET) {
/*
* Got "write(f, ...", make
* f the active file, and save
* it and its type for use in
* processing the rest of the
* arguments to write.
*/
putRV((char *) 0 , cbn , CURFILEOFFSET , NLOCAL ,
PCCTM_PTR|PCCT_STRTY );
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_UNIT" );
file = argv->list_node.list;
filetype = ap->type;
(void) stklval(argv->list_node.list, NOFLAGS);
putop( PCC_CALL , PCCT_INT );
putop( PCC_ASSIGN , PCCTM_PTR|PCCT_STRTY );
putdot( filename , line );
/*
* Skip over the first argument
*/
argv = argv->list_node.next;
argc--;
} else {
/*
* Set up for writing on
* standard output.
*/
putRV((char *) 0, cbn , CURFILEOFFSET ,
NLOCAL , PCCTM_PTR|PCCT_STRTY );
putLV( "_output" , 0 , 0 , NGLOBAL ,
PCCTM_PTR|PCCT_STRTY );
putop( PCC_ASSIGN , PCCTM_PTR|PCCT_STRTY );
putdot( filename , line );
output->nl_flags |= NUSED;
}
} else {
putRV((char *) 0, cbn , CURFILEOFFSET , NLOCAL ,
PCCTM_PTR|PCCT_STRTY );
putLV( "_output" , 0 , 0 , NGLOBAL , PCCTM_PTR|PCCT_STRTY );
putop( PCC_ASSIGN , PCCTM_PTR|PCCT_STRTY );
putdot( filename , line );
output->nl_flags |= NUSED;
}
/*
* Loop and process each
* of the arguments.
*/
for (; argv != TR_NIL; argv = argv->list_node.next) {
soffset_flag = FALSE;
/*
* fmtspec indicates the type (CONstant or VARiable)
* and number (none, WIDTH, and/or PRECision)
* of the fields in the printf format for this
* output variable.
* fmt is the format output indicator (D, E, F, O, X, S)
* fmtstart = 0 for leading blank; = 1 for no blank
*/
fmtspec = NIL;
fmt = 'D';
fmtstart = 1;
al = argv->list_node.list;
if (al == NIL)
continue;
if (al->tag == T_WEXP)
alv = al->wexpr_node.expr1;
else
alv = al;
if (alv == TR_NIL)
continue;
codeoff();
ap = stkrval(alv, NLNIL , (long) RREQ );
codeon();
if (ap == NLNIL)
continue;
typ = classify(ap);
if (al->tag == T_WEXP) {
/*
* Handle width expressions.
* The basic game here is that width
* expressions get evaluated. If they
* are constant, the value is placed
* directly in the format string.
* Otherwise the value is pushed onto
* the stack and an indirection is
* put into the format string.
*/
if (al->wexpr_node.expr3 ==
(struct tnode *) OCT)
fmt = 'O';
else if (al->wexpr_node.expr3 ==
(struct tnode *) HEX)
fmt = 'X';
else if (al->wexpr_node.expr3 != TR_NIL) {
/*
* Evaluate second format spec
*/
if ( constval(al->wexpr_node.expr3)
&& isa( con.ctype , "i" ) ) {
fmtspec += CONPREC;
prec = con.crval;
} else {
fmtspec += VARPREC;
}
fmt = 'f';
switch ( typ ) {
case TINT:
if ( opt( 's' ) ) {
standard();
error("Writing %ss with two write widths is non-standard", clnames[typ]);
}
/* and fall through */
case TDOUBLE:
break;
default:
error("Cannot write %ss with two write widths", clnames[typ]);
continue;
}
}
/*
* Evaluate first format spec
*/
if (al->wexpr_node.expr2 != TR_NIL) {
if ( constval(al->wexpr_node.expr2)
&& isa( con.ctype , "i" ) ) {
fmtspec += CONWIDTH;
field = con.crval;
} else {
fmtspec += VARWIDTH;
}
}
if ((fmtspec & CONPREC) && prec < 0 ||
(fmtspec & CONWIDTH) && field < 0) {
error("Negative widths are not allowed");
continue;
}
if ( opt('s') &&
((fmtspec & CONPREC) && prec == 0 ||
(fmtspec & CONWIDTH) && field == 0)) {
standard();
error("Zero widths are non-standard");
}
}
if (filetype != nl+T1CHAR) {
if (fmt == 'O' || fmt == 'X') {
error("Oct/hex allowed only on text files");
continue;
}
if (fmtspec) {
error("Write widths allowed only on text files");
continue;
}
/*
* Generalized write, i.e.
* to a non-textfile.
*/
putleaf( PCC_ICON , 0 , 0
, (int) (PCCM_ADDTYPE(
PCCM_ADDTYPE(
PCCM_ADDTYPE( p2type( filetype )
, PCCTM_PTR )
, PCCTM_FTN )
, PCCTM_PTR ))
, "_FNIL" );
(void) stklval(file, NOFLAGS);
putop( PCC_CALL
, PCCM_ADDTYPE( p2type( filetype ) , PCCTM_PTR ) );
putop( PCCOM_UNARY PCC_MUL , p2type( filetype ) );
/*
* file^ := ...
*/
switch ( classify( filetype ) ) {
case TBOOL:
case TCHAR:
case TINT:
case TSCAL:
precheck( filetype , "_RANG4" , "_RSNG4" );
/* and fall through */
case TDOUBLE:
case TPTR:
ap = rvalue( argv->list_node.list , filetype , RREQ );
break;
default:
ap = rvalue( argv->list_node.list , filetype , LREQ );
break;
}
if (ap == NIL)
continue;
if (incompat(ap, filetype, argv->list_node.list)) {
cerror("Type mismatch in write to non-text file");
continue;
}
switch ( classify( filetype ) ) {
case TBOOL:
case TCHAR:
case TINT:
case TSCAL:
postcheck(filetype, ap);
sconv(p2type(ap), p2type(filetype));
/* and fall through */
case TDOUBLE:
case TPTR:
putop( PCC_ASSIGN , p2type( filetype ) );
putdot( filename , line );
break;
default:
putstrop(PCC_STASG,
PCCM_ADDTYPE(p2type(filetype),
PCCTM_PTR),
(int) lwidth(filetype),
align(filetype));
putdot( filename , line );
break;
}
/*
* put(file)
*/
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_PUT" );
putRV((char *) 0 , cbn , CURFILEOFFSET , NLOCAL ,
PCCTM_PTR|PCCT_STRTY );
putop( PCC_CALL , PCCT_INT );
putdot( filename , line );
continue;
}
/*
* Write to a textfile
*
* Evaluate the expression
* to be written.
*/
if (fmt == 'O' || fmt == 'X') {
if (opt('s')) {
standard();
error("Oct and hex are non-standard");
}
if (typ == TSTR || typ == TDOUBLE) {
error("Can't write %ss with oct/hex", clnames[typ]);
continue;
}
if (typ == TCHAR || typ == TBOOL)
typ = TINT;
}
/*
* If there is no format specified by the programmer,
* implement the default.
*/
switch (typ) {
case TPTR:
warning();
if (opt('s')) {
standard();
}
error("Writing %ss to text files is non-standard",
clnames[typ]);
/* and fall through */
case TINT:
if (fmt == 'f') {
typ = TDOUBLE;
goto tdouble;
}
if (fmtspec == NIL) {
if (fmt == 'D')
field = 10;
else if (fmt == 'X')
field = 8;
else if (fmt == 'O')
field = 11;
else
panic("fmt1");
fmtspec = CONWIDTH;
}
break;
case TCHAR:
tchar:
fmt = 'c';
break;
case TSCAL:
warning();
if (opt('s')) {
standard();
}
error("Writing %ss to text files is non-standard",
clnames[typ]);
case TBOOL:
fmt = 's';
break;
case TDOUBLE:
tdouble:
switch (fmtspec) {
case NIL:
field = 14 + (5 + EXPOSIZE);
prec = field - (5 + EXPOSIZE);
fmt = 'e';
fmtspec = CONWIDTH + CONPREC;
break;
case CONWIDTH:
field -= REALSPC;
if (field < 1)
field = 1;
prec = field - (5 + EXPOSIZE);
if (prec < 1)
prec = 1;
fmtspec += CONPREC;
fmt = 'e';
break;
case VARWIDTH:
fmtspec += VARPREC;
fmt = 'e';
break;
case CONWIDTH + CONPREC:
case CONWIDTH + VARPREC:
field -= REALSPC;
if (field < 1)
field = 1;
}
format[0] = ' ';
fmtstart = 1 - REALSPC;
break;
case TSTR:
(void) constval( alv );
switch ( classify( con.ctype ) ) {
case TCHAR:
typ = TCHAR;
goto tchar;
case TSTR:
strptr = con.cpval;
for (strnglen = 0; *strptr++; strnglen++) /* void */;
strptr = con.cpval;
break;
default:
strnglen = width(ap);
break;
}
fmt = 's';
strfmt = fmtspec;
if (fmtspec == NIL) {
fmtspec = SKIP;
break;
}
if (fmtspec & CONWIDTH) {
if (field <= strnglen)
fmtspec = SKIP;
else
field -= strnglen;
}
break;
default:
error("Can't write %ss to a text file", clnames[typ]);
continue;
}
/*
* Generate the format string
*/
switch (fmtspec) {
default:
panic("fmt2");
case NIL:
if (fmt == 'c') {
if ( opt( 't' ) ) {
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN|PCCT_INT , PCCTM_PTR )
, "_WRITEC" );
putRV((char *) 0 , cbn , CURFILEOFFSET ,
NLOCAL , PCCTM_PTR|PCCT_STRTY );
(void) stkrval( alv , NLNIL , (long) RREQ );
putop( PCC_CM , PCCT_INT );
} else {
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN|PCCT_INT , PCCTM_PTR )
, "_fputc" );
(void) stkrval( alv , NLNIL ,
(long) RREQ );
}
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_ACTFILE" );
putRV((char *) 0, cbn , CURFILEOFFSET ,
NLOCAL , PCCTM_PTR|PCCT_STRTY );
putop( PCC_CALL , PCCT_INT );
putop( PCC_CM , PCCT_INT );
putop( PCC_CALL , PCCT_INT );
putdot( filename , line );
} else {
sprintf(&format[1], "%%%c", fmt);
goto fmtgen;
}
case SKIP:
break;
case CONWIDTH:
sprintf(&format[1], "%%%1d%c", field, fmt);
goto fmtgen;
case VARWIDTH:
sprintf(&format[1], "%%*%c", fmt);
goto fmtgen;
case CONWIDTH + CONPREC:
sprintf(&format[1], "%%%1d.%1d%c", field, prec, fmt);
goto fmtgen;
case CONWIDTH + VARPREC:
sprintf(&format[1], "%%%1d.*%c", field, fmt);
goto fmtgen;
case VARWIDTH + CONPREC:
sprintf(&format[1], "%%*.%1d%c", prec, fmt);
goto fmtgen;
case VARWIDTH + VARPREC:
sprintf(&format[1], "%%*.*%c", fmt);
fmtgen:
if ( opt( 't' ) ) {
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_WRITEF" );
putRV((char *) 0 , cbn , CURFILEOFFSET ,
NLOCAL , PCCTM_PTR|PCCT_STRTY );
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_ACTFILE" );
putRV((char *) 0 , cbn , CURFILEOFFSET ,
NLOCAL , PCCTM_PTR|PCCT_STRTY );
putop( PCC_CALL , PCCT_INT );
putop( PCC_CM , PCCT_INT );
} else {
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_fprintf" );
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_ACTFILE" );
putRV((char *) 0 , cbn , CURFILEOFFSET ,
NLOCAL , PCCTM_PTR|PCCT_STRTY );
putop( PCC_CALL , PCCT_INT );
}
putCONG( &format[ fmtstart ]
, strlen( &format[ fmtstart ] )
, LREQ );
putop( PCC_CM , PCCT_INT );
if ( fmtspec & VARWIDTH ) {
/*
* either
* ,(temp=width,MAX(temp,...)),
* or
* , MAX( width , ... ) ,
*/
if ( ( typ == TDOUBLE &&
al->wexpr_node.expr3 == TR_NIL )
|| typ == TSTR ) {
soffset_flag = TRUE;
soffset = sizes[cbn].curtmps;
tempnlp = tmpalloc((long) (sizeof(long)),
nl+T4INT, REGOK);
putRV((char *) 0 , cbn ,
tempnlp -> value[ NL_OFFS ] ,
tempnlp -> extra_flags , PCCT_INT );
ap = stkrval( al->wexpr_node.expr2 ,
NLNIL , (long) RREQ );
putop( PCC_ASSIGN , PCCT_INT );
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_MAX" );
putRV((char *) 0 , cbn ,
tempnlp -> value[ NL_OFFS ] ,
tempnlp -> extra_flags , PCCT_INT );
} else {
if (opt('t')
|| typ == TSTR || typ == TDOUBLE) {
putleaf( PCC_ICON , 0 , 0
,PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT, PCCTM_PTR )
,"_MAX" );
}
ap = stkrval( al->wexpr_node.expr2,
NLNIL , (long) RREQ );
}
if (ap == NLNIL)
continue;
if (isnta(ap,"i")) {
error("First write width must be integer, not %s", nameof(ap));
continue;
}
switch ( typ ) {
case TDOUBLE:
putleaf( PCC_ICON , REALSPC , 0 , PCCT_INT , (char *) 0 );
putop( PCC_CM , PCCT_INT );
putleaf( PCC_ICON , 1 , 0 , PCCT_INT , (char *) 0 );
putop( PCC_CM , PCCT_INT );
putop( PCC_CALL , PCCT_INT );
if ( al->wexpr_node.expr3 == TR_NIL ) {
/*
* finish up the comma op
*/
putop( PCC_COMOP , PCCT_INT );
fmtspec &= ~VARPREC;
putop( PCC_CM , PCCT_INT );
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_MAX" );
putRV((char *) 0 , cbn ,
tempnlp -> value[ NL_OFFS ] ,
tempnlp -> extra_flags ,
PCCT_INT );
putleaf( PCC_ICON ,
5 + EXPOSIZE + REALSPC ,
0 , PCCT_INT , (char *) 0 );
putop( PCC_CM , PCCT_INT );
putleaf( PCC_ICON , 1 , 0 , PCCT_INT , (char *) 0 );
putop( PCC_CM , PCCT_INT );
putop( PCC_CALL , PCCT_INT );
}
putop( PCC_CM , PCCT_INT );
break;
case TSTR:
putleaf( PCC_ICON , strnglen , 0 , PCCT_INT , (char *) 0 );
putop( PCC_CM , PCCT_INT );
putleaf( PCC_ICON , 0 , 0 , PCCT_INT , (char *) 0 );
putop( PCC_CM , PCCT_INT );
putop( PCC_CALL , PCCT_INT );
putop( PCC_COMOP , PCCT_INT );
putop( PCC_CM , PCCT_INT );
break;
default:
if (opt('t')) {
putleaf( PCC_ICON , 0 , 0 , PCCT_INT , (char *) 0 );
putop( PCC_CM , PCCT_INT );
putleaf( PCC_ICON , 0 , 0 , PCCT_INT , (char *) 0 );
putop( PCC_CM , PCCT_INT );
putop( PCC_CALL , PCCT_INT );
}
putop( PCC_CM , PCCT_INT );
break;
}
}
/*
* If there is a variable precision,
* evaluate it
*/
if (fmtspec & VARPREC) {
if (opt('t')) {
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_MAX" );
}
ap = stkrval( al->wexpr_node.expr3 ,
NLNIL , (long) RREQ );
if (ap == NIL)
continue;
if (isnta(ap,"i")) {
error("Second write width must be integer, not %s", nameof(ap));
continue;
}
if (opt('t')) {
putleaf( PCC_ICON , 0 , 0 , PCCT_INT , (char *) 0 );
putop( PCC_CM , PCCT_INT );
putleaf( PCC_ICON , 0 , 0 , PCCT_INT , (char *) 0 );
putop( PCC_CM , PCCT_INT );
putop( PCC_CALL , PCCT_INT );
}
putop( PCC_CM , PCCT_INT );
}
/*
* evaluate the thing we want printed.
*/
switch ( typ ) {
case TPTR:
case TCHAR:
case TINT:
(void) stkrval( alv , NLNIL , (long) RREQ );
putop( PCC_CM , PCCT_INT );
break;
case TDOUBLE:
ap = stkrval( alv , NLNIL , (long) RREQ );
if (isnta(ap, "d")) {
sconv(p2type(ap), PCCT_DOUBLE);
}
putop( PCC_CM , PCCT_INT );
break;
case TSCAL:
case TBOOL:
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_NAM" );
ap = stkrval( alv , NLNIL , (long) RREQ );
sprintf( format , PREFIXFORMAT , LABELPREFIX
, listnames( ap ) );
putleaf( PCC_ICON , 0 , 0 ,
(int) (PCCTM_PTR | PCCT_CHAR), format );
putop( PCC_CM , PCCT_INT );
putop( PCC_CALL , PCCT_INT );
putop( PCC_CM , PCCT_INT );
break;
case TSTR:
putCONG( "" , 0 , LREQ );
putop( PCC_CM , PCCT_INT );
break;
default:
panic("fmt3");
break;
}
putop( PCC_CALL , PCCT_INT );
putdot( filename , line );
}
/*
* Write the string after its blank padding
*/
if (typ == TSTR ) {
if ( opt( 't' ) ) {
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_WRITES" );
putRV((char *) 0 , cbn , CURFILEOFFSET ,
NLOCAL , PCCTM_PTR|PCCT_STRTY );
ap = stkrval(alv, NLNIL , (long) RREQ );
putop( PCC_CM , PCCT_INT );
} else {
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_fwrite" );
ap = stkrval(alv, NLNIL , (long) RREQ );
}
if (strfmt & VARWIDTH) {
/*
* min, inline expanded as
* temp < len ? temp : len
*/
putRV((char *) 0 , cbn ,
tempnlp -> value[ NL_OFFS ] ,
tempnlp -> extra_flags , PCCT_INT );
putleaf( PCC_ICON , strnglen , 0 , PCCT_INT , (char *) 0 );
putop( PCC_LT , PCCT_INT );
putRV((char *) 0 , cbn ,
tempnlp -> value[ NL_OFFS ] ,
tempnlp -> extra_flags , PCCT_INT );
putleaf( PCC_ICON , strnglen , 0 , PCCT_INT , (char *) 0 );
putop( PCC_COLON , PCCT_INT );
putop( PCC_QUEST , PCCT_INT );
} else {
if ( ( fmtspec & SKIP )
&& ( strfmt & CONWIDTH ) ) {
strnglen = field;
}
putleaf( PCC_ICON , strnglen , 0 , PCCT_INT , (char *) 0 );
}
putop( PCC_CM , PCCT_INT );
putleaf( PCC_ICON , 1 , 0 , PCCT_INT , (char *) 0 );
putop( PCC_CM , PCCT_INT );
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_ACTFILE" );
putRV((char *) 0, cbn , CURFILEOFFSET , NLOCAL ,
PCCTM_PTR|PCCT_STRTY );
putop( PCC_CALL , PCCT_INT );
putop( PCC_CM , PCCT_INT );
putop( PCC_CALL , PCCT_INT );
putdot( filename , line );
}
if (soffset_flag) {
tmpfree(&soffset);
soffset_flag = FALSE;
}
}
/*
* Done with arguments.
* Handle writeln and
* insufficent number of args.
*/
switch (p->value[0] &~ NSTAND) {
case O_WRITEF:
if (argc == 0)
error("Write requires an argument");
break;
case O_MESSAGE:
if (argc == 0)
error("Message requires an argument");
case O_WRITLN:
if (filetype != nl+T1CHAR)
error("Can't 'writeln' a non text file");
if ( opt( 't' ) ) {
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_WRITLN" );
putRV((char *) 0 , cbn , CURFILEOFFSET ,
NLOCAL , PCCTM_PTR|PCCT_STRTY );
} else {
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_fputc" );
putleaf( PCC_ICON , '\n' , 0 , (int) PCCT_CHAR , (char *) 0 );
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_ACTFILE" );
putRV((char *) 0 , cbn , CURFILEOFFSET ,
NLOCAL , PCCTM_PTR|PCCT_STRTY );
putop( PCC_CALL , PCCT_INT );
putop( PCC_CM , PCCT_INT );
}
putop( PCC_CALL , PCCT_INT );
putdot( filename , line );
break;
}
return;
case O_READ4:
case O_READLN:
/*
* Set up default
* file "input".
*/
file = NIL;
filetype = nl+T1CHAR;
/*
* Determine the file implied
* for the read and generate
* code to make it the active file.
*/
if (argv != TR_NIL) {
codeoff();
ap = stkrval(argv->list_node.list, NLNIL, (long) RREQ );
codeon();
if (ap == NLNIL)
argv = argv->list_node.next;
if (ap != NLNIL && ap->class == FILET) {
/*
* Got "read(f, ...", make
* f the active file, and save
* it and its type for use in
* processing the rest of the
* arguments to read.
*/
file = argv->list_node.list;
filetype = ap->type;
putRV((char *) 0, cbn , CURFILEOFFSET , NLOCAL ,
PCCTM_PTR|PCCT_STRTY );
putleaf( PCC_ICON , 0 , 0
, PCCM_ADDTYPE( PCCTM_FTN | PCCT_INT , PCCTM_PTR )
, "_UNIT" );
(void) stklval(argv->list_node.list, NOFLAGS);
putop( PCC_CALL , PCCT_INT );
putop( PCC_ASSIGN , PCCTM_PTR|PCCT_STRTY );
putdot( filename , line );
argv = argv->list_node.next;
argc--;
} else {
/*
* Default is read from
* standard input.
*/
putRV((char *) 0, cbn , CURFILEOFFSET , NLOCAL ,
PCCTM_PTR|PCCT_STRTY );
putLV( "_input" , 0 , 0 , NGLOBAL ,
PCCTM_PTR|PCCT_STRTY );
putop( PCC_ASSIGN , PCCTM_PTR|PCCT_STRTY );
putdot( filename , line );
input->nl_flags |= NUSED;
}
} else {
putRV((char *) 0, cbn , CURFILEOFFSET , NLOCAL ,
PCCTM_PTR|PCCT_STRTY );
putLV( "_input" , 0 , 0 , NGLOBAL , PCCTM_PTR|PCCT_STRTY );
putop( PCC_ASSIGN , PCCTM_PTR|PCCT_STRTY );
putdot( filename , line );
input->nl_flags |= NUSED;
}
/*
* Loop and process each
* of the arguments.