From 879ec1b385d290a4375682ec613a9e7a1967fc7d Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 10 Oct 2018 13:56:25 -0400 Subject: Feature/source loc refactor (#668) * * Remove the need for IRHighLevelDecoration in Emit * Use the IRLayoutDecoration for GeometryShaderPrimitiveTypeModifier * Initial look at at variable byte encoding, and simple unit test. * Fixing problems with comparison due to naming differences with slang/fxc. * * More tests and perf improvements for byte encoding. * Mechanism to detect processor and processor features in main slang header. * Split out cpu based defines into slang-cpu-defines.h so do not polute slang.h * Support for variable byte encoding on serialization. * Removed unused flag. * Fix warning. * Fix calcMsByte32 for 0 values without using intrinsic. * Fix a mistake in calculating maximum instruction size. * Introduced the idea of SourceUnit. * Small improvements around naming. Add more functionality - including getting the HumaneLoc. * Add support for #line default * Compiling with new SourceLoc handling. * Fix off by one on #line directives. * Can use 32bits for SourceLoc. Fix serialize to use that. * Small fixes and comment on usage. * Premake run. * Fix signed warning. * Fix typo on StringSlicePool::has found in review. --- source/core/core.vcxproj | 5 +++- source/core/core.vcxproj.filters | 27 ++++++++++++------ source/core/slang-string-slice-pool.cpp | 43 ++++++++++++++++++++++++++++ source/core/slang-string-slice-pool.h | 50 +++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 10 deletions(-) create mode 100644 source/core/slang-string-slice-pool.cpp create mode 100644 source/core/slang-string-slice-pool.h (limited to 'source/core') diff --git a/source/core/core.vcxproj b/source/core/core.vcxproj index ec6195940..d1ed4715f 100644 --- a/source/core/core.vcxproj +++ b/source/core/core.vcxproj @@ -183,11 +183,13 @@ + + @@ -203,6 +205,7 @@ + @@ -210,7 +213,7 @@ - + diff --git a/source/core/core.vcxproj.filters b/source/core/core.vcxproj.filters index f9eeef43a..7936ca11b 100644 --- a/source/core/core.vcxproj.filters +++ b/source/core/core.vcxproj.filters @@ -1,4 +1,4 @@ - + @@ -45,6 +45,12 @@ Header Files + + Header Files + + + Header Files + Header Files @@ -60,6 +66,9 @@ Header Files + + Header Files + Header Files @@ -81,14 +90,14 @@ Header Files - - Header Files - Source Files + + Source Files + Source Files @@ -101,6 +110,9 @@ Source Files + + Source Files + Source Files @@ -116,13 +128,10 @@ Source Files - - Source Files - - + Source Files - + \ No newline at end of file diff --git a/source/core/slang-string-slice-pool.cpp b/source/core/slang-string-slice-pool.cpp new file mode 100644 index 000000000..88963d68f --- /dev/null +++ b/source/core/slang-string-slice-pool.cpp @@ -0,0 +1,43 @@ +#include "slang-string-slice-pool.h" + +namespace Slang { + +StringSlicePool::StringSlicePool() : + m_arena(1024) +{ + clear(); +} + +void StringSlicePool::clear() +{ + m_slices.SetSize(1); + m_slices[0] = UnownedStringSlice::fromLiteral(""); + + m_map.Clear(); +} + +StringSlicePool::Handle StringSlicePool::add(const Slice& slice) +{ + const int* indexPtr = m_map.TryGetValue(slice); + if (indexPtr) + { + return Handle(*indexPtr); + } + + // Create a scoped copy + UnownedStringSlice scopePath(m_arena.allocateString(slice.begin(), slice.size()), slice.size()); + + const int index = int(m_slices.Count()); + + m_slices.Add(scopePath); + m_map.Add(scopePath, index); + return Handle(index); +} + +int StringSlicePool::findIndex(const Slice& slice) const +{ + const int* index = m_map.TryGetValue(slice); + return index ? *index : -1; + +} +} // namespace Slang diff --git a/source/core/slang-string-slice-pool.h b/source/core/slang-string-slice-pool.h new file mode 100644 index 000000000..e6846b3dd --- /dev/null +++ b/source/core/slang-string-slice-pool.h @@ -0,0 +1,50 @@ +#ifndef SLANG_STRING_SLICE_POOL_H +#define SLANG_STRING_SLICE_POOL_H + +#include "slang-string.h" + +#include "list.h" +#include "slang-memory-arena.h" +#include "dictionary.h" + +namespace Slang { + +class StringSlicePool +{ +public: + /// Handle of 0 is null. If accessed will be returned as the empty string + enum class Handle : uint32_t; + typedef UnownedStringSlice Slice; + + /// Returns the index of a slice, if contained, or -1 if not found + int findIndex(const Slice& slice) const; + + /// True if has the slice + bool has(const Slice& slice) { return findIndex(slice) >= 0; } + /// Add a slice + Handle add(const Slice& slice); + + /// Empty contents + void clear(); + + /// Get the slice from the handle + const UnownedStringSlice& getSlice(Handle handle) const { return m_slices[UInt(handle)]; } + + /// Get all the slices + const List& getSlices() const { return m_slices; } + + /// Convert a handle to and index. (A handle is just an index!) + static int asIndex(Handle handle) { return int(handle); } + + /// Ctor + StringSlicePool(); + +protected: + List m_slices; + Dictionary m_map; + MemoryArena m_arena; +}; + +} // namespace Slang + +#endif // SLANG_STRING_SLICE_POOL_H -- cgit v1.2.3