Website Summary Files Commits Branches @commit @diff @stats
path: root / spasm / src / asm / symbol.cpp
blob: d434060f369995d89d3506c5ec65153a73e0382b (plain) (blame)
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
#include <list>
#include <string>

#include "symbol.hpp"

namespace SpasmImpl
{
namespace ASM
{
const size_t Symbol::notdefined = 0xff;
// const size_t Symbol::notdefined = ~0ull;

Symbol::Symbol() : _definition(notdefined) {}

Symbol::Symbol(const std::string& identifier, size_t definition)
    : _identifier(identifier), _definition(definition)
{
}

const std::string& Symbol::identifier() const
{
    return _identifier;
}

size_t Symbol::definition() const
{
    return _definition;
}

void Symbol::define(size_t definition)
{
    _definition = definition;
}

size_t Symbol::add_position(size_t position)
{
    _positions.push_back(position);

    return _definition;
}

Symbol::Positions_t::const_iterator Symbol::positions_begin() const
{
    return _positions.begin();
}

Symbol::Positions_t::const_iterator Symbol::positions_end() const
{
    return _positions.end();
}

Symbol_Table::Symbol_Table() {}

/*!
** Search the table for symbol by identifier
**
** \param identifier - identifier of the symbol
**
** \return pointer to the symbol if it exists or NULL otherwise.
*/
Symbol* Symbol_Table::find(const std::string& identifier) const
{
    const_iterator i = _table.find(identifier);
    return (i != _table.end()) ? i->second : NULL;
}

/*!
** Insert symbol with identifier indentifier on position position
** of just add new position for symbol.
** \param identifier - identifer of the symbol
** \param position - position of the symbol
**
** \return pointer to the symbol
*/
Symbol* Symbol_Table::insert(const std::string& identifier, size_t position)
{
    Symbol* psymbol = find(identifier);
    if (psymbol == NULL)
    {
        psymbol = new Symbol(identifier);
        _table.insert(std::make_pair(identifier, psymbol));
    }
    psymbol->add_position(position);

    return psymbol;
}

/*!
** define symbol with identifier indentifier
** If the symbol already exists its definition is changed
** \param identifier - identifer of the symbol
** \param definition - definition of the symbol
**
** \return pointer to the symbol
*/
Symbol* Symbol_Table::define(const std::string& identifier, size_t definition)
{
    Symbol* psymbol = find(identifier);
    if (psymbol == NULL)
    {
        psymbol = new Symbol(identifier, definition);
        _table.insert(std::make_pair(identifier, psymbol));
    }
    else
        psymbol->define(definition);

    return psymbol;
}

Symbol_Table::const_iterator Symbol_Table::begin() const
{
    return _table.begin();
}

Symbol_Table::const_iterator Symbol_Table::end() const
{
    return _table.end();
}

}  // namespace ASM
}  // namespace SpasmImpl