ulp_macro.c 10 KB

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