Skip to content

Latest commit

 

History

History
112 lines (89 loc) · 5.29 KB

HACKING.md

File metadata and controls

112 lines (89 loc) · 5.29 KB

Contributing

Where to start?

To get an understanding of the internals please read this overview and start digging around in the code. If you want to get involved you should read the language specification and start writing small Monty programs to get to know the language.

If you've done that you may start fixing some bugs you might have encountered or refer to the implemented features list.

Building & Testing

Please refer to the README for instructions on how to build the project. Of course you'll need maven and the JDK installed. The basic commands you need to know are:

Command Description
mvn compile Build the java sources and run the formatter.
mvn test Run mvn compile and the test suite.
mvn package Run mvn test and build an executable jar.

This project uses the java-formatter-plugin with rules defined in src/main/resources/moco-code-conv.xml. Please import these into your IDE or run mvn compile before contributing code to format your changes accordingly.

Submitting a merge request

If you want to submit to the moco code base please use the Github merge request feature. Please make sure to have atomic commits and well formed commit messages. Please also ensure to only commit necessary changes, that the tests still pass and the jar is executable and that you used the formatter and stripped trailing whitespaces.

Your request will be merged, if at least two project members singed off your proposal and none vetoed.

Architecture

Libraries

This project builds on top a few libraries, managed by maven. These are:

Library Description
ANTLR4 See parser generator.
Argparse4j Used for parsing the command line arguments.
Apache Commons: IO, Lang Used in the code generation and for files IO.
JUnit, Mockito, Hamcrest Used for the unit- and integration-Tests.

Parser Generator: ANTLR4

ANTLR4 is a parser generator that builds the parser for the Monty syntax using the syntax grammar (de.uni.bremen.monty.moco.antlr.Monty.g4) and some lexer rules (src/main/antlr4/imports/lex.g4) at compile time. The generated parser (de.uni.bremen.monty.moco.antlr.MontyParser.java) is used within de.uni.bremen.monty.moco.ast.AntlrAdapter.java to build the parse tree. This parse tree is simplified using de.uni.bremen.monty.moco.ast.ASTBuilder.java to an abstract syntax tree using classes from the package de.uni.bremen.monty.moco.ast.

Visitors

To traverse the abstract syntax tree generated by de.uni.bremen.monty.moco.ast.ASTBuilder.java the compiler uses the visitor design pattern. Every visitor traverses the entire abstract syntax tree and performs various tasks. These are the visitors in the order in which they run.

Visitor Description
SetParentVisitor.java Set the parent node for each node. This is necessary for some operations later.
DeclarationVisitor.java Set up the scopes and declare every type and variable in the right one.
ResolveVisitor.java Resolve every Identifier for the associated declaration.
TypeCheckVisitor.java Check for type safety.
ControlFlowVisitor.java Check for correct control flow. e.g. if a function has a return in every branch.
NameManglingVisitor.java Mangle every Identifier for LLVM to manage scopes and reserved LLVM chars.
CodeGenerationVisitor.java Generate the LLVM-IR using support classes. See here.

The visitors implement a visit(*) method for each node type and use a technique called double dispatch to ensure that the right visit() method was called. Please use the comfort method visitDoubleDispatched(ASTNode) within a visitor.

Code Generation

The de.uni.bremen.monty.moco.visitor.CodeGenerationVisitor.java visits the abstract syntax tree and generates the LLVM-IR using classes from the de.uni.bremen.monty.moco.codegeneration package. Some classes in particular are interesting:

Class Description
..codegeneration.CodeGenerator.java Complex methods to generate code using the CodeConext. e.g. calling a function.
..codegeneration.context.CodeContext.java Low level LLVM equivalent methods.
..codegeneration.types.TypeConverter.java Maps a ClassDeclaration to a LLVM type.
..codegeneration.types.LLVMTypeFactory.java Utility functions for type creation and conversion.
..codegeneration.identifier.LLVMIdentifierFactory.java Utility functions for identifier creation and conversion.
..codegeneration.voodoo.BlackMagic.java Generate the body for built-in methods.