forked from ldc-developers/ldc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wasm: Add a preliminary TargetABI implementation (ldc-developers#4758)
To resolve ldc-developers#4757 and make our wasm ABI a bit more compatible with clang/emscripten's. This includes switching to 128-bit `real`.
- Loading branch information
Showing
5 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//===-- wasm.cpp ----------------------------------------------------------===// | ||
// | ||
// LDC – the LLVM D compiler | ||
// | ||
// This file is distributed under the BSD-style LDC license. See the LICENSE | ||
// file for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// see https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "gen/abi/generic.h" | ||
|
||
using namespace dmd; | ||
|
||
namespace { | ||
Type *getSingleWrappedScalarType(Type *t) { | ||
t = t->toBasetype(); | ||
|
||
if (auto ts = t->isTypeStruct()) { | ||
if (ts->sym->fields.length != 1) | ||
return nullptr; | ||
return getSingleWrappedScalarType(ts->sym->fields[0]->type); | ||
} | ||
|
||
if (auto tsa = t->isTypeSArray()) { | ||
if (tsa->dim->toInteger() != 1) | ||
return nullptr; | ||
return getSingleWrappedScalarType(tsa->nextOf()); | ||
} | ||
|
||
return t->isscalar() | ||
// some more pointers: | ||
|| t->ty == TY::Tclass || t->ty == TY::Taarray | ||
? t | ||
: nullptr; | ||
} | ||
} | ||
|
||
struct WasmTargetABI : TargetABI { | ||
static bool isDirectlyPassedAggregate(Type *t) { | ||
// Structs and static arrays are generally passed byval, except for POD | ||
// aggregates wrapping a single scalar type. | ||
|
||
if (!DtoIsInMemoryOnly(t)) // not a struct or static array | ||
return false; | ||
|
||
// max scalar type size is 16 (`real`); return early if larger | ||
if (size(t) > 16 || !isPOD(t)) | ||
return false; | ||
|
||
Type *singleWrappedScalarType = getSingleWrappedScalarType(t); | ||
return singleWrappedScalarType && | ||
// not passed directly if over-aligned | ||
DtoAlignment(t) <= DtoAlignment(singleWrappedScalarType); | ||
} | ||
|
||
bool returnInArg(TypeFunction *tf, bool) override { | ||
if (tf->isref()) { | ||
return false; | ||
} | ||
|
||
Type *rt = tf->next->toBasetype(); | ||
return passByVal(tf, rt); | ||
} | ||
|
||
bool passByVal(TypeFunction *, Type *t) override { | ||
return DtoIsInMemoryOnly(t) && !isDirectlyPassedAggregate(t); | ||
} | ||
|
||
void rewriteFunctionType(IrFuncTy &) override {} | ||
}; | ||
|
||
// The public getter for abi.cpp. | ||
TargetABI *getWasmTargetABI() { return new WasmTargetABI; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters