mirror of
https://gitlab.com/freepascal.org/fpc/pas2js.git
synced 2025-08-22 11:49:06 +02:00
pastojs: bigint shl const
This commit is contained in:
parent
e3a287b9b3
commit
205431fb27
@ -6860,10 +6860,6 @@ begin
|
|||||||
// convert "a div b" to "Math.floor(a/b)"
|
// convert "a div b" to "Math.floor(a/b)"
|
||||||
Result:=CreateMathFloor(El,Result);
|
Result:=CreateMathFloor(El,Result);
|
||||||
end;
|
end;
|
||||||
eopShl,eopShr:
|
|
||||||
if (aResolver<>nil) and (LeftResolved.BaseType in [btIntDouble,btUIntDouble]) then
|
|
||||||
aResolver.LogMsg(20190228220225,mtWarning,nBitWiseOperationIs32Bit,
|
|
||||||
sBitWiseOperationIs32Bit,[],El);
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end;
|
end;
|
||||||
@ -6933,6 +6929,7 @@ var
|
|||||||
SNE: TJSEqualityExpressionSNE;
|
SNE: TJSEqualityExpressionSNE;
|
||||||
JSBinClass: TJSBinaryClass;
|
JSBinClass: TJSBinaryClass;
|
||||||
ResolvedEl: TPasResolverResult;
|
ResolvedEl: TPasResolverResult;
|
||||||
|
AInt, BInt: TMaxPrecInt;
|
||||||
begin
|
begin
|
||||||
{$IFDEF VerbosePas2JS}
|
{$IFDEF VerbosePas2JS}
|
||||||
writeln('TPasToJSConverter.ConvertBinaryExpressionRes OpCode="',OpcodeStrings[El.OpCode],'" Left=',GetResolverResultDbg(LeftResolved),' Right=',GetResolverResultDbg(RightResolved));
|
writeln('TPasToJSConverter.ConvertBinaryExpressionRes OpCode="',OpcodeStrings[El.OpCode],'" Left=',GetResolverResultDbg(LeftResolved),' Right=',GetResolverResultDbg(RightResolved));
|
||||||
@ -7004,6 +7001,61 @@ begin
|
|||||||
Call.AddArg(B); B:=nil;
|
Call.AddArg(B); B:=nil;
|
||||||
exit;
|
exit;
|
||||||
end
|
end
|
||||||
|
else if El.OpCode in [eopShl,eopShr] then
|
||||||
|
begin
|
||||||
|
if LeftResolved.BaseType in [btIntDouble,btUIntDouble] then
|
||||||
|
begin
|
||||||
|
// BigInt shl/shr JavaScript bitwise operators only supports 32bit
|
||||||
|
if IsLiteralInteger(B,BInt) then
|
||||||
|
begin
|
||||||
|
// BigInt shl/shr const
|
||||||
|
if BInt>=54 then
|
||||||
|
begin
|
||||||
|
// A shl 54 -> 0
|
||||||
|
// A shr 54 -> 0
|
||||||
|
Result:=CreateLiteralNumber(El,0);
|
||||||
|
FreeAndNil(A);
|
||||||
|
FreeAndNil(B);
|
||||||
|
exit;
|
||||||
|
end
|
||||||
|
else if BInt<=0 then
|
||||||
|
begin
|
||||||
|
// A shl 0 -> A
|
||||||
|
// A shr 0 -> A
|
||||||
|
Result:=A;
|
||||||
|
A:=nil;
|
||||||
|
FreeAndNil(B);
|
||||||
|
exit;
|
||||||
|
end
|
||||||
|
else if IsLiteralInteger(A,AInt) then
|
||||||
|
begin
|
||||||
|
// const shl const -> const
|
||||||
|
if El.OpCode=eopShl then
|
||||||
|
AInt:=AInt shl BInt
|
||||||
|
else
|
||||||
|
AInt:=AInt shr BInt;
|
||||||
|
if (AInt>=0) and (AInt<=MaxSafeIntDouble) then
|
||||||
|
begin
|
||||||
|
TJSLiteral(A).Value.AsNumber:=AInt;
|
||||||
|
Result:=A;
|
||||||
|
FreeAndNil(B);
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
end
|
||||||
|
else if El.OpCode=eopShr then
|
||||||
|
begin
|
||||||
|
// BigInt shr const -> Math.floor(A/otherconst)
|
||||||
|
Result:=CreateMathFloor(El,CreateDivideNumber(El,A,TMaxPrecInt(1) shl BInt));
|
||||||
|
A:=nil;
|
||||||
|
FreeAndNil(B);
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
// ToDo: BigInt shl const
|
||||||
|
end;
|
||||||
|
aResolver.LogMsg(20190228220225,mtWarning,nBitWiseOperationIs32Bit,
|
||||||
|
sBitWiseOperationIs32Bit,[],El);
|
||||||
|
end;
|
||||||
|
end
|
||||||
else if (LeftResolved.BaseType=btCurrency) or (RightResolved.BaseType=btCurrency) then
|
else if (LeftResolved.BaseType=btCurrency) or (RightResolved.BaseType=btCurrency) then
|
||||||
begin
|
begin
|
||||||
case El.OpCode of
|
case El.OpCode of
|
||||||
|
@ -263,7 +263,9 @@ type
|
|||||||
Procedure TestInteger;
|
Procedure TestInteger;
|
||||||
Procedure TestIntegerRange;
|
Procedure TestIntegerRange;
|
||||||
Procedure TestIntegerTypecasts;
|
Procedure TestIntegerTypecasts;
|
||||||
Procedure TestBitwiseShlNativeIntWarn;
|
Procedure TestInteger_BitwiseShrNativeInt;
|
||||||
|
Procedure TestInteger_BitwiseShlNativeInt;
|
||||||
|
Procedure TestInteger_BitwiseShlNativeIntWarn;
|
||||||
Procedure TestCurrency;
|
Procedure TestCurrency;
|
||||||
Procedure TestForBoolDo;
|
Procedure TestForBoolDo;
|
||||||
Procedure TestForIntDo;
|
Procedure TestForIntDo;
|
||||||
@ -6428,7 +6430,57 @@ begin
|
|||||||
'']));
|
'']));
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TTestModule.TestBitwiseShlNativeIntWarn;
|
procedure TTestModule.TestInteger_BitwiseShrNativeInt;
|
||||||
|
begin
|
||||||
|
StartProgram(false);
|
||||||
|
Add([
|
||||||
|
'var',
|
||||||
|
' i: nativeint;',
|
||||||
|
'begin',
|
||||||
|
' i:=i shr 0;',
|
||||||
|
' i:=i shr 1;',
|
||||||
|
' i:=i shr 3;',
|
||||||
|
' i:=i shr 54;',
|
||||||
|
'']);
|
||||||
|
ConvertProgram;
|
||||||
|
CheckResolverUnexpectedHints;
|
||||||
|
CheckSource('TestInteger_BitwiseShrNativeInt',
|
||||||
|
LinesToStr([
|
||||||
|
'this.i = 0;',
|
||||||
|
'']),
|
||||||
|
LinesToStr([
|
||||||
|
'$mod.i = $mod.i;',
|
||||||
|
'$mod.i = Math.floor($mod.i / 2);',
|
||||||
|
'$mod.i = Math.floor($mod.i / 8);',
|
||||||
|
'$mod.i = 0;',
|
||||||
|
'']));
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TTestModule.TestInteger_BitwiseShlNativeInt;
|
||||||
|
begin
|
||||||
|
StartProgram(false);
|
||||||
|
Add([
|
||||||
|
'var',
|
||||||
|
' i: nativeint;',
|
||||||
|
'begin',
|
||||||
|
' i:=i shl 0;',
|
||||||
|
' i:=i shl 54;',
|
||||||
|
' i:=123456789012 shl 1;',
|
||||||
|
'']);
|
||||||
|
ConvertProgram;
|
||||||
|
CheckResolverUnexpectedHints;
|
||||||
|
CheckSource('TestInteger_BitwiseShrNativeInt',
|
||||||
|
LinesToStr([
|
||||||
|
'this.i = 0;',
|
||||||
|
'']),
|
||||||
|
LinesToStr([
|
||||||
|
'$mod.i = $mod.i;',
|
||||||
|
'$mod.i = 0;',
|
||||||
|
'$mod.i = 246913578024;',
|
||||||
|
'']));
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TTestModule.TestInteger_BitwiseShlNativeIntWarn;
|
||||||
begin
|
begin
|
||||||
StartProgram(false);
|
StartProgram(false);
|
||||||
Add([
|
Add([
|
||||||
@ -6438,7 +6490,7 @@ begin
|
|||||||
' i:=i shl 3;',
|
' i:=i shl 3;',
|
||||||
'']);
|
'']);
|
||||||
ConvertProgram;
|
ConvertProgram;
|
||||||
CheckSource('TestBitwiseShlNativeIntWarn',
|
CheckSource('TestInteger_BitwiseShlNativeIntWarn',
|
||||||
LinesToStr([
|
LinesToStr([
|
||||||
'this.i = 0;',
|
'this.i = 0;',
|
||||||
'']),
|
'']),
|
||||||
|
Loading…
Reference in New Issue
Block a user