FasmOnDelphi Update + Fasm Implementation Update

This commit is contained in:
2018-04-12 22:14:08 +03:00
parent 0e25c2e70f
commit 46952fd74d
5 changed files with 64 additions and 24 deletions

2
.gitmodules vendored
View File

@@ -1,6 +1,6 @@
[submodule "FasmOnDelphi"] [submodule "FasmOnDelphi"]
path = lib/FasmOnDelphi path = lib/FasmOnDelphi
url = git@git.teamfnd.ru:artem3213212/FasmOnDelphi.git url = https://github.com/TeamFND/FasmOnDelphi.git
[submodule "lib/pLua-XE"] [submodule "lib/pLua-XE"]
path = lib/pLua-XE path = lib/pLua-XE
url = https://github.com/felipedaragon/pLua-XE.git url = https://github.com/felipedaragon/pLua-XE.git

View File

@@ -33,10 +33,11 @@ type
p:Pointer; p:Pointer;
sb:NativeUInt; sb:NativeUInt;
funcs:TDictionary<string,NativeUInt>; funcs:TDictionary<string,NativeUInt>;
regvars:TDictionary<string,TPair<NativeUInt,PTypeInfo>>;
function GetFuntion(Name:string):TRTBFunc;override; function GetFuntion(Name:string):TRTBFunc;override;
{function GetVar(Name:string):TRTBVar;override;} {function GetVar(Name:string):TRTBVar;override;}
public public
constructor Create(p:Pointer;sb:NativeUInt;funcs:TDictionary<string,NativeUInt>); constructor Create(p:Pointer;sb:NativeUInt;funcs:TDictionary<string,NativeUInt>;regvars:TDictionary<string,TPair<NativeUInt,PTypeInfo>>);
destructor Destroy;override; destructor Destroy;override;
end; end;
protected protected
@@ -69,9 +70,9 @@ type
procedure DelVariable(NameSpace:string;Name:string);override;} procedure DelVariable(NameSpace:string;Name:string);override;}
procedure &Register(NameSpace:string;Name:string;&Type:TRTBType);override; procedure &Register(NameSpace:string;Name:string;&Type:TRTBType);override;
//procedure UnRegister(NameSpace:string;Name:string;&Type:TRTBType);override; procedure UnRegister(NameSpace:string;Name:string);override;
procedure RegisterFunction(NameSpace:string;Name:string);override; procedure RegisterFunction(NameSpace:string;Name:string);override;
//procedure UnRegisterFunction(NameSpace:string;Name:string);override; procedure UnRegisterFunction(NameSpace:string;Name:string);override;
function Compilate:TRTBModule;override; function Compilate:TRTBModule;override;
destructor Destroy;override; destructor Destroy;override;
@@ -123,18 +124,18 @@ begin
Result:=TRTBFasmLibFunc.Create(GetProcAddress(Lib,pwidechar(Name))); Result:=TRTBFasmLibFunc.Create(GetProcAddress(Lib,pwidechar(Name)));
end;*) end;*)
{$POINTERMATH ON}
function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.GetFuntion(Name:string):TRTBFunc; function TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.GetFuntion(Name:string):TRTBFunc;
begin begin
Result:=TRTBFasmFunc.Create(Pointer(PNativeUInt(NativeUInt(funcs.Items[Name])+NativeUInt(p))^+NativeUInt(p))); Result:=TRTBFasmFunc.Create(Pointer(PNativeUInt(NativeUInt(funcs.Items[Name])+NativeUInt(p))^+NativeUInt(p)));
end; end;
constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.Create(p:Pointer;sb:NativeUInt;funcs:TDictionary<string,NativeUInt>); constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.Create(p:Pointer;sb:NativeUInt;funcs:TDictionary<string,NativeUInt>;regvars:TDictionary<string,TPair<NativeUInt,PTypeInfo>>);
begin begin
Self.p:=VirtualAlloc(nil,sb,MEM_COMMIT,PAGE_EXECUTE_READWRITE); Self.p:=VirtualAlloc(nil,sb,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
CopyMemory(Self.p,p,sb); CopyMemory(Self.p,p,sb);
Self.sb:=sb; Self.sb:=sb;
Self.funcs:=funcs; Self.funcs:=funcs;
Self.regvars:=regvars;
FreeMem(p); FreeMem(p);
end; end;
@@ -168,6 +169,7 @@ inherited Create(Compiler);
FText:=''; FText:='';
libs:=TStringList.Create; libs:=TStringList.Create;
funcs:=TStringList.Create; funcs:=TStringList.Create;
regvars:=TList<TPair<string,PTypeInfo>>.Create;
end; end;
{function TRTBFasmCompiler.TRTBFasmSource.LoadLib(Name:string):TRTBLib; {function TRTBFasmCompiler.TRTBFasmSource.LoadLib(Name:string):TRTBLib;
@@ -178,12 +180,24 @@ end;}
procedure TRTBFasmCompiler.TRTBFasmSource.&Register(NameSpace:string;Name:string;&Type:TRTBType); procedure TRTBFasmCompiler.TRTBFasmSource.&Register(NameSpace:string;Name:string;&Type:TRTBType);
begin begin
if NameSpace<>'' then if NameSpace<>'' then
&Register('',NameSpace+'.'+Name,&Type) regvars.Add(TPair<string,PTypeInfo>.Create(NameSpace+'.'+Name,&Type))
else else
begin regvars.Add(TPair<string,PTypeInfo>.Create(Name,&Type));
//
end; end;
procedure TRTBFasmCompiler.TRTBFasmSource.UnRegister(NameSpace:string;Name:string);
var
i:NativeUInt;
begin
if NameSpace<>'' then
Name:=NameSpace+'.'+Name;
if regvars.Count<>0 then
for i:=0 to regvars.Count-1 do
if regvars.Items[i].Key=Name then
begin
regvars.Delete(i);
Break;
end;
end; end;
procedure TRTBFasmCompiler.TRTBFasmSource.RegisterFunction(NameSpace:string;Name:string); procedure TRTBFasmCompiler.TRTBFasmSource.RegisterFunction(NameSpace:string;Name:string);
@@ -194,35 +208,58 @@ else
funcs.Add(Name); funcs.Add(Name);
end; end;
procedure TRTBFasmCompiler.TRTBFasmSource.UnRegisterFunction(NameSpace:string;Name:string);
var
i:NativeUInt;
begin
if NameSpace<>'' then
Name:=NameSpace+'.'+Name;
with funcs do
Delete(IndexOf(Name));
end;
function TRTBFasmCompiler.TRTBFasmSource.Compilate:TRTBModule; function TRTBFasmCompiler.TRTBFasmSource.Compilate:TRTBModule;
var var
templib,pointerDecl,FuncDecl:string; templib,pointerDecl,PreDecl:string;
Res:TFasmResult; Res:TFasmResult;
i:NativeUInt; i,base:NativeUInt;
FuncDict:TDictionary<string,NativeUInt>; FuncDict:TDictionary<string,NativeUInt>;
RegVarDict:TDictionary<string,TPair<NativeUInt,PTypeInfo>>;
begin begin
case SizeOf(pointer) of case SizeOf(pointer) of
2:pointerDecl:='dw '; 2:pointerDecl:='dw ';
4:pointerDecl:='dd '; 4:pointerDecl:='dd ';
8:pointerDecl:='dq '; 8:pointerDecl:='dq ';
end; end;
FuncDecl:=''; PreDecl:='';
FuncDict:=TDictionary<string,NativeUInt>.Create(); FuncDict:=TDictionary<string,NativeUInt>.Create();
for i:=0 to funcs.Count-1 do with funcs do
begin if Count<>0 then
FuncDecl:=FuncDecl+pointerDecl+funcs.Strings[i]+sLineBreak; for i:=0 to Count-1 do
FuncDict.Add(funcs.Strings[i],i*SizeOf(pointer)); begin
end; PreDecl:=PreDecl+pointerDecl+Strings[i]+sLineBreak;
Res:=FasmAssemble(FuncDecl+Text+GetIncLibs,(Compiler as TRTBFasmCompiler).CompilerMem,(Compiler as TRTBFasmCompiler).MaxSteps); FuncDict.Add(Strings[i],i*SizeOf(pointer));
end;
base:=SizeOf(pointer)*funcs.Count;
RegVarDict:=TDictionary<string,TPair<NativeUInt,PTypeInfo>>.Create();
with regvars do
if Count<>0 then
for i:=0 to Count-1 do
begin
PreDecl:=PreDecl+pointerDecl+Items[i].Key+sLineBreak;
RegVarDict.Add(Items[i].Key,TPair<NativeUInt,PTypeInfo>.Create(base+i*SizeOf(pointer),Items[i].Value));
end;
Res:=FasmAssemble(PreDecl+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:=TRTBFasmModule.Create(Res.OutData,Res.sb,FuncDict); Result:=TRTBFasmModule.Create(Res.OutData,Res.sb,FuncDict,RegVarDict);
end; end;
destructor TRTBFasmCompiler.TRTBFasmSource.Destroy; destructor TRTBFasmCompiler.TRTBFasmSource.Destroy;
begin begin
FreeAndNil(libs); FreeAndNil(libs);
FreeAndNil(funcs); FreeAndNil(funcs);
FreeAndNil(regvars);
end; end;
constructor TRTBFasmCompiler.Create(FasmPath:String=FasmPath;AsDll:boolean=false); constructor TRTBFasmCompiler.Create(FasmPath:String=FasmPath;AsDll:boolean=false);

View File

@@ -72,7 +72,7 @@ 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 UnRegister(NameSpace:string;Name:string);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; procedure UnRegisterFunction(NameSpace:string;Name:string);virtual;abstract;

View File

@@ -21,13 +21,16 @@ procedure TRuntimeBuilderTestObject.Test1();
var var
Fasm:TRTBFasmCompiler; Fasm:TRTBFasmCompiler;
Src:TRTBSource; Src:TRTBSource;
Module:TRTBModule;
Func1:TRTBFunc; Func1:TRTBFunc;
begin begin
Fasm:=TRTBFasmCompiler.Create('..\..\..\lib\FasmOnDelphi\fasmw172\fasm'); Fasm:=TRTBFasmCompiler.Create('..\..\..\lib\FasmOnDelphi\fasmw172\fasm');
Src:=Fasm.GenNewSrc; Src:=Fasm.GenNewSrc;
Src.Text:='use32'+sLineBreak+'main:'+sLineBreak+'pop ecx'+sLineBreak+'pop eax'+sLineBreak+'jmp ecx'; Src.Text:='use32'+sLineBreak+'main:'+sLineBreak+'pop ecx'+sLineBreak+'pop eax'+sLineBreak+'jmp ecx'+sLineBreak+'Pmain dd 0';
Src.RegisterFunction('','main'); Src.RegisterFunction('','main');
Func1:=src.Compilate.Funtion['main']; Src.Register('','Pmain',TypeInfo(pointer));
Module:=Src.Compilate;
Func1:=Module.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');