TDL [codegen]: improving class structure

This commit is contained in:
Philippe Tillet
2018-12-17 18:38:02 -05:00
parent 9dfa6993fb
commit 97acf52dca
3 changed files with 44 additions and 31 deletions

View File

@@ -6,6 +6,12 @@
#include <list> #include <list>
#include <string> #include <string>
namespace llvm{
class LLVMType;
}
namespace tdl{ namespace tdl{
class module; class module;
@@ -197,7 +203,8 @@ class no_op: public statement { };
// Types // Types
class declarator: public node{ class declarator: public node{
public:
virtual llvm::LLVMType llvm_type(TYPE_T spec) const = 0;
}; };
class pointer_declarator: public declarator{ class pointer_declarator: public declarator{
@@ -210,35 +217,33 @@ public:
return this; return this;
} }
llvm::LLVMType llvm_type(TYPE_T spec) const;
private: private:
unsigned order_; unsigned order_;
}; };
class tile_declarator: public declarator{ class tile_declarator: public declarator{
public: public:
tile_declarator(node *shapes) tile_declarator(node *decl, node *shapes)
: shapes_((list<constant*>*)(shapes)) { } : decl_(decl), shapes_((list<constant*>*)(shapes)) { }
llvm::LLVMType llvm_type(TYPE_T spec) const;
public: public:
const node* decl_;
const list<constant*>* shapes_; const list<constant*>* shapes_;
}; };
class parameter: public declarator {
public:
parameter(TYPE_T spec, node *decl)
: spec_(spec), decl_(decl) { }
public:
const TYPE_T spec_;
const node *decl_;
};
class function_declarator: public declarator{ class function_declarator: public declarator{
public: public:
function_declarator(node *args) function_declarator(node *decl, node *args)
: args_((list<node*>*)args) { } : decl_(decl), args_((list<node*>*)args) { }
llvm::LLVMType llvm_type(TYPE_T spec) const;
public: public:
const node* decl_;
const list<node*>* args_; const list<node*>* args_;
}; };
@@ -247,6 +252,8 @@ public:
compound_declarator(node *ptr, node *tile) compound_declarator(node *ptr, node *tile)
: ptr_(ptr), tile_(tile) { } : ptr_(ptr), tile_(tile) { }
llvm::LLVMType llvm_type(TYPE_T spec) const;
public: public:
const node *ptr_; const node *ptr_;
const node *tile_; const node *tile_;
@@ -257,11 +264,24 @@ public:
init_declarator(node *decl, node *initializer) init_declarator(node *decl, node *initializer)
: decl_(decl), initializer_(initializer){ } : decl_(decl), initializer_(initializer){ }
llvm::LLVMType llvm_type(TYPE_T spec) const;
public: public:
const node *decl_; const node *decl_;
const node *initializer_; const node *initializer_;
}; };
class parameter: public node {
public:
parameter(TYPE_T spec, node *decl)
: spec_(spec), decl_(decl) { }
llvm::LLVMType* llvm_type() const;
public:
const TYPE_T spec_;
const node *decl_;
};
class type: public node{ class type: public node{
public: public:
@@ -276,10 +296,11 @@ public:
/* Function definition */ /* Function definition */
class function_definition: public node{ class function_definition: public node{
public: public:
function_definition(node *header, node *body) function_definition(TYPE_T spec, node *header, node *body)
: header_((function_declarator *)header), body_((compound_statement*)body) { } : spec_(spec), header_((function_declarator *)header), body_((compound_statement*)body) { }
public: public:
const TYPE_T spec_;
const function_declarator *header_; const function_declarator *header_;
const compound_statement *body_; const compound_statement *body_;
}; };

View File

@@ -84,7 +84,7 @@ abstract_declarator
; ;
direct_abstract_declarator direct_abstract_declarator
: '[' constant_list ']' { $$ = new tile_declarator($1); } : '[' constant_list ']' { $$ = new tile_declarator(nullptr, $1); }
constant : constant :
CONSTANT { $$ = new constant(atoi(yytext)); } CONSTANT { $$ = new constant(atoi(yytext)); }
@@ -279,17 +279,12 @@ iteration_statement
direct_declarator direct_declarator
: identifier { $$ = $1; } : identifier { $$ = $1; }
| direct_declarator '[' constant_list ']' { $$ = new tile_declarator($2); } | identifier '[' constant_list ']' { $$ = new tile_declarator($1, $2); }
| direct_declarator '(' parameter_list ')' { $$ = new function_declarator($2); } | identifier '(' parameter_list ')' { $$ = new function_declarator($1, $2); }
| direct_declarator '(' identifier_list ')' { $$ = new function_declarator($2); } | identifier '(' ')' { $$ = new function_declarator($1, nullptr); }
| direct_declarator '(' ')' { $$ = new function_declarator(nullptr); }
;
identifier_list
: identifier { $$ = new list<identifier*>((identifier*)$1); }
| identifier_list ',' identifier { $$ = append_ptr_list<identifier>($1, $2); }
; ;
parameter_list parameter_list
: parameter_declaration { $$ = new list<parameter*>((parameter*)$1); } : parameter_declaration { $$ = new list<parameter*>((parameter*)$1); }
| parameter_list ',' parameter_declaration { $$ = append_ptr_list<parameter>($1, $2); } | parameter_list ',' parameter_declaration { $$ = append_ptr_list<parameter>($1, $2); }
@@ -298,7 +293,6 @@ parameter_list
parameter_declaration parameter_declaration
: declaration_specifiers declarator { $$ = new parameter(get_type_spec($1), $2); } : declaration_specifiers declarator { $$ = new parameter(get_type_spec($1), $2); }
| declaration_specifiers abstract_declarator { $$ = new parameter(get_type_spec($1), $2); } | declaration_specifiers abstract_declarator { $$ = new parameter(get_type_spec($1), $2); }
| declaration_specifiers { $$ = new parameter(get_type_spec($1), nullptr); }
; ;
@@ -346,7 +340,6 @@ external_declaration
; ;
function_definition function_definition
: declaration_specifiers declarator compound_statement : type_specifier declarator compound_statement { $$ = new function_definition(get_type_spec($1), $2, $3); }
| declarator compound_statement { $$ = new function_definition($1, $2); }
; ;

View File

@@ -1,4 +1,3 @@
#include "codegen.h"
#include "ast.h" #include "ast.h"
namespace tdl{ namespace tdl{