emitinlinextensa.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2016 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdarg.h>
  30. #include <assert.h>
  31. #include "py/emit.h"
  32. #include "py/asmxtensa.h"
  33. #if MICROPY_EMIT_INLINE_XTENSA
  34. struct _emit_inline_asm_t {
  35. asm_xtensa_t as;
  36. uint16_t pass;
  37. mp_obj_t *error_slot;
  38. mp_uint_t max_num_labels;
  39. qstr *label_lookup;
  40. };
  41. STATIC void emit_inline_xtensa_error_msg(emit_inline_asm_t *emit, const char *msg) {
  42. *emit->error_slot = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg);
  43. }
  44. STATIC void emit_inline_xtensa_error_exc(emit_inline_asm_t *emit, mp_obj_t exc) {
  45. *emit->error_slot = exc;
  46. }
  47. emit_inline_asm_t *emit_inline_xtensa_new(mp_uint_t max_num_labels) {
  48. emit_inline_asm_t *emit = m_new_obj(emit_inline_asm_t);
  49. memset(&emit->as, 0, sizeof(emit->as));
  50. mp_asm_base_init(&emit->as.base, max_num_labels);
  51. emit->max_num_labels = max_num_labels;
  52. emit->label_lookup = m_new(qstr, max_num_labels);
  53. return emit;
  54. }
  55. void emit_inline_xtensa_free(emit_inline_asm_t *emit) {
  56. m_del(qstr, emit->label_lookup, emit->max_num_labels);
  57. mp_asm_base_deinit(&emit->as.base, false);
  58. m_del_obj(emit_inline_asm_t, emit);
  59. }
  60. STATIC void emit_inline_xtensa_start_pass(emit_inline_asm_t *emit, pass_kind_t pass, mp_obj_t *error_slot) {
  61. emit->pass = pass;
  62. emit->error_slot = error_slot;
  63. if (emit->pass == MP_PASS_CODE_SIZE) {
  64. memset(emit->label_lookup, 0, emit->max_num_labels * sizeof(qstr));
  65. }
  66. mp_asm_base_start_pass(&emit->as.base, pass == MP_PASS_EMIT ? MP_ASM_PASS_EMIT : MP_ASM_PASS_COMPUTE);
  67. asm_xtensa_entry(&emit->as, 0);
  68. }
  69. STATIC void emit_inline_xtensa_end_pass(emit_inline_asm_t *emit, mp_uint_t type_sig) {
  70. asm_xtensa_exit(&emit->as);
  71. asm_xtensa_end_pass(&emit->as);
  72. }
  73. STATIC mp_uint_t emit_inline_xtensa_count_params(emit_inline_asm_t *emit, mp_uint_t n_params, mp_parse_node_t *pn_params) {
  74. if (n_params > 4) {
  75. emit_inline_xtensa_error_msg(emit, "can only have up to 4 parameters to Xtensa assembly");
  76. return 0;
  77. }
  78. for (mp_uint_t i = 0; i < n_params; i++) {
  79. if (!MP_PARSE_NODE_IS_ID(pn_params[i])) {
  80. emit_inline_xtensa_error_msg(emit, "parameters must be registers in sequence a2 to a5");
  81. return 0;
  82. }
  83. const char *p = qstr_str(MP_PARSE_NODE_LEAF_ARG(pn_params[i]));
  84. if (!(strlen(p) == 2 && p[0] == 'a' && p[1] == '2' + i)) {
  85. emit_inline_xtensa_error_msg(emit, "parameters must be registers in sequence a2 to a5");
  86. return 0;
  87. }
  88. }
  89. return n_params;
  90. }
  91. STATIC bool emit_inline_xtensa_label(emit_inline_asm_t *emit, mp_uint_t label_num, qstr label_id) {
  92. assert(label_num < emit->max_num_labels);
  93. if (emit->pass == MP_PASS_CODE_SIZE) {
  94. // check for duplicate label on first pass
  95. for (uint i = 0; i < emit->max_num_labels; i++) {
  96. if (emit->label_lookup[i] == label_id) {
  97. return false;
  98. }
  99. }
  100. }
  101. emit->label_lookup[label_num] = label_id;
  102. mp_asm_base_label_assign(&emit->as.base, label_num);
  103. return true;
  104. }
  105. typedef struct _reg_name_t { byte reg; byte name[3]; } reg_name_t;
  106. STATIC const reg_name_t reg_name_table[] = {
  107. {0, "a0\0"},
  108. {1, "a1\0"},
  109. {2, "a2\0"},
  110. {3, "a3\0"},
  111. {4, "a4\0"},
  112. {5, "a5\0"},
  113. {6, "a6\0"},
  114. {7, "a7\0"},
  115. {8, "a8\0"},
  116. {9, "a9\0"},
  117. {10, "a10"},
  118. {11, "a11"},
  119. {12, "a12"},
  120. {13, "a13"},
  121. {14, "a14"},
  122. {15, "a15"},
  123. };
  124. // return empty string in case of error, so we can attempt to parse the string
  125. // without a special check if it was in fact a string
  126. STATIC const char *get_arg_str(mp_parse_node_t pn) {
  127. if (MP_PARSE_NODE_IS_ID(pn)) {
  128. qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
  129. return qstr_str(qst);
  130. } else {
  131. return "";
  132. }
  133. }
  134. STATIC mp_uint_t get_arg_reg(emit_inline_asm_t *emit, const char *op, mp_parse_node_t pn) {
  135. const char *reg_str = get_arg_str(pn);
  136. for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(reg_name_table); i++) {
  137. const reg_name_t *r = &reg_name_table[i];
  138. if (reg_str[0] == r->name[0]
  139. && reg_str[1] == r->name[1]
  140. && reg_str[2] == r->name[2]
  141. && (reg_str[2] == '\0' || reg_str[3] == '\0')) {
  142. return r->reg;
  143. }
  144. }
  145. emit_inline_xtensa_error_exc(emit,
  146. mp_obj_new_exception_msg_varg(&mp_type_SyntaxError,
  147. "'%s' expects a register", op));
  148. return 0;
  149. }
  150. STATIC uint32_t get_arg_i(emit_inline_asm_t *emit, const char *op, mp_parse_node_t pn, int min, int max) {
  151. mp_obj_t o;
  152. if (!mp_parse_node_get_int_maybe(pn, &o)) {
  153. emit_inline_xtensa_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "'%s' expects an integer", op));
  154. return 0;
  155. }
  156. uint32_t i = mp_obj_get_int_truncated(o);
  157. if (min != max && ((int)i < min || (int)i > max)) {
  158. emit_inline_xtensa_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "'%s' integer %d is not within range %d..%d", op, i, min, max));
  159. return 0;
  160. }
  161. return i;
  162. }
  163. STATIC int get_arg_label(emit_inline_asm_t *emit, const char *op, mp_parse_node_t pn) {
  164. if (!MP_PARSE_NODE_IS_ID(pn)) {
  165. emit_inline_xtensa_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "'%s' expects a label", op));
  166. return 0;
  167. }
  168. qstr label_qstr = MP_PARSE_NODE_LEAF_ARG(pn);
  169. for (uint i = 0; i < emit->max_num_labels; i++) {
  170. if (emit->label_lookup[i] == label_qstr) {
  171. return i;
  172. }
  173. }
  174. // only need to have the labels on the last pass
  175. if (emit->pass == MP_PASS_EMIT) {
  176. emit_inline_xtensa_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "label '%q' not defined", label_qstr));
  177. }
  178. return 0;
  179. }
  180. #define RRR (0)
  181. #define RRI8 (1)
  182. #define RRI8_B (2)
  183. typedef struct _opcode_table_3arg_t {
  184. uint16_t name; // actually a qstr, which should fit in 16 bits
  185. uint8_t type;
  186. uint8_t a0 : 4;
  187. uint8_t a1 : 4;
  188. } opcode_table_3arg_t;
  189. STATIC const opcode_table_3arg_t opcode_table_3arg[] = {
  190. // arithmetic opcodes: reg, reg, reg
  191. {MP_QSTR_and_, RRR, 0, 1},
  192. {MP_QSTR_or_, RRR, 0, 2},
  193. {MP_QSTR_xor, RRR, 0, 3},
  194. {MP_QSTR_add, RRR, 0, 8},
  195. {MP_QSTR_sub, RRR, 0, 12},
  196. {MP_QSTR_mull, RRR, 2, 8},
  197. // load/store/addi opcodes: reg, reg, imm
  198. // upper nibble of type encodes the range of the immediate arg
  199. {MP_QSTR_l8ui, RRI8 | 0x10, 2, 0},
  200. {MP_QSTR_l16ui, RRI8 | 0x30, 2, 1},
  201. {MP_QSTR_l32i, RRI8 | 0x50, 2, 2},
  202. {MP_QSTR_s8i, RRI8 | 0x10, 2, 4},
  203. {MP_QSTR_s16i, RRI8 | 0x30, 2, 5},
  204. {MP_QSTR_s32i, RRI8 | 0x50, 2, 6},
  205. {MP_QSTR_l16si, RRI8 | 0x30, 2, 9},
  206. {MP_QSTR_addi, RRI8 | 0x00, 2, 12},
  207. // branch opcodes: reg, reg, label
  208. {MP_QSTR_ball, RRI8_B, ASM_XTENSA_CC_ALL, 0},
  209. {MP_QSTR_bany, RRI8_B, ASM_XTENSA_CC_ANY, 0},
  210. {MP_QSTR_bbc, RRI8_B, ASM_XTENSA_CC_BC, 0},
  211. {MP_QSTR_bbs, RRI8_B, ASM_XTENSA_CC_BS, 0},
  212. {MP_QSTR_beq, RRI8_B, ASM_XTENSA_CC_EQ, 0},
  213. {MP_QSTR_bge, RRI8_B, ASM_XTENSA_CC_GE, 0},
  214. {MP_QSTR_bgeu, RRI8_B, ASM_XTENSA_CC_GEU, 0},
  215. {MP_QSTR_blt, RRI8_B, ASM_XTENSA_CC_LT, 0},
  216. {MP_QSTR_bnall, RRI8_B, ASM_XTENSA_CC_NALL, 0},
  217. {MP_QSTR_bne, RRI8_B, ASM_XTENSA_CC_NE, 0},
  218. {MP_QSTR_bnone, RRI8_B, ASM_XTENSA_CC_NONE, 0},
  219. };
  220. STATIC void emit_inline_xtensa_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_args, mp_parse_node_t *pn_args) {
  221. size_t op_len;
  222. const char *op_str = (const char*)qstr_data(op, &op_len);
  223. if (n_args == 0) {
  224. if (op == MP_QSTR_ret_n) {
  225. asm_xtensa_op_ret_n(&emit->as);
  226. } else {
  227. goto unknown_op;
  228. }
  229. } else if (n_args == 1) {
  230. if (op == MP_QSTR_callx0) {
  231. uint r0 = get_arg_reg(emit, op_str, pn_args[0]);
  232. asm_xtensa_op_callx0(&emit->as, r0);
  233. } else if (op == MP_QSTR_j) {
  234. int label = get_arg_label(emit, op_str, pn_args[0]);
  235. asm_xtensa_j_label(&emit->as, label);
  236. } else if (op == MP_QSTR_jx) {
  237. uint r0 = get_arg_reg(emit, op_str, pn_args[0]);
  238. asm_xtensa_op_jx(&emit->as, r0);
  239. } else {
  240. goto unknown_op;
  241. }
  242. } else if (n_args == 2) {
  243. uint r0 = get_arg_reg(emit, op_str, pn_args[0]);
  244. if (op == MP_QSTR_beqz) {
  245. int label = get_arg_label(emit, op_str, pn_args[1]);
  246. asm_xtensa_bccz_reg_label(&emit->as, ASM_XTENSA_CCZ_EQ, r0, label);
  247. } else if (op == MP_QSTR_bnez) {
  248. int label = get_arg_label(emit, op_str, pn_args[1]);
  249. asm_xtensa_bccz_reg_label(&emit->as, ASM_XTENSA_CCZ_NE, r0, label);
  250. } else if (op == MP_QSTR_mov || op == MP_QSTR_mov_n) {
  251. // we emit mov.n for both "mov" and "mov_n" opcodes
  252. uint r1 = get_arg_reg(emit, op_str, pn_args[1]);
  253. asm_xtensa_op_mov_n(&emit->as, r0, r1);
  254. } else if (op == MP_QSTR_movi) {
  255. // for convenience we emit l32r if the integer doesn't fit in movi
  256. uint32_t imm = get_arg_i(emit, op_str, pn_args[1], 0, 0);
  257. asm_xtensa_mov_reg_i32(&emit->as, r0, imm);
  258. } else {
  259. goto unknown_op;
  260. }
  261. } else if (n_args == 3) {
  262. // search table for 3 arg instructions
  263. for (uint i = 0; i < MP_ARRAY_SIZE(opcode_table_3arg); i++) {
  264. const opcode_table_3arg_t *o = &opcode_table_3arg[i];
  265. if (op == o->name) {
  266. uint r0 = get_arg_reg(emit, op_str, pn_args[0]);
  267. uint r1 = get_arg_reg(emit, op_str, pn_args[1]);
  268. if (o->type == RRR) {
  269. uint r2 = get_arg_reg(emit, op_str, pn_args[2]);
  270. asm_xtensa_op24(&emit->as, ASM_XTENSA_ENCODE_RRR(0, o->a0, o->a1, r0, r1, r2));
  271. } else if (o->type == RRI8_B) {
  272. int label = get_arg_label(emit, op_str, pn_args[2]);
  273. asm_xtensa_bcc_reg_reg_label(&emit->as, o->a0, r0, r1, label);
  274. } else {
  275. int shift, min, max;
  276. if ((o->type & 0xf0) == 0) {
  277. shift = 0;
  278. min = -128;
  279. max = 127;
  280. } else {
  281. shift = (o->type & 0xf0) >> 5;
  282. min = 0;
  283. max = 0xff << shift;
  284. }
  285. uint32_t imm = get_arg_i(emit, op_str, pn_args[2], min, max);
  286. asm_xtensa_op24(&emit->as, ASM_XTENSA_ENCODE_RRI8(o->a0, o->a1, r1, r0, (imm >> shift) & 0xff));
  287. }
  288. return;
  289. }
  290. }
  291. goto unknown_op;
  292. } else {
  293. goto unknown_op;
  294. }
  295. return;
  296. unknown_op:
  297. emit_inline_xtensa_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "unsupported Xtensa instruction '%s' with %d arguments", op_str, n_args));
  298. return;
  299. /*
  300. branch_not_in_range:
  301. emit_inline_xtensa_error_msg(emit, "branch not in range");
  302. return;
  303. */
  304. }
  305. const emit_inline_asm_method_table_t emit_inline_xtensa_method_table = {
  306. emit_inline_xtensa_start_pass,
  307. emit_inline_xtensa_end_pass,
  308. emit_inline_xtensa_count_params,
  309. emit_inline_xtensa_label,
  310. emit_inline_xtensa_op,
  311. };
  312. #endif // MICROPY_EMIT_INLINE_XTENSA