comparison graal/com.oracle.max.asmdis/src/com/sun/max/asm/sparc/complete/SPARCAssembler.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.sparc.complete;
24
25 import static com.sun.max.asm.sparc.GPR.*;
26
27 import com.sun.max.asm.*;
28 import com.sun.max.asm.sparc.*;
29 import com.sun.max.lang.*;
30 import com.sun.max.program.*;
31
32 /**
33 * The base class for the 32-bit and 64-bit SPARC assemblers. This class also defines
34 * the more complex synthetic SPARC instructions.
35 */
36 public abstract class SPARCAssembler extends SPARCLabelAssembler {
37
38 public static SPARCAssembler createAssembler(WordWidth wordWidth) {
39 switch (wordWidth) {
40 case BITS_32:
41 return new SPARC32Assembler();
42 case BITS_64:
43 return new SPARC64Assembler();
44 default:
45 throw ProgramError.unexpected("Invalid word width specification");
46 }
47 }
48
49 // Utilities:
50
51 public static int hi(int i) {
52 return (i & 0xfffffc00) >> 10;
53 }
54
55 public static int lo(int i) {
56 return i & 0x000003ff;
57 }
58
59 public static int hi(long i) {
60 return hi((int) i);
61 }
62
63 public static int lo(long i) {
64 return lo((int) i);
65 }
66
67 public static int uhi(long i) {
68 return hi((int) (i >> 32));
69 }
70
71 public static int ulo(long i) {
72 return lo((int) (i >> 32));
73 }
74
75 public static boolean isSimm11(int value) {
76 return Ints.numberOfEffectiveSignedBits(value) <= 11;
77 }
78
79 public static boolean isSimm13(int value) {
80 return Ints.numberOfEffectiveSignedBits(value) <= 13;
81 }
82
83 public static boolean isSimm13(long value) {
84 return Longs.numberOfEffectiveSignedBits(value) <= 13;
85 }
86
87 public static boolean isSimm19(int value) {
88 return Ints.numberOfEffectiveSignedBits(value) <= 19;
89 }
90
91 public static int setswNumberOfInstructions(int imm) {
92 if (0 <= imm && lo(imm) == 0) {
93 return 1;
94 } else if (-4096 <= imm && imm <= 4095) {
95 return 1;
96 } else if (imm >= 0 || lo(imm) == 0) {
97 return 2;
98 } else {
99 return 3;
100 }
101 }
102
103 public static int setuwNumberOfInstructions(int imm) {
104 if (lo(imm) == 0) {
105 return 1;
106 } else if (0 <= imm && imm <= 4095) {
107 return 1;
108 } else {
109 return 2;
110 }
111 }
112
113 @Override
114 protected void emitPadding(int numberOfBytes) throws AssemblyException {
115 if ((numberOfBytes & 0x3) != 0) {
116 throw new AssemblyException("Cannot pad instruction stream with a number of bytes not divisble by 4");
117 }
118 for (int i = 0; i < numberOfBytes >> 2; i++) {
119 nop();
120 }
121 }
122
123 // Complex synthetic instructions according to appendix G3 of the SPARC Architecture Manual V9:
124
125 public void setuw(int imm, GPR rd) {
126 if (lo(imm) == 0) {
127 sethi(hi(imm), rd);
128 } else if (0 <= imm && imm <= 4095) {
129 or(G0, imm, rd);
130 } else {
131 sethi(hi(imm), rd);
132 or(rd, lo(imm), rd);
133 }
134 }
135
136 public void set(int imm, GPR rd) {
137 setuw(imm, rd);
138 }
139
140 public void setsw(int imm, GPR rd) {
141 if (0 <= imm && lo(imm) == 0) {
142 sethi(hi(imm), rd);
143 } else if (-4096 <= imm && imm <= 4095) {
144 or(G0, imm, rd);
145 } else if (imm < 0 && lo(imm) == 0) {
146 sethi(hi(imm), rd);
147 sra(rd, G0, rd);
148 } else if (imm >= 0) {
149 sethi(hi(imm), rd);
150 or(rd, lo(imm), rd);
151 } else {
152 sethi(hi(imm), rd);
153 or(rd, lo(imm), rd);
154 sra(rd, G0, rd);
155 }
156 }
157
158 public void setx(long imm, GPR temp, GPR rd) {
159 sethi(uhi(imm), temp);
160 or(temp, ulo(imm), temp);
161 sllx(temp, 32, temp);
162 sethi(hi(imm), rd);
163 or(rd, temp, rd);
164 or(rd, lo(imm), rd);
165 }
166
167 public void sethi(Label label, final GPR rd) {
168 final int startPosition = currentPosition();
169 emitInt(0);
170 new InstructionWithAddress(this, startPosition, startPosition + 4, label) {
171 @Override
172 protected void assemble() throws AssemblyException {
173 final int imm22 = hi(addressAsInt());
174 sethi(imm22, rd);
175 }
176 };
177 }
178
179 public void add(final GPR rs1, final Label label, final GPR rd) {
180 final int startPosition = currentPosition();
181 emitInt(0);
182 new InstructionWithAddress(this, startPosition, startPosition + 4, label) {
183 @Override
184 protected void assemble() throws AssemblyException {
185 final int simm13 = lo(addressAsInt());
186 add(rs1, simm13, rd);
187 }
188 };
189 }
190
191 public void ld(final GPR rs1, final Label base, final Label target, final SFPR rd) {
192 final int startPosition = currentPosition();
193 emitInt(0);
194 new InstructionWithOffset(this, startPosition, startPosition + 4, target) {
195 @Override
196 protected void assemble() throws AssemblyException {
197 ld(rs1, labelOffsetRelative(target, base), rd);
198 }
199 };
200 }
201
202 public void ldd(final GPR rs1, final Label base, final Label target, final DFPR rd) {
203 final int startPosition = currentPosition();
204 emitInt(0);
205 new InstructionWithOffset(this, startPosition, startPosition + 4, target) {
206 @Override
207 protected void assemble() throws AssemblyException {
208 ldd(rs1, labelOffsetRelative(target, base), rd);
209 }
210 };
211 }
212
213 public void ldx(final GPR rs1, final Label base, final Label target, final GPR rd) {
214 final int startPosition = currentPosition();
215 emitInt(0);
216 new InstructionWithOffset(this, startPosition, startPosition + 4, target) {
217 @Override
218 protected void assemble() throws AssemblyException {
219 ldx(rs1, labelOffsetRelative(target, base), rd);
220 }
221 };
222 }
223
224 public void add(final GPR rs1, final Label base, final Label target, final GPR rd) {
225 final int startPosition = currentPosition();
226 emitInt(0);
227 new InstructionWithOffset(this, startPosition, startPosition + 4, target) {
228 @Override
229 protected void assemble() throws AssemblyException {
230 add(rs1, labelOffsetRelative(target, base), rd);
231 }
232 };
233 }
234 public void addcc(final GPR rs1, final Label base, final Label target, final GPR rd) {
235 final int startPosition = currentPosition();
236 emitInt(0);
237 new InstructionWithOffset(this, startPosition, startPosition + 4, target) {
238 @Override
239 protected void assemble() throws AssemblyException {
240 addcc(rs1, labelOffsetRelative(target, base), rd);
241 }
242 };
243 }
244 }