ulp_macro.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. /* Comparison function used to sort the relocations array */
  52. static int reloc_sort_func(const void* p_lhs, const void* p_rhs)
  53. {
  54. const reloc_info_t lhs = *(const reloc_info_t*) p_lhs;
  55. const reloc_info_t rhs = *(const reloc_info_t*) p_rhs;
  56. if (lhs.label < rhs.label) {
  57. return -1;
  58. } else if (lhs.label > rhs.label) {
  59. return 1;
  60. }
  61. // label numbers are equal
  62. if (lhs.type < rhs.type) {
  63. return -1;
  64. } else if (lhs.type > rhs.type) {
  65. return 1;
  66. }
  67. // both label number and type are equal
  68. return 0;
  69. }
  70. /* Processing branch and label macros involves four steps:
  71. *
  72. * 1. Iterate over program and count all instructions
  73. * with "macro" opcode. Allocate relocations array
  74. * with number of entries equal to number of macro
  75. * instructions.
  76. *
  77. * 2. Remove all fake instructions with "macro" opcode
  78. * and record their locations into relocations array.
  79. * Removal is done using two pointers. Instructions
  80. * are read from read_ptr, and written to write_ptr.
  81. * When a macro instruction is encountered,
  82. * its contents are recorded into the appropriate
  83. * table, and then read_ptr is advanced again.
  84. * When a real instruction is encountered, it is
  85. * read via read_ptr and written to write_ptr.
  86. * In the end, all macro instructions are removed,
  87. * size of the program (expressed in words) is
  88. * reduced by the total number of macro instructions
  89. * which were present.
  90. *
  91. * 3. Sort relocations array by label number, and then
  92. * by type ("label" or "branch") if label numbers
  93. * match. This is done to simplify lookup on the next
  94. * step.
  95. *
  96. * 4. Iterate over entries of relocations table.
  97. * For each label number, label entry comes first
  98. * because the array was sorted at the previous step.
  99. * Label address is recorded, and all subsequent
  100. * "branch" entries which point to the same label number
  101. * are processed. For each branch entry, correct offset
  102. * or absolute address is calculated, depending on branch
  103. * type, and written into the appropriate field of
  104. * the instruction.
  105. *
  106. */
  107. static esp_err_t do_single_reloc(ulp_insn_t* program, uint32_t load_addr,
  108. reloc_info_t label_info, reloc_info_t branch_info)
  109. {
  110. size_t insn_offset = branch_info.addr - load_addr;
  111. ulp_insn_t* insn = &program[insn_offset];
  112. // B and BX have the same layout of opcode/sub_opcode fields,
  113. // and share the same opcode
  114. assert(insn->b.opcode == OPCODE_BRANCH
  115. && "branch macro was applied to a non-branch instruction");
  116. switch (insn->b.sub_opcode) {
  117. case SUB_OPCODE_B: {
  118. int32_t offset = ((int32_t) label_info.addr) - ((int32_t) branch_info.addr);
  119. uint32_t abs_offset = abs(offset);
  120. uint32_t sign = (offset >= 0) ? 0 : 1;
  121. if (abs_offset > 127) {
  122. ESP_LOGW(TAG, "target out of range: branch from %x to %x",
  123. branch_info.addr, label_info.addr);
  124. return ESP_ERR_ULP_BRANCH_OUT_OF_RANGE;
  125. }
  126. insn->b.offset = abs_offset;
  127. insn->b.sign = sign;
  128. break;
  129. }
  130. case SUB_OPCODE_BX: {
  131. assert(insn->bx.reg == 0 &&
  132. "relocation applied to a jump with offset in register");
  133. insn->bx.addr = label_info.addr;
  134. break;
  135. }
  136. default:
  137. assert(false && "unexpected sub-opcode");
  138. }
  139. return ESP_OK;
  140. }
  141. esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* program, size_t* psize)
  142. {
  143. const ulp_insn_t* read_ptr = program;
  144. const ulp_insn_t* end = program + *psize;
  145. size_t macro_count = 0;
  146. // step 1: calculate number of macros
  147. while (read_ptr < end) {
  148. ulp_insn_t r_insn = *read_ptr;
  149. if (r_insn.macro.opcode == OPCODE_MACRO) {
  150. ++macro_count;
  151. }
  152. ++read_ptr;
  153. }
  154. size_t real_program_size = *psize - macro_count;
  155. const size_t ulp_mem_end = CONFIG_ESP32_ULP_COPROC_RESERVE_MEM / sizeof(ulp_insn_t);
  156. if (load_addr > ulp_mem_end) {
  157. ESP_LOGW(TAG, "invalid load address %x, max is %x",
  158. load_addr, ulp_mem_end);
  159. return ESP_ERR_ULP_INVALID_LOAD_ADDR;
  160. }
  161. if (real_program_size + load_addr > ulp_mem_end) {
  162. ESP_LOGE(TAG, "program too big: %d words, max is %d words",
  163. real_program_size, ulp_mem_end);
  164. return ESP_ERR_ULP_SIZE_TOO_BIG;
  165. }
  166. // If no macros found, copy the program and return.
  167. if (macro_count == 0) {
  168. memcpy(((ulp_insn_t*) RTC_SLOW_MEM) + load_addr, program, *psize * sizeof(ulp_insn_t));
  169. return ESP_OK;
  170. }
  171. reloc_info_t* reloc_info =
  172. (reloc_info_t*) malloc(sizeof(reloc_info_t) * macro_count);
  173. if (reloc_info == NULL) {
  174. return ESP_ERR_NO_MEM;
  175. }
  176. // step 2: record macros into reloc_info array
  177. // and remove them from then program
  178. read_ptr = program;
  179. ulp_insn_t* output_program = ((ulp_insn_t*) RTC_SLOW_MEM) + load_addr;
  180. ulp_insn_t* write_ptr = output_program;
  181. uint32_t cur_insn_addr = load_addr;
  182. reloc_info_t* cur_reloc = reloc_info;
  183. while (read_ptr < end) {
  184. ulp_insn_t r_insn = *read_ptr;
  185. if (r_insn.macro.opcode == OPCODE_MACRO) {
  186. switch(r_insn.macro.sub_opcode) {
  187. case SUB_OPCODE_MACRO_LABEL:
  188. *cur_reloc = RELOC_INFO_LABEL(r_insn.macro.label,
  189. cur_insn_addr);
  190. break;
  191. case SUB_OPCODE_MACRO_BRANCH:
  192. *cur_reloc = RELOC_INFO_BRANCH(r_insn.macro.label,
  193. cur_insn_addr);
  194. break;
  195. default:
  196. assert(0 && "invalid sub_opcode for macro insn");
  197. }
  198. ++read_ptr;
  199. assert(read_ptr != end && "program can not end with macro insn");
  200. ++cur_reloc;
  201. } else {
  202. // normal instruction (not a macro)
  203. *write_ptr = *read_ptr;
  204. ++read_ptr;
  205. ++write_ptr;
  206. ++cur_insn_addr;
  207. }
  208. }
  209. // step 3: sort relocations array
  210. qsort(reloc_info, macro_count, sizeof(reloc_info_t),
  211. reloc_sort_func);
  212. // step 4: walk relocations array and fix instructions
  213. reloc_info_t* reloc_end = reloc_info + macro_count;
  214. cur_reloc = reloc_info;
  215. while(cur_reloc < reloc_end) {
  216. reloc_info_t label_info = *cur_reloc;
  217. assert(label_info.type == RELOC_TYPE_LABEL);
  218. ++cur_reloc;
  219. while (cur_reloc < reloc_end) {
  220. if (cur_reloc->type == RELOC_TYPE_LABEL) {
  221. if(cur_reloc->label == label_info.label) {
  222. ESP_LOGE(TAG, "duplicate label definition: %d",
  223. label_info.label);
  224. free(reloc_info);
  225. return ESP_ERR_ULP_DUPLICATE_LABEL;
  226. }
  227. break;
  228. }
  229. if (cur_reloc->label != label_info.label) {
  230. ESP_LOGE(TAG, "branch to an inexistent label: %d",
  231. cur_reloc->label);
  232. free(reloc_info);
  233. return ESP_ERR_ULP_UNDEFINED_LABEL;
  234. }
  235. esp_err_t rc = do_single_reloc(output_program, load_addr,
  236. label_info, *cur_reloc);
  237. if (rc != ESP_OK) {
  238. free(reloc_info);
  239. return rc;
  240. }
  241. ++cur_reloc;
  242. }
  243. }
  244. free(reloc_info);
  245. *psize = real_program_size;
  246. return ESP_OK;
  247. }