Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support of MSVC __asm {} statement #262

Closed
Bertk opened this issue Jul 27, 2014 · 7 comments
Closed

support of MSVC __asm {} statement #262

Bertk opened this issue Jul 27, 2014 · 7 comments
Assignees
Milestone

Comments

@Bertk
Copy link
Contributor

Bertk commented Jul 27, 2014

Microsoft C++ uses a proprietary __asm keyword which is not handled by cxx-squid. The analysis of the source file will fail and no workaround is known so far.

Syntax:
__asm assembly-instruction [ ; ]
__asm { assembly-instruction-list } [ ; ]

The lexer shall support this Microsoft specific statement.
See also: http://jira.sonarsource.com/browse/CPP-648

@Bertk
Copy link
Contributor Author

Bertk commented Jul 27, 2014

sample file:

// http://msdn.microsoft.com/en-us/library/45yd4tzz.aspx
// Microsoft C++ support for the Standard C++ asm keyword is limited to the fact that the compiler
// will not generate an error on the keyword. However, an asm block will not generate any meaningful code.
// Use __asm instead of asm.

#define __asm asm

//  Microsoft specific asm
// Syntax:
//  __asm assembly-instruction [ ; ]
//  __asm { assembly-instruction-list } [ ; ]

// see http://msdn.microsoft.com/en-us/library/ydwz5zc6.aspx

// Power2_inline_asm.c
// compile with: /EHsc
// processor: x86

#include <stdio.h>

int power2( int num, int power );

int main( void )
{
    printf_s( "3 times 2 to the power of 5 is %d\n", power2( 3, 5) );
}
int power2( int num, int power )
{
// Microsoft specific asm

   __asm
   {
      mov eax, num
      mov ecx, power
      shl eax, cl
   }

   __asm
   {
      mov eax, num    ; Get first argument
      mov ecx, power  ; Get second argument
      shl eax, cl     ; EAX = EAX * ( 2 to the power of CL )
   }

   // Return with result in EAX
}

int alt_1 (void)
{
    int array[10];

    __asm mov array[6], bx ;
    __asm mov array[6], bx ;  Store BX at array+6 (not scaled)

    array[6] = 0;         /* Store 0 at array+24 (scaled) */
}

int alt_2 (void)
{
    __asm push ebp   __asm mov  ebp, esp   __asm sub  esp, __LOCAL_SIZE
}

@guwirth
Copy link
Collaborator

guwirth commented Jul 27, 2014

Hi,

Some hints below:

Mock compiler specific keywords with:
#define _asm asm
#define __asm asm

ISO standard defines:
asm("assembler code");

Many compiler are supporting:
//C++ Code
asm {
//Assembler Code
}
//more C++ Code

VS: http://msdn.microsoft.com/en-us/library/45yd4tzz.aspx
__asm assembly-instruction ; opt
__asm { assembly-instruction-list }; opt

GCC:
asm("assembler code");
_ asm _("assembler code");

Current parser implementation:
b.rule(asmDefinition).is(CxxKeyword.ASM, "(", STRING, ")", ";");

So yes, VS syntax is not supported. Would add support, but only with keyword asm. Others could be simulated with preprocessor.

Regards

@Bertk
Copy link
Contributor Author

Bertk commented Aug 3, 2014

An additional keyword '__asm' is not necessary and the '#define __asm asm' directive should do the job. The important part is anyway to support 'asm { assembly-instruction-list }' and 'asm assembly-instruction' syntax.

@guwirth
Copy link
Collaborator

guwirth commented Nov 20, 2014

This two cases seems to be easy to support:
__asm assembly-instruction ;
__asm { assembly-instruction-list }; opt

Interesting is this case (without semicolon at the end). How to parse for EOL?
__asm assembly-instruction

@guwirth
Copy link
Collaborator

guwirth commented Dec 27, 2014

Solved except syntax
__asm assembly-instruction // without ; at eol

@Bertk please test

@Bertk
Copy link
Contributor Author

Bertk commented Dec 28, 2014

Works fine with this preprocessor settings:

#define _asm asm
#define __asm asm

Thank you.

@guwirth guwirth self-assigned this Dec 30, 2014
@guwirth
Copy link
Collaborator

guwirth commented Dec 31, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants