Static specialization done
This commit is contained in:
@@ -25,8 +25,6 @@ type
|
||||
TStaticSpecializer = class(TAstTransformer, IAstSpecializer)
|
||||
private
|
||||
FEnvironment: IEnvironment;
|
||||
FMonomorphCache: TMonomorphCache;
|
||||
FFunctionRegistry: IFunctionDefinitionRegistry;
|
||||
|
||||
function GetStaticRtlFunction(const AName: string; const AArgTypes: TArray<IStaticType>): TSpecializedMethod;
|
||||
|
||||
@@ -53,9 +51,6 @@ begin
|
||||
inherited Create;
|
||||
Assert(Assigned(AEnvironment));
|
||||
FEnvironment := AEnvironment;
|
||||
FMonomorphCache := AEnvironment.MonomorphCache;
|
||||
FFunctionRegistry := AEnvironment.FunctionRegistry; // Get registry
|
||||
Assert(Assigned(FFunctionRegistry));
|
||||
end;
|
||||
|
||||
class function TStaticSpecializer.Specialize(
|
||||
@@ -139,7 +134,7 @@ begin
|
||||
// 4. Check the Environment (Instance) Cache
|
||||
key := TMonoCacheKey.Create(calleeIdent.Address, argTypes);
|
||||
|
||||
if FMonomorphCache.TryGetValue(key, specializedMethod) then
|
||||
if FEnvironment.MonomorphCache.TryGetValue(key, specializedMethod) then
|
||||
begin
|
||||
// 4a. Cache Hit (Environment): Found user-defined or previously specialized fn
|
||||
Result :=
|
||||
@@ -158,7 +153,7 @@ begin
|
||||
if Assigned(specializedMethod.Target) then
|
||||
begin
|
||||
// 5a. Cache Hit (RTL): Found a native, static RTL function
|
||||
FMonomorphCache.Add(key, specializedMethod);
|
||||
FEnvironment.MonomorphCache.Add(key, specializedMethod);
|
||||
|
||||
Result :=
|
||||
TAst.FunctionCall(
|
||||
@@ -172,10 +167,25 @@ begin
|
||||
end;
|
||||
|
||||
// 6. Cache Miss (User Code)
|
||||
funcDef := FFunctionRegistry.Resolve(calleeIdent.Address);
|
||||
funcDef := FEnvironment.FunctionRegistry.Resolve(calleeIdent.Address);
|
||||
|
||||
if (funcDef <> nil) then
|
||||
begin
|
||||
// Cannot specialize closures or functions with nested closures
|
||||
// that might depend on the local environment being detached.
|
||||
// We check if it's a lambda expression node (which it usually is)
|
||||
if funcDef.Kind = akLambdaExpression then
|
||||
begin
|
||||
var lambdaDef := funcDef.AsLambdaExpression;
|
||||
if (Length(lambdaDef.Upvalues) > 0) or (lambdaDef.HasNestedLambdas) then
|
||||
begin
|
||||
// This function relies on local scope context. We cannot lift it
|
||||
// to a static compilation unit safely. Fallback to dynamic dispatch.
|
||||
Result := TAst.FunctionCall(newCallee, newArgs, Node.StaticType, Node.IsTailCall, nil);
|
||||
exit;
|
||||
end;
|
||||
end;
|
||||
|
||||
// 6a. Compile func with KNOWN TYPES
|
||||
var compiled := FEnvironment.Compile(funcDef, argTypes);
|
||||
|
||||
@@ -183,7 +193,7 @@ begin
|
||||
// Get the return type from the *full function type* returned by Compile
|
||||
var returnType := compiled.StaticType.Signatures[0].ReturnType;
|
||||
specializedMethod := TSpecializedMethod.Create(compiled.Func, returnType);
|
||||
FMonomorphCache.Add(key, specializedMethod);
|
||||
FEnvironment.MonomorphCache.Add(key, specializedMethod);
|
||||
|
||||
// 6e. Return the new node
|
||||
Result :=
|
||||
|
||||
Reference in New Issue
Block a user