1 //: Advanced notation for the common/easy case where a recipe takes some fixed
  2 //: number of ingredients and yields some fixed number of products.
  3 
  4 :(scenario recipe_with_header)
  5 def main [
  6   1:num/raw <- add2 3, 5
  7 ]
  8 def add2 x:num, y:num -> z:num [
  9   local-scope
 10   load-ingredients
 11   z:num <- add x, y
 12   return z
 13 ]
 14 +mem: storing 8 in location 1
 15 
 16 //: When loading recipes save any header.
 17 
 18 :(before "End recipe Fields")
 19 bool has_header;
 20 vector<reagent> ingredients;
 21 vector<reagent> products;
 22 :(before "End recipe Constructor")
 23 has_header = false;
 24 
 25 :(before "End Recipe Refinements")
 26 if (in.peek() != '[') {
 27   trace(9999, "parse") << "recipe has a header; parsing" << end();
 28   load_recipe_header(in, result);
 29 }
 30 
 31 :(code)
 32 void load_recipe_header(istream& in, recipe& result) {
 33   result.has_header = true;
 34   while (has_data(in) && in.peek() != '[' && in.peek() != '\n') {
 35   ¦ string s = next_word(in);
 36   ¦ if (s.empty()) {
 37   ¦ ¦ assert(!has_data(in));
 38   ¦ ¦ raise << "incomplete recipe header at end of file (0)\n" << end();
 39   ¦ ¦ return;
 40   ¦ }
 41   ¦ if (s == "<-")
 42   ¦ ¦ raise << "recipe " << result.name << " should say '->' and not '<-'\n" << end();
 43   ¦ if (s == "->") break;
 44   ¦ result.ingredients.push_back(reagent(s));
 45   ¦ trace(9999, "parse") << "header ingredient: " << result.ingredients.back().original_string << end();
 46   ¦ skip_whitespace_but_not_newline(in);
 47   }
 48   while (has_data(in) && in.peek() != '[' && in.peek() != '\n') {
 49   ¦ string s = next_word(in);
 50   ¦ if (s.empty()) {
 51   ¦ ¦ assert(!has_data(in));
 52   ¦ ¦ raise << "incomplete recipe header at end of file (1)\n" << end();
 53   ¦ ¦ return;
 54   ¦ }
 55   ¦ result.products.push_back(reagent(s));
 56   ¦ trace(9999, "parse") << "header product: " << result.products.back().original_string << end();
 57   ¦ skip_whitespace_but_not_newline(in);
 58   }
 59   // End Load Recipe Header(result)
 60 }
 61 
 62 :(scenario recipe_handles_stray_comma)
 63 def main [
 64   1:num/raw <- add2 3, 5
 65 ]
 66 def add2 x:num, y:num -> z:num, [
 67   local-scope
 68   load-ingredients
 69   z:num <- add x, y
 70   return z
 71 ]
 72 +mem: storing 8 in location 1
 73 
 74 :(scenario recipe_handles_stray_comma_2)
 75 def main [
 76   foo
 77 ]
 78 def foo, [
 79   1:num/raw <- add 2, 2
 80 ]
 81 def bar [
 82   1:num/raw <- add 2, 3
 83 ]
 84 +mem: storing 4 in location 1
 85 
 86 :(scenario recipe_handles_wrong_arrow)
 87 % Hide_errors = true;
 88 def foo a:num <- b:num [
 89 ]
 90 +error: recipe foo should say '->' and not '<-'
 91 
 92 :(scenario recipe_handles_missing_bracket)
 93 % Hide_errors = true;
 94 def main
 95 ]
 96 +error: main: recipe body must begin with '['
 97 
 98 :(scenario recipe_handles_missing_bracket_2)
 99 % Hide_errors = true;
