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 Add2, 22 Ashr2, 23 Div2, 24 Exp, 25 Mod2, 26 Mul2, 27 Returnundefined, 28 Shl2, 29 Shr2, 30 Sttoglobalrecord, 31 Sub2, 32 Tryldglobalbyname, 33 Trystglobalbyname, 34 Imm, 35 Ldai, 36 Sta, 37 VReg, 38 IRNode 39} from "../../src/irnodes"; 40import { checkInstructions, compileMainSnippet } from "../utils/base"; 41import { creatAstFromSnippet } from "../utils/asthelper" 42import { PandaGen } from '../../src/pandagen'; 43 44describe("OperationEqualTest", function () { 45 it("plusEqual", function () { 46 let insns = compileMainSnippet("let a = 2;\n" + 47 "a += 3;"); 48 let lhs = new VReg(); 49 IRNode.pg = new PandaGen("", creatAstFromSnippet("let a = 2;\n" + 50 "a += 3;"), 0, undefined); 51 52 let expected = [ 53 new Ldai(new Imm(2)), 54 new Sttoglobalrecord(new Imm(0), 'a'), 55 new Tryldglobalbyname(new Imm(1), 'a'), 56 new Sta(lhs), 57 new Ldai(new Imm(3)), 58 new Add2(new Imm(2), lhs), 59 new Trystglobalbyname(new Imm(3), 'a'), 60 new Returnundefined() 61 ]; 62 expect(checkInstructions(insns, expected)).to.be.true; 63 }); 64 65 it("minusEqual", function () { 66 let insns = compileMainSnippet("let a = 5;\n" + 67 "a -= 7;"); 68 let lhs = new VReg(); 69 IRNode.pg = new PandaGen("", creatAstFromSnippet("let a = 5;\n" + 70 "a -= 7;"), 0, undefined); 71 72 let expected = [ 73 new Ldai(new Imm(5)), 74 new Sttoglobalrecord(new Imm(0), 'a'), 75 new Tryldglobalbyname(new Imm(1), 'a'), 76 new Sta(lhs), 77 new Ldai(new Imm(7)), 78 new Sub2(new Imm(2), lhs), 79 new Trystglobalbyname(new Imm(3), 'a'), 80 new Returnundefined() 81 ]; 82 expect(checkInstructions(insns, expected)).to.be.true; 83 }); 84 85 it("asteriskEqual", function () { 86 let insns = compileMainSnippet("let a = 2;\n" + 87 "a *= 4;"); 88 let lhs = new VReg(); 89 IRNode.pg = new PandaGen("", creatAstFromSnippet("let a = 2;\n" + 90 "a *= 4;"), 0, undefined); 91 92 let expected = [ 93 new Ldai(new Imm(2)), 94 new Sttoglobalrecord(new Imm(0), 'a'), 95 new Tryldglobalbyname(new Imm(1), 'a'), 96 new Sta(lhs), 97 new Ldai(new Imm(4)), 98 new Mul2(new Imm(2), lhs), 99 new Trystglobalbyname(new Imm(3), 'a'), 100 new Returnundefined() 101 ]; 102 expect(checkInstructions(insns, expected)).to.be.true; 103 }); 104 105 it("AsteriskAsteriskEqualsToken", function () { 106 let insns = compileMainSnippet("let a = 2;\n" + 107 "a **= 3;"); 108 let lhs = new VReg(); 109 IRNode.pg = new PandaGen("", creatAstFromSnippet("let a = 2;\n" + 110 "a **= 3;"), 0, undefined); 111 112 let expected = [ 113 new Ldai(new Imm(2)), 114 new Sttoglobalrecord(new Imm(0), 'a'), 115 new Tryldglobalbyname(new Imm(1), 'a'), 116 new Sta(lhs), 117 new Ldai(new Imm(3)), 118 new Exp(new Imm(2), lhs), 119 new Trystglobalbyname(new Imm(3), 'a'), 120 new Returnundefined() 121 ] 122 expect(checkInstructions(insns, expected)).to.be.true; 123 }); 124 125 it("slashEqual", function () { 126 let insns = compileMainSnippet("let a = 5;\n" + 127 "a /= 3;"); 128 let lhs = new VReg(); 129 IRNode.pg = new PandaGen("", creatAstFromSnippet("let a = 5;\n" + 130 "a /= 3;"), 0, undefined); 131 132 let expected = [ 133 new Ldai(new Imm(5)), 134 new Sttoglobalrecord(new Imm(0), 'a'), 135 new Tryldglobalbyname(new Imm(1), 'a'), 136 new Sta(lhs), 137 new Ldai(new Imm(3)), 138 new Div2(new Imm(2), lhs), 139 new Trystglobalbyname(new Imm(3), 'a'), 140 new Returnundefined() 141 ]; 142 expect(checkInstructions(insns, expected)).to.be.true; 143 }); 144 145 it("percentEqual", function () { 146 let insns = compileMainSnippet("let a = 15;\n" + 147 "a %= 7;"); 148 let lhs = new VReg(); 149 IRNode.pg = new PandaGen("", creatAstFromSnippet("let a = 5;\n" + 150 "a /= 3;"), 0, undefined); 151 152 let expected = [ 153 new Ldai(new Imm(15)), 154 new Sttoglobalrecord(new Imm(0), 'a'), 155 new Tryldglobalbyname(new Imm(1), 'a'), 156 new Sta(lhs), 157 new Ldai(new Imm(7)), 158 new Mod2(new Imm(2), lhs), 159 new Trystglobalbyname(new Imm(3), 'a'), 160 new Returnundefined() 161 ]; 162 expect(checkInstructions(insns, expected)).to.be.true; 163 }); 164 165 it("lessThanLessThanEqual", function () { 166 let insns = compileMainSnippet("let a = 8;\n" + 167 "a <<= 3;"); 168 let lhs = new VReg(); 169 IRNode.pg = new PandaGen("", creatAstFromSnippet("let a = 8;\n" + 170 "a <<= 3;"), 0, undefined); 171 172 let expected = [ 173 new Ldai(new Imm(8)), 174 new Sttoglobalrecord(new Imm(0), 'a'), 175 new Tryldglobalbyname(new Imm(1), 'a'), 176 new Sta(lhs), 177 new Ldai(new Imm(3)), 178 new Shl2(new Imm(2), lhs), 179 new Trystglobalbyname(new Imm(3), 'a'), 180 new Returnundefined() 181 ]; 182 expect(checkInstructions(insns, expected)).to.be.true; 183 }); 184 185 it("greaterThanGreaterThanEqual", function () { 186 let insns = compileMainSnippet("let a = 4;\n" + 187 "a >>= 1;"); 188 let lhs = new VReg(); 189 IRNode.pg = new PandaGen("", creatAstFromSnippet("let a = 4;\n" + 190 "a >>= 1;"), 0, undefined); 191 192 let expected = [ 193 new Ldai(new Imm(4)), 194 new Sttoglobalrecord(new Imm(0), 'a'), 195 new Tryldglobalbyname(new Imm(1), 'a'), 196 new Sta(lhs), 197 new Ldai(new Imm(1)), 198 new Ashr2(new Imm(2), lhs), 199 new Trystglobalbyname(new Imm(3), 'a'), 200 new Returnundefined() 201 ]; 202 203 expect(checkInstructions(insns, expected)).to.be.true; 204 }); 205 206 it("greaterThanGreaterThanGreaterThanEqual", function () { 207 let insns = compileMainSnippet("let a = 8;\n" + 208 "a >>>= 2;"); 209 let lhs = new VReg(); 210 IRNode.pg = new PandaGen("", creatAstFromSnippet("let a = 8;\n" + 211 "a >>>= 2;"), 0, undefined); 212 213 let expected = [ 214 new Ldai(new Imm(8)), 215 new Sttoglobalrecord(new Imm(0), 'a'), 216 new Tryldglobalbyname(new Imm(1), 'a'), 217 new Sta(lhs), 218 new Ldai(new Imm(2)), 219 new Shr2(new Imm(2), lhs), 220 new Trystglobalbyname(new Imm(3), 'a'), 221 new Returnundefined() 222 ]; 223 224 expect(checkInstructions(insns, expected)).to.be.true; 225 }); 226}); 227