pio_assembler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <cstdio>
  7. #include <iterator>
  8. #include "pio_assembler.h"
  9. #include "parser.hpp"
  10. #ifdef _MSC_VER
  11. #pragma warning(disable : 4996) // fopen
  12. #endif
  13. using syntax_error = yy::parser::syntax_error;
  14. std::string output_format::default_name = "c-sdk";
  15. pio_assembler::pio_assembler() {
  16. }
  17. int pio_assembler::generate(std::shared_ptr<output_format> _format, const std::string &_source,
  18. const std::string &_dest, const std::vector<std::string> &_options) {
  19. format = _format;
  20. source = _source;
  21. dest = _dest;
  22. options = _options;
  23. location.initialize(&source);
  24. scan_begin();
  25. yy::parser parse(*this);
  26. // parse.set_debug_level(false);
  27. int res = parse();
  28. scan_end();
  29. return res;
  30. }
  31. void program::add_instruction(std::shared_ptr<instruction> inst) {
  32. uint limit = MAX_INSTRUCTIONS;
  33. if (instructions.size() >= limit) {
  34. // todo take offset into account
  35. std::stringstream msg;
  36. msg << "program instruction limit of " << limit << " instruction(s) exceeded";
  37. throw syntax_error(inst->location, msg.str());
  38. }
  39. if (!sideset_opt && !inst->sideset) {
  40. std::stringstream msg;
  41. msg << "instruction requires 'side' to specify side set value for the instruction because non optional sideset was specified for the program at " << sideset.location;
  42. throw syntax_error(inst->location, msg.str());
  43. }
  44. instructions.push_back(inst);
  45. }
  46. using syntax_error = yy::parser::syntax_error;
  47. void program::add_symbol(std::shared_ptr<symbol> symbol) {
  48. const auto &existing = pioasm->get_symbol(symbol->name, this);
  49. if (existing) {
  50. std::stringstream msg;
  51. if (symbol->is_label != existing->is_label) {
  52. msg << "'" << symbol->name << "' was already defined as a " << (existing->is_label ? "label" : "value")
  53. << " at " << existing->location;
  54. } else if (symbol->is_label) {
  55. msg << "label '" << symbol->name << "' was already defined at " << existing->location;
  56. } else {
  57. msg << "'" << symbol->name << "' was already defined at " << existing->location;
  58. }
  59. throw syntax_error(symbol->location, msg.str());
  60. }
  61. symbols.insert(std::pair<std::string, std::shared_ptr<::symbol>>(symbol->name, symbol));
  62. ordered_symbols.push_back(symbol);
  63. }
  64. int resolvable::resolve(const program &program) {
  65. return resolve(program.pioasm, &program);
  66. }
  67. int unary_operation::resolve(pio_assembler *pioasm, const program *program, const resolvable &scope) {
  68. int value = arg->resolve(pioasm, program, scope);
  69. switch (op) {
  70. case negate:
  71. return -value;
  72. case reverse: {
  73. // slow is fine
  74. uint result = 0;
  75. for (uint i = 0; i < 32; i++) {
  76. result <<= 1u;
  77. if (value & 1u) {
  78. result |= 1u;
  79. }
  80. value >>= 1u;
  81. }
  82. return result;
  83. }
  84. default:
  85. throw syntax_error(location, "internal error");
  86. }
  87. }
  88. int binary_operation::resolve(pio_assembler *pioasm, const program *program, const resolvable &scope) {
  89. int lvalue = left->resolve(pioasm, program, scope);
  90. int rvalue = right->resolve(pioasm, program, scope);
  91. switch (op) {
  92. case add:
  93. return lvalue + rvalue;
  94. case subtract:
  95. return lvalue - rvalue;
  96. case multiply:
  97. return lvalue * rvalue;
  98. case divide:
  99. return lvalue / rvalue;
  100. case and_:
  101. return lvalue & rvalue;
  102. case or_:
  103. return lvalue | rvalue;
  104. case xor_:
  105. return lvalue ^ rvalue;
  106. default:
  107. throw syntax_error(location, "internal error");
  108. }
  109. }
  110. void program::set_wrap(const yy::location &l) {
  111. if (wrap) {
  112. std::stringstream msg;
  113. msg << ".wrap was already specified at " << wrap->location;
  114. throw syntax_error(l, msg.str());
  115. }
  116. if (instructions.empty()) {
  117. throw syntax_error(l, ".wrap cannot be placed before the first program instruction");
  118. }
  119. wrap = resolvable_int(l, instructions.size() - 1);
  120. }
  121. void program::set_wrap_target(const yy::location &l) {
  122. if (wrap_target) {
  123. std::stringstream msg;
  124. msg << ".wrap_target was already specified at " << wrap_target->location;
  125. throw syntax_error(l, msg.str());
  126. }
  127. wrap_target = resolvable_int(l, instructions.size());
  128. }
  129. void program::add_code_block(const code_block &block) {
  130. code_blocks[block.lang].push_back(block);
  131. }
  132. void program::add_lang_opt(std::string lang, std::string name, std::string value) {
  133. lang_opts[lang].emplace_back(name, value);
  134. }
  135. void program::finalize() {
  136. if (sideset.value) {
  137. int bits = sideset.value->resolve(*this);
  138. if (bits < 0) {
  139. throw syntax_error(sideset.value->location, "number of side set bits must be positive");
  140. }
  141. sideset_max = (1u << bits) - 1;
  142. if (sideset_opt) bits++;
  143. sideset_bits_including_opt = bits;
  144. if (bits > 5) {
  145. if (sideset_opt)
  146. throw syntax_error(sideset.value->location, "maximum number of side set bits with optional is 4");
  147. else
  148. throw syntax_error(sideset.value->location, "maximum number of side set bits is 5");
  149. }
  150. delay_max = (1u << (5 - bits)) - 1;
  151. } else {
  152. sideset_max = 0;
  153. delay_max = 31;
  154. }
  155. }
  156. int name_ref::resolve(pio_assembler *pioasm, const program *program, const resolvable &scope) {
  157. auto symbol = pioasm->get_symbol(name, program);
  158. if (symbol) {
  159. if (symbol->resolve_started) {
  160. std::stringstream msg;
  161. msg << "circular dependency in definition of '" << name << "'; detected at " << location << ")";
  162. throw syntax_error(scope.location, msg.str());
  163. }
  164. try {
  165. symbol->resolve_started++;
  166. int rc = symbol->value->resolve(pioasm, program, scope);
  167. symbol->resolve_started--;
  168. return rc;
  169. } catch (syntax_error &e) {
  170. symbol->resolve_started--;
  171. throw e;
  172. }
  173. } else {
  174. std::stringstream msg;
  175. msg << "undefined symbol '" << name << "'";
  176. throw syntax_error(location, msg.str());
  177. }
  178. }
  179. uint instruction::encode(const program &program) {
  180. raw_encoding raw = raw_encode(program);
  181. int _delay = delay->resolve(program);
  182. if (_delay < 0) {
  183. throw syntax_error(delay->location, "instruction delay must be positive");
  184. }
  185. if (_delay > program.delay_max) {
  186. if (program.delay_max == 31) {
  187. throw syntax_error(delay->location, "instruction delay must be <= 31");
  188. } else {
  189. std::stringstream msg;
  190. msg << "the instruction delay limit is " << program.delay_max << " because of the side set specified at "
  191. << program.sideset.location;
  192. throw syntax_error(delay->location, msg.str());
  193. }
  194. }
  195. int _sideset = 0;
  196. if (sideset) {
  197. _sideset = sideset->resolve(program);
  198. if (_sideset < 0) {
  199. throw syntax_error(sideset->location, "side set value must be >=0");
  200. }
  201. if (_sideset > program.sideset_max) {
  202. std::stringstream msg;
  203. msg << "the maximum side set value is " << program.sideset_max << " based on the configuration specified at "
  204. << program.sideset.location;
  205. throw syntax_error(sideset->location, msg.str());
  206. }
  207. _sideset <<= (5u - program.sideset_bits_including_opt);
  208. if (program.sideset_opt) {
  209. _sideset |= 0x10u;
  210. }
  211. }
  212. return (((uint) raw.type) << 13u) | (((uint) _delay | (uint) _sideset) << 8u) | (raw.arg1 << 5u) | raw.arg2;
  213. }
  214. raw_encoding instruction::raw_encode(const program &program) {
  215. throw syntax_error(location, "internal error");
  216. }
  217. uint instr_word::encode(const program &program) {
  218. uint value = encoding->resolve(program);
  219. if (value > 0xffffu) {
  220. throw syntax_error(location, ".word value must be a positive 16 bit value");
  221. }
  222. return value;
  223. }
  224. raw_encoding instr_jmp::raw_encode(const program &program) {
  225. int dest = target->resolve(program);
  226. if (dest < 0) {
  227. throw syntax_error(target->location, "jmp target address must be positive");
  228. } else if (dest >= (int)program.instructions.size()) {
  229. std::stringstream msg;
  230. msg << "jmp target address " << dest << " is beyond the end of the program";
  231. throw syntax_error(target->location, msg.str());
  232. }
  233. return {inst_type::jmp, (uint) cond, (uint) dest};
  234. }
  235. raw_encoding instr_in::raw_encode(const program &program) {
  236. int v = value->resolve(program);
  237. if (v < 1 || v > 32) {
  238. throw syntax_error(value->location, "'in' bit count must be >= 1 and <= 32");
  239. }
  240. return {inst_type::in, (uint) src, (uint) v & 0x1fu};
  241. }
  242. raw_encoding instr_out::raw_encode(const program &program) {
  243. int v = value->resolve(program);
  244. if (v < 1 || v > 32) {
  245. throw syntax_error(value->location, "'out' bit count must be >= 1 and <= 32");
  246. }
  247. return {inst_type::out, (uint) dest, (uint) v & 0x1fu};
  248. }
  249. raw_encoding instr_set::raw_encode(const program &program) {
  250. int v = value->resolve(program);
  251. if (v < 0 || v > 31) {
  252. throw syntax_error(value->location, "'set' bit count must be >= 0 and <= 31");
  253. }
  254. return {inst_type::set, (uint) dest, (uint) v};
  255. }
  256. raw_encoding instr_wait::raw_encode(const program &program) {
  257. uint pol = polarity->resolve(program);
  258. if (pol > 1) {
  259. throw syntax_error(polarity->location, "'wait' polarity must be 0 or 1");
  260. }
  261. uint arg2 = source->param->resolve(program);
  262. switch (source->target) {
  263. case wait_source::irq:
  264. if (arg2 > 7) throw syntax_error(source->param->location, "irq number must be must be >= 0 and <= 7");
  265. break;
  266. case wait_source::gpio:
  267. if (arg2 > 31)
  268. throw syntax_error(source->param->location, "absolute GPIO number must be must be >= 0 and <= 31");
  269. break;
  270. case wait_source::pin:
  271. if (arg2 > 31) throw syntax_error(polarity->location, "pin number must be must be >= 0 and <= 31");
  272. break;
  273. }
  274. return {inst_type::wait, (pol << 2u) | (uint) source->target, arg2 | (source->flag ? 0x10u : 0u)};
  275. }
  276. raw_encoding instr_irq::raw_encode(const program &program) {
  277. uint arg2 = num->resolve(program);
  278. if (arg2 > 7) throw syntax_error(num->location, "irq number must be must be >= 0 and <= 7");
  279. if (relative) arg2 |= 0x10u;
  280. return {inst_type::irq, (uint)modifiers, arg2};
  281. }
  282. std::vector<compiled_source::symbol> pio_assembler::public_symbols(program &program) {
  283. std::vector<std::shared_ptr<symbol>> public_symbols;
  284. std::remove_copy_if(program.ordered_symbols.begin(), program.ordered_symbols.end(),
  285. std::inserter(public_symbols, public_symbols.end()),
  286. [](const std::shared_ptr<symbol> &s) { return !s->is_public; });
  287. std::vector<compiled_source::symbol> rc;
  288. std::transform(public_symbols.begin(), public_symbols.end(), std::back_inserter(rc),
  289. [&](const std::shared_ptr<symbol> &s) {
  290. return compiled_source::symbol(s->name, s->value->resolve(program), s->is_label);
  291. });
  292. return rc;
  293. }
  294. int pio_assembler::write_output() {
  295. std::set<std::string> known_output_formats;
  296. std::transform(output_format::all().begin(), output_format::all().end(),
  297. std::inserter(known_output_formats, known_output_formats.begin()),
  298. [&](std::shared_ptr<output_format> &f) {
  299. return f->name;
  300. });
  301. compiled_source source;
  302. source.global_symbols = public_symbols(get_dummy_global_program());
  303. for (auto &program : programs) {
  304. program.finalize();
  305. source.programs.emplace_back(compiled_source::program(program.name));
  306. auto &cprogram = source.programs[source.programs.size() - 1];
  307. cprogram = compiled_source::program(program.name);
  308. // encode the instructions
  309. std::transform(program.instructions.begin(), program.instructions.end(),
  310. std::back_inserter(cprogram.instructions), [&](std::shared_ptr<instruction> &inst) {
  311. return inst->encode(program);
  312. });
  313. for (const auto &e : program.code_blocks) {
  314. bool ok = false;
  315. for(const auto &o : known_output_formats) {
  316. if (o == e.first || 0 == e.first.find(o+"-")) {
  317. ok = true;
  318. break;
  319. }
  320. }
  321. if (!ok) {
  322. std::cerr << e.second[0].location << ": warning, unknown code block output type '" << e.first << "'\n";
  323. known_output_formats.insert(e.first);
  324. }
  325. }
  326. if (program.wrap) cprogram.wrap = program.wrap->resolve(program); else cprogram.wrap = std::max((int)program.instructions.size() - 1, 0);
  327. if (program.wrap_target) cprogram.wrap_target = program.wrap_target->resolve(program); else cprogram.wrap_target = 0;
  328. if (program.origin.value) cprogram.origin = program.origin.value->resolve(program);
  329. if (program.sideset.value) {
  330. cprogram.sideset_bits_including_opt = program.sideset_bits_including_opt;
  331. cprogram.sideset_opt = program.sideset_opt;
  332. cprogram.sideset_pindirs = program.sideset_pindirs;
  333. }
  334. std::transform(program.code_blocks.begin(), program.code_blocks.end(), std::inserter(cprogram.code_blocks, cprogram.code_blocks.begin()), [](const std::pair<std::string, std::vector<code_block>>&e) {
  335. std::vector<std::string> blocks;
  336. std::transform(e.second.begin(), e.second.end(), std::back_inserter(blocks), [&](const code_block& block) {
  337. return block.contents;
  338. });
  339. return std::pair<std::string, std::vector<std::string>>(e.first, blocks);
  340. });
  341. cprogram.lang_opts = program.lang_opts;
  342. cprogram.symbols = public_symbols(program);
  343. }
  344. if (programs.empty()) {
  345. std::cout << "warning: input contained no programs" << std::endl;
  346. }
  347. return format->output(dest, options, source);
  348. }
  349. FILE *output_format::open_single_output(std::string destination) {
  350. FILE *out = destination == "-" ? stdout : fopen(destination.c_str(), "w");
  351. if (!out) {
  352. std::cerr << "Can't open output file '" << destination << "'" << std::endl;
  353. }
  354. return out;
  355. }