Merge pull request #2657 from def-/noreturn
Pass noReturn pragma to C code.
This commit is contained in:
commit
9422dbf337
6 changed files with 31 additions and 25 deletions
|
|
@ -676,8 +676,11 @@ proc genProcAux(m: BModule, prc: PSym) =
|
||||||
closureSetup(p, prc)
|
closureSetup(p, prc)
|
||||||
genStmts(p, prc.getBody) # modifies p.locals, p.init, etc.
|
genStmts(p, prc.getBody) # modifies p.locals, p.init, etc.
|
||||||
var generatedProc: Rope
|
var generatedProc: Rope
|
||||||
|
if sfNoReturn in prc.flags:
|
||||||
|
if hasDeclspec in extccomp.CC[extccomp.cCompiler].props:
|
||||||
|
header = "__declspec(noreturn) " & header
|
||||||
if sfPure in prc.flags:
|
if sfPure in prc.flags:
|
||||||
if hasNakedDeclspec in extccomp.CC[extccomp.cCompiler].props:
|
if hasDeclspec in extccomp.CC[extccomp.cCompiler].props:
|
||||||
header = "__declspec(naked) " & header
|
header = "__declspec(naked) " & header
|
||||||
generatedProc = rfmt(nil, "$N$1 {$n$2$3$4}$N$N",
|
generatedProc = rfmt(nil, "$N$1 {$n$2$3$4}$N$N",
|
||||||
header, p.s(cpsLocals), p.s(cpsInit), p.s(cpsStmts))
|
header, p.s(cpsLocals), p.s(cpsInit), p.s(cpsStmts))
|
||||||
|
|
@ -720,8 +723,10 @@ proc genProcPrototype(m: BModule, sym: PSym) =
|
||||||
var header = genProcHeader(m, sym)
|
var header = genProcHeader(m, sym)
|
||||||
if sym.typ.callConv != ccInline and crossesCppBoundary(m, sym):
|
if sym.typ.callConv != ccInline and crossesCppBoundary(m, sym):
|
||||||
header = "extern \"C\" " & header
|
header = "extern \"C\" " & header
|
||||||
if sfPure in sym.flags and hasNakedAttribute in CC[cCompiler].props:
|
if sfPure in sym.flags and hasAttribute in CC[cCompiler].props:
|
||||||
header.add(" __attribute__((naked))")
|
header.add(" __attribute__((naked))")
|
||||||
|
if sfNoReturn in sym.flags and hasAttribute in CC[cCompiler].props:
|
||||||
|
header.add(" __attribute__((noreturn))")
|
||||||
add(m.s[cfsProcHeaders], rfmt(nil, "$1;$n", header))
|
add(m.s[cfsProcHeaders], rfmt(nil, "$1;$n", header))
|
||||||
|
|
||||||
proc genProcNoForward(m: BModule, prc: PSym) =
|
proc genProcNoForward(m: BModule, prc: PSym) =
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,8 @@ type
|
||||||
hasAssume, # CC has __assume (Visual C extension)
|
hasAssume, # CC has __assume (Visual C extension)
|
||||||
hasGcGuard, # CC supports GC_GUARD to keep stack roots
|
hasGcGuard, # CC supports GC_GUARD to keep stack roots
|
||||||
hasGnuAsm, # CC's asm uses the absurd GNU assembler syntax
|
hasGnuAsm, # CC's asm uses the absurd GNU assembler syntax
|
||||||
hasNakedDeclspec, # CC has __declspec(naked)
|
hasDeclspec, # CC has __declspec(X)
|
||||||
hasNakedAttribute # CC has __attribute__((naked))
|
hasAttribute, # CC has __attribute__((X))
|
||||||
TInfoCCProps* = set[TInfoCCProp]
|
TInfoCCProps* = set[TInfoCCProp]
|
||||||
TInfoCC* = tuple[
|
TInfoCC* = tuple[
|
||||||
name: string, # the short name of the compiler
|
name: string, # the short name of the compiler
|
||||||
|
|
@ -85,7 +85,7 @@ compiler gcc:
|
||||||
structStmtFmt: "$1 $3 $2 ", # struct|union [packed] $name
|
structStmtFmt: "$1 $3 $2 ", # struct|union [packed] $name
|
||||||
packedPragma: "__attribute__((__packed__))",
|
packedPragma: "__attribute__((__packed__))",
|
||||||
props: {hasSwitchRange, hasComputedGoto, hasCpp, hasGcGuard, hasGnuAsm,
|
props: {hasSwitchRange, hasComputedGoto, hasCpp, hasGcGuard, hasGnuAsm,
|
||||||
hasNakedAttribute})
|
hasAttribute})
|
||||||
|
|
||||||
# LLVM Frontend for GCC/G++
|
# LLVM Frontend for GCC/G++
|
||||||
compiler llvmGcc:
|
compiler llvmGcc:
|
||||||
|
|
@ -127,7 +127,7 @@ compiler vcc:
|
||||||
asmStmtFrmt: "__asm{$n$1$n}$n",
|
asmStmtFrmt: "__asm{$n$1$n}$n",
|
||||||
structStmtFmt: "$3$n$1 $2",
|
structStmtFmt: "$3$n$1 $2",
|
||||||
packedPragma: "#pragma pack(1)",
|
packedPragma: "#pragma pack(1)",
|
||||||
props: {hasCpp, hasAssume, hasNakedDeclspec})
|
props: {hasCpp, hasAssume, hasDeclspec})
|
||||||
|
|
||||||
# Intel C/C++ Compiler
|
# Intel C/C++ Compiler
|
||||||
compiler icl:
|
compiler icl:
|
||||||
|
|
|
||||||
|
|
@ -1110,7 +1110,7 @@ var programResult* {.exportc: "nim_program_result".}: int
|
||||||
## prematurely using ``quit``, this value is ignored.
|
## prematurely using ``quit``, this value is ignored.
|
||||||
|
|
||||||
proc quit*(errorcode: int = QuitSuccess) {.
|
proc quit*(errorcode: int = QuitSuccess) {.
|
||||||
magic: "Exit", importc: "exit", header: "<stdlib.h>", noReturn.}
|
magic: "Exit", importc: "exit", header: "<stdlib.h>", noreturn.}
|
||||||
## Stops the program immediately with an exit code.
|
## Stops the program immediately with an exit code.
|
||||||
##
|
##
|
||||||
## Before stopping the program the "quit procedures" are called in the
|
## Before stopping the program the "quit procedures" are called in the
|
||||||
|
|
@ -2270,20 +2270,21 @@ when hostOS == "standalone":
|
||||||
include panicoverride
|
include panicoverride
|
||||||
|
|
||||||
when not declared(sysFatal):
|
when not declared(sysFatal):
|
||||||
template sysFatal(exceptn: typedesc, message: string) =
|
when hostOS == "standalone":
|
||||||
when hostOS == "standalone":
|
proc sysFatal(exceptn: typedesc, message: string) {.inline.} =
|
||||||
panic(message)
|
panic(message)
|
||||||
else:
|
|
||||||
|
proc sysFatal(exceptn: typedesc, message, arg: string) {.inline.} =
|
||||||
|
rawoutput(message)
|
||||||
|
panic(arg)
|
||||||
|
else:
|
||||||
|
proc sysFatal(exceptn: typedesc, message: string) {.inline, noReturn.} =
|
||||||
var e: ref exceptn
|
var e: ref exceptn
|
||||||
new(e)
|
new(e)
|
||||||
e.msg = message
|
e.msg = message
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
template sysFatal(exceptn: typedesc, message, arg: string) =
|
proc sysFatal(exceptn: typedesc, message, arg: string) {.inline, noReturn.} =
|
||||||
when hostOS == "standalone":
|
|
||||||
rawoutput(message)
|
|
||||||
panic(arg)
|
|
||||||
else:
|
|
||||||
var e: ref exceptn
|
var e: ref exceptn
|
||||||
new(e)
|
new(e)
|
||||||
e.msg = message & arg
|
e.msg = message & arg
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,11 @@
|
||||||
|
|
||||||
# simple integer arithmetic with overflow checking
|
# simple integer arithmetic with overflow checking
|
||||||
|
|
||||||
proc raiseOverflow {.compilerproc, noinline, noreturn.} =
|
proc raiseOverflow {.compilerproc, noinline.} =
|
||||||
# a single proc to reduce code size to a minimum
|
# a single proc to reduce code size to a minimum
|
||||||
sysFatal(OverflowError, "over- or underflow")
|
sysFatal(OverflowError, "over- or underflow")
|
||||||
|
|
||||||
proc raiseDivByZero {.compilerproc, noinline, noreturn.} =
|
proc raiseDivByZero {.compilerproc, noinline.} =
|
||||||
sysFatal(DivByZeroError, "division by zero")
|
sysFatal(DivByZeroError, "division by zero")
|
||||||
|
|
||||||
proc addInt64(a, b: int64): int64 {.compilerProc, inline.} =
|
proc addInt64(a, b: int64): int64 {.compilerProc, inline.} =
|
||||||
|
|
@ -327,13 +327,13 @@ when not declared(mulInt):
|
||||||
# We avoid setting the FPU control word here for compatibility with libraries
|
# We avoid setting the FPU control word here for compatibility with libraries
|
||||||
# written in other languages.
|
# written in other languages.
|
||||||
|
|
||||||
proc raiseFloatInvalidOp {.noinline, noreturn.} =
|
proc raiseFloatInvalidOp {.noinline.} =
|
||||||
sysFatal(FloatInvalidOpError, "FPU operation caused a NaN result")
|
sysFatal(FloatInvalidOpError, "FPU operation caused a NaN result")
|
||||||
|
|
||||||
proc nanCheck(x: float64) {.compilerProc, inline.} =
|
proc nanCheck(x: float64) {.compilerProc, inline.} =
|
||||||
if x != x: raiseFloatInvalidOp()
|
if x != x: raiseFloatInvalidOp()
|
||||||
|
|
||||||
proc raiseFloatOverflow(x: float64) {.noinline, noreturn.} =
|
proc raiseFloatOverflow(x: float64) {.noinline.} =
|
||||||
if x > 0.0:
|
if x > 0.0:
|
||||||
sysFatal(FloatOverflowError, "FPU operation caused an overflow")
|
sysFatal(FloatOverflowError, "FPU operation caused an overflow")
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -9,16 +9,16 @@
|
||||||
|
|
||||||
# Implementation of some runtime checks.
|
# Implementation of some runtime checks.
|
||||||
|
|
||||||
proc raiseRangeError(val: BiggestInt) {.compilerproc, noreturn, noinline.} =
|
proc raiseRangeError(val: BiggestInt) {.compilerproc, noinline.} =
|
||||||
when hostOS == "standalone":
|
when hostOS == "standalone":
|
||||||
sysFatal(RangeError, "value out of range")
|
sysFatal(RangeError, "value out of range")
|
||||||
else:
|
else:
|
||||||
sysFatal(RangeError, "value out of range: ", $val)
|
sysFatal(RangeError, "value out of range: ", $val)
|
||||||
|
|
||||||
proc raiseIndexError() {.compilerproc, noreturn, noinline.} =
|
proc raiseIndexError() {.compilerproc, noinline.} =
|
||||||
sysFatal(IndexError, "index out of bounds")
|
sysFatal(IndexError, "index out of bounds")
|
||||||
|
|
||||||
proc raiseFieldError(f: string) {.compilerproc, noreturn, noinline.} =
|
proc raiseFieldError(f: string) {.compilerproc, noinline.} =
|
||||||
sysFatal(FieldError, f, " is not accessible")
|
sysFatal(FieldError, f, " is not accessible")
|
||||||
|
|
||||||
proc chckIndx(i, a, b: int): int =
|
proc chckIndx(i, a, b: int): int =
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,13 @@ proc exit(code: int) {.importc, header: "<stdlib.h>", cdecl.}
|
||||||
proc rawoutput(s: string) =
|
proc rawoutput(s: string) =
|
||||||
printf("%s\n", s)
|
printf("%s\n", s)
|
||||||
|
|
||||||
proc panic(s: string) =
|
proc panic(s: string) {.noreturn.} =
|
||||||
rawoutput(s)
|
rawoutput(s)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
# Alternatively we also could implement these 2 here:
|
# Alternatively we also could implement these 2 here:
|
||||||
#
|
#
|
||||||
# template sysFatal(exceptn: typeDesc, message: string)
|
# proc sysFatal(exceptn: typeDesc, message: string) {.noReturn.}
|
||||||
# template sysFatal(exceptn: typeDesc, message, arg: string)
|
# proc sysFatal(exceptn: typeDesc, message, arg: string) {.noReturn.}
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue