haskellassemblyghcinline-assembly

Inline assembly in Haskell


Can I somehow use inline assembly in Haskell (similar to what GCC does for C)?

I want to compare my Haskell code to the reference implementation (ASM) and this seems the most straightforward way. I guess I could just call Haskell from C and use GCC inline assembly, but I'm still interested if I can do it the other way around.

(I'm on Linux/x86)


Solution

  • There are two ways:

    Both solutions use inline assembly on the C side. The former is the most idiomatic. Here's an example, from the rdtsc package:

    cycles.h:

    static __inline__ ticks getticks(void)
    {
         unsigned int tbl, tbu0, tbu1;
    
         do {
          __asm__ __volatile__ ("mftbu %0" : "=r"(tbu0));
          __asm__ __volatile__ ("mftb %0" : "=r"(tbl));
          __asm__ __volatile__ ("mftbu %0" : "=r"(tbu1));
         } while (tbu0 != tbu1);
    
         return (((unsigned long long)tbu0) << 32) | tbl;
    }
    

    rdtsc.c:

    unsigned long long rdtsc(void)
    {    
      return getticks();
    }
    

    rdtsc.h:

    unsigned long long rdtsc(void);
    

    rdtsc.hs:

    foreign import ccall unsafe "rdtsc.h" rdtsc :: IO Word64
    

    Finally: