{"id":377,"date":"2012-07-28T13:34:44","date_gmt":"2012-07-28T13:34:44","guid":{"rendered":"http:\/\/www.coolbasic.com\/blog\/?p=377"},"modified":"2012-07-28T13:34:44","modified_gmt":"2012-07-28T13:34:44","slug":"branching-rules","status":"publish","type":"post","link":"https:\/\/www.coolbasic.com\/blog\/2012\/07\/28\/branching-rules\/","title":{"rendered":"Branching rules"},"content":{"rendered":"<p>Last time I talked about synthesis and IL generation. Currently, half of the statements already write valid byte code. I have now completed the branching infrastructure that I mentioned in my previous blog post, and most conditional structures and loops are now prepared \u2013 including the <code>If<\/code>, <code>While<\/code>, and <code>Until<\/code> structures. The <code>For-Next<\/code> loop and the <code>Select-Case<\/code> structure are next. But before going there I decided to wrap up a quick summary about branching rules and what they mean in terms of code compilation.<\/p>\n<p>First of all, there\u2019s a mechanic in place that eliminates unreachable code. For example, if you had an If condition that has a constant expression that always evaluates to false, the compiler knows this during compile time and will not write the byte code for that block. Similarly, if you had a <code>While<\/code> loop that is known to be always true the compiler doesn\u2019t write the byte code for the expression at all. Code elimination also applies to nested code blocks, meaning that everything inside an unreachable code block is ignored when the byte code output is written. Next, let\u2019s look at the specs:<\/p>\n<h4>The If structure<\/h4>\n<ol>\n<li>The <code>If<\/code> and <code>ElseIf<\/code> statements must know the next <code>ElseIf<\/code>\/<code>Else<\/code> statement in order to branch when the expression is false<\/li>\n<li>The <code>If<\/code>, <code>ElseIf<\/code> and <code>Else<\/code> statements must know the <code>EndIf<\/code> statement that is associated with the condition chain; The <code>If<\/code> statement needs it in order to branch when the expression is false, and the <code>ElseIf<\/code> and <code>Else<\/code> statements need it in order to issue an unconditional jump to indicate the end of the previous block<\/li>\n<li>The <code>ElseIf<\/code> and <code>Else<\/code> statements must add an unconditional jump to the corresponding <code>EndIf<\/code> statement before any other byte code unless the statement in question is and <code>ElseIf<\/code> with a constant value expression of false<\/li>\n<li>If the <code>If<\/code>\/<code>ElseIf<\/code> expression is a constant value, don\u2019t write expression byte code<\/li>\n<li>If any of the <code>If<\/code> or <code>ElseIf<\/code> blocks evaluated to a constant expression of true, don\u2019t write byte code for any of the subsequent blocks in the same chain<\/li>\n<li>If an <code>If<\/code> block or an <code>ElseIf<\/code> block has a constant expression of false, don\u2019t write its byte code. This includes nested code blocks<\/li>\n<\/ol>\n<p>In addition to the rules listed above there are some minor tweaks that accomplish cleaner byte code output; I\u2019ve eliminated some branching instructions that are not needed, for example.<\/p>\n<h4>The Repeat-Until structure<\/h4>\n<ol>\n<li>The <code>Until<\/code> and <code>Forever<\/code> statements must know the corresponding <code>Repeat<\/code> statement in order to branch to the correct location\n<li>If the <code>Until<\/code> statement\u2019s expression is a constant value of true, do nothing\n<li>If the <code>Until<\/code> statement\u2019s expression is a constant value of false, add an unconditional jump\n<li> The <code>Forever<\/code> statement just adds an unconditional jump\n<\/ol>\n<h4>The While-EndWhile structure<\/h4>\n<ol>\n<li>The <code>While<\/code> statement needs to know the corresponding <code>EndWhile<\/code> statement in order to branch when the expression is false\n<li> The <code>EndWhile<\/code> statement needs to know the corresponding <code>While<\/code> statement in order to issue an unconditional jump\n<li> If the <code>While<\/code> statement\u2019s expression is a constant value of false, don\u2019t write byte code for the expression or code block\n<li>If the <code>While<\/code> statement\u2019s expression is a constant value of true, don\u2019t write the expression\u2019s byte code\n<li>The <code>EndWhile<\/code> statement only adds an unconditional jump to the <code>While<\/code> statement\n<\/ol>\n<h4>Controlling loops<\/h4>\n<p>The old \u201c<code>Exit<\/code>\u201d statement is now renamed as \u201c<code>Break<\/code>\u201d. It exits the <code>Repeat<\/code>, <code>While<\/code>, <code>For<\/code>, and <code>Foreach<\/code> loops, and continues execution from after the loop.<\/p>\n<p>Similarly, a new loop control statement has been added. The \u201c<code>Continue<\/code>\u201d statement continues the encapsulating loop from its next iteration pass, but doesn\u2019t exit the loop unless the associated condition dictates so.<\/p>\n<p>The implementation of these is quite straight-forward; The <code>Break<\/code> statement needs to know the loop\u2019s ending statement, and the <code>Continue<\/code> statement needs to know the loop\u2019s starting statement. Both issue an unconditional jump.<\/p>\n<p>I haven\u2019t yet implemented these, and will retain doing so until I get the <code>For-Next<\/code> structure done. I might write a similar blog post about the <code>For-Next<\/code> structure and <code>Select-Case<\/code> structure next time\u2026<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time I talked about synthesis and IL generation. Currently, half of the statements already write valid byte code. I have now completed the branching infrastructure that I mentioned in my previous blog post, and most conditional structures and loops are now prepared \u2013 including the If, While, and Until structures. The For-Next loop and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[5],"_links":{"self":[{"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/posts\/377"}],"collection":[{"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/comments?post=377"}],"version-history":[{"count":5,"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/posts\/377\/revisions"}],"predecessor-version":[{"id":382,"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/posts\/377\/revisions\/382"}],"wp:attachment":[{"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/media?parent=377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/categories?post=377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/tags?post=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}