system.nim refactorings for IC (#25295)

Generally useful refactoring as it produces better code.

(cherry picked from commit 0f7b378467)
This commit is contained in:
Andreas Rumpf
2025-11-19 16:27:31 +01:00
committed by narimiran
parent a5e73ff408
commit 431e01eaf2
13 changed files with 253 additions and 191 deletions

View File

@@ -19,12 +19,16 @@ import std/private/miscdollars
type InstantiationInfo = tuple[filename: string, line: int, column: int]
{.push overflowChecks: off, rangeChecks: off.}
proc `$`(info: InstantiationInfo): string =
# The +1 is needed here
# instead of overriding `$` (and changing its meaning), consider explicit name.
result = ""
result.toLocation(info.filename, info.line, info.column + 1)
{.pop.}
# ---------------------------------------------------------------------------

View File

@@ -29,18 +29,23 @@ const
# doAssert res == digits100
# ```
proc utoa2Digits*(buf: var openArray[char]; pos: int; digits: uint32) {.inline.} =
{.push checks: off, stackTrace: off.}
when not defined(nimHasEnforceNoRaises):
{.pragma: enforceNoRaises.}
proc utoa2Digits*(buf: var openArray[char]; pos: int; digits: uint32) {.inline, enforceNoRaises.} =
buf[pos] = digits100[2 * digits]
buf[pos+1] = digits100[2 * digits + 1]
#copyMem(buf, unsafeAddr(digits100[2 * digits]), 2 * sizeof((char)))
proc trailingZeros2Digits*(digits: uint32): int {.inline.} =
proc trailingZeros2Digits*(digits: uint32): int {.inline, enforceNoRaises.} =
trailingZeros100[digits]
when defined(js):
proc numToString(a: SomeInteger): cstring {.importjs: "((#) + \"\")".}
func addChars[T](result: var string, x: T, start: int, n: int) {.inline.} =
func addChars[T](result: var string, x: T, start: int, n: int) {.inline, enforceNoRaises.} =
let old = result.len
result.setLen old + n
template impl =
@@ -52,10 +57,10 @@ func addChars[T](result: var string, x: T, start: int, n: int) {.inline.} =
{.noSideEffect.}:
copyMem result[old].addr, x[start].unsafeAddr, n
func addChars[T](result: var string, x: T) {.inline.} =
func addChars[T](result: var string, x: T) {.inline, enforceNoRaises.} =
addChars(result, x, 0, x.len)
func addIntImpl(result: var string, x: uint64) {.inline.} =
func addIntImpl(result: var string, x: uint64) {.inline, enforceNoRaises.} =
var tmp {.noinit.}: array[24, char]
var num = x
var next = tmp.len - 1
@@ -79,8 +84,6 @@ func addIntImpl(result: var string, x: uint64) {.inline.} =
dec next
addChars(result, tmp, next, tmp.len - next)
when not defined(nimHasEnforceNoRaises):
{.pragma: enforceNoRaises.}
func addInt*(result: var string, x: uint64) {.enforceNoRaises.} =
when nimvm: addIntImpl(result, x)

View File

@@ -4,7 +4,13 @@ template toLocation*(result: var string, file: string | cstring, line: int, col:
## avoids spurious allocations
# Hopefully this can be re-used everywhere so that if a user needs to customize,
# it can be done in a single place.
result.add file
when file is cstring:
var i = 0
while file[i] != '\0':
add(result, file[i])
inc i
else:
result.add file
if line > 0:
result.add "("
addInt(result, line)