100 def main
101   local-scope
102   {
103   }
104 ]
105 # doesn't overflow line when reading header
106 -parse: header ingredient: local-scope
107 +error: main: recipe body must begin with '['
108 
109 :(scenario recipe_handles_missing_bracket_3)
110 % Hide_errors = true;
111 def main  # comment
112   local-scope
113   {
114   }
115 ]
116 # doesn't overflow line when reading header
117 -parse: header ingredient: local-scope
118 +error: main: recipe body must begin with '['
119 
120 :(after "Begin debug_string(recipe x)")
121 out << "ingredients:\n";
122 for (int i = 0;  i < SIZE(x.ingredients);  ++i)
123   out << "  " << debug_string(x.ingredients.at(i)) << '\n';
124 out << "products:\n";
125 for (int i = 0;  i < SIZE(x.products);  ++i)
126   out << "  " << debug_string(x.products.at(i)) << '\n';
127 
128 //: If a recipe never mentions any ingredients or products, assume it has a header.
129 
130 :(scenario recipe_without_ingredients_or_products_has_header)
131 def test [
132   1:num <- copy 34
133 ]
134 +parse: recipe test has a header
135 
136 :(before "End Recipe Body(result)")
137 if (!result.has_header) {
138   result.has_header = true;
139   for (int i = 0;  i < SIZE(result.steps);  ++i) {
140   ¦ const instruction& inst = result.steps.at(i);
141   ¦ if ((inst.name == "reply" && !inst.ingredients.empty())
142   ¦ ¦ ¦ || (inst.name == "return" && !inst.ingredients.empty())
143   ¦ ¦ ¦ || inst.name == "next-ingredient"
144   ¦ ¦ ¦ || inst.name == "ingredient"
145   ¦ ¦ ¦ || inst.name == "rewind-ingredients") {
146   ¦ ¦ result.has_header = false;
147   ¦ ¦ break;
148   ¦ }
149   }
150 }
151 if (result.has_header) {
152   trace(9999, "parse") << "recipe " << result.name << " has a header" << end();
153 }
154 
155 //: Support type abbreviations in headers.
156 
157 :(scenario type_abbreviations_in_recipe_headers)
158 def main [
159   local-scope
160   a:text <- foo
161   1:char/raw <- index *a, 0
162 ]
163 def foo -> a:text [  # 'text' is an abbreviation
164   local-scope
165   load-ingredients
166   a <- new [abc]
167 ]
168 +mem: storing 97 in location 1
169 
170 :(before "End Expand Type Abbreviations(caller)")
171 for (long int i = 0;  i < SIZE(caller.ingredients);  ++i)
172   expand_type_abbreviations(caller.ingredients.at(i).type);
173 for (long int i = 0;  i < SIZE(caller.products);  ++i)
174   expand_type_abbreviations(caller.products.at(i).type);
175 
176 //: Rewrite 'load-ingredients' to instructions to create all reagents in the header.
177 
178 :(before "End Rewrite Instruction(curr, recipe result)")
179 if (curr.name == "load-ingredients") {
180   curr.clear();
181   recipe_ordinal op = get(Recipe_ordinal, "next-ingredient-without-typechecking");
182   for (int i = 0;  i < SIZE(result.ingredients);  ++i) {
183   ¦ curr.operation = op;
184   ¦ curr.name = "next-ingredient-without-typechecking";
185   ¦ curr.products.push_back(result.ingredients.at(i));
186   ¦ result.steps.push_back(curr);
187   ¦ curr.clear();
188   }
189 }
190 if (curr.name == "next-ingredient-without-typechecking") {
191   raise << maybe(result.name) << "never call 'next-ingredient-without-typechecking' directly\n" << end();
192   curr.clear();
193 }
194 
195 //: internal version of next-ingredient; don't call this directly
196 :(before "End Primitive Recipe Declarations")
197 NEXT_INGREDIENT_WITHOUT_TYPECHECKING,
198 :(before "End Primitive Recipe Numbers")
199 put(Recipe_ordinal, "next-ingredient-without-typechecking", NEXT_INGREDIENT_WITHOUT_TYPECHECKING);
200 :(before "End Primitive Recipe Checks")
201 case NEXT_INGREDIENT_WITHOUT_TYPECHECKING: {
202   break;
203 }
204 :(before "End Primitive Recipe Implementations")
205 case NEXT_INGREDIENT_WITHOUT_TYPECHECKING: {
206   assert(!Current_routine->calls.empty());
207   if (current_call().next_ingredient_to_process < SIZE(current_call().ingredient_atoms)) {
208   ¦ products.push_back(
209   ¦ ¦ ¦ current_call().ingredient_atoms.at(current_call().next_ingredient_to_process));
210   ¦ assert(SIZE(products) == 1);  products.resize(2);  // push a new vector
211   ¦ products.at(1).push_back(1);
212   ¦ ++current_call().next_ingredient_to_process;
213   }
214   else {
215   ¦ products.resize(2);
216   ¦ // pad the first product with sufficient zeros to match its type
217   ¦ products.at(0).resize(size_of(current_instruction().products.at(0)));
218   ¦ products.at(1).push_back(0);
219   }
220   break;
221 }
222 
223 //: more useful error messages if someone forgets 'load-ingredients'
224 
225 :(scenario load_ingredients_missing_error)
226 % Hide_errors = true;
227 def foo a:num [
228   local-scope
229   b:num <- add a:num, 1
230 ]
231 +error: foo: tried to read ingredient 'a' in 'b:num <- add a:num, 1' but it hasn't been written to yet
232 +error:   did you forget 'load-ingredients'?
233 
234 :(after "use-before-set Error")
235 if (is_present_in_ingredients(caller, ingredient.name))
236   raise << "  did you forget 'load-ingredients'?\n" << end();
237 
238 :(scenario load_ingredients_missing_error_2)
239 % Hide_errors = true;
240 def foo a:num [
241   local-scope
242   b:num <- add a, 1
243 ]
244 +error: foo: missing type for 'a' in 'b:num <- add a, 1'
245 +error:   did you forget 'load-ingredients'?
246 
247 :(after "missing-type Error 1")
248 if (is_present_in_ingredients(get(Recipe, get(Recipe_ordinal, recipe_name)), x.name))
249   raise << "  did you forget 'load-ingredients'?\n" << end();
250 
251 :(code)
252 bool is_present_in_ingredients(const recipe& callee, const string& ingredient_name) {
253   for (int i = 0;  i < SIZE(callee.ingredients);  ++i) {
254   ¦ if (callee.ingredients.at(i).name == ingredient_name)
255   ¦ ¦ return true;
256   }
257   return false;
258 }
259 
260 //:: Check all calls against headers.
261 
262 :(scenario show_clear_error_on_bad_call)
263 % Hide_errors = true;
264 def main [
265   1:num <- foo 34
266 ]
267 def foo x:point -> y:num [
268   local-scope
269   load-ingredients
270   return 35
271 ]
272 +error: main: ingredient 0 has the wrong type at '1:num <- foo 34'
273 
274 :(scenario show_clear_error_on_bad_call_2)
275 % Hide_errors = true;
276 def main [
277   1:point <- foo 34
278 ]
279 def foo x:num -> y:num [
280   local-scope
281   load-ingredients
282   return x
283 ]
284 +error: main: product 0 has the wrong type at '1:point <- foo 34'
285 
286 :(after "Transform.push_back(check_instruction)")
287 Transform.push_back(check_calls_against_header);  // idempotent
288 :(code)
289 void check_calls_against_header(const recipe_ordinal r) {
290   const recipe& caller = get(Recipe, r);
291   trace(9991, "transform") << "--- type-check calls inside recipe " << caller.name << end();
292   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
293   ¦ const instruction& inst = caller.steps.at(i);
294   ¦ if (inst.operation < MAX_PRIMITIVE_RECIPES) continue;
295   ¦ const recipe& callee = get(Recipe, inst.operation);
296   ¦ if (!callee.has_header) continue;
297   ¦ for (long int i = 0;  i < min(SIZE(inst.ingredients), SIZE(callee.ingredients));  ++i) {
298   ¦ ¦ // ingredients coerced from call to callee
299   ¦ ¦ if (!types_coercible(callee.ingredients.at(i), inst.ingredients.at(i)))
300   ¦ ¦ ¦ raise << maybe(caller.name) << "ingredient " << i << " has the wrong type at '" << inst.original_string << "'\n" << end();
301   ¦ }
302   ¦ for (long int i = 0;  i < min(SIZE(inst.products), SIZE(callee.products));  ++i) {
303   ¦ ¦ if (is_dummy(inst.products.at(i))) continue;
304   ¦ ¦ // products coerced from callee to call
305   ¦ ¦ if (!types_coercible(inst.products.at(i), callee.products.at(i)))
306   ¦ ¦ ¦ raise << maybe(caller.name) << "product " << i << " has the wrong type at '" << inst.original_string << "'\n" << end();
307   ¦ }
308   }
309 }
310 
311 //:: Check types going in and out of all recipes with headers.
312 
313 :(scenarios transform)
314 :(scenario recipe_headers_are_checked)
315 % Hide_errors = true;
316 def add2 x:num, y:num -> z:num [
317   local-scope
318   load-ingredients
319   z:&:num <- copy 0/unsafe
320   return z
321 ]
322 +error: add2: replied with the wrong type at 'return z'
323 
324 :(before "End Checks")
325 Transform.push_back(check_return_instructions_against_header);  // idempotent
326 
327 :(code)
328 void check_return_instructions_against_header(const recipe_ordinal r) {
329   const recipe& caller_recipe = get(Recipe, r);
330   if (!caller_recipe.has_header) return;
331   trace(9991, "transform") << "--- checking return instructions against header for " << caller_recipe.name << end();
332   for (int i = 0;  i < SIZE(caller_recipe.steps);  ++i) {
333   ¦ const instruction& inst = caller_recipe.steps.at(i);
334   ¦ if (inst.name != "reply" && inst.name != "return") continue;
335   ¦ if (SIZE(caller_recipe.products) != SIZE(inst.ingredients)) {
336   ¦ ¦ raise << maybe(caller_recipe.name) << "replied with the wrong number of products at '" << inst.original_string << "'\n" << end();
337   ¦ ¦ continue;
338   ¦ }
339   ¦ for (int i = 0;  i < SIZE(caller_recipe.products);  ++i) {
340   ¦ ¦ if (!types_match(caller_recipe.products.at(i), inst.ingredients.at(i)))
341   ¦ ¦ ¦ raise << maybe(caller_recipe.name) << "replied with the wrong type at '" << inst.original_string << "'\n" << end();
342   ¦ }
343   }
344 }
345 
346 :(scenario recipe_headers_are_checked_2)
347 % Hide_errors = true;
348 def add2 x:num, y:num [
349   local-scope
350   load-ingredients
351   z:&:num <- copy 0/unsafe
352   return z
353 ]
354 +error: add2: replied with the wrong number of products at 'return z'
355 
356 :(scenario recipe_headers_are_checked_against_transformed_instructions)
357 % Hide_errors = true;
358 def foo -> x:num [
359   local-scope
360   x:num <- copy 0
361   z:bool <- copy 0/false
362   return-if z, z
363 ]
364 +error: foo: replied with the wrong type at 'return-if z, z'
365 
366 :(scenario recipe_headers_check_for_duplicate_names)
367 % Hide_errors = true;
368 def foo x:num, x:num -> z:num [
369   local-scope
370   load-ingredients
371   return z
372 ]
373 +error: foo: 'x' can't repeat in the ingredients
374 
375 :(scenario recipe_headers_check_for_duplicate_names_2)
376 % Hide_errors = true;
377 def foo x:num, x:num [  # no result
378   local-scope
379   load-ingredients
380 ]
381 +error: foo: 'x' can't repeat in the ingredients
382 
383 :(scenario recipe_headers_check_for_missing_types)
384 % Hide_errors = true;
385 def main [
386   foo 0
387 ]
388 def foo a [  # no type for 'a'
389 ]
390 +error: foo: ingredient 'a' has no type
391 
392 :(before "End recipe Fields")
393 map<string, int> ingredient_index;
394 
395 :(after "Begin Instruction Modifying Transforms")
396 Transform.push_back(check_header_ingredients);  // idempotent
397 
398 :(code)
399 void check_header_ingredients(const recipe_ordinal r) {
400   recipe& caller_recipe = get(Recipe, r);
401   caller_recipe.ingredient_index.clear();
402   trace(9991, "transform") << "--- checking return instructions against header for " << caller_recipe.name << end();
403   for (int i = 0;  i < SIZE(caller_recipe.ingredients);  ++i) {
404   ¦ if (caller_recipe.ingredients.at(i).type == NULL)
405   ¦ ¦ raise << maybe(caller_recipe.name) << "ingredient '" << caller_recipe.ingredients.at(i).name << "' has no type\n" << end();
406   ¦ if (contains_key(caller_recipe.ingredient_index, caller_recipe.ingredients.at(i).name))
407   ¦ ¦ raise << maybe(caller_recipe.name) << "'" << caller_recipe.ingredients.at(i).name << "' can't repeat in the ingredients\n" << end();
408   ¦ put(caller_recipe.ingredient_index, caller_recipe.ingredients.at(i).name, i);
409   }
410 }
411 
412 //: Deduce types from the header if possible.
413 
414 :(scenarios run)
415 :(scenario deduce_instruction_types_from_recipe_header)
416 def main [
417   1:num/raw <- add2 3, 5
418 ]
419 def add2 x:num, y:num -> z:num [
420   local-scope
421   load-ingredients
422   z <- add x, y  # no type for z
423   return z
424 ]
425 +mem: storing 8 in location 1
426 
427 :(after "Begin Type Modifying Transforms")
428 Transform.push_back(deduce_types_from_header);  // idempotent
429 
430 :(code)
431 void deduce_types_from_header(const recipe_ordinal r) {
432   recipe& caller_recipe = get(Recipe, r);
433   if (caller_recipe.products.empty()) return;
434   trace(9991, "transform") << "--- deduce types from header for " << caller_recipe.name << end();
435   map<string, const type_tree*> header_type;
436   for (int i = 0;  i < SIZE(caller_recipe.ingredients);  ++i) {
437   ¦ if (!caller_recipe.ingredients.at(i).type) continue;  // error handled elsewhere
438   ¦ put(header_type, caller_recipe.ingredients.at(i).name, caller_recipe.ingredients.at(i).type);
439   ¦ trace(9993, "transform") << "type of " << caller_recipe.ingredients.at(i).name << " is " << names_to_string(caller_recipe.ingredients.at(i).type) << end();
440   }
441   for (int i = 0;  i < SIZE(caller_recipe.products);  ++i) {
442   ¦ if (!caller_recipe.products.at(i).type) continue;  // error handled elsewhere
443   ¦ put(header_type, caller_recipe.products.at(i).name, caller_recipe.products.at(i).type);
444   ¦ trace(9993, "transform") << "type of " << caller_recipe.products.at(i).name << " is " << names_to_string(caller_recipe.products.at(i).type) << end();
445   }
446   for (int i = 0;  i < SIZE(caller_recipe.steps);  ++i) {
447   ¦ instruction& inst = caller_recipe.steps.at(i);
448   ¦ trace(9992, "transform") << "instruction: " << to_string(inst) << end();
449   ¦ for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
450   ¦ ¦ if (inst.ingredients.at(i).type) continue;
451   ¦ ¦ if (header_type.find(inst.ingredients.at(i).name) == header_type.end())
452   ¦ ¦ ¦ continue;
453   ¦ ¦ if (!contains_key(header_type, inst.ingredients.at(i).name)) continue;  // error handled elsewhere
454   ¦ ¦ inst.ingredients.at(i).type = new type_tree(*get(header_type, inst.ingredients.at(i).name));
455   ¦ ¦ trace(9993, "transform") << "type of " << inst.ingredients.at(i).name <ss="p">>      <span class="Constant">&quot;run: storing 0x00000011\n&quot;</span>
<span id="L20" class="LineNr">  20 </span>  <span class="Delimiter">);</span>
<span id="L21" class="LineNr">  21 </span><span class="Delimiter">}</span>
<span id="L22" class="LineNr">  22 </span>
<span id="L23" class="LineNr">  23 </span><span class="Delimiter">:(before &quot;End Single-Byte Opcodes&quot;)</span>
<span id="L24" class="LineNr">  24 </span><span class="Normal">case</span> <span class="Constant">0x01</span>: <span class="Delimiter">{</span>  <span class="Comment">// add r32 to r/m32</span>
<span id="L25" class="LineNr">  25 </span>  <span class="Normal">uint8_t</span> modrm = <a href='010vm.cc.html#L338'>next</a><span class="Delimiter">();</span>
<span id="L26" class="LineNr">  26 </span>  <span class="Normal">uint8_t</span> arg2 = <span class="Delimiter">(</span>modrm&gt;&gt;<span class="Constant">3</span><span class="Delimiter">)</span>&amp;<span class="Constant">0x7</span><span class="Delimiter">;</span>
<span id="L27" class="LineNr">  27 </span>  <a href='003trace.cc.html#L96'>trace</a><span class="Delimiter">(</span><span class="SpecialChar">Callstack_depth</span>+<span class="Constant">1</span><span class="Delimiter">,</span> <span class="Constant">&quot;run&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;add &quot;</span> &lt;&lt; <a href='013direct_addressing.cc.html#L136'>rname</a><span class="Delimiter">(</span>arg2<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot; to r/m32&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
<span id="L28" class="LineNr">  28 </span>  <span class="Normal">int32_t</span>* signed_arg1 = <a href='013direct_addressing.cc.html#L101'>effective_address</a><span class="Delimiter">(</span>modrm<span class="Delimiter">);</span>
<span id="L29" class="LineNr">  29 </span>  <span class="Normal">int32_t</span> signed_result = *signed_arg1 + <span class="SpecialChar"><a href='010vm.cc.html#L25'>Reg</a></span>[arg2]<span class="Delimiter">.</span>i<span class="Delimiter">;</span>
<span id="L30" class="LineNr">  30 </span>  SF = <span class="Delimiter">(</span>signed_result &lt; <span class="Constant">0</span><span class="Delimiter">);</span>
<span id="L31" class="LineNr">  31 </span>  <a href='010vm.cc.html#L89'>ZF</a> = <span class="Delimiter">(</span>signed_result == <span class="Constant">0</span><span class="Delimiter">);</span>
<span id="L32" class="LineNr">  32 </span>  <span class="Normal">int64_t</span> signed_full_result = <span class="Normal">static_cast</span>&lt;<span class="Normal">int64_t</span>&gt;<span class="Delimiter">(</span>*signed_arg1<span class="Delimiter">)</span> + <span class="SpecialChar"><a href='010vm.cc.html#L25'>Reg</a></span>[arg2]<span class="Delimiter">.</span>i<span class="Delimiter">;</span>
<span id="L33" class="LineNr">  33 </span>  <a href='010vm.cc.html#L91'>OF</a> = <span class="Delimiter">(</span>signed_result != signed_full_result<span class="Delimiter">);</span>
<span id="L34" class="LineNr">  34 </span>  <span class="Comment">// set CF</span>
<span id="L35" class="LineNr">  35 </span>  <span class="Normal">uint32_t</span> unsigned_arg1 = <span class="Normal">static_cast</span>&lt;<span class="Normal">uint32_t</span>&gt;<span class="Delimiter">(</span>*signed_arg1<span class="Delimiter">);</span>
<span id="L36" class="LineNr">  36 </span>  <span class="Normal">uint32_t</span> unsigned_result = unsigned_arg1 + <span class="SpecialChar"><a href='010vm.cc.html#L25'>Reg</a></span>[arg2]<span class="Delimiter">.</span>u<span class="Delimiter">;</span>
<span id="L37" class="LineNr">  37 </span>  <span class="Normal">uint64_t</span> unsigned_full_result = <span class="Normal">static_cast</span>&lt;<span class="Normal">uint64_t</span>&gt;<span class="Delimiter">(</span>unsigned_arg1<span class="Delimiter">)</span> + <span class="SpecialChar"><a href='010vm.cc.html#L25'>Reg</a></span>[arg2]<span class="Delimiter">.</span>u<span class="Delimiter">;</span>
<span id="L38" class="LineNr">  38 </span>  <a href='010vm.cc.html#L90'>CF</a> = <span class="Delimiter">(</span>unsigned_result != unsigned_full_result<span class="Delimiter">);</span>
<span id="L39" class="LineNr">  39 </span>  <a href='003trace.cc.html#L96'>trace</a><span class="Delimiter">(</span><span class="SpecialChar">Callstack_depth</span>+<span class="Constant">1</span><span class="Delimiter">,</span> <span class="Constant">&quot;run&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;SF=&quot;</span> &lt;&lt; SF &lt;&lt; <span class="Constant">&quot;; ZF=&quot;</span>