summaryrefslogtreecommitdiffstats
path: root/common/EvalContext.cpp
blob: 876bb819b454b59035d96124912ffa1bcc82cc5e (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
/*
 * File:	EvalContext.h
 *
 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
 * See included license file for license details.
 */

#include "EvalContext.h"
#include <stdexcept>
#include "format_string.h"

using namespace elftosb;

EvalContext::EvalContext()
:	m_sourcesManager(0)
{
}

EvalContext::~EvalContext()
{
}

bool EvalContext::isVariableDefined(const std::string & name)
{
	variable_map_t::const_iterator it = m_variables.find(name);
	return it != m_variables.end();
}

uint32_t EvalContext::getVariableValue(const std::string & name)
{
	variable_map_t::const_iterator it = m_variables.find(name);
	if (it == m_variables.end())
	{
		throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
	}
	
	return it->second.m_value;
}

int_size_t EvalContext::getVariableSize(const std::string & name)
{
	variable_map_t::const_iterator it = m_variables.find(name);
	if (it == m_variables.end())
	{
		throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
	}
	
	return it->second.m_size;
}

void EvalContext::setVariable(const std::string & name, uint32_t value, int_size_t size)
{
	// check if var is locked
	variable_map_t::const_iterator it = m_variables.find(name);
	if (it != m_variables.end() && it->second.m_isLocked)
	{
		return;
	}
	
	// set var info
	variable_info_t info;
	info.m_value = value;
	info.m_size = size;
	info.m_isLocked = false;
	m_variables[name] = info;
}

bool EvalContext::isVariableLocked(const std::string & name)
{
	variable_map_t::const_iterator it = m_variables.find(name);
	if (it == m_variables.end())
	{
		throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
	}
	
	return it->second.m_isLocked;
}

void EvalContext::lockVariable(const std::string & name)
{
	variable_map_t::iterator it = m_variables.find(name);
	if (it == m_variables.end())
	{
		throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
	}
	
	it->second.m_isLocked = true;
}

void EvalContext::unlockVariable(const std::string & name)
{
	variable_map_t::iterator it = m_variables.find(name);
	if (it == m_variables.end())
	{
		throw std::runtime_error("undefined variable");
	}
	
	it->second.m_isLocked = false;
}

void EvalContext::dump()
{
	variable_map_t::iterator it = m_variables.begin();
	for (; it != m_variables.end(); ++it)
	{
		variable_info_t & info = it->second;
		const char * lockedString = info.m_isLocked ? "locked" : "unlocked";
		printf("%s = %u:%d (%s)\n", it->first.c_str(), info.m_value, (int)info.m_size, lockedString);
	}
}