Fix i386 indirect assignments through cast expressions
authorRobert Pengelly <robertapengelly@hotmail.com>
Tue, 6 Oct 2026 05:25:55 +0000 (06:25 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Tue, 6 Oct 2026 05:25:55 +0000 (06:25 +0100)
amd64.c
i386.c

diff --git a/amd64.c b/amd64.c
index 74319d6be08951303bda354132711e18b5ee8be6..77ecbd93f1ee5a1d5622cb7095684637e21dd6f5 100644 (file)
--- a/amd64.c
+++ b/amd64.c
@@ -80,6 +80,7 @@ static int index_step_size (int size) {
 
 static int rhs_last_pointer_depth = 0;
 static int rhs_last_pointed_size = 0;
+static int rhs_deref_cast_operand = 0;
 
 static int rhs_last_expression_is_void = 0;
 static int rhs_allow_void_function_value = 0;
@@ -21093,7 +21094,10 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) {
                      * unary '*'.  Otherwise the caller sees the unconsumed
                      * identifier/operator and reports a false "expected )".
                      */
+                    ++rhs_deref_cast_operand;
                     emit_load_assignment_rhs_to_reg (reg);
+                    
+                    --rhs_deref_cast_operand;
                     goto dereference_loaded_address;
                 
                 }
@@ -21417,6 +21421,31 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) {
         
     dereference_loaded_address:
         
+        if (tok.kind == TOK_ASSIGN) {
+        
+            get_token ();
+            
+            amd64_emit_push_reg_now (reg);
+            emit_load_assignment_rhs_expression_to_reg ("rax");
+            
+            amd64_emit_pop_reg_now ("rdx");
+            emit_store_reg_to_deref_reg_now ("rdx", "rax", deref_size);
+            
+            if (strcmp (reg, "rax") != 0 && state->ofp) {
+            
+                if (state->syntax & ASM_SYNTAX_INTEL) {
+                    fprintf (state->ofp, "    mov %s, rax\n", reg);
+                } else {
+                    fprintf (state->ofp, "    movq %%rax, %%%s\n", reg);
+                }
+            
+            }
+            
+            clear_rhs_last_pointer_info ();
+            return;
+        
+        }
+        
         deref_pointer_depth = rhs_last_pointer_depth;
         deref_pointed_size = rhs_last_pointed_size;
         
@@ -24073,7 +24102,7 @@ static void emit_load_floating_rhs_operand_now (int result_size) {
         
         }
         
-        if (is_assignment_operator (tok.kind)) {
+        if (!rhs_deref_cast_operand && is_assignment_operator (tok.kind)) {
         
             enum token_kind assign_op = tok.kind;
             
diff --git a/i386.c b/i386.c
index fbf3bf2b7a69fd24e3d82596d92571a84e7e0847..a91b8094e4dc07da26314e92b16c48a82b5177c7 100644 (file)
--- a/i386.c
+++ b/i386.c
@@ -51,6 +51,7 @@ static int index_step_size (int size) {
 
 static int rhs_last_pointer_depth = 0;
 static int rhs_last_pointed_size = 0;
+static int rhs_deref_cast_operand = 0;
 
 static int rhs_last_expression_is_void = 0;
 static int rhs_allow_void_function_value = 0;
@@ -19768,7 +19769,10 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) {
                      * unary '*'.  Otherwise the caller sees the unconsumed
                      * identifier/operator and reports a false "expected )".
                      */
+                    ++rhs_deref_cast_operand;
                     emit_load_assignment_rhs_to_reg (reg);
+                    
+                    --rhs_deref_cast_operand;
                     goto dereference_loaded_address;
                 
                 }
@@ -20063,6 +20067,40 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) {
         
     dereference_loaded_address:
         
+        if (tok.kind == TOK_ASSIGN) {
+        
+            get_token ();
+            emit_push_reg_now (reg);
+            
+            if (deref_size == (DATA_LLONG & 0x1f)) {
+            
+                emit_load_assignment_rhs_expression_to_pair ("eax", "edx", deref_is_unsigned);
+                emit_pop_reg_now ("ecx");
+                emit_store_pair_to_deref_reg_now ("ecx", "eax", "edx");
+            
+            } else {
+            
+                emit_load_assignment_rhs_expression_to_reg ("eax");
+                emit_pop_reg_now ("edx");
+                emit_store_reg_to_deref_reg_now ("edx", "eax", deref_size);
+            
+            }
+            
+            if (strcmp (reg, "eax") != 0 && state->ofp) {
+            
+                if (state->syntax & ASM_SYNTAX_INTEL) {
+                    fprintf (state->ofp, "    mov %s, eax\n", reg);
+                } else {
+                    fprintf (state->ofp, "    movl %%eax, %%%s\n", reg);
+                }
+            
+            }
+            
+            clear_rhs_last_pointer_info ();
+            return;
+        
+        }
+        
         deref_pointer_depth = rhs_last_pointer_depth;
         deref_pointed_size = rhs_last_pointed_size;
         
@@ -20875,10 +20913,11 @@ static void emit_load_assignment_rhs_to_reg (const char *reg) {
         
         }
         
-        if (is_assignment_operator (tok.kind)) {
+        if (!rhs_deref_cast_operand && is_assignment_operator (tok.kind)) {
         
             enum token_kind assign_op = tok.kind;
             struct local_symbol *dst;
+            
             int global_index;
             int dst_size;
             int dst_is_floating;
@@ -28011,7 +28050,12 @@ static int parse_cast_indirect_assignment_statement (int lvalue_outer_paren_dept
             free (addr_name);
         
         } else {
+        
+            ++rhs_deref_cast_operand;
+            
             emit_load_assignment_rhs_to_reg ("edx");
+            --rhs_deref_cast_operand;
+        
         }
         
         for (;;) {
@@ -28104,6 +28148,19 @@ static int parse_cast_indirect_assignment_statement (int lvalue_outer_paren_dept
     
     if (op == TOK_ASSIGN) {
     
+        if (deref_size == (DATA_LLONG & 0x1f)) {
+        
+            emit_push_reg_now ("edx");
+            emit_load_assignment_rhs_expression_to_pair ("eax", "edx", 1);
+            
+            emit_pop_reg_now ("ecx");
+            emit_store_pair_to_deref_reg_now ("ecx", "eax", "edx");
+            
+            expect_semi_or_recover ();
+            return 1;
+        
+        }
+        
         emit_push_reg_now ("edx");
         emit_load_assignment_rhs_expression_to_reg ("eax");
         emit_pop_reg_now ("edx");