Skip to content

Commit 28933e1

Browse files
QuantumSegfaultthewilsonator
authored andcommitted
Add LDC-specific DRuntime changes.
1 parent 76d0e76 commit 28933e1

5 files changed

Lines changed: 71 additions & 1 deletion

File tree

runtime/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,12 @@ file(GLOB_RECURSE DRUNTIME_D_OPENBSD ${RUNTIME_DIR}/src/core/sys/openbsd/*.d)
181181
file(GLOB_RECURSE DRUNTIME_D_POSIX ${RUNTIME_DIR}/src/core/sys/posix/*.d)
182182
file(GLOB_RECURSE DRUNTIME_D_SOLARIS ${RUNTIME_DIR}/src/core/sys/solaris/*.d)
183183
file(GLOB_RECURSE DRUNTIME_D_WINDOWS ${RUNTIME_DIR}/src/core/sys/windows/*.d)
184+
file(GLOB_RECURSE DRUNTIME_D_WASI ${RUNTIME_DIR}/src/core/sys/wasi/*.d)
184185
list(REMOVE_ITEM DRUNTIME_D
185186
${DRUNTIME_D_BIONIC} ${DRUNTIME_D_DARWIN} ${DRUNTIME_D_DRAGONFLYBSD}
186187
${DRUNTIME_D_FREEBSD} ${DRUNTIME_D_LINUX} ${DRUNTIME_D_NETBSD}
187188
${DRUNTIME_D_OPENBSD} ${DRUNTIME_D_POSIX} ${DRUNTIME_D_SOLARIS}
188-
${DRUNTIME_D_WINDOWS}
189+
${DRUNTIME_D_WINDOWS} ${DRUNTIME_D_WASI}
189190
)
190191
if("${TARGET_SYSTEM}" MATCHES "Windows")
191192
list(APPEND DRUNTIME_D ${DRUNTIME_D_WINDOWS})
@@ -206,6 +207,8 @@ elseif("${TARGET_SYSTEM}" MATCHES "UNIX")
206207
list(APPEND DRUNTIME_D ${DRUNTIME_D_OPENBSD})
207208
elseif("${TARGET_SYSTEM}" MATCHES "SunOS")
208209
list(APPEND DRUNTIME_D ${DRUNTIME_D_SOLARIS})
210+
elseif("${TARGET_SYSTEM}" MATCHES "WASI")
211+
list(APPEND DRUNTIME_D ${DRUNTIME_D_WASI})
209212
endif()
210213
endif()
211214

runtime/druntime/src/core/thread/osthread.d

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,24 @@ version (LDC)
18311831
static assert(0);
18321832
}
18331833
}
1834+
else version (WebAssembly)
1835+
{
1836+
/* Must NOT be naked, but is safe to inline.
1837+
*
1838+
* The function prologue is what loads the `__stack_pointer` global
1839+
* into a fake "physical" register (which becomes a Wasm local).
1840+
* Manipulation of the stack is done with the local, and only synced
1841+
* back out to `__stack_pointer` across function boundaries.
1842+
*
1843+
* `llvm_stackaddress` gives us access to this fake register
1844+
* and allows for better optimization than inline asm would.
1845+
*/
1846+
private extern(D) void* getStackTop() nothrow @nogc
1847+
{
1848+
import ldc.intrinsics;
1849+
return llvm_stackaddress();
1850+
}
1851+
}
18341852
else
18351853
{
18361854
/* The use of intrinsic llvm_frameaddress is a reasonable default for
@@ -1874,6 +1892,14 @@ version (LDC_Windows)
18741892
static assert(false, "Architecture not supported.");
18751893
}
18761894
}
1895+
else version (WebAssembly)
1896+
{
1897+
private extern(C) extern void* __stack_low;
1898+
private extern(D) void* getStackBottom() nothrow @nogc
1899+
{
1900+
return __stack_low;
1901+
}
1902+
}
18771903
else
18781904
private extern(D) void* getStackBottom() nothrow @nogc
18791905
{

runtime/druntime/src/ldc/eh_wasm.d

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* This module implements the runtime-part of LDC exceptions
3+
* on WebAssembly (Wasm EH)
4+
*/
5+
6+
module ldc.eh_wasm;
7+
8+
version (WebAssembly):
9+
10+
extern (C) void _d_throw_exception(Throwable t) {
11+
import core.stdc.stdio : fwrite, stdout, putc;
12+
import ldc.intrinsics : llvm_trap;
13+
14+
auto msg = t.toString();
15+
fwrite(msg.ptr, msg.length, 1, stdout);
16+
putc('\n', stdout);
17+
18+
llvm_trap();
19+
}
20+
21+
extern (C) void _Unwind_Resume(void*) {
22+
import core.stdc.stdio : puts;
23+
import ldc.intrinsics : llvm_trap;
24+
25+
puts("Cannot EH unwind on Wasm (yet).");
26+
llvm_trap();
27+
}
28+
29+
extern (C) void _d_eh_enter_catch(void*) {
30+
import core.stdc.stdio : puts;
31+
import ldc.intrinsics : llvm_trap;
32+
33+
puts("Cannot EH catch on Wasm (yet).");
34+
llvm_trap();
35+
}

runtime/druntime/src/ldc/intrinsics.di

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ pragma(LDC_intrinsic, "llvm.returnaddress")
5454
pragma(LDC_intrinsic, "llvm.frameaddress.p0")
5555
void* llvm_frameaddress(uint level);
5656

57+
/// The 'llvm.stackaddress' intrinsic returns the starting address of the stack
58+
// region that may be used by called functions.
59+
pragma(LDC_intrinsic, "llvm.stackaddress.p0")
60+
void* llvm_stackaddress();
61+
5762
/// The 'llvm.stacksave' intrinsic is used to remember the current state of the
5863
/// function stack, for use with llvm.stackrestore. This is useful for
5964
/// implementing language features like scoped automatic variable sized arrays

runtime/druntime/src/rt/sections_ldc.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ else version (DragonFlyBSD) {}
3232
else version (NetBSD) {}
3333
else version (OpenBSD) {}
3434
else version (Windows) {}
35+
else version (WebAssembly) {}
3536
else version (LDC):
3637

3738
import rt.minfo;

0 commit comments

Comments
 (0)