Skip to content
Newer
Older
100644 102 lines (84 sloc) 2.73 KB
78b0b49 @steveicarus Support struct members that are packed arrays.
authored Mar 25, 2012
1 /*
2 * Copyright (c) 2012 Stephen Williams (steve@icarus.com)
3 *
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
f5aafc3 @arunpersaud updated FSF-address
arunpersaud authored Aug 28, 2012
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
78b0b49 @steveicarus Support struct members that are packed arrays.
authored Mar 26, 2012
18 */
19
20 # include "nettypes.h"
21 # include <cassert>
22
23 using namespace std;
24
14f229d @steveicarus Parse support for dynamic arrays.
authored Jul 13, 2012
25 nettype_base_t::~nettype_base_t()
26 {
27 }
28
b4cc9d1 @steveicarus Handle packed arrays of packed struct in l-value expressions.
authored Aug 5, 2012
29 long nettype_base_t::packed_width(void) const
30 {
31 return 0;
32 }
33
78b0b49 @steveicarus Support struct members that are packed arrays.
authored Mar 26, 2012
34 unsigned long netrange_width(const list<netrange_t>&packed)
35 {
36 unsigned wid = 1;
37 for (list<netrange_t>::const_iterator cur = packed.begin()
38 ; cur != packed.end() ; ++cur) {
14f229d @steveicarus Parse support for dynamic arrays.
authored Jul 14, 2012
39 unsigned use_wid = cur->width();
78b0b49 @steveicarus Support struct members that are packed arrays.
authored Mar 26, 2012
40 wid *= use_wid;
41 }
42
43 return wid;
44 }
45
46 /*
47 * Given a netrange_t list (which represent packed dimensions) and a
48 * prefix of calculated index values, calculate the canonical offset
49 * and width of the resulting slice. In this case, the "sb" argument
50 * is an extra index of the prefix.
51 */
52 bool prefix_to_slice(const std::list<netrange_t>&dims,
53 const std::list<long>&prefix, long sb,
54 long&loff, unsigned long&lwid)
55 {
56 assert(prefix.size() < dims.size());
57
58 size_t acc_wid = 1;
59 list<netrange_t>::const_iterator pcur = dims.end();
60 for (size_t idx = prefix.size()+1 ; idx < dims.size() ; idx += 1) {
61 -- pcur;
62 acc_wid *= pcur->width();
63 }
64
65 lwid = acc_wid;
66
67 -- pcur;
14f229d @steveicarus Parse support for dynamic arrays.
authored Jul 14, 2012
68 if (sb < pcur->get_msb() && sb < pcur->get_lsb())
78b0b49 @steveicarus Support struct members that are packed arrays.
authored Mar 26, 2012
69 return false;
14f229d @steveicarus Parse support for dynamic arrays.
authored Jul 14, 2012
70 if (sb > pcur->get_msb() && sb > pcur->get_lsb())
78b0b49 @steveicarus Support struct members that are packed arrays.
authored Mar 26, 2012
71 return false;
72
73 long acc_off = 0;
14f229d @steveicarus Parse support for dynamic arrays.
authored Jul 14, 2012
74 if (pcur->get_msb() >= pcur->get_lsb())
75 acc_off += (sb - pcur->get_lsb()) * acc_wid;
78b0b49 @steveicarus Support struct members that are packed arrays.
authored Mar 26, 2012
76 else
14f229d @steveicarus Parse support for dynamic arrays.
authored Jul 14, 2012
77 acc_off += (sb - pcur->get_msb()) * acc_wid;
78b0b49 @steveicarus Support struct members that are packed arrays.
authored Mar 26, 2012
78
7ce8a9b @caryr Fix some cppcheck warnings in the main directory
caryr authored Aug 8, 2012
79 if (prefix.empty()) {
78b0b49 @steveicarus Support struct members that are packed arrays.
authored Mar 26, 2012
80 loff = acc_off;
81 return true;
82 }
83
84 lwid *= pcur->width();
85
86 list<long>::const_iterator icur = prefix.end();
87 do {
88 -- pcur;
89 -- icur;
90 acc_wid *= pcur->width();
14f229d @steveicarus Parse support for dynamic arrays.
authored Jul 14, 2012
91 if (pcur->get_msb() >= pcur->get_lsb())
92 acc_off += (*icur - pcur->get_lsb()) * acc_wid;
78b0b49 @steveicarus Support struct members that are packed arrays.
authored Mar 26, 2012
93 else
14f229d @steveicarus Parse support for dynamic arrays.
authored Jul 14, 2012
94 acc_off += (*icur - pcur->get_msb()) * acc_wid;
78b0b49 @steveicarus Support struct members that are packed arrays.
authored Mar 26, 2012
95
96 } while (icur != prefix.begin());
97
98 loff = acc_off;
99
100 return true;
101 }
Something went wrong with that request. Please try again.