[backport] run nimpretty on os-related stuff

This commit is contained in:
narimiran 2019-09-27 12:17:00 +02:00
commit 34d0be2ec1
10 changed files with 191 additions and 182 deletions

View file

@ -39,7 +39,8 @@ proc bitxor*[T: SomeInteger](x, y: T): T {.magic: "BitxorI", noSideEffect.}
const useBuiltins = not defined(noIntrinsicsBitOpts)
const noUndefined = defined(noUndefinedBitOpts)
const useGCC_builtins = (defined(gcc) or defined(llvm_gcc) or defined(clang)) and useBuiltins
const useGCC_builtins = (defined(gcc) or defined(llvm_gcc) or
defined(clang)) and useBuiltins
const useICC_builtins = defined(icc) and useBuiltins
const useVCC_builtins = defined(vcc) and useBuiltins
const arch64 = sizeof(int) == 8
@ -196,7 +197,8 @@ template parityImpl[T](value: T): int =
when useGCC_builtins:
# Returns the number of set 1-bits in value.
proc builtin_popcount(x: cuint): cint {.importc: "__builtin_popcount", cdecl.}
proc builtin_popcountll(x: culonglong): cint {.importc: "__builtin_popcountll", cdecl.}
proc builtin_popcountll(x: culonglong): cint {.
importc: "__builtin_popcountll", cdecl.}
# Returns the bit parity in value
proc builtin_parity(x: cuint): cint {.importc: "__builtin_parity", cdecl.}
@ -216,17 +218,24 @@ when useGCC_builtins:
elif useVCC_builtins:
# Counts the number of one bits (population count) in a 16-, 32-, or 64-byte unsigned integer.
proc builtin_popcnt16(a2: uint16): uint16 {.importc: "__popcnt16" header: "<intrin.h>", noSideEffect.}
proc builtin_popcnt32(a2: uint32): uint32 {.importc: "__popcnt" header: "<intrin.h>", noSideEffect.}
proc builtin_popcnt64(a2: uint64): uint64 {.importc: "__popcnt64" header: "<intrin.h>", noSideEffect.}
proc builtin_popcnt16(a2: uint16): uint16 {.
importc: "__popcnt16"header: "<intrin.h>", noSideEffect.}
proc builtin_popcnt32(a2: uint32): uint32 {.
importc: "__popcnt"header: "<intrin.h>", noSideEffect.}
proc builtin_popcnt64(a2: uint64): uint64 {.
importc: "__popcnt64"header: "<intrin.h>", noSideEffect.}
# Search the mask data from most significant bit (MSB) to least significant bit (LSB) for a set bit (1).
proc bitScanReverse(index: ptr culong, mask: culong): cuchar {.importc: "_BitScanReverse", header: "<intrin.h>", noSideEffect.}
proc bitScanReverse64(index: ptr culong, mask: uint64): cuchar {.importc: "_BitScanReverse64", header: "<intrin.h>", noSideEffect.}
proc bitScanReverse(index: ptr culong, mask: culong): cuchar {.
importc: "_BitScanReverse", header: "<intrin.h>", noSideEffect.}
proc bitScanReverse64(index: ptr culong, mask: uint64): cuchar {.
importc: "_BitScanReverse64", header: "<intrin.h>", noSideEffect.}
# Search the mask data from least significant bit (LSB) to the most significant bit (MSB) for a set bit (1).
proc bitScanForward(index: ptr culong, mask: culong): cuchar {.importc: "_BitScanForward", header: "<intrin.h>", noSideEffect.}
proc bitScanForward64(index: ptr culong, mask: uint64): cuchar {.importc: "_BitScanForward64", header: "<intrin.h>", noSideEffect.}
proc bitScanForward(index: ptr culong, mask: culong): cuchar {.
importc: "_BitScanForward", header: "<intrin.h>", noSideEffect.}
proc bitScanForward64(index: ptr culong, mask: uint64): cuchar {.
importc: "_BitScanForward64", header: "<intrin.h>", noSideEffect.}
template vcc_scan_impl(fnc: untyped; v: untyped): int =
var index: culong
@ -238,16 +247,22 @@ elif useICC_builtins:
# Intel compiler intrinsics: http://fulla.fnal.gov/intel/compiler_c/main_cls/intref_cls/common/intref_allia_misc.htm
# see also: https://software.intel.com/en-us/node/523362
# Count the number of bits set to 1 in an integer a, and return that count in dst.
proc builtin_popcnt32(a: cint): cint {.importc: "_popcnt" header: "<immintrin.h>", noSideEffect.}
proc builtin_popcnt64(a: uint64): cint {.importc: "_popcnt64" header: "<immintrin.h>", noSideEffect.}
proc builtin_popcnt32(a: cint): cint {.
importc: "_popcnt"header: "<immintrin.h>", noSideEffect.}
proc builtin_popcnt64(a: uint64): cint {.
importc: "_popcnt64"header: "<immintrin.h>", noSideEffect.}
# Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.
proc bitScanForward(p: ptr uint32, b: uint32): cuchar {.importc: "_BitScanForward", header: "<immintrin.h>", noSideEffect.}
proc bitScanForward64(p: ptr uint32, b: uint64): cuchar {.importc: "_BitScanForward64", header: "<immintrin.h>", noSideEffect.}
proc bitScanForward(p: ptr uint32, b: uint32): cuchar {.
importc: "_BitScanForward", header: "<immintrin.h>", noSideEffect.}
proc bitScanForward64(p: ptr uint32, b: uint64): cuchar {.
importc: "_BitScanForward64", header: "<immintrin.h>", noSideEffect.}
# Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.
proc bitScanReverse(p: ptr uint32, b: uint32): cuchar {.importc: "_BitScanReverse", header: "<immintrin.h>", noSideEffect.}
proc bitScanReverse64(p: ptr uint32, b: uint64): cuchar {.importc: "_BitScanReverse64", header: "<immintrin.h>", noSideEffect.}
proc bitScanReverse(p: ptr uint32, b: uint32): cuchar {.
importc: "_BitScanReverse", header: "<immintrin.h>", noSideEffect.}
proc bitScanReverse64(p: ptr uint32, b: uint64): cuchar {.
importc: "_BitScanReverse64", header: "<immintrin.h>", noSideEffect.}
template icc_scan_impl(fnc: untyped; v: untyped): int =
var index: uint32

