BUG: Bad Code for __fastcall Function Calling an Intrinsic (114875)



The information in this article applies to:

  • Microsoft Visual C++ 1.0
  • Microsoft Visual C++ 1.5

This article was previously published under Q114875
1.00 1.50 WINDOWS kbtool kbbuglist

SYMPTOMS

The compiler generates incorrect code to call a function that uses the register calling convention when one of the parameters to the function is the return value of another function. The register calling convention can be specified by using the __fastcall keyword or by compiling with the /Gr compiler switch. The generated code may crash or behave unpredictably.

CAUSE

The problem is triggered by the embedded function call being optimized to use an intrinsic form of the function. The code generated to pass the parameter to the __fastcall function is incorrect. The intrinsic form of functions are used when the /Oi compiler switch is used or when the function is specified as intrinsic by a #pragma intrinsic() statement.

RESOLUTION

To avoid the problem, do one of the following:

  • Use the /f compiler switch instead of the /f- compiler switch.

    -or-
  • Change the function from register calling convention to another calling convention.

    -or-
  • Force the compiler not to use an intrinsic form of the embedded function by using a #pragma function () statement.

STATUS

Microsoft has confirmed this to be a bug in C/C++ for MS-DOS versions 8.0, and 8.0c. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

This is not a problem in Microsoft C/C++ 32-bit compiler, version 8.0, included in Visual C++ 32-bit Edition, version 1.0.

Sample Code

/* Compile options needed: /f- /Oi /Gs
*/ 

// The program incorrectly prints 0, 1 instead of 0, 0.

#include <stdio.h>
#include <string.h>

// #pragma function (strcmp)

int __fastcall func (int nIndex)
{
    return nIndex;
}

int main (void)
{
    printf ("%d\n", (strcmp("free", "alloc") == 0) );
    printf ("%d\n", func(strcmp("free", "alloc") == 0) );
    return 0;
}
				

Modification Type:MinorLast Reviewed:7/5/2005
Keywords:kb16bitonly KB114875