Skip to content

Commit

Permalink
added bunch of stuff like arithmetics(partially completed switch stat…
Browse files Browse the repository at this point in the history
…ement for now) and parametered function handling
  • Loading branch information
songmeric committed Mar 21, 2024
1 parent 6183386 commit 3cfbad2
Show file tree
Hide file tree
Showing 26 changed files with 510 additions and 61 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
CXXFLAGS += -std=c++20 -W -Wall -g -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -fsanitize=address -static-libasan -O0 -rdynamic --coverage -I include

CXXFLAGS += -DYYDEBUG=1
CXXFLAGS += -I build
CFLAGS += -I build

SOURCES := $(wildcard src/*.cpp)
DEPENDENCIES := $(patsubst src/%.cpp,build/%.d,$(SOURCES))
DEPENDENCIES = $(patsubst src/%.cpp,build/%.d,$(SOURCES))

OBJECTS := $(patsubst src/%.cpp,build/%.o,$(SOURCES))
OBJECTS += build/parser.tab.o build/lexer.yy.o
Expand Down
2 changes: 2 additions & 0 deletions include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "ast_variable_ref.hpp"

#include "ast_if_statement.hpp"
#include "ast_binary_op.hpp"
#include "ast_parameter_decl.hpp"

extern Node *ParseAST(std::string file_name);

Expand Down
25 changes: 25 additions & 0 deletions include/ast_binary_op.hpp
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
19 changes: 16 additions & 3 deletions include/ast_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
#include <vector>
#include <map>

struct Node;

class Variable
{
public:
std::string type;
std::string name;
size_t offset = 0;
ssize_t offset = 0;
bool inMemory = true;
};

class Scope
{
public:
std::vector<Variable> locals;
size_t localsSize;
size_t localsSize = 0;
};

class Function {
Expand Down Expand Up @@ -47,9 +49,20 @@ class Context

size_t SizeOfType(std::string const &) const;

std::string NewLabel();

std::vector<Function> functions;

int currentFunction = -1;
int currentFunction{-1};

// Stashed by DirectDeclarator
// to hold on to them until the function definition
Node *functionParameters{};

ssize_t lastLabel{};

// Stashed by ParameterDecl in its emit
std::vector<Variable> parameterDecls;

Variable *DeclareVariable(
std::string const &type,
Expand Down
4 changes: 3 additions & 1 deletion include/ast_direct_declarator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ class DirectDeclarator : public Node
{
private:
Node *identifier_;
Node *parameters_;

public:
DirectDeclarator(Node *identifier) : identifier_(identifier){};
DirectDeclarator(Node *identifier, Node *parameters)
: identifier_(identifier), parameters_(parameters) {}
~DirectDeclarator()
{
delete identifier_;
Expand Down
63 changes: 63 additions & 0 deletions include/ast_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,72 @@ class NodeList : public Node
}
}

Node *Child(size_t i) const
{
return nodes_.at(i);
}

Node *operator[](size_t i) const
{
return Child(i);
}

size_t size() const
{
return nodes_.size();
}

void PushBack(Node *item);
virtual void EmitRISC(std::ostream &stream, Context &context) const override;
virtual void Print(std::ostream &stream) const override;
};


enum Operator {
// Basics
OP_ADD,
OP_SUB,
OP_MUL,
OP_DIV,
OP_MOD,

// Function call
OP_CALL,

// Indirect (->)
OP_INDIRECT,
OP_MEMBER,

// Shifts
OP_LEFT,
OP_RIGHT,

// Comparisons
OP_CMPLT,
OP_CMPLE,
OP_CMPEQ,
OP_CMPGE,
OP_CMPGT,
OP_CMPNE,

// Comma
OP_COMMA,

// Bitwise
OP_BITAND,
OP_BITOR,
OP_BITXOR,

// Logical
OP_LOGAND,
OP_LOGOR,

// Unary
OP_INC,
OP_DEC,
OP_BITNOT,
OP_LOGNOT,
OP_ADDRESSOF
};

#endif
26 changes: 26 additions & 0 deletions include/ast_parameter_decl.hpp
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
6 changes: 6 additions & 0 deletions oneplusone.c
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;
}

66 changes: 66 additions & 0 deletions oneplusone.s
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
7 changes: 7 additions & 0 deletions oneplusone.s.printed
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
int f() {
int c = 8;
return a+
b*
c*
5;
}
57 changes: 57 additions & 0 deletions src/ast_binary_op.cpp
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);
}
6 changes: 6 additions & 0 deletions src/ast_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Function *Context::DeclareFunction(
std::string const &name)
{
currentFunction = functions.size();

Function &fn = functions.emplace_back();
fn.type = type;
fn.name = name;
Expand All @@ -29,6 +30,11 @@ size_t Context::SizeOfType(std::string const& /*type*/) const
return 4;
}

std::string Context::NewLabel()
{
return ".L" + std::to_string(++lastLabel);
}

Variable *Context::DeclareVariable(
std::string const& type, std::string const& name)
{
Expand Down
4 changes: 4 additions & 0 deletions src/ast_direct_declarator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ void DirectDeclarator::EmitRISC(std::ostream &stream, Context &context) const
{
identifier_->EmitRISC(stream, context);
stream << ":" << std::endl;

if (parameters_)
parameters_->EmitRISC(stream, context);
context.functionParameters = parameters_;
}

void DirectDeclarator::Print(std::ostream &stream) const
Expand Down
Loading

0 comments on commit 3cfbad2

Please sign in to comment.