1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/compiler/test_stubs.h"
17
18 #include "ecmascript/compiler/llvm_ir_builder.h"
19 #include "ecmascript/compiler/stub_builder-inl.h"
20 #include "ecmascript/compiler/variable_type.h"
21 #include "ecmascript/message_string.h"
22
23 namespace panda::ecmascript::kungfu {
24 using namespace panda::ecmascript;
25 #ifndef NDEBUG
GenerateCircuit()26 void FooAOTStubBuilder::GenerateCircuit()
27 {
28 GateRef glue = PtrArgument(0);
29 GateRef env = TaggedArgument(1);
30 GateRef argc = Int64Argument(2);
31 GateRef calltarget = TaggedArgument(3);
32 GateRef newtarget = TaggedArgument(4);
33 GateRef thisObj = TaggedArgument(5);
34 GateRef a = TaggedArgument(6);
35 GateRef b = TaggedArgument(7);
36 GateRef bcOffset = Int32(1);
37 (void)calltarget;
38 GateRef barIndex = IntToTaggedInt(Int32(CommonStubCSigns::BarAOT));
39 GateRef numArgs = IntToTaggedInt(Int32(2));
40 GateRef barfunc = CallRuntime(glue, RTSTUB_ID(DefineAotFunc), {barIndex, numArgs});
41 GateRef result =
42 CallNGCRuntime(glue, RTSTUB_ID(JSCall), {glue, env, argc, barfunc, newtarget, thisObj, a, b, bcOffset});
43 Return(result);
44 }
45
GenerateCircuit()46 void BarAOTStubBuilder::GenerateCircuit()
47 {
48 GateRef glue = PtrArgument(0);
49 [[maybe_unused]] GateRef env = TaggedArgument(1);
50 [[maybe_unused]] GateRef argc = Int64Argument(2);
51 [[maybe_unused]] GateRef calltarget = TaggedArgument(3);
52 [[maybe_unused]] GateRef newtarget = TaggedArgument(4);
53 [[maybe_unused]] GateRef thisObj = TaggedArgument(5);
54 GateRef a = TaggedArgument(6);
55 GateRef b = TaggedArgument(7);
56 GateRef result = CallRuntime(glue, RTSTUB_ID(Add2), {a, b});
57 Return(result);
58 }
59
GenerateCircuit()60 void Foo1AOTStubBuilder::GenerateCircuit()
61 {
62 GateRef glue = PtrArgument(0);
63 GateRef env = TaggedArgument(1);
64 GateRef argc = Int64Argument(2);
65 GateRef calltarget = TaggedArgument(3);
66 GateRef newtarget = TaggedArgument(4);
67 GateRef thisObj = TaggedArgument(5);
68 GateRef a = TaggedArgument(6);
69 GateRef b = TaggedArgument(7);
70 GateRef bcOffset = Int32(1);
71 (void)calltarget;
72 GateRef barIndex = IntToTaggedInt(Int32(CommonStubCSigns::Bar1AOT));
73 GateRef numArgs = IntToTaggedInt(Int32(3));
74 GateRef barfunc = CallRuntime(glue, RTSTUB_ID(DefineAotFunc), {barIndex, numArgs});
75 GateRef result =
76 CallNGCRuntime(glue, RTSTUB_ID(JSCall), {glue, env, argc, barfunc, newtarget, thisObj, a, b, bcOffset});
77 Return(result);
78 }
79
GenerateCircuit()80 void Bar1AOTStubBuilder::GenerateCircuit()
81 {
82 GateRef glue = PtrArgument(0);
83 GateRef env = TaggedArgument(1);
84 GateRef argc = Int64Argument(2);
85 GateRef calltarget = TaggedArgument(3);
86 GateRef newtarget = TaggedArgument(4);
87 GateRef thisObj = TaggedArgument(5);
88 (void)env;
89 (void)argc;
90 (void)calltarget;
91 (void)newtarget;
92 (void)thisObj;
93 GateRef a = TaggedArgument(6);
94 GateRef b = TaggedArgument(7);
95 GateRef c = TaggedArgument(8);
96 GateRef result = CallRuntime(glue, RTSTUB_ID(Add2), {a, b});
97 GateRef result2 = CallRuntime(glue, RTSTUB_ID(Add2), {result, c});
98 Return(result2);
99 }
100
GenerateCircuit()101 void Foo2AOTStubBuilder::GenerateCircuit()
102 {
103 GateRef glue = PtrArgument(0);
104 GateRef env = TaggedArgument(1);
105 GateRef argc = Int64Argument(2);
106 GateRef calltarget = TaggedArgument(3);
107 GateRef newtarget = TaggedArgument(4);
108 GateRef thisObj = TaggedArgument(5);
109 GateRef a = TaggedArgument(6);
110 GateRef b = TaggedArgument(7);
111 GateRef bcOffset = Int32(1);
112 (void)calltarget;
113 GateRef actualArgC = Int64Add(argc, Int64(1));
114 GateRef barIndex = IntToTaggedInt(Int32(CommonStubCSigns::BarAOT));
115 GateRef numArgs = IntToTaggedInt(Int32(2));
116 GateRef barfunc = CallRuntime(glue, RTSTUB_ID(DefineAotFunc), {barIndex, numArgs});
117 GateRef result = CallNGCRuntime(glue, RTSTUB_ID(JSCall), {glue, env, actualArgC, barfunc, newtarget, thisObj,
118 a, b, Undefined(), bcOffset});
119 Return(result);
120 }
121
GenerateCircuit()122 void FooNativeAOTStubBuilder::GenerateCircuit()
123 {
124 GateRef glue = PtrArgument(0);
125 GateRef env = TaggedArgument(1);
126 GateRef argc = Int64Argument(2);
127 GateRef calltarget = TaggedArgument(3);
128 GateRef newtarget = TaggedArgument(4);
129 GateRef thisObj = TaggedArgument(5);
130 GateRef a = TaggedArgument(6);
131 GateRef b = TaggedArgument(7);
132 GateRef bcOffset = Int32(1);
133 (void)calltarget;
134 GateRef actualArgC = Int64Add(argc, Int64(1));
135 GateRef printfunc = CallRuntime(glue, RTSTUB_ID(GetPrintFunc), {});
136 GateRef result = CallNGCRuntime(glue, RTSTUB_ID(JSCall), {glue, env, actualArgC, printfunc, newtarget, thisObj,
137 a, b, Undefined(), bcOffset});
138 Return(result);
139 }
140
GenerateCircuit()141 void FooBoundAOTStubBuilder::GenerateCircuit()
142 {
143 GateRef glue = PtrArgument(0);
144 GateRef env = TaggedArgument(1);
145 GateRef argc = Int64Argument(2);
146 GateRef calltarget = TaggedArgument(3);
147 GateRef newtarget = TaggedArgument(4);
148 GateRef thisObj = TaggedArgument(5);
149 GateRef a = TaggedArgument(6);
150 GateRef b = TaggedArgument(7);
151 GateRef bindArguments = IntToTaggedInt(Int32(37));
152 GateRef bcOffset = Int32(1);
153 (void)calltarget;
154 GateRef numArgs = IntToTaggedInt(Int32(2));
155 GateRef barIndex = IntToTaggedInt(Int32(CommonStubCSigns::BarAOT));
156 GateRef barfunc = CallRuntime(glue, RTSTUB_ID(DefineAotFunc), {barIndex, numArgs});
157 GateRef bindfunc = CallRuntime(glue, RTSTUB_ID(GetBindFunc), {barfunc});
158 GateRef newjsfunc = CallNGCRuntime(glue, RTSTUB_ID(JSCall), {glue, env, Int64(5), bindfunc, newtarget, barfunc,
159 Int64(0x02), bindArguments, bcOffset});
160 GateRef result = CallNGCRuntime(glue, RTSTUB_ID(JSCall), {glue, env, argc, newjsfunc, newtarget, thisObj,
161 a, b, bcOffset});
162 Return(result);
163 }
164
GenerateCircuit()165 void FooProxyAOTStubBuilder::GenerateCircuit()
166 {
167 GateRef glue = PtrArgument(0);
168 GateRef env = TaggedArgument(1);
169 GateRef argc = Int64Argument(2);
170 [[maybe_unused]] GateRef calltarget = TaggedArgument(3);
171 GateRef newtarget = TaggedArgument(4);
172 GateRef thisObj = TaggedArgument(5);
173 GateRef a = TaggedArgument(6);
174 GateRef b = TaggedArgument(7);
175 GateRef bcOffset = Int32(1);
176
177 GateRef barIndex = IntToTaggedInt(Int32(CommonStubCSigns::BarAOT));
178 GateRef numArgs = IntToTaggedInt(Int32(2));
179 GateRef barfunc = CallRuntime(glue, RTSTUB_ID(DefineAotFunc), {barIndex, numArgs});
180
181 GateRef proxyfunc = CallRuntime(glue, RTSTUB_ID(DefineProxyFunc), {barfunc});
182 GateRef result =
183 CallNGCRuntime(glue, RTSTUB_ID(JSCall), {glue, env, argc, proxyfunc, newtarget, thisObj, a, b, bcOffset});
184 Return(result);
185 }
186
GenerateCircuit()187 void FooProxy2AOTStubBuilder::GenerateCircuit()
188 {
189 GateRef glue = PtrArgument(0);
190 GateRef env = TaggedArgument(1);
191 GateRef argc = Int64Argument(2);
192 [[maybe_unused]] GateRef calltarget = TaggedArgument(3);
193 GateRef newtarget = TaggedArgument(4);
194 GateRef thisObj = TaggedArgument(5);
195 GateRef a = TaggedArgument(6);
196 GateRef b = TaggedArgument(7);
197 GateRef bcOffset = Int32(1);
198
199 GateRef barIndex = IntToTaggedInt(Int32(CommonStubCSigns::Bar2AOT));
200 GateRef numArgs = IntToTaggedInt(Int32(2));
201 GateRef barfunc = CallRuntime(glue, RTSTUB_ID(DefineAotFunc), {barIndex, numArgs});
202 GateRef proxyHandler = CallRuntime(glue, RTSTUB_ID(DefineProxyHandler), {barfunc});
203
204 GateRef proxyfunc = CallRuntime(glue, RTSTUB_ID(DefineProxyFunc2), {barfunc, proxyHandler});
205 GateRef result =
206 CallNGCRuntime(glue, RTSTUB_ID(JSCall), {glue, env, argc, proxyfunc, newtarget, thisObj, a, b, bcOffset});
207 Return(result);
208 }
209
GenerateCircuit()210 void Bar2AOTStubBuilder::GenerateCircuit()
211 {
212 [[maybe_unused]] GateRef glue = PtrArgument(0);
213 [[maybe_unused]] GateRef env = TaggedArgument(1);
214 [[maybe_unused]] GateRef argc = Int64Argument(2);
215 [[maybe_unused]] GateRef calltarget = TaggedArgument(3);
216 [[maybe_unused]] GateRef newtarget = TaggedArgument(4);
217 [[maybe_unused]] GateRef thisObj = TaggedArgument(5);
218 CallRuntime(glue, RTSTUB_ID(DumpTaggedType), {thisObj});
219 Return(thisObj);
220 }
221
GenerateCircuit()222 void TestAbsoluteAddressRelocationStubBuilder::GenerateCircuit()
223 {
224 auto env = GetEnvironment();
225 GateRef a = Int64Argument(0);
226 Label start(env);
227 Jump(&start);
228 Bind(&start);
229 GateRef globalValueC = RelocatableData(0xabc);
230 GateRef dummyValueC = Load(VariableType::INT64(), globalValueC);
231 GateRef result = ZExtInt1ToInt64(Int64Equal(a, dummyValueC));
232 Return(result);
233 }
234 #endif
235 } // namespace panda::ecmascript::kungfu
236