pio_assembler.cpp 15 KB

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