Karl Schimpf | 2e7daef | 2015-01-09 13:04:13 -0800 | [diff] [blame] | 1 | //===- unittest/IceParseInstsTest.cpp - test instruction errors -----------===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/ADT/STLExtras.h" |
| 11 | #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" |
| 12 | #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h" |
| 13 | |
| 14 | #include "BitcodeMunge.h" |
| 15 | |
| 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | static const uint64_t Terminator = 0x5768798008978675LL; |
| 23 | |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 24 | // Note: alignment stored as 0 or log2(Alignment)+1. |
| 25 | uint64_t getEncAlignPower(unsigned Power) { |
| 26 | return Power + 1; |
| 27 | } |
| 28 | uint64_t getEncAlignZero() { return 0; } |
| 29 | |
Karl Schimpf | 2e7daef | 2015-01-09 13:04:13 -0800 | [diff] [blame] | 30 | /// Test how we report a call arg that refers to nonexistent call argument |
| 31 | TEST(IceParseInstsTest, NonexistentCallArg) { |
| 32 | const uint64_t BitcodeRecords[] = { |
| 33 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::MODULE_BLOCK_ID, 2, Terminator, |
| 34 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::TYPE_BLOCK_ID_NEW, 2, Terminator, |
| 35 | 3, naclbitc::TYPE_CODE_NUMENTRY, 3, Terminator, |
| 36 | 3, naclbitc::TYPE_CODE_INTEGER, 32, Terminator, |
| 37 | 3, naclbitc::TYPE_CODE_VOID, Terminator, |
| 38 | 3, naclbitc::TYPE_CODE_FUNCTION, 0, 1, 0, 0, Terminator, |
| 39 | 0, naclbitc::BLK_CODE_EXIT, Terminator, |
| 40 | 3, naclbitc::MODULE_CODE_FUNCTION, 2, 0, 1, 0, Terminator, |
| 41 | 3, naclbitc::MODULE_CODE_FUNCTION, 2, 0, 0, 0, Terminator, |
| 42 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 2, Terminator, |
| 43 | 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator, |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 44 | // Note: 100 is a bad value index in next line. |
Karl Schimpf | 2e7daef | 2015-01-09 13:04:13 -0800 | [diff] [blame] | 45 | 3, naclbitc::FUNC_CODE_INST_CALL, 0, 4, 2, 100, Terminator, |
| 46 | 3, naclbitc::FUNC_CODE_INST_RET, Terminator, |
| 47 | 0, naclbitc::BLK_CODE_EXIT, Terminator, |
| 48 | 0, naclbitc::BLK_CODE_EXIT, Terminator |
| 49 | }; |
| 50 | |
Karl Schimpf | 2e7daef | 2015-01-09 13:04:13 -0800 | [diff] [blame] | 51 | // Show bitcode objdump for BitcodeRecords. |
| 52 | NaClObjDumpMunger DumpMunger(BitcodeRecords, |
| 53 | array_lengthof(BitcodeRecords), Terminator); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 54 | EXPECT_FALSE(DumpMunger.runTest("Nonexistent call arg")); |
| 55 | EXPECT_EQ(" 66:4| 3: <34, 0, 4, 2, 100> | call void @f0(i32 " |
| 56 | "%p0, i32 @f0);\n" |
| 57 | "Error(66:4): Invalid relative value id: 100 (Must be <= 4)\n", |
| 58 | DumpMunger.getLinesWithSubstring("66:4")); |
Karl Schimpf | 2e7daef | 2015-01-09 13:04:13 -0800 | [diff] [blame] | 59 | |
| 60 | // Show that we get appropriate error when parsing in Subzero. |
| 61 | IceTest::SubzeroBitcodeMunger Munger( |
| 62 | BitcodeRecords, array_lengthof(BitcodeRecords), Terminator); |
| 63 | EXPECT_FALSE(Munger.runTest("Nonexistent call arg")); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 64 | EXPECT_EQ("Error(66:4): Invalid function record: <34 0 4 2 100>\n", |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 65 | Munger.getTestResults()); |
Karl Schimpf | 2e7daef | 2015-01-09 13:04:13 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 68 | /// Test how we recognize alignments in alloca instructions. |
| 69 | TEST(IceParseInstsTests, AllocaAlignment) { |
| 70 | const uint64_t BitcodeRecords[] = { |
| 71 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::MODULE_BLOCK_ID, 2, Terminator, |
| 72 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::TYPE_BLOCK_ID_NEW, 2, Terminator, |
| 73 | 3, naclbitc::TYPE_CODE_NUMENTRY, 4, Terminator, |
| 74 | 3, naclbitc::TYPE_CODE_INTEGER, 32, Terminator, |
| 75 | 3, naclbitc::TYPE_CODE_VOID, Terminator, |
| 76 | 3, naclbitc::TYPE_CODE_FUNCTION, 0, 1, 0, Terminator, |
| 77 | 3, naclbitc::TYPE_CODE_INTEGER, 8, Terminator, |
| 78 | 0, naclbitc::BLK_CODE_EXIT, Terminator, |
| 79 | 3, naclbitc::MODULE_CODE_FUNCTION, 2, 0, 0, 0, Terminator, |
| 80 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 2, Terminator, |
| 81 | 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator, |
| 82 | 3, naclbitc::FUNC_CODE_INST_ALLOCA, 1, getEncAlignPower(0), Terminator, |
| 83 | 3, naclbitc::FUNC_CODE_INST_RET, Terminator, |
| 84 | 0, naclbitc::BLK_CODE_EXIT, Terminator, |
| 85 | 0, naclbitc::BLK_CODE_EXIT, Terminator}; |
| 86 | |
| 87 | const uint64_t ReplaceIndex = 11; // index for FUNC_CODE_INST_ALLOCA |
| 88 | |
| 89 | // Show text when alignment is 1. |
| 90 | NaClObjDumpMunger DumpMunger(BitcodeRecords, array_lengthof(BitcodeRecords), |
| 91 | Terminator); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 92 | EXPECT_TRUE(DumpMunger.runTest("Good alloca alignment 1")); |
| 93 | EXPECT_EQ(" 62:4| 3: <19, 1, 1> | %v0 = alloca i8, i32 " |
| 94 | "%p0, align 1;\n", |
| 95 | DumpMunger.getLinesWithSubstring("62:4")); |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 96 | |
| 97 | // Show that we can handle alignment of 1. |
| 98 | IceTest::SubzeroBitcodeMunger Munger( |
| 99 | BitcodeRecords, array_lengthof(BitcodeRecords), Terminator); |
| 100 | EXPECT_TRUE(Munger.runTest("Good alloca alignment 1")); |
| 101 | |
| 102 | // Show what happens when changing alignment to 0. |
| 103 | const uint64_t Align0[] = { |
| 104 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 105 | 3, naclbitc::FUNC_CODE_INST_ALLOCA, 1, getEncAlignZero(), Terminator, |
| 106 | }; |
| 107 | EXPECT_TRUE(Munger.runTest("Good alloca alignment 0", Align0, |
| 108 | array_lengthof(Align0))); |
| 109 | EXPECT_TRUE(DumpMunger.runTestForAssembly("Good alloca alignment 0", Align0, |
| 110 | array_lengthof(Align0))); |
| 111 | EXPECT_EQ(" %v0 = alloca i8, i32 %p0, align 0;\n", |
| 112 | DumpMunger.getLinesWithSubstring("alloca")); |
| 113 | |
| 114 | // Show what happens when changing alignment to 2**30. |
| 115 | const uint64_t Align30[] = { |
| 116 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 117 | 3, naclbitc::FUNC_CODE_INST_ALLOCA, 1, getEncAlignPower(30), Terminator, |
| 118 | }; |
| 119 | EXPECT_FALSE(Munger.runTest("Bad alloca alignment 30", Align30, |
| 120 | array_lengthof(Align30))); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 121 | EXPECT_EQ("Error(62:4): Invalid function record: <19 1 31>\n", |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 122 | Munger.getTestResults()); |
| 123 | |
| 124 | EXPECT_FALSE(DumpMunger.runTestForAssembly("Bad alloca alignment 30", Align30, |
| 125 | array_lengthof(Align30))); |
| 126 | EXPECT_EQ(" %v0 = alloca i8, i32 %p0, align 0;\n", |
| 127 | DumpMunger.getLinesWithSubstring("alloca")); |
| 128 | EXPECT_EQ( |
| 129 | "Error(62:4): Alignment can't be greater than 2**29. Found: 2**30\n", |
| 130 | DumpMunger.getLinesWithSubstring("Error")); |
| 131 | |
| 132 | // Show what happens when changing alignment to 2**29. |
| 133 | const uint64_t Align29[] = { |
| 134 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 135 | 3, naclbitc::FUNC_CODE_INST_ALLOCA, 1, getEncAlignPower(29), Terminator, |
| 136 | }; |
| 137 | EXPECT_TRUE(Munger.runTest("Good alloca alignment 29", Align29, |
| 138 | array_lengthof(Align29))); |
| 139 | EXPECT_TRUE(DumpMunger.runTestForAssembly("Good alloca alignment 29", Align29, |
| 140 | array_lengthof(Align29))); |
| 141 | EXPECT_EQ(" %v0 = alloca i8, i32 %p0, align 536870912;\n", |
| 142 | DumpMunger.getLinesWithSubstring("alloca")); |
| 143 | } |
| 144 | |
| 145 | // Test how we recognize alignments in load i32 instructions. |
| 146 | TEST(IceParseInstsTests, LoadI32Alignment) { |
| 147 | const uint64_t BitcodeRecords[] = { |
| 148 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::MODULE_BLOCK_ID, 2, Terminator, |
| 149 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::TYPE_BLOCK_ID_NEW, 2, Terminator, |
| 150 | 3, naclbitc::TYPE_CODE_NUMENTRY, 2, Terminator, |
| 151 | 3, naclbitc::TYPE_CODE_INTEGER, 32, Terminator, |
| 152 | 3, naclbitc::TYPE_CODE_FUNCTION, 0, 0, 0, Terminator, |
| 153 | 0, naclbitc::BLK_CODE_EXIT, Terminator, |
| 154 | 3, naclbitc::MODULE_CODE_FUNCTION, 1, 0, 0, 0, Terminator, |
| 155 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 2, Terminator, |
| 156 | 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator, |
| 157 | 3, naclbitc::FUNC_CODE_INST_LOAD, 1, getEncAlignPower(0), 0, Terminator, |
| 158 | 3, naclbitc::FUNC_CODE_INST_RET, 1, Terminator, |
| 159 | 0, naclbitc::BLK_CODE_EXIT, Terminator, |
| 160 | 0, naclbitc::BLK_CODE_EXIT, Terminator}; |
| 161 | |
| 162 | const uint64_t ReplaceIndex = 9; // index for FUNC_CODE_INST_LOAD |
| 163 | |
| 164 | // Show text when alignment is 1. |
| 165 | NaClObjDumpMunger DumpMunger(BitcodeRecords, array_lengthof(BitcodeRecords), |
| 166 | Terminator); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 167 | EXPECT_TRUE(DumpMunger.runTest("Good load i32 alignment 1")); |
| 168 | EXPECT_EQ(" 58:4| 3: <20, 1, 1, 0> | %v0 = load i32* %p0, " |
| 169 | "align 1;\n", |
| 170 | DumpMunger.getLinesWithSubstring("58:4")); |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 171 | IceTest::SubzeroBitcodeMunger Munger( |
| 172 | BitcodeRecords, array_lengthof(BitcodeRecords), Terminator); |
| 173 | EXPECT_TRUE(Munger.runTest("Good load i32 alignment 1")); |
| 174 | |
| 175 | // Show what happens when changing alignment to 0. |
| 176 | const uint64_t Align0[] = { |
| 177 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 178 | 3, naclbitc::FUNC_CODE_INST_LOAD, 1, getEncAlignZero(), 0, Terminator, |
| 179 | }; |
| 180 | EXPECT_FALSE(Munger.runTest("Bad load i32 alignment 0", Align0, |
| 181 | array_lengthof(Align0))); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 182 | EXPECT_EQ("Error(58:4): Invalid function record: <20 1 0 0>\n", |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 183 | Munger.getTestResults()); |
| 184 | EXPECT_FALSE(DumpMunger.runTestForAssembly("Bad load i32 alignment 0", Align0, |
| 185 | array_lengthof(Align0))); |
| 186 | EXPECT_EQ(" %v0 = load i32* %p0, align 0;\n" |
| 187 | "Error(58:4): load: Illegal alignment for i32. Expects: 1\n", |
| 188 | DumpMunger.getLinesWithSubstring("load")); |
| 189 | |
| 190 | // Show what happens when changing alignment to 4. |
| 191 | const uint64_t Align4[] = { |
| 192 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 193 | 3, naclbitc::FUNC_CODE_INST_LOAD, 1, getEncAlignPower(2), 0, Terminator, |
| 194 | }; |
| 195 | EXPECT_FALSE(Munger.runTest("Bad load i32 alignment 4", Align4, |
| 196 | array_lengthof(Align4))); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 197 | EXPECT_EQ("Error(58:4): Invalid function record: <20 1 3 0>\n", |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 198 | Munger.getTestResults()); |
| 199 | EXPECT_FALSE(DumpMunger.runTestForAssembly("Bad load i32 alignment 4", Align4, |
| 200 | array_lengthof(Align4))); |
| 201 | EXPECT_EQ(" %v0 = load i32* %p0, align 4;\n" |
| 202 | "Error(58:4): load: Illegal alignment for i32. Expects: 1\n", |
| 203 | DumpMunger.getLinesWithSubstring("load")); |
| 204 | |
| 205 | // Show what happens when changing alignment to 2**29. |
| 206 | const uint64_t Align29[] = { |
| 207 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 208 | 3, naclbitc::FUNC_CODE_INST_LOAD, 1, getEncAlignPower(29), 0, Terminator, |
| 209 | }; |
| 210 | EXPECT_FALSE(Munger.runTest("Bad load i32 alignment 29", Align29, |
| 211 | array_lengthof(Align29))); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 212 | EXPECT_EQ("Error(58:4): Invalid function record: <20 1 30 0>\n", |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 213 | Munger.getTestResults()); |
| 214 | EXPECT_FALSE(DumpMunger.runTestForAssembly("Bad load i32 alignment 29", |
| 215 | Align29, array_lengthof(Align29))); |
| 216 | EXPECT_EQ(" %v0 = load i32* %p0, align 536870912;\n" |
| 217 | "Error(58:4): load: Illegal alignment for i32. Expects: 1\n", |
| 218 | DumpMunger.getLinesWithSubstring("load")); |
| 219 | |
| 220 | // Show what happens when changing alignment to 2**30. |
| 221 | const uint64_t Align30[] = { |
| 222 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 223 | 3, naclbitc::FUNC_CODE_INST_LOAD, 1, getEncAlignPower(30), 0, Terminator, |
| 224 | }; |
| 225 | EXPECT_FALSE(Munger.runTest("Bad load i32 alignment 30", Align30, |
| 226 | array_lengthof(Align30))); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 227 | EXPECT_EQ("Error(58:4): Invalid function record: <20 1 31 0>\n", |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 228 | Munger.getTestResults()); |
| 229 | EXPECT_FALSE(DumpMunger.runTestForAssembly("Bad load i32 alignment 30", |
| 230 | Align30, array_lengthof(Align30))); |
| 231 | EXPECT_EQ(" %v0 = load i32* %p0, align 0;\n" |
| 232 | "Error(58:4): load: Illegal alignment for i32. Expects: 1\n", |
| 233 | DumpMunger.getLinesWithSubstring("load")); |
| 234 | } |
| 235 | |
| 236 | // Test how we recognize alignments in load float instructions. |
| 237 | TEST(IceParseInstsTests, LoadFloatAlignment) { |
| 238 | const uint64_t BitcodeRecords[] = { |
| 239 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::MODULE_BLOCK_ID, 2, Terminator, |
| 240 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::TYPE_BLOCK_ID_NEW, 2, Terminator, |
| 241 | 3, naclbitc::TYPE_CODE_NUMENTRY, 3, Terminator, |
| 242 | 3, naclbitc::TYPE_CODE_FLOAT, Terminator, |
| 243 | 3, naclbitc::TYPE_CODE_INTEGER, 32, Terminator, |
| 244 | 3, naclbitc::TYPE_CODE_FUNCTION, 0, 0, 1, Terminator, |
| 245 | 0, naclbitc::BLK_CODE_EXIT, Terminator, |
| 246 | 3, naclbitc::MODULE_CODE_FUNCTION, 2, 0, 0, 0, Terminator, |
| 247 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 2, Terminator, |
| 248 | 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator, |
| 249 | 3, naclbitc::FUNC_CODE_INST_LOAD, 1, getEncAlignPower(0), 0, Terminator, |
| 250 | 3, naclbitc::FUNC_CODE_INST_RET, 1, Terminator, |
| 251 | 0, naclbitc::BLK_CODE_EXIT, Terminator, |
| 252 | 0, naclbitc::BLK_CODE_EXIT, Terminator}; |
| 253 | |
| 254 | const uint64_t ReplaceIndex = 10; // index for FUNC_CODE_INST_LOAD |
| 255 | |
| 256 | // Show text when alignment is 1. |
| 257 | NaClObjDumpMunger DumpMunger(BitcodeRecords, array_lengthof(BitcodeRecords), |
| 258 | Terminator); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 259 | EXPECT_TRUE(DumpMunger.runTest("Good load float alignment 1")); |
| 260 | EXPECT_EQ(" 58:4| 3: <20, 1, 1, 0> | %v0 = load float* " |
| 261 | "%p0, align 1;\n", |
| 262 | DumpMunger.getLinesWithSubstring("58:4")); |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 263 | IceTest::SubzeroBitcodeMunger Munger( |
| 264 | BitcodeRecords, array_lengthof(BitcodeRecords), Terminator); |
| 265 | EXPECT_TRUE(Munger.runTest("Good load float alignment 1")); |
| 266 | |
| 267 | // Show what happens when changing alignment to 0. |
| 268 | const uint64_t Align0[] = { |
| 269 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 270 | 3, naclbitc::FUNC_CODE_INST_LOAD, 1, getEncAlignZero(), 0, Terminator, |
| 271 | }; |
| 272 | EXPECT_FALSE(Munger.runTest("Bad load float alignment 0", Align0, |
| 273 | array_lengthof(Align0))); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 274 | EXPECT_EQ("Error(58:4): Invalid function record: <20 1 0 0>\n", |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 275 | Munger.getTestResults()); |
| 276 | EXPECT_FALSE(DumpMunger.runTestForAssembly("Bad load float alignment 0", |
| 277 | Align0, array_lengthof(Align0))); |
| 278 | EXPECT_EQ( |
| 279 | " %v0 = load float* %p0, align 0;\n" |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 280 | "Error(58:4): load: Illegal alignment for float. Expects: 1 or 4\n", |
| 281 | DumpMunger.getLinesWithSubstring("load")); |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 282 | |
| 283 | // Show what happens when changing alignment to 4. |
| 284 | const uint64_t Align4[] = { |
| 285 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 286 | 3, naclbitc::FUNC_CODE_INST_LOAD, 1, getEncAlignPower(2), 0, Terminator, |
| 287 | }; |
| 288 | EXPECT_TRUE(Munger.runTest("Good load float alignment 4", Align4, |
| 289 | array_lengthof(Align4))); |
| 290 | EXPECT_TRUE(DumpMunger.runTestForAssembly("Good load float alignment 4", |
| 291 | Align4, array_lengthof(Align4))); |
| 292 | EXPECT_EQ(" %v0 = load float* %p0, align 4;\n", |
| 293 | DumpMunger.getLinesWithSubstring("load")); |
| 294 | |
| 295 | const uint64_t Align29[] = { |
| 296 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 297 | 3, naclbitc::FUNC_CODE_INST_LOAD, 1, getEncAlignPower(29), 0, Terminator, |
| 298 | }; |
| 299 | EXPECT_FALSE(Munger.runTest("Bad load float alignment 29", Align29, |
| 300 | array_lengthof(Align29))); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 301 | EXPECT_EQ("Error(58:4): Invalid function record: <20 1 30 0>\n", |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 302 | Munger.getTestResults()); |
| 303 | EXPECT_FALSE(DumpMunger.runTestForAssembly("Bad load float alignment 29", |
| 304 | Align29, array_lengthof(Align29))); |
| 305 | EXPECT_EQ( |
| 306 | " %v0 = load float* %p0, align 536870912;\n" |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 307 | "Error(58:4): load: Illegal alignment for float. Expects: 1 or 4\n", |
| 308 | DumpMunger.getLinesWithSubstring("load")); |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 309 | |
| 310 | // Show what happens when changing alignment to 2**30. |
| 311 | const uint64_t Align30[] = { |
| 312 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 313 | 3, naclbitc::FUNC_CODE_INST_LOAD, 1, getEncAlignPower(30), 0, Terminator, |
| 314 | }; |
| 315 | EXPECT_FALSE(Munger.runTest("Bad load float alignment 30", Align30, |
| 316 | array_lengthof(Align30))); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 317 | EXPECT_EQ("Error(58:4): Invalid function record: <20 1 31 0>\n", |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 318 | Munger.getTestResults()); |
| 319 | EXPECT_FALSE(DumpMunger.runTestForAssembly("Bad load float alignment 30", |
| 320 | Align30, array_lengthof(Align30))); |
| 321 | EXPECT_EQ( |
| 322 | " %v0 = load float* %p0, align 0;\n" |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 323 | "Error(58:4): load: Illegal alignment for float. Expects: 1 or 4\n", |
| 324 | DumpMunger.getLinesWithSubstring("load")); |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | // Test how we recognize alignments in store instructions. |
| 328 | TEST(NaClParseInstsTests, StoreAlignment) { |
| 329 | const uint64_t BitcodeRecords[] = { |
| 330 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::MODULE_BLOCK_ID, 2, Terminator, |
| 331 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::TYPE_BLOCK_ID_NEW, 2, Terminator, |
| 332 | 3, naclbitc::TYPE_CODE_NUMENTRY, 3, Terminator, |
| 333 | 3, naclbitc::TYPE_CODE_FLOAT, Terminator, |
| 334 | 3, naclbitc::TYPE_CODE_INTEGER, 32, Terminator, |
| 335 | 3, naclbitc::TYPE_CODE_FUNCTION, 0, 0, 1, 0, Terminator, |
| 336 | 0, naclbitc::BLK_CODE_EXIT, Terminator, |
| 337 | 3, naclbitc::MODULE_CODE_FUNCTION, 2, 0, 0, 0, Terminator, |
| 338 | 1, naclbitc::BLK_CODE_ENTER, naclbitc::FUNCTION_BLOCK_ID, 2, Terminator, |
| 339 | 3, naclbitc::FUNC_CODE_DECLAREBLOCKS, 1, Terminator, |
| 340 | 3, naclbitc::FUNC_CODE_INST_STORE, 2, 1, getEncAlignPower(0), Terminator, |
| 341 | 3, naclbitc::FUNC_CODE_INST_RET, 1, Terminator, |
| 342 | 0, naclbitc::BLK_CODE_EXIT, Terminator, |
| 343 | 0, naclbitc::BLK_CODE_EXIT, Terminator}; |
| 344 | |
| 345 | const uint64_t ReplaceIndex = 10; // index for FUNC_CODE_INST_STORE |
| 346 | |
| 347 | // Show text when alignment is 1. |
| 348 | NaClObjDumpMunger DumpMunger(BitcodeRecords, array_lengthof(BitcodeRecords), |
| 349 | Terminator); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 350 | EXPECT_TRUE(DumpMunger.runTest("Good Store Alignment 1")); |
| 351 | EXPECT_EQ(" 62:4| 3: <24, 2, 1, 1> | store float %p1, " |
| 352 | "float* %p0, \n", |
| 353 | DumpMunger.getLinesWithSubstring("62:4")); |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 354 | IceTest::SubzeroBitcodeMunger Munger( |
| 355 | BitcodeRecords, array_lengthof(BitcodeRecords), Terminator); |
| 356 | EXPECT_TRUE(Munger.runTest("Good store alignment")); |
| 357 | |
| 358 | // Show what happens when changing alignment to 0. |
| 359 | const uint64_t Align0[] = { |
| 360 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 361 | 3, naclbitc::FUNC_CODE_INST_STORE, 2, 1, getEncAlignZero(), Terminator, |
| 362 | }; |
| 363 | EXPECT_FALSE( |
| 364 | Munger.runTest("Bad store alignment 0", Align0, array_lengthof(Align0))); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 365 | EXPECT_EQ("Error(62:4): Invalid function record: <24 2 1 0>\n", |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 366 | Munger.getTestResults()); |
| 367 | EXPECT_FALSE(DumpMunger.runTestForAssembly("Bad store alignment 0", Align0, |
| 368 | array_lengthof(Align0))); |
| 369 | EXPECT_EQ( |
| 370 | " store float %p1, float* %p0, align 0;\n" |
| 371 | "Error(62:4): store: Illegal alignment for float. Expects: 1 or 4\n", |
| 372 | DumpMunger.getLinesWithSubstring("store")); |
| 373 | |
| 374 | // Show what happens when changing alignment to 4. |
| 375 | const uint64_t Align4[] = { |
| 376 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 377 | 3, naclbitc::FUNC_CODE_INST_STORE, 2, 1, getEncAlignPower(2), Terminator, |
| 378 | }; |
| 379 | EXPECT_TRUE( |
| 380 | Munger.runTest("Bad store alignment 4", Align4, array_lengthof(Align4))); |
| 381 | EXPECT_TRUE(DumpMunger.runTestForAssembly("Good store alignment 4", Align4, |
| 382 | array_lengthof(Align4))); |
| 383 | |
| 384 | // Show what happens when changing alignment to 8. |
| 385 | const uint64_t Align8[] = { |
| 386 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 387 | 3, naclbitc::FUNC_CODE_INST_STORE, 2, 1, getEncAlignPower(3), Terminator, |
| 388 | }; |
| 389 | EXPECT_FALSE( |
| 390 | Munger.runTest("Bad store alignment 8", Align8, array_lengthof(Align8))); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 391 | EXPECT_EQ("Error(62:4): Invalid function record: <24 2 1 4>\n", |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 392 | Munger.getTestResults()); |
| 393 | EXPECT_FALSE(DumpMunger.runTestForAssembly("Bad store alignment 8", Align8, |
| 394 | array_lengthof(Align8))); |
| 395 | EXPECT_EQ( |
| 396 | " store float %p1, float* %p0, align 8;\n" |
| 397 | "Error(62:4): store: Illegal alignment for float. Expects: 1 or 4\n", |
| 398 | DumpMunger.getLinesWithSubstring("store")); |
| 399 | |
| 400 | // Show what happens when changing alignment to 2**29. |
| 401 | const uint64_t Align29[] = { |
| 402 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 403 | 3, naclbitc::FUNC_CODE_INST_STORE, 2, 1, getEncAlignPower(29), Terminator, |
| 404 | }; |
| 405 | EXPECT_FALSE(Munger.runTest("Bad store alignment 29", Align29, |
| 406 | array_lengthof(Align29))); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 407 | EXPECT_EQ("Error(62:4): Invalid function record: <24 2 1 30>\n", |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 408 | Munger.getTestResults()); |
| 409 | EXPECT_FALSE(DumpMunger.runTestForAssembly("Bad store alignment 29", Align29, |
| 410 | array_lengthof(Align29))); |
| 411 | EXPECT_EQ( |
| 412 | " store float %p1, float* %p0, align 536870912;\n" |
| 413 | "Error(62:4): store: Illegal alignment for float. Expects: 1 or 4\n", |
| 414 | DumpMunger.getLinesWithSubstring("store")); |
| 415 | |
| 416 | const uint64_t Align30[] = { |
| 417 | ReplaceIndex, NaClBitcodeMunger::Replace, |
| 418 | // Note: alignment stored as 0 or log2(Alignment)+1. |
| 419 | 3, naclbitc::FUNC_CODE_INST_STORE, 2, 1, getEncAlignPower(30), Terminator, |
| 420 | }; |
| 421 | EXPECT_FALSE(Munger.runTest("Bad store alignment 30", Align30, |
| 422 | array_lengthof(Align30))); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame^] | 423 | EXPECT_EQ("Error(62:4): Invalid function record: <24 2 1 31>\n", |
Karl Schimpf | af238b2 | 2015-01-20 12:19:15 -0800 | [diff] [blame] | 424 | Munger.getTestResults()); |
| 425 | EXPECT_FALSE(DumpMunger.runTestForAssembly("Bad Store alignment 30", Align30, |
| 426 | array_lengthof(Align30))); |
| 427 | EXPECT_EQ( |
| 428 | " store float %p1, float* %p0, align 0;\n" |
| 429 | "Error(62:4): store: Illegal alignment for float. Expects: 1 or 4\n", |
| 430 | DumpMunger.getLinesWithSubstring("store")); |
| 431 | } |
| 432 | |
Karl Schimpf | 2e7daef | 2015-01-09 13:04:13 -0800 | [diff] [blame] | 433 | } // end of anonymous namespace |