• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import { CmdOptions } from "../cmdOptions";
17import {
18    IRNode
19} from "../irnodes";
20import { PandaGen } from "../pandagen";
21import { VariableScope } from "../scope";
22import {
23    newLexicalEnv,
24    storeAccumulator
25} from "./bcGenUtil";
26import { CacheList, getVregisterCache } from "./vregisterCache";
27
28function createLexEnv(pandaGen: PandaGen, scope: VariableScope): IRNode[] {
29    let lexEnvVars = scope.getNumLexEnv();
30    let insns: IRNode[] = [];
31    let scopeInfoId: string | undefined = undefined;
32    let lexVarInfo = scope.getLexVarInfo();
33    if (CmdOptions.isDebugMode()) {
34        scopeInfoId = pandaGen.appendScopeInfo(lexVarInfo);
35    }
36
37    insns.push(
38        newLexicalEnv(lexEnvVars, scopeInfoId),
39        storeAccumulator(getVregisterCache(pandaGen, CacheList.LexEnv))
40    );
41
42    return insns;
43}
44
45export function expandLexEnv(pandaGen: PandaGen): IRNode[] {
46    let scope = pandaGen.getScope()!.getNearestVariableScope();
47    let insns: IRNode[];
48
49    if (!scope) {
50        throw new Error("pandagen must have one variable scope");
51    }
52
53    if (scope.need2CreateLexEnv()) {
54        insns = createLexEnv(pandaGen, scope);
55    }
56
57    return insns;
58}
59