浏览代码

pastojs: bigint shl/shr int

git-svn-id: trunk@41594 -
Mattias Gaertner 6 年之前
父节点
当前提交
1ab29b43b7
共有 3 个文件被更改,包括 39 次插入25 次删除
  1. 14 3
      packages/pastojs/src/fppas2js.pp
  2. 6 22
      packages/pastojs/tests/tcmodules.pas
  3. 19 0
      utils/pas2js/dist/rtl.js

+ 14 - 3
packages/pastojs/src/fppas2js.pp

@@ -573,6 +573,8 @@ type
     pbifnAsExt,
     pbifnAsExt,
     pbifnBitwiseNativeIntAnd,
     pbifnBitwiseNativeIntAnd,
     pbifnBitwiseNativeIntOr,
     pbifnBitwiseNativeIntOr,
+    pbifnBitwiseNativeIntShl,
+    pbifnBitwiseNativeIntShr,
     pbifnBitwiseNativeIntXor,
     pbifnBitwiseNativeIntXor,
     pbifnCheckMethodCall,
     pbifnCheckMethodCall,
     pbifnCheckVersion,
     pbifnCheckVersion,
@@ -739,6 +741,8 @@ const
     'asExt', // rtl.asExt
     'asExt', // rtl.asExt
     'and', // pbifnBitwiseNativeIntAnd,
     'and', // pbifnBitwiseNativeIntAnd,
     'or', // pbifnBitwiseNativeIntOr,
     'or', // pbifnBitwiseNativeIntOr,
+    'shl', // pbifnBitwiseNativeIntShl,
+    'shr', // pbifnBitwiseNativeIntShr,
     'xor', // pbifnBitwiseNativeIntXor,
     'xor', // pbifnBitwiseNativeIntXor,
     'checkMethodCall',
     'checkMethodCall',
     'checkVersion',
     'checkVersion',
@@ -7212,10 +7216,17 @@ begin
           FreeAndNil(B);
           FreeAndNil(B);
           exit;
           exit;
           end;
           end;
-          // ToDo: BigInt shl const
         end;
         end;
-      aResolver.LogMsg(20190228220225,mtWarning,nBitWiseOperationIs32Bit,
-        sBitWiseOperationIs32Bit,[],El);
+      // use rtl.shl(a,b)
+      Call:=CreateCallExpression(El);
+      Result:=Call;
+      if El.OpCode=eopShl then
+        Call.Expr:=CreateMemberExpression([GetBIName(pbivnRTL),GetBIName(pbifnBitwiseNativeIntShl)])
+      else
+        Call.Expr:=CreateMemberExpression([GetBIName(pbivnRTL),GetBIName(pbifnBitwiseNativeIntShr)]);
+      Call.AddArg(A); A:=nil;
+      Call.AddArg(B); B:=nil;
+      exit;
       end;
       end;
     end
     end
   else if (LeftResolved.BaseType=btCurrency) or (RightResolved.BaseType=btCurrency) then
   else if (LeftResolved.BaseType=btCurrency) or (RightResolved.BaseType=btCurrency) then

+ 6 - 22
packages/pastojs/tests/tcmodules.pas

@@ -265,7 +265,6 @@ type
     Procedure TestIntegerTypecasts;
     Procedure TestIntegerTypecasts;
     Procedure TestInteger_BitwiseShrNativeInt;
     Procedure TestInteger_BitwiseShrNativeInt;
     Procedure TestInteger_BitwiseShlNativeInt;
     Procedure TestInteger_BitwiseShlNativeInt;
-    Procedure TestInteger_BitwiseShlNativeIntWarn;
     Procedure TestCurrency;
     Procedure TestCurrency;
     Procedure TestForBoolDo;
     Procedure TestForBoolDo;
     Procedure TestForIntDo;
     Procedure TestForIntDo;
