https://github.com/akkartik/mu/blob/master/034address.cc
  1 //: Addresses help us spend less time copying data around.
  2 
  3 //: So far we've been operating on primitives like numbers and characters, and
  4 //: we've started combining these primitives together into larger logical
  5 //: units (containers or arrays) that may contain many different primitives at
  6 //: once. Containers and arrays can grow quite large in complex programs, and
  7 //: we'd like some way to efficiently share them between recipes without
  8 //: constantly having to make copies. Right now 'next-ingredient' and 'return'
  9 //: copy data across recipe boundaries. To avoid copying large quantities of
 10 //: data around, we'll use *addresses*. An address is a bookmark to some
 11 //: arbitrary quantity of data (the *payload*). It's a primitive, so it's as
 12 //: efficient to copy as a number. To read or modify the payload 'pointed to'
 13 //: by an address, we'll perform a *lookup*.
 14 //:
 15 //: The notion of 'lookup' isn't an instruction like 'add' or 'subtract'.
 16 //: Instead it's an operation that can be performed when reading any of the
 17 //: ingredients of an instruction, and when writing to any of the products. To
 18 //: write to the payload of an ingredient rather than its value, simply add
 19 //: the /lookup property to it. Modern computers provide efficient support for
 20 //: addresses and lookups, making this a realistic feature.
 21 //:
 22 //: To create addresses and allocate memory exclusively for their use, use
 23 //: 'new'. Memory is a finite resource so if the computer can't satisfy your
 24 //: request, 'new' may return a 0 (null) address.
 25 //:
 26 //: Computers these days have lots of memory so in practice we can often
 27 //: assume we'll never run out. If you start running out however, say in a
 28 //: long-running program, you'll need to switch mental gears and start
 29 //: husbanding our memory more carefully. The most important tool to avoid
 30 //: wasting memory is to 'abandon' an address when you don't need it anymore.
 31 //: That frees up the memory allocated to it to be reused in future calls to
 32 //: 'new'.
 33 
 34 //: Since memory can be reused multiple times, it can happen that you have a
 35 //: stale copy to an address that has since been abandoned and reused. Using
 36 //: the stale address is almost never safe, but it can be very hard to track
 37 //: down such copies because any errors caused by them may occur even millions
 38 //: of instructions after the copy or abandon instruction. To help track down
 39 //: such issues, Mu tracks an 'alloc id' for each allocation it makes. The
 40 //: first call to 'new' has an alloc id of 1, the second gets 2, and so on.
 41 //: The alloc id is never reused.
 42 :(before "End Globals")
 43 long long Next_alloc_id = 0;
 44 :(before "End Reset")
 45 Next_alloc_id = 0;
 46 
 47 //: The 'new' instruction records alloc ids both in the memory being allocated
 48 //: and *also* in the address. The 'abandon' instruction clears alloc ids in
 49 //: both places as well. Tracking alloc ids in this manner allows us to raise
 50 //: errors about stale addresses much earlier: 'lookup' operations always
 51 //: compare alloc ids between the address and its payload.
 52 
 53 //: todo: give 'new' a custodian ingredient. Following malloc/free is a temporary hack.
 54 
 55 :(scenario new)
 56 # call 'new' two times with identical types without modifying the results; you
 57 # should get back different results
 58 def main [
 59   10:&:num <- new num:type
 60   12:&:num <- new num:type
 61   20:bool <- equal 10:&:num, 12:&:num
 62 ]
 63 +mem: storing 1000 in location 11
 64 +mem: storing 0 in location 20
 65 
 66 :(scenario new_array)
 67 # call 'new' with a second ingredient to allocate an array of some type rather than a single copy
 68 def main [
 69   10:&:@:num <- new num:type, 5
 70   12:&:num <- new num:type
 71   20:num/alloc2, 21:num/alloc1 <- deaddress 10:&:@:num, 12:&:num
 72   30:num <- subtract 21:num/alloc2, 20:num/alloc1
 73 ]
 74 +run: {10: ("address" "array" "number")} <- new {num: "type"}, {5: "literal"}
 75 +mem: array length is 5
 76 # skip alloc id in allocation
 77 +mem: storing 1000 in location 11
 78 # don't forget the extra locations for alloc id and array length
 79 +mem: storing 7 in location 30
 80 
 81 :(scenario dilated_reagent_with_new)
 82 def main [
 83   10:&:&:num <- new {(& num): type}
 84 ]
 85 +new: size of '(& num)' is 2
 86 
 87 //: 'new' takes a weird 'type' as its first ingredient; don't error on it
 88 :(before "End Mu Types Initialization")
 89 put(Type_ordinal, "type", 0);
 90 :(code)
 91 bool is_mu_type_literal(const reagent& r) {
 92   return is_literal(r) && r.type && r.type->name == "type";
 93 }
 94 
 95 :(before "End Primitive Recipe Declarations")
 96 NEW,
 97 :(before "End Primitive Recipe Numbers")
 98 put(Recipe_ordinal, "new", NEW);
 99 :(before "End Primitive Recipe Checks")
