diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-06-06 17:48:39 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-06 17:48:39 -0400 |
| commit | fc083a75b94ac4b4e735b4a7ff566191b9123f74 (patch) | |
| tree | ac1168718ac36099374373fdb55b9be178a0a9fa /source/slang/slang-emit-cpp.cpp | |
| parent | 0d9071bd1511ee2cb7d6ba6ce9e250d25613ddca (diff) | |
Split out target code generation from CLikeSourceEmitter (#976)
* * Added SourceStyle to CLikeSourceEmitter, to limit cases to actual target types.
* Made Impl methods _ prefixed
* Small tidyup
* * SourceStream -> SourceWriter
* use slang-emit- prefix on SourceWriter file
* * Remove EmitContext -> merge into CLikeSourceEmitter
* slang-c-like-source-emitter -> slang-emit-source.cpp
* ExtensionUsageTracker -> GLSLExtensionTracker
slang-extension-usage-tracker.cpp/.h -> slang-emit-glsl-extension-tracker.cpp/.h
* emit-source.cpp.h -> emit-c-like.cpp/.h
* Small fix to move where some _ prefixed functions are declared in CLikeSourceEmitter.
* * CLikeSourceEmitter::CInfo -> Desc
* Functions to get and find CodeGenTarget by name
* Split out empty language impls
* Create an impl based on SourceStyle
* * CodeGenTarget conversion to and from string
* Move HLSL specific functions to HLSLEmitSource.
* Emitting texture and image types.
* Move move GLSL specific functionality to GLSLSourceEmitter
* Split more out of slang-emit-c-like
* Refactor more out of slang-emit-c-like
* * tryEmitIRInstExprImpl(IRInst* inst, IREmitMode mode, const EmitOpInfo& inOuterPrec)
* Fix bug around output of uintBitsToFloat
* More work refactoring out target specifics from slang-emit-c-like
* Move functions that are only implemented once in GLSL impl into their Impl method.
* Move rate qualification out of slang-emit-c-like
* * Added getEmitOpForOp - allows for table usage so different ops can be dealt with the same way
* Moved vector comparison to slang-emit-glsl
*
* * Use EmitOpInfo to control output in slang-emit-c-like.cpp for unary ops
* Move more functionality from CLikeSourceEmitter to HLSLSourceEmitter
* Make output of parameters implementaion specific.
* Extracted interpolation modifiers.
* Remove IR from methods that don't need them.
* Remove IR from method names.
* Refactor handling of output of types - to make the impls implement the full path without lots of cases for specific impls
* Add variable declaration modifiers and matrix layout to larget specific in slang-emit.
* Make target specific internal functions _ prefixed.
Diffstat (limited to 'source/slang/slang-emit-cpp.cpp')
| -rw-r--r-- | source/slang/slang-emit-cpp.cpp | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/source/slang/slang-emit-cpp.cpp b/source/slang/slang-emit-cpp.cpp new file mode 100644 index 000000000..08d5d2ee8 --- /dev/null +++ b/source/slang/slang-emit-cpp.cpp @@ -0,0 +1,221 @@ +// slang-emit-cpp.cpp +#include "slang-emit-cpp.h" + +#include "../core/slang-writer.h" + +#include "slang-emit-source-writer.h" +#include "slang-mangled-lexer.h" + +#include <assert.h> + +namespace Slang { + + +static IROp _getCType(IROp op) +{ + switch (op) + { + case kIROp_VoidType: + case kIROp_BoolType: + { + return op; + } + case kIROp_Int8Type: + case kIROp_Int16Type: + case kIROp_IntType: + case kIROp_UInt8Type: + case kIROp_UInt16Type: + case kIROp_UIntType: + { + // Promote all these to Int + return kIROp_IntType; + } + case kIROp_Int64Type: + case kIROp_UInt64Type: + { + // Promote all these to Int16, we can just vary the call to make these work + return kIROp_Int64Type; + } + case kIROp_DoubleType: + { + return kIROp_DoubleType; + } + case kIROp_HalfType: + case kIROp_FloatType: + { + // Promote both to float + return kIROp_FloatType; + } + default: + { + SLANG_ASSERT(!"Unhandled type"); + return kIROp_undefined; + } + } +} + +static UnownedStringSlice _getCTypeVecPostFix(IROp op) +{ + switch (op) + { + case kIROp_BoolType: return UnownedStringSlice::fromLiteral("B"); + case kIROp_IntType: return UnownedStringSlice::fromLiteral("I"); + case kIROp_FloatType: return UnownedStringSlice::fromLiteral("F"); + case kIROp_Int64Type: return UnownedStringSlice::fromLiteral("I64"); + case kIROp_DoubleType: return UnownedStringSlice::fromLiteral("F64"); + default: return UnownedStringSlice::fromLiteral("?"); + } +} + +#if 0 +static UnownedStringSlice _getCTypeName(IROp op) +{ + switch (op) + { + case kIROp_BoolType: return UnownedStringSlice::fromLiteral("Bool"); + case kIROp_IntType: return UnownedStringSlice::fromLiteral("I32"); + case kIROp_FloatType: return UnownedStringSlice::fromLiteral("F32"); + case kIROp_Int64Type: return UnownedStringSlice::fromLiteral("I64"); + case kIROp_DoubleType: return UnownedStringSlice::fromLiteral("F64"); + default: return UnownedStringSlice::fromLiteral("?"); + } +} +#endif + +void CPPSourceEmitter::_emitCVecType(IROp op, Int size) +{ + m_writer->emit("Vec"); + const UnownedStringSlice postFix = _getCTypeVecPostFix(_getCType(op)); + m_writer->emit(postFix); + if (postFix.size() > 1) + { + m_writer->emit("_"); + } + m_writer->emit(size); +} + +void CPPSourceEmitter::_emitCMatType(IROp op, IRIntegerValue rowCount, IRIntegerValue colCount) +{ + m_writer->emit("Mat"); + const UnownedStringSlice postFix = _getCTypeVecPostFix(_getCType(op)); + m_writer->emit(postFix); + if (postFix.size() > 1) + { + m_writer->emit("_"); + } + m_writer->emit(rowCount); + m_writer->emit(colCount); +} + +void CPPSourceEmitter::_emitCFunc(BuiltInCOp cop, IRType* type) +{ + emitSimpleType(type); + m_writer->emit("_"); + + switch (cop) + { + case BuiltInCOp::Init: m_writer->emit("init"); + case BuiltInCOp::Splat: m_writer->emit("splat"); break; + } +} + +void CPPSourceEmitter::emitParameterGroupImpl(IRGlobalParam* varDecl, IRUniformParameterGroupType* type) +{ + SLANG_UNUSED(varDecl); + SLANG_UNUSED(type); + SLANG_ASSERT(!"Not implemented"); +} + +void CPPSourceEmitter::emitEntryPointAttributesImpl(IRFunc* irFunc, EntryPointLayout* entryPointLayout) +{ + SLANG_UNUSED(irFunc); + SLANG_UNUSED(entryPointLayout); + SLANG_ASSERT(!"Not implemented"); +} + +void CPPSourceEmitter::emitVectorTypeNameImpl(IRType* elementType, IRIntegerValue elementCount) +{ + _emitCVecType(elementType->op, Int(elementCount)); +} + +void CPPSourceEmitter::emitSimpleTypeImpl(IRType* type) +{ + switch (type->op) + { + case kIROp_VoidType: + case kIROp_BoolType: + case kIROp_Int8Type: + case kIROp_Int16Type: + case kIROp_IntType: + case kIROp_Int64Type: + case kIROp_UInt8Type: + case kIROp_UInt16Type: + case kIROp_UIntType: + case kIROp_UInt64Type: + case kIROp_FloatType: + case kIROp_DoubleType: + { + m_writer->emit(getDefaultBuiltinTypeName(type->op)); + return; + } + case kIROp_HalfType: + { + m_writer->emit("float"); + return; + } + case kIROp_StructType: + m_writer->emit(getName(type)); + return; + + case kIROp_VectorType: + { + auto vecType = (IRVectorType*)type; + emitVectorTypeNameImpl(vecType->getElementType(), GetIntVal(vecType->getElementCount())); + return; + } + case kIROp_MatrixType: + { + auto matType = (IRMatrixType*)type; + + const auto rowCount = GetIntVal(matType->getRowCount()); + const auto colCount = GetIntVal(matType->getColumnCount()); + + _emitCMatType(matType->getElementType()->op, rowCount, colCount); + + return; + } + } + + SLANG_DIAGNOSE_UNEXPECTED(getSink(), SourceLoc(), "unhandled type for cpp target"); +} + + +bool CPPSourceEmitter::tryEmitInstExprImpl(IRInst* inst, IREmitMode mode, const EmitOpInfo& inOuterPrec) +{ + SLANG_UNUSED(inOuterPrec); + + //EmitOpInfo outerPrec = inOuterPrec; + //bool needClose = false; + switch (inst->op) + { + case kIROp_Construct: + case kIROp_makeVector: + case kIROp_MakeMatrix: + // Simple constructor call + if (inst->getOperandCount() == 1) + { + _emitCFunc(BuiltInCOp::Splat, inst->getDataType()); + emitArgs(inst, mode); + } + else + { + _emitCFunc(BuiltInCOp::Init, inst->getDataType()); + emitArgs(inst, mode); + } + return true; + default: + return false; + } +} + +} // namespace Slang |
