Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>"Unrolling a loop" normally means replacing a repetition with a sequence of actions. The loop:</p> <pre><code>for (int i = 0; i &lt; 4; ++i) { a[i] = b[i] + c[i]; } </code></pre> <p>would unroll into the equivalent:</p> <pre><code>a[0] = b[0] + c[0]; a[1] = b[1] + c[1]; a[2] = b[2] + c[2]; a[3] = b[3] + c[3]; </code></pre> <p>It appears to me that whoever was being quoted by Wikipedia was using the phrase in a somewhat metaphorical sense. So, in that sense...</p> <p>Your sample would normally be invoked inside a interpreter that is walking a tree of AST nodes, which might look something like this:</p> <pre><code> ASSIGN | +--+---+ | | REF MINUS | | x +--+---+ | | VAR PLUS | | a +--+--+ | | VAR CONST | | b 3 </code></pre> <p>and the <code>interpret</code> function would have additional options:</p> <pre><code>int interpret(node) { switch(node) { case PLUS: return interpret(child(0))+interpret(child(1)); case MINUS: return interpret(child(0))-interpret(child(1)); case ASSIGN: return set(child(0), interpret(child(1)); case VAR: return fetch(child(0)); case CONST: return value(child(0)); ... } } </code></pre> <p>If you walk the AST with that <code>interpet</code> function (actually performing the operations), you're interpreting. But if the function <em>records</em> the actions to be performed, rather than <em>executing</em> them, you're compiling. In pseudocode (actually, pseudocode <em>twice</em>, as I'm assuming a hypothetical stack machine as the compilation target):</p> <pre><code>string compile(node) { switch(node) { case PLUS: return(compile(child(0))) + compile(child(1)) + ADD); case MINUS: return(compile(child(0))) + compile(child(1)) + SUB); case ASSIGN: return(PUSHA(child(0))) + compile(child(1)) + STORE); case REF: return(PUSHA(child(0))); case VAR: return(PUSHA(child(0)) + FETCH); case CONST: return(PUSHLIT + value(child(0))); ... } } </code></pre> <p>Invoking <code>compile</code> on that AST (ignoring any pseudocode typos ;-) would spit out something like:</p> <pre><code>PUSHA x PUSHA a FETCH PUSHA b FETCH PUSHLIT 3 ADD SUB STORE </code></pre> <p>FWIW, I'd tend to think of that as unrolling the AST, rather than unrolling the interpreter, but won't criticize somebody else's metaphor without reading it in context.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload