Class PanicThrowInstrs

java.lang.Object
org.ek9lang.compiler.phase7.support.PanicThrowInstrs

public final class PanicThrowInstrs extends Object
THE single-source-of-truth for emitting the "construct and throw an org.ek9.lang::Panic" IR sequence - EK9's one representation of an uncatchable fatal invariant breach.

A Panic is a SIBLING of Exception (not a subtype), so it cannot be caught by user try/catch; it propagates to the generated program _main, which reports it and exits with the Panic's exit code. Every fatal-invariant site lowers to the identical sequence:

  msg  = LOAD_LITERAL <message>,  org.ek9.lang::String  ; RETAIN ; SCOPE_REGISTER
  code = LOAD_LITERAL <exitCode>, org.ek9.lang::Integer ; RETAIN ; SCOPE_REGISTER
  p    = CALL org.ek9.lang::Panic.<init>(String, Integer)                ; RETAIN
  THROW p

This helper emits ONLY that msg/code/construct/throw body - no labels. The caller owns the surrounding guard: evaluate the invariant, BRANCH_TRUE past this sequence to an ok-label when it holds, emit this, then place the ok-label (exactly the shape the three call sites use).

Do NOT re-derive this sequence inline - call emit(IRGenerationContext, List, String, String, String, DebugInfo). It was consolidated (SPEC-assertPanic §6) from ConstrainedTypeDfnGenerator (constrained-type breach = 1st site) once user require and the for-range set-check became the 2nd/3rd sites. Omitting the RETAIN/SCOPE_REGISTER on the msg/code temps orphans them on the ARC/LLVM backend - a leak on every breach.

  • Field Details

    • DEFAULT_EXIT_CODE

      public static final String DEFAULT_EXIT_CODE
      The exit code every Panic site uses when it reaches the generated program _main handler. Shared across constrained-type breaches, user require, and the compiler-injected for-range set-check so all fatal-invariant sites agree (SPEC-assertPanic §9.1). The exit code travels inside the Panic, so distinct sites could diverge in future - but they share this until there is a reason not to.
      See Also:
  • Method Details

    • locationSuffix

      public static String locationSuffix(DebugInfo debugInfo)
      Format the " at file:line:col" suffix appended to a Panic message, or "" when the location is unknown. Matches the exact form the old REQUIRE backend produced, so that moving require/for-range message formatting phase-ward leaves runtime message text byte-identical.
    • emit

      public static void emit(IRGenerationContext stackContext, List<IRInstr> instructions, String message, String exitCode, String scopeId, DebugInfo debugInfo)
      Append the construct-and-throw-Panic sequence to instructions.
      Parameters:
      stackContext - for temp-name generation
      instructions - the instruction list to append to
      message - the fully-formed human-readable Panic message (becomes a String literal)
      exitCode - the exit-code literal (typically DEFAULT_EXIT_CODE)
      scopeId - the scope the msg/code temps register into for ARC cleanup
      debugInfo - source location attached to every emitted instruction