summaryrefslogtreecommitdiffstats
path: root/utils/regtools/lib/soc_desc.cpp
blob: 413c30936f2306a08d20610b7aa93bae4abe00ad (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
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2012 by Amaury Pouly
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "soc_desc.hpp"
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <stdio.h>
#include <string.h>

#define XML_CHAR_TO_CHAR(s) ((const char *)(s))

#define BEGIN_ATTR_MATCH(attr) \
    for(xmlAttr *a = attr; a; a = a->next) {

#define MATCH_X_ATTR(attr_name, hook, ...) \
    if(strcmp(XML_CHAR_TO_CHAR(a->name), attr_name) == 0) { \
        std::string s; \
        if(!parse_text_attr(a, s) || !hook(s, __VA_ARGS__)) \
            return false; \
    }

#define SOFT_MATCH_X_ATTR(attr_name, hook, ...) \
    if(strcmp(XML_CHAR_TO_CHAR(a->name), attr_name) == 0) { \
        std::string s; \
        if(parse_text_attr(a, s)) \
            hook(s, __VA_ARGS__); \
    }

#define SOFT_MATCH_SCT_ATTR(attr_name, var) \
    SOFT_MATCH_X_ATTR(attr_name, validate_sct_hook, var)

#define MATCH_TEXT_ATTR(attr_name, var) \
    MATCH_X_ATTR(attr_name, validate_string_hook, var)

#define MATCH_UINT32_ATTR(attr_name, var) \
    MATCH_X_ATTR(attr_name, validate_uint32_hook, var)

#define MATCH_BITRANGE_ATTR(attr_name, first, last) \
    MATCH_X_ATTR(attr_name, validate_bitrange_hook, first, last)

#define END_ATTR_MATCH() \
    }

#define BEGIN_NODE_MATCH(node) \
    for(xmlNode *sub = node; sub; sub = sub->next) {

#define MATCH_ELEM_NODE(node_name, array, parse_fn) \
    if(sub->type == XML_ELEMENT_NODE && strcmp(XML_CHAR_TO_CHAR(sub->name), node_name) == 0) { \
        array.resize(array.size() + 1); \
        if(!parse_fn(sub, array[array.size() - 1])) \
            return false; \
    }

#define SOFT_MATCH_ELEM_NODE(node_name, array, parse_fn) \
    if(sub->type == XML_ELEMENT_NODE && strcmp(XML_CHAR_TO_CHAR(sub->name), node_name) == 0) { \
        array.resize(array.size() + 1); \
        if(!parse_fn(sub, array[array.size() - 1])) \
            array.pop_back(); \
    }

#define END_NODE_MATCH() \
    }

bool validate_string_hook(const std::string& str, std::string& s)
{
    s = str;
    return true;
}

bool validate_sct_hook(const std::string& str, soc_reg_flags_t& flags)
{
    if(str == "yes") flags |= REG_HAS_SCT;
    else if(str != "no") return false;
    return true;
}

bool validate_unsigned_long_hook(const std::string& str, unsigned long& s)
{
    char *end;
    s = strtoul(str.c_str(), &end, 0);
    return *end == 0;
}

bool validate_uint32_hook(const std::string& str, uint32_t& s)
{
    unsigned long u;
    if(!validate_unsigned_long_hook(str, u)) return false;
#if ULONG_MAX > UINT32_MAX
    if(u > UINT32_MAX) return false;
#endif
    s = u;
    return true;
}

bool validate_bitrange_hook(const std::string& str, unsigned& first, unsigned& last)
{
    unsigned long a, b;
    size_t sep = str.find(':');
    if(sep == std::string::npos) return false;
    if(!validate_unsigned_long_hook(str.substr(0, sep), a)) return false;
    if(!validate_unsigned_long_hook(str.substr(sep + 1), b)) return false;
    if(a > 31 || b > 31 || a < b) return false;
    first = b;
    last = a;
    return true;
}

bool parse_text_attr(xmlAttr *attr, std::string& s)
{
    if(attr->children != attr->last)
        return false;
    if(attr->children->type != XML_TEXT_NODE)
        return false;
    s = XML_CHAR_TO_CHAR(attr->children->content);
    return true;
}

bool parse_value_elem(xmlNode *node, soc_reg_field_value_t& value)
{
    BEGIN_ATTR_MATCH(node->properties)
        MATCH_TEXT_ATTR("name", value.name)
        MATCH_UINT32_ATTR("value", value.value)
    END_ATTR_MATCH()

    return true;
}

bool parse_field_elem(xmlNode *node, soc_reg_field_t& field)
{
    BEGIN_ATTR_MATCH(node->properties)
        MATCH_TEXT_ATTR("name", field.name)
        MATCH_BITRANGE_ATTR("bitrange", field.first_bit, field.last_bit)
    END_ATTR_MATCH()

    BEGIN_NODE_MATCH(node->children)
        SOFT_MATCH_ELEM_NODE("value", field.value, parse_value_elem)
    END_NODE_MATCH()

    return true;
}

bool parse_reg_addr_elem(xmlNode *node, soc_reg_addr_t& addr)
{
    BEGIN_ATTR_MATCH(node->properties)
        MATCH_TEXT_ATTR("name", addr.name)
        MATCH_UINT32_ATTR("addr", addr.addr)
    END_ATTR_MATCH()

    return true;
}

bool parse_reg_formula_elem(xmlNode *node, soc_reg_formula_t& formula)
{
    BEGIN_ATTR_MATCH(node->properties)
        MATCH_TEXT_ATTR("string", formula.string)
    END_ATTR_MATCH()

    formula.type = REG_FORMULA_STRING;

    return true;
}

bool parse_reg_elem(xmlNode *node, soc_reg_t& reg)
{
    std::vector< soc_reg_formula_t > formulas;
    BEGIN_ATTR_MATCH(node->properties)
        MATCH_TEXT_ATTR("name", reg.name)
        SOFT_MATCH_SCT_ATTR("sct", reg.flags)
    END_ATTR_MATCH()

    BEGIN_NODE_MATCH(node->children)
        MATCH_ELEM_NODE("addr", reg.addr, parse_reg_addr_elem)
        MATCH_ELEM_NODE("formula", formulas, parse_reg_formula_elem)
        MATCH_ELEM_NODE("field", reg.field, parse_field_elem)
    END_NODE_MATCH()

    if(formulas.size() > 1)
    {
        fprintf(stderr, "Only one formula is allowed per register\n");
        return false;
    }
    if(formulas.size() == 1)
        reg.formula = formulas[0];

    return true;
}

bool parse_dev_addr_elem(xmlNode *node, soc_dev_addr_t& addr)
{
    BEGIN_ATTR_MATCH(node->properties)
        MATCH_TEXT_ATTR("name", addr.name)
        MATCH_UINT32_ATTR("addr", addr.addr)
    END_ATTR_MATCH()

    return true;
}

bool parse_dev_elem(xmlNode *node, soc_dev_t& dev)
{
    BEGIN_ATTR_MATCH(node->properties)
        MATCH_TEXT_ATTR("name", dev.name)
        MATCH_TEXT_ATTR("version", dev.version)
    END_ATTR_MATCH()

    BEGIN_NODE_MATCH(node->children)
        MATCH_ELEM_NODE("addr", dev.addr, parse_dev_addr_elem)
        MATCH_ELEM_NODE("reg", dev.reg, parse_reg_elem)
    END_NODE_MATCH()

    return true;
}

bool parse_soc_elem(xmlNode *node, soc_t& soc)
{
    BEGIN_ATTR_MATCH(node->properties)
        MATCH_TEXT_ATTR("name", soc.name)
        MATCH_TEXT_ATTR("desc", soc.desc)
    END_ATTR_MATCH()

    BEGIN_NODE_MATCH(node->children)
        MATCH_ELEM_NODE("dev", soc.dev, parse_dev_elem)
    END_NODE_MATCH()

    return true;
}

bool parse_root_elem(xmlNode *node, std::vector< soc_t >& soc)
{
    BEGIN_NODE_MATCH(node)
        MATCH_ELEM_NODE("soc", soc, parse_soc_elem)
    END_NODE_MATCH()
    return true;
}

bool soc_desc_parse_xml(const std::string& filename, std::vector< soc_t >& socs)
{
    LIBXML_TEST_VERSION

    xmlDoc *doc = xmlReadFile(filename.c_str(), NULL, 0);
    if(doc == NULL)
        return false;

    xmlNode *root_element = xmlDocGetRootElement(doc);

    bool ret = parse_root_elem(root_element, socs);

    xmlFreeDoc(doc);
    xmlCleanupParser();

    return ret;
}