LWN.net Logo

An introduction to creating GCC plugins: also GCC MELT or GCC Python

An introduction to creating GCC plugins: also GCC MELT or GCC Python

Posted Sep 29, 2011 6:42 UTC (Thu) by bstarynk (guest, #63409)
Parent article: An introduction to creating GCC plugins

There are also some simpler way to extend GCC: coding GCC plugins in C is tedious, and there are some alternatives:

  • First, use GCC-MELT (disclaimer: I, Basile, am the main author of GCC MELT). MELT is a high-level domain specific language to ease the development of GCC extensions. The MELT language provides some high-level features: ability to do powerful pattern matching on GCC internal data (like Gimple): first-class dynamically typed garbage-collected values; ability to add new GCC passes in MELT; the MELT language is translated to C, and you can even add small C code chunks in your MELT code; MELT has a syntax similar to Lisp or Scheme; your MELT extensions can analyze or modify GCC internal representations, for various tasks like static analysis, coding rules validation, specific warnings, optimization, code refactoring, etc. MELT is available as a GCC [meta-] plugin.
  • There is also a Gcc Python Plugin which enables you to code your GCC plugin-like extensions in Python. However, Python don't give you pattern matching on GCC internal representations (like MELT does), and the GCC Python plugin is less mature than MELT so gives you access to less internal GCC data.

The main point is that people should consider extending GCC for their own needs and this is now possible with GCC plugin machinery, either in C, or in MELT or Python.

-- Basile Starynkevitch


(Log in to post comments)

An introduction to creating GCC plugins: also GCC MELT or GCC Python

Posted Sep 29, 2011 20:19 UTC (Thu) by dave_malcolm (subscriber, #15013) [Link]

Thanks for mentioning the gcc Python plugin.

I'm the author of the Python plugin, so naturally I'm fond of it.

The lack of pattern matching within Python's syntax hasn't been a major issue to me in practice. Also, I like to think that it's become significantly more mature than when I first announced it :)

I suspect that MELT will lead to faster-executing results, given that CPython isn't the fastest runtime in the world - though speed hasn't been an issue for me yet, either.

Inspired by this LWN article, I had a go at implementing a spell-checking GCC pass in Python, using my plugin.

You can see the results at:
http://readthedocs.org/docs/gcc-python-plugin/en/latest/w...

I believe my version is considerably simpler than doing it in C (about 35 lines of Python code, including comments), and, I hope, easier to read.

The Python implementation of the spell checker is slightly different to the one in the article, in that I'm using gcc.Gimple.walk_tree() on each statement to (recursively) visit all tree nodes it references. I'm also using gcc.warning() and gcc.inform() to integrate with GCC's warnings subsystem, so that the error messages are in GCC's normal format (so that e.g. when I click on them in emacs it takes me to the source line containing the error).

One of the nice things about using Python for this is all of the 3rd-party libraries that are available. In the Python implementation I'm using the "enchant" module that came out of AbiWord's spellchecker, and this makes it easy to offer spelling suggestions. There is a huge collection of Python libraries out there, so if you want to e.g. pause compilation part-way through and serve the internal-representation over HTTP, or dump it all into a database etc, there are lots of possibilities there.

Hope this is helpful
Dave

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds