<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mu - linux/406int32.mu</title>
<meta name="Generator" content="Vim/8.2">
<meta name="plugin-version" content="vim8.1_v2">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=,use_input_for_pc=fallback">
<meta name="colorscheme" content="minimal-light">
<style>
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; }
a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.PreProc { color: #c000c0; }
.LineNr { }
.Constant { color: #008787; }
.muFunction { color: #af5f00; text-decoration: underline; }
.Delimiter { color: #c000c0; }
.muComment { color: #005faf; }
.Special { color: #ff6060; }
-->
</style>
<script>
<!--
/* 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/main/linux/406int32.mu'>https://github.com/akkartik/mu/blob/main/linux/406int32.mu</a>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr"> 1 </span><span class="muComment"># Some slow but convenient helpers</span>
<span id="L2" class="LineNr"> 2 </span>
<span id="L3" class="LineNr"> 3 </span><span class="muComment"># slow, iterative shift-left instruction</span>
<span id="L4" class="LineNr"> 4 </span><span class="muComment"># preconditions: _nr >= 0, _dr > 0</span>
<span id="L5" class="LineNr"> 5 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='406int32.mu.html#L5'>repeated-shift-left</a></span> nr: int, dr: int<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span>
<span id="L6" class="LineNr"> 6 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> copy nr
<span id="L7" class="LineNr"> 7 </span> <span class="Delimiter">{</span>
<span id="L8" class="LineNr"> 8 </span> compare dr, <span class="Constant">0</span>
<span id="L9" class="LineNr"> 9 </span> <span class="PreProc">break-if-<=</span>
<span id="L10" class="LineNr">10 </span> result <span class="Special"><-</span> shift-left <span class="Constant">1</span>
<span id="L11" class="LineNr">11 </span> decrement dr
<span id="L12" class="LineNr">12 </span> <span class="PreProc">loop</span>
<span id="L13" class="LineNr">13 </span> <span class="Delimiter">}</span>
<span id="L14" class="LineNr">14 </span> <span class="PreProc">return</span> result
<span id="L15" class="LineNr">15 </span><span class="Delimiter">}</span>
<span id="L16" class="LineNr">16 </span>
<span id="L17" class="LineNr">17 </span><span class="muComment"># slow, iterative shift-right instruction</span>
<span id="L18" class="LineNr">18 </span><span class="muComment"># preconditions: _nr >= 0, _dr > 0</span>
<span id="L19" class="LineNr">19 </span><span class="PreProc">fn</span> <span class="muFunction"><a href='406int32.mu.html#L19'>repeated-shift-right</a></span> nr: int, dr: int<span class="PreProc"> -> </span>_/eax: int <span class="Delimiter">{</span>
<span id="L20" class="LineNr">20 </span> <span class="PreProc">var</span> result/eax: int <span class="Special"><-</span> copy nr
<span id="L21" class="LineNr">21 </span> <span class="Delimiter">{</span>
<span id="L22" class="LineNr">22 </span> compare dr<!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 - 033exclusive_container.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.cSpecial { color: #008000; }
.SalientComment { color: #00ffff; }
.traceContains { color: #008000; }
.Comment { color: #9090ff; }
.Delimiter { color: #a04060; }
.Special { color: #ff6060; }
.Identifier { color: #804000; }
.Constant { color: #00a0a0; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Exclusive containers contain exactly one of a fixed number of 'variants'</span>
<span class="Comment">//: of different types.</span>
<span class="Comment">//:</span>
<span class="Comment">//: They also implicitly contain a tag describing precisely which variant is</span>
<span class="Comment">//: currently stored in them.</span>
<span class="Delimiter">:(before "End Mu Types Initialization")</span>
<span class="Comment">//: We'll use this container as a running example, with two number elements.</span>
<span class="Delimiter">{</span>
type_ordinal tmp = Type_ordinal[<span class="Constant">"number-or-point"</span>] = Next_type_ordinal++<span class="Delimiter">;</span>
Type[tmp]<span class="Delimiter">.</span>size = <span class="Constant">2</span><span class="Delimiter">;</span>
Type[tmp]<span class="Delimiter">.</span>kind = exclusive_container<span class="Delimiter">;</span>
Type[tmp]<span class="Delimiter">.</span>name = <span class="Constant">"number-or-point"</span><span class="Delimiter">;</span>
vector<type_ordinal> t1<span class="Delimiter">;</span>
t1<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>number<span class="Delimiter">);</span>
Type[tmp]<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>t1<span class="Delimiter">);</span>
vector<type_ordinal> t2<span class="Delimiter">;</span>
t2<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>point<span class="Delimiter">);</span>
Type[tmp]<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>t2<span class="Delimiter">);</span>
Type[tmp]<span class="Delimiter">.</span>element_names<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span><span class="Constant">"i"</span><span class="Delimiter">);</span>
Type[tmp]<span class="Delimiter">.</span>element_names<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span><span class="Constant">"p"</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">//: Tests in this layer often explicitly setup memory before reading it as an</span>
<span class="Comment">//: array. Don't do this in general. I'm tagging exceptions with /raw to</span>
<span class="Comment">//: avoid warnings.</span>
<span class="Delimiter">:(scenario copy_exclusive_container)</span>
<span class="Comment"># Copying exclusive containers copies all their contents and an extra location for the tag.</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">1</span> <span class="Comment"># 'point' variant</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">34</span>
<span class="Constant">3</span>:number<span class="Special"> <- </span>copy <span class="Constant">35</span>
<span class="Constant">4</span>:number-or-point<span class="Special"> <- </span>copy <span class="Constant">1</span>:number-or-point/<span class="Special">raw</span> <span class="Comment"># unsafe</span>
]
<span class="traceContains">+mem: storing 1 in location 4</span>
<span class="traceContains">+mem: storing 34 in location 5</span>
<span class="traceContains">+mem: storing 35 in location 6</span>
<span class="Delimiter">:(before "End size_of(types) Cases")</span>
if <span class="Delimiter">(</span>t<span class="Delimiter">.</span>kind == exclusive_container<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// size of an exclusive container is the size of its largest variant</span>
<span class="Comment">// (So like containers, it can't contain arrays.)</span>
long long int result = <span class="Constant">0</span><span class="Delimiter">;</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < t<span class="Delimiter">.</span>size<span class="Delimiter">;</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
long long int tmp = size_of<span class="Delimiter">(</span>t<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
if <span class="Delimiter">(</span>tmp > result<span class="Delimiter">)</span> result = tmp<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// ...+1 for its tag.</span>
<span class="Identifier">return</span> result+<span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: To access variants of an exclusive container, use 'maybe-convert'.</span>
<span class="Comment">//: It always returns an address (so that you can modify it) or null (to</span>
<span class="Comment">//: signal that the conversion failed (because the container contains a</span>
<span class="Comment">//: different variant).</span>
<span class="Comment">//: 'maybe-convert' requires a literal in ingredient 1. We'll use a synonym</span>
<span class="Comment">//: called 'variant'.</span>
<span class="Delimiter">:(before "End Mu Types Initialization")</span>
Type_ordinal[<span class="Constant">"variant"</span>] = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">:(scenario maybe_convert)</span>
recipe main [
<span class="Constant">12</span>:number<span class="Special"> <- </span>copy <span class="Constant">1</span>
<span class="Constant">13</span>:number<span class="Special"> <- </span>copy <span class="Constant">35</span>
<span class="Constant">14</span>:number<span class="Special"> <- </span>copy <span class="Constant">36</span>
<span class="Constant">20</span>:address:point<span class="Special"> <- </span>maybe-convert <span class="Constant">12</span>:number-or-point/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">1</span>:variant <span class="Comment"># unsafe</span>
]
<span class="traceContains">+mem: storing 13 in location 20</span>
<span class="Delimiter">:(scenario maybe_convert_fail)</span>
recipe main [
<span class="Constant">12</span>:number<span class="Special"> <- </span>copy <span class="Constant">1</span>
<span class="Constant">13</span>:number<span class="Special"> <- </span>copy <span class="Constant">35</span>
<span class="Constant">14</span>:number<span class="Special"> <- </span>copy <span class="Constant">36</span>
<span class="Constant">20</span>:address:point<span class="Special"> <- </span>maybe-convert <span class="Constant">12</span>:number-or-point/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">0</span>:variant <span class="Comment"># unsafe</span>
]
<span class="traceContains">+mem: storing 0 in location 20</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
MAYBE_CONVERT<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
Recipe_ordinal[<span class="Constant">"maybe-convert"</span>] = MAYBE_CONVERT<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
case MAYBE_CONVERT: <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>ingredients<span class="Delimiter">)</span> != <span class="Constant">2</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">": 'maybe-convert' expects exactly 2 ingredients in '"</span> << current_instruction<span class="Delimiter">().</span>to_string<span class="Delimiter">()</span> << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
reagent base = canonize<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span>
long long int base_address = base<span class="Delimiter">.</span>value<span class="Delimiter">;</span>
if <span class="Delimiter">(</span>base_address == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">": tried to access location 0 in '"</span> << current_instruction<span class="Delimiter">().</span>to_string<span class="Delimiter">()</span> << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
if <span class="Delimiter">(</span>base<span class="Delimiter">.</span>types<span class="Delimiter">.</span>empty<span class="Delimiter">()</span> || Type[base<span class="Delimiter">.</span>types<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span>]<span class="Delimiter">.</span>kind != exclusive_container<span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << current_recipe_name <span class="Delimiter">()</span> << <span class="Constant">": first ingredient of 'maybe-convert' should be an exclusive-container, but got "</span> << base<span class="Delimiter">.</span>original_string << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
if <span class="Delimiter">(</span>!is_literal<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
raise << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">": second ingredient of 'maybe-convert' should have type 'variant', but got "</span> << current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>original_string << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
long long int tag = current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>value<span class="Delimiter">;</span>
long long int result<span class="Delimiter">;</span>
if <span class="Delimiter">(</span>tag == static_cast<long long int><span class="Delimiter">(</span>Memory[base_address]<span class="Delimiter">))</span> <span class="Delimiter">{</span>
result = base_address+<span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
else <span class="Delimiter">{</span>
result = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: Allow exclusive containers to be defined in mu code.</span>
<span class="Delimiter">:(scenario exclusive_container)</span>
exclusive-container foo [
x:number
y:number
]
<span class="traceContains">+parse: reading exclusive-container foo</span>
<span class="traceContains">+parse: element name: x</span>
<span class="traceContains">+parse: type: 1</span>
<span class="traceContains">+parse: element name: y</span>
<span class="traceContains">+parse: type: 1</span>
<span class="Delimiter">:(before "End Command Handlers")</span>
else if <span class="Delimiter">(</span>command == <span class="Constant">"exclusive-container"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
insert_container<span class="Delimiter">(</span>command<span class="Delimiter">,</span> exclusive_container<span class="Delimiter">,</span> in<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: To construct exclusive containers out of variant types, use 'merge'.</span>
<span class="Delimiter">:(scenario lift_to_exclusive_container)</span>
exclusive-container foo [
x:number
y:number
]
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">34</span>
<span class="Constant">2</span>:foo<span class="Special"> <- </span>merge <span class="Constant">0</span>/x<span class="Delimiter">,</span> <span class="Constant">1</span>:number
<span class="Constant">4</span>:foo<span class="Special"> <- </span>merge <span class="Constant">1</span>/x<span class="Delimiter">,</span> <span class="Constant">1</span>:number
]
<span class="traceContains">+mem: storing 0 in location 2</span>
<span class="traceContains">+mem: storing 34 in location 3</span>
<span class="traceContains">+mem: storing 1 in location 4</span>
<span class="traceContains">+mem: storing 34 in location 5</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->