<?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>Christian's Projects</title>
	<atom:link href="http://www.incasoftware.de/~kamm/projects/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.incasoftware.de/~kamm/projects</link>
	<description></description>
	<lastBuildDate>Wed, 06 Mar 2013 15:14:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Getting more out of the range-based for statement in C++11</title>
		<link>http://www.incasoftware.de/~kamm/projects/index.php/2013/03/06/getting-more-out-of-the-range-based-for-statement-in-c11/</link>
		<comments>http://www.incasoftware.de/~kamm/projects/index.php/2013/03/06/getting-more-out-of-the-range-based-for-statement-in-c11/#comments</comments>
		<pubDate>Wed, 06 Mar 2013 15:14:10 +0000</pubDate>
		<dc:creator>Christian</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.incasoftware.de/~kamm/projects/?p=132</guid>
		<description><![CDATA[Among the most useful features added to C++11 is the range-based for statement. It is defined to be equivalent to the usual iterator-based loop from begin() to end() and makes standard iteration look way more appealing. Keeping the noise out of a large fraction of iterator-based loops is great, but other common for loops are [...]]]></description>
				<content:encoded><![CDATA[<p>Among the most useful features added to C++11 is <a href="http://www.codesynthesis.com/~boris/blog/2012/05/16/cxx11-range-based-for-loop/">the range-based for statement</a>. It is defined to be equivalent to the usual iterator-based loop from begin() to end() and makes standard iteration look way more appealing.</p>
<p>Keeping the noise out of a large fraction of iterator-based loops is great, but other common for loops are missing out! They have not received a convenient shorthand. Luckily, you can make some yourself by feeding the right containers to the range-based for statement.<br />
<span id="more-132"></span><br />
For example, the regular counting loops<br />
<code><br />
for (auto i = begin, e = end; i &lt; e; ++i)<br />
for (size_t i = 0, e = container.size(); i &lt; e; ++i)<br />
</code></p>
<p>can be written as<br />
<code><br />
for (const auto i : range(begin, end))<br />
for (const auto i : indices(container))<br />
</code></p>
<p>if you define indices() and range() the right way.</p>
<p>Using the range-based version has no drawbacks: it is easier to read, quicker to write and compiles to equivalent assembly (calls to operator!= and operator++ get optimized away even at g++ -O1).</p>
<p>You can do a lot with the range-based for statement. What I have found useful so far is:</p>
<ul>
<li>range(begin, end): for (auto i = begin, e = end; i != e; ++i)</li>
<li>range(end): same as range(0, end)</li>
<li>indices(container): same as range(container.size())</li>
<li>keys(container_of_pairs): shorthand for getting only value.first</li>
<li>values(container_of_pairs): shorthand for getting only value.second</li>
</ul>
<p>The <a href="https://github.com/celeraone/cpp-utils/blob/master/iteration_ranges.h">code is available on github</a> under the Boost Software License. It has no external dependencies and using it should be as simple as putting the header file into your include path. It is also fairly minimal, implementing only what is needed to make it work. A lot could be added, like rbegin(), rend(), generic reverse(), correct iterators, &#8230;</p>
<p>The approach has its limits, and one of them is exposed when you implement something like enumerate():<br />
<code><br />
for (auto e : enumerate(c)) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;// e.index is the current index<br />
&nbsp;&nbsp;&nbsp;&nbsp;// e.value is the current value<br />
}<br />
</code></p>
<p>For standard containers (as well as the keys() and values() ranges above) the type in the for statement controls whether you get a copy or a reference of what you are iterating over as well as whether it should be const or not:<br />
<code><br />
for (const auto e : c) // e is a const copy<br />
for (auto &amp; e : c) // e is a mutable reference<br />
</code></p>
<p>That can only work as long as what you want to put into e is an lvalue (i.e. c.begin() is an output iterator). It breaks down for enumerate() which cannot return a reference without extra overhead.</p>
<p>That is why I decided to split enumerate() into two functions. enumerate_byref() always grabs &#8216;value&#8217; by reference and enumerate_byval() makes a copy. Usage then becomes:<br />
<code><br />
for (const auto e : enumerate_byref(container))<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.value = true; // ok, modifies container!<br />
for (auto e : enumerate_byval(container))<br />
&nbsp;&nbsp;&nbsp;&nbsp;e.value = true; // ok, changes copy<br />
</code></p>
<p>Which is far from the ideal syntax, but at least it is explicit about what is going on.</p>
<p>Thanks to my employer, <a href="http://www.celeraone.com/">CeleraOne GmbH</a>, for permitting me to publish this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.incasoftware.de/~kamm/projects/index.php/2013/03/06/getting-more-out-of-the-range-based-for-statement-in-c11/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Recent activity</title>
		<link>http://www.incasoftware.de/~kamm/projects/index.php/2011/11/03/recent-activity/</link>
		<comments>http://www.incasoftware.de/~kamm/projects/index.php/2011/11/03/recent-activity/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 19:52:03 +0000</pubDate>
		<dc:creator>Christian</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Qml]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[Qt Creator]]></category>

		<guid isPermaLink="false">http://www.incasoftware.de/~kamm/projects/?p=125</guid>
		<description><![CDATA[Over the last year I&#8217;ve been focusing on Qt Creator and have done several posts on the Qt Labs blog: RPATH and RUNPATH Qt Creator editor news New QML editor features Qt Simulator is going public]]></description>
				<content:encoded><![CDATA[<p>Over the last year I&#8217;ve been focusing on <a href="http://qt.nokia.com/products/developer-tools/">Qt Creator</a> and have done several posts on the <a href="http://labs.qt.nokia.com">Qt Labs blog</a>:</p>
<ul>
<li><a href="http://labs.qt.nokia.com/2011/10/28/rpath-and-runpath/">RPATH and RUNPATH</a>
<li><a href="http://labs.qt.nokia.com/2011/09/28/qt-creator-editor-news/">Qt Creator editor news</a>
<li><a href="http://labs.qt.nokia.com/2010/11/30/new-qml-editor-features/">New QML editor features</a>
<li><a href="http://labs.qt.nokia.com/2010/05/31/qt-simulator-is-going-public/">Qt Simulator is going public</a>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.incasoftware.de/~kamm/projects/index.php/2011/11/03/recent-activity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Star Guard level editor</title>
		<link>http://www.incasoftware.de/~kamm/projects/index.php/2010/05/06/star-guard-level-editor/</link>
		<comments>http://www.incasoftware.de/~kamm/projects/index.php/2010/05/06/star-guard-level-editor/#comments</comments>
		<pubDate>Thu, 06 May 2010 17:22:28 +0000</pubDate>
		<dc:creator>Christian</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Qml]]></category>
		<category><![CDATA[Qt]]></category>

		<guid isPermaLink="false">http://www.incasoftware.de/~kamm/projects/?p=85</guid>
		<description><![CDATA[I spent the last few evenings working on a fun side project which is now complete: a level editor for Loren Schmidt&#8217;s Star Guard. the first level of Star Guard the same scene in the editor A few things came together that caused me to make this level editor: I was looking for an excuse [...]]]></description>
				<content:encoded><![CDATA[<p>I spent the last few evenings working on a fun side project which is now complete: a level editor for Loren Schmidt&#8217;s <a href="http://vacuumflowers.com/star_guard/star_guard.html">Star Guard</a>.<br />
<span id="more-85"></span></p>
<div style="float: right; margin-left: 40px; margin-bottom: 20px; text-align: center; width: 50%;"><img src="http://www.incasoftware.de/~kamm/starguardleveleditor/sg-level1.png" alt="first level of Star Guard" />
<p>the first level of Star Guard</p>
</div>
<div style="float: right; margin-left: 40px; text-align: center; width: 50%;"><img src="http://www.incasoftware.de/~kamm/starguardleveleditor/sg-level1-editor.png" alt="the same scene in the editor" />
<p>the same scene in the editor</p>
</div>
<p>A few things came together that caused me to make this level editor:</p>
<ul>
<li>I was looking for an excuse to do something interesting with QML.</li>
<li>Star Guard is a great game and stores its levels in friendly, easily accessible XML files.</li>
<li>When I emailed Loren about the project, he allowed me to use some of the game&#8217;s artwork.</li>
</ul>
<p>The whole thing was surprisingly easy to do, took a couple of days and amounted to less than 1200 lines of code &#8211; one half of it QML and the other C++.</p>
<p>If you don&#8217;t know about QML: It&#8217;s a fairly new part of the <a href="http://qt.nokia.com">Qt framework</a>, essentially an extension of JavaScript to build user interfaces declaratively &#8211; and great fun to work with. All the details can be found on <a href="http://doc.qt.nokia.com/4.7-snapshot/declarativeui.html">its documentation page</a> and if you want to get started, the <a href="http://qt.nokia.com/developer/qt-qtcreator-prerelease">Qt 4.7 beta1 packages have just been released</a>.</p>
<p>The editor uses QML to describe every aspect of the user interface. It&#8217;s turned out to be very useful in the beginning of the project when the editor was still QML-only and the interface layout and behavior changed a lot. It also means that it&#8217;d be very easy to redo the user interface now &#8211; even for someone without any C++ knowledge. Also, not having to recompile after every step simply makes fiddling with the UI a lot more pleasant. </p>
<p>Of course there&#8217;s the disadvantage of not following the native look and feel. But no one ever complains that most games don&#8217;t use it &#8211; and I don&#8217;t think it&#8217;s necessary for a level editor to look just like your spreadsheet application either.</p>
<p>The C++ part deals with the backend work, like providing a model that holds the map, a QML item that paints it, and loading from and saving to files. I used the QML plugin mechanism to expose these to the UI code.</p>
<p>The editor is fully functional; I consider it done and will not continue working on it. If someone wants to improve it or base his own tile based level editor on it, feel free! I release the code under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License 1.0</a>.</p>
<p><a href="http://www.incasoftware.de/~kamm/starguardleveleditor/starguard-leveleditor-linux64.tar.bz2">Linux binary (64 bit) and source code (12 MB)</a><br />
<a href="http://www.incasoftware.de/~kamm/starguardleveleditor/starguard-leveleditor-win32.zip">Windows binary and source code (10 MB)</a><br />
<a href="http://www.incasoftware.de/~kamm/starguardleveleditor/starguard-leveleditor-source.tar.bz2">source code only (.tar.bz2, 20 kB)</a><br />
<a href="http://www.incasoftware.de/~kamm/starguardleveleditor/starguard-leveleditor-source.zip">source code only (.zip, 30 kB)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.incasoftware.de/~kamm/projects/index.php/2010/05/06/star-guard-level-editor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LDC 0.9.2 released</title>
		<link>http://www.incasoftware.de/~kamm/projects/index.php/2010/03/19/ldc-0-9-2-released/</link>
		<comments>http://www.incasoftware.de/~kamm/projects/index.php/2010/03/19/ldc-0-9-2-released/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 08:29:49 +0000</pubDate>
		<dc:creator>Christian</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[D]]></category>
		<category><![CDATA[LDC]]></category>

		<guid isPermaLink="false">http://www.incasoftware.de/~kamm/projects/?p=77</guid>
		<description><![CDATA[A new version of LDC, the LLVM based compiler for the D programming language has been released. It is built with DMDFE version 1.057 and LLVM 2.6. The runtime library has been upgraded to Tango 0.99.9. In addition to up-to-date dependencies, this release incorporates a wealth of fixes and improvements by Benjamin Kramer, Frits van [...]]]></description>
				<content:encoded><![CDATA[<p>A new version of <a href="http://www.dsource.org/projects/ldc">LDC</a>, the <a href="http://www.llvm.org/">LLVM</a> based compiler for the <a href="http://www.digitalmars.com/d">D programming language</a> has been released. It is built with DMDFE version 1.057 and LLVM 2.6. The runtime library has been upgraded to <a href="http://www.dsource.org/projects/tango">Tango 0.99.9</a>.</p>
<p>In addition to up-to-date dependencies, this release incorporates a wealth of fixes and improvements by Benjamin Kramer, Frits van Bommel, Kelly Wilson, Leandro Lucarella, Matti Niemenmaa, Moritz Warning, Robert Clipsham, Tomas Lindquist Olsen and me.</p>
<p><a href="http://www.incasoftware.de/~kamm/ldc/ldc-0.9.2-x86_64.tbz2">Linux x86-64 download</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.incasoftware.de/~kamm/projects/index.php/2010/03/19/ldc-0-9-2-released/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>LDC 0.9.1 released</title>
		<link>http://www.incasoftware.de/~kamm/projects/index.php/2009/05/27/ldc-091-released/</link>
		<comments>http://www.incasoftware.de/~kamm/projects/index.php/2009/05/27/ldc-091-released/#comments</comments>
		<pubDate>Wed, 27 May 2009 18:25:54 +0000</pubDate>
		<dc:creator>Christian</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[D]]></category>
		<category><![CDATA[LDC]]></category>

		<guid isPermaLink="false">http://www.incasoftware.de/~kamm/projects/?p=72</guid>
		<description><![CDATA[The release 0.9.1 of LDC, the LLVM based compiler for the D programming language, contains the following major improvements: lots of bug fixes x86-64 support is mature inline asm improved (we now define D_Inline_Asm) cross-compilation support uses boehm-gc during compilation (x86-32 only) D specific optimizations: turn GC allocations to allocas if possible simplify or remove [...]]]></description>
				<content:encoded><![CDATA[<p>The release 0.9.1 of <a href="http://www.dsource.org/projects/ldc">LDC</a>, the <a href="http://www.llvm.org">LLVM</a> based compiler for the <a href="http://www.digitalmars.com/d">D programming language</a>, contains the following major improvements:<br />
<span id="more-72"></span></p>
<ul>
<li><a href="http://www.dsource.org/projects/ldc/report/15">lots of bug fixes</a></li>
<li>x86-64 support is mature</li>
<li>inline asm improved (we now define D_Inline_Asm)</li>
<li>cross-compilation support</li>
<li>uses boehm-gc during compilation (x86-32 only)</li>
<li>D specific optimizations:
<ul>
<li>turn GC allocations to allocas if possible</li>
<li>simplify or remove certain calls to D runtime functions</li>
</ul>
</li>
</ul>
<p>The command line interface of LDC now has added options in line with other LLVM based tools. Please use the ldmd wrapper if you want a drop-in replacement for DMD.</p>
<p><a href="http://www.incasoftware.de/~kamm/ldc/ldc-0.9.1-x86_32.tar.bz2">Linux x86-32 download</a><br />
<a href="http://www.incasoftware.de/~kamm/ldc/ldc-0.9.1-x86_64.tar.bz2">Linux x86-64 download</a> </p>
<p>Tomas Lindquist Olsen<br />
Christian Kamm<br />
Frits van Bommel<br />
Kelly Wilson</p>
]]></content:encoded>
			<wfw:commentRss>http://www.incasoftware.de/~kamm/projects/index.php/2009/05/27/ldc-091-released/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>LDC presentation video, Tango Conference 2008</title>
		<link>http://www.incasoftware.de/~kamm/projects/index.php/2009/02/28/ldc-presentation-tango-2008/</link>
		<comments>http://www.incasoftware.de/~kamm/projects/index.php/2009/02/28/ldc-presentation-tango-2008/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 15:27:07 +0000</pubDate>
		<dc:creator>Christian</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[D]]></category>
		<category><![CDATA[LDC]]></category>

		<guid isPermaLink="false">http://www.incasoftware.de/~kamm/projects/?p=53</guid>
		<description><![CDATA[Peter Modzelewski, member of the illustrious team0xf and an organizer of the Tango Conference 2008, has posted the video and slides of the talk on LDC Tomas Lindquist Olsen and I gave in September. The original summary was: The new D1 compiler based on the strong fundament of LLVM, the DMD frontend and the Tango [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://petermodzelewski.blogspot.com/">Peter Modzelewski</a>, member of the illustrious <a href="http://team0xf.com/">team0xf</a> and an organizer of the <a href="http://www.dsource.org/projects/tango/wiki/TangoConference2008">Tango Conference 2008</a>, has posted <a href="http://petermodzelewski.blogspot.com/2009/02/tango-conference-2008-ldc.html">the video and slides</a> of the talk on LDC Tomas Lindquist Olsen and I gave in September. </p>
<p>The original summary was:</p>
<blockquote><p>The new D1 compiler based on the strong fundament of LLVM, the DMD frontend and the Tango runtime is maturing rapidly. We&#8217;ll take a brief look at LLVM, report on LLVMDC&#8217;s status and discuss the future of the project.</p></blockquote>
<p>Have fun watching the video and thanks again to the organizers for an excellent conference.<br />
<span id="more-53"></span><br />
Here&#8217;s a short list of major changes since then: </p>
<ul>
<li>Almost immediately after the lecture we renamed the compiler to LDC, which has worked out very well and should simplify the next presentation by avoiding tounge twisting sentences that contain LLVM, LLVMDC, DMD and GDC in close proximity. </li>
<li>We <a href="http://www.incasoftware.de/~kamm/projects/index.php/2009/01/09/ldc-09-released/">released LDC 0.9</a> for x86-32 Linux in early January.</li>
<li>Support for x86-64 Linux has improved significantly due to contributions by Kelly Wilson and Frits van Bommel. Our platform support entry for it now reads: &#8220;no big open issues,<br />
dstress results pretty much the same as x86-32 Linux&#8221; &#8211; The next release will very likely include x86-64 Linux binaries!</li>
<li>LDC switched to <a href="http://www.llvm.org">LLVM 2.5</a>.</li>
<li>ABI conformance work continued: The D calling convention is fully implemented and naked functions are supported.</li>
<li>A lot of tickets have been closed; this includes a large amount of bugfixes, but also higher level issues like allowing template functions to be inlined and basic cross compilation.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.incasoftware.de/~kamm/projects/index.php/2009/02/28/ldc-presentation-tango-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LDC 0.9 released</title>
		<link>http://www.incasoftware.de/~kamm/projects/index.php/2009/01/09/ldc-09-released/</link>
		<comments>http://www.incasoftware.de/~kamm/projects/index.php/2009/01/09/ldc-09-released/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 23:21:47 +0000</pubDate>
		<dc:creator>Christian</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[D]]></category>
		<category><![CDATA[LDC]]></category>
		<category><![CDATA[LLVM]]></category>

		<guid isPermaLink="false">http://www.incasoftware.de/~kamm/projects/?p=28</guid>
		<description><![CDATA[The first version of LDC, the LLVM based compiler for version one of the D programming language has been released for x86-32 Linux. Get it here! We had already announced this release during the Tango conference in September (we hope the video of our presentation will be out soon), but &#8211; as was to be [...]]]></description>
				<content:encoded><![CDATA[<p>The first version of <a href="http://www.dsource.org/projects/ldc">LDC</a>, the <a href="http://www.llvm.org">LLVM</a> based compiler for version one of the <a href="http://www.digitalmars.com/d/">D programming language</a> has been released for x86-32 Linux. <a href="http://www.incasoftware.de/~kamm/ldc/ldc-0.9.tbz2">Get it here!</a><br />
<span id="more-28"></span><br />
We had already announced this release during the <a href="http://www.dsource.org/projects/tango/wiki/TangoConference2008">Tango conference</a> in September (we hope the video of our presentation will be out soon), but &#8211; as was to be expected &#8211; it took a bit longer than planned. </p>
<p>LDC ships with a precompiled <a href="http://www.dsource.org/projects/tango">Tango</a> rev 4237 and passes all except two of Tango&#8217;s unittests (io.digest.Md2 and text.locale.Posix fail). <a href="http://www.dsource.org/projects/dstress">DStress</a> <a href="http://www.incasoftware.de/~kamm/ldc/tests/index.html">results</a> also look favorable and the <a href="http://www.dsource.org/projects/gtkd">GtkD</a> demos work. The chances are good that your code will work with it too!</p>
<p>There are several known issues, the most severe being:</p>
<ul>
<li>LDC does not compile <a href="http://www.dsource.org/projects/DWT">DWT</a> successfully for unknown reasons</li>
<li><a href="http://www.dsource.org/projects/ldc/ticket/149">an LLVM 2.4 bug</a> sometimes leads to linker errors when unreachable code is optimized away</li>
<li><a href="http://www.dsource.org/projects/ldc/ticket/67">LDC doesn&#8217;t fully follow the D calling convention</a> (but it&#8217;s close!)</li>
<li>we inherit every DMD-frontend related bug in the <a href="http://d.puremagic.com/issues/">D bug tracker</a></li>
<li><a href="http://www.dsource.org/projects/ldc/report/1">&#8230; there are more in our tracker</a></li>
</ul>
<p>If you encounter a bug, please check our <a href="http://www.dsource.org/projects/ldc/report/1">bug tracker</a> and create a new ticket if the issue isn&#8217;t listed yet. Maybe you are feeling adventurous and want to try fixing it yourself; in that case take a look at our <a href="http://www.dsource.org/projects/ldc">getting started guide</a>.</p>
<p>LDC could <a href="http://www.dsource.org/projects/ldc/wiki/PlatformSupport">support other platforms</a>. Furthest along so far are</p>
<ul>
<li>x86-64 Linux: needs people to start fixing smaller bugs, exception bug (<a href="http://www.llvm.org/bugs/show_bug.cgi?id=3214">LLVM issue</a>)</li>
<li>x86-32 Mac: small runtime issues, needs tests</li>
<li>x86-32 Windows: exceptions not supported (LLVM issue)</li>
</ul>
<p>but support for these platforms won&#8217;t improve on its own! Several friendly people have offered their help &#8211; we need more of those!</p>
<p>For those with big CTFE memory needs, we have an <a href="http://www.incasoftware.de/~kamm/ldc/ldc-0.9-gc.tbz2">experimental version of LDC available</a> which has the compile-time garbage collector enabled. In the future, we&#8217;re going to experiment with a <a href="http://www.dsource.org/projects/ldc/ticket/125">forward reference hack</a>, but right now it still introduces too many regressions.</p>
<p>Feedback and questions are appreciated and should go to the <a href="http://groups.google.com/group/ldc-dev">mailing list</a>. Alternatively, we&#8217;re often seen in #ldc on FreeNode.</p>
<p>Tomas Lindquist Olsen, Christian Kamm</p>
]]></content:encoded>
			<wfw:commentRss>http://www.incasoftware.de/~kamm/projects/index.php/2009/01/09/ldc-09-released/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Exception handling in LDC using LLVM</title>
		<link>http://www.incasoftware.de/~kamm/projects/index.php/2008/08/19/exception-handling-in-llvmdc-using-llvm/</link>
		<comments>http://www.incasoftware.de/~kamm/projects/index.php/2008/08/19/exception-handling-in-llvmdc-using-llvm/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 13:24:39 +0000</pubDate>
		<dc:creator>Christian</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[D]]></category>
		<category><![CDATA[exception handling]]></category>
		<category><![CDATA[LDC]]></category>
		<category><![CDATA[LLVM]]></category>

		<guid isPermaLink="false">http://www.incasoftware.de/~kamm/projects/?p=9</guid>
		<description><![CDATA[Exception handling is an integral part of the D programming language. Naturally LDC, aiming to be a complying compiler, needs to provide it. Here I describe how exactly user code, generated LLVM IR, the unwinding library and the LDC runtime interact to make it all work &#8211; at least on x86 Linux. There is some [...]]]></description>
				<content:encoded><![CDATA[<p>Exception handling is an integral part of the D programming language. Naturally LDC, aiming to be a complying compiler, needs to provide it. Here I describe how exactly user code, generated LLVM IR, the unwinding library and the LDC runtime interact to make it all work &#8211; at least on x86 Linux.<br />
<span id="more-9"></span><br />
There is some <a href="http://llvm.org/docs/ExceptionHandling.html">documentation on exception handling with LLVM</a> and the pages linked from there contain further information, in particular the <a href="http://www.codesourcery.com/cxx-abi/abi-eh.html">details on the unwinding runtime</a>. Unfortunately, examples of actual use are hard to find, so trial and error has played a major role in learning the workings of LLVM EH. I&#8217;ll try to present a complete example here, but will assume you&#8217;ve at least skimmed through both documents.</p>
<p>First, the throw statement. Its basic job is simple: invoke the exception handling runtime by calling _Unwind_RaiseException with the address of an _Unwind_Exception struct. This struct contains, among some private data, an eight-byte exception class to identify the language and vendor it originates from (for LDC we set it to &#8220;D1\0\0&#8243; and &#8220;LLDC&#8221;) and a cleanup callback. Since it is necessary to communicate the exception object that is being thrown to the handler code, this struct is embedded in a larger one. Later, the address of this surrounding struct can be computed from the address of the unwind_info member.</p>
<p>Consequently, the outer struct looks like this</p>
<p><code>struct _d_exception {<br />
&nbsp;&nbsp;Object exception_object;<br />
&nbsp;&nbsp;_Unwind_Exception unwind_info;<br />
}</code></p>
<p>and the code to invoke the unwinding runtime is straightforward:</p>
<p><code>void _d_throw_exception(Object e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;if (e !is null) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_d_exception* exc_struct = new _d_exception;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exc_struct.unwind_info.exception_class[0..4] = &quot;LLDC&quot;;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exc_struct.unwind_info.exception_class[4..8] = &quot;D1\0\0&quot;;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exc_struct.exception_object = e;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_Unwind_RaiseException(&amp;exc_struct.unwind_info);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;abort();<br />
}</code></p>
<p>What happens on a throw is essentially the following: </p>
<ul>
<li> _Unwind_RaiseException traces the stack by looking at the unwind tables and, for each frame that has a landing pad set up, calls a &#8216;personality function&#8217;, asking it whether it can handle the exception object. </li>
<li>Once one is found, it traces the stack again, this time telling the personality functions to execute the code in any intervening finally blocks. </li>
<li>In the end, it calls the function for the final landing pad with arguments indicating that control is to be transferred to the catch handler.</li>
</ul>
<p>Luckily, exception handling in D can be implemented using only a single personality function for all landing pads. This personality function decides what to do for each individual landing pad by parsing the language specific area of the unwind data. This area contains three tables: the callsite table, the action table and the classinfo table. </p>
<ul>
<li>The callsite table maps instruction address ranges to indices into the action table. These address ranges mark the beginning and end of the code in a try block.</li>
<li>The action table contains chains of indices into the classinfo table and values that will be used to identify the action to the handler code later. An action corresponds to a catch or finally block.</li>
<li>The classinfo table holds the addresses to the classinfos of each class used in a catch parameter.</li>
</ul>
<p>When the personality function is called and given the context of a certain landing pad, it looks up the instruction pointer, finds the right entry in the callsite table and then walks the corresponding action chain. For each possible action, it checks whether the thrown exception object is derived from the class specified by the respective classinfo. Once a match is found, it knows that this landing pad is responsible for the exception. When it is called again with instructions to transfer control to the handler, the personality function passes the exception object and the index from the action table to the hander code.</p>
<p>If you&#8217;re interested in the code that accomplishes this, take a look <a href="http://www.dsource.org/projects/ldc/browser/runtime/internal/eh.d">here</a>.</p>
<p>The last step required to make EH work is to provide the handler code and to write out the correct unwind tables. Let&#8217;s look at some user code and what it is essentially turned into by LDC (of course the actual output is LLVM IR). The situation grows considerably more complex when there are nested try-catch-finallys in the same stack frame, but I hope this snippet illustrates the basic ideas. </p>
<table width="100%">
<tr>
<td><code><br />
try <br />
{<br />
&nbsp;&nbsp;code_try();<br />
}<br />
catch(ExceptionClass ec)<br />
{<br />
&nbsp;&nbsp;code_catch(ec);<br />
}<br />
finally<br />
{<br />
&nbsp;&nbsp;code_finally();<br />
}<br />
</code></td>
<td><code><br />
// this is an invoke with <br />
// &#039;handler&#039; as exception target<br />
code_try();<br />
goto end;<br />
&nbsp;<br />
handler:<br />
&nbsp;<br />
ehptr = llvm.eh.exception();<br />
ehsel = llvm.eh.selector(<br />
&nbsp;&nbsp;&nbsp;&nbsp;ehptr, <br />
&nbsp;&nbsp;&nbsp;&nbsp;&amp;_d_eh_personality, <br />
&nbsp;&nbsp;&nbsp;&nbsp;ExceptionClass.classinfo, <br />
&nbsp;&nbsp;&nbsp;&nbsp;0);<br />
&nbsp;<br />
switch(ehsel)<br />
{<br />
&nbsp;&nbsp;case 1:<br />
&nbsp;&nbsp;&nbsp;&nbsp;code_catch(ehptr);<br />
&nbsp;&nbsp;&nbsp;&nbsp;goto end;<br />
&nbsp;&nbsp;default: // ehsel == 0<br />
&nbsp;&nbsp;&nbsp;&nbsp;code_finally();<br />
&nbsp;&nbsp;&nbsp;&nbsp;_Unwind_Resume(&amp;ehptr.unwind_info);<br />
}<br />
// unreachable<br />
&nbsp;<br />
end:<br />
code_finally();<br />
</code></td>
</tr>
</table>
<p>The llvm.eh.* intrinsics get the exception object and the action table index that are passed in by the personality function as mentioned above. But there&#8217;s more going on here: the selector intrinsic also tells LLVM what the data in the unwind tables should be. In particular, the personality function and the exception classinfos are set here. The zero indicates the finally block. The call to code_try() has been turned into an invoke, which makes LLVM emit an entry in the callsite table for it.</p>
<p>As you can see, the unwinding runtime and LLVM code generator are tied closely via the two intrinsics and thus supporting other runtimes such as Windows structured exception handling will be nigh-impossible without changes to LLVM. Hopefully, getting llvm-gcc to support exception handling on Windows will be enough of an incentive for the LLVM team to provide that feature eventually.</p>
<p>Another thing to bear in mind is that LLVM&#8217;s exception support is, at the moment, very C++ specific. The code generator can fill the language specific data area only with the three C++ style tables mentioned above. Fortunately, D&#8217;s exceptions are similar enough that we can get the right behavior by inserting suitable values into these tables.</p>
<p>For now, the implementation in LDC has only been tested on x86 Linux, though the PowerPC target should work as well. EH on x86-64 Linux will supposedly be enabled in the next LLVM release. The remaining issues should be solved as LLVM matures, enabling LDC to provide correct exception handling support on more platforms.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.incasoftware.de/~kamm/projects/index.php/2008/08/19/exception-handling-in-llvmdc-using-llvm/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Compilers for the D programming language: DMD, GDC and LDC</title>
		<link>http://www.incasoftware.de/~kamm/projects/index.php/2008/07/09/compilers-for-the-d-programming-language/</link>
		<comments>http://www.incasoftware.de/~kamm/projects/index.php/2008/07/09/compilers-for-the-d-programming-language/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 17:12:26 +0000</pubDate>
		<dc:creator>Christian</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[D]]></category>
		<category><![CDATA[LDC]]></category>
		<category><![CDATA[LLVM]]></category>

		<guid isPermaLink="false">http://www.incasoftware.de/~kamm/projects/?p=8</guid>
		<description><![CDATA[Currently, users of the D programming language can essentially choose between two mature compilers: DMD and GDC. I&#8217;ve started helping with the development of a third, LDC, and think the first public alpha version can&#8217;t be far off. In this post, I&#8217;ll give a short overview of the compilers available and say a few words [...]]]></description>
				<content:encoded><![CDATA[<p>Currently, users of the <a href="http://www.digitalmars.com/d/">D programming language</a> can essentially choose between two mature compilers: DMD and GDC. I&#8217;ve started helping with the development of a third, LDC, and think the first public alpha version can&#8217;t be far off. In this post, I&#8217;ll give a short overview of the compilers available and say a few words about LDC.<br />
<span id="more-8"></span><br />
First, there is the reference compiler DMD that is maintained and improved by D&#8217;s inventor Walter Bright. It has a stable branch for D 1.0 as well as a bleeding edge counterpart for version 2.0 of the D specification that&#8217;s still under development. One part of it, the front-end responsible for parsing code, building a syntax tree and running semantics, is open source, available under the GPL or Artistic license. Its back-end, however, is not available and thus only Walter can compile DMD.</p>
<p>This is not a failing in itself. Indeed most users will never have the need or desire to compile their compiler from source. Yet it also prohibits testing patches for the front-end, makes debugging nigh impossible and generally complicates helping with DMD&#8217;s development.</p>
<p>The alternative &#8211; and often the only alternative if your target is not x86 Linux or Windows &#8211; is <a href="http://dgcc.sourceforge.net/">GDC</a>. David Friedman took the open front-end and tied it to the GCC back-end, he even supports D 2.0 to some extend. While it works well and is fully open source, it has, unfortunately, remained a one-man effort. For me it was a combination of GCC&#8217;s daunting code base and the fact that there had been no activity for several months that stopped me from helping out.</p>
<p>Instead I eventually joined a different project: <a href="http://www.dsource.org/projects/ldc/">LDC</a>. It is similar to GDC in that it also takes the open DMD front-end and aims to combine it with an equally free code generator. The back-end, though, is different: as the name suggests LDC emits <a href="http://www.llvm.org/">LLVM</a> bitcode, which can be compiled to native code, but could also be used for Just-in-Time compilation among other things. I&#8217;m also happy to say that even though LLVM is a large and ambitious project, it remains surprisingly easy to learn and work with.</p>
<p>Almost the first thing I did when I started contributing to LDC a few months back, was to integrate Thomas Kühne&#8217;s exhaustive <a href="http://dstress.kuehne.cn/">DStress test suite</a> with LDC: it is very useful for finding bugs and regressions and can even serve as a sort of <a href="http://www.incasoftware.de/~kamm/ldc/tests/index.html">crude progress indicator</a> (crude because there is no relationship between number of bugs and number of tests; there are about 1000 tests for inline assembly and only 30 or so for exception handling). As you can see, there are still a handful (<a href="http://www.incasoftware.de/~kamm/ldc/tests/index.html">517 at the last count</a>) of regressions with respect to DMD, but their number has been decreasing steadily.</p>
<p>In the last months, Tomas Lindquist Olsen, who started the project about a year ago and is responsible for almost all of its existing functionality, and I have added the last major missing parts to the compiler: inline assembly (thanks to David Friedman for the asm parsing and rewriting code!), exception handling and the synchronized statement are now supported. With these out of the way, goals for the future are squashing bugs, fixing some linking issues and getting LDC to work properly on Windows.</p>
<p>Speaking of other platforms: As Tomas and I both develop on x86 Linux, other configurations didn&#8217;t get much testing yet. Some people reported partial successes on <a href="http://dsource.org/forums/viewtopic.php?t=4019&#038;sid=7972120d03053e1f4619d7fef4832030">FreeBSD</a> and <a href="http://dsource.org/forums/viewtopic.php?t=3296&#038;sid=7972120d03053e1f4619d7fef4832030">Sparc</a> but more testers would be welcome. You can generally contact us by email or in #ldc on freenode. All in all, we&#8217;ve made great progress and if we can keep it up, LDC will be in the same league as DMD and GDC soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.incasoftware.de/~kamm/projects/index.php/2008/07/09/compilers-for-the-d-programming-language/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Game In A Day: PyDogs</title>
		<link>http://www.incasoftware.de/~kamm/projects/index.php/2008/04/12/game-in-a-day-pydogs/</link>
		<comments>http://www.incasoftware.de/~kamm/projects/index.php/2008/04/12/game-in-a-day-pydogs/#comments</comments>
		<pubDate>Sat, 12 Apr 2008 14:42:58 +0000</pubDate>
		<dc:creator>Christian</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[game in a day]]></category>
		<category><![CDATA[game programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.incasoftware.de/~kamm/projects/?p=6</guid>
		<description><![CDATA[Creating computer games is usually envisioned as a long and intricate process, requiring years of work by dedicated teams of professionals. Even in the open source world game projects often end up unfinished and abandoned after months of work have gone into them. The Game-in-a-Day events people have reported on during the last years defy [...]]]></description>
				<content:encoded><![CDATA[<p>Creating computer games is usually envisioned as a long and intricate process, requiring years of work by dedicated teams of professionals. Even in the open source world game projects often end up unfinished and abandoned after months of work have gone into them. The Game-in-a-Day events <a title="Zero Lodge of ALGDS" href="http://zero.algds.org">people have reported on</a> during the last years defy that paradigm: during these sessions the main goal is to get things done and to get them done quickly.</p>
<p>This refreshing change from usual development practice along with it being a challenge as well as a good time with friends have made Game-in-a-Day events appealing enough for whole communities (edit: link removed, unfortunately dead by now) to have formed around them. Last October I decided to mail all my friends and host one myself.<br />
<span id="more-6"></span><br />
After the usual scheduling woes Roman Pohrt, Torsten Gellert, Felix Stürmer and Anne Pohrt (part-time) brought their computers and participated. Julian Bischof couldn&#8217;t bring his equipment but created some music for us remotely.</p>
<p>As our group contained no real artists, it was clear from the start that our result would not look particularly pretty. On the other hand, everyone had some programming experience, albeit evenly distributed between C++, D, Python and Java. Consequently, we figured we would try to make up for it in gameplay and features. That also meant we could skip the obligatory asteroids clone and tackle something more complicated.</p>
<p>The collective search for a game idea finished quickly. We were going to improve on <a href="http://en.wikipedia.org/wiki/C-Dogs">C-Dogs</a> by supporting multiple players on one computer. We also planned to replace its line-of-sight effect with my (then recent) <a href="http://www.incasoftware.de/~kamm/projects/index.php/2007/09/08/soft-shadows-2d">soft shadows implementation</a>, hoping to overshadow our lack of art by composing our levels from dark hallways and by using lots of special effects.</p>
<p>Next was the choice of programming language and libraries. Since there was no common denominator, we selected <a href="http://www.python.org/">Python</a> and <a href="http://www.pyglet.org/">pyglet</a>. It turned out to be a very good decision. Even though half of us had not used the language before, the essentials were easy to pick up and soon everyone was able to contribute: At the end of the first day I made our previously hardcoded weapons system more modular, then went to bed. When I pulled the changes next morning, there were four different guns (and an electro zapper) and someone had added code so they could be put down and picked up.</p>
<p>Our source management tool, <a href="http://www.selenic.com/mercurial">mercurial</a>, was employed less successfully. We had one or two cases where we accidentally automerged into the wrong direction and propagated those incorrect changes through our repositories. However, I maintain that these issues could have been avoided if we had been more familiar with mercurial and that a centralized version control system would have had even more trouble with the highly concurrent nature of our development.</p>
<p>This is when we got started. After about 16 hours of work (spread out over the weekend) we ended up with the current feature set:</p>
<ul>
<li>up to three players can play simultaneously</li>
<li>they can move, shoot and bump into walls</li>
<li>the levels are randomly generated</li>
<li>there are three kinds of monsters, all with different behaviors&lt;/li>
<li>players have hitpoints and can die</li>
<li>five different types of weapons lie scattered around the levels and can be picked up</li>
<li>background music plays</li>
<li>finding the blue goal portal teleports to the next level</li>
</ul>
<p>From the start we structured our efforts pretty well. At least one person was always working on the next logical step or an absolutely necessary subsystem. We also implemented quick placeholders for things that were to come later. For instance, it took a lot more effort to get solid random level generation working than we expected, but due to a square placeholder level it did not delay the development of other components at all.</p>
<p>Nevertheless, there are some things that did not get implemented or fixed in time:</p>
<ul>
<li>the game is too slow</li>
<li>collision detection breaks often</li>
<li>level generation sometimes produces incorrect geometry</li>
<li>line of sight and shadows are missing</li>
<li>there is no menu</li>
</ul>
<p>The slowness is definitely our major issue. We started profiling and sped up some parts of the code considerably, but it wouldn&#8217;t help. In the end, we even concluded that while Python had been an ideal choice from the perspective of getting everyone involved and productive quickly, it had also failed us due to its lack of speed.<br />
In hindsight, I&#8217;m pretty sure we were wrong in our assumption that some Python code was to blame for the speed deficiency. If I comment out our level drawing, which is basically a set of OpenGL calls, my framerate increases dramatically. Thus this is probably some OpenGL usage or hardware acceleration issue. (edit: installing the new pyglet 1.1 makes the game run almost as fast as it did with the level drawing commented out; it was a library problem after all)</p>
<p>While the unreliable collision detection is merely an effect of the low framerate, the missing menu is excusable and the incorrect level generation is rare and only a graphical glitch, the lack of shadows and line of sight is painfully obvious. We just didn&#8217;t get around to implementing it. It seemed to not be essential for basic gameplay and hence was postponed time and again. Without it, however, our game is missing a key aspect; something that was supposed to make it unique and different. </p>
<p>Even with these defects, I&#8217;m amazed by what we have achieved in such a short time. Especially considering that we started from scratch with a programming language half the team was not familiar with. It was a pity we had to stop when the result was not yet what we had envisioned, but that&#8217;s the premise of these events: try to get as far as you can before the time is up, then stop and take a look at what you&#8217;ve done.</p>
<p>We certainly had a good time and I plan on doing this again next autumn.</p>
<p>Screenshots:</p>
<table width="100%">
<tr>
<td>
<a href="http://www.incasoftware.de/~kamm/projects/wp-content/articles/game-in-a-day-pydogs/shot0.png"><img class="center" src="http://www.incasoftware.de/~kamm/projects/wp-content/articles/game-in-a-day-pydogs/shot0-small.png" alt="screenshot" /></a>
</td>
<td>
<a href="http://www.incasoftware.de/~kamm/projects/wp-content/articles/game-in-a-day-pydogs/shot1.png"><img class="center" src="http://www.incasoftware.de/~kamm/projects/wp-content/articles/game-in-a-day-pydogs/shot1-small.png" alt="screenshot" /></a>
</td>
<td>
<a href="http://www.incasoftware.de/~kamm/projects/wp-content/articles/game-in-a-day-pydogs/shot2.png"><img class="center" src="http://www.incasoftware.de/~kamm/projects/wp-content/articles/game-in-a-day-pydogs/shot2-small.png" alt="screenshot" /></a>
</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.incasoftware.de/~kamm/projects/index.php/2008/04/12/game-in-a-day-pydogs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