View file

@ -133,8 +133,9 @@ const
LacksDevPackages* = {Distribution.Gentoo, Distribution.Slackware,
Distribution.ArchLinux}
var unameRes, releaseRes, hostnamectlRes: string ## we cache the result of the 'uname -a'
## execution for faster platform detections.
# we cache the result of the 'uname -a'
# execution for faster platform detections.
var unameRes, releaseRes, hostnamectlRes: string
template unameRelease(cmd, cache): untyped =
if cache.len == 0:
@ -173,7 +174,8 @@ proc detectOsImpl(d: Distribution): bool =
result = defined(haiku)
else:
let dd = toLowerAscii($d)
result = dd in toLowerAscii(uname()) or dd in toLowerAscii(release()) or ("operating system: " & dd) in toLowerAscii(hostnamectl())
result = dd in toLowerAscii(uname()) or dd in toLowerAscii(release()) or
("operating system: " & dd) in toLowerAscii(hostnamectl())
template detectOs*(d: untyped): bool =
## Distro/OS detection. For convenience the

View file

@ -163,7 +163,8 @@ proc open*(filename: string, mode: FileMode = fmRead,
if readonly: shareMode else: shareMode or FILE_SHARE_WRITE,
nil,
if newFileSize != -1: CREATE_ALWAYS else: OPEN_EXISTING,
if readonly: FILE_ATTRIBUTE_READONLY or flags else: FILE_ATTRIBUTE_NORMAL or flags,
if readonly: FILE_ATTRIBUTE_READONLY or flags
else: FILE_ATTRIBUTE_NORMAL or flags,
0)
when useWinUnicode:
@ -312,8 +313,8 @@ when defined(posix) or defined(nimdoc):
raiseOSError(osLastError())
when defined(linux): #Maybe NetBSD, too?
#On Linux this can be over 100 times faster than a munmap,mmap cycle.
proc mremap(old: pointer; oldSize,newSize: csize; flags: cint): pointer {.
importc: "mremap", header: "<sys/mman.h>" .}
proc mremap(old: pointer; oldSize, newSize: csize; flags: cint):
pointer {.importc: "mremap", header: "<sys/mman.h>".}
let newAddr = mremap(f.mem, csize(f.size), csize(newFileSize), cint(1))
if newAddr == cast[pointer](MAP_FAILED):
raiseOSError(osLastError())
@ -427,7 +428,8 @@ iterator memSlices*(mfile: MemFile, delim='\l', eat='\r'): MemSlice {.inline.} =
ms.data = cast[pointer](cast[int](ending) +% 1) # skip delim
remaining = mfile.size - (ms.data -! mfile.mem)
iterator lines*(mfile: MemFile, buf: var TaintedString, delim='\l', eat='\r'): TaintedString {.inline.} =
iterator lines*(mfile: MemFile, buf: var TaintedString, delim = '\l',
eat = '\r'): TaintedString {.inline.} =
## Replace contents of passed buffer with each new line, like
## `readLine(File) <system.html#readLine,File,TaintedString>`_.
## `delim`, `eat`, and delimiting logic is exactly as for
@ -507,8 +509,8 @@ proc mmsWriteData(s: Stream, buffer: pointer, bufLen: int) =
moveMem(cast[pointer](p), buffer, bufLen)
inc(MemMapFileStream(s).pos, bufLen)
proc newMemMapFileStream*(filename: string, mode: FileMode = fmRead, fileSize: int = -1):
MemMapFileStream =
proc newMemMapFileStream*(filename: string, mode: FileMode = fmRead,
fileSize: int = -1): MemMapFileStream =
## creates a new stream from the file named `filename` with the mode `mode`.
## Raises ## `OSError` if the file cannot be opened. See the `system
## <system.html>`_ module for a list of available FileMode enums.

