1 /*
2  * File:  EvalContext.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 
8 #include "EvalContext.h"
9 #include <stdexcept>
10 #include "format_string.h"
11 
12 using namespace elftosb;
13 
EvalContext()14 EvalContext::EvalContext()
15 :         m_sourcesManager(0)
16 {
17 }
18 
~EvalContext()19 EvalContext::~EvalContext()
20 {
21 }
22 
isVariableDefined(const std::string & name)23 bool EvalContext::isVariableDefined(const std::string & name)
24 {
25           variable_map_t::const_iterator it = m_variables.find(name);
26           return it != m_variables.end();
27 }
28 
getVariableValue(const std::string & name)29 uint32_t EvalContext::getVariableValue(const std::string & name)
30 {
31           variable_map_t::const_iterator it = m_variables.find(name);
32           if (it == m_variables.end())
33           {
34                     throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
35           }
36 
37           return it->second.m_value;
38 }
39 
getVariableSize(const std::string & name)40 int_size_t EvalContext::getVariableSize(const std::string & name)
41 {
42           variable_map_t::const_iterator it = m_variables.find(name);
43           if (it == m_variables.end())
44           {
45                     throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
46           }
47 
48           return it->second.m_size;
49 }
50 
setVariable(const std::string & name,uint32_t value,int_size_t size)51 void EvalContext::setVariable(const std::string & name, uint32_t value, int_size_t size)
52 {
53           // check if var is locked
54           variable_map_t::const_iterator it = m_variables.find(name);
55           if (it != m_variables.end() && it->second.m_isLocked)
56           {
57                     return;
58           }
59 
60           // set var info
61           variable_info_t info;
62           info.m_value = value;
63           info.m_size = size;
64           info.m_isLocked = false;
65           m_variables[name] = info;
66 }
67 
isVariableLocked(const std::string & name)68 bool EvalContext::isVariableLocked(const std::string & name)
69 {
70           variable_map_t::const_iterator it = m_variables.find(name);
71           if (it == m_variables.end())
72           {
73                     throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
74           }
75 
76           return it->second.m_isLocked;
77 }
78 
lockVariable(const std::string & name)79 void EvalContext::lockVariable(const std::string & name)
80 {
81           variable_map_t::iterator it = m_variables.find(name);
82           if (it == m_variables.end())
83           {
84                     throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
85           }
86 
87           it->second.m_isLocked = true;
88 }
89 
unlockVariable(const std::string & name)90 void EvalContext::unlockVariable(const std::string & name)
91 {
92           variable_map_t::iterator it = m_variables.find(name);
93           if (it == m_variables.end())
94           {
95                     throw std::runtime_error("undefined variable");
96           }
97 
98           it->second.m_isLocked = false;
99 }
100 
dump()101 void EvalContext::dump()
102 {
103           variable_map_t::iterator it = m_variables.begin();
104           for (; it != m_variables.end(); ++it)
105           {
106                     variable_info_t & info = it->second;
107                     const char * lockedString = info.m_isLocked ? "locked" : "unlocked";
108                     printf("%s = %u:%d (%s)\n", it->first.c_str(), info.m_value, (int)info.m_size, lockedString);
109           }
110 }
111 
112