summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorzah <zahary@gmail.com>2019-02-26 16:48:55 +0200
committerAndreas Rumpf <rumpf_a@web.de>2019-02-26 15:48:55 +0100
commitca4b971bc81b2e751e0388d80896fde7079b1679 (patch)
treee92e9f519c1464c47c26e776e1f5cac5a20e105a /lib
parentba38c05eb62a1b6e0b36b92886c43bed2cabd90a (diff)
downloadNim-ca4b971bc81b2e751e0388d80896fde7079b1679.tar.gz
Initial version of the hot-code reloading support for native targets (#10729)
* squashed work by Zahary

* squashing a ton of useful history... otherwise rebasing on top of upstream Nim after commit 82c009a2cbc5d07ab9a847f1c58228a20efaf219 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)
Diffstat (limited to 'lib')
-rw-r--r--lib/core/hotcodereloading.nim27
-rw-r--r--lib/core/macros.nim7
-rw-r--r--lib/core/strs.nim2
-rw-r--r--lib/core/typeinfo.nim2
-rw-r--r--lib/nimbase.h5
-rw-r--r--lib/nimhcr.nim652
-rw-r--r--lib/nimhcr.nim.cfg5
-rw-r--r--lib/nimrtl.nim.cfg1
-rw-r--r--lib/pure/collections/sharedstrings.nim2
-rw-r--r--lib/pure/os.nim2
-rw-r--r--lib/pure/reservedmem.nim241
-rw-r--r--lib/pure/strformat.nim6
-rw-r--r--lib/pure/strtabs.nim87
-rw-r--r--lib/system.nim20
-rw-r--r--lib/system/cgprocs.nim8
-rw-r--r--lib/system/chcks.nim2
-rw-r--r--lib/system/dyncalls.nim2
-rw-r--r--lib/system/excpt.nim4
-rw-r--r--lib/system/gc_common.nim8
-rw-r--r--lib/system/memory.nim6
-rw-r--r--lib/system/sysstr.nim2
-rw-r--r--lib/windows/winlean.nim4
22 files changed, 1026 insertions, 69 deletions
diff --git a/lib/core/hotcodereloading.nim b/lib/core/hotcodereloading.nim
new file mode 100644
index 000000000..8b48b3d69
--- /dev/null
+++ b/lib/core/hotcodereloading.nim
@@ -0,0 +1,27 @@
+when defined(hotcodereloading):
+  import
+    macros
+
+  template beforeCodeReload*(body: untyped) =
+    hcrAddEventHandler(true, proc = body) {.executeOnReload.}
+
+  template afterCodeReload*(body: untyped) =
+    hcrAddEventHandler(false, proc = body) {.executeOnReload.}
+
+  macro hasModuleChanged*(module: typed): untyped =
+    if module.kind != nnkSym or module.symKind != nskModule:
+      error "hasModuleChanged expects a module symbol", module
+    return newCall(bindSym"hcrHasModuleChanged", newLit(module.signatureHash))
+
+  proc hasAnyModuleChanged*(): bool = hcrReloadNeeded()
+
+  when not defined(JS):
+    template performCodeReload* = hcrPerformCodeReload()
+  else:
+    template performCodeReload* = discard
+else:
+  template beforeCodeReload*(body: untyped) = discard
+  template afterCodeReload*(body: untyped) = discard
+  template hasModuleChanged*(module: typed): bool = false
+  proc hasAnyModuleChanged*(): bool = false
+  template performCodeReload*() = discard
diff --git a/lib/core/macros.nim b/lib/core/macros.nim
index 461afb963..8e6b93a11 100644
--- a/lib/core/macros.nim
+++ b/lib/core/macros.nim
@@ -347,6 +347,13 @@ object
     doAssert(dumpTypeImpl(b) == t)
     doAssert(dumpTypeImpl(c) == t)
 
+when defined(nimHasSignatureHashInMacro):
+  proc signatureHash*(n: NimNode): string {.magic: "NSigHash", noSideEffect.}
+    ## Returns a stable identifier derived from the signature of a symbol.
+    ## The signature combines many factors such as the type of the symbol,
+    ## the owning module of the symbol and others. The same identifier is
+    ## used in the back-end to produce the mangled symbol name.
+
 proc getTypeImpl*(n: typedesc): NimNode {.magic: "NGetType", noSideEffect.}
   ## Version of ``getTypeImpl`` which takes a ``typedesc``.
 
diff --git a/lib/core/strs.nim b/lib/core/strs.nim
index ccbde76fe..e55c88493 100644
--- a/lib/core/strs.nim
+++ b/lib/core/strs.nim
@@ -125,7 +125,7 @@ proc cstrToNimstr(str: cstring): NimStringV2 {.compilerRtl.} =
   if str == nil: toNimStr(str, 0)
   else: toNimStr(str, str.len)
 
-proc nimToCStringConv(s: NimStringV2): cstring {.compilerProc, inline.} =
+proc nimToCStringConv(s: NimStringV2): cstring {.compilerProc, nonReloadable, inline.} =
   if s.len == 0: result = cstring""
   else: result = cstring(unsafeAddr s.p.data)
 
diff --git a/lib/core/typeinfo.nim b/lib/core/typeinfo.nim
index d6dd16b54..fe958c7f5 100644
--- a/lib/core/typeinfo.nim
+++ b/lib/core/typeinfo.nim
@@ -27,8 +27,6 @@
 include "system/inclrtl.nim"
 include "system/hti.nim"
 
-import system/indexerrors
-
 {.pop.}
 
 type
diff --git a/lib/nimbase.h b/lib/nimbase.h
index ba4273726..9fd475c85 100644
--- a/lib/nimbase.h
+++ b/lib/nimbase.h
@@ -354,7 +354,12 @@ typedef NU8 NU;
 #  endif
 #endif
 
+// for now there isn't an easy way for C code to reach the program result
+// when hot code reloading is ON - users will have to:
+// load the nimhcr.dll, get the hcrGetGlobal proc from there and use it
+#ifndef NIM_HOT_CODE_RELOADING
 extern NI nim_program_result;
+#endif
 
 typedef float NF32;
 typedef double NF64;
