[CLANG][X86][inline-asm]Clang support for inline assembly MS style

Hey

I’m working on adding support for inline asm to LLVM.org.

I am trying to determine what is the best place to insert code to enable use of enumerated constants

when using Microsoft style inline asm in a similar fashion to the way macros are used.

EXAMPLE:

We should probably rewrite enums in the frontend into integer constants. I guess the fix would live in buildMSAsmString.

I do believe this will be the easiest place to implement this feature.

I’ve noticed I can pass the parser to the buildMSAsmString and use the Parser::getActions() member method as a way to access the Sema::lookupInlineAsmIdentifier. However I am still not sure I fully understand how to properly create its parameters and safely use the lookup result (of type ExprResult).

Would appreciate any suggestions or leads on what is the best approach to lookup the identifiers (in order to determine if they are enum constants).

Thanks,

Matan Haroush

After further investigation we have 2 viable locations to be considered for the final design as described below,

I’d would like to bring this up for debate, any insight you may have to offer regarding the recommended approach would be appreciated.

  1. Handle before buildMSAsmString takes the AsmTokens vector (the tokens are parsed by clang fe, buildMSAsmString is called to create the string buffer on which the AsmParser is called).

In this approach we can iterate over AsmTokens, look up each identifier token and replace tokens which refer to ConstEnumDecl with their constant value.

a. This is target independent.

b. Handling is similar handle to macro constants.

c. We can reuse ParseMSAsmIdentifier

· We also effectively parse the assembly string twice, thus we need to ignore any possible duplicate diagnostics (i.e. turn off temporarily).

· This solution was able to pass lit tests.

d. However this forces us to check each and every identifier in AsmToks vector, since clang does not know how to differentiate operands from instructions or any other identifier tokens.

  1. Handle in X86AsmParser - when the target parser iterates over each of the instructions in the asm string and construct the IR inline asm string.

a. This is target dependent

b. Handling is similar to variable operands, we can exercise one of two options:

i. Adding an input attribute to the IR string (i32 <const_val>) paired with replacing the identifier with $<reference_number> similarly to how GCC extended asm works.

GCC extended asm In ATT dialect is transformed as follows:

asm(“movl %0 + 13,%%eax" : :“i”(A)) ---- > call void asm sideeffect "movl $0 + 13,%eax”, “i,~{dirflag},~{fpsr},~{flags}”(i32 12)….” // where A is an enum constant with val =12

For MS style inline asm we would want to generate:

asm("mov eax ,A + 13") ---- > call void asm sideeffect inteldialect " mov eax ,$0 + 13 ", “i,~{dirflag},~{fpsr},~{flags}”(i32 12)….”

ii. Using the AsmRewrite mechanism to replace the identifier directly w/o folding the expression.

asm("mov eax ,A + 13") ---- > call void asm sideeffect inteldialect " mov eax ,$12 + 13 ", “~{dirflag},~{fpsr},~{flags}”….”

c. Normally the second pass of the X86AsmParser compress any remaining arithmetic expressions into a canonical form resulting in a legal instruction.

Feel free to contact me if any clarification is needed.

since this is where identifiers for variables are resolved and transformed.

ping

I agree, I think you want to do the second thing, where we hook into the existing variable lookup mechanism and replace it with an immediate operand.