comparison graal/Runtime/src/com/sun/hotspot/c1x/HotSpotXirGenerator.java @ 2297:099e697d8934

Renaming c1x4hotspotsrc => graal and HotSpotVM => Runtime
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Fri, 22 Apr 2011 15:08:53 +0200
parents
children 55caa3034872
comparison
equal deleted inserted replaced
2296:34354e2e40a3 2297:099e697d8934
1 /*
2 * Copyright (c) 2010 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 static com.sun.cri.ci.CiCallingConvention.Type.*;
24 import static com.sun.hotspot.c1x.TemplateFlag.*;
25
26 import java.lang.reflect.*;
27 import java.util.*;
28 import java.util.concurrent.*;
29
30 import com.sun.c1x.target.amd64.*;
31 import com.sun.cri.ci.CiAddress.Scale;
32 import com.sun.cri.ci.CiRegister.*;
33 import com.sun.cri.ci.*;
34 import com.sun.cri.ri.*;
35 import com.sun.cri.ri.RiType.Representation;
36 import com.sun.cri.xir.*;
37 import com.sun.cri.xir.CiXirAssembler.XirLabel;
38 import com.sun.cri.xir.CiXirAssembler.XirMark;
39 import com.sun.cri.xir.CiXirAssembler.XirOperand;
40 import com.sun.cri.xir.CiXirAssembler.XirParameter;
41
42 /**
43 *
44 * @author Thomas Wuerthinger, Lukas Stadler
45 */
46 public class HotSpotXirGenerator implements RiXirGenerator {
47
48 // this needs to correspond to c1x_CodeInstaller.hpp
49 // @formatter:off
50 private static final Integer MARK_VERIFIED_ENTRY = 0x0001;
51 private static final Integer MARK_UNVERIFIED_ENTRY = 0x0002;
52 private static final Integer MARK_OSR_ENTRY = 0x0003;
53 private static final Integer MARK_UNWIND_ENTRY = 0x0004;
54 private static final Integer MARK_EXCEPTION_HANDLER_ENTRY = 0x0005;
55 private static final Integer MARK_DEOPT_HANDLER_ENTRY = 0x0006;
56
57 private static final Integer MARK_STATIC_CALL_STUB = 0x1000;
58
59 private static final Integer MARK_INVOKE_INVALID = 0x2000;
60 private static final Integer MARK_INVOKEINTERFACE = 0x2001;
61 private static final Integer MARK_INVOKESTATIC = 0x2002;
62 private static final Integer MARK_INVOKESPECIAL = 0x2003;
63 private static final Integer MARK_INVOKEVIRTUAL = 0x2004;
64
65 private static final Integer MARK_IMPLICIT_NULL = 0x3000;
66
67 private static final Integer MARK_KLASS_PATCHING = 0x4000;
68 private static final Integer MARK_DUMMY_OOP_RELOCATION = 0x4001;
69 private static final Integer MARK_ACCESS_FIELD_PATCHING = 0x4002;
70 // @formatter:on
71
72 private final HotSpotVMConfig config;
73 private final CiTarget target;
74 private final RiRegisterConfig registerConfig;
75 private final Compiler compiler;
76
77 private CiXirAssembler asm;
78
79 public HotSpotXirGenerator(HotSpotVMConfig config, CiTarget target, RiRegisterConfig registerConfig, Compiler compiler) {
80 this.config = config;
81 this.target = target;
82 this.registerConfig = registerConfig;
83 this.compiler = compiler;
84 }
85
86 private SimpleTemplates prologueTemplates = new SimpleTemplates(STATIC_METHOD) {
87
88 @Override
89 protected XirTemplate create(CiXirAssembler asm, long flags) {
90 asm.restart(CiKind.Void);
91 XirOperand framePointer = asm.createRegisterTemp("frame pointer", CiKind.Word, AMD64.rbp);
92 XirOperand stackPointer = asm.createRegisterTemp("stack pointer", CiKind.Word, AMD64.rsp);
93 XirLabel unverifiedStub = null;
94
95 asm.mark(MARK_OSR_ENTRY);
96 asm.mark(MARK_UNVERIFIED_ENTRY);
97 if (!is(STATIC_METHOD, flags)) {
98 unverifiedStub = asm.createOutOfLineLabel("unverified");
99
100 XirOperand temp = asm.createRegisterTemp("temp (r10)", CiKind.Word, AMD64.r10);
101 XirOperand cache = asm.createRegisterTemp("cache (rax)", CiKind.Word, AMD64.rax);
102
103 CiCallingConvention conventions = registerConfig.getCallingConvention(JavaCallee, new CiKind[] {CiKind.Object}, target);
104 XirOperand receiver = asm.createRegisterTemp("receiver", CiKind.Word, conventions.locations[0].asRegister());
105
106 asm.pload(CiKind.Word, temp, receiver, asm.i(config.hubOffset), false);
107 asm.jneq(unverifiedStub, cache, temp);
108 }
109 asm.align(config.codeEntryAlignment);
110 asm.mark(MARK_VERIFIED_ENTRY);
111 asm.stackOverflowCheck();
112 asm.push(framePointer);
113 asm.mov(framePointer, stackPointer);
114 asm.pushFrame();
115
116 // -- out of line -------------------------------------------------------
117 XirOperand thread = asm.createRegisterTemp("thread", CiKind.Word, AMD64.r15);
118 XirOperand exceptionOop = asm.createTemp("exception oop", CiKind.Object);
119 XirLabel unwind = asm.createOutOfLineLabel("unwind");
120 asm.bindOutOfLine(unwind);
121
122 asm.mark(MARK_UNWIND_ENTRY);
123
124 asm.pload(CiKind.Object, exceptionOop, thread, asm.i(config.threadExceptionOopOffset), false);
125 asm.pstore(CiKind.Object, thread, asm.i(config.threadExceptionOopOffset), asm.createConstant(CiConstant.NULL_OBJECT), false);
126 asm.pstore(CiKind.Long, thread, asm.i(config.threadExceptionPcOffset), asm.l(0), false);
127
128 asm.callRuntime(config.unwindExceptionStub, null, exceptionOop);
129 asm.shouldNotReachHere();
130
131 asm.mark(MARK_EXCEPTION_HANDLER_ENTRY);
132 asm.callRuntime(config.handleExceptionStub, null);
133 asm.shouldNotReachHere();
134
135 asm.nop(1);
136 asm.mark(MARK_DEOPT_HANDLER_ENTRY);
137 asm.callRuntime(config.handleDeoptStub, null);
138 asm.shouldNotReachHere();
139
140 if (!is(STATIC_METHOD, flags)) {
141 asm.bindOutOfLine(unverifiedStub);
142 asm.jmpRuntime(config.inlineCacheMissStub);
143 }
144
145 return asm.finishTemplate(is(STATIC_METHOD, flags) ? "static prologue" : "prologue");
146 }
147 };
148
149 private SimpleTemplates epilogueTemplates = new SimpleTemplates(STATIC_METHOD, SYNCHRONIZED) {
150
151 @Override
152 protected XirTemplate create(CiXirAssembler asm, long flags) {
153 asm.restart(CiKind.Void);
154 XirOperand framePointer = asm.createRegisterTemp("frame pointer", CiKind.Word, AMD64.rbp);
155
156 asm.popFrame();
157 asm.pop(framePointer);
158
159 // TODO safepoint check
160
161 return asm.finishTemplate("epilogue");
162 }
163 };
164
165 private SimpleTemplates safepointTemplates = new SimpleTemplates() {
166
167 @Override
168 protected XirTemplate create(CiXirAssembler asm, long flags) {
169 asm.restart(CiKind.Void);
170
171 // XirOperand temp = asm.createRegister("temp", CiKind.Word, AMD64.rax);
172 // asm.pload(CiKind.Word, temp, asm.w(config.safepointPollingAddress), true);
173
174 return asm.finishTemplate("safepoint");
175 }
176 };
177
178 private SimpleTemplates exceptionObjectTemplates = new SimpleTemplates() {
179
180 @Override
181 protected XirTemplate create(CiXirAssembler asm, long flags) {
182 XirOperand result = asm.restart(CiKind.Object);
183 XirOperand thread = asm.createRegisterTemp("thread", CiKind.Word, AMD64.r15);
184
185 asm.pload(CiKind.Object, result, thread, asm.i(config.threadExceptionOopOffset), false);
186 asm.pstore(CiKind.Object, thread, asm.i(config.threadExceptionOopOffset), asm.o(null), false);
187 asm.pstore(CiKind.Long, thread, asm.i(config.threadExceptionPcOffset), asm.l(0), false);
188
189 return asm.finishTemplate("exception object");
190 }
191 };
192
193 private SimpleTemplates resolveClassTemplates = new SimpleTemplates(UNRESOLVED) {
194
195 @Override
196 protected XirTemplate create(CiXirAssembler asm, long flags) {
197 XirOperand result = asm.restart(CiKind.Word);
198 if (is(UNRESOLVED, flags)) {
199 UnresolvedClassPatching patching = new UnresolvedClassPatching(asm, result, config);
200 patching.emitInline();
201 // -- out of line -------------------------------------------------------
202 patching.emitOutOfLine();
203 } else {
204 XirOperand type = asm.createConstantInputParameter("type", CiKind.Object);
205 asm.mov(result, type);
206 }
207 return asm.finishTemplate(is(UNRESOLVED, flags) ? "resolve class (unresolved)" : "resolve class");
208 }
209 };
210
211 private SimpleTemplates invokeInterfaceTemplates = new SimpleTemplates(NULL_CHECK) {
212
213 @Override
214 protected XirTemplate create(CiXirAssembler asm, long flags) {
215 asm.restart();
216 XirParameter receiver = asm.createInputParameter("receiver", CiKind.Object);
217 XirParameter addr = asm.createConstantInputParameter("addr", CiKind.Word);
218 XirOperand temp = asm.createRegisterTemp("temp", CiKind.Word, AMD64.rax);
219
220 if (is(NULL_CHECK, flags)) {
221 asm.nop(1);
222 asm.mark(MARK_IMPLICIT_NULL);
223 asm.pload(CiKind.Word, temp, receiver, true);
224 }
225 asm.mark(MARK_INVOKEINTERFACE);
226 asm.mov(temp, asm.createConstant(CiConstant.forObject(HotSpotProxy.DUMMY_CONSTANT_OBJ)));
227
228 return asm.finishTemplate(addr, "invokeinterface");
229 }
230 };
231
232 private SimpleTemplates invokeVirtualTemplates = new SimpleTemplates(NULL_CHECK) {
233
234 @Override
235 protected XirTemplate create(CiXirAssembler asm, long flags) {
236 asm.restart();
237 XirParameter receiver = asm.createInputParameter("receiver", CiKind.Object);
238 XirParameter addr = asm.createConstantInputParameter("addr", CiKind.Word);
239 XirOperand temp = asm.createRegisterTemp("temp", CiKind.Word, AMD64.rax);
240
241 if (is(NULL_CHECK, flags)) {
242 asm.nop(1);
243 asm.mark(MARK_IMPLICIT_NULL);
244 asm.pload(CiKind.Word, temp, receiver, true);
245 }
246 asm.mark(MARK_INVOKEVIRTUAL);
247 asm.mov(temp, asm.createConstant(CiConstant.forObject(HotSpotProxy.DUMMY_CONSTANT_OBJ)));
248
249 return asm.finishTemplate(addr, "invokevirtual");
250 }
251 };
252
253 private SimpleTemplates invokeSpecialTemplates = new SimpleTemplates(NULL_CHECK) {
254
255 @Override
256 protected XirTemplate create(CiXirAssembler asm, long flags) {
257 asm.restart();
258 XirParameter receiver = asm.createInputParameter("receiver", CiKind.Object);
259 XirParameter addr = asm.createConstantInputParameter("addr", CiKind.Word);
260 XirOperand temp = asm.createRegisterTemp("temp", CiKind.Word, AMD64.rax);
261 XirLabel stub = asm.createOutOfLineLabel("call stub");
262
263 if (is(NULL_CHECK, flags)) {
264 asm.nop(1);
265 asm.mark(MARK_IMPLICIT_NULL);
266 asm.pload(CiKind.Word, temp, receiver, true);
267 }
268 asm.mark(MARK_INVOKESPECIAL);
269
270 // -- out of line -------------------------------------------------------
271 asm.bindOutOfLine(stub);
272 XirOperand method = asm.createRegisterTemp("method", CiKind.Word, AMD64.rbx);
273 asm.mark(MARK_STATIC_CALL_STUB, XirMark.CALLSITE);
274 asm.mov(method, asm.w(0L));
275 XirLabel dummy = asm.createOutOfLineLabel("dummy");
276 asm.jmp(dummy);
277 asm.bindOutOfLine(dummy);
278
279 return asm.finishTemplate(addr, "invokespecial");
280 }
281 };
282
283 private SimpleTemplates invokeStaticTemplates = new SimpleTemplates() {
284
285 @Override
286 protected XirTemplate create(CiXirAssembler asm, long flags) {
287 asm.restart();
288 XirParameter addr = asm.createConstantInputParameter("addr", CiKind.Word);
289
290 XirLabel stub = asm.createOutOfLineLabel("call stub");
291 asm.mark(MARK_INVOKESTATIC);
292
293 // -- out of line -------------------------------------------------------
294 asm.bindOutOfLine(stub);
295 XirOperand method = asm.createRegisterTemp("method", CiKind.Word, AMD64.rbx);
296 asm.mark(MARK_STATIC_CALL_STUB, XirMark.CALLSITE);
297 asm.mov(method, asm.w(0L));
298 XirLabel dummy = asm.createOutOfLineLabel("dummy");
299 asm.jmp(dummy);
300 asm.bindOutOfLine(dummy);
301
302 return asm.finishTemplate(addr, "invokestatic");
303 }
304 };
305
306 private SimpleTemplates monitorEnterTemplates = new SimpleTemplates(NULL_CHECK) {
307
308 @Override
309 protected XirTemplate create(CiXirAssembler asm, long flags) {
310 asm.restart(CiKind.Void);
311 XirParameter object = asm.createInputParameter("object", CiKind.Object);
312 XirParameter lock = asm.createInputParameter("lock", CiKind.Word);
313
314 if (is(NULL_CHECK, flags)) {
315 asm.nop(1);
316 asm.mark(MARK_IMPLICIT_NULL);
317 asm.pload(CiKind.Word, asm.createTemp("temp", CiKind.Word), object, true);
318 }
319
320
321 // (tw) It is important to use for this runtime call the debug info AFTER the monitor enter. Otherwise the monitor object
322 // is not correctly garbage collected.
323 final boolean useInfoAfter = true;
324
325 if (config.useFastLocking) {
326 useRegisters(asm, AMD64.rax, AMD64.rbx);
327 useRegisters(asm, getGeneralParameterRegister(0));
328 useRegisters(asm, getGeneralParameterRegister(1));
329 asm.callRuntime(config.fastMonitorEnterStub, null, useInfoAfter, object, lock);
330 } else {
331 asm.reserveOutgoingStack(target.wordSize * 2);
332 asm.pstore(CiKind.Object, asm.createRegister("rsp", CiKind.Word, AMD64.RSP.asRegister()), asm.i(target.wordSize), object, false);
333 asm.pstore(CiKind.Word, asm.createRegister("rsp", CiKind.Word, AMD64.RSP.asRegister()), asm.i(0), lock, false);
334 asm.callRuntime(config.monitorEnterStub, null, useInfoAfter);
335 }
336
337 return asm.finishTemplate("monitorEnter");
338 }
339 };
340
341 private CiRegister getGeneralParameterRegister(int index) {
342 return registerConfig.getCallingConventionRegisters(CiCallingConvention.Type.RuntimeCall, RegisterFlag.CPU)[index];
343 }
344
345 private SimpleTemplates monitorExitTemplates = new SimpleTemplates(NULL_CHECK) {
346
347 @Override
348 protected XirTemplate create(CiXirAssembler asm, long flags) {
349 asm.restart(CiKind.Void);
350 XirParameter object = asm.createInputParameter("object", CiKind.Object);
351 XirParameter lock = asm.createInputParameter("lock", CiKind.Word);
352
353 if (config.useFastLocking) {
354 useRegisters(asm, AMD64.rax, AMD64.rbx);
355 useRegisters(asm, getGeneralParameterRegister(0));
356 useRegisters(asm, getGeneralParameterRegister(1));
357 asm.callRuntime(config.fastMonitorExitStub, null, object, lock);
358 } else {
359 asm.reserveOutgoingStack(target.wordSize);
360 asm.pstore(CiKind.Word, asm.createRegister("rsp", CiKind.Word, AMD64.RSP.asRegister()), asm.i(0), lock, false);
361 asm.callRuntime(config.monitorExitStub, null);
362 }
363
364 return asm.finishTemplate("monitorExit");
365 }
366 };
367
368 private KindTemplates getFieldTemplates = new KindTemplates(NULL_CHECK, UNRESOLVED) {
369
370 @Override
371 protected XirTemplate create(CiXirAssembler asm, long flags, CiKind kind) {
372 XirOperand result = asm.restart(kind);
373 XirParameter object = asm.createInputParameter("object", CiKind.Object);
374
375 if (is(UNRESOLVED, flags)) {
376 UnresolvedFieldPatching fieldPatching = new UnresolvedFieldPatching(asm, object, result, false, is(NULL_CHECK, flags), config);
377 fieldPatching.emitInline();
378 // -- out of line -------------------------------------------------------
379 fieldPatching.emitOutOfLine();
380 return asm.finishTemplate("getfield<" + kind + ">");
381 }
382 XirParameter fieldOffset = asm.createConstantInputParameter("fieldOffset", CiKind.Int);
383 if (is(NULL_CHECK, flags)) {
384 asm.nop(1);
385 asm.mark(MARK_IMPLICIT_NULL);
386 }
387 asm.pload(kind, result, object, fieldOffset, is(NULL_CHECK, flags));
388 return asm.finishTemplate("getfield<" + kind + ">");
389 }
390 };
391
392 private KindTemplates writeBarrierTemplate = new KindTemplates() {
393
394 @Override
395 protected XirTemplate create(CiXirAssembler asm, long flags, CiKind kind) {
396 asm.restart(CiKind.Void);
397 XirParameter object = asm.createInputParameter("object", CiKind.Object);
398
399 // Need temp operand, because the write barrier destroys the object pointer.
400 XirOperand temp = asm.createTemp("temp", CiKind.Object);
401 asm.mov(temp, object);
402
403 writeBarrier(asm, temp);
404 return asm.finishTemplate("writeBarrier");
405 }
406 };
407
408 private KindTemplates putFieldTemplates = new KindTemplates(WRITE_BARRIER, NULL_CHECK, UNRESOLVED) {
409
410 @Override
411 protected XirTemplate create(CiXirAssembler asm, long flags, CiKind kind) {
412 asm.restart(CiKind.Void);
413 XirParameter object = asm.createInputParameter("object", CiKind.Object);
414 XirParameter value = asm.createInputParameter("value", kind);
415
416 if (is(UNRESOLVED, flags)) {
417 UnresolvedFieldPatching fieldPatching = new UnresolvedFieldPatching(asm, object, value, true, is(NULL_CHECK, flags), config);
418 fieldPatching.emitInline();
419 // -- out of line -------------------------------------------------------
420 fieldPatching.emitOutOfLine();
421 return asm.finishTemplate("putfield<" + kind + ">");
422 }
423 XirParameter fieldOffset = asm.createConstantInputParameter("fieldOffset", CiKind.Int);
424 if (kind == CiKind.Object) {
425 verifyPointer(asm, value);
426 }
427 if (is(NULL_CHECK, flags)) {
428 asm.nop(1);
429 asm.mark(MARK_IMPLICIT_NULL);
430 }
431 asm.pstore(kind, object, fieldOffset, value, is(NULL_CHECK, flags));
432 if (is(WRITE_BARRIER, flags)) {
433 XirOperand temp = asm.createTemp("temp", CiKind.Word);
434 asm.mov(temp, object);
435 writeBarrier(asm, temp);
436 }
437 return asm.finishTemplate("putfield<" + kind + ">");
438 }
439 };
440
441 private final IndexTemplates newInstanceTemplates = new IndexTemplates() {
442
443 @Override
444 protected XirTemplate create(CiXirAssembler asm, long flags, int size) {
445 XirOperand result = asm.restart(CiKind.Word);
446 XirOperand type = asm.createInputParameter("type", CiKind.Object);
447
448 XirOperand temp1 = asm.createRegisterTemp("temp1", CiKind.Word, AMD64.rcx);
449 XirOperand temp2 = asm.createRegisterTemp("temp2", CiKind.Word, AMD64.rbx);
450 XirOperand temp2i = asm.createRegisterTemp("temp2i", CiKind.Int, AMD64.rbx);
451 useRegisters(asm, AMD64.rsi);
452 XirLabel tlabFull = asm.createOutOfLineLabel("tlab full");
453 XirLabel resume = asm.createInlineLabel("resume");
454
455 // check if the class is already initialized
456 asm.pload(CiKind.Int, temp2i, type, asm.i(config.klassStateOffset), false);
457 asm.jneq(tlabFull, temp2i, asm.i(config.klassStateFullyInitialized));
458
459 XirOperand thread = asm.createRegisterTemp("thread", CiKind.Word, AMD64.r15);
460 asm.pload(CiKind.Word, result, thread, asm.i(config.threadTlabTopOffset), false);
461 asm.add(temp1, result, asm.w(size));
462 asm.pload(CiKind.Word, temp2, thread, asm.i(config.threadTlabEndOffset), false);
463
464 asm.jgt(tlabFull, temp1, temp2);
465 asm.pstore(CiKind.Word, thread, asm.i(config.threadTlabTopOffset), temp1, false);
466
467 asm.bindInline(resume);
468
469 asm.pload(CiKind.Word, temp1, type, asm.i(config.instanceHeaderPrototypeOffset), false);
470 asm.pstore(CiKind.Word, result, temp1, false);
471 asm.pstore(CiKind.Object, result, asm.i(config.hubOffset), type, false);
472
473 if (size > 2 * target.wordSize) {
474 asm.mov(temp1, asm.w(0));
475 for (int offset = 2 * target.wordSize; offset < size; offset += target.wordSize) {
476 asm.pstore(CiKind.Word, result, asm.i(offset), temp1, false);
477 }
478 }
479
480 // -- out of line -------------------------------------------------------
481 asm.bindOutOfLine(tlabFull);
482 XirOperand arg = asm.createRegisterTemp("runtime call argument", CiKind.Object, AMD64.rdx);
483 asm.mov(arg, type);
484 useRegisters(asm, AMD64.rax);
485 asm.callRuntime(config.newInstanceStub, result);
486 asm.jmp(resume);
487
488 return asm.finishTemplate("new instance");
489 }
490 };
491
492 private SimpleTemplates newInstanceUnresolvedTemplates = new SimpleTemplates() {
493
494 @Override
495 protected XirTemplate create(CiXirAssembler asm, long flags) {
496 XirOperand result = asm.restart(CiKind.Word);
497 XirOperand arg = asm.createRegisterTemp("runtime call argument", CiKind.Object, AMD64.rdx);
498
499 UnresolvedClassPatching patching = new UnresolvedClassPatching(asm, arg, config);
500
501 patching.emitInline();
502 useRegisters(asm, AMD64.rbx, AMD64.rcx, AMD64.rsi, AMD64.rax);
503 asm.callRuntime(config.unresolvedNewInstanceStub, result);
504
505 // -- out of line -------------------------------------------------------
506 patching.emitOutOfLine();
507
508 return asm.finishTemplate("new instance");
509 }
510 };
511
512 private SimpleTemplates newObjectArrayCloneTemplates = new SimpleTemplates() {
513
514 @Override
515 protected XirTemplate create(CiXirAssembler asm, long flags) {
516 XirOperand result = asm.restart(CiKind.Object);
517 XirParameter lengthParam = asm.createInputParameter("length", CiKind.Int, true);
518 XirParameter src = asm.createInputParameter("src", CiKind.Object);
519
520 // Set up length and hub.
521 XirOperand length = asm.createRegisterTemp("length", CiKind.Int, AMD64.rbx);
522 XirOperand hub = asm.createRegisterTemp("hub", CiKind.Object, AMD64.rdx);
523 asm.pload(CiKind.Object, hub, src, asm.i(config.hubOffset), false);
524 asm.mov(length, lengthParam);
525
526 useRegisters(asm, AMD64.rsi, AMD64.rcx, AMD64.rdi, AMD64.rax);
527 asm.callRuntime(config.newObjectArrayStub, result);
528 return asm.finishTemplate("objectArrayClone");
529 }
530 };
531
532 private SimpleTemplates newObjectArrayTemplates = new SimpleTemplates(UNRESOLVED) {
533
534 @Override
535 protected XirTemplate create(CiXirAssembler asm, long flags) {
536 emitNewTypeArray(asm, flags, CiKind.Object, config.useFastNewObjectArray, config.newObjectArrayStub);
537 return asm.finishTemplate(is(UNRESOLVED, flags) ? "newObjectArray (unresolved)" : "newObjectArray");
538 }
539 };
540
541 private void emitNewTypeArray(CiXirAssembler asm, long flags, CiKind kind, boolean useFast, long slowPathStub) {
542 XirOperand result = asm.restart(CiKind.Word);
543
544 XirParameter lengthParam = asm.createInputParameter("length", CiKind.Int, true);
545
546 XirOperand length = asm.createRegisterTemp("length", CiKind.Int, AMD64.rbx);
547 XirOperand hub = asm.createRegisterTemp("hub", CiKind.Object, AMD64.rdx);
548
549 // Registers rsi, rcx, rdi, and rax are needed by the runtime call.
550 // Hub needs to be on rdx, length on rbx.
551 XirOperand temp1 = asm.createRegisterTemp("temp1", CiKind.Word, AMD64.rcx);
552 XirOperand temp2 = asm.createRegisterTemp("temp2", CiKind.Word, AMD64.rax);
553 XirOperand temp3 = asm.createRegisterTemp("temp3", CiKind.Word, AMD64.rdi);
554 XirOperand size = asm.createRegisterTemp("size", CiKind.Int, AMD64.rsi);
555
556 UnresolvedClassPatching patching = null;
557 if (is(UNRESOLVED, flags)) {
558 // insert the patching code for class resolving - the hub will end up in "hub"
559 patching = new UnresolvedClassPatching(asm, hub, config);
560 patching.emitInline();
561 } else {
562 asm.mov(hub, asm.createConstantInputParameter("hub", CiKind.Object));
563 }
564
565 asm.mov(length, lengthParam);
566
567 if (useFast) {
568
569 XirLabel slowPath = asm.createOutOfLineLabel("slowPath");
570
571 XirLabel done = asm.createInlineLabel("done");
572
573 // Check for negative array size.
574 // TODO: Also check for upper bound
575 asm.jlt(slowPath, length, asm.i(0));
576
577 final int aligning = target.wordSize;
578 final int arrayLengthOffset = target.wordSize * 2;
579 final int arrayElementOffset = config.getArrayOffset(kind);
580
581 // Calculate aligned size
582 asm.mov(size, length);
583 int scale = CiUtil.log2(kind.sizeInBytes(target.wordSize));
584 if (scale != 0) {
585 asm.shl(size, size, asm.i(scale));
586 }
587 asm.add(size, size, asm.i(arrayElementOffset + aligning - 1));
588 long mask = 0xFFFFFFFFL;
589 mask <<= CiUtil.log2(aligning);
590 asm.and(size, size, asm.i((int) mask));
591
592 // Try tlab allocation
593 XirOperand thread = asm.createRegisterTemp("thread", CiKind.Word, AMD64.r15);
594 asm.pload(CiKind.Word, result, thread, asm.i(config.threadTlabTopOffset), false);
595 asm.add(temp1, result, size);
596 asm.pload(CiKind.Word, temp2, thread, asm.i(config.threadTlabEndOffset), false);
597 asm.jgt(slowPath, temp1, temp2);
598 asm.pstore(CiKind.Word, thread, asm.i(config.threadTlabTopOffset), temp1, false);
599
600 // Now the new object is in result, store mark word and klass
601 asm.pload(CiKind.Word, temp1, hub, asm.i(config.instanceHeaderPrototypeOffset), false);
602 asm.pstore(CiKind.Word, result, temp1, false);
603 asm.pstore(CiKind.Object, result, asm.i(config.hubOffset), hub, false);
604
605 // Store array length
606 asm.pstore(CiKind.Int, result, asm.i(arrayLengthOffset), length, false);
607
608 // Initialize with 0
609 XirLabel top = asm.createInlineLabel("top");
610 asm.sub(size, size, asm.i(arrayElementOffset));
611 asm.shr(size, size, asm.i(Scale.Times8.log2));
612 asm.jeq(done, size, asm.i(0));
613 asm.xor(temp3, temp3, temp3);
614 asm.bindInline(top);
615 asm.pstore(CiKind.Word, result, size, temp3, arrayElementOffset - target.wordSize, Scale.Times8, false);
616 asm.decAndJumpNotZero(top, size);
617
618 asm.bindInline(done);
619
620 // Slow path
621 asm.bindOutOfLine(slowPath);
622 asm.callRuntime(slowPathStub, result);
623 asm.jmp(done);
624 } else {
625 asm.callRuntime(slowPathStub, result);
626 }
627
628 if (patching != null) {
629 patching.emitOutOfLine();
630 }
631 }
632
633 private KindTemplates newTypeArrayTemplates = new KindTemplates() {
634 @Override
635 protected XirTemplate create(CiXirAssembler asm, long flags, CiKind kind) {
636 emitNewTypeArray(asm, flags, kind, config.useFastNewTypeArray, config.newTypeArrayStub);
637 return asm.finishTemplate("newTypeArray<" + kind.toString() + ">");
638 }
639 };
640
641 private final IndexTemplates multiNewArrayTemplate = new IndexTemplates(UNRESOLVED) {
642
643 @Override
644 protected XirTemplate create(CiXirAssembler asm, long flags, int dimensions) {
645 XirOperand result = asm.restart(CiKind.Object);
646
647 XirOperand hub = asm.createRegisterTemp("hub", CiKind.Object, AMD64.rax);
648 XirOperand rank = asm.createRegisterTemp("rank", CiKind.Int, AMD64.rbx);
649 XirOperand sizes = asm.createRegisterTemp("sizes", CiKind.Long, AMD64.rcx);
650 XirOperand thread = asm.createRegisterTemp("thread", CiKind.Long, AMD64.r15);
651 asm.add(sizes, thread, asm.l(config.threadMultiNewArrayStorage));
652 for (int i = 0; i < dimensions; i++) {
653 XirParameter length = asm.createInputParameter("length" + i, CiKind.Int, true);
654 asm.pstore(CiKind.Int, sizes, asm.i(i * target.sizeInBytes(CiKind.Int)), length, false);
655 }
656
657 UnresolvedClassPatching patching = null;
658 if (is(UNRESOLVED, flags)) {
659 // insert the patching code for class resolving - the hub will end up in "hub"
660 patching = new UnresolvedClassPatching(asm, hub, config);
661 patching.emitInline();
662 } else {
663 asm.mov(hub, asm.createConstantInputParameter("hub", CiKind.Object));
664 }
665
666 asm.mov(rank, asm.i(dimensions));
667 useRegisters(asm, AMD64.rax);
668 asm.callRuntime(config.newMultiArrayStub, result);
669 if (is(UNRESOLVED, flags)) {
670 patching.emitOutOfLine();
671 }
672 return asm.finishTemplate(is(UNRESOLVED, flags) ? "multiNewArray" + dimensions + " (unresolved)" : "multiNewArray" + dimensions);
673 }
674 };
675
676 private SimpleTemplates checkCastTemplates = new SimpleTemplates(NULL_CHECK, UNRESOLVED) {
677
678 @Override
679 protected XirTemplate create(CiXirAssembler asm, long flags) {
680 asm.restart();
681 XirParameter object = asm.createInputParameter("object", CiKind.Object);
682 final XirOperand hub;
683 final UnresolvedClassPatching patching;
684 if (is(UNRESOLVED, flags)) {
685 hub = asm.createTemp("hub", CiKind.Object);
686 // insert the patching code for class resolving - the hub will end up in "hub"
687 patching = new UnresolvedClassPatching(asm, hub, config);
688 patching.emitInline();
689 } else {
690 hub = asm.createConstantInputParameter("hub", CiKind.Object);
691 patching = null;
692 }
693
694 XirOperand objHub = asm.createTemp("objHub", CiKind.Object);
695
696 XirLabel end = asm.createInlineLabel("end");
697 XirLabel slowPath = asm.createOutOfLineLabel("slow path");
698
699 if (is(NULL_CHECK, flags)) {
700 // null can be cast to anything
701 asm.jeq(end, object, asm.o(null));
702 }
703
704 asm.pload(CiKind.Object, objHub, object, asm.i(config.hubOffset), false);
705 // if we get an exact match: succeed immediately
706 asm.jneq(slowPath, objHub, hub);
707 asm.bindInline(end);
708
709 // -- out of line -------------------------------------------------------
710 asm.bindOutOfLine(slowPath);
711 checkSubtype(asm, objHub, objHub, hub);
712 asm.jneq(end, objHub, asm.o(null));
713 XirOperand scratch = asm.createRegisterTemp("scratch", CiKind.Object, AMD64.r10);
714 asm.mov(scratch, object);
715 asm.callRuntime(config.throwClassCastException, null);
716 asm.shouldNotReachHere();
717
718 if (is(UNRESOLVED, flags)) {
719 patching.emitOutOfLine();
720 }
721
722 return asm.finishTemplate(object, "instanceof");
723 }
724 };
725
726 private SimpleTemplates instanceOfTemplates = new SimpleTemplates(NULL_CHECK, UNRESOLVED) {
727
728 @Override
729 protected XirTemplate create(CiXirAssembler asm, long flags) {
730 XirOperand result = asm.restart(CiKind.Boolean);
731 XirParameter object = asm.createInputParameter("object", CiKind.Object);
732 final XirOperand hub;
733 final UnresolvedClassPatching patching;
734 if (is(UNRESOLVED, flags)) {
735 hub = asm.createTemp("hub", CiKind.Object);
736 // insert the patching code for class resolving - the hub will end up in "hub"
737 patching = new UnresolvedClassPatching(asm, hub, config);
738 patching.emitInline();
739 } else {
740 hub = asm.createConstantInputParameter("hub", CiKind.Object);
741 patching = null;
742 }
743
744 XirOperand objHub = asm.createTemp("objHub", CiKind.Object);
745
746 XirLabel end = asm.createInlineLabel("end");
747 XirLabel slowPath = asm.createOutOfLineLabel("slow path");
748
749 if (is(NULL_CHECK, flags)) {
750 // null isn't "instanceof" anything
751 asm.mov(result, asm.b(false));
752 asm.jeq(end, object, asm.o(null));
753 }
754
755 asm.pload(CiKind.Object, objHub, object, asm.i(config.hubOffset), false);
756 // if we get an exact match: succeed immediately
757 asm.mov(result, asm.b(true));
758 asm.jneq(slowPath, objHub, hub);
759 asm.bindInline(end);
760
761 // -- out of line -------------------------------------------------------
762 asm.bindOutOfLine(slowPath);
763 checkSubtype(asm, result, objHub, hub);
764 asm.jmp(end);
765
766 if (is(UNRESOLVED, flags)) {
767 patching.emitOutOfLine();
768 }
769
770 return asm.finishTemplate("instanceof");
771 }
772 };
773
774 private XirOperand genArrayLength(XirOperand array, boolean implicitNullException) {
775 XirOperand length = asm.createTemp("length", CiKind.Int);
776 genArrayLength(asm, length, array, implicitNullException);
777 return length;
778 }
779
780 private void genArrayLength(CiXirAssembler asm, XirOperand length, XirOperand array, boolean implicitNullException) {
781 if (implicitNullException) {
782 // asm.nop(1);
783 asm.mark(MARK_IMPLICIT_NULL);
784 }
785 asm.pload(CiKind.Int, length, array, asm.i(config.arrayLengthOffset), implicitNullException);
786 }
787
788 private KindTemplates arrayLoadTemplates = new KindTemplates(NULL_CHECK, READ_BARRIER, BOUNDS_CHECK, GIVEN_LENGTH) {
789
790 @Override
791 protected XirTemplate create(CiXirAssembler asm, long flags, CiKind kind) {
792 XirOperand result = asm.restart(kind);
793 XirParameter array = asm.createInputParameter("array", CiKind.Object);
794 XirParameter index = asm.createInputParameter("index", CiKind.Int, true);
795 XirLabel failBoundsCheck = null;
796 // if the length is known the array cannot be null
797 boolean implicitNullException = is(NULL_CHECK, flags);
798
799 if (is(BOUNDS_CHECK, flags)) {
800 // load the array length and check the index
801 failBoundsCheck = asm.createOutOfLineLabel("failBoundsCheck");
802 XirOperand length;
803 if (is(GIVEN_LENGTH, flags)) {
804 length = asm.createInputParameter("length", CiKind.Int, true);
805 } else {
806 length = genArrayLength(array, implicitNullException);
807 }
808 asm.jugteq(failBoundsCheck, index, length);
809 implicitNullException = false;
810 }
811 int elemSize = target.sizeInBytes(kind);
812 if (implicitNullException) {
813 asm.nop(1);
814 asm.mark(MARK_IMPLICIT_NULL);
815 }
816 asm.pload(kind, result, array, index, config.getArrayOffset(kind), Scale.fromInt(elemSize), implicitNullException);
817 if (is(BOUNDS_CHECK, flags)) {
818 asm.bindOutOfLine(failBoundsCheck);
819 asm.callRuntime(config.throwArrayIndexException, null);
820 asm.shouldNotReachHere();
821 }
822 return asm.finishTemplate("arrayload<" + kind + ">");
823 }
824 };
825
826 private SimpleTemplates getClassTemplates = new SimpleTemplates() {
827 @Override
828 protected XirTemplate create(CiXirAssembler asm, long flags) {
829 XirOperand result = asm.restart(CiKind.Object);
830 XirOperand object = asm.createInputParameter("object", CiKind.Object);
831 if (is(NULL_CHECK, flags)) {
832 asm.nop(1);
833 }
834 asm.pload(CiKind.Object, result, object, asm.i(config.hubOffset), is(NULL_CHECK, flags));
835 asm.pload(CiKind.Object, result, result, asm.i(config.classMirrorOffset), false);
836 return asm.finishTemplate("currentThread");
837 }
838 };
839
840 private SimpleTemplates currentThreadTemplates = new SimpleTemplates() {
841 @Override
842 protected XirTemplate create(CiXirAssembler asm, long flags) {
843 XirOperand result = asm.restart(CiKind.Object);
844 XirOperand thread = asm.createRegisterTemp("thread", CiKind.Word, AMD64.r15);
845 asm.pload(CiKind.Object, result, thread, asm.i(config.threadObjectOffset), false);
846 return asm.finishTemplate("currentThread");
847 }
848 };
849
850 @Override
851 public XirSnippet genCurrentThread(XirSite site) {
852 return new XirSnippet(currentThreadTemplates.get(site));
853 }
854
855 @Override
856 public XirSnippet genGetClass(XirSite site, XirArgument object) {
857 return new XirSnippet(getClassTemplates.get(site), object);
858 }
859
860 private KindTemplates arrayCopyTemplates = new KindTemplates() {
861
862 @Override
863 protected XirTemplate create(CiXirAssembler asm, long flags, CiKind kind) {
864 asm.restart(CiKind.Void);
865 XirParameter src = asm.createInputParameter("src", CiKind.Object);
866 XirParameter srcPos = asm.createInputParameter("srcPos", CiKind.Int, true);
867 XirParameter dest = asm.createInputParameter("dest", CiKind.Object);
868 XirParameter destPos = asm.createInputParameter("destPos", CiKind.Int, true);
869 XirParameter length = asm.createInputParameter("length", CiKind.Int, true);
870
871 XirOperand tempSrc = asm.createTemp("tempSrc", CiKind.Word);
872 XirOperand tempDest = asm.createTemp("tempDest", CiKind.Word);
873 XirOperand lengthOperand = asm.createRegisterTemp("lengthOperand", CiKind.Int, AMD64.rax);
874
875 XirOperand compHub = null;
876 XirOperand valueHub = null;
877 XirOperand temp = null;
878 XirLabel store = null;
879 XirLabel slowStoreCheck = null;
880
881 if (is(STORE_CHECK, flags)) {
882 valueHub = asm.createRegisterTemp("valueHub", CiKind.Word, AMD64.rdi);
883 compHub = asm.createRegisterTemp("compHub", CiKind.Word, AMD64.rsi);
884 temp = asm.createRegisterTemp("temp", CiKind.Word, AMD64.r10);
885 }
886
887 // Calculate the factor for the repeat move instruction.
888 int elementSize = kind.sizeInBytes(target.wordSize);
889 int factor;
890 boolean wordSize;
891 if (elementSize >= target.wordSize) {
892 assert elementSize % target.wordSize == 0;
893 wordSize = true;
894 factor = elementSize / target.wordSize;
895 } else {
896 factor = elementSize;
897 wordSize = false;
898 }
899
900 // Adjust the length if the factor is not 1.
901 if (factor != 1) {
902 asm.shl(lengthOperand, length, asm.i(CiUtil.log2(factor)));
903 } else {
904 asm.mov(lengthOperand, length);
905 }
906
907 // Set the start and the end pointer.
908 asm.lea(tempSrc, src, srcPos, config.getArrayOffset(kind), Scale.fromInt(elementSize));
909 asm.lea(tempDest, dest, destPos, config.getArrayOffset(kind), Scale.fromInt(elementSize));
910
911 XirLabel reverse = null;
912 XirLabel normal = null;
913
914 if (is(STORE_CHECK, flags)) {
915 reverse = asm.createInlineLabel("reverse");
916 asm.jneq(reverse, src, dest);
917 }
918
919 if (!is(STORE_CHECK, flags) && !is(INPUTS_DIFFERENT, flags) && !is(INPUTS_SAME, flags)) {
920 normal = asm.createInlineLabel("normal");
921 asm.jneq(normal, src, dest);
922 }
923
924 if (!is(INPUTS_DIFFERENT, flags)) {
925 if (reverse == null) {
926 reverse = asm.createInlineLabel("reverse");
927 }
928 asm.jlt(reverse, srcPos, destPos);
929 }
930
931 if (!is(STORE_CHECK, flags) && !is(INPUTS_DIFFERENT, flags) && !is(INPUTS_SAME, flags)) {
932 asm.bindInline(normal);
933 }
934
935 // Everything set up => repeat mov.
936 if (wordSize) {
937 asm.repmov(tempSrc, tempDest, lengthOperand);
938 } else {
939 asm.repmovb(tempSrc, tempDest, lengthOperand);
940 }
941
942 if (!is(INPUTS_DIFFERENT, flags) || is(STORE_CHECK, flags)) {
943
944 XirLabel end = asm.createInlineLabel("end");
945 asm.jmp(end);
946
947 // Implement reverse copy, because srcPos < destPos and src == dest.
948 asm.bindInline(reverse);
949
950 if (is(STORE_CHECK, flags)) {
951 asm.pload(CiKind.Object, compHub, dest, asm.i(config.hubOffset), false);
952 asm.pload(CiKind.Object, compHub, compHub, asm.i(config.arrayClassElementOffset), false);
953 }
954
955 CiKind copyKind = wordSize ? CiKind.Object : CiKind.Byte;
956 XirOperand tempValue = asm.createTemp("tempValue", copyKind);
957 XirLabel start = asm.createInlineLabel("start");
958 asm.bindInline(start);
959 asm.sub(lengthOperand, lengthOperand, asm.i(1));
960 asm.jlt(end, lengthOperand, asm.i(0));
961
962 Scale scale = wordSize ? Scale.fromInt(target.wordSize) : Scale.Times1;
963 asm.pload(copyKind, tempValue, tempSrc, lengthOperand, 0, scale, false);
964
965 if (is(STORE_CHECK, flags)) {
966 slowStoreCheck = asm.createOutOfLineLabel("slowStoreCheck");
967 store = asm.createInlineLabel("store");
968 asm.jeq(store, tempValue, asm.o(null)); // first check if value is null
969 asm.pload(CiKind.Object, valueHub, tempValue, asm.i(config.hubOffset), false);
970 asm.jneq(slowStoreCheck, compHub, valueHub); // then check component hub matches value hub
971 asm.bindInline(store);
972 }
973
974 asm.pstore(copyKind, tempDest, lengthOperand, tempValue, 0, scale, false);
975
976 asm.jmp(start);
977 asm.bindInline(end);
978 }
979
980 if (kind == CiKind.Object) {
981 // Do write barriers
982 asm.lea(tempDest, dest, destPos, config.getArrayOffset(kind), Scale.fromInt(elementSize));
983 asm.shr(tempDest, tempDest, asm.i(config.cardtableShift));
984 asm.pstore(CiKind.Boolean, asm.w(config.cardtableStartAddress), tempDest, asm.b(false), false);
985
986 XirOperand tempDestEnd = tempSrc; // Reuse src temp
987 asm.lea(tempDestEnd, dest, destPos, config.getArrayOffset(kind), Scale.fromInt(elementSize));
988 asm.add(tempDestEnd, tempDestEnd, length);
989 asm.shr(tempDestEnd, tempDestEnd, asm.i(config.cardtableShift));
990
991 // Jump to out-of-line write barrier loop if the array is big.
992 XirLabel writeBarrierLoop = asm.createOutOfLineLabel("writeBarrierLoop");
993 asm.jneq(writeBarrierLoop, tempDest, tempSrc);
994 XirLabel back = asm.createInlineLabel("back");
995 asm.bindInline(back);
996
997 asm.bindOutOfLine(writeBarrierLoop);
998 asm.pstore(CiKind.Boolean, asm.w(config.cardtableStartAddress), tempDestEnd, asm.b(false), false);
999 asm.sub(tempDestEnd, tempDestEnd, asm.i(1));
1000 asm.jneq(writeBarrierLoop, tempDestEnd, tempDest);
1001 asm.jmp(back);
1002 }
1003
1004 if (is(STORE_CHECK, flags)) {
1005 assert kind == CiKind.Object;
1006 useRegisters(asm, AMD64.rax);
1007 asm.bindOutOfLine(slowStoreCheck);
1008 checkSubtype(asm, temp, valueHub, compHub);
1009 asm.jneq(store, temp, asm.w(0));
1010 XirOperand scratch = asm.createRegisterTemp("scratch", CiKind.Object, AMD64.r10);
1011 asm.mov(scratch, valueHub);
1012 asm.callRuntime(config.throwArrayStoreException, null);
1013 asm.jmp(store);
1014 }
1015
1016 return asm.finishTemplate("arraycopy<" + kind + ">");
1017 }
1018 };
1019
1020 private KindTemplates arrayStoreTemplates = new KindTemplates(NULL_CHECK, WRITE_BARRIER, BOUNDS_CHECK, STORE_CHECK, GIVEN_LENGTH) {
1021
1022 @Override
1023 protected XirTemplate create(CiXirAssembler asm, long flags, CiKind kind) {
1024 asm.restart(CiKind.Void);
1025 XirParameter array = asm.createInputParameter("array", CiKind.Object);
1026 XirParameter index = asm.createInputParameter("index", CiKind.Int, true);
1027 XirParameter value = asm.createInputParameter("value", kind, kind != CiKind.Object);
1028 XirOperand temp = asm.createTemp("temp", CiKind.Word);
1029 XirOperand valueHub = null;
1030 XirOperand compHub = null;
1031 XirLabel store = asm.createInlineLabel("store");
1032 XirLabel failBoundsCheck = null;
1033 XirLabel slowStoreCheck = null;
1034 // if the length is known the array cannot be null
1035 boolean implicitNullException = is(NULL_CHECK, flags);
1036
1037 if (is(BOUNDS_CHECK, flags)) {
1038 // load the array length and check the index
1039 failBoundsCheck = asm.createOutOfLineLabel("failBoundsCheck");
1040 XirOperand length;
1041 if (is(GIVEN_LENGTH, flags)) {
1042 length = asm.createInputParameter("length", CiKind.Int);
1043 } else {
1044 length = asm.createTemp("length", CiKind.Int);
1045 if (implicitNullException) {
1046 asm.nop(1);
1047 asm.mark(MARK_IMPLICIT_NULL);
1048 }
1049 asm.pload(CiKind.Int, length, array, asm.i(config.arrayLengthOffset), implicitNullException);
1050 implicitNullException = false;
1051 }
1052 asm.jugteq(failBoundsCheck, index, length);
1053
1054 }
1055 if (is(STORE_CHECK, flags) && kind == CiKind.Object) {
1056 slowStoreCheck = asm.createOutOfLineLabel("slowStoreCheck");
1057 asm.jeq(store, value, asm.o(null)); // first check if value is null
1058 valueHub = asm.createTemp("valueHub", CiKind.Object);
1059 compHub = asm.createTemp("compHub", CiKind.Object);
1060 if (implicitNullException) {
1061 asm.mark(MARK_IMPLICIT_NULL);
1062 }
1063 asm.pload(CiKind.Object, compHub, array, asm.i(config.hubOffset), implicitNullException);
1064 asm.pload(CiKind.Object, compHub, compHub, asm.i(config.arrayClassElementOffset), false);
1065 asm.pload(CiKind.Object, valueHub, value, asm.i(config.hubOffset), false);
1066 asm.jneq(slowStoreCheck, compHub, valueHub); // then check component hub matches value hub
1067
1068 implicitNullException = false;
1069 }
1070 asm.bindInline(store);
1071 int elemSize = target.sizeInBytes(kind);
1072
1073 if (implicitNullException) {
1074 asm.mark(MARK_IMPLICIT_NULL);
1075 }
1076 int disp = config.getArrayOffset(kind);
1077 Scale scale = Scale.fromInt(elemSize);
1078 if (kind == CiKind.Object) {
1079 verifyPointer(asm, value);
1080 }
1081 if (is(WRITE_BARRIER, flags)) {
1082 asm.lea(temp, array, index, disp, scale);
1083 asm.pstore(kind, temp, value, implicitNullException);
1084 writeBarrier(asm, temp);
1085 } else {
1086 asm.pstore(kind, array, index, value, disp, scale, implicitNullException);
1087 }
1088
1089 // -- out of line -------------------------------------------------------
1090 if (is(BOUNDS_CHECK, flags)) {
1091 asm.bindOutOfLine(failBoundsCheck);
1092 asm.callRuntime(config.throwArrayIndexException, null);
1093 asm.shouldNotReachHere();
1094 }
1095 if (is(STORE_CHECK, flags) && kind == CiKind.Object) {
1096 useRegisters(asm, AMD64.rax);
1097 asm.bindOutOfLine(slowStoreCheck);
1098 checkSubtype(asm, temp, valueHub, compHub);
1099 asm.jneq(store, temp, asm.w(0));
1100 XirOperand scratch = asm.createRegisterTemp("scratch", CiKind.Object, AMD64.r10);
1101 asm.mov(scratch, valueHub);
1102 asm.callRuntime(config.throwArrayStoreException, null);
1103 asm.jmp(store);
1104 }
1105 return asm.finishTemplate("arraystore<" + kind + ">");
1106 }
1107 };
1108
1109 private SimpleTemplates arrayLengthTemplates = new SimpleTemplates(NULL_CHECK) {
1110
1111 @Override
1112 protected XirTemplate create(CiXirAssembler asm, long flags) {
1113 XirOperand result = asm.restart(CiKind.Int);
1114 XirParameter object = asm.createInputParameter("object", CiKind.Object);
1115 if (is(NULL_CHECK, flags)) {
1116 asm.nop(1);
1117 asm.mark(MARK_IMPLICIT_NULL);
1118 }
1119 verifyPointer(asm, object);
1120 asm.pload(CiKind.Int, result, object, asm.i(config.arrayLengthOffset), true);
1121 return asm.finishTemplate("arrayLength");
1122 }
1123 };
1124
1125 @Override
1126 public XirSnippet genPrologue(XirSite site, RiMethod method) {
1127 boolean staticMethod = Modifier.isStatic(method.accessFlags());
1128 return new XirSnippet(staticMethod ? prologueTemplates.get(site, STATIC_METHOD) : prologueTemplates.get(site));
1129 }
1130
1131 @Override
1132 public XirSnippet genEpilogue(XirSite site, RiMethod method) {
1133 return new XirSnippet(epilogueTemplates.get(site));
1134 }
1135
1136 @Override
1137 public XirSnippet genSafepoint(XirSite site) {
1138 return new XirSnippet(safepointTemplates.get(site));
1139 }
1140
1141 @Override
1142 public XirSnippet genExceptionObject(XirSite site) {
1143 return new XirSnippet(exceptionObjectTemplates.get(site));
1144 }
1145
1146 @Override
1147 public XirSnippet genResolveClass(XirSite site, RiType type, Representation rep) {
1148 assert rep == Representation.ObjectHub || rep == Representation.StaticFields || rep == Representation.JavaClass : "unexpected representation: " + rep;
1149 if (type.isResolved()) {
1150 return new XirSnippet(resolveClassTemplates.get(site), XirArgument.forObject(type));
1151 }
1152 return new XirSnippet(resolveClassTemplates.get(site, UNRESOLVED));
1153 }
1154
1155 @Override
1156 public XirSnippet genIntrinsic(XirSite site, XirArgument[] arguments, RiMethod method) {
1157 return null;
1158 }
1159
1160 @Override
1161 public XirSnippet genInvokeInterface(XirSite site, XirArgument receiver, RiMethod method) {
1162 return new XirSnippet(invokeInterfaceTemplates.get(site), receiver, XirArgument.forWord(0));
1163 }
1164
1165 @Override
1166 public XirSnippet genInvokeVirtual(XirSite site, XirArgument receiver, RiMethod method) {
1167 return new XirSnippet(invokeVirtualTemplates.get(site), receiver, XirArgument.forWord(0));
1168 }
1169
1170 @Override
1171 public XirSnippet genInvokeSpecial(XirSite site, XirArgument receiver, RiMethod method) {
1172 return new XirSnippet(invokeSpecialTemplates.get(site), receiver, XirArgument.forWord(0));
1173 }
1174
1175 @Override
1176 public XirSnippet genInvokeStatic(XirSite site, RiMethod method) {
1177 return new XirSnippet(invokeStaticTemplates.get(site), XirArgument.forWord(0));
1178 }
1179
1180 @Override
1181 public XirSnippet genMonitorEnter(XirSite site, XirArgument receiver, XirArgument lockAddress) {
1182 return new XirSnippet(monitorEnterTemplates.get(site), receiver, lockAddress);
1183 }
1184
1185 @Override
1186 public XirSnippet genMonitorExit(XirSite site, XirArgument receiver, XirArgument lockAddress) {
1187 return new XirSnippet(monitorExitTemplates.get(site), receiver, lockAddress);
1188 }
1189
1190 @Override
1191 public XirSnippet genGetField(XirSite site, XirArgument object, RiField field) {
1192 if (field.isResolved()) {
1193 return new XirSnippet(getFieldTemplates.get(site, field.kind()), object, XirArgument.forInt(((HotSpotField) field).offset()));
1194 }
1195 return new XirSnippet(getFieldTemplates.get(site, field.kind(), UNRESOLVED), object);
1196 }
1197
1198 @Override
1199 public XirSnippet genWriteBarrier(XirArgument object) {
1200 return new XirSnippet(writeBarrierTemplate.get(null, CiKind.Void), object);
1201 }
1202
1203 @Override
1204 public XirSnippet genPutField(XirSite site, XirArgument object, RiField field, XirArgument value) {
1205 if (field.isResolved()) {
1206 return new XirSnippet(putFieldTemplates.get(site, field.kind()), object, value, XirArgument.forInt(((HotSpotField) field).offset()));
1207 }
1208 return new XirSnippet(putFieldTemplates.get(site, field.kind(), UNRESOLVED), object, value);
1209 }
1210
1211 @Override
1212 public XirSnippet genGetStatic(XirSite site, XirArgument object, RiField field) {
1213 if (field.isResolved()) {
1214 return new XirSnippet(getFieldTemplates.get(site, field.kind()), object, XirArgument.forInt(((HotSpotField) field).offset()));
1215 }
1216 return new XirSnippet(getFieldTemplates.get(site, field.kind(), UNRESOLVED), object);
1217 }
1218
1219 @Override
1220 public XirSnippet genPutStatic(XirSite site, XirArgument object, RiField field, XirArgument value) {
1221 if (field.isResolved()) {
1222 return new XirSnippet(putFieldTemplates.get(site, field.kind()), object, value, XirArgument.forInt(((HotSpotField) field).offset()));
1223 }
1224 return new XirSnippet(putFieldTemplates.get(site, field.kind(), UNRESOLVED), object, value);
1225 }
1226
1227 @Override
1228 public XirSnippet genNewInstance(XirSite site, RiType type) {
1229 if (type.isResolved()) {
1230 int instanceSize = ((HotSpotTypeResolved) type).instanceSize();
1231 return new XirSnippet(newInstanceTemplates.get(site, instanceSize), XirArgument.forObject(type));
1232 }
1233 return new XirSnippet(newInstanceUnresolvedTemplates.get(site));
1234 }
1235
1236 @Override
1237 public XirSnippet genNewArray(XirSite site, XirArgument length, CiKind elementKind, RiType componentType, RiType arrayType) {
1238 if (elementKind == CiKind.Object) {
1239 if (arrayType.isResolved()) {
1240 return new XirSnippet(newObjectArrayTemplates.get(site), length, XirArgument.forObject(arrayType));
1241 }
1242 return new XirSnippet(newObjectArrayTemplates.get(site, UNRESOLVED), length);
1243 }
1244 assert arrayType == null;
1245 arrayType = compiler.getVMEntries().getPrimitiveArrayType(elementKind);
1246 return new XirSnippet(newTypeArrayTemplates.get(site, elementKind), length, XirArgument.forObject(arrayType));
1247 }
1248
1249 @Override
1250 public XirSnippet genNewObjectArrayClone(XirSite site, XirArgument newLength, XirArgument referenceArray) {
1251 return new XirSnippet(newObjectArrayCloneTemplates.get(site), newLength, referenceArray);
1252 }
1253
1254 @Override
1255 public XirSnippet genNewMultiArray(XirSite site, XirArgument[] lengths, RiType type) {
1256 if (type.isResolved()) {
1257 XirArgument[] params = Arrays.copyOf(lengths, lengths.length + 1);
1258 params[lengths.length] = XirArgument.forObject(type);
1259 return new XirSnippet(multiNewArrayTemplate.get(site, lengths.length), params);
1260 }
1261 return new XirSnippet(multiNewArrayTemplate.get(site, lengths.length, UNRESOLVED), lengths);
1262 }
1263
1264 @Override
1265 public XirSnippet genCheckCast(XirSite site, XirArgument receiver, XirArgument hub, RiType type) {
1266 if (type.isResolved()) {
1267 return new XirSnippet(checkCastTemplates.get(site), receiver, hub);
1268 }
1269 return new XirSnippet(checkCastTemplates.get(site, UNRESOLVED), receiver);
1270 }
1271
1272 @Override
1273 public XirSnippet genInstanceOf(XirSite site, XirArgument object, XirArgument hub, RiType type) {
1274 if (type.isResolved()) {
1275 return new XirSnippet(instanceOfTemplates.get(site), object, hub);
1276 }
1277 return new XirSnippet(instanceOfTemplates.get(site, UNRESOLVED), object);
1278 }
1279
1280 @Override
1281 public XirSnippet genArrayLoad(XirSite site, XirArgument array, XirArgument index, XirArgument length, CiKind elementKind, RiType elementType) {
1282 if (length == null || !site.requiresBoundsCheck()) {
1283 return new XirSnippet(arrayLoadTemplates.get(site, elementKind), array, index);
1284 }
1285 return new XirSnippet(arrayLoadTemplates.get(site, elementKind, GIVEN_LENGTH), array, index, length);
1286 }
1287
1288 @Override
1289 public XirSnippet genArrayStore(XirSite site, XirArgument array, XirArgument index, XirArgument length, XirArgument value, CiKind elementKind, RiType elementType) {
1290 if (length == null || !site.requiresBoundsCheck()) {
1291 return new XirSnippet(arrayStoreTemplates.get(site, elementKind), array, index, value);
1292 }
1293 return new XirSnippet(arrayStoreTemplates.get(site, elementKind, GIVEN_LENGTH), array, index, value, length);
1294 }
1295
1296 @Override
1297 public XirSnippet genArrayCopy(XirSite site, XirArgument src, XirArgument srcPos, XirArgument dest, XirArgument destPos, XirArgument length, RiType elementType, boolean inputsSame, boolean inputsDifferent) {
1298 if (elementType == null) {
1299 return null;
1300 }
1301 assert !inputsDifferent || !inputsSame;
1302 XirTemplate template = null;
1303 if (inputsDifferent) {
1304 template = arrayCopyTemplates.get(site, elementType.kind(), INPUTS_DIFFERENT);
1305 } else if (inputsSame) {
1306 template = arrayCopyTemplates.get(site, elementType.kind(), INPUTS_SAME);
1307 } else {
1308 template = arrayCopyTemplates.get(site, elementType.kind());
1309 }
1310 return new XirSnippet(template, src, srcPos, dest, destPos, length);
1311 }
1312
1313 @Override
1314 public XirSnippet genArrayLength(XirSite site, XirArgument array) {
1315 return new XirSnippet(arrayLengthTemplates.get(site), array);
1316 }
1317
1318 @Override
1319 public List<XirTemplate> buildTemplates(CiXirAssembler asm) {
1320 this.asm = asm;
1321 List<XirTemplate> templates = new ArrayList<XirTemplate>();
1322 return templates;
1323 }
1324
1325 private static class UnresolvedClassPatching {
1326
1327 private final XirLabel patchSite;
1328 private final XirLabel replacement;
1329 private final XirLabel patchStub;
1330 private final CiXirAssembler asm;
1331 private final HotSpotVMConfig config;
1332 private final XirOperand arg;
1333 private State state;
1334
1335 private enum State {
1336 New, Inline, Finished
1337 }
1338
1339 public UnresolvedClassPatching(CiXirAssembler asm, XirOperand arg, HotSpotVMConfig config) {
1340 this.asm = asm;
1341 this.arg = arg;
1342 this.config = config;
1343 patchSite = asm.createInlineLabel("patch site");
1344 replacement = asm.createOutOfLineLabel("replacement");
1345 patchStub = asm.createOutOfLineLabel("patch stub");
1346
1347 state = State.New;
1348 }
1349
1350 public void emitInline() {
1351 assert state == State.New;
1352
1353 asm.bindInline(patchSite);
1354 asm.mark(MARK_DUMMY_OOP_RELOCATION);
1355
1356 asm.jmp(patchStub);
1357
1358 // TODO: make this more generic & safe - this is needed to create space for patching
1359 asm.nop(5);
1360
1361 state = State.Inline;
1362 }
1363
1364 public void emitOutOfLine() {
1365 assert state == State.Inline;
1366
1367 asm.bindOutOfLine(replacement);
1368 XirMark begin = asm.mark(null);
1369 asm.mov(arg, asm.createConstant(CiConstant.NULL_OBJECT));
1370 XirMark end = asm.mark(null);
1371 // make this piece of data look like an instruction
1372 asm.rawBytes(new byte[] {(byte) 0xb8, 0, 0, 0x05, 0});
1373 asm.mark(MARK_KLASS_PATCHING, begin, end);
1374 asm.bindOutOfLine(patchStub);
1375 asm.callRuntime(config.loadKlassStub, null);
1376 asm.jmp(patchSite);
1377
1378 state = State.Finished;
1379 }
1380 }
1381
1382 private static class UnresolvedFieldPatching {
1383
1384 private final XirLabel patchSite;
1385 private final XirLabel replacement;
1386 private final XirLabel patchStub;
1387 private final CiXirAssembler asm;
1388 private final HotSpotVMConfig config;
1389 private State state;
1390 private final XirOperand receiver;
1391 private final XirOperand value;
1392 private final boolean put;
1393 private final boolean nullCheck;
1394
1395 private enum State {
1396 New, Inline, Finished
1397 }
1398
1399 public UnresolvedFieldPatching(CiXirAssembler asm, XirOperand receiver, XirOperand value, boolean put, boolean nullCheck, HotSpotVMConfig config) {
1400 this.asm = asm;
1401 this.receiver = receiver;
1402 this.value = value;
1403 this.put = put;
1404 this.nullCheck = nullCheck;
1405 this.config = config;
1406 patchSite = asm.createInlineLabel("patch site");
1407 replacement = asm.createOutOfLineLabel("replacement");
1408 patchStub = asm.createOutOfLineLabel("patch stub");
1409
1410 state = State.New;
1411 }
1412
1413 public void emitInline() {
1414 assert state == State.New;
1415 if (nullCheck) {
1416 asm.nop(1);
1417 }
1418 asm.bindInline(patchSite);
1419 asm.mark(MARK_DUMMY_OOP_RELOCATION);
1420 if (nullCheck) {
1421 asm.mark(MARK_IMPLICIT_NULL);
1422 }
1423 asm.safepoint();
1424 asm.jmp(patchStub);
1425
1426 // TODO: make this more generic & safe - this is needed to create space for patching
1427 asm.nop(5);
1428
1429 state = State.Inline;
1430 }
1431
1432 public void emitOutOfLine() {
1433 assert state == State.Inline;
1434
1435 asm.bindOutOfLine(replacement);
1436 XirMark begin = asm.mark(null);
1437 if (put) {
1438 asm.pstore(value.kind, receiver, asm.i(Integer.MAX_VALUE), value, false);
1439 } else {
1440 asm.pload(value.kind, value, receiver, asm.i(Integer.MAX_VALUE), false);
1441 }
1442 XirMark end = asm.mark(null);
1443 // make this piece of data look like an instruction
1444 asm.rawBytes(new byte[] {(byte) 0xb8, 0, 0, 0x05, 0});
1445 asm.mark(MARK_ACCESS_FIELD_PATCHING, begin, end);
1446 asm.bindOutOfLine(patchStub);
1447 asm.callRuntime(config.accessFieldStub, null);
1448 asm.jmp(patchSite);
1449
1450 // Check if we need NOP instructions like in C1 to "not destroy the world".
1451
1452 state = State.Finished;
1453 }
1454 }
1455
1456 private void verifyPointer(CiXirAssembler asm, XirOperand pointer) {
1457 if (config.verifyPointers) {
1458 // The verify pointer stub wants the argument in a fixed register.
1459 XirOperand fixed = asm.createRegisterTemp("fixed", CiKind.Object, AMD64.r13);
1460 asm.push(fixed);
1461 asm.mov(fixed, pointer);
1462 asm.callRuntime(config.verifyPointerStub, null);
1463 asm.pop(fixed);
1464 }
1465 }
1466
1467 private void checkSubtype(CiXirAssembler asm, XirOperand result, XirOperand objHub, XirOperand hub) {
1468 asm.push(objHub);
1469 asm.push(hub);
1470 asm.callRuntime(config.instanceofStub, null);
1471 asm.pop(result);
1472 asm.pop(result);
1473 }
1474
1475 private void useRegisters(CiXirAssembler asm, CiRegister... registers) {
1476 if (registers != null) {
1477 for (CiRegister register : registers) {
1478 asm.createRegisterTemp("reg", CiKind.Illegal, register);
1479 }
1480 }
1481 }
1482
1483 private void writeBarrier(CiXirAssembler asm, XirOperand base) {
1484 asm.shr(base, base, asm.i(config.cardtableShift));
1485 asm.pstore(CiKind.Boolean, asm.w(config.cardtableStartAddress), base, asm.b(false), false);
1486 }
1487
1488 public boolean is(TemplateFlag check, long flags) {
1489 return (flags & check.bits()) == check.bits();
1490 }
1491
1492 /**
1493 * Base class for all the ondemand template generators. It is not normally subclassed directly, but through one of
1494 * its subclasses (SimpleTemplates, KindTemplates, IndexTemplates).
1495 *
1496 * @author Lukas Stadler
1497 */
1498 private abstract class Templates {
1499
1500 private ConcurrentHashMap<Long, XirTemplate> templates = new ConcurrentHashMap<Long, XirTemplate>();
1501 private final long mask;
1502
1503 /**
1504 * Each flag passed to this method will cause templates with and without it to be generated.
1505 */
1506 public Templates(TemplateFlag... flags) {
1507 this.mask = getBits((int) INDEX_MASK, null, flags);
1508 }
1509
1510 protected abstract XirTemplate create(CiXirAssembler asm, long flags);
1511
1512 protected long getBits(int index, XirSite site, TemplateFlag... flags) {
1513 long bits = index;
1514 if (site != null) {
1515 bits |= site.requiresNullCheck() ? NULL_CHECK.bits() : 0;
1516 bits |= site.requiresReadBarrier() ? READ_BARRIER.bits() : 0;
1517 bits |= site.requiresWriteBarrier() ? WRITE_BARRIER.bits() : 0;
1518 bits |= site.requiresArrayStoreCheck() ? STORE_CHECK.bits() : 0;
1519 bits |= site.requiresBoundsCheck() ? BOUNDS_CHECK.bits() : 0;
1520 }
1521 if (flags != null) {
1522 for (TemplateFlag flag : flags) {
1523 bits |= flag.bits();
1524 }
1525 }
1526 return bits;
1527 }
1528
1529 protected XirTemplate getInternal(long flags) {
1530 flags = flags & mask;
1531 XirTemplate template = templates.get(flags);
1532 if (template == null) {
1533 template = create(HotSpotXirGenerator.this.asm.copy(), flags);
1534 templates.put(flags, template);
1535 }
1536 return template;
1537 }
1538 }
1539
1540 private abstract class SimpleTemplates extends Templates {
1541
1542 public SimpleTemplates(TemplateFlag... flags) {
1543 super(flags);
1544 }
1545
1546 public XirTemplate get(XirSite site, TemplateFlag... flags) {
1547 return getInternal(getBits(0, site, flags));
1548 }
1549 }
1550
1551 private abstract class IndexTemplates extends Templates {
1552
1553 public IndexTemplates(TemplateFlag... flags) {
1554 super(flags);
1555 }
1556
1557 @Override
1558 protected final XirTemplate create(CiXirAssembler asm, long flags) {
1559 return create(asm, flags & FLAGS_MASK, (int) (flags & INDEX_MASK));
1560 }
1561
1562 protected abstract XirTemplate create(CiXirAssembler asm, long flags, int index);
1563
1564 public XirTemplate get(XirSite site, int size, TemplateFlag... flags) {
1565 return getInternal(getBits(size, site, flags));
1566 }
1567 }
1568
1569 private abstract class KindTemplates extends Templates {
1570
1571 public KindTemplates(TemplateFlag... flags) {
1572 super(flags);
1573 }
1574
1575 @Override
1576 protected final XirTemplate create(CiXirAssembler asm, long flags) {
1577 return create(asm, flags & FLAGS_MASK, CiKind.VALUES[(int) (flags & INDEX_MASK)]);
1578 }
1579
1580 protected abstract XirTemplate create(CiXirAssembler asm, long flags, CiKind kind);
1581
1582 public XirTemplate get(XirSite site, CiKind kind, TemplateFlag... flags) {
1583 return getInternal(getBits(kind.ordinal(), site, flags));
1584 }
1585 }
1586 }