mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-03 19:52:36 +00:00
* squashed work by Zahary
* squashing a ton of useful history... otherwise rebasing on top of upstream Nim after commit 82c009a2cb would be impossible.
* Code review changes; Working test suite (without code reloading enabled)
* - documentation
- implemented the HCR test - almost works...
- fix the issue on Unix where for executable targets the source file for the main module of a project in nimcache was being overwritten with the binary itself (and thus the actual source code was lost)
- fixing embedded paths to shared objects on unix (the "lib" prefix was being prepended to the entire path instead of just the filename)
- other fixes
- removing unnecessary includes since that file is already included in chcks.nim which is in turn included in system.nim (and previously was getting imported in chcks.nim but then system.nim improts something... and that breaks HCR (perhaps it could be fixed but it would be nice not to import anything in system))
* fix for clang & C++ - explicitly casting a function pointer to void*
more stable mangling of parameter names when HCR is on
the length of the static arrays in the DatInit functions is now part of the name of the variables, so when they get resized they get also recreated
more stable mangling for inline functions - no longer depends on the module which first used them
work on the new complicated HCR test - turned surprisingly complex - WIP
test now successfully passes even when re-running `koch test` (previously when the nimcache wasn't cold that lead to errors)
better documentation
calling setStackBottomWith for PreMain
passes over the HcrInit/DatInit/Init calls of all modules are now in the proper order (first all of one type, then all of the next). Also typeinfo globals are registered (created) in a single pass before the DatInit pass (because of the way generic instantiations are handled)
Fix the test suite execution on macOs
fix for being able to query the program arguments when using HCR on posix!
other fixes
* Bugfix: Fix a compilation error in C++ mode when a function pointer
is converted to a raw pointer
* basic documentation for the new hot code reloading semantics
* Add change log entry
* Don't re-execute the top-level statements while reloading JS code
* fix a number of tests broken in a recent bugfix
* Review changes
* Added {.executeOnReload.} pragma that indicates top-level statements
that should be executed on each reload. To make this work, I've modified
the way the `if (hcr_init_) {...}` guards are produced in the init code.
This still needs more work as the new guards seem to be inserted within
the previously generated guards.
This change also removes the need for `lastRegistedGlobal` in nimhcr.
* Implemented the `signatureHash` magic and the `hasModuleChanged` API
depending on it (the actual logic is not imlemented yet).
* Add the "hcr" prefix to all HCR-related symbols in the system module.
Added a new `hotcodereloading` module exporting the high-level API to
the user.
Besides being more hygienic, this was also required in order to make
it possible to use macros in the high-level API. Without the split,
`system` would have to import `macros`, which was going to produce
the well-known init problems.
* Attempted to solve the "GC markers problem".
Crashes were expected with the previous code, because the GC markers
were compiled as normal procs are registered in the GC. When their
module is unloaded, dangling pointers will remain in the GC tables.
To solve this issue, I don't register any GC markers when HCR is on,
but I add them to the HCR globals metadata and I use a single marker
registed in nimhcr during the initialization of the system module that
will be responsible for marking all globals.
* fix a compilation error
* - implemented the hasModuleChanged functionality
- tuples can be returned and broken into different vars in global scope
- added comments for the closnig scopes of the if statements in the init proc
- the new executeOnReload pragma works now!
- other fixes
* finally! fixing this hack in a proper way - declaring the destructor out of line (out of the class body) - we no longer need to forward-declare popCurrentExceptionEx
* Force full module parsing
This is a temporary hack that breaks some tests. I'll investigate
later how these can be fixed.
* tuples are now properly handled when global!
* these comments mess up the codegen in debug mode when $n is not actually a new line (or something like that) - these labels are intended only for GOTO labels anyway...
* "solved" the issue with the .pdb locks on windows when a binary is being debugged and hot code reloading is used at the same time
* fixes after rebasing...
* small fixes for the test
* better handling of globals! no more compiler crashes for locals with the global pragma, also simplified code around loops in global scope which have local vars (actually globals)
* we can now use the global pragma even for ... globals!
* the right output
* lets try those boehm GC tests
* after the test is ran it will be at its starting state - no git modifications
* clarification in the docs
* removed unnecessary line directives for forward declarations of functions - they were causing trouble with hot code reloading when no semantic change propagates to the main module but a line directive got changed and thus the main module had to be recompiled since the .c code had changed
* fixed bug! was inserting duplicate keys into the table and later was removing only 1 copy of all the duplicates (after a few reloads)
* no longer breaking into DatInit code when not supposed to
* fixes after rebasing
* yet more fixes after rebasing
* Update jssys.nim
* Rework the HCR path-handling logic
After reviewing the code more carefully, I've noticed that the old logic
will be broken when the user overrides the '--out:f' compiler option.
Besides fixing this issues, I took the opportunity to implement the
missing '--outdir:d' option.
Other changes:
* ./koch test won't overwrite any HCR and RTL builds located in nim/lib
* HCR and RTL are compiled with --threads:on by default
* Clean up the globals registration logic
* Handle non-flattened top-level stmtlists in JS as well
* The HCR is not supported with the Boehm GC yet
Also fixes some typos and the expected output of the HCR integration test
* The GC marker procs are now properly used as trampolines
* Fix the HCR integration test in release builds
* Fix ./koch tools
* this forward declaration doesn't seem to be necessary, and in fact breaks HCR because a 2nd function pointer is emitted for this externed/rtl func
* the forward declaration I removed in the last commit was actually necessary
* Attempt to make all tests green
* Fix tgenscript
* BAT file for running the HCR integration test on Windows [skip ci]
* Fix the docgen tests
* A final fix for Travis (hopefully)
385 lines
12 KiB
Nim
385 lines
12 KiB
Nim
#
|
|
#
|
|
# The Nim Compiler
|
|
# (c) Copyright 2017 Andreas Rumpf
|
|
#
|
|
# See the file "copying.txt", included in this
|
|
# distribution, for details about the copyright.
|
|
#
|
|
|
|
## Computes hash values for routine (proc, method etc) signatures.
|
|
|
|
import ast, md5, tables, ropes
|
|
from hashes import Hash
|
|
from astalgo import debug
|
|
import types
|
|
from strutils import startsWith, contains
|
|
|
|
when false:
|
|
type
|
|
SigHash* = uint32 ## a hash good enough for a filename or a proc signature
|
|
|
|
proc sdbmHash(hash: SigHash, c: char): SigHash {.inline.} =
|
|
return SigHash(c) + (hash shl 6) + (hash shl 16) - hash
|
|
|
|
template `&=`*(x: var SigHash, c: char) = x = sdbmHash(x, c)
|
|
template `&=`*(x: var SigHash, s: string) =
|
|
for c in s: x = sdbmHash(x, c)
|
|
|
|
else:
|
|
type
|
|
SigHash* = distinct Md5Digest
|
|
|
|
const
|
|
cb64 = [
|
|
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
|
|
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
|
|
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
|
|
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
|
|
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9a",
|
|
"9b", "9c"]
|
|
|
|
proc toBase64a(s: cstring, len: int): string =
|
|
## encodes `s` into base64 representation.
|
|
result = newStringOfCap(((len + 2) div 3) * 4)
|
|
result.add '_'
|
|
var i = 0
|
|
while i < len - 2:
|
|
let a = ord(s[i])
|
|
let b = ord(s[i+1])
|
|
let c = ord(s[i+2])
|
|
result.add cb64[a shr 2]
|
|
result.add cb64[((a and 3) shl 4) or ((b and 0xF0) shr 4)]
|
|
result.add cb64[((b and 0x0F) shl 2) or ((c and 0xC0) shr 6)]
|
|
result.add cb64[c and 0x3F]
|
|
inc(i, 3)
|
|
if i < len-1:
|
|
let a = ord(s[i])
|
|
let b = ord(s[i+1])
|
|
result.add cb64[a shr 2]
|
|
result.add cb64[((a and 3) shl 4) or ((b and 0xF0) shr 4)]
|
|
result.add cb64[((b and 0x0F) shl 2)]
|
|
elif i < len:
|
|
let a = ord(s[i])
|
|
result.add cb64[a shr 2]
|
|
result.add cb64[(a and 3) shl 4]
|
|
|
|
proc `$`*(u: SigHash): string =
|
|
toBase64a(cast[cstring](unsafeAddr u), sizeof(u))
|
|
proc `&=`(c: var MD5Context, s: string) = md5Update(c, s, s.len)
|
|
proc `&=`(c: var MD5Context, ch: char) = md5Update(c, unsafeAddr ch, 1)
|
|
proc `&=`(c: var MD5Context, r: Rope) =
|
|
for l in leaves(r): md5Update(c, l, l.len)
|
|
proc `&=`(c: var MD5Context, i: BiggestInt) =
|
|
md5Update(c, cast[cstring](unsafeAddr i), sizeof(i))
|
|
|
|
template lowlevel(v) =
|
|
md5Update(c, cast[cstring](unsafeAddr(v)), sizeof(v))
|
|
|
|
proc `==`*(a, b: SigHash): bool =
|
|
# {.borrow.}
|
|
result = equalMem(unsafeAddr a, unsafeAddr b, sizeof(a))
|
|
|
|
proc hash*(u: SigHash): Hash =
|
|
result = 0
|
|
for x in 0..3:
|
|
result = (result shl 8) or u.MD5Digest[x].int
|
|
type
|
|
ConsiderFlag* = enum
|
|
CoProc
|
|
CoType
|
|
CoOwnerSig
|
|
CoIgnoreRange
|
|
|
|
proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag])
|
|
|
|
proc hashSym(c: var MD5Context, s: PSym) =
|
|
if sfAnon in s.flags or s.kind == skGenericParam:
|
|
c &= ":anon"
|
|
else:
|
|
var it = s
|
|
while it != nil:
|
|
c &= it.name.s
|
|
c &= "."
|
|
it = it.owner
|
|
|
|
proc hashTypeSym(c: var MD5Context, s: PSym) =
|
|
if sfAnon in s.flags or s.kind == skGenericParam:
|
|
c &= ":anon"
|
|
else:
|
|
var it = s
|
|
while it != nil:
|
|
if sfFromGeneric in it.flags and it.kind in routineKinds and
|
|
it.typ != nil:
|
|
hashType c, it.typ, {CoProc}
|
|
c &= it.name.s
|
|
c &= "."
|
|
it = it.owner
|
|
|
|
proc hashTree(c: var MD5Context, n: PNode) =
|
|
if n == nil:
|
|
c &= "\255"
|
|
return
|
|
let k = n.kind
|
|
c &= char(k)
|
|
# we really must not hash line information. 'n.typ' is debatable but
|
|
# shouldn't be necessary for now and avoids potential infinite recursions.
|
|
case n.kind
|
|
of nkEmpty, nkNilLit, nkType: discard
|
|
of nkIdent:
|
|
c &= n.ident.s
|
|
of nkSym:
|
|
hashSym(c, n.sym)
|
|
of nkCharLit..nkUInt64Lit:
|
|
let v = n.intVal
|
|
lowlevel v
|
|
of nkFloatLit..nkFloat64Lit:
|
|
let v = n.floatVal
|
|
lowlevel v
|
|
of nkStrLit..nkTripleStrLit:
|
|
c &= n.strVal
|
|
else:
|
|
for i in 0..<n.len: hashTree(c, n.sons[i])
|
|
|
|
proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]) =
|
|
if t == nil:
|
|
c &= "\254"
|
|
return
|
|
|
|
case t.kind
|
|
of tyGenericInvocation:
|
|
for i in countup(0, sonsLen(t) - 1):
|
|
c.hashType t.sons[i], flags
|
|
of tyDistinct:
|
|
if CoType in flags:
|
|
c.hashType t.lastSon, flags
|
|
else:
|
|
c.hashSym(t.sym)
|
|
of tyGenericInst:
|
|
if sfInfixCall in t.base.sym.flags:
|
|
# This is an imported C++ generic type.
|
|
# We cannot trust the `lastSon` to hold a properly populated and unique
|
|
# value for each instantiation, so we hash the generic parameters here:
|
|
let normalizedType = t.skipGenericAlias
|
|
for i in 0 .. normalizedType.len - 2:
|
|
c.hashType t.sons[i], flags
|
|
else:
|
|
c.hashType t.lastSon, flags
|
|
of tyAlias, tySink, tyUserTypeClasses, tyInferred, tyOwned:
|
|
c.hashType t.lastSon, flags
|
|
of tyBool, tyChar, tyInt..tyUInt64:
|
|
# no canonicalization for integral types, so that e.g. ``pid_t`` is
|
|
# produced instead of ``NI``:
|
|
c &= char(t.kind)
|
|
if t.sym != nil and {sfImportc, sfExportc} * t.sym.flags != {}:
|
|
c.hashSym(t.sym)
|
|
of tyObject, tyEnum:
|
|
if t.typeInst != nil:
|
|
# prevent against infinite recursions here, see bug #8883:
|
|
let inst = t.typeInst
|
|
t.typeInst = nil
|
|
assert inst.kind == tyGenericInst
|
|
for i in countup(0, inst.len - 2):
|
|
c.hashType inst.sons[i], flags
|
|
t.typeInst = inst
|
|
return
|
|
c &= char(t.kind)
|
|
# Every cyclic type in Nim need to be constructed via some 't.sym', so this
|
|
# is actually safe without an infinite recursion check:
|
|
if t.sym != nil:
|
|
if {sfCompilerProc} * t.sym.flags != {}:
|
|
doAssert t.sym.loc.r != nil
|
|
# The user has set a specific name for this type
|
|
c &= t.sym.loc.r
|
|
elif CoOwnerSig in flags:
|
|
c.hashTypeSym(t.sym)
|
|
else:
|
|
c.hashSym(t.sym)
|
|
if {sfAnon, sfGenSym} * t.sym.flags != {}:
|
|
# Generated object names can be identical, so we need to
|
|
# disambiguate furthermore by hashing the field types and names.
|
|
if t.n.len > 0:
|
|
let oldFlags = t.sym.flags
|
|
# Mild hack to prevent endless recursion.
|
|
t.sym.flags = t.sym.flags - {sfAnon, sfGenSym}
|
|
for n in t.n:
|
|
assert(n.kind == nkSym)
|
|
let s = n.sym
|
|
c.hashSym s
|
|
c.hashType s.typ, flags
|
|
t.sym.flags = oldFlags
|
|
else:
|
|
# The object has no fields: we _must_ add something here in order to
|
|
# make the hash different from the one we produce by hashing only the
|
|
# type name.
|
|
c &= ".empty"
|
|
else:
|
|
c &= t.id
|
|
if t.len > 0 and t.sons[0] != nil:
|
|
hashType c, t.sons[0], flags
|
|
of tyRef, tyPtr, tyGenericBody, tyVar:
|
|
c &= char(t.kind)
|
|
c.hashType t.lastSon, flags
|
|
if tfVarIsPtr in t.flags: c &= ".varisptr"
|
|
of tyFromExpr:
|
|
c &= char(t.kind)
|
|
c.hashTree(t.n)
|
|
of tyTuple:
|
|
c &= char(t.kind)
|
|
if t.n != nil and CoType notin flags:
|
|
assert(sonsLen(t.n) == sonsLen(t))
|
|
for i in countup(0, sonsLen(t.n) - 1):
|
|
assert(t.n.sons[i].kind == nkSym)
|
|
c &= t.n.sons[i].sym.name.s
|
|
c &= ':'
|
|
c.hashType(t.sons[i], flags+{CoIgnoreRange})
|
|
c &= ','
|
|
else:
|
|
for i in countup(0, sonsLen(t) - 1): c.hashType t.sons[i], flags+{CoIgnoreRange}
|
|
of tyRange:
|
|
if CoIgnoreRange notin flags:
|
|
c &= char(t.kind)
|
|
c.hashTree(t.n)
|
|
c.hashType(t.sons[0], flags)
|
|
of tyStatic:
|
|
c &= char(t.kind)
|
|
c.hashTree(t.n)
|
|
c.hashType(t.sons[0], flags)
|
|
of tyProc:
|
|
c &= char(t.kind)
|
|
c &= (if tfIterator in t.flags: "iterator " else: "proc ")
|
|
if CoProc in flags and t.n != nil:
|
|
let params = t.n
|
|
for i in 1..<params.len:
|
|
let param = params[i].sym
|
|
c &= param.name.s
|
|
c &= ':'
|
|
c.hashType(param.typ, flags)
|
|
c &= ','
|
|
c.hashType(t.sons[0], flags)
|
|
else:
|
|
for i in 0..<t.len: c.hashType(t.sons[i], flags)
|
|
c &= char(t.callConv)
|
|
# purity of functions doesn't have to affect the mangling (which is in fact
|
|
# problematic for HCR - someone could have cached a pointer to another
|
|
# function which changes its purity and suddenly the cached pointer is danglign)
|
|
# IMHO anything that doesn't affect the overload resolution shouldn't be part of the mangling...
|
|
# if CoType notin flags:
|
|
# if tfNoSideEffect in t.flags: c &= ".noSideEffect"
|
|
# if tfThread in t.flags: c &= ".thread"
|
|
if tfVarargs in t.flags: c &= ".varargs"
|
|
of tyArray:
|
|
c &= char(t.kind)
|
|
for i in 0..<t.len: c.hashType(t.sons[i], flags-{CoIgnoreRange})
|
|
else:
|
|
c &= char(t.kind)
|
|
for i in 0..<t.len: c.hashType(t.sons[i], flags)
|
|
if tfNotNil in t.flags and CoType notin flags: c &= "not nil"
|
|
|
|
when defined(debugSigHashes):
|
|
import db_sqlite
|
|
|
|
let db = open(connection="sighashes.db", user="araq", password="",
|
|
database="sighashes")
|
|
db.exec(sql"DROP TABLE IF EXISTS sighashes")
|
|
db.exec sql"""CREATE TABLE sighashes(
|
|
id integer primary key,
|
|
hash varchar(5000) not null,
|
|
type varchar(5000) not null,
|
|
unique (hash, type))"""
|
|
# select hash, type from sighashes where hash in
|
|
# (select hash from sighashes group by hash having count(*) > 1) order by hash;
|
|
|
|
proc hashType*(t: PType; flags: set[ConsiderFlag] = {CoType}): SigHash =
|
|
var c: MD5Context
|
|
md5Init c
|
|
hashType c, t, flags+{CoOwnerSig}
|
|
md5Final c, result.Md5Digest
|
|
when defined(debugSigHashes):
|
|
db.exec(sql"INSERT OR IGNORE INTO sighashes(type, hash) VALUES (?, ?)",
|
|
typeToString(t), $result)
|
|
|
|
proc hashProc*(s: PSym): SigHash =
|
|
var c: MD5Context
|
|
md5Init c
|
|
hashType c, s.typ, {CoProc}
|
|
|
|
var m = s
|
|
while m.kind != skModule: m = m.owner
|
|
let p = m.owner
|
|
assert p.kind == skPackage
|
|
c &= p.name.s
|
|
c &= "."
|
|
c &= m.name.s
|
|
if sfDispatcher in s.flags:
|
|
c &= ".dispatcher"
|
|
# so that createThread[void]() (aka generic specialization) gets a unique
|
|
# hash, we also hash the line information. This is pretty bad, but the best
|
|
# solution for now:
|
|
#c &= s.info.line
|
|
md5Final c, result.Md5Digest
|
|
|
|
proc hashNonProc*(s: PSym): SigHash =
|
|
var c: MD5Context
|
|
md5Init c
|
|
hashSym(c, s)
|
|
var it = s
|
|
while it != nil:
|
|
c &= it.name.s
|
|
c &= "."
|
|
it = it.owner
|
|
# for bug #5135 we also take the position into account, but only
|
|
# for parameters, because who knows what else position dependency
|
|
# might cause:
|
|
if s.kind == skParam:
|
|
c &= s.position
|
|
md5Final c, result.Md5Digest
|
|
|
|
proc hashOwner*(s: PSym): SigHash =
|
|
var c: MD5Context
|
|
md5Init c
|
|
var m = s
|
|
while m.kind != skModule: m = m.owner
|
|
let p = m.owner
|
|
assert p.kind == skPackage
|
|
c &= p.name.s
|
|
c &= "."
|
|
c &= m.name.s
|
|
|
|
md5Final c, result.Md5Digest
|
|
|
|
proc sigHash*(s: PSym): SigHash =
|
|
if s.kind in routineKinds and s.typ != nil:
|
|
result = hashProc(s)
|
|
else:
|
|
result = hashNonProc(s)
|
|
|
|
proc idOrSig*(s: PSym, currentModule: string,
|
|
sigCollisions: var CountTable[SigHash]): Rope =
|
|
if s.kind in routineKinds and s.typ != nil:
|
|
# signatures for exported routines are reliable enough to
|
|
# produce a unique name and this means produced C++ is more stable wrt
|
|
# Nim changes:
|
|
let sig = hashProc(s)
|
|
result = rope($sig)
|
|
#let m = if s.typ.callConv != ccInline: findPendingModule(m, s) else: m
|
|
let counter = sigCollisions.getOrDefault(sig)
|
|
#if sigs == "_jckmNePK3i2MFnWwZlp6Lg" and s.name.s == "contains":
|
|
# echo "counter ", counter, " ", s.id
|
|
if counter != 0:
|
|
result.add "_" & rope(counter+1)
|
|
# this minor hack is necessary to make tests/collections/thashes compile.
|
|
# The inlined hash function's original module is ambiguous so we end up
|
|
# generating duplicate names otherwise:
|
|
if s.typ.callConv == ccInline:
|
|
result.add rope(currentModule)
|
|
sigCollisions.inc(sig)
|
|
else:
|
|
let sig = hashNonProc(s)
|
|
result = rope($sig)
|
|
let counter = sigCollisions.getOrDefault(sig)
|
|
if counter != 0:
|
|
result.add "_" & rope(counter+1)
|
|
sigCollisions.inc(sig)
|
|
|