An open API service indexing awesome lists of open source software.

https://github.com/antonioalmeida/feup-comp

Repository to host Compilers' [COMP] project.
https://github.com/antonioalmeida/feup-comp

comp compilers feup javacc jjtree mieic

Last synced: 9 months ago
JSON representation

Repository to host Compilers' [COMP] project.

Awesome Lists containing this project

README

          

## yal (yet another language)

### GROUP: G27

António Cunha Seco Fernandes de Almeida, up201505836, Grade: 20, Contribution: 25%

Francisco Tuna Andrade, up201503481, Grade: 20, Contribution: 25%

Gil Dinis Magalhães Teixeira, up201505735, Grade: 20, Contribution: 25%

Tiago Alexandre de Sousa Dias da Silva, up201404689, Grade: 20, Contribution: 25%

GLOBAL Grade of the project: 20

### SUMMARY:

As a project for the Compilers course at Porto Engineering University, we implemented a compiler capable of generating Java bytecode, a low level language capable of operation the Java Virtual Machine from Yal, a high level programing language. The main features of our compiler are its ability to perform syntatic analysis, semantic analysis and generating low level code.

### EXECUTE:

#### Compiling:
Inside base directory
```
$ sh compile.sh
```

#### Running:

##### Running Files Inside `src/testsuite`
Inside base directory
```
$ sh run.sh [-r=INT] [-o]

# Example
$ sh run.sh code_generation testArithmetic
```

##### Running Other `yal` Files
Inside base directory
```
$ java -jar yal2jvm.jar [-r=] [-o]

# Example
$ java -jar yal2jvm.jar test1.yal -r=3 -o
```

#### Running Chosen Examples (`src/examples` directory):
Inside base directory
```
$ sh example.sh [-r=INT] [-o]

# Example
$ sh example.sh quicksort
```

#### DEALING WITH SYNTACTIC ERRORS:

Our tool shows all the syntatic errors found, so that it does not exit after the first error. For example, if it founds an error in the assignement of a variable, the parser skips to the next semicolon and starts looking for syntatic errors from there. Our tool was able to report correctly all the errors in all files of the folder "MyFirstYalExamples_1" that can be found in the link "yal Examples" in the Moodle page of this course.

#### SEMANTIC ANALYSIS:

In our compiler we implemented all the semantic rules mentioned in the project description and in the slides of this course. For example:
- We check that if a variable is going to be assigned to another, they must have the same type (note: an array can be assigned to a scalar, in that case it means fill all the array with the value of the scalar)
- We check that if a variable is going to be used in an expression it must have been initialized to type SCALAR
- We check that the return value of a function should be initialized
- We check that when a function is called there should be a function with the same signature (i.e. same name and same type of arguments)
- We check that for the value returned by a function to be used in an expression, that return value should be a SCALAR
- We check that a variable used in a function call as parameter has been initialized
- We attributed scopes for whiles, ifs and elses so that a variable declared inside a scope is not valid outside
- In the specific case that a variable is declared inside an if statement and in an else statement that follows with the same type, that variable becomes valid to be used outside those if and else scopes

#### INTERMEDIATE REPRESENTATIONS (IRs):

Although we did not use IRs to generate the code, we used an IR to implement optimization -r. That IR consists of an array of instructions, that contains all the instructions of a given function. Each instruction contains its uses and defs and also the id's of instructions that are its antecessor and successor.

#### CODE GENERATION:

The code generation uses the AST as the basis with support from the symbol tables, mostly for variable type checks. While going through it, it the generates the appropriate code. There are special cases, such as, limits generation which happens in the end, and integer assign to all array positions which has to generate code for a loop in order to fill all positions. We tried to make code generation as modular as possible which allowed us to not repeat code unless absolutely necessary. Besides covering the normal cases, it uses low cost instructions such as "iinc","iload","istore","astore","aload" and the various comparisons with 0. All the .yal examples from the "MyFirstYalExamples" run correctly, unless an error is supposed to occur, after the jasmin conversion to .class files. Same occurs for the "Extra yal examples" folder.

#### OVERVIEW:

Our project has a total of four packages: "parser" that contains the code generated by jjtree, "semantic" that contains the classes related to Symbols and Functions Table, "codeGeneration" that contains the classes related to the code generation and "utils" that contains functions used by all packages.
Our compiler does not use any third-party tools. We implemented the optimization -r using the graph coloring algorithm studied in class.

#### TESTSUITE AND TEST INFRASTRUCTURE:

The tool's testsuite provides automated tests through JUnit4 for most of the given examples, as well as our own tests. Each test's purpose is specified on its filename.

How to run (inside base directory)
```
$ sh compile.sh # compiles code and tests
$ sh test.sh

# Example - running code generation tests
$ sh test.sh CodeGenerationTests
```

#### TASK DISTRIBUTION:

António Almeida - Syntatic Analysis, part of Semantic Analysis, part of Code Generation - base, ifs, variable indexing, stack state controller, Automated Testing and Execution

Francisco Andrade - Syntatic Analysis, Symbol and Functions Tables implementation, Semantic Analysis, Implementation of optimization -r

Gil Teixeira - AST generation; Base for code generation, Function, Arithmetic expressions, Loops and Arrays code generation; Optimizations related to the "-o" option.

Tiago Silva - AST generation; Base for code generation, Function, Arithmetic expressions, Loops and Arrays code generation; Optimizations related to low cost instructions.

#### PROS:

In addition to the requested features, our tool features a few additional optimizations:
- Allows overload of functions with different argument types and different number of arguments
- Assigns variable to the minimum number of registers possible if option -r is set
- Performs constant propagation if option -o is set (uses while templates by default)
- Lower cost ifs in case of comparisons with 0 ('ifeq', 'ifge', 'ifgt', 'ifle','iflt', 'ifne')

#### CONS:

- Does not allow the return variable of a function to be a global variable