File: wasm.cpp

package info (click to toggle)
ldc 1%3A1.40.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 63,308 kB
  • sloc: cpp: 85,368; ansic: 21,877; makefile: 1,705; sh: 1,018; asm: 584; objc: 135; exp: 48; python: 12
file content (77 lines) | stat: -rw-r--r-- 2,215 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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; }