//: Transform to maintain multiple variants of a recipe depending on the //: number and types of the ingredients and products. Allows us to use nice //: names like 'print' or 'length' in many mutually extensible ways. :(scenario static_dispatch) recipe main [ 7:number/raw <- test 3 ] recipe test a:number -> z:number [ z <- copy 1 ] recipe test a:number, b:number -> z:number [ z <- copy 2 ] +mem: storing 1 in location 7 //: When loading recipes, accumulate variants if headers don't collide, and //: raise a warning if headers collide. :(before "End Globals") map > Recipe_variants; :(before "End One-time Setup") put(Recipe_variants, "main", vector()); // since we manually added main to Recipe_ordinal :(before "End Setup") for (map >::iterator p = Recipe_variants.begin(); p != Recipe_variants.end(); ++p) { for (long long int i = 0; i < SIZE(p->second); ++i) { if (p->second.at(i) >= Reserved_for_tests) p->second.at(i) = -1; // just leave a ghost } } :(before "End Load Recipe Header(result)") if (contains_key(Recipe_ordinal, result.name)) { const recipe_ordinal r = get(Recipe_ordinal, result.name); if ((!contains_key(Recipe, r) || get(Recipe, r).has_header) && !variant_already_exists(result)) { string new_name = next_unused_recipe_name(result.name); put(Recipe_ordinal, new_name, Next_recipe_ordinal++); get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, new_name)); result.name = new_name; } } else { // save first variant put(Recipe_ordinal, result.name, Next_recipe_ordinal++); get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, result.name)); } :(code) bool variant_already_exists(const recipe& rr) { const vector& variants = get_or_insert(Recipe_variants, rr.name); for (long long int i = 0; i < SIZE(variants); ++i) { if (contains_key(Recipe, variants.at(i)) && all_reagents_match(rr, get(Recipe, variants.at(i)))) { return true; } } return false; } bool all_reagents_match(const recipe& r1, const recipe& r2) { if (SIZE(r1.ingredients) != SIZE(r2.ingredients)) return false; if (SIZE(r1.products) != SIZE(r2.products)) return false; for (long long int i = 0; i < SIZE(r1.ingredients); ++i) { if (!deeply_equal_types(r1.ingredients.at(i).properties.at(0).second, r2.ingredients.at(i).properties.at(0).second)) { return false; } } for (long long int i = 0; i < SIZE(r1.products); ++i) { if (!deeply_equal_types(r1.products.at(i).properties.at(0).second, r2.products.at(i).properties.at(0).second)) { return false; } } return true; } :(before "End Globals") set Literal_type_names; :(before "End One-time Setup") Literal_type_names.insert("number"); Literal_type_names.insert("character"); :(code) bool deeply_equal_types(const string_tree* a, const string_tree* b) { if (!a) return !b; if (!b) return !a; if (a->value == "literal" && b->value == "literal") return true; if (a->value == "literal") return Literal_type_names.find(b->value) != Literal_type_names.end(); if (b->value == "literal") return Literal_type_names.find(a->value) != Literal_type_names.end(); return a->value == b->value && deeply_equal_types(a->left, b->left) && deeply_equal_types(a->right, b->right); } string next_unused_recipe_name(const string& recipe_name) { for (long long int i = 2; ; ++i) { ostringstream out; out << recipe_name << '_' << i; if (!contains_key(Recipe_ordinal, out.str())) return out.str(); } } //: Once all the recipes are loaded, transform their bodies to replace each //: call with the most suitable variant. :(scenario static_dispatch_picks_most_similar_variant) recipe main [ 7:number/raw <- test 3, 4, 5 ] recipe test a:number -> z:number [ z <- copy 1 ] recipe test a:number, b:number -> z:number [ z <- copy 2 ] +mem: storing 2 in location 7 //: after filling in all missing types (because we'll be introducing 'blank' types in this transform in a later layer, for shape-shifting recipes) :(after "End Type Modifying Transforms") Transform.push_back(resolve_ambiguous_calls); // idempotent :(code) void resolve_ambiguous_calls(recipe_ordinal r) { recipe& caller_recipe = get(Recipe, r); trace(9991, "transform") << "--- resolve ambiguous calls for recipe " << caller_recipe.name << end(); //? cerr << "--- resolve ambiguous calls for recipe " << caller_recipe.name << '\n'; for (long long int index = 0; index < SIZE(caller_recipe.steps); ++index) { instruction& inst = caller_recipe.steps.at(index); if (inst.is_label) continue; if (get_or_insert(Recipe_variants, inst.name).empty()) continue; replace_best_variant(inst, caller_recipe); } } void replace_best_variant(instruction& inst, const recipe& caller_recipe) { trace(9992, "transform") << "instruction " << inst.name << end(); vector& variants = get(Recipe_variants, inst.name); //? trace(9992, "transform") << "checking base: " << get(Recipe_ordinal, inst.name) << end(); long long int best_score = variant_score(inst, get(Recipe_ordinal, inst.name)); trace(9992, "transform") << "score for base: " << best_score << end(); for (long long int i = 0; i < SIZE(variants); ++i) { //? trace(9992, "transform") << "checking variant " << i << ": " << variants.at(i) << end(); long long int current_score = variant_score(inst, variants.at(i)); trace(9992, "transform") << "score for variant " << i << ": " << current_score << end(); if (current_score > best_sco
/*
 *
 *           Nim's Runtime Library
 *       (c) Copyright 2017 Emery Hemingway
 *
 *   See the file "copying.txt", included in this
 *   distribution, for details about the copyright.
 *
 */

#ifndef _GENODE_CPP__SYSLOCKS_H_
#define _GENODE_CPP__SYSLOCKS_H_

/* Genode includes */
#include <base/semaphore.h>
#include <base/mutex.h>

namespace Nim {
	struct SysLock;
	struct SysCond;
}

struct Nim::SysLock
{
	Genode::Mutex _mutex_a, _mutex_b;
	bool         _locked;

	void acquireSys()
	{
		Genode::Mutex::Guard guard(_mutex_a);
		_locked = true;
		_mutex_b.acquire();