-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
894 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
bin/ | ||
.vscode | ||
*.o | ||
*.tab.hpp | ||
*.tab.cpp | ||
*.yy.cpp | ||
*.output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
int f() | ||
{ | ||
return 5; | ||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Assembler directives | ||
==================== | ||
"The assembler implements a number of directives that control the assembly of instructions into an object file. These directives give the ability to include arbitrary data in the object file, control exporting of symbols, selection of sections, alignment of data, assembly options for compression, position dependent and position independent code" - quote from [RISC-V Assembler Reference](https://michaeljclark.github.io/asm.html). | ||
|
||
The linked guide explains in details all available directives, but fortunately you only need a very small subset to start with and even the more advanced features only require a few additional directives. While [Godbolt](https://godbolt.org/z/vMMnWbsff) emits some directives, to see all of them (more than you actually need) you are advised to run: | ||
|
||
```riscv64-unknown-elf-gcc -std=c90 -pedantic -ansi -O0 -march=rv32imfd -mabi=ilp32d -S [source-file.c] -o [dest-file.s]```. | ||
|
||
The below picture offers a quick walk-through of a very simple program with detailed annotations describing the meaning behind the included directives. Some of them a crucial (e.g. section specifiers, labels, data emitting) while others not so much (e.g. file attributes, compiler identifier, symbol types) - you will get a feel for them during the development of the compiler. Most importantly, you only need to set the correct section and provide function directives as long as you deal with local variables. **In other words, you can postpone studying this document in details until you decide to deal with global variables.** | ||
|
||
![Assembler directives](./assembler_directives.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Basic compiler | ||
============== | ||
|
||
For the first time ever, you are provided with a basic compiler that can lex, parse and generate (incorrect) code for the following program: | ||
``` | ||
int f() { | ||
return; | ||
} | ||
``` | ||
|
||
The output assembly is hardcoded, so that the basic compiler passes one of the provided test cases. However, having a functioning compiler should allow you to hopefully jump-start the development of the actually interesting parts of this coursework while avoiding the common early pitfalls that students have faced in previous years. It should also allow you to better understand the underlying C90 grammar and have an easier time when adding new features. | ||
|
||
The provided basic compiler is able to traverse the following AST related to the above program. In order to expand its capabilities, you should develop the parser and the corresponding code generation at the same time - do not try to fully implement one before the other. | ||
|
||
![int_main_return_tree](./int_main_return_tree.png) | ||
|
||
|
||
The lexer and parser are loosely based on the "official" grammar covered [here](https://www.lysator.liu.se/c/ANSI-C-grammar-l.html) and [here](https://www.lysator.liu.se/c/ANSI-C-grammar-y.html) respectively. While they should suffice for a significant portions of features, you might need to improve them to implement the more advanced ones. If you find the grammar too complicated to understand, it is also perfectly fine to create your own simple grammar and build upon it as you add more features. | ||
|
||
You can follow the patterns introduced for the code generation part of the basic compiler, but you might find adjusting them to your needs be better in the long run. You are recommended to follow the coding style that best suits you while hopefully picking strong design skills throughout the development of your compiler. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef AST_HPP | ||
#define AST_HPP | ||
|
||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "ast_direct_declarator.hpp" | ||
#include "ast_function_definition.hpp" | ||
#include "ast_identifier.hpp" | ||
#include "ast_jump_statement.hpp" | ||
#include "ast_node.hpp" | ||
#include "ast_type_specifier.hpp" | ||
|
||
extern Node* parseAST(std::string file_name); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef AST_CONTEXT | ||
#define AST_CONTEXT | ||
|
||
// An object of class Context is passed between AST nodes during compilation to provide adequate context | ||
class Context { | ||
/* TODO decide what goes inside here */ | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef AST_DIRECT_DECLARATOR | ||
#define AST_DIRECT_DECLARATOR | ||
|
||
#include "ast_node.hpp" | ||
|
||
class DirectDeclarator : public Node { | ||
public: | ||
DirectDeclarator(Node* identifier); | ||
~DirectDeclarator() {}; | ||
void emitRISC(std::ostream &stream, Context &context) const; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef AST_FUNCTION_DEFINITION_HPP | ||
#define AST_FUNCTION_DEFINITION_HPP | ||
|
||
#include "ast_node.hpp" | ||
|
||
class FunctionDefinition : public Node { | ||
public: | ||
FunctionDefinition(Node* declaration_specifiers, Node* declarator, Node* compound_statement); | ||
~FunctionDefinition() {}; | ||
void emitRISC(std::ostream &stream, Context &context) const; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef AST_IDENTIFIER | ||
#define AST_IDENTIFIER | ||
|
||
#include "ast_node.hpp" | ||
|
||
class Identifier : public Node { | ||
private: | ||
std::string* identifier; | ||
public: | ||
Identifier(std::string* _identifier) : identifier(_identifier) {}; | ||
~Identifier() {delete identifier;}; | ||
void emitRISC(std::ostream &stream, Context &context) const; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef AST_JUMP_STATEMENT | ||
#define AST_JUMP_STATEMENT | ||
|
||
#include "ast_node.hpp" | ||
|
||
class JumpStatement : public Node { | ||
public: | ||
JumpStatement() {}; | ||
~JumpStatement() {}; | ||
void emitRISC(std::ostream &stream, Context &context) const; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef AST_NODE_HPP | ||
#define AST_NODE_HPP | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
#include "ast_context.hpp" | ||
|
||
class Node { | ||
protected: | ||
std::vector<Node*> branches; | ||
|
||
public: | ||
Node() {}; | ||
virtual ~Node(); | ||
virtual void emitRISC(std::ostream &stream, Context &context) const = 0; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef AST_TYPE_SPECIFIER | ||
#define AST_TYPE_SPECIFIER | ||
|
||
#include "ast_node.hpp" | ||
|
||
class TypeSpecifier : public Node { | ||
private: | ||
std::string type; | ||
public: | ||
TypeSpecifier(std::string _type) : type(_type) {}; | ||
~TypeSpecifier() {}; | ||
void emitRISC(std::ostream &stream, Context &context) const {}; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "ast_context.hpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "ast_direct_declarator.hpp" | ||
|
||
DirectDeclarator::DirectDeclarator(Node* identifier) { | ||
branches.insert(branches.end(), {identifier}); | ||
} | ||
|
||
void DirectDeclarator::emitRISC(std::ostream &stream, Context &context) const { | ||
// Emit identifier | ||
branches[0]->emitRISC(stream, context); | ||
stream << ":" << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "ast_function_definition.hpp" | ||
|
||
FunctionDefinition::FunctionDefinition(Node* declaration_specifiers, Node* declarator, Node* compound_statement) { | ||
branches.insert(branches.end(), {declaration_specifiers, declarator, compound_statement}); | ||
} | ||
|
||
void FunctionDefinition::emitRISC(std::ostream &stream, Context &context) const { | ||
// Emit declarator | ||
branches[1]->emitRISC(stream, context); | ||
|
||
// Emit compound_statement | ||
branches[2]->emitRISC(stream, context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "ast_identifier.hpp" | ||
|
||
void Identifier::emitRISC(std::ostream &stream, Context &context) const { | ||
stream << *identifier; | ||
} |
Oops, something went wrong.