diff --git a/lib/nimhcr.nim b/lib/nimhcr.nim
new file mode 100644
index 000000000..f3afac347
--- /dev/null
+++ b/lib/nimhcr.nim
@@ -0,0 +1,652 @@
+#
+#
+#            Nim's Runtime Library
+#        (c) Copyright 2018 Nim Contributors
+#
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+#
+
+## This is the Nim hot code reloading run-time for the native targets.
+##
+## This minimal dynamic library is not subject to reloading when the
+## `hotCodeReloading` build mode is enabled. It's responsible for providing
+## a permanent memory location for all globals and procs within a program
+## and orchestrating the reloading. For globals, this is easily achieved
+## by storing them on the heap. For procs, we produce on the fly simple
+## trampolines that can be dynamically overwritten to jump to a different
+## target. In the host program, all globals and procs are first registered
+## here with ``hcrRegisterGlobal`` and ``hcrRegisterProc`` and then the
+## returned permanent locations are used in every reference to these symbols
+## onwards.
+##
+## Detailed description:
+##
+## When code is compiled with the hotCodeReloading option for native targets
+## a couple of things happen for all modules in a project:
+## - the useNimRtl option is forced (including when building the HCR runtime too)
+## - all modules of a target get built into separate shared libraries
+##   - the smallest granularity of reloads is modules
+##   - for each .c (or .cpp) in the corresponding nimcache folder of the project
+##     a shared object is built with the name of the source file + DLL extension
+##   - only the main module produces whatever the original project type intends
+##     (again in nimcache) and is then copied to its original destination
+##   - linking is done in parallel - just like compilation
+## - function calls to functions from the same project go through function pointers:
+##   - with a few exceptions - see the nonReloadable pragma
+##   - the forward declarations of the original functions become function
+##     pointers as static globals with the same names
+##   - the original function definitions get suffixed with <name>_actual
+##   - the function pointers get initialized with the address of the corresponding
+##     function in the DatInit of their module through a call to either hcrRegisterProc
+##     or hcrGetProc. When being registered, the <name>_actual address is passed to
+##     hcrRegisterProc and a permanent location is returned and assigned to the pointer.
+##     This way the implementation (<name>_actual) can change but the address for it
+##     will be the same - this works by just updating a jump instruction (trampoline).
+##     For functions from other modules hcrGetProc is used (after they are registered).
+## - globals are initialized only once and their state is preserved
+##   - including locals with the {.global.} pragma
+##   - their definitions are changed into pointer definitions which are initialized
+##     in the DatInit() of their module with calls to hcrRegisterGlobal (supplying the
+##     size of the type that this HCR runtime should allocate) and a bool is returned
+##     which when true triggers the initialization code for the global (only once).
+##     Globals from other modules: a global pointer coupled with a hcrGetGlobal call.
+##   - globals which have already been initialized cannot have their values changed
+##     by changing their initialization - use a handler or some other mechanism
+##   - new globals can be introduced when reloading
+## - top-level code (global scope) is executed only once - at the first module load
+## - the runtime knows every symbol's module owner (globals and procs)
+## - both the RTL and HCR shared libraries need to be near the program for execution
+##   - same folder, in the PATH or LD_LIBRARY_PATH env var, etc (depending on OS)
+## - the main module is responsible for initializing the HCR runtime
+##   - the main module loads the RTL and HCR shared objects
+##   - after that a call to hcrInit() is done in the main module which triggers
+##     the loading of all modules the main one imports, and doing that for the
+##     dependencies of each module recursively. Basically a DFS traversal.
+##   - then initialization takes place with several passes over all modules:
+##     - HcrInit - initializes the pointers for HCR procs such as hcrRegisterProc
+##     - HcrCreateTypeInfos - creates globals which will be referenced in the next pass
+##     - DatInit - usual dat init + register/get procs and get globals
+##     - Init - it does the following multiplexed operations:
+##       - register globals (if already registered - then just retrieve pointer)
+##       - execute top level scope (only if loaded for the first time)
+##   - when modules are loaded the originally built shared libraries get copied in
+##     the same folder and the copies are loaded instead of the original files
+##   - a module import tree is built in the runtime (and maintained when reloading)
+## - hcrPerformCodeReload
+##   - named `performCodeReload`, requires the hotcodereloading module
+##   - explicitly called by the user - the current active callstack shouldn't contain
+##     any functions which are defined in modules that will be reloaded (or crash!).
+##     The reason is that old dynalic libraries get unloaded.
+##     Example:
+##       if A is the main module and it imports B, then only B is reloadable and only
+##       if when calling hcrPerformCodeReload there is no function defined in B in the
+##       current active callstack at the point of the call (it has to be done from A)
+##   - for reloading to take place the user has to have rebuilt parts of the application
+##     without changes affecting the main module in any way - it shouldn't be rebuilt.
+##   - to determine what needs to be reloaded the runtime starts traversing the import
+##     tree from the root and checks the timestamps of the loaded shared objects
+##   - modules that are no longer referenced are unloaded and cleaned up properly
+##   - symbols (procs/globals) that have been removed in the code are also cleaned up
+##     - so changing the init of a global does nothing, but removing it, reloading,
+##       and then re-introducing it with a new initializer works
+##   - new modules can be imported, and imports can also be reodereded/removed
+##   - hcrReloadNeeded() can be used to determine if any module needs reloading
+##     - named `hasAnyModuleChanged`, requires the hotcodereloading module
+## - code in the beforeCodeReload/afterCodeReload handlers is executed on each reload
+##   - require the hotcodereloading module
+##   - such handlers can be added and removed
+##   - before each reload all "beforeCodeReload" handlers are executed and after
+##     that all handlers (including "after") from the particular module are deleted
+##   - the order of execution is the same as the order of top-level code execution.
+##     Example: if A imports B which imports C, then all handlers in C will be executed
+##     first (from top to bottom) followed by all from B and lastly all from A
+##   - after the reload all "after" handlers are executed the same way as "before"
+##   - the handlers for a reloaded module are always removed when reloading and then
+##     registered when the top-level scope is executed (thanks to `executeOnReload`)
+##
+## TODO - after first merge in upstream Nim:
+##
+## - profile
+##   - build speed with and without hot code reloading - difference should be small
+##   - runtime degradation of HCR-enabled code - important!!!
+## - ARM support for the trampolines
+## - investigate:
+##   - rethink the closure iterators
+##     - ability to keep old versions of dynamic libraries alive
+##       - because of async server code
+##       - perhaps with refcounting of .dlls for unfinished closures
+##   - linking with static libs
+##     - all shared objects for each module will (probably) have to link to them
+##       - state in static libs gets duplicated
+##       - linking is slow and therefore iteration time suffers
+##         - have just a single .dll for all .nim files and bulk reload?
+##   - think about the compile/link/passC/passL/emit/injectStmt pragmas
+##     - if a passC pragma is introduced (either written or dragged in by a new
+##       import) the whole command line for compilation changes - for example:
+##         winlean.nim: {.passC: "-DWIN32_LEAN_AND_MEAN".}
+##   - play with plugins/dlls/lfIndirect/lfDynamicLib/lfExportLib - shouldn't add an extra '*'
+##   - everything thread-local related
+## - tests
+##   - add a new travis build matrix entry which builds everything with HCR enabled
+##     - currently building with useNimRtl is problematic - lots of problems...
+##     - how to supply the nimrtl/nimhcr shared objects to all test binaries...?
+##     - think about building to C++ instead of only to C - added type safety
+##   - run tests through valgrind and the sanitizers! of HUGE importance!
+##
+## TODO - nice to have cool stuff:
+##
+## - separate handling of global state for much faster reloading and manipulation
+##   - imagine sliders in an IDE for tweaking variables
+##   - perhaps using shared memory
+## - multi-dll projects - how everything can be reloaded..?
+##   - a single HCR instance shared across multiple .dlls
+##   - instead of having to call hcrPerformCodeReload from a function in each dll
+##     - which currently renders the main module of each dll not reloadable
+## - ability to check with the current callstack if a reload is "legal"
+##   - if it is in any function which is in a module about to be reloaded ==> error
+## - pragma annotations for files - to be excluded from dll shenanigans
+##   - for such file-global pragmas look at codeReordering or injectStmt
+##   - how would the initialization order be kept? messy...
+##   - per function exclude pragmas would be TOO messy and hard...
+## - C code calling stable exportc interface of nim code (for bindings)
+##   - generate proxy functions with the stable names
+##     - in a non-reloadable part (the main binary) that call the function pointers
+##     - parameter passing/forwarding - how? use the same trampoline jumping?
+##     - extracting the dependencies for these stubs/proxies will be hard...
+## - changing memory layout of types - detecting this..?
+##   - implement with registerType() call to HCR runtime...?
+##     - and checking if a previously registered type matches
+##   - issue an error
+##     - or let the user handle this by transferring the state properly
+##       - perhaps in the before/afterCodeReload handlers
+## - optimization: calls to procs within a module (+inlined) to use the _actual versions
+## - implement executeOnReload for global vars too - not just statements (and document!)
+## - cleanup at shutdown - freeing all globals
+##
+## TODO - unimportant:
+##
+## - have a "bad call" trampoline that all no-longer-present functions are routed to call there
+##     - so the user gets some error msg if he calls a dangling pointer instead of a crash
+## - before/afterCodeReload and hasModuleChanged should be accessible only where appropriate
+## - nim_program_result is inaccessible in HCR mode from external C code (see nimbase.h)
+## - proper .json build file - but the format is different... multiple link commands...
+## - avoid registering globals on each loop when using an iterator in global scope
+##
+## TODO - REPL:
+## - proper way (as proposed by Zahary):
+##   - parse the input code and put everything in global scope except for
+##     statements with side effects only - those go in afterCodeReload blocks
+## - my very hacky idea: just append to a closure iterator the new statements
+##   followed by a yield statement. So far I can think of 2 problems:
+##   - import and some other code cannot be written inside of a proc -
+##     has to be parsed and extracted in the outer scope
+##   - when new variables are created they are actually locals to the closure
+##     so the struct for the closure state grows in memory, but it has already
+##     been allocated when the closure was created with the previous smaller size.
+##     That would lead to working with memory outside of the initially allocated
+##     block. Perhaps something can be done about this - some way of re-allocating
+##     the state and transferring the old...
+
+when not defined(JS) and (defined(hotcodereloading) or
+                          defined(createNimHcr) or
+                          defined(testNimHcr)):
+  const
+    dllExt = when defined(windows): "dll"
+             elif defined(macosx): "dylib"
+             else: "so"
+  type
+    HcrProcGetter* = proc (libHandle: pointer, procName: cstring): pointer {.nimcall.}
+    HcrGcMarkerProc = proc () {.nimcall.}
+    HcrModuleInitializer* = proc () {.nimcall.}
+
+when defined(createNimHcr):
+  when system.appType != "lib":
+    {.error: "This file has to be compiled as a library!".}
+
+  import os, tables, sets, times, strutils, reservedmem, dynlib
+
+  template trace(args: varargs[untyped]) =
+    when defined(testNimHcr) or defined(traceHcr):
+      echo args
+
+  proc sanitize(arg: Time): string =
+    when defined(testNimHcr): return "<time>"
+    else: return $arg
+
+  proc sanitize(arg: string|cstring): string =
+    when defined(testNimHcr): return ($arg).splitFile.name.splitFile.name
+    else: return $arg
+
+  {.pragma: nimhcr, compilerProc, exportc, dynlib.}
+
+  when hostCPU in ["i386", "amd64"]:
+    type
+      ShortJumpInstruction {.packed.} = object
+        opcode: byte
+        offset: int32
+
+      LongJumpInstruction {.packed.} = object
+        opcode1: byte
+        opcode2: byte
+        offset: int32
+        absoluteAddr: pointer
+
+    proc writeJump(jumpTableEntry: ptr LongJumpInstruction, targetFn: pointer) =
+      let
+        jumpFrom = jumpTableEntry.shift(sizeof(ShortJumpInstruction))
+        jumpDistance = distance(jumpFrom, targetFn)
+
+      if abs(jumpDistance) < 0x7fff0000:
+        let shortJump = cast[ptr ShortJumpInstruction](jumpTableEntry)
+        shortJump.opcode = 0xE9 # relative jump
+        shortJump.offset = int32(jumpDistance)
+      else:
+        jumpTableEntry.opcode1 = 0xff # indirect absolute jump
+        jumpTableEntry.opcode2 = 0x25
+        when hostCPU == "i386":
+          # on x86 we write the absolute address of the following pointer
+          jumpTableEntry.offset = cast[int32](addr jumpTableEntry.absoluteAddr)
+        else:
+          # on x64, we use a relative address for the same location
+          jumpTableEntry.offset = 0
+        jumpTableEntry.absoluteAddr = targetFn
+
+  elif hostCPU == "arm":
+    const jumpSize = 8
+  elif hostCPU == "arm64":
+    const jumpSize = 16
+
+  const defaultJumpTableSize = case hostCPU
+                               of "i386": 50
+                               of "amd64": 500
+                               else: 50
+
+  let jumpTableSizeStr = getEnv("HOT_CODE_RELOADING_JUMP_TABLE_SIZE")
+  let jumpTableSize = if jumpTableSizeStr.len > 0: parseInt(jumpTableSizeStr)
+                      else: defaultJumpTableSize
+
+  # TODO: perhaps keep track of free slots due to removed procs using a free list
+  var jumpTable = ReservedMemSeq[LongJumpInstruction].init(
+    memStart = cast[pointer](0x10000000),
+    maxLen = jumpTableSize * 1024 * 1024 div sizeof(LongJumpInstruction),
+    accessFlags = memExecReadWrite)
+
+  type
+    ProcSym = object
+      jump: ptr LongJumpInstruction
+      gen: int
+
+    GlobalVarSym = object
+      p: pointer
+      markerProc: HcrGcMarkerProc
+      gen: int
+
+    ModuleDesc = object
+      procs: Table[string, ProcSym]
+      globals: Table[string, GlobalVarSym]
+      imports: seq[string]
+      handle: LibHandle
+      hash: string
+      gen: int
+      lastModification: Time
+      handlers: seq[tuple[isBefore: bool, cb: proc ()]]
+
+  proc newModuleDesc(): ModuleDesc =
+    result.procs = initTable[string, ProcSym]()
+    result.globals = initTable[string, GlobalVarSym]()
+    result.handle = nil
+    result.gen = -1
+    result.lastModification = low(Time)
+
+  # the global state necessary for traversing and reloading the module import tree
+  var modules = initTable[string, ModuleDesc]()
+  var root: string
+  var system: string
+  var mainDatInit: HcrModuleInitializer
+  var generation = 0
+
+  # necessary for queries such as "has module X changed" - contains all but the main module
+  var hashToModuleMap = initTable[string, string]()
+
+  # necessary for registering handlers and keeping them up-to-date
+  var currentModule: string
+
+  # supplied from the main module - used by others to initialize pointers to this runtime
+  var hcrDynlibHandle: pointer
+  var getProcAddr: HcrProcGetter
+
+  proc hcrRegisterProc*(module: cstring, name: cstring, fn: pointer): pointer {.nimhcr.} =
+    trace "  register proc: ", module.sanitize, " ", name
+    # Please note: We must allocate a local copy of the strings, because the supplied
+    # `cstring` will reside in the data segment of a DLL that will be later unloaded.
+    let name = $name
+    let module = $module
+
+    var jumpTableEntryAddr: ptr LongJumpInstruction
+
+    modules[module].procs.withValue(name, p):
+      trace "    update proc: ", name
+      jumpTableEntryAddr = p.jump
+      p.gen = generation
+    do:
+      let len = jumpTable.len
+      jumpTable.setLen(len + 1)
+      jumpTableEntryAddr = addr jumpTable[len]
+      modules[module].procs[name] = ProcSym(jump: jumpTableEntryAddr, gen: generation)
+
+    writeJump jumpTableEntryAddr, fn
+    return jumpTableEntryAddr
+
+  proc hcrGetProc*(module: cstring, name: cstring): pointer {.nimhcr.} =
+    trace "  get proc: ", module.sanitize, " ", name
+    return modules[$module].procs[$name].jump
+
+  proc hcrRegisterGlobal*(module: cstring,
+                          name: cstring,
+                          size: Natural,
+                          gcMarker: HcrGcMarkerProc,
+                          outPtr: ptr pointer): bool {.nimhcr.} =
+    trace "  register global: ", module.sanitize, " ", name
+    # Please note: We must allocate local copies of the strings, because the supplied
+    # `cstring` will reside in the data segment of a DLL that will be later unloaded.
+    # Also using a ptr pointer instead of a var pointer (an output parameter)
+    # because for the C++ backend var parameters use references and in this use case
+    # it is not possible to cast an int* (for example) to a void* and then pass it
+    # to void*& since the casting yields an rvalue and references bind only to lvalues.
+    let name = $name
+    let module = $module
+
+    modules[module].globals.withValue(name, global):
+      trace "    update global: ", name
+      outPtr[] = global.p
+      global.gen = generation
+      global.markerProc = gcMarker
+      return false
+    do:
+      outPtr[] = alloc0(size)
+      modules[module].globals[name] = GlobalVarSym(p: outPtr[],
+                                                   gen: generation,
+                                                   markerProc: gcMarker)
+      return true
+
+  proc hcrGetGlobal*(module: cstring, name: cstring): pointer {.nimhcr.} =
+    trace "  get global: ", module.sanitize, " ", name
+    return modules[$module].globals[$name].p
+
+  proc getListOfModules(cstringArray: ptr pointer): seq[string] =
+    var curr = cast[ptr cstring](cstringArray)
+    while len(curr[]) > 0:
+      result.add($curr[])
+      curr = cast[ptr cstring](cast[int64](curr) + sizeof(ptr cstring))
+
+  template cleanup(collection, body) =
+    var toDelete: seq[string]
+    for name, data in collection.pairs:
+      if data.gen < generation:
+        toDelete.add(name)
+        trace "HCR Cleaning ", astToStr(collection), " :: ", name, " ", data.gen
+    for name {.inject.} in toDelete:
+      body
+
+  proc cleanupGlobal(module: string, name: string) =
+    var g: GlobalVarSym
+    if modules[module].globals.take(name, g):
+      dealloc g.p
+
+  proc cleanupSymbols(module: string) =
+    cleanup modules[module].globals:
+      cleanupGlobal(module, name)
+
+    cleanup modules[module].procs:
+      modules[module].procs.del(name)
+
+  proc unloadDll(name: string) =
+    if modules[name].handle != nil:
+      unloadLib(modules[name].handle)
+
+  proc loadDll(name: cstring) {.nimhcr.} =
+    let name = $name
+    trace "HCR LOADING: ", name.sanitize
+    if modules.contains(name):
+      unloadDll(name)
+    else:
+      modules.add(name, newModuleDesc())
+
+    let copiedName = name & ".copy." & dllExt
+    copyFile(name, copiedName)
+
+    let lib = loadLib(copiedName)
+    assert lib != nil
+    modules[name].handle = lib
+    modules[name].gen = generation
+    modules[name].lastModification = getLastModificationTime(name)
+
+    # update the list of imports by the module
+    let getImportsProc = cast[proc (): ptr pointer {.nimcall.}](
+      checkedSymAddr(lib, "HcrGetImportedModules"))
+    modules[name].imports = getListOfModules(getImportsProc())
+    # get the hash of the module
+    let getHashProc = cast[proc (): cstring {.nimcall.}](
+      checkedSymAddr(lib, "HcrGetSigHash"))
+    modules[name].hash = $getHashProc()
+    hashToModuleMap[modules[name].hash] = name
+
+    # Remove handlers for this module if reloading - they will be re-registered.
+    # In order for them to be re-registered we need to de-register all globals
+    # that trigger the registering of handlers through calls to hcrAddEventHandler
+    modules[name].handlers.setLen(0)
+
+  proc initHcrData(name: cstring) {.nimhcr.} =
+    trace "HCR Hcr init: ", name.sanitize
+    cast[proc (h: pointer, gpa: HcrProcGetter) {.nimcall.}](
+      checkedSymAddr(modules[$name].handle, "HcrInit000"))(hcrDynlibHandle, getProcAddr)
+
+  proc initTypeInfoGlobals(name: cstring) {.nimhcr.} =
+    trace "HCR TypeInfo globals init: ", name.sanitize
+    cast[HcrModuleInitializer](checkedSymAddr(modules[$name].handle, "HcrCreateTypeInfos"))()
+
+  proc initPointerData(name: cstring) {.nimhcr.} =
+    trace "HCR Dat init: ", name.sanitize
+    cast[HcrModuleInitializer](checkedSymAddr(modules[$name].handle, "DatInit000"))()
+
+  proc initGlobalScope(name: cstring) {.nimhcr.} =
+    trace "HCR Init000: ", name.sanitize
+    # set the currently inited module - necessary for registering the before/after HCR handlers
+    currentModule = $name
+    cast[HcrModuleInitializer](checkedSymAddr(modules[$name].handle, "Init000"))()
+
+  var modulesToInit: seq[string] = @[]
+  var allModulesOrderedByDFS: seq[string] = @[]
+
+  proc recursiveDiscovery(dlls: seq[string]) =
+    for curr in dlls:
+      if modules.contains(curr):
+        # skip updating modules that have already been updated to the latest generation
+        if modules[curr].gen >= generation:
+          trace "HCR SKIP: ", curr.sanitize, " gen is already: ", modules[curr].gen
+          continue
+        # skip updating an unmodified module but continue traversing its dependencies
+        if modules[curr].lastModification >= getLastModificationTime(curr):
+          trace "HCR SKIP (not modified): ", curr.sanitize, " ", modules[curr].lastModification.sanitize
+          # update generation so module doesn't get collected
+          modules[curr].gen = generation
+          # recurse to imported modules - they might be changed
+          recursiveDiscovery(modules[curr].imports)
+          allModulesOrderedByDFS.add(curr)
+          continue
+      loadDll(curr)
+      # first load all dependencies of the current module and init it after that
+      recursiveDiscovery(modules[curr].imports)
+
+      allModulesOrderedByDFS.add(curr)
+      modulesToInit.add(curr)
+
+  proc initModules() =
+    # first init the pointers to hcr functions and also do the registering of typeinfo globals
+    for curr in modulesToInit:
+      initHcrData(curr)
+      initTypeInfoGlobals(curr)
+    # for now system always gets fully inited before any other module (including when reloading)
+    initPointerData(system)
+    initGlobalScope(system)
+    # proceed with the DatInit calls - for all modules - including the main one!
+    for curr in allModulesOrderedByDFS:
+      if curr != system:
+        initPointerData(curr)
+    mainDatInit()
+    # execute top-level code (in global scope)
+    for curr in modulesToInit:
+      if curr != system:
+        initGlobalScope(curr)
+    # cleanup old symbols which are gone now
+    for curr in modulesToInit:
+      cleanupSymbols(curr)
+
+  proc hcrInit*(moduleList: ptr pointer, main, sys: cstring,
+                datInit: HcrModuleInitializer, handle: pointer, gpa: HcrProcGetter) {.nimhcr.} =
+    trace "HCR INITING: ", main.sanitize, " gen: ", generation
+    # initialize globals
+    root = $main
+    system = $sys
+    mainDatInit = datInit
+    hcrDynlibHandle = handle
+    getProcAddr = gpa
+    # the root is already added and we need it because symbols from it will also be registered in the HCR system
+    modules[root].imports = getListOfModules(moduleList)
+    modules[root].gen = high(int) # something huge so it doesn't get collected
+    # recursively initialize all modules
+    recursiveDiscovery(modules[root].imports)
+    initModules()
+    # the next module to be inited will be the root
+    currentModule = root
+
+  proc hcrHasModuleChanged*(moduleHash: string): bool {.nimhcr.} =
+    let module = hashToModuleMap[moduleHash]
+    return modules[module].lastModification < getLastModificationTime(module)
+
+  proc hcrReloadNeeded*(): bool {.nimhcr.} =
+    for hash, _ in hashToModuleMap:
+      if hcrHasModuleChanged(hash):
+        return true
+    return false
+
+  proc hcrPerformCodeReload*() {.nimhcr.} =
+    if not hcrReloadNeeded():
+      trace "HCR - no changes"
+      return
+
+    # We disable the GC during the reload, because the reloading procedures
+    # will replace type info objects and GC marker procs. This seems to create
+    # problems when the GC is executed while the reload is underway.
+    # Future versions of NIMHCR won't use the GC, because all globals and the
+    # metadata needed to access them will be placed in shared memory, so they
+    # can be manipulted from external programs without reloading.
+    GC_disable()
+    defer: GC_enable()
+
+    inc(generation)
+    trace "HCR RELOADING: ", generation
+
+    var traversedHandlerModules = initSet[string]()
+
+    proc recursiveExecuteHandlers(isBefore: bool, module: string) =
+      # do not process an already traversed module
+      if traversedHandlerModules.containsOrIncl(module): return
+      traversedHandlerModules.incl module
+      # first recurse to do a DFS traversal
+      for curr in modules[module].imports:
+        recursiveExecuteHandlers(isBefore, curr)
+      # and then execute the handlers - from leaf modules all the way up to the root module
+      for curr in modules[module].handlers:
+        if curr.isBefore == isBefore:
+         curr.cb()
+
+    # first execute the before reload handlers
+    traversedHandlerModules.clear()
+    recursiveExecuteHandlers(true, root)
+
+    # do the reloading
+    modulesToInit = @[]
+    allModulesOrderedByDFS = @[]
+    recursiveDiscovery(modules[root].imports)
+    initModules()
+
+    # execute the after reload handlers
+    traversedHandlerModules.clear()
+    recursiveExecuteHandlers(false, root)
+
+    # collecting no longer referenced modules - based on their generation
+    cleanup modules:
+      cleanupSymbols(name)
+      unloadDll(name)
+      hashToModuleMap.del(modules[name].hash)
+      modules.del(name)
+
+  proc hcrAddEventHandler*(isBefore: bool, cb: proc ()) {.nimhcr.} =
+    modules[currentModule].handlers.add(
+      (isBefore: isBefore, cb: cb))
+
+  proc hcrAddModule*(module: cstring) {.nimhcr.} =
+    if not modules.contains($module):
+      modules.add($module, newModuleDesc())
+
+  proc hcrGeneration*(): int {.nimhcr.} =
+    generation
+
+  proc hcrMarkGlobals*() {.nimhcr, nimcall, gcsafe.} =
+    # This is gcsafe, because it will be registered
+    # only in the GC of the main thread.
+    {.gcsafe.}:
+      for _, module in modules:
+        for _, global in module.globals:
+          if global.markerProc != nil:
+            global.markerProc()
+
+elif defined(hotcodereloading) or defined(testNimHcr):
+  when not defined(JS):
+    const
+      nimhcrLibname = when defined(windows): "nimhcr." & dllExt
+                      elif defined(macosx): "libnimhcr." & dllExt
+                      else: "libnimhcr." & dllExt
+
+    {.pragma: nimhcr, compilerProc, importc, dynlib: nimhcrLibname.}
+
+    proc hcrRegisterProc*(module: cstring, name: cstring, fn: pointer): pointer {.nimhcr.}
+
+    proc hcrGetProc*(module: cstring, name: cstring): pointer {.nimhcr.}
+
+    proc hcrRegisterGlobal*(module: cstring, name: cstring, size: Natural,
+                            gcMarker: HcrGcMarkerProc, outPtr: ptr pointer): bool {.nimhcr.}
+    proc hcrGetGlobal*(module: cstring, name: cstring): pointer {.nimhcr.}
+
+    proc hcrInit*(moduleList: ptr pointer,
+                  main, sys: cstring,
+                  datInit: HcrModuleInitializer,
+                  handle: pointer,
+                  gpa: HcrProcGetter) {.nimhcr.}
+
+    proc hcrAddModule*(module: cstring) {.nimhcr.}
+
+    proc hcrHasModuleChanged*(moduleHash: string): bool {.nimhcr.}
+
+    proc hcrReloadNeeded*(): bool {.nimhcr.}
+
+    proc hcrPerformCodeReload*() {.nimhcr.}
+
+    proc hcrAddEventHandler*(isBefore: bool, cb: proc ()) {.nimhcr.}
+
+    proc hcrMarkGlobals*() {.nimhcr, nimcall, gcsafe.}
+
+    when declared(nimRegisterGlobalMarker):
+      nimRegisterGlobalMarker(hcrMarkGlobals)
+
+  else:
+    proc hcrHasModuleChanged*(moduleHash: string): bool =
+      # TODO
+      false
+
+    proc hcrAddEventHandler*(isBefore: bool, cb: proc ()) =
+      # TODO
+      discard
+
diff --git a/lib/nimhcr.nim.cfg b/lib/nimhcr.nim.cfg
new file mode 100644
index 000000000..282bec27c
--- /dev/null
+++ b/lib/nimhcr.nim.cfg
@@ -0,0 +1,5 @@
+--app:lib
+--threads:on
+-d:useNimRtl
+-d:createNimHcr
+
diff --git a/lib/nimrtl.nim.cfg b/lib/nimrtl.nim.cfg
index b60de183a..a5cec596f 100644
--- a/lib/nimrtl.nim.cfg
+++ b/lib/nimrtl.nim.cfg
@@ -1,5 +1,6 @@
 # The RTL.dll needs to be compiled with these options!
 
 --app:lib