100 case NEW: {
101   const recipe& caller = get(Recipe, r);
102   if (inst.ingredients.empty() || SIZE(inst.ingredients) > 2) {
103     raise << maybe(caller.name) << "'new' requires one or two ingredients, but got '" << to_original_string(inst) << "'\n" << end();
104     break;
105   }
106   // End NEW Check Special-cases
107   const reagent& type = inst.ingredients.at(0);
108   if (!is_mu_type_literal(type)) {
109     raise << maybe(caller.name) << "first ingredient of 'new' should be a type, but got '" << type.original_string << "'\n" << end();
110     break;
111   }
112   if (SIZE(inst.ingredients) > 1 && !is_mu_number(inst.ingredients.at(1))) {
113     raise << maybe(caller.name) << "second ingredient of 'new' should be a number (array length), but got '" << type.original_string << "'\n" << end();
114     break;
115   }
116   if (inst.products.empty()) {
117     raise << maybe(caller.name) << "result of 'new' should never be ignored\n" << end();
118     break;
119   }
120   if (!product_of_new_is_valid(inst)) {
121     raise << maybe(caller.name) << "product of 'new' has incorrect type: '" << to_original_string(inst) << "'\n" << end();
122     break;
123   }
124   break;
125 }
126 :(code)
127 bool product_of_new_is_valid(const instruction& inst) {
128   reagent/*copy*/ product = inst.products.at(0);
129   // Update NEW product in Check
130   if (!product.type || product.type->atom || product.type->left->value != Address_type_ordinal)
131     return false;
132   drop_from_type(product, "address");
133   if (SIZE(inst.ingredients) > 1) {
134     // array allocation
135     if (!product.type || product.type->atom || product.type->left->value != Array_type_ordinal)
136       return false;
137     drop_from_type(product, "array");
138   }
139   reagent/*local*/ expected_product(new_type_tree(inst.ingredients.at(0).name));
140   return types_strictly_match(product, expected_product);
141 }
142 
143 void drop_from_type(reagent& r, string expected_type) {
144   assert(!r.type->atom);
145   if (r.type->left->name != expected_type) {
146     raise << "can't drop2 " << expected_type << " from '" << to_string(r) << "'\n" << end();
147     return;
148   }
149   // r.type = r.type->right
150   type_tree* tmp = r.type;
151   r.type = tmp->right;
152   tmp->right = NULL;
153   delete tmp;
154   // if (!r.type->right) r.type = r.type->left
155   assert(!r.type->atom);
156   if (r.type->right) return;
157   tmp = r.type;
158   r.type = tmp->left;
159   tmp->left = NULL;
160   delete tmp;
161 }
162 
163 :(scenario new_returns_incorrect_type)
164 % Hide_errors = true;
165 def main [
166   1:bool <- new num:type
167 ]
168 +error: main: product of 'new' has incorrect type: '1:bool <- new num:type'
169 
170 :(scenario new_discerns_singleton_list_from_atom_container)
171 % Hide_errors = true;
172 def main [
173   1:&:num <- new {(num): type}  # should be '{num: type}'
174 ]
175 +error: main: product of 'new' has incorrect type: '1:&:num <- new {(num): type}'
176 
177 :(scenario new_with_type_abbreviation)
178 def main [
179   1:&:num <- new num:type
180 ]
181 $error: 0
182 
183 :(scenario new_with_type_abbreviation_inside_compound)
184 def main [
185   {1: (address address number), raw: ()} <- new {(& num): type}
186 ]
187 $error: 0
188 
189 :(scenario equal_result_of_new_with_null)
190 def main [
191   1:&:num <- new num:type
192   10:bool <- equal 1:&:num, null
193 ]
194 +mem: storing 0 in location 10
195 
196 //: To implement 'new', a Mu transform turns all 'new' instructions into
197 //: 'allocate' instructions that precompute the amount of memory they want to
198 //: allocate.
199 
200 //: Ensure that we never call 'allocate' directly, and that there's no 'new'
201 //: instructions left after the transforms have run.
202 :(before "End Primitive Recipe Checks")
203 case ALLOCATE: {
204   raise << "never call 'allocate' directly'; always use 'new'\n" << end();
205   break;
206 }
207 :(before "End Primitive Recipe Implementations")
208 case NEW: {
209   raise << "no implementation for 'new'; why wasn't it translated to 'allocate'? Please save a copy of your program and send it to Kartik.\n" << end();
210   break;
211 }
212 
213 :(after "Transform.push_back(check_instruction)")  // check_instruction will guard against direct 'allocate' instructions below
214 Transform.push_back(transform_new_to_allocate);  // idempotent
215 
216 :(code)
217 void transform_new_to_allocate(const recipe_ordinal r) {
218   trace(9991, "transform") << *str = p_contact_create_display_string(contact, "__prof_default");

    assert_string_equal("bob", str);

    p_contact_free(contact);
    free(str);
}

