For the last year and a half or so, I've been working on a transpiler from C++ through Racket and back to C++, to bring the metaprogramming benefits of a real macro system to C++ and CUDA. The motivation for this has a lot to do with GPU programming and performance portability, but it also allows you to do some pretty cool things as a standalone project too, so I thought I'd share it here. There are still a number of rough edges, mostly related to language coverage, but it already lets you handle a lot of stuff.

Consider the following program, in C++ (with an extended syntax to recognize macro forms beginning with @):

int main(int argc, char ** argv) {
   @Loop1d(test_loop)[@ I][= 0 + 0][= 0 + argc] {
      int j1;
      @Loop1d(test_loop)[@ J][= 0][= argc] {
         const int j = static_cast<int>(@J);
         j1 = static_cast<int>(@I);
         if(strlen(*(argv + j1)) > 0)
            (*(argv + j1))++;
         puts(*(argv + j1));
         if(strlen(*(argv + j)) > 0)
            (*(argv + j))++;
         puts(*(argv + j));
   const char nl = '\n';
   return 0;

One of the key problems with C++'s syntax from the perspective of macro authoring is that it's very hard to parse, so the builtin preprocessor limits you to blind text manipulations. This means you can't easily compose blocks of code in the same way you would function calls, because you have to worry about their scopes getting mixed up. So the first thing we do is use (my fork of) clang to turn it into a new form, where we can easily manipulate the syntax tree without worrying about parsing too much:

#lang Cxx
  (Loop1d "Documents/Brown/Proteins/racket-tests/SkelImpls/Loop1d.rkt")
 (defun () (int (!)) main ((() (int (!)) argc) (() (char * * (!)) argv))
    (@ Loop1d (test_loop) ([@ I][= (+ 0 0)][= (+ 0 argc)])
        (def (() (int (!)) j1))
        (@ Loop1d (test_loop) ([@ J][= 0][= argc])
            (def (() (const int (!)) j = (static_cast (int(!))  (@ J () ()))))
            (= j1 (static_cast (int(!))  (@ I () ())))
            (if (> (call strlen (* ((+ argv j1)))) 0)
                (>++ ((* ((+ argv j1))))))
            (call puts (* ((+ argv j1))))
            (if (> (call strlen (* ((+ argv j)))) 0)
                (>++ ((* ((+ argv j))))))
            (call puts (* ((+ argv j))))))))
    (def (() (const char (!)) nl = #\u0a))
    (call puts (& nl))
    (return 0))))

Surprise! The code looks awfully LISP-y now. That's because it is! All Racket programs begin with a #lang directive telling them what module should be used for the initial identifier bindings used for macro expansion. The Racket compiler then recursively expands macros until only core forms remain! Our Cxx module essentially binds all of the core syntax of C++ as simple macros to aid in walking the syntax tree, along with @ that binds our special macros we'll using for transforming the C++ code. So eventually our program will turn into something like this:

(module anonymous-module Cxx
    (module configure-runtime '#%kernel
      (#%module-begin (#%require racket/runtime-config) (#%app configure '#f)))
     (lambda ()
       (let-values ()
          '("/* The expanded C++ code ends up here as a string*/\n"))
         (#%app void)))

And running it will re-emit the displayed string containing our final C++ syntax.

Before we jump ahead, let's look at what goes on with our Loop1d macro:

(define Loop1d
   (lambda (params-table) ; This allows the requiring module to pass through important bits of configuration, should they be necessary
     (lambda (kind name args child)
       (let*-values ([(itr-id itr-macro) (values #'j (macroize (extract-id-arg (car args))))]
                     [(itr-init itr-final) (as-values extract-expr-arg (cdr args))])
          (list itr-macro)
          (with-syntax ([itr-id itr-id])
                (with-syntax ([itr-id (syntax-local-introduce #'itr-id)]) #'itr-id)) #:no-table #t))
             ([itr-id itr-id][itr-init itr-init][itr-final itr-final][child child])
           #'(for ((def (() (int (!)) itr-id = (itr-init))) (< itr-id (itr-final)) (++< itr-id)) child))))))))

Look at the very last line: it produces a for loop with a bunch of values substituted in, that we've extracted from the macro syntax. But it always uses "j" as the loop variable (and introduces a new macro, available when we expand the child code, that we can use to access that value, since the programmer doesn't necessarily know what's going on internally), which seems like it could cause some problems.

Thankfully, unlike C++'s usual preprocessor macros, the Racket macro system isn't just operating on dumb text, each element of a syntax object also stores meta-data keeping tracking of where it came from, so all of those "j"s have a unique identity, even though they share a name. So our Cxx module checks this metadata on each identifier it finds as it writes the expanded program back out as regular old C++, and generates a unique identifier for every identifier introduced by a macro rather than the programmer:

(if (syntax-original? (syntax-local-introduce id))
    (let loop
      ([btv (generate-temporary id)])
      (if (set-member? uniq-table (syntax->datum btv))
          (loop (generate-temporary btv))
            (dict-set! bind-table id btv)
            (set-add! uniq-table (syntax->datum btv))

So when we run the Racket program, we get this well-behaved set of nested loops, as if by magic:

int main(int argc, char **argv) {
  for (int j12 = ((0 + 0)); j12 < (0 + argc); ++j12) {
    int j1;
    for (int j3 = (0); j3 < argc; ++j3) {
      const int j = (static_cast<int>(j3));
      j1 = static_cast<int>(j12);
      if (strlen(*(argv + j1)) > 0)
        (*(argv + j1))++;
      puts(*(argv + j1));
      if (strlen(*(argv + j)) > 0)
        (*(argv + j))++;
      puts(*(argv + j));
  const char nl = '\x0a';
  return 0;

To see how this can be put to use for even cooler applications, take a look at the examples in the document I linked above, where I walk through some example CUDA code from NVIDIA and rewrite it with Racket macros.
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum