• 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    loadLexicalEnv,
24    newLexicalEnv,
25    storeAccumulator
26} from "./bcGenUtil";
27import { CacheList, getVregisterCache } from "./vregisterCache";
28
29function createLexEnv(pandaGen: PandaGen, scope: VariableScope): IRNode[] {
30    let lexEnvVars = scope.getNumLexEnv();
31    let insns: IRNode[] = [];
32    let scopeInfoIdx: number | undefined = undefined;
33    let lexVarInfo = scope.getLexVarInfo();
34    if (CmdOptions.isDebugMode()) {
35        scopeInfoIdx = pandaGen.appendScopeInfo(lexVarInfo);
36    }
37
38    insns.push(
39        newLexicalEnv(lexEnvVars, scopeInfoIdx),
40        storeAccumulator(getVregisterCache(pandaGen, CacheList.LexEnv))
41    );
42
43    return insns;
44}
45
46function loadLexEnv(pandaGen: PandaGen): IRNode[] {
47    let insns: IRNode[] = [];
48
49    insns.push(
50        loadLexicalEnv(),
51        storeAccumulator(getVregisterCache(pandaGen, CacheList.LexEnv)),
52    );
53    return insns;
54}
55
56export function expandLexEnv(pandaGen: PandaGen): IRNode[] {
57    let scope = pandaGen.getScope()!.getNearestVariableScope();
58    let insns: IRNode[];
59
60    if (!scope) {
61        throw new Error("pandagen must have one variable scope");
62    }
63
64    if (scope.need2CreateLexEnv()) {
65        insns = createLexEnv(pandaGen, scope);
66    } else {
67        insns = loadLexEnv(pandaGen);
68    }
69
70    return insns;
71}
72