Static specialization WIP

This commit is contained in:
Michael Schimmel
2025-11-19 14:38:40 +01:00
parent c129c1a3ae
commit 138e7ac454
26 changed files with 2349 additions and 1110 deletions
+414 -21
View File
@@ -12,68 +12,193 @@ type
// Contains the "pure" native implementations.
TRtlFunctions = record
public
[TRtlFunction('+')]
// (* --- Dynamic Fallbacks --- *)
[TRtlExport('+')]
class function Add(const Args: TArray<TDataValue>): TDataValue; overload; static;
[TRtlFunction('-')]
[TRtlExport('-')]
class function Subtract(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('*')]
[TRtlExport('*')]
class function Multiply(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('/')]
[TRtlExport('/')]
class function Divide(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('=')]
[TRtlExport('=')]
class function Equal(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('<>')]
[TRtlExport('<>')]
class function NotEqual(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('<')]
[TRtlExport('<')]
class function LessThan(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('<=')]
[TRtlExport('<=')]
class function LessThanOrEqual(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('>')]
[TRtlExport('>')]
class function GreaterThan(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('>=')]
[TRtlExport('>=')]
class function GreaterThanOrEqual(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('not')]
[TRtlExport('not')]
class function LogicalNot(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('Abs')]
// (* Dynamic fallbacks for TScalar input *)
[TRtlExport('Abs')]
class function Abs(const Arg: TScalar): TScalar; static;
[TRtlFunction('Trunc')]
[TRtlExport('Trunc')]
class function Trunc(const Arg: TScalar): TScalar; static;
[TRtlFunction('Ceil')]
[TRtlExport('Ceil')]
class function Ceil(const Arg: TScalar): TScalar; static;
[TRtlFunction('Floor')]
[TRtlExport('Floor')]
class function Floor(const Arg: TScalar): TScalar; static;
[TRtlFunction('Sign')]
[TRtlExport('Sign')]
class function Sign(const Arg: TScalar): TScalar; static;
[TRtlFunction('Memoize')]
// (* Other dynamic functions *)
[TRtlExport('Memoize')]
class function Memoize(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('Map')]
[TRtlExport('Map')]
class function Map(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('Reduce')]
[TRtlExport('Reduce')]
class function Reduce(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('Where')]
[TRtlExport('Where')]
class function Where(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlFunction('Any')]
[TRtlExport('Any')]
class function Any(const Args: TArray<TDataValue>): TDataValue; static;
// (* --- Static Specializations (for Monomorphization) --- *)
// (* Schema: FunctionName_Arg1_ArgN_Return *)
// Add
[TRtlExport('+', True)]
class function Add_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('+', True)]
class function Add_Float_Float_Float(A, B: Double): Double; static;
[TRtlExport('+', True)]
class function Add_Ordinal_Float_Float(A: Int64; B: Double): Double; static;
[TRtlExport('+', True)]
class function Add_Float_Ordinal_Float(A: Double; B: Int64): Double; static;
// Subtract
[TRtlExport('-', True)]
class function Subtract_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('-', True)]
class function Subtract_Float_Float_Float(A, B: Double): Double; static;
[TRtlExport('-', True)]
class function Subtract_Ordinal_Float_Float(A: Int64; B: Double): Double; static;
[TRtlExport('-', True)]
class function Subtract_Float_Ordinal_Float(A: Double; B: Int64): Double; static;
// Multiply
[TRtlExport('*', True)]
class function Multiply_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('*', True)]
class function Multiply_Float_Float_Float(A, B: Double): Double; static;
[TRtlExport('*', True)]
class function Multiply_Ordinal_Float_Float(A: Int64; B: Double): Double; static;
[TRtlExport('*', True)]
class function Multiply_Float_Ordinal_Float(A: Double; B: Int64): Double; static;
// Divide (NOT pure due to DivByZero)
[TRtlExport('/')]
class function Divide_Ordinal_Ordinal_Float(A, B: Int64): Double; static;
[TRtlExport('/')]
class function Divide_Float_Float_Float(A, B: Double): Double; static;
[TRtlExport('/')]
class function Divide_Ordinal_Float_Float(A: Int64; B: Double): Double; static;
[TRtlExport('/')]
class function Divide_Float_Ordinal_Float(A: Double; B: Int64): Double; static;
// Comparisons (Return Ordinal)
[TRtlExport('=', True)]
class function Equal_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('=', True)]
class function Equal_Float_Float_Ordinal(A, B: Double): Int64; static;
[TRtlExport('=', True)]
class function Equal_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64; static;
[TRtlExport('=', True)]
class function Equal_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
[TRtlExport('=', True)]
class function Equal_Keyword_Keyword_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('<>', True)]
class function NotEqual_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('<>', True)]
class function NotEqual_Float_Float_Ordinal(A, B: Double): Int64; static;
[TRtlExport('<>', True)]
class function NotEqual_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64; static;
[TRtlExport('<>', True)]
class function NotEqual_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
[TRtlExport('<>', True)]
class function NotEqual_Keyword_Keyword_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('<', True)]
class function Less_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('<', True)]
class function Less_Float_Float_Ordinal(A, B: Double): Int64; static;
[TRtlExport('<', True)]
class function Less_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64; static;
[TRtlExport('<', True)]
class function Less_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
[TRtlExport('<=', True)]
class function LessOrEqual_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('<=', True)]
class function LessOrEqual_Float_Float_Ordinal(A, B: Double): Int64; static;
[TRtlExport('<=', True)]
class function LessOrEqual_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64; static;
[TRtlExport('<=', True)]
class function LessOrEqual_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
[TRtlExport('>', True)]
class function Greater_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('>', True)]
class function Greater_Float_Float_Ordinal(A, B: Double): Int64; static;
[TRtlExport('>', True)]
class function Greater_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64; static;
[TRtlExport('>', True)]
class function Greater_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
[TRtlExport('>=', True)]
class function GreaterOrEqual_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
[TRtlExport('>=', True)]
class function GreaterOrEqual_Float_Float_Ordinal(A, B: Double): Int64; static;
[TRtlExport('>=', True)]
class function GreaterOrEqual_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64; static;
[TRtlExport('>=', True)]
class function GreaterOrEqual_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
// Unary
[TRtlExport('-', True)]
class function Negate_Ordinal_Ordinal(A: Int64): Int64; static;
[TRtlExport('-', True)]
class function Negate_Float_Float(A: Double): Double; static;
[TRtlExport('not', True)]
class function Not_Ordinal_Ordinal(A: Int64): Int64; static;
// Standard functions
[TRtlExport('Abs', True)]
class function Abs_Ordinal_Ordinal(A: Int64): Int64; static;
[TRtlExport('Abs', True)]
class function Abs_Float_Float(A: Double): Double; static;
[TRtlExport('Trunc', True)]
class function Trunc_Ordinal_Ordinal(A: Int64): Int64; static;
[TRtlExport('Trunc', True)]
class function Trunc_Float_Ordinal(A: Double): Int64; static;
end;
implementation
@@ -456,4 +581,272 @@ begin
Result := TScalar.FromInt64(0);
end;
{ TRtlFunctions - Static Specializations }
// --- Add ---
class function TRtlFunctions.Add_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64;
begin
Result := A + B;
end;
class function TRtlFunctions.Add_Float_Float_Float(A, B: Double): Double;
begin
Result := A + B;
end;
class function TRtlFunctions.Add_Ordinal_Float_Float(A: Int64; B: Double): Double;
begin
Result := A + B;
end;
class function TRtlFunctions.Add_Float_Ordinal_Float(A: Double; B: Int64): Double;
begin
Result := A + B;
end;
// --- Subtract ---
class function TRtlFunctions.Subtract_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64;
begin
Result := A - B;
end;
class function TRtlFunctions.Subtract_Float_Float_Float(A, B: Double): Double;
begin
Result := A - B;
end;
class function TRtlFunctions.Subtract_Ordinal_Float_Float(A: Int64; B: Double): Double;
begin
Result := A - B;
end;
class function TRtlFunctions.Subtract_Float_Ordinal_Float(A: Double; B: Int64): Double;
begin
Result := A - B;
end;
// --- Multiply ---
class function TRtlFunctions.Multiply_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64;
begin
Result := A * B;
end;
class function TRtlFunctions.Multiply_Float_Float_Float(A, B: Double): Double;
begin
Result := A * B;
end;
class function TRtlFunctions.Multiply_Ordinal_Float_Float(A: Int64; B: Double): Double;
begin
Result := A * B;
end;
class function TRtlFunctions.Multiply_Float_Ordinal_Float(A: Double; B: Int64): Double;
begin
Result := A * B;
end;
// --- Divide ---
class function TRtlFunctions.Divide_Ordinal_Ordinal_Float(A, B: Int64): Double;
begin
if B = 0 then
raise EDivByZero.Create('Division by zero.');
Result := A / B;
end;
class function TRtlFunctions.Divide_Float_Float_Float(A, B: Double): Double;
begin
if B = 0.0 then
raise EDivByZero.Create('Division by zero.');
Result := A / B;
end;
class function TRtlFunctions.Divide_Ordinal_Float_Float(A: Int64; B: Double): Double;
begin
if B = 0.0 then
raise EDivByZero.Create('Division by zero.');
Result := A / B;
end;
class function TRtlFunctions.Divide_Float_Ordinal_Float(A: Double; B: Int64): Double;
begin
if B = 0 then
raise EDivByZero.Create('Division by zero.');
Result := A / B;
end;
// --- Comparisons ---
(* Delphi bools: 0=False, 1=True. We return Int64 (0 or 1) *)
class function TRtlFunctions.Equal_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64;
begin
Result := Ord(A = B);
end;
class function TRtlFunctions.Equal_Float_Float_Ordinal(A, B: Double): Int64;
begin
Result := Ord(A = B); // Note: Standard float comparison
end;
class function TRtlFunctions.Equal_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64;
begin
Result := Ord(A = B);
end;
class function TRtlFunctions.Equal_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64;
begin
Result := Ord(A = B);
end;
class function TRtlFunctions.Equal_Keyword_Keyword_Ordinal(A, B: Int64): Int64;
begin
// Keywords are passed as their Int64 index
Result := Ord(A = B);
end;
class function TRtlFunctions.NotEqual_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64;
begin
Result := Ord(A <> B);
end;
class function TRtlFunctions.NotEqual_Float_Float_Ordinal(A, B: Double): Int64;
begin
Result := Ord(A <> B);
end;
class function TRtlFunctions.NotEqual_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64;
begin
Result := Ord(A <> B);
end;
class function TRtlFunctions.NotEqual_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64;
begin
Result := Ord(A <> B);
end;
class function TRtlFunctions.NotEqual_Keyword_Keyword_Ordinal(A, B: Int64): Int64;
begin
Result := Ord(A <> B);
end;
class function TRtlFunctions.Less_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64;
begin
Result := Ord(A < B);
end;
class function TRtlFunctions.Less_Float_Float_Ordinal(A, B: Double): Int64;
begin
Result := Ord(A < B);
end;
class function TRtlFunctions.Less_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64;
begin
Result := Ord(A < B);
end;
class function TRtlFunctions.Less_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64;
begin
Result := Ord(A < B);
end;
class function TRtlFunctions.LessOrEqual_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64;
begin
Result := Ord(A <= B);
end;
class function TRtlFunctions.LessOrEqual_Float_Float_Ordinal(A, B: Double): Int64;
begin
Result := Ord(A <= B);
end;
class function TRtlFunctions.LessOrEqual_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64;
begin
Result := Ord(A <= B);
end;
class function TRtlFunctions.LessOrEqual_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64;
begin
Result := Ord(A <= B);
end;
class function TRtlFunctions.Greater_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64;
begin
Result := Ord(A > B);
end;
class function TRtlFunctions.Greater_Float_Float_Ordinal(A, B: Double): Int64;
begin
Result := Ord(A > B);
end;
class function TRtlFunctions.Greater_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64;
begin
Result := Ord(A > B);
end;
class function TRtlFunctions.Greater_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64;
begin
Result := Ord(A > B);
end;
class function TRtlFunctions.GreaterOrEqual_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64;
begin
Result := Ord(A >= B);
end;
class function TRtlFunctions.GreaterOrEqual_Float_Float_Ordinal(A, B: Double): Int64;
begin
Result := Ord(A >= B);
end;
class function TRtlFunctions.GreaterOrEqual_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64;
begin
Result := Ord(A >= B);
end;
class function TRtlFunctions.GreaterOrEqual_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64;
begin
Result := Ord(A >= B);
end;
// --- Unary ---
class function TRtlFunctions.Negate_Ordinal_Ordinal(A: Int64): Int64;
begin
Result := -A;
end;
class function TRtlFunctions.Negate_Float_Float(A: Double): Double;
begin
Result := -A;
end;
class function TRtlFunctions.Not_Ordinal_Ordinal(A: Int64): Int64;
begin
// Lisp-style 'not': 0 -> 1, everything else -> 0
Result := Ord(A = 0);
end;
// --- Standard functions ---
class function TRtlFunctions.Abs_Ordinal_Ordinal(A: Int64): Int64;
begin
Result := System.Abs(A);
end;
class function TRtlFunctions.Abs_Float_Float(A: Double): Double;
begin
Result := System.Abs(A);
end;
class function TRtlFunctions.Trunc_Ordinal_Ordinal(A: Int64): Int64;
begin
Result := A; // No-op
end;
class function TRtlFunctions.Trunc_Float_Ordinal(A: Double): Int64;
begin
Result := System.Trunc(A);
end;
end.