Browse Source

Specialized fpc_copy_with_move_semantics for destructive copies.

Rika Ichinose 9 months ago
parent
commit
798d793cdc
4 changed files with 33 additions and 87 deletions
  1. 6 5
      compiler/nld.pas
  2. 3 4
      rtl/inc/compproc.inc
  3. 22 76
      rtl/inc/rtti.inc
  4. 2 2
      rtl/java/jcompproc.inc

+ 6 - 5
compiler/nld.pas

@@ -1016,16 +1016,17 @@ implementation
             not is_const(left) and
             not is_const(left) and
             not(target_info.system in systems_garbage_collected_managed_types) then
             not(target_info.system in systems_garbage_collected_managed_types) then
          begin
          begin
-           hp:=ccallparanode.create(cordconstnode.create(
-                  ord(tempreturnfromcall),pasbool1type,false),
-               ccallparanode.create(caddrnode.create_internal(
+           hp:=ccallparanode.create(caddrnode.create_internal(
                   crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
                   crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
                ccallparanode.create(ctypeconvnode.create_internal(
                ccallparanode.create(ctypeconvnode.create_internal(
                  caddrnode.create_internal(left),voidpointertype),
                  caddrnode.create_internal(left),voidpointertype),
                ccallparanode.create(ctypeconvnode.create_internal(
                ccallparanode.create(ctypeconvnode.create_internal(
                  caddrnode.create_internal(right),voidpointertype),
                  caddrnode.create_internal(right),voidpointertype),
-               nil))));
-           result:=ccallnode.createintern('fpc_copy_proc',hp);
+               nil)));
+           if tempreturnfromcall then
+             result:=ccallnode.createintern('fpc_copy_with_move_semantics_proc',hp)
+           else
+             result:=ccallnode.createintern('fpc_copy_proc',hp);
            firstpass(result);
            firstpass(result);
            left:=nil;
            left:=nil;
            right:=nil;
            right:=nil;

+ 3 - 4
rtl/inc/compproc.inc

@@ -736,12 +736,11 @@ procedure fpc_initialize_array(data,typeinfo : pointer;count : SizeInt); compile
 procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); compilerproc;
 procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); compilerproc;
 procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); compilerproc;
 procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); compilerproc;
 procedure fpc_decref_array(data,typeinfo: pointer; count: sizeint); compilerproc;
 procedure fpc_decref_array(data,typeinfo: pointer; count: sizeint); compilerproc;
-{$ifdef FPC_MANAGED_MOVE}
-Function fpc_Copy (Src, Dest, TypeInfo : Pointer; DoMove : Boolean) : SizeInt; compilerproc;
-Procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer; DoMove : Boolean); compilerproc; inline;
-{$else}
 Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt; compilerproc;
 Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt; compilerproc;
 Procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer); compilerproc; inline;
 Procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer); compilerproc; inline;
+{$ifdef FPC_MANAGED_MOVE}
+Function fpc_Copy_with_move_semantics (Src, Dest, TypeInfo : Pointer) : SizeInt; compilerproc;
+Procedure fpc_Copy_with_move_semantics_proc (Src, Dest, TypeInfo : Pointer); compilerproc; inline;
 {$endif FPC_MANAGED_MOVE}
 {$endif FPC_MANAGED_MOVE}
 {$endif FPC_HAS_FEATURE_RTTI}
 {$endif FPC_HAS_FEATURE_RTTI}
 
 

+ 22 - 76
rtl/inc/rtti.inc

@@ -249,17 +249,11 @@ begin
   end;
   end;
 end;
 end;
 
 
