Christian's Projects » programming http://www.incasoftware.de/~kamm/projects Mon, 26 Jun 2017 20:29:06 +0000 en-US hourly 1 http://wordpress.org/?v=3.5.1 Better diffs in gerrit code review http://www.incasoftware.de/~kamm/projects/index.static/2013/10/03/better-diffs-in-gerrit-code-review/ http://www.incasoftware.de/~kamm/projects/index.static/2013/10/03/better-diffs-in-gerrit-code-review/#comments Thu, 03 Oct 2013 06:25:54 +0000 Christian http://www.incasoftware.de/~kamm/projects/?p=177 Code review is critical for quality code. There are many ways to do it and many tools to help with the process, such as gerrit, phabricator, reviewboard and github code review.

We use gerrit, which is nice. But there’s one annoying problem: when a change author rebases a change and pushes the rebased patch for review the patch set diff is nearly useless. That either makes the reviewer’s job much harder or leads to people avoiding rebases. Unfortunately the existing ticket about the issue is four years old.

I spent some time on fixing it: Our gerrit shows a hunk in the diff from A to B only if it isn’t in the diff between their parent commits, or if there is a conflict. This reliably hides unrelated changes, like those from rebases, while erring on the side of showing too much in case of conflicting edits or large hunks.

Unfortunately the patch isn’t ready for upstream: It doesn’t work with unified diff view or with gerrit’s new change screen (the new side-by-side view can’t have the left and right side be different without highlighting the change). Also it’s not configurable.

Feel free to contact me if you’re interested in the details or want to pick this up!

]]>
http://www.incasoftware.de/~kamm/projects/index.static/2013/10/03/better-diffs-in-gerrit-code-review/feed/ 0
Improving REQ sockets in ZMQ 4 http://www.incasoftware.de/~kamm/projects/index.static/2013/09/25/improving-req-sockets-in-zmq-4/ http://www.incasoftware.de/~kamm/projects/index.static/2013/09/25/improving-req-sockets-in-zmq-4/#comments Wed, 25 Sep 2013 10:12:30 +0000 Christian http://www.incasoftware.de/~kamm/projects/?p=166 I contributed two new socket options to libzmq 4.0: ZMQ_REQ_CORRELATE and ZMQ_REQ_RELAXED. They simplify usage of REQ sockets in situations where you would have had to reset the whole socket or switch to a DEALER socket with earlier versions. More specifically, you can now send a new request on a REQ socket when you’ve given up on getting a response for the preceding request.

ZMQ defines several socket types that can be used to implement different messaging patterns. REQ sockets provide the basic “send a request, wait for the response, send the next request, …” pattern and are used frequently. In practice things break though and you’re not guaranteed to actually get a response to a request. The old REQ sockets strictly enforce the send/recv/send/recv/… cycle and have to be completely reset when a timeout is detected.

This is unintuitive and people have commented on it in several times.

With ZMQ_REQ_RELAXED enabled, calling send() while waiting for a reply becomes valid. It will give up on the old request and send a new one.

When enabling ZMQ_REQ_RELAXED, always enable ZMQ_REQ_CORRELATE as well. It ensures the receive call only reports the reply to the latest request. Otherwise, if the old reply comes in after all, you might receive that and think it’s the reply for the later request.

The new option only changes the behavior for send() in a state where it would previously have returned an EFSM error. It is safe to enable it in nearly all cases. libzmq wants to make it as easy as possible for users to migrate to new versions, however, so it is not enabled by default.

Contributing to libzmq has been simple and rewarding. They use a interesting “merge first, review later” process which sounds problematic – but which seems to work for them and definitely made me feel welcome as a contributor.

]]>
http://www.incasoftware.de/~kamm/projects/index.static/2013/09/25/improving-req-sockets-in-zmq-4/feed/ 0
Getting more out of the range-based for statement in C++11 http://www.incasoftware.de/~kamm/projects/index.static/2013/03/06/getting-more-out-of-the-range-based-for-statement-in-c11/ http://www.incasoftware.de/~kamm/projects/index.static/2013/03/06/getting-more-out-of-the-range-based-for-statement-in-c11/#comments Wed, 06 Mar 2013 15:14:10 +0000 Christian http://www.incasoftware.de/~kamm/projects/?p=132 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 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.

For example, the regular counting loops

for (auto i = begin, e = end; i < e; ++i)
for (size_t i = 0, e = container.size(); i < e; ++i)

can be written as

for (const auto i : range(begin, end))
for (const auto i : indices(container))

if you define indices() and range() the right way.

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).

You can do a lot with the range-based for statement. What I have found useful so far is:

  • range(begin, end): for (auto i = begin, e = end; i != e; ++i)
  • range(end): same as range(0, end)
  • indices(container): same as range(container.size())
  • keys(container_of_pairs): shorthand for getting only value.first
  • values(container_of_pairs): shorthand for getting only value.second

The code is available on github 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, …

The approach has its limits, and one of them is exposed when you implement something like enumerate():

for (auto e : enumerate(c)) {
    // e.index is the current index
    // e.value is the current value
}

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:

