about summary refs log blame commit diff stats
path: root/html/046global.cc.html
blob: 4bae44f9dde7ee4b50e3283f8cb8e520526a2ea5 (plain) (tree)
1
2
3
4
5
6
7
8
9
10



                                                                                          
                                

                                                
                                  

                                                                                         

                       
                                                                                                 
                                                                                            

                                      
                             
                             
                            

                              
                                  
                                                                           
                               

        





                               

       
                         









                                                                                                           

                                                       
                                        
                                                                                          

                                                                                                                                                       
                                                                                          

                                                                                                                                                       
                                                            


                                                                                                                                 
                                                                                                                          
 
                                                                                                    
                                                                  
                                                                                                                         
                                                                  

                                                                                                   
                                                                              
                                                                                                                                                                                              

                                                                                                                   
                                                                                                                                                                                                                                                                    


                                                                                             
                                                                            

                                                                              

                                                                                                                                                                                                                               
                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        




                                                                                                                                                                                                          

                                                                                                       
                                                                                        


                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                    





                                                                                        

                                                                                                                                                                                                    
                                                                                                            
                                                                                                                          
 

                                                                                      

                                                                                                  
                                                                                                                                                                                                                                                       



                                        
                                                                                                                                                                                           

                                                                                                                                                                                                                                         



                                
                                     
<!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 - 046global.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<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-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Constant { color: #00a0a0; }
.cSpecial { color: #008000; }
.muRecipe { color: #ff8700; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.traceContains { color: #008000; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.Identifier { color: #fcb165; }
-->
</style>

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

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: So far we have local variables, and we can nest local variables of short</span>
<span class="Comment">//: lifetimes inside longer ones. Now let's support 'global' variables that</span>
<span class="Comment">//: last for the life of a routine. If we create multiple routines they won't</span>
<span class="Comment">//: have access to each other's globals.</span>
<span class="Comment">//:</span>
<span class="Comment">//: This feature is still experimental and half-baked. You can't name global</span>
<span class="Comment">//: variables, and so like in most tests they don't get checked for types (the</span>
<span class="Comment">//: only known hole in the type system, can cause memory corruption). We might</span>
<span class="Comment">//: fix these issues if we ever use globals. Or we might just drop the feature</span>
<span class="Comment">//: entirely.</span>

<span class="Delimiter">:(scenario global_space)</span>
<span class="muRecipe">def</span> main [
  <span class="Comment"># pretend address:array:location; in practice we'll use new</span>
  <span class="Constant">10</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># refcount</span>
  <span class="Constant">11</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">5</span>  <span class="Comment"># length</span>
  <span class="Comment"># pretend address:array:location; in practice we'll use new</span>
  <span class="Constant">20</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># refcount</span>
  <span class="Constant">21</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">5</span>  <span class="Comment"># length</span>
  <span class="Comment"># actual start of this recipe</span>
  global-space:space<span class="Special"> &lt;- </span>copy <span class="Constant">20</span>/unsafe
  <span class="Normal">default</span>-space:space<span class="Special"> &lt;- </span>copy <span class="Constant">10</span>/unsafe
  <span class="Constant">1</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
  <span class="Special">1:num/space:global</span><span class="Special"> &lt;- </span>copy <span class="Constant">24</span>
]
<span class="Comment"># store to default space: 10 + (skip refcount and length) 2 + (index) 1</span>
<span class="traceContains">+mem: storing 23 in location 13</span>
<span class="Comment"># store to chained space: (contents of location 12) 20 + (refcount and length) 2 + (index) 1</span>
<span class="traceContains">+mem: storing 24 in location 23</span>

<span class="Comment">//: to support it, create another special variable called global space</span>
<span class="Delimiter">:(before &quot;End is_disqualified Cases&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>name == <span class="Constant">&quot;global-space&quot;</span><span class="Delimiter">)</span>
  x<span class="Delimiter">.</span>initialized = <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End is_special_name Cases&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>s == <span class="Constant">&quot;global-space&quot;</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>

<span class="Comment">//: writes to this variable go to a field in the current routine</span>
<span class="Delimiter">:(before &quot;End routine Fields&quot;)</span>
<span class="Normal">int</span> global_space<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End routine Constructor&quot;)</span>
global_space = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">:(after &quot;Begin Preprocess write_memory(x, data)&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>name == <span class="Constant">&quot;global-space&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!scalar<span class="Delimiter">(</span>data<span class="Delimiter">)</span> || !is_space<span class="Delimiter">(</span>x<span class="Delimiter">))</span>
    raise &lt;&lt; maybe<span class="Delimiter">(</span>current_recipe_name<span class="Delimiter">())</span> &lt;&lt; <span class="Constant">&quot;'global-space' should be of type address:array:location, but tried to write '&quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;'</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>Current_routine<span class="Delimiter">-&gt;</span>global_space<span class="Delimiter">)</span>
    raise &lt;&lt; <span class="Constant">&quot;routine already has a global-space; you can't over-write your globals&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  Current_routine<span class="Delimiter">-&gt;</span>global_space = data<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
  <span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Comment">//: now marking variables as /space:global looks them up inside this field</span>
<span class="Delimiter">:(after &quot;int space_base(const reagent&amp; x)&quot;)</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_global<span class="Delimiter">(</span>x<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!Current_routine<span class="Delimiter">-&gt;</span>global_space<span class="Delimiter">)</span>
      raise &lt;&lt; <span class="Constant">&quot;routine has no global space</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">return</span> Current_routine<span class="Delimiter">-&gt;</span>global_space + <span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>

<span class="Comment">//: for now let's not bother giving global variables names.</span>
<span class="Comment">//: don't want to make them too comfortable to use.</span>

<span class="Delimiter">:(scenario global_space_with_names)</span>
<span class="muRecipe">def</span> main [
  global-space:space<span class="Special"> &lt;- </span><span class="Normal">new</span> <span class="Constant">location:type</span><span class="Delimiter">,</span> <span class="Constant">10</span>
  <span class="Normal">x</span>:num<span class="Special"> &lt;- </span>copy <span class="Constant">23</span>
  <span class="Special">1:num/space:global</span><span class="Special"> &lt;- </span>copy <span class="Constant">24</span>
]
<span class="Comment"># don't complain about mixing numeric addresses and names</span>
$error: <span class="Constant">0</span>

<span class="Delimiter">:(after &quot;bool is_numeric_location(const reagent&amp; x)&quot;)</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>is_global<span class="Delimiter">(</span>x<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>

<span class="Comment">//: helpers</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">bool</span> is_global<span class="Delimiter">(</span><span class="Normal">const</span> reagent&amp; x<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  string_tree* s = property<span class="Delimiter">(</span>x<span class="Delimiter">,</span> <span class="Constant">&quot;space&quot;</span><span class="Delimiter">);</span>
  <span class="Identifier">return</span> s &amp;&amp; s<span class="Delimiter">-&gt;</span>atom &amp;&amp; s<span class="Delimiter">-&gt;</span>value == <span class="Constant">&quot;global&quot;</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->