|
|
# GHC Commentary: What the hell is a `.cmm` file?
|
|
|
|
|
|
|
|
|
A `.cmm` file is rather like C--. The syntax is almost C-- (a few constructs are missing), and it is augmented with some macros that are expanded by GHC's code generator (eg. `INFO_TABLE()`). A `.cmm` file is compiled by GHC itself: the syntax is parsed by [compiler/cmm/CmmParse.y](/trac/ghc/browser/ghc/compiler/cmm/CmmParse.y) and [compiler/cmm/CmmLex.x](/trac/ghc/browser/ghc/compiler/cmm/CmmLex.x) into the [Cmm](commentary/compiler/cmm-type) data type, where it is then passed through one of the [back-ends](commentary/compiler/backends).
|
|
|
A `.cmm` file is rather like C--. The syntax is almost C-- (a few constructs are missing), and it is augmented with some macros that are expanded by GHC's code generator (eg. `INFO_TABLE()`). A `.cmm` file is compiled by GHC itself: the syntax is parsed by [compiler/cmm/CmmParse.y](/ghc/ghc/tree/master/ghc/compiler/cmm/CmmParse.y) and [compiler/cmm/CmmLex.x](/trac/ghc/browser/ghc/compiler/cmm/CmmLex.x) into the [Cmm](commentary/compiler/cmm-type) data type, where it is then passed through one of the [back-ends](commentary/compiler/backends).
|
|
|
|
|
|
|
|
|
We use the C preprocessor on `.cmm` files, making extensive use of macros to make writing this low-level code a bit less tedious and error-prone. Most of our C-- macros are in [includes/Cmm.h](/trac/ghc/browser/ghc/includes/Cmm.h). One useful fact about the macros is `P_` is an alias for `gcptr`, and you should not use it for non-garbage-collected pointers.
|
|
|
We use the C preprocessor on `.cmm` files, making extensive use of macros to make writing this low-level code a bit less tedious and error-prone. Most of our C-- macros are in [includes/Cmm.h](/ghc/ghc/tree/master/ghc/includes/Cmm.h). One useful fact about the macros is `P_` is an alias for `gcptr`, and you should not use it for non-garbage-collected pointers.
|
|
|
|
|
|
## Reading references
|
|
|
|
... | ... | @@ -12,8 +12,8 @@ We use the C preprocessor on `.cmm` files, making extensive use of macros to mak |
|
|
Reading material for learning Cmm is somewhat scattered, so I (Arash) have created a list of useful links. Since the Cmm language is changing as GHC changes, I have prioritized resources that are not too old. (*Feel free to add/remove/modify this list! :)*)
|
|
|
|
|
|
- An overview of Cmm is given in [David Terei's bachelor thesis](https://davidterei.com/downloads/papers/terei:2009:honours_thesis.pdf) (chapter 2.4.3).
|
|
|
- The comments in the beginning of [compiler/cmm/CmmParse.y](/trac/ghc/browser/ghc/compiler/cmm/CmmParse.y) is super-useful and kept up to date. The rest of the file contains the *grammar* of the language. Afraid of grammars? Edward Yang wrote this fantastic [blog post](http://blog.ezyang.com/2013/07/no-grammar-no-problem/) on how to understand the constructs of Cmm by using the grammar.
|
|
|
- Cmm has a preprocessor like the one in C and many of the macros are defined in [includes/Cmm.h](/trac/ghc/browser/ghc/includes/Cmm.h).
|
|
|
- The comments in the beginning of [compiler/cmm/CmmParse.y](/ghc/ghc/tree/master/ghc/compiler/cmm/CmmParse.y) is super-useful and kept up to date. The rest of the file contains the *grammar* of the language. Afraid of grammars? Edward Yang wrote this fantastic [blog post](http://blog.ezyang.com/2013/07/no-grammar-no-problem/) on how to understand the constructs of Cmm by using the grammar.
|
|
|
- Cmm has a preprocessor like the one in C and many of the macros are defined in [includes/Cmm.h](/ghc/ghc/tree/master/ghc/includes/Cmm.h).
|
|
|
- In 2012, Simon Marlow extended the Cmm language by adding a new high-level syntax which can be used when you don't need low-level access (like registers). The [commit](https://github.com/ghc/ghc/commit/a7c0387d20c1c9994d1100b14fbb8fb4e28a259e) explains the details.
|
|
|
- Cmm is also described [on this wiki](commentary/compiler/cmm-type), but it is written before the new syntax was introduced.
|
|
|
- Stack frame types are created using `INFO_TABLE_RET`, the syntax can be confusing since there are both *arguments* and *fields*, I (Arash) have not seen anything like it in other programming languages. I tried to explain it in my [master thesis](http://arashrouhani.com/papers/master-thesis.pdf) (sections 4.2 and 4.2.1).
|
... | ... | |