Skip to content

Commit

Permalink
Merge pull request #52 from AjayBrahmakshatriya/master
Browse files Browse the repository at this point in the history
Added support for reference types and creating named variables with decl
  • Loading branch information
AjayBrahmakshatriya authored Sep 1, 2023
2 parents 21796c2 + e7fe322 commit ed1dd16
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 8 deletions.
1 change: 1 addition & 0 deletions include/blocks/block_replacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class block_replacer : public block_visitor {
virtual void visit(std::shared_ptr<type>) override;
virtual void visit(std::shared_ptr<scalar_type>) override;
virtual void visit(std::shared_ptr<pointer_type>) override;
virtual void visit(std::shared_ptr<reference_type>) override;
virtual void visit(std::shared_ptr<function_type>) override;
virtual void visit(std::shared_ptr<array_type>) override;
virtual void visit(std::shared_ptr<builder_var_type>) override;
Expand Down
2 changes: 2 additions & 0 deletions include/blocks/block_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class var;
class type;
class scalar_type;
class pointer_type;
class reference_type;
class function_type;
class array_type;
class builder_var_type;
Expand Down Expand Up @@ -119,6 +120,7 @@ class block_visitor {
virtual void visit(std::shared_ptr<type>);
virtual void visit(std::shared_ptr<scalar_type>);
virtual void visit(std::shared_ptr<pointer_type>);
virtual void visit(std::shared_ptr<reference_type>);
virtual void visit(std::shared_ptr<function_type>);
virtual void visit(std::shared_ptr<array_type>);
virtual void visit(std::shared_ptr<builder_var_type>);
Expand Down
1 change: 1 addition & 0 deletions include/blocks/c_code_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class c_code_generator : public block_visitor {
virtual void visit(var::Ptr);
virtual void visit(scalar_type::Ptr);
virtual void visit(pointer_type::Ptr);
virtual void visit(reference_type::Ptr);
virtual void visit(array_type::Ptr);
virtual void visit(builder_var_type::Ptr);
virtual void visit(named_type::Ptr);
Expand Down
11 changes: 11 additions & 0 deletions include/blocks/var.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ class pointer_type : public type {
}
virtual void dump(std::ostream &, int) override;
};

class reference_type : public type {
public:
typedef std::shared_ptr<reference_type> Ptr;
type::Ptr referenced_type;
virtual void accept(block_visitor *a) override {
a->visit(self<reference_type>());
}
virtual void dump(std::ostream &, int) override;
};

class function_type : public type {
public:
typedef std::shared_ptr<function_type> Ptr;
Expand Down
10 changes: 10 additions & 0 deletions include/builder/block_type_extractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ class type_extractor<T *> {
return type;
}
};
// Type specialization for reference type
template <typename T>
class type_extractor<T &> {
public:
static block::type::Ptr extract_type(void) {
block::reference_type::Ptr type = std::make_shared<block::reference_type>();
type->referenced_type = type_extractor<T>::extract_type();
return type;
}
};

template <>
class type_extractor<void> {
Expand Down
6 changes: 4 additions & 2 deletions include/builder/dyn_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ struct as_global {
// With name is just like as_global but can be used locally
struct with_name {
std::string name;
with_name(const std::string &n) : name(n) {}
bool with_decl;
with_name(const std::string &n, bool wd = false) : name(n), with_decl(wd) {}
};

template <typename T>
Expand Down Expand Up @@ -220,8 +221,9 @@ class dyn_var_impl : public var {
}
dyn_var_impl(const with_name &v) {
// with_name constructors don't usually get declarations
create_dyn_var(true);
create_dyn_var(!v.with_decl);
block_var->var_name = v.name;
block_var->preferred_name = "";
var_name = v.name;
}
dyn_var_impl(const dyn_var_sentinel_type &a, std::string name = "") {
Expand Down
12 changes: 6 additions & 6 deletions include/builder/static_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ class static_var {
"= " TOSTRING(MAX_TRACKING_VARIABLE_SIZE));
T val;

static_var(const static_var& other): static_var((T) other) {}
static_var& operator=(const static_var& other) {
*this = (T) other;
static_var(const static_var &other) : static_var((T)other) {}
static_var &operator=(const static_var &other) {
*this = (T)other;
return *this;
}

template <typename OT>
static_var(const static_var<OT> &other): static_var((T)(OT) other) {}
static_var(const static_var<OT> &other) : static_var((T)(OT)other) {}
template <typename OT>
static_var& operator=(const static_var<OT> &other) {
*this = (T) (OT) other;
static_var &operator=(const static_var<OT> &other) {
*this = (T)(OT)other;
return *this;
}

Expand Down
18 changes: 18 additions & 0 deletions samples/outputs.var_names/sample10
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,25 @@ STMT_BLOCK
VAR_EXPR
VAR (a_0)
INT_CONST (6)
DECL_STMT
REFERENCE_TYPE
SCALAR_TYPE (INT)
VAR (b_1)
SQ_BKT_EXPR
VAR_EXPR
VAR (a_0)
INT_CONST (0)
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (b_1)
PLUS_EXPR
VAR_EXPR
VAR (b_1)
INT_CONST (1)
{
int* a_0;
a_0[5] = a_0[6];
int& b_1 = a_0[0];
b_1 = b_1 + 1;
}
23 changes: 23 additions & 0 deletions samples/outputs.var_names/sample49
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FUNC_DECL
SCALAR_TYPE (VOID)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (my_var)
NO_INITIALIZATION
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (my_var)
INT_CONST (1)
DECL_STMT
SCALAR_TYPE (INT)
VAR (y_0)
VAR_EXPR
VAR (my_var)
void foo (void) {
int my_var;
my_var = 1;
int y_0 = my_var;
}

18 changes: 18 additions & 0 deletions samples/outputs/sample10
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,25 @@ STMT_BLOCK
VAR_EXPR
VAR (var0)
INT_CONST (6)
DECL_STMT
REFERENCE_TYPE
SCALAR_TYPE (INT)
VAR (var1)
SQ_BKT_EXPR
VAR_EXPR
VAR (var0)
INT_CONST (0)
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (var1)
PLUS_EXPR
VAR_EXPR
VAR (var1)
INT_CONST (1)
{
int* var0;
var0[5] = var0[6];
int& var1 = var0[0];
var1 = var1 + 1;
}
23 changes: 23 additions & 0 deletions samples/outputs/sample49
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FUNC_DECL
SCALAR_TYPE (VOID)
STMT_BLOCK
DECL_STMT
SCALAR_TYPE (INT)
VAR (my_var)
NO_INITIALIZATION
EXPR_STMT
ASSIGN_EXPR
VAR_EXPR
VAR (my_var)
INT_CONST (1)
DECL_STMT
SCALAR_TYPE (INT)
VAR (var0)
VAR_EXPR
VAR (my_var)
void foo (void) {
int my_var;
my_var = 1;
int var0 = my_var;
}

2 changes: 2 additions & 0 deletions samples/sample10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ using builder::dyn_var;
static void foo(void) {
dyn_var<int *> a;
a[5] = a[6];
dyn_var<int &> b = a[0];
b = b + 1;
}
int main(int argc, char *argv[]) {
builder::builder_context context;
Expand Down
20 changes: 20 additions & 0 deletions samples/sample49.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "blocks/c_code_generator.h"
#include "builder/dyn_var.h"

using builder::dyn_var;

static void foo(void) {
std::string name = "my_var";
dyn_var<int> v = builder::with_name(name, true);
v = 1;
dyn_var<int> v1 = builder::with_name(name);
dyn_var<int> y = v1;
}
int main(int argc, char *argv[]) {
builder::builder_context context;
auto ast = context.extract_function_ast(foo, "foo");
ast->dump(std::cout, 0);

block::c_code_generator::generate_code(ast, std::cout, 0);
return 0;
}
4 changes: 4 additions & 0 deletions src/blocks/block_replacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ void block_replacer::visit(pointer_type::Ptr a) {
a->pointee_type = rewrite<type>(a->pointee_type);
node = a;
}
void block_replacer::visit(reference_type::Ptr a) {
a->referenced_type = rewrite<type>(a->referenced_type);
node = a;
}
void block_replacer::visit(function_type::Ptr a) {
a->return_type = rewrite<type>(a->return_type);
for (unsigned int i = 0; i < a->arg_types.size(); i++) {
Expand Down
3 changes: 3 additions & 0 deletions src/blocks/block_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ void block_visitor::visit(scalar_type::Ptr a) {}
void block_visitor::visit(pointer_type::Ptr a) {
a->pointee_type->accept(this);
}
void block_visitor::visit(reference_type::Ptr a) {
a->referenced_type->accept(this);
}
void block_visitor::visit(function_type::Ptr a) {
a->return_type->accept(this);
for (unsigned int i = 0; i < a->arg_types.size(); i++)
Expand Down
7 changes: 7 additions & 0 deletions src/blocks/c_code_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ void c_code_generator::visit(pointer_type::Ptr type) {
type->pointee_type->accept(this);
oss << "*";
}
void c_code_generator::visit(reference_type::Ptr type) {
if (!isa<scalar_type>(type->referenced_type) && !isa<pointer_type>(type->referenced_type) &&
!isa<named_type>(type->referenced_type))
assert(false && "Printing pointers of complex type is not supported yet");
type->referenced_type->accept(this);
oss << "&";
}
void c_code_generator::visit(array_type::Ptr type) {
if (!isa<scalar_type>(type->element_type) && !isa<pointer_type>(type->element_type) &&
!isa<named_type>(type->element_type))
Expand Down
5 changes: 5 additions & 0 deletions src/blocks/var.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ void pointer_type::dump(std::ostream &oss, int indent) {
oss << "POINTER_TYPE" << std::endl;
pointee_type->dump(oss, indent + 1);
}
void reference_type::dump(std::ostream &oss, int indent) {
printer::indent(oss, indent);
oss << "REFERENCE_TYPE" << std::endl;
referenced_type->dump(oss, indent + 1);
}
void function_type::dump(std::ostream &oss, int indent) {
printer::indent(oss, indent);
oss << "FUNCITON_TYPE" << std::endl;
Expand Down
5 changes: 5 additions & 0 deletions src/blocks/var_namer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ void var_namer::visit(decl_stmt::Ptr stmt) {
stmt->decl_var = collected_decls[so];
return;
}

// If a variable already has a name, created with with_name(_, true), skip naming it
if (stmt->decl_var->var_name != "")
return;

// We have found a new variable decl, first assign it a name
if (stmt->decl_var->preferred_name != "") {
stmt->decl_var->var_name = stmt->decl_var->preferred_name + "_" + std::to_string(var_counter);
Expand Down

0 comments on commit ed1dd16

Please sign in to comment.