1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * 26 * iexpr.c -- instanced expression cache module 27 * 28 * this module provides a cache of fully instantized expressions. 29 */ 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 #include <stdio.h> 34 #include <string.h> 35 #include "alloc.h" 36 #include "out.h" 37 #include "lut.h" 38 #include "tree.h" 39 #include "ptree.h" 40 #include "itree.h" 41 #include "ipath.h" 42 #include "iexpr.h" 43 #include "stats.h" 44 #include "eval.h" 45 #include "config.h" 46 47 #define IEXPRSZ 1024 /* hash table size */ 48 49 static struct stats *Niexpr; 50 51 /* the cache is a hash table of these structs */ 52 static struct iexpr { 53 struct node *np; 54 struct iexpr *next; /* next entry in hash bucket */ 55 } *Cache[IEXPRSZ]; 56 57 /* 58 * iexpr_init -- initialize the iexpr module 59 */ 60 void 61 iexpr_init(void) 62 { 63 Niexpr = stats_new_counter("iexpr.niexpr", "iexpr cache entries", 1); 64 } 65 66 /* 67 * iexpr_hash -- produce a simple hash from an instanced expression tree 68 */ 69 static unsigned 70 iexpr_hash(struct node *np) 71 { 72 if (np == NULL) 73 return (1); 74 75 switch (np->t) { 76 case T_GLOBID: 77 return ((int)np->u.globid.s); 78 79 case T_ASSIGN: 80 case T_CONDIF: 81 case T_CONDELSE: 82 case T_NE: 83 case T_EQ: 84 case T_LT: 85 case T_LE: 86 case T_GT: 87 case T_GE: 88 case T_BITAND: 89 case T_BITOR: 90 case T_BITXOR: 91 case T_BITNOT: 92 case T_LSHIFT: 93 case T_RSHIFT: 94 case T_LIST: 95 case T_AND: 96 case T_OR: 97 case T_NOT: 98 case T_ADD: 99 case T_SUB: 100 case T_MUL: 101 case T_DIV: 102 case T_MOD: 103 return ((int)np->t * 104 (iexpr_hash(np->u.expr.left) + 105 iexpr_hash(np->u.expr.right))); 106 107 case T_NAME: 108 return ((int)np->u.name.s); 109 110 case T_EVENT: 111 return (iexpr_hash(np->u.event.ename) + 112 iexpr_hash(np->u.event.epname)); 113 114 case T_FUNC: 115 return ((int)np->u.func.s + 116 iexpr_hash(np->u.func.arglist)); 117 118 case T_QUOTE: 119 return ((int)np->u.quote.s); 120 121 case T_NUM: 122 return ((int)np->u.ull); 123 124 default: 125 outfl(O_DIE, np->file, np->line, 126 "iexpr_hash: unexpected node type: %s", 127 ptree_nodetype2str(np->t)); 128 } 129 /*NOTREACHED*/ 130 return (1); 131 } 132 133 /* 134 * iexpr_cmp -- compare two instanced expression trees 135 */ 136 static int 137 iexpr_cmp(struct node *np1, struct node *np2) 138 { 139 int diff; 140 141 if (np1 == np2) 142 return (0); 143 144 if (np1 == NULL) 145 return (1); 146 147 if (np2 == NULL) 148 return (-1); 149 150 if (np1->t != np2->t) 151 return (np2->t - np1->t); 152 153 /* types match, need to see additional info matches */ 154 switch (np1->t) { 155 case T_GLOBID: 156 return (np2->u.globid.s - np1->u.globid.s); 157 158 case T_ASSIGN: 159 case T_CONDIF: 160 case T_CONDELSE: 161 case T_NE: 162 case T_EQ: 163 case T_LT: 164 case T_LE: 165 case T_GT: 166 case T_GE: 167 case T_BITAND: 168 case T_BITOR: 169 case T_BITXOR: 170 case T_BITNOT: 171 case T_LSHIFT: 172 case T_RSHIFT: 173 case T_LIST: 174 case T_AND: 175 case T_OR: 176 case T_NOT: 177 case T_ADD: 178 case T_SUB: 179 case T_MUL: 180 case T_DIV: 181 case T_MOD: 182 diff = iexpr_cmp(np1->u.expr.left, np2->u.expr.left); 183 if (diff != 0) 184 return (diff); 185 return (iexpr_cmp(np1->u.expr.right, np2->u.expr.right)); 186 187 case T_NAME: 188 if (np2->u.name.s != np1->u.name.s) 189 return (np2->u.name.s - np1->u.name.s); 190 diff = iexpr_cmp(np1->u.name.child, np2->u.name.child); 191 if (diff != 0) 192 return (diff); 193 return (iexpr_cmp(np1->u.name.next, np2->u.name.next)); 194 195 case T_EVENT: 196 diff = iexpr_cmp(np1->u.event.ename, np2->u.event.ename); 197 if (diff != 0) 198 return (diff); 199 return (iexpr_cmp(np1->u.event.epname, np2->u.event.epname)); 200 201 case T_FUNC: 202 if (np1->u.func.s != np2->u.func.s) 203 return (np2->u.func.s - np1->u.func.s); 204 return (iexpr_cmp(np1->u.func.arglist, np2->u.func.arglist)); 205 206 case T_QUOTE: 207 return (np2->u.quote.s - np1->u.quote.s); 208 209 case T_NUM: 210 if (np2->u.ull > np1->u.ull) 211 return (1); 212 else if (np1->u.ull > np2->u.ull) 213 return (-1); 214 else 215 return (0); 216 217 default: 218 outfl(O_DIE, np1->file, np1->line, 219 "iexpr_cmp: unexpected node type: %s", 220 ptree_nodetype2str(np1->t)); 221 } 222 /*NOTREACHED*/ 223 return (0); 224 } 225 226 /* 227 * iexpr -- find instanced expr in cache, or add it if necessary 228 */ 229 struct node * 230 iexpr(struct node *np) 231 { 232 unsigned idx = iexpr_hash(np) % IEXPRSZ; 233 struct iexpr *bucketp = Cache[idx]; 234 struct iexpr *cp; 235 236 /* search cache */ 237 for (cp = bucketp; cp != NULL; cp = cp->next) 238 if (iexpr_cmp(cp->np, np) == 0) { 239 /* found it */ 240 tree_free(np); 241 return (cp->np); 242 } 243 244 /* allocate new cache entry */ 245 cp = MALLOC(sizeof (*cp)); 246 cp->np = np; 247 cp->next = bucketp; 248 Cache[idx] = cp; 249 250 stats_counter_bump(Niexpr); 251 252 return (np); 253 } 254 255 /* 256 * iexpr_cached -- return true if np is in the iexpr cache 257 */ 258 int 259 iexpr_cached(struct node *np) 260 { 261 struct iexpr *cp = Cache[iexpr_hash(np) % IEXPRSZ]; 262 263 /* search cache */ 264 for (; cp != NULL; cp = cp->next) 265 if (iexpr_cmp(cp->np, np) == 0) { 266 /* found it */ 267 return (1); 268 } 269 270 return (0); 271 } 272 273 /* 274 * iexpr_fini -- free the iexpr cache 275 */ 276 void 277 iexpr_fini(void) 278 { 279 int i; 280 281 for (i = 0; i < IEXPRSZ; i++) { 282 struct iexpr *cp; 283 struct iexpr *ncp; 284 285 for (cp = Cache[i]; cp != NULL; cp = ncp) { 286 tree_free(cp->np); 287 ncp = cp->next; 288 FREE(cp); 289 } 290 Cache[i] = NULL; 291 } 292 } 293