D.gnu - Would APEs work with D?
- Raimondo Mancino (63/63) May 17 2021 Hello,
- Iain Buclaw (6/21) May 18 2021 The command would be pretty much the same as gcc (`-nostdinc
- Imperatorn (2/9) May 18 2021 Brace yourself... ImportC is coming.
- redthing1 (3/13) Mar 22 Now that ImportC is here, may I ask: did you get this working?
- Imperatorn (2/17) Apr 15 I actually didn't find time to try it. But maybe someone else has?
Hello, assuming that I have little experience with assembly, so I couldn't check if the ape.S is actually compatible, and that I also still have little experience with D: I was reading [this very interesting article](https://justine.lol/ape.html) and while reading it I had the impression that it would be possible to produce D APEs. It does require some ABI changes as far as I can tell, but I think that excluding Phobos (since it would require more work to get started) a simple hello world using [Cosmopolitan](https://github.com/jart/cosmopolitan/) would do the job (maybe we could have more complex code compiling with ` pragma(mangle)` for ABI compatibility and interop). If we look at Cosmopolitan's hello.c: ```c /* Compile with: gcc -g -O -static \ -fno-pie -no-pie -mno-red-zone -nostdlib -nostdinc \ -o hello.com hello.c \ -Wl,--oformat=binary -Wl,--gc-sections -Wl,-z,max-page-size=0x1000 -fuse-ld=bfd \ -Wl,-T,ape.lds \ -include cosmopolitan.h \ crt.o ape.o cosmopolitan.a */ #include "libc/stdio/stdio.h" int main() { printf("%s\n", "hello world"); return 0; } ``` My guess is that we could produce a portable executable with Cosmopolitan and thus write something like this in our hello.d: ```d /* I am not sure how would we compile this, but * I guess it's going to be something like: gdc -g -O -static \ -fno-pie -fuse-ld=bfd -mno-red-zone -fno-druntime -o hello.com hello.d -Wl,--oformat=binary -Wl,--gc-sections -Wl,-z,max-page-size=0x1000 -fuse-ld=bfd \ -Wl,-T,ape.lds crt.o ape.o cosmopolitan.a */ pragma(lib, "cosmopolitan"); // or in dub.sdl/.json nogc: // I am not sure whether the default GC would work extern(C) int printf(const char*, ...) trusted nothrow; extern(C) int main() { printf("%s\n", "hello world"); return 0; } ``` If I understand correctly, the needed steps would be: 1. Build the binary 2. Write PE magic number 3. Write BIOS boot sector (if needed) 4. Write sh compatible execution script (load executable code through qemu) 5. Write platform-specific sections and tables (ELF, *BSD, ...) 6. Put executable code and data sections 7. PKZIP magic (if needed) What do you think about it?
May 17 2021
On Monday, 17 May 2021 at 11:08:33 UTC, Raimondo Mancino wrote:My guess is that we could produce a portable executable with Cosmopolitan and thus write something like this in our hello.d: ```d /* I am not sure how would we compile this, but * I guess it's going to be something like: gdc -g -O -static \ -fno-pie -fuse-ld=bfd -mno-red-zone -fno-druntime -o hello.com hello.d -Wl,--oformat=binary -Wl,--gc-sections -Wl,-z,max-page-size=0x1000 -fuse-ld=bfd \ -Wl,-T,ape.lds crt.o ape.o cosmopolitan.a */ What do you think about it?The command would be pretty much the same as gcc (`-nostdinc -nostdlib`), but instead of `-include cosmopolitan.h`, you'd pass a cosmopolitan binding module (importC may solve the need for that), and a minimal (possibly empty) object.d to satisfy the compiler's need to have an object module.
May 18 2021
On Tuesday, 18 May 2021 at 08:40:10 UTC, Iain Buclaw wrote:On Monday, 17 May 2021 at 11:08:33 UTC, Raimondo Mancino wrote:Brace yourself... ImportC is coming.[...]The command would be pretty much the same as gcc (`-nostdinc -nostdlib`), but instead of `-include cosmopolitan.h`, you'd pass a cosmopolitan binding module (importC may solve the need for that), and a minimal (possibly empty) object.d to satisfy the compiler's need to have an object module.
May 18 2021
On Tuesday, 18 May 2021 at 14:43:52 UTC, Imperatorn wrote:On Tuesday, 18 May 2021 at 08:40:10 UTC, Iain Buclaw wrote:Now that ImportC is here, may I ask: did you get this working? I'm also very interested in it.On Monday, 17 May 2021 at 11:08:33 UTC, Raimondo Mancino wrote:Brace yourself... ImportC is coming.[...]The command would be pretty much the same as gcc (`-nostdinc -nostdlib`), but instead of `-include cosmopolitan.h`, you'd pass a cosmopolitan binding module (importC may solve the need for that), and a minimal (possibly empty) object.d to satisfy the compiler's need to have an object module.
Mar 22
On Friday, 22 March 2024 at 15:12:16 UTC, redthing1 wrote:On Tuesday, 18 May 2021 at 14:43:52 UTC, Imperatorn wrote:I actually didn't find time to try it. But maybe someone else has?On Tuesday, 18 May 2021 at 08:40:10 UTC, Iain Buclaw wrote:Now that ImportC is here, may I ask: did you get this working? I'm also very interested in it.On Monday, 17 May 2021 at 11:08:33 UTC, Raimondo Mancino wrote:Brace yourself... ImportC is coming.[...]The command would be pretty much the same as gcc (`-nostdinc -nostdlib`), but instead of `-include cosmopolitan.h`, you'd pass a cosmopolitan binding module (importC may solve the need for that), and a minimal (possibly empty) object.d to satisfy the compiler's need to have an object module.
Apr 15