@@ -58,34 +58,9 @@ function* buildVariableDeclaration(
5858 if ( needsMemoryAllocation ) {
5959 // For types that need memory allocation
6060
61- // For reference types, calculate size needed
62- let sizeValue : Ir . Value ;
63-
64- // Check if we have an initializer to determine size
65- if ( decl . initializer ) {
66- // For hex literals, calculate actual size needed
67- if (
68- Ast . Expression . isLiteral ( decl . initializer ) &&
69- Ast . Expression . Literal . isHex ( decl . initializer )
70- ) {
71- const hexLiteral = decl . initializer as Ast . Expression . Literal ;
72- const hexValue = hexLiteral . value . startsWith ( "0x" )
73- ? hexLiteral . value . slice ( 2 )
74- : hexLiteral . value ;
75- const byteSize = hexValue . length / 2 ;
76- // Add 32 bytes for length prefix
77- sizeValue = Ir . Value . constant (
78- BigInt ( byteSize + 32 ) ,
79- Ir . Type . Scalar . uint256 ,
80- ) ;
81- } else {
82- // Default size for other initializers
83- sizeValue = Ir . Value . constant ( 64n , Ir . Type . Scalar . uint256 ) ;
84- }
85- } else {
86- // Default size when no initializer
87- sizeValue = Ir . Value . constant ( 64n , Ir . Type . Scalar . uint256 ) ;
88- }
61+ // A placeholder allocation, for a local declared without an
62+ // initializer; with one, the local's value is the initializer's
63+ const sizeValue = Ir . Value . constant ( 64n , Ir . Type . Scalar . uint256 ) ;
8964
9065 // Allocate memory
9166 const allocTemp = yield * Process . Variables . newTemp ( ) ;
@@ -98,14 +73,9 @@ function* buildVariableDeclaration(
9873 } as Ir . Instruction ) ;
9974
10075 // Declare the SSA variable and directly use the allocTemp as its value
101- // With an initializer other than a hex literal, the local's value
102- // is the initializer's (below), not this allocation.
103- const builtLater =
104- ! ! decl . initializer &&
105- ! (
106- Ast . Expression . isLiteral ( decl . initializer ) &&
107- Ast . Expression . Literal . isHex ( decl . initializer )
108- ) ;
76+ // With an initializer, the local's value is the initializer's
77+ // (below), not this allocation.
78+ const builtLater = ! ! decl . initializer ;
10979 yield * Process . Variables . declareWithExistingTemp (
11080 decl . name ,
11181 irType ,
@@ -120,71 +90,24 @@ function* buildVariableDeclaration(
12090 kind : "rvalue" ,
12191 } ) ;
12292
123- // For reference types, we need to handle initialization
124- // Check the initializer type to determine how to store it
125- if ( ! builtLater ) {
126- const hexLiteral = decl . initializer as Ast . Expression . Literal ;
127- if ( Ast . Expression . Literal . isHex ( hexLiteral ) ) {
128- const hexValue = hexLiteral . value . startsWith ( "0x" )
129- ? hexLiteral . value . slice ( 2 )
130- : hexLiteral . value ;
131- const byteSize = hexValue . length / 2 ;
132-
133- // Store length at the beginning
134- yield * Process . Instructions . emit ( {
135- kind : "write" ,
136- location : "memory" ,
137- offset : Ir . Value . temp ( allocTemp , Ir . Type . Scalar . uint256 ) ,
138- length : Ir . Value . constant ( 32n , Ir . Type . Scalar . uint256 ) ,
139- value : Ir . Value . constant ( BigInt ( byteSize ) , Ir . Type . Scalar . uint256 ) ,
140- operationDebug : yield * Process . Debug . forAstNode ( decl ) ,
141- } as Ir . Instruction . Write ) ;
142-
143- // Store the actual bytes data after the length
144- const dataOffsetTemp = yield * Process . Variables . newTemp ( ) ;
145- yield * Process . Instructions . emit ( {
146- kind : "binary" ,
147- op : "add" ,
148- left : Ir . Value . temp ( allocTemp , Ir . Type . Scalar . uint256 ) ,
149- right : Ir . Value . constant ( 32n , Ir . Type . Scalar . uint256 ) ,
150- dest : dataOffsetTemp ,
151- operationDebug : yield * Process . Debug . forAstNode ( decl ) ,
152- } as Ir . Instruction . BinaryOp ) ;
153-
154- yield * Process . Instructions . emit ( {
155- kind : "write" ,
156- location : "memory" ,
157- offset : Ir . Value . temp ( dataOffsetTemp , Ir . Type . Scalar . uint256 ) ,
158- length : Ir . Value . constant ( BigInt ( byteSize ) , Ir . Type . Scalar . uint256 ) ,
159- value : value ,
160- operationDebug : yield * Process . Debug . forAstNode ( decl ) ,
161- } as Ir . Instruction . Write ) ;
162- }
93+ // The value is already a reference to memory (a literal, an
94+ // array or a slice): the local refers to it
95+ if ( value . kind === "temp" ) {
96+ yield * Process . Variables . updateSsaToExistingTemp (
97+ decl . name ,
98+ value . id ,
99+ irType ,
100+ ) ;
163101 } else {
164- // For string literals, slice expressions and other bytes operations,
165- // the value is already a reference to memory
166- // We need to copy the slice result to the new allocation
167- // This is a simplified version - a full implementation would need to
168- // handle different cases more carefully
169-
170- // If the value is a temp, update the SSA variable to point to it
171- if ( value . kind === "temp" ) {
172- yield * Process . Variables . updateSsaToExistingTemp (
173- decl . name ,
174- value . id ,
175- irType ,
176- ) ;
177- } else {
178- // For non-temp values, we need to copy it
179- yield * Process . Instructions . emit ( {
180- kind : "binary" ,
181- op : "add" ,
182- left : value ,
183- right : Ir . Value . constant ( 0n , Ir . Type . Scalar . uint256 ) ,
184- dest : allocTemp ,
185- operationDebug : yield * Process . Debug . forAstNode ( decl ) ,
186- } as Ir . Instruction . BinaryOp ) ;
187- }
102+ // For non-temp values, we need to copy it
103+ yield * Process . Instructions . emit ( {
104+ kind : "binary" ,
105+ op : "add" ,
106+ left : value ,
107+ right : Ir . Value . constant ( 0n , Ir . Type . Scalar . uint256 ) ,
108+ dest : allocTemp ,
109+ operationDebug : yield * Process . Debug . forAstNode ( decl ) ,
110+ } as Ir . Instruction . BinaryOp ) ;
188111 }
189112 }
190113 } else {
0 commit comments