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 expect 18} from 'chai'; 19import 'mocha'; 20import { 21 Dec, 22 Inc, 23 Istrue, 24 Neg, 25 Not, 26 Returnundefined, 27 Sttoglobalrecord, 28 Tonumber, 29 Tryldglobalbyname, 30 Trystglobalbyname, 31 Imm, 32 Jeqz, 33 Jmp, 34 Label, 35 Lda, 36 Ldai, 37 Sta, 38 VReg, 39 IRNode 40} from "../../src/irnodes"; 41import { checkInstructions, compileMainSnippet } from "../utils/base"; 42import { creatAstFromSnippet } from "../utils/asthelper"; 43import { PandaGen } from '../../src/pandagen'; 44 45describe("PrefixOperationsTest", function () { 46 it('let i = 5; ++i', function () { 47 let insns = compileMainSnippet("let i = 5; let j = ++i"); 48 IRNode.pg = new PandaGen("foo", creatAstFromSnippet("let i = 5; let j = ++i"), 0, undefined); 49 50 let temp = new VReg(); 51 52 let expected = [ 53 new Ldai(new Imm(5)), 54 new Sttoglobalrecord(new Imm(0), 'i'), 55 new Tryldglobalbyname(new Imm(1), 'i'), 56 new Sta(temp), 57 new Lda(temp), 58 new Inc(new Imm(2)), 59 new Trystglobalbyname(new Imm(3), 'i'), 60 new Sttoglobalrecord(new Imm(4), 'j'), 61 new Returnundefined() 62 ]; 63 64 expect(checkInstructions(insns, expected)).to.be.true; 65 }); 66 67 it('let i = 5; --i', function () { 68 let insns = compileMainSnippet("let i = 5; let j = --i"); 69 IRNode.pg = new PandaGen("foo", creatAstFromSnippet("let i = 5; let j = --i"), 0, undefined); 70 71 let temp = new VReg(); 72 73 let expected = [ 74 new Ldai(new Imm(5)), 75 new Sttoglobalrecord(new Imm(0), 'i'), 76 new Tryldglobalbyname(new Imm(1), 'i'), 77 new Sta(temp), 78 new Lda(temp), 79 new Dec(new Imm(2)), 80 new Trystglobalbyname(new Imm(3), 'i'), 81 new Sttoglobalrecord(new Imm(4), 'j'), 82 new Returnundefined() 83 ]; 84 85 expect(checkInstructions(insns, expected)).to.be.true; 86 }); 87 88 it('let i = 5; let j = +i', function () { 89 let insns = compileMainSnippet("let i = 5; let j = +i"); 90 IRNode.pg = new PandaGen("foo", creatAstFromSnippet("let i = 5; let j = +i"), 0, undefined); 91 92 let temp = new VReg(); 93 94 let expected = [ 95 new Ldai(new Imm(5)), 96 new Sttoglobalrecord(new Imm(0), 'i'), 97 new Tryldglobalbyname(new Imm(1), 'i'), 98 new Sta(temp), 99 new Lda(temp), 100 new Tonumber(new Imm(2)), 101 new Sttoglobalrecord(new Imm(3), 'j'), 102 new Returnundefined() 103 ]; 104 105 expect(checkInstructions(insns, expected)).to.be.true; 106 }); 107 108 it('let i = 5; let j = -i', function () { 109 let insns = compileMainSnippet("let i = 5; let j = -i"); 110 IRNode.pg = new PandaGen("foo", creatAstFromSnippet("let i = 5; let j = -i"), 0, undefined); 111 112 let temp = new VReg(); 113 114 let expected = [ 115 new Ldai(new Imm(5)), 116 new Sttoglobalrecord(new Imm(0), 'i'), 117 new Tryldglobalbyname(new Imm(1), 'i'), 118 new Sta(temp), 119 new Lda(temp), 120 new Neg(new Imm(2)), 121 new Sttoglobalrecord(new Imm(3), 'j'), 122 new Returnundefined() 123 ]; 124 125 expect(checkInstructions(insns, expected)).to.be.true; 126 }); 127 128 it('let i = 5; let j = !i', function () { 129 let insns = compileMainSnippet("let i = 5; let j = !i"); 130 IRNode.pg = new PandaGen("foo", creatAstFromSnippet("let i = 5; let j = !i"), 0, undefined); 131 132 let preLabel = new Label(); 133 let postLabel = new Label(); 134 let expected = [ 135 new Ldai(new Imm(5)), 136 new Sttoglobalrecord(new Imm(0), 'i'), 137 new Tryldglobalbyname(new Imm(1), 'i'), 138 new Sta(new VReg()), 139 new Istrue(), 140 new Jeqz(preLabel), 141 new Lda(new VReg()), 142 new Jmp(postLabel), 143 preLabel, 144 new Lda(new VReg()), 145 postLabel, 146 new Sttoglobalrecord(new Imm(2), 'j'), 147 new Returnundefined() 148 ]; 149 expect(checkInstructions(insns, expected)).to.be.true; 150 }); 151 152 it('let i = 5; let j = ~i', function () { 153 let insns = compileMainSnippet("let i = 5; let j = ~i"); 154 IRNode.pg = new PandaGen("foo", creatAstFromSnippet("let i = 5; let j = ~i"), 0, undefined); 155 156 let tempI = new VReg(); 157 158 let expected = [ 159 new Ldai(new Imm(5)), 160 new Sttoglobalrecord(new Imm(0), 'i'), 161 new Tryldglobalbyname(new Imm(1), 'i'), 162 new Sta(tempI), 163 new Lda(tempI), 164 new Not(new Imm(2)), 165 new Sttoglobalrecord(new Imm(3), 'j'), 166 new Returnundefined() 167 ]; 168 169 expect(checkInstructions(insns, expected)).to.be.true; 170 }); 171});