OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/assembly_program.h" | 5 #include "courgette/assembly_program.h" |
6 | 6 |
7 #include <memory.h> | 7 #include <memory.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 // Emits a single byte. | 83 // Emits a single byte. |
84 class ByteInstruction : public Instruction { | 84 class ByteInstruction : public Instruction { |
85 public: | 85 public: |
86 explicit ByteInstruction(uint8 value) : Instruction(DEFBYTE, value) {} | 86 explicit ByteInstruction(uint8 value) : Instruction(DEFBYTE, value) {} |
87 uint8 byte_value() const { return info_; } | 87 uint8 byte_value() const { return info_; } |
88 }; | 88 }; |
89 | 89 |
90 // Emits a single byte. | 90 // Emits a single byte. |
91 class BytesInstruction : public Instruction { | 91 class BytesInstruction : public Instruction { |
92 public: | 92 public: |
93 BytesInstruction(const uint8* values, uint32 len) | 93 BytesInstruction(const uint8* values, size_t len) |
94 : Instruction(DEFBYTES, 0), | 94 : Instruction(DEFBYTES, 0), |
95 values_(values), | 95 values_(values), |
96 len_(len) {} | 96 len_(len) {} |
97 const uint8* byte_values() const { return values_; } | 97 const uint8* byte_values() const { return values_; } |
98 uint32 len() const { return len_; } | 98 size_t len() const { return len_; } |
99 | 99 |
100 private: | 100 private: |
101 const uint8* values_; | 101 const uint8* values_; |
102 uint32 len_; | 102 size_t len_; |
103 }; | 103 }; |
104 | 104 |
105 // A ABS32 to REL32 instruction emits a reference to a label's address. | 105 // A ABS32 to REL32 instruction emits a reference to a label's address. |
106 class InstructionWithLabel : public Instruction { | 106 class InstructionWithLabel : public Instruction { |
107 public: | 107 public: |
108 InstructionWithLabel(OP op, Label* label) | 108 InstructionWithLabel(OP op, Label* label) |
109 : Instruction(op, 0), label_(label) { | 109 : Instruction(op, 0), label_(label) { |
110 if (label == NULL) NOTREACHED(); | 110 if (label == NULL) NOTREACHED(); |
111 } | 111 } |
112 Label* label() const { return label_; } | 112 Label* label() const { return label_; } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 | 172 |
173 CheckBool AssemblyProgram::EmitOriginInstruction(RVA rva) { | 173 CheckBool AssemblyProgram::EmitOriginInstruction(RVA rva) { |
174 return Emit(new(std::nothrow) OriginInstruction(rva)); | 174 return Emit(new(std::nothrow) OriginInstruction(rva)); |
175 } | 175 } |
176 | 176 |
177 CheckBool AssemblyProgram::EmitByteInstruction(uint8 byte) { | 177 CheckBool AssemblyProgram::EmitByteInstruction(uint8 byte) { |
178 return Emit(GetByteInstruction(byte)); | 178 return Emit(GetByteInstruction(byte)); |
179 } | 179 } |
180 | 180 |
181 CheckBool AssemblyProgram::EmitBytesInstruction(const uint8* values, | 181 CheckBool AssemblyProgram::EmitBytesInstruction(const uint8* values, |
182 uint32 len) { | 182 size_t len) { |
183 return Emit(new(std::nothrow) BytesInstruction(values, len)); | 183 return Emit(new(std::nothrow) BytesInstruction(values, len)); |
184 } | 184 } |
185 | 185 |
186 CheckBool AssemblyProgram::EmitRel32(Label* label) { | 186 CheckBool AssemblyProgram::EmitRel32(Label* label) { |
187 return Emit(new(std::nothrow) InstructionWithLabel(REL32, label)); | 187 return Emit(new(std::nothrow) InstructionWithLabel(REL32, label)); |
188 } | 188 } |
189 | 189 |
190 CheckBool AssemblyProgram::EmitRel32ARM(uint16 op, Label* label, | 190 CheckBool AssemblyProgram::EmitRel32ARM(uint16 op, Label* label, |
191 const uint8* arm_op, uint16 op_size) { | 191 const uint8* arm_op, uint16 op_size) { |
192 return Emit(new(std::nothrow) InstructionWithLabelARM(REL32ARM, op, label, | 192 return Emit(new(std::nothrow) InstructionWithLabelARM(REL32ARM, op, label, |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 } | 414 } |
415 case DEFBYTE: { | 415 case DEFBYTE: { |
416 uint8 b = static_cast<ByteInstruction*>(instruction)->byte_value(); | 416 uint8 b = static_cast<ByteInstruction*>(instruction)->byte_value(); |
417 if (!encoded->AddCopy(1, &b)) | 417 if (!encoded->AddCopy(1, &b)) |
418 return NULL; | 418 return NULL; |
419 break; | 419 break; |
420 } | 420 } |
421 case DEFBYTES: { | 421 case DEFBYTES: { |
422 const uint8* byte_values = | 422 const uint8* byte_values = |
423 static_cast<BytesInstruction*>(instruction)->byte_values(); | 423 static_cast<BytesInstruction*>(instruction)->byte_values(); |
424 uint32 len = static_cast<BytesInstruction*>(instruction)->len(); | 424 size_t len = static_cast<BytesInstruction*>(instruction)->len(); |
425 | 425 |
426 if (!encoded->AddCopy(len, byte_values)) | 426 if (!encoded->AddCopy(len, byte_values)) |
427 return NULL; | 427 return NULL; |
428 break; | 428 break; |
429 } | 429 } |
430 case REL32: { | 430 case REL32: { |
431 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); | 431 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); |
432 if (!encoded->AddRel32(label->index_)) | 432 if (!encoded->AddRel32(label->index_)) |
433 return NULL; | 433 return NULL; |
434 break; | 434 break; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 break; | 540 break; |
541 } | 541 } |
542 default: | 542 default: |
543 break; | 543 break; |
544 } | 544 } |
545 } | 545 } |
546 | 546 |
547 return true; | 547 return true; |
548 } | 548 } |
549 | 549 |
550 void AssemblyProgram::PrintLabelCounts(RVAToLabel* labels) { | |
551 for (RVAToLabel::const_iterator p = labels->begin(); p != labels->end(); | |
552 ++p) { | |
553 Label* current = p->second; | |
554 if (current->index_ != Label::kNoIndex) | |
555 printf("%d\n", current->count_); | |
556 } | |
557 } | |
558 | |
559 void AssemblyProgram::CountRel32ARM() { | |
560 PrintLabelCounts(&rel32_labels_); | |
561 } | |
562 | |
563 //////////////////////////////////////////////////////////////////////////////// | 550 //////////////////////////////////////////////////////////////////////////////// |
564 | 551 |
565 Status TrimLabels(AssemblyProgram* program) { | 552 Status TrimLabels(AssemblyProgram* program) { |
566 if (program->TrimLabels()) | 553 if (program->TrimLabels()) |
567 return C_OK; | 554 return C_OK; |
568 else | 555 else |
569 return C_TRIM_FAILED; | 556 return C_TRIM_FAILED; |
570 } | 557 } |
571 | 558 |
572 Status Encode(AssemblyProgram* program, EncodedProgram** output) { | 559 Status Encode(AssemblyProgram* program, EncodedProgram** output) { |
573 *output = NULL; | 560 *output = NULL; |
574 EncodedProgram *encoded = program->Encode(); | 561 EncodedProgram *encoded = program->Encode(); |
575 if (encoded) { | 562 if (encoded) { |
576 *output = encoded; | 563 *output = encoded; |
577 return C_OK; | 564 return C_OK; |
578 } else { | 565 } else { |
579 return C_GENERAL_ERROR; | 566 return C_GENERAL_ERROR; |
580 } | 567 } |
581 } | 568 } |
582 | 569 |
583 } // namespace courgette | 570 } // namespace courgette |
OLD | NEW |