aboutsummaryrefslogtreecommitdiffstats
path: root/c_compiler/src/declaration.cpp
blob: b151077a4bbdf0b317691c3f4ed5b43dd0614543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "declaration.hpp"
#include "bindings.hpp"
#include "type.hpp"
#include "expression.hpp"

#include <cstdio>


// Declaration definition

Declaration::Declaration(const std::string& id, Expression* initializer)
    : id_(id), initializer_(initializer), extern_declaration_(false)
{}

void Declaration::print() const
{
    if(next_declaration_ != nullptr)
	next_declaration_->print();
    
    if(id_ != "")
	printf("%s\n", id_.c_str());
}

void Declaration::printXml() const
{    
    if(next_declaration_ != nullptr)
	next_declaration_->printXml();

    if(next_list_declaration_ != nullptr)
	next_list_declaration_->printXml();

    if(id_ != "")
	printf("<Variable id=\"%s\" />", id_.c_str());
}

VariableStackBindings Declaration::printAsm(VariableStackBindings bindings, unsigned& label_count) const
{
    (void)label_count;
    if(!extern_declaration_)
    {
	if(initializer_ == nullptr)
	    printf("\t.comm\t%s,4,4\n", id_.c_str());
	else
	    printf("\t.data\n\t.globl\t%s\n%s:\n\t.word\t%d\n",
		   id_.c_str(), id_.c_str(), initializer_->constantFold());
    }
    
    bindings.insertBinding(id_, type_, -1);
    return bindings;
}

VariableStackBindings Declaration::localAsm(VariableStackBindings bindings, unsigned& label_count) const
{
    if(next_declaration_ != nullptr)
	bindings = next_declaration_->localAsm(bindings, label_count);

    if(next_list_declaration_ != nullptr)
	bindings = next_list_declaration_->localAsm(bindings, label_count);
    
    if(id_ != "")
    {
	if(initializer_ != nullptr)
	    initializer_->printAsm(bindings, label_count);
	else
	    printf("\tmove\t$2,$0\n");

	int stack_position = bindings.currentStackPosition();
	printf("\tsw\t$2,%d($fp)\n", stack_position);
	bindings.insertBinding(id_, type_, stack_position);
	bindings.increaseStackPosition();
    }

    return bindings;
}

void Declaration::linkDeclaration(Declaration* next_declaration)
{
    DeclarationPtr decl_ptr(next_declaration);
    next_declaration_ = decl_ptr;
}

void Declaration::linkListDeclaration(Declaration* next_declaration)
{
    DeclarationPtr decl_ptr(next_declaration);
    next_list_declaration_ = decl_ptr;
}

void Declaration::setType(TypePtr type)
{
    type_ = type;
}

void Declaration::setInitializer(Expression* initializer)
{
    ExpressionPtr expression_ptr(initializer);
    initializer_ = expression_ptr;
}

void Declaration::setExternDeclaration(bool is_extern)
{
    extern_declaration_ = is_extern;
}

DeclarationPtr Declaration::getNext() const
{
    return next_declaration_;
}

DeclarationPtr Declaration::getNextListItem() const
{
    return next_list_declaration_;
}

std::string Declaration::getId() const
{
    return id_;
}

TypePtr Declaration::getType() const
{
    return type_;
}