Skip to content

Commit fce7e0a

Browse files
committed
Move the parse_num_arg() handler table to the class itself since one such instance is needed per class object
Signed-off-by: Sonal Santan <sonal.santan@amd.com>
1 parent 2a5d626 commit fce7e0a

2 files changed

Lines changed: 31 additions & 44 deletions

File tree

src/cpp/common/assembler_state.cpp

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
#include "regex_wrapper.h"
88
#include "aiebu/aiebu_error.h"
99

10-
#include <functional>
11-
#include <unordered_map>
12-
1310
namespace aiebu {
1411

1512
assembler_state::
@@ -162,59 +159,23 @@ process(bool makeunique)
162159
* 3. If string is hex number string (start with "0x"): it return decimal equavalent
163160
* 4. If string is numeric string: it will convert to decimal
164161
*/
165-
uint32_t assembler_state::parse_num_arg(const std::string& str) {
166-
static const std::unordered_map<std::string, std::function<uint32_t(const std::string&)>> handlers = {
167-
{"@", [this](const std::string& s) -> uint32_t {
168-
//If string start with '@': it can be either pad name or label name
169-
auto key = s.substr(1);
170-
if (m_scratchpad.find(key) != m_scratchpad.end())
171-
return m_scratchpad[key]->get_base() + m_scratchpad[key]->get_offset();
172-
if (m_labelmap.find(key) != m_labelmap.end())
173-
return m_labelmap[key]->get_pos();
174-
throw error(error::error_code::invalid_asm, "Label " + key + " not present in label map\n");
175-
}},
176-
{"s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
177-
{"mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
178-
{"mem_s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
179-
{"mem_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
180-
{"shim_s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
181-
{"shim_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
182-
{"tile_s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
183-
{"tile_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
184-
{"shim_ctrl_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }}
185-
};
186-
162+
uint32_t assembler_state::parse_num_arg(const std::string &str) const {
187163
// check if its pad/label/actor
188164
for (const auto& [prefix, handler] : handlers) {
189165
if (str.rfind(prefix) == 0) {
190166
return handler(str);
191167
}
192168
}
193169

194-
/*
195-
if (str.rfind("tile_") == 0)
196-
{
197-
// Parse and return perticular col and row 32bit base address eg: tile_0_1
198-
constexpr static size_t col_start = 5;
199-
constexpr static size_t len_of_underscore = 1;
200-
constexpr static size_t row_mask = 0x1F;
201-
constexpr static size_t col_mask = 0x7F;
202-
constexpr static size_t col_shift = 5;
203-
size_t row_start = col_start + len_of_underscore + str.substr(col_start).rfind("_");
204-
uint32_t col = std::stoi(str.substr(col_start));
205-
uint32_t row = std::stoi(str.substr(row_start));
206-
return (((col & col_mask) << col_shift) | (row & row_mask));
207-
}
208-
*/
209170
static const regex kTileRe{R"(^tile_(\d+)_(\d+)$)"};
210171
smatch m;
211-
if (regex_match(str, m, kTileRe) && (m.size() == 3))
172+
if ((str[0] == 't') && regex_match(str, m, kTileRe) && (m.size() == 3))
212173
{
213174
constexpr static size_t row_mask = 0x1F;
214175
constexpr static size_t col_mask = 0x7F;
215176
constexpr static size_t col_shift = 5;
216-
const uint32_t col = std::stoi(m[1].str());
217-
const uint32_t row = std::stoi(m[2].str());
177+
const uint32_t col = std::stoi(m[1]);
178+
const uint32_t row = std::stoi(m[2]);
218179
return (((col & col_mask) << col_shift) | (row & row_mask));
219180
}
220181
else if (str.rfind("0x") == 0)

src/cpp/common/assembler_state.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <memory>
1616
#include <map>
1717
#include <set>
18+
#include <functional>
19+
#include <unordered_map>
1820

1921
namespace aiebu {
2022

@@ -145,6 +147,7 @@ class assembler_state : public std::enable_shared_from_this<assembler_state>
145147
assembler_state(const assembler_state& rhs) = default;
146148
assembler_state& operator=(const assembler_state& rhs) = delete;
147149
assembler_state(assembler_state &&s) = default;
150+
148151
public:
149152
std::shared_ptr<std::map<std::string, std::shared_ptr<isa_op>>> m_isa;
150153
std::vector<std::shared_ptr<asm_data>>& m_data;
@@ -182,7 +185,7 @@ class assembler_state : public std::enable_shared_from_this<assembler_state>
182185
return (m_scratchpad.find(label) != m_scratchpad.end());
183186
}
184187

185-
uint32_t parse_num_arg(const std::string& str);
188+
uint32_t parse_num_arg(const std::string& str) const;
186189

187190
void process(bool makeunique);
188191

@@ -246,6 +249,29 @@ class assembler_state : public std::enable_shared_from_this<assembler_state>
246249
virtual symbol::patch_schema get_shim_dma_patching() const = 0;
247250
virtual symbol::patch_schema get_control_packet_patching() const = 0;
248251
virtual ~assembler_state() = default;
252+
253+
private:
254+
const std::unordered_map<std::string, std::function<uint32_t(const std::string&)>> handlers = {
255+
{"@", [this](const std::string& s) -> uint32_t {
256+
//If string start with '@': it can be either pad name or label name
257+
auto key = s.substr(1);
258+
if (m_scratchpad.find(key) != m_scratchpad.end())
259+
return m_scratchpad[key]->get_base() + m_scratchpad[key]->get_offset();
260+
if (m_labelmap.find(key) != m_labelmap.end())
261+
return m_labelmap[key]->get_pos();
262+
throw error(error::error_code::invalid_asm, "Label " + key + " not present in label map\n");
263+
}},
264+
{"s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
265+
{"mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
266+
{"mem_s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
267+
{"mem_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
268+
{"shim_s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
269+
{"shim_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
270+
{"tile_s2mm_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
271+
{"tile_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }},
272+
{"shim_ctrl_mm2s_", [this](const std::string& s) -> uint32_t { return get_actor(s); }}
273+
};
274+
249275
};
250276

251277

0 commit comments

Comments
 (0)