-
Notifications
You must be signed in to change notification settings - Fork 0
/
dt.cpp
399 lines (332 loc) · 9.53 KB
/
dt.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <fstream>
#include <iostream>
#include <map>
#include <regex>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include <locale>
using namespace std;
inline std::string str_to_lower(const std::string &str)
{
std::locale loc;
string s;
for (size_t i = 0; i < str.size(); i++)
s.push_back(std::tolower(str[i], loc));
return s;
}
// Function to get substring from the last newline character
std::string getSubstringFromLastNewline(const std::string& input) {
size_t lastNewlinePos = input.rfind('\n');
if (lastNewlinePos != std::string::npos) {
if (lastNewlinePos + 1 < input.length()) {
return input.substr(lastNewlinePos + 1);
} else {
return "";
}
} else {
return input;
}
}
// Utility to trim whitespace
std::string trim(const std::string& str) {
const char* whitespace = " \t\n\r";
size_t start = str.find_first_not_of(whitespace);
if (start == std::string::npos) return "";
size_t end = str.find_last_not_of(whitespace);
return str.substr(start, end - start + 1);
}
std::string removeComments(const std::string& code) {
std::string result;
bool inMultiLineComment = false;
for (size_t i = 0; i < code.size(); ++i) {
if (!inMultiLineComment) {
if (i + 1 < code.size() && code[i] == '/' &&
code[i + 1] == '/') {
// Skip single-line comment
while (i < code.size() && code[i] != '\n') {
++i;
}
result += '\n'; // Keep the new line character
} else if (i + 1 < code.size() && code[i] == '/' &&
code[i + 1] == '*') {
// Enter multi-line comment
inMultiLineComment = true;
++i; // Skip the '*'
} else {
// Copy the character if it's not a comment
result += code[i];
}
} else {
if (i + 1 < code.size() && code[i] == '*' &&
code[i + 1] == '/') {
// Exit multi-line comment
inMultiLineComment = false;
++i; // Skip the '/'
}
// Ignore characters inside multi-line comments
}
}
return result;
}
// Helper function to extract the hex value if it exists
bool extractHexValue(const std::string& str, unsigned long& hexValue) {
size_t pos = str.find_last_of('@');
if (pos != std::string::npos && pos + 1 < str.size()) {
std::string hexPart = str.substr(pos + 1);
std::istringstream iss(hexPart);
iss >> std::hex >> hexValue;
if (!iss.fail()) {
return true;
}
}
return false;
}
std::string getAfterColon(const std::string& str) {
std::string r = str;
size_t pos = str.find(':');
if (pos != std::string::npos && pos + 1 < str.size()) {
r = trim(str.substr(pos + 1));
}
r = str_to_lower(r);
return r; // If no colon, return the original string
}
// Custom comparator for the map
struct CustomCompare {
bool operator()(const std::string& ai, const std::string& bi) const {
std::string a = removeComments(ai);
std::string b = removeComments(bi);
unsigned long hexA, hexB;
bool aHasHex = extractHexValue(a, hexA);
bool bHasHex = extractHexValue(b, hexB);
if (aHasHex && bHasHex) {
return hexA < hexB; // Compare by hex value if both
// have @hexvalue
} else if (aHasHex) {
return false; // a comes before b if only a has
// @hexvalue
} else if (bHasHex) {
return true; // b comes before a if only b has
// @hexvalue
} else {
return getAfterColon(a) < getAfterColon(b);
// Otherwise, compare lexicographically
}
}
};
std::map<std::string, int> g_map;
int g_common_pri;
int g_vendor_pri;
struct CustomCompare_property : public CustomCompare {
bool operator()(const std::string& ai, const std::string& bi) const {
int x = g_common_pri;
int y = x;
if (bi.empty()) return false;
if (ai.empty()) return true;
if (ai.find(',') != std::string::npos) x = g_vendor_pri;
if (bi.find(',') != std::string::npos) y = g_vendor_pri;
if (g_map.find(ai) != g_map.end()) x = g_map[ai];
if (g_map.find(bi) != g_map.end()) y = g_map[bi];
if (x != y) return x < y;
return CustomCompare::operator()(ai, bi);
}
};
class DeviceTreeNode {
public:
std::string name;
std::map<std::string, std::string, CustomCompare_property> properties;
std::map<std::string, DeviceTreeNode*, CustomCompare> children;
DeviceTreeNode(const std::string& name) : name(name) {}
// Add property key-value pair
void add_property(const std::string& key, const std::string& value) {
properties[key] = value;
}
// Add child node
void add_child(DeviceTreeNode* child) { children[child->name] = child; }
// Destructor to free children
~DeviceTreeNode() {
for (auto& child : children) {
delete child.second;
}
}
// Print the node and its children recursively
void print_tree(int indent = -1) const {
std::string indentation;
if (indent >= 0) {
indentation = std::string(indent, '\t');
std::cout << "\n" << indentation << name << " {\n";
}
for (const auto& prop : properties) {
std::cout << indentation << "\t" << prop.first;
if (prop.second.length() > 0)
std::cout << " = " << prop.second;
std::cout << ";\n";
}
for (const auto& child : children) {
child.second->print_tree(indent + 1);
}
if (indent >= 0) std::cout << indentation << "};\n";
}
};
std::string GetNodePath(std::vector<DeviceTreeNode*> node_stack) {
std::string str;
if (node_stack.size() <= 1) return str;
for (int i = 1; i < node_stack.size(); i++) {
str += getSubstringFromLastNewline(
getAfterColon(removeComments(trim(node_stack[i]->name))));
str += '/';
}
return str;
}
class DeviceTreeParser {
public:
DeviceTreeNode* root;
std::string last_key_value;
std::map<DeviceTreeNode*, std::string> last_node_value;
std::map<std::string, DeviceTreeNode*>
label_map; // To store labeled nodes
DeviceTreeParser() { root = new DeviceTreeNode("root"); }
~DeviceTreeParser() { delete root; }
// Parse the device tree file
void parse(const std::string& file_path, bool warning = false) {
std::ifstream file(file_path);
if (!file.is_open()) {
std::cerr << "Error: Could not open file " << file_path
<< "\n";
return;
}
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
file.close();
// Remove comments and trim the content
content = remove_comments(content);
content = trim(content);
DeviceTreeNode* current_node = root;
std::vector<DeviceTreeNode*> node_stack = {root};
std::string buffer;
for (size_t i = 0; i < content.size(); ++i) {
char c = content[i];
if (c == '{') {
// Start of a new node
buffer = trim(buffer);
DeviceTreeNode* new_node =
new DeviceTreeNode(buffer);
current_node->add_child(new_node);
node_stack.push_back(new_node);
CustomCompare compare;
if (compare(new_node->name,
last_node_value[current_node]) &&
warning)
std::cout << "wrong order: "
<< GetNodePath(node_stack)
<< "\n";
last_node_value[current_node] = new_node->name;
current_node = new_node;
buffer.clear();
last_key_value = "";
} else if (c == '}') {
// End of current node
node_stack.pop_back();
current_node = node_stack.back();
} else if (c == ';') {
// End of a property definition
buffer = trim(buffer);
if (!buffer.empty()) {
auto key_value =
parse_key_value(buffer);
if (!key_value.first.empty()) {
current_node->add_property(
key_value.first,
key_value.second);
CustomCompare_property compare;
if (compare(key_value.first,
last_key_value) &&
warning)
std::cout
<< "wrong order: "
<< GetNodePath(
node_stack)
<< key_value.first
<< "\n";
last_key_value =
key_value.first;
}
}
buffer.clear();
} else {
buffer += c;
}
}
}
void print_tree() const { root->print_tree(); }
private:
// Utility to remove comments
std::string remove_comments(const std::string& content) const {
std::regex comment_regex(R"(\/\*.*?\*\/|\/\/.*$)");
return std::regex_replace(content, comment_regex, "");
}
// Parse a key-value pair from a line like "key = value;"
std::pair<std::string, std::string> parse_key_value(
const std::string& line) const {
size_t equal_pos = line.find('=');
if (equal_pos == std::string::npos) return {trim(line), ""};
std::string key = trim(line.substr(0, equal_pos));
std::string value = trim(line.substr(equal_pos + 1));
return {key, value};
}
};
//first group
const char* g_order[] = {
"compatible",
"reg",
"reg-names",
"ranges",
"#interrupt-cells",
"interrupt-controller",
"interrupts",
"interrupt-names",
"#gpio-cells",
"gpio-controller",
"gpio-ranges",
"#address-cells",
"#size-cells",
"clocks",
"clock-names",
"assigned-clocks",
"assigned-clock-parents",
"assigned-clock-rates",
"dmas",
"dma-names",
};
//common property alphabet order.
//common property put after alphabet order
const char* g_order_2nd[] = {
"gpio",
"enable-active-high",
};
//vendor property
//"status", last one
const char* g_order_last[] = {
"status",
};
int dt_format(std::string filename, bool check) {
if (g_map.size() == 0) {
int prj = 0;
for (int j = 0; j < sizeof(g_order) / sizeof(g_order[0]); j++)
g_map[g_order[j]] = prj++;
g_common_pri = prj;
prj++;
for (int j = 0; j < sizeof(g_order_2nd) / sizeof(g_order_2nd[0]); j++)
g_map[g_order_2nd[j]] = prj++;
g_vendor_pri = prj;
prj++;
for (int j = 0; j < sizeof(g_order_last) / sizeof(g_order_last[0]); j++)
g_map[g_order_last[j]] = prj++;
}
DeviceTreeParser parser;
parser.parse(filename, check);
if (!check) parser.print_tree();
return 0;
}