aboutsummaryrefslogtreecommitdiffstats
path: root/c_compiler/src/declaration.cpp
blob: 9faf133113b04c3d3e44ff51fc9794ed39715aa4 (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
#include "declaration.hpp"
#include "bindings.hpp"
#include "type.hpp"
#include "expression.hpp"

#include <iostream>


// Declaration definition

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

void Declaration::print() const
{
    if(next_declaration_ != nullptr)
	next_declaration_->print();
    
    if(id_ != "")
	std::cout << id_ << std::endl;
}

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

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

    if(id_ != "")
	std::cout << "<Variable id=\""<< id_ << "\" />" << std::endl;
}

VariableStackBindings Declaration::printAsm(VariableStackBindings bindings) const
{
    // if(init == nullptr)
    // 	std::cout << "\t.comm\t" << id << ",4,4" << std::endl;
    // else {
    // 	std::cout << "\t.data\n\t.globl\t" << id << std::endl;
    // 	std::cout << id << ":\n\t.word\t" << std::endl;
    // }

    // return bindings;

    if(next_declaration_ != nullptr)
	bindings = next_declaration_->printAsm(bindings);

    if(next_list_declaration_ != nullptr)
	bindings = next_list_declaration_->printAsm(bindings);
    
    if(id_ != "") {
	if(initializer_ != nullptr)
	    initializer_->printAsm(bindings);
	else
	    std::cout << "\tmove\t$2,$0" << std::endl;

	int32_t stack_position = bindings.currentStackPosition();

	std::cout << "\tsw\t$2," << stack_position << "($fp)" << std::endl;

	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(Type* type)
{
    TypePtr type_ptr(type);
    type_ = type_ptr;
}

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

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

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

std::string Declaration::getType() const
{
    return type_->getType();
}