• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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
16import 'mocha';
17import * as ts from "typescript";
18import { CacheExpander } from "../../src/pass/cacheExpander";
19import {
20    SnippetCompiler
21} from "../utils/base";
22import { Pass } from "../../src/pass";
23import { RegAlloc } from "../../src/regAllocator";
24import { TypeChecker } from "../../src/typeChecker";
25import { CmdOptions } from "../../src/cmdOptions";
26import {
27    LiteralBuffer,
28    Literal
29} from "../../src/base/literal";
30import { VReg } from "../../src/irnodes";
31
32let options = {
33    outDir: '../tmp/build',
34    allowJs: true,
35    noEmitOnError: true,
36    noImplicitAny: true,
37    target: 2,
38    module: 5,
39    strictNullChecks: true,
40    skipLibCheck: true,
41    alwaysStrict: true
42}
43
44export function compileTsWithType(fileName: string): { literalBufferArray: LiteralBuffer[]; snippetCompiler: SnippetCompiler; } {
45
46    CmdOptions.parseUserCmd([""]);
47    let filePath = [fileName];
48    let program = ts.createProgram(filePath, options);
49    let sourceFile = program.getSourceFile(fileName);
50    let typeChecker = TypeChecker.getInstance();
51    typeChecker.setTypeChecker(program.getTypeChecker());
52    let passes: Pass[] = [
53        new CacheExpander(),
54        new RegAlloc()
55    ];
56    let snippetCompiler = new SnippetCompiler();
57    let literalBufferArray: Array<LiteralBuffer> = new Array<LiteralBuffer>();
58    snippetCompiler.compile(sourceFile!.getFullText(), passes, literalBufferArray);
59    return {
60        literalBufferArray: literalBufferArray,
61        snippetCompiler: snippetCompiler
62    }
63}
64
65export function createVRegTypePair(input: any): Map<string, number> {
66    let id2TypeIndex: Map<string, number> = new Map<string, number>();
67    for (let rol of input) {
68        id2TypeIndex.set(rol[0], rol[1]);
69    }
70    return id2TypeIndex;
71}
72
73export function compareVReg2Type(expectedMap: Map<string, number>, generated: VReg[]): boolean {
74    for (let vreg of generated) {
75        let name = vreg.getVariableName();
76        if (name === "4funcObj" || name === "4newTarget" || name === "this") {
77            continue;
78        }
79        let vregMarker = "#" + vreg.num + "#" + vreg.getVariableName();
80        if (!expectedMap.has(vregMarker)) {
81            console.log("verg not found: ", vregMarker);
82            return false;
83        }
84        if (expectedMap.get(vregMarker) != vreg.getTypeIndex()) {
85            console.log("Unmatched expected type with vreg type:");
86            console.log("vreg num:", vregMarker);
87            console.log("vreg type:", vreg.getTypeIndex());
88            console.log("expected type:", expectedMap.get(vregMarker));
89            return false;
90        }
91    }
92    return true;
93}
94
95export function createLiteralBufferArray(input: any): LiteralBuffer[] {
96    let literalBufferArray: Array<LiteralBuffer> = new Array<LiteralBuffer>();
97    for (let i = 0; i < input.length; i++) {
98        let buff = input[i];
99        let literalBuffer: LiteralBuffer = new LiteralBuffer();
100        for (let rol of buff) {
101            let literal = new Literal(rol[0], rol[1]);
102            literalBuffer.addLiterals(literal);
103        }
104        literalBuffer.setKey(`_${i}`);
105        literalBufferArray.push(literalBuffer);
106    }
107    return literalBufferArray;
108}
109
110function printLiteralLog(expected: Literal, generated: Literal): void {
111    console.log("expected literals:");
112    console.log(expected);
113    console.log("unmatched literals:");
114    console.log(generated);
115}
116
117export function compareLiteralBuffer(expected: Array<LiteralBuffer>, generated: Array<LiteralBuffer>): boolean {
118    let size = expected.length;
119    for (let j = 0; j < size; j++) {
120        let expectedLiterals = expected[j].getLiterals();
121        let generatedLiterals = generated[j].getLiterals();
122        if (expectedLiterals.length != generatedLiterals.length) {
123            console.log("length miss-match in literals ", j);
124            return false;
125        }
126        for (let i = 0; i < generatedLiterals.length; i++) {
127            if (expectedLiterals[i].getTag() != generatedLiterals[i].getTag()) {
128                printLiteralLog(expectedLiterals[i], generatedLiterals[i]);
129                return false;
130            }
131            if (expectedLiterals[i].getValue() != generatedLiterals[i].getValue()) {
132                printLiteralLog(expectedLiterals[i], generatedLiterals[i]);
133                return false;
134            }
135        }
136    }
137    return true;
138}