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 <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | |
10 #include <algorithm> | 11 #include <algorithm> |
11 #include <map> | 12 #include <map> |
12 #include <set> | 13 #include <set> |
13 #include <sstream> | 14 #include <sstream> |
15 #include <utility> | |
grt (UTC plus 2)
2016/01/26 16:39:54
the four includes above this appear to be unused,
huangs
2016/01/26 18:42:24
Done.
| |
14 #include <vector> | 16 #include <vector> |
15 | 17 |
16 #include "base/logging.h" | 18 #include "base/logging.h" |
17 #include "base/macros.h" | 19 #include "base/macros.h" |
18 #include "base/memory/scoped_ptr.h" | |
19 | |
20 #include "courgette/courgette.h" | 20 #include "courgette/courgette.h" |
21 #include "courgette/encoded_program.h" | 21 #include "courgette/encoded_program.h" |
22 | 22 |
23 namespace courgette { | 23 namespace courgette { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 // Sets the current address for the emitting instructions. | 27 // Sets the current address for the emitting instructions. |
28 class OriginInstruction : public Instruction { | 28 class OriginInstruction : public Instruction { |
29 public: | 29 public: |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 ++index; | 358 ++index; |
359 ++fill_infill_count; | 359 ++fill_infill_count; |
360 } | 360 } |
361 } | 361 } |
362 | 362 |
363 VLOG(1) << " fill forward " << fill_forward_count | 363 VLOG(1) << " fill forward " << fill_forward_count |
364 << " backward " << fill_backward_count | 364 << " backward " << fill_backward_count |
365 << " infill " << fill_infill_count; | 365 << " infill " << fill_infill_count; |
366 } | 366 } |
367 | 367 |
368 EncodedProgram* AssemblyProgram::Encode() const { | 368 scoped_ptr<EncodedProgram> AssemblyProgram::Encode() const { |
369 scoped_ptr<EncodedProgram> encoded(new EncodedProgram()); | 369 scoped_ptr<EncodedProgram> encoded(new EncodedProgram()); |
370 | 370 |
371 encoded->set_image_base(image_base_); | 371 encoded->set_image_base(image_base_); |
372 | 372 |
373 if (!encoded->DefineLabels(abs32_labels_, rel32_labels_)) | 373 if (!encoded->DefineLabels(abs32_labels_, rel32_labels_)) |
374 return NULL; | 374 return nullptr; |
375 | 375 |
376 for (size_t i = 0; i < instructions_.size(); ++i) { | 376 for (size_t i = 0; i < instructions_.size(); ++i) { |
377 Instruction* instruction = instructions_[i]; | 377 Instruction* instruction = instructions_[i]; |
378 | 378 |
379 switch (instruction->op()) { | 379 switch (instruction->op()) { |
380 case ORIGIN: { | 380 case ORIGIN: { |
381 OriginInstruction* org = static_cast<OriginInstruction*>(instruction); | 381 OriginInstruction* org = static_cast<OriginInstruction*>(instruction); |
382 if (!encoded->AddOrigin(org->origin_rva())) | 382 if (!encoded->AddOrigin(org->origin_rva())) |
383 return NULL; | 383 return nullptr; |
384 break; | 384 break; |
385 } | 385 } |
386 case DEFBYTE: { | 386 case DEFBYTE: { |
387 uint8_t b = static_cast<ByteInstruction*>(instruction)->byte_value(); | 387 uint8_t b = static_cast<ByteInstruction*>(instruction)->byte_value(); |
388 if (!encoded->AddCopy(1, &b)) | 388 if (!encoded->AddCopy(1, &b)) |
389 return NULL; | 389 return nullptr; |
390 break; | 390 break; |
391 } | 391 } |
392 case DEFBYTES: { | 392 case DEFBYTES: { |
393 const uint8_t* byte_values = | 393 const uint8_t* byte_values = |
394 static_cast<BytesInstruction*>(instruction)->byte_values(); | 394 static_cast<BytesInstruction*>(instruction)->byte_values(); |
395 size_t len = static_cast<BytesInstruction*>(instruction)->len(); | 395 size_t len = static_cast<BytesInstruction*>(instruction)->len(); |
396 | 396 |
397 if (!encoded->AddCopy(len, byte_values)) | 397 if (!encoded->AddCopy(len, byte_values)) |
398 return NULL; | 398 return nullptr; |
399 break; | 399 break; |
400 } | 400 } |
401 case REL32: { | 401 case REL32: { |
402 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); | 402 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); |
403 if (!encoded->AddRel32(label->index_)) | 403 if (!encoded->AddRel32(label->index_)) |
404 return NULL; | 404 return nullptr; |
405 break; | 405 break; |
406 } | 406 } |
407 case REL32ARM: { | 407 case REL32ARM: { |
408 Label* label = | 408 Label* label = |
409 static_cast<InstructionWithLabelARM*>(instruction)->label(); | 409 static_cast<InstructionWithLabelARM*>(instruction)->label(); |
410 uint16_t compressed_op = | 410 uint16_t compressed_op = |
411 static_cast<InstructionWithLabelARM*>(instruction)->compressed_op(); | 411 static_cast<InstructionWithLabelARM*>(instruction)->compressed_op(); |
412 if (!encoded->AddRel32ARM(compressed_op, label->index_)) | 412 if (!encoded->AddRel32ARM(compressed_op, label->index_)) |
413 return NULL; | 413 return nullptr; |
414 break; | 414 break; |
415 } | 415 } |
416 case ABS32: { | 416 case ABS32: { |
417 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); | 417 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); |
418 if (!encoded->AddAbs32(label->index_)) | 418 if (!encoded->AddAbs32(label->index_)) |
419 return NULL; | 419 return nullptr; |
420 break; | 420 break; |
421 } | 421 } |
422 case ABS64: { | 422 case ABS64: { |
423 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); | 423 Label* label = static_cast<InstructionWithLabel*>(instruction)->label(); |
424 if (!encoded->AddAbs64(label->index_)) | 424 if (!encoded->AddAbs64(label->index_)) |
425 return NULL; | 425 return nullptr; |
426 break; | 426 break; |
427 } | 427 } |
428 case MAKEPERELOCS: { | 428 case MAKEPERELOCS: { |
429 if (!encoded->AddPeMakeRelocs(kind_)) | 429 if (!encoded->AddPeMakeRelocs(kind_)) |
430 return NULL; | 430 return nullptr; |
431 break; | 431 break; |
432 } | 432 } |
433 case MAKEELFRELOCS: { | 433 case MAKEELFRELOCS: { |
434 if (!encoded->AddElfMakeRelocs()) | 434 if (!encoded->AddElfMakeRelocs()) |
435 return NULL; | 435 return nullptr; |
436 break; | 436 break; |
437 } | 437 } |
438 case MAKEELFARMRELOCS: { | 438 case MAKEELFARMRELOCS: { |
439 if (!encoded->AddElfARMMakeRelocs()) | 439 if (!encoded->AddElfARMMakeRelocs()) |
440 return NULL; | 440 return nullptr; |
441 break; | 441 break; |
442 } | 442 } |
443 default: { | 443 default: { |
444 NOTREACHED() << "Unknown Insn OP kind"; | 444 NOTREACHED() << "Unknown Insn OP kind"; |
445 } | 445 } |
446 } | 446 } |
447 } | 447 } |
448 | 448 |
449 return encoded.release(); | 449 return encoded; |
450 } | 450 } |
451 | 451 |
452 Instruction* AssemblyProgram::GetByteInstruction(uint8_t byte) { | 452 Instruction* AssemblyProgram::GetByteInstruction(uint8_t byte) { |
453 if (!byte_instruction_cache_) { | 453 if (!byte_instruction_cache_) { |
454 Instruction** ram = nullptr; | 454 Instruction** ram = nullptr; |
455 if (!base::UncheckedMalloc(sizeof(Instruction*) * 256, | 455 if (!base::UncheckedMalloc(sizeof(Instruction*) * 256, |
456 reinterpret_cast<void**>(&ram))) { | 456 reinterpret_cast<void**>(&ram))) { |
457 return nullptr; | 457 return nullptr; |
458 } | 458 } |
459 byte_instruction_cache_.reset(ram); | 459 byte_instruction_cache_.reset(ram); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
523 } else { | 523 } else { |
524 ++it; | 524 ++it; |
525 } | 525 } |
526 } | 526 } |
527 | 527 |
528 return true; | 528 return true; |
529 } | 529 } |
530 | 530 |
531 //////////////////////////////////////////////////////////////////////////////// | 531 //////////////////////////////////////////////////////////////////////////////// |
532 | 532 |
533 Status Encode(AssemblyProgram* program, EncodedProgram** output) { | 533 Status Encode(const AssemblyProgram& program, |
grt (UTC plus 2)
2016/01/26 16:39:54
i don't see the point of this function since the r
huangs
2016/01/26 18:42:24
I think the intention of these weird wrappers is t
| |
534 *output = NULL; | 534 scoped_ptr<EncodedProgram>* output) { |
535 EncodedProgram *encoded = program->Encode(); | 535 output->reset(nullptr); |
grt (UTC plus 2)
2016/01/26 16:39:54
please document this subtlety
// Explicitly rele
huangs
2016/01/26 18:42:24
Done.
| |
536 if (encoded) { | 536 |
537 *output = encoded; | 537 scoped_ptr<EncodedProgram> encoded = program.Encode(); |
grt (UTC plus 2)
2016/01/26 16:39:54
this intermediate variable isn't needed:
*output
huangs
2016/01/26 18:42:24
Done.
| |
538 return C_OK; | 538 if (!encoded) |
539 } else { | |
540 return C_GENERAL_ERROR; | 539 return C_GENERAL_ERROR; |
541 } | 540 |
541 *output = std::move(encoded); | |
542 return C_OK; | |
542 } | 543 } |
543 | 544 |
544 } // namespace courgette | 545 } // namespace courgette |
OLD | NEW |