Giter VIP home page Giter VIP logo

minijava-compiler's Introduction

Compiler-Praktikum

Implementation of an x86 compiler for MiniJava, a subset of Java.

Build Status

Getting Started

Clone, build and run:

git clone https://github.com/jenox/Compiler-Praktikum.git
cd Compiler-Praktikum
git submodule update --init --recursive

# Building the compiler is as easy as calling the build script
./build

# Use the run script to compile MiniJava code
./run TestProgram.java

Prerequisites

The compiler needs the following dependencies:

  • A recent Java SE version (at least JDK 8 is required)
  • Swift 4.2 (if you do not already have this, we recommend using swiftenv to install it)
  • gcc to assemble and link the MiniJava binaries

Built With

Authors

  • Christian Schnorr - jenox
  • Maximilian Stemmer-Grabow - mxsg
  • Maik Wiesner - uheai
  • Daniel Krueger - dnlkrgr

minijava-compiler's People

Contributors

dnlkrgr avatar jenox avatar martinnnnnnn avatar mxsg avatar uheai avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

bethewind

minijava-compiler's Issues

Typabbildung

Ziel

Funktionalität, welche die primitiven und benutzerdefinierten Typen übersetzt in Typen von firm.
(ab Folie 53 im "Transformation"-Foliensatz)

Target Machine Values

Für die verschiedenen Typen von Expressions müssen tarvals (target machine values) erstellt werden.
Ich weiß nicht, ob firm auch schon automatisch dabei Offset und Alignment berechnet.
Könnte sein, dass man das auch noch machen muss.
Falls das nicht richtig berechnet wird, macht es den x86-Code langsamer.
https://pp.ipd.kit.edu/firm/api_latest/a00127.html#details

Procedure Graph

Tarvals können dann als Parameter benutzt werden, um Knoten im Graphen zu erstellen.
Hier zum Beispiel ein Constknoten für Integerkonstanten:
https://pp.ipd.kit.edu/firm/api_latest/a00157.html

Activation-Record-Management

Da unsere Sprache auch Rekursion unterstützt, brauchen wir Activation Records.
(ab Folie 71 im "Transformation"-Foliensatz)

firm bietet dazu den Frame Type an, um Stack Frames / Activation-Records zu repräsentieren.
firm berechnet dabei auch schon automatisch die Offsets für den Zugriff der Elemente des Frames.
Bei jedem Methodenaufruf muss ein Activation Record erzeugt und nach dem Durchlauf zerstört werden.
https://pp.ipd.kit.edu/firm/api_latest/a00140.html

Refactor IR generation

Split into

  • Type and entity generation
  • Method body code generation
  • Lowering
  • Code generation (also for switching libFirm backend for our own backend later)

Incorrect integer overflow handling

  • INT_MIN / 1 should produce INT_MIN
  • How to handle this? (use 64 bit integer arithmetics and cast to 32 bit?)
  • Other edge cases from the Java-Spec?
  • Behavior in the case of overflows

Symboltabelle

Goal

Have a symbol table class that can be used by the name and type analysis visitors.
Offer the following methods:

  • void enterScope()
  • void leaveScope()
  • void enterDeclaration(String ident, Declaration declaration)
  • Declaration getCurrentDeclaration(String ident)
  • boolean isDeclarationInCurrentScope(String ident)

Desired Behavior

  • Stack of HashMaps
  • entering new scope -> push new map onto stack
  • new declaration -> add (identifier, declaration) to map
  • leaving scope -> pop from stack

Optimization: String table

  • new declaration -> set (indexOf(identifier), declaration) in string table
  • leaving scope -> reset mapping of identifiers and declarations in string table

Abbildung von AST-Elementen auf Zwischencodeelemente

Ziel

Einen Visitor implementieren, welcher den AST abläuft und dessen Elemente in firm-Element übersetzt.

Entities erstellen

Methodendeklarationen und Felder müssen wohl Entities in firm sein. Entities zeigen auf ihren Besitzer (die Klasse).
https://pp.ipd.kit.edu/firm/tutorial/#creating-the-entities

Mehr zu Entities: https://pp.ipd.kit.edu/firm/api_latest/a00005.html#details

Vielleicht entspricht in firm die Kategorie "Type System" den Deklarationen im AST?

Methodengraphen erstellen

https://pp.ipd.kit.edu/firm/tutorial/#constructing-function-graphs
*Methodendeklarationen bekommen ihre eigenen Graphen

  • Der Graph referenziert die Methoden-Entity
  • Jeder Parameter ist eine Projektion aus den Argumenten
  • für den Methoden-Body wird ein eigener Knoten aufgebaut und handleBody aufgerufen

Mehr über den Methodentypen in firm: https://pp.ipd.kit.edu/firm/api_latest/a00133.html#details

Expressions

https://pp.ipd.kit.edu/firm/tutorial/#handling-expressions

Binäre Expressions

