optimistic_recompilation.js revision 1643:133ea8746b37
1/* 2 * Copyright (c) 2014, 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 24/** 25 * Ask Debug for an event log of favourable events instead of using --log flags printing to screen 26 * @test 27 * @bug 8037086,8038398 28 * @fork 29 * @option -Dnashorn.debug=true 30 * @option --log=recompile:quiet 31 * @option --optimistic-types=true 32 * @run 33 */ 34 35var forName = java.lang.Class["forName(String)"]; 36var RuntimeEvent = forName("jdk.nashorn.internal.runtime.events.RuntimeEvent").static; 37var getValue = RuntimeEvent.class.getMethod("getValue"); 38var RewriteException = forName("jdk.nashorn.internal.runtime.RewriteException").static; 39var getReturnType = RewriteException.class.getMethod("getReturnType"); 40var RecompilationEvent = forName("jdk.nashorn.internal.runtime.events.RecompilationEvent").static; 41var getReturnValue = RecompilationEvent.class.getMethod("getReturnValue"); 42var setReturnTypeAndValue = []; 43var expectedValues = []; 44 45function checkExpectedRecompilation(f, expectedValues, testCase) { 46 Debug.clearRuntimeEvents(); 47 print(f()); 48 events = Debug.getRuntimeEvents(); 49 //make sure we got runtime events 50 print("events = " + (events.toString().indexOf("RuntimeEvent") != -1)); 51 if (events.length == expectedValues.length) { 52 for (var i in events) { 53 var e = events[i]; 54 var returnValue = getReturnValue.invoke(e); 55 if (typeof returnValue != 'undefined') { 56 setReturnTypeAndValue[i] = [getReturnType.invoke(getValue.invoke(e)), returnValue]; 57 } else { 58 returnValue = "undefined"; 59 setReturnTypeAndValue[i] = [getReturnType.invoke(getValue.invoke(e)), returnValue]; 60 } 61 if (!setReturnTypeAndValue[i].toString().equals(expectedValues[i].toString())) { 62 fail("The return values are not as expected. Expected value: " + expectedValues[i] + " and got: " + setReturnTypeAndValue[i] + " in test case: " + f); 63 } 64 } 65 } else { 66 fail("Number of Deoptimizing recompilation is not correct, expected: " + expectedValues.length + " and found: " + events.length + " in test case: " + f); 67 } 68} 69 70checkExpectedRecompilation(function divisionByZeroTest() {var x = { a: 2, b:1 }; x.a = Number.POSITIVE_INFINITY; x.b = 0; print(x.a/x.b); return 1;}, 71 expectedValues =[['double', 'Infinity']]); 72checkExpectedRecompilation(function divisionWithRemainderTest() {var x = { a: 7, b:2 }; print(x.a/x.b); return 1;}, expectedValues =[['double', '3.5']]); 73checkExpectedRecompilation(function infinityMultiplicationTest() {var x = { a: Number.POSITIVE_INFINITY, b: Number.POSITIVE_INFINITY}; print(x.a*x.b); return 1;}, 74 expectedValues =[['double', 'Infinity']]); 75checkExpectedRecompilation(function maxValueMultiplicationTest() {var x = { a: Number.MAX_VALUE, b: Number.MAX_VALUE}; print(x.a*x.b); return 1;}, 76 expectedValues =[['double', '1.7976931348623157e+308']]); 77checkExpectedRecompilation(function divisionByInfinityTest() {var x = { a: -1, b: Number.POSITIVE_INFINITY}; print(x.a/x.b); return 1;}, 78 expectedValues =[['double', 'Infinity']]); 79checkExpectedRecompilation(function divisionByStringTest() {var x = { a: Number.POSITIVE_INFINITY, b: 'Hello'}; print(x.a/x.b); return 1;}, 80 expectedValues =[['double', 'Infinity']]); 81checkExpectedRecompilation(function nestedFunctionTest() {var a=3,b,c; function f() {var x = 2, y =1; function g(){var y = x; var z = a; z = x*y; print(a*b)} g()}f(); return 1;}, 82 expectedValues =[['object', 'undefined']]); 83checkExpectedRecompilation(function functionTest(a,b,c) { d = (a + b) * c; print(d); return 1;}, expectedValues =[['double', 'NaN']]); 84checkExpectedRecompilation(function andTest(a,b) { d = a && b; print(d); return 1;}, expectedValues =[['object', 'undefined']]); 85