summary refs log tree commit diff stats
path: root/lib/system/gc_interface.nim
blob: 9183e6d48a575d95748d2708bf0ff56380d8b758 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# ----------------- GC interface ---------------------------------------------
const
  usesDestructors = defined(gcDestructors) or defined(gcHooks)

when not usesDestructors:
  {.pragma: nodestroy.}

when hasAlloc:
  type
    GC_Strategy* = enum  ## The strategy the GC should use for the application.
      gcThroughput,      ## optimize for throughput
      gcResponsiveness,  ## optimize for responsiveness (default)
      gcOptimizeTime,    ## optimize for speed
      gcOptimizeSpace    ## optimize for memory footprint

when hasAlloc and not defined(js) and not usesDestructors:
  proc GC_disable*() {.rtl, inl, benign.}
    ## Disables the GC. If called `n` times, `n` calls to `GC_enable`
    ## are needed to reactivate the GC.
    ##
    ## Note that in most circumstances one should only disable
    ## the mark and sweep phase with
    ## `GC_disableMarkAndSweep <#GC_disableMarkAndSweep>`_.

  proc GC_enable*() {.rtl, inl, benign.}
    ## Enables the GC again.

  proc GC_fullCollect*() {.rtl, benign.}
    ## Forces a full garbage collection pass.
    ## Ordinary code does not need to call this (and should not).

  proc GC_enableMarkAndSweep*() {.rtl, benign.}
  proc GC_disableMarkAndSweep*() {.rtl, benign.}
    ## The current implementation uses a reference counting garbage collector
    ## with a seldomly run mark and sweep phase to free cycles. The mark and
    ## sweep phase may take a long time and is not needed if the application
    ## does not create cycles. Thus the mark and sweep phase can be deactivated
    ## and activated separately from the rest of the GC.

  proc GC_getStatistics*(): string {.rtl, benign.}
    ## Returns an informative string about the GC's activity. This may be useful
    ## for tweaking.

  proc GC_ref*[T](x: ref T) {.magic: "GCref", benign.}
  proc GC_ref*[T](x: seq[T]) {.magic: "GCref", benign.}
  proc GC_ref*(x: string) {.magic: "GCref", benign.}
    ## Marks the object `x` as referenced, so that it will not be freed until
    ## it is unmarked via `GC_unref`.
    ## If called n-times for the same object `x`,
    ## n calls to `GC_unref` are needed to unmark `x`.

  proc GC_unref*[T](x: ref T) {.magic: "GCunref", benign.}
  proc GC_unref*[T](x: seq[T]) {.magic: "GCunref", benign.}
  proc GC_unref*(x: string) {.magic: "GCunref", benign.}
    ## See the documentation of `GC_ref <#GC_ref,string>`_.

  proc nimGC_setStackBottom*(theStackBottom: pointer) {.compilerRtl, noinline, benign.}
    ## Expands operating GC stack range to `theStackBottom`. Does nothing
      ## if current stack bottom is already lower than `theStackBottom`.

when hasAlloc and defined(js):
  template GC_disable* =
    {.warning: "GC_disable is a no-op in JavaScript".}

  template GC_enable* =
    {.warning: "GC_enable is a no-op in JavaScript".}

  template GC_fullCollect* =
    {.warning: "GC_fullCollect is a no-op in JavaScript".}

  template GC_setStrategy* =
    {.warning: "GC_setStrategy is a no-op in JavaScript".}

  template GC_enableMarkAndSweep* =
    {.warning: "GC_enableMarkAndSweep is a no-op in JavaScript".}

  template GC_disableMarkAndSweep* =
    {.warning: "GC_disableMarkAndSweep is a no-op in JavaScript".}

  template GC_ref*[T](x: ref T) =
    {.warning: "GC_ref is a no-op in JavaScript".}

  template GC_ref*[T](x: seq[T]) =
    {.warning: "GC_ref is a no-op in JavaScript".}

  template GC_ref*(x: string) =
    {.warning: "GC_ref is a no-op in JavaScript".}

  template GC_unref*[T](x: ref T) =
    {.warning: "GC_unref is a no-op in JavaScript".}

  template GC_unref*[T](x: seq[T]) =
    {.warning: "GC_unref is a no-op in JavaScript".}

  template GC_unref*(x: string) =
    {.warning: "GC_unref is a no-op in JavaScript".}

  template GC_getStatistics*(): string =
    {.warning: "GC_getStatistics is a no-op in JavaScript".}
    ""