+--threads:on
 --define:createNimRtl
 
diff --git a/lib/pure/collections/sharedstrings.nim b/lib/pure/collections/sharedstrings.nim
index ca52ec63c..17e2e5888 100644
--- a/lib/pure/collections/sharedstrings.nim
+++ b/lib/pure/collections/sharedstrings.nim
@@ -12,8 +12,6 @@
 type
   UncheckedCharArray = UncheckedArray[char]
 
-import system/indexerrors
-
 type
   Buffer = ptr object
     refcount: int
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index 0b9c8babc..fec4ed843 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -47,7 +47,7 @@
 include "system/inclrtl"
 
 import
-  strutils, pathnorm, system/indexerrors
+  strutils, pathnorm
 
 const weirdTarget = defined(nimscript) or defined(js)
 
diff --git a/lib/pure/reservedmem.nim b/lib/pure/reservedmem.nim
new file mode 100644
index 000000000..22e2b5096
--- /dev/null
+++ b/lib/pure/reservedmem.nim
@@ -0,0 +1,241 @@
+#
+#
+#            Nim's Runtime Library
+#        (c) Copyright 2015 Nim Contributors
+#
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+#
+
+## :Authors: Zahary Karadjov
+##
+## This module provides utilities for reserving a portions of the
+## address space of a program without consuming physical memory.
+## It can be used to implement a dynamically resizable buffer that
+## is guaranteed to remain in the same memory location. The buffer
+## will be able to grow up to the size of the initially reserved
+## portion of the address space.
+
+from ospaths import raiseOSError, osLastError
+
+template distance*(lhs, rhs: pointer): int =
+  cast[int](rhs) - cast[int](lhs)
+
+template shift*(p: pointer, distance: int): pointer =
+  cast[pointer](cast[int](p) + distance)
+
+type
+  MemAccessFlags* = int
+
+  ReservedMem* = object
+    memStart: pointer
+    usedMemEnd: pointer
+    committedMemEnd: pointer
+    memEnd: pointer
+    maxCommittedAndUnusedPages: int
+    accessFlags: MemAccessFlags
+
+  ReservedMemSeq*[T] = object
+    mem: ReservedMem
+
+when defined(windows):
+  import winlean
+
+  type
+    SYSTEM_INFO {.final, pure.} = object
+      u1: uint32
+      dwPageSize: uint32
+      lpMinimumApplicationAddress: pointer
+      lpMaximumApplicationAddress: pointer
+      dwActiveProcessorMask: ptr uint32
+      dwNumberOfProcessors: uint32
+      dwProcessorType: uint32
+      dwAllocationGranularity: uint32
+      wProcessorLevel: uint16
+      wProcessorRevision: uint16
+
+  proc getSystemInfo(lpSystemInfo: ptr SYSTEM_INFO) {.stdcall, dynlib: "kernel32", importc: "GetSystemInfo".}
+
+  proc getAllocationGranularity: uint =
+    var sysInfo: SYSTEM_INFO
+    getSystemInfo(addr sysInfo)
+    return uint(sysInfo.dwAllocationGranularity)
+
+  let allocationGranularity = getAllocationGranularity().int
+
+  const
+    memNoAccess       = MemAccessFlags(PAGE_NOACCESS)
+    memExec*          = MemAccessFlags(PAGE_EXECUTE)
+    memExecRead*      = MemAccessFlags(PAGE_EXECUTE_READ)
+    memExecReadWrite* = MemAccessFlags(PAGE_EXECUTE_READWRITE)
+    memRead*          = MemAccessFlags(PAGE_READONLY)
+    memReadWrite*     = MemAccessFlags(PAGE_READWRITE)
+
+  template check(expr) =
+    let r = expr
+    if r == cast[type(r)](0):
+      raiseOSError(osLastError())
+
+else:
+  import posix
+
+  var MAP_ANONYMOUS {.importc: "MAP_ANONYMOUS", header: "<sys/mman.h>".}: cint
+  var MAP_NORESERVE {.importc: "MAP_NORESERVE", header: "<sys/mman.h>".}: cint
+  # var MAP_FIXED_NOREPLACE {.importc: "MAP_FIXED_NOREPLACE", header: "<sys/mman.h>".}: cint
+
+  var SC_PAGESIZE {.importc: "_SC_PAGESIZE", header: "<unistd.h>".}: cint
+
+  let allocationGranularity = sysconf(SC_PAGESIZE)
+
+  let
+    memNoAccess       = MemAccessFlags(PROT_NONE)
+    memExec*          = MemAccessFlags(PROT_EXEC)
+    memExecRead*      = MemAccessFlags(PROT_EXEC or PROT_READ)
+    memExecReadWrite* = MemAccessFlags(PROT_EXEC or PROT_READ or PROT_WRITE)
+    memRead*          = MemAccessFlags(PROT_READ)
+    memReadWrite*     = MemAccessFlags(PROT_READ or PROT_WRITE)
+
+  template check(expr) =
+    if not expr:
+      raiseOSError(osLastError())
+
+func nextAlignedOffset(n, alignment: int): int =
+  result = n
+  let m = n mod alignment
+  if m != 0: result += alignment - m
+
+
+when defined(windows):
+  const
+    MEM_DECOMMIT = 0x4000
+    MEM_RESERVE = 0x2000
+    MEM_COMMIT = 0x1000
+  proc virtualFree(lpAddress: pointer, dwSize: int,
+                   dwFreeType: int32): cint {.header: "<windows.h>", stdcall,
+                   importc: "VirtualFree".}
+  proc virtualAlloc(lpAddress: pointer, dwSize: int, flAllocationType,
+                    flProtect: int32): pointer {.
+                    header: "<windows.h>", stdcall, importc: "VirtualAlloc".}
+
+proc init*(T: type ReservedMem,
+           maxLen: Natural,
+           initLen: Natural = 0,
+           initCommitLen = initLen,
+           memStart = pointer(nil),
+           accessFlags = memReadWrite,
+           maxCommittedAndUnusedPages = 3): ReservedMem =
+
+  assert initLen <= initCommitLen
+  let commitSize = nextAlignedOffset(initCommitLen, allocationGranularity)
+
+  when defined(windows):
+    result.memStart = virtualAlloc(memStart, maxLen, MEM_RESERVE, accessFlags.cint)
+    check result.memStart
+    if commitSize > 0:
+      check virtualAlloc(result.memStart, commitSize, MEM_COMMIT, accessFlags.cint)
+  else:
+    var allocFlags = MAP_PRIVATE or MAP_ANONYMOUS # or MAP_NORESERVE
+    # if memStart != nil:
+    #  allocFlags = allocFlags or MAP_FIXED_NOREPLACE
+    result.memStart = mmap(memStart, maxLen, PROT_NONE, allocFlags, -1, 0)
+    check result.memStart != MAP_FAILED
+    if commitSize > 0:
+      check mprotect(result.memStart, commitSize, cint(accessFlags)) == 0
+
+  result.usedMemEnd = result.memStart.shift(initLen)
+  result.committedMemEnd = result.memStart.shift(commitSize)
+  result.memEnd = result.memStart.shift(maxLen)
+  result.accessFlags = accessFlags
+  result.maxCommittedAndUnusedPages = maxCommittedAndUnusedPages
+
+func len*(m: ReservedMem): int =
+  distance(m.memStart, m.usedMemEnd)
+
+func commitedLen*(m: ReservedMem): int =
+  distance(m.memStart, m.committedMemEnd)
+
+func maxLen*(m: ReservedMem): int =
+  distance(m.memStart, m.memEnd)
+
+proc setLen*(m: var ReservedMem, newLen: int) =
+  let len = m.len
+  m.usedMemEnd = m.memStart.shift(newLen)
+  if newLen > len:
+    let d = distance(m.committedMemEnd, m.usedMemEnd)
+    if d > 0:
+      let commitExtensionSize = nextAlignedOffset(d, allocationGranularity)
+      when defined(windows):
+        check virtualAlloc(m.committedMemEnd, commitExtensionSize,
+                           MEM_COMMIT, m.accessFlags.cint)
+      else:
+        check mprotect(m.committedMemEnd, commitExtensionSize, m.accessFlags.cint) == 0
+  else:
+    let d = distance(m.usedMemEnd, m.committedMemEnd) -
+            m.maxCommittedAndUnusedPages * allocationGranularity
+    if d > 0:
+      let commitSizeShrinkage = nextAlignedOffset(d, allocationGranularity)
+      let newCommitEnd = m.committedMemEnd.shift(-commitSizeShrinkage)
+
+      when defined(windows):
+        check virtualFree(newCommitEnd, commitSizeShrinkage, MEM_DECOMMIT)
+      else:
+        check posix_madvise(newCommitEnd, commitSizeShrinkage,
+                            POSIX_MADV_DONTNEED) == 0
+
+      m.committedMemEnd = newCommitEnd
+
+proc init*(SeqType: type ReservedMemSeq,
+           maxLen: Natural,
+           initLen: Natural = 0,
+           initCommitLen: Natural = 0,
+           memStart = pointer(nil),
+           accessFlags = memReadWrite,
+           maxCommittedAndUnusedPages = 3): SeqType =
+
+  let elemSize = sizeof(SeqType.T)
+  result.mem = ReservedMem.init(maxLen * elemSize,
+                                initLen * elemSize,
+                                initCommitLen * elemSize,
+                                memStart, accessFlags,
+                                maxCommittedAndUnusedPages)
+
+func `[]`*[T](s: ReservedMemSeq[T], pos: Natural): lent T =
+  let elemAddr = s.mem.memStart.shift(pos * sizeof(T))
+  rangeCheck elemAddr < s.mem.usedMemEnd
+  result = (cast[ptr T](elemAddr))[]
+
+func `[]`*[T](s: var ReservedMemSeq[T], pos: Natural): var T =
+  let elemAddr = s.mem.memStart.shift(pos * sizeof(T))
+  rangeCheck elemAddr < s.mem.usedMemEnd
+  result = (cast[ptr T](elemAddr))[]
+
+func `[]`*[T](s: ReservedMemSeq[T], rpos: BackwardsIndex): lent T =
+  return s[int(s.len) - int(rpos)]
+
+func `[]`*[T](s: var ReservedMemSeq[T], rpos: BackwardsIndex): var T =
+  return s[int(s.len) - int(rpos)]
+
+func len*[T](s: ReservedMemSeq[T]): int =
+  s.mem.len div sizeof(T)
+
+proc setLen*[T](s: var ReservedMemSeq[T], newLen: int) =
+  # TODO call destructors
+  s.mem.setLen(newLen * sizeof(T))
+
+proc add*[T](s: var ReservedMemSeq[T], val: T) =
+  let len = s.len
+  s.setLen(len + 1)
+  s[len] = val
+
+proc pop*[T](s: var ReservedMemSeq[T]): T =
+  assert s.usedMemEnd != s.memStart
+  let lastIdx = s.len - 1
+  result = s[lastIdx]
+  s.setLen(lastIdx)
+
+func commitedLen*[T](s: ReservedMemSeq[T]): int =
+  s.mem.commitedLen div sizeof(T)
+
+func maxLen*[T](s: ReservedMemSeq[T]): int =
+  s.mem.maxLen div sizeof(T)
+
diff --git a/lib/pure/strformat.nim b/lib/pure/strformat.nim
index f13eb5e8e..8623a43e0 100644
--- a/lib/pure/strformat.nim
+++ b/lib/pure/strformat.nim
@@ -285,7 +285,11 @@ macro `&`*(pattern: string): untyped =
   var i = 0
   let res = genSym(nskVar, "fmtRes")
   result = newNimNode(nnkStmtListExpr, lineInfoFrom=pattern)
