comparison graal/com.oracle.max.asmdis/src/com/sun/max/asm/gen/AssemblerGenerator.java @ 4142:bc8527f3071c

Adjust code base to new level of warnings.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sun, 18 Dec 2011 05:24:06 +0100
parents e233f5660da4
children
comparison
equal deleted inserted replaced
4141:04d21be7a24f 4142:bc8527f3071c
116 return name + "(" + paramTypes.substring(1, paramTypes.length() - 1) + ")"; 116 return name + "(" + paramTypes.substring(1, paramTypes.length() - 1) + ")";
117 } 117 }
118 118
119 } 119 }
120 120
121 private List<Template_Type> filterTemplates(List<Template_Type> templates) { 121 private List<Template_Type> filterTemplates(List<Template_Type> templateList) {
122 if (!generateRedundantInstructionsOption.getValue()) { 122 if (!generateRedundantInstructionsOption.getValue()) {
123 final List<Template_Type> result = new LinkedList<Template_Type>(); 123 final List<Template_Type> result = new LinkedList<>();
124 for (Template_Type template : templates) { 124 for (Template_Type template : templateList) {
125 if (!template.isRedundant()) { 125 if (!template.isRedundant()) {
126 result.add(template); 126 result.add(template);
127 } 127 }
128 } 128 }
129 return result; 129 return result;
130 } 130 }
131 return templates; 131 return templateList;
132 } 132 }
133 133
134 /** 134 /**
135 * Initializes the set of label and raw templates that will be generated as assembler methods. 135 * Initializes the set of label and raw templates that will be generated as assembler methods.
136 * This includes doing any filtering out of templates based on an {@linkplain #assemblerInterfaceNameOption assembler interface}. 136 * This includes doing any filtering out of templates based on an {@linkplain #assemblerInterfaceNameOption assembler interface}.
141 final String assemblerInterfaceName = assemblerInterfaceNameOption.getValue(); 141 final String assemblerInterfaceName = assemblerInterfaceNameOption.getValue();
142 if (assemblerInterfaceName == null) { 142 if (assemblerInterfaceName == null) {
143 templates = filterTemplates(assembly().templates()); 143 templates = filterTemplates(assembly().templates());
144 labelTemplates = filterTemplates(assembly().labelTemplates()); 144 labelTemplates = filterTemplates(assembly().labelTemplates());
145 } else { 145 } else {
146 final List<Template_Type> newTemplates = new ArrayList<Template_Type>(); 146 final List<Template_Type> newTemplates = new ArrayList<>();
147 final List<Template_Type> newLabelTemplates = new ArrayList<Template_Type>(); 147 final List<Template_Type> newLabelTemplates = new ArrayList<>();
148 148
149 Class assemberInterface = null; 149 Class assemberInterface = null;
150 try { 150 try {
151 assemberInterface = Class.forName(assemblerInterfaceName); 151 assemberInterface = Class.forName(assemblerInterfaceName);
152 ProgramError.check(assemberInterface.isInterface(), "The class " + assemblerInterfaceName + " is not an interface"); 152 ProgramError.check(assemberInterface.isInterface(), "The class " + assemblerInterfaceName + " is not an interface");
153 } catch (ClassNotFoundException e) { 153 } catch (ClassNotFoundException e) {
154 throw ProgramError.unexpected("The assembler interface class " + assemblerInterfaceName + " must be on the class path"); 154 throw ProgramError.unexpected("The assembler interface class " + assemblerInterfaceName + " must be on the class path");
155 } 155 }
156 final Set<MethodKey> assemblerInterfaceMethods = new HashSet<MethodKey>(); 156 final Set<MethodKey> assemblerInterfaceMethods = new HashSet<>();
157 for (Method assemblerInterfaceMethod : assemberInterface.getDeclaredMethods()) { 157 for (Method assemblerInterfaceMethod : assemberInterface.getDeclaredMethods()) {
158 assemblerInterfaceMethods.add(new MethodKey(assemblerInterfaceMethod)); 158 assemblerInterfaceMethods.add(new MethodKey(assemblerInterfaceMethod));
159 } 159 }
160 160
161 for (Template_Type labelTemplate : assembly().labelTemplates()) { 161 for (Template_Type labelTemplate : assembly().labelTemplates()) {
211 */ 211 */
212 private File getSourceFileFor(String className) { 212 private File getSourceFileFor(String className) {
213 return new File(outputDirectoryOption.getValue(), className.replace('.', File.separatorChar) + ".java").getAbsoluteFile(); 213 return new File(outputDirectoryOption.getValue(), className.replace('.', File.separatorChar) + ".java").getAbsoluteFile();
214 } 214 }
215 215
216 protected final String formatParameterList(String separator, List<? extends Parameter> parameters, boolean typesOnly) { 216 protected static final String formatParameterList(String separator, List<? extends Parameter> parameters, boolean typesOnly) {
217 String sep = separator; 217 String sep = separator;
218 final StringBuilder sb = new StringBuilder(); 218 final StringBuilder sb = new StringBuilder();
219 for (Parameter parameter : parameters) { 219 for (Parameter parameter : parameters) {
220 sb.append(sep); 220 sb.append(sep);
221 sb.append(Classes.getSimpleName(parameter.type(), true)); 221 sb.append(Classes.getSimpleName(parameter.type(), true));
237 */ 237 */
238 protected abstract int printMethod(IndentWriter writer, Template_Type template); 238 protected abstract int printMethod(IndentWriter writer, Template_Type template);
239 239
240 /** 240 /**
241 * Prints the source code for support methods that are used by the methods printed by {@link #printMethod(IndentWriter, Template)}. 241 * Prints the source code for support methods that are used by the methods printed by {@link #printMethod(IndentWriter, Template)}.
242 * @param writer
242 * 243 *
243 * @return the number of subroutines printed 244 * @return the number of subroutines printed
244 */ 245 */
245 protected int printSubroutines(IndentWriter writer) { 246 protected int printSubroutines(IndentWriter writer) {
246 return 0; 247 return 0;
253 * @param templateList the list of templates for which code is being generated 254 * @param templateList the list of templates for which code is being generated
254 * @return a set of packages sorted by name 255 * @return a set of packages sorted by name
255 */ 256 */
256 public Set<String> getImportPackages(String className, Iterable<Template_Type> templateList) { 257 public Set<String> getImportPackages(String className, Iterable<Template_Type> templateList) {
257 final String outputPackage = getPackageName(className); 258 final String outputPackage = getPackageName(className);
258 final Set<String> packages = new TreeSet<String>(); 259 final Set<String> packages = new TreeSet<>();
259 packages.add(getPackageName(AssemblyException.class)); 260 packages.add(getPackageName(AssemblyException.class));
260 packages.add(getPackageName(Label.class)); 261 packages.add(getPackageName(Label.class));
261 for (Template_Type template : templateList) { 262 for (Template_Type template : templateList) {
262 for (Parameter parameter : template.parameters()) { 263 for (Parameter parameter : template.parameters()) {
263 final Class type = parameter.type(); 264 final Class type = parameter.type();
286 * This method is overridden by subclasses that may generate the code for 2 related label templates 287 * This method is overridden by subclasses that may generate the code for 2 related label templates
287 * in a single assembler method. For example, on X86 most branch instructions can take offsets of variable bit widths 288 * in a single assembler method. For example, on X86 most branch instructions can take offsets of variable bit widths
288 * and the logic for decoding the bit width of a {@link Label} value may be generated in a single assembler method. 289 * and the logic for decoding the bit width of a {@link Label} value may be generated in a single assembler method.
289 * <p> 290 * <p>
290 * The default implementation of this method returns {@code false}. 291 * The default implementation of this method returns {@code false}.
292 * @param labelTemplate
291 */ 293 */
292 protected boolean omitLabelTemplate(Template_Type labelTemplate) { 294 protected boolean omitLabelTemplate(Template_Type labelTemplate) {
293 return false; 295 return false;
294 } 296 }
295 297
296 /** 298 /**
297 * Gets a reference to the architecture manual section describing the given template. The 299 * Gets a reference to the architecture manual section describing the given template. The
298 * returned string should conform to the format of the {@code @see} Javadoc tag. 300 * returned string should conform to the format of the {@code @see} Javadoc tag.
301 * @param template
299 */ 302 */
300 protected String getJavadocManualReference(Template_Type template) { 303 protected String getJavadocManualReference(Template_Type template) {
301 return null; 304 return null;
302 } 305 }
303 306
306 * print what raw instruction they are derived from. 309 * print what raw instruction they are derived from.
307 * 310 *
308 * @param extraLinks 311 * @param extraLinks
309 * a sequence to which extra javadoc links should be appended 312 * a sequence to which extra javadoc links should be appended
310 */ 313 */
314 @SuppressWarnings("unused")
311 protected void printExtraMethodJavadoc(IndentWriter writer, Template_Type template, List<String> extraLinks, boolean forLabelAssemblerMethod) { 315 protected void printExtraMethodJavadoc(IndentWriter writer, Template_Type template, List<String> extraLinks, boolean forLabelAssemblerMethod) {
312 } 316 }
313 317
314 private boolean seenNoSuchAssemblerMethodError; 318 private boolean seenNoSuchAssemblerMethodError;
315 319
317 * Writes the Javadoc comment for an assembler method. 321 * Writes the Javadoc comment for an assembler method.
318 * 322 *
319 * @param template the template from which the assembler method is generated 323 * @param template the template from which the assembler method is generated
320 */ 324 */
321 protected void printMethodJavadoc(IndentWriter writer, Template_Type template, boolean forLabelAssemblerMethod) { 325 protected void printMethodJavadoc(IndentWriter writer, Template_Type template, boolean forLabelAssemblerMethod) {
322 final List<String> extraLinks = new LinkedList<String>(); 326 final List<String> extraLinks = new LinkedList<>();
323 final List<? extends Parameter> parameters = getParameters(template, forLabelAssemblerMethod); 327 final List<? extends Parameter> parameters = getParameters(template, forLabelAssemblerMethod);
324 writer.println("/**"); 328 writer.println("/**");
325 writer.println(" * Pseudo-external assembler syntax: {@code " + template.externalName() + externalMnemonicSuffixes(parameters) + " }" + externalParameters(parameters)); 329 writer.println(" * Pseudo-external assembler syntax: {@code " + template.externalName() + externalMnemonicSuffixes(parameters) + " }" + externalParameters(parameters));
326 330
327 final boolean printExampleInstruction = true; 331 final boolean printExampleInstruction = true;
328 if (printExampleInstruction) { 332 if (printExampleInstruction) {
329 333
330 final List<Argument> arguments = new ArrayList<Argument>(); 334 final List<Argument> arguments = new ArrayList<>();
331 final AddressMapper addressMapper = new AddressMapper(); 335 final AddressMapper addressMapper = new AddressMapper();
332 for (Parameter p : template.parameters()) { 336 for (Parameter p : template.parameters()) {
333 final Argument exampleArg = p.getExampleArgument(); 337 final Argument exampleArg = p.getExampleArgument();
334 if (exampleArg != null) { 338 if (exampleArg != null) {
335 arguments.add(exampleArg); 339 arguments.add(exampleArg);
358 } 362 }
359 } 363 }
360 } 364 }
361 365
362 printExtraMethodJavadoc(writer, template, extraLinks, forLabelAssemblerMethod); 366 printExtraMethodJavadoc(writer, template, extraLinks, forLabelAssemblerMethod);
363 final List<InstructionConstraint> constraints = new ArrayList<InstructionConstraint>(template.instructionDescription().specifications().size()); 367 final List<InstructionConstraint> constraints = new ArrayList<>(template.instructionDescription().specifications().size());
364 for (Object s : template.instructionDescription().specifications()) { 368 for (Object s : template.instructionDescription().specifications()) {
365 if (s instanceof InstructionConstraint) { 369 if (s instanceof InstructionConstraint) {
366 constraints.add((InstructionConstraint) s); 370 constraints.add((InstructionConstraint) s);
367 } 371 }
368 } 372 }
392 writer.println(" */"); 396 writer.println(" */");
393 } 397 }
394 398
395 protected abstract DisassembledInstruction generateExampleInstruction(Template_Type template, List<Argument> arguments) throws AssemblyException; 399 protected abstract DisassembledInstruction generateExampleInstruction(Template_Type template, List<Argument> arguments) throws AssemblyException;
396 400
397 private String externalParameters(List< ? extends Parameter> parameters) { 401 private static String externalParameters(List< ? extends Parameter> parameters) {
398 final StringBuilder sb = new StringBuilder(); 402 final StringBuilder sb = new StringBuilder();
399 boolean first = true; 403 boolean first = true;
400 for (Parameter parameter : parameters) { 404 for (Parameter parameter : parameters) {
401 if (!ExternalMnemonicSuffixArgument.class.isAssignableFrom(parameter.type())) { 405 if (!ExternalMnemonicSuffixArgument.class.isAssignableFrom(parameter.type())) {
402 if (!first) { 406 if (!first) {
407 } 411 }
408 } 412 }
409 return sb.toString(); 413 return sb.toString();
410 } 414 }
411 415
412 private String externalMnemonicSuffixes(List< ? extends Parameter> parameters) { 416 private static String externalMnemonicSuffixes(List< ? extends Parameter> parameters) {
413 final StringBuilder sb = new StringBuilder(); 417 final StringBuilder sb = new StringBuilder();
414 for (Parameter parameter : parameters) { 418 for (Parameter parameter : parameters) {
415 if (ExternalMnemonicSuffixArgument.class.isAssignableFrom(parameter.type())) { 419 if (ExternalMnemonicSuffixArgument.class.isAssignableFrom(parameter.type())) {
416 boolean first = true; 420 boolean first = true;
417 String close = "]"; 421 String close = "]";
446 final CharArraySource charArrayWriter = new CharArraySource((int) sourceFile.length()); 450 final CharArraySource charArrayWriter = new CharArraySource((int) sourceFile.length());
447 final IndentWriter writer = new IndentWriter(new PrintWriter(charArrayWriter)); 451 final IndentWriter writer = new IndentWriter(new PrintWriter(charArrayWriter));
448 writer.indent(); 452 writer.indent();
449 453
450 int codeLineCount = 0; 454 int codeLineCount = 0;
451 final Map<InstructionDescription, Integer> instructionDescriptions = new HashMap<InstructionDescription, Integer>(); 455 final Map<InstructionDescription, Integer> instructionDescriptions = new HashMap<>();
452 int maxTemplatesPerDescription = 0; 456 int maxTemplatesPerDescription = 0;
453 int i = 0; 457 int i = 0;
454 for (Template_Type template : templateList) { 458 for (Template_Type template : templateList) {
455 printMethodComment(writer, template, i + 1, false); 459 printMethodComment(writer, template, i + 1, false);
456 codeLineCount += printMethod(writer, template); 460 codeLineCount += printMethod(writer, template);
492 protected static List<Parameter> getParameters(Template template, boolean forLabelAssemblerMethod) { 496 protected static List<Parameter> getParameters(Template template, boolean forLabelAssemblerMethod) {
493 if (!forLabelAssemblerMethod || template.labelParameterIndex() == -1) { 497 if (!forLabelAssemblerMethod || template.labelParameterIndex() == -1) {
494 final Class<List<Parameter>> type = null; 498 final Class<List<Parameter>> type = null;
495 return Utils.cast(type, template.parameters()); 499 return Utils.cast(type, template.parameters());
496 } 500 }
497 final List<Parameter> parameters = new ArrayList<Parameter>(template.parameters()); 501 final List<Parameter> parameters = new ArrayList<>(template.parameters());
498 parameters.set(template.labelParameterIndex(), LabelParameter.LABEL); 502 parameters.set(template.labelParameterIndex(), LabelParameter.LABEL);
499 return parameters; 503 return parameters;
500 } 504 }
501 505
502 protected void printLabelMethodHead(IndentWriter writer, Template_Type template, List<Parameter> parameters) { 506 protected void printLabelMethodHead(IndentWriter writer, Template_Type template, List<Parameter> parameters) {
663 assemblerClassName, 667 assemblerClassName,
664 labelInstructionSubclassGenerator); 668 labelInstructionSubclassGenerator);
665 labelMethodHelperClasses.add(stringWriter.toString()); 669 labelMethodHelperClasses.add(stringWriter.toString());
666 } 670 }
667 671
668 private final List<String> labelMethodHelperClasses = new ArrayList<String>(); 672 private final List<String> labelMethodHelperClasses = new ArrayList<>();
669 673
670 private void printLabelMethodHelperClass( 674 private void printLabelMethodHelperClass(
671 IndentWriter writer, 675 IndentWriter writer,
672 Template_Type template, 676 Template_Type template,
673 List<Parameter> parameters, 677 List<Parameter> parameters,
763 767
764 if (rawAssemblerClassName.equals(labelAssemblerClassName)) { 768 if (rawAssemblerClassName.equals(labelAssemblerClassName)) {
765 if (rawAssemblerMethodsUpdated || labelAssemblerMethodsUpdated) { 769 if (rawAssemblerMethodsUpdated || labelAssemblerMethodsUpdated) {
766 System.out.println("modified: " + getSourceFileFor(rawAssemblerClassName)); 770 System.out.println("modified: " + getSourceFileFor(rawAssemblerClassName));
767 if (!ToolChain.compile(AssemblerGenerator.class, rawAssemblerClassName)) { 771 if (!ToolChain.compile(AssemblerGenerator.class, rawAssemblerClassName)) {
768 List<Template_Type> allTemplates = new ArrayList<Template_Type>(templates()); 772 List<Template_Type> allTemplates = new ArrayList<>(templates());
769 allTemplates.addAll(labelTemplates()); 773 allTemplates.addAll(labelTemplates());
770 throw ProgramError.unexpected("compilation failed for: " + rawAssemblerClassName + 774 throw ProgramError.unexpected("compilation failed for: " + rawAssemblerClassName +
771 "[Maybe missing an import statement for one of the following packages: " + 775 "[Maybe missing an import statement for one of the following packages: " +
772 getImportPackages(rawAssemblerClassName, allTemplates)); 776 getImportPackages(rawAssemblerClassName, allTemplates));
773 } 777 }