@@ -487,68 +487,78 @@ calculate_struct_field_sizes_offsets(AOTCompData *comp_data, bool is_target_x86,
487487}
488488#endif
489489
490- AOTCompData *
491- aot_create_comp_data (WASMModule * module , const char * target_arch ,
492- bool gc_enabled )
490+ /**
491+ * Checks if target architecture is 64-bit based on target_arch string.
492+ *
493+ * @param target_arch The target architecture string (e.g. "x86_64", "aarch64")
494+ * @return true if target is 64-bit architecture, false otherwise
495+ *
496+ * If target_arch is NULL, detection is based on UINTPTR_MAX.
497+ * Otherwise looks for "64" in target_arch string.
498+ */
499+ static bool
500+ arch_is_64bit (const char * target_arch )
493501{
494- AOTCompData * comp_data ;
495- uint32 import_global_data_size_64bit = 0 , global_data_size_64bit = 0 , i , j ;
496- uint32 import_global_data_size_32bit = 0 , global_data_size_32bit = 0 ;
497- uint64 size ;
498- bool is_64bit_target = false;
499- #if WASM_ENABLE_GC != 0
500- bool is_target_x86 = false;
501- #endif
502-
503- #if WASM_ENABLE_GC != 0
504502 if (!target_arch ) {
505- #if defined(BUILD_TARGET_X86_64 ) || defined(BUILD_TARGET_AMD_64 ) \
506- || defined(BUILD_TARGET_X86_32 )
507- is_target_x86 = true;
503+ #if UINTPTR_MAX == UINT64_MAX
504+ return true;
505+ #else
506+ return false;
508507#endif
509508 }
510- else {
511- if (!strncmp (target_arch , "x86_64" , 6 )
512- || !strncmp (target_arch , "i386" , 4 ))
513- is_target_x86 = true;
514- }
515- #endif
509+ /* All 64bit targets contains "64" string in their target name */
510+ return strstr (target_arch , "64" ) != NULL ;
511+ }
516512
513+ /**
514+ * Checks if target architecture is x86/x64 based on target_arch string.
515+ *
516+ * @param target_arch The target architecture string (e.g. "x86_64", "i386")
517+ * @return true if target is x86/x64 architecture, false otherwise
518+ *
519+ * If target_arch is NULL, detection is based on build-time definitions.
520+ * Otherwise checks for x86_64 or i386 in target_arch string.
521+ */
522+ static bool
523+ arch_is_x86 (const char * target_arch )
524+ {
517525 if (!target_arch ) {
518- #if UINTPTR_MAX == UINT64_MAX
519- is_64bit_target = true;
526+ #if defined(BUILD_TARGET_X86_64 ) || defined(BUILD_TARGET_AMD_64 ) \
527+ || defined(BUILD_TARGET_X86_32 )
528+ return true;
529+ #else
530+ return false;
520531#endif
521532 }
522- else {
523- /* All 64bit targets contains "64" string in their target name */
524- if (strstr (target_arch , "64" ) != NULL ) {
525- is_64bit_target = true;
526- }
527- }
528-
529- /* Allocate memory */
530- if (!(comp_data = wasm_runtime_malloc (sizeof (AOTCompData )))) {
531- aot_set_last_error ("create compile data failed.\n" );
532- return NULL ;
533- }
533+ return !strncmp (target_arch , "x86_64" , 6 )
534+ || !strncmp (target_arch , "i386" , 4 );
535+ }
534536
535- memset (comp_data , 0 , sizeof (AOTCompData ));
537+ /**
538+ * Initialize memory information in AOT compilation data from WASM module.
539+ *
540+ * @param comp_data the AOT compilation data structure to initialize
541+ * @param module the source WASM module containing memory information
542+ * @return true if initialization succeeded, false otherwise
543+ */
544+ static bool
545+ aot_init_memories (AOTCompData * comp_data , WASMModule * module )
546+ {
547+ uint32 i , j ;
548+ uint64 size ;
536549
537550 comp_data -> memory_count =
538551 module -> import_memory_count + module -> memory_count ;
539552
540- /* TODO: create import memories */
541-
542553 /* Allocate memory for memory array, reserve one AOTMemory space at least */
543- /* TODO: multi-memory */
544554 if (!comp_data -> memory_count )
545555 comp_data -> memory_count = 1 ;
546556
547557 size = (uint64 )comp_data -> memory_count * sizeof (AOTMemory );
548558 if (size >= UINT32_MAX
549559 || !(comp_data -> memories = wasm_runtime_malloc ((uint32 )size ))) {
550560 aot_set_last_error ("create memories array failed.\n" );
551- goto fail ;
561+ return false ;
552562 }
553563 memset (comp_data -> memories , 0 , size );
554564
@@ -580,22 +590,30 @@ aot_create_comp_data(WASMModule *module, const char *target_arch,
580590 }
581591 }
582592
583- /* Create memory data segments */
584- comp_data -> mem_init_data_count = module -> data_seg_count ;
585- if (comp_data -> mem_init_data_count > 0
586- && !(comp_data -> mem_init_data_list =
587- aot_create_mem_init_data_list (module )))
588- goto fail ;
593+ return true;
594+ }
595+
596+ /**
597+ * Initialize table information in AOT compilation data from WASM module.
598+ *
599+ * @param comp_data the AOT compilation data structure to initialize
600+ * @param module the source WASM module containing table information
601+ * @return true if initialization succeeded, false otherwise
602+ */
603+ static bool
604+ aot_init_tables (AOTCompData * comp_data , WASMModule * module )
605+ {
606+ uint32 i , j ;
607+ uint64 size ;
589608
590- /* Create tables */
591609 comp_data -> table_count = module -> import_table_count + module -> table_count ;
592610
593611 if (comp_data -> table_count > 0 ) {
594612 size = sizeof (AOTTable ) * (uint64 )comp_data -> table_count ;
595613 if (size >= UINT32_MAX
596614 || !(comp_data -> tables = wasm_runtime_malloc ((uint32 )size ))) {
597- aot_set_last_error ("create memories array failed.\n" );
598- goto fail ;
615+ aot_set_last_error ("create tables array failed.\n" );
616+ return false ;
599617 }
600618 memset (comp_data -> tables , 0 , size );
601619 for (i = 0 ; i < comp_data -> table_count ; i ++ ) {
@@ -641,64 +659,150 @@ aot_create_comp_data(WASMModule *module, const char *target_arch,
641659 }
642660 }
643661
644- /* Create table data segments */
662+ return true;
663+ }
664+
665+ /**
666+ * Initialize memory segment information in AOT compilation data.
667+ *
668+ * @param comp_data the AOT compilation data structure to initialize
669+ * @param module the source WASM module containing memory segments
670+ * @return true if initialization succeeded, false otherwise
671+ */
672+ static bool
673+ aot_init_memory_segments (AOTCompData * comp_data , WASMModule * module )
674+ {
675+ comp_data -> mem_init_data_count = module -> data_seg_count ;
676+ if (comp_data -> mem_init_data_count > 0
677+ && !(comp_data -> mem_init_data_list =
678+ aot_create_mem_init_data_list (module ))) {
679+ return false;
680+ }
681+ return true;
682+ }
683+
684+ /**
685+ * Initialize table segment information in AOT compilation data.
686+ *
687+ * @param comp_data the AOT compilation data structure to initialize
688+ * @param module the source WASM module containing table segments
689+ * @return true if initialization succeeded, false otherwise
690+ */
691+ static bool
692+ aot_init_table_segments (AOTCompData * comp_data , WASMModule * module )
693+ {
645694 comp_data -> table_init_data_count = module -> table_seg_count ;
646695 if (comp_data -> table_init_data_count > 0
647696 && !(comp_data -> table_init_data_list =
648- aot_create_table_init_data_list (module )))
649- goto fail ;
697+ aot_create_table_init_data_list (module ))) {
698+ return false;
699+ }
700+ return true;
701+ }
650702
651- /* Create import globals */
703+ /**
704+ * Initialize global variable information in AOT compilation data.
705+ *
706+ * @param comp_data the AOT compilation data structure to initialize
707+ * @param module the source WASM module containing global information
708+ * @param gc_enabled whether garbage collection is enabled
709+ * @param import_global_data_size_64bit [out] size of imported global data for
710+ * 64-bit
711+ * @param import_global_data_size_32bit [out] size of imported global data for
712+ * 32-bit
713+ * @param global_data_size_64bit [out] size of global data for 64-bit
714+ * @param global_data_size_32bit [out] size of global data for 32-bit
715+ * @return true if initialization succeeded, false otherwise
716+ */
717+ static bool
718+ aot_init_globals (AOTCompData * comp_data , WASMModule * module , bool gc_enabled ,
719+ uint32 * import_global_data_size_64bit ,
720+ uint32 * import_global_data_size_32bit ,
721+ uint32 * global_data_size_64bit , uint32 * global_data_size_32bit )
722+ {
652723 comp_data -> import_global_count = module -> import_global_count ;
653724 if (comp_data -> import_global_count > 0
654725 && !(comp_data -> import_globals = aot_create_import_globals (
655- module , gc_enabled , & import_global_data_size_64bit ,
656- & import_global_data_size_32bit )))
657- goto fail ;
726+ module , gc_enabled , import_global_data_size_64bit ,
727+ import_global_data_size_32bit ))) {
728+ return false;
729+ }
658730
659- /* Create globals */
660731 comp_data -> global_count = module -> global_count ;
661732 if (comp_data -> global_count
662733 && !(comp_data -> globals = aot_create_globals (
663- module , gc_enabled , import_global_data_size_64bit ,
664- import_global_data_size_32bit , & global_data_size_64bit ,
665- & global_data_size_32bit )))
666- goto fail ;
734+ module , gc_enabled , * import_global_data_size_64bit ,
735+ * import_global_data_size_32bit , global_data_size_64bit ,
736+ global_data_size_32bit ))) {
737+ return false;
738+ }
667739
668740 comp_data -> global_data_size_64bit =
669- import_global_data_size_64bit + global_data_size_64bit ;
741+ * import_global_data_size_64bit + * global_data_size_64bit ;
670742 comp_data -> global_data_size_32bit =
671- import_global_data_size_32bit + global_data_size_32bit ;
743+ * import_global_data_size_32bit + * global_data_size_32bit ;
744+
745+ return true;
746+ }
672747
673- /* Create types, they are checked by wasm loader */
748+ /**
749+ * Initialize type information in AOT compilation data.
750+ *
751+ * @param comp_data the AOT compilation data structure to initialize
752+ * @param module the source WASM module containing type information
753+ * @param is_target_x86 whether the target architecture is x86
754+ * @param gc_enabled whether garbage collection is enabled
755+ * @return true if initialization succeeded, false otherwise
756+ */
757+ static bool
758+ aot_init_types (AOTCompData * comp_data , WASMModule * module , bool is_target_x86 ,
759+ bool gc_enabled )
760+ {
674761 comp_data -> type_count = module -> type_count ;
675762 comp_data -> types = module -> types ;
676763#if WASM_ENABLE_GC != 0
677- /* Calculate the field sizes and field offsets for 64-bit and 32-bit
678- targets since they may vary in 32-bit target and 64-bit target */
679764 calculate_struct_field_sizes_offsets (comp_data , is_target_x86 , gc_enabled );
680765#endif
766+ return true;
767+ }
681768
682- /* Create import functions */
769+ /**
770+ * Initialize function information in AOT compilation data.
771+ *
772+ * @param comp_data the AOT compilation data structure to initialize
773+ * @param module the source WASM module containing function information
774+ * @param is_64bit_target whether the target architecture is 64-bit
775+ * @return true if initialization succeeded, false otherwise
776+ */
777+ static bool
778+ aot_init_functions (AOTCompData * comp_data , WASMModule * module ,
779+ bool is_64bit_target )
780+ {
683781 comp_data -> import_func_count = module -> import_function_count ;
684782 if (comp_data -> import_func_count
685- && !(comp_data -> import_funcs = aot_create_import_funcs (module )))
686- goto fail ;
783+ && !(comp_data -> import_funcs = aot_create_import_funcs (module ))) {
784+ return false;
785+ }
687786
688- /* Create functions */
689787 comp_data -> func_count = module -> function_count ;
690788 if (comp_data -> func_count
691789 && !(comp_data -> funcs =
692- aot_create_funcs (module , is_64bit_target ? 8 : 4 )))
693- goto fail ;
790+ aot_create_funcs (module , is_64bit_target ? 8 : 4 ))) {
791+ return false;
792+ }
694793
695- #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
696- /* Create custom name section */
697- comp_data -> name_section_buf = module -> name_section_buf ;
698- comp_data -> name_section_buf_end = module -> name_section_buf_end ;
699- #endif
794+ return true;
795+ }
700796
701- /* Create aux data/heap/stack information */
797+ /**
798+ * Initialize auxiliary data in AOT compilation data.
799+ *
800+ * @param comp_data the AOT compilation data structure to initialize
801+ * @param module the source WASM module containing auxiliary data
802+ */
803+ static void
804+ aot_init_aux_data (AOTCompData * comp_data , WASMModule * module )
805+ {
702806 comp_data -> aux_data_end_global_index = module -> aux_data_end_global_index ;
703807 comp_data -> aux_data_end = module -> aux_data_end ;
704808 comp_data -> aux_heap_base_global_index = module -> aux_heap_base_global_index ;
@@ -717,6 +821,43 @@ aot_create_comp_data(WASMModule *module, const char *target_arch,
717821 comp_data -> string_literal_ptrs_wp = module -> string_literal_ptrs ;
718822 comp_data -> string_literal_lengths_wp = module -> string_literal_lengths ;
719823#endif
824+ }
825+
826+ AOTCompData *
827+ aot_create_comp_data (WASMModule * module , const char * target_arch ,
828+ bool gc_enabled )
829+ {
830+ AOTCompData * comp_data ;
831+ uint32 import_global_data_size_64bit = 0 , global_data_size_64bit = 0 ;
832+ uint32 import_global_data_size_32bit = 0 , global_data_size_32bit = 0 ;
833+ bool is_64bit_target = arch_is_64bit (target_arch );
834+ bool is_target_x86 = arch_is_x86 (target_arch );
835+
836+ if (!(comp_data = wasm_runtime_malloc (sizeof (AOTCompData )))) {
837+ aot_set_last_error ("create compile data failed.\n" );
838+ return NULL ;
839+ }
840+ memset (comp_data , 0 , sizeof (AOTCompData ));
841+
842+ if (!aot_init_memories (comp_data , module )
843+ || !aot_init_memory_segments (comp_data , module )
844+ || !aot_init_tables (comp_data , module )
845+ || !aot_init_table_segments (comp_data , module )
846+ || !aot_init_globals (comp_data , module , gc_enabled ,
847+ & import_global_data_size_64bit ,
848+ & import_global_data_size_32bit ,
849+ & global_data_size_64bit , & global_data_size_32bit )
850+ || !aot_init_types (comp_data , module , is_target_x86 , gc_enabled )
851+ || !aot_init_functions (comp_data , module , is_64bit_target )) {
852+ goto fail ;
853+ }
854+
855+ #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
856+ comp_data -> name_section_buf = module -> name_section_buf ;
857+ comp_data -> name_section_buf_end = module -> name_section_buf_end ;
858+ #endif
859+
860+ aot_init_aux_data (comp_data , module );
720861
721862 comp_data -> wasm_module = module ;
722863
0 commit comments