2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/yosys.h"
21 #include "kernel/sigtools.h"
24 PRIVATE_NAMESPACE_BEGIN
26 Const
make_value(string
&value
)
28 if (GetSize(value
) >= 2 && value
.front() == '"' && value
.back() == '"')
29 return Const(value
.substr(1, GetSize(value
)-2));
32 SigSpec::parse(sig
, nullptr, value
);
33 return sig
.as_const();
36 bool string_compare_nocase(const string
&str1
, const string
&str2
)
38 if (str1
.size() != str2
.size())
41 for (size_t i
= 0; i
< str1
.size(); i
++)
43 char ch1
= str1
[i
], ch2
= str2
[i
];
44 if ('a' <= ch1
&& ch1
<= 'z')
46 if ('a' <= ch2
&& ch2
<= 'z')
55 bool match_name(string
&name
, IdString
&id
, bool ignore_case
=false)
57 string str1
= RTLIL::escape_id(name
);
58 string str2
= id
.str();
61 return string_compare_nocase(str1
, str2
);
66 bool match_value(string
&value
, Const
&val
, bool ignore_case
=false)
68 if (ignore_case
&& ((val
.flags
& RTLIL::CONST_FLAG_STRING
) != 0) && GetSize(value
) && value
.front() == '"' && value
.back() == '"') {
69 string str1
= value
.substr(1, GetSize(value
)-2);
70 string str2
= val
.decode_string();
71 return string_compare_nocase(str1
, str2
);
74 return make_value(value
) == val
;
77 struct AttrmapAction
{
78 virtual ~AttrmapAction() { }
79 virtual bool apply(IdString
&id
, Const
&val
) = 0;
82 struct AttrmapTocase
: AttrmapAction
{
84 bool apply(IdString
&id
, Const
&) YS_OVERRIDE
{
85 if (match_name(name
, id
, true))
86 id
= RTLIL::escape_id(name
);
91 struct AttrmapRename
: AttrmapAction
{
92 string old_name
, new_name
;
93 bool apply(IdString
&id
, Const
&) YS_OVERRIDE
{
94 if (match_name(old_name
, id
))
95 id
= RTLIL::escape_id(new_name
);
100 struct AttrmapMap
: AttrmapAction
{
102 string old_name
, new_name
;
103 string old_value
, new_value
;
104 bool apply(IdString
&id
, Const
&val
) YS_OVERRIDE
{
105 if (match_name(old_name
, id
) && match_value(old_value
, val
, true)) {
106 id
= RTLIL::escape_id(new_name
);
107 val
= make_value(new_value
);
113 struct AttrmapRemove
: AttrmapAction
{
116 bool apply(IdString
&id
, Const
&val
) YS_OVERRIDE
{
117 return !(match_name(name
, id
) && (!has_value
|| match_value(value
, val
)));
121 void attrmap_apply(string objname
, vector
<std::unique_ptr
<AttrmapAction
>> &actions
, dict
<RTLIL::IdString
, RTLIL::Const
> &attributes
)
123 dict
<RTLIL::IdString
, RTLIL::Const
> new_attributes
;
125 for (auto attr
: attributes
)
127 auto new_attr
= attr
;
128 for (auto &action
: actions
)
129 if (!action
->apply(new_attr
.first
, new_attr
.second
))
130 goto delete_this_attr
;
132 if (new_attr
!= attr
)
133 log("Changed attribute on %s: %s=%s -> %s=%s\n", objname
.c_str(),
134 log_id(attr
.first
), log_const(attr
.second
), log_id(new_attr
.first
), log_const(new_attr
.second
));
136 new_attributes
[new_attr
.first
] = new_attr
.second
;
140 log("Removed attribute on %s: %s=%s\n", objname
.c_str(), log_id(attr
.first
), log_const(attr
.second
));
143 attributes
.swap(new_attributes
);
146 struct AttrmapPass
: public Pass
{
147 AttrmapPass() : Pass("attrmap", "renaming attributes") { }
148 void help() YS_OVERRIDE
150 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
152 log(" attrmap [options] [selection]\n");
154 log("This command renames attributes and/or mapps key/value pairs to\n");
155 log("other key/value pairs.\n");
157 log(" -tocase <name>\n");
158 log(" Match attribute names case-insensitively and set it to the specified\n");
161 log(" -rename <old_name> <new_name>\n");
162 log(" Rename attributes as specified\n");
164 log(" -map <old_name>=<old_value> <new_name>=<new_value>\n");
165 log(" Map key/value pairs as indicated.\n");
167 log(" -imap <old_name>=<old_value> <new_name>=<new_value>\n");
168 log(" Like -map, but use case-insensitive match for <old_value> when\n");
169 log(" it is a string value.\n");
171 log(" -remove <name>=<value>\n");
172 log(" Remove attributes matching this pattern.\n");
175 log(" Operate on module attributes instead of attributes on wires and cells.\n");
177 log("For example, mapping Xilinx-style \"keep\" attributes to Yosys-style:\n");
179 log(" attrmap -tocase keep -imap keep=\"true\" keep=1 \\\n");
180 log(" -imap keep=\"false\" keep=0 -remove keep=0\n");
183 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
185 log_header(design
, "Executing ATTRMAP pass (move or copy attributes).\n");
187 bool modattr_mode
= false;
188 vector
<std::unique_ptr
<AttrmapAction
>> actions
;
191 for (argidx
= 1; argidx
< args
.size(); argidx
++)
193 std::string arg
= args
[argidx
];
194 if (arg
== "-tocase" && argidx
+1 < args
.size()) {
195 auto action
= new AttrmapTocase
;
196 action
->name
= args
[++argidx
];
197 actions
.push_back(std::unique_ptr
<AttrmapAction
>(action
));
200 if (arg
== "-rename" && argidx
+2 < args
.size()) {
201 auto action
= new AttrmapRename
;
202 action
->old_name
= args
[++argidx
];
203 action
->new_name
= args
[++argidx
];
204 actions
.push_back(std::unique_ptr
<AttrmapAction
>(action
));
207 if ((arg
== "-map" || arg
== "-imap") && argidx
+2 < args
.size()) {
208 string arg1
= args
[++argidx
];
209 string arg2
= args
[++argidx
];
211 size_t p
= arg1
.find("=");
212 if (p
!= string::npos
) {
213 val1
= arg1
.substr(p
+1);
214 arg1
= arg1
.substr(0, p
);
217 if (p
!= string::npos
) {
218 val2
= arg2
.substr(p
+1);
219 arg2
= arg2
.substr(0, p
);
221 auto action
= new AttrmapMap
;
222 action
->imap
= (arg
== "-map");
223 action
->old_name
= arg1
;
224 action
->new_name
= arg2
;
225 action
->old_value
= val1
;
226 action
->new_value
= val2
;
227 actions
.push_back(std::unique_ptr
<AttrmapAction
>(action
));
230 if (arg
== "-remove" && argidx
+1 < args
.size()) {
231 string arg1
= args
[++argidx
], val1
;
232 size_t p
= arg1
.find("=");
233 if (p
!= string::npos
) {
234 val1
= arg1
.substr(p
+1);
235 arg1
= arg1
.substr(0, p
);
237 auto action
= new AttrmapRemove
;
239 action
->has_value
= (p
!= string::npos
);
240 action
->value
= val1
;
241 actions
.push_back(std::unique_ptr
<AttrmapAction
>(action
));
244 if (arg
== "-modattr") {
250 extra_args(args
, argidx
, design
);
254 for (auto module
: design
->selected_whole_modules())
255 attrmap_apply(stringf("%s", log_id(module
)), actions
, module
->attributes
);
259 for (auto module
: design
->selected_modules())
261 for (auto wire
: module
->selected_wires())
262 attrmap_apply(stringf("%s.%s", log_id(module
), log_id(wire
)), actions
, wire
->attributes
);
264 for (auto cell
: module
->selected_cells())
265 attrmap_apply(stringf("%s.%s", log_id(module
), log_id(cell
)), actions
, cell
->attributes
);
271 PRIVATE_NAMESPACE_END