1 21 package gov.nasa.jpf.jvm; 22 import java.io.BufferedReader ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileReader ; 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.regex.Matcher ; 30 import java.util.regex.Pattern ; 31 import org.apache.bcel.classfile.JavaClass; 32 import org.apache.bcel.classfile.Method; 33 34 49 public class ConfigAttributor extends DefaultAttributor { 50 51 public ConfigAttributor() { 52 try { 53 parse("jpf-attributes"); 54 } catch (FileNotFoundException e) { 55 } catch (IOException e) { 56 System.err.println("IO exception reading attribute file: " + e); 57 System.err.println("Ignoring file"); 58 relevance_rules.clear(); 59 atomic_rules.clear(); 60 } 61 } 62 63 public boolean isMethodAtomic (JavaClass jc, Method mth, String uniqueName) { 64 String cls = jc.getClassName(); 65 Iterator itr = atomic_rules.iterator(); 66 while (itr.hasNext()) { 67 NameRule rule = (NameRule) itr.next(); 68 if (rule.isMatch(cls, uniqueName)) { 69 System.out.println("Found atomic rule for " + cls + ":" + 70 uniqueName); 71 return rule.getResult() == 1; 72 } 73 } 74 return super.isMethodAtomic(jc, mth, uniqueName); 76 } 77 78 82 public int getSchedulingRelevance (JavaClass jc, Method mth, String uniqueName) { 83 String cls = jc.getClassName(); 84 Iterator itr = relevance_rules.iterator(); 85 while (itr.hasNext()) { 86 NameRule rule = (NameRule) itr.next(); 87 if (rule.isMatch(cls, uniqueName)) { 88 int result = rule.getResult(); 89 System.out.println("Found scheduling rule for " + cls + ":" + 90 uniqueName + " = " + result); 91 return result; 92 } 93 } 94 return super.getSchedulingRelevance(jc, mth, uniqueName); 96 } 97 98 private void parse(String filename 99 ) throws FileNotFoundException , IOException { 100 BufferedReader config_file = new BufferedReader (new FileReader (filename)); 101 System.out.println("Using JPF attributes from " + filename); 102 String line = config_file.readLine(); 103 String current_section = null; 104 while (line != null) { 105 line = line.trim(); 106 if (line.length() != 0 && line.charAt(0) != '#') { 107 if (line.charAt(0) == '[') { 108 current_section = parseSection(line); 109 } else if ("relevance".equals(current_section)) { 110 parseRelevanceRule(line); 111 } else if ("atomic".equals(current_section)) { 112 parseAtomicRule(line); 113 } else { 114 System.err.println("Rules found in section " + current_section + 115 ", which is unknown."); 116 } 117 } 118 line = config_file.readLine(); 119 } 120 } 121 122 private static final Pattern section_pattern = 123 Pattern.compile("\\[([a-zA-Z]+)\\]"); 124 125 private String parseSection(String line) { 126 Matcher match = section_pattern.matcher(line); 127 if (match.matches()) { 128 return match.group(1); 129 } 130 System.err.println("Problem parsing section name " + line); 131 return null; 132 } 133 134 private static final Pattern relevance_rule_pattern = 135 Pattern.compile("([^ ]+) +([^ ]+) +(always|never|neverbody|runnables|sync)"); 136 137 private void parseRelevanceRule(String line) { 138 Matcher match = relevance_rule_pattern.matcher(line); 139 if (match.matches()) { 140 int result = 0; 141 String value = match.group(3); 142 if ("always".equals(value)) { 143 result = MethodInfo.SR_ALWAYS; 144 } else if ("never".equals(value)) { 145 result = MethodInfo.SR_NEVER; 146 } else if ("neverbody".equals(value)) { 147 result = MethodInfo.SR_NEVERBODY; 148 } else if ("runnables".equals(value)) { 149 result = MethodInfo.SR_RUNNABLES; 150 } else if ("sync".equals(value)) { 151 result = MethodInfo.SR_SYNC; 152 } else { 153 System.err.println("Unknown relevance value: " + value); 154 return; 155 } 156 relevance_rules.add(new NameRule(match.group(1), match.group(2), result)); 157 } else { 158 System.err.println("Relevance rule parse error on line: " + line); 159 } 160 } 161 162 private static final Pattern atomic_rule_pattern = 163 Pattern.compile("([^ ]+) +([^ ]+) +(true|false)"); 164 165 private void parseAtomicRule(String line) { 166 Matcher match = atomic_rule_pattern.matcher(line); 167 if (match.matches()) { 168 int result = 0; 169 String value = match.group(3); 170 if ("true".equals(value)) { 171 result = 1; 172 } else if ("false".equals(value)) { 173 result = 0; 174 } else { 175 System.err.println("Unknown atomic value: " + value); 176 return; 177 } 178 atomic_rules.add(new NameRule(match.group(1), match.group(2), result)); 179 } else { 180 System.err.println("Atomic rule parse error on line: " + line); 181 } 182 } 183 184 189 private static class NameRule { 190 NameRule(String class_pattern_string, 191 String method_pattern_string, 192 int value) { 193 class_pattern = Pattern.compile(class_pattern_string); 194 method_pattern = Pattern.compile(method_pattern_string); 195 result = value; 196 } 197 198 boolean isMatch(String class_name, String method_name) { 199 Matcher class_match = class_pattern.matcher(class_name); 200 if (class_match.matches()) { 201 return method_pattern.matcher(method_name).matches(); 202 } 203 return false; 204 } 205 206 int getResult() { 207 return result; 208 } 209 210 Pattern class_pattern; 211 Pattern method_pattern; 212 int result; 213 } 214 215 private List relevance_rules = new ArrayList (); 216 private List atomic_rules = new ArrayList (); 217 } 218 219 | Popular Tags |