Things you can do with Clasp #1

Now that people are starting to build Clasp I thought I would say a few things about what you can do with it.

Clasp is an implementation of Common Lisp (80-90% complete) and I plan to make it an ANSI compliant Common Lisp as soon as possible. I also plan to add a faster compiler and expose some powerful C++ libraries to extend its capabilities.

But here are a few things you can do right away to get a sense of what Clasp does and to test some of its capabilities. Code that you type will (look-like-this). Don’t worry about how what you see isn’t colored or highlighted as shown here, I’m exploring different ways of rendering program output in this blog.

(defun add (x y) (+ x y))

ADD

(add 3 5)

8

(disassemble 'add)

Disassembling function: #<COMMON-LISP:COMPILED-FUNCTION CORE:ADD>

"#<LLVM-SYS::FUNCTION 
define internal void @ADD({ {}*, i32 }* %result-ptr, { {}* }* %closed-af-ptr, i32 %num-varargs, {}* %farg0, {}* %farg1, {}* %farg2, i8* %va-list) {
\"(TRY-0).entry\":
  %lambda-args-42- = alloca { {}* }
  call void @newAFsp({ {}* }* %lambda-args-42-)
  %temp = alloca { {}* }
  call void @newTsp({ {}* }* %temp)
  %0 = alloca { {}* }
  call void @newTsp({ {}* }* %0)
  call void @makeValueFrame({ {}* }* %lambda-args-42-, i32 2, i32 2000058)
  call void @setParentOfActivationFrame({ {}* }* %lambda-args-42-, { {}* }* %closed-af-ptr)
  %correct-num-args = icmp eq i32 %num-varargs, 2
  br i1 %correct-num-args, label %\"(TRY-0).continue3\", label %\"(TRY-0).error\"

\"(TRY-0).error\":                                  ; preds = %\"(TRY-0).entry\"
  %enough-args = icmp slt i32 %num-varargs, 2
  br i1 %enough-args, label %\"(TRY-0).error1\", label %\"(TRY-0).continue\"
...

That gobbledy-gook is pure LLVM-IR code generated by the Clasp Common Lisp compiler and using the fantastic LLVM library. It is exactly the same intermediate language that the Clang compiler converts C++/C/Objective-C into before it lowers it to machine code. Clasp shares the same LLVM backend library with the Clang compiler and once Clasp generates LLVM-IR, that is as efficient as that generated by Clang, then Clasp will generate code that runs as fast as C++ and C!

For some more information on Common Lisp you can check out the following links or use Google. I haven’t tested the tutorial against Clasp but almost everything should work.

For a tutorial:
http://en.wikibooks.org/wiki/Common_Lisp/First_steps/Beginner_tutorial

For beginners:
http://www.amazon.com/Land-Lisp-Learn-Program-Game/dp/1593272812/ref=sr_1_1?s=books&ie=UTF8&qid=1412084665&sr=1-1&keywords=land+of+lisp

I’ll post more later.

Advertisements

11 thoughts on “Things you can do with Clasp #1

  1. Hi there,

    By taking advantage of the fact that Clasp uses LLVM as a backend, is there a possibility that it could be used “on the metal?”.

    • Hi, thanks for the feedback! Could you say a little more about what you mean by “on the metal”? I have a picture in my head of what that means and to me it means “generate native code” – that is what Clasp does with the LLVM-MCJIT just in time compiler and when it compiles Common Lisp to regular object files and links them to dynamically loadable libraries.

      • Sorry for being a bit ambiguous.

        What I meant by on the metal is something running natively on the hardware. No runtime, no OS, no shared libs, but a plain binary. I understand that if one were to attempt such a thing, they’d be (very) limited to a subset of the Common Lisp language.

        I know by making some of those guarantees, you lose a ton of the functionality of Lisp, but it seems to me that anyone using it for that purpose could write any support functions needed to add that back in at runtime (i.e. dynamic memory allocation, etc).

        It’s a pretty far-fetched idea, but something that I thought worth asking about.

      • Ah, got it, no problem. It’s not too far fetched. I developed Clasp to run on large supercomputers that have no virtual memory and little (no?) access to shared libraries. It links statically for that purpose. Can Clang compile programs that run on the metal? If so then Clasp should be able to do so as well. I might be completely out to lunch here because it never occurred to me to compile stuff to run without an operating system.

  2. Yes, Clang can run on the metal by using -ffreestanding and a few other flags. Needless to say, you lose a lot of the C/C++ standard library (such as malloc/free/new/delete, stdio/iostream, etc.), but you still have the expressiveness of C/C++. If these libraries are needed, they are generally reimplemented for the target platform and statically linked in to the binary.

    If Clasp runs in the environment that you describe and statically links by default, it sounds like (similar to Clang) support libraries could be written to provide the functionality needed that is missing in a freestanding environment (i.e. dynamic memory allocation/IO/etc.).

    • Note that the MPS (the memory management system used by Clang) is designed to be as portable as possible across standard C implementations, including “free-standing” implementations (it has run on a variety of embedded systems, and our original colloquial notion was that it should be able to run “on a washing machine”). It is hard to get high GC performance without OS support (e.g. page protection for “memory barriers”), but the MPS will still run in those circumstances, and in principle a layer to provide that support could be written and plugged into the MPS to regain that performance.

  3. Being able to run “on the metal”, as Lawrence Brogan said, is the eminent condition that leads to one of the “holy grails” of the younger Lisp community, i.e. the design of a new Lisp Machine with its own Lisp-OS, independent from any underlying OS (‘younger’, because elder Lisp’ers did already have them, only they have been lost).

    • This sounds like an epic quest! A new Lisp Machine independent of any underlying OS. Wait for the new Clasp compiler that will generate faster code (spring 2015 with luck).

      • If that would ever happen, it would arguably contribute sparking a new Renaissance, not just for the Lisp community, but for the whole programming world. In my humble opinion, a true Lisp Machine is still one of the most powerful, flexible and deeply satisfying development tools ever built.

  4. drmeister thanks for your work, Clasp definitely fills some gaps by bringing the flexibility of functional languages to lower level development, and could be for the native machine what Clojure is for the JVM.
    Again, it is almost impossible to believe that all the work has been done by just one person on top of his regular job. Bravo! I’d like to join the project, but my job and several side projects tie my hands and I don’t know how much time and effort I would be able to devote, but I am definitely closely watching Clasp.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s