1 //: Routines can be put in a 'waiting' state, from which it will be ready to
  2 //: run again when a specific memory location changes its value. This is Mu's
  3 //: basic technique for orchestrating the order in which different routines
  4 //: operate.
  5 
  6 :(scenario wait_for_location)
  7 def f1 [
  8   10:num <- copy 34
  9   start-running f2
 10   20:location <- copy 10/unsafe
 11   wait-for-reset-then-set 20:location
 12   # wait for f2 to run and reset location 1
 13   30:num <- copy 10:num
 14 ]
 15 def f2 [
 16   10:location <- copy 0/unsafe
 17 ]
 18 +schedule: f1
 19 +run: waiting for location 10 to reset
 20 +schedule: f2
 21 +schedule: waking up routine 1
 22 +schedule: f1
 23 +mem: storing 1 in location 30
 24 
 25 //: define the new state that all routines can be in
 26 
 27 :(before "End routine States")
 28 WAITING,
 29 :(before "End routine Fields")
 30 // only if state == WAITING
 31 int waiting_on_location;
 32 :(before "End routine Constructor")
 33 waiting_on_location = 0;
 34 
 35 :(before "End Mu Test Teardown")
 36 if (Passed && any_routines_waiting())
 37   raise << Current_scenario->name << ": deadlock!\n" << end();
 38 :(before "End Run Routine")
 39 if (any_routines_waiting()) {
 40   raise << "deadlock!\n" << end();
 41   dump_waiting_routines();
 42 }
 43 :(before "End Test Teardown")
 44 if (Passed && any_routines_with_error())
 45   raise << "some routines died with errors\n" << end();
 46 :(code)
 47 bool any_routines_waiting() {
 48   for (int i = 0;  i < SIZE(Routines);  ++i) {
 49   ¦ if (Routines.at(i)->state == WAITING)
 50   ¦ ¦ return true;
 51   }
 52   return false;
 53 }
 54 void dump_waiting_routines() {
 55   for (int i = 0;  i < SIZE(Routines);  ++i) {
 56   ¦ if (Routines.at(i)->state == WAITING)
 57   ¦ ¦ cerr << i << ": " << routine_label(Routines.at(i)) << '\n';
 58   }
 59 }
 60 
 61 :(scenario wait_for_location_can_deadlock)
 62 % Hide_errors = true;
 63 def main [
 64   10:num <- copy 1
 65   20:location <- copy 10/unsafe
 66   wait-for-reset-then-set 20:location
 67 ]
 68 +error: deadlock!
 69 
 70 //: Primitive recipe to put routines in that state.
 71 //: This primitive is also known elsewhere as compare-and-set (CAS). Used to
 72 //: build locks.
 73 
 74 :(before "End Primitive Recipe Declarations")
 75 WAIT_FOR_RESET_THEN_SET,
 76 :(before "End Primitive Recipe Numbers")
 77 put(Recipe_ordinal, "wait-for-reset-then-set", WAIT_FOR_RESET_THEN_SET);
 78 :(before "End Primitive Recipe Checks")
 79 case WAIT_FOR_RESET_THEN_SET: {
 80   if (SIZE(inst.ingredients) != 1) {
 81   ¦ raise << maybe(get(Recipe, r).name) << "'wait-for-reset-then-set' requires exactly one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
 82   ¦ break;
 83   }
 84   if (!is_mu_location(inst.ingredients.at(0))) {
 85   ¦ raise << maybe(get(Recipe, r).name) << "'wait-for-reset-then-set' requires a location ingredient, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
 86   }
 87   break;
 88 }
 89 :(before "End Primitive Recipe Implementations")
 90 case WAIT_FOR_RESET_THEN_SET: {
 91   int loc = static_cast<int>(ingredients.at(0).at(0));
 92   trace(9998, "run") << "wait: *" << loc << " = " << get_or_insert(Memory, loc) << end();
 93   if (get_or_insert(Memory, loc) == 0) {
 94   ¦ trace(9998, "run") << "location " << loc << " is already 0; setting" << end();
 95   ¦ put(Memory, loc, 1);
 96   ¦ break;
 97   }
 98   trace(9998, "run") << "waiting for location " << loc << " to reset" << end();
 99   Current_routine->state = WAITING;
100   Current_routine->waiting_on_location = loc;
101   break;
102 }
103 
104 //: Counterpart to unlock a lock.
105 :(before "End Primitive Recipe Declarations")
106 RESET,
107 :(before "End Primitive Recipe Numbers")
108 put(Recipe_ordinal, "reset", RESET);
109 :(before "End Primitive Recipe Checks")
110 case RESET: {
111   if (SIZE(inst.ingredients) != 1) {
112   ¦ raise << maybe(get(Recipe, r).name) << "'reset' requires exactly one ingredient, but got '" << to_original_string(inst) << "'\n" << end();
113   ¦ break;
114   }
115   if (!is_mu_location(inst.ingredients.at(0))) {
116   ¦ raise << maybe(get(Recipe, r).name) << "'reset' requires a location ingredient, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
117   }
118   break;
119 }
120 :(before "End Primitive Recipe Implementations")
121 case RESET: {
122   int loc = static_cast<int>(ingredients.at(0).at(0));
123   put(Memory, loc, 0);
124   trace(9998, "run") << "reset: *" << loc << " = " << get_or_insert(Memory, loc) << end();
125   break;
126 }
127 
128 //: scheduler tweak to get routines out of that state
129 
130 :(before "End Scheduler State Transitions")
131 for (int i = 0;  i < SIZE(Routines);  ++i) {
132   if (Routines.at(i)->state != WAITING) continue;
133   int loc = Routines.at(i)->waiting_on_location;
134   if (loc && get_or_insert(Memory, loc) == 0) {
135   ¦ trace(9999, "schedule") << "waking up routine " << Routines.at(i)->id << end();
136   ¦ put(Memory, loc, 1);
137   ¦ Routines.at(i)->state = RUNNING;
138   ¦ Routines.at(i)->waiting_on_location = 0;
139   }
140 }
141 
142 //: Primitive to help compute locations to wait on.
143 //: Only supports elements immediately inside containers; no arrays or
144 //: containers within containers yet.
145 
146 :(scenario get_location)
147 def main [
148   12:num <- copy 34
149   13:num <- copy 35
150   15:location <- get-location 12:point, 1:offset
151 ]
152 +mem: storing 13 in location 15
153 
154 :(before "End Primitive Recipe Declarations")
155 GET_LOCATION,
156 :(before "End Primitive Recipe Numbers")
157 put(Recipe_ordinal, "get-location", GET_LOCATION);
158 :(before "End Primitive Recipe Checks")
159 case GET_LOCATION: {
160   if (SIZE(inst.ingredients) != 2) {
161   ¦ raise << maybe(get(Recipe, r).name) << "'get-location' expects exactly 2 ingredients in '" << to_original_string(inst) << "'\n" << end();
162   ¦ break;
163   }
164   reagent/*copy*/ base = inst.ingredients.at(0);
165   if (!canonize_type(base)) break;
166   if (!base.type) {
167   ¦ raise << maybe(get(Recipe, r).name) << "first ingredient of 'get-location' should be a container, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
168   ¦ break;
169   }
170   const type_tree* base_root_type = base.type->atom ? base.type : base.type->left;
171   if (!base_root_type->atom || base_root_type->value == 0 || !contains_key(Type, base_root_type->value) || get(Type, base_root_type->value).kind != CONTAINER) {
172   ¦ raise << maybe(get(Recipe, r).name) << "first ingredient of 'get-location' should be a container, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
173   ¦ break;
174   }
175   type_ordinal base_type = base.type->value;
176   const reagent& offset = inst.ingredients.at(1);
177   if (!is_literal(offset) || !is_mu_scalar(offset)) {
178   ¦ raise << maybe(get(Recipe, r).name) << "second ingredient of 'get-location' should have type 'offset', but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
179   ¦ break;
180   }
181   int offset_value = 0;
182   //: later layers will permit non-integer offsets
183   if (is_integer(offset.name)) {
184   ¦ offset_value = to_integer(offset.name);
185   ¦ if (offset_value < 0 || offset_value >= SIZE(get(Type, base_type).elements)) {
186   ¦ ¦ raise << maybe(get(Recipe, r).name) << "invalid offset " << offset_value << " for '" << get(Type, base_type).name << "'\n" << end();
187   ¦ ¦ break;
188   ¦ }
189   }
190   else {
191   ¦ offset_value = offset.value;
192   }
193   if (inst.products.empty()) break;
194   if (!is_mu_location(inst.products.at(0))) {
195   ¦ raise << maybe(get(Recipe, r).name) << "'get-location " << base.original_string << ", " << offset.original_string << "' should write to type location but '" << inst.products.at(0).name << "' has type '" << names_to_string_without_quotes(inst.products.at(0).type) << "'\n" << end();
196   ¦ break;
197   }
198   break;
199 }
200 :(before "End Primitive Recipe Implementations")
201 case GET_LOCATION: {
202   reagent/*copy*/ base = current_instruction().ingredients.at(0);
203   canonize(base);
204   int base_address = base.value;
205   if (base_address == 0) {
206   ¦ raise << maybe(current_recipe_name()) << "tried to access location 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
207   ¦ break;
208   }
209   const type_tree* base_type = get_base_type(base.type);
210   int offset = ingredients.at(1).at(0);
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 072slice.subx</title>
<meta name="Generator" content="Vim/8.1">
<meta name="plugin-version" content="vim8.1_v1">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal-light">
<style type="text/css">
<!--
pre { font-family: monospace; color: #000000; background-color: #c6c6c6; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #c6c6c6; }
a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.subxComment { color: #005faf; }
.subxS2Comment { color: #8a8a8a; }
.Folded { color: #080808; background-color: #949494; }
.LineNr { }
.subxS1Comment { color: #0000af; }
.subxFunction { color: #af5f00; text-decoration: underline; }
.Normal { color: #000000; background-color: #c6c6c6; padding-bottom: 1px; }
.subxH1Comment { color: #005faf; text-decoration: underline; }
.Constant { color: #008787; }
.subxTest { color: #5f8700; }
-->
</style>

<script type='text/javascript'>
<!--

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  var lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'>
<a href='https://github.com/akkartik/mu/blob/master/072slice.subx'>https://github.com/akkartik/mu/blob/master/072slice.subx</a>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr">   1 </span><span class="subxComment"># new data structure: a slice is an open interval of addresses [start, end)</span>
<span id="L2" class="LineNr">   2 </span><span class="subxComment"># that includes 'start' but not 'end'</span>
<span id="L3" class="LineNr">   3 </span>
<span id="L4" class="LineNr">   4 </span>== code
<span id="L5" class="LineNr">   5 </span><span class="subxComment">#   instruction                     effective address                                                   register    displacement    immediate</span>
<span id="L6" class="LineNr">   6 </span><span class="subxS1Comment"># . op          subop               mod             rm32          base        index         scale       r32</span>
<span id="L7" class="LineNr">   7 </span><span class="subxS1Comment"># . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes</span>
<span id="L8" class="LineNr">   8 </span>
<span id="L9" class="LineNr">   9 </span><span class="subxFunction">slice-empty?</span>:  <span class="subxComment"># s : (addr slice) -&gt; eax : boolean</span>
<span id="L10" class="LineNr">  10 </span>    <span class="subxS1Comment"># . prologue</span>
<span id="L11" class="LineNr">  11 </span>    55/push-ebp
<span id="L12" class="LineNr">  12 </span>    89/copy                         3/mod/direct    5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          4/r32/esp  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy esp to ebp</span>
<span id="L13" class="LineNr">  13 </span>    <span class="subxS1Comment"># . save registers</span>
<span id="L14" class="LineNr">  14 </span>    51/push-ecx
<span id="L15" class="LineNr">  15 </span>    <span class="subxComment"># ecx = s</span>
<span id="L16" class="LineNr">  16 </span>    8b/copy                         1/mod/*+disp8   5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          1/r32/ecx   8/disp8        <span class="Normal"> . </span>                <span class="subxComment"># copy *(ebp+8) to ecx</span>
<span id="L17" class="LineNr">  17 </span>    <span class="subxComment"># if (s-&gt;start &gt;= s-&gt;end) return true</span>
<span id="L18" class="LineNr">  18 </span>    <span class="subxS1Comment"># . eax = s-&gt;start</span>
<span id="L19" class="LineNr">  19 </span>    8b/copy                         0/mod/indirect  1/rm32/ecx   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          0/r32/eax  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy *ecx to eax</span>
<span id="L20" class="LineNr">  20 </span>    <span class="subxS1Comment"># . if (eax &gt;= s-&gt;end) return true</span>
<span id="L21" class="LineNr">  21 </span>    3b/compare                      1/mod/*+disp8   1/rm32/ecx   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          0/r32/eax   4/disp8        <span class="Normal"> . </span>                <span class="subxComment"># compare eax with *(ecx+4)</span>
<span id="L22" class="LineNr">  22 </span>    b8/copy-to-eax  1/imm32/true
<span id="L23" class="LineNr">  23 </span>    73/jump-if-addr&gt;=  $slice-empty?:end/disp8
<span id="L24" class="LineNr">  24 </span>    b8/copy-to-eax  0/imm32/false
<span id="L25" class="LineNr">  25 </span><span class="Constant">$slice-empty?:end</span>:
<span id="L26" class="LineNr">  26 </span>    <span class="subxS1Comment"># . restore registers</span>
<span id="L27" class="LineNr">  27 </span>    59/pop-to-ecx
<span id="L28" class="LineNr">  28 </span>    <span class="subxS1Comment"># . epilogue</span>
<span id="L29" class="LineNr">  29 </span>    89/copy                         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          5/r32/ebp  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy ebp to esp</span>
<span id="L30" class="LineNr">  30 </span>    5d/pop-to-ebp
<span id="L31" class="LineNr">  31 </span>    c3/return
<span id="L32" class="LineNr">  32 </span>
<span id="L33" class="LineNr">  33 </span><span class="subxTest">test-slice-empty-true</span>:
<span id="L34" class="LineNr">  34 </span>    <span class="subxS1Comment"># . prologue</span>
<span id="L35" class="LineNr">  35 </span>    55/push-ebp
<span id="L36" class="LineNr">  36 </span>    89/copy                         3/mod/direct    5/rm32/ebp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          4/r32/esp  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy esp to ebp</span>
<span id="L37" class="LineNr">  37 </span>    <span class="subxComment"># var slice/ecx : slice = {34, 34}</span>
<span id="L38" class="LineNr">  38 </span>    68/push  34/imm32/end
<span id="L39" class="LineNr">  39 </span>    68/push  34/imm32/start
<span id="L40" class="LineNr">  40 </span>    89/copy                         3/mod/direct    1/rm32/ecx   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          4/r32/esp  <span class="Normal"> . </span>             <span class="Normal"> . </span>                <span class="subxComment"># copy esp to ecx</span>
<span id="L41" class="LineNr">  41 </span>    <span class="subxComment"># slice-empty?(slice)</span>
<span id="L42" class="LineNr">  42 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L43" class="LineNr">  43 </span>    51/push-ecx
<span id="L44" class="LineNr">  44 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L45" class="LineNr">  45 </span>    e8/call  <a href='072slice.subx.html#L9'>slice-empty?</a>/disp32
<span id="L46" class="LineNr">  46 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L47" class="LineNr">  47 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              4/imm32           <span class="subxComment"># add to esp</span>
<span id="L48" class="LineNr">  48 </span>    <span class="subxComment"># check-ints-equal(eax, 1, msg)</span>
<span id="L49" class="LineNr">  49 </span>    <span class="subxS2Comment"># . . push args</span>
<span id="L50" class="LineNr">  50 </span>    68/push  <span class="Constant">&quot;F - test-slice-empty-true&quot;</span>/imm32
<span id="L51" class="LineNr">  51 </span>    68/push  1/imm32
<span id="L52" class="LineNr">  52 </span>    50/push-eax
<span id="L53" class="LineNr">  53 </span>    <span class="subxS2Comment"># . . call</span>
<span id="L54" class="LineNr">  54 </span>    e8/call  <a href='051test.subx.html#L24'>check-ints-equal</a>/disp32
<span id="L55" class="LineNr">  55 </span>    <span class="subxS2Comment"># . . discard args</span>
<span id="L56" class="LineNr">  56 </span>    81          0/subop/add         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>         <span class="Normal"> . </span>         <span class="Normal"> . </span>              0xc/imm32         <span class="subxComment"># add to esp</span>
<span id="L57" class="LineNr">  57 </span>    <span class="subxS1Comment"># . epilogue</span>
<span id="L58" class="LineNr">  58 </span>    89/copy                         3/mod/direct    4/rm32/esp   <span class="Normal"> . </span>         <span class="Normal"> . </span>           <span class="Normal"> . </span>          5/r32/ebp  <span