View file

@ -71,16 +71,11 @@ const poDemon* {.deprecated.} = poDaemon ## Nim versions before 0.20
## and this is needed just for backward compatibility.
proc execProcess*(command: string,
workingDir: string = "",
args: openArray[string] = [],
env: StringTableRef = nil,
options: set[ProcessOption] = {poStdErrToStdOut,
poUsePath,
poEvalCommand}): TaintedString {.
rtl, extern: "nosp$1",
tags: [ExecIOEffect, ReadIOEffect,
RootEffect].}
proc execProcess*(command: string, workingDir: string = "",
args: openArray[string] = [], env: StringTableRef = nil,
options: set[ProcessOption] = {poStdErrToStdOut, poUsePath, poEvalCommand}):
TaintedString {.rtl, extern: "nosp$1",
tags: [ExecIOEffect, ReadIOEffect, RootEffect].}
## A convenience procedure that executes ``command`` with ``startProcess``
## and returns its output as a string.
##
@ -102,8 +97,8 @@ proc execProcess*(command: string,
## # Note: outp may have an interleave of text from the nim compile
## # and any output from mytestfile when it runs
proc execCmd*(command: string): int {.rtl, extern: "nosp$1", tags: [ExecIOEffect,
ReadIOEffect, RootEffect].}
proc execCmd*(command: string): int {.rtl, extern: "nosp$1",
tags: [ExecIOEffect, ReadIOEffect, RootEffect].}
## Executes ``command`` and returns its error code.
##
## Standard input, output, error streams are inherited from the calling process.
@ -121,13 +116,11 @@ proc execCmd*(command: string): int {.rtl, extern: "nosp$1", tags: [ExecIOEffect
## .. code-block:: Nim
## let errC = execCmd("nim c -r mytestfile.nim")
proc startProcess*(command: string,
workingDir: string = "",
args: openArray[string] = [],
env: StringTableRef = nil,
proc startProcess*(command: string, workingDir: string = "",
args: openArray[string] = [], env: StringTableRef = nil,
options: set[ProcessOption] = {poStdErrToStdOut}):
owned(Process) {.rtl, extern: "nosp$1", tags: [ExecIOEffect, ReadEnvEffect,
RootEffect].}
owned(Process) {.rtl, extern: "nosp$1",
tags: [ExecIOEffect, ReadEnvEffect, RootEffect].}
## Starts a process. `Command` is the executable file, `workingDir` is the
## process's working directory. If ``workingDir == ""`` the current directory
## is used (default). `args` are the command line arguments that are passed to the
@ -303,11 +296,10 @@ proc countProcessors*(): int {.rtl, extern: "nosp$1".} =
result = cpuinfo.countProcessors()
proc execProcesses*(cmds: openArray[string],
options = {poStdErrToStdOut, poParentStreams},
n = countProcessors(),
options = {poStdErrToStdOut, poParentStreams}, n = countProcessors(),
beforeRunEvent: proc(idx: int) = nil,
afterRunEvent: proc(idx: int, p: Process) = nil): int
{.rtl, extern: "nosp$1",
afterRunEvent: proc(idx: int, p: Process) = nil):
int {.rtl, extern: "nosp$1",
tags: [ExecIOEffect, TimeEffect, ReadEnvEffect, RootEffect].} =
## Executes the commands `cmds` in parallel.
## Creates `n` processes that execute in parallel.
@ -419,14 +411,14 @@ proc execProcesses*(cmds: openArray[string],
close(p)
when not defined(useNimRtl):
proc execProcess(command: string,
workingDir: string = "",
args: openArray[string] = [],
env: StringTableRef = nil,
options: set[ProcessOption] = {poStdErrToStdOut,
poUsePath,
poEvalCommand}): TaintedString =
var p = startProcess(command, workingDir=workingDir, args=args, env=env, options=options)
proc execProcess(command: string, workingDir: string = "",
args: openArray[string] = [], env: StringTableRef = nil,
options: set[ProcessOption] = {poStdErrToStdOut, poUsePath,
poEvalCommand}):
TaintedString =
var p = startProcess(command, workingDir = workingDir, args = args,
env = env, options = options)
var outp = outputStream(p)
result = TaintedString""
var line = newStringOfCap(120).TaintedString
@ -503,14 +495,12 @@ when defined(Windows) and not defined(useNimRtl):
# O_RDONLY {.importc: "_O_RDONLY", header: "<fcntl.h>".}: int
proc myDup(h: Handle; inherit: WINBOOL = 1): Handle =
let thisProc = getCurrentProcess()
if duplicateHandle(thisProc, h,
thisProc, addr result,0,inherit,
if duplicateHandle(thisProc, h, thisProc, addr result, 0, inherit,
DUPLICATE_SAME_ACCESS) == 0:
raiseOSError(osLastError())
proc createAllPipeHandles(si: var STARTUPINFO;
stdin, stdout, stderr: var Handle;
hash: int) =
stdin, stdout, stderr: var Handle; hash: int) =
var sa: SECURITY_ATTRIBUTES
sa.nLength = sizeof(SECURITY_ATTRIBUTES).cint
sa.lpSecurityDescriptor = nil
@ -569,11 +559,10 @@ when defined(Windows) and not defined(useNimRtl):
proc fileClose(h: Handle) {.inline.} =
if h > 4: discard closeHandle(h)
proc startProcess(command: string,
workingDir: string = "",
args: openArray[string] = [],
env: StringTableRef = nil,
options: set[ProcessOption] = {poStdErrToStdOut}): owned Process =
proc startProcess(command: string, workingDir: string = "",
args: openArray[string] = [], env: StringTableRef = nil,
options: set[ProcessOption] = {poStdErrToStdOut}):
owned Process =
var
si: STARTUPINFO
procInfo: PROCESS_INFORMATION
@ -842,11 +831,10 @@ elif not defined(useNimRtl):
tags: [ExecIOEffect, ReadEnvEffect, ReadDirEffect, RootEffect], cdecl, gcsafe.}
{.pop.}
proc startProcess(command: string,
workingDir: string = "",
args: openArray[string] = [],
env: StringTableRef = nil,
options: set[ProcessOption] = {poStdErrToStdOut}): owned Process =
proc startProcess(command: string, workingDir: string = "",
args: openArray[string] = [], env: StringTableRef = nil,
options: set[ProcessOption] = {poStdErrToStdOut}):
owned Process =
var
pStdin, pStdout, pStderr: array[0..1, cint]
new(result)
@ -1066,7 +1054,7 @@ elif not defined(useNimRtl):
discard execve(data.sysCommand, data.sysArgs, data.sysEnv)
startProcessFail(data)
{.pop}
{.pop.}
proc close(p: Process) =
if poParentStreams notin p.options:

View file

@ -18,14 +18,15 @@ type
pcLinkToDir ## path refers to a symbolic link to a directory
proc staticWalkDir(dir: string; relative: bool): seq[
tuple[kind: PathComponent, path: string]] =
tuple[kind: PathComponent; path: string]] =
discard
iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path: string] =
iterator walkDir*(dir: string; relative = false): tuple[kind: PathComponent;
path: string] =
for k, v in items(staticWalkDir(dir, relative)):
yield (k, v)
iterator walkDirRec*(dir: string, filter={pcFile, pcDir}): string =
iterator walkDirRec*(dir: string; filter = {pcFile, pcDir}): string =
var stack = @[dir]
while stack.len > 0:
for k, p in walkDir(stack.pop()):

View file

@ -56,7 +56,8 @@ proc isDotDot(x: string; bounds: (int, int)): bool =
proc isSlash(x: string; bounds: (int, int)): bool =
bounds[1] == bounds[0] and x[bounds[0]] in {DirSep, AltSep}
proc addNormalizePath*(x: string; result: var string; state: var int; dirSep = DirSep) =
proc addNormalizePath*(x: string; result: var string; state: var int;
dirSep = DirSep) =
## Low level proc. Undocumented.
# state: 0th bit set if isAbsolute path. Other bits count

View file

@ -41,7 +41,7 @@ when defined(windows):
VectoredHandler = proc (p: PEXCEPTION_POINTERS): LONG {.stdcall.}
proc addVectoredExceptionHandler(firstHandler: ULONG,
handler: VectoredHandler): pointer {.
importc: "AddVectoredExceptionHandler", stdcall, dynlib: "kernel32.dll"}
importc: "AddVectoredExceptionHandler", stdcall, dynlib: "kernel32.dll".}
{.push stackTrace: off.}
proc segfaultHandler(p: PEXCEPTION_POINTERS): LONG {.stdcall.} =