Skip to content

Commit

Permalink
os-ansic
Browse files Browse the repository at this point in the history
  • Loading branch information
zevv committed Jan 10, 2020
1 parent e9e1140 commit d0a9edd
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 68 deletions.
6 changes: 5 additions & 1 deletion compiler/platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type
osNone, osDos, osWindows, osOs2, osLinux, osMorphos, osSkyos, osSolaris,
osIrix, osNetbsd, osFreebsd, osOpenbsd, osDragonfly, osAix, osPalmos, osQnx,
osAmiga, osAtari, osNetware, osMacos, osMacosx, osIos, osHaiku, osAndroid, osVxWorks
osGenode, osJS, osNimVM, osStandalone, osNintendoSwitch
osGenode, osJS, osNimVM, osStandalone, osNintendoSwitch, osAnsiC

type
TInfoOSProp* = enum
Expand Down Expand Up @@ -177,6 +177,10 @@ const
objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
scriptExt: ".sh", curDir: ".", exeExt: ".elf", extSep: ".",
props: {ospNeedsPIC, ospPosix}),
(name: "AnsiC", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/",
objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/",
scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".",
props: {}),
]

type
Expand Down
4 changes: 2 additions & 2 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2097,7 +2097,7 @@ template sysAssert(cond: bool, msg: string) =

const hasAlloc = (hostOS != "standalone" or not defined(nogc)) and not defined(nimscript)

when not defined(JS) and not defined(nimscript) and hostOS != "standalone":
when not defined(JS) and not defined(nimscript) and hostOS != "standalone" and hostOS != "ansic":
include "system/cgprocs"
when not defined(JS) and not defined(nimscript) and hasAlloc and not defined(nimSeqsV2):
proc addChar(s: NimString, c: char): NimString {.compilerproc, benign.}
Expand Down Expand Up @@ -3723,7 +3723,7 @@ when not defined(JS): #and not defined(nimscript):
{.pop.} # stack trace
{.pop.} # stack trace

when hostOS != "standalone" and not defined(nimscript):
when hostOS != "standalone" and hostOS != "ansic" and not defined(nimscript):
include "system/dyncalls"
when not defined(nimscript):
include "system/sets"
Expand Down
97 changes: 45 additions & 52 deletions lib/system/excpt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ when defined(nativeStacktrace) and nativeStackTraceSupported:
# interested in
enabled = true

when not hasThreadSupport:
when hasSomeStackTrace and not hasThreadSupport:
var
tempFrames: array[0..127, PFrame] # should not be alloc'd on stack

Expand Down Expand Up @@ -261,52 +261,53 @@ proc `$`(s: seq[StackTraceEntry]): string =
elif s[i].line == reraisedFromEnd: result.add "]]\n"
else: addFrameEntry(result, s[i])

proc auxWriteStackTrace(f: PFrame, s: var string) =
when hasThreadSupport:
when hasSomeStackTrace:

proc auxWriteStackTrace(f: PFrame, s: var string) =
when hasThreadSupport:
var
tempFrames: array[0..127, PFrame] # but better than a threadvar
const
firstCalls = 32
var
tempFrames: array[0..127, PFrame] # but better than a threadvar
const
firstCalls = 32
var
it = f
i = 0
total = 0
# setup long head:
while it != nil and i <= high(tempFrames)-firstCalls:
tempFrames[i] = it
inc(i)
inc(total)
it = it.prev
# go up the stack to count 'total':
var b = it
while it != nil:
inc(total)
it = it.prev
var skipped = 0
if total > len(tempFrames):
# skip N
skipped = total-i-firstCalls+1
for j in 1..skipped:
if b != nil: b = b.prev
# create '...' entry:
tempFrames[i] = nil
inc(i)
# setup short tail:
while b != nil and i <= high(tempFrames):
tempFrames[i] = b
inc(i)
b = b.prev
for j in countdown(i-1, 0):
if tempFrames[j] == nil:
add(s, "(")
add(s, $skipped)
add(s, " calls omitted) ...\n")
else:
addFrameEntry(s, tempFrames[j])
it = f
i = 0
total = 0
# setup long head:
while it != nil and i <= high(tempFrames)-firstCalls:
tempFrames[i] = it
inc(i)
inc(total)
it = it.prev
# go up the stack to count 'total':
var b = it
while it != nil:
inc(total)
it = it.prev
var skipped = 0
if total > len(tempFrames):
# skip N
skipped = total-i-firstCalls+1
for j in 1..skipped:
if b != nil: b = b.prev
# create '...' entry:
tempFrames[i] = nil
inc(i)
# setup short tail:
while b != nil and i <= high(tempFrames):
tempFrames[i] = b
inc(i)
b = b.prev
for j in countdown(i-1, 0):
if tempFrames[j] == nil:
add(s, "(")
add(s, $skipped)
add(s, " calls omitted) ...\n")
else:
addFrameEntry(s, tempFrames[j])

