comparison graal/com.oracle.max.asmdis/src/com/sun/max/asm/gen/risc/RiscInstructionDescriptionCreator.java @ 3733:e233f5660da4

Added Java files from Maxine project.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 19:59:18 +0100
parents
children bc8527f3071c
comparison
equal deleted inserted replaced
3732:3e2e8b8abdaf 3733:e233f5660da4
1 /*
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.sun.max.asm.gen.risc;
24
25 import java.util.*;
26
27 import com.sun.max.*;
28 import com.sun.max.asm.gen.*;
29 import com.sun.max.asm.gen.risc.bitRange.*;
30 import com.sun.max.asm.gen.risc.field.*;
31 import com.sun.max.program.*;
32
33 /**
34 */
35 public abstract class RiscInstructionDescriptionCreator extends InstructionDescriptionCreator<RiscInstructionDescription> {
36
37 protected final RiscTemplateCreator templateCreator;
38
39 protected RiscInstructionDescriptionCreator(Assembly assembly, RiscTemplateCreator templateCreator) {
40 super(assembly);
41 this.templateCreator = templateCreator;
42 }
43
44 @Override
45 protected RiscInstructionDescription createInstructionDescription(List<Object> specifications) {
46 return new RiscInstructionDescription(specifications);
47 }
48
49 private int firstStringIndex(List<Object> specifications) {
50 for (int i = 0; i < specifications.size(); i++) {
51 if (specifications.get(i) instanceof String) {
52 return i;
53 }
54 }
55 throw ProgramError.unexpected("template instruction description without name");
56 }
57
58 private void setFirstString(List<Object> specifications, String value) {
59 specifications.set(firstStringIndex(specifications), value);
60 }
61
62 private void eliminateConstraintFor(Parameter parameter, List<Object> specifications) {
63 for (final Iterator iterator = specifications.iterator(); iterator.hasNext();) {
64 final Object s = iterator.next();
65 if (s instanceof InstructionConstraint) {
66 final InstructionConstraint constraint = (InstructionConstraint) s;
67 if (constraint.referencesParameter(parameter)) {
68 iterator.remove();
69 }
70 }
71 }
72 }
73
74 private boolean updateSpecifications(List<Object> specifications, Object pattern) {
75 for (int i = 0; i < specifications.size(); i++) {
76 final Object specification = specifications.get(i);
77 if (specification.equals(pattern)) {
78 specifications.set(i, pattern);
79 return true;
80 } else if (pattern instanceof RiscConstant && (specification instanceof OperandField || specification instanceof OptionField)) {
81 final RiscConstant constant = (RiscConstant) pattern;
82 final RiscField constantField = constant.field();
83 final RiscField variableField = (RiscField) specification;
84 if (variableField.equals(constantField)) {
85 specifications.set(i, pattern);
86 if (specification instanceof Parameter) {
87 eliminateConstraintFor((Parameter) specification, specifications);
88 }
89 return true;
90 }
91 } else if (pattern instanceof InstructionConstraint && !(pattern instanceof Parameter)) {
92 specifications.add(pattern);
93 return true;
94 } else if (pattern instanceof RiscField) {
95 if (((RiscField) pattern).bitRange() instanceof OmittedBitRange) {
96 specifications.add(pattern);
97 return true;
98 }
99 }
100 }
101 return false;
102 }
103
104 private RiscInstructionDescription createSyntheticInstructionDescription(String name, RiscTemplate template, Object[] patterns) {
105 final List<Object> specifications = new ArrayList<Object>(template.instructionDescription().specifications());
106 for (Object pattern : patterns) {
107 if (!updateSpecifications(specifications, pattern)) {
108 // InstructionDescription with the same name, but different specifications, skip it:
109 Trace.line(3, name + " not updated with " + pattern + " in " + specifications);
110 return null;
111 }
112 }
113 setFirstString(specifications, name);
114 final Class<List<Object>> type = null;
115 return (RiscInstructionDescription) defineInstructionDescription(Utils.cast(type, specifications)).beSynthetic();
116 }
117
118 /**
119 * Creates a synthetic instruction from a previously defined (raw or synthetic) instruction
120 * by replacing one or more parameters of the instruction with a constant or alternative parameter.
121 *
122 * @param name the internal (base) name of the new synthetic instruction
123 * @param templateName the internal name of the original instruction on which the synthetic instruction is based
124 * @param patterns the replacements for one or more parameters of the original instruction
125 * @return the newly created instruction descriptions resulting from the substitution wrapped in a RiscInstructionDescriptionModifier
126 */
127 protected RiscInstructionDescriptionModifier synthesize(String name, String templateName, Object... patterns) {
128 final List<RiscInstructionDescription> instructionDescriptions = new ArrayList<RiscInstructionDescription>();
129 // Creating a new VariableSequence here prevents iterator comodification below:
130 final List<? extends RiscTemplate> nameTemplates = templateCreator.nameToTemplates(templateName);
131 if (!nameTemplates.isEmpty()) {
132 final List<RiscTemplate> templates = new ArrayList<RiscTemplate>(nameTemplates);
133 assert !templates.isEmpty();
134 for (RiscTemplate template : templates) {
135 final RiscInstructionDescription instructionDescription = createSyntheticInstructionDescription(name, template, patterns);
136 if (instructionDescription != null) {
137 instructionDescriptions.add(instructionDescription);
138 }
139 }
140 }
141 ProgramError.check(!instructionDescriptions.isEmpty());
142 return new RiscInstructionDescriptionModifier(instructionDescriptions);
143 }
144 }