ada_output.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * Ada specifications generated by this assembler depend on the RP.PIO package,
  7. * available in rp2040_hal.
  8. *
  9. * https://github.com/JeremyGrosser/rp2040_hal
  10. * https://github.com/JeremyGrosser/pico_bsp
  11. * https://github.com/JeremyGrosser/pico_examples
  12. */
  13. #include <algorithm>
  14. #include <iostream>
  15. #include "output_format.h"
  16. #include "pio_disassembler.h"
  17. struct ada_output : public output_format {
  18. struct factory {
  19. factory() {
  20. output_format::add(new ada_output());
  21. }
  22. };
  23. ada_output() : output_format("ada") {}
  24. std::string get_description() override {
  25. return "Ada specification";
  26. }
  27. void output_symbols(FILE *out, const std::vector<compiled_source::symbol> &symbols) {
  28. int count = 0;
  29. for (const auto &s : symbols) {
  30. if (!s.is_label) {
  31. fprintf(out, "%s : constant := %d;\n", s.name.c_str(), s.value);
  32. count++;
  33. }
  34. }
  35. if (count) {
  36. fprintf(out, "\n");
  37. count = 0;
  38. }
  39. for (const auto &s : symbols) {
  40. if (s.is_label) {
  41. fprintf(out, " Offset_%s : constant := %d;\n", s.name.c_str(), s.value);
  42. count++;
  43. }
  44. }
  45. if (count) {
  46. fprintf(out, "\n");
  47. }
  48. }
  49. void ada_case(std::string &identifier) {
  50. for(std::string::size_type i = 0; i < identifier.size(); ++i) {
  51. if ((i == 0) || (identifier[i - 1] == '_')) {
  52. identifier[i] = toupper(identifier[i]);
  53. }
  54. }
  55. }
  56. void header(FILE *out, const std::string msg, const int indent) {
  57. const std::string dashes = std::string(msg.length() + 6, '-');
  58. const std::string indent_str= std::string(indent, ' ');
  59. fprintf(out, "%s%s\n", indent_str.c_str(), dashes.c_str());
  60. fprintf(out, "%s-- %s --\n", indent_str.c_str(), msg.c_str());
  61. fprintf(out, "%s%s\n", indent_str.c_str(), dashes.c_str());
  62. fprintf(out, "\n");
  63. }
  64. int output(std::string destination, std::vector<std::string> output_options,
  65. const compiled_source &source) override {
  66. for (const auto &program : source.programs) {
  67. for(const auto &p : program.lang_opts) {
  68. if (p.first.size() >= name.size() && p.first.compare(0, name.size(), name) == 0) {
  69. std::cerr << "warning: " << name << " does not support output options; " << p.first << " lang_opt ignored.\n";
  70. }
  71. }
  72. }
  73. std::string package_name;
  74. switch (output_options.size()) {
  75. case 0:
  76. std::cerr << "error: missing package name options for Ada format" << std::endl;
  77. return 1;
  78. case 1:
  79. package_name = output_options[0]; // Package name from command options
  80. break;
  81. default:
  82. std::cerr << "error: too many options for Ada format" << std::endl;
  83. return 1;
  84. }
  85. FILE *out = open_single_output(destination);
  86. if (!out) return 1;
  87. header(out, "This file is autogenerated by pioasm; do not edit!", 0);
  88. fprintf(out, "pragma Style_Checks (Off);\n\n");
  89. fprintf(out, "with RP.PIO;\n\n");
  90. fprintf(out, "package %s is\n", package_name.c_str());
  91. for (const auto &program : source.programs) {
  92. std::string trailing_comma = ", ";
  93. std::string prog_name= program.name;
  94. ada_case(prog_name);
  95. fprintf(out, "\n");
  96. header(out, prog_name, 3);
  97. output_symbols(out, source.global_symbols);
  98. fprintf(out, " %s_Wrap_Target : constant := %d;\n", prog_name.c_str(), program.wrap_target);
  99. fprintf(out, " %s_Wrap : constant := %d;\n", prog_name.c_str(), program.wrap);
  100. fprintf(out, "\n");
  101. output_symbols(out, program.symbols);
  102. fprintf(out, " %s_Program_Instructions : RP.PIO.Program := (\n", prog_name.c_str());
  103. for (int i = 0; i < (int)program.instructions.size(); i++) {
  104. const auto &inst = program.instructions[i];
  105. if (i == program.wrap_target) {
  106. fprintf(out, " -- .wrap_target\n");
  107. }
  108. if (i == (int)program.instructions.size() - 1) {
  109. trailing_comma = ");";
  110. }
  111. fprintf(out, " 16#%04x#%s -- %2d: %s\n", inst, trailing_comma.c_str(), i,
  112. disassemble(inst, program.sideset_bits_including_opt.get(), program.sideset_opt).c_str());
  113. if (i == program.wrap) {
  114. fprintf(out, " -- .wrap\n");
  115. }
  116. }
  117. }
  118. fprintf(out, "\n");
  119. fprintf(out, "end %s;\n", package_name.c_str());
  120. fclose(out);
  121. return 0;
  122. }
  123. };
  124. static ada_output::factory creator;