void contact_presence_offline(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
        "is offline", FALSE);

    const char *presence = p_contact_presence(contact);

    assert_string_equal("offline", presence);

    p_contact_free(contact);
}

void contact_presence_uses_highest_priority(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
        "is offline", FALSE);

    Resource *resource10 = resource_new("resource10", RESOURCE_ONLINE, NULL, 10);
    Resource *resource20 = resource_new("resource20", RESOURCE_CHAT, NULL, 20);
    Resource *resource30 = resource_new("resource30", RESOURCE_AWAY, NULL, 30);
    Resource *resource1 = resource_new("resource1", RESOURCE_XA, NULL, 1);
    Resource *resource2 = resource_new("resource2", RESOURCE_DND, NULL, 2);
    p_contact_set_presence(contact, resource10);
    p_contact_set_presence(contact, resource20);
    p_contact_set_presence(contact, resource30);
    p_contact_set_presence(contact, resource1);
    p_contact_set_presence(contact, resource2);

    const char *presence = p_contact_presence(contact);

    assert_string_equal("away", presence);

    p_contact_free(contact);
}

void contact_presence_chat_when_same_prioroty(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
        "is offline", FALSE);

    Resource *resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 10);
    Resource *resource_chat = resource_new("resource_chat", RESOURCE_CHAT, NULL, 10);
    Resource *resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
    Resource *resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
    Resource *resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
    p_contact_set_presence(contact, resource_online);
    p_contact_set_presence(contact, resource_chat);
    p_contact_set_presence(contact, resource_away);
    p_contact_set_presence(contact, resource_xa);
    p_contact_set_presence(contact, resource_dnd);

    const char *presence = p_contact_presence(contact);

    assert_string_equal("chat", presence);

    p_contact_free(contact);
}

void contact_presence_online_when_same_prioroty(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
        "is offline", FALSE);

    Resource *resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 10);
    Resource *resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
    Resource *resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
    Resource *resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
    p_contact_set_presence(contact, resource_online);
    p_contact_set_presence(contact, resource_away);
    p_contact_set_presence(contact, resource_xa);
    p_contact_set_presence(contact, resource_dnd);

    const char *presence = p_contact_presence(contact);

    assert_string_equal("online", presence);

    p_contact_free(contact);
}

void contact_presence_away_when_same_prioroty(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
        "is offline", FALSE);

    Resource *resource_away = resource_new("resource_away", RESOURCE_AWAY, NULL, 10);
    Resource *resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
    Resource *resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
    p_contact_set_presence(contact, resource_away);
    p_contact_set_presence(contact, resource_xa);
    p_contact_set_presence(contact, resource_dnd);

    const char *presence = p_contact_presence(contact);

    assert_string_equal("away", presence);

    p_contact_free(contact);
}

void contact_presence_xa_when_same_prioroty(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
        "is offline", FALSE);

    Resource *resource_xa = resource_new("resource_xa", RESOURCE_XA, NULL, 10);
    Resource *resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
    p_contact_set_presence(contact, resource_xa);
    p_contact_set_presence(contact, resource_dnd);

    const char *presence = p_contact_presence(contact);

    assert_string_equal("xa", presence);

    p_contact_free(contact);
}

void contact_presence_dnd(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
        "is offline", FALSE);

    Resource *resource_dnd = resource_new("resource_dnd", RESOURCE_DND, NULL, 10);
    p_contact_set_presence(contact, resource_dnd);

    const char *presence = p_contact_presence(contact);

    assert_string_equal("dnd", presence);

    p_contact_free(contact);
}

void contact_subscribed_when_to(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, "to",
        "is offline", FALSE);

    gboolean result = p_contact_subscribed(contact);

    assert_true(result);

    p_contact_free(contact);
}

void contact_subscribed_when_both(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, "both",
        "is offline", FALSE);

    gboolean result = p_contact_subscribed(contact);

    assert_true(result);

    p_contact_free(contact);
}

void contact_not_subscribed_when_from(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, "from",
        "is offline", FALSE);

    gboolean result = p_contact_subscribed(contact);

    assert_false(result);

    p_contact_free(contact);
}

void contact_not_subscribed_when_no_subscription_value(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
        "is offline", FALSE);

    gboolean result = p_contact_subscribed(contact);

    assert_false(result);

    p_contact_free(contact);
}

void contact_not_available(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
        "is offline", FALSE);

    gboolean result = p_contact_is_available(contact);

    assert_false(result);

    p_contact_free(contact);
}

void contact_not_available_when_highest_priority_away(void **state)
{
    PContact contact = p_contact_new("bob@server.com", "bob", NULL, NULL,
        "is offline", FALSE);

    Resource *resource_online = resource_new("resource_online", RESOURCE_ONLINE, NULL, 10);
    Resource *resource_chat = resource_new("resource_chat", RESOURCE_CHAT, NULL, 10);