Disassembler.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include "decompiler/Disassembler.h"
  16. Disassembler::Disassembler(InstVec &insts) : insts_(insts), address_base_(0){}
  17. void Disassembler::Open(const char *filename){
  18. stream_ = std::make_unique<BinaryReader>(BinaryReader::ReadAll(filename));
  19. }
  20. void Disassembler::DoDumpDisassembly(std::ostream &output){
  21. InstIterator inst;
  22. for (inst = insts_.begin(); inst != insts_.end(); ++ inst) output << *inst << "\n";
  23. }
  24. void Disassembler::Disassemble() {
  25. if (insts_.empty()) {
  26. stream_->Seek(0);
  27. DoDisassemble();
  28. }
  29. }
  30. void Disassembler::DumpDisassembly(std::ostream &output){
  31. Disassemble();
  32. DoDumpDisassembly(output);
  33. }
  34. /**
  35. * NIB is probably for nibble.
  36. *
  37. * @param[in] v @todo Understand and document.
  38. * @return @todo Understand and document.
  39. */
  40. static inline unsigned int Nib2(unsigned int v){return (v & 0xF);}
  41. /**
  42. * NIB is probably for nibble.
  43. *
  44. * @param[in] v @todo Understand and document.
  45. * @return @todo Understand and document.
  46. */
  47. static inline unsigned int Nib1(unsigned int v){return (v >> 4) & 0xF;}
  48. void Disassembler::ReadParams(InstPtr inst, const char *types){
  49. // Handle [] blocks as working on an individual element (i.e a BYTE,WORD etc)
  50. // this syntax allows picking of nibbles and bit fields into their own parameters.
  51. while (*types){
  52. std::string type(types, 1);
  53. if (type == "N"){ // Read nibbles
  54. const uint8 byte = stream_->ReadU8();
  55. inst->AddParam(new IntValue(Nib1(byte), false));
  56. inst->AddParam(new IntValue(Nib2(byte), false));
  57. address_ ++;
  58. }
  59. else if (type == "U"){
  60. const uint8 byte = stream_->ReadU8();
  61. inst->AddParam(new IntValue((byte >> 5) & 0x7, false));
  62. inst->AddParam(new IntValue((byte & 0x1F), false));
  63. address_ ++;
  64. }
  65. else inst->AddParam(ReadParameter(inst, type));
  66. types ++;
  67. }
  68. }
  69. void Disassembler::ReadParams(
  70. InstPtr inst, const char *types, const std::vector<std::string>& params
  71. ){
  72. while (*types){
  73. std::string type(types, 1);
  74. if (type == "N") address_ ++;
  75. else if (type == "U") address_ ++;
  76. types++;
  77. }
  78. }
  79. ValuePtr Disassembler::ReadParameter(InstPtr inst, std::string type){
  80. ValuePtr ret_val = NULL;
  81. if (type == "b"){ // signed byte.
  82. ret_val = new IntValue(stream_->ReadS8(), true);
  83. address_++;
  84. }
  85. else if (type == "B"){ // unsigned byte.
  86. ret_val = new IntValue((uint32)stream_->ReadU8(), false);
  87. address_++;
  88. }
  89. else if (type == "s"){ // 16-bit signed integer (short), little-endian.
  90. ret_val = new IntValue(stream_->ReadS16(), true);
  91. address_ += 2;
  92. }
  93. else if (type == "w"){ // 16-bit unsigned integer (word), little-endian.
  94. ret_val = new IntValue((uint32)stream_->ReadU16(), false);
  95. address_ += 2;
  96. }
  97. else if (type == "i"){ // 32-bit signed integer (int), little-endian.
  98. ret_val = new IntValue(stream_->ReadS32(), true);
  99. address_ += 4;
  100. }
  101. else if (type == "d"){ // 32-bit unsigned integer (dword), little-endian.
  102. ret_val = new IntValue(stream_->ReadU32(), false);
  103. address_ += 4;
  104. }
  105. else throw UnknownOpcodeParameterException(type);
  106. return ret_val;
  107. }