diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-07-09 13:59:30 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-09 13:59:30 -0400 |
| commit | f52f5cd4a7b5b71617b949fc62a78abe8c4822b3 (patch) | |
| tree | 48f636f33e199b3508d779941c034f7be91debc4 /source/slang/slang-emit-c-like.cpp | |
| parent | 691ebae763e29327249735d67fbb231c75b17840 (diff) | |
WIP: slang to C++ code generation (#997)
* WIP: Emitting Cpp
* Added HLSLType instead of using IRInst - because they don't seem to be deduped.
* Removed need for lexer to take a String.
Added mechansim to lookup intrinsic functions on C++.
* A c/c++ cross compilation test.
* WIP Cpp output using cloning and slang types.
* More work to generate mul funcs.
* WIP: Outputting some simple C++.
* Expose findOrEmitHoistableInst to IRBuilder to aid cloning,
* Simplification for checking for BasicTypes.
Test infrastructure compiles output C++ code.
* Dot and mat/vec multiplication output.
* First pass at swizzling.
* First support for binary ops.
* Builtin binary and unary functions.
* Any and all.
* WIP adding support for other functions.
Added code to generate function signature.
* Add scalar functions to slang-cpp-prelude.h
* Support for most built in operations.
* Tested first ternary.
* Checking the emitting of corner cases functions - normalize, length, any, all, normalize, reflect.
* Check asfloat etc work.
* Fmod support.
* WIP Array handling in C++.
* First stage in being able to handl arbitrary type output for CLikeSourceEmitter
* Removed Handler/Emitter split - so can implement more easily complex type naming.
* Array passing by value first pass.
* Rename Array -> FixedArray
* Outputs structs in C++.
* Emit the thread config.
* Dimension -> TypeDimension
* SpecializedOperation -> SpecializedIntrinsic
Operation -> IntrinsicOp
Use shared impl of isNominalOp
Commented use of m_uniqueModule etc.
* Add code to test slang->cpp when compiled doesn't have errors. Does so by building shared library and exporting the entry point.
* Fix linux clang/gcc compile error about override not being specified.
* Make sure c-cross-compile is run on linux targets/smoke.
* Remove c-cross-compile.slang from smoke.
* Fix running tests/cross-compile/c-cross-compile.slang on Ubuntu 16.04
* Only add -std=c++11 for C++ source.
Diffstat (limited to 'source/slang/slang-emit-c-like.cpp')
| -rw-r--r-- | source/slang/slang-emit-c-like.cpp | 92 |
1 files changed, 48 insertions, 44 deletions
diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp index 02a89c220..4f718f884 100644 --- a/source/slang/slang-emit-c-like.cpp +++ b/source/slang/slang-emit-c-like.cpp @@ -34,7 +34,7 @@ struct CLikeSourceEmitter::EDeclarator { enum class Flavor { - name, + Name, Array, UnsizedArray, }; @@ -42,9 +42,8 @@ struct CLikeSourceEmitter::EDeclarator EDeclarator* next = nullptr; // Used for `Flavor::name` - Name* name; - SourceLoc loc; - + const StringSliceLoc* nameAndLoc; + // Used for `Flavor::Array` IRInst* elementCount; }; @@ -149,8 +148,8 @@ void CLikeSourceEmitter::emitDeclarator(EDeclarator* declarator) switch (declarator->flavor) { - case EDeclarator::Flavor::name: - m_writer->emitName(declarator->name, declarator->loc); + case EDeclarator::Flavor::Name: + m_writer->emitName(*declarator->nameAndLoc); break; case EDeclarator::Flavor::Array: @@ -250,40 +249,57 @@ void CLikeSourceEmitter::_emitType(IRType* type, EDeclarator* declarator) } -void CLikeSourceEmitter::emitType( - IRType* type, - SourceLoc const& typeLoc, - Name* name, - SourceLoc const& nameLoc) +void CLikeSourceEmitter::emitTypeImpl(IRType* type, const StringSliceLoc* nameAndLoc) { - m_writer->advanceToSourceLocation(typeLoc); + if (nameAndLoc) + { + // TODO(JS): No call to the previous version of this method was passing in a typeLoc, so disabled for + // now for simplicity + //m_writer->advanceToSourceLocation(typeLoc); - EDeclarator nameDeclarator; - nameDeclarator.flavor = EDeclarator::Flavor::name; - nameDeclarator.name = name; - nameDeclarator.loc = nameLoc; - _emitType(type, &nameDeclarator); + EDeclarator nameDeclarator; + nameDeclarator.flavor = EDeclarator::Flavor::Name; + nameDeclarator.nameAndLoc = nameAndLoc; + _emitType(type, &nameDeclarator); + } + else + { + _emitType(type, nullptr); + } } void CLikeSourceEmitter::emitType(IRType* type, Name* name) { - emitType(type, SourceLoc(), name, SourceLoc()); + SLANG_ASSERT(name); + StringSliceLoc nameAndLoc(name->text.getUnownedSlice()); + emitType(type, &nameAndLoc); } void CLikeSourceEmitter::emitType(IRType* type, const String& name) { - // HACK: the rest of the code wants a `Name`, - // so we'll create one for a bit... - Name tempName; - tempName.text = name; + StringSliceLoc nameAndLoc(name.getUnownedSlice()); + emitType(type, &nameAndLoc); +} - emitType(type, SourceLoc(), &tempName, SourceLoc()); +void CLikeSourceEmitter::emitType(IRType* type) +{ + emitType(type, (StringSliceLoc*)nullptr); } +void CLikeSourceEmitter::emitType(IRType* type, Name* name, SourceLoc const& nameLoc) +{ + SLANG_ASSERT(name); -void CLikeSourceEmitter::emitType(IRType* type) + StringSliceLoc nameAndLoc; + nameAndLoc.loc = nameLoc; + nameAndLoc.name = name->text.getUnownedSlice(); + + emitType(type, &nameAndLoc); +} + +void CLikeSourceEmitter::emitType(IRType* type, NameLoc const& nameAndLoc) { - _emitType(type, nullptr); + emitType(type, nameAndLoc.name, nameAndLoc.loc); } // @@ -324,20 +340,6 @@ bool CLikeSourceEmitter::isTargetIntrinsicModifierApplicable(const String& targe } } -void CLikeSourceEmitter::emitType(IRType* type, Name* name, SourceLoc const& nameLoc) -{ - emitType( - type, - SourceLoc(), - name, - nameLoc); -} - -void CLikeSourceEmitter::emitType(IRType* type, NameLoc const& nameAndLoc) -{ - emitType(type, nameAndLoc.name, nameAndLoc.loc); -} - bool CLikeSourceEmitter::isTargetIntrinsicModifierApplicable(IRTargetIntrinsicDecoration* decoration) { auto targetName = String(decoration->getTargetName()); @@ -1533,12 +1535,10 @@ void CLikeSourceEmitter::emitIntrinsicCallExpr( // auto linkageDecoration = valueForName->findDecoration<IRLinkageDecoration>(); SLANG_ASSERT(linkageDecoration); - auto mangledName = String(linkageDecoration->getMangledName()); - - + // We will use the `MangledLexer` to // help us split the original name into its pieces. - MangledLexer lexer(mangledName); + MangledLexer lexer(linkageDecoration->getMangledName()); // We'll read through the qualified name of the // symbol (e.g., `Texture2D<T>.Sample`) and then @@ -1655,7 +1655,7 @@ void CLikeSourceEmitter::emitCallExpr(IRCall* inst, IREmitMode mode, EmitOpInfo maybeCloseParens(needClose); } } - + void CLikeSourceEmitter::emitInstExpr(IRInst* inst, IREmitMode mode, const EmitOpInfo& inOuterPrec) { // Try target specific impl first @@ -1663,7 +1663,11 @@ void CLikeSourceEmitter::emitInstExpr(IRInst* inst, IREmitMode mode, const EmitO { return; } + defaultEmitInstExpr(inst, mode, inOuterPrec); +} +void CLikeSourceEmitter::defaultEmitInstExpr(IRInst* inst, IREmitMode mode, const EmitOpInfo& inOuterPrec) +{ EmitOpInfo outerPrec = inOuterPrec; bool needClose = false; switch(inst->op) |
