General question - better descriped.

Hi everyone,

i'm currently trying to write an application that let you "code" with
nodes. (think of a node like a function that has input and output slots
that can get connected).

I need a few functions that are predefined and can be used in the code
(so i want to parse and create an AST for those functions/variables and
then i need to parse C++ code (one node has c++ code that should be
executed. So in the end i've nodes that are for types, then one for
operations (the functions).

The code from the nodes must be able to run the code from it's node with
the parameters i pass to the node's slots or in the node itself and i
need it to be able to create own functions that must be available in the
code of the nodes.

so for example i've a typenode that declares and defines variables, for
example vec3f for 3d vector types.

Now my question would be: Where should i start reading or is there a
special article how to achive what i want ?? As far as i know i need
clang to parse my own functions so he knows about my types and my
functions (one for each operation nodes and the structure declarations).
this creates one AST for each type/function). then i need to build an
AST for my own nodes that must be interpreted and compiled with llvm and
then run the code at runtime.

I hope one of you guys can point me in the right direction because i'm
completely new to clang and llvm (but i found a book about "learning
llvm basic libaries that helps a bit but they do not talk much about
clang (so i only know the basics about llvm and have not much of a clue
about clang.

best regards,

Steven Truppe

As far as I understand it you are basically trying to create your own domain specific language for scientific computation?
Clang seems to kinda match what you need but I don’t think it’s able to support function definition in another function which sounds like something you’d need. Your best approach would be using some lexer/parser generate tool to code your own compiler frontend (GREAT DEAL OF WORK) or hack up libClang a bit. Alternatively there are a bunch of readily usable LLVM based languages like Halide that are designed for generating efficient tensor computation code so changing your workflow a bit to re-use those libraries/frameworks is probably a better idea, in my humble opinion. Unless I’m missing something obvious here.

Zhang

I explain it in more detail:

I'm writing an application that works completely with nodes (think about
a node like a function with parameters and in and output slots. there
will be type-nodes like vec3f, int, float, etc. and operation nodes (the
functions that are predefined). So as far as i know i need clang to
parse and create an AST from the nodes (which have access to the
predefined functions and variables). The nodes that are create with a
GUI and the user can connect those nodes to create higher order
functions. (if you know houdini you might know the system i'm trying to
create).

And the code should be C++ (for the predefined functions and the
operator nodes). So no own language just parse and execute in realtime a
self created AST (with predefined functions that must be available for
the code that every operator node has included. More like a graphical
interface to let the use create it's own AST and then run it in realtime
(so the user don' t need programming skills and can create smaller
applications only with the graphical interface.

I was not able to find a clang book, only for llvm and they don't go
into much detail when it comes to clang.

I hope someone here can point me in the right direction (name of a clang
book or links to the documentation that i need to get things done.

best regards.

Steven Truppe

I’m still kinda lost, some example would help.
Anyway I’ll try my best here. If the core code(functions) are predefined and is written in C++,I’d say re-use libClang to compile them into Bitcode/LLVM IR, then perform your own linking/inlining procedures, add function calls etc to sort of “pipe” everything together. Basically:

  • Setting up a libClang’s CompilerInstance with headers and stuff.
  • Emit a new C++ source code from the user’s input and use your CompilerInstance to run EmitLLVMOnlyAction. (See this article http://fdiv.net/2012/08/15/compiling-code-clang-api for a hands-on walkthrough. See the Compile a .c file into a Module part)
  • Some IR level transformation if you need to.

In this way you should be able to tackle the problem without hacking with libClang or anything, if my understanding is correct. Also for any libClang related question, cfe-dev is a more suitable place.

Zhang