c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Support inline assembler

Open lerno opened this issue 4 years ago • 2 comments

lerno avatar May 18 '21 17:05 lerno

I like D and Delphi/FreePascal approach:

D:

asm pure nothrow @nogc
{
     mov EDX, EAX;
     shr EAX, 1;
}

FreePascal:

procedure Set8087CW(cw: word);
begin
 default8087cw := cw;
 asm
   fnclex
   fldcw cw
 end;
end;

function Get8087CW: word; assembler;
var
  tmp: word;
asm
  fnstcw tmp
  movw   tmp, %ax
  andl   $0xffff, %eax
end;

data-man avatar May 18 '21 17:05 data-man

I do as well, but at least in the case for D that means an actual grammar, which then needs to be different depending on processor. If we look at GCC extended asm:

int a=10, b;
asm ("movl %1, %%eax; 
      movl %%eax, %0;" :"=r"(b) :"r"(a)  :"%eax" 
    ); 

The general form being: asm( <assembler> : <output> : <input> : <clobber> );

One could perhaps change this to:

asm(<output>;<input>;<clobber>)
{
 <assembler>
}

Our example above:

int a=10, b;
asm("=r"(b); "r"(a); "%eax") 
{
  movl %1, %%eax; 
  movl %%eax, %0;
}

I think most of this should lex, so that essentially one should be able to SKIP forward to the end '}' even though the internal data is mostly mumbo jumbo.

Alternatively one changes the lexing mode, causing the { } to basically act as implicit string creators.

One can also further actually decide to drop the "" from the internals of the asm arguments:

asm(=r(b); r(a); %eax) would still be possible to parse. Alternatively one pushed that down into the body of the asm, for example like this:

asm 
{
 || =r(b) : r(a) : %eax ||
  movl %1, %%eax; 
  movl %%eax, %0;
}

Note though that by pushing it down it is harder to use the variables.

Other asm parsers do have {} replacements, so:

asm(%eax) 
{
  movl %{a}, %%eax; 
  movl %%eax, %{b};
}

Although that requires the ability to specify data for the parameters elsewhere.

A downside deviating from GCC/Clang too much is also that copying C code is harder.

lerno avatar May 18 '21 18:05 lerno

@lerno I guess this issue can be closed in favor of #265.

data-man avatar Aug 29 '22 08:08 data-man

Ok, let's continue the discussion there.

lerno avatar Aug 29 '22 08:08 lerno