hex_output.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include "output_format.h"
  7. #include <iostream>
  8. struct hex_output : public output_format {
  9. struct factory {
  10. factory() {
  11. output_format::add(new hex_output());
  12. }
  13. };
  14. hex_output() : output_format("hex") {}
  15. std::string get_description() {
  16. return "Raw hex output (only valid for single program inputs)";
  17. }
  18. virtual int output(std::string destination, std::vector<std::string> output_options,
  19. const compiled_source &source) {
  20. FILE *out = open_single_output(destination);
  21. if (!out) return 1;
  22. if (source.programs.size() > 1) {
  23. // todo don't have locations any more!
  24. std::cerr << "error: hex output only supports a single program input\n";
  25. return 1;
  26. }
  27. for (const auto &i : source.programs[0].instructions) {
  28. fprintf(out, "%04x\n", i);
  29. }
  30. if (out != stdout) { fclose(out); }
  31. return 0;
  32. }
  33. };
  34. static hex_output::factory creator;