1 //: Spaces help isolate recipes from each other. You can create them at will,
  2 //: and all addresses in arguments are implicitly based on the 'default-space'
  3 //: (unless they have the /raw property)
  4 //:
  5 //: Spaces are often called 'scopes' in other languages. Stack frames are a
  6 //: limited form of space that can't outlive callers.
  7 //:
  8 //: Warning: messing with 'default-space' can corrupt memory. Don't share
  9 //: default-space between recipes. Later we'll see how to chain spaces safely.
 10 //:
 11 //: Tests in this layer can write to a location as part of one type, and read
 12 //: it as part of another. This is unsafe and insecure, and we'll stop doing
 13 //: this once we switch to variable names.
 14 
 15 //: Under the hood, a space is an array of locations in memory.
 16 :(before "End Mu Types Initialization")
 17 put(Type_abbreviations, "space", new_type_tree("address:array:location"));
 18 
 19 :(scenario set_default_space)
 20 def main [
 21   # prepare default-space address
 22   10:num/alloc-id, 11:num <- copy 0, 1000
 23   # prepare default-space payload
 24   1000:num <- copy 0  # alloc id of payload
 25   1001:num <- copy 5  # length
 26   # actual start of this recipe
 27   default-space:space <- copy 10:&:@:location
 28   # if default-space is 1000, then:
 29   #   1000: alloc id
 30   #   1001: array size
 31   #   1002: location 0 (space for the chaining slot; described later; often unused)
 32 
/* See LICENSE file for copyright and license details. */

/* appearance */
#define BARPOS			BarTop /* BarBot, BarOff */
#define BORDERPX		1
#define FONT			"-*-terminus-medium-r-*-*-12-*-*-*-*-*-iso10646-*"
#define DARK /* LIGHT */
#ifdef DARK
#define NORMBORDERCOLOR		"#222"
#define NORMBGCOLOR		"#000"
#define NORMFGCOLOR		"#eee"
#define SELBORDERCOLOR		"#09f"
#else
#define NORMBORDERCOLOR		"#666"
#define NORMBGCOLOR		"#eee"
#define NORMFGCOLOR		"#222"
#define SELBORDERCOLOR		"#f00"
#endif
#define SELBGCOLOR		"#369"
#define SELFGCOLOR		"#fff"

/* tagging */
#define TAGS \
const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", NULL };
#define RULES \
static Rule rules[] = { \
	/* class:instance:title regex	tags regex	isfloating */ \
	{ "Firefox",			"3",		False }, \
	{ "Gimp",			NULL,		True }, \
	{ "MPlayer",			NULL,		True }, \
	{ "Acroread",			NULL,		True }, \
};

/* layout(s) */
#include "tile.h"
#define LAYOUTS \
static Layout layouts[] = { \
	/* symbol		function */ \
	{ "[]=",		tile }, /* first entry is default */ \
	{ "><>",		floating }, \
};
#define RESIZEHINTS		True	/* False - respect size hints in tiled resizals */
#define MWFACT			0.6	/* master width factor [0.1 .. 0.9] */
#define SNAP			32	/* snap pixel */

/* key definitions */
#define MODKEY			Mod1Mask
#define KEYS \
Key keys[] = { \
	/* modifier			key		function	argument */ \
	{ MODKEY,			XK_p,		spawn, \
		"exe=`dmenu_path | dmenu -fn '"FONT"' -nb '"NORMBGCOLOR"' -nf '"NORMFGCOLOR"'" \
		" -sb '"SELBGCOLOR"' -sf '"SELFGCOLOR"'` && exec $exe" }, \
	{ MODKEY|ShiftMask,		XK_Return,	spawn, \
		"exec xterm -bg '"NORMBGCOLOR"' -fg '"NORMFGCOLOR"' -cr '"NORMFGCOLOR"' +sb -fn '"FONT"'" }, \
	{ MODKEY,			XK_space,	setlayout,	NULL }, \
	{ MODKEY,			XK_b,		togglebar,	NULL }, \
	{ MODKEY,			XK_j,		focusnext,	NULL }, \
	{ MODKEY,			XK_k,		focusprev,	NULL }, \
	{ MODKEY,			XK_h,		setmwfact,	"-0.05" }, \
	{ MODKEY,			XK_l,		setmwfact,	"+0.05" }, \
	{ MODKEY,			XK_m,		togglemax,	NULL }, \
	{ MODKEY,			XK_Return,	zoom,		NULL }, \
	{ MODKEY|ShiftMask,		XK_space,	togglefloating,	NULL }, \
	{ MODKEY|ShiftMask,		XK_c,		killclient,	NULL }, \
	{ MODKEY,			XK_0,		view,		NULL }, \
	{ MODKEY,			XK_1,		view,		tags[0] }, \
	{ MODKEY,			XK_2,		view,		tags[1] }, \
	{ MODKEY,			XK_3,		view,		tags[2] }, \
	{ MODKEY,			XK_4,		view,		tags[3] }, \
	{ MODKEY,			XK_5,		view,		tags[4] }, \
	{ MODKEY,			XK_6,		view,		tags[5] }, \
	{ MODKEY,			XK_7,		view,		tags[6] }, \
	{ MODKEY,			XK_8,		view,		tags[7] }, \
	{ MODKEY,			XK_9,		view,		tags[8] }, \
	{ MODKEY|ControlMask,		XK_1,		toggleview,	tags[0] }, \
	{ MODKEY|ControlMask,		XK_2,		toggleview,	tags[1] }, \
	{ MODKEY|ControlMask,		XK_3,		toggleview,	tags[2] }, \
	{ MODKEY|ControlMask,		XK_4,		toggleview,	tags[3] }, \
	{ MODKEY|ControlMask,		XK_5,		toggleview,	tags[4] }, \
	{ MODKEY|ControlMask,		XK_6,		toggleview,	tags[5] }, \
	{ MODKEY|ControlMask,		XK_7,		toggleview,	tags[6] }, \
	{ MODKEY|ControlMask,		XK_8,		toggleview,	tags[7] }, \
	{ MODKEY|ControlMask,		XK_9,		toggleview,	tags[8] }, \
	{ MODKEY|ShiftMask,		XK_0,		tag,		NULL }, \
	{ MODKEY|ShiftMask,		XK_1,		tag,		tags[0] }, \
	{ MODKEY|ShiftMask,		XK_2,		tag,		tags[1] }, \
	{ MODKEY|ShiftMask,		XK_3,		tag,		tags[2] }, \
	{ MODKEY|ShiftMask,		XK_4,		tag,		tags[3] }, \
	{ MODKEY|ShiftMask,		XK_5,		tag,		tags[4] }, \
	{ MODKEY|ShiftMask,		XK_6,		tag,		tags[5] }, \
	{ MODKEY|ShiftMask,		XK_7,		tag,		tags[6] }, \
	{ MODKEY|ShiftMask,		XK_8,		tag,		tags[7] }, \
	{ MODKEY|ShiftMask,		XK_9,		tag,		tags[8] }, \
	{ MODKEY|ControlMask|ShiftMask,	XK_1,		toggletag,	tags[0] }, \
	{ MODKEY|ControlMask|ShiftMask,	XK_2,		toggletag,	tags[1] }, \
	{ MODKEY|ControlMask|ShiftMask,	XK_3,		toggletag,	tags[2] }, \
	{ MODKEY|ControlMask|ShiftMask,	XK_4,		toggletag,	tags[3] }, \
	{ MODKEY|ControlMask|ShiftMask,	XK_5,		toggletag,	tags[4] }, \
	{ MODKEY|ControlMask|ShiftMask,	XK_6,		toggletag,	tags[5] }, \
	{ MODKEY|ControlMask|ShiftMask,	XK_7,		toggletag,	tags[6] }, \
	{ MODKEY|ControlMask|ShiftMask,	XK_8,		toggletag,	tags[7] }, \
	{ MODKEY|ControlMask|ShiftMask,	XK_9,		toggletag,	tags[8] }, \
	{ MODKEY|ShiftMask,		XK_q,		quit,		NULL }, \
};
an id="L113" class="LineNr">113 } 114 115 //: reads and writes to the 'default-space' variable have special behavior 116 117 :(after "Begin Preprocess write_memory(x, data)") 118 if (x.name == "default-space") { 119 if (!is_mu_space(x)) 120 raise << maybe(current_recipe_name()) << "'default-space' should be of type address:array:location, but is " << to_string(x.type) << '\n' << end(); 121 if (SIZE(data) != 2) 122 raise << maybe(current_recipe_name()) << "'default-space' getting data from non-address\n" << end(); 123 current_call().default_space = data.at(/*skip alloc id*/1); 124 return; 125 } 126 :(code) 127 bool is_mu_space(reagent/*copy*/ x) { 128 canonize_type(x); 129 if (!is_compound_type_starting_with(x.type, "address")) return false; 130 drop_from_type(x, "address"); 131 if (!is_compound_type_starting_with(x.type, "array")) return false; 132 drop_from_type(x, "array"); 133 return x.type && x.type->atom && x.type->name == "location"; 134 } 135 136 :(scenario get_default_space) 137 def main [ 138 # prepare default-space address 139 10:num/alloc-id, 11:num <- copy 0, 1000 140 # prepare default-space payload 141 1000:num <- copy 0 # alloc id of payload 142 1001:num <- copy 5 # length 143 # actual start of this recipe 144 default-space:space <- copy 10:space 145 2:space/raw <- copy default-space:space 146 ] 147 +mem: storing 1000 in location 3 148 149 :(after "Begin Preprocess read_memory(x)") 150 if (x.name == "default-space") { 151 vector<double> result; 152 result.push_back(/*alloc id*/0); 153 result.push_back(current_call().default_space); 154 return result; 155 } 156 157 //:: fix 'get' 158 159 :(scenario lookup_sidesteps_default_space_in_get) 160 def main [ 161 # prepare default-space address 162 10:num/alloc-id, 11:num <- copy 0, 1000 163 # prepare default-space payload 164 1000:num <- copy 0 # alloc id of payload 165 1001:num <- copy 5 # length 166 # prepare payload outside the local scope 167 2000:num/alloc-id, 2001:num/x, 2002:num/y <- copy 0, 34, 35 168 # actual start of this recipe 169 default-space:space <- copy 10:space 170 # a local address 171 2:num, 3:num <- copy 0, 2000 172 3000:num/raw <- get *2:&:point, 1:offset 173 ] 174 +mem: storing 35 in location 3000 175 176 :(before "Read element" following "case GET:") 177 element.properties.push_back(pair<string, string_tree*>("raw", NULL)); 178 179 //:: fix 'index' 180 181 :(scenario lookup_sidesteps_default_space_in_index) 182 def main [ 183 # prepare default-space address 184 10:num/alloc-id, 11:num <- copy 0, 1000 185 # prepare default-space payload 186 1000:num <- copy 0 # alloc id of payload 187 1001:num <- copy 5 # length 188 # prepare an array address 189 20:num/alloc-id, 21:num <- copy 0, 2000 190 # prepare an array payload 191 2000:num/alloc-id, 2001:num/length, 2002:num/index:0, 2003:num/index:1 <- copy 0, 2, 34, 35 192 # actual start of this recipe 193 default-space:space <- copy 10:&:@:location 194 1:&:@:num <- copy 20:&:@:num/raw 195 3000:num/raw <- index *1:&:@:num, 1 196 ] 197 +mem: storing 35 in location 3000 198 199 :(before "Read element" following "case INDEX:") 200 element.properties.push_back(pair<string, string_tree*>("raw", NULL)); 201 202 //:: 'local-scope' is a convenience operation to automatically deduce 203 //:: the amount of space to allocate in a default space with names 204 205 :(scenario local_scope) 206 def main [ 207 local-scope 208 x:num <- copy 0 209 y:num <- copy 3 210 ] 211 # allocate space for x and y, as well as the chaining slot at indices 0 and 1 212 +mem: array length is 4 213 214 :(before "End is_disqualified Special-cases") 215 if (x.name == "number-of-locals") 216 x.initialized = true; 217 :(before "End is_special_name Special-cases") 218 if (s == "number-of-locals") return true; 219 220 :(before "End Rewrite Instruction(curr, recipe result)") 221 // rewrite 'local-scope' to 222 // ``` 223 // default-space:space <- new location:type, number-of-locals:literal 224 // ``` 225 // where number-of-locals is Name[recipe][""] 226 if (curr.name == "local-scope") { 227 rewrite_default_space_instruction(curr); 228 } 229 :(code) 230 void rewrite_default_space_instruction(instruction& curr) { 231 if (!curr.ingredients.empty()) 232 raise << "'" << to_original_string(curr) << "' can't take any ingredients\n" << end(); 233 curr.name = "new"; 234 curr.ingredients.push_back(reagent("location:type")); 235 curr.ingredients.push_back(reagent("number-of-locals:literal")); 236 if (!curr.products.empty()) 237 raise << "local-scope can't take any results\n" << end(); 238 curr.products.push_back(reagent("default-space:space")); 239 } 240 :(after "Begin Preprocess read_memory(x)") 241 if (x.name == "number-of-locals") { 242 vector<double> result; 243 result.push_back(Name[get(Recipe_ordinal, current_recipe_name())][""]); 244 if (result.back() == 0) 245 raise << "no space allocated for default-space in recipe " << current_recipe_name() << "; are you using names?\n" << end(); 246 return result; 247 } 248 :(after "Begin Preprocess write_memory(x, data)") 249 if (x.name == "number-of-locals") { 250 raise << maybe(current_recipe_name()) << "can't write to special name 'number-of-locals'\n" << end(); 251 return; 252 } 253 254 //:: all recipes must set default-space one way or another 255 256 :(before "End Globals") 257 bool Hide_missing_default_space_errors = true; 258 :(before "End Checks") 259 Transform.push_back(check_default_space); // idempotent 260 :(code) 261 void check_default_space(const recipe_ordinal r) { 262 if (Hide_missing_default_space_errors) return; // skip previous core tests; this is only for Mu code 263 const recipe& caller = get(Recipe, r); 264 // End check_default_space Special-cases 265 // assume recipes with only numeric addresses know what they're doing (usually tests) 266 if (!contains_non_special_name(r)) return; 267 trace(9991, "transform") << "--- check that recipe " << caller.name << " sets default-space" << end(); 268 if (caller.steps.empty()) return; 269 if (!starts_by_setting_default_space(caller)) 270 raise << caller.name << " does not seem to start with 'local-scope' or 'default-space'\n" << end(); 271 } 272 bool starts_by_setting_default_space(const recipe& r) { 273 return !r.steps.empty() 274 && !r.steps.at(0).products.empty() 275 && r.steps.at(0).products.at(0).name == "default-space"; 276 } 277 278 :(after "Load Mu Prelude") 279 Hide_missing_default_space_errors = false; 280 :(after "Test Runs") 281 Hide_missing_default_space_errors = true; 282 :(after "Running Main") 283 Hide_missing_default_space_errors = false; 284 285 :(code) 286 bool contains_non_special_name(const recipe_ordinal r) { 287 for (map<string, int>::iterator p = Name[r].begin(); p != Name[r].end(); ++p) { 288 if (p->first.empty()) continue; 289 if (p->first.find("stash_") == 0) continue; // generated by rewrite_stashes_to_text (cross-layer) 290 if (!is_special_name(p->first)) 291 return true; 292 } 293 return false; 294 }