elliotchance / c2go

⚖️ A tool for transpiling C to Go.
MIT License
2.08k stars 154 forks source link

Inline Assembler #228

Open mrigger opened 7 years ago

mrigger commented 7 years ago

Inline assembler is not supported by c2go, for example, the code fragment below.

unsigned long rdtsc() {
  unsigned int tickl, tickh;
  asm("rdtsc":"=a"(tickl),"=d"(tickh));
  return ((long)tickh << 32)|tickl;
}

Currently, having inline assembler in a program to be converted results in an error message: panic: unknown node type: 'GCCAsmStmt 0x3a991f8 <line:5:3, col:38>'.

Note that sqlite3 also contains a similar code fragment.

cznic commented 7 years ago

Note that sqlite3 also contains a similar code fragment.

Link?

mrigger commented 7 years ago

You can download sqlite3 from https://www.sqlite.org/snapshot/sqlite-snapshot-201708251543.tar.gz and grep for rdtsc (or use this link to an unofficial repo). It contains this fragment:

#elif (defined(__GNUC__) && defined(__x86_64__))

  __inline__ sqlite_uint64 sqlite3Hwtime(void){
      unsigned long val;
      __asm__ __volatile__ ("rdtsc" : "=A" (val));
      return val;
  }
elliotchance commented 7 years ago

Thanks for reporting this. It's interesting that you ran into this error when the mac and linux versions of clang on Travis did not. It must be because the preprocessor did not output this code.

Unfortunately Go does not support inline asm, so I'm not sure what the longer term fix for this is (perhaps using configuration to stub off such functions that contain asm). However, I will put a fix for the syntax parser in today.

elliotchance commented 7 years ago

See #229

mrigger commented 7 years ago

Thanks for addressing the issue! I did not actually run into this problem on sqlite3; I'm just looking how different projects handle inline assembly. In Sulong, a C/LLVM IR interpreter on the JVM, we want to support inline assembly and have started to implement inline assembly for AMD64 by emulating its behavior in Java. For example, see the implementation of the rdtsc instructions: click me.

I'm currently doing an analysis of >1000 Github projects to determine which inline assembly instructions to implement to support as many projects as possible. rdtsc is one of the instructions that is widely used and occurs in 25% of the projects that use inline assembly. I can post a link to the study once it is completed, if you are interested.

elliotchance commented 7 years ago

That's interesting I would like to see the results of the study.

I'm not at all familiar with assembly. However if it is a requirement people need when converting their C code there are some more creative solutions that may work...

Konstantin8105 commented 6 years ago

In according to README of project https://github.com/minio/c2goasm , this is can help. Example of using in article: https://blog.minio.io/c2goasm-c-to-go-assembly-bb723d2f777f I haven't any expertise about assembler.