diff options
| author | Yong He <yonghe@outlook.com> | 2025-04-28 11:42:22 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-28 11:42:22 -0700 |
| commit | c39c29bf4c52a85d7c83cc8b66ae45e265f9e078 (patch) | |
| tree | 969339828d49d7db92ed9294a17bd34cc021db84 /source/core | |
| parent | 8f6c6e333c06ae1c3b9f00396563c14a2ae09b4d (diff) | |
Add Slang Byte Code generation and interpreter. (#6896)
* Add Slang Byte Code generation and interpreter.
* Fix compile issues.
* format code
* More compile fix.
* Fix clang issue.
* Fix more clang issues.
* Another clang fix.
* Fix clang issues.
* Fix another clang issue.
* Fix wasm build.
* Update building.md
* Fix test-server.
* Fix compile error.
* Fix bug.
---------
Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/slang-string-escape-util.cpp | 15 | ||||
| -rw-r--r-- | source/core/slang-string-escape-util.h | 3 | ||||
| -rw-r--r-- | source/core/slang-string-util.cpp | 153 | ||||
| -rw-r--r-- | source/core/slang-string-util.h | 5 | ||||
| -rw-r--r-- | source/core/slang-type-text-util.cpp | 1 |
5 files changed, 177 insertions, 0 deletions
diff --git a/source/core/slang-string-escape-util.cpp b/source/core/slang-string-escape-util.cpp index 42e757b23..0645d94ba 100644 --- a/source/core/slang-string-escape-util.cpp +++ b/source/core/slang-string-escape-util.cpp @@ -1184,4 +1184,19 @@ StringEscapeUtil::Handler* StringEscapeUtil::getHandler(Style style) return SLANG_OK; } +String StringEscapeUtil::escapeString(UnownedStringSlice input, StringEscapeUtil::Style style) +{ + StringBuilder sb; + auto handler = StringEscapeUtil::getHandler(style); + StringEscapeUtil::appendQuoted(handler, input, sb); + return sb.produceString(); +} + +String StringEscapeUtil::unescapeString(UnownedStringSlice input, StringEscapeUtil::Style style) +{ + StringBuilder sb; + auto handler = StringEscapeUtil::getHandler(style); + StringEscapeUtil::appendUnquoted(handler, input, sb); + return sb.produceString(); +} } // namespace Slang diff --git a/source/core/slang-string-escape-util.h b/source/core/slang-string-escape-util.h index 6e02d772d..ece8de79f 100644 --- a/source/core/slang-string-escape-util.h +++ b/source/core/slang-string-escape-util.h @@ -113,6 +113,9 @@ struct StringEscapeUtil Handler* handler, const UnownedStringSlice& slice, StringBuilder& out); + + static String escapeString(UnownedStringSlice input, Style style = Style::Slang); + static String unescapeString(UnownedStringSlice input, Style style = Style::Slang); }; diff --git a/source/core/slang-string-util.cpp b/source/core/slang-string-util.cpp index ba234e30b..65758fd14 100644 --- a/source/core/slang-string-util.cpp +++ b/source/core/slang-string-util.cpp @@ -390,6 +390,159 @@ UnownedStringSlice StringUtil::getAtInSplit( return builder; } +template<typename T> +static T readValue(ArrayView<const void*> ptrToArgs, Count& argIndex) +{ + if (argIndex < ptrToArgs.getCount()) + { + T value; + memcpy(&value, ptrToArgs[argIndex], sizeof(T)); + argIndex++; + return value; + } + return T(); +} + +String StringUtil::makeStringWithFormatFromArgArray( + const char* format, + ArrayView<const void*> ptrToArgs) +{ + if (!format) + { + return String(); + } + StringBuilder builder; + const char* ptr = format; + Count argIndex = 0; + auto consumeString = [&]() + { + if (argIndex < ptrToArgs.getCount()) + { + const char* strPtr = *(const char**)ptrToArgs[argIndex]; + argIndex++; + if (strPtr) + { + // Append the string to the builder + builder.append(strPtr); + } + } + }; +#define ADVANCE_PTR \ + ptr++; \ + if (!*ptr) \ + { \ + return builder.produceString(); \ + } + + while (*ptr) + { + if (*ptr == '%') + { + const char* formatStart = ptr; + ADVANCE_PTR; + if (*ptr == 's') + { + // If we have a %s, then we want to append the data + consumeString(); + // Move past the 's' + ADVANCE_PTR; + continue; + } + if (*ptr == '-') + { + // If we have a %- then we want to continue parsing format string. + ADVANCE_PTR; + } + while (CharUtil::isDigit(*ptr)) + { + // Skip the digits after the '.' + ADVANCE_PTR; + } + if (*ptr == '.') + { + ADVANCE_PTR; + while (CharUtil::isDigit(*ptr)) + { + // Skip the digits after the '.' + ADVANCE_PTR; + } + } + int isLong = 0; + if (*ptr == 'l' || *ptr == 'L') + { + // If we have a 'l' or 'L', then we want to skip it. + ADVANCE_PTR; + isLong = 1; + if (*ptr == 'l' || *ptr == 'L') + { + // If we have another 'l' or 'L', then we want to skip it too. + ADVANCE_PTR; + isLong = 2; + } + } + const char typeChar = *ptr; + ADVANCE_PTR; + String formatStr = UnownedStringSlice(formatStart, ptr); + switch (CharUtil::toLower(typeChar)) + { + case 'd': + case 'x': + case 'i': + case 'u': + case 'o': + case 'c': + if (isLong == 2) + { + StringUtil::appendFormat( + builder, + formatStr.getBuffer(), + readValue<int64_t>(ptrToArgs, argIndex)); + } + else + { + StringUtil::appendFormat( + builder, + formatStr.getBuffer(), + readValue<int>(ptrToArgs, argIndex)); + } + break; + case 'e': + case 'f': + case 'g': + if (isLong != 0) + { + StringUtil::appendFormat( + builder, + formatStr.getBuffer(), + readValue<double>(ptrToArgs, argIndex)); + } + else + { + StringUtil::appendFormat( + builder, + formatStr.getBuffer(), + readValue<float>(ptrToArgs, argIndex)); + } + break; + case 'n': + break; + case '%': + // If we have a '%%' then we want to append a single '%' + builder.appendChar('%'); + continue; + } + } + else + { + // Just append the character + builder.appendChar(*ptr); + ptr++; + } + } + return builder.produceString(); +} + + /* static */ UnownedStringSlice StringUtil::getSlice(ISlangBlob* blob) { if (blob) diff --git a/source/core/slang-string-util.h b/source/core/slang-string-util.h index 9e7b2d65a..4f4368ba3 100644 --- a/source/core/slang-string-util.h +++ b/source/core/slang-string-util.h @@ -135,6 +135,11 @@ struct StringUtil /// Create a string from the format string applying args (like sprintf) static String makeStringWithFormat(const char* format, ...); + /// Create a string from the format string and arguments in a buffer. + static String makeStringWithFormatFromArgArray( + const char* format, + ArrayView<const void*> ptrToArgs); + /// Given a string held in a blob, returns as a String /// Returns an empty string if blob is nullptr static String getString(ISlangBlob* blob); diff --git a/source/core/slang-type-text-util.cpp b/source/core/slang-type-text-util.cpp index 01ce0d1ac..2261ed614 100644 --- a/source/core/slang-type-text-util.cpp +++ b/source/core/slang-type-text-util.cpp @@ -87,6 +87,7 @@ static const TypeTextUtil::CompileTargetInfo s_compileTargetInfos[] = { "wgsl-spirv-asm,wgsl-spirv-assembly", "SPIR-V assembly via WebGPU shading language"}, {SLANG_WGSL_SPIRV, "wgsl-spirv", "wgsl-spirv", "SPIR-V via WebGPU shading language"}, + {SLANG_HOST_VM, "slang-vm", "slangvm,slang-vm", "Slang VM byte code"}, }; static const NamesDescriptionValue s_languageInfos[] = { |
