Mercurial > hg > truffle
comparison graal/com.oracle.max.asmdis/src/com/sun/max/asm/gen/risc/RiscTemplate.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.asm.gen.*; | |
28 import com.sun.max.asm.gen.risc.field.*; | |
29 import com.sun.max.program.*; | |
30 | |
31 /** | |
32 */ | |
33 public class RiscTemplate extends Template implements RiscInstructionDescriptionVisitor { | |
34 | |
35 private final List<RiscField> allFields = new LinkedList<RiscField>(); | |
36 private final List<OperandField> operandFields = new LinkedList<OperandField>(); | |
37 private final List<OptionField> optionFields = new LinkedList<OptionField>(); | |
38 private final List<OperandField> parameters = new ArrayList<OperandField>(); | |
39 private final List<Option> options = new LinkedList<Option>(); | |
40 | |
41 private int opcode; | |
42 private int opcodeMask; | |
43 private RiscTemplate canonicalRepresentative; | |
44 | |
45 public RiscTemplate(InstructionDescription instructionDescription) { | |
46 super(instructionDescription); | |
47 } | |
48 | |
49 @Override | |
50 public RiscInstructionDescription instructionDescription() { | |
51 return (RiscInstructionDescription) super.instructionDescription(); | |
52 } | |
53 | |
54 private RiscTemplate synthesizedFrom; | |
55 | |
56 public void setSynthesizedFrom(RiscTemplate synthesizedFrom) { | |
57 assert instructionDescription().isSynthetic(); | |
58 this.synthesizedFrom = synthesizedFrom; | |
59 } | |
60 | |
61 public RiscTemplate synthesizedFrom() { | |
62 return synthesizedFrom; | |
63 } | |
64 | |
65 /** | |
66 * Adds the value of a constant field to the opcode of the instruction and | |
67 * updates the opcode mask to include the bits of the field. | |
68 * | |
69 * @param field a field containing a constant value | |
70 * @param value the constant value | |
71 */ | |
72 private void organizeConstant(RiscField field, int value) { | |
73 try { | |
74 opcode |= field.bitRange().assembleUnsignedInt(value); | |
75 opcodeMask |= field.bitRange().instructionMask(); | |
76 } catch (IndexOutOfBoundsException indexOutOfBoundsException) { | |
77 throw ProgramError.unexpected("operand for constant field " + field.name() + " does not fit: " + value); | |
78 } | |
79 } | |
80 | |
81 public void visitField(RiscField field) { | |
82 allFields.add(field); | |
83 if (field instanceof OperandField) { | |
84 final OperandField operandField = (OperandField) field; | |
85 if (field instanceof OffsetParameter) { | |
86 setLabelParameterIndex(); | |
87 } | |
88 if (operandField.boundTo() == null) { | |
89 parameters.add(operandField); | |
90 } | |
91 operandFields.add(operandField); | |
92 } else if (field instanceof OptionField) { | |
93 optionFields.add((OptionField) field); | |
94 } else if (field instanceof ReservedField) { | |
95 organizeConstant(field, 0); | |
96 } else { | |
97 throw ProgramError.unexpected("unknown or unallowed type of field: " + field); | |
98 } | |
99 } | |
100 | |
101 public void visitConstant(RiscConstant constant) { | |
102 organizeConstant(constant.field(), constant.value()); | |
103 } | |
104 | |
105 public void visitConstraint(InstructionConstraint constraint) { | |
106 } | |
107 | |
108 /** | |
109 * Sets the internal name of this template from a given string it is not already set. | |
110 * | |
111 * @param string a string specified in the to consider | |
112 */ | |
113 public void visitString(String string) { | |
114 if (internalName() == null) { | |
115 setInternalName(string); | |
116 } | |
117 } | |
118 | |
119 public List<OperandField> operandFields() { | |
120 return operandFields; | |
121 } | |
122 | |
123 public int opcode() { | |
124 return opcode; | |
125 } | |
126 | |
127 public int opcodeMask() { | |
128 return opcodeMask; | |
129 } | |
130 | |
131 public List<OptionField> optionFields() { | |
132 return optionFields; | |
133 } | |
134 | |
135 public void addOptionField(OptionField f) { | |
136 allFields.add(f); | |
137 optionFields.add(f); | |
138 } | |
139 | |
140 public int specificity() { | |
141 return Integer.bitCount(opcodeMask); | |
142 } | |
143 | |
144 public void organizeOption(Option option, RiscTemplate canonicalRepresentative) { | |
145 instructionDescription().setExternalName(externalName() + option.externalName()); | |
146 setInternalName(internalName() + option.name()); | |
147 try { | |
148 opcode |= option.field().bitRange().assembleUnsignedInt(option.value()); | |
149 opcodeMask |= option.field().bitRange().instructionMask(); | |
150 } catch (IndexOutOfBoundsException e) { | |
151 throw ProgramError.unexpected("Option: " + option.name() + " does not fit in field " + option.field().name()); | |
152 } | |
153 | |
154 options.add(option); | |
155 if (option.isRedundant()) { | |
156 this.canonicalRepresentative = canonicalRepresentative; | |
157 } | |
158 } | |
159 | |
160 @Override | |
161 public Template canonicalRepresentative() { | |
162 return canonicalRepresentative; | |
163 } | |
164 | |
165 @Override | |
166 public String assemblerMethodName() { | |
167 return internalName(); | |
168 } | |
169 | |
170 @Override | |
171 public List<Operand> operands() { | |
172 throw ProgramError.unexpected("unimplemented"); | |
173 } | |
174 | |
175 @Override | |
176 public List<OperandField> parameters() { | |
177 return parameters; | |
178 } | |
179 | |
180 @Override | |
181 public String toString() { | |
182 return "<" + getClass().getSimpleName() + " #" + serial() + ": " + internalName() + " " + Integer.toHexString(opcode()) + ", " + parameters() + ">"; | |
183 } | |
184 | |
185 } |