• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
  • comments
  • views

Implementing Higher Order Functions on Top of LLVM

Sign in to follow this  
Followers 0


One of my favorite features of the Epoch programming language is the inclusion of first-class higher order functions:apply : (thefunction : ){ thefunction()}apply : string param, (thefunction : string -> string) -> string ret = thefunction(param)mutate : string param -> string ret = param ; " foo"entrypoint :{ string s = apply("test", mutate) assert(s == "test foo") apply(passtest)}In the old VM model (which, as I've written about quite a bit recently, is going away) this is pretty easy to do. I just store the name of the function I want to invoke on the stack, and the VM knows how to jump into that function using an instruction called INVOKE_INDIRECT.

This works because the calling of functions is not checked at runtime - it's verified statically by the compiler, but once the bytecode is generated, there's no sanity checking to ensure that I've bound the correct number of parameters to the stack before invoking a function. Not having any checking allows higher-order functions to be invoked trivially in Epoch's calling convention: the callee simply reads off the expected parameters, and everything just works.

However, moving to JIT native code implementation on LLVM has proven tricky in this particular area. LLVM does not let you simply call arbitrary functions without knowing their signatures - which is a totally reasonable restriction.

Our saving grace is that we know the signature of the function already - it was checked at compile time, after all - so we just need to look at the Epoch bytecode metadata to figure out how to tell LLVM to invoke a function.

Remember how I mentioned that the VM used to call functions by name? This is a minor headache, because while the function signature is statically known, the mapping of the name to actual code can change dynamically at runtime. (If it couldn't, higher order functions would be kind of useless.) Suppose my example program selected a function to pass to apply() based on user input; the JIT layer wouldn't know which function call to embed into the LLVM bitcode.

The lazy solution would be to store the mapping in memory and use a thunk to translate the function name on the stack back into a code address. However, this would represent an annoying runtime cost, and might obliterate the possibility of doing certain inlining optimizations for simple uses of higher-order functions. So that option is out.

The proper fix is to change the compiler a bit. We'll still emit function names and push them onto the stack in the bytecode; however, we'll include a type annotation that mentions that this isn't just another string handle, but actually a function name handle.

Next, once the bytecode is loaded into the JIT layer, we'll detect these annotations and note each time a function's name is used as a higher order function parameter. Instead of emitting code to store the name on the machine stack, we'll look up the LLVM generated function for the target, and emit a function pointer instead. Remember, we know the signature statically, so we can get away with this without violating type safety in LLVM.

Last but not least, we implement INVOKE_INDIRECT in such a way that it looks at the function pointer it's being asked to call, and then fixes up the machine stack and calls it.

Unfortunately, it turns out that adding the metadata emission of function signatures is a nontrivial modification to the code. But it's the right thing to do, so I'm going to tackle it either way.

So far I'm an hour or so into this project and it looks like it might take a hefty couple of days to get it done properly. Such is life, I suppose.

I look forward to having 3 or 4 more of the compiler tests in the suite pass once this change is in!

[edit a few hours later] It's now 3:22 AM. 51 of 54 tests are passing. I am a happy, happy hacker.

Sign in to follow this  
Followers 0


There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now