@@ -6443,24 +6442,27 @@ begin
   StartProgram(false);
   StartProgram(false);
   Add([
   Add([
   'var',
   'var',
-  '  i: nativeint;',
+  '  i,j: nativeint;',
   'begin',
   'begin',
   '  i:=i shr 0;',
   '  i:=i shr 0;',
   '  i:=i shr 1;',
   '  i:=i shr 1;',
   '  i:=i shr 3;',
   '  i:=i shr 3;',
   '  i:=i shr 54;',
   '  i:=i shr 54;',
+  '  i:=j shr i;',
   '']);
   '']);
   ConvertProgram;
   ConvertProgram;
   CheckResolverUnexpectedHints;
   CheckResolverUnexpectedHints;
   CheckSource('TestInteger_BitwiseShrNativeInt',
   CheckSource('TestInteger_BitwiseShrNativeInt',
     LinesToStr([
     LinesToStr([
     'this.i = 0;',
     'this.i = 0;',
+    'this.j = 0;',
     '']),
     '']),
     LinesToStr([
     LinesToStr([
     '$mod.i = $mod.i;',
     '$mod.i = $mod.i;',
     '$mod.i = Math.floor($mod.i / 2);',
     '$mod.i = Math.floor($mod.i / 2);',
     '$mod.i = Math.floor($mod.i / 8);',
     '$mod.i = Math.floor($mod.i / 8);',
     '$mod.i = 0;',
     '$mod.i = 0;',
+    '$mod.i = rtl.shr($mod.j, $mod.i);',
     '']));
     '']));
 end;
 end;
 
 
@@ -6474,6 +6476,7 @@ begin
   '  i:=i shl 0;',
   '  i:=i shl 0;',
   '  i:=i shl 54;',
   '  i:=i shl 54;',
   '  i:=123456789012 shl 1;',
   '  i:=123456789012 shl 1;',
+  '  i:=i shl 1;',
   '']);
   '']);
   ConvertProgram;
   ConvertProgram;
   CheckResolverUnexpectedHints;
   CheckResolverUnexpectedHints;
@@ -6485,29 +6488,10 @@ begin
     '$mod.i = $mod.i;',
     '$mod.i = $mod.i;',
     '$mod.i = 0;',
     '$mod.i = 0;',
     '$mod.i = 246913578024;',
     '$mod.i = 246913578024;',
+    '$mod.i = rtl.shl($mod.i, 1);',
     '']));
     '']));
 end;
 end;
 
 
-procedure TTestModule.TestInteger_BitwiseShlNativeIntWarn;
-begin
-  StartProgram(false);
-  Add([
-  'var',
-  '  i: nativeint;',
-  'begin',
-  '  i:=i shl 3;',
-  '']);
-  ConvertProgram;
-  CheckSource('TestInteger_BitwiseShlNativeIntWarn',
-    LinesToStr([
-    'this.i = 0;',
-    '']),
-    LinesToStr([
-    '$mod.i = $mod.i << 3;',
-    '']));
-  CheckHint(mtWarning,nBitWiseOperationIs32Bit,sBitWiseOperationIs32Bit);
-end;
-
 procedure TTestModule.TestCurrency;
 procedure TTestModule.TestCurrency;
 begin
 begin
   StartProgram(false);
   StartProgram(false);

+ 19 - 0
utils/pas2js/dist/rtl.js

@@ -26,6 +26,8 @@ var rtl = {
     if (rtl.version != v) throw "expected rtl version "+v+", but found "+rtl.version;
     if (rtl.version != v) throw "expected rtl version "+v+", but found "+rtl.version;
   },
   },
 
 
+  hiInt: Math.pow(2,53),
+
   hasString: function(s){
   hasString: function(s){
     return rtl.isString(s) && (s.length>0);
     return rtl.isString(s) && (s.length>0);
   },
   },
@@ -1089,6 +1091,23 @@ var rtl = {
     return h*hi + l;
     return h*hi + l;
   },
   },
 
 
+  shr: function(a,b){
+    if (a<0) a += rtl.hiInt;
+    if (a<0x80000000) return a >> b;
+    if (b<=0) return a;
+    if (b>54) return 0;
+    return Math.floor(a / Math.pow(2,b));
+  },
+
+  shl: function(a,b){
+    if (a<0) a += rtl.hiInt;
+    if (b<=0) return a;
+    if (b>54) return 0;
+    var r = a * (2**b);
+    if (r <= rtl.hiInt) return r;
+    return r % rtl.hiInt;
+  },
+
   initRTTI: function(){
   initRTTI: function(){
     if (rtl.debug_rtti) rtl.debug('initRTTI');
     if (rtl.debug_rtti) rtl.debug('initRTTI');