Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * \file ir_validate.cpp |
| 26 | * |
| 27 | * Attempts to verify that various invariants of the IR tree are true. |
| 28 | * |
| 29 | * In particular, at the moment it makes sure that no single |
| 30 | * ir_instruction node except for ir_variable appears multiple times |
| 31 | * in the ir tree. ir_variable does appear multiple times: Once as a |
| 32 | * declaration in an exec_list, and multiple times as the endpoint of |
| 33 | * a dereference chain. |
| 34 | */ |
| 35 | |
| 36 | #include <inttypes.h> |
| 37 | #include "ir.h" |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 38 | #include "ir_hierarchical_visitor.h" |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 39 | #include "hash_table.h" |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 40 | |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 41 | |
| 42 | class ir_validate : public ir_hierarchical_visitor { |
| 43 | public: |
| 44 | ir_validate() |
| 45 | { |
Ian Romanick | d1a1ee5 | 2010-07-06 14:49:14 -0700 | [diff] [blame] | 46 | this->ht = hash_table_ctor(0, hash_table_pointer_hash, |
| 47 | hash_table_pointer_compare); |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 48 | |
Ian Romanick | c67016d | 2010-07-02 13:30:23 -0700 | [diff] [blame] | 49 | this->current_function = NULL; |
| 50 | |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 51 | this->callback = ir_validate::validate_ir; |
| 52 | this->data = ht; |
| 53 | } |
| 54 | |
| 55 | ~ir_validate() |
| 56 | { |
| 57 | hash_table_dtor(this->ht); |
| 58 | } |
| 59 | |
| 60 | virtual ir_visitor_status visit(ir_variable *v); |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame^] | 61 | virtual ir_visitor_status visit(ir_dereference_variable *ir); |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 62 | |
Ian Romanick | c67016d | 2010-07-02 13:30:23 -0700 | [diff] [blame] | 63 | virtual ir_visitor_status visit_enter(ir_function *ir); |
| 64 | virtual ir_visitor_status visit_leave(ir_function *ir); |
| 65 | virtual ir_visitor_status visit_enter(ir_function_signature *ir); |
| 66 | |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 67 | static void validate_ir(ir_instruction *ir, void *data); |
| 68 | |
Ian Romanick | c67016d | 2010-07-02 13:30:23 -0700 | [diff] [blame] | 69 | ir_function *current_function; |
| 70 | |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 71 | struct hash_table *ht; |
| 72 | }; |
| 73 | |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame^] | 74 | |
| 75 | ir_visitor_status |
| 76 | ir_validate::visit(ir_dereference_variable *ir) |
| 77 | { |
| 78 | if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) { |
| 79 | printf("ir_dereference_variable @ %p does not specify a variable %p\n", |
| 80 | ir, ir->var); |
| 81 | abort(); |
| 82 | } |
| 83 | |
| 84 | if (hash_table_find(ht, ir->var) == NULL) { |
| 85 | printf("ir_dereference_variable @ %p specifies undeclared variable " |
| 86 | "`%s' @ %p\n", |
| 87 | ir, ir->var->name, ir->var); |
| 88 | abort(); |
| 89 | } |
| 90 | |
| 91 | return visit_continue; |
| 92 | } |
| 93 | |
| 94 | |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 95 | ir_visitor_status |
Ian Romanick | c67016d | 2010-07-02 13:30:23 -0700 | [diff] [blame] | 96 | ir_validate::visit_enter(ir_function *ir) |
| 97 | { |
| 98 | /* Function definitions cannot be nested. |
| 99 | */ |
| 100 | if (this->current_function != NULL) { |
| 101 | printf("Function definition nested inside another function " |
| 102 | "definition:\n"); |
| 103 | printf("%s %p inside %s %p\n", |
| 104 | ir->name, ir, |
| 105 | this->current_function->name, this->current_function); |
| 106 | abort(); |
| 107 | } |
| 108 | |
| 109 | /* Store the current function hierarchy being traversed. This is used |
| 110 | * by the function signature visitor to ensure that the signatures are |
| 111 | * linked with the correct functions. |
| 112 | */ |
| 113 | this->current_function = ir; |
| 114 | |
| 115 | this->validate_ir(ir, this->data); |
| 116 | |
| 117 | return visit_continue; |
| 118 | } |
| 119 | |
| 120 | ir_visitor_status |
| 121 | ir_validate::visit_leave(ir_function *ir) |
| 122 | { |
| 123 | (void) ir; |
| 124 | |
| 125 | this->current_function = NULL; |
| 126 | return visit_continue; |
| 127 | } |
| 128 | |
| 129 | ir_visitor_status |
| 130 | ir_validate::visit_enter(ir_function_signature *ir) |
| 131 | { |
| 132 | if (this->current_function != ir->function()) { |
| 133 | printf("Function signature nested inside wrong function " |
| 134 | "definition:\n"); |
| 135 | printf("%p inside %s %p instead of %s %p\n", |
| 136 | ir, |
| 137 | this->current_function->name, this->current_function, |
| 138 | ir->function_name(), ir->function()); |
| 139 | abort(); |
| 140 | } |
| 141 | |
| 142 | this->validate_ir(ir, this->data); |
| 143 | |
| 144 | return visit_continue; |
| 145 | } |
| 146 | |
| 147 | ir_visitor_status |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 148 | ir_validate::visit(ir_variable *ir) |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 149 | { |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 150 | /* An ir_variable is the one thing that can (and will) appear multiple times |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame^] | 151 | * in an IR tree. It is added to the hashtable so that it can be used |
| 152 | * in the ir_dereference_variable handler to ensure that a variable is |
| 153 | * declared before it is dereferenced. |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 154 | */ |
Ian Romanick | 8baf21b | 2010-07-12 13:55:32 -0700 | [diff] [blame^] | 155 | hash_table_insert(ht, ir, ir); |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 156 | return visit_continue; |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | ir_validate::validate_ir(ir_instruction *ir, void *data) |
| 161 | { |
| 162 | struct hash_table *ht = (struct hash_table *) data; |
| 163 | |
| 164 | if (hash_table_find(ht, ir)) { |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 165 | printf("Instruction node present twice in ir tree:\n"); |
| 166 | ir->print(); |
| 167 | printf("\n"); |
| 168 | abort(); |
| 169 | } |
Ian Romanick | 865cf2d | 2010-06-22 18:41:50 -0700 | [diff] [blame] | 170 | hash_table_insert(ht, ir, ir); |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void |
| 174 | validate_ir_tree(exec_list *instructions) |
| 175 | { |
| 176 | ir_validate v; |
| 177 | |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 178 | v.run(instructions); |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 179 | } |