Skip to content

Commit 24ecee8

Browse files
authored
Merge pull request #3987 from no1wudi/fix
Break aot_create_comp_data into small functions
2 parents 38cf274 + 9b80766 commit 24ecee8

File tree

1 file changed

+220
-79
lines changed
  • core/iwasm/compilation

1 file changed

+220
-79
lines changed

core/iwasm/compilation/aot.c

+220-79
Original file line numberDiff line numberDiff line change
@@ -502,68 +502,78 @@ calculate_struct_field_sizes_offsets(AOTCompData *comp_data, bool is_target_x86,
502502
}
503503
#endif
504504

505-
AOTCompData *
506-
aot_create_comp_data(WASMModule *module, const char *target_arch,
507-
bool gc_enabled)
505+
/**
506+
* Checks if target architecture is 64-bit based on target_arch string.
507+
*
508+
* @param target_arch The target architecture string (e.g. "x86_64", "aarch64")
509+
* @return true if target is 64-bit architecture, false otherwise
510+
*
511+
* If target_arch is NULL, detection is based on UINTPTR_MAX.
512+
* Otherwise looks for "64" in target_arch string.
513+
*/
514+
static bool
515+
arch_is_64bit(const char *target_arch)
508516
{
509-
AOTCompData *comp_data;
510-
uint32 import_global_data_size_64bit = 0, global_data_size_64bit = 0, i, j;
511-
uint32 import_global_data_size_32bit = 0, global_data_size_32bit = 0;
512-
uint64 size;
513-
bool is_64bit_target = false;
514-
#if WASM_ENABLE_GC != 0
515-
bool is_target_x86 = false;
516-
#endif
517-
518-
#if WASM_ENABLE_GC != 0
519517
if (!target_arch) {
520-
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
521-
|| defined(BUILD_TARGET_X86_32)
522-
is_target_x86 = true;
518+
#if UINTPTR_MAX == UINT64_MAX
519+
return true;
520+
#else
521+
return false;
523522
#endif
524523
}
525-
else {
526-
if (!strncmp(target_arch, "x86_64", 6)
527-
|| !strncmp(target_arch, "i386", 4))
528-
is_target_x86 = true;
529-
}
530-
#endif
524+
/* All 64bit targets contains "64" string in their target name */
525+
return strstr(target_arch, "64") != NULL;
526+
}
531527

528+
/**
529+
* Checks if target architecture is x86/x64 based on target_arch string.
530+
*
531+
* @param target_arch The target architecture string (e.g. "x86_64", "i386")
532+
* @return true if target is x86/x64 architecture, false otherwise
533+
*
534+
* If target_arch is NULL, detection is based on build-time definitions.
535+
* Otherwise checks for x86_64 or i386 in target_arch string.
536+
*/
537+
static bool
538+
arch_is_x86(const char *target_arch)
539+
{
532540
if (!target_arch) {
533-
#if UINTPTR_MAX == UINT64_MAX
534-
is_64bit_target = true;
541+
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
542+
|| defined(BUILD_TARGET_X86_32)
543+
return true;
544+
#else
545+
return false;
535546
#endif
536547
}
537-
else {
538-
/* All 64bit targets contains "64" string in their target name */
539-
if (strstr(target_arch, "64") != NULL) {
540-
is_64bit_target = true;
541-
}
542-
}
543-
544-
/* Allocate memory */
545-
if (!(comp_data = wasm_runtime_malloc(sizeof(AOTCompData)))) {
546-
aot_set_last_error("create compile data failed.\n");
547-
return NULL;
548-
}
548+
return !strncmp(target_arch, "x86_64", 6)
549+
|| !strncmp(target_arch, "i386", 4);
550+
}
549551

