/
PASS2.C
4509 lines (3397 loc) · 137 KB
/
PASS2.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 NOTICE */
/****************************************************************************/
/* A PL/I Compiler for the Win32 platform. */
/* Copyright (C) 1997 - 2006 Hugh Gleaves. */
/* */
/* 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 2 */
/* */
/* 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, write to the Free Software */
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 */
/* USA. */
/****************************************************************************/
/****************************************************************************/
/* PL/1 Compiler For DOS. */
/****************************************************************************/
/****************************************************************************/
/* Modification History */
/****************************************************************************/
/* Who When What */
/* ------------------------------------------------------------------------ */
/* HWG1 12-04-91 Initial Prototype. */
/* HWG2 19-04-91 Rewritten to work in a purely recursive manner */
/* HWG3 01-05-91 Structure and pointer qualification added */
/* HWG4 10-05-91 Bug to do with mismatch in arg counts fixed when */
/* checking calls to external procedures. */
/* HWG5 18-07-91 Functions cant be called, procs MUST be called. */
/* HWG6 22-07-91 Returns cant have result in a proc, but must have */
/* a result in a function. */
/* HWG7 22-07-91 Funcs must now have at-least one return stmt. */
/* HWG8 02-08-91 References are now resolved totally in pass2. */
/* HWG9 12-08-91 Ambigous refs not reported/found when the ref was */
/* just a simple unqualified ref, ie: a = b(3); */
/* and 'b' is ambiguous. */
/* HWG10 08-09-91 Calls to procs were being mistaken for functions */
/* in the function 'validate_reference'. */
/* HWG11 08-09-91 Specific errors now exist for too many/few args */
/* HWG12 10-09-91 Ext entries with wrong num args, were reported as */
/* array mis-subscripting ! */
/* HWG13 13-09-91 Offset expression trees are built for every */
/* reference processeed. This will be used by code */
/* generator to create accessing code. */
/* HWG14 16-09-91 allocate_node was NOT declared in here but the it */
/* still compiled and linked OK, but got a run error. */
/* HWG15 16-09-91 Erroneous errors were reported, for variables that */
/* had not actually been resolved. This was due to */
/* the fact that 'validate_reference' was not doing */
/* a test to see if the ref had been resolved and so */
/* was simply using a NULL symbol ptr blindly. */
/* HWG16 18-09-91 There appears to be a printing error in Bornat(141)*/
/* regarding vector offset calculations. The correct */
/* method was hand derived, and also confirmed in the */
/* Barrett and Couch compiler book. */
/* Both 'build_vector_index' and 'multiply_dims' were */
/* modified. */
/* HWG17 20-09-91 build_oe_tree was incorrectly processing call nodes*/
/* when it should not process these references. */
/* HWG18 23-09-91 Undeclared parameters were reported as errors they */
/* are now reported as warnings. */
/* HWG19 28-09-91 Calls to internal procedures were being resolved */
/* incorrectly when an internal proc had same name as */
/* parent proc. find_inner_block now looks for an */
/* internal proc first, before looking at parent. */
/* HWG20 12-10-91 The structure of the parse-tree was altered so that*/
/* the tree root is now held in the block-node of a */
/* PL/1 block. All phases now have to walk the tree */
/* by starting at the block node. */
/* HWG21 22-10-91 When refs to internal procs are resolved we now */
/* setup the line-number and other fields correctly. */
/* */
/* HWG22 23-10-91 The 'main' procedure cannot have any parameters. */
/* */
/* HWG23 29-10-91 Arg/Param compatibility included for internal proc */
/* as well as external procs. */
/* HWG24 02-11-91 When an undeclared name was compiler-declared as a */
/* bin(15,0) its precision was not being set. */
/* HWG25 28-11-91 Check included to test that functions are reffered */
/* to correctly in expressions. */
/* */
/* HWG26 30-11-91 The conversion for decimal to binary has been has */
/* been incorporated, with conversion attributes */
/* calculated and stored for use by the code generator*/
/* HWG27 19-12-91 When a qualified ref was not resolved, because of */
/* a member element not existing, pass2 still tried to*/
/* validate etc the partially built ref tree, this */
/* was causing a system hang. See HWG15 also. */
/* HWG28 20-12-91 Select's and When's were not being processed in */
/* here, they are now. */
/* HWG29 23-12-91 Processing of the 'signal' statement included. */
/* */
/* HWG30 01-01-92 check_logical wasnt doing anything with the sub */
/* expressions of a logical operator. */
/* HWG31 05-01-92 When processing a put node and inserting call */
/* nodes to support routines, chained put nodes were */
/* causing a problem. If a put pointed to a previous */
/* put and that put had been replaced, we got a prob */
/* when we tried to process that second put. */
/* HWG32 15-01-92 When building offset expressions, the compiler was */
/* not setting the attribs ptr in Ref nodes that it */
/* created. This caused array address calculations to */
/* foul-up. */
/* */
/* HWG33 17-01-92 When building offset expressions, the compiler was */
/* faulting unless a subscripted qualified reference */
/* had its subscripts specified in the 'normal' way */
/* that is: a.b.c.d(1,2,3,4) was not being treated */
/* the same as: a(1).b(2).c(3).d(4) ALL such refs are */
/* now converted to the latter from by using the */
/* function: reorder_subscripts. */
/* */
/* HWG34 19-01-92 The function 'simplify_oe_tree' has been coded. */
/* it had a very subtle bug however. It used pointers */
/* that it did not initialise, and the first time it */
/* was called they were (probably) zero/null. */
/* However when presented with certain recursive types*/
/* of references it was possible for an old stack */
/* frame to be allocated again to this function. */
/* Consequently previous VALID ptr values would exist */
/* that misled the function into building calculation */
/* trees when it shouldnt. ALWAYS INITIALISE vars !! */
/* */
/* HWG35 20-01-92 'qualify_reference' was not 'moving' the sublist */
/* down the ref chain as it qualified a ref. */
/* */
/* HWG36 07-03-93 Assigment's now have any BY NAME option verified. */
/* */
/* HWG37 14/08/93 Assignments to cnstants are now reported ! */
/* */
/* HWG38 20-01-96 Conversion ops, were being declared with no line */
/* number, this caused symtab printing code to go */
/* down the tube, cos it expects all names to be */
/* declared with a valid line number ! */
/* function 'dlc_entry' was changed. */
/* */
/* HWG39 27-04-96 reorder_subscripts() was trying to append a */
/* non-existent subscript node to a reference. */
/* This only occurs when a reference has fewer */
/* subscripts than dimensions. */
/* */
/* HWG40 08-05-96 Included support for PL/I RECORD I/O stuff. */
/* */
/* HWG41 02-10-02 Subscripted scalars (ie not arrays) with > 1 */
/* subscript caused a trap. This fix prevents the */
/* array analysis being called when number of subs is */
/* not equal to number of dims. */
/* */
/* HWG42 02-10-02 check_loop should only scrutinize loop counter if */
/* one is present! The code was assuming one. */
/* */
/* HWG43 26-04-06 Added a flag to Ref node that indicates whether */
/* the reference is/was resolved. We should check */
/* this before assuming the ref tree is useable. */
/* The func: 'check_reference' is used a lot and this */
/* should be changed to return true/false. */
/****************************************************************************/
/****************************************************************************/
/* Functional Description */
/****************************************************************************/
/* This source file contains all PL/1 semantic checking, and consists of a */
/* set of mutually recursive functions that walk the parse-trees built by */
/* pass1. */
/* All refs are resolved to their declarations and validated in this phase. */
/* This pass will (for each type of tree node) verify that all sub-trees of */
/* a node are valid, and that all PL/1 references have the correct PL/1 */
/* attributes for the context of their use. Where appropriate, conversion */
/* operators are inserted into the parse-tree. */
/* Result attributes are deduced, and propagated up each expression tree. */
/* After a reference has been resolved, its offset expression is calculated */
/* as an expression tree, and its root held in ref->ofx_ptr. */
/* The offset expression reflects a calculation that must be performed at */
/* runtime in order to access the desired location. The offset expression */
/* will be translated to memory accessing code by the code generator. */
/****************************************************************************/
# include "math.h"
# include "setjmp.h"
# include "stdlib.h"
# include "stdio.h"
# include "string.h"
# include "c_types.h"
# include "tokens.h"
# include "nodes.h"
# include "symtab.h"
# include "convert.h"
# define chur unsigned char
# define _LINE_ ((short)__LINE__)
jmp_buf exit_pass2;
Any_ptr allocate_node (short );
Any_ptr free_node (Any_ptr);
void insert_node (Any_ptr,Any_ptr);
static Any_ptr get_next_node (void);
static short process_nodes (Any_ptr);
short nodetype (Any_ptr);
void report (short ,char *,short );
static void check_select (void);
static void check_when (void);
static void check_other (void);
static void check_on (void);
static void check_entry (void);
static void check_procedure (void);
static void check_if (void);
static void check_stop (void);
static void check_call (void);
static void test_arguments (Ref_ptr);
static void check_put (void);
static void check_open (void);
static void check_return (void);
static void check_loop (void);
static void check_do (void);
static void check_begin (void);
static void check_allocate (void);
static void check_open (void);
static void check_close (void);
static void check_read (void);
static void check_write (void);
static void check_rewrite (void);
static void check_delete (void);
static void check_get (void);
static void check_intarg (Intarg_ptr);
static void check_outsrc (Outsrc_ptr);
static void check_format (Format_ptr);
static void check_free (void);
static void check_goto (void);
static void check_signal (void);
static void check_end (void);
static void check_label (void);
static void check_assignment (void);
static Any_ptr check_concat (Any_ptr);
static Any_ptr check_arithmetic (Any_ptr);
static Any_ptr check_logical (Any_ptr);
static Any_ptr check_expression (Any_ptr);
static void check_operand (Any_ptr);
void set_error_line (Any_ptr);
static chur get_common_scale (Oper_ptr);
static chur get_common_base (Oper_ptr);
static void get_precision (Oper_ptr);
static short next_node_type (Any_ptr);
static void resolve_qualification (Block_ptr,Ref_ptr);
static void check_leave (void);
static short arithmetic (short);
static short qualified (Ref_ptr);
/***************************************************************************/
/* Reference procesing functions. */
/***************************************************************************/
static void validate_reference (Ref_ptr);
static void resolve_reference (Ref_ptr);
static Symbol_ptr find_declaration (Block_ptr, char *);
static Symbol_ptr find_inner_dcl (Block_ptr, char *);
static Symbol_ptr search_structure (Symbol_ptr, char *);
Block_ptr find_inner_block (Block_ptr, char *);
static void resolve_qualification (Block_ptr,Ref_ptr);
static void resolve_struc_ref (char *,Symbol_ptr,Ref_ptr);
static void qualify_reference (Ref_ptr);
static void reorder_subscripts (Ref_ptr);
void append_subscript (Ref_ptr,Sub_ptr);
static Sub_ptr remove_subscript (Ref_ptr);
chur check_reference (Ref_ptr);
static Ref_ptr get_reference_tail (Ref_ptr);
static short subscript_count (Ref_ptr);
static short dimension_count (Ref_ptr);
static short num_inherited_dims (Symbol_ptr);
static short check_sublist (Sub_ptr);
static void build_oe_tree (Ref_ptr);
static Any_ptr build_component_oe (Ref_ptr);
static Any_ptr build_vector_index (Ref_ptr);
static Any_ptr subtract_one (Symbol_ptr,Any_ptr);
static Any_ptr multiply_dims (Symbol_ptr,short );
static Any_ptr build_dim_tree (Symbol_ptr,Dim_ptr);
static Symbol_ptr create_constant (Block_ptr,char *,short );
static Any_ptr simplify_oe_tree (Any_ptr);
short builtin (char *);
/****************************************************************************/
/* Conversion processing functions. */
/****************************************************************************/
static Any_ptr apply_conversion (Oper_ptr);
static Any_ptr select_conversion (Any_ptr,short ,short ,short ,short );
static Any_ptr insert_conversion (Symbol_ptr,Any_ptr,Symbol_ptr);
static Any_ptr cv_fixed_bin (Any_ptr);
static Any_ptr cv_fixed_dec (Any_ptr);
static Any_ptr cv_float_bin (Any_ptr);
static Any_ptr cv_float_dec (Any_ptr);
static Symbol_ptr dcl_entry (char *);
static Symbol_ptr dcl_temp (void);
/* flags used to record the conversions used */
short conversions_in_use = 0;
short cv_fixed_bin_r = 0;
short cv_fixed_dec_r = 0;
short cv_float_dec_r = 0;
short cv_float_bin_r = 0;
short cv_arithmetic_r = 0;
/****************************************************************************/
/* Miscellaneous declarations. */
/****************************************************************************/
short ref_was_resolved = 0;
Any_ptr curr_ptr = NULL;
long unique_val = 0;
extern char line_no[10]; /* used by error report function */
extern short trace_pass2; /* for compiler debugging */
extern short semantic_reqd;
long matches;
extern short optimize_reqd;
/*extern*/ Block_ptr block_root;
/*****************************************/
Block_ptr curr_block = NULL; /* This is also set by dcl phase when it */
/* tries to resolve refs used in the */
/* targets of based/defined variables. */
/* dcl sets it to the declarator of the */
/* based/defined symbol it is processing */
/*****************************************/
/****************************************************************************/
/* This is the main function entry for pass2 processing. */
/****************************************************************************/
void
enter_pass2 (void)
{
Any_ptr ptr;
if (setjmp(exit_pass2) == 0) /* Exit pass2, if we get a null node-ptr */
{ /* this may happen if pass1 exited early */
ptr = get_next_node(); /* due to early EOF. In this case the */
process_nodes (ptr); /* parse-tree may be incomplete. */
}
else
return;
}
/***************************************************************************/
/* This function will take a node-ptr and call a pass2 function to process */
/* that node ! */
/***************************************************************************/
static
short
process_nodes (Any_ptr node_ptr)
{
Begin_ptr bg_ptr;
Procedure_ptr p_ptr;
Block_ptr b_saved;
Block_ptr cb_ptr; /* child block */
Any_ptr here_ptr;
short ntype;
char ntypestr[32];
if (node_ptr == NULL)
return (0);
ntype = nodetype(node_ptr);
switch (ntype) {
case (PROCEDURE):
b_saved = curr_block;
p_ptr = node_ptr;
curr_block = p_ptr->proc;
here_ptr = curr_ptr;
curr_ptr = p_ptr->proc->first_stmt;
check_procedure ();
curr_ptr = here_ptr;
curr_block = b_saved;
break;
case (ENTRY):
check_entry();
break;
case (IF):
check_if();
break;
case (SELECT):
check_select();
break;
case (WHEN):
check_when();
break;
case (OTHER):
check_other();
break;
case (STOP):
check_stop();
break;
case (CALL):
check_call();
break;
case (RETURN):
check_return();
break;
case (LOOP):
check_loop();
break;
case (DO):
check_do();
break;
case (BEGIN):
b_saved = curr_block;
bg_ptr = node_ptr;
curr_block = bg_ptr->block;
here_ptr = curr_ptr;
curr_ptr = bg_ptr->block->first_stmt;
check_begin ();
curr_ptr = here_ptr;
curr_block = b_saved;
break;
case (ALLOCATE):
check_allocate();
break;
case (FREE):
check_free();
break;
case (GOTO):
check_goto();
break;
case (SIGNAL):
check_signal();
break;
case (LEAVE):
check_leave();
break;
case (END):
return(END);
case (LABEL):
check_label();
break;
case (ASSIGNMENT):
check_assignment();
break;
case (PUT):
check_put();
break;
case (ON):
check_on();
break;
case (OPEN):
check_open();
break;
case (CLOSE):
check_close();
break;
case (READ):
check_read();
break;
case (WRITE):
check_write();
break;
case (DELETE):
check_delete();
break;
case (REWRITE):
check_rewrite();
break;
case (GET):
check_get();
break;
default:
{
set_error_line(node_ptr); /* set global error line number, for diagnostics */
itoa(ntype,ntypestr,10);
report (62,ntypestr,_LINE_); /* weird node found ! */
return(0);
}
}
if (nodetype(node_ptr) == PROCEDURE)
{
/******************************************************************/
/* OK We have processed a PL/1 block, but we must now process all */
/* child blocks of this block. */
/******************************************************************/
cb_ptr = p_ptr->proc->child;
while (cb_ptr != NULL)
{
process_nodes(cb_ptr->first_stmt);
cb_ptr = cb_ptr->sister;
}
}
return(0);
}
/****************************************************************/
/* This function is called to check/process an expression tree. */
/* It returns a pointer to a (possibly new) expression tree. */
/****************************************************************/
static
Any_ptr
check_expression (Any_ptr node_ptr)
{
short nt;
char ntypestr[32];
if (node_ptr == NULL)
return(NULL);
nt = nodetype(node_ptr);
switch (nt) {
case(OR):
case(AND):
case(GT):
case(LT):
case(GE):
case(LE):
case(NOTGT):
case(NOTLT):
case(NOTGE):
case(NOTLE):
case(NOTEQUAL):
case(EQUALS):
return(check_logical (node_ptr));
case(CONCAT):
return(check_concat (node_ptr));
case(PLUS):
case(MINUS):
case(TIMES):
case(DIVIDE):
return(check_arithmetic (node_ptr));
case(REFERENCE):
check_reference (node_ptr);
check_operand (node_ptr);
return(node_ptr);
case(SYMBOL):
return(node_ptr); /* check_symbol not implemented yet */
default:
{
itoa(nt,ntypestr,10);
set_error_line(node_ptr);
report(62,ntypestr,_LINE_);
}
}
return(node_ptr); /* return same expression node */
}
/****************************************************************************/
/* Process the operands of a logical PL/I operator. */
/****************************************************************************/
static
Any_ptr
check_logical (Any_ptr g_ptr)
{
Oper_ptr o_ptr;
o_ptr = g_ptr;
o_ptr->left_ptr = check_expression (o_ptr->left_ptr);
o_ptr->rite_ptr = check_expression (o_ptr->rite_ptr);
return(g_ptr);
}
/****************************************************************************/
/* Process the operands of miscellaneous PL/I operators. */
/****************************************************************************/
static
Any_ptr
check_concat (Any_ptr g_ptr)
{
return(g_ptr);
}
/*************************************************************************/
/* Analyse and process a PL/1 arithmetic operator node, and any subnodes */
/*************************************************************************/
static
Any_ptr
check_arithmetic (Any_ptr g_ptr)
{
Ref_ptr r_ptr = NULL;
Oper_ptr ptr = NULL;
Symbol_ptr a_ptr = NULL;
Any_ptr new_expr = NULL;
chur s;
chur b;
ptr = g_ptr;
if (nodetype(ptr->left_ptr) == REFERENCE)
{
r_ptr = ptr->left_ptr;
if (check_reference(r_ptr))
{
a_ptr = r_ptr->attribs;
if (a_ptr->computational == 0)
report (76,a_ptr->spelling,_LINE_);
}
}
else
ptr->left_ptr = check_expression (ptr->left_ptr);
if (nodetype(ptr->rite_ptr) == REFERENCE)
{
r_ptr = ptr->rite_ptr;
if (check_reference(r_ptr))
{
a_ptr = r_ptr->attribs;
if (a_ptr->computational == 0)
report (76,a_ptr->spelling,_LINE_);
}
}
else
ptr->rite_ptr = check_expression (ptr->rite_ptr);
/*********************************************************************/
/* A NOTE ON ARITHMETIC OPERAND CONVERSION */
/*********************************************************************/
/* The ANSI rules say (ie Page 320 Infix-Add) that either or both of */
/* the arguments for an arithmetic operator may be converted if they*/
/* are different. */
/* We cannot determine the conversions required for these arguments */
/* until the base and scale has been determined for the result. */
/* Once we have done this we convert none, one or both arguments to */
/* the base and scale (derived earlier) for the result. */
/* So if we have dec + bin, the result type is bin, so we must */
/* convert the dec to a bin before adding. */
/* So the processing proceeds in three steps: */
/* 1. Determine the results derived base and scale. */
/* 2. Apply conversion operators to those arguments that need it. */
/* 3. Using the the arguments new precisions, determine result prec. */
/*********************************************************************/
/*********************************************************************/
/* Now determine the base and scale attributes of each sub-result in */
/* the expression-tree. */
/*********************************************************************/
b = get_common_base (ptr); /* bin/dec */
s = get_common_scale (ptr); /* float/fixed */
/***********************************************************************/
/* Set this operator nodes base & scale, from the common base/scale. */
/***********************************************************************/
ptr->base = b;
ptr->scale = s;
/***********************************************************************/
/* Now examine the operator nodes to see if their arguments need to be */
/* subjected to a conversion. */
/***********************************************************************/
new_expr = apply_conversion(ptr);
/************************************************************************/
/* Having applied conversion operators, we can now deduce the precision */
/* of this operators arithmetic result using the ANSI rules. */
/************************************************************************/
get_precision (ptr);
return (new_expr);
}
/*************************************************************************/
/* Analyse and process a PROCEDURE node. */
/*************************************************************************/
static
void
check_procedure (void)
{
Procedure_ptr p_ptr = NULL;
Symbol_ptr s_ptr = NULL;
Argument_ptr a_ptr = NULL;
Any_ptr next;
p_ptr = curr_ptr;
next = get_next_node();
/***************************************************************/
/* Report any undeclared parameters, that this block may have. */
/***************************************************************/
a_ptr = p_ptr->argument;
while (a_ptr != NULL)
{
s_ptr = a_ptr->arg_ptr;
if (s_ptr->declared == 0)
{
/***********************************************************/
/* This undeclared name must now be declared as bin(15). */
/***********************************************************/
s_ptr->type = BINARY;
s_ptr->declared = 1;
s_ptr->scale = FIXED;
s_ptr->prec_1 = 15;
s_ptr->prec_2 = 0;
s_ptr->bytes = 2;
s_ptr->class = PARAMETER;
report (-77,s_ptr->spelling,_LINE_);
}
a_ptr = a_ptr->next_arg;
}
if ((curr_block->function) && (curr_block->num_rets == 0))
report(112,curr_block->block_name,_LINE_);
if ((curr_block->function) && (curr_block->main))
report(120,curr_block->block_name,_LINE_);
if ((curr_block->main) && (curr_block->num_args > 0))
report(121,curr_block->block_name,_LINE_);
while (process_nodes (next) == 0)
{
next = get_next_node();
}
return; /* An END was read so were done ! */
}
/*************************************************************************/
/* Analyse and process an IF node */
/*************************************************************************/
static
void
check_if (void)
{
If_ptr ptr;
Any_ptr next;
ptr = curr_ptr;
next = curr_ptr; /* save current location in tree */
set_error_line (ptr);
ptr->expression = check_expression (ptr->expression);
curr_ptr = ptr->then_ptr;
process_nodes (curr_ptr);
if (ptr->else_ptr != ptr->stmt_ptr)
{
curr_ptr = ptr->else_ptr;
process_nodes (curr_ptr);
}
curr_ptr = next;
return;
}
/*************************************************************************/
/* Analyse and process a STOP node. */
/*************************************************************************/
static
void
check_stop (void)
{
return;
}
/*************************************************************************/
/* Analyse and process a CALL node. */
/*************************************************************************/
static
void
check_call (void)
{
Call_ptr ptr;
Ref_ptr r_ptr;
ptr = curr_ptr;
set_error_line (ptr);
/*******************************************************************/
/* The argument of a call statement MUST be an ENTRY ! */
/*******************************************************************/
r_ptr = ptr->entry;
if (check_reference (r_ptr) == 0)
return;
if (r_ptr->symbol->structure)
{
report (73,r_ptr->symbol->spelling,_LINE_);
return;
}
if (r_ptr->symbol->type != ENTRY)
{
report (64,r_ptr->symbol->spelling,_LINE_);
goto no_further_tests; /* Dont bother checking nout else */
}
/****************************************************************/
/* See wether the entry is internal or external, then check arg */
/* counts match. */
/****************************************************************/
if ((r_ptr->symbol->proc_ptr) == NULL) /* external */
{
if (r_ptr->num_subs > r_ptr->symbol->num_dims)
report(98,r_ptr->symbol->spelling,_LINE_);
if (r_ptr->num_subs < r_ptr->symbol->num_dims)
report(86,r_ptr->symbol->spelling,_LINE_);
}
else
{
/************************************************************/
/* If this is an immediate recursive call, then the block */
/* MUST be declared as recursive. */
/************************************************************/
if (r_ptr->symbol->declarator == r_ptr->symbol->proc_ptr)
if (r_ptr->symbol->proc_ptr->recursive == 0)
report(104,r_ptr->spelling,_LINE_);
if ((r_ptr->num_subs) > (r_ptr->symbol->proc_ptr->num_args))
report (98,r_ptr->symbol->spelling,_LINE_);
if ((r_ptr->num_subs) < (r_ptr->symbol->proc_ptr->num_args))
report (86,r_ptr->symbol->spelling,_LINE_);
if (r_ptr->symbol->proc_ptr->function)
report (97,r_ptr->symbol->spelling,_LINE_); /* cant call a function */
/* else
r_ptr->symbol->proc_ptr->called = 1; */ /* Block is called ! */
}
/********************************************************************/
/* If the entry takes parameters, then ensure that data-types match */
/********************************************************************/
if (r_ptr->num_subs > 0)
test_arguments (r_ptr);
no_further_tests:
return;
}
/***************************************************************************/
/* This function test that arguments and parameters match in a CALL stmt. */
/***************************************************************************/
static
void
test_arguments (Ref_ptr r_ptr)
{
Data_ptr parm_ptr;
Sub_ptr sub_ptr;
Ref_ptr arg_ptr;
Argument_ptr argt_ptr;
short arg_type;
Symbol_ptr attribs;
Any_ptr proc_node_ptr;
Procedure_ptr p_ptr;
/********************************************************************/
/* Internal and external procedures have their arg descriptors held */
/* differently, so the analysis is in two forms. */
/********************************************************************/
if (r_ptr->symbol->proc_ptr == NULL)
{
parm_ptr = r_ptr->symbol->array_ptr;
sub_ptr = r_ptr->sublist;
while (parm_ptr != NULL)
{
if (sub_ptr == NULL)
return;
arg_type = nodetype(sub_ptr->expression);
if (arg_type != REFERENCE)
; /* report(107,"",_LINE_); expressions are passed by val anyway */
else
{
arg_ptr = sub_ptr->expression;
attribs = arg_ptr->attribs;
/***********************************************************/
/* If the arg isnt a constant, then verify the bloody ting */
/***********************************************************/
if ((attribs->class != CONSTANT) && (attribs->class != BUILTIN))
{
if ((parm_ptr->data_type == CHARACTER) &&
(parm_ptr->asterisk))
{
if ((attribs->varying != parm_ptr->varying) ||
(attribs->type != CHARACTER))
/**************************************************/
/* We have a mismatch ! */
/**************************************************/
{
sub_ptr->pass_by = VALUE;
report(-107,arg_ptr->symbol->spelling,_LINE_);
goto next_iteration_1;
}
}
else
{
if ((attribs->type != parm_ptr->data_type) ||
(attribs->prec_1 != parm_ptr->prec_1) ||
(attribs->prec_2 != parm_ptr->prec_2) ||
(attribs->scale != parm_ptr->scale) ||
(attribs->varying != parm_ptr->varying))
/**************************************************/
/* We have a mismatch ! */
/**************************************************/
{
sub_ptr->pass_by = VALUE;
report(-107,arg_ptr->symbol->spelling,_LINE_);
goto next_iteration_1;
}
}
}
else
sub_ptr->pass_by = VALUE;
}
next_iteration_1:
sub_ptr = sub_ptr->next_ptr;
parm_ptr = parm_ptr->next_ptr;
}
}