Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: XE8 - BCC32 - code generation question



Permlink Replies: 2 - Last Post: Apr 13, 2016 10:51 AM Last Post By: david hoke Threads: [ Previous | Next ]
Jan Dijkstra

Posts: 206
Registered: 11/4/99
XE8 - BCC32 - code generation question
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 13, 2016 7:31 AM
Given the following function
void __fastcall SActiveFilter::PassThroughFilter (TObject *sender, TLicListItem *item, TItemEligable &eligable)
{
  TBfwCoreIdItem *cItem = dynamic_cast<TBfwCoreIdItem *> (item);
  if (cItem)
  {
    // Only kick in the user supplier event handler when the item gets a pass
    // from our internal filter.
    bool pass = true;
 
    if (cItem->IsActive (date))
    {
      if (! incActive) pass = false;
    }
    else
    {
      if (! incInactive) pass = false;
    }
 
    if (pass)
    {
      if (filter) filter (sender, item, eligable);
    }
    else
    {
      eligable = ieReject;
    }
  }
  else
  {
    if (filter) filter (sender, item, eligable);
  }
}

the bcc32 compiler generated the following entry code for the function
 ;
 ;	void __fastcall SActiveFilter::PassThroughFilter (TObject *sender, TLicListItem *item, TItemEligable &eligable)
 ;
	?debug L 672
@359:
	push      ebp
	mov       ebp,esp
	push      ecx
	push      ebx
	push      esi
	push      edi
	mov       edi,ecx
	*mov       dword ptr [ebp-4],edx*
        mov       esi,eax
 ;
 ;	{
 ;	  TBfwCoreIdItem *cItem = dynamic_cast<TBfwCoreIdItem *> (item);
 ;
	?debug L 674
?live16464@16: ; ESI = this, EDI = item
	mov       edx,dword ptr [@@bfwCore@TBfwCoreIdItem@]
	mov       eax,edi
	call      @__DynamicCastVCLptr$qqrpvt1
 
.....
 
?live16464@240: ;
@381:
@382:
@379:
@383:
	pop       edi
	pop       esi
	pop       ebx
	pop       ecx
	pop       ebp
	ret       4

It's the line "mov dword ptr [ebp-4],edx" my question is about.

Why did it generate that line? What it does is overrunning the value of ecx that was saved on the stack, and that gets restored with the exit code. I checked with the CPU debugger. When the function returns, the value that was in edx upon function entry is now in ecx, and the original value in ecx is lost.

I'm slightly worried that bcc32 may do that elsewhere too, breaking the assumption that values saved at function entry get restored at function exit. I know that this rule doesn't apply to eax and edx (those hold function return values).
david hoke

Posts: 616
Registered: 2/9/07
Re: XE8 - BCC32 - code generation question
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 13, 2016 10:48 AM   in response to: Jan Dijkstra in response to: Jan Dijkstra
Jan Dijkstra wrote:

and the original value in ecx is lost.

ISTR that ecx is utilized as part of fastcall calling convention, but
couldn't quickly find details.

Maybe Remy will recall (or find) more than I currently remember...
david hoke

Posts: 616
Registered: 2/9/07
Re: XE8 - BCC32 - code generation question
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 13, 2016 10:51 AM   in response to: Jan Dijkstra in response to: Jan Dijkstra
Jan Dijkstra wrote:

and the original value in ecx is lost.

still not much detail, but seems to confirm ecx usage in 32bit borland:
http://www.agner.org/optimize/calling_conventions.pdf
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02