API Update+Base FASM Support

This commit is contained in:
2018-04-12 16:35:32 +03:00
parent 08d7761837
commit 0e25c2e70f
5 changed files with 138 additions and 80 deletions

2
.gitmodules vendored
View File

@@ -1,5 +1,5 @@
[submodule "FasmOnDelphi"] [submodule "FasmOnDelphi"]
path = FasmOnDelphi path = lib/FasmOnDelphi
url = git@git.teamfnd.ru:artem3213212/FasmOnDelphi.git url = git@git.teamfnd.ru:artem3213212/FasmOnDelphi.git
[submodule "lib/pLua-XE"] [submodule "lib/pLua-XE"]
path = lib/pLua-XE path = lib/pLua-XE

View File

@@ -5,7 +5,7 @@ interface
//{$INLINE auto} //{$INLINE auto}
uses uses
System.Classes,System.TypInfo,System.IOUtils,System.Rtti,Winapi.Windows, System.Classes,System.TypInfo,System.IOUtils,System.Rtti,System.Generics.Collections,Winapi.Windows,
RuntimeBuilder,FasmOnDelphi; RuntimeBuilder,FasmOnDelphi;
type type
@@ -13,42 +13,68 @@ type
protected type protected type
TRTBFasmSource=class(TRTBSource) TRTBFasmSource=class(TRTBSource)
protected type protected type
TRTBFasmFunc=class(TRTBFunc) TRTBFasmModule=class(TRTBModule)
protected
p:Pointer;
sb:NativeUInt;
public
constructor Create(p:Pointer;sb:NativeUInt);
function Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;override;
destructor Destroy;override;
end;
TRTBFasmLib=class(TRTBLib)
private type private type
TRTBFasmLibFunc=class(TRTBFasmFunc) TRTBFasmFunc=class(TRTBFunc)
private
p:Pointer;
public public
constructor Create(p:Pointer); constructor Create(p:Pointer);
destructor Destroy;override; function Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;override;
end; end;
{TRTBFasmVar=class(TRTBVar)
protected
function SetVal(Val:TValue);virtual;abstract;
function GetVal:TValue;virtual;abstract;
public
property Val:TValue read GetVal write SetVal;
end;}
private private
filename:string; p:Pointer;
Lib:NativeUInt; sb:NativeUInt;
funcs:TDictionary<string,NativeUInt>;
function GetFuntion(Name:string):TRTBFunc;override; function GetFuntion(Name:string):TRTBFunc;override;
{function GetVar(Name:string):TRTBVar;override;}
public public
constructor Create(Name:string); constructor Create(p:Pointer;sb:NativeUInt;funcs:TDictionary<string,NativeUInt>);
destructor Destroy;override; destructor Destroy;override;
end; end;
protected protected
libs:TStringList; libs:TStringList;
function GetIncLibs():string; funcs:TStringList;
protected regvars:TList<TPair<string,PTypeInfo>>;
FText:string; FText:string;
function GetIncLibs():string;
function GetText:string;override; function GetText:string;override;
procedure SetText(S:string);override; procedure SetText(S:string);override;
public public
function LoadLib(Name:string):TRTBLib;override;
constructor Create(Compiler:TRTBFasmCompiler); constructor Create(Compiler:TRTBFasmCompiler);
function CompilateAsFunc:TRTBFunc;override;
function CompilateAsLib:TRTBLib;override; {procedure LoadLib(Name:string);override;
procedure UnLoadLib(Name:string);override;
procedure AddNameSpace(Name:string);override;
procedure DelNameSpace(Name:string);override;
procedure AddType(NameSpace:string;Name:string;&Type:TRTBType);override;
procedure ExportType(NameSpace:string;Name:string;&Type:TRTBType);override;
procedure DelType(NameSpace:string;Name:string);override;
procedure AddConst(NameSpace:string;Name:string;Val:TValue);override;
procedure ExportConst(NameSpace:string;Name:string;Val:TValue);override;
procedure DelConst(NameSpace:string;Name:string);override;
procedure AddVariable(NameSpace:string;Name:string;var Data);override;
procedure ExportVariable(NameSpace:string;Name:string;var Data);override;
procedure DelVariable(NameSpace:string;Name:string);override;}
procedure &Register(NameSpace:string;Name:string;&Type:TRTBType);override;
//procedure UnRegister(NameSpace:string;Name:string;&Type:TRTBType);override;
procedure RegisterFunction(NameSpace:string;Name:string);override;
//procedure UnRegisterFunction(NameSpace:string;Name:string);override;
function Compilate:TRTBModule;override;
destructor Destroy;override;
end; end;
public public
CompilerMem:NativeUInt; CompilerMem:NativeUInt;
@@ -61,21 +87,19 @@ implementation
uses System.SysUtils; uses System.SysUtils;
constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmFunc.Create(p:Pointer;sb:NativeUInt); constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.TRTBFasmFunc.Create(p:Pointer);
begin begin
Self.p:=VirtualAlloc(nil,sb,MEM_COMMIT ,PAGE_EXECUTE_READWRITE); inherited Create();
CopyMemory(Self.p,p,sb); Self.p:=p;
Self.sb:=sb;
FreeMem(p);
end; end;
function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmFunc.Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue; function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.TRTBFasmFunc.Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;
begin begin
{$IFDEF CPUX64} {$IFDEF CPUX64}
Result:=Invoke(p,args,ccReg,OutType); Result:=Invoke(p,args,ccReg,OutType);
{$ELSE} {$ELSE}
case CallType of case CallType of
CRTBCallTypeRegister,CRTBCallTypeDefault:Result:=Invoke(p,args,ccReg,OutType); CRTBCallTypeRegister:Result:=Invoke(p,args,ccReg,OutType);
CRTBCallTypeStdCall:Result:=Invoke(p,args,ccStdCall,OutType); CRTBCallTypeStdCall:Result:=Invoke(p,args,ccStdCall,OutType);
CRTBCallTypeCdecl:Result:=Invoke(p,args,ccCdecl,OutType); CRTBCallTypeCdecl:Result:=Invoke(p,args,ccCdecl,OutType);
CRTBCallTypePascal:Result:=Invoke(p,args,ccPascal,OutType); CRTBCallTypePascal:Result:=Invoke(p,args,ccPascal,OutType);
@@ -84,13 +108,7 @@ end;
{$ENDIF} {$ENDIF}
end; end;
destructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmFunc.Destroy; (*constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmLib.TRTBFasmLibFunc.Create(p:Pointer);
begin
VirtualFree(p,sb,MEM_RELEASE);
p:=nil;
end;
constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmLib.TRTBFasmLibFunc.Create(p:Pointer);
begin begin
Self.p:=p; Self.p:=p;
end; end;
@@ -103,17 +121,35 @@ end;
function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmLib.GetFuntion(Name:string):TRTBFunc; function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmLib.GetFuntion(Name:string):TRTBFunc;
begin begin
Result:=TRTBFasmLibFunc.Create(GetProcAddress(Lib,pwidechar(Name))); Result:=TRTBFasmLibFunc.Create(GetProcAddress(Lib,pwidechar(Name)));
end;*)
{$POINTERMATH ON}
function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.GetFuntion(Name:string):TRTBFunc;
begin
Result:=TRTBFasmFunc.Create(Pointer(PNativeUInt(NativeUInt(funcs.Items[Name])+NativeUInt(p))^+NativeUInt(p)));
end; end;
constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmLib.Create(Name:string); constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.Create(p:Pointer;sb:NativeUInt;funcs:TDictionary<string,NativeUInt>);
begin begin
filename:=Name; Self.p:=VirtualAlloc(nil,sb,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
Lib:=LoadLibrary(pwidechar(Name)); CopyMemory(Self.p,p,sb);
Self.sb:=sb;
Self.funcs:=funcs;
FreeMem(p);
end; end;
destructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmLib.Destroy; destructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.Destroy;
begin begin
FreeLibrary(Lib); VirtualFree(p,sb,MEM_RELEASE);
end;
function TRTBFasmCompiler.TRTBFasmSource.GetIncLibs():string;
var
i:integer;
begin
Result:='';
for i:=0 to libs.count-1 do
Result:=Result+sLineBreak+'include '+#39+libs.Strings[i]+#39;
end; end;
function TRTBFasmCompiler.TRTBFasmSource.GetText:string; function TRTBFasmCompiler.TRTBFasmSource.GetText:string;
@@ -126,53 +162,72 @@ begin
FText:=S; FText:=S;
end; end;
function TRTBFasmCompiler.TRTBFasmSource.GetIncLibs():string;
var
i:integer;
begin
Result:='';
for i:=0 to libs.count-1 do
Result:=Result+sLineBreak+'include '+#39+libs.Strings[i]+#39;
end;
constructor TRTBFasmCompiler.TRTBFasmSource.Create(Compiler:TRTBFasmCompiler); constructor TRTBFasmCompiler.TRTBFasmSource.Create(Compiler:TRTBFasmCompiler);
begin begin
inherited Create(Compiler); inherited Create(Compiler);
FText:=''; FText:='';
libs:=TStringList.Create; libs:=TStringList.Create;
funcs:=TStringList.Create;
end; end;
function TRTBFasmCompiler.TRTBFasmSource.LoadLib(Name:string):TRTBLib; {function TRTBFasmCompiler.TRTBFasmSource.LoadLib(Name:string):TRTBLib;
begin begin
libs.Add(Name); libs.Add(Name);
end;}
procedure TRTBFasmCompiler.TRTBFasmSource.&Register(NameSpace:string;Name:string;&Type:TRTBType);
begin
if NameSpace<>'' then
&Register('',NameSpace+'.'+Name,&Type)
else
begin
//
end;
end; end;
function TRTBFasmCompiler.TRTBFasmSource.CompilateAsFunc:TRTBFunc; procedure TRTBFasmCompiler.TRTBFasmSource.RegisterFunction(NameSpace:string;Name:string);
var
Res:TFasmResult;
begin begin
Res:=FasmAssemble(Text+(Compiler as TRTBFasmCompiler).GetIncLibs,(Compiler as TRTBFasmCompiler).CompilerMem,(Compiler as TRTBFasmCompiler).MaxSteps); if NameSpace<>'' then
if Res.Error<>FASM_OK then funcs.Add(NameSpace+'.'+Name)
raise Exception.Create(Res.OutStr); else
Result:=TRTBFasmFunc.Create(Res.OutData,Res.sb); funcs.Add(Name);
end; end;
function TRTBFasmCompiler.TRTBFasmSource.CompilateAsLib:TRTBLib; function TRTBFasmCompiler.TRTBFasmSource.Compilate:TRTBModule;
var var
templib:string; templib,pointerDecl,FuncDecl:string;
Res:TFasmResult; Res:TFasmResult;
i:NativeUInt;
FuncDict:TDictionary<string,NativeUInt>;
begin begin
templib:=TPath.GetTempFileName; case SizeOf(pointer) of
Res:=FasmAssembleToFile({$IFDEF CPUX64}'format PE64 DLL'{$ELSE}'format PE DLL'{$ENDIF}+ 2:pointerDecl:='dw ';
(Compiler as TRTBFasmCompiler).GetIncLibs+sLineBreak+Text,templib, 4:pointerDecl:='dd ';
(Compiler as TRTBFasmCompiler).CompilerMem,(Compiler as TRTBFasmCompiler).MaxSteps); 8:pointerDecl:='dq ';
end;
FuncDecl:='';
FuncDict:=TDictionary<string,NativeUInt>.Create();
for i:=0 to funcs.Count-1 do
begin
FuncDecl:=FuncDecl+pointerDecl+funcs.Strings[i]+sLineBreak;
FuncDict.Add(funcs.Strings[i],i*SizeOf(pointer));
end;
Res:=FasmAssemble(FuncDecl+Text+GetIncLibs,(Compiler as TRTBFasmCompiler).CompilerMem,(Compiler as TRTBFasmCompiler).MaxSteps);
if Res.Error<>FASM_OK then if Res.Error<>FASM_OK then
raise Exception.Create(Res.OutStr); raise Exception.Create(Res.OutStr);
Result:=TRTBFasmLib.Create(templib); Result:=TRTBFasmModule.Create(Res.OutData,Res.sb,FuncDict);
end;
destructor TRTBFasmCompiler.TRTBFasmSource.Destroy;
begin
FreeAndNil(libs);
FreeAndNil(funcs);
end; end;
constructor TRTBFasmCompiler.Create(FasmPath:String=FasmPath;AsDll:boolean=false); constructor TRTBFasmCompiler.Create(FasmPath:String=FasmPath;AsDll:boolean=false);
begin begin
inherited Create();
CompilerMem:=1024*1024*16; CompilerMem:=1024*1024*16;
MaxSteps:=65535; MaxSteps:=65535;
OpenFASM(FasmPath,AsDll); OpenFASM(FasmPath,AsDll);

View File

@@ -6,7 +6,7 @@ uses
System.TypInfo,System.Rtti; System.TypInfo,System.Rtti;
const const
CRTBCallTypeNil=ccReg; CRTBCallTypeNil=ccStdCall;
CRTBCallTypeRegister=ccReg; CRTBCallTypeRegister=ccReg;
CRTBCallTypeStdCall=ccStdCall; CRTBCallTypeStdCall=ccStdCall;
CRTBCallTypeCdecl=ccCdecl; CRTBCallTypeCdecl=ccCdecl;
@@ -25,23 +25,24 @@ type
TRTBFunc=class abstract TRTBFunc=class abstract
public public
function Call(Name:string;OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;virtual;abstract; function Call(OutType:PTypeInfo;args:TArray<TValue>;CallType:TRTBCallType=CRTBCallTypeDefault):TValue;virtual;abstract;
end; end;
TRTBVar=class abstract TRTBVar=class abstract
protected protected
function SetVal(TValue);virtual;abstract; procedure SetVal(Val:TValue);virtual;abstract;
function GetVal:TValue;virtual;abstract; function GetVal:TValue;virtual;abstract;
public public
property Val:TValue read GetVal write SetVal; property Val:TValue read GetVal write SetVal;
end; end;
TRTBLib=class abstract TRTBModule=class abstract
protected protected
function GetFuntion(Name:string):TRTBFunc;virtual;abstract; function GetFuntion(Name:string):TRTBFunc;virtual;abstract;
function GetVar(Name:string):TRTBVar;virtual;abstract;
public public
property Funtion[Name:string]:TRTBFunc read GetFuntion; property Funtion[Name:string]:TRTBFunc read GetFuntion;
property &Var[Name:string]:TRTBVar read GetFuntion; property &Var[Name:string]:TRTBVar read GetVar;
end; end;
TRTBSource=class abstract TRTBSource=class abstract
@@ -71,11 +72,13 @@ type
procedure DelVariable(NameSpace:string;Name:string);virtual;abstract; procedure DelVariable(NameSpace:string;Name:string);virtual;abstract;
procedure &Register(NameSpace:string;Name:string;&Type:TRTBType);virtual;abstract; procedure &Register(NameSpace:string;Name:string;&Type:TRTBType);virtual;abstract;
procedure UnRegister(NameSpace:string;Name:string;&Type:TRTBType);virtual;abstract;
procedure &RegisterFunction(NameSpace:string;Name:string);virtual;abstract; procedure &RegisterFunction(NameSpace:string;Name:string);virtual;abstract;
procedure UnRegisterFunction(NameSpace:string;Name:string);virtual;abstract;
function Compilate:TRTBLib;virtual;abstract; function Compilate:TRTBModule;virtual;abstract;
procedure LoadFromFile(&File:string); procedure LoadFromFile(&File:string);virtual;
procedure SaveToFile(&File:string); procedure SaveToFile(&File:string);virtual;
property Text:string read GetText write SetText; property Text:string read GetText write SetText;
end; end;

View File

@@ -467,13 +467,13 @@
<Operation>1</Operation> <Operation>1</Operation>
</Platform> </Platform>
</DeployClass> </DeployClass>
<ProjectRoot Platform="iOSDevice32" Name="$(PROJECTNAME).app"/> <ProjectRoot Platform="iOSSimulator" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="Android" Name="$(PROJECTNAME)"/> <ProjectRoot Platform="Android" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="Win32" Name="$(PROJECTNAME)"/> <ProjectRoot Platform="Win32" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="iOSDevice64" Name="$(PROJECTNAME).app"/> <ProjectRoot Platform="iOSDevice64" Name="$(PROJECTNAME).app"/>
<ProjectRoot Platform="Win64" Name="$(PROJECTNAME)"/> <ProjectRoot Platform="Win64" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="OSX32" Name="$(PROJECTNAME)"/> <ProjectRoot Platform="OSX32" Name="$(PROJECTNAME)"/>
<ProjectRoot Platform="iOSSimulator" Name="$(PROJECTNAME).app"/> <ProjectRoot Platform="iOSDevice32" Name="$(PROJECTNAME).app"/>
</Deployment> </Deployment>
<Platforms> <Platforms>
<Platform value="OSX32">False</Platform> <Platform value="OSX32">False</Platform>

View File

@@ -23,24 +23,24 @@ var
Src:TRTBSource; Src:TRTBSource;
Func1:TRTBFunc; Func1:TRTBFunc;
begin begin
{Fasm:=TRTBFasmCompiler.Create('..\..\..\FasmOnDelphi\fasmw172\fasm'); Fasm:=TRTBFasmCompiler.Create('..\..\..\lib\FasmOnDelphi\fasmw172\fasm');
Src:=Fasm.GenNewSrc; Src:=Fasm.GenNewSrc;
Src.Text:='use32'+sLineBreak+'pop ecx'+sLineBreak+'pop eax'+sLineBreak+'jmp ecx'; Src.Text:='use32'+sLineBreak+'main:'+sLineBreak+'pop ecx'+sLineBreak+'pop eax'+sLineBreak+'jmp ecx';
Func1:=src.CompilateAsFunc; Src.RegisterFunction('','main');
Func1:=src.Compilate.Funtion['main'];
if 454<>Func1.Call(TypeInfo(integer),[454],CRTBCallTypeStdCall).AsInteger then if 454<>Func1.Call(TypeInfo(integer),[454],CRTBCallTypeStdCall).AsInteger then
begin begin
raise Exception.Create('Error in test1'); raise Exception.Create('Error in test1');
end; end;
FreeAndNil(Func1); FreeAndNil(Func1);
FreeAndNil(Src); FreeAndNil(Src);
FreeAndNil(Fasm);} FreeAndNil(Fasm);
end; end;
procedure TRuntimeBuilderTestObject.Test2(); procedure TRuntimeBuilderTestObject.Test2();
var var
Fasm:TRTBFasmCompiler; Fasm:TRTBFasmCompiler;
Src:TRTBSource; Src:TRTBSource;
lib:TRTBLib;
Func1:TRTBFunc; Func1:TRTBFunc;
begin begin
{Fasm:=TRTBFasmCompiler.Create('..\..\..\FasmOnDelphi\fasmw172\fasm'); {Fasm:=TRTBFasmCompiler.Create('..\..\..\FasmOnDelphi\fasmw172\fasm');