summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-dll-export.cpp
blob: c525934fe7f176d1cd836a218087108be4533df9 (plain)
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
// slang-ir-dll-export.cpp
#include "slang-ir-dll-export.h"

#include "slang-ir-insts.h"
#include "slang-ir-marshal-native-call.h"
#include "slang-ir-util.h"
#include "slang-ir.h"

namespace Slang
{

struct DllExportContext
{
    IRModule* module;
    DiagnosticSink* diagnosticSink;

    void processFunc(IRFunc* func, IRDllExportDecoration* dllExportDecoration)
    {
        NativeCallMarshallingContext marshalContext;
        marshalContext.diagnosticSink = diagnosticSink;
        IRBuilder builder(module);
        auto wrapper = marshalContext.generateDLLExportWrapperFunc(builder, func);
        dllExportDecoration->removeFromParent();
        dllExportDecoration->insertAtStart(wrapper);
        builder.addNameHintDecoration(wrapper, dllExportDecoration->getFunctionName());
        builder.addExternCppDecoration(wrapper, dllExportDecoration->getFunctionName());
        builder.addPublicDecoration(wrapper);
        builder.addKeepAliveDecoration(wrapper);
        builder.addHLSLExportDecoration(wrapper);
        removeLinkageDecorations(func);
    }

    void processModule()
    {
        struct Candidate
        {
            IRFunc* func;
            IRDllExportDecoration* exportDecoration;
        };
        List<Candidate> candidates;
        for (auto childFunc : module->getGlobalInsts())
        {
            switch (childFunc->getOp())
            {
            case kIROp_Func:
                if (auto dllExportDecoration = childFunc->findDecoration<IRDllExportDecoration>())
                {
                    candidates.add(Candidate{as<IRFunc>(childFunc), dllExportDecoration});
                }
                break;
            default:
                break;
            }
        }

        for (auto candidate : candidates)
        {
            processFunc(candidate.func, candidate.exportDecoration);
        }
    }
};

void generateDllExportFuncs(IRModule* module, DiagnosticSink* sink)
{
    DllExportContext context;
    context.module = module;
    context.diagnosticSink = sink;
    return context.processModule();
}

} // namespace Slang