1 /*
2 * Copyright (c) 2021-2024 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 "literals.h"
17
18 #include "compiler/core/pandagen.h"
19 #include "ir/base/templateElement.h"
20 #include "ir/expressions/taggedTemplateExpression.h"
21 #include "ir/expressions/templateLiteral.h"
22
23 namespace ark::es2panda::compiler {
GetTemplateObject(PandaGen * pg,const ir::TaggedTemplateExpression * lit)24 void Literals::GetTemplateObject(PandaGen *pg, const ir::TaggedTemplateExpression *lit)
25 {
26 RegScope rs(pg);
27 VReg templateArg = pg->AllocReg();
28 VReg indexReg = pg->AllocReg();
29 VReg rawArr = pg->AllocReg();
30 VReg cookedArr = pg->AllocReg();
31
32 const ir::TemplateLiteral *templateLit = lit->Quasi();
33
34 pg->CreateEmptyArray(templateLit);
35 pg->StoreAccumulator(templateLit, rawArr);
36
37 pg->CreateEmptyArray(templateLit);
38 pg->StoreAccumulator(templateLit, cookedArr);
39
40 size_t elemIndex = 0;
41
42 for (const auto *element : templateLit->Quasis()) {
43 pg->LoadAccumulatorInt(element, elemIndex);
44 pg->StoreAccumulator(element, indexReg);
45
46 pg->LoadAccumulatorString(element, element->Raw());
47 pg->StoreObjByValue(element, rawArr, indexReg);
48
49 pg->LoadAccumulatorString(element, element->Cooked());
50 pg->StoreObjByValue(element, cookedArr, indexReg);
51
52 elemIndex++;
53 }
54
55 pg->CreateEmptyArray(lit);
56 pg->StoreAccumulator(lit, templateArg);
57
58 elemIndex = 0;
59 pg->LoadAccumulatorInt(lit, elemIndex);
60 pg->StoreAccumulator(lit, indexReg);
61
62 pg->LoadAccumulator(lit, rawArr);
63 pg->StoreObjByValue(lit, templateArg, indexReg);
64
65 elemIndex++;
66 pg->LoadAccumulatorInt(lit, elemIndex);
67 pg->StoreAccumulator(lit, indexReg);
68
69 pg->LoadAccumulator(lit, cookedArr);
70 pg->StoreObjByValue(lit, templateArg, indexReg);
71
72 pg->GetTemplateObject(lit, templateArg);
73 }
74 } // namespace ark::es2panda::compiler
75