550-
memset(comp_data, 0, sizeof(AOTCompData));
552+
/**
553+
* Initialize memory information in AOT compilation data from WASM module.
554+
*
555+
* @param comp_data the AOT compilation data structure to initialize
556+
* @param module the source WASM module containing memory information
557+
* @return true if initialization succeeded, false otherwise
558+
*/
559+
static bool
560+
aot_init_memories(AOTCompData *comp_data, WASMModule *module)
561+
{
562+
uint32 i, j;
563+
uint64 size;
551564

552565
comp_data->memory_count =
553566
module->import_memory_count + module->memory_count;
554567

555-
/* TODO: create import memories */
556-
557568
/* Allocate memory for memory array, reserve one AOTMemory space at least */
558-
/* TODO: multi-memory */
559569
if (!comp_data->memory_count)
560570
comp_data->memory_count = 1;
561571

562572
size = (uint64)comp_data->memory_count * sizeof(AOTMemory);
563573
if (size >= UINT32_MAX
564574
|| !(comp_data->memories = wasm_runtime_malloc((uint32)size))) {
565575
aot_set_last_error("create memories array failed.\n");
566-
goto fail;
576+
return false;
567577
}
568578
memset(comp_data->memories, 0, size);
569579

@@ -595,22 +605,30 @@ aot_create_comp_data(WASMModule *module, const char *target_arch,
595605
}
596606
}
597607

598-
/* Create memory data segments */
599-
comp_data->mem_init_data_count = module->data_seg_count;
600-
if (comp_data->mem_init_data_count > 0
601-
&& !(comp_data->mem_init_data_list =
602-
aot_create_mem_init_data_list(module)))
603-
goto fail;
608+
return true;
609+
}
610+
611+
/**
612+
* Initialize table information in AOT compilation data from WASM module.
613+
*
614+
* @param comp_data the AOT compilation data structure to initialize
615+
* @param module the source WASM module containing table information
616+
* @return true if initialization succeeded, false otherwise
617+
*/
618+
static bool
619+
aot_init_tables(AOTCompData *comp_data, WASMModule *module)
620+
{
621+
uint32 i, j;
622+
uint64 size;
604623

605-
/* Create tables */
606624
comp_data->table_count = module->import_table_count + module->table_count;
607625

608626
if (comp_data->table_count > 0) {
609627
size = sizeof(AOTTable) * (uint64)comp_data->table_count;
610628
if (size >= UINT32_MAX
611629
|| !(comp_data->tables = wasm_runtime_malloc((uint32)size))) {
612-
aot_set_last_error("create memories array failed.\n");
613-
goto fail;
630+
aot_set_last_error("create tables array failed.\n");
631+
return false;
614632
}
615633
memset(comp_data->tables, 0, size);
616634
for (i = 0; i < comp_data->table_count; i++) {
@@ -656,64 +674,150 @@ aot_create_comp_data(WASMModule *module, const char *target_arch,
656674
}
657675
}
658676

659-
/* Create table data segments */
677+
return true;
678+
}
679+
680+
/**
681+
* Initialize memory segment information in AOT compilation data.
682+
*
683+
* @param comp_data the AOT compilation data structure to initialize
684+
* @param module the source WASM module containing memory segments
685+
* @return true if initialization succeeded, false otherwise
686+
*/
687+
static bool
688+
aot_init_memory_segments(AOTCompData *comp_data, WASMModule *module)
689+
{
690+
comp_data->mem_init_data_count = module->data_seg_count;
691+
if (comp_data->mem_init_data_count > 0
692+
&& !(comp_data->mem_init_data_list =
693+
aot_create_mem_init_data_list(module))) {
694+
return false;
695+
}
696+
return true;
697+
}
698+
699+
/**
700+
* Initialize table segment information in AOT compilation data.
701+
*
702+
* @param comp_data the AOT compilation data structure to initialize
703+
* @param module the source WASM module containing table segments
704+
* @return true if initialization succeeded, false otherwise
705+
*/
706+
static bool
707+
aot_init_table_segments(AOTCompData *comp_data, WASMModule *module)
708+
{
660709
comp_data->table_init_data_count = module->table_seg_count;
661710
if (comp_data->table_init_data_count > 0
662711
&& !(comp_data->table_init_data_list =
663-
aot_create_table_init_data_list(module)))
664-
goto fail;
712+
aot_create_table_init_data_list(module))) {
713+
return false;
714+
}
715+
return true;
716+
}
665717

