Ver1.3 && alignment size could be set by argument

This commit is contained in:
LuChiChick 2025-08-11 14:49:05 +08:00
parent ee707fda1a
commit 8f9dc89b9a
6 changed files with 43 additions and 14 deletions

View File

@ -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 // 名称字符串最大长度

View File

@ -7,6 +7,9 @@ extern "C"
#include "stdio.h"
}
// 地址对齐长度
extern size_t addr_alignment_size;
// 类型列表
extern type_node *type_list_head;

View File

@ -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");
// 遍历文件链表

View File

@ -5,6 +5,9 @@ extern "C"
#include "stdio.h"
}
// 地址对齐长度
size_t addr_alignment_size = DEFAULT_ADDR_ALIGNMENT_SIZE;
// 类型列表
type_node *type_list_head = nullptr;

View File

@ -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",

View File

@ -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));
}
// 输出日志