Next: , Previous: , Up: Implementation   [Contents][Index]

6.2 Backend creation

The coNCePTuaL compiler’s backend generates code from an abstract syntax tree ( AST). The compiler was designed to support a variety of code generators, each targeting a particular programming language and communication library. There are two ways to create a new coNCePTuaL backend. Either a backend supporting an arbitrary language and communication library can be written from scratch or a C-based backend can be derived from .

In the former case, the backend must define an NCPTL_CodeGen class. NCPTL_CodeGen class must contain a generate method with the following signature:

def generate(self, ast, filesource='<stdin>', filetarget='-',

That is, generate takes as arguments a class object, the root of an abstract-syntax tree (as defined in, the name of the input file containing coNCePTuaL code (to be used for outputting error messages), the name of the output file to produce, and the complete coNCePTuaL source code (which is both stored in prologue comments and passed to the run-time library). generate should invoke self.postorder_traversal to traverse the AST in a post-order fashion, calling various code-generating methods as it proceeds. The NCPTL_CodeGen must implement all of the methods listed in Method calls, each of which corresponds to some component of the coNCePTuaL grammar. Each method takes a “self” class object an a node of the AST (of type AST ).

The compiler front-end, ncptl, invokes the following two methods, which must be defined by the backend’s NCPTL_CodeGen class:

def compile_only(self, progfilename, codelines, outfilename,
                 verbose, keepints):
def compile_and_link(self, progfilename, codelines, outfilename,
                     verbose, keepints):

The compile_only method compiles the backend-specific code into an object file. The compile_and_link method compiles the backend-specific code into an object file and links it into an executable file. For some backends, the notions of “compile” and “link” may not be appropriate. In that situation, the backend should perform the closest meaningful operations. For example, the dot_ast backend (see The dot_ast backend) compiles to a .dot file and links into the target graphics format (.ps by default).

For both the compile_only and compile_and_link methods, progfilename is the name of the coNCePTuaL input file specified on the ncptl command line or the string ‘<command line>’ if a program was specified literally with --program. codelines is the output from the generate method, i.e., a list of lines of backend-specific code. outfilename is the name of the target file specified on the ncptl command line with --output or the string ‘-’ if --output was not used. If verbose is ‘1’, the method should write each operation it plans to perform to the standard-error device. For consistency, comment lines should begin with ‘#’; shell commands should be output verbatim. If verbose is ‘0’, corresponding to the ncptl --quiet option, the method should output nothing but error messages. Finally, keepints corresponds to the --keep-ints option to ncptl. If equal to ‘0’, all intermediate files should be deleted before returning; if equal to ‘1’, intermediate files should be preserved. See Compiling coNCePTuaL programs, for a description of the various command-line options to ncptl.

As long as NCPTL_CodeGen implements all of the required functions it is free to generate code in any way that it sees fit. However, Method calls, lists a large number of methods, many of which will be identical across multiple code generators for the same language but different communication libraries. To simplify a common case, C plus some messaging library, coNCePTuaL provides , to which the remainder of the Implementation chapter is devoted to explaining.

Next: , Previous: , Up: Implementation   [Contents][Index]

Scott Pakin,