fix(ulp): enable astyle linter and format ULP component
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2010-2023 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2010-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -84,7 +84,6 @@ static int reloc_sort_func(const void* p_lhs, const void* p_rhs)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* Processing branch and label macros involves four steps:
|
||||
*
|
||||
* 1. Iterate over program and count all instructions
|
||||
@@ -124,52 +123,52 @@ static int reloc_sort_func(const void* p_lhs, const void* p_rhs)
|
||||
*/
|
||||
|
||||
static esp_err_t do_single_reloc(ulp_insn_t* program, uint32_t load_addr,
|
||||
reloc_info_t label_info, reloc_info_t the_reloc)
|
||||
reloc_info_t label_info, reloc_info_t the_reloc)
|
||||
{
|
||||
size_t insn_offset = the_reloc.addr - load_addr;
|
||||
ulp_insn_t* insn = &program[insn_offset];
|
||||
|
||||
switch (the_reloc.type) {
|
||||
case RELOC_TYPE_BRANCH: {
|
||||
// B, BS and BX have the same layout of opcode/sub_opcode fields,
|
||||
// and share the same opcode. B and BS also have the same layout of
|
||||
// offset and sign fields.
|
||||
assert(insn->b.opcode == OPCODE_BRANCH
|
||||
&& "branch macro was applied to a non-branch instruction");
|
||||
switch (insn->b.sub_opcode) {
|
||||
case SUB_OPCODE_B:
|
||||
case SUB_OPCODE_BS:{
|
||||
int32_t offset = ((int32_t) label_info.addr) - ((int32_t) the_reloc.addr);
|
||||
uint32_t abs_offset = abs(offset);
|
||||
uint32_t sign = (offset >= 0) ? 0 : 1;
|
||||
if (abs_offset > 127) {
|
||||
ESP_LOGW(TAG, "target out of range: branch from %x to %x",
|
||||
the_reloc.addr, label_info.addr);
|
||||
return ESP_ERR_ULP_BRANCH_OUT_OF_RANGE;
|
||||
}
|
||||
insn->b.offset = abs_offset; //== insn->bs.offset = abs_offset;
|
||||
insn->b.sign = sign; //== insn->bs.sign = sign;
|
||||
break;
|
||||
}
|
||||
case SUB_OPCODE_BX:{
|
||||
assert(insn->bx.reg == 0 &&
|
||||
"relocation applied to a jump with offset in register");
|
||||
insn->bx.addr = label_info.addr;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
assert(false && "unexpected branch sub-opcode");
|
||||
case RELOC_TYPE_BRANCH: {
|
||||
// B, BS and BX have the same layout of opcode/sub_opcode fields,
|
||||
// and share the same opcode. B and BS also have the same layout of
|
||||
// offset and sign fields.
|
||||
assert(insn->b.opcode == OPCODE_BRANCH
|
||||
&& "branch macro was applied to a non-branch instruction");
|
||||
switch (insn->b.sub_opcode) {
|
||||
case SUB_OPCODE_B:
|
||||
case SUB_OPCODE_BS: {
|
||||
int32_t offset = ((int32_t) label_info.addr) - ((int32_t) the_reloc.addr);
|
||||
uint32_t abs_offset = abs(offset);
|
||||
uint32_t sign = (offset >= 0) ? 0 : 1;
|
||||
if (abs_offset > 127) {
|
||||
ESP_LOGW(TAG, "target out of range: branch from %x to %x",
|
||||
the_reloc.addr, label_info.addr);
|
||||
return ESP_ERR_ULP_BRANCH_OUT_OF_RANGE;
|
||||
}
|
||||
insn->b.offset = abs_offset; //== insn->bs.offset = abs_offset;
|
||||
insn->b.sign = sign; //== insn->bs.sign = sign;
|
||||
break;
|
||||
}
|
||||
case RELOC_TYPE_LABELPC: {
|
||||
assert((insn->alu_imm.opcode == OPCODE_ALU && insn->alu_imm.sub_opcode == SUB_OPCODE_ALU_IMM && insn->alu_imm.sel == ALU_SEL_MOV)
|
||||
&& "pc macro was applied to an incompatible instruction");
|
||||
insn->alu_imm.imm = label_info.addr;
|
||||
case SUB_OPCODE_BX: {
|
||||
assert(insn->bx.reg == 0 &&
|
||||
"relocation applied to a jump with offset in register");
|
||||
insn->bx.addr = label_info.addr;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
assert(false && "unknown reloc type");
|
||||
assert(false && "unexpected branch sub-opcode");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case RELOC_TYPE_LABELPC: {
|
||||
assert((insn->alu_imm.opcode == OPCODE_ALU && insn->alu_imm.sub_opcode == SUB_OPCODE_ALU_IMM && insn->alu_imm.sel == ALU_SEL_MOV)
|
||||
&& "pc macro was applied to an incompatible instruction");
|
||||
insn->alu_imm.imm = label_info.addr;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
assert(false && "unknown reloc type");
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -191,12 +190,12 @@ esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* prog
|
||||
const size_t ulp_mem_end = CONFIG_ULP_COPROC_RESERVE_MEM / sizeof(ulp_insn_t);
|
||||
if (load_addr > ulp_mem_end) {
|
||||
ESP_LOGW(TAG, "invalid load address %"PRIx32", max is %x",
|
||||
load_addr, ulp_mem_end);
|
||||
load_addr, ulp_mem_end);
|
||||
return ESP_ERR_ULP_INVALID_LOAD_ADDR;
|
||||
}
|
||||
if (real_program_size + load_addr > ulp_mem_end) {
|
||||
ESP_LOGE(TAG, "program too big: %d words, max is %d words",
|
||||
real_program_size, ulp_mem_end);
|
||||
real_program_size, ulp_mem_end);
|
||||
return ESP_ERR_ULP_SIZE_TOO_BIG;
|
||||
}
|
||||
// If no macros found, copy the program and return.
|
||||
@@ -205,7 +204,7 @@ esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* prog
|
||||
return ESP_OK;
|
||||
}
|
||||
reloc_info_t* reloc_info =
|
||||
(reloc_info_t*) malloc(sizeof(reloc_info_t) * macro_count);
|
||||
(reloc_info_t*) malloc(sizeof(reloc_info_t) * macro_count);
|
||||
if (reloc_info == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
@@ -221,20 +220,20 @@ esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* prog
|
||||
ulp_insn_t r_insn = *read_ptr;
|
||||
if (r_insn.macro.opcode == OPCODE_MACRO) {
|
||||
switch (r_insn.macro.sub_opcode) {
|
||||
case SUB_OPCODE_MACRO_LABEL:
|
||||
*cur_reloc = RELOC_INFO_LABEL(r_insn.macro.label,
|
||||
cur_insn_addr);
|
||||
break;
|
||||
case SUB_OPCODE_MACRO_BRANCH:
|
||||
*cur_reloc = RELOC_INFO_BRANCH(r_insn.macro.label,
|
||||
cur_insn_addr);
|
||||
break;
|
||||
case SUB_OPCODE_MACRO_LABELPC:
|
||||
*cur_reloc = RELOC_INFO_LABELPC(r_insn.macro.label,
|
||||
cur_insn_addr);
|
||||
break;
|
||||
default:
|
||||
assert(0 && "invalid sub_opcode for macro insn");
|
||||
case SUB_OPCODE_MACRO_LABEL:
|
||||
*cur_reloc = RELOC_INFO_LABEL(r_insn.macro.label,
|
||||
cur_insn_addr);
|
||||
break;
|
||||
case SUB_OPCODE_MACRO_BRANCH:
|
||||
*cur_reloc = RELOC_INFO_BRANCH(r_insn.macro.label,
|
||||
cur_insn_addr);
|
||||
break;
|
||||
case SUB_OPCODE_MACRO_LABELPC:
|
||||
*cur_reloc = RELOC_INFO_LABELPC(r_insn.macro.label,
|
||||
cur_insn_addr);
|
||||
break;
|
||||
default:
|
||||
assert(0 && "invalid sub_opcode for macro insn");
|
||||
}
|
||||
++read_ptr;
|
||||
assert(read_ptr != end && "program can not end with macro insn");
|
||||
@@ -250,20 +249,20 @@ esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* prog
|
||||
|
||||
// step 3: sort relocations array
|
||||
qsort(reloc_info, macro_count, sizeof(reloc_info_t),
|
||||
reloc_sort_func);
|
||||
reloc_sort_func);
|
||||
|
||||
// step 4: walk relocations array and fix instructions
|
||||
reloc_info_t* reloc_end = reloc_info + macro_count;
|
||||
cur_reloc = reloc_info;
|
||||
while(cur_reloc < reloc_end) {
|
||||
while (cur_reloc < reloc_end) {
|
||||
reloc_info_t label_info = *cur_reloc;
|
||||
assert(label_info.type == RELOC_TYPE_LABEL);
|
||||
++cur_reloc;
|
||||
while (cur_reloc < reloc_end) {
|
||||
if (cur_reloc->type == RELOC_TYPE_LABEL) {
|
||||
if(cur_reloc->label == label_info.label) {
|
||||
if (cur_reloc->label == label_info.label) {
|
||||
ESP_LOGE(TAG, "duplicate label definition: %d",
|
||||
label_info.label);
|
||||
label_info.label);
|
||||
free(reloc_info);
|
||||
return ESP_ERR_ULP_DUPLICATE_LABEL;
|
||||
}
|
||||
@@ -271,12 +270,12 @@ esp_err_t ulp_process_macros_and_load(uint32_t load_addr, const ulp_insn_t* prog
|
||||
}
|
||||
if (cur_reloc->label != label_info.label) {
|
||||
ESP_LOGE(TAG, "branch to an inexistent label: %d",
|
||||
cur_reloc->label);
|
||||
cur_reloc->label);
|
||||
free(reloc_info);
|
||||
return ESP_ERR_ULP_UNDEFINED_LABEL;
|
||||
}
|
||||
esp_err_t rc = do_single_reloc(output_program, load_addr,
|
||||
label_info, *cur_reloc);
|
||||
label_info, *cur_reloc);
|
||||
if (rc != ESP_OK) {
|
||||
free(reloc_info);
|
||||
return rc;
|
||||
|
||||
Reference in New Issue
Block a user