@@ -117,14 +117,17 @@ 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 pageSizeLog2 = module ->memories .front ()->pageSizeLog2 ;
121122 Block* body = b.makeBlock ();
122123 // end = memory size in bytes
123- body->list .push_back (
124- b.makeLocalSet (end,
125- b.makeBinary (BinaryOp::MulInt32,
126- b.makeMemorySize (memory),
127- b.makeConst (Memory::kPageSize ))));
124+ body->list .push_back (b.makeLocalSet (
125+ end,
126+ pageSizeLog2 == 0
127+ ? static_cast <Expression*>(b.makeMemorySize (memoryName))
128+ : static_cast <Expression*>(b.makeBinary (BinaryOp::ShlInt32,
129+ b.makeMemorySize (memoryName),
130+ b.makeConst (pageSizeLog2)))));
128131 // if dst + size > memsize or src + size > memsize, then trap.
129132 body->list .push_back (b.makeIf (
130133 b.makeBinary (BinaryOp::OrInt32,
@@ -187,9 +190,9 @@ struct LLVMMemoryCopyFillLowering
187190 b.makeLocalGet (src, Type::i32 ),
188191 b.makeLocalGet (i, Type::i32 )),
189192 Type::i32 ,
190- memory ),
193+ memoryName ),
191194 Type::i32 ,
192- memory ),
195+ memoryName ),
193196 // i += step
194197 b.makeLocalSet (i,
195198 b.makeBinary (BinaryOp::AddInt32,
@@ -203,19 +206,23 @@ struct LLVMMemoryCopyFillLowering
203206 void createMemoryFillFunc (Module* module ) {
204207 Builder b (*module );
205208 Index dst = 0 , val = 1 , size = 2 ;
206- Name memory = module ->memories .front ()->name ;
209+ Name memoryName = module ->memories .front ()->name ;
210+ Address::address32_t pageSizeLog2 = module ->memories .front ()->pageSizeLog2 ;
207211 Block* body = b.makeBlock ();
208212
209213 // 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 ()));
214+ body->list .push_back (b.makeIf (
215+ b.makeBinary (
216+ BinaryOp::GtUInt32,
217+ b.makeBinary (BinaryOp::AddInt32,
218+ b.makeLocalGet (dst, Type::i32 ),
219+ b.makeLocalGet (size, Type::i32 )),
220+ pageSizeLog2 == 0
221+ ? static_cast <Expression*>(b.makeMemorySize (memoryName))
222+ : static_cast <Expression*>(b.makeBinary (BinaryOp::ShlInt32,
223+ b.makeMemorySize (memoryName),
224+ b.makeConst (pageSizeLog2)))),
225+ b.makeUnreachable ()));
219226
220227 body->list .push_back (b.makeBlock (
221228 " out" ,
@@ -241,7 +248,7 @@ struct LLVMMemoryCopyFillLowering
241248 b.makeLocalGet (size, Type::i32 )),
242249 b.makeLocalGet (val, Type::i32 ),
243250 Type::i32 ,
244- memory ),
251+ memoryName ),
245252 b.makeBreak (" copy" , nullptr )}))));
246253 module ->getFunction (memFillFuncName)->body = body;
247254 }
0 commit comments