| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "courgette/encoded_program.h" | 5 #include "courgette/encoded_program.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "courgette/image_utils.h" | 14 #include "courgette/image_utils.h" |
| 15 #include "courgette/label_manager.h" | 15 #include "courgette/label_manager.h" |
| 16 #include "courgette/streams.h" | 16 #include "courgette/streams.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace courgette { | 19 namespace courgette { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Helper class to instantiate RVAToLabel while managing allocation. | 23 // Helper class to instantiate RVAToLabel while managing allocation. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 44 | 44 |
| 45 const std::vector<Label>& GetLabels() const { return labels_; } | 45 const std::vector<Label>& GetLabels() const { return labels_; } |
| 46 | 46 |
| 47 private: | 47 private: |
| 48 std::vector<Label> labels_; // Stores all Labels. | 48 std::vector<Label> labels_; // Stores all Labels. |
| 49 RVAToLabel label_map_; // Has pointers to |labels_| elements. | 49 RVAToLabel label_map_; // Has pointers to |labels_| elements. |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 // Creates a simple new program with given addresses. The orders of elements | 52 // Creates a simple new program with given addresses. The orders of elements |
| 53 // in |abs32_specs| and |rel32_specs| are important. | 53 // in |abs32_specs| and |rel32_specs| are important. |
| 54 scoped_ptr<EncodedProgram> CreateTestProgram(RVAToLabelMaker* abs32_maker, | 54 std::unique_ptr<EncodedProgram> CreateTestProgram( |
| 55 RVAToLabelMaker* rel32_maker) { | 55 RVAToLabelMaker* abs32_maker, |
| 56 RVAToLabelMaker* rel32_maker) { |
| 56 RVAToLabel* abs32_labels = abs32_maker->Make(); | 57 RVAToLabel* abs32_labels = abs32_maker->Make(); |
| 57 RVAToLabel* rel32_labels = rel32_maker->Make(); | 58 RVAToLabel* rel32_labels = rel32_maker->Make(); |
| 58 | 59 |
| 59 scoped_ptr<EncodedProgram> program(new EncodedProgram()); | 60 std::unique_ptr<EncodedProgram> program(new EncodedProgram()); |
| 60 | 61 |
| 61 uint32_t base = 0x00900000; | 62 uint32_t base = 0x00900000; |
| 62 program->set_image_base(base); | 63 program->set_image_base(base); |
| 63 | 64 |
| 64 EXPECT_TRUE(program->DefineLabels(*abs32_labels, *rel32_labels)); | 65 EXPECT_TRUE(program->DefineLabels(*abs32_labels, *rel32_labels)); |
| 65 | 66 |
| 66 EXPECT_TRUE(program->AddOrigin(0)); // Start at base. | 67 EXPECT_TRUE(program->AddOrigin(0)); // Start at base. |
| 67 | 68 |
| 68 // Arbitrary: Add instructions in the order they're defined in |*_maker|. | 69 // Arbitrary: Add instructions in the order they're defined in |*_maker|. |
| 69 for (const Label& label : abs32_maker->GetLabels()) | 70 for (const Label& label : abs32_maker->GetLabels()) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 89 // Create a simple program with a few addresses and references and | 90 // Create a simple program with a few addresses and references and |
| 90 // check that the bits produced are as expected. | 91 // check that the bits produced are as expected. |
| 91 TEST(EncodedProgramTest, Test) { | 92 TEST(EncodedProgramTest, Test) { |
| 92 // ABS32 index 7 <-- base + 4. | 93 // ABS32 index 7 <-- base + 4. |
| 93 RVAToLabelMaker abs32_label_maker; | 94 RVAToLabelMaker abs32_label_maker; |
| 94 abs32_label_maker.Add(7, 4); | 95 abs32_label_maker.Add(7, 4); |
| 95 // REL32 index 5 <-- base + 0. | 96 // REL32 index 5 <-- base + 0. |
| 96 RVAToLabelMaker rel32_label_maker; | 97 RVAToLabelMaker rel32_label_maker; |
| 97 rel32_label_maker.Add(5, 0); | 98 rel32_label_maker.Add(5, 0); |
| 98 | 99 |
| 99 scoped_ptr<EncodedProgram> program( | 100 std::unique_ptr<EncodedProgram> program( |
| 100 CreateTestProgram(&abs32_label_maker, &rel32_label_maker)); | 101 CreateTestProgram(&abs32_label_maker, &rel32_label_maker)); |
| 101 | 102 |
| 102 // Serialize and deserialize. | 103 // Serialize and deserialize. |
| 103 SinkStreamSet sinks; | 104 SinkStreamSet sinks; |
| 104 EXPECT_TRUE(program->WriteTo(&sinks)); | 105 EXPECT_TRUE(program->WriteTo(&sinks)); |
| 105 program.reset(); | 106 program.reset(); |
| 106 | 107 |
| 107 SinkStream sink; | 108 SinkStream sink; |
| 108 bool can_collect = sinks.CopyTo(&sink); | 109 bool can_collect = sinks.CopyTo(&sink); |
| 109 EXPECT_TRUE(can_collect); | 110 EXPECT_TRUE(can_collect); |
| 110 | 111 |
| 111 const void* buffer = sink.Buffer(); | 112 const void* buffer = sink.Buffer(); |
| 112 size_t length = sink.Length(); | 113 size_t length = sink.Length(); |
| 113 | 114 |
| 114 SourceStreamSet sources; | 115 SourceStreamSet sources; |
| 115 bool can_get_source_streams = sources.Init(buffer, length); | 116 bool can_get_source_streams = sources.Init(buffer, length); |
| 116 EXPECT_TRUE(can_get_source_streams); | 117 EXPECT_TRUE(can_get_source_streams); |
| 117 | 118 |
| 118 scoped_ptr<EncodedProgram> encoded2(new EncodedProgram()); | 119 std::unique_ptr<EncodedProgram> encoded2(new EncodedProgram()); |
| 119 bool can_read = encoded2->ReadFrom(&sources); | 120 bool can_read = encoded2->ReadFrom(&sources); |
| 120 EXPECT_TRUE(can_read); | 121 EXPECT_TRUE(can_read); |
| 121 | 122 |
| 122 // Finally, try to assemble. | 123 // Finally, try to assemble. |
| 123 SinkStream assembled; | 124 SinkStream assembled; |
| 124 bool can_assemble = encoded2->AssembleTo(&assembled); | 125 bool can_assemble = encoded2->AssembleTo(&assembled); |
| 125 EXPECT_TRUE(can_assemble); | 126 EXPECT_TRUE(can_assemble); |
| 126 encoded2.reset(); | 127 encoded2.reset(); |
| 127 | 128 |
| 128 const uint8_t golden[] = { | 129 const uint8_t golden[] = { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 141 RVAToLabelMaker abs32_label_maker; | 142 RVAToLabelMaker abs32_label_maker; |
| 142 abs32_label_maker.Add(7, 11); | 143 abs32_label_maker.Add(7, 11); |
| 143 abs32_label_maker.Add(3, 2); | 144 abs32_label_maker.Add(3, 2); |
| 144 abs32_label_maker.Add(5, 23); | 145 abs32_label_maker.Add(5, 23); |
| 145 // Relative addresses by index: [16, 7, _, 32]. | 146 // Relative addresses by index: [16, 7, _, 32]. |
| 146 RVAToLabelMaker rel32_label_maker; | 147 RVAToLabelMaker rel32_label_maker; |
| 147 rel32_label_maker.Add(0, 16); | 148 rel32_label_maker.Add(0, 16); |
| 148 rel32_label_maker.Add(3, 32); | 149 rel32_label_maker.Add(3, 32); |
| 149 rel32_label_maker.Add(1, 7); | 150 rel32_label_maker.Add(1, 7); |
| 150 | 151 |
| 151 scoped_ptr<EncodedProgram> program( | 152 std::unique_ptr<EncodedProgram> program( |
| 152 CreateTestProgram(&abs32_label_maker, &rel32_label_maker)); | 153 CreateTestProgram(&abs32_label_maker, &rel32_label_maker)); |
| 153 | 154 |
| 154 SinkStreamSet sinks; | 155 SinkStreamSet sinks; |
| 155 EXPECT_TRUE(program->WriteTo(&sinks)); | 156 EXPECT_TRUE(program->WriteTo(&sinks)); |
| 156 program.reset(); | 157 program.reset(); |
| 157 | 158 |
| 158 // Check indexes and addresses in sinks. | 159 // Check indexes and addresses in sinks. |
| 159 const uint8_t golden_abs32_indexes[] = { | 160 const uint8_t golden_abs32_indexes[] = { |
| 160 0x03, 0x07, 0x03, 0x05 // 3 indexes: [7, 3, 5]. | 161 0x03, 0x07, 0x03, 0x05 // 3 indexes: [7, 3, 5]. |
| 161 }; | 162 }; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 const uint8_t golden_rel32_addresses[] = { | 194 const uint8_t golden_rel32_addresses[] = { |
| 194 0x04, // 4 address deltas. | 195 0x04, // 4 address deltas. |
| 195 0x20, 0x11, 0x00, 0x32, | 196 0x20, 0x11, 0x00, 0x32, |
| 196 }; | 197 }; |
| 197 EXPECT_TRUE(CompareSink(golden_rel32_addresses, | 198 EXPECT_TRUE(CompareSink(golden_rel32_addresses, |
| 198 arraysize(golden_rel32_addresses), | 199 arraysize(golden_rel32_addresses), |
| 199 sinks.stream(kStreamRel32Addresses))); | 200 sinks.stream(kStreamRel32Addresses))); |
| 200 } | 201 } |
| 201 | 202 |
| 202 } // namespace courgette | 203 } // namespace courgette |
| OLD | NEW |