-{$ifdef FPC_MANAGED_MOVE}
-{ define alias for internal use in the system unit }
-Function fpc_Copy_internal (Src, Dest, TypeInfo : Pointer; DoMove : Boolean) : SizeInt;[external name 'FPC_COPY'];
 
 
-Function fpc_Copy (Src, Dest, TypeInfo : Pointer; DoMove : Boolean) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
-{$else}
 { define alias for internal use in the system unit }
 { define alias for internal use in the system unit }
 Function fpc_Copy_internal (Src, Dest, TypeInfo : Pointer) : SizeInt;[external name 'FPC_COPY'];
 Function fpc_Copy_internal (Src, Dest, TypeInfo : Pointer) : SizeInt;[external name 'FPC_COPY'];
 
 
 Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
 Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY']; compilerproc;
-{$endif FPC_MANAGED_MOVE}
 var
 var
   copiedsize,
   copiedsize,
   expectedoffset,
   expectedoffset,
@@ -273,41 +267,14 @@ begin
   case PTypeKind(TypeInfo)^ of
   case PTypeKind(TypeInfo)^ of
 {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
 {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
     tkAstring:
     tkAstring:
-{$ifdef FPC_MANAGED_MOVE}
-      if domove then
-        begin
-          fpc_AnsiStr_Decr_Ref(PPointer(Dest)^);
-          PPointer(Dest)^:=PPointer(src)^;
-          PPointer(Src)^:=nil;
-        end
-      else
-{$endif FPC_MANAGED_MOVE}
       fpc_AnsiStr_Assign(PPointer(Dest)^,PPointer(Src)^);
       fpc_AnsiStr_Assign(PPointer(Dest)^,PPointer(Src)^);
 {$endif FPC_HAS_FEATURE_ANSISTRINGS}
 {$endif FPC_HAS_FEATURE_ANSISTRINGS}
 {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
 {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
   {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
   {$ifndef FPC_WIDESTRING_EQUAL_UNICODESTRING}
     tkWstring:
     tkWstring:
-{$ifdef FPC_MANAGED_MOVE}
-      if domove then
-        begin
-          fpc_WideStr_Decr_Ref(PPointer(Dest)^);
-          PPointer(Dest)^:=PPointer(src)^;
-          PPointer(Src)^:=nil;
-        end
-      else
-{$endif FPC_MANAGED_MOVE}
       fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
       fpc_WideStr_Assign(PPointer(Dest)^,PPointer(Src)^);
   {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
   {$endif FPC_WIDESTRING_EQUAL_UNICODESTRING}
     tkUstring:
     tkUstring:
-{$ifdef FPC_MANAGED_MOVE}
-      if domove then
-        begin
-          fpc_UnicodeStr_Decr_Ref(PPointer(Dest)^);
-          PPointer(Dest)^:=PPointer(src)^;
-          PPointer(Src)^:=nil;
-        end
-      else
-{$endif FPC_MANAGED_MOVE}
       fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
       fpc_UnicodeStr_Assign(PPointer(Dest)^,PPointer(Src)^);
 {$endif FPC_HAS_FEATURE_WIDESTRINGS}
 {$endif FPC_HAS_FEATURE_WIDESTRINGS}
     tkArray:
     tkArray:
@@ -324,11 +291,7 @@ begin
         { Process elements }
         { Process elements }
         for I:=1 to EleCount do
         for I:=1 to EleCount do
           begin
           begin
-{$ifdef FPC_MANAGED_MOVE}
-            fpc_Copy_internal(Src+Offset,Dest+Offset,Info,DoMove);
-{$else}
             fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
             fpc_Copy_internal(Src+Offset,Dest+Offset,Info);
-{$endif FPC_MANAGED_MOVE}
             inc(Offset,copiedsize);
             inc(Offset,copiedsize);
           end;
           end;
       end;
       end;
@@ -339,15 +302,6 @@ begin
       begin
       begin
         Temp:=RTTIRecordInfoInit(typeinfo);
         Temp:=RTTIRecordInfoInit(typeinfo);
         Result:=PRecordInfoInit(Temp)^.Size;
         Result:=PRecordInfoInit(Temp)^.Size;
-{$ifdef FPC_MANAGED_MOVE}
-        if domove then
-          begin
-            int_finalize(Dest,TypeInfo);
-            move(src^,dest^,result);
-            int_initialize(Src,TypeInfo);
-          end
-        else
-{$endif FPC_MANAGED_MOVE}
         if Assigned(PRecordInfoInit(Temp)^.recordop) and Assigned(PRecordInfoInit(Temp)^.recordop^.Copy) then
         if Assigned(PRecordInfoInit(Temp)^.recordop) and Assigned(PRecordInfoInit(Temp)^.recordop^.Copy) then
           PRecordInfoInit(Temp)^.recordop^.Copy(Src,Dest)
           PRecordInfoInit(Temp)^.recordop^.Copy(Src,Dest)
         else
         else
@@ -362,11 +316,7 @@ begin
                 Offset:=PRecordElement(Temp)^.Offset;
                 Offset:=PRecordElement(Temp)^.Offset;
                 if Offset>expectedoffset then
                 if Offset>expectedoffset then
                   move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
                   move((Src+expectedoffset)^,(Dest+expectedoffset)^,Offset-expectedoffset);
-{$ifdef FPC_MANAGED_MOVE}
-                expectedoffset:=Offset+fpc_Copy_internal(Src+Offset,Dest+Offset,PRecordElement(Temp)^.TypeInfo^,DoMove);
-{$else}
                 expectedoffset:=Offset+fpc_Copy_internal(Src+Offset,Dest+Offset,PRecordElement(Temp)^.TypeInfo^);
                 expectedoffset:=Offset+fpc_Copy_internal(Src+Offset,Dest+Offset,PRecordElement(Temp)^.TypeInfo^);
-{$endif FPC_MANAGED_MOVE}
                 Inc(PRecordElement(Temp));
                 Inc(PRecordElement(Temp));
               end;
               end;
             { elements remaining? }
             { elements remaining? }
@@ -376,27 +326,10 @@ begin
       end;
       end;
 {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
 {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
     tkDynArray:
     tkDynArray:
-{$ifdef FPC_MANAGED_MOVE}
-      if domove then
-        begin
-          fpc_dynarray_clear(PPointer(Dest)^,TypeInfo);
-          PPointer(Dest)^:=PPointer(src)^;
-          PPointer(Src)^:=nil;
-        end
-      else
-{$endif FPC_MANAGED_MOVE}
       fpc_dynarray_assign(PPointer(Dest)^,PPointer(Src)^,typeinfo);
       fpc_dynarray_assign(PPointer(Dest)^,PPointer(Src)^,typeinfo);
 {$endif FPC_HAS_FEATURE_DYNARRAYS}
 {$endif FPC_HAS_FEATURE_DYNARRAYS}
 {$ifdef FPC_HAS_FEATURE_CLASSES}
 {$ifdef FPC_HAS_FEATURE_CLASSES}
     tkInterface:
     tkInterface:
-{$ifdef FPC_MANAGED_MOVE}
-      if domove then
-        begin
-          PPointer(Dest)^:=PPointer(src)^;
-          PPointer(Src)^:=nil;
-        end
-      else
-{$endif FPC_MANAGED_MOVE}
       fpc_intf_assign(PPointer(Dest)^,PPointer(Src)^);
       fpc_intf_assign(PPointer(Dest)^,PPointer(Src)^);
 {$endif FPC_HAS_FEATURE_CLASSES}
 {$endif FPC_HAS_FEATURE_CLASSES}
 {$ifdef FPC_HAS_FEATURE_VARIANTS}
 {$ifdef FPC_HAS_FEATURE_VARIANTS}
@@ -413,15 +346,32 @@ end;
 { For internal use by the compiler, because otherwise $x- can cause trouble. }
 { For internal use by the compiler, because otherwise $x- can cause trouble. }
 { Generally disabling extended syntax checking for all compilerprocs may     }
 { Generally disabling extended syntax checking for all compilerprocs may     }
 { have unintended side-effects                                               }
 { have unintended side-effects                                               }
+procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
+begin
+  fpc_copy_internal(src,dest,typeinfo);
+end;
+
+
 {$ifdef FPC_MANAGED_MOVE}
 {$ifdef FPC_MANAGED_MOVE}
-procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer; DoMove : Boolean);compilerproc; inline;
+function fpc_Copy_with_move_semantics (Src, Dest, TypeInfo : Pointer) : SizeInt;[Public,alias : 'FPC_COPY_WITH_MOVE_SEMANTICS']; compilerproc;
+var
+  tki : pointer;
 begin
 begin
-  fpc_copy_internal(src,dest,typeinfo,domove);
+  tki:=aligntoqword(typeInfo+2+PByte(typeInfo)[1]);
+  if PTypeKind(TypeInfo)^=tkArray then { Only tkArray, tkObject, tkRecord are possible, search for 'fpc_copy_proc' in compiler/nld.pas. }
+    result:=PArrayInfo(tki)^.Size
+  else
+    result:=PRecordInfoInit(tki)^.Size;
+  int_finalize(Dest,TypeInfo);
+  move(src^,dest^,result);
+  int_initialize(Src,TypeInfo);
 end;
 end;
-{$else}
-procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
+
+Function fpc_Copy_with_move_semantics_internal (Src, Dest, TypeInfo : Pointer) : SizeInt;[external name 'FPC_COPY_WITH_MOVE_SEMANTICS'];
+
+procedure fpc_Copy_with_move_semantics_proc (Src, Dest, TypeInfo : Pointer);compilerproc; inline;
 begin
 begin
-  fpc_copy_internal(src,dest,typeinfo);
+  fpc_Copy_with_move_semantics_internal(src,dest,typeinfo);
 end;
 end;
 {$endif FPC_MANAGED_MOVE}
 {$endif FPC_MANAGED_MOVE}
 
 
@@ -478,10 +428,6 @@ procedure CopyArray(dest, source, typeInfo: Pointer; count: SizeInt);
   begin
   begin
     if RTTIManagementAndSize(typeinfo, rotCopy, size, manBuiltin)<>manNone then
     if RTTIManagementAndSize(typeinfo, rotCopy, size, manBuiltin)<>manNone then
       for i:=0 to count-1 do
       for i:=0 to count-1 do
-{$ifdef FPC_MANAGED_MOVE}
-        fpc_Copy_internal(source+size*i, dest+size*i, typeInfo, False);
-{$else}
         fpc_Copy_internal(source+size*i, dest+size*i, typeInfo);
         fpc_Copy_internal(source+size*i, dest+size*i, typeInfo);
-{$endif FPC_MANAGED_MOVE}
   end;
   end;
 
 

+ 2 - 2
rtl/java/jcompproc.inc

@@ -701,8 +701,8 @@ procedure fpc_initialize_array(data,typeinfo : pointer;count : SizeInt); compile
 procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); compilerproc;
 procedure fpc_finalize_array(data,typeinfo : pointer;count : SizeInt); compilerproc;
 procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); compilerproc;
 procedure fpc_addref_array(data,typeinfo: pointer; count: SizeInt); compilerproc;
 procedure fpc_decref_array(data,typeinfo: pointer; count: sizeint); compilerproc;
 procedure fpc_decref_array(data,typeinfo: pointer; count: sizeint); compilerproc;
-Function fpc_Copy (Src, Dest, TypeInfo : Pointer; DoMove : Boolean) : SizeInt; compilerproc;
-Procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer; DoMove : Boolean); compilerproc; inline;
+Function fpc_Copy (Src, Dest, TypeInfo : Pointer) : SizeInt; compilerproc;
+Procedure fpc_Copy_proc (Src, Dest, TypeInfo : Pointer); compilerproc; inline;
 {$endif FPC_HAS_FEATURE_RTTI}
 {$endif FPC_HAS_FEATURE_RTTI}
 *)
 *)
 { array initialisation helpers (for open array "out" parameters whose elements
 { array initialisation helpers (for open array "out" parameters whose elements