diff --git a/Inc/Config.hpp b/Inc/Config.hpp index c2a737c..3e39c11 100644 --- a/Inc/Config.hpp +++ b/Inc/Config.hpp @@ -1,7 +1,7 @@ #ifndef __CONFIG_HPP__ #define __CONFIG_HPP__ -#define ADDR_ALIGNMENT_SIZE 4 // 地址对齐方式 +#define DEFAULT_ADDR_ALIGNMENT_SIZE 4 // 默认地址对齐长度 #define VARIABLE_NAME_STR_LENGTH_MAX 100 // 名称字符串最大长度 diff --git a/Inc/Global_Variables.hpp b/Inc/Global_Variables.hpp index 200b5d1..fd7f029 100644 --- a/Inc/Global_Variables.hpp +++ b/Inc/Global_Variables.hpp @@ -7,6 +7,9 @@ extern "C" #include "stdio.h" } +// 地址对齐长度 +extern size_t addr_alignment_size; + // 类型列表 extern type_node *type_list_head; diff --git a/Src/Core_Functions.cpp b/Src/Core_Functions.cpp index 782b0a6..9466493 100644 --- a/Src/Core_Functions.cpp +++ b/Src/Core_Functions.cpp @@ -25,6 +25,28 @@ void solve_args(int argc, char *argv[]) if (argv[count][0] == '-') { + // -a 字节对齐长度设置 + if (!strcmp(argv[count], "-a")) + { + // 错误输入处理 + if (count + 1 == argc) + break; + if (argv[count + 1][0] == '-') + continue; + + size_t value = 0; + sscanf(argv[count + 1], "%u", &value); + if ((value % 2 == 0 && value != 0) || value == 1) + { + log_printf(LOG_SUCCESS, "Alignment size set to %u.", value); + addr_alignment_size = value; + } + else + log_printf(LOG_FAILURE, "Illegal alignment arg \"%s\", set to default %u.", argv[count + 1], addr_alignment_size); + count++; + continue; + } + // -r 参考A2L输入 if (!strcmp(argv[count], "-r")) { @@ -175,6 +197,7 @@ void solve_args(int argc, char *argv[]) { printf("\n"); log_printf(LOG_INFO, "Workflow details:"); + printf(" ├─Alignment size: %u\n", addr_alignment_size); printf(" ├─Source or header files:\n"); // 遍历文件链表 diff --git a/Src/Global_Variables.cpp b/Src/Global_Variables.cpp index 1168351..613d0ac 100644 --- a/Src/Global_Variables.cpp +++ b/Src/Global_Variables.cpp @@ -5,6 +5,9 @@ extern "C" #include "stdio.h" } +// 地址对齐长度 +size_t addr_alignment_size = DEFAULT_ADDR_ALIGNMENT_SIZE; + // 类型列表 type_node *type_list_head = nullptr; diff --git a/Src/Main.cpp b/Src/Main.cpp index a4b8779..3bed898 100644 --- a/Src/Main.cpp +++ b/Src/Main.cpp @@ -14,7 +14,7 @@ extern "C" int main(int argc, char *argv[]) { printf("\n\n"); - log_printf(LOG_SYS_INFO, "SrcToA2L Ver1.2"); + log_printf(LOG_SYS_INFO, "SrcToA2L Ver1.3"); log_printf(LOG_SYS_INFO, "Auther: LuChiChick"); log_printf(LOG_SYS_INFO, "%s\n%s\n%s\n\n", "Open source links:", " ├─Github: https://git.luchichick.cn/LuChiChick/SrcToA2L", diff --git a/Src/Tool_Functions.cpp b/Src/Tool_Functions.cpp index 5f5a559..3e33585 100644 --- a/Src/Tool_Functions.cpp +++ b/Src/Tool_Functions.cpp @@ -664,12 +664,12 @@ void f_print_calibration(FILE *file, variable_info v_info) for (size_t sub_count = 0; sub_count < sub_element_count; sub_count++) { // 计算出现的最大对齐 - if (alignment_max < (type_size[out_type] < ADDR_ALIGNMENT_SIZE ? type_size[out_type] : ADDR_ALIGNMENT_SIZE)) - alignment_max = (type_size[out_type] < ADDR_ALIGNMENT_SIZE ? type_size[out_type] : ADDR_ALIGNMENT_SIZE); + if (alignment_max < (type_size[out_type] < addr_alignment_size ? type_size[out_type] : addr_alignment_size)) + alignment_max = (type_size[out_type] < addr_alignment_size ? type_size[out_type] : addr_alignment_size); // 地址对齐 - if (addr_offset % (type_size[out_type] < ADDR_ALIGNMENT_SIZE ? type_size[out_type] : ADDR_ALIGNMENT_SIZE) != 0) - addr_offset += (type_size[out_type] < ADDR_ALIGNMENT_SIZE ? type_size[out_type] : ADDR_ALIGNMENT_SIZE) - (addr_offset % (type_size[out_type] < ADDR_ALIGNMENT_SIZE ? type_size[out_type] : ADDR_ALIGNMENT_SIZE)); + if (addr_offset % (type_size[out_type] < addr_alignment_size ? type_size[out_type] : addr_alignment_size) != 0) + addr_offset += (type_size[out_type] < addr_alignment_size ? type_size[out_type] : addr_alignment_size) - (addr_offset % (type_size[out_type] < addr_alignment_size ? type_size[out_type] : addr_alignment_size)); // 拼接输出名称 if (v_info.type == STRUCTURE) @@ -721,8 +721,8 @@ void f_print_calibration(FILE *file, variable_info v_info) // 补齐结构体末尾的空余字节偏移 if (v_info.type == STRUCTURE) - if (addr_offset % (alignment_max < ADDR_ALIGNMENT_SIZE ? alignment_max : ADDR_ALIGNMENT_SIZE) != 0) - addr_offset += (alignment_max < ADDR_ALIGNMENT_SIZE ? alignment_max : ADDR_ALIGNMENT_SIZE) - (addr_offset % (alignment_max < ADDR_ALIGNMENT_SIZE ? alignment_max : ADDR_ALIGNMENT_SIZE)); + if (addr_offset % (alignment_max < addr_alignment_size ? alignment_max : addr_alignment_size) != 0) + addr_offset += (alignment_max < addr_alignment_size ? alignment_max : addr_alignment_size) - (addr_offset % (alignment_max < addr_alignment_size ? alignment_max : addr_alignment_size)); } // 输出日志 @@ -849,12 +849,12 @@ void f_print_measurement(FILE *file, variable_info v_info) for (size_t sub_count = 0; sub_count < sub_element_count; sub_count++) { // 计算出现的最大对齐 - if (alignment_max < (type_size[out_type] < ADDR_ALIGNMENT_SIZE ? type_size[out_type] : ADDR_ALIGNMENT_SIZE)) - alignment_max = (type_size[out_type] < ADDR_ALIGNMENT_SIZE ? type_size[out_type] : ADDR_ALIGNMENT_SIZE); + if (alignment_max < (type_size[out_type] < addr_alignment_size ? type_size[out_type] : addr_alignment_size)) + alignment_max = (type_size[out_type] < addr_alignment_size ? type_size[out_type] : addr_alignment_size); // 地址对齐 - if (addr_offset % (type_size[out_type] < ADDR_ALIGNMENT_SIZE ? type_size[out_type] : ADDR_ALIGNMENT_SIZE) != 0) - addr_offset += (type_size[out_type] < ADDR_ALIGNMENT_SIZE ? type_size[out_type] : ADDR_ALIGNMENT_SIZE) - (addr_offset % (type_size[out_type] < ADDR_ALIGNMENT_SIZE ? type_size[out_type] : ADDR_ALIGNMENT_SIZE)); + if (addr_offset % (type_size[out_type] < addr_alignment_size ? type_size[out_type] : addr_alignment_size) != 0) + addr_offset += (type_size[out_type] < addr_alignment_size ? type_size[out_type] : addr_alignment_size) - (addr_offset % (type_size[out_type] < addr_alignment_size ? type_size[out_type] : addr_alignment_size)); // 拼接输出名称 if (v_info.type == STRUCTURE) @@ -906,8 +906,8 @@ void f_print_measurement(FILE *file, variable_info v_info) // 补齐结构体末尾的空余字节偏移 if (v_info.type == STRUCTURE) - if (addr_offset % (alignment_max < ADDR_ALIGNMENT_SIZE ? alignment_max : ADDR_ALIGNMENT_SIZE) != 0) - addr_offset += (alignment_max < ADDR_ALIGNMENT_SIZE ? alignment_max : ADDR_ALIGNMENT_SIZE) - (addr_offset % (alignment_max < ADDR_ALIGNMENT_SIZE ? alignment_max : ADDR_ALIGNMENT_SIZE)); + if (addr_offset % (alignment_max < addr_alignment_size ? alignment_max : addr_alignment_size) != 0) + addr_offset += (alignment_max < addr_alignment_size ? alignment_max : addr_alignment_size) - (addr_offset % (alignment_max < addr_alignment_size ? alignment_max : addr_alignment_size)); } // 输出日志