parser.yy 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. %skeleton "lalr1.cc" /* -*- C++ -*- */
  7. %require "3.4.2"
  8. %defines
  9. %define api.token.constructor
  10. %define api.value.type variant
  11. /*%define parse.assert*/
  12. %define api.location.file "location.h"
  13. %define parse.lac full
  14. /* define parse.trace*/
  15. %define parse.error verbose
  16. %no-lines
  17. %locations
  18. %code requires {
  19. #include <string>
  20. #include <fstream>
  21. #include <sstream>
  22. #include "pio_types.h"
  23. struct pio_assembler;
  24. #ifdef _MSC_VER
  25. #pragma warning(disable : 4065) // default only switch statement
  26. #endif
  27. }
  28. // The parsing context.
  29. %param { pio_assembler& pioasm }
  30. %code {
  31. #include "pio_assembler.h"
  32. #ifdef _MSC_VER
  33. #pragma warning(disable : 4244) // possible loss of data (valid warning, but there is a software check / missing cast)
  34. #endif
  35. }
  36. %define api.token.prefix {TOK_}
  37. %token
  38. END 0 "end of file"
  39. NEWLINE "end of line"
  40. COMMA ","
  41. COLON ":"
  42. LPAREN "("
  43. RPAREN ")"
  44. LBRACKET "["
  45. RBRACKET "]"
  46. PLUS "+"
  47. MINUS "-"
  48. MULTIPLY "*"
  49. DIVIDE "/"
  50. OR "|"
  51. AND "&"
  52. XOR "^"
  53. POST_DECREMENT "--"
  54. NOT_EQUAL "!="
  55. NOT "!"
  56. REVERSE "::"
  57. EQUAL "="
  58. PROGRAM ".program"
  59. WRAP_TARGET ".wrap_target"
  60. WRAP ".wrap"
  61. DEFINE ".define"
  62. SIDE_SET ".side_set"
  63. WORD ".word"
  64. ORIGIN ".origin"
  65. LANG_OPT ".lang_opt"
  66. JMP "jmp"
  67. WAIT "wait"
  68. IN "in"
  69. OUT "out"
  70. PUSH "push"
  71. PULL "pull"
  72. MOV "mov"
  73. IRQ "irq"
  74. SET "set"
  75. NOP "nop"
  76. PIN "pin"
  77. GPIO "gpio"
  78. OSRE "osre"
  79. PINS "pins"
  80. NULL "null"
  81. PINDIRS "pindirs"
  82. BLOCK "block"
  83. NOBLOCK "noblock"
  84. IFEMPTY "ifempty"
  85. IFFULL "iffull"
  86. NOWAIT "nowait"
  87. CLEAR "clear"
  88. REL "rel"
  89. X "x"
  90. Y "y"
  91. EXEC "exec"
  92. PC "pc"
  93. ISR "isr"
  94. OSR "osr"
  95. OPTIONAL "opt"
  96. SIDE "side"
  97. STATUS "status"
  98. PUBLIC "public"
  99. ;
  100. %token
  101. <std::string> ID "identifier"
  102. <std::string> STRING "string"
  103. <std::string> NON_WS "text"
  104. <std::string> CODE_BLOCK_START "code block"
  105. <std::string> CODE_BLOCK_CONTENTS "%}" // bit ugly but if there is no end this is what we will be missing
  106. <std::string> UNKNOWN_DIRECTIVE
  107. <int> INT "integer"
  108. ;
  109. %left REVERSE
  110. %left MULTIPLY DIVIDE
  111. %left PLUS MINUS
  112. %left AND OR XOR
  113. %printer { yyo << "..."; } <*>;
  114. %%
  115. file:
  116. lines END { if (pioasm.error_count || pioasm.write_output()) YYABORT; }
  117. ;
  118. lines:
  119. line
  120. | lines NEWLINE line;
  121. line:
  122. PROGRAM ID { if (!pioasm.add_program(@$, $2)) { std::stringstream msg; msg << "program " << $2 << " already exists"; error(@$, msg.str()); abort(); } }
  123. | directive
  124. | instruction { pioasm.get_current_program(@1, "instruction").add_instruction($1); }
  125. | label_decl instruction { auto &p = pioasm.get_current_program(@2, "instruction"); p.add_label($1); p.add_instruction($2); }
  126. | label_decl { pioasm.get_current_program(@1, "label").add_label($1); }
  127. | code_block
  128. | %empty
  129. | error { if (pioasm.error_count > 6) { std::cerr << "\ntoo many errors; aborting.\n"; YYABORT; } }
  130. ;
  131. code_block:
  132. CODE_BLOCK_START CODE_BLOCK_CONTENTS { std::string of = $1; if (of.empty()) of = output_format::default_name; pioasm.get_current_program(@$, "code block", false, false).add_code_block( code_block(@$, of, $2)); }
  133. %type <std::shared_ptr<symbol>> label_decl;
  134. label_decl:
  135. symbol_def COLON { $1->is_label = true; $$ = $1; }
  136. directive:
  137. DEFINE symbol_def expression { $2->is_label = false; $2->value = $3; pioasm.get_current_program(@1, ".define", false, false).add_symbol($2); }
  138. | ORIGIN value { pioasm.get_current_program(@1, ".origin", true).set_origin(@$, $2); }
  139. | SIDE_SET value OPTIONAL PINDIRS { pioasm.get_current_program(@1, ".side_set", true).set_sideset(@$, $2, true, true); }
  140. | SIDE_SET value OPTIONAL { pioasm.get_current_program(@1, ".side_set", true).set_sideset(@$, $2, true, false); }
  141. | SIDE_SET value PINDIRS { pioasm.get_current_program(@1, ".side_set", true).set_sideset(@$, $2, false, true); }
  142. | SIDE_SET value { pioasm.get_current_program(@1, ".side_set", true).set_sideset(@$, $2, false, false); }
  143. | WRAP_TARGET { pioasm.get_current_program(@1, ".wrap_target").set_wrap_target(@$); }
  144. | WRAP { pioasm.get_current_program(@1, ".wrap").set_wrap(@$); }
  145. | WORD value { pioasm.get_current_program(@1, "instruction").add_instruction(std::shared_ptr<instruction>(new instr_word(@$, $2))); }
  146. | LANG_OPT NON_WS NON_WS EQUAL INT { pioasm.get_current_program(@1, ".lang_opt").add_lang_opt($2, $3, std::to_string($5)); }
  147. | LANG_OPT NON_WS NON_WS EQUAL STRING { pioasm.get_current_program(@1, ".lang_opt").add_lang_opt($2, $3, $5); }
  148. | LANG_OPT NON_WS NON_WS EQUAL NON_WS { pioasm.get_current_program(@1, ".lang_opt").add_lang_opt($2, $3, $5); }
  149. | LANG_OPT error { error(@$, "expected format is .lang_opt language option_name = option_value"); }
  150. | UNKNOWN_DIRECTIVE { std::stringstream msg; msg << "unknown directive " << $1; throw syntax_error(@$, msg.str()); }
  151. ;
  152. /* value is a more limited top level expression... requiring parenthesis */
  153. %type <std::shared_ptr<resolvable>> value;
  154. value: INT { $$ = resolvable_int(@$, $1); }
  155. | ID { $$ = std::shared_ptr<resolvable>(new name_ref(@$, $1)); }
  156. | LPAREN expression RPAREN { $$ = $2; }
  157. %type <std::shared_ptr<resolvable>> expression;
  158. expression:
  159. value
  160. | expression PLUS expression { $$ = std::shared_ptr<binary_operation>(new binary_operation(@$, binary_operation::add, $1, $3)); }
  161. | expression MINUS expression { $$ = std::shared_ptr<binary_operation>(new binary_operation(@$, binary_operation::subtract, $1, $3)); }
  162. | expression MULTIPLY expression { $$ = std::shared_ptr<binary_operation>(new binary_operation(@$, binary_operation::multiply, $1, $3)); }
  163. | expression DIVIDE expression { $$ = std::shared_ptr<binary_operation>(new binary_operation(@$, binary_operation::divide, $1, $3)); }
  164. | expression OR expression { $$ = std::shared_ptr<binary_operation>(new binary_operation(@$, binary_operation::or_, $1, $3)); }
  165. | expression AND expression { $$ = std::shared_ptr<binary_operation>(new binary_operation(@$, binary_operation::and_, $1, $3)); }
  166. | expression XOR expression { $$ = std::shared_ptr<binary_operation>(new binary_operation(@$, binary_operation::xor_, $1, $3)); }
  167. | MINUS expression { $$ = std::shared_ptr<unary_operation>(new unary_operation(@$, unary_operation::negate, $2)); }
  168. | REVERSE expression { $$ = std::shared_ptr<unary_operation>(new unary_operation(@$, unary_operation::reverse, $2)); }
  169. %type <std::shared_ptr<instruction>> instruction;
  170. instruction:
  171. base_instruction sideset delay { $$ = $1; $$->sideset = $2; $$->delay = $3; }
  172. | base_instruction delay sideset { $$ = $1; $$->delay = $2; $$->sideset = $3; }
  173. | base_instruction sideset { $$ = $1; $$->sideset = $2; $$->delay = resolvable_int(@$, 0); }
  174. | base_instruction delay { $$ = $1; $$->delay = $2; }
  175. | base_instruction { $$ = $1; $$->delay = resolvable_int(@$, 0); }
  176. %type <std::shared_ptr<instruction>> base_instruction;
  177. base_instruction:
  178. NOP { $$ = std::shared_ptr<instruction>(new instr_nop(@$)); }
  179. | JMP condition comma expression { $$ = std::shared_ptr<instruction>(new instr_jmp(@$, $2, $4)); }
  180. | WAIT value wait_source { $$ = std::shared_ptr<instruction>(new instr_wait(@$, $2, $3)); }
  181. | WAIT value COMMA value { std::stringstream msg; location l; l.begin = @2.end; l.end = @3.end; msg << "expected irq, gpio or pin after the polarity value and before the \",\""; throw yy::parser::syntax_error(l, msg.str()); }
  182. | WAIT wait_source { $$ = std::shared_ptr<instruction>(new instr_wait(@$, resolvable_int(@$, 1), $2)); }
  183. | IN in_source comma value { $$ = std::shared_ptr<instruction>(new instr_in(@$, $2, $4)); }
  184. | OUT out_target comma value { $$ = std::shared_ptr<instruction>(new instr_out(@$, $2, $4)); }
  185. | PUSH if_full blocking { $$ = std::shared_ptr<instruction>(new instr_push(@$, $2, $3)); }
  186. | PULL if_empty blocking { $$ = std::shared_ptr<instruction>(new instr_pull(@$, $2, $3)); }
  187. | MOV mov_target comma mov_op mov_source { $$ = std::shared_ptr<instruction>(new instr_mov(@$, $2, $5, $4)); }
  188. | IRQ irq_modifiers value REL { $$ = std::shared_ptr<instruction>(new instr_irq(@$, $2, $3, true)); }
  189. | IRQ irq_modifiers value { $$ = std::shared_ptr<instruction>(new instr_irq(@$, $2, $3)); }
  190. | SET set_target comma value { $$ = std::shared_ptr<instruction>(new instr_set(@$, $2, $4)); }
  191. ;
  192. %type <std::shared_ptr<resolvable>> delay;
  193. delay:
  194. LBRACKET expression RBRACKET { $$ = $2; }
  195. %type <std::shared_ptr<resolvable>> sideset;
  196. sideset:
  197. SIDE value { $$ = $2; }
  198. %type <enum condition> condition;
  199. condition:
  200. NOT X { $$ = condition::xz; }
  201. | X POST_DECREMENT { $$ = condition::xnz__; }
  202. | NOT Y { $$ = condition::yz; }
  203. | Y POST_DECREMENT { $$ = condition::ynz__; }
  204. | X NOT_EQUAL Y { $$ = condition::xney; }
  205. | PIN { $$ = condition::pin; }
  206. | NOT OSRE { $$ = condition::osrez; }
  207. | %empty { $$ = condition::al; }
  208. %type <std::shared_ptr<wait_source>> wait_source;
  209. wait_source:
  210. IRQ comma value REL { $$ = std::shared_ptr<wait_source>(new wait_source(wait_source::irq, $3, true)); }
  211. | IRQ comma value { $$ = std::shared_ptr<wait_source>(new wait_source(wait_source::irq, $3, false)); }
  212. | GPIO comma value { $$ = std::shared_ptr<wait_source>(new wait_source(wait_source::gpio, $3)); }
  213. | PIN comma value { $$ = std::shared_ptr<wait_source>(new wait_source(wait_source::pin, $3)); }
  214. comma: COMMA | %empty /* not a huge fan of forcing commas */
  215. %type <enum in_out_set> in_source;
  216. in_source: PINS { $$ = in_out_set::in_out_set_pins; }
  217. | X { $$ = in_out_set::in_out_set_x; }
  218. | Y { $$ = in_out_set::in_out_set_y; }
  219. | NULL { $$ = in_out_set::in_out_null; }
  220. | ISR { $$ = in_out_set::in_out_isr; }
  221. | OSR { $$ = in_out_set::in_osr; }
  222. | STATUS { $$ = in_out_set::in_status; }
  223. %type <enum in_out_set> out_target;
  224. out_target: PINS { $$ = in_out_set::in_out_set_pins; }
  225. | X { $$ = in_out_set::in_out_set_x; }
  226. | Y { $$ = in_out_set::in_out_set_y; }
  227. | NULL { $$ = in_out_set::in_out_null; }
  228. | PINDIRS { $$ = in_out_set::in_out_set_pindirs; }
  229. | ISR { $$ = in_out_set::in_out_isr; }
  230. | PC { $$ = in_out_set::out_set_pc; }
  231. | EXEC { $$ = in_out_set::out_exec; }
  232. %type <enum mov> mov_target;
  233. mov_target: PINS { $$ = mov::pins; }
  234. | X { $$ = mov::x; }
  235. | Y { $$ = mov::y; }
  236. | EXEC { $$ = mov::exec; }
  237. | PC { $$ = mov::pc; }
  238. | ISR { $$ = mov::isr; }
  239. | OSR { $$ = mov::osr; }
  240. %type <enum mov> mov_source;
  241. mov_source: PINS { $$ = mov::pins; }
  242. | X { $$ = mov::x; }
  243. | Y { $$ = mov::y; }
  244. | NULL { $$ = mov::null; }
  245. | STATUS { $$ = mov::status; }
  246. | ISR { $$ = mov::isr; }
  247. | OSR { $$ = mov::osr; }
  248. %type <enum mov_op> mov_op;
  249. mov_op:
  250. NOT { $$ = mov_op::invert; }
  251. | REVERSE { $$ = mov_op::bit_reverse; }
  252. | %empty { $$ = mov_op::none; }
  253. %type <enum in_out_set> set_target;
  254. set_target:
  255. PINS { $$ = in_out_set::in_out_set_pins; }
  256. | X { $$ = in_out_set::in_out_set_x; }
  257. | Y { $$ = in_out_set::in_out_set_y; }
  258. | PINDIRS { $$ = in_out_set::in_out_set_pindirs; }
  259. %type <bool> if_full;
  260. if_full:
  261. IFFULL { $$ = true; }
  262. | %empty { $$ = false; }
  263. %type <bool> if_empty;
  264. if_empty:
  265. IFEMPTY { $$ = true; }
  266. | %empty { $$ = false; }
  267. %type <bool> blocking;
  268. blocking:
  269. BLOCK { $$ = true; }
  270. | NOBLOCK { $$ = false; }
  271. | %empty { $$ = true; }
  272. %type <enum irq> irq_modifiers;
  273. irq_modifiers:
  274. CLEAR { $$ = irq::clear; }
  275. | WAIT { $$ = irq::set_wait; }
  276. | NOWAIT { $$ = irq::set; }
  277. | SET { $$ = irq::set; }
  278. | %empty { $$ = irq::set; }
  279. %type <std::shared_ptr<symbol>> symbol_def;
  280. symbol_def:
  281. ID { $$ = std::shared_ptr<symbol>(new symbol(@$, $1)); }
  282. | PUBLIC ID { $$ = std::shared_ptr<symbol>(new symbol(@$, $2, true)); }
  283. | MULTIPLY ID { $$ = std::shared_ptr<symbol>(new symbol(@$, $2, true)); }
  284. %%
  285. void yy::parser::error(const location_type& l, const std::string& m)
  286. {
  287. if (l.begin.filename) {
  288. std::cerr << l << ": " << m << '\n';
  289. pioasm.error_count++;
  290. if (l.begin.line == l.end.line && *l.begin.filename == *l.end.filename) {
  291. std::ifstream file(l.begin.filename->c_str());
  292. std::string line;
  293. for(int i = 0; i < l.begin.line; ++i) {
  294. std::getline(file, line);
  295. }
  296. fprintf(stderr, "%5d | %s\n", l.begin.line, line.c_str());
  297. fprintf(stderr, "%5s | %*s", "", l.begin.column, "^");
  298. for (int i = l.begin.column; i < l.end.column - 1; i++) {
  299. putc ('~', stderr);
  300. }
  301. putc ('\n', stderr);
  302. }
  303. } else {
  304. std::cerr << m << '\n';
  305. }
  306. }