proc stackTraceAvailable*(): bool
proc stackTraceAvailable*(): bool

when hasSomeStackTrace:
proc rawWriteStackTrace(s: var string) =
when defined(nimStackTraceOverride):
add(s, "Traceback (most recent call last, using override)\n")
Expand Down Expand Up @@ -425,14 +426,6 @@ when gotoBasedExceptions:
currException = nil
quit(1)

addQuitProc(proc () {.noconv.} =
if currException != nil:
reportUnhandledError(currException)
# emulate: ``programResult = 1`` via abort() and a nop signal handler.
c_signal(SIGABRT, (proc (sign: cint) {.noconv, benign.} = discard))
c_abort()
)

proc raiseExceptionAux(e: sink(ref Exception)) {.nodestroy.} =
if localRaiseHook != nil:
if not localRaiseHook(e): return
Expand Down
19 changes: 6 additions & 13 deletions lib/system/mmdisp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,11 @@ elif defined(gogc):
proc deallocOsPages(r: var MemRegion) {.inline.} = discard
proc deallocOsPages() {.inline.} = discard

elif defined(nogc) and defined(useMalloc):
elif (defined(nogc) or defined(gcDestructors)) and defined(useMalloc):

when not defined(useNimRtl):
proc alloc(size: Natural): pointer =
var x = c_malloc(size + sizeof(size))
var x = c_malloc (size + sizeof(size)).csize_t
if x == nil: raiseOutOfMem()

cast[ptr int](x)[] = size
Expand All @@ -371,7 +371,7 @@ elif defined(nogc) and defined(useMalloc):
var x = cast[pointer](cast[int](p) - sizeof(newsize))
let oldsize = cast[ptr int](x)[]

x = c_realloc(x, newsize + sizeof(newsize))
x = c_realloc(x, (newsize + sizeof(newsize)).csize_t)

if x == nil: raiseOutOfMem()

Expand All @@ -384,13 +384,13 @@ elif defined(nogc) and defined(useMalloc):
proc dealloc(p: pointer) = c_free(cast[pointer](cast[int](p) - sizeof(int)))

proc allocShared(size: Natural): pointer =
result = c_malloc(size)
result = c_malloc(size.csize_t)
if result == nil: raiseOutOfMem()
proc allocShared0(size: Natural): pointer =
result = alloc(size)
zeroMem(result, size)
proc reallocShared(p: pointer, newsize: Natural): pointer =
result = c_realloc(p, newsize)
result = c_realloc(p, newsize.csize_t)
if result == nil: raiseOutOfMem()
proc deallocShared(p: pointer) = c_free(p)

Expand All @@ -400,7 +400,7 @@ elif defined(nogc) and defined(useMalloc):
proc GC_setStrategy(strategy: GC_Strategy) = discard
proc GC_enableMarkAndSweep() = discard
proc GC_disableMarkAndSweep() = discard
proc GC_getStatistics(): string = return ""
#proc GC_getStatistics(): string = return ""

proc getOccupiedMem(): int = discard
proc getFreeMem(): int = discard
Expand All @@ -410,13 +410,6 @@ elif defined(nogc) and defined(useMalloc):

proc initGC() = discard

proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
result = alloc0(size)
proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} =
result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize))
cast[PGenericSeq](result).len = len
cast[PGenericSeq](result).reserved = len

proc newObjNoInit(typ: PNimType, size: int): pointer =
result = alloc(size)

Expand Down
11 changes: 11 additions & 0 deletions lib/system/osalloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -295,5 +295,16 @@ elif hostOS == "standalone" or defined(StandaloneHeapSize):
proc osDeallocPages(p: pointer, size: int) {.inline.} =
if bumpPointer-size == cast[int](p):
dec bumpPointer, size

elif hostOS == "ansic":
proc osAllocPages(size: int): pointer {.inline.} =
result = c_malloc(size.csize_t)

proc osTryAllocPages(size: int): pointer {.inline.} =
result = c_malloc(size.csize_t)

proc osDeallocPages(p: pointer, size: int) {.inline.} =
c_free(p)

else:
{.error: "Port memory manager to your platform".}

0 comments on commit d0a9edd

Please sign in to comment.