-  result.add newVarStmt(res, newCall(bindSym"newStringOfCap", newLit(f.len + count(f, '{')*10)))
+  # XXX: https://github.com/nim-lang/Nim/issues/8405
+  # When compiling with -d:useNimRtl, certain procs such as `count` from the strutils
+  # module are not accessible at compile-time:
+  let expectedGrowth = when defined(useNimRtl): 0 else: count(f, '{') * 10
+  result.add newVarStmt(res, newCall(bindSym"newStringOfCap", newLit(f.len + expectedGrowth)))
   var strlit = ""
   while i < f.len:
     if f[i] == '{':
diff --git a/lib/pure/strtabs.nim b/lib/pure/strtabs.nim
index 36e4e376f..2568f83c2 100644
--- a/lib/pure/strtabs.nim
+++ b/lib/pure/strtabs.nim
@@ -143,51 +143,6 @@ template get(t: StringTableRef, key: string) =
     else:
       raise newException(KeyError, "key not found")
 
-proc `[]=`*(t: StringTableRef, key, val: string) {.
-  rtlFunc, extern: "nstPut", noSideEffect.}
-
-proc newStringTable*(mode: StringTableMode): StringTableRef {.
-  rtlFunc, extern: "nst$1".} =
-  ## Creates a new empty string table.
-  ##
-  ## See also:
-  ## * `newStringTable(keyValuePairs) proc
-  ##   <#newStringTable,varargs[tuple[string,string]],StringTableMode>`_
-  new(result)
-  result.mode = mode
-  result.counter = 0
-  newSeq(result.data, startSize)
-
-proc newStringTable*(keyValuePairs: varargs[string],
-                     mode: StringTableMode): StringTableRef {.
-  rtlFunc, extern: "nst$1WithPairs".} =
-  ## Creates a new string table with given `key, value` string pairs.
-  ##
-  ## `StringTableMode` must be specified.
-  runnableExamples:
-    var mytab = newStringTable("key1", "val1", "key2", "val2",
-                               modeCaseInsensitive)
-
-  result = newStringTable(mode)
-  var i = 0
-  while i < high(keyValuePairs):
-    result[keyValuePairs[i]] = keyValuePairs[i + 1]
-    inc(i, 2)
-
-proc newStringTable*(keyValuePairs: varargs[tuple[key, val: string]],
-                     mode: StringTableMode = modeCaseSensitive): StringTableRef {.
-  rtlFunc, extern: "nst$1WithTableConstr".} =
-  ## Creates a new string table with given `(key, value)` tuple pairs.
-  ##
-  ## The default mode is case sensitive.
-  runnableExamples:
-    var
-      mytab1 = newStringTable({"key1": "val1", "key2": "val2"}, modeCaseInsensitive)
-      mytab2 = newStringTable([("key3", "val3"), ("key4", "val4")])
-
-  result = newStringTable(mode)
-  for key, val in items(keyValuePairs): result[key] = val
-
 proc len*(t: StringTableRef): int {.rtlFunc, extern: "nst$1".} =
   ## Returns the number of keys in `t`.
   result = t.counter