for (const auto e : c) // e is a const copy
for (auto & e : c) // e is a mutable reference

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.

That is why I decided to split enumerate() into two functions. enumerate_byref() always grabs ‘value’ by reference and enumerate_byval() makes a copy. Usage then becomes:

for (const auto e : enumerate_byref(container))
    e.value = true; // ok, modifies container!
for (auto e : enumerate_byval(container))
    e.value = true; // ok, changes copy

Which is far from the ideal syntax, but at least it is explicit about what is going on.

Thanks to my employer, CeleraOne GmbH, for permitting me to publish this.

]]>
http://www.incasoftware.de/~kamm/projects/index.static/2013/03/06/getting-more-out-of-the-range-based-for-statement-in-c11/feed/ 3
Recent activity http://www.incasoftware.de/~kamm/projects/index.static/2011/11/03/recent-activity/ http://www.incasoftware.de/~kamm/projects/index.static/2011/11/03/recent-activity/#comments Thu, 03 Nov 2011 19:52:03 +0000 Christian http://www.incasoftware.de/~kamm/projects/?p=125 Over the last year I’ve been focusing on Qt Creator and have done several posts on the Qt Labs blog:

]]>
http://www.incasoftware.de/~kamm/projects/index.static/2011/11/03/recent-activity/feed/ 0
Star Guard level editor http://www.incasoftware.de/~kamm/projects/index.static/2010/05/06/star-guard-level-editor/ http://www.incasoftware.de/~kamm/projects/index.static/2010/05/06/star-guard-level-editor/#comments Thu, 06 May 2010 17:22:28 +0000 Christian http://www.incasoftware.de/~kamm/projects/?p=85 I spent the last few evenings working on a fun side project which is now complete: a level editor for Loren Schmidt’s Star Guard.

first level of Star Guard

the first level of Star Guard

the same scene in the editor

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 to do something interesting with QML.
  • Star Guard is a great game and stores its levels in friendly, easily accessible XML files.
  • When I emailed Loren about the project, he allowed me to use some of the game’s artwork.

The whole thing was surprisingly easy to do, took a couple of days and amounted to less than 1200 lines of code – one half of it QML and the other C++.

If you don’t know about QML: It’s a fairly new part of the Qt framework, essentially an extension of JavaScript to build user interfaces declaratively – and great fun to work with. All the details can be found on its documentation page and if you want to get started, the Qt 4.7 beta1 packages have just been released.

The editor uses QML to describe every aspect of the user interface. It’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’d be very easy to redo the user interface now – 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.

Of course there’s the disadvantage of not following the native look and feel. But no one ever complains that most games don’t use it – and I don’t think it’s necessary for a level editor to look just like your spreadsheet application either.

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.

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 Boost Software License 1.0.

Linux binary (64 bit) and source code (12 MB)
Windows binary and source code (10 MB)
source code only (.tar.bz2, 20 kB)
source code only (.zip, 30 kB)

]]>
http://www.incasoftware.de/~kamm/projects/index.static/2010/05/06/star-guard-level-editor/feed/ 0
LDC 0.9.2 released http://www.incasoftware.de/~kamm/projects/index.static/2010/03/19/ldc-0-9-2-released/ http://www.incasoftware.de/~kamm/projects/index.static/2010/03/19/ldc-0-9-2-released/#comments Fri, 19 Mar 2010 08:29:49 +0000 Christian http://www.incasoftware.de/~kamm/projects/?p=77 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 Bommel, Kelly Wilson, Leandro Lucarella, Matti Niemenmaa, Moritz Warning, Robert Clipsham, Tomas Lindquist Olsen and me.

Linux x86-64 download

]]>
http://www.incasoftware.de/~kamm/projects/index.static/2010/03/19/ldc-0-9-2-released/feed/ 3
LDC 0.9.1 released http://www.incasoftware.de/~kamm/projects/index.static/2009/05/27/ldc-091-released/ http://www.incasoftware.de/~kamm/projects/index.static/2009/05/27/ldc-091-released/#comments Wed, 27 May 2009 18:25:54 +0000 Christian http://www.incasoftware.de/~kamm/projects/?p=72 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 certain calls to D runtime functions

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.

Linux x86-32 download
Linux x86-64 download

Tomas Lindquist Olsen
Christian Kamm
Frits van Bommel
Kelly Wilson

]]>
http://www.incasoftware.de/~kamm/projects/index.static/2009/05/27/ldc-091-released/feed/ 3
LDC presentation video, Tango Conference 2008 http://www.incasoftware.de/~kamm/projects/index.static/2009/02/28/ldc-presentation-tango-2008/ http://www.incasoftware.de/~kamm/projects/index.static/2009/02/28/ldc-presentation-tango-2008/#comments Sat, 28 Feb 2009 15:27:07 +0000 Christian http://www.incasoftware.de/~kamm/projects/?p=53 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 runtime is maturing rapidly. We’ll take a brief look at LLVM, report on LLVMDC’s status and discuss the future of the project.

Have fun watching the video and thanks again to the organizers for an excellent conference.

