moderate system cleanup & refactor (#20355)

* system refactor, move out 600 lines

* compilation, slice, backwardsindex, misc_num moved out of system
* some procs/types moved into arithmetics, basic_types
* system no longer depends on syncio
* some procs moved around to fit with their surroundings

* make exceptions an import, old ops to misc_num

* move instantiationInfo back

* move back nim version, fix windows echo

* include compilation

* better docs for imported modules, fix unsigned ops

also remove ze, ze64, toU8, toU16, toU32 with nimPreviewSlimSystem

* fix terminal

* workaround IC test & weird csize bug, changelog

* move NimMajor etc back to compilation, rebase for CI

* try ic fix

* form single `indices`, slim out TaintedString, try fix IC

* fix CI, update changelog, addQuitProc

* fix CI

* try fix CI

* actually fix CI finally hopefully

* Update lib/system/compilation.nim

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>

* update kochdocs

* hopefully fix csize uses for slimsystem

* fix tquit

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
This commit is contained in:
metagn 2022-09-28 22:28:45 +03:00 • committed by GitHub
commit 919a889ba8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 1167 additions and 1102 deletions

View file

@ -40,8 +40,8 @@ type
TRadixNode {.pure, inheritable.} = object
kind: TRadixNodeKind
TRadixNodeLinear = object of TRadixNode
len: int8
keys: array[0..31, int8]
len: uint8
keys: array[0..31, uint8]
vals: array[0..31, PRadixNode]
TRadixNodeFull = object of TRadixNode
@ -49,8 +49,8 @@ type
TRadixNodeLeafBits = object of TRadixNode
b: array[0..7, int]
TRadixNodeLeafLinear = object of TRadixNode
len: int8
keys: array[0..31, int8]
len: uint8
keys: array[0..31, uint8]
var
root: PRadixNode
@ -59,8 +59,8 @@ proc searchInner(r: PRadixNode, a: int): PRadixNode =
case r.kind
of rnLinear:
var x = cast[ptr TRadixNodeLinear](r)
for i in 0..ze(x.len)-1:
if ze(x.keys[i]) == a: return x.vals[i]
for i in 0..int(x.len)-1:
if int(x.keys[i]) == a: return x.vals[i]
of rnFull:
var x = cast[ptr TRadixNodeFull](r)
return x.b[a]
@ -87,8 +87,8 @@ proc searchLeaf(r: PRadixNode, a: int): bool =
return testBit(x.b[a /% BitsPerUnit], a)
of rnLeafLinear:
var x = cast[ptr TRadixNodeLeafLinear](r)
for i in 0..ze(x.len)-1:
if ze(x.keys[i]) == a: return true
for i in 0..int(x.len)-1:
if int(x.keys[i]) == a: return true
else: assert(false)
proc exclLeaf(r: PRadixNode, a: int) =
@ -98,9 +98,9 @@ proc exclLeaf(r: PRadixNode, a: int) =
resetBit(x.b[a /% BitsPerUnit], a)
of rnLeafLinear:
var x = cast[ptr TRadixNodeLeafLinear](r)
var L = ze(x.len)
var L = int(x.len)
for i in 0..L-1:
if ze(x.keys[i]) == a:
if int(x.keys[i]) == a:
x.keys[i] = x.keys[L-1]
dec(x.len)
return
@ -131,8 +131,8 @@ proc addLeaf(r: var PRadixNode, a: int): bool =
# a linear node:
var x = cast[ptr TRadixNodeLinear](alloc0(sizeof(TRadixNodeLinear)))
x.kind = rnLeafLinear
x.len = 1'i8
x.keys[0] = toU8(a)
x.len = 1'u8
x.keys[0] = uint8(a)
r = x
return false # not already in set
case r.kind
@ -141,18 +141,18 @@ proc addLeaf(r: var PRadixNode, a: int): bool =
return testOrSetBit(x.b[a /% BitsPerUnit], a)
of rnLeafLinear:
var x = cast[ptr TRadixNodeLeafLinear](r)
var L = ze(x.len)
var L = int(x.len)
for i in 0..L-1:
if ze(x.keys[i]) == a: return true
if int(x.keys[i]) == a: return true
if L <= high(x.keys):
x.keys[L] = toU8(a)
x.keys[L] = uint8(a)
inc(x.len)
else:
# transform into a full node:
var y = cast[ptr TRadixNodeLeafBits](alloc0(sizeof(TRadixNodeLeafBits)))
y.kind = rnLeafBits
for i in 0..ze(x.len)-1:
var u = ze(x.keys[i])
for i in 0..int(x.len)-1:
var u = int(x.keys[i])
setBit(y.b[u /% BitsPerUnit], u)
setBit(y.b[a /% BitsPerUnit], a)
dealloc(r)
@ -167,26 +167,26 @@ proc addInner(r: var PRadixNode, a: int, d: int): bool =
# a linear node:
var x = cast[ptr TRadixNodeLinear](alloc0(sizeof(TRadixNodeLinear)))
x.kind = rnLinear
x.len = 1'i8
x.keys[0] = toU8(k)
x.len = 1'u8
x.keys[0] = uint8(k)
r = x
return addInner(x.vals[0], a, d-8)
case r.kind
of rnLinear:
var x = cast[ptr TRadixNodeLinear](r)
var L = ze(x.len)
var L = int(x.len)
for i in 0..L-1:
if ze(x.keys[i]) == k: # already exists
if int(x.keys[i]) == k: # already exists
return addInner(x.vals[i], a, d-8)
if L <= high(x.keys):
x.keys[L] = toU8(k)
x.keys[L] = uint8(k)
inc(x.len)
return addInner(x.vals[L], a, d-8)
else:
# transform into a full node:
var y = cast[ptr TRadixNodeFull](alloc0(sizeof(TRadixNodeFull)))
y.kind = rnFull
for i in 0..L-1: y.b[ze(x.keys[i])] = x.vals[i]
for i in 0..L-1: y.b[int(x.keys[i])] = x.vals[i]
dealloc(r)
r = y
return addInner(y.b[k], a, d-8)
@ -211,8 +211,8 @@ iterator innerElements(r: PRadixNode): tuple[prefix: int, n: PRadixNode] =
yield (i, r.b[i])
of rnLinear:
var r = cast[ptr TRadixNodeLinear](r)
for i in 0..ze(r.len)-1:
yield (ze(r.keys[i]), r.vals[i])
for i in 0..int(r.len)-1:
yield (int(r.keys[i]), r.vals[i])
else: assert(false)
iterator leafElements(r: PRadixNode): int =
@ -228,8 +228,8 @@ iterator leafElements(r: PRadixNode): int =
yield i*BitsPerUnit+j
of rnLeafLinear:
var r = cast[ptr TRadixNodeLeafLinear](r)
for i in 0..ze(r.len)-1:
yield ze(r.keys[i])
for i in 0..int(r.len)-1:
yield int(r.keys[i])
else: assert(false)
iterator elements*(r: PRadixNode): ByteAddress {.inline.} =

View file

@ -56,9 +56,13 @@ discard $x0
const x1 = cast[uint](-1)
discard $(x1,)
# bug #13698
let n: csize = 1 # xxx should that be csize_t or is that essential here?
doAssert $n.int32 == "1"
when not defined(nimPreviewSlimSystem):
# bug #13698
let n: csize = 1 # xxx should that be csize_t or is that essential here?
doAssert $n.int32 == "1"
let n2: csize_t = 1
doAssert $n2.int32 == "1"
# bug #14616

View file

@ -7,7 +7,7 @@ import
strutils
type
TBuffer = array[0..10, int8]
TBuffer = array[0..10, uint8]
proc toVarNum(x: int32, b: var TBuffer) =
# encoding: first bit indicates end of number (0 if at end)
@ -21,11 +21,11 @@ proc toVarNum(x: int32, b: var TBuffer) =
# anyway
a = abs(x)
# first 6 bits:
b[0] = toU8(ord(a >% 63'i32) shl 7 or (ord(x < 0'i32) shl 6) or (int(a) and 63))
b[0] = uint8(ord(a >% 63'i32) shl 7 or (ord(x < 0'i32) shl 6) or (int(a) and 63))
a = (a shr 6'i32) and 0x03ffffff # skip first 6 bits
var i = 1
while a != 0'i32:
b[i] = toU8(ord(a >% 127'i32) shl 7 or (int(a) and 127))
b[i] = uint8(ord(a >% 127'i32) shl 7 or (int(a) and 127))
inc(i)
a = a shr 7'i32
@ -41,40 +41,40 @@ proc toVarNum64(x: int64, b: var TBuffer) =
# anyway
a = abs(x)
# first 6 bits:
b[0] = toU8(ord(a >% 63'i64) shl 7 or (ord(x < 0'i64) shl 6) or int(a and 63))
b[0] = uint8(ord(a >% 63'i64) shl 7 or (ord(x < 0'i64) shl 6) or int(a and 63))
a = (a shr 6) and 0x03ffffffffffffff # skip first 6 bits
var i = 1
while a != 0'i64:
b[i] = toU8(ord(a >% 127'i64) shl 7 or int(a and 127))
b[i] = uint8(ord(a >% 127'i64) shl 7 or int(a and 127))
inc(i)
a = a shr 7
proc toNum64(b: TBuffer): int64 =
# treat first byte different:
result = ze64(b[0]) and 63
result = int64(b[0]) and 63
var
i = 0
Shift = 6'i64
while (ze(b[i]) and 128) != 0:
while (int(b[i]) and 128) != 0:
inc(i)
result = result or ((ze64(b[i]) and 127) shl Shift)
result = result or ((int64(b[i]) and 127) shl Shift)
inc(Shift, 7)
if (ze(b[0]) and 64) != 0: # sign bit set?
if (int(b[0]) and 64) != 0: # sign bit set?
result = not result +% 1
# this is the same as ``- result``
# but gives no overflow error for low(int)
proc toNum(b: TBuffer): int32 =
# treat first byte different:
result = int32 ze(b[0]) and 63
result = int32(b[0]) and 63
var
i = 0
Shift = 6'i32
while (ze(b[i]) and 128) != 0:
while (int(b[i]) and 128) != 0:
inc(i)
result = result or ((int32(ze(b[i])) and 127'i32) shl Shift)
result = result or ((int32(b[i]) and 127'i32) shl Shift)
Shift = Shift + 7'i32
if (ze(b[0]) and (1 shl 6)) != 0: # sign bit set?
if (int(b[0]) and (1 shl 6)) != 0: # sign bit set?
result = (not result) +% 1'i32
# this is the same as ``- result``
# but gives no overflow error for low(int)