CompiledVirtualMachine.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #include "CompiledVirtualMachine.hpp"
  16. #include "Pcg32.hpp"
  17. #include "common.hpp"
  18. #include "instructions.hpp"
  19. #include <stdexcept>
  20. namespace RandomX {
  21. CompiledVirtualMachine::CompiledVirtualMachine(bool softAes) : VirtualMachine(softAes) {
  22. #if !defined(_M_X64) && !defined(__x86_64__)
  23. throw std::runtime_error("Compiled VM only supports x86-64 CPUs");
  24. #endif
  25. }
  26. void CompiledVirtualMachine::setDataset(dataset_t ds, bool lightClient) {
  27. if (lightClient) {
  28. throw std::runtime_error("Compiled VM does not support light-client mode");
  29. }
  30. VirtualMachine::setDataset(ds, lightClient);
  31. }
  32. void CompiledVirtualMachine::initializeProgram(const void* seed) {
  33. Pcg32 gen(seed);
  34. for (unsigned i = 0; i < sizeof(reg) / sizeof(Pcg32::result_type); ++i) {
  35. *(((uint32_t*)&reg) + i) = gen();
  36. }
  37. compiler.generateProgram(gen);
  38. mem.ma = (gen() ^ *(((uint32_t*)seed) + 4)) & ~7;
  39. mem.mx = *(((uint32_t*)seed) + 5);
  40. }
  41. void CompiledVirtualMachine::execute() {
  42. //executeProgram(reg, mem, scratchpad, readDataset);
  43. compiler.getProgramFunc()(reg, mem, scratchpad);
  44. #ifdef TRACE
  45. for (int32_t i = InstructionCount - 1; i >= 0; --i) {
  46. std::cout << std::hex << tracepad[i].u64 << std::endl;
  47. }
  48. #endif
  49. }
  50. }