an>.ingredients.at(in).name << '\n' << end(); } inst.ingredients.at(in).set_value(lookup_name(inst.ingredients.at(in), r)); } for (long long int out = 0; out < SIZE(inst.products); ++out) { if (is_numeric_location(inst.products.at(out))) numeric_locations_used = true; if (is_named_location(inst.products.at(out))) names_used = true; if (disqualified(inst.products.at(out), inst, Recipe[r].name)) continue; if (names.find(inst.products.at(out).name) == names.end()) { trace("name") << "assign " << inst.products.at(out).name << " " << curr_idx << end(); names[inst.products.at(out).name] = curr_idx; curr_idx += size_of(inst.products.at(out)); } inst.products.at(out).set_value(lookup_name(inst.products.at(out), r)); } } if (names_used && numeric_locations_used) raise_error << maybe(Recipe[r].name) << "mixing variable names and numeric addresses\n" << end(); } bool disqualified(/*mutable*/ reagent& x, const instruction& inst, const string& recipe_name) { if (!x.type) { raise_error << maybe(recipe_name) << "missing type for " << x.original_string << " in '" << inst.to_string() << "'\n" << end(); return true; } if (is_raw(x)) return true; if (is_literal(x)) return true; if (is_integer(x.name)) return true; // End Disqualified Reagents if (x.initialized) return true; return false; } bool already_transformed(const reagent& r, const map<string, long long int>& names) { return names.find(r.name) != names.end(); } long long int lookup_name(const reagent& r, const recipe_ordinal default_recipe) { return Name[default_recipe][r.name]; } type_ordinal skip_addresses(type_tree* type, const string& recipe_name) { for (; type; type = type->right) { if (type->value != Type_ordinal["address"]) return type->value; } raise_error << maybe(recipe_name) << "expected a container" << '\n' << end(); return -1; } int find_element_name(const type_ordinal t, const string& name, const string& recipe_name) { const type_info& container = Type[t]; for (long long int i = 0; i < SIZE(container.element_names); ++i) { if (container.element_names.at(i) == name) return i; } raise_error << maybe(recipe_name) << "unknown element " << name << " in container " << Type[t].name << '\n' << end(); return -1; } bool is_numeric_location(const reagent& x) { if (is_literal(x)) return false; if (is_raw(x)) return false; if (x.name == "0") return false; // used for chaining lexical scopes return is_integer(x.name); } bool is_named_location(const reagent& x) { if (is_literal(x)) return false; if (is_raw(x)) return false; if (is_special_name(x.name)) return false; return !is_integer(x.name); } bool is_special_name(const string& s) { if (s == "_") return true; if (s == "0") return true; // End is_special_name Cases return false; } :(scenario transform_names_passes_dummy) # _ is just a dummy result that never gets consumed recipe main [ _, x:number <- copy 0, 1 ] +name: assign x 1 -name: assign _ 1 //: an escape hatch to suppress name conversion that we'll use later :(scenarios run) :(scenario transform_names_passes_raw) % Hide_errors = true; recipe main [ x:number/raw <- copy 0 ] -name: assign x 1 +error: can't write to location 0 in 'x:number/raw <- copy 0' :(scenarios transform) :(scenario transform_names_fails_when_mixing_names_and_numeric_locations) % Hide_errors = true; recipe main [ x:number <- copy 1:number ] +error: main: mixing variable names and numeric addresses :(scenario transform_names_fails_when_mixing_names_and_numeric_locations_2) % Hide_errors = true; recipe main [ x:number <- copy 1 1:number <- copy x:number ] +error: main: mixing variable names and numeric addresses :(scenario transform_names_does_not_fail_when_mixing_names_and_raw_locations) % Hide_errors = true; recipe main [ x:number <- copy 1:number/raw ] -error: main: mixing variable names and numeric addresses $error: 0 :(scenario transform_names_does_not_fail_when_mixing_names_and_literals) % Hide_errors = true; recipe main [ x:number <- copy 1 ] -error: main: mixing variable names and numeric addresses $error: 0 //:: Support element names for containers in 'get' and 'get-address'. //: update our running example container for the next test :(before "End Mu Types Initialization") Type[point].element_names.push_back("x"); Type[point].element_names.push_back("y"); :(scenario transform_names_transforms_container_elements) recipe main [ p:address:point <- copy 0 # unsafe a:number <- get *p:address:point, y:offset b:number <- get *p:address:point, x:offset ] +name: element y of type point is at offset 1 +name: element x of type point is at offset 0 :(after "Per-recipe Transforms") // replace element names of containers with offsets if (inst.operation == Recipe_ordinal["get"] || inst.operation == Recipe_ordinal["get-address"]) { if (SIZE(inst.ingredients) != 2) { raise_error << maybe(Recipe[r].name) << "exactly 2 ingredients expected in '" << inst.to_string() << "'\n" << end(); break; } if (!is_literal(inst.ingredients.at(1))) raise_error << maybe(Recipe[r].name) << "expected ingredient 1 of " << (inst.operation == Recipe_ordinal["get"] ? "'get'" : "'get-address'") << " to have type 'offset'; got " << inst.ingredients.at(1).original_string << '\n' << end(); if (inst.ingredients.at(1).name.find_first_not_of("0123456789") != string::npos) { // since first non-address in base type must be a container, we don't have to canonize type_ordinal base_type = skip_addresses(inst.ingredients.at(0).type, Recipe[r].name); inst.ingredients.at(1).set_value(find_element_name(base_type, inst.ingredients.at(1).name, Recipe[r].name)); trace("name") << "element " << inst.ingredients.at(1).name << " of type " << Type[base_type].name << " is at offset " << no_scientific(inst.ingredients.at(1).value) << end(); } } //: this test is actually illegal so can't call run :(scenarios transform) :(scenario transform_names_handles_containers) recipe main [ a:point <- copy 0/raw b:number <- copy 0/raw ] +name: assign a 1 +name: assign b 3 //:: Support variant names for exclusive containers in 'maybe-convert'. :(scenarios run) :(scenario transform_names_handles_exclusive_containers) recipe main [ 12:number <- copy 1 13:number <- copy 35 14:number <- copy 36 20:address:point <- maybe-convert 12:number-or-point/raw, p:variant # unsafe ] +name: variant p of type number-or-point has tag 1 +mem: storing 13 in location 20 :(after "Per-recipe Transforms") // convert variant names of exclusive containers if (inst.operation == Recipe_ordinal["maybe-convert"]) { if (SIZE(inst.ingredients) != 2) { raise_error << maybe(Recipe[r].name) << "exactly 2 ingredients expected in '" << inst.to_string() << "'\n" << end(); break; } assert(is_literal(inst.ingredients.at(1))); if (inst.ingredients.at(1).name.find_first_not_of("0123456789") != string::npos) { // since first non-address in base type must be an exclusive container, we don't have to canonize type_ordinal base_type = skip_addresses(inst.ingredients.at(0).type, Recipe[r].name); inst.ingredients.at(1).set_value(find_element_name(base_type, inst.ingredients.at(1).name, Recipe[r].name)); trace("name") << "variant " << inst.ingredients.at(1).name << " of type " << Type[base_type].name << " has tag " << no_scientific(inst.ingredients.at(1).value) << end(); } }