comparison graal/com.oracle.graal.asm/src/com/oracle/graal/asm/Assembler.java @ 14032:d1c1f103d42c

renamed com.oracle.graal.asm.AbstractAssembler to com.oracle.graal.asm.Assembler
author twisti
date Thu, 27 Feb 2014 11:36:25 -0800
parents graal/com.oracle.graal.asm/src/com/oracle/graal/asm/AbstractAssembler.java@390c4b742890
children 164903a50a9a
comparison
equal deleted inserted replaced
14031:390c4b742890 14032:d1c1f103d42c
1 /*
2 * Copyright (c) 2009, 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.oracle.graal.asm;
24
25 import java.nio.*;
26 import java.util.*;
27
28 import com.oracle.graal.api.code.*;
29
30 /**
31 * The platform-independent base class for the assembler.
32 */
33 public abstract class Assembler {
34
35 public final TargetDescription target;
36
37 /**
38 * Backing code buffer.
39 */
40 private final Buffer codeBuffer;
41
42 public Assembler(TargetDescription target) {
43 this.target = target;
44 if (target.arch.getByteOrder() == ByteOrder.BIG_ENDIAN) {
45 this.codeBuffer = new Buffer.BigEndian();
46 } else {
47 this.codeBuffer = new Buffer.LittleEndian();
48 }
49 }
50
51 /**
52 * Returns the current position of the underlying code buffer.
53 *
54 * @return current position in code buffer
55 */
56 public int position() {
57 return codeBuffer.position();
58 }
59
60 public final void emitByte(int x) {
61 codeBuffer.emitByte(x);
62 }
63
64 public final void emitShort(int x) {
65 codeBuffer.emitShort(x);
66 }
67
68 public final void emitInt(int x) {
69 codeBuffer.emitInt(x);
70 }
71
72 public final void emitLong(long x) {
73 codeBuffer.emitLong(x);
74 }
75
76 public final void emitByte(int b, int pos) {
77 codeBuffer.emitByte(b, pos);
78 }
79
80 public final void emitShort(int b, int pos) {
81 codeBuffer.emitShort(b, pos);
82 }
83
84 public final void emitInt(int b, int pos) {
85 codeBuffer.emitInt(b, pos);
86 }
87
88 public final void emitLong(long b, int pos) {
89 codeBuffer.emitLong(b, pos);
90 }
91
92 public final int getByte(int pos) {
93 return codeBuffer.getByte(pos);
94 }
95
96 public final int getShort(int pos) {
97 return codeBuffer.getShort(pos);
98 }
99
100 public final int getInt(int pos) {
101 return codeBuffer.getInt(pos);
102 }
103
104 private static final String NEWLINE = System.getProperty("line.separator");
105
106 /**
107 * Some GPU architectures have a text based encoding.
108 */
109 public final void emitString(String x) {
110 emitString0("\t"); // XXX REMOVE ME pretty-printing
111 emitString0(x);
112 emitString0(NEWLINE);
113 }
114
115 // XXX for pretty-printing
116 public final void emitString0(String x) {
117 codeBuffer.emitBytes(x.getBytes(), 0, x.length());
118 }
119
120 public void emitString(String s, int pos) {
121 codeBuffer.emitBytes(s.getBytes(), pos);
122 }
123
124 /**
125 * Closes this assembler. No extra data can be written to this assembler after this call.
126 *
127 * @param trimmedCopy if {@code true}, then a copy of the underlying byte array up to (but not
128 * including) {@code position()} is returned
129 * @return the data in this buffer or a trimmed copy if {@code trimmedCopy} is {@code true}
130 */
131 public byte[] close(boolean trimmedCopy) {
132 return codeBuffer.close(trimmedCopy);
133 }
134
135 public void bind(Label l) {
136 assert !l.isBound() : "can bind label only once";
137 l.bind(position());
138 l.patchInstructions(this);
139 }
140
141 public abstract void align(int modulus);
142
143 public abstract void jmp(Label l);
144
145 protected abstract void patchJumpTarget(int branch, int jumpTarget);
146
147 private Map<Label, String> nameMap;
148
149 /**
150 * Creates a name for a label.
151 *
152 * @param l the label for which a name is being created
153 * @param id a label identifier that is unique with the scope of this assembler
154 * @return a label name in the form of "L123"
155 */
156 protected String createLabelName(Label l, int id) {
157 return "L" + id;
158 }
159
160 /**
161 * Gets a name for a label, creating it if it does not yet exist. By default, the returned name
162 * is only unique with the scope of this assembler.
163 */
164 public String nameOf(Label l) {
165 if (nameMap == null) {
166 nameMap = new HashMap<>();
167 }
168 String name = nameMap.get(l);
169 if (name == null) {
170 name = createLabelName(l, nameMap.size());
171 nameMap.put(l, name);
172 }
173 return name;
174 }
175
176 /**
177 * This is used by the CompilationResultBuilder to convert a {@link StackSlot} to an
178 * {@link AbstractAddress}.
179 */
180 public abstract AbstractAddress makeAddress(Register base, int displacement);
181
182 /**
183 * Returns a target specific placeholder address that can be used for code patching.
184 */
185 public abstract AbstractAddress getPlaceholder();
186 }