From e5a75563a1ba2e378353af8b937b8b7bb0fe2c2b Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 21 Jun 2022 14:55:59 -0700 Subject: Lower throwing COM interface method. (#2282) * Lower throwing COM interface method. * Fix. * Fix warnings. Co-authored-by: Yong He --- source/slang/slang-ir-dll-import.cpp | 98 +++++------------------------------- 1 file changed, 12 insertions(+), 86 deletions(-) (limited to 'source/slang/slang-ir-dll-import.cpp') diff --git a/source/slang/slang-ir-dll-import.cpp b/source/slang/slang-ir-dll-import.cpp index 02743cba4..b123dfb03 100644 --- a/source/slang/slang-ir-dll-import.cpp +++ b/source/slang/slang-ir-dll-import.cpp @@ -3,6 +3,7 @@ #include "slang-ir.h" #include "slang-ir-insts.h" +#include "slang-ir-marshal-native-call.h" namespace Slang { @@ -83,82 +84,15 @@ struct DllImportContext return stringGetBufferFunc; } - IRType* getNativeType(IRBuilder& builder, IRType* type) - { - switch (type->getOp()) - { - case kIROp_StringType: - return builder.getPtrType(builder.getCharType()); - default: - return type; - } - } - - IRType* getNativeFuncType(IRBuilder& builder, IRFunc* func) - { - List nativeParamTypes; - auto declaredFuncType = func->getDataType(); - assert(declaredFuncType->getOp() == kIROp_FuncType); - for (UInt i = 0; i < declaredFuncType->getParamCount(); ++i) - { - auto paramType = declaredFuncType->getParamType(i); - nativeParamTypes.add(getNativeType(builder, as(paramType))); - } - IRType* returnType = getNativeType(builder, func->getResultType()); - auto funcType = builder.getFuncType( - nativeParamTypes.getCount(), (IRType**)nativeParamTypes.getBuffer(), returnType); - - return funcType; - } - - void marshalImportRefParameter(IRBuilder& builder, IRParam* param, List& args) - { - SLANG_UNUSED(builder); - - auto innerType = as(param->getDataType())->getValueType(); - switch (innerType->getOp()) - { - case kIROp_StringType: - { - diagnosticSink->diagnose( - param->sourceLoc, - Diagnostics::invalidTypeMarshallingForImportedDLLSymbol, - param->getParent()->getParent()); - } - break; - default: - args.add(param); - break; - } - } - - void marshalImportParameter(IRBuilder& builder, IRParam* param, List& args) - { - switch (param->getDataType()->getOp()) - { - case kIROp_InOutType: - case kIROp_RefType: - return marshalImportRefParameter(builder, param, args); - case kIROp_StringType: - { - auto getStringBufferFunc = getStringGetBufferFunc(); - args.add(builder.emitCallInst( - builder.getPtrType(builder.getCharType()), getStringBufferFunc, 1, (IRInst**)¶m)); - } - break; - default: - args.add(param); - break; - } - } void processFunc(IRFunc* func, IRDllImportDecoration* dllImportDecoration) { assert(func->getFirstBlock() == nullptr); IRBuilder builder(sharedBuilder); + NativeCallMarshallingContext marshalContext; - auto nativeType = getNativeFuncType(builder, func); + auto nativeType = marshalContext.getNativeFuncType(builder, func->getDataType()); builder.setInsertInto(module->getModuleInst()); auto funcPtr = builder.createGlobalVar(nativeType); builder.setInsertInto(funcPtr); @@ -178,12 +112,6 @@ struct DllImportContext params.add(builder.emitParam((IRType*)paramType)); } - // Marshal parameters to arguments into native func. - List args; - for (auto param : params) - { - marshalImportParameter(builder, param, args); - } IRInst* cmpArgs[] = {builder.emitLoad(nativeType, funcPtr), builder.getPtrValue(nullptr)}; auto isUninitialized = builder.emitIntrinsicInst(builder.getBoolType(), kIROp_Eql, 2, cmpArgs); @@ -209,17 +137,15 @@ struct DllImportContext builder.emitBranch(afterBlock); builder.setInsertInto(afterBlock); - IRType* nativeReturnType = getNativeType(builder, func->getResultType()); - auto nativeFunc = builder.emitLoad(funcPtr); - auto call = builder.emitCallInst(nativeReturnType, nativeFunc, args); - if (declaredFuncType->getResultType()->getOp() != kIROp_VoidType) - { - builder.emitReturn(call); - } - else - { - builder.emitReturn(); - } + marshalContext.diagnosticSink = diagnosticSink; + auto callResult = marshalContext.marshalNativeCall( + builder, + declaredFuncType, + nativeType, + builder.emitLoad(funcPtr), + params.getCount(), + (IRInst**)params.getBuffer()); + builder.emitReturn(callResult); } void processModule() -- cgit v1.2.3