Files
Nim/lib/system/embedded.nim
Yuriy Glukhov 02f73120ae Fixes #21235, #23602, #24978, #25018 (#25030)
Reworked closureiter transformation.

- Convolutedly nested finallies should cause no problems now.
- CurrentException state now follows nim runtime rules (pushes and pops
appropriately), and mimics normal code, which is somewhat buggy, see
#25031
- Previously state optimization (removing empty states or extra jumps)
missed some opportunities, I've reimplemented it to do everything
possible to optimize the states. At this point any extra states or jumps
should be considered a bug.

The resulting codegen (compiled binaries) is also slightly smaller.

**BUT:**
- I had to change C++ reraising logic, see expt.nim. Because with
closure iters `currentException` is not always in sync with C++'s notion
of current exception. From my tests and understanding of C++ runtime
there should not be any problems, but I'm only 99% sure :)
- I've reused `nfNoRewrite` flag in one specific case during the
transformation. This flag is also used in term-rewriting logic. Again,
99% sure, these 2 scenarios will never intersect.

(cherry picked from commit 36f8cefa85)
2025-07-08 16:14:05 +02:00

62 lines
2.1 KiB
Nim

#
#
# Nim's Runtime Library
# (c) Copyright 2012 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
# Bare-bones implementation of some things for embedded targets.
proc chckIndx(i, a, b: int): int {.inline, compilerproc.}
proc chckRange(i, a, b: int): int {.inline, compilerproc.}
proc chckRangeF(x, a, b: float): float {.inline, compilerproc.}
proc chckNil(p: pointer) {.inline, compilerproc.}
proc nimFrame(s: PFrame) {.compilerRtl, inl, exportc: "nimFrame".} = discard
proc popFrame {.compilerRtl, inl.} = discard
proc setFrame(s: PFrame) {.compilerRtl, inl.} = discard
when not gotoBasedExceptions:
proc pushSafePoint(s: PSafePoint) {.compilerRtl, inl.} = discard
proc popSafePoint {.compilerRtl, inl.} = discard
proc pushCurrentException(e: ref Exception) {.compilerRtl, inl.} = discard
proc popCurrentException {.compilerRtl, inl.} = discard
# some platforms have native support for stack traces:
const
nativeStackTraceSupported = false
hasSomeStackTrace = false
proc quitOrDebug() {.noreturn, importc: "abort", header: "<stdlib.h>", nodecl.}
proc raiseException(e: ref Exception, ename: cstring) {.compilerRtl.} =
sysFatal(ReraiseDefect, "exception handling is not available")
proc raiseExceptionEx(e: sink(ref Exception), ename, procname, filename: cstring,
line: int) {.compilerRtl.} =
sysFatal(ReraiseDefect, "exception handling is not available")
proc reraiseException() {.compilerRtl.} =
sysFatal(ReraiseDefect, "no exception to reraise")
proc raiseDefect() {.compilerRtl.} =
sysFatal(ReraiseDefect, "exception handling is not available")
proc writeStackTrace() = discard
proc unsetControlCHook() = discard
proc setControlCHook(hook: proc () {.noconv.}) = discard
when gotoBasedExceptions:
var nimInErrorMode {.threadvar.}: bool
proc nimErrorFlag(): ptr bool {.compilerRtl, inl.} =
result = addr(nimInErrorMode)
proc nimTestErrorFlag() {.compilerRtl.} =
if nimInErrorMode:
sysFatal(ReraiseDefect, "exception handling is not available")