1 /* Main program for the checking library. */
3 /* Copyright (c) 1994 Stanford University
7 This software is provided under the terms described in
8 the "suif_copyright.h" include file. */
10 #include <suif_copyright.h>
12 #define _MODULE_ "libcheck.a"
14 #pragma implementation "check.h"
16 #define RCS_BASE_FILE check_cc
19 #include "check_internal.h"
23 "$Id: check.cc,v 1.1.1.1 1998/06/16 15:15:43 brm Exp $")
25 /*----------------------------------------------------------------------*
27 *----------------------------------------------------------------------*
32 The SUIF ``check'' library provides functions for user
33 programs to check the legality and consistency of various SUIF
34 objects for debugging purposes. An entire SUIF file can be
35 checked by using the ``checksuif'' program, which uses the
36 routines in this library.
39 *----------------------------------------------------------------------*
41 *----------------------------------------------------------------------*/
43 DECLARE_DLIST_CLASS(label_sym_list
, label_sym
*);
44 DECLARE_DLIST_CLASS(label_sym_list_list
, label_sym_list
*);
45 DECLARE_DLIST_CLASS(string_index_list
, string_index
*);
47 boolean checkfail
= FALSE
;
48 boolean checkabort
= FALSE
;
49 boolean pointer_freedom
= FALSE
;
51 base_symtab
*current_scope
= NULL
;
52 tree_node
*current_node
= NULL
;
54 static instruction_list
*pending_instr_ops
= NULL
;
55 static string_index
*defined_labs
= NULL
;
56 static label_sym_list
*used_labs
= NULL
;
57 static string_index_list
*old_defined_labs
= NULL
;
58 static label_sym_list_list
*old_used_labs
= NULL
;
59 static boolean in_annote
= FALSE
;
61 static void check_node_list(tree_node_list
*the_node_list
);
62 static void check_suif_object(suif_object
*the_suif_object
);
63 static void check_annote_list(annote_list
*the_annotes
);
64 static void check_annote(annote
*the_annote
);
65 static void check_immed_list(immed_list
*the_immeds
);
66 static void check_immed(immed
*the_immed
);
67 static void check_sym_addr(sym_addr
*the_addr
);
68 static void check_fse_ref(file_set_entry
*the_fse
);
69 static void check_tree_instr_ref(tree_instr
*the_tree_instr
);
70 static void check_tree_block_ref(tree_block
*the_block
);
71 static void check_tree_proc_ref(tree_proc
*the_tree_proc
);
72 static void check_tree_node_ref(tree_node
*the_node
);
73 static void check_instruction_ref(instruction
*the_instr
);
74 static void check_symtab_ref(base_symtab
*the_symtab
);
75 static void check_var_sym_ref(var_sym
*the_var
);
76 static void check_proc_sym_ref(proc_sym
*the_proc
);
77 static void check_label_sym_ref(label_sym
*the_label
);
78 static void check_sym_node_ref(sym_node
*symbol
);
79 static void check_array_bound(array_bound the_bound
);
80 static void check_func_type_ref(func_type
*the_func_type
);
81 static boolean
check_object_type_ref(type_node
*the_type
);
82 static boolean
check_type_ref(type_node
*the_type
);
83 static boolean
scope_in_scope(base_symtab
*outer
, base_symtab
*inner
);
84 static void push_label_scope(void);
85 static void pop_label_scope(void);
86 static void register_label_def(label_sym
*the_label
);
87 static void register_label_use(label_sym
*the_label
);
89 extern void init_check(int & /* argc */, char * /* argv */ [])
94 extern void exit_check(void)
99 extern void check_global_symtab(global_symtab
*the_global_symtab
)
101 if (the_global_symtab
== NULL
)
103 problem("NULL pointer for global_symtab *");
107 push_clue(the_global_symtab
);
109 base_symtab
*the_base_symtab
= the_global_symtab
;
111 if (the_base_symtab
->kind() != SYMTAB_GLOBAL
)
113 problem("object passed as global_symtab isn't a global_symtab");
114 pop_clue(the_global_symtab
);
118 check_symtab(the_base_symtab
);
120 pop_clue(the_global_symtab
);
123 extern void check_file_symtab(file_symtab
*the_file_symtab
)
125 if (the_file_symtab
== NULL
)
127 problem("NULL pointer used as file_symtab *");
131 push_clue(the_file_symtab
);
133 base_symtab
*the_base_symtab
= the_file_symtab
;
135 if (the_base_symtab
->kind() != SYMTAB_FILE
)
137 problem("object passed as file_symtab isn't a file_symtab");
138 pop_clue(the_file_symtab
);
142 check_symtab(the_base_symtab
);
144 pop_clue(the_file_symtab
);
147 extern void check_symtab(base_symtab
*the_symtab
)
149 if (the_symtab
== NULL
)
151 problem("NULL pointer used for base_symtab *");
155 push_clue(the_symtab
);
157 suif_object
*the_suif_object
= the_symtab
;
159 if (the_suif_object
->object_kind() != SYMTAB_OBJ
)
161 problem("object passed as symtab isn't a symtab");
162 pop_clue(the_symtab
);
166 base_symtab
*old_scope
= current_scope
;
167 current_scope
= the_symtab
;
169 check_suif_object(the_suif_object
);
171 if (the_symtab
->name() == NULL
)
172 problem("NULL name in symtab");
174 base_symtab
*parent_symtab
= the_symtab
->parent();
175 if (parent_symtab
!= NULL
)
177 check_symtab_ref(parent_symtab
);
178 base_symtab_list_iter
child_iter(parent_symtab
->children());
181 if (child_iter
.is_empty())
183 problem("child symtab not in parent's children() list");
186 base_symtab
*test_symtab
= child_iter
.step();
187 if (test_symtab
== the_symtab
)
192 if (the_symtab
->symbols() == NULL
)
194 problem("NULL symbol list in symbol table");
195 current_scope
= old_scope
;
196 pop_clue(the_symtab
);
200 sym_node_list_iter
sym_iter(the_symtab
->symbols());
201 while (!sym_iter
.is_empty())
203 sym_node
*this_symbol
= sym_iter
.step();
204 check_suif_object(this_symbol
);
205 check_sym_node_ref(this_symbol
);
207 push_clue(this_symbol
);
209 if (this_symbol
->parent() != the_symtab
)
210 problem("symbol's parent() != the table containing it");
212 switch (this_symbol
->kind())
216 proc_sym
*this_proc
= (proc_sym
*)this_symbol
;
218 file_set_entry
*this_fse
= this_proc
->file();
219 if (this_fse
!= NULL
)
220 check_fse_ref(this_fse
);
222 check_func_type_ref(this_proc
->type());
224 tree_proc
*the_tree_proc
= this_proc
->block();
225 if (the_tree_proc
!= NULL
)
226 check_tree_proc_ref(the_tree_proc
);
228 if (!the_symtab
->is_global())
229 problem("proc_sym in non-global symbol table");
239 var_sym
*this_var
= (var_sym
*)this_symbol
;
241 check_object_type_ref(this_var
->type());
242 if ((this_var
->type()->size() == 0) &&
243 (this_var
->is_auto() || this_var
->has_var_def()))
245 problem("var_sym with zero size");
248 var_sym
*parent_var
= this_var
->parent_var();
249 if (parent_var
!= NULL
)
251 check_var_sym_ref(parent_var
);
252 if (parent_var
->parent() != the_symtab
)
253 problem("parent_var() in different table");
254 unsigned num_children
= parent_var
->num_children();
256 for (child_num
= 0; child_num
< num_children
; ++child_num
)
258 if (parent_var
->child_var(child_num
) == this_var
)
261 if (child_num
== num_children
)
262 problem("parent_var() doesn't contain child");
265 unsigned num_children
= this_var
->num_children();
266 for (unsigned child_num
= 0; child_num
< num_children
;
269 var_sym
*this_child
= this_var
->child_var(child_num
);
270 check_var_sym_ref(this_child
);
271 if (this_child
->parent() != the_symtab
)
272 problem("child_var() in different table");
273 if (this_child
->parent_var() != this_var
)
274 problem("child_var() with different parent_var()");
277 if ((this_var
->parent_var() == NULL
) &&
278 ((the_symtab
->is_block() && this_var
->is_static()) ||
279 the_symtab
->is_file()))
281 if (!this_var
->has_var_def())
282 problem("`%s' has no definition", this_var
->name());
286 if (((parent_var
!= NULL
) ||
287 (the_symtab
->kind() != SYMTAB_GLOBAL
)) &&
288 this_var
->has_var_def())
290 problem("`%s' shouldn't have a definition, but does",
295 if (this_var
->has_var_def())
297 var_def
*this_def
= this_var
->definition();
298 if (this_def
== NULL
)
300 problem("var_def for `%s' not found",
305 if (this_var
->is_param())
307 if (!the_symtab
->is_proc())
309 problem("parameter symbol in non-procedure symtab");
313 proc_symtab
*the_proc_tab
= (proc_symtab
*)the_symtab
;
314 sym_node_list_iter
param_iter(the_proc_tab
->params());
317 if (param_iter
.is_empty())
319 problem("symbol `%s' marked param is not in"
320 " parameter list", this_var
->name());
323 sym_node
*test_sym
= param_iter
.step();
324 if (test_sym
== this_var
)
333 problem("illegal symbol type");
336 pop_clue(this_symbol
);
339 if (the_symtab
->var_defs() == NULL
)
341 problem("NULL var_def list in symbol table");
342 current_scope
= old_scope
;
343 pop_clue(the_symtab
);
347 var_def_list_iter
def_iter(the_symtab
->var_defs());
348 while (!def_iter
.is_empty())
350 var_def
*this_def
= def_iter
.step();
352 if (this_def
== NULL
)
354 problem("NULL var_def * in symtab var_def list");
360 suif_object
*the_suif_object
= this_def
;
362 if (the_suif_object
->object_kind() != DEF_OBJ
)
364 problem("object in symtab var_def list isn't a var_def");
368 check_suif_object(the_suif_object
);
370 if (this_def
->parent() != the_symtab
)
371 problem("var_def's parent() != the table containing it");
373 var_sym
*this_var
= this_def
->variable();
374 check_var_sym_ref(this_var
);
376 if ((this_var
->parent() != the_symtab
) &&
377 ((this_var
->parent() != fileset
->globals()) ||
378 (the_symtab
->parent() != fileset
->globals())))
380 problem("var_sym for `%s' in different symbol table than its"
381 " var_def", this_var
->name());
384 if (this_def
->alignment() != get_alignment(this_var
->type()))
385 problem("wrong alignment for variable `%s'", this_var
->name());
387 if (!type_is_compatible_with_initializations(this_var
->type(),
390 problem("misalignment of initialization data");
396 if (the_symtab
->types() == NULL
)
398 problem("NULL type list in symbol table");
399 current_scope
= old_scope
;
400 pop_clue(the_symtab
);
404 type_node_list_iter
type_iter(the_symtab
->types());
405 while (!type_iter
.is_empty())
407 type_node
*this_type
= type_iter
.step();
408 check_suif_object(this_type
);
409 check_type_ref(this_type
);
411 push_clue(this_type
);
413 if (this_type
->parent() != the_symtab
)
414 problem("type's parent() != the table containing it");
416 switch (this_type
->op())
420 base_type
*this_base_type
= (base_type
*)this_type
;
421 if (this_base_type
->size() < 0)
422 problem("integer type with negative size");
423 if (this_base_type
->size() == 0)
424 problem("integer type with zero size");
429 base_type
*this_base_type
= (base_type
*)this_type
;
430 if (this_base_type
->size() < 0)
431 problem("floating-point type with negative size");
432 if (this_base_type
->size() == 0)
433 problem("floating-point type with zero size");
434 if (!this_base_type
->is_signed())
435 problem("unsigned floating-point type");
440 base_type
*this_base_type
= (base_type
*)this_type
;
441 if (this_base_type
->size() != 0)
442 problem("void type with non-zero size");
447 ptr_type
*this_ptr
= (ptr_type
*)this_type
;
448 check_type_ref(this_ptr
->ref_type());
453 array_type
*this_array
= (array_type
*)this_type
;
454 type_node
*elem_type
= this_array
->elem_type();
455 check_object_type_ref(elem_type
);
456 check_array_bound(this_array
->lower_bound());
457 check_array_bound(this_array
->upper_bound());
458 if (this_array
->lower_bound().is_unknown())
459 problem("array type with unknown lower bound");
460 if (elem_type
->unqual()->is_array())
462 array_type
*elem_array
=
463 (array_type
*)(elem_type
->unqual());
464 if (elem_array
->upper_bound().is_unknown())
465 problem("sub-array with unknown upper bound");
471 func_type
*this_func
= (func_type
*)this_type
;
472 check_object_type_ref(this_func
->return_type());
473 if (!this_func
->args_known())
475 if (this_func
->has_varargs())
477 problem("function with unknown args but varargs flag"
480 if (this_func
->num_args() != 0)
481 problem("function with unknown args num_args() != 0");
483 unsigned num_args
= this_func
->num_args();
484 for (unsigned arg_num
= 0; arg_num
< num_args
; ++arg_num
)
485 check_object_type_ref(this_func
->arg_type(arg_num
));
492 struct_type
*this_struct
= (struct_type
*)this_type
;
493 if (this_struct
->name() == NULL
)
494 problem("struct type with NULL name");
495 int total_size
= this_struct
->size();
497 problem("struct_type with negative size");
498 int last_field_end
= 0;
499 unsigned num_fields
= this_struct
->num_fields();
500 for (unsigned field_num
= 0; field_num
< num_fields
;
503 int this_offset
= this_struct
->offset(field_num
);
504 type_node
*field_type
= this_struct
->field_type(field_num
);
505 check_object_type_ref(field_type
);
506 int field_size
= field_type
->size();
507 if (this_offset
+ field_size
> total_size
)
508 problem("field goes past end of struct type");
510 problem("field with negative offset in struct type");
512 problem("struct type with field of negative size");
513 else if (field_size
== 0)
514 problem("struct type with field of zero size");
515 if (this_struct
->op() == TYPE_STRUCT
)
517 if (this_offset
< last_field_end
)
518 problem("out-of-order fields in struct type");
519 last_field_end
= this_offset
+ field_size
;
521 else if (this_struct
->op() == TYPE_UNION
)
523 if (this_offset
!= 0)
524 problem("union with non-zero field offset");
531 enum_type
*this_enum
= (enum_type
*)this_type
;
532 if (this_enum
->size() < 0)
533 problem("enumerated type with negative size");
534 if (this_enum
->size() == 0)
535 problem("enumerated type with zero size");
540 case TYPE_CALL_BY_REF
:
543 modifier_type
*this_modifier
= (modifier_type
*)this_type
;
544 check_object_type_ref(this_modifier
->base());
548 problem("unknown type op()");
554 if (the_symtab
->children() == NULL
)
556 problem("NULL children list in symbol table");
557 current_scope
= old_scope
;
558 pop_clue(the_symtab
);
562 base_symtab_list_iter
child_iter(the_symtab
->children());
563 while (!child_iter
.is_empty())
565 base_symtab
*this_child
= child_iter
.step();
566 check_symtab_ref(this_child
);
567 push_clue(this_child
);
568 if (this_child
->parent() != the_symtab
)
569 problem("child symtab with wrong parent");
570 pop_clue(this_child
);
573 switch (the_symtab
->kind())
579 file_symtab
*the_file_symtab
= (file_symtab
*)the_symtab
;
580 check_fse_ref(the_file_symtab
->fse());
585 proc_symtab
*the_proc_symtab
= (proc_symtab
*)the_symtab
;
586 tree_block
*the_tree_block
= the_proc_symtab
->block();
587 if (!the_tree_block
->is_proc())
588 problem("proc_symtab with non-proc block");
589 tree_proc
*the_tree_proc
= (tree_proc
*)the_tree_block
;
590 check_tree_proc_ref(the_tree_proc
);
591 if (the_tree_proc
->proc_syms() != the_proc_symtab
)
592 problem("block() of proc_symtab has different proc_syms()");
593 sym_node_list_iter
param_iter(the_proc_symtab
->params());
594 while (!param_iter
.is_empty())
596 sym_node
*this_param
= param_iter
.step();
597 if (!this_param
->is_var())
599 problem("non-variable in parameter list");
603 var_sym
*this_var
= (var_sym
*)this_param
;
604 check_var_sym_ref(this_var
);
605 if (this_var
->parent() != the_proc_symtab
)
607 problem("variable in parameter list is from a "
608 "different symbol table");
610 if (!this_var
->is_param())
611 problem("non-parameter in parameter list");
618 block_symtab
*the_block_symtab
= (block_symtab
*)the_symtab
;
619 tree_block
*the_tree_block
= the_block_symtab
->block();
620 check_tree_block_ref(the_tree_block
);
621 if (the_tree_block
->symtab() != the_block_symtab
)
622 problem("block() of block_symtab has different symtab()");
626 problem("unknown symtab kind()");
629 current_scope
= old_scope
;
630 pop_clue(the_symtab
);
633 extern void check_proc(tree_proc
*the_proc
)
635 tree_node
*the_tree_node
= (tree_node
*)the_proc
;
636 check_node(the_tree_node
);
638 if (!the_tree_node
->is_proc())
639 problem("object used as a tree_proc isn't a tree_proc");
643 extern void check_node(tree_node
*the_node
)
645 if (the_node
== NULL
)
647 problem("NULL pointer used as tree_node *");
653 suif_object
*the_suif_object
= the_node
;
655 if (the_suif_object
->object_kind() != TREE_OBJ
)
657 problem("object used as a tree_node isn't a tree_node");
662 check_suif_object(the_suif_object
);
664 tree_node
*old_node
= current_node
;
665 current_node
= the_node
;
667 tree_node_list_e
*list_e
= the_node
->list_e();
670 if (list_e
->contents
!= the_node
)
671 problem("list_e() of tree_node does not contain it");
674 tree_node_list
*parent
= the_node
->parent();
678 problem("tree_node with non-NULL parent() but NULL list_e()");
679 tree_node_list_e
*follow_e
= parent
->head();
682 if (follow_e
== NULL
)
684 problem("parent of tree_node does not contain it");
687 if (follow_e
== list_e
)
689 follow_e
= follow_e
->next();
693 base_symtab
*node_scope
= the_node
->scope();
694 if (node_scope
!= NULL
)
696 suif_object
*scope_suif_object
= node_scope
;
697 if (scope_suif_object
->object_kind() != SYMTAB_OBJ
)
699 problem("scope() in tree_node isn't really a base_symtab");
704 base_symtab
*old_scope
= current_scope
;
705 if (node_scope
!= NULL
)
707 if (current_scope
== NULL
)
709 current_scope
= node_scope
;
713 if (current_scope
!= node_scope
)
714 problem("scope() in tree_node is wrong");
718 switch (the_node
->kind())
722 tree_instr
*the_tree_instr
= (tree_instr
*)the_node
;
723 instruction
*the_instr
= the_tree_instr
->instr();
724 check_instruction(the_instr
);
725 if (the_instr
->parent() != the_tree_instr
)
727 problem("instr() of tree_instr does not have the tree_instr"
734 tree_loop
*the_loop
= (tree_loop
*)the_node
;
736 check_label_sym_ref(the_loop
->contlab());
737 check_label_sym_ref(the_loop
->brklab());
738 check_label_sym_ref(the_loop
->toplab());
741 register_label_def(the_loop
->contlab());
742 register_label_def(the_loop
->brklab());
743 check_node_list(the_loop
->body());
747 register_label_def(the_loop
->toplab());
748 check_node_list(the_loop
->test());
755 tree_for
*the_for
= (tree_for
*)the_node
;
757 var_sym
*index
= the_for
->index();
758 boolean index_signed
= FALSE
;
759 boolean index_type_known
= FALSE
;
762 problem("NULL index in tree_for");
766 check_var_sym_ref(index
);
767 if (check_object_type_ref(index
->type()) &&
768 check_object_type_ref(index
->type()->unqual()))
770 type_node
*ind_type_unqual
= index
->type()->unqual();
771 if (ind_type_unqual
->op() == TYPE_INT
)
773 base_type
*the_base
= (base_type
*)ind_type_unqual
;
774 index_type_known
= TRUE
;
775 index_signed
= the_base
->is_signed();
779 problem("type of tree_for index is not an integer");
784 switch (the_for
->test())
794 if (index_type_known
&& !index_signed
)
795 problem("signed test on unsigned index in tree_for");
802 if (index_type_known
&& index_signed
)
803 problem("unsigned test on signed index in tree_for");
806 problem("illegal tree_node kind");
809 if (!the_for
->lb_op().type()->is_same(index
->type()))
810 problem("bad lb_op() type");
811 if (!the_for
->ub_op().type()->is_same(index
->type()))
812 problem("bad ub_op() type");
813 if (!the_for
->step_op().type()->op() == TYPE_INT
)
814 problem("type of tree_for step_op() is not an integer");
816 check_label_sym_ref(the_for
->contlab());
817 check_label_sym_ref(the_for
->brklab());
820 check_node_list(the_for
->lb_list());
824 check_node_list(the_for
->ub_list());
828 check_node_list(the_for
->step_list());
832 register_label_def(the_for
->contlab());
833 register_label_def(the_for
->brklab());
834 check_node_list(the_for
->body());
838 check_node_list(the_for
->landing_pad());
845 tree_if
*the_if
= (tree_if
*)the_node
;
847 check_label_sym_ref(the_if
->jumpto());
850 register_label_def(the_if
->jumpto());
851 check_node_list(the_if
->header());
855 check_node_list(the_if
->then_part());
859 check_node_list(the_if
->else_part());
866 tree_block
*the_block
= (tree_block
*)the_node
;
868 block_symtab
*the_symtab
= the_block
->symtab();
869 check_symtab(the_symtab
);
870 if (the_symtab
->block() != the_block
)
871 problem("block with symtab() with wrong block() pointer");
873 base_symtab
*the_base_symtab
= the_symtab
;
874 if (!the_base_symtab
->is_block())
875 problem("symtab of block is not block_symtab");
877 if (the_block
->is_proc())
880 base_symtab
*save_scope
= current_scope
;
881 current_scope
= the_block
->symtab();
882 check_node_list(the_block
->body());
883 current_scope
= save_scope
;
885 if (the_block
->is_proc())
888 if (the_block
->is_proc())
890 tree_proc
*the_tree_proc
= (tree_proc
*)the_block
;
891 proc_sym
*the_proc_sym
= the_tree_proc
->proc();
892 check_proc_sym_ref(the_proc_sym
);
893 if (!the_symtab
->is_proc())
894 problem("symtab of proc is not proc_symtab");
896 func_type
*the_func_type
= the_proc_sym
->type();
897 if (the_func_type
->args_known())
899 proc_symtab
*the_proc_symtab
= (proc_symtab
*)the_symtab
;
900 sym_node_list_iter
param_iter(the_proc_symtab
->params());
901 unsigned num_args
= the_func_type
->num_args();
902 for (unsigned arg_num
= 0; arg_num
< num_args
; ++arg_num
)
904 sym_node
*this_sym
= param_iter
.step();
905 if (this_sym
== NULL
)
907 problem("number of parameters in formal argument "
908 "list (%lu) is less than the number in "
909 "the function type (%lu)",
910 (unsigned long)arg_num
,
911 (unsigned long)num_args
);
914 if (!this_sym
->is_var())
916 var_sym
*this_param
= (var_sym
*)this_sym
;
917 if (this_param
->type() !=
918 the_func_type
->arg_type(arg_num
))
920 problem("mismatch between the type of formal "
921 "argument %u and the type in the function "
925 if (!param_iter
.is_empty())
927 problem("number of parameters in formal argument list "
928 "(%lu) is greater than the number in the "
929 "function type (%lu)",
931 (the_proc_symtab
->params()->count()),
932 (unsigned long)num_args
);
940 problem("illegal tree_node kind");
943 current_scope
= old_scope
;
944 current_node
= old_node
;
948 extern void check_operand(operand the_operand
)
950 switch (the_operand
.kind())
955 check_var_sym_ref(the_operand
.symbol());
959 instruction
*this_instr
= the_operand
.instr();
960 if (the_operand
.is_expr())
961 check_instruction(this_instr
);
963 check_instruction_ref(this_instr
);
964 if (this_instr
->dst_op().is_symbol())
965 problem("instruction used as operand with symbol destination");
969 problem("unknown kind() for operand");
973 extern void check_instruction(instruction
*the_instr
)
975 if (the_instr
== NULL
)
977 problem("NULL pointer used as instruction *");
981 push_clue(the_instr
);
983 suif_object
*the_suif_object
= the_instr
;
985 if (the_suif_object
->object_kind() != INSTR_OBJ
)
987 problem("object passed as an instruction isn't an instruction");
992 check_suif_object(the_suif_object
);
994 check_object_type_ref(the_instr
->result_type());
996 tree_instr
*parent
= the_instr
->parent();
998 check_tree_instr_ref(parent
);
1000 operand dest_op
= the_instr
->dst_op();
1001 switch (dest_op
.kind())
1006 check_var_sym_ref(dest_op
.symbol());
1010 instruction
*dest_instr
= dest_op
.instr();
1011 check_instruction_ref(dest_instr
);
1012 unsigned num_dest_srcs
= dest_instr
->num_srcs();
1014 for (src_num
= 0; src_num
< num_dest_srcs
; ++src_num
)
1016 if (dest_instr
->src_op(src_num
) == operand(the_instr
))
1019 if (src_num
== num_dest_srcs
)
1020 problem("destination does not use instruction as source");
1021 if ((!operand(the_instr
).is_expr()) && (pending_instr_ops
!= NULL
))
1022 pending_instr_ops
->append(the_instr
);
1026 problem("unknown kind() for destination operand");
1029 switch (the_instr
->format())
1033 problem("instruction with format() ``inf_none''");
1042 in_bj
*the_bj
= (in_bj
*)the_instr
;
1043 check_label_sym_ref(the_bj
->target());
1044 register_label_use(the_bj
->target());
1049 in_ldc
*the_ldc
= (in_ldc
*)the_instr
;
1051 immed value
= the_ldc
->value();
1052 check_immed(&value
);
1054 type_ops result_base_op
= the_instr
->result_type()->unqual()->op();
1055 switch (value
.kind())
1058 case im_extended_int
:
1059 if ((result_base_op
!= TYPE_INT
) &&
1060 (result_base_op
!= TYPE_ENUM
) &&
1061 (result_base_op
!= TYPE_PTR
))
1063 problem("ldc of integer constant with non-integer,"
1064 " non-pointer type");
1068 case im_extended_float
:
1069 if (result_base_op
!= TYPE_FLOAT
)
1071 problem("ldc of floating-point constant with "
1072 "non-floating-point type");
1077 sym_node
*the_symbol
= value
.symbol();
1078 if (the_symbol
->is_var())
1080 var_sym
*the_var
= (var_sym
*)the_symbol
;
1081 if (!the_var
->is_addr_taken())
1083 problem("ldc of address of symbol `%s' with "
1084 "is_addr_taken() flag FALSE",
1088 if (!the_instr
->result_type()->unqual()->is_ptr())
1089 problem("ldc of symbol with non-pointer result type");
1093 problem("illegal immed kind `%s' in ldc instruction",
1098 if (!immed_fits(value
, the_instr
->result_type()))
1099 problem("value of ldc does not fit result type");
1113 in_mbr
*the_mbr
= (in_mbr
*)the_instr
;
1114 check_label_sym_ref(the_mbr
->default_lab());
1115 register_label_use(the_mbr
->default_lab());
1116 unsigned num_labs
= the_mbr
->num_labs();
1117 for (unsigned lab_num
= 0; lab_num
< num_labs
; ++lab_num
)
1119 check_label_sym_ref(the_mbr
->label(lab_num
));
1120 register_label_use(the_mbr
->label(lab_num
));
1126 in_lab
*the_lab_instr
= (in_lab
*)the_instr
;
1127 check_label_sym_ref(the_lab_instr
->label());
1128 register_label_def(the_lab_instr
->label());
1133 in_gen
*the_gen_instr
= (in_gen
*)the_instr
;
1134 const char *name
= the_gen_instr
->name();
1137 problem("in_gen with NULL name");
1139 else if (name
!= lexicon
->enter(name
)->sp
)
1141 problem("in_gen instruction with name not in lexicon");
1147 problem("unknown format() for instruction");
1152 check_type_instr(the_instr
, the_instr
->parent());
1154 unsigned num_srcs
= the_instr
->num_srcs();
1155 for (unsigned src_num
= 0; src_num
< num_srcs
; ++src_num
)
1157 operand this_op
= the_instr
->src_op(src_num
);
1158 check_operand(this_op
);
1159 if (this_op
.is_instr())
1161 instruction
*this_src_instr
= this_op
.instr();
1162 if (this_src_instr
->dst_op() != operand(the_instr
))
1164 problem("instruction source operand does not use instruction"
1167 if ((!this_op
.is_expr()) && (pending_instr_ops
!= NULL
))
1169 instruction_list_e
*follow_instr
= pending_instr_ops
->head();
1172 if (follow_instr
== NULL
)
1174 problem("instruction used as source is not live");
1177 if (follow_instr
->contents
== this_src_instr
)
1179 pending_instr_ops
->remove(follow_instr
);
1182 follow_instr
= follow_instr
->next();
1188 pop_clue(the_instr
);
1191 extern void problem(char *fmt
, ...)
1200 extern void vproblem(char *fmt
, va_list ap
)
1202 char *new_fmt
= new char[strlen(fmt
) + 13];
1203 sprintf(new_fmt
, "%s%s", (in_annote
? "[in annote] " : ""), fmt
);
1205 verror_line(checkabort
? 0 : 1, current_node
, new_fmt
, ap
);
1207 vwarning_line(current_node
, new_fmt
, ap
);
1213 extern void problem_instr(tree_node
*the_node
, instruction
*the_instr
,
1219 vproblem_instr(the_node
, the_instr
, fmt
, ap
);
1223 extern void vproblem_instr(tree_node
*the_node
, instruction
*the_instr
,
1224 char *fmt
, va_list ap
)
1226 tree_node
*old_current
= current_node
;
1227 if (the_node
!= NULL
)
1228 current_node
= the_node
;
1229 char *new_fmt
= new char[strlen(fmt
) + 30];
1230 sprintf(new_fmt
, "instruction (%u) - %s", the_instr
->number(), fmt
);
1231 vproblem(new_fmt
, ap
);
1233 current_node
= old_current
;
1236 static void check_node_list(tree_node_list
*the_node_list
)
1238 if (the_node_list
== NULL
)
1240 problem("NULL pointer used as tree_node_list *");
1244 tree_node
*parent
= the_node_list
->parent();
1247 check_tree_node_ref(parent
);
1248 boolean not_used
= FALSE
;
1249 switch (parent
->kind())
1252 problem("tree_node_list with tree_instr as parent");
1256 tree_loop
*the_loop
= (tree_loop
*)parent
;
1257 not_used
= ((the_loop
->body() != the_node_list
) &&
1258 (the_loop
->test() != the_node_list
));
1263 tree_for
*the_for
= (tree_for
*)parent
;
1264 not_used
= ((the_for
->body() != the_node_list
) &&
1265 (the_for
->landing_pad() != the_node_list
) &&
1266 (the_for
->lb_list() != the_node_list
) &&
1267 (the_for
->ub_list() != the_node_list
) &&
1268 (the_for
->step_list() != the_node_list
));
1273 tree_if
*the_if
= (tree_if
*)parent
;
1274 not_used
= ((the_if
->header() != the_node_list
) &&
1275 (the_if
->then_part() != the_node_list
) &&
1276 (the_if
->else_part() != the_node_list
));
1281 tree_block
*the_block
= (tree_block
*)parent
;
1282 not_used
= (the_block
->body() != the_node_list
);
1286 problem("unknown kind() for parent of tree_node_list");
1290 problem("parent of tree_node_list does not use it");
1293 if ((pending_instr_ops
!= NULL
) && (!pending_instr_ops
->is_empty()))
1294 problem("instruction operand live across list beginning");
1295 instruction_list
*old_pending_instrs
= pending_instr_ops
;
1296 pending_instr_ops
= new instruction_list
;
1298 tree_node_list_iter
node_iter(the_node_list
);
1299 while (!node_iter
.is_empty())
1301 tree_node
*this_node
= node_iter
.step();
1302 check_node(this_node
);
1305 if (!pending_instr_ops
->is_empty())
1306 problem("instruction operand live across list end");
1307 delete pending_instr_ops
;
1308 pending_instr_ops
= old_pending_instrs
;
1311 static void check_suif_object(suif_object
*the_suif_object
)
1313 if (the_suif_object
== NULL
)
1315 problem("NULL pointer used as suif_object *");
1319 push_clue(the_suif_object
);
1321 object_kinds this_kind
= the_suif_object
->object_kind();
1333 problem("object passed as a suif_object isn't a suif_object");
1334 pop_clue(the_suif_object
);
1338 annote_list
*the_annotes
= the_suif_object
->annotes();
1339 check_annote_list(the_annotes
);
1341 pop_clue(the_suif_object
);
1344 static void check_annote_list(annote_list
*the_annotes
)
1346 if (the_annotes
== NULL
)
1348 problem("NULL pointer used as annote_list *");
1352 annote_list_iter
the_iter(the_annotes
);
1353 while (!the_iter
.is_empty())
1355 annote
*this_annote
= the_iter
.step();
1356 check_annote(this_annote
);
1360 static void check_annote(annote
*the_annote
)
1362 boolean old_in_annote
= in_annote
;
1365 if (the_annote
== NULL
)
1367 problem("NULL annote");
1371 push_clue(the_annote
);
1373 const char *name
= the_annote
->name();
1376 problem("annote with NULL name");
1377 pop_clue(the_annote
);
1381 if (name
!= lexicon
->enter(name
)->sp
)
1383 problem("annote with name not in lexicon");
1384 pop_clue(the_annote
);
1388 annote_def
*definition
= lookup_annote(name
);
1389 if ((definition
!= NULL
) && (!definition
->is_structured()))
1391 immed_list
*the_immeds
= (immed_list
*)the_annote
->data();
1392 if (the_immeds
!= NULL
)
1393 check_immed_list(the_immeds
);
1396 in_annote
= old_in_annote
;
1397 pop_clue(the_annote
);
1400 static void check_immed_list(immed_list
*the_immeds
)
1402 if (the_immeds
== NULL
)
1404 problem("NULL pointer used as immed_list *");
1408 immed_list_iter
the_iter(the_immeds
);
1409 while (!the_iter
.is_empty())
1411 immed this_immed
= the_iter
.step();
1412 check_immed(&this_immed
);
1416 static void check_immed(immed
*the_immed
)
1418 switch (the_immed
->kind())
1422 case im_extended_int
:
1423 if (the_immed
->ext_integer() == NULL
)
1425 problem("NULL data for extended integer immed");
1427 else if (the_immed
->ext_integer() !=
1428 lexicon
->enter(the_immed
->ext_integer())->sp
)
1430 problem("extended integer immed data not in lexicon");
1434 const char *follow
= the_immed
->ext_integer();
1438 problem("empty digit string for extended integer immed");
1439 while (*follow
!= 0)
1443 problem("dash in extended integer not in the first"
1447 if ((*follow
< '0') || (*follow
> '9'))
1449 problem("non-digit `%c' in extended integer", *follow
);
1457 if (the_immed
->string() == NULL
)
1459 problem("NULL for string immed");
1461 else if (the_immed
->string() !=
1462 lexicon
->enter(the_immed
->string())->sp
)
1464 problem("string immed with data that is not entered in the"
1470 case im_extended_float
:
1471 if (the_immed
->ext_flt() == NULL
)
1473 problem("NULL data for extended floating-point immed");
1475 else if (the_immed
->ext_flt() !=
1476 lexicon
->enter(the_immed
->ext_flt())->sp
)
1478 problem("extended floating-point immed data not in lexicon");
1482 const char *follow
= the_immed
->ext_flt();
1485 if ((*follow
== 0) || (*follow
== 'e') || (*follow
== 'E'))
1487 problem("empty mantissa for extended floating-point"
1490 boolean decimal_point_seen
= FALSE
;
1491 boolean in_exponent
= FALSE
;
1492 while (*follow
!= 0)
1496 problem("dash in %s of extended floating-point immed"
1497 " not in the first position",
1498 (in_exponent
? "exponent" : "mantissa"));
1501 else if (*follow
== '.')
1505 problem("decimal point in exponent of extended"
1506 " floating-point immed");
1511 if (decimal_point_seen
)
1513 problem("more than one decimal point in "
1514 "mantissa of extended floating-point"
1520 decimal_point_seen
= TRUE
;
1524 else if ((*follow
== 'e') || (*follow
== 'E'))
1528 problem("multiple exponents in extended "
1529 "floating-point immed");
1535 if (follow
[1] == '-')
1539 problem("empty exponent in extended "
1540 "floating-point immed");
1544 else if ((*follow
< '0') || (*follow
> '9'))
1546 problem("illegal character `%c' in extended "
1547 "floating-point immed", *follow
);
1556 sym_addr this_addr
= the_immed
->addr();
1557 check_sym_addr(&this_addr
);
1561 check_type_ref(the_immed
->type());
1564 check_operand(the_immed
->op());
1567 check_instruction(the_immed
->instr());
1572 problem("illegal immed kind `%c'", the_immed
->kind());
1577 static void check_sym_addr(sym_addr
*the_addr
)
1579 sym_node
*the_sym
= the_addr
->symbol();
1580 check_sym_node_ref(the_sym
);
1581 if (the_addr
->offset() != 0)
1583 if (!the_sym
->is_var())
1585 problem("address of non-var sym `%s' used with non-zero offset",
1591 static void check_fse_ref(file_set_entry
*the_fse
)
1593 if (the_fse
== NULL
)
1595 problem("NULL pointer used as file_set_entry *");
1601 suif_object
*the_suif_object
= the_fse
;
1603 if (the_suif_object
->object_kind() != FILE_OBJ
)
1605 problem("object used as file_set_entry isn't a file_set_entry");
1613 static void check_tree_instr_ref(tree_instr
*the_tree_instr
)
1615 tree_node
*the_tree_node
= the_tree_instr
;
1616 check_tree_node_ref(the_tree_node
);
1617 push_clue(the_tree_instr
);
1618 if (!the_tree_node
->is_instr())
1619 problem("object used as tree_instr isn't a tree_instr");
1620 pop_clue(the_tree_instr
);
1623 static void check_tree_block_ref(tree_block
*the_block
)
1625 tree_node
*the_tree_node
= the_block
;
1626 check_tree_node_ref(the_tree_node
);
1627 push_clue(the_block
);
1628 if (!the_tree_node
->is_block())
1629 problem("object used as tree_block isn't a tree_block");
1630 pop_clue(the_block
);
1633 static void check_tree_proc_ref(tree_proc
*the_tree_proc
)
1635 tree_block
*the_block
= the_tree_proc
;
1636 check_tree_block_ref(the_block
);
1637 push_clue(the_tree_proc
);
1638 if (!the_block
->is_proc())
1639 problem("object used as tree_proc isn't a tree_proc");
1640 pop_clue(the_tree_proc
);
1643 static void check_tree_node_ref(tree_node
*the_node
)
1645 if (the_node
== NULL
)
1647 problem("NULL pointer used as tree_node *");
1651 push_clue(the_node
);
1653 suif_object
*the_suif_object
= the_node
;
1655 if (the_suif_object
->object_kind() != TREE_OBJ
)
1657 problem("object used as tree_node isn't a tree_node");
1665 static void check_instruction_ref(instruction
*the_instr
)
1667 if (the_instr
== NULL
)
1669 problem("NULL pointer used as instruction *");
1673 push_clue(the_instr
);
1675 suif_object
*the_suif_object
= the_instr
;
1677 if (the_suif_object
->object_kind() != INSTR_OBJ
)
1678 problem("object used as instruction isn't an instruction");
1680 pop_clue(the_instr
);
1683 static void check_symtab_ref(base_symtab
*the_symtab
)
1685 if (the_symtab
== NULL
)
1687 problem("NULL pointer used as base_symtab *");
1691 push_clue(the_symtab
);
1693 suif_object
*the_suif_object
= the_symtab
;
1695 if (the_suif_object
->object_kind() != SYMTAB_OBJ
)
1697 problem("object used as base_symtab isn't a base_symtab");
1698 pop_clue(the_symtab
);
1702 pop_clue(the_symtab
);
1705 static void check_var_sym_ref(var_sym
*the_var
)
1707 sym_node
*this_sym
= the_var
;
1708 check_sym_node_ref(the_var
);
1710 if (this_sym
->kind() != SYM_VAR
)
1711 problem("object used as var_sym isn't a var_sym");
1715 static void check_proc_sym_ref(proc_sym
*the_proc
)
1717 sym_node
*this_sym
= the_proc
;
1718 check_sym_node_ref(the_proc
);
1719 push_clue(the_proc
);
1720 if (this_sym
->kind() != SYM_PROC
)
1721 problem("object used as proc_sym isn't a proc_sym");
1725 static void check_label_sym_ref(label_sym
*the_label
)
1727 sym_node
*this_sym
= the_label
;
1728 check_sym_node_ref(the_label
);
1729 push_clue(the_label
);
1730 if (this_sym
->kind() != SYM_LABEL
)
1731 problem("object used as label_sym isn't a label_sym");
1732 pop_clue(the_label
);
1735 static void check_sym_node_ref(sym_node
*symbol
)
1739 problem("NULL pointer used as sym_node *");
1745 suif_object
*the_suif_object
= symbol
;
1747 if (the_suif_object
->object_kind() != SYM_OBJ
)
1749 problem("object used as sym_node isn't a sym_node");
1754 if (symbol
->name() == NULL
)
1756 problem("symbol has NULL name");
1761 base_symtab
*parent
= symbol
->parent();
1764 problem("symbol `%s' has no symbol table", symbol
->name());
1769 check_symtab_ref(parent
);
1771 sym_node_list_iter
sym_iter(parent
->symbols());
1774 if (sym_iter
.is_empty())
1776 problem("symbol `%s' is not in the list of symbols for its parent",
1781 sym_node
*test_sym
= sym_iter
.step();
1782 if (test_sym
== symbol
)
1786 if (current_scope
== NULL
)
1792 if (!scope_in_scope(parent
, current_scope
))
1793 problem("symbol `%s' used out of its scope", symbol
->name());
1798 static void check_array_bound(array_bound the_bound
)
1800 if (the_bound
.is_variable())
1801 check_var_sym_ref(the_bound
.variable());
1804 static void check_func_type_ref(func_type
*the_func_type
)
1806 type_node
*the_type_node
= (type_node
*)the_func_type
;
1807 check_type_ref(the_type_node
);
1808 push_clue(the_func_type
);
1809 if (!the_type_node
->is_func())
1810 problem("non-function type used as function type");
1811 pop_clue(the_func_type
);
1814 static boolean
check_object_type_ref(type_node
*the_type
)
1816 boolean ok
= check_type_ref(the_type
);
1819 push_clue(the_type
);
1820 if (the_type
->is_func())
1822 problem("function type used as object type");
1830 static boolean
check_type_ref(type_node
*the_type
)
1832 if (the_type
== NULL
)
1834 problem("NULL pointer used as type_node *");
1838 push_clue(the_type
);
1840 suif_object
*the_suif_object
= the_type
;
1842 if (the_suif_object
->object_kind() != TYPE_OBJ
)
1844 problem("object passed as type_node isn't a type_node");
1849 base_symtab
*parent
= the_type
->parent();
1852 problem("type has no symbol table");
1857 check_symtab_ref(parent
);
1859 type_node_list_iter
type_iter(parent
->types());
1862 if (type_iter
.is_empty())
1864 problem("type `%u' is not in the list of types for its parent",
1865 the_type
->type_id());
1869 type_node
*test_type
= type_iter
.step();
1870 if (test_type
== the_type
)
1874 if (current_scope
== NULL
)
1880 if (!scope_in_scope(parent
, current_scope
))
1881 problem("type used out of its scope");
1887 static boolean
scope_in_scope(base_symtab
*outer
, base_symtab
*inner
)
1889 if ((outer
== NULL
) || (inner
== NULL
))
1892 base_symtab
*follow
= inner
;
1894 while (follow
!= NULL
)
1896 if (follow
== outer
)
1898 follow
= follow
->parent();
1904 static void push_label_scope(void)
1906 if (defined_labs
!= NULL
)
1908 if (old_defined_labs
== NULL
)
1909 old_defined_labs
= new string_index_list
;
1910 old_defined_labs
->push(defined_labs
);
1912 defined_labs
= new tree_string_index
;
1914 if (used_labs
!= NULL
)
1916 if (old_used_labs
== NULL
)
1917 old_used_labs
= new label_sym_list_list
;
1918 old_used_labs
->push(used_labs
);
1920 used_labs
= new label_sym_list
;
1923 static void pop_label_scope(void)
1925 assert(used_labs
!= NULL
);
1926 assert(defined_labs
!= NULL
);
1927 while (!used_labs
->is_empty())
1929 label_sym
*this_lab
= used_labs
->pop();
1930 if (!defined_labs
->exists(this_lab
->name()))
1932 problem("label `%s' used as target outside of its legal range",
1938 delete defined_labs
;
1940 if (old_defined_labs
!= NULL
)
1942 assert(old_used_labs
!= NULL
);
1943 assert(!old_defined_labs
->is_empty());
1944 assert(!old_used_labs
->is_empty());
1945 defined_labs
= old_defined_labs
->pop();
1946 used_labs
= old_used_labs
->pop();
1947 if (old_defined_labs
->is_empty())
1949 assert(old_used_labs
->is_empty());
1950 delete old_defined_labs
;
1951 delete old_used_labs
;
1952 old_defined_labs
= NULL
;
1953 old_used_labs
= NULL
;
1957 assert(!old_used_labs
->is_empty());
1962 assert(old_used_labs
== NULL
);
1963 defined_labs
= NULL
;
1968 static void register_label_def(label_sym
*the_label
)
1970 if (defined_labs
!= NULL
)
1971 defined_labs
->enter(the_label
->name(), the_label
);
1974 static void register_label_use(label_sym
*the_label
)
1976 if (used_labs
!= NULL
)
1977 used_labs
->append(the_label
);