summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-marshal-native-call.cpp
blob: 8e342cb2606782a69906f5ea7b2fb4a4cc0fb2f4 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// slang-ir-marshal-native-call.h
#include "slang-ir-marshal-native-call.h"
#include "slang-ir.h"
#include "slang-ir-insts.h"

namespace Slang
{

    IRType* NativeCallMarshallingContext::getNativeType(IRBuilder& builder, IRType* type)
    {
        switch (type->getOp())
        {
        case kIROp_StringType:
            return builder.getNativeStringType();
        case kIROp_InterfaceType:
            return builder.getNativePtrType(type);
        case kIROp_ComPtrType:
            return builder.getNativePtrType((IRType*)as<IRComPtrType>(type)->getOperand(0));
        default:
            return type;
        }
    }

    IRFuncType* NativeCallMarshallingContext::getNativeFuncType(
        IRBuilder& builder, IRFuncType* declaredFuncType)
    {
        List<IRInst*> nativeParamTypes;
        assert(declaredFuncType->getOp() == kIROp_FuncType);
        for (UInt i = 0; i < declaredFuncType->getParamCount(); ++i)
        {
            auto paramType = declaredFuncType->getParamType(i);
            nativeParamTypes.add(getNativeType(builder, as<IRType>(paramType)));
        }
        IRType* returnType = declaredFuncType->getResultType();
        if (auto resultType = as<IRResultType>(declaredFuncType->getResultType()))
        {
            auto nativeResultType = getNativeType(builder, resultType->getValueType());
            nativeParamTypes.add(builder.getPtrType(nativeResultType));
            returnType = resultType->getErrorType();
        }
        else
        {
            returnType = getNativeType(builder, returnType);
        }
        auto funcType = builder.getFuncType(
            nativeParamTypes.getCount(), (IRType**)nativeParamTypes.getBuffer(), returnType);

        return funcType;
    }

    void NativeCallMarshallingContext::marshalRefManagedValueToNativeValue(
        IRBuilder& builder, IRInst* originalArg, List<IRInst*>& args)
    {
        auto ptrTypeBase = as<IRPtrTypeBase>(originalArg->getDataType());
        SLANG_RELEASE_ASSERT(ptrTypeBase);
        switch (ptrTypeBase->getValueType()->getOp())
        {
        case kIROp_InterfaceType:
        case kIROp_ComPtrType:
            args.add(builder.emitGetManagedPtrWriteRef(originalArg));
            break;
        default:
            args.add(originalArg);
            break;
        }
    }

    void NativeCallMarshallingContext::marshalManagedValueToNativeValue(
        IRBuilder& builder, IRInst* originalArg, List<IRInst*>& args)
    {
        switch (originalArg->getDataType()->getOp())
        {
        case kIROp_InOutType:
        case kIROp_RefType:
            return marshalRefManagedValueToNativeValue(
                builder, originalArg, args);
        case kIROp_StringType:
            {
                auto nativeStr = builder.emitGetNativeString(originalArg);
                args.add(nativeStr);
            }
            break;
        case kIROp_InterfaceType:
            {
                auto nativePtr = builder.emitGetNativePtr(originalArg);
                args.add(nativePtr);
            }
            break;
        default:
            args.add(originalArg);
            break;
        }
    }

    IRInst* NativeCallMarshallingContext::marshalNativeValueToManagedValue(
        IRBuilder& builder, IRInst* nativeVal)
    {
        switch (nativeVal->getDataType()->getOp())
        {
        case kIROp_NativeStringType:
            {
                return builder.emitMakeString(nativeVal);
            }
            break;
        case kIROp_NativePtrType:
            {
                SLANG_RELEASE_ASSERT(nativeVal->getDataType()->getOperand(0)->getOp() == kIROp_InterfaceType);
                auto comPtrVar = builder.emitVar(builder.getComPtrType((IRType*)nativeVal->getDataType()->getOperand(0)));
                builder.emitManagedPtrAttach(comPtrVar, nativeVal);
                return builder.emitLoad(comPtrVar);
            }
            break;
        case kIROp_InterfaceType:
            {
                auto comPtrVar = builder.emitVar(nativeVal->getDataType());
                builder.emitManagedPtrAttach(comPtrVar, nativeVal);
                return builder.emitLoad(comPtrVar);
            }
            break;
        default:
            return nativeVal;
            break;
        }
    }

    IRInst* NativeCallMarshallingContext::marshalNativeCall(
        IRBuilder& builder,
        IRFuncType* originalFuncType,
        IRFuncType* nativeFuncType,
        IRInst* nativeFunc,
        Int argCount,
        IRInst*const* originalArgs)
    {
        // Marshal parameters to arguments into native func.
        List<IRInst*> args;
        for (Int i = 0; i < argCount; i++)
        {
            marshalManagedValueToNativeValue(builder, originalArgs[i], args);
        }
        IRType* originalReturnType = originalFuncType->getResultType();

        IRVar* resultVar = nullptr;
        if (auto resultType = as<IRResultType>(originalReturnType))
        {
            // Declare a local variable to receive result.
            resultVar = builder.emitVar(getNativeType(builder, resultType->getValueType()));
            args.add(resultVar);
        }

        // Insert call.
        IRInst* call = builder.emitCallInst(nativeFuncType->getResultType(), nativeFunc, args);

        // TODO: marshal output/ref args back to original args.

        IRInst* returnValue = call;

        // Marshal result and out arguments back to managed values.
        if (auto resultType = as<IRResultType>(originalReturnType))
        {
            auto val = builder.emitLoad(resultVar);
            auto err = call;
            val = marshalNativeValueToManagedValue(builder, val);
            auto intErr = err;
            if (err->getDataType()->getOp() != kIROp_IntType)
            {
                intErr = builder.emitConstructorInst(builder.getIntType(), 1, &err);
            }
            auto errIsError = builder.emitLess(intErr, builder.getIntValue(builder.getIntType(), 0));
            IRBlock *trueBlock, *falseBlock, *afterBlock;
            builder.emitIfElseWithBlocks(errIsError, trueBlock, falseBlock, afterBlock);
            builder.setInsertInto(trueBlock);
            returnValue = builder.emitMakeResultError(resultType, err);
            builder.emitBranch(afterBlock, 1, &returnValue);
            builder.setInsertInto(falseBlock);
            returnValue = builder.emitMakeResultValue(resultType, val);
            builder.emitBranch(afterBlock, 1, &returnValue);
            builder.setInsertInto(afterBlock);
            returnValue = builder.emitParam(resultType);
        }
        else
        {
            returnValue = marshalNativeValueToManagedValue(builder, call);
        }
        return returnValue;
    }

} // namespace Slang