666-
/* Create import globals */
718+
/**
719+
* Initialize global variable information in AOT compilation data.
720+
*
721+
* @param comp_data the AOT compilation data structure to initialize
722+
* @param module the source WASM module containing global information
723+
* @param gc_enabled whether garbage collection is enabled
724+
* @param import_global_data_size_64bit [out] size of imported global data for
725+
* 64-bit
726+
* @param import_global_data_size_32bit [out] size of imported global data for
727+
* 32-bit
728+
* @param global_data_size_64bit [out] size of global data for 64-bit
729+
* @param global_data_size_32bit [out] size of global data for 32-bit
730+
* @return true if initialization succeeded, false otherwise
731+
*/
732+
static bool
733+
aot_init_globals(AOTCompData *comp_data, WASMModule *module, bool gc_enabled,
734+
uint32 *import_global_data_size_64bit,
735+
uint32 *import_global_data_size_32bit,
736+
uint32 *global_data_size_64bit, uint32 *global_data_size_32bit)
737+
{
667738
comp_data->import_global_count = module->import_global_count;
668739
if (comp_data->import_global_count > 0
669740
&& !(comp_data->import_globals = aot_create_import_globals(
670-
module, gc_enabled, &import_global_data_size_64bit,
671-
&import_global_data_size_32bit)))
672-
goto fail;
741+
module, gc_enabled, import_global_data_size_64bit,
742+
import_global_data_size_32bit))) {
743+
return false;
744+
}
673745

674-
/* Create globals */
675746
comp_data->global_count = module->global_count;
676747
if (comp_data->global_count
677748
&& !(comp_data->globals = aot_create_globals(
678-
module, gc_enabled, import_global_data_size_64bit,
679-
import_global_data_size_32bit, &global_data_size_64bit,
680-
&global_data_size_32bit)))
681-
goto fail;
749+
module, gc_enabled, *import_global_data_size_64bit,
750+
*import_global_data_size_32bit, global_data_size_64bit,
751+
global_data_size_32bit))) {
752+
return false;
753+
}
682754

683755
comp_data->global_data_size_64bit =
684-
import_global_data_size_64bit + global_data_size_64bit;
756+
*import_global_data_size_64bit + *global_data_size_64bit;
685757
comp_data->global_data_size_32bit =
686-
import_global_data_size_32bit + global_data_size_32bit;
758+
*import_global_data_size_32bit + *global_data_size_32bit;
759+
760+
return true;
761+
}
687762

688-
/* Create types, they are checked by wasm loader */
763+
/**
764+
* Initialize type information in AOT compilation data.
765+
*
766+
* @param comp_data the AOT compilation data structure to initialize
767+
* @param module the source WASM module containing type information
768+
* @param is_target_x86 whether the target architecture is x86
769+
* @param gc_enabled whether garbage collection is enabled
770+
* @return true if initialization succeeded, false otherwise
771+
*/
772+
static bool
773+
aot_init_types(AOTCompData *comp_data, WASMModule *module, bool is_target_x86,
774+
bool gc_enabled)
775+
{
689776
comp_data->type_count = module->type_count;
690777
comp_data->types = module->types;
691778
#if WASM_ENABLE_GC != 0
692-
/* Calculate the field sizes and field offsets for 64-bit and 32-bit
693-
targets since they may vary in 32-bit target and 64-bit target */
694779
calculate_struct_field_sizes_offsets(comp_data, is_target_x86, gc_enabled);
695780
#endif
781+
return true;
782+
}
696783

