• 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 {
17    VReg
18} from "../irnodes";
19import { PandaGen } from "../pandagen";
20import {
21    // expandBigInt,
22    // expandBoolean,
23    expandFalse,
24    // expandFunction,
25    expandGlobal,
26    expandHole,
27    expandInfinity,
28    expandNaN,
29    expandNull,
30    // expandNumber,
31    // expandObject,
32    // expandRegExp,
33    // expandString,
34    expandSymbol,
35    expandTrue,
36    expandUndefined,
37    expandFunc
38} from "./builtIn";
39import { expandLexEnv } from "./lexEnv";
40export enum CacheList {
41    MIN,
42    NaN = MIN,
43    HOLE,
44    FUNC, // load function
45    Infinity,
46    undefined,
47    Symbol,
48    Null,
49    Global,
50    LexEnv, // Lex Env must come before True and False, because LexEnv depends on True and False
51    True,
52    False,
53    MAX
54}
55let cacheExpandHandlers = new Map([
56    [CacheList.HOLE, expandHole],
57    [CacheList.NaN, expandNaN],
58    [CacheList.Infinity, expandInfinity],
59    [CacheList.undefined, expandUndefined],
60    [CacheList.Symbol, expandSymbol],
61    [CacheList.Null, expandNull],
62    [CacheList.Global, expandGlobal],
63    [CacheList.LexEnv, expandLexEnv],
64    [CacheList.True, expandTrue],
65    [CacheList.False, expandFalse],
66    [CacheList.FUNC, expandFunc],
67]);
68
69class CacheItem {
70    constructor(handler: Function) {
71        this.flag = false;
72        this.vreg = undefined;
73        this.expander = handler;
74    }
75    private flag: boolean;
76    private vreg: VReg | undefined;
77    private expander: Function;
78    isNeeded() {
79        return this.flag;
80    }
81    getCache(): VReg {
82        if (!this.flag || !this.vreg) {
83            this.flag = true;
84            this.vreg = new VReg();
85        }
86        return this.vreg;
87    }
88    getExpander() {
89        return this.expander;
90    }
91}
92
93export class VregisterCache {
94    private cache: CacheItem[] = [];
95    constructor() {
96        for (let i = CacheList.MIN; i < CacheList.MAX; ++i) {
97            let handler = cacheExpandHandlers.get(i);
98            if (!handler) {
99                throw new Error("invalid expand handler");
100            }
101            this.cache[i] = new CacheItem(handler);
102        }
103    }
104    getCache(index: CacheList) {
105        if (index < CacheList.MIN || index > CacheList.MAX) {
106            throw new Error("invalid builtin index");
107        }
108        return this.cache[index];
109    }
110}
111
112export function getVregisterCache(pandaGen: PandaGen, index: CacheList) {
113    let cache = pandaGen.getVregisterCache();
114    let cacheItem = cache.getCache(index);
115
116    return cacheItem.getCache();
117}