diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2020-02-06 14:31:09 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-06 14:31:09 -0500 |
| commit | d3331fba6eaab44646010b556106da38925d43e0 (patch) | |
| tree | f54115540a457375a5d050bbfe1b04855b3f791b /source/slang/slang-emit-c-like.cpp | |
| parent | 9c84cceffba26817721a23a1a85a48644bf3a560 (diff) | |
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.
Diffstat (limited to 'source/slang/slang-emit-c-like.cpp')
| -rw-r--r-- | source/slang/slang-emit-c-like.cpp | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp index c2bfad0a7..a383caecf 100644 --- a/source/slang/slang-emit-c-like.cpp +++ b/source/slang/slang-emit-c-like.cpp @@ -756,27 +756,57 @@ void CLikeSourceEmitter::emitSimpleValueImpl(IRInst* inst) case BaseType::Int16: case BaseType::Int: { + // NOTE! This hack is required, otherwise we get different results across targets. + // You'd hope that outputting L suffix would be enough to make this work, and not require a cast, but testing shows L suffix + // does not have the same meaning across targets. + // + // For example + // + // uint64_t v = 0x80000000; + // + // When output this becomes... + // v_0 = uint64_t(-2147483648L); + // + // On MSVC/Gcc/Clang this is equal to 0x80000000, elsewhere it's 0xffffffff80000000 elsewhere. + // Note that '-' isn't the issue because v0 = uint64_t(0x80000000L); produces the same issue + // + // If we use a cast, we get the same result across targets (which is why the hack is here). + // + // Why? It's not clear - it seems likely that it's related to the order of how the promotion takes place. + // + // If we convert from int32_t -> uint64_t, there are two possible scenarios + // 1) int32_t -> int64_t -> uint64_t (ie widen first then do sign type change) + // 2) int32_t -> uint32_t -> uint64_t (ie do sign type change then widen) + // + // 2 would produce what we see on C++, 1 what we see everywhere else. + // + // Why having a cast or having a suffix would make a difference though is not clear. It is also possible that the + // L suffix is just ignored, and the literal is really a 'non typed' int literal in C++. + + m_writer->emit("int("); m_writer->emit(litInst->value.intVal); - break; + m_writer->emit(")"); + return; } case BaseType::UInt8: case BaseType::UInt16: case BaseType::UInt: { m_writer->emit(UInt(litInst->value.intVal)); + m_writer->emit("U"); break; } case BaseType::Int64: { m_writer->emitInt64(int64_t(litInst->value.intVal)); - m_writer->emit("ll"); + m_writer->emit("LL"); break; } case BaseType::UInt64: { SLANG_COMPILE_TIME_ASSERT(sizeof(litInst->value.intVal) >= sizeof(uint64_t)); m_writer->emitUInt64(uint64_t(litInst->value.intVal)); - m_writer->emit("ull"); + m_writer->emit("ULL"); break; } } |
