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.
- 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.
- 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.