summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-llvm-compiler.cpp
blob: b34c05d42e2070f188c9565092df8ccf9f453736 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// slang-llvm-compiler.cpp
#include "slang-llvm-compiler.h"

#include "../core/slang-common.h"

namespace Slang
{

class AliasDepreciatedDownstreamCompiler : public DownstreamCompilerBase
{
public:

    virtual SLANG_NO_THROW SlangResult SLANG_MCALL compile(const CompileOptions& options, IArtifact** outArtifact) SLANG_OVERRIDE;
    virtual SLANG_NO_THROW bool SLANG_MCALL canConvert(const ArtifactDesc& from, const ArtifactDesc& to) SLANG_OVERRIDE { return m_inner->canConvert(from, to); }
    virtual SLANG_NO_THROW SlangResult SLANG_MCALL convert(IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact) SLANG_OVERRIDE { return m_inner->convert(from, to, outArtifact); }
    virtual SLANG_NO_THROW SlangResult SLANG_MCALL getVersionString(slang::IBlob** outVersionString) { return m_inner->getVersionString(outVersionString); }
    virtual SLANG_NO_THROW bool SLANG_MCALL isFileBased() { return m_inner->isFileBased(); }

    template <typename T>
    void initCompileOptionsDepreciated()
    {
        m_compileOptionsOffset = T::kStart;
    }

    AliasDepreciatedDownstreamCompiler(IDownstreamCompiler* inner) :
        m_inner(inner)
    {
        m_desc = inner->getDesc();
    }

    ComPtr<IDownstreamCompiler> m_inner;
    ptrdiff_t m_compileOptionsOffset = 0;
};

SlangResult AliasDepreciatedDownstreamCompiler::compile(const CompileOptions& options, IArtifact** outArtifact)
{
    if (m_compileOptionsOffset == 0)
    {
        return m_inner->compile(options, outArtifact);
    }
    const uint8_t* ptr = ((const uint8_t*)&options) + m_compileOptionsOffset;
    return m_inner->compile(*(const CompileOptions*)ptr, outArtifact);
}

/* static */SlangResult LLVMDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set)
{
    ComPtr<ISlangSharedLibrary> library;

    SLANG_RETURN_ON_FAIL(DownstreamCompilerUtil::loadSharedLibrary(path, loader, nullptr, "slang-llvm", library));

    SLANG_ASSERT(library);
    if (!library)
    {
        return SLANG_FAIL;
    }

    typedef SlangResult(*CreateDownstreamCompilerFunc)(const Guid& intf, IDownstreamCompiler** outCompiler);

    ComPtr<IDownstreamCompiler> downstreamCompiler;

    if (auto fnV2 = (CreateDownstreamCompilerFunc)library->findFuncByName("createLLVMDownstreamCompiler_V2"))
    {
        ComPtr<IDownstreamCompiler> innerDownstreamCompiler;

        SLANG_RETURN_ON_FAIL(fnV2(IDownstreamCompiler::getTypeGuid(), innerDownstreamCompiler.writeRef()));

        // We then need to wrap
        AliasDepreciatedDownstreamCompiler* fix = new AliasDepreciatedDownstreamCompiler(innerDownstreamCompiler);
        downstreamCompiler = fix;
        fix->initCompileOptionsDepreciated<DownstreamCompileOptions_AliasDepreciated1>();
    }
    else if (auto fnV3 = (CreateDownstreamCompilerFunc)library->findFuncByName("createLLVMDownstreamCompiler_V3"))
    {
        SLANG_RETURN_ON_FAIL(fnV3(IDownstreamCompiler::getTypeGuid(), downstreamCompiler.writeRef()));
    }
    else
    {
        return SLANG_FAIL;
    }

    set->addSharedLibrary(library);
    set->addCompiler(downstreamCompiler);
    return SLANG_OK;
}

}