diff --git a/.gitmodules b/.gitmodules index 9998281..61a2917 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "FasmOnDelphi"] path = lib/FasmOnDelphi - url = git@git.teamfnd.ru:artem3213212/FasmOnDelphi.git + url = https://github.com/TeamFND/FasmOnDelphi.git [submodule "lib/pLua-XE"] path = lib/pLua-XE url = https://github.com/felipedaragon/pLua-XE.git diff --git a/Source/RuntimeBuilder.Fasm.pas b/Source/RuntimeBuilder.Fasm.pas index 3dac9f0..162f608 100644 --- a/Source/RuntimeBuilder.Fasm.pas +++ b/Source/RuntimeBuilder.Fasm.pas @@ -33,10 +33,11 @@ type p:Pointer; sb:NativeUInt; funcs:TDictionary; + regvars:TDictionary>; function GetFuntion(Name:string):TRTBFunc;override; {function GetVar(Name:string):TRTBVar;override;} public - constructor Create(p:Pointer;sb:NativeUInt;funcs:TDictionary); + constructor Create(p:Pointer;sb:NativeUInt;funcs:TDictionary;regvars:TDictionary>); destructor Destroy;override; end; protected @@ -69,9 +70,9 @@ type 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 UnRegister(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; destructor Destroy;override; @@ -123,18 +124,18 @@ begin 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; -constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.Create(p:Pointer;sb:NativeUInt;funcs:TDictionary); +constructor TRTBFasmCompiler.TRTBFasmSource.TRTBFasmModule.Create(p:Pointer;sb:NativeUInt;funcs:TDictionary;regvars:TDictionary>); begin Self.p:=VirtualAlloc(nil,sb,MEM_COMMIT,PAGE_EXECUTE_READWRITE); CopyMemory(Self.p,p,sb); Self.sb:=sb; Self.funcs:=funcs; +Self.regvars:=regvars; FreeMem(p); end; @@ -168,6 +169,7 @@ inherited Create(Compiler); FText:=''; libs:=TStringList.Create; funcs:=TStringList.Create; +regvars:=TList>.Create; end; {function TRTBFasmCompiler.TRTBFasmSource.LoadLib(Name:string):TRTBLib; @@ -178,12 +180,24 @@ end;} procedure TRTBFasmCompiler.TRTBFasmSource.&Register(NameSpace:string;Name:string;&Type:TRTBType); begin if NameSpace<>'' then - &Register('',NameSpace+'.'+Name,&Type) + regvars.Add(TPair.Create(NameSpace+'.'+Name,&Type)) else -begin - - // + regvars.Add(TPair.Create(Name,&Type)); 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; procedure TRTBFasmCompiler.TRTBFasmSource.RegisterFunction(NameSpace:string;Name:string); @@ -194,35 +208,58 @@ else funcs.Add(Name); 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; var - templib,pointerDecl,FuncDecl:string; + templib,pointerDecl,PreDecl:string; Res:TFasmResult; - i:NativeUInt; + i,base:NativeUInt; FuncDict:TDictionary; + RegVarDict:TDictionary>; begin case SizeOf(pointer) of 2:pointerDecl:='dw '; 4:pointerDecl:='dd '; 8:pointerDecl:='dq '; end; -FuncDecl:=''; +PreDecl:=''; FuncDict:=TDictionary.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); +with funcs do + if Count<>0 then + for i:=0 to Count-1 do + begin + PreDecl:=PreDecl+pointerDecl+Strings[i]+sLineBreak; + FuncDict.Add(Strings[i],i*SizeOf(pointer)); + end; +base:=SizeOf(pointer)*funcs.Count; +RegVarDict:=TDictionary>.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.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 raise Exception.Create(Res.OutStr); -Result:=TRTBFasmModule.Create(Res.OutData,Res.sb,FuncDict); +Result:=TRTBFasmModule.Create(Res.OutData,Res.sb,FuncDict,RegVarDict); end; destructor TRTBFasmCompiler.TRTBFasmSource.Destroy; begin FreeAndNil(libs); FreeAndNil(funcs); +FreeAndNil(regvars); end; constructor TRTBFasmCompiler.Create(FasmPath:String=FasmPath;AsDll:boolean=false); diff --git a/Source/RuntimeBuilder.pas b/Source/RuntimeBuilder.pas index 9dedae3..a709878 100644 --- a/Source/RuntimeBuilder.pas +++ b/Source/RuntimeBuilder.pas @@ -72,7 +72,7 @@ type procedure DelVariable(NameSpace:string;Name:string);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 UnRegisterFunction(NameSpace:string;Name:string);virtual;abstract; diff --git a/Tests/Unit1.pas b/Tests/Unit1.pas index f9b6759..f90a8f3 100644 --- a/Tests/Unit1.pas +++ b/Tests/Unit1.pas @@ -21,13 +21,16 @@ procedure TRuntimeBuilderTestObject.Test1(); var Fasm:TRTBFasmCompiler; Src:TRTBSource; + Module:TRTBModule; Func1:TRTBFunc; begin Fasm:=TRTBFasmCompiler.Create('..\..\..\lib\FasmOnDelphi\fasmw172\fasm'); 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'); -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 begin raise Exception.Create('Error in test1'); diff --git a/lib/FasmOnDelphi b/lib/FasmOnDelphi index dd1e5ff..acb5bd9 160000 --- a/lib/FasmOnDelphi +++ b/lib/FasmOnDelphi @@ -1 +1 @@ -Subproject commit dd1e5ff80a517e7fd0bc5c6df39df842789f4fc0 +Subproject commit acb5bd9c9b6f2bebf0d7e134e2978118a1a88b3c