Here’s a short list of major changes since then:

  • 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.
  • We released LDC 0.9 for x86-32 Linux in early January.
  • 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: “no big open issues,
    dstress results pretty much the same as x86-32 Linux” – The next release will very likely include x86-64 Linux binaries!
  • LDC switched to LLVM 2.5.
  • ABI conformance work continued: The D calling convention is fully implemented and naked functions are supported.
  • 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.
]]>
http://www.incasoftware.de/~kamm/projects/index.static/2009/02/28/ldc-presentation-tango-2008/feed/ 0
LDC 0.9 released http://www.incasoftware.de/~kamm/projects/index.static/2009/01/09/ldc-09-released/ http://www.incasoftware.de/~kamm/projects/index.static/2009/01/09/ldc-09-released/#comments Thu, 08 Jan 2009 23:21:47 +0000 Christian http://www.incasoftware.de/~kamm/projects/?p=28 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 – as was to be expected – it took a bit longer than planned.

LDC ships with a precompiled Tango rev 4237 and passes all except two of Tango’s unittests (io.digest.Md2 and text.locale.Posix fail). DStress results also look favorable and the GtkD demos work. The chances are good that your code will work with it too!

There are several known issues, the most severe being:

If you encounter a bug, please check our bug tracker and create a new ticket if the issue isn’t listed yet. Maybe you are feeling adventurous and want to try fixing it yourself; in that case take a look at our getting started guide.

LDC could support other platforms. Furthest along so far are

  • x86-64 Linux: needs people to start fixing smaller bugs, exception bug (LLVM issue)
  • x86-32 Mac: small runtime issues, needs tests
  • x86-32 Windows: exceptions not supported (LLVM issue)

but support for these platforms won’t improve on its own! Several friendly people have offered their help – we need more of those!

For those with big CTFE memory needs, we have an experimental version of LDC available which has the compile-time garbage collector enabled. In the future, we’re going to experiment with a forward reference hack, but right now it still introduces too many regressions.

Feedback and questions are appreciated and should go to the mailing list. Alternatively, we’re often seen in #ldc on FreeNode.

Tomas Lindquist Olsen, Christian Kamm

]]>
http://www.incasoftware.de/~kamm/projects/index.static/2009/01/09/ldc-09-released/feed/ 11
Exception handling in LDC using LLVM http://www.incasoftware.de/~kamm/projects/index.static/2008/08/19/exception-handling-in-llvmdc-using-llvm/ http://www.incasoftware.de/~kamm/projects/index.static/2008/08/19/exception-handling-in-llvmdc-using-llvm/#comments Tue, 19 Aug 2008 13:24:39 +0000 Christian http://www.incasoftware.de/~kamm/projects/?p=9 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 – at least on x86 Linux.

There is some documentation on exception handling with LLVM and the pages linked from there contain further information, in particular the details on the unwinding runtime. 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’ll try to present a complete example here, but will assume you’ve at least skimmed through both documents.

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 “D1\0\0″ and “LLDC”) 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.

Consequently, the outer struct looks like this

struct _d_exception {
  Object exception_object;
  _Unwind_Exception unwind_info;
}

and the code to invoke the unwinding runtime is straightforward:

void _d_throw_exception(Object e) {
    if (e !is null) {
        _d_exception* exc_struct = new _d_exception;
        exc_struct.unwind_info.exception_class[0..4] = "LLDC";
        exc_struct.unwind_info.exception_class[4..8] = "D1\0\0";
        exc_struct.exception_object = e;
        _Unwind_RaiseException(&exc_struct.unwind_info);
    }
    abort();
}

What happens on a throw is essentially the following:

  • _Unwind_RaiseException traces the stack by looking at the unwind tables and, for each frame that has a landing pad set up, calls a ‘personality function’, asking it whether it can handle the exception object.
  • Once one is found, it traces the stack again, this time telling the personality functions to execute the code in any intervening finally blocks.
  • 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.

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.

  • 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.
  • 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.
  • The classinfo table holds the addresses to the classinfos of each class used in a catch parameter.

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.

If you’re interested in the code that accomplishes this, take a look here.

The last step required to make EH work is to provide the handler code and to write out the correct unwind tables. Let’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.


try
{
  code_try();
}
catch(ExceptionClass ec)
{
  code_catch(ec);
}
finally
{
  code_finally();
}

// this is an invoke with
// 'handler' as exception target
code_try();
goto end;
 
handler:
 
ehptr = llvm.eh.exception();
ehsel = llvm.eh.selector(
    ehptr,
    &_d_eh_personality,
    ExceptionClass.classinfo,
    0);
 
switch(ehsel)
{
  case 1:
    code_catch(ehptr);
    goto end;
  default: // ehsel == 0
    code_finally();
    _Unwind_Resume(&ehptr.unwind_info);
}
// unreachable
 
end:
code_finally();

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’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.

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.

Another thing to bear in mind is that LLVM’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’s exceptions are similar enough that we can get the right behavior by inserting suitable values into these tables.

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.

]]>
http://www.incasoftware.de/~kamm/projects/index.static/2008/08/19/exception-handling-in-llvmdc-using-llvm/feed/ 7