From d3331fba6eaab44646010b556106da38925d43e0 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 6 Feb 2020 14:31:09 -0500 Subject: Literal handling improvements (#1202) * WIP: 64 literal diagnostic and truncation. * Improve how integer truncation is handled/supported. Added literal-int64.slang test. Set a suffix on all literals. Fixed problem on C++ based targets where l suffix was not the same as int() cast. So on C++ derived emitters, int() is used instead of l suffix to have same behavior across targets. * Add literal diagnostic testing. * Allow lexer to lex - in front of literals. * Fix lexing and converting int literal with -. * Too large small values of floats become inf. Handling writing inf types out on different targets. Add function to deterimine if a float literals kind. * Roll back the support of lexer lexing negative literals. * Fixed tests broken because of diagnostics numbers. Improved _isFinite * Fix compilation on linux. * Fix problem with abs on linux - use Math::Abs. * Fix typo. * * Improve warnings for float literals zeroed * Improved 64 bit type documentation * Handle half * Improved comments * Fixed tests broken * Use capital letters for suffixes. * Make default behavior on outputting a int literal that is an 'int32_t' is cast (not suffix) to avoid platform inconsistencies. Improve documentation for 64 bit types. Make tests cover material in docs. * Fixed tests. * Rename FloatKind::Normal -> Finite * Fix half zero check. --- source/slang/slang.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) (limited to 'source/slang/slang.cpp') diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index bfc77b2e3..5fdec8259 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -37,6 +37,61 @@ namespace Slang { +/* static */const BaseTypeInfo BaseTypeInfo::s_info[Index(BaseType::CountOf)] = +{ + { 0, 0, uint8_t(BaseType::Void) }, + { uint8_t(sizeof(bool)), 0, uint8_t(BaseType::Bool) }, + { uint8_t(sizeof(int8_t)), BaseTypeInfo::Flag::Signed | BaseTypeInfo::Flag::Integer , uint8_t(BaseType::Int8) }, + { uint8_t(sizeof(int16_t)), BaseTypeInfo::Flag::Signed | BaseTypeInfo::Flag::Integer , uint8_t(BaseType::Int16) }, + { uint8_t(sizeof(int32_t)), BaseTypeInfo::Flag::Signed | BaseTypeInfo::Flag::Integer , uint8_t(BaseType::Int) }, + { uint8_t(sizeof(int64_t)), BaseTypeInfo::Flag::Signed | BaseTypeInfo::Flag::Integer , uint8_t(BaseType::Int64) }, + { uint8_t(sizeof(uint8_t)), BaseTypeInfo::Flag::Integer , uint8_t(BaseType::UInt8) }, + { uint8_t(sizeof(uint16_t)), BaseTypeInfo::Flag::Integer , uint8_t(BaseType::UInt16) }, + { uint8_t(sizeof(uint32_t)), BaseTypeInfo::Flag::Integer , uint8_t(BaseType::UInt) }, + { uint8_t(sizeof(uint64_t)), BaseTypeInfo::Flag::Integer, uint8_t(BaseType::UInt64) }, + { uint8_t(sizeof(uint16_t)), BaseTypeInfo::Flag::FloatingPoint , uint8_t(BaseType::Half) }, + { uint8_t(sizeof(float)), BaseTypeInfo::Flag::FloatingPoint , uint8_t(BaseType::Float) }, + { uint8_t(sizeof(double)), BaseTypeInfo::Flag::FloatingPoint , uint8_t(BaseType::Double) }, +}; + +/* static */bool BaseTypeInfo::check() +{ + for (Index i = 0; i < SLANG_COUNT_OF(s_info); ++i) + { + if (s_info[i].baseType != i) + { + SLANG_ASSERT(!"Inconsistency between the s_info table and BaseInfo"); + return false; + } + } + return true; +} + +/* static */UnownedStringSlice BaseTypeInfo::asText(BaseType baseType) +{ + switch (baseType) + { + case BaseType::Void: return UnownedStringSlice::fromLiteral("void"); + case BaseType::Bool: return UnownedStringSlice::fromLiteral("bool"); + case BaseType::Int8: return UnownedStringSlice::fromLiteral("int8_t"); + case BaseType::Int16: return UnownedStringSlice::fromLiteral("int16_t"); + case BaseType::Int: return UnownedStringSlice::fromLiteral("int"); + case BaseType::Int64: return UnownedStringSlice::fromLiteral("int64_t"); + case BaseType::UInt8: return UnownedStringSlice::fromLiteral("uint8_t"); + case BaseType::UInt16: return UnownedStringSlice::fromLiteral("uint16_t"); + case BaseType::UInt: return UnownedStringSlice::fromLiteral("uint"); + case BaseType::UInt64: return UnownedStringSlice::fromLiteral("uint64_t"); + case BaseType::Half: return UnownedStringSlice::fromLiteral("half"); + case BaseType::Float: return UnownedStringSlice::fromLiteral("float"); + case BaseType::Double: return UnownedStringSlice::fromLiteral("double"); + default: + { + SLANG_ASSERT(!"Unknown basic type"); + return UnownedStringSlice(); + } + } +} + // Allocate static const storage for the various interface IDs that the Slang API needs to expose static const Guid IID_IComponentType = SLANG_UUID_IComponentType; static const Guid IID_IEntryPoint = SLANG_UUID_IEntryPoint; @@ -48,6 +103,8 @@ static const Guid IID_ISlangUnknown = SLANG_UUID_ISlangUnknown; void Session::init() { + SLANG_ASSERT(BaseTypeInfo::check()); + ::memset(m_downstreamCompilerLocators, 0, sizeof(m_downstreamCompilerLocators)); DownstreamCompilerUtil::setDefaultLocators(m_downstreamCompilerLocators); m_downstreamCompilerSet = new DownstreamCompilerSet; @@ -2548,7 +2605,10 @@ Session::~Session() destroyTypeCheckingCache(); - builtinTypes = decltype(builtinTypes)(); + for (Index i = 0; i < SLANG_COUNT_OF(builtinTypes); ++i) + { + builtinTypes[i].setNull(); + } // destroy modules next loadedModuleCode = decltype(loadedModuleCode)(); } -- cgit v1.2.3