Memory Safe Inline Assembly
NOTE: This is a pre-release feature. The Fil-C 0.679 release does not ship with this feature. To test this feature, you need to build from source.
GCC and clang both support an incredibly powerful inline assembly syntax. For example:
unsigned rotate(unsigned x, unsigned char c) { asm("roll %1, %0" : "+r"(x) : "c"(c) : "cc"); return x; }
Instructs the compiler to emit assembly based on the roll %1, %0 template, where %1 is filled in with %cl , %0 is filled in with whichever register holds x , and c is moved into the %ecx register just before the roll instruction. Additionally, the compiler is told that the instruction will change the value of x and change the value of control flags.
This seems like it cannot possibly be safe! What if the programmer did something wrong, like omitted the + in "+r" , or forgot the the "cc" clobber? In Yolo-C, if you make such a mistake, the compiler happily miscompiles your code in those cases.
Yet Fil-C supports this inline assembly syntax and it's completely safe!
This document explains why Fil-C supports inline assembly at all and then goes into the details of how that support is achieved while maintaining both programmer intent (you still get the assembly template you asked for) and complete memory safety (if you do something wrong, you'll panic or get an illegal instruction trap, at worst).
Why Inline Assembly?
While reviewing folks' C and C++ code, I've found the following reasons for inline assembly, where 1 is most common:
... continue reading