ulp_macro.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright 2010-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include "esp_attr.h"
  18. #include "esp_err.h"
  19. #include "esp_log.h"
  20. #include "esp32/ulp.h"
  21. #include "soc/soc.h"
  22. #include "soc/rtc_cntl_reg.h"
  23. #include "soc/sens_reg.h"
  24. #include "sdkconfig.h"
  25. static const char* TAG = "ulp";
  26. typedef struct {
  27. uint32_t label : 16;
  28. uint32_t addr : 11;
  29. uint32_t unused : 1;
  30. uint32_t type : 4;
  31. } reloc_info_t;
  32. #define RELOC_TYPE_LABEL 0
  33. #define RELOC_TYPE_BRANCH 1
  34. /* This record means: there is a label at address
  35. * insn_addr, with number label_num.
  36. */
  37. #define RELOC_INFO_LABEL(label_num, insn_addr) (reloc_info_t) { \
  38. .label = label_num, \
  39. .addr = insn_addr, \
  40. .unused = 0, \
  41. .type = RELOC_TYPE_LABEL }
  42. /* This record means: there is a branch instruction at
  43. * insn_addr, it needs to be changed to point to address
  44. * of label label_num.
  45. */
  46. #define RELOC_INFO_BRANCH(label_num, insn_addr) (reloc_info_t) { \
  47. .label = label_num, \
  48. .addr = insn_addr, \
  49. .unused = 0, \
  50. .type = RELOC_TYPE_BRANCH }
  51. /* Processing branch and label macros involves four steps:
  52. *
  53. * 1. Iterate over program and count all instructions
  54. * with "macro" opcode. Allocate relocations array
  55. * with number of entries equal to number of macro
  56. * instructions.
  57. *
  58. * 2. Remove all fake instructions with "macro" opcode
  59. * and record their locations into relocations array.
  60. * Removal is done using two pointers. Instructions
  61. * are read from read_ptr, and written to write_ptr.
  62. * When a macro instruction is encountered,
  63. * its contents are recorded into the appropriate
  64. * table, and then read_ptr is advanced again.
  65. * When a real instruction is encountered, it is
  66. * read via read_ptr and written to write_ptr.
  67. * In the end, all macro instructions are removed,
  68. * size of the program (expressed in words) is
  69. * reduced by the total number of macro instructions
  70. * which were present.
  71. *
  72. * 3. Sort relocations array by label number, and then
  73. * by type ("label" or "branch") if label numbers
  74. * match. This is done to simplify lookup on the next
  75. * step.
  76. *
  77. * 4. Iterate over entries of relocations table.
  78. * For each label number, label entry comes first
  79. * because the array was sorted at the previous step.
  80. * Label address is recorded, and all subsequent
  81. * "branch" entries which point to the same label number
  82. * are processed. For each branch entry, correct offset
  83. * or absolute address is calculated, depending on branch
  84. * type, and written into the appropriate field of
  85. * the instruction.
  86. *
  87. */
  88. static esp_err_t do_single_reloc(ulp_insn_t* program, uint32_t load_addr,
  89. reloc_info_t label_info, reloc_info_t branch_info)
  90. {
  91. size_t insn_offset = branch_info.addr - load_addr;
  92. ulp_insn_t* insn = &program[insn_offset];
  93. // B and BX have the same layout of opcode/sub_opcode fields,
  94. // and share the same opcode
  95. assert(insn->b.opcode == OPCODE_BRANCH
  96. && "branch macro was applied to a non-branch instruction");
  97. switch (insn->b.sub_opcode) {
  98. case SUB_OPCODE_B: {
  99. int32_t offset = ((int32_t) label_info.addr) - ((int32_t) branch_info.addr);
  100. uint32_t abs_offset = abs(offset);
  101. uint32_t sign = (offset >= 0) ? 0 : 1;
  102. if (abs_offset > 127) {
  103. ESP_LOGW(TAG, "target out of range: branch from %x to %x",
  104. branch_info.addr, label_info.addr);
  105. return ESP_ERR_ULP_BRANCH_OUT_OF_RANGE;
  106. }
  107. insn->b.offset = abs_offset;
  108. insn->b.sign = sign;
  109. break;
  110. }
  111. case SUB_OPCODE_BX: {
  112. assert(insn->bx.reg == 0 &&
  113. "relocation applied to a jump with offset in register");
  114. insn->bx.addr = label_info.addr;
  115. break;
  116. }
  117. default:
  118. assert(false && "unexpected sub-opcode");
  119. }
  120. return ESP_OK;
  121. }
  122. esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* program, size_t* psize)
  123. {
  124. const ulp_insn_t* read_ptr = program;
  125. const ulp_insn_t* end = program + *psize;
  126. size_t macro_count = 0;
  127. // step 1: calculate number of macros
  128. while (read_ptr < end) {
  129. ulp_insn_t r_insn = *read_ptr;
  130. if (r_insn.macro.opcode == OPCODE_MACRO) {
  131. ++macro_count;
  132. }
  133. ++read_ptr;
  134. }
  135. size_t real_program_size = *psize - macro_count;
  136. const size_t ulp_mem_end = CONFIG_ULP_COPROC_RESERVE_MEM / sizeof(ulp_insn_t);
  137. if (load_addr > ulp_mem_end) {
  138. ESP_LOGW(TAG, "invalid load address %x, max is %x",
  139. load_addr, ulp_mem_end);
  140. return ESP_ERR_ULP_INVALID_LOAD_ADDR;
  141. }
  142. if (real_program_size + load_addr > ulp_mem_end) {
  143. ESP_LOGE(TAG, "program too big: %d words, max is %d words",
  144. real_program_size, ulp_mem_end);
  145. return ESP_ERR_ULP_SIZE_TOO_BIG;
  146. }
  147. // If no macros found, copy the program and return.
  148. if (macro_count == 0) {
  149. memcpy(((ulp_insn_t*) RTC_SLOW_MEM) + load_addr, program, *psize * sizeof(ulp_insn_t));
  150. return ESP_OK;
  151. }
  152. reloc_info_t* reloc_info =
  153. (reloc_info_t*) malloc(sizeof(reloc_info_t) * macro_count);
  154. if (reloc_info == NULL) {
  155. return ESP_ERR_NO_MEM;
  156. }
  157. // step 2: record macros into reloc_info array
  158. // and remove them from then program
  159. read_ptr = program;
  160. ulp_insn_t* output_program = ((ulp_insn_t*) RTC_SLOW_MEM) + load_addr;
  161. ulp_insn_t* write_ptr = output_program;
  162. uint32_t cur_insn_addr = load_addr;
  163. reloc_info_t* cur_reloc = reloc_info;
  164. while (read_ptr < end) {
  165. ulp_insn_t r_insn = *read_ptr;
  166. if (r_insn.macro.opcode == OPCODE_MACRO) {
  167. switch(r_insn.macro.sub_opcode) {
  168. case SUB_OPCODE_MACRO_LABEL:
  169. *cur_reloc = RELOC_INFO_LABEL(r_insn.macro.label,
  170. cur_insn_addr);
  171. break;
  172. case SUB_OPCODE_MACRO_BRANCH:
  173. *cur_reloc = RELOC_INFO_BRANCH(r_insn.macro.label,
  174. cur_insn_addr);
  175. break;
  176. default:
  177. assert(0 && "invalid sub_opcode for macro insn");
  178. }
  179. ++read_ptr;
  180. assert(read_ptr != end && "program can not end with macro insn");
  181. ++cur_reloc;
  182. } else {
  183. // normal instruction (not a macro)
  184. *write_ptr = *read_ptr;
  185. ++read_ptr;
  186. ++write_ptr;
  187. ++cur_insn_addr;
  188. }
  189. }
  190. // step 3: sort relocations array
  191. int reloc_sort_func(const void* p_lhs, const void* p_rhs) {
  192. const reloc_info_t lhs = *(const reloc_info_t*) p_lhs;
  193. const reloc_info_t rhs = *(const reloc_info_t*) p_rhs;
  194. if (lhs.label < rhs.label) {
  195. return -1;
  196. } else if (lhs.label > rhs.label) {
  197. return 1;
  198. }
  199. // label numbers are equal
  200. if (lhs.type < rhs.type) {
  201. return -1;
  202. } else if (lhs.type > rhs.type) {
  203. return 1;
  204. }
  205. // both label number and type are equal
  206. return 0;
  207. }
  208. qsort(reloc_info, macro_count, sizeof(reloc_info_t),
  209. reloc_sort_func);
  210. // step 4: walk relocations array and fix instructions
  211. reloc_info_t* reloc_end = reloc_info + macro_count;
  212. cur_reloc = reloc_info;
  213. while(cur_reloc < reloc_end) {
  214. reloc_info_t label_info = *cur_reloc;
  215. assert(label_info.type == RELOC_TYPE_LABEL);
  216. ++cur_reloc;
  217. while (cur_reloc < reloc_end) {
  218. if (cur_reloc->type == RELOC_TYPE_LABEL) {
  219. if(cur_reloc->label == label_info.label) {
  220. ESP_LOGE(TAG, "duplicate label definition: %d",
  221. label_info.label);
  222. free(reloc_info);
  223. return ESP_ERR_ULP_DUPLICATE_LABEL;
  224. }
  225. break;
  226. }
  227. if (cur_reloc->label != label_info.label) {
  228. ESP_LOGE(TAG, "branch to an inexistent label: %d",
  229. cur_reloc->label);
  230. free(reloc_info);
  231. return ESP_ERR_ULP_UNDEFINED_LABEL;
  232. }
  233. esp_err_t rc = do_single_reloc(output_program, load_addr,
  234. label_info, *cur_reloc);
  235. if (rc != ESP_OK) {
  236. free(reloc_info);
  237. return rc;
  238. }
  239. ++cur_reloc;
  240. }
  241. }
  242. free(reloc_info);
  243. *psize = real_program_size;
  244. return ESP_OK;
  245. }