150 points · by adamnemecek · 10y ago · 68 comments
Support for Motorola 68000 has never been finished but this guy's abandoned fork offers an interesting glimpse into how one might start going about it though:
https://github.com/kwaters/llvm-m68k/commits/m68k
A bit over my head for now but I'm using this as a fun occasion to get back into "proper" computer science. Dabbling is my favourite learning mode so consulting the LLVM Programmer's Manual while hacking on a more isolated and really simple task is something I'm very much looking forward to at this point.
Still, if there is anybody with more knowledge who could chip in a bit on supporting m68k or LLVM in general I'd be super curious to read about it :)
On the list of "stuff llvm got wrong", i feel like this wouldn't even make the top hundred.
I think the only situation where exceptions help is in constructors, but there are better solutions to that problem, like explicit object construction methods that can return an error.
I think Go's approach to error handling is the best - explicit and in context. Rust's `Result` looks quite nice though I haven' tried it yet.
I've said it here before, but this is my classification of errors that exceptions are commonly used for:
1) Programming error: null pointer exception, library invariants broken, etc. These generally shouldn't be caught, and could arguably be replaced by fatal assertions, but that's typically not a polite thing to do in a library.
2) Non-deterministic failure condition: something outside the control of the calling code failed in a way that it cannot predict ahead of time. A network connection broke, a file was deleted unexpectedly, a device was detached, database server died, etc. There's something of an argument for annotating functions that can fail this way in the manner of Java's checked exceptions and it has a strong relationship with the IO monad in Haskell (and similar problems with encapsulation and hiding implementation details). These kinds of errors can sometimes be handled, and handling them with exceptions isn't much better or worse than error codes. An advantage of exceptions is that they makes it easy to abort the whole task and go back to the request handler (server code) or event dispatch loop (UI code). Exceptions can carry more state than error codes, too.
3) Business logic problem: the user, for want of a better term, of the program tried to do something that is not allowed / possible, and the program needs to abort the current task and inform the user of the problem. Exceptions are as good a means of aborting as most others. These should only be caught at the top level, whereupon they can be converted into something for human / 422 response / whatever consumption.
In almost all cases, you cannot proceed and there is nothing you can do in reaction to the error. Aborting and unwinding the call stack is usually the correct thing to do.
I haven't used Go (I'm about to for a small project, though) so I can't comment on the "if err != nil" idiom, but it seems like a step back by encouraging code duplication where it's not needed. I'm a fan of the pattern matching in rust, but it's still rather orthogonal to exceptions as it allows you to deal with an error when you try to access state whereas exceptions are most useful when they appear mid operation.
Either your function can complete its task successfully, in which case it satisfies whatever invariants are required of it and returns accordingly, or it can’t, in which case it can throw an exception to indicate that inability to return successfully. In the success case, the calling code can then continue based on whatever returned value and/or side effects the function was supposed to provide. In the failure case, the calling code may well fail itself if it can also no longer complete its task successfully, and so on up the stack until you reach a level in your design that knows how to recover from the failure or stop the entire program gracefully, which is where you catch the exception.
I’ve never quite understood the idea that exceptions are similar to goto. Other than transferring control over potentially long “distances” within a program, they don’t seem to have much in common at all. To me, a much closer analogy would be returning early from a function on success, if you reach a point in your algorithm where you already know the required answer to whatever question your calling code was asking. Another similar situation would be using a break or continue statement to finish an iteration early within a loop, or adding extra guard cases to stop a recursion early, again if you already know the outcome and continuing with the full algorithm has no advantage.
It's even harder to get right concurrency with exceptions :D
The bad idea was making language features optional.
Library writers are forced to either support all possible combinations, don't use any of them or use them and see their library being rejected by those that won't turn on the features even at point gun.
One of the things that made me initially enjoy Java was that there weren't features to turn on or off.
A ban on C++ exceptions is a huge red flag for me. A project has to be really special if I'm going to contribute to it after it's crippled the language.
void read_file_by_lines(const char* path, void (*callback)(const char* line)) {
FILE* file = fopen(path, "r");
size_t length = 0;
char* line = NULL;
while (-1 != getline(&line, &length, file)) {
callback(line);
}
free(line);
fclose(file);
}
This C function should be compatible with C++. If exceptions are off, it is. If exceptions are on then it's dangerous because if an exception is thrown through callback, then the file handle and line buffer will leak.C++ exceptions are predicated on the assumption that all resources are managed using RAII. In the real world, this isn't true, especially considering that C doesn't even have RAII.
Python programmers (including me) love their exceptions, because Python is a dynamic language, and exposes system resources as reference counted objects.
I think there are examples related to inter-library exceptions that might be much more subtle than this one, but I've never understood the subtlety.
So by having exceptions, with code that only knows about C semantics but was compiled with C++ mode, you open the door to all sorts of nasty surprises.
So exceptions can cause resource leaks and other issues like being thrown in the middle of a UNIX signal.
Which leads to the situation that sadly many library writers avoid them.
I still enjoy using C++, but the best place for it is as infrastructure language for the bottom layer when performance requires it, then using something else on the other layers.
That could be a fun thing to have a language competition that way, to see which language "feels" good: easy to read, expressive, etc.
If I wanted to implement a language with Lua-style coroutines, could I target LLVM?