@@ -292,6 +247,48 @@ proc `[]=`*(t: StringTableRef, key, val: string) {.
     rawInsert(t, t.data, key, val)
     inc(t.counter)
 
+proc newStringTable*(mode: StringTableMode): StringTableRef {.
+  rtlFunc, extern: "nst$1".} =
+  ## Creates a new empty string table.
+  ##
+  ## See also:
+  ## * `newStringTable(keyValuePairs) proc
+  ##   <#newStringTable,varargs[tuple[string,string]],StringTableMode>`_
+  new(result)
+  result.mode = mode
+  result.counter = 0
+  newSeq(result.data, startSize)
+
+proc newStringTable*(keyValuePairs: varargs[string],
+                     mode: StringTableMode): StringTableRef {.
+  rtlFunc, extern: "nst$1WithPairs".} =
+  ## Creates a new string table with given `key, value` string pairs.
+  ##
+  ## `StringTableMode` must be specified.
+  runnableExamples:
+    var mytab = newStringTable("key1", "val1", "key2", "val2",
+                               modeCaseInsensitive)
+
+  result = newStringTable(mode)
+  var i = 0
+  while i < high(keyValuePairs):
+    result[keyValuePairs[i]] = keyValuePairs[i + 1]
+    inc(i, 2)
+
+proc newStringTable*(keyValuePairs: varargs[tuple[key, val: string]],
+                     mode: StringTableMode = modeCaseSensitive): StringTableRef {.
+  rtlFunc, extern: "nst$1WithTableConstr".} =
+  ## Creates a new string table with given `(key, value)` tuple pairs.
+  ##
+  ## The default mode is case sensitive.
+  runnableExamples:
+    var
+      mytab1 = newStringTable({"key1": "val1", "key2": "val2"}, modeCaseInsensitive)
+      mytab2 = newStringTable([("key3", "val3"), ("key4", "val4")])
+
+  result = newStringTable(mode)
+  for key, val in items(keyValuePairs): result[key] = val
+
 proc raiseFormatException(s: string) =
   var e: ref ValueError
   new(e)
diff --git a/lib/system.nim b/lib/system.nim
index 441b7e263..eac13aeb3 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -417,6 +417,12 @@ when not defined(niminheritable):
   {.pragma: inheritable.}
 when not defined(nimunion):
   {.pragma: unchecked.}
+when not defined(nimHasHotCodeReloading):
+  {.pragma: nonReloadable.}
+when defined(hotCodeReloading):
+  {.pragma: hcrInline, inline.}
+else:
+  {.pragma: hcrInline.}
 
 # comparison operators:
 proc `==`*[Enum: enum](x, y: Enum): bool {.magic: "EqEnum", noSideEffect.}
@@ -1590,7 +1596,7 @@ when defined(nodejs) and not defined(nimscript):
   var programResult* {.importc: "process.exitCode".}: int
   programResult = 0
 else:
-  var programResult* {.exportc: "nim_program_result".}: int
+  var programResult* {.compilerproc, exportc: "nim_program_result".}: int
     ## modify this variable to specify the exit code of the program
     ## under normal circumstances. When the program is terminated
     ## prematurely using ``quit``, this value is ignored.
@@ -3802,6 +3808,15 @@ template doAssert*(cond: untyped, msg = "") =
   const expr = astToStr(cond)
   assertImpl(cond, msg, expr, true)
 
+when compileOption("rangechecks"):
+  template rangeCheck*(cond) =
+    ## Helper for performing user-defined range checks.
+    ## Such checks will be performed only when the ``rangechecks``
+    ## compile-time option is enabled.
+    if not cond: sysFatal(RangeError, "range check failed")
+else:
+  template rangeCheck*(cond) = discard
+
 iterator items*[T](a: seq[T]): T {.inline.} =
   ## iterates over each item of `a`.
   var i = 0
@@ -4251,3 +4266,6 @@ export widestrs
 
 import system/io
 export io
+
+when not defined(createNimHcr):
+  include nimhcr
diff --git a/lib/system/cgprocs.nim b/lib/system/cgprocs.nim
index 72219c2b7..9d0d248c3 100644
--- a/lib/system/cgprocs.nim
+++ b/lib/system/cgprocs.nim
@@ -13,8 +13,8 @@ type
   LibHandle = pointer       # private type
   ProcAddr = pointer        # library loading and loading of procs:
 
-proc nimLoadLibrary(path: string): LibHandle {.compilerproc.}
-proc nimUnloadLibrary(lib: LibHandle) {.compilerproc.}
-proc nimGetProcAddr(lib: LibHandle, name: cstring): ProcAddr {.compilerproc.}
+proc nimLoadLibrary(path: string): LibHandle {.compilerproc, hcrInline, nonReloadable.}
+proc nimUnloadLibrary(lib: LibHandle) {.compilerproc, hcrInline, nonReloadable.}
+proc nimGetProcAddr(lib: LibHandle, name: cstring): ProcAddr {.compilerproc, hcrInline, nonReloadable.}
 
-proc nimLoadLibraryError(path: string) {.compilerproc, noinline.}
+proc nimLoadLibraryError(path: string) {.compilerproc, hcrInline, nonReloadable.}
diff --git a/lib/system/chcks.nim b/lib/system/chcks.nim
index 0840d863a..789d709f7 100644
--- a/lib/system/chcks.nim
+++ b/lib/system/chcks.nim
@@ -8,7 +8,7 @@
 #
 
 # Implementation of some runtime checks.
-import system/indexerrors
+include system/indexerrors
 
 proc raiseRangeError(val: BiggestInt) {.compilerproc, noinline.} =
   when hostOS == "standalone":
diff --git a/lib/system/dyncalls.nim b/lib/system/dyncalls.nim
index 528587d05..74bdd5372 100644
--- a/lib/system/dyncalls.nim
+++ b/lib/system/dyncalls.nim
@@ -33,7 +33,7 @@ proc nimLoadLibraryError(path: string) =
     discard MessageBoxA(0, msg[0].addr, nil, 0)
   quit(1)
 
-proc procAddrError(name: cstring) {.noinline.} =
+proc procAddrError(name: cstring) {.compilerproc, nonReloadable, hcrInline.} =
   # carefully written to avoid memory allocation:
   cstderr.rawWrite("could not import: ")
   cstderr.rawWrite(name)
diff --git a/lib/system/excpt.nim b/lib/system/excpt.nim
index 93fd693e0..cb2cda214 100644
--- a/lib/system/excpt.nim
+++ b/lib/system/excpt.nim
@@ -20,9 +20,9 @@ var
 proc c_fwrite(buf: pointer, size, n: csize, f: CFilePtr): cint {.
   importc: "fwrite", header: "<stdio.h>".}
 
-proc rawWrite(f: CFilePtr, s: string|cstring) =
+proc rawWrite(f: CFilePtr, s: cstring) {.compilerproc, nonreloadable, hcrInline.} =
   # we cannot throw an exception here!
-  discard c_fwrite(cstring(s), 1, s.len, f)
+  discard c_fwrite(s, 1, s.len, f)
 
 when not defined(windows) or not defined(guiapp):
   proc writeToStdErr(msg: cstring) = rawWrite(cstderr, msg)
diff --git a/lib/system/gc_common.nim b/lib/system/gc_common.nim
index 5af64ae20..91c0244ea 100644
--- a/lib/system/gc_common.nim
+++ b/lib/system/gc_common.nim
@@ -446,10 +446,10 @@ proc deallocHeap*(runFinalizers = true; allowGcAfterwards = true) =
 type
   GlobalMarkerProc = proc () {.nimcall, benign.}
 var
-  globalMarkersLen: int
-  globalMarkers: array[0..3499, GlobalMarkerProc]
-  threadLocalMarkersLen: int
-  threadLocalMarkers: array[0..3499, GlobalMarkerProc]
+  globalMarkersLen {.exportc.}: int
+  globalMarkers {.exportc.}: array[0..3499, GlobalMarkerProc]
+  threadLocalMarkersLen {.exportc.}: int
+  threadLocalMarkers {.exportc.}: array[0..3499, GlobalMarkerProc]
   gHeapidGenerator: int
 
 proc nimRegisterGlobalMarker(markerProc: GlobalMarkerProc) {.compilerProc.} =
diff --git a/lib/system/memory.nim b/lib/system/memory.nim
index f86fd4696..318dffa2d 100644
--- a/lib/system/memory.nim
+++ b/lib/system/memory.nim
@@ -11,7 +11,7 @@ proc nimCopyMem(dest, source: pointer, size: Natural) {.compilerproc, inline.} =
       d[i] = s[i]
       inc i
 
-proc nimSetMem(a: pointer, v: cint, size: Natural) {.inline.} =
+proc nimSetMem(a: pointer, v: cint, size: Natural) {.nonReloadable, inline.} =
   when useLibC:
     c_memset(a, v, size)
   else:
@@ -22,7 +22,7 @@ proc nimSetMem(a: pointer, v: cint, size: Natural) {.inline.} =
       a[i] = v
       inc i
 
-proc nimZeroMem(p: pointer, size: Natural) {.compilerproc, inline.} =
+proc nimZeroMem(p: pointer, size: Natural) {.compilerproc, nonReloadable, inline.} =
   nimSetMem(p, 0, size)
 
 proc nimCmpMem(a, b: pointer, size: Natural): cint {.compilerproc, inline.} =
@@ -37,7 +37,7 @@ proc nimCmpMem(a, b: pointer, size: Natural): cint {.compilerproc, inline.} =
       if d != 0: return d
       inc i
 
-proc nimCStrLen(a: cstring): csize {.compilerproc, inline.} =
+proc nimCStrLen(a: cstring): csize {.compilerproc, nonReloadable, inline.} =
   when useLibC:
     c_strlen(a)
   else:
diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim
index 33cd4415f..9ba432459 100644
--- a/lib/system/sysstr.nim
+++ b/lib/system/sysstr.nim
@@ -82,7 +82,7 @@ proc copyStr(s: NimString, start: int): NimString {.compilerProc.} =
   if s == nil: return nil
   result = copyStrLast(s, start, s.len-1)
 
-proc nimToCStringConv(s: NimString): cstring {.compilerProc, inline.} =
+proc nimToCStringConv(s: NimString): cstring {.compilerProc, nonReloadable, inline.} =
   if s == nil or s.len == 0: result = cstring""
   else: result = cstring(addr s.data)
 
diff --git a/lib/windows/winlean.nim b/lib/windows/winlean.nim
index d1bfbd447..49d65c251 100644
--- a/lib/windows/winlean.nim
+++ b/lib/windows/winlean.nim
@@ -683,6 +683,10 @@ const
   FILE_BEGIN* = 0'i32
   INVALID_SET_FILE_POINTER* = -1'i32
   NO_ERROR* = 0'i32
+  PAGE_NOACCESS* = 0x01'i32
+  PAGE_EXECUTE* = 0x10'i32
+  PAGE_EXECUTE_READ* = 0x20'i32
+  PAGE_EXECUTE_READWRITE* = 0x40'i32
   PAGE_READONLY* = 2'i32
   PAGE_READWRITE* = 4'i32
   FILE_MAP_READ* = 4'i32