@@ -874,6 +874,7 @@ LLVMValueRef codegen_expr_array(CodeGenContext *ctx, AstNode *node) {
874874
875875 AstNode * * elements = node -> expr .array .elements ;
876876 size_t element_count = node -> expr .array .element_count ;
877+ size_t target_size = node -> expr .array .target_size ; // NEW: Get target size for padding
877878
878879 if (element_count == 0 ) {
879880 fprintf (stderr , "Error: Empty array literals not supported\n" );
@@ -888,16 +889,21 @@ LLVMValueRef codegen_expr_array(CodeGenContext *ctx, AstNode *node) {
888889 }
889890
890891 LLVMTypeRef element_type = LLVMTypeOf (first_element );
891- LLVMTypeRef array_type = LLVMArrayType (element_type , element_count );
892+
893+ // NEW: Use target_size if set (for padding), otherwise use actual element_count
894+ size_t actual_array_size = (target_size > 0 ) ? target_size : element_count ;
895+ LLVMTypeRef array_type = LLVMArrayType (element_type , actual_array_size );
892896
893897 // Check if all elements are constants
894898 bool all_constants = LLVMIsConstant (first_element );
899+
900+ // NEW: Allocate for the FULL size (including padding)
895901 LLVMValueRef * element_values = (LLVMValueRef * )arena_alloc (
896- ctx -> arena , sizeof (LLVMValueRef ) * element_count , alignof(LLVMValueRef ));
902+ ctx -> arena , sizeof (LLVMValueRef ) * actual_array_size , alignof(LLVMValueRef ));
897903
898904 element_values [0 ] = first_element ;
899905
900- // Generate remaining elements and check for constants
906+ // Generate provided elements
901907 for (size_t i = 1 ; i < element_count ; i ++ ) {
902908 element_values [i ] = codegen_expr (ctx , elements [i ]);
903909 if (!element_values [i ]) {
@@ -922,9 +928,21 @@ LLVMValueRef codegen_expr_array(CodeGenContext *ctx, AstNode *node) {
922928 }
923929 }
924930
925- // *** ADD THE NEW CODE HERE ***
931+ // NEW: Pad remaining elements with zeros if target_size > element_count
932+ if (target_size > element_count ) {
933+ LLVMValueRef zero_value = LLVMConstNull (element_type );
934+
935+ for (size_t i = element_count ; i < target_size ; i ++ ) {
936+ element_values [i ] = zero_value ;
937+ }
938+
939+ // If we're padding, we can't be all constants unless zeros count
940+ // (which they do, so keep checking)
941+ // zero_value is always constant, so all_constants remains unchanged
942+ }
943+
926944 // Check if any element references a global from another module
927- for (size_t i = 0 ; i < element_count && all_constants ; i ++ ) {
945+ for (size_t i = 0 ; i < actual_array_size && all_constants ; i ++ ) {
928946 if (LLVMIsConstant (element_values [i ]) &&
929947 LLVMIsAGlobalVariable (element_values [i ])) {
930948 // Check if it's from a different module
@@ -937,17 +955,16 @@ LLVMValueRef codegen_expr_array(CodeGenContext *ctx, AstNode *node) {
937955 }
938956 }
939957 }
940- // *** END NEW CODE ***
941958
942959 if (all_constants ) {
943- // Create constant array
944- return LLVMConstArray (element_type , element_values , element_count );
960+ // Create constant array (now with padding)
961+ return LLVMConstArray (element_type , element_values , actual_array_size );
945962 } else {
946- // Create runtime array
963+ // Create runtime array (now with padding)
947964 LLVMValueRef array_alloca =
948965 LLVMBuildAlloca (ctx -> builder , array_type , "array_literal" );
949966
950- for (size_t i = 0 ; i < element_count ; i ++ ) {
967+ for (size_t i = 0 ; i < actual_array_size ; i ++ ) {
951968 // Create GEP to element
952969 LLVMValueRef indices [2 ];
953970 indices [0 ] = LLVMConstInt (LLVMInt32TypeInContext (ctx -> context ), 0 , false);
0 commit comments