comparison graal/GraalCompiler/src/com/sun/c1x/asm/TargetMethodAssembler.java @ 2677:0ea5f12e873a

use com.oracle.max.asm project for assembler
author Christian.Wimmer@Oracle.com
date Fri, 13 May 2011 17:09:20 -0700
parents
children 7ed72769d51a
comparison
equal deleted inserted replaced
2676:e0e89714e2f1 2677:0ea5f12e873a
1 /*
2 * Copyright (c) 2011, 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.c1x.asm;
24
25 import java.util.*;
26
27 import com.oracle.max.asm.*;
28 import com.sun.c1x.*;
29 import com.sun.c1x.debug.*;
30 import com.sun.c1x.ir.*;
31 import com.sun.c1x.lir.*;
32 import com.sun.c1x.util.*;
33 import com.sun.cri.ci.*;
34 import com.sun.cri.ri.*;
35
36 public class TargetMethodAssembler {
37 public final AbstractAssembler asm;
38 public final CiTargetMethod targetMethod;
39 public List<ExceptionInfo> exceptionInfoList;
40 protected int lastSafepointPos;
41
42 public TargetMethodAssembler(AbstractAssembler asm) {
43 this.asm = asm;
44 this.targetMethod = new CiTargetMethod();
45 }
46
47 public void setFrameSize(int frameSize) {
48 targetMethod.setFrameSize(frameSize);
49 }
50
51 public CiTargetMethod.Mark recordMark(Object id, CiTargetMethod.Mark[] references) {
52 return targetMethod.recordMark(asm.codeBuffer.position(), id, references);
53 }
54
55 public void blockComment(String s) {
56 targetMethod.addAnnotation(new CiTargetMethod.CodeComment(asm.codeBuffer.position(), s));
57 }
58
59 public CiTargetMethod finishTargetMethod(Object name, RiRuntime runtime, int registerRestoreEpilogueOffset, boolean isStub) {
60 // Install code, data and frame size
61 targetMethod.setTargetCode(asm.codeBuffer.close(false), asm.codeBuffer.position());
62 targetMethod.setRegisterRestoreEpilogueOffset(registerRestoreEpilogueOffset);
63
64 // Record exception handlers if they exist
65 if (exceptionInfoList != null) {
66 for (ExceptionInfo ei : exceptionInfoList) {
67 int codeOffset = ei.codeOffset;
68 for (ExceptionHandler handler : ei.exceptionHandlers) {
69 int entryOffset = handler.entryCodeOffset();
70 RiType caughtType = handler.handler.catchType();
71 targetMethod.recordExceptionHandler(codeOffset, ei.bci, 0, entryOffset, handler.handlerBCI(), caughtType);
72 }
73 }
74 }
75
76 if (C1XOptions.PrintMetrics) {
77 C1XMetrics.TargetMethods++;
78 C1XMetrics.CodeBytesEmitted += targetMethod.targetCodeSize();
79 C1XMetrics.SafepointsEmitted += targetMethod.safepoints.size();
80 C1XMetrics.DirectCallSitesEmitted += targetMethod.directCalls.size();
81 C1XMetrics.IndirectCallSitesEmitted += targetMethod.indirectCalls.size();
82 C1XMetrics.DataPatches += targetMethod.dataReferences.size();
83 C1XMetrics.ExceptionHandlersEmitted += targetMethod.exceptionHandlers.size();
84 }
85
86 if (C1XOptions.PrintAssembly && !TTY.isSuppressed() && !isStub) {
87 Util.printSection("Target Method", Util.SECTION_CHARACTER);
88 TTY.println("Name: " + name);
89 TTY.println("Frame size: " + targetMethod.frameSize());
90 TTY.println("Register size: " + asm.target.arch.registerReferenceMapBitCount);
91
92 if (C1XOptions.PrintCodeBytes) {
93 Util.printSection("Code", Util.SUB_SECTION_CHARACTER);
94 TTY.println("Code: %d bytes", targetMethod.targetCodeSize());
95 Util.printBytes(0L, targetMethod.targetCode(), 0, targetMethod.targetCodeSize(), C1XOptions.PrintAssemblyBytesPerLine);
96 }
97
98 Util.printSection("Disassembly", Util.SUB_SECTION_CHARACTER);
99 String disassembly = runtime.disassemble(targetMethod);
100 TTY.println(disassembly);
101 boolean noDis = disassembly == null || disassembly.length() == 0;
102
103 Util.printSection("Safepoints", Util.SUB_SECTION_CHARACTER);
104 for (CiTargetMethod.Safepoint x : targetMethod.safepoints) {
105 TTY.println(x.toString());
106 if (noDis && x.debugInfo != null) {
107 TTY.println(CiUtil.indent(x.debugInfo.toString(), " "));
108 }
109 }
110
111 Util.printSection("Direct Call Sites", Util.SUB_SECTION_CHARACTER);
112 for (CiTargetMethod.Call x : targetMethod.directCalls) {
113 TTY.println(x.toString());
114 if (noDis && x.debugInfo != null) {
115 TTY.println(CiUtil.indent(x.debugInfo.toString(), " "));
116 }
117 }
118
119 Util.printSection("Indirect Call Sites", Util.SUB_SECTION_CHARACTER);
120 for (CiTargetMethod.Call x : targetMethod.indirectCalls) {
121 TTY.println(x.toString());
122 if (noDis && x.debugInfo != null) {
123 TTY.println(CiUtil.indent(x.debugInfo.toString(), " "));
124 }
125 }
126
127 Util.printSection("Data Patches", Util.SUB_SECTION_CHARACTER);
128 for (CiTargetMethod.DataPatch x : targetMethod.dataReferences) {
129 TTY.println(x.toString());
130 }
131
132 Util.printSection("Marks", Util.SUB_SECTION_CHARACTER);
133 for (CiTargetMethod.Mark x : targetMethod.marks) {
134 TTY.println(x.toString());
135 }
136
137 Util.printSection("Exception Handlers", Util.SUB_SECTION_CHARACTER);
138 for (CiTargetMethod.ExceptionHandler x : targetMethod.exceptionHandlers) {
139 TTY.println(x.toString());
140 }
141 }
142
143 return targetMethod;
144 }
145
146 public void recordExceptionHandlers(int pcOffset, LIRDebugInfo info) {
147 if (info != null) {
148 if (info.exceptionHandlers != null) {
149 if (exceptionInfoList == null) {
150 exceptionInfoList = new ArrayList<ExceptionInfo>(4);
151 }
152 exceptionInfoList.add(new ExceptionInfo(pcOffset, info.exceptionHandlers, info.state.bci));
153 }
154 }
155 }
156
157 public void recordImplicitException(int pcOffset, LIRDebugInfo info) {
158 // record an implicit exception point
159 if (info != null) {
160 assert lastSafepointPos < pcOffset;
161 lastSafepointPos = pcOffset;
162 targetMethod.recordSafepoint(pcOffset, info.debugInfo());
163 recordExceptionHandlers(pcOffset, info);
164 }
165 }
166
167 public void recordDirectCall(int posBefore, int posAfter, Object target, LIRDebugInfo info) {
168 CiDebugInfo debugInfo = info != null ? info.debugInfo() : null;
169 assert lastSafepointPos < posAfter;
170 lastSafepointPos = posAfter;
171 targetMethod.recordCall(posBefore, target, debugInfo, true);
172 }
173
174 public void recordIndirectCall(int posBefore, int posAfter, Object target, LIRDebugInfo info) {
175 CiDebugInfo debugInfo = info != null ? info.debugInfo() : null;
176 assert lastSafepointPos < posAfter;
177 lastSafepointPos = posAfter;
178 targetMethod.recordCall(posBefore, target, debugInfo, false);
179 }
180
181 public void recordSafepoint(int pos, LIRDebugInfo info) {
182 // safepoints always need debug info
183 CiDebugInfo debugInfo = info.debugInfo();
184 assert lastSafepointPos < pos;
185 lastSafepointPos = pos;
186 targetMethod.recordSafepoint(pos, debugInfo);
187 }
188
189 public CiAddress recordDataReferenceInCode(CiConstant data) {
190 assert data != null;
191
192 int pos = asm.codeBuffer.position();
193
194 if (C1XOptions.TraceRelocation) {
195 TTY.print("Data reference in code: pos = %d, data = %s", pos, data.toString());
196 }
197
198 targetMethod.recordDataReference(pos, data);
199 return CiAddress.Placeholder;
200 }
201
202 public int lastSafepointPos() {
203 return lastSafepointPos;
204 }
205 }