697-
/* Create import functions */
784+
/**
785+
* Initialize function information in AOT compilation data.
786+
*
787+
* @param comp_data the AOT compilation data structure to initialize
788+
* @param module the source WASM module containing function information
789+
* @param is_64bit_target whether the target architecture is 64-bit
790+
* @return true if initialization succeeded, false otherwise
791+
*/
792+
static bool
793+
aot_init_functions(AOTCompData *comp_data, WASMModule *module,
794+
bool is_64bit_target)
795+
{
698796
comp_data->import_func_count = module->import_function_count;
699797
if (comp_data->import_func_count
700-
&& !(comp_data->import_funcs = aot_create_import_funcs(module)))
701-
goto fail;
798+
&& !(comp_data->import_funcs = aot_create_import_funcs(module))) {
799+
return false;
800+
}
702801

703-
/* Create functions */
704802
comp_data->func_count = module->function_count;
705803
if (comp_data->func_count
706804
&& !(comp_data->funcs =
707-
aot_create_funcs(module, is_64bit_target ? 8 : 4)))
708-
goto fail;
805+
aot_create_funcs(module, is_64bit_target ? 8 : 4))) {
806+
return false;
807+
}
709808

710-
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
711-
/* Create custom name section */
712-
comp_data->name_section_buf = module->name_section_buf;
713-
comp_data->name_section_buf_end = module->name_section_buf_end;
714-
#endif
809+
return true;
810+
}
715811

716-
/* Create aux data/heap/stack information */
812+
/**
813+
* Initialize auxiliary data in AOT compilation data.
814+
*
815+
* @param comp_data the AOT compilation data structure to initialize
816+
* @param module the source WASM module containing auxiliary data
817+
*/
818+
static void
819+
aot_init_aux_data(AOTCompData *comp_data, WASMModule *module)
820+
{
717821
comp_data->aux_data_end_global_index = module->aux_data_end_global_index;
718822
comp_data->aux_data_end = module->aux_data_end;
719823
comp_data->aux_heap_base_global_index = module->aux_heap_base_global_index;
@@ -732,6 +836,43 @@ aot_create_comp_data(WASMModule *module, const char *target_arch,
732836
comp_data->string_literal_ptrs_wp = module->string_literal_ptrs;
733837
comp_data->string_literal_lengths_wp = module->string_literal_lengths;
734838
#endif
839+
}
840+
841+
AOTCompData *
842+
aot_create_comp_data(WASMModule *module, const char *target_arch,
843+
bool gc_enabled)
844+
{
845+
AOTCompData *comp_data;
846+
uint32 import_global_data_size_64bit = 0, global_data_size_64bit = 0;
847+
uint32 import_global_data_size_32bit = 0, global_data_size_32bit = 0;
848+
bool is_64bit_target = arch_is_64bit(target_arch);
849+
bool is_target_x86 = arch_is_x86(target_arch);
850+
851+
if (!(comp_data = wasm_runtime_malloc(sizeof(AOTCompData)))) {
852+
aot_set_last_error("create compile data failed.\n");
853+
return NULL;
854+
}
855+
memset(comp_data, 0, sizeof(AOTCompData));
856+
857+
if (!aot_init_memories(comp_data, module)
858+
|| !aot_init_memory_segments(comp_data, module)
859+
|| !aot_init_tables(comp_data, module)
860+
|| !aot_init_table_segments(comp_data, module)
861+
|| !aot_init_globals(comp_data, module, gc_enabled,
862+
&import_global_data_size_64bit,
863+
&import_global_data_size_32bit,
864+
&global_data_size_64bit, &global_data_size_32bit)
865+
|| !aot_init_types(comp_data, module, is_target_x86, gc_enabled)
866+
|| !aot_init_functions(comp_data, module, is_64bit_target)) {
867+
goto fail;
868+
}
869+
870+
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
871+
comp_data->name_section_buf = module->name_section_buf;
872+
comp_data->name_section_buf_end = module->name_section_buf_end;
873+
#endif
874+
875+
aot_init_aux_data(comp_data, module);
735876

736877
comp_data->wasm_module = module;
737878

0 commit comments

Comments
 (0)