<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><font face="Monaco" style="font-size: 11px;" class="">Hi,</font><div class=""><font face="Monaco" style="font-size: 11px;" class=""><br class=""></font></div><div class=""><font face="Monaco" style="font-size: 11px;" class="">I am looking into how the python compiler generates basic blocks during the CFG generation process and my expectations from CFG theory seems to be at odds with how the python compiler actually generates its CFG. Take the following code snippet for example:</font></div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class=""><font face="Courier" class="">def median(pool):</font></div></div><div class=""><div class=""><font face="Courier" class=""> copy = sorted(pool)</font></div></div><div class=""><div class=""><font face="Courier" class=""> size = len(copy)</font></div></div><div class=""><div class=""><font face="Courier" class=""> if size % 2 == 1:</font></div></div><div class=""><div class=""><font face="Courier" class=""> return copy[(size - 1) / 2]</font></div></div><div class=""><div class=""><font face="Courier" class=""> else:</font></div></div><div class=""><div class=""><font face="Courier" class=""> return (copy[size/2 - 1] + copy[size/2]) / 2</font></div></div><div class=""><br class=""></div></blockquote><div class=""><font face="Monaco" class=""><span style="font-size: 11px;" class="">From my understanding of basic blocks in compilers, the above code snippet should have at least 3 basic blocks as follows:</span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 11px;" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>1. Block 1 - all instructions up to and including those for the if test.</span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 11px;" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>2. Block 2 - all instructions for the if body i.e the first return statement.</span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 11px;" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>3. Block 3 - instructions for the else block i.e. the second return statement.</span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 11px;" class=""><br class=""></span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 11px;" class="">My understanding of the the section on Control flow Graphs in the “Design of the CPython Compiler” also alludes to this - </span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 11px;" class=""> </span></font></div><div class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><span style="color: rgb(62, 67, 73); line-height: 21.6px; widows: 1; background-color: rgb(255, 255, 255);" class=""><font face="Courier" size="1" class="">As an example, consider an ‘if’ statement with an ‘else’ block. The guard on the ‘if’ is a basic block which is pointed to by the basic block containing the code leading to the ‘if’ statement. The ‘if’ statement block contains jumps (which are exit points) to the true body of the ‘if’ and the ‘else’ body (which may be NULL), each of which are their own basic blocks. Both of those blocks in turn point to the basic block representing the code following the entire ‘if’ statement.</font></span></blockquote></blockquote></div><div class=""><br class=""></div><div class=""><font face="Monaco" style="font-size: 11px;" class="">The CPython compiler however seems to generate 2 basic blocks for the above snippets - </font></div><div class=""><font face="Monaco" style="font-size: 11px;" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>1. Block 1 - all instructions up to and including the if statement and the body of the if statement (the first return statement in this case)</font></div><div class=""><font face="Monaco" style="font-size: 11px;" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>2. Block 2 - instructions for the else block (the second return statement)</font></div><div class=""><font face="Monaco" style="font-size: 11px;" class=""><br class=""></font></div><div class=""><font face="Monaco" style="font-size: 11px;" class="">Is there any reason for this or have I somehow missed something in the CFG generation process?</font></div><div class=""><font face="Monaco" style="font-size: 11px;" class=""><br class=""></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 11px;" class="">Regards,</span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 11px;" class=""><br class=""></span></font></div><div class=""><font face="Monaco" class=""><span style="font-size: 11px;" class="">Obi</span></font></div></body></html>