comparison graal/com.oracle.max.asmdis/src/com/sun/max/asm/gen/risc/RiscExternalInstruction.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.*;
28 import com.sun.max.asm.dis.*;
29 import com.sun.max.asm.gen.*;
30 import com.sun.max.asm.gen.risc.field.*;
31 import com.sun.max.lang.*;
32
33 /**
34 * Output of RISC instructions in external assembler format
35 * We use exactly the same syntax as Sun's SPARC assembler "as"
36 * and GNU's assembler "gas", except for branches to detected labels.
37 * In the latter case, the label's name followed by ":"
38 * is printed instead of ".".
39 *
40 * Examples of branch instructions without labels:
41 *
42 * brz,a . +20
43 * bne . -200
44 *
45 * Examples of branch instructions with detected labels:
46 *
47 * ba L1: +112
48 * be,pt L2: -50
49 */
50 public class RiscExternalInstruction implements RiscInstructionDescriptionVisitor {
51
52 protected final RiscTemplate template;
53 protected final LinkedList<Argument> arguments;
54 protected final ImmediateArgument address;
55 protected final AddressMapper addressMapper;
56
57 public RiscExternalInstruction(RiscTemplate template, List<Argument> arguments) {
58 this.template = template;
59 this.arguments = new LinkedList<Argument>(arguments);
60 this.address = null;
61 this.addressMapper = null;
62 }
63
64 public RiscExternalInstruction(RiscTemplate template, List<Argument> arguments, ImmediateArgument address, AddressMapper addressMapper) {
65 this.template = template;
66 this.arguments = new LinkedList<Argument>(arguments);
67 this.address = address;
68 this.addressMapper = addressMapper;
69 }
70
71 private String nameString;
72
73 public String name() {
74 if (nameString == null) {
75 nameString = template.externalName();
76 for (Argument argument : arguments) {
77 if (argument instanceof ExternalMnemonicSuffixArgument) {
78 final String suffix = argument.externalValue();
79 nameString += suffix;
80 }
81 }
82 }
83 return nameString;
84 }
85
86 private String operandsString;
87
88 public String operands() {
89 if (operandsString == null) {
90 operandsString = "";
91 RiscInstructionDescriptionVisitor.Static.visitInstructionDescription(this, template.instructionDescription());
92 }
93 return operandsString;
94 }
95
96 @Override
97 public String toString() {
98 return Strings.padLengthWithSpaces(name(), 10) + " " + operands();
99 }
100
101 private void print(String s) {
102 operandsString += s;
103 }
104
105 private void printBranchDisplacement(ImmediateArgument immediateArgument) {
106 final int delta = (int) immediateArgument.asLong();
107 if (address != null) {
108 final ImmediateArgument targetAddress = address.plus(delta);
109 final DisassembledLabel label = addressMapper.labelAt(targetAddress);
110 if (label != null) {
111 print(label.name() + ": ");
112 }
113 } else {
114 // (tw) No longer checked for absolute branch, always print "."
115 print(". ");
116 }
117 if (delta >= 0) {
118 print("+");
119 }
120 print(Integer.toString(delta));
121 }
122
123 private Object previousSpecification;
124
125 public void visitField(RiscField field) {
126 if (field instanceof OperandField) {
127 final OperandField operandField = (OperandField) field;
128 if (operandField.boundTo() != null) {
129 return;
130 }
131 final Argument argument = arguments.remove();
132 if (argument instanceof ExternalMnemonicSuffixArgument) {
133 return;
134 }
135 if (previousSpecification != null && !(previousSpecification instanceof String)) {
136 print(", ");
137 }
138 if (argument instanceof ImmediateArgument) {
139 final ImmediateArgument immediateArgument = (ImmediateArgument) argument;
140 if (field instanceof BranchDisplacementOperandField) {
141 printBranchDisplacement(immediateArgument);
142 } else {
143 if (operandField.isSigned()) {
144 print(immediateArgument.signedExternalValue());
145 } else {
146 print(immediateArgument.externalValue());
147 }
148 }
149 } else {
150 print(argument.externalValue());
151 }
152 previousSpecification = field;
153 }
154 }
155
156 public void visitConstant(RiscConstant constant) {
157 }
158
159 private boolean writingStrings;
160
161 public void visitString(String string) {
162 if (writingStrings) {
163 print(string);
164 previousSpecification = string;
165 }
166 writingStrings = true;
167 }
168
169 public void visitConstraint(InstructionConstraint constraint) {
170 }
171
172 }