• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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/expressions/taggedTemplateExpression.h>
20 #include <ir/expressions/templateLiteral.h>
21 
22 namespace panda::es2panda::compiler {
23 
24 // Literals
25 
GetTemplateObject(PandaGen * pg,const ir::TaggedTemplateExpression * lit)26 void Literals::GetTemplateObject(PandaGen *pg, const ir::TaggedTemplateExpression *lit)
27 {
28     RegScope rs(pg);
29     VReg templateArg = pg->AllocReg();
30     VReg indexReg = pg->AllocReg();
31     VReg rawArr = pg->AllocReg();
32     VReg cookedArr = pg->AllocReg();
33 
34     const ir::TemplateLiteral *templateLit = lit->Quasi();
35 
36     pg->CreateEmptyArray(templateLit);
37     pg->StoreAccumulator(templateLit, rawArr);
38 
39     pg->CreateEmptyArray(templateLit);
40     pg->StoreAccumulator(templateLit, cookedArr);
41 
42     size_t elemIndex = 0;
43 
44     for (const auto *element : templateLit->Quasis()) {
45         pg->LoadAccumulatorInt(element, elemIndex);
46         pg->StoreAccumulator(element, indexReg);
47 
48         pg->LoadAccumulatorString(element, element->Raw());
49         pg->DefineFieldByValue(element, rawArr, indexReg);
50         // Generate ldundefined when element is escape error
51         if (element->EscapeError()) {
52             pg->LoadConst(element, compiler::Constant::JS_UNDEFINED);
53         } else {
54             pg->LoadAccumulatorString(element, element->Cooked());
55         }
56         pg->DefineFieldByValue(element, cookedArr, indexReg);
57 
58         elemIndex++;
59     }
60 
61     pg->CreateEmptyArray(lit);
62     pg->StoreAccumulator(lit, templateArg);
63 
64     elemIndex = 0;
65     pg->LoadAccumulatorInt(lit, elemIndex);
66     pg->StoreAccumulator(lit, indexReg);
67 
68     pg->LoadAccumulator(lit, rawArr);
69     pg->DefineFieldByValue(lit, templateArg, indexReg);
70 
71     elemIndex++;
72     pg->LoadAccumulatorInt(lit, elemIndex);
73     pg->StoreAccumulator(lit, indexReg);
74 
75     pg->LoadAccumulator(lit, cookedArr);
76     pg->DefineFieldByValue(lit, templateArg, indexReg);
77 
78     pg->GetTemplateObject(lit, templateArg);
79 }
80 
81 }  // namespace panda::es2panda::compiler
82