OpenCores
URL https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [debugger/] [src/] [common/] [attribute.h] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 sergeykhbr
/*
2
 *  Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15
 */
16
 
17
#ifndef __DEBUGGER_ATTRIBUTE_H__
18
#define __DEBUGGER_ATTRIBUTE_H__
19
 
20
#include <stdint.h>
21
#include <string.h>
22
#include <iattr.h>
23
 
24
namespace debugger {
25
 
26
enum KindType {
27
        Attr_Invalid,
28
        Attr_String,
29
        Attr_Integer,
30
        Attr_UInteger,
31
        Attr_Floating,
32
        Attr_List,
33
        Attr_Data,
34
        Attr_Nil,
35
        Attr_Dict,
36
        Attr_Boolean,
37
        Attr_Interface,
38
        Attr_PyObject,
39
};
40
 
41
class AttributePairType;
42
 
43
class AttributeType : public IAttribute {
44
 public:
45
    KindType kind_;
46
    unsigned size_;
47
    union {
48
        char *string;
49
        int64_t integer;
50
        bool boolean;
51
        double floating;
52
        AttributeType *list;
53
        AttributePairType *dict;
54
        uint8_t *data;
55
        uint8_t data_bytes[8];  // Data without allocation
56
        void *py_object;
57
        IFace *iface;
58
        char *uobject;
59
    } u_;
60
 
61
    AttributeType(const AttributeType& other) {
62
        size_ = 0;
63
        clone(&other);
64
    }
65
 
66
    AttributeType() {
67
        kind_ = Attr_Invalid;
68
        size_ = 0;
69
        u_.integer = 0;
70
    }
71
    ~AttributeType() {
72
        attr_free();
73
    }
74
 
75
    /** IAttribute */
76
    virtual void allocAttrName(const char *name);
77
    virtual void freeAttrName();
78
    virtual void allocAttrDescription(const char *descr);
79
    virtual void freeAttrDescription();
80
 
81
    void attr_free();
82
 
83
    explicit AttributeType(const char *str) {
84
        make_string(str);
85
    }
86
 
87
    explicit AttributeType(IFace *mod) {
88
        kind_ = Attr_Interface;
89
        u_.iface = mod;
90
    }
91
 
92
    explicit AttributeType(KindType type) {
93
        kind_ = type;
94
        size_ = 0;
95
        u_.integer = 0;
96
    }
97
 
98
    explicit AttributeType(bool val) {
99
        kind_ = Attr_Boolean;
100
        size_ = 0;
101
        u_.boolean = val;
102
    }
103
 
104
    AttributeType(KindType type, uint64_t v) {
105
        if (type == Attr_Integer) {
106
            make_int64(static_cast<int64_t>(v));
107
        } else if (type == Attr_UInteger) {
108
            make_uint64(v);
109
        }
110
    }
111
 
112
    unsigned size() const { return size_; }
113
 
114
    bool is_floating() const {
115
        return kind_ == Attr_Floating;
116
    }
117
 
118
    double to_float() const {
119
        return u_.floating;
120
    }
121
 
122
    bool is_integer() const {
123
        return kind_ == Attr_Integer || kind_ == Attr_UInteger;
124
    }
125
 
126
    bool is_int64() const {
127
        return kind_ == Attr_Integer;
128
    }
129
 
130
    int to_int() const {
131
        return static_cast<int>(u_.integer);
132
    }
133
 
134
    uint32_t to_uint32() const {
135
        return static_cast<uint32_t>(u_.integer);
136
    }
137
 
138
    int64_t to_int64() const {
139
        return u_.integer;
140
    }
141
 
142
    bool is_uint64() const {
143
        return kind_ == Attr_UInteger;
144
    }
145
 
146
    uint64_t to_uint64() const {
147
        return u_.integer;
148
    }
149
 
150
    bool is_bool() const {
151
        return kind_ == Attr_Boolean;
152
    }
153
 
154
    bool to_bool() const { return u_.boolean; }
155
 
156
    bool is_string() const {
157
        return kind_ == Attr_String;
158
    }
159
 
160
    const char * to_string() const {
161
        return u_.string;
162
    }
163
 
164
    bool is_equal(const char *v);
165
 
166
    // capitalize letters in string;
167
    const char * to_upper() const {
168
        if (kind_ != Attr_String) {
169
            return 0;
170
        }
171
        char *p = u_.string;
172
        while (*p) {
173
            if (p[0] >= 'a' && p[0] <= 'z') {
174
                p[0] = p[0] - 'a' + 'A';
175
            }
176
            p++;
177
        }
178
        return u_.string;
179
    }
180
 
181
    bool is_list() const {
182
        return kind_ == Attr_List;
183
    }
184
 
185
    bool is_dict() const {
186
        return kind_ == Attr_Dict;
187
    }
188
 
189
    bool is_data() const {
190
        return kind_ == Attr_Data;
191
    }
192
 
193
    bool is_iface() const {
194
        return kind_ == Attr_Interface;
195
    }
196
 
197
    IFace *to_iface() const {
198
        return u_.iface;
199
    }
200
 
201
    bool is_nil() const {
202
        return kind_ == Attr_Nil;
203
    }
204
 
205
    bool is_invalid() const {
206
        return kind_ == Attr_Invalid;
207
    }
208
 
209
 
210
    void clone(const AttributeType *v);
211
 
212
    void make_nil() {
213
        kind_ = Attr_Nil;
214
        size_ = 0;
215
        u_.integer = 0;
216
    }
217
 
218
    void make_iface(IFace *value) {
219
        kind_ = Attr_Interface;
220
        u_.iface = value;
221
    }
222
 
223
    void make_floating(double value) {
224
        kind_ = Attr_Floating;
225
        size_ = 0;
226
        u_.floating = value;
227
    }
228
 
229
    void force_to_floating() {
230
        kind_ = Attr_Floating;
231
    }
232
 
233
    void make_int64(int64_t value) {
234
        kind_ = Attr_Integer;
235
        size_ = 0;
236
        u_.integer = value;
237
    }
238
 
239
    void make_uint64(uint64_t value) {
240
        kind_ = Attr_UInteger;
241
        size_ = 0;
242
        u_.integer = value;
243
    }
244
 
245
    void make_boolean(bool value) {
246
        kind_ = Attr_Boolean;
247
        size_ = 0;
248
        u_.boolean = value;
249
    }
250
 
251
    void make_string(const char *value);
252
 
253
    void make_data(unsigned size);
254
 
255
    void make_data(unsigned size, const void *data);
256
 
257
    void realloc_data(unsigned size);
258
 
259
    void make_list(unsigned size);
260
 
261
    void add_to_list(const AttributeType *item) {
262
        realloc_list(size()+1);
263
        (*this)[size()-1] = (*item);
264
    }
265
 
266
    void insert_to_list(unsigned idx, const AttributeType *item);
267
 
268
    void remove_from_list(unsigned idx);
269
 
270
    void trim_list(unsigned start, unsigned end);
271
 
272
    void swap_list_item(unsigned n, unsigned m);
273
 
274
    void realloc_list(unsigned size);
275
 
276
    void make_dict();
277
    void realloc_dict(unsigned size);
278
 
279
    // Getter:
280
    double floating() const { return u_.floating; }
281
 
282
    int64_t integer() const { return u_.integer; }
283
 
284
    const char *string() const { return u_.string; }
285
 
286
    bool boolean() const { return u_.boolean; }
287
 
288
    const AttributeType *list(unsigned idx) const {
289
        return &u_.list[idx];
290
    }
291
    AttributeType *list(unsigned idx) {
292
        return &u_.list[idx];
293
    }
294
 
295
    /* Quicksort algorithm with 'list' attribute */
296
    void sort(int idx = 0);
297
 
298
    bool has_key(const char *key) const;
299
 
300
    const AttributeType *dict_key(unsigned idx) const;
301
    AttributeType *dict_key(unsigned idx);
302
 
303
    const AttributeType *dict_value(unsigned idx) const;
304
    AttributeType *dict_value(unsigned idx);
305
 
306
    const uint8_t *data() const {
307
        if (size_ > 8) {
308
            return u_.data;
309
        }
310
        return u_.data_bytes;
311
    }
312
    uint8_t *data() {
313
        if (size_ > 8) {
314
            return u_.data;
315
        }
316
        return u_.data_bytes;
317
    }
318
 
319
    AttributeType& operator=(const AttributeType& other);
320
 
321
    /**
322
     * @brief Access to the single element of the 'list' attribute:
323
     */
324
    const AttributeType& operator[](unsigned idx) const;
325
    /** @overload */
326
    AttributeType& operator[](unsigned idx);
327
 
328
    /**
329
     * @brief Access to the single value attribute of the 'dictionary':
330
     */
331
    const AttributeType& operator[](const char *key) const;
332
    /** @overload */
333
    AttributeType& operator[](const char *key);
334
 
335
    /**
336
     * @brief Access to the single byte of the 'data' attribute:
337
     */
338
    const uint8_t& operator()(unsigned idx) const;
339
 
340
    const AttributeType& to_config();
341
    void from_config(const char *str);
342
};
343
 
344
class AttributePairType {
345
 public:
346
    AttributeType key_;
347
    AttributeType value_;
348
};
349
 
350
}  // namespace debugger
351
 
352
#endif  // __DEBUGGER_ATTRIBUTE_H__

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.