@@ -1177,22 +1177,40 @@ function builtin_rotl(ctx: BuiltinContext): ExpressionRef {
1177
1177
case TypeKind . U8 :
1178
1178
case TypeKind . U16 : {
1179
1179
// (value << (shift & mask)) | (value >>> ((0 - shift) & mask))
1180
- return module . binary ( BinaryOp . OrI32 ,
1180
+ let flow = compiler . currentFlow ;
1181
+ let temp1 = flow . getTempLocal ( type ) ;
1182
+ flow . setLocalFlag ( temp1 . index , LocalFlags . WRAPPED ) ;
1183
+ let temp2 = flow . getTempLocal ( type ) ;
1184
+ flow . setLocalFlag ( temp2 . index , LocalFlags . WRAPPED ) ;
1185
+
1186
+ let ret = module . binary ( BinaryOp . OrI32 ,
1181
1187
module . binary (
1182
1188
BinaryOp . ShlI32 ,
1183
- arg0 ,
1184
- module . binary ( BinaryOp . AndI32 , arg1 , module . i32 ( type . size - 1 ) )
1189
+ module . local_tee ( temp1 . index , arg0 ) ,
1190
+ module . binary (
1191
+ BinaryOp . AndI32 ,
1192
+ module . local_tee ( temp2 . index , arg1 ) ,
1193
+ module . i32 ( type . size - 1 )
1194
+ )
1185
1195
) ,
1186
1196
module . binary (
1187
1197
BinaryOp . ShrU32 ,
1188
- arg0 ,
1198
+ module . local_get ( temp1 . index , NativeType . I32 ) ,
1189
1199
module . binary (
1190
1200
BinaryOp . AndI32 ,
1191
- module . binary ( BinaryOp . SubI32 , module . i32 ( 0 ) , arg1 ) ,
1201
+ module . binary (
1202
+ BinaryOp . SubI32 ,
1203
+ module . i32 ( 0 ) ,
1204
+ module . local_get ( temp2 . index , NativeType . I32 )
1205
+ ) ,
1192
1206
module . i32 ( type . size - 1 )
1193
1207
)
1194
1208
)
1195
1209
) ;
1210
+ flow . freeTempLocal ( temp2 ) ;
1211
+ flow . freeTempLocal ( temp1 ) ;
1212
+
1213
+ return ret ;
1196
1214
}
1197
1215
case TypeKind . I32 :
1198
1216
case TypeKind . U32 : return module . binary ( BinaryOp . RotlI32 , arg0 , arg1 ) ;
@@ -1240,22 +1258,40 @@ function builtin_rotr(ctx: BuiltinContext): ExpressionRef {
1240
1258
case TypeKind . U8 :
1241
1259
case TypeKind . U16 : {
1242
1260
// (value >>> (shift & mask)) | (value << ((0 - shift) & mask))
1243
- return module . binary ( BinaryOp . OrI32 ,
1261
+ let flow = compiler . currentFlow ;
1262
+ let temp1 = flow . getTempLocal ( type ) ;
1263
+ flow . setLocalFlag ( temp1 . index , LocalFlags . WRAPPED ) ;
1264
+ let temp2 = flow . getTempLocal ( type ) ;
1265
+ flow . setLocalFlag ( temp2 . index , LocalFlags . WRAPPED ) ;
1266
+
1267
+ let ret = module . binary ( BinaryOp . OrI32 ,
1244
1268
module . binary (
1245
1269
BinaryOp . ShrU32 ,
1246
- arg0 ,
1247
- module . binary ( BinaryOp . AndI32 , arg1 , module . i32 ( type . size - 1 ) )
1270
+ module . local_tee ( temp1 . index , arg0 ) ,
1271
+ module . binary (
1272
+ BinaryOp . AndI32 ,
1273
+ module . local_tee ( temp2 . index , arg1 ) ,
1274
+ module . i32 ( type . size - 1 )
1275
+ )
1248
1276
) ,
1249
1277
module . binary (
1250
1278
BinaryOp . ShlI32 ,
1251
- arg0 ,
1279
+ module . local_get ( temp1 . index , NativeType . I32 ) ,
1252
1280
module . binary (
1253
1281
BinaryOp . AndI32 ,
1254
- module . binary ( BinaryOp . SubI32 , module . i32 ( 0 ) , arg1 ) ,
1282
+ module . binary (
1283
+ BinaryOp . SubI32 ,
1284
+ module . i32 ( 0 ) ,
1285
+ module . local_get ( temp2 . index , NativeType . I32 )
1286
+ ) ,
1255
1287
module . i32 ( type . size - 1 )
1256
1288
)
1257
1289
)
1258
1290
) ;
1291
+ flow . freeTempLocal ( temp2 ) ;
1292
+ flow . freeTempLocal ( temp1 ) ;
1293
+
1294
+ return ret ;
1259
1295
}
1260
1296
case TypeKind . I32 :
1261
1297
case TypeKind . U32 : return module . binary ( BinaryOp . RotrI32 , arg0 , arg1 ) ;
0 commit comments