aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/utils.h
blob: 8942905fe3584af5ff0fa39c6d2e3a63c3eac035 (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
/*
 *  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.
 *
 */

// This file contains various c++ utility routines and helper classes that
// do not depend on any other components of yosys (except stuff like log_*).

#include "kernel/yosys.h"

#ifndef UTILS_H
#define UTILS_H

YOSYS_NAMESPACE_BEGIN

// ------------------------------------------------
// A map-like container, but you can save and restore the state
// ------------------------------------------------

template<typename Key, typename T, typename OPS = hash_ops<Key>>
struct stackmap
{
private:
	std::vector<dict<Key, T*, OPS>> backup_state;
	dict<Key, T, OPS> current_state;
	static T empty_tuple;

public:
	stackmap() { }
	stackmap(const dict<Key, T, OPS> &other) : current_state(other) { }

	template<typename Other>
	void operator=(const Other &other)
	{
		for (auto &it : current_state)
			if (!backup_state.empty() && backup_state.back().count(it.first) == 0)
				backup_state.back()[it.first] = new T(it.second);
		current_state.clear();

		for (auto &it : other)
			set(it.first, it.second);
	}

	bool has(const Key &k)
	{
		return current_state.count(k) != 0;
	}

	void set(const Key &k, const T &v)
	{
		if (!backup_state.empty() && backup_state.back().count(k) == 0)
			backup_state.back()[k] = current_state.count(k) ? new T(current_state.at(k)) : nullptr;
		current_state[k] = v;
	}

	void unset(const Key &k)
	{
		if (!backup_state.empty() && backup_state.back().count(k) == 0)
			backup_state.back()[k] = current_state.count(k) ? new T(current_state.at(k)) : nullptr;
		current_state.erase(k);
	}

	const T &get(const Key &k)
	{
		if (current_state.count(k) == 0)
			return empty_tuple;
		return current_state.at(k);
	}

	void reset(const Key &k)
	{
		for (int i = GetSize(backup_state)-1; i >= 0; i--)
			if (backup_state[i].count(k) != 0) {
				if (backup_state[i].at(k) == nullptr)
					current_state.erase(k);
				else
					current_state[k] = *backup_state[i].at(k);
				return;
			}
		current_state.erase(k);
	}

	const dict<Key, T, OPS> &stdmap()
	{
		return current_state;
	}

	void save()
	{
		backup_state.resize(backup_state.size()+1);
	}

	void restore()
	{
		log_assert(!backup_state.empty());
		for (auto &it : backup_state.back())
			if (it.second != nullptr) {
				current_state[it.first] = *it.second;
				delete it.second;
			} else
				current_state.erase(it.first);
		backup_state.pop_back();
	}

	~stackmap()
	{
		while (!backup_state.empty())
			restore();
	}
};


// ------------------------------------------------
// A simple class for topological sorting
// ------------------------------------------------

template<typename T, typename C = std::less<T>>
struct TopoSort
{
	bool analyze_loops, found_loops;
	std::map<T, std::set<T, C>, C> database;
	std::set<std::set<T, C>> loops;
	std::vector<T> sorted;

	TopoSort()
	{
		analyze_loops = true;
		found_loops = false;
	}

	void node(T n)
	{
		if (database.count(n) == 0)
			database[n] = std::set<T, C>();
	}

	void edge(T left, T right)
	{
		node(left);
		database[right].insert(left);
	}

	void sort_worker(const T &n, std::set<T, C> &marked_cells, std::set<T, C> &active_cells, std::vector<T> &active_stack)
	{
		if (active_cells.count(n)) {
			found_loops = true;
			if (analyze_loops) {
				std::set<T, C> loop;
				for (int i = GetSize(active_stack)-1; i >= 0; i--) {
					loop.insert(active_stack[i]);
					if (active_stack[i] == n)
						break;
				}
				loops.insert(loop);
			}
			return;
		}

		if (marked_cells.count(n))
			return;

		if (!database.at(n).empty())
		{
			if (analyze_loops)
				active_stack.push_back(n);
			active_cells.insert(n);

			for (auto &left_n : database.at(n))
				sort_worker(left_n, marked_cells, active_cells, active_stack);

			if (analyze_loops)
				active_stack.pop_back();
			active_cells.erase(n);
		}

		marked_cells.insert(n);
		sorted.push_back(n);
	}

	bool sort()
	{
		loops.clear();
		sorted.clear();
		found_loops = false;

		std::set<T, C> marked_cells;
		std::set<T, C> active_cells;
		std::vector<T> active_stack;

		for (auto &it : database)
			sort_worker(it.first, marked_cells, active_cells, active_stack);

		log_assert(GetSize(sorted) == GetSize(database));
		return !found_loops;
	}
};

YOSYS_NAMESPACE_END

#endif
assert -1 == string.find(s[0:-2], "\n"), s if (s[-1:]=="\n"): out_row = out_row+1 out_col = 0 else: out_col = spos[1]+len(s) ###################################################################### # Records a name and parameters. The name is either a class name or # a function name. Then the parameter is either the base class or # the function parameters. # The name is stored in the global variable "name", the parameters # in "param". # The variable "record_state" holds the current state of this internal # state machine. # The recording is started by calling start_recording(). # # In: type, tok ###################################################################### def rec_name_n_param(type, tok): global record_state,name,param,doc_string,bracket_counter s = record_state # State 0: Do nothing. if (s==0): return # State 1: Remember name. elif (s==1): name = tok record_state = 2 # State 2: Wait for opening bracket or colon elif (s==2): if (tok=='('): bracket_counter = 1 record_state=3 if (tok==':'): record_state=4 # State 3: Store parameter (or base class) and wait for an ending bracket elif (s==3): if (tok=='*' or tok=='**'): tok='' if (tok=='('): bracket_counter = bracket_counter+1 if (tok==')'): bracket_counter = bracket_counter-1 if bracket_counter==0: record_state=4 else: param=param+tok # State 4: Look for doc string elif (s==4): if (type==token.NEWLINE or type==token.INDENT or type==token.SLASHEQUAL): return elif (tok==":"): return elif (type==token.STRING): while tok[:1]=='r' or tok[:1]=='u': tok=tok[1:] while tok[:1]=='"': tok=tok[1:] while tok[-1:]=='"': tok=tok[:-1] doc_string=tok record_state=0 ###################################################################### # Starts the recording of a name & param part. # The function rec_name_n_param() has to be fed with tokens. After # the necessary tokens are fed the name and parameters can be found # in the global variables "name" und "param". ###################################################################### def start_recording(): global record_state,param,name, doc_string record_state=1 name="" param="" doc_string="" ###################################################################### # Test if recording is finished ###################################################################### def is_recording_finished(): global record_state return record_state==0 ###################################################################### ## Gather comment block ###################################################################### def gather_comment(type,tok,spos): global comment_block,comment_finished if (type!=tokenize.COMMENT): comment_finished = 1 else: # Output old comment block if a new one is started. if (comment_finished): print_comment(spos) comment_finished=0 if (tok[0:2]=="##" and tok[0:3]!="###"): append_comment_lines(tok[2:]) ###################################################################### ## Output comment block and empty buffer. ###################################################################### def print_comment(spos): global comment_block,comment_finished if (comment_block!=[]): output("/** ",spos) for c in comment_block: output(c,spos) output("*/\n",spos) comment_block = [] comment_finished = 0 ###################################################################### def set_state(s): global stateStack stateStack[len(stateStack)-1]=s ###################################################################### def get_state(): global stateStack return stateStack[len(stateStack)-1] ###################################################################### def push_state(s): global stateStack stateStack.append(s) ###################################################################### def pop_state(): global stateStack stateStack.pop() ###################################################################### def tok_eater(type, tok, spos, epos, line): global stateStack,name,param,class_spos,def_spos,import_spos global doc_string, modules, import_token, module_has_docstring global protection_level, private_member global out_row while out_row + 1 < spos[0]: output("\n", (0, 0)) rec_name_n_param(type,tok) if (string.replace(string.strip(tok)," ","")=="##private:"): protection_level = "private" output("private:\n",spos) elif (string.replace(string.strip(tok)," ","")=="##protected:"): protection_level = "protected" output("protected:\n",spos) elif (string.replace(string.strip(tok)," ","")=="##public:"): protection_level = "public" output("public:\n",spos) else: gather_comment(type,tok,spos) state = get_state() # sys.stderr.write("%d: %s\n"%(state, tok)) # OUTSIDE if (state==OUTSIDE): if (tok=="class"): start_recording() class_spos = spos push_state(BUILD_CLASS_DECL) elif (tok=="def"): start_recording() def_spos = spos push_state(BUILD_DEF_DECL) elif (tok=="import") or (tok=="from"): import_token = tok import_spos = spos modules = [] push_state(IMPORT) elif (spos[1] == 0 and tok[:3] == '"""'): # Capture module docstring as namespace documentation module_has_docstring = True append_comment_lines("\\namespace %s\n" % namespace) append_comment_lines(tok[3:-3]) print_comment(spos) # IMPORT elif (state==IMPORT): if (type==token.NAME): modules.append(tok) set_state(IMPORT_OP) # IMPORT_OP elif (state==IMPORT_OP): if (tok=="."): set_state(IMPORT_APPEND) elif (tok==","): set_state(IMPORT) else: for m in modules: output('#include "'+m.replace('.',os.path.sep)+'.py"\n', import_spos, immediate=1) if import_token=="from": output('using namespace '+m.replace('.', '::')+';\n', import_spos) pop_state() # IMPORT_APPEND elif (state==IMPORT_APPEND): if (type==token.NAME): modules[len(modules)-1]+="."+tok set_state(IMPORT_OP) # BUILD_CLASS_DECL elif (state==BUILD_CLASS_DECL): if (is_recording_finished()): s = "class "+name if (param!=""): s = s+" : public "+param.replace('.','::') if (doc_string!=""): append_comment_lines(doc_string) print_comment(class_spos) output(s+"\n",class_spos) output("{\n",(class_spos[0]+1,class_spos[1])) protection_level = "public" output(" public:\n",(class_spos[0]+2,class_spos[1])) set_state(BUILD_CLASS_BODY) # BUILD_CLASS_BODY elif (state==BUILD_CLASS_BODY): if (type!=token.INDENT and type!=token.NEWLINE and type!=40 and type!=tokenize.NL and type!=tokenize.COMMENT and (spos[1]<=class_spos[1])): output("}; // end of class\n",(out_row+1,class_spos[1])) pop_state() elif (tok=="def"): start_recording() def_spos = spos push_state(BUILD_DEF_DECL) # BUILD_DEF_DECL elif (state==BUILD_DEF_DECL): if (is_recording_finished()): param = param.replace("\n", " ") param = param.replace("=", " = ") params = param.split(",") if BUILD_CLASS_BODY in stateStack: if len(name) > 1 \ and name[0:2] == '__' \ and name[len(name)-2:len(name)] != '__' \ and protection_level != 'private': private_member = True output(" private:\n",(def_spos[0]+2,def_spos[1])) if (doc_string != ""): append_comment_lines(doc_string) print_comment(def_spos) output_function_decl(name, params) # output("{\n",(def_spos[0]+1,def_spos[1])) set_state(BUILD_DEF_BODY) # BUILD_DEF_BODY elif (state==BUILD_DEF_BODY): if (type!=token.INDENT and type!=token.NEWLINE \ and type!=40 and type!=tokenize.NL \ and (spos[1]<=def_spos[1])): # output("} // end of method/function\n",(out_row+1,def_spos[1])) if private_member and protection_level != 'private': private_member = False output(" " + protection_level + ":\n",(def_spos[0]+2,def_spos[1])) pop_state() # else: # output(tok,spos) def output_function_decl(name, params): global def_spos # Do we document a class method? then remove the 'self' parameter if params[0] == 'self': preamble = '' params = params[1:] else: preamble = 'static ' if params[0] == 'cls': params = params[1:] param_string = string.join(params, ", Type ") if param_string == '': param_string = '(' + param_string + ');\n' else: param_string = '(Type ' + param_string + ');\n' output(preamble, def_spos) output(name, def_spos) output(param_string, def_spos) def append_comment_lines(lines): map(append_comment_line, doc_string.split('\n')) paramRE = re.compile(r'(@param \w+):') def append_comment_line(line): global paramRE comment_block.append(paramRE.sub(r'\1', line) + '\n') def dump(filename): f = open(filename) r = f.readlines() for s in r: sys.stdout.write(s) def filter(filename): global name, module_has_docstring, source_root path,name = os.path.split(filename) root,ext = os.path.splitext(name) if source_root and path.find(source_root) == 0: path = path[len(source_root):] if path[0] == os.sep: path = path[1:] ns = path.split(os.sep) else: ns = [] ns.append(root) for n in ns: output("namespace " + n + " {\n",(0,0)) # set module name for tok_eater to use if there's a module doc string name = root # sys.stderr.write('Filtering "'+filename+'"...') f = open(filename) tokenize.tokenize(f.readline, tok_eater) f.close() print_comment((0,0)) output("\n",(0,0)) for n in ns: output("} // end of namespace\n",(0,0)) if not module_has_docstring: # Put in default namespace documentation output('/** \\namespace '+root+' \n',(0,0)) output(' \\brief Module "%s" */\n'%(root),(0,0)) for s in outbuffer: outfile.write(s) def filterFile(filename, out=sys.stdout): global outfile outfile = out try: root,ext = os.path.splitext(filename) if ext==".py": filter(filename) else: dump(filename) # sys.stderr.write("OK\n") except IOError,e: sys.stderr.write(e[1]+"\n") ###################################################################### # preparePath def preparePath(path): """Prepare a path. Checks if the path exists and creates it if it does not exist. """ if not os.path.exists(path): parent = os.path.dirname(path) if parent!="": preparePath(parent) os.mkdir(path) # isNewer def isNewer(file1,file2): """Check if file1 is newer than file2. file1 must be an existing file. """ if not os.path.exists(file2): return True return os.stat(file1)[ST_MTIME]>os.stat(file2)[ST_MTIME] # convert def convert(srcpath, destpath): """Convert a Python source tree into a C+ stub tree. All *.py files in srcpath (including sub-directories) are filtered and written to destpath. If destpath exists, only the files that have been modified are filtered again. Files that were deleted from srcpath are also deleted in destpath if they are still present. The function returns the number of processed *.py files. """ count=0 sp = os.path.join(srcpath,"*") sfiles = glob.glob(sp) dp = os.path.join(destpath,"*") dfiles = glob.glob(dp) leftovers={} for df in dfiles: leftovers[os.path.basename(df)]=1 for srcfile in sfiles: basename = os.path.basename(srcfile) if basename in leftovers: del leftovers[basename] # Is it a subdirectory? if os.path.isdir(srcfile): sdir = os.path.join(srcpath,basename) ddir = os.path.join(destpath,basename) count+=convert(sdir, ddir) continue # Check the extension (only *.py will be converted) root, ext = os.path.splitext(srcfile) if ext.lower()!=".py": continue destfile = os.path.join(destpath,basename) if destfile==srcfile: print "WARNING: Input and output names are identical!" sys.exit(1) count+=1 # sys.stdout.write("%s\015"%(srcfile)) if isNewer(srcfile, destfile): preparePath(os.path.dirname(destfile)) # out=open(destfile,"w") # filterFile(srcfile, out) # out.close() os.system("python %s -f %s>%s"%(sys.argv[0],srcfile,destfile)) # Delete obsolete files in destpath for df in leftovers: dname=os.path.join(destpath,df) if os.path.isdir(dname): try: shutil.rmtree(dname) except: print "Can't remove obsolete directory '%s'"%dname else: try: os.remove(dname) except: print "Can't remove obsolete file '%s'"%dname return count ###################################################################### ###################################################################### ###################################################################### filter_file = False source_root = None try: opts, args = getopt.getopt(sys.argv[1:], "hfr:", ["help"]) except getopt.GetoptError,e: print e sys.exit(1) for o,a in opts: if o=="-f": filter_file = True if o=="-r": source_root = os.path.abspath(a) if filter_file: # Filter the specified file and print the result to stdout filename = string.join(args) filterFile(os.path.abspath(filename)) else: if len(args)!=2: sys.stderr.write("%s options input output\n"%(os.path.basename(sys.argv[0]))) sys.exit(1) # Filter an entire Python source tree print '"%s" -> "%s"\n'%(args[0],args[1]) c=convert(args[0],args[1]) print "%d files"%(c)