Skip to content

Using McLab for development

Sameer Jagdale edited this page Apr 20, 2015 · 6 revisions

Mclab is developed to provide infrastructure to develop various compiler tools for MATLAB. All the tools developed in the Sable lab for MATLAB reuse parts of McLab. Examples of the tools that can be developed using Mclab include static backends for languages such as Fortran, a MATLAB interpreter using the McLab lexer and parser and implementing Aspect oriented programming for MATLAB. The recommended method of using McLab is to compile the project into a jar file and add the jar to the classpath of the project using McLab. The following sections will explain how McLab can be used for tool development. ###Implementing a static backend McLab provides various analyses and transformations designed to make the job of writing a static backend for Matlab easier. Three backends have already been implemented by students at the Sable lab and are listed below.

  • Mc2For : A Fortran backend.
  • MiX10 : A backend for IBM's X10 language.
  • VrirGenerator: A backend for VRIR, a language agnostic IR for array based languages developed as part of the Velociraptor toolkit at the Sable lab.

Depending on the target language, a variety of information about the program maybe required, such as the type and dimensions(or shape) for the variables in the program. McLab implements analyses, namely Value analysis and Shape analysis to collect information about the type and shape respectively. Additionally, McLab also implements analyses to determine whether a variable is complex, ranges of variable values and which variables are integers. These analyses are all implemented using the Tamer analysis framework provided by McLab. Additional analyses can be implemented using Tamer.

The existing backend implementations also use analyses implemented using the McSAF framework.These analyses are used by default by Tamer.

Generating a CallGraph using Tamer and Performing analyses.

The Tamer analyses mentioned above are all part of the BasicTamerTool. This section describes how the BasicTamerTool can be used to generate a callgraph of the MATLAB program and perform the analyses. In order to generate the callgraph, the tool requires two input parameters :

  • The types, shape and complexity(whether the variable is complex or real) of the input parameters of the entry point function passed as a String array.
  • A FileEnviroment object of the entry point function.

The FileEnvironment object is in turn created using a GenericFile object which takes the input file of the entry point function. Following is an example of the same.

String fileIn = fileDir + "/" + fileName;
File file = new File(fileIn);
GenericFile gFile = GenericFile.create(file.getAbsolutePath());
FileEnvironment env = new FileEnvironment(gFile);

Next the callgraph needs to be generated and the analysis need to be performed.

ValueAnalysis<AggrValue<BasicMatrixValue>> analysis = BasicTamerTool
				.analyze(inputArgs, env);

The analysis information can be accessed invoking the following method

analysis.getNodeList().get(index).getAnalysis();

The AST node for the function can be accessed using the following method.

StaticFunction function = analysis.getNodeList.get(i).getAst();

The AST represents the TameIR, a three address code based representation for MATLAB. The integerOkay analysis is turned off by default and has to be turned on in the following manner.

BasicTamerTool.setDoIntOk(true);

Traversing the AST

Both the Tamer and McSAF frameworks allow AST traversal using the Visitor pattern. Static backends can exploit this traversal implementation while implementing the code generator.

In the case of the TameIR AST, the class traversing the tree is required to extend TIRAbstractNodeCaseHandler. TIRAbsractNodeCaseHandler supports default behaviour for each of the nodes in the AST. It does not provide the implementation for the ASTNode, the topmost level node in the AST. The class inheriting is required to provide the implementation. The method name for a node in the AST is case<Node_Name>. For example the method for the TIRForStmt will be

 public void caseTIRForStmt(TIRForStmt node);

The default implementation of the method simply calls the equivalent method for the McSAF AST. Assuming that the tree traversal class is called TreeTraversal, and TreeTraversalObject is an object of that class, the method for a specific node of the AST can be invoked as follows.

node.analyze(TreeTraversalObject);

An example implementation of the caseASTNode method, which as mentioned before is not implemented by the TIRAbstractNodeCaseHandler is

public void caseASTNode(ASTNode node) {
    for(int i = 0; i < node.getNumChild();i++) {
        node.getChild(i).analyze(this); 
    }
}

Tree traversal can also be implemented similarly by extending the NatlabAbstractNodeCaseHandler.