# 计算帧槽偏移量 # 假设有500个参数,每个参数一个帧槽 # 然后有500个alloca,每个alloca一个帧槽 # 然后有1个load,一个帧槽 # 参数帧槽 num_params = 500 param_slots = [] for i in range(num_params): if i < 8: # 前8个参数:CreateFrameIndex(is_stack_arg = false) param_slots.append((i, False, False)) # (index, is_stack_arg, is_callee_stack_arg) else: # 第9个及以后的参数:CreateCalleeStackArgFrameIndex(is_stack_arg = true, is_callee_stack_arg = true) param_slots.append((i, True, True)) # alloca帧槽 num_alloca = 500 alloca_slots = [] for i in range(num_alloca): index = num_params + i alloca_slots.append((index, False, False)) # load帧槽 load_slots = [(num_params + num_alloca, False, False)] # 所有帧槽 all_slots = param_slots + alloca_slots + load_slots # 计算偏移量 local_cursor = 0 stack_arg_cursor = 0 offsets = [] for index, is_stack_arg, is_callee_stack_arg in all_slots: if is_stack_arg: offset = stack_arg_cursor stack_arg_cursor += 8 else: local_cursor = ((local_cursor + 4 - 1) // 4) * 4 # align to 4 local_cursor += 4 offset = -local_cursor offsets.append((index, offset, is_stack_arg, is_callee_stack_arg)) # 打印前10个和后10个帧槽的偏移量 print("前10个帧槽:") for i in range(10): index, offset, is_stack_arg, is_callee_stack_arg = offsets[i] print(f" 索引 {index}: offset = {offset}, is_stack_arg = {is_stack_arg}") print("\n第490到500个帧槽:") for i in range(489, 500): index, offset, is_stack_arg, is_callee_stack_arg = offsets[i] print(f" 索引 {index}: offset = {offset}, is_stack_arg = {is_stack_arg}") print("\n第500到510个帧槽(alloca):") for i in range(500, 510): index, offset, is_stack_arg, is_callee_stack_arg = offsets[i] print(f" 索引 {index}: offset = {offset}, is_stack_arg = {is_stack_arg}") print("\n最后一个帧槽(load):") index, offset, is_stack_arg, is_callee_stack_arg = offsets[-1] print(f" 索引 {index}: offset = {offset}, is_stack_arg = {is_stack_arg}") # 计算local_size local_size = ((local_cursor + 16 - 1) // 16) * 16 print(f"\nlocal_size = {local_size}") # 计算sp_offset_base sp_offset_base = local_size + 16 print(f"sp_offset_base = {sp_offset_base}") # 更新is_callee_stack_arg的偏移量 print("\n更新后的is_callee_stack_arg帧槽偏移量:") for i, (index, offset, is_stack_arg, is_callee_stack_arg) in enumerate(offsets): if is_callee_stack_arg: new_offset = sp_offset_base + offset print(f" 索引 {index}: offset = {new_offset} (原offset = {offset})") if i >= 490 and i < 500: break