2010-08-18 Andreas Kling <andreas.kling@nokia.com>
[WebKit-https.git] / JavaScriptCore / jit / ThunkGenerators.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ThunkGenerators.h"
28
29 #include "CodeBlock.h"
30 #include <wtf/text/StringImpl.h>
31 #include "SpecializedThunkJIT.h"
32
33 #if ENABLE(JIT)
34
35 namespace JSC {
36
37 static void stringCharLoad(SpecializedThunkJIT& jit)
38 {
39     // load string
40     jit.loadJSStringArgument(SpecializedThunkJIT::ThisArgument, SpecializedThunkJIT::regT0);
41     // regT0 now contains this, and is a non-rope JSString*
42
43     // Load string length to regT2, and start the process of loading the data pointer into regT0
44     jit.load32(MacroAssembler::Address(SpecializedThunkJIT::regT0, ThunkHelpers::jsStringLengthOffset()), SpecializedThunkJIT::regT2);
45     jit.loadPtr(MacroAssembler::Address(SpecializedThunkJIT::regT0, ThunkHelpers::jsStringValueOffset()), SpecializedThunkJIT::regT0);
46     jit.loadPtr(MacroAssembler::Address(SpecializedThunkJIT::regT0, ThunkHelpers::stringImplDataOffset()), SpecializedThunkJIT::regT0);
47
48     // load index
49     jit.loadInt32Argument(0, SpecializedThunkJIT::regT1); // regT1 contains the index
50
51     // Do an unsigned compare to simultaneously filter negative indices as well as indices that are too large
52     jit.appendFailure(jit.branch32(MacroAssembler::AboveOrEqual, SpecializedThunkJIT::regT1, SpecializedThunkJIT::regT2));
53
54     // Load the character
55     jit.load16(MacroAssembler::BaseIndex(SpecializedThunkJIT::regT0, SpecializedThunkJIT::regT1, MacroAssembler::TimesTwo, 0), SpecializedThunkJIT::regT0);
56 }
57
58 static void charToString(SpecializedThunkJIT& jit, JSGlobalData* globalData, MacroAssembler::RegisterID src, MacroAssembler::RegisterID dst, MacroAssembler::RegisterID scratch)
59 {
60     jit.appendFailure(jit.branch32(MacroAssembler::AboveOrEqual, src, MacroAssembler::Imm32(0x100)));
61     jit.move(MacroAssembler::ImmPtr(globalData->smallStrings.singleCharacterStrings()), scratch);
62     jit.loadPtr(MacroAssembler::BaseIndex(scratch, src, MacroAssembler::ScalePtr, 0), dst);
63     jit.appendFailure(jit.branchTestPtr(MacroAssembler::Zero, dst));
64 }
65
66 MacroAssemblerCodePtr charCodeAtThunkGenerator(JSGlobalData* globalData, ExecutablePool* pool)
67 {
68     SpecializedThunkJIT jit(1, globalData, pool);
69     stringCharLoad(jit);
70     jit.returnInt32(SpecializedThunkJIT::regT0);
71     return jit.finalize(globalData->jitStubs->ctiNativeCall());
72 }
73
74 MacroAssemblerCodePtr charAtThunkGenerator(JSGlobalData* globalData, ExecutablePool* pool)
75 {
76     SpecializedThunkJIT jit(1, globalData, pool);
77     stringCharLoad(jit);
78     charToString(jit, globalData, SpecializedThunkJIT::regT0, SpecializedThunkJIT::regT0, SpecializedThunkJIT::regT1);
79     jit.returnJSCell(SpecializedThunkJIT::regT0);
80     return jit.finalize(globalData->jitStubs->ctiNativeCall());
81 }
82
83 MacroAssemblerCodePtr fromCharCodeThunkGenerator(JSGlobalData* globalData, ExecutablePool* pool)
84 {
85     SpecializedThunkJIT jit(1, globalData, pool);
86     // load char code
87     jit.loadInt32Argument(0, SpecializedThunkJIT::regT0);
88     charToString(jit, globalData, SpecializedThunkJIT::regT0, SpecializedThunkJIT::regT0, SpecializedThunkJIT::regT1);
89     jit.returnJSCell(SpecializedThunkJIT::regT0);
90     return jit.finalize(globalData->jitStubs->ctiNativeCall());
91 }
92
93 MacroAssemblerCodePtr sqrtThunkGenerator(JSGlobalData* globalData, ExecutablePool* pool)
94 {
95 #if USE(JSVALUE64) || USE(JSVALUE32_64)
96     SpecializedThunkJIT jit(1, globalData, pool);
97     if (!jit.supportsFloatingPointSqrt())
98         return globalData->jitStubs->ctiNativeCall();
99
100     jit.loadDoubleArgument(0, SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::regT0);
101     jit.sqrtDouble(SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::fpRegT0);
102     jit.returnDouble(SpecializedThunkJIT::fpRegT0);
103     return jit.finalize(globalData->jitStubs->ctiNativeCall());
104 #else
105     UNUSED_PARAM(pool);
106     return globalData->jitStubs->ctiNativeCall();
107 #endif
108 }
109
110 static const double oneConstant = 1.0;
111 static const double negativeHalfConstant = -0.5;
112
113 MacroAssemblerCodePtr powThunkGenerator(JSGlobalData* globalData, ExecutablePool* pool)
114 {
115 #if USE(JSVALUE64) || USE(JSVALUE32_64)
116     SpecializedThunkJIT jit(2, globalData, pool);
117     if (!jit.supportsFloatingPoint())
118         return globalData->jitStubs->ctiNativeCall();
119
120     jit.loadDouble(&oneConstant, SpecializedThunkJIT::fpRegT1);
121     jit.loadDoubleArgument(0, SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::regT0);
122     MacroAssembler::Jump nonIntExponent;
123     jit.loadInt32Argument(1, SpecializedThunkJIT::regT0, nonIntExponent);
124     jit.appendFailure(jit.branch32(MacroAssembler::LessThan, SpecializedThunkJIT::regT0, MacroAssembler::Imm32(0)));
125     
126     MacroAssembler::Jump exponentIsZero = jit.branchTest32(MacroAssembler::Zero, SpecializedThunkJIT::regT0);
127     MacroAssembler::Label startLoop(jit.label());
128
129     MacroAssembler::Jump exponentIsEven = jit.branchTest32(MacroAssembler::Zero, SpecializedThunkJIT::regT0, MacroAssembler::Imm32(1));
130     jit.mulDouble(SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::fpRegT1);
131     exponentIsEven.link(&jit);
132     jit.mulDouble(SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::fpRegT0);
133     jit.rshift32(MacroAssembler::Imm32(1), SpecializedThunkJIT::regT0);
134     jit.branchTest32(MacroAssembler::NonZero, SpecializedThunkJIT::regT0).linkTo(startLoop, &jit);
135
136     exponentIsZero.link(&jit);
137
138     {
139         SpecializedThunkJIT::JumpList doubleResult;
140         jit.branchConvertDoubleToInt32(SpecializedThunkJIT::fpRegT1, SpecializedThunkJIT::regT0, doubleResult, SpecializedThunkJIT::fpRegT0);
141         jit.returnInt32(SpecializedThunkJIT::regT0);
142         doubleResult.link(&jit);
143         jit.returnDouble(SpecializedThunkJIT::fpRegT1);
144     }
145
146     if (jit.supportsFloatingPointSqrt()) {
147         nonIntExponent.link(&jit);
148         jit.loadDouble(&negativeHalfConstant, SpecializedThunkJIT::fpRegT3);
149         jit.loadDoubleArgument(1, SpecializedThunkJIT::fpRegT2, SpecializedThunkJIT::regT0);
150         jit.appendFailure(jit.branchDouble(MacroAssembler::DoubleLessThanOrEqual, SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::fpRegT1));
151         jit.appendFailure(jit.branchDouble(MacroAssembler::DoubleNotEqualOrUnordered, SpecializedThunkJIT::fpRegT2, SpecializedThunkJIT::fpRegT3));
152         jit.sqrtDouble(SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::fpRegT0);
153         jit.divDouble(SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::fpRegT1);
154
155         SpecializedThunkJIT::JumpList doubleResult;
156         jit.branchConvertDoubleToInt32(SpecializedThunkJIT::fpRegT1, SpecializedThunkJIT::regT0, doubleResult, SpecializedThunkJIT::fpRegT0);
157         jit.returnInt32(SpecializedThunkJIT::regT0);
158         doubleResult.link(&jit);
159         jit.returnDouble(SpecializedThunkJIT::fpRegT1);
160     } else
161         jit.appendFailure(nonIntExponent);
162
163     return jit.finalize(globalData->jitStubs->ctiNativeCall());
164 #else
165     UNUSED_PARAM(pool);
166     return globalData->jitStubs->ctiNativeCall();
167 #endif
168 }
169
170 }
171
172 #endif // ENABLE(JIT)