comparison c1x4hotspotsrc/HotSpotVM/src/com/sun/hotspot/c1x/CompilerImpl.java @ 2285:762de4b26788

turn Compiler and HotSpotTypeResolved into interfaces
author Lukas Stadler <lukas.stadler@jku.at>
date Fri, 08 Apr 2011 13:43:05 +0200
parents
children 8c426c2891c8
comparison
equal deleted inserted replaced
2284:569d3fe7d65c 2285:762de4b26788
1 /*
2 * Copyright (c) 2011 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
5 * that is described in this document. In particular, and without limitation, these intellectual property
6 * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
7 * more additional patents or pending patent applications in the U.S. and in other countries.
8 *
9 * U.S. Government Rights - Commercial software. Government users are subject to the Sun
10 * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
11 * supplements.
12 *
13 * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
14 * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
15 * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
16 * U.S. and other countries.
17 *
18 * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
19 * Company, Ltd.
20 */
21 package com.sun.hotspot.c1x;
22
23 import java.io.*;
24 import java.lang.management.*;
25 import java.lang.reflect.Proxy;
26 import java.net.*;
27
28 import com.sun.c1x.*;
29 import com.sun.c1x.target.amd64.*;
30 import com.sun.cri.xir.*;
31 import com.sun.hotspot.c1x.logging.*;
32 import com.sun.hotspot.c1x.server.*;
33 import com.sun.hotspot.c1x.server.ReplacingStreams.ReplacingInputStream;
34 import com.sun.hotspot.c1x.server.ReplacingStreams.ReplacingOutputStream;
35
36 /**
37 * Singleton class holding the instance of the C1XCompiler.
38 *
39 * @author Thomas Wuerthinger, Lukas Stadler
40 */
41 public final class CompilerImpl implements Compiler {
42
43 private static Compiler theInstance;
44 private static boolean PrintGCStats = false;
45
46 private final VMEntries vmEntries;
47 private final VMExits vmExits;
48
49 private final C1XCompiler compiler;
50
51 public static Compiler getInstance() {
52 if (theInstance == null) {
53 theInstance = new CompilerImpl(null);
54 Runtime.getRuntime().addShutdownHook(new ShutdownThread());
55 }
56 return theInstance;
57 }
58
59 public static class ShutdownThread extends Thread {
60
61 @Override
62 public void run() {
63 VMExitsNative.compileMethods = false;
64 if (C1XOptions.PrintMetrics) {
65 C1XMetrics.print();
66 }
67 if (C1XOptions.PrintTimers) {
68 C1XTimers.print();
69 }
70 if (PrintGCStats) {
71 printGCStats();
72 }
73 }
74 }
75
76 public static void printGCStats() {
77 long totalGarbageCollections = 0;
78 long garbageCollectionTime = 0;
79
80 for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
81 long count = gc.getCollectionCount();
82 if (count >= 0) {
83 totalGarbageCollections += count;
84 }
85
86 long time = gc.getCollectionTime();
87 if (time >= 0) {
88 garbageCollectionTime += time;
89 }
90 }
91
92 System.out.println("Total Garbage Collections: " + totalGarbageCollections);
93 System.out.println("Total Garbage Collection Time (ms): " + garbageCollectionTime);
94 }
95
96 public static Compiler initializeServer(VMEntries entries) {
97 assert theInstance == null;
98 theInstance = new CompilerImpl(entries);
99 return theInstance;
100 }
101
102 @Override
103 public VMEntries getVMEntries() {
104 return vmEntries;
105 }
106
107 @Override
108 public VMExits getVMExits() {
109 return vmExits;
110 }
111
112 private CompilerImpl(VMEntries entries) {
113 // remote compilation (will not create a C1XCompiler)
114 String remote = System.getProperty("c1x.remote");
115 if (remote != null) {
116 try {
117 System.out.println("C1X compiler started in client/server mode, server: " + remote);
118 Socket socket = new Socket(remote, 1199);
119 ReplacingStreams streams = new ReplacingStreams();
120
121 ReplacingOutputStream output = streams.new ReplacingOutputStream(new BufferedOutputStream(socket.getOutputStream()));
122 // required, because creating an ObjectOutputStream writes a header, but doesn't flush the stream
123 output.flush();
124 ReplacingInputStream input = streams.new ReplacingInputStream(new BufferedInputStream(socket.getInputStream()));
125 input.setCompiler(this);
126
127 InvocationSocket invocation = new InvocationSocket(output, input);
128 vmEntries = new VMEntriesNative();
129 vmExits = (VMExits) Proxy.newProxyInstance(VMExits.class.getClassLoader(), new Class<?>[] { VMExits.class}, invocation);
130 invocation.setDelegate(vmEntries);
131 compiler = null;
132 return;
133 } catch (IOException t) {
134 System.out.println("Connection to compilation server FAILED.");
135 throw new RuntimeException(t);
136 }
137 }
138
139 // initialize VMEntries
140 if (entries == null)
141 entries = new VMEntriesNative();
142
143 // initialize VMExits
144 VMExits exits = new VMExitsNative(this);
145
146 // logging, etc.
147 if (CountingProxy.ENABLED) {
148 exits = CountingProxy.getProxy(VMExits.class, exits);
149 entries = CountingProxy.getProxy(VMEntries.class, entries);
150 }
151 if (Logger.ENABLED) {
152 exits = LoggingProxy.getProxy(VMExits.class, exits);
153 entries = LoggingProxy.getProxy(VMEntries.class, entries);
154 }
155
156 // set the final fields
157 vmEntries = entries;
158 vmExits = exits;
159
160 // initialize compiler and C1XOptions
161 HotSpotVMConfig config = vmEntries.getConfiguration();
162 config.check();
163
164 // these options are important - c1x4hotspot will not generate correct code without them
165 C1XOptions.GenSpecialDivChecks = true;
166 C1XOptions.NullCheckUniquePc = true;
167 C1XOptions.InvokeSnippetAfterArguments = true;
168 C1XOptions.StackShadowPages = config.stackShadowPages;
169
170 HotSpotRuntime runtime = new HotSpotRuntime(config, this);
171 HotSpotRegisterConfig registerConfig = runtime.globalStubRegConfig;
172
173 final int wordSize = 8;
174 final int stackFrameAlignment = 16;
175 HotSpotTarget target = new HotSpotTarget(new AMD64(), true, wordSize, stackFrameAlignment, config.vmPageSize, wordSize, true);
176
177 RiXirGenerator generator = new HotSpotXirGenerator(config, target, registerConfig, this);
178 if (Logger.ENABLED) {
179 generator = LoggingProxy.getProxy(RiXirGenerator.class, generator);
180 }
181 compiler = new C1XCompiler(runtime, target, generator, registerConfig);
182 }
183
184 @Override
185 public C1XCompiler getCompiler() {
186 return compiler;
187 }
188
189 }