-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added bunch of stuff like arithmetics(partially completed switch stat…
…ement for now) and parametered function handling
- Loading branch information
Showing
26 changed files
with
510 additions
and
61 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
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,25 @@ | ||
#ifndef AST_BINARY_OP_HPP | ||
#define AST_BINARY_OP_HPP | ||
|
||
#include "ast_node.hpp" | ||
|
||
class BinaryOp : public Node | ||
{ | ||
private: | ||
Node *lhs_; | ||
Operator oper_; | ||
Node *rhs_; | ||
|
||
public: | ||
BinaryOp(Node *lhs, Operator oper, Node *rhs) | ||
: lhs_(lhs), oper_(oper), rhs_(rhs){} | ||
~BinaryOp() | ||
{ | ||
delete lhs_; | ||
delete rhs_; | ||
}; | ||
void EmitRISC(std::ostream &stream, Context &context) const override; | ||
void Print(std::ostream &stream) const override; | ||
}; | ||
|
||
#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
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,26 @@ | ||
#ifndef AST_PARAMETER_DECL_HPP | ||
#define AST_PARAMETER_DECL_HPP | ||
|
||
#include "ast_node.hpp" | ||
|
||
class ParameterDecl : public Node | ||
{ | ||
private: | ||
Node *declaration_specifiers_; | ||
Node *declarator_; | ||
|
||
public: | ||
ParameterDecl(Node *declaration_specifiers, | ||
Node *declarator) | ||
: declaration_specifiers_(declaration_specifiers) | ||
, declarator_(declarator){}; | ||
~ParameterDecl() | ||
{ | ||
delete declaration_specifiers_; | ||
delete declarator_; | ||
}; | ||
void EmitRISC(std::ostream &stream, Context &context) const override; | ||
void Print(std::ostream &stream) const override; | ||
}; | ||
|
||
#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,6 @@ | ||
int f(int a, int b) | ||
{ | ||
int c = 8; | ||
return a + b * c * 5; | ||
} | ||
|
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,66 @@ | ||
.text | ||
.globl f | ||
f: | ||
# Allocate space to save ra and caller's s0 | ||
addi sp,sp,-8 | ||
# Save caller's ra | ||
sw ra,4(sp) | ||
# Save caller's s0 | ||
sw s0,0(sp) | ||
# Copy stack pointer into frame pointer | ||
mv s0,sp | ||
# Store parameter 0 ( a) | ||
sw a0,18446744073709551612(s0) | ||
# Store parameter 1 ( b) | ||
sw a1,18446744073709551608(s0) | ||
# Store parameter 2 ( a) | ||
sw a2,18446744073709551604(s0) | ||
# Store parameter 3 ( b) | ||
sw a3,18446744073709551600(s0) | ||
# Load int constant | ||
li a0, 8 | ||
addi sp,sp,-4 | ||
sw a0,-20(s0) | ||
# Evaluating left side for add | ||
# Load variable "a" | ||
lw a0,-4(s0) | ||
# Pushing left side result for add | ||
addi sp,sp,-4 | ||
sw a0,0(sp) | ||
# Eva;iatomg side result for add | ||
# Evaluating left side for mul | ||
# Evaluating left side for mul | ||
# Load variable "b" | ||
lw a0,-8(s0) | ||
# Pushing left side result for mul | ||
addi sp,sp,-4 | ||
sw a0,0(sp) | ||
# Eva;iatomg side result for mul | ||
# Load variable "c" | ||
lw a0,-20(s0) | ||
# Popping saved left side result for mul | ||
lw a1,0(sp) | ||
addi sp,sp,4 | ||
# Executing operation for mul | ||
mul a0,a1,a0 | ||
# Pushing left side result for mul | ||
addi sp,sp,-4 | ||
sw a0,0(sp) | ||
# Eva;iatomg side result for mul | ||
# Load int constant | ||
li a0, 5 | ||
# Popping saved left side result for mul | ||
lw a1,0(sp) | ||
addi sp,sp,4 | ||
# Executing operation for mul | ||
mul a0,a1,a0 | ||
# Popping saved left side result for add | ||
lw a1,0(sp) | ||
addi sp,sp,4 | ||
# Executing operation for add | ||
add a0,a1,a0 | ||
mv sp,s0 | ||
lw s0,(sp) | ||
lw ra,4(sp) | ||
addi sp,sp,8 | ||
ret |
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,7 @@ | ||
int f() { | ||
int c = 8; | ||
return a+ | ||
b* | ||
c* | ||
5; | ||
} |
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,57 @@ | ||
#include <sstream> | ||
#include <cassert> | ||
|
||
#include "ast_binary_op.hpp" | ||
#include <unistd.h> | ||
|
||
|
||
void BinaryOp::EmitRISC(std::ostream &stream, Context &context) const | ||
{ | ||
char const *insn = nullptr; | ||
switch (oper_) { | ||
case OP_ADD: insn = "add"; break; | ||
case OP_SUB: insn = "sub"; break; | ||
case OP_MUL: insn = "mul"; break; | ||
case OP_DIV: insn = "div"; break; | ||
case OP_BITAND: insn = "and"; break; | ||
case OP_BITOR: insn = "orb"; break; | ||
case OP_BITXOR: insn = "xor"; break; | ||
case OP_LEFT: insn = "shl"; break; | ||
case OP_RIGHT: insn = "sra"; break; | ||
default: assert(!"FIXME: Unhandled operator"); | ||
} | ||
|
||
stream << "# Evaluating left side for " << insn << "\n"; | ||
lhs_->EmitRISC(stream, context); | ||
stream << "# Pushing left side result for " << insn << "\n"; | ||
stream << "addi sp,sp,-4\n"; | ||
stream << "sw a0,0(sp)\n"; | ||
stream << "# Eva;iatomg side result for " << insn << "\n"; | ||
rhs_->EmitRISC(stream, context); | ||
stream << "# Popping saved left side result for " << insn << "\n"; | ||
stream << "lw a1,0(sp)\n"; | ||
stream << "addi sp,sp,4\n"; | ||
|
||
stream << "# Executing operation for " << insn << "\n"; | ||
stream << insn << " a0,a1,a0\n"; | ||
} | ||
|
||
void BinaryOp::Print(std::ostream &stream) const | ||
{ | ||
lhs_->Print(stream); | ||
char const *optxt = nullptr; | ||
switch (oper_) { | ||
case OP_ADD: optxt = "+"; break; | ||
case OP_SUB: optxt = "-"; break; | ||
case OP_MUL: optxt = "*"; break; | ||
case OP_DIV: optxt = "/"; break; | ||
case OP_BITAND: optxt = "&"; break; | ||
case OP_BITOR: optxt = "|"; break; | ||
case OP_BITXOR: optxt = "^"; break; | ||
case OP_LEFT: optxt = "<<"; break; | ||
case OP_RIGHT: optxt = ">>"; break; | ||
default: assert(!"FIXME: Unhandled operator"); | ||
} | ||
stream << optxt << "\n"; | ||
rhs_->Print(stream); | ||
} |
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
Oops, something went wrong.