aboutsummaryrefslogtreecommitdiffstats
path: root/passes/techmap/attrmap.cc
blob: dec81d2164f82674d0acd919ca044a6a86106c6a (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
269
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at>
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "kernel/yosys.h"
#include "kernel/sigtools.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

Const make_value(string &value)
{
	if (GetSize(value) >= 2 && value.front() == '"' && value.back() == '"')
		return Const(value.substr(1, GetSize(value)-2));

	SigSpec sig;
	SigSpec::parse(sig, nullptr, value);
	return sig.as_const();
}

bool string_compare_nocase(const string &str1, const string &str2)
{
	if (str1.size() != str2.size())
		return false;

	for (size_t i = 0; i < str1.size(); i++)
	{
		char ch1 = str1[i], ch2 = str2[i];
		if ('a' <= ch1 && ch1 <= 'z')
			ch1 -= 'a' - 'A';
		if ('a' <= ch2 && ch2 <= 'z')
			ch2 -= 'a' - 'A';
		if (ch1 != ch2)
			return false;
	}

	return true;
}

bool match_name(string &name, IdString &id, bool ignore_case=false)
{
	string str1 = RTLIL::escape_id(name);
	string str2 = id.str();

	if (ignore_case)
		return string_compare_nocase(str1, str2);

	return str1 == str2;
}

bool match_value(string &value, Const &val, bool ignore_case=false)
{
	if (ignore_case && ((val.flags & RTLIL::CONST_FLAG_STRING) != 0) && GetSize(value) && value.front() == '"' && value.back() == '"') {
		string str1 = value.substr(1, GetSize(value)-2);
		string str2 = val.decode_string();
		return string_compare_nocase(str1, str2);
	}

	return make_value(value) == val;
}

struct AttrmapAction {
	virtual ~AttrmapAction() { }
	virtual bool apply(IdString &id, Const &val) = 0;
};

struct AttrmapTocase : AttrmapAction {
	string name;
	virtual bool apply(IdString &id, Const&) {
		if (match_name(name, id, true))
			id = RTLIL::escape_id(name);
		return true;
	}
};

struct AttrmapRename : AttrmapAction {
	string old_name, new_name;
	virtual bool apply(IdString &id, Const&) {
		if (match_name(old_name, id))
			id = RTLIL::escape_id(new_name);
		return true;
	}
};

struct AttrmapMap : AttrmapAction {
	bool imap;
	string old_name, new_name;
	string old_value, new_value;
	virtual bool apply(IdString &id, Const &val) {
		if (match_name(old_name, id) && match_value(old_value, val, true)) {
			id = RTLIL::escape_id(new_name);
			val = make_value(new_value);
		}
		return true;
	}
};

struct AttrmapRemove : AttrmapAction {
	string name, value;
	virtual bool apply(IdString &id, Const &val) {
		return !(match_name(name, id) && match_value(value, val));
	}
};

void attrmap_apply(string objname, vector<std::unique_ptr<AttrmapAction>> &actions, dict<RTLIL::IdString, RTLIL::Const> &attributes)
{
	dict<RTLIL::IdString, RTLIL::Const> new_attributes;

	for (auto attr : attributes)
	{
		auto new_attr = attr;
		for (auto &action : actions)
			if (!action->apply(new_attr.first, new_attr.second))
				goto delete_this_attr;

		if (new_attr != attr)
			log("Changed attribute on %s: %s=%s -> %s=%s\n", objname.c_str(),
					log_id(attr.first), log_const(attr.second), log_id(new_attr.first), log_const(new_attr.second));

		new_attributes[new_attr.first] = new_attr.second;

		if (0)
	delete_this_attr:
			log("Removed attribute on %s: %s=%s\n", objname.c_str(), log_id(attr.first), log_const(attr.second));
	}

	attributes.swap(new_attributes);
}

struct AttrmapPass : public Pass {
	AttrmapPass() : Pass("attrmap", "renaming attributes") { }
	virtual void help()
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    attrmap [options] [selection]\n");
		log("\n");
		log("This command renames attributes and/or mapps key/value pairs to\n");
		log("other key/value pairs.\n");
		log("\n");
		log("    -tocase <name>\n");
		log("        Match attribute names case-insensitively and set it to the specified\n");
		log("        name.\n");
		log("\n");
		log("    -rename <old_name> <new_name>\n");
		log("        Rename attributes as specified\n");
		log("\n");
		log("    -map <old_name>=<old_value> <new_name>=<new_value>\n");
		log("        Map key/value pairs as indicated.\n");
		log("\n");
		log("    -imap <old_name>=<old_value> <new_name>=<new_value>\n");
		log("        Like -map, but use case-insensitive match for <old_value> when\n");
		log("        it is a string value.\n");
		log("\n");
		log("    -remove <name>=<value>\n");
		log("        Remove attributes matching this pattern.\n");
		log("\n");
		log("    -modattr\n");
		log("        Operate on module attributes instead of attributes on wires and cells.\n");
		log("\n");
		log("For example, mapping Xilinx-style \"keep\" attributes to Yosys-style:\n");
		log("\n");
		log("    attrmap -tocase keep -imap keep=\"true\" keep=1 \\\n");
		log("            -imap keep=\"false\" keep=0 -remove keep=0\n");
		log("\n");
	}
	virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
	{
		log_header(design, "Executing ATTRMAP pass (move or copy attributes).\n");

		bool modattr_mode = false;
		vector<std::unique_ptr<AttrmapAction>> actions;

		size_t argidx;
		for (argidx = 1; argidx < args.size(); argidx++)
		{
			std::string arg = args[argidx];
			if (arg == "-tocase" && argidx+1 < args.size()) {
				auto action = new AttrmapTocase;
				action->name = args[++argidx];
				actions.push_back(std::unique_ptr<AttrmapAction>(action));
				continue;
			}
			if (arg == "-rename" && argidx+2 < args.size()) {
				auto action = new AttrmapRename;
				action->old_name = args[++argidx];
				action->new_name = args[++argidx];
				actions.push_back(std::unique_ptr<AttrmapAction>(action));
				continue;
			}
			if ((arg == "-map" || arg == "-imap") && argidx+2 < args.size()) {
				string arg1 = args[++argidx];
				string arg2 = args[++argidx];
				string val1, val2;
				size_t p = arg1.find("=");
				if (p != string::npos) {
					val1 = arg1.substr(p+1);
					arg1 = arg1.substr(0, p);
				}
				p = arg2.find("=");
				if (p != string::npos) {
					val2 = arg2.substr(p+1);
					arg2 = arg2.substr(0, p);
				}
				auto action = new AttrmapMap;
				action->imap = (arg == "-map");
				action->old_name = arg1;
				action->new_name = arg2;
				action->old_value = val1;
				action->new_value = val2;
				actions.push_back(std::unique_ptr<AttrmapAction>(action));
				continue;
			}
			if (arg == "-remove" && argidx+1 < args.size()) {
				string arg1 = args[++argidx], val1;
				size_t p = arg1.find("=");
				if (p != string::npos) {
					val1 = arg1.substr(p+1);
					arg1 = arg1.substr(0, p);
				}
				auto action = new AttrmapRemove;
				action->name = arg1;
				action->value = val1;
				actions.push_back(std::unique_ptr<AttrmapAction>(action));
				continue;
			}
			if (arg == "-modattr") {
				modattr_mode = true;
				continue;
			}
			break;
		}
		extra_args(args, argidx, design);

		if (modattr_mode)
		{
			for (auto module : design->selected_whole_modules())
				attrmap_apply(stringf("%s", log_id(module)), actions, module->attributes);
		}
		else
		{
			for (auto module : design->selected_modules())
			{
				for (auto wire : module->selected_wires())
					attrmap_apply(stringf("%s.%s", log_id(module), log_id(wire)), actions, wire->attributes);

				for (auto cell : module->selected_cells())
					attrmap_apply(stringf("%s.%s", log_id(module), log_id(cell)), actions, cell->attributes);
			}
		}
	}
} AttrmapPass;

PRIVATE_NAMESPACE_END