Mixin C
Iain Buclaw
ibuclaw at gdcproject.org
Fri Mar 8 12:13:23 UTC 2024
On Friday, 8 March 2024 at 03:23:12 UTC, Paul Backus wrote:
> ```d
> module libwhatever;
>
> mixin(C) {
> #include <libwhatever.h>
> }
> ```
>
> I haven't spent much time fleshing out the details of this
> idea, but it seems pretty promising. What do you guys think?
The first thing that comes to mind is that it'll allow doing
things inline that would otherwise be impossible in D.
```d
import std.stdio;
extern(C) int func(int x);
mixin(C) {
// gdc supports these asm-declarations via gcc (ldc should
too via clang)
asm("
.globl func
.type func, @function
func:
.cfi_startproc
movl %edi, %eax
addl $1, %eax
ret
.cfi_endproc
");
}
int main()
{
int n = func(72);
// mixin(C) not necessary as gdc+ldc support this natively
with asm{""::;}
mixin(C) {
asm ("leal (%0,%0,4),%0"
: "=r" (n)
: "0" (n));
}
writeln("73*5 = ", n); // 73*5 = 365
// ditto, asm{""}, but this is purely for presentation.
mixin(C) {
asm ("movq $60, %rax\n"
"movq $2, %rdi\n"
"syscall");
}
assert(0);
}
```
More information about the dip.ideas
mailing list