---
# System prepended metadata

title: JMM Compiler

---

# JMM Compiler

## GROUP 1A

TODO: Definir autoavaliação e contribuição

| Name                | Student Number | Grade | Contribution |
| ------------------- | -------------- | ----- | ------------ |
| Francisco Gonçalves | up201704790    |       |              |
| Luís Ramos          | up201706253    |       |              |
| Martim Silva        | up201705205    |       |              |

GLOBAL Grade of the project: 18

---

## SUMMARY: (Describe what your tool does and its main features.)

- The tool compiles .jmm files written in Java-- which is a simpler version of Java.
- The tool receives the .jmm files and goes through 4 main steps before generating the final output:
  1. The program parses the file by performing a syntatic analysis and generates a JSON file with a tree that represents the Java-- class
  2. With the parser result the compiler performs a semantic analysis
  3. The result of the previous operation is now going to be converted into a Low Level Intermediate Representation
  4. After that and using the LLIR result the compiler generates files with JVM instructions accepted by jasmin
- Finally, Jasmin translates the last output into Java bytecodes (.class files).

---

## DEALING WITH SYNTACTIC ERRORS: (Describe how the syntactic error recovery of your tool works. Does it exit after the first error?)

TODO: Descrever fase sintática e geração do JSON

---

## SEMANTIC ANALYSIS: (Refer the semantic rules implemented by your tool.)

### Main steps

1. Build Analysis Table (symbol table)
2. Type Analysis
3. Initialization Analysis

### Scenarios covered with reports:

- Duplicated imports
- Redeclarations of variables
- Redeclarations of methods
- Redeclarations of function parameters
- Missing imports
- Invalid Types used
- Accessing length of a non array
- A method could not be found
- Invalid parameters to method
- Types don't match
- Array assignment to a non array variable
- Array initialization with a Type different than int
- Variable not initialized
- Variables were not initialized

### OLLIR

## CODE GENERATION: (describe how the code generation of your tool works and identify the possible problems your tool has regarding code generation.)

### Code Generation Features

- [x] Class
- [x] Fields
- [x] Methods
- [x] Instructions
- [x] Conditionals (`if` and `if-else`)
- [x] Loops (`while`)
- [x] Arrays
  - [x] Array initialization (`newarray int`)
  - [x] Array Store (`astore`)
  - [x] Array Access (`aload`)
  - [x] Array Position Store (`iastore`)
  - [x] Array Position Access (`iaload`)
- [x] Limits (`.limit stack` and `.limit locals`)

### Code Generation Instruction Selection

- [x] `iconst_`, `bipush`, `sipush`, `ldc`, for pushing integer to the stack with the lowest cost.
- [x] `iinc` for incrementing/decrementing local variables by a constant value.
- [x] `ishl`, `ishr` for using shifts with multiplications/division with a power of 2 number.
- [x] `ineg` for subtracting a variable to 0.
- [x] `iflt`, `iflt`, `ifge`, `ifgt`, `ifeq`, `ifneq` for if statements comparing with 0

### Extra features/details

- [ ] Declaration of objects with constructor and parameters
- [ ] Use do-while template when possible
- [x] Variables with keyword names: array, i32, ret, bool, field, method and void
- [x] Variables starting with $
- [x] Checks if a variable is initialized
- [x] Functions overload
- [x] Pop instructions to avoid the accumulation of stack size

### Custom Tests

Link to [custom tests](https://git.fe.up.pt/compilers2021/comp2021-1a/-/tree/master/test/fixtures/private/demo)

- Person (**Test1**)
- Factorial (**Test2**)
- Calendar (**Test3**)
- Shapes (**Test4**)
- ShapesExtra (**Test5**)

## TASK DISTRIBUTION: (Identify the set of tasks done by each member of the project. You can divide this by checkpoint it if helps)

We developed the project in a collaborative manner using Discord and pair programming most of the time. Decisions concerning the implementation, such as architecture/organization, logic, data structures, algorithms and approach were discussed by all the elements of the group.

TODO: Distribuir tasks
Francisco

-

Luís Ramos

-

Martim Silva

-

TODO: Rever isto

## PROS: (Identify the most positive aspects of your tool)

- Function overloading
- Meaningful error/warning reports at the semantic level
- Optimizations implemented (-o)
- Register allocation (graph coloring)
- Organized implementation

## CONS:

- There is one `LOOKAHEAD(2)` in the `.jjt`
- We could implement more optimizations such as dead code removal, if optimization, etc.