https://pp.ipd.kit.edu/firm/tutorial/#id1
Für die BinaryOperations braucht man wohl einen Procedure Graph https://pp.ipd.kit.edu/firm/api_latest/a00109.html#details.
In dem kann man dann Knoten erstellen, welche einer BinaryOperation entsprechen, wie z.B. hier der Add-Knoten: https://pp.ipd.kit.edu/firm/api_latest/a00143.html

Variablen-Expressions

https://pp.ipd.kit.edu/firm/tutorial/#id2
Deren Sprache hat keine lokale Variablen, wir müssen das da anders umsetzen.

Methodenaufrufe

https://pp.ipd.kit.edu/firm/tutorial/#id3

Tracking issue: Compiler crashes

In some cases, our compiler crashes when executing the transformation and trying to generate native code using the provided libfirm backend.

(Un)identified causes:

  • Variable offset (see #77)
  • Alignment and type layout (see #79)
  • Projection modes (see #82)
  • maybe other causes?

Typanalyse

Goal

Implement a visitor class that performs type analysis.
Desired output: Attributed Syntax Tree where for all statements and expressions their type attribute is filled out.

Dependencies

  • Symbol table (at least know what the API looks like)
  • classes of the attribute grammar (what classes are references; what classes are declarations, classes need accept method)
  • Visitor (at least know what the API looks like)

Desired behavior

Don't know how many traversals are needed here

Collecting Type Information

  • All boolean expressions have type boolean.
  • All arithmetic expressions have type integer.
  • All statements except for the return statement have type void.
  • Collect the types of all expressions. The type of expressions is the same type as its subexpression(s).
  • Collect the type of all return statements. Their types are the same as their expression.
  • All references have the same type as their declaration (look up decl attribute -> look up type attribute of Declaration).

Type Checking

  • In assigments and variable declarations, the types on both sides have to be equal.
  • When invoking a method, based on the identifier, the types of all arguments have to match with types of the parameters (look up decl attribute)
  • A return statement must have the same type as the return type of its method.
  • null matches matches any UserDefinedTypes and Array Expressions (does it match anything else?).

When types are incosistent, it is necessary to give helpful errors (with location).

Fehler aus semantischer Analyse beheben

  • -2147483647 und -/*comment*/2147483647 gültig, aber -(2147483647) nicht
  • System == System und System.out == System.out ungültig
  • return this.f() aus void-Methode, wenn f void zurück gibt, ungültig
  • invalid redeclaration of main in secondary class

  • Tests dafür schreiben

Checkstyle anpassen

  • if else try catch etc. auf eigene Zeile
  • explicit this
  • prefer wildcard imports
  • kein trailing Whitespace
  • 4 Spaces statt Tabs

AST für semantische Analyse vorbereiten

Goal

Classes and functionality to translate the abstract syntrax tree into an unfilled attributed syntrax tree.

Desired behavior

Declarations

There should be a superclass Declaration.
The following classes should extend it:

  • ClassDeclaration
  • Field
  • Method
  • Parameter

References

There should be a superclass Reference with an attribute decl of type Declaration.
The following classes could extend it:

  • UserDefinedType
  • MethodInvocation
  • FieldAccess
  • NewObjectExpression
  • NewArrayExpression
  • ThisExpression
  • IdentifierExpression
  • IdentifierAndArgumentsExpression

Not sure: should only IdentifierExpression and IdentifierAndArgumentsExpression be references?

Type attribute

All statements and expressions should get a type attribute.
They should have a superclass, for example: TypeableNode.

VisitableNode

All nodes should extend a superclass VisitableNode (which offers a accept(Visitor v) method).

Wrong variable access offsets

May only affect invocations from the main method. Produces the error:

Assertion failed: (pos + 1 < irg->n_loc), function set_r_value, file ir/ir/ircons.c, line 511.

System Calls überdenken

Aktuell globale System-Variable und dummy MethodDeclarations für write, flush etc. Für System.out.write(5) ist dann aber wirklich MethodInvocation(ExplicitFieldAccess(VariableAccess(System), out), write, 5) im AST.

Ich glaube nicht, dass es in dieser Form in die Transformation gehen sollte. Wir könnten stattdessen neue Expressions für die verschiedenen Syscalls einführen und in der semantischen Analyse den AST dann minimal umbauen.

@dnlkrgr @uheai @mxsg Meinung / Ideen?

Namensanalyse

Goal

Implement a visitor class that performs name analysis.
Desired output: Attributed Syntax Tree where all references have their decl attribute filled out.

Dependencies

  • Symbol table (at least know what the API looks like)
  • classes of the attribute grammar (what classes are references; what classes are declarations, classes need accept method)
  • Visitor (at least know what the API looks like)

Desired Behavior

The tree needs to be traversed twice, because MiniJava allows use-before-declare.
** Though, maybe it's still possible to compute the whole thing with one traversal?**

  1. First traversal: Collect all declarations. Also check for variables that are already declared.
  2. Second traversal: Set the decl attribute for all references. Check for undefined variables or variable use out of scope.

This can be implemented via Visitor Pattern whereby the Visitor owns the symbol table with the declarations.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.