{"id":374,"date":"2012-07-20T16:42:06","date_gmt":"2012-07-20T16:42:06","guid":{"rendered":"http:\/\/www.coolbasic.com\/blog\/?p=374"},"modified":"2012-07-20T16:42:06","modified_gmt":"2012-07-20T16:42:06","slug":"moving-from-analysis-to-synthesis","status":"publish","type":"post","link":"https:\/\/www.coolbasic.com\/blog\/2012\/07\/20\/moving-from-analysis-to-synthesis\/","title":{"rendered":"Moving from analysis to synthesis"},"content":{"rendered":"<p>Now that I\u2019m on my summer vacation, I\u2019ve had some time to return to the CoolBasic Classic compiler again (after a month or two of slacking, pardon me), and I\u2019m happy to say that the analysis phase of the compilation process is now basically done. After code analysis the compiler has all the information it needs in order to produce the final byte code output. This phase is called \u201csynthesis\u201d, and it executes by recursively iterating through all statements based on their scope. This approach enables the compiler to omit byte code generation for unreachable code blocks such as <code>If<\/code>\/<code>ElseIf<\/code>\/<code>While<\/code>\/<code>Case<\/code> blocks that have a constant expression that evaluates to false. Moreover, we could have those user-defined Functions and Subs who aren\u2019t used anywhere in code not to be included to the output IL at all. However, it\u2019s important to process all those code blocks fully even though they wouldn\u2019t get written to IL because we want the compiler to validate the entire source code.<\/p>\n<p>Some of the statements are already written into the output byte code stream such as the assignment statement and a <code>Sub<\/code> call. Similarly to function calls, the compiler inserts any omitted optional parameters and injects any type conversion operators that might be needed. There are still dozens of statement types to process, but all of them should be quite simple to implement. I decided to tackle the <code>If<\/code>\/<code>ElseIf<\/code>\/<code>Else<\/code> structure next because it requires some branching infrastructure I need to develop first. Once that\u2019s sorted out it\u2019s easy to implement other program flow control statements such as the <code>While<\/code> and <code>Until<\/code> loops.<\/p>\n<p>Branching i.e. jumping is an interesting topic on its own because it\u2019s a common key element and therefore needs to be very efficient. Labels are local to their enclosing Function or Sub symbol, and also the Root (the main program) has its own label \u201cscope\u201d. This means that the programmer can have identically named labels as long as they exist in a different \u201cstack fragment\u201d. The <code>Root<\/code>\/<code>Function<\/code>\/<code>Sub<\/code> symbols encompass a dictionary of labels, and they\u2019re populated already in the parsing phase so that all labels are known during synthesis. The compiler can therefore match forward jumps without expensive scanning. On the other hand, this technique only applies to <code>GoTo<\/code> statements \u2013 many other statement types need to generate some jump targets.<\/p>\n<p>Statements are processed in the natural program flow order during synthesis. For example, an <code>If<\/code> statement is processed before the corresponding <code>EndIf<\/code> statement, but the <code>If<\/code> statement in question still needs to know where the <code>EndIf<\/code> statement is in order to jump to the proper location if the expression is false. The compiler needs to link all cognate <code>If<\/code>\/<code>ElseIf<\/code>\/<code>Else<\/code>\/<code>EndIf<\/code> statements together so that they can access the next member in the chain as well as the final <code>EndIf<\/code>. For branching to work, the compiler stores the byte code pointer of each statement (labels are considered statements as well even though they don\u2019t generate any IL) when they get iterated by the synthesizer. Of course the <code>If<\/code> statement can\u2019t know the final address of the <code>EndIf<\/code> statement upon processing because the <code>EndIf<\/code> doesn\u2019t yet have the calculated offset, i.e. forward jumps cannot be determined fully at this point.<\/p>\n<p>To solve this problem, all jumps (apart from <code>GoTo<\/code> statements) are cached in a special list that consist of <em>tuples<\/em> of an IL jump instruction and the target statement. Just before the IL is physically written to a file the compiler simply iterates this list and fills in the correct jump instruction addresses with the calculated code offsets found in the completely processed statements.<\/p>\n<p>We\u2019re quite close from having a working compiler prototype for internal testing \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that I\u2019m on my summer vacation, I\u2019ve had some time to return to the CoolBasic Classic compiler again (after a month or two of slacking, pardon me), and I\u2019m happy to say that the analysis phase of the compilation process is now basically done. After code analysis the compiler has all the information it [&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\/374"}],"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=374"}],"version-history":[{"count":1,"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/posts\/374\/revisions"}],"predecessor-version":[{"id":375,"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/posts\/374\/revisions\/375"}],"wp:attachment":[{"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/media?parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/categories?post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coolbasic.com\/blog\/wp-json\/wp\/v2\/tags?post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}