<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IR &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/ir/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Fri, 27 Apr 2018 15:27:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Code Tip: Be Careful With Loops</title>
		<link>https://blog.xojo.com/2018/03/14/code-tip-be-careful-with-loops/</link>
		
		<dc:creator><![CDATA[Norman Palardy]]></dc:creator>
		<pubDate>Wed, 14 Mar 2018 07:16:10 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[IR]]></category>
		<category><![CDATA[LLVM]]></category>
		<category><![CDATA[Loops]]></category>
		<category><![CDATA[Uint8]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=3979</guid>

					<description><![CDATA[Every once in a while we get a question or post like this one that wonders why a loop might turn into an infinite loop or why it doesn't behave as expected and stop when the loop reaches the limit. And this behaviour is not specifically a Xojo thing, most programming languages do this. In fact, there are some very well know encryption algorithms, like BlowFish, that rely on this behaviour.]]></description>
										<content:encoded><![CDATA[<p>Every once in a while we get a question or post like <a href="https://forum.xojo.com/conversation/post/377126">this one</a> that wonders why a loop might turn into an infinite loop or why it doesn&#8217;t behave as expected and stop when the loop reaches the limit.</p>
<p><span id="more-3979"></span></p>
<p>In this case the specific FOR NEXT loop in question is :</p>
<pre>For X As UInt8 = 0 To 255 
  Dim strAnything As String = "Hello World" 
Next
</pre>
<p>If you run this code you&#8217;ll find that it runs forever. You&#8217;ve accidentally created an infinite loop.</p>
<p>How did this come to be this way? There are two things involved. One is what range of values a Uint8 can actually store. And the second is the actual code generated for the for loop itself. When these two combine the stopping conditions will never occur.</p>
<p><a href="http://developer.xojo.com/integer-size-specific">Uint8 values</a> can hold a range of values like any integer. They are all positive. And the range is from 0 &#8211; 255.</p>
<p>So a Uint8 will NEVER hold a value &gt; 255 or &lt; 0.</p>
<p>Combine this with the fact that a FOR loop is generated using roughly this underlying code</p>
<pre>Dim loopCounter As UInt8
Dim loopLimit As Integer = 255
Dim loopBase As Integer = 254
Dim loopStep As Integer = 1

loopCounter = loopBase

loopTop:
  If loopCounter &gt; loopLimit Then
    GoTo loopEnd
  End If
  
  // loop body code would be here
  // its been elided to focus on the actual loop mechanics
  //    dim strAnything as string = "Hello World" 
  // 
  loopCounter = loopCounter + loopStep // NOTE THIS CAN CAUSE OVERFLOW !

  GoTo LoopTop

loopEnd:
</pre>
<p>This is NOT the actual code used. It is just a Xojo version of the actual underlying <a href="https://blog.xojo.com/2018/01/24/compilers-104-ir-generation/">LLVM IR</a> code that gets generated.</p>
<p>Hopefully this explains the confusion. Since a Uint8 cannot hold a value greater than 255, you will find that when loopCounter IS already 255 and the increment, or loopStep in this example, is added to the existing value the loopCounter goes NOT from 255 to 256 but back to 0. It wraps around. This isn&#8217;t a bug.</p>
<p>The addition of the loopStep value causes the Uint8 to overflow since the value 256, in binary, requires 9 bits, not 8 but the Uint8 can only hold 8 bits. And so when you add it the 8 bits the value <em>can</em> hold are all 0 and the loop counter resets to 0.</p>
<p>And this behavior is not specifically a Xojo thing, most programming languages do this. In fact, there are some very well know encryption algorithms, like <a href="https://en.wikipedia.org/wiki/Blowfish_(cipher)">BlowFish</a>, that rely on this  .</p>
<p>In the specific sample code that prompted this post, a loop counter that is an Integer or Uint16 is needed because they can hold values that exceed the loops upper bound.</p>
<p>Loops that count down have a similar problem. but counting down and underflowing instead of overflowing.</p>
<p>The moral of the story is to stick to using the general <a href="http://developer.xojo.com/integer">Integer data type</a> unless you have a specific need for a <a href="http://developer.xojo.com/integer-size-specific">size-specific Integer type</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Compilers 104 &#8211; IR Generation</title>
		<link>https://blog.xojo.com/2018/01/24/compilers-104-ir-generation/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Wed, 24 Jan 2018 07:10:44 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[IR]]></category>
		<category><![CDATA[LLVM]]></category>
		<guid isPermaLink="false">http://blog.xojo.com/?p=2833</guid>

					<description><![CDATA[This is the fourth post in our Compiler series. Now that the Semantic Analyzer has verified that the code is actually correct and created syntax trees, it's time to talk about IR generation.]]></description>
										<content:encoded><![CDATA[<p>Now that the Semantic Analyzer has verified that the code is actually correct and created syntax trees, it&#8217;s time to talk about IR generation.</p>
<p>This is the fourth post in our Compiler series. Other posts:</p>
<ul>
<li><a href="https://blog.xojo.com/2017/12/04/llvm-everywhere/">LLVM Everywhere</a></li>
<li><a href="https://blog.xojo.com/2017/12/06/compilers-101-overview-and-lexer/">Compilers 101 &#8211; Overview and Lexer</a></li>
<li><a href="https://blog.xojo.com/2017/12/08/compilers-102-parser/">Compilers 102 &#8211; Parser</a></li>
<li><a href="https://blog.xojo.com/2018/01/22/compilers-103-semantic-analyzer/">Compilers 103 &#8211; Semantic Analyzer</a></li>
</ul>
<p><span id="more-2833"></span></p>
<h2>What is IR?</h2>
<p>After the Semantic Analyzer, the next step is to turn the trees that it validated and added type information to into a representation that is much closer to what the machine is actually going to generate. This representation is called an intermediate representation (IR). The Xojo compiler, when building for 64-bit or ARM, uses <a href="https://llvm.org/docs/LangRef.html">LLVM IR</a>.</p>
<p>The resulting LLVM IR describes the actual control flow of the program and every single thing that will be in the final program. In addition to the user-visible code that gets executed and the implicit conversions that are now in the trees, it also needs to contain all of the hidden, behind the scenes calls like reference counting and introspection metadata.</p>
<p>LLVM IR is actually higher level and more abstract than the actual assembly that it’ll end up generating. For example, unlike assembly language, it’s entirely strongly typed and the Xojo compiler has to be very precise in what it generates (which is a good thing!).</p>
<h2>IR Code Generation</h2>
<p>To get started, here is the abstract syntax tree that was previously created by the Semantic Analyzer:</p>
<p>￼<img fetchpriority="high" decoding="async" class="alignnone wp-image-2831" src="https://blog.xojo.com/wp-content/uploads/2017/05/2017-05-19_09-02-03.png" alt="" width="600" height="292" /></p>
<p>To generate IR, the compiler walks through the above tree, <a href="https://en.wikipedia.org/wiki/Depth-first_search">depth first</a>, to get to the leaf nodes. Doing this gets us to the BinaryOperator* for the multiple on the lower right side of the tree.</p>
<p>The LLVM IR to multiply those two values (<a href="https://llvm.org/docs/LangRef.html#mul-instruction">mul</a>) looks like this:</p>
<pre>%1 = <a href="https://llvm.org/docs/LangRef.html#mul-instruction">mul</a> i32 2, 4</pre>
<p>Now it works backwards through the tree. So the next item is the implicit cast, which has to cast the value that was calculated in the previous command:</p>
<pre>%2 = <a href="https://llvm.org/docs/LangRef.html#sitofp-to-instruction">sitofp</a> i32 %1 to double</pre>
<p>The <a href="https://llvm.org/docs/LangRef.html#sitofp-to-instruction">sitofp</a> IR command means &#8220;Signed Integer to Floating Point&#8221;.</p>
<p>Continuing up the tree, the binary operator is next, so it can now grab the left hand-side value to apply to the right-hand side value. This is the IR to add the values:</p>
<pre>%3 = <a href="https://llvm.org/docs/LangRef.html#fadd-instruction">fadd</a> double 3.14, %2</pre>
<p>The <a href="https://llvm.org/docs/LangRef.html#fadd-instruction">fadd</a> IR command means &#8220;floating point add&#8221;.</p>
<p>And continuing up the tree, the implicit cast is next:</p>
<pre>%4 = <a href="https://llvm.org/docs/LangRef.html#fptosi-to-instruction">fptosi</a> double %3 to i32</pre>
<p>The <a href="https://llvm.org/docs/LangRef.html#fptosi-to-instruction">fptosi</a> IR command means &#8220;floating point to signed integer&#8221;.</p>
<p>Lastly, we reach the actual assignment (<a href="https://llvm.org/docs/LangRef.html#store-instruction">store</a>) with IR that looks like this:</p>
<pre><a href="https://llvm.org/docs/LangRef.html#store-instruction">store</a> i32 %4, i32* @sum</pre>
<p>Here is the complete IR that gets generated:</p>
<pre>%1 = mul i32 2, 4
%2 = sitofp i32 %1 to double
%3 = fadd double 3.14, %2
%4 = fptosi double %3 to i32
store i32 %4, i32* @sum</pre>
<p>Reading through this you should now understand why no one wants to manually write code at such a low-level.</p>
<p>This is the last part of the compiler that is considered to be part of the front end. The rest of the compiler components belong to the <a href="https://blog.xojo.com/2018/01/29/compilers-105-back-end-overview/">back end</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
