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

@ -16,9 +16,9 @@ type
proc c_str(a: stdString): cstring {.importcpp: "(char *)(#.c_str())", header: "<string>".}
proc len(a: stdString): csize {.importcpp: "(#.length())", header: "<string>".}
proc len(a: stdString): csize_t {.importcpp: "(#.length())", header: "<string>".}
proc setChar(a: var stdString, i: csize, c: char) {.importcpp: "(#[#] = #)", header: "<string>".}
proc setChar(a: var stdString, i: csize_t, c: char) {.importcpp: "(#[#] = #)", header: "<string>".}
proc `*`*[T](this: stdUniquePtr[T]): var T {.noSideEffect, importcpp: "(* #)", header: "<memory>".}

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)

View file

@ -74,37 +74,37 @@ block tn8vsint16:
import strutils
block tcolors:
type TColor = distinct int32
type TColor = distinct uint32
proc rgb(r, g, b: range[0..255]): TColor =
result = TColor(r or g shl 8 or b shl 16)
proc `$`(c: TColor): string =
result = "#" & toHex(int32(c), 6)
result = "#" & toHex(uint32(c), 6)
echo rgb(34, 55, 255)
when false:
block:
type
TColor = distinct int32
TColorComponent = distinct int8
TColor = distinct uint32
TColorComponent = distinct uint8
proc red(a: TColor): TColorComponent =
result = TColorComponent(int32(a) and 0xff'i32)
result = TColorComponent(uint32(a) and 0xff'u32)
proc green(a: TColor): TColorComponent =
result = TColorComponent(int32(a) shr 8'i32 and 0xff'i32)
result = TColorComponent(uint32(a) shr 8'u32 and 0xff'u32)
proc blue(a: TColor): TColorComponent =
result = TColorComponent(int32(a) shr 16'i32 and 0xff'i32)
result = TColorComponent(uint32(a) shr 16'u32 and 0xff'u32)
proc rgb(r, g, b: range[0..255]): TColor =
result = TColor(r or g shl 8 or b shl 8)
proc `+!` (a, b: TColorComponent): TColorComponent =
## saturated arithmetic:
result = TColorComponent(min(ze(int8(a)) + ze(int8(b)), 255))
result = TColorComponent(min(int(uint8(a)) + int(uint8(b)), 255))
proc `+` (a, b: TColor): TColor =
## saturated arithmetic for colors makes sense, I think:
return rgb(red(a) +! red(b), green(a) +! green(b), blue(a) +! blue(b))
return rgb(int(red(a) +! red(b)), int(green(a) +! green(b)), int(blue(a) +! blue(b)))
rgb(34, 55, 255)
discard rgb(34, 55, 255)
block:
type

View file

@ -1,4 +1,5 @@
discard """
disabled: true
output: '''
just exiting...
'''

View file

@ -6,7 +6,7 @@ type
TRadixNode {.inheritable.} = object
kind: TRadixNodeKind
TRadixNodeLinear = object of TRadixNode
len: int8
len: uint8
keys: array[0..31, char]
vals: array[0..31, PRadixNode]
TRadixNodeFull = object of TRadixNode
@ -24,7 +24,7 @@ proc search(r: PRadixNode, s: string): PRadixNode =
case r.kind
of rnLinear:
var x = PRadixNodeLinear(r)
for j in 0..ze(x.len)-1:
for j in 0..int(x.len)-1:
if x.keys[j] == s[i]:
if s[i] == '\0': return r
r = x.vals[j]
@ -63,9 +63,9 @@ proc excl*(r: var PRadixNode, s: string) =
of rnFull: PRadixNodeFull(x).b['\0'] = nil
of rnLinear:
var x = PRadixNodeLinear(x)
for i in 0..ze(x.len)-1:
for i in 0..int(x.len)-1:
if x.keys[i] == '\0':
swap(x.keys[i], x.keys[ze(x.len)-1])
swap(x.keys[i], x.keys[int(x.len)-1])
dec(x.len)
break

View file

@ -5,10 +5,10 @@ proc get_values(): (seq[int8], seq[int16], seq[int32]) =
let i8 = -3'i8
let i16 = -3'i16
let i32 = -3'i32
doAssert i8.ze == 0xFD
doAssert i8.ze64 == 0xFD
doAssert i16.ze == 0xFFFD
doAssert i16.ze64 == 0xFFFD
doAssert int(cast[uint8](i8)) == 0xFD
doAssert int64(cast[uint8](i8)) == 0xFD
doAssert int(cast[uint16](i16)) == 0xFFFD
doAssert int64(cast[uint16](i16)) == 0xFFFD
result[0] = @[]; result[1] = @[]; result[2] = @[]