@@ -117,14 +117,19 @@ struct LLVMMemoryCopyFillLowering
117117 void createMemoryCopyFunc (Module* module ) {
118118 Builder b (*module );
119119 Index dst = 0 , src = 1 , size = 2 , start = 3 , end = 4 , step = 5 , i = 6 ;
120- Name memory = module ->memories .front ()->name ;
120+ Name memoryName = module ->memories .front ()->name ;
121+ Address::address32_t memory_page_size =
122+ module ->memories .front ()->pageSizeLog2 ;
121123 Block* body = b.makeBlock ();
122124 // end = memory size in bytes
123125 body->list .push_back (
124126 b.makeLocalSet (end,
125- b.makeBinary (BinaryOp::MulInt32,
126- b.makeMemorySize (memory),
127- b.makeConst (Memory::kPageSize ))));
127+ memory_page_size == 0
128+ ? static_cast <Expression*>(b.makeMemorySize (memoryName))
129+ : static_cast <Expression*>(
130+ b.makeBinary (BinaryOp::ShlInt32,
131+ b.makeMemorySize (memoryName),
132+ b.makeConst (memory_page_size)))));
128133 // if dst + size > memsize or src + size > memsize, then trap.
129134 body->list .push_back (b.makeIf (
130135 b.makeBinary (BinaryOp::OrInt32,
@@ -187,9 +192,9 @@ struct LLVMMemoryCopyFillLowering
187192 b.makeLocalGet (src, Type::i32 ),
188193 b.makeLocalGet (i, Type::i32 )),
189194 Type::i32 ,
190- memory ),
195+ memoryName ),
191196 Type::i32 ,
192- memory ),
197+ memoryName ),
193198 // i += step
194199 b.makeLocalSet (i,
195200 b.makeBinary (BinaryOp::AddInt32,
@@ -203,19 +208,24 @@ struct LLVMMemoryCopyFillLowering
203208 void createMemoryFillFunc (Module* module ) {
204209 Builder b (*module );
205210 Index dst = 0 , val = 1 , size = 2 ;
206- Name memory = module ->memories .front ()->name ;
211+ Name memoryName = module ->memories .front ()->name ;
212+ Address::address32_t memory_page_size =
213+ module ->memories .front ()->pageSizeLog2 ;
207214 Block* body = b.makeBlock ();
208215
209216 // if dst + size > memsize in bytes, then trap.
210- body->list .push_back (
211- b.makeIf (b.makeBinary (BinaryOp::GtUInt32,
212- b.makeBinary (BinaryOp::AddInt32,
213- b.makeLocalGet (dst, Type::i32 ),
214- b.makeLocalGet (size, Type::i32 )),
215- b.makeBinary (BinaryOp::MulInt32,
216- b.makeMemorySize (memory),
217- b.makeConst (Memory::kPageSize ))),
218- b.makeUnreachable ()));
217+ body->list .push_back (b.makeIf (
218+ b.makeBinary (BinaryOp::GtUInt32,
219+ b.makeBinary (BinaryOp::AddInt32,
220+ b.makeLocalGet (dst, Type::i32 ),
221+ b.makeLocalGet (size, Type::i32 )),
222+ memory_page_size == 0
223+ ? static_cast <Expression*>(b.makeMemorySize (memoryName))
224+ : static_cast <Expression*>(
225+ b.makeBinary (BinaryOp::ShlInt32,
226+ b.makeMemorySize (memoryName),
227+ b.makeConst (memory_page_size)))),
228+ b.makeUnreachable ()));
219229
220230 body->list .push_back (b.makeBlock (
221231 " out" ,
@@ -241,7 +251,7 @@ struct LLVMMemoryCopyFillLowering
241251 b.makeLocalGet (size, Type::i32 )),
242252 b.makeLocalGet (val, Type::i32 ),
243253 Type::i32 ,
244- memory ),
254+ memoryName ),
245255 b.makeBreak (" copy" , nullptr )}))));
246256 module ->getFunction (memFillFuncName)->body = body;
247257 }
0 commit comments