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.
18 # Author Benedikt Tutzer
23 #Map c++ operator Syntax to Python functions
24 wrappable_operators
= {
35 #Restrict certain strings from being function names in Python
46 "continue" : "continue_",
60 "nonlocal" : "nonlocal_",
72 #These can be used without any explicit conversion
73 primitive_types
= ["void", "bool", "int", "double", "size_t", "std::string",
74 "string", "State", "char_p"]
78 #Ways to link between Python- and C++ Objects
79 class link_types(Enum
):
80 global_list
= 1 #Manage a global list of objects in C++, the Python
81 #object contains a key to find the corresponding C++
82 #object and a Pointer to the object to verify it is
83 #still the same, making collisions unlikely to happen
84 ref_copy
= 2 #The Python object contains a copy of the C++ object.
85 #The C++ object is deleted when the Python object gets
87 pointer
= 3 #The Python Object contains a pointer to it's C++
89 derive
= 4 #The Python-Wrapper is derived from the C++ object.
91 class attr_types(Enum
):
102 def __init__(self
, name
, classes
):
104 self
.classes
= classes
106 #Splits a list by the given delimiter, without splitting strings inside
107 #pointy-brackets (< and >)
108 def split_list(str_def
, delim
):
109 str_def
= str_def
.strip()
110 if len(str_def
) == 0:
112 if str_def
.count(delim
) == 0:
114 if str_def
.count("<") == 0:
115 return str_def
.split(delim
)
116 if str_def
.find("<") < str_def
.find(" "):
117 closing
= find_closing(str_def
[str_def
.find("<")+1:], "<", ">") + str_def
.find("<")
118 comma
= str_def
[closing
:].find(delim
)
121 comma
= closing
+ comma
123 comma
= str_def
.find(delim
)
124 rest
= split_list(str_def
[comma
+1:], delim
)
125 ret
= [str_def
[:comma
]]
126 if rest
!= None and len(rest
) != 0:
134 attr_type
= attr_types
.default
136 def __init__(self
, name
= "", cont
= None, attr_type
= attr_types
.default
):
139 self
.attr_type
= attr_type
144 if self
.name
in enum_names
:
145 text
= enum_by_name(self
.name
).namespace
+ "::" + self
.name
146 if self
.cont
!= None:
147 return known_containers
[self
.name
].typename
151 def gen_text_cpp(self
):
153 if self
.attr_type
== attr_types
.star
:
155 if self
.name
in primitive_types
:
156 return self
.name
+ postfix
157 if self
.name
in enum_names
:
158 return enum_by_name(self
.name
).namespace
+ "::" + self
.name
+ postfix
159 if self
.name
in classnames
:
160 return class_by_name(self
.name
).namespace
+ "::" + self
.name
+ postfix
162 if self
.cont
!= None:
164 for a
in self
.cont
.args
:
165 text
+= a
.gen_text_cpp() + ", "
171 def from_string(str_def
, containing_file
, line_number
):
172 str_def
= str_def
.strip()
173 if len(str_def
) == 0:
175 str_def
= str_def
.replace("RTLIL::SigSig", "std::pair<SigSpec, SigSpec>").replace("SigSig", "std::pair<SigSpec, SigSpec>")
179 t
.attr_type
= attr_types
.default
180 if str_def
.find("<") != -1:# and str_def.find("<") < str_def.find(" "):
181 candidate
= WContainer
.from_string(str_def
, containing_file
, line_number
)
182 if candidate
== None:
184 t
.name
= str_def
[:str_def
.find("<")]
186 if t
.name
.count("*") + t
.name
.count("&") > 1:
189 if t
.name
.count("*") == 1 or str_def
[0] == '*' or str_def
[-1] == '*':
190 t
.attr_type
= attr_types
.star
191 t
.name
= t
.name
.replace("*","")
192 elif t
.name
.count("&&") == 1:
193 t
.attr_type
= attr_types
.ampamp
194 t
.name
= t
.name
.replace("&&","")
195 elif t
.name
.count("&") == 1 or str_def
[0] == '&' or str_def
[-1] == '&':
196 t
.attr_type
= attr_types
.amp
197 t
.name
= t
.name
.replace("&","")
200 if(t
.name
not in known_containers
):
206 if str.startswith(str_def
, "unsigned "):
208 str_def
= str_def
[9:]
209 while str.startswith(str_def
, "long "):
210 prefix
= "long " + prefix
211 str_def
= str_def
[5:]
212 while str.startswith(str_def
, "short "):
213 prefix
= "short " + prefix
214 str_def
= str_def
[6:]
216 str_def
= str_def
.split("::")[-1]
218 if str_def
.count("*") + str_def
.count("&") >= 2:
221 if str_def
.count("*") == 1:
222 t
.attr_type
= attr_types
.star
223 str_def
= str_def
.replace("*","")
224 elif str_def
.count("&&") == 1:
225 t
.attr_type
= attr_types
.ampamp
226 str_def
= str_def
.replace("&&","")
227 elif str_def
.count("&") == 1:
228 t
.attr_type
= attr_types
.amp
229 str_def
= str_def
.replace("&","")
231 if len(str_def
) > 0 and str_def
.split("::")[-1] not in primitive_types
and str_def
.split("::")[-1] not in classnames
and str_def
.split("::")[-1] not in enum_names
:
234 if str_def
.count(" ") == 0:
235 t
.name
= (prefix
+ str_def
).replace("char_p", "char *")
240 #Represents a container-type
245 def from_string(str_def
, containing_file
, line_number
):
246 if str_def
== None or len(str_def
) < 4:
249 cont
.name
= str_def
[:str_def
.find("<")]
250 str_def
= str_def
[str_def
.find("<")+1:find_closing(str_def
, "<", ">")]
252 for arg
in split_list(str_def
, ","):
253 candidate
= WType
.from_string(arg
.strip(), containing_file
, line_number
)
254 if candidate
== None:
256 if candidate
.name
== "void":
258 cont
.args
.append(candidate
)
261 #Translators between Python and C++ containers
265 typename
= "DefaultType"
266 orig_name
= "DefaultCpp"
269 def gen_type(c
, types
):
270 return "\nImplement a function that outputs the c++ type of this container here\n"
273 def translate(c
, varname
, types
, prefix
):
274 return "\nImplement a function translating a python container to a c++ container here\n"
277 def translate_cpp(c
, varname
, types
, prefix
, ref
):
278 return "\nImplement a function translating a c++ container to a python container here\n"
280 #Translates list-types (vector, pool, set), that only differ in their name and
281 #the name of the insertion function
282 class PythonListTranslator(Translator
):
283 typename
= "boost::python::list"
284 insert_name
= "Default"
286 #generate the c++ type string
288 def gen_type(c
, types
):
289 text
= c
.orig_name
+ "<"
290 if types
[0].name
in primitive_types
:
291 text
+= types
[0].name
292 elif types
[0].name
in known_containers
:
293 text
+= known_containers
[types
[0].name
].gen_type(types
[0].cont
.args
)
295 text
+= class_by_name(types
[0].name
).namespace
+ "::" + types
[0].name
296 if types
[0].attr_type
== attr_types
.star
:
301 #Generate C++ code to translate from a boost::python::list
303 def translate(c
, varname
, types
, prefix
):
304 text
= prefix
+ c
.gen_type(types
) + " " + varname
+ "___tmp;"
305 cntr_name
= "cntr_" + str(Translator
.tmp_cntr
)
306 Translator
.tmp_cntr
= Translator
.tmp_cntr
+ 1
307 text
+= prefix
+ "for(int " + cntr_name
+ " = 0; " + cntr_name
+ " < len(" + varname
+ "); " + cntr_name
+ "++)"
309 tmp_name
= "tmp_" + str(Translator
.tmp_cntr
)
310 Translator
.tmp_cntr
= Translator
.tmp_cntr
+ 1
311 if types
[0].name
in known_containers
:
312 text
+= prefix
+ "\t" + known_containers
[types
[0].name
].typename
+ " " + tmp_name
+ " = boost::python::extract<" + known_containers
[types
[0].name
].typename
+ ">(" + varname
+ "[" + cntr_name
+ "]);"
313 text
+= known_containers
[types
[0].name
].translate(tmp_name
, types
[0].cont
.args
, prefix
+"\t")
314 tmp_name
= tmp_name
+ "___tmp"
315 text
+= prefix
+ "\t" + varname
+ "___tmp." + c
.insert_name
+ "(" + tmp_name
+ ");"
316 elif types
[0].name
in classnames
:
317 text
+= prefix
+ "\t" + types
[0].name
+ "* " + tmp_name
+ " = boost::python::extract<" + types
[0].name
+ "*>(" + varname
+ "[" + cntr_name
+ "]);"
318 if types
[0].attr_type
== attr_types
.star
:
319 text
+= prefix
+ "\t" + varname
+ "___tmp." + c
.insert_name
+ "(" + tmp_name
+ "->get_cpp_obj());"
321 text
+= prefix
+ "\t" + varname
+ "___tmp." + c
.insert_name
+ "(*" + tmp_name
+ "->get_cpp_obj());"
323 text
+= prefix
+ "\t" + types
[0].name
+ " " + tmp_name
+ " = boost::python::extract<" + types
[0].name
+ ">(" + varname
+ "[" + cntr_name
+ "]);"
324 text
+= prefix
+ "\t" + varname
+ "___tmp." + c
.insert_name
+ "(" + tmp_name
+ ");"
328 #Generate C++ code to translate to a boost::python::list
330 def translate_cpp(c
, varname
, types
, prefix
, ref
):
331 text
= prefix
+ c
.typename
+ " " + varname
+ "___tmp;"
332 tmp_name
= "tmp_" + str(Translator
.tmp_cntr
)
333 Translator
.tmp_cntr
= Translator
.tmp_cntr
+ 1
335 text
+= prefix
+ "for(auto " + tmp_name
+ " : *" + varname
+ ")"
337 text
+= prefix
+ "for(auto " + tmp_name
+ " : " + varname
+ ")"
339 if types
[0].name
in classnames
:
340 if types
[0].attr_type
== attr_types
.star
:
341 text
+= prefix
+ "\t" + varname
+ "___tmp.append(" + types
[0].name
+ "::get_py_obj(" + tmp_name
+ "));"
343 text
+= prefix
+ "\t" + varname
+ "___tmp.append(*" + types
[0].name
+ "::get_py_obj(&" + tmp_name
+ "));"
344 elif types
[0].name
in known_containers
:
345 text
+= known_containers
[types
[0].name
].translate_cpp(tmp_name
, types
[0].cont
.args
, prefix
+ "\t", types
[0].attr_type
== attr_types
.star
)
346 text
+= prefix
+ "\t" + varname
+ "___tmp.append(" + tmp_name
+ "___tmp);"
348 text
+= prefix
+ "\t" + varname
+ "___tmp.append(" + tmp_name
+ ");"
352 #Sub-type for std::set
353 class SetTranslator(PythonListTranslator
):
354 insert_name
= "insert"
355 orig_name
= "std::set"
357 #Sub-type for std::vector
358 class VectorTranslator(PythonListTranslator
):
359 insert_name
= "push_back"
360 orig_name
= "std::vector"
363 class PoolTranslator(PythonListTranslator
):
364 insert_name
= "insert"
367 #Translates dict-types (dict, std::map), that only differ in their name and
368 #the name of the insertion function
369 class PythonDictTranslator(Translator
):
370 typename
= "boost::python::dict"
371 insert_name
= "Default"
374 def gen_type(c
, types
):
375 text
= c
.orig_name
+ "<"
376 if types
[0].name
in primitive_types
:
377 text
+= types
[0].name
378 elif types
[0].name
in known_containers
:
379 text
+= known_containers
[types
[0].name
].gen_type(types
[0].cont
.args
)
381 text
+= class_by_name(types
[0].name
).namespace
+ "::" + types
[0].name
382 if types
[0].attr_type
== attr_types
.star
:
385 if types
[1].name
in primitive_types
:
386 text
+= types
[1].name
387 elif types
[1].name
in known_containers
:
388 text
+= known_containers
[types
[1].name
].gen_type(types
[1].cont
.args
)
390 text
+= class_by_name(types
[1].name
).namespace
+ "::" + types
[1].name
391 if types
[1].attr_type
== attr_types
.star
:
396 #Generate c++ code to translate from a boost::python::dict
398 def translate(c
, varname
, types
, prefix
):
399 text
= prefix
+ c
.gen_type(types
) + " " + varname
+ "___tmp;"
400 text
+= prefix
+ "boost::python::list " + varname
+ "_keylist = " + varname
+ ".keys();"
401 cntr_name
= "cntr_" + str(Translator
.tmp_cntr
)
402 Translator
.tmp_cntr
= Translator
.tmp_cntr
+ 1
403 text
+= prefix
+ "for(int " + cntr_name
+ " = 0; " + cntr_name
+ " < len(" + varname
+ "_keylist); " + cntr_name
+ "++)"
405 key_tmp_name
= "key_tmp_" + str(Translator
.tmp_cntr
)
406 val_tmp_name
= "val_tmp_" + str(Translator
.tmp_cntr
)
407 Translator
.tmp_cntr
= Translator
.tmp_cntr
+ 1
409 if types
[0].name
in known_containers
:
410 text
+= prefix
+ "\t" + known_containers
[types
[0].name
].typename
+ " " + key_tmp_name
+ " = boost::python::extract<" + known_containers
[types
[0].name
].typename
+ ">(" + varname
+ "_keylist[ " + cntr_name
+ " ]);"
411 text
+= known_containers
[types
[0].name
].translate(key_tmp_name
, types
[0].cont
.args
, prefix
+"\t")
412 key_tmp_name
= key_tmp_name
+ "___tmp"
413 elif types
[0].name
in classnames
:
414 text
+= prefix
+ "\t" + types
[0].name
+ "* " + key_tmp_name
+ " = boost::python::extract<" + types
[0].name
+ "*>(" + varname
+ "_keylist[ " + cntr_name
+ " ]);"
416 text
+= prefix
+ "\t" + types
[0].name
+ " " + key_tmp_name
+ " = boost::python::extract<" + types
[0].name
+ ">(" + varname
+ "_keylist[ " + cntr_name
+ " ]);"
418 if types
[1].name
in known_containers
:
419 text
+= prefix
+ "\t" + known_containers
[types
[1].name
].typename
+ " " + val_tmp_name
+ " = boost::python::extract<" + known_containers
[types
[1].name
].typename
+ ">(" + varname
+ "[" + varname
+ "_keylist[ " + cntr_name
+ " ]]);"
420 text
+= known_containers
[types
[1].name
].translate(val_tmp_name
, types
[1].cont
.args
, prefix
+"\t")
421 val_tmp_name
= val_tmp_name
+ "___tmp"
422 elif types
[1].name
in classnames
:
423 text
+= prefix
+ "\t" + types
[1].name
+ "* " + val_tmp_name
+ " = boost::python::extract<" + types
[1].name
+ "*>(" + varname
+ "[" + varname
+ "_keylist[ " + cntr_name
+ " ]]);"
425 text
+= prefix
+ "\t" + types
[1].name
+ " " + val_tmp_name
+ " = boost::python::extract<" + types
[1].name
+ ">(" + varname
+ "[" + varname
+ "_keylist[ " + cntr_name
+ " ]]);"
427 text
+= prefix
+ "\t" + varname
+ "___tmp." + c
.insert_name
+ "(std::pair<" + types
[0].gen_text_cpp() + ", " + types
[1].gen_text_cpp() + ">("
429 if types
[0].name
not in classnames
:
432 if types
[0].attr_type
!= attr_types
.star
:
434 text
+= key_tmp_name
+ "->get_cpp_obj()"
437 if types
[1].name
not in classnames
:
440 if types
[1].attr_type
!= attr_types
.star
:
442 text
+= val_tmp_name
+ "->get_cpp_obj()"
443 text
+= "));\n" + prefix
+ "}"
446 #Generate c++ code to translate to a boost::python::dict
448 def translate_cpp(c
, varname
, types
, prefix
, ref
):
449 text
= prefix
+ c
.typename
+ " " + varname
+ "___tmp;"
450 tmp_name
= "tmp_" + str(Translator
.tmp_cntr
)
451 Translator
.tmp_cntr
= Translator
.tmp_cntr
+ 1
453 text
+= prefix
+ "for(auto " + tmp_name
+ " : *" + varname
+ ")"
455 text
+= prefix
+ "for(auto " + tmp_name
+ " : " + varname
+ ")"
457 if types
[1].name
in known_containers
:
458 text
+= prefix
+ "\tauto " + tmp_name
+ "_second = " + tmp_name
+ ".second;"
459 text
+= known_containers
[types
[1].name
].translate_cpp(tmp_name
+ "_second", types
[1].cont
.args
, prefix
+ "\t", types
[1].attr_type
== attr_types
.star
)
461 if types
[0].name
in classnames
:
462 text
+= prefix
+ "\t" + varname
+ "___tmp[" + types
[0].name
+ "::get_py_obj(" + tmp_name
+ ".first)] = "
463 elif types
[0].name
not in known_containers
:
464 text
+= prefix
+ "\t" + varname
+ "___tmp[" + tmp_name
+ ".first] = "
466 if types
[1].name
in classnames
:
467 if types
[1].attr_type
== attr_types
.star
:
468 text
+= types
[1].name
+ "::get_py_obj(" + tmp_name
+ ".second);"
470 text
+= "*" + types
[1].name
+ "::get_py_obj(&" + tmp_name
+ ".second);"
471 elif types
[1].name
in known_containers
:
472 text
+= tmp_name
+ "_second___tmp;"
474 text
+= tmp_name
+ ".second;"
479 class DictTranslator(PythonDictTranslator
):
480 insert_name
= "insert"
483 #Sub_type for std::map
484 class MapTranslator(PythonDictTranslator
):
485 insert_name
= "insert"
486 orig_name
= "std::map"
488 #Translator for std::pair. Derived from PythonDictTranslator because the
489 #gen_type function is the same (because both have two template parameters)
490 class TupleTranslator(PythonDictTranslator
):
491 typename
= "boost::python::tuple"
492 orig_name
= "std::pair"
494 #Generate c++ code to translate from a boost::python::tuple
496 def translate(c
, varname
, types
, prefix
):
497 text
= prefix
+ types
[0].name
+ " " + varname
+ "___tmp_0 = boost::python::extract<" + types
[0].name
+ ">(" + varname
+ "[0]);"
498 text
+= prefix
+ types
[1].name
+ " " + varname
+ "___tmp_1 = boost::python::extract<" + types
[1].name
+ ">(" + varname
+ "[1]);"
499 text
+= prefix
+ TupleTranslator
.gen_type(types
) + " " + varname
+ "___tmp("
500 if types
[0].name
.split(" ")[-1] in primitive_types
:
501 text
+= varname
+ "___tmp_0, "
503 text
+= varname
+ "___tmp_0.get_cpp_obj(), "
504 if types
[1].name
.split(" ")[-1] in primitive_types
:
505 text
+= varname
+ "___tmp_1);"
507 text
+= varname
+ "___tmp_1.get_cpp_obj());"
510 #Generate c++ code to translate to a boost::python::tuple
512 def translate_cpp(c
, varname
, types
, prefix
, ref
):
513 # if the tuple is a pair of SigSpecs (aka SigSig), then we need
514 # to call get_py_obj() on each item in the tuple
515 if types
[0].name
in classnames
:
516 first_var
= types
[0].name
+ "::get_py_obj(" + varname
+ ".first)"
518 first_var
= varname
+ ".first"
519 if types
[1].name
in classnames
:
520 second_var
= types
[1].name
+ "::get_py_obj(" + varname
+ ".second)"
522 second_var
= varname
+ ".second"
523 text
= prefix
+ TupleTranslator
.typename
+ " " + varname
+ "___tmp = boost::python::make_tuple(" + first_var
+ ", " + second_var
+ ");"
526 #Associate the Translators with their c++ type
528 "std::set" : SetTranslator
,
529 "std::vector" : VectorTranslator
,
530 "pool" : PoolTranslator
,
531 "dict" : DictTranslator
,
532 "std::pair" : TupleTranslator
,
533 "std::map" : MapTranslator
544 def __init__(self
, wtype
, varname
, is_const
= False, default_value
= None):
546 self
.varname
= varname
547 self
.is_const
= is_const
548 self
.default_value
= None
549 self
.container
= None
552 def from_string(str_def
, containing_file
, line_number
):
556 arg
= Attribute(None, None)
561 arg
.default_value
= None
563 if str.startswith(str_def
, "const "):
565 str_def
= str_def
[6:]
566 if str.startswith(str_def
, "unsigned "):
568 str_def
= str_def
[9:]
569 while str.startswith(str_def
, "long "):
570 prefix
= "long " + prefix
571 str_def
= str_def
[5:]
572 while str.startswith(str_def
, "short "):
573 prefix
= "short " + prefix
574 str_def
= str_def
[6:]
576 if str_def
.find("<") != -1 and str_def
.find("<") < str_def
.find(" "):
577 closing
= find_closing(str_def
[str_def
.find("<"):], "<", ">") + str_def
.find("<") + 1
578 arg
.wtype
= WType
.from_string(str_def
[:closing
].strip(), containing_file
, line_number
)
579 str_def
= str_def
[closing
+1:]
581 if str_def
.count(" ") > 0:
582 arg
.wtype
= WType
.from_string(prefix
+ str_def
[:str_def
.find(" ")].strip(), containing_file
, line_number
)
583 str_def
= str_def
[str_def
.find(" ")+1:]
585 arg
.wtype
= WType
.from_string(prefix
+ str_def
.strip(), containing_file
, line_number
)
589 if arg
.wtype
== None:
591 if str_def
.count("=") == 0:
592 arg
.varname
= str_def
.strip()
593 if arg
.varname
.find(" ") > 0:
596 arg
.varname
= str_def
[:str_def
.find("=")].strip()
597 if arg
.varname
.find(" ") > 0:
599 str_def
= str_def
[str_def
.find("=")+1:].strip()
600 arg
.default_value
= str_def
[arg
.varname
.find("=")+1:].strip()
601 if len(arg
.varname
) == 0:
604 if arg
.varname
[0] == '*':
605 arg
.wtype
.attr_type
= attr_types
.star
606 arg
.varname
= arg
.varname
[1:]
607 elif arg
.varname
[0] == '&':
608 if arg
.wtype
.attr_type
!= attr_types
.default
:
610 if arg
.varname
[1] == '&':
611 arg
.wtype
.attr_type
= attr_types
.ampamp
612 arg
.varname
= arg
.varname
[2:]
614 arg
.wtype
.attr_type
= attr_types
.amp
615 arg
.varname
= arg
.varname
[1:]
618 #Generates the varname. If the attribute has no name in the header file,
620 def gen_varname(self
):
621 if self
.varname
!= None:
623 if self
.wtype
.name
== "void":
626 self
.pos
= Attribute
.pos_counter
627 Attribute
.pos_counter
= Attribute
.pos_counter
+ 1
628 return "gen_varname_" + str(self
.pos
)
630 #Generates the text for the function headers with wrapper types
631 def gen_listitem(self
):
635 if self
.wtype
.name
in classnames
:
636 return prefix
+ self
.wtype
.name
+ "* " + self
.gen_varname()
637 if self
.wtype
.name
in known_containers
:
638 return prefix
+ known_containers
[self
.wtype
.name
].typename
+ " " + self
.gen_varname()
639 return prefix
+ self
.wtype
.name
+ " " + self
.gen_varname()
641 #Generates the test for the function headers with c++ types
642 def gen_listitem_cpp(self
):
647 if self
.wtype
.attr_type
== attr_types
.star
:
649 elif self
.wtype
.attr_type
== attr_types
.amp
:
651 elif self
.wtype
.attr_type
== attr_types
.ampamp
:
653 if self
.wtype
.name
in known_containers
:
654 return prefix
+ known_containers
[self
.wtype
.name
].gen_type(self
.wtype
.cont
.args
) + " " + infix
+ self
.gen_varname()
655 if self
.wtype
.name
in classnames
:
656 return prefix
+ class_by_name(self
.wtype
.name
).namespace
+ "::" + self
.wtype
.name
+ " " + infix
+ self
.gen_varname()
657 return prefix
+ self
.wtype
.name
+ " " + infix
+ self
.gen_varname()
659 #Generates the listitem withtout the varname, so the signature can be
661 def gen_listitem_hash(self
):
665 if self
.wtype
.name
in classnames
:
666 return prefix
+ self
.wtype
.name
+ "* "
667 if self
.wtype
.name
in known_containers
:
668 return known_containers
[self
.wtype
.name
].typename
669 return prefix
+ self
.wtype
.name
671 #Generate Translation code for the attribute
672 def gen_translation(self
):
673 if self
.wtype
.name
in known_containers
:
674 return known_containers
[self
.wtype
.name
].translate(self
.gen_varname(), self
.wtype
.cont
.args
, "\n\t\t")
677 #Generate Translation code from c++ for the attribute
678 def gen_translation_cpp(self
):
679 if self
.wtype
.name
in known_containers
:
680 return known_containers
[self
.wtype
.name
].translate_cpp(self
.gen_varname(), self
.wtype
.cont
.args
, "\n\t\t", self
.wtype
.attr_type
== attr_types
.star
)
683 #Generate Text for the call
685 ret
= self
.gen_varname()
686 if self
.wtype
.name
in known_containers
:
687 if self
.wtype
.attr_type
== attr_types
.star
:
688 return "&" + ret
+ "___tmp"
689 return ret
+ "___tmp"
690 if self
.wtype
.name
in classnames
:
691 if self
.wtype
.attr_type
!= attr_types
.star
:
693 return ret
+ "->get_cpp_obj()"
694 if self
.wtype
.name
== "char *" and self
.gen_varname() in ["format", "fmt"]:
695 return "\"%s\", " + self
.gen_varname()
696 if self
.wtype
.attr_type
== attr_types
.star
:
700 def gen_call_cpp(self
):
701 ret
= self
.gen_varname()
702 if self
.wtype
.name
.split(" ")[-1] in primitive_types
or self
.wtype
.name
in enum_names
:
703 if self
.wtype
.attr_type
== attr_types
.star
:
706 if self
.wtype
.name
not in classnames
:
707 if self
.wtype
.attr_type
== attr_types
.star
:
708 return "&" + ret
+ "___tmp"
709 return ret
+ "___tmp"
710 if self
.wtype
.attr_type
!= attr_types
.star
:
712 return self
.wtype
.name
+ "::get_py_obj(" + self
.gen_varname() + ")"
714 #Generate cleanup code
715 def gen_cleanup(self
):
716 if self
.wtype
.name
in primitive_types
or self
.wtype
.name
in classnames
or self
.wtype
.name
in enum_names
or not self
.wtype
.attr_type
== attr_types
.star
or (self
.wtype
.name
in known_containers
and self
.wtype
.attr_type
== attr_types
.star
):
718 return "\n\t\tdelete " + self
.gen_varname() + "___tmp;"
733 def __init__(self
, name
, link_type
, id_
, string_id
= None, hash_id
= None, needs_clone
= False):
735 self
.namespace
= None
736 self
.base_class
= None
737 self
.link_type
= link_type
739 self
.string_id
= string_id
740 self
.hash_id
= hash_id
741 self
.needs_clone
= needs_clone
744 self
.found_constrs
= []
746 def printable_constrs(self
):
748 for con
in self
.found_constrs
:
749 if not con
.protected
:
753 def gen_decl(self
, filename
):
754 long_name
= self
.namespace
+ "::" + self
.name
756 text
= "\n\t// WRAPPED from " + filename
757 text
+= "\n\tstruct " + self
.name
758 if self
.link_type
== link_types
.derive
:
759 text
+= " : public " + self
.namespace
+ "::" + self
.name
762 if self
.link_type
!= link_types
.derive
:
764 text
+= "\t\t" + long_name
+ "* ref_obj;\n"
766 if self
.link_type
== link_types
.ref_copy
or self
.link_type
== link_types
.pointer
:
767 text
+= "\n\t\t" + long_name
+ "* get_cpp_obj() const\n\t\t{\n\t\t\treturn ref_obj;\n\t\t}\n"
768 elif self
.link_type
== link_types
.global_list
:
769 text
+= "\t\t" + self
.id_
.wtype
.name
+ " " + self
.id_
.varname
+ ";\n"
770 text
+= "\n\t\t" + long_name
+ "* get_cpp_obj() const\n\t\t{"
771 text
+= "\n\t\t\t" + long_name
+ "* ret = " + long_name
+ "::get_all_" + self
.name
.lower() + "s()->at(this->" + self
.id_
.varname
+ ");"
772 text
+= "\n\t\t\tif(ret != NULL && ret == this->ref_obj)"
773 text
+= "\n\t\t\t\treturn ret;"
774 text
+= "\n\t\t\tthrow std::runtime_error(\"" + self
.name
+ "'s c++ object does not exist anymore.\");"
775 text
+= "\n\t\t\treturn NULL;"
778 #if self.link_type != link_types.pointer:
779 text
+= "\n\t\tstatic " + self
.name
+ "* get_py_obj(" + long_name
+ "* ref)\n\t\t{"
780 text
+= "\n\t\t\tif(ref == nullptr){"
781 text
+= "\n\t\t\t\tthrow std::runtime_error(\"" + self
.name
+ " does not exist.\");"
783 text
+= "\n\t\t\t" + self
.name
+ "* ret = (" + self
.name
+ "*)malloc(sizeof(" + self
.name
+ "));"
784 if self
.link_type
== link_types
.pointer
:
785 text
+= "\n\t\t\tret->ref_obj = ref;"
786 if self
.link_type
== link_types
.ref_copy
:
788 text
+= "\n\t\t\tret->ref_obj = ref->clone();"
790 text
+= "\n\t\t\tret->ref_obj = new "+long_name
+"(*ref);"
791 if self
.link_type
== link_types
.global_list
:
792 text
+= "\n\t\t\tret->ref_obj = ref;"
793 text
+= "\n\t\t\tret->" + self
.id_
.varname
+ " = ret->ref_obj->" + self
.id_
.varname
+ ";"
794 text
+= "\n\t\t\treturn ret;"
797 if self
.link_type
== link_types
.ref_copy
:
798 text
+= "\n\t\tstatic " + self
.name
+ "* get_py_obj(" + long_name
+ " ref)\n\t\t{"
799 text
+= "\n\t\t\t" + self
.name
+ "* ret = (" + self
.name
+ "*)malloc(sizeof(" + self
.name
+ "));"
801 text
+= "\n\t\t\tret->ref_obj = ref.clone();"
803 text
+= "\n\t\t\tret->ref_obj = new "+long_name
+"(ref);"
804 text
+= "\n\t\t\treturn ret;"
807 for con
in self
.found_constrs
:
808 text
+= con
.gen_decl()
809 for var
in self
.found_vars
:
810 text
+= var
.gen_decl()
811 for fun
in self
.found_funs
:
812 text
+= fun
.gen_decl()
815 if self
.link_type
== link_types
.derive
:
817 for fun
in self
.found_funs
:
818 if fun
.name
in duplicates
:
820 duplicates
[fun
.name
].gen_alias()
822 duplicates
[fun
.name
] = fun
824 text
+= "\n\t\t" + long_name
+ "* get_cpp_obj() const\n\t\t{\n\t\t\treturn (" + self
.namespace
+ "::" + self
.name
+"*)this;\n\t\t}\n"
825 text
+= "\n\t\tstatic " + self
.name
+ "* get_py_obj(" + long_name
+ "* ref)\n\t\t{"
826 text
+= "\n\t\t\treturn (" + self
.name
+ "*)ref;"
829 for con
in self
.found_constrs
:
830 text
+= con
.gen_decl_derive()
831 for var
in self
.found_vars
:
832 text
+= var
.gen_decl()
833 for fun
in self
.found_funs
:
834 text
+= fun
.gen_decl_virtual()
836 if self
.hash_id
!= None:
837 text
+= "\n\t\tunsigned int get_hash_py()"
839 text
+= "\n\t\t\treturn get_cpp_obj()->" + self
.hash_id
+ ";"
844 if self
.link_type
== link_types
.derive
:
845 text
+= "\n\tstruct " + self
.name
+ "Wrap : " + self
.name
+ ", boost::python::wrapper<" + self
.name
+ ">"
848 for con
in self
.found_constrs
:
849 text
+= con
.gen_decl_wrapperclass()
850 for fun
in self
.found_funs
:
851 text
+= fun
.gen_default_impl()
855 text
+= "\n\tstd::ostream &operator<<(std::ostream &ostr, const " + self
.name
+ " &ref)"
857 text
+= "\n\t\tostr << \"" + self
.name
858 if self
.string_id
!= None:
860 text
+= " << ref.get_cpp_obj()->" + self
.string_id
861 text
+= " << \"\\\"\""
863 text
+= " at \" << ref.get_cpp_obj()"
865 text
+= "\n\t\treturn ostr;"
871 def gen_funs(self
, filename
):
873 if self
.link_type
!= link_types
.derive
:
874 for con
in self
.found_constrs
:
875 text
+= con
.gen_def()
876 for var
in self
.found_vars
:
877 text
+= var
.gen_def()
878 for fun
in self
.found_funs
:
879 text
+= fun
.gen_def()
881 for var
in self
.found_vars
:
882 text
+= var
.gen_def()
883 for fun
in self
.found_funs
:
884 text
+= fun
.gen_def_virtual()
887 def gen_boost_py_body(self
):
889 if self
.printable_constrs() == 0 or not self
.contains_default_constr():
892 text
+= "\n\t\t\t.def(boost::python::self_ns::str(boost::python::self_ns::self))"
893 text
+= "\n\t\t\t.def(boost::python::self_ns::repr(boost::python::self_ns::self))"
894 for con
in self
.found_constrs
:
895 text
+= con
.gen_boost_py()
896 for var
in self
.found_vars
:
897 text
+= var
.gen_boost_py()
899 for fun
in self
.found_funs
:
900 text
+= fun
.gen_boost_py()
901 if fun
.is_static
and fun
.alias
not in static_funs
:
902 static_funs
.append(fun
.alias
)
903 for fun
in static_funs
:
904 text
+= "\n\t\t\t.staticmethod(\"" + fun
+ "\")"
906 if self
.hash_id
!= None:
907 text
+= "\n\t\t\t.def(\"__hash__\", &" + self
.name
+ "::get_hash_py)"
908 text
+= "\n\t\t\t;\n"
911 def gen_boost_py(self
):
912 body
= self
.gen_boost_py_body()
913 if self
.link_type
== link_types
.derive
:
914 text
= "\n\t\tclass_<" + self
.name
+ ">(\"Cpp" + self
.name
+ "\""
916 text
+= "\n\t\tclass_<" + self
.name
917 text
+= "Wrap, boost::noncopyable"
918 text
+= ">(\"" + self
.name
+ "\""
921 text
= "\n\t\tclass_<" + self
.name
+ ">(\"" + self
.name
+ "\""
926 def contains_default_constr(self
):
927 for c
in self
.found_constrs
:
932 #CONFIGURE HEADER-FILES TO BE PARSED AND CLASSES EXPECTED IN THEM HERE
935 Source("kernel/celltypes",[
936 WClass("CellType", link_types
.pointer
, None, None, "type.hash()", True),
937 WClass("CellTypes", link_types
.pointer
, None, None, None, True)
940 Source("kernel/consteval",[
941 WClass("ConstEval", link_types
.pointer
, None, None, None, True)
944 Source("kernel/log",[]),
945 Source("kernel/register",[
946 WClass("Pass", link_types
.derive
, None, None, None, True),
949 Source("kernel/rtlil",[
950 WClass("IdString", link_types
.ref_copy
, None, "str()", "hash()"),
951 WClass("Const", link_types
.ref_copy
, None, "as_string()", "hash()"),
952 WClass("AttrObject", link_types
.ref_copy
, None, None, None),
953 WClass("Selection", link_types
.ref_copy
, None, None, None),
954 WClass("Monitor", link_types
.derive
, None, None, None),
955 WClass("CaseRule",link_types
.ref_copy
, None, None, None, True),
956 WClass("SwitchRule",link_types
.ref_copy
, None, None, None, True),
957 WClass("SyncRule", link_types
.ref_copy
, None, None, None, True),
958 WClass("Process", link_types
.ref_copy
, None, "name.c_str()", "name.hash()"),
959 WClass("SigChunk", link_types
.ref_copy
, None, None, None),
960 WClass("SigBit", link_types
.ref_copy
, None, None, "hash()"),
961 WClass("SigSpec", link_types
.ref_copy
, None, None, "hash()"),
962 WClass("Cell", link_types
.global_list
, Attribute(WType("unsigned int"), "hashidx_"), "name.c_str()", "hash()"),
963 WClass("Wire", link_types
.global_list
, Attribute(WType("unsigned int"), "hashidx_"), "name.c_str()", "hash()"),
964 WClass("Memory", link_types
.global_list
, Attribute(WType("unsigned int"), "hashidx_"), "name.c_str()", "hash()"),
965 WClass("Module", link_types
.global_list
, Attribute(WType("unsigned int"), "hashidx_"), "name.c_str()", "hash()"),
966 WClass("Design", link_types
.global_list
, Attribute(WType("unsigned int"), "hashidx_"), "hashidx_", "hash()")
969 #Source("kernel/satgen",[
972 #Source("libs/ezsat/ezsat",[
975 #Source("libs/ezsat/ezminisat",[
978 Source("kernel/sigtools",[
979 WClass("SigMap", link_types
.pointer
, None, None, None, True)
982 Source("kernel/yosys",[
985 Source("kernel/cost",[])
988 blacklist_methods
= ["YOSYS_NAMESPACE::Pass::run_register", "YOSYS_NAMESPACE::Module::Pow", "YOSYS_NAMESPACE::Module::Bu0", "YOSYS_NAMESPACE::CaseRule::optimize"]
990 enum_names
= ["State","SyncType","ConstFlags"]
992 enums
= [] #Do not edit
995 unowned_functions
= []
998 for source
in sources
:
999 for wclass
in source
.classes
:
1000 classnames
.append(wclass
.name
)
1002 def class_by_name(name
):
1003 for source
in sources
:
1004 for wclass
in source
.classes
:
1005 if wclass
.name
== name
:
1009 def enum_by_name(name
):
1015 def find_closing(text
, open_tok
, close_tok
):
1016 if text
.find(open_tok
) == -1 or text
.find(close_tok
) <= text
.find(open_tok
):
1017 return text
.find(close_tok
)
1018 return text
.find(close_tok
) + find_closing(text
[text
.find(close_tok
)+1:], open_tok
, close_tok
) + 1
1020 def unpretty_string(s
):
1022 while s
.find(" ") != -1:
1023 s
= s
.replace(" "," ")
1024 while s
.find("\t") != -1:
1025 s
= s
.replace("\t"," ")
1026 s
= s
.replace(" (","(")
1034 def from_string(str_def
, namespace
, line_number
):
1035 str_def
= str_def
.strip()
1036 if not str.startswith(str_def
, "enum "):
1038 if str_def
.count(";") != 1:
1040 str_def
= str_def
[5:]
1042 split
= str_def
.split(":")
1043 if(len(split
) != 2):
1045 enum
.name
= split
[0].strip()
1046 if enum
.name
not in enum_names
:
1049 if str_def
.count("{") != str_def
.count("}") != 1:
1051 if len(str_def
) < str_def
.find("}")+2 or str_def
[str_def
.find("}")+1] != ';':
1053 str_def
= str_def
.split("{")[-1].split("}")[0]
1055 for val
in str_def
.split(','):
1056 enum
.values
.append(val
.strip().split('=')[0].strip())
1057 enum
.namespace
= namespace
1060 def gen_boost_py(self
):
1061 text
= "\n\t\tenum_<" + self
.namespace
+ "::" + self
.name
+ ">(\"" + self
.name
+ "\")\n"
1062 for value
in self
.values
:
1063 text
+= "\t\t\t.value(\"" + value
+ "\"," + self
.namespace
+ "::" + value
+ ")\n"
1068 ret
= "Enum " + self
.namespace
+ "::" + self
.name
+ "(\n"
1069 for val
in self
.values
:
1070 ret
= ret
+ "\t" + val
+ "\n"
1074 return __str__(self
)
1079 containing_file
= None
1084 def __init__(self
, containing_file
, class_
):
1085 self
.orig_text
= "Auto generated default constructor"
1087 self
.containing_file
= containing_file
1088 self
.member_of
= class_
1089 self
.protected
= False
1091 def from_string(str_def
, containing_file
, class_
, line_number
, protected
= False):
1094 if str_def
.count("delete;") > 0:
1096 con
= WConstructor(containing_file
, class_
)
1097 con
.orig_text
= str_def
1099 con
.duplicate
= False
1100 con
.protected
= protected
1101 if str.startswith(str_def
, "inline "):
1102 str_def
= str_def
[7:]
1103 if not str.startswith(str_def
, class_
.name
+ "("):
1105 str_def
= str_def
[len(class_
.name
)+1:]
1106 found
= find_closing(str_def
, "(", ")")
1109 str_def
= str_def
[0:found
].strip()
1110 if len(str_def
) == 0:
1112 for arg
in split_list(str_def
, ","):
1113 parsed
= Attribute
.from_string(arg
.strip(), containing_file
, line_number
)
1116 con
.args
.append(parsed
)
1120 if self
.duplicate
or self
.protected
:
1122 text
= "\n\t\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1123 text
+= "\n\t\t" + self
.member_of
.name
+ "("
1124 for arg
in self
.args
:
1125 text
+= arg
.gen_listitem() + ", "
1126 if len(self
.args
) > 0:
1131 def gen_decl_derive(self
):
1132 if self
.duplicate
or self
.protected
:
1134 text
= "\n\t\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1135 text
+= "\n\t\t" + self
.member_of
.name
+ "("
1136 for arg
in self
.args
:
1137 text
+= arg
.gen_listitem() + ", "
1138 if len(self
.args
) > 0:
1141 if len(self
.args
) == 0:
1144 text
+= self
.member_of
.namespace
+ "::" + self
.member_of
.name
+ "("
1145 for arg
in self
.args
:
1146 text
+= arg
.gen_call() + ", "
1147 if len(self
.args
) > 0:
1152 def gen_decl_wrapperclass(self
):
1153 if self
.duplicate
or self
.protected
:
1155 text
= "\n\t\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1156 text
+= "\n\t\t" + self
.member_of
.name
+ "Wrap("
1157 for arg
in self
.args
:
1158 text
+= arg
.gen_listitem() + ", "
1159 if len(self
.args
) > 0:
1162 if len(self
.args
) == 0:
1165 text
+= self
.member_of
.name
+ "("
1166 for arg
in self
.args
:
1167 text
+= arg
.gen_call() + ", "
1168 if len(self
.args
) > 0:
1173 def gen_decl_hash_py(self
):
1174 text
= self
.member_of
.name
+ "("
1175 for arg
in self
.args
:
1176 text
+= arg
.gen_listitem_hash() + ", "
1177 if len(self
.args
) > 0:
1183 if self
.duplicate
or self
.protected
:
1185 text
= "\n\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1186 text
+= "\n\t" + self
.member_of
.name
+ "::" + self
.member_of
.name
+ "("
1187 for arg
in self
.args
:
1188 text
+= arg
.gen_listitem() + ", "
1189 if len(self
.args
) > 0:
1192 for arg
in self
.args
:
1193 text
+= arg
.gen_translation()
1194 if self
.member_of
.link_type
!= link_types
.derive
:
1195 text
+= "\n\t\tthis->ref_obj = new " + self
.member_of
.namespace
+ "::" + self
.member_of
.name
+ "("
1196 for arg
in self
.args
:
1197 text
+= arg
.gen_call() + ", "
1198 if len(self
.args
) > 0:
1200 if self
.member_of
.link_type
!= link_types
.derive
:
1202 if self
.member_of
.link_type
== link_types
.global_list
:
1203 text
+= "\n\t\tthis->" + self
.member_of
.id_
.varname
+ " = this->ref_obj->" + self
.member_of
.id_
.varname
+ ";"
1204 for arg
in self
.args
:
1205 text
+= arg
.gen_cleanup()
1209 def gen_boost_py(self
):
1210 if self
.duplicate
or self
.protected
or len(self
.args
) == 0:
1212 text
= "\n\t\t\t.def(init"
1215 text
+= a
.gen_listitem_hash() + ", "
1216 text
= text
[0:-2] + ">())"
1224 ret_attr_type
= attr_types
.default
1230 containing_file
= None
1235 def from_string(str_def
, containing_file
, class_
, line_number
, namespace
):
1236 if str_def
.count("delete;") > 0:
1239 func
.is_static
= False
1240 func
.is_inline
= False
1241 func
.is_virtual
= False
1242 func
.ret_attr_type
= attr_types
.default
1243 func
.is_operator
= False
1244 func
.member_of
= None
1245 func
.orig_text
= str_def
1247 func
.containing_file
= containing_file
1248 func
.member_of
= class_
1249 func
.duplicate
= False
1250 func
.namespace
= namespace
1251 str_def
= str_def
.replace("operator ","operator")
1252 if str.startswith(str_def
, "static "):
1253 func
.is_static
= True
1254 str_def
= str_def
[7:]
1256 func
.is_static
= False
1257 if str.startswith(str_def
, "inline "):
1258 func
.is_inline
= True
1259 str_def
= str_def
[7:]
1261 func
.is_inline
= False
1262 if str.startswith(str_def
, "virtual "):
1263 func
.is_virtual
= True
1264 str_def
= str_def
[8:]
1266 func
.is_virtual
= False
1268 if str_def
.count(" ") == 0:
1271 parts
= split_list(str_def
.strip(), " ")
1276 if part
in ["unsigned", "long", "short"]:
1277 prefix
+= part
+ " "
1286 func
.ret_type
= WType
.from_string(prefix
+ parts
[0], containing_file
, line_number
)
1288 if func
.ret_type
== None:
1292 for part
in parts
[2:]:
1293 str_def
= str_def
+ " " + part
1295 found
= str_def
.find("(")
1296 if found
== -1 or (str_def
.find(" ") != -1 and found
> str_def
.find(" ")):
1298 func
.name
= str_def
[:found
]
1299 str_def
= str_def
[found
:]
1300 if func
.name
.find("operator") != -1 and str.startswith(str_def
, "()("):
1302 str_def
= str_def
[2:]
1303 str_def
= str_def
[1:]
1304 if func
.name
.find("operator") != -1:
1305 func
.is_operator
= True
1306 if func
.name
.find("*") == 0:
1307 func
.name
= func
.name
.replace("*", "")
1308 func
.ret_type
.attr_type
= attr_types
.star
1309 if func
.name
.find("&&") == 0:
1310 func
.name
= func
.name
.replace("&&", "")
1311 func
.ret_type
.attr_type
= attr_types
.ampamp
1312 if func
.name
.find("&") == 0:
1313 func
.name
= func
.name
.replace("&", "")
1314 func
.ret_type
.attr_type
= attr_types
.amp
1316 found
= find_closing(str_def
, "(", ")")
1319 str_def
= str_def
[0:found
]
1320 if func
.name
in blacklist_methods
:
1322 if func
.namespace
!= None and func
.namespace
!= "":
1323 if (func
.namespace
+ "::" + func
.name
) in blacklist_methods
:
1325 if func
.member_of
!= None:
1326 if (func
.namespace
+ "::" + func
.member_of
.name
+ "::" + func
.name
) in blacklist_methods
:
1328 if func
.is_operator
and func
.name
.replace(" ","").replace("operator","").split("::")[-1] not in wrappable_operators
:
1331 testname
= func
.name
1332 if func
.is_operator
:
1333 testname
= testname
[:testname
.find("operator")]
1334 if testname
.count(")") != 0 or testname
.count("(") != 0 or testname
.count("~") != 0 or testname
.count(";") != 0 or testname
.count(">") != 0 or testname
.count("<") != 0 or testname
.count("throw") != 0:
1337 func
.alias
= func
.name
1338 if func
.name
in keyword_aliases
:
1339 func
.alias
= keyword_aliases
[func
.name
]
1340 str_def
= str_def
[:found
].strip()
1341 if(len(str_def
) == 0):
1343 for arg
in split_list(str_def
, ","):
1344 if arg
.strip() == "...":
1346 parsed
= Attribute
.from_string(arg
.strip(), containing_file
, line_number
)
1349 func
.args
.append(parsed
)
1352 def gen_alias(self
):
1353 self
.alias
= self
.name
1354 for arg
in self
.args
:
1355 self
.alias
+= "__" + arg
.wtype
.gen_text_cpp().replace("::", "_").replace("<","_").replace(">","_").replace(" ","").replace("*","").replace(",","")
1360 text
= "\n\t\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1364 text
+= self
.ret_type
.gen_text() + " " + self
.alias
+ "("
1365 for arg
in self
.args
:
1366 text
+= arg
.gen_listitem()
1368 if len(self
.args
) > 0:
1373 def gen_decl_virtual(self
):
1376 if not self
.is_virtual
:
1377 return self
.gen_decl()
1378 text
= "\n\t\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1379 text
+= "\n\t\tvirtual "
1382 text
+= self
.ret_type
.gen_text() + " py_" + self
.alias
+ "("
1383 for arg
in self
.args
:
1384 text
+= arg
.gen_listitem()
1386 if len(self
.args
) > 0:
1389 if len(self
.args
) == 0:
1393 for arg
in self
.args
:
1394 text
+= "\n\t\t\t(void)" + arg
.gen_varname() + ";"
1396 text
+= "\n\t\tvirtual "
1399 text
+= self
.ret_type
.gen_text() + " " + self
.name
+ "("
1400 for arg
in self
.args
:
1401 text
+= arg
.gen_listitem_cpp()
1403 if len(self
.args
) > 0:
1405 text
+= ") YS_OVERRIDE;\n"
1408 def gen_decl_hash_py(self
):
1409 text
= self
.ret_type
.gen_text() + " " + self
.alias
+ "("
1410 for arg
in self
.args
:
1411 text
+= arg
.gen_listitem_hash() + ", "
1412 if len(self
.args
) > 0:
1420 text
= "\n\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1421 text
+= "\n\t" + self
.ret_type
.gen_text() + " "
1422 if self
.member_of
!= None:
1423 text
+= self
.member_of
.name
+ "::"
1424 text
+= self
.alias
+ "("
1425 for arg
in self
.args
:
1426 text
+= arg
.gen_listitem()
1428 if len(self
.args
) > 0:
1431 for arg
in self
.args
:
1432 text
+= arg
.gen_translation()
1434 if self
.ret_type
.name
!= "void":
1435 if self
.ret_type
.name
in known_containers
:
1436 text
+= self
.ret_type
.gen_text_cpp()
1438 text
+= self
.ret_type
.gen_text()
1439 if self
.ret_type
.name
in classnames
or (self
.ret_type
.name
in known_containers
and self
.ret_type
.attr_type
== attr_types
.star
):
1442 if self
.ret_type
.name
in classnames
:
1443 text
+= self
.ret_type
.name
+ "::get_py_obj("
1444 if self
.member_of
== None:
1445 text
+= "::" + self
.namespace
+ "::" + self
.alias
+ "("
1446 elif self
.is_static
:
1447 text
+= self
.member_of
.namespace
+ "::" + self
.member_of
.name
+ "::" + self
.name
+ "("
1449 text
+= "this->get_cpp_obj()->" + self
.name
+ "("
1450 for arg
in self
.args
:
1451 text
+= arg
.gen_call() + ", "
1452 if len(self
.args
) > 0:
1454 if self
.ret_type
.name
in classnames
:
1457 for arg
in self
.args
:
1458 text
+= arg
.gen_cleanup()
1459 if self
.ret_type
.name
!= "void":
1460 if self
.ret_type
.name
in classnames
:
1461 text
+= "\n\t\treturn *ret_;"
1462 elif self
.ret_type
.name
in known_containers
:
1463 text
+= known_containers
[self
.ret_type
.name
].translate_cpp("ret_", self
.ret_type
.cont
.args
, "\n\t\t", self
.ret_type
.attr_type
== attr_types
.star
)
1464 text
+= "\n\t\treturn ret____tmp;"
1466 text
+= "\n\t\treturn ret_;"
1470 def gen_def_virtual(self
):
1473 if not self
.is_virtual
:
1474 return self
.gen_def()
1475 text
= "\n\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1479 text
+= self
.ret_type
.gen_text() + " " + self
.member_of
.name
+ "::" + self
.name
+ "("
1480 for arg
in self
.args
:
1481 text
+= arg
.gen_listitem_cpp()
1483 if len(self
.args
) > 0:
1486 for arg
in self
.args
:
1487 text
+= arg
.gen_translation_cpp()
1489 if self
.member_of
== None:
1490 text
+= "::" + self
.namespace
+ "::" + self
.alias
+ "("
1491 elif self
.is_static
:
1492 text
+= self
.member_of
.namespace
+ "::" + self
.member_of
.name
+ "::" + self
.name
+ "("
1494 text
+= "py_" + self
.alias
+ "("
1495 for arg
in self
.args
:
1496 text
+= arg
.gen_call_cpp() + ", "
1497 if len(self
.args
) > 0:
1499 if self
.ret_type
.name
in classnames
:
1502 for arg
in self
.args
:
1503 text
+= arg
.gen_cleanup()
1507 def gen_default_impl(self
):
1510 if not self
.is_virtual
:
1512 text
= "\n\n\t\t" + self
.ret_type
.gen_text() + " py_" + self
.alias
+ "("
1513 for arg
in self
.args
:
1514 text
+= arg
.gen_listitem() + ", "
1515 if len(self
.args
) > 0:
1518 call_string
= "py_" + self
.alias
+ "("
1519 for arg
in self
.args
:
1520 call_string
+= arg
.gen_varname() + ", "
1521 if len(self
.args
) > 0:
1522 call_string
= call_string
[0:-2]
1526 text
+= "\n\t\t\tif(boost::python::override py_" + self
.alias
+ " = this->get_override(\"py_" + self
.alias
+ "\"))"
1527 text
+= "\n\t\t\t\t" + call_string
1528 text
+= "\n\t\t\telse"
1529 text
+= "\n\t\t\t\t" + self
.member_of
.name
+ "::" + call_string
1532 text
+= "\n\n\t\t" + self
.ret_type
.gen_text() + " default_py_" + self
.alias
+ "("
1533 for arg
in self
.args
:
1534 text
+= arg
.gen_listitem() + ", "
1535 if len(self
.args
) > 0:
1538 text
+= "\n\t\t\tthis->" + self
.member_of
.name
+ "::" + call_string
1543 def gen_boost_py(self
):
1546 if self
.member_of
== None:
1549 text
= "\n\t\t\t.def"
1550 if len(self
.args
) > -1:
1551 if self
.ret_type
.name
in known_containers
:
1552 text
+= "<" + known_containers
[self
.ret_type
.name
].typename
+ " "
1554 text
+= "<" + self
.ret_type
.name
+ " "
1555 if self
.member_of
== None or self
.is_static
:
1558 text
+= "(" + self
.member_of
.name
+ "::*)("
1560 text
+= a
.gen_listitem_hash() + ", "
1561 if len(self
.args
) > 0:
1562 text
= text
[0:-2] + ")>"
1566 if self
.is_operator
:
1567 text
+= "(\"" + wrappable_operators
[self
.name
.replace("operator","")] + "\""
1569 if self
.member_of
!= None and self
.member_of
.link_type
== link_types
.derive
and self
.is_virtual
:
1570 text
+= "(\"py_" + self
.alias
+ "\""
1572 text
+= "(\"" + self
.alias
+ "\""
1573 if self
.member_of
!= None:
1574 text
+= ", &" + self
.member_of
.name
+ "::"
1575 if self
.member_of
.link_type
== link_types
.derive
and self
.is_virtual
:
1576 text
+= "py_" + self
.alias
1577 text
+= ", &" + self
.member_of
.name
+ "Wrap::default_py_" + self
.alias
1583 text
+= ", " + "YOSYS_PYTHON::" + self
.alias
+ ");"
1588 wtype
= attr_types
.default
1590 containing_file
= None
1595 def from_string(str_def
, containing_file
, class_
, line_number
, namespace
):
1597 member
.orig_text
= str_def
1600 member
.containing_file
= containing_file
1601 member
.member_of
= class_
1602 member
.namespace
= namespace
1603 member
.is_const
= False
1605 if str.startswith(str_def
, "const "):
1606 member
.is_const
= True
1607 str_def
= str_def
[6:]
1609 if str_def
.count(" ") == 0:
1612 parts
= split_list(str_def
.strip(), " ")
1617 if part
in ["unsigned", "long", "short"]:
1618 prefix
+= part
+ " "
1627 member
.wtype
= WType
.from_string(prefix
+ parts
[0], containing_file
, line_number
)
1629 if member
.wtype
== None:
1633 for part
in parts
[2:]:
1634 str_def
= str_def
+ " " + part
1636 if str_def
.find("(") != -1 or str_def
.find(")") != -1 or str_def
.find("{") != -1 or str_def
.find("}") != -1:
1639 found
= str_def
.find(";")
1643 found_eq
= str_def
.find("=")
1647 member
.name
= str_def
[:found
]
1648 str_def
= str_def
[found
+1:]
1649 if member
.name
.find("*") == 0:
1650 member
.name
= member
.name
.replace("*", "")
1651 member
.wtype
.attr_type
= attr_types
.star
1652 if member
.name
.find("&&") == 0:
1653 member
.name
= member
.name
.replace("&&", "")
1654 member
.wtype
.attr_type
= attr_types
.ampamp
1655 if member
.name
.find("&") == 0:
1656 member
.name
= member
.name
.replace("&", "")
1657 member
.wtype
.attr_type
= attr_types
.amp
1659 if(len(str_def
.strip()) != 0):
1662 if len(member
.name
.split(",")) > 1:
1664 for name
in member
.name
.split(","):
1665 name
= name
.strip();
1666 member_list
.append(WMember())
1667 member_list
[-1].orig_text
= member
.orig_text
1668 member_list
[-1].wtype
= member
.wtype
1669 member_list
[-1].name
= name
1670 member_list
[-1].containing_file
= member
.containing_file
1671 member_list
[-1].member_of
= member
.member_of
1672 member_list
[-1].namespace
= member
.namespace
1673 member_list
[-1].is_const
= member
.is_const
1679 text
= "\n\t\t" + self
.wtype
.gen_text() + " get_var_py_" + self
.name
+ "();\n"
1682 if self
.wtype
.name
in classnames
:
1683 text
+= "\n\t\tvoid set_var_py_" + self
.name
+ "(" + self
.wtype
.gen_text() + " *rhs);\n"
1685 text
+= "\n\t\tvoid set_var_py_" + self
.name
+ "(" + self
.wtype
.gen_text() + " rhs);\n"
1689 text
= "\n\t" + self
.wtype
.gen_text() + " " + self
.member_of
.name
+"::get_var_py_" + self
.name
+ "()"
1690 text
+= "\n\t{\n\t\t"
1691 if self
.wtype
.attr_type
== attr_types
.star
:
1692 text
+= "if(this->get_cpp_obj()->" + self
.name
+ " == NULL)\n\t\t\t"
1693 text
+= "throw std::runtime_error(\"Member \\\"" + self
.name
+ "\\\" is NULL\");\n\t\t"
1694 if self
.wtype
.name
in known_containers
:
1695 text
+= self
.wtype
.gen_text_cpp()
1697 text
+= self
.wtype
.gen_text()
1699 if self
.wtype
.name
in classnames
or (self
.wtype
.name
in known_containers
and self
.wtype
.attr_type
== attr_types
.star
):
1702 if self
.wtype
.name
in classnames
:
1703 text
+= self
.wtype
.name
+ "::get_py_obj("
1704 if self
.wtype
.attr_type
!= attr_types
.star
:
1706 text
+= "this->get_cpp_obj()->" + self
.name
1707 if self
.wtype
.name
in classnames
:
1711 if self
.wtype
.name
in classnames
:
1712 text
+= "\n\t\treturn *ret_;"
1713 elif self
.wtype
.name
in known_containers
:
1714 text
+= known_containers
[self
.wtype
.name
].translate_cpp("ret_", self
.wtype
.cont
.args
, "\n\t\t", self
.wtype
.attr_type
== attr_types
.star
)
1715 text
+= "\n\t\treturn ret____tmp;"
1717 text
+= "\n\t\treturn ret_;"
1723 ret
= Attribute(self
.wtype
, "rhs");
1725 if self
.wtype
.name
in classnames
:
1726 text
+= "\n\tvoid " + self
.member_of
.name
+ "::set_var_py_" + self
.name
+ "(" + self
.wtype
.gen_text() + " *rhs)"
1728 text
+= "\n\tvoid " + self
.member_of
.name
+ "::set_var_py_" + self
.name
+ "(" + self
.wtype
.gen_text() + " rhs)"
1730 text
+= ret
.gen_translation()
1731 text
+= "\n\t\tthis->get_cpp_obj()->" + self
.name
+ " = " + ret
.gen_call() + ";"
1736 def gen_boost_py(self
):
1737 text
= "\n\t\t\t.add_property(\"" + self
.name
+ "\", &" + self
.member_of
.name
+ "::get_var_py_" + self
.name
1738 if not self
.is_const
:
1739 text
+= ", &" + self
.member_of
.name
+ "::set_var_py_" + self
.name
1745 wtype
= attr_types
.default
1747 containing_file
= None
1751 def from_string(str_def
, containing_file
, line_number
, namespace
):
1753 glbl
.orig_text
= str_def
1756 glbl
.containing_file
= containing_file
1757 glbl
.namespace
= namespace
1758 glbl
.is_const
= False
1760 if not str.startswith(str_def
, "extern"):
1762 str_def
= str_def
[7:]
1764 if str.startswith(str_def
, "const "):
1765 glbl
.is_const
= True
1766 str_def
= str_def
[6:]
1768 if str_def
.count(" ") == 0:
1771 parts
= split_list(str_def
.strip(), " ")
1776 if part
in ["unsigned", "long", "short"]:
1777 prefix
+= part
+ " "
1786 glbl
.wtype
= WType
.from_string(prefix
+ parts
[0], containing_file
, line_number
)
1788 if glbl
.wtype
== None:
1792 for part
in parts
[2:]:
1793 str_def
= str_def
+ " " + part
1795 if str_def
.find("(") != -1 or str_def
.find(")") != -1 or str_def
.find("{") != -1 or str_def
.find("}") != -1:
1798 found
= str_def
.find(";")
1802 found_eq
= str_def
.find("=")
1806 glbl
.name
= str_def
[:found
]
1807 str_def
= str_def
[found
+1:]
1808 if glbl
.name
.find("*") == 0:
1809 glbl
.name
= glbl
.name
.replace("*", "")
1810 glbl
.wtype
.attr_type
= attr_types
.star
1811 if glbl
.name
.find("&&") == 0:
1812 glbl
.name
= glbl
.name
.replace("&&", "")
1813 glbl
.wtype
.attr_type
= attr_types
.ampamp
1814 if glbl
.name
.find("&") == 0:
1815 glbl
.name
= glbl
.name
.replace("&", "")
1816 glbl
.wtype
.attr_type
= attr_types
.amp
1818 if(len(str_def
.strip()) != 0):
1821 if len(glbl
.name
.split(",")) > 1:
1823 for name
in glbl
.name
.split(","):
1824 name
= name
.strip();
1825 glbl_list
.append(WGlobal())
1826 glbl_list
[-1].orig_text
= glbl
.orig_text
1827 glbl_list
[-1].wtype
= glbl
.wtype
1828 glbl_list
[-1].name
= name
1829 glbl_list
[-1].containing_file
= glbl
.containing_file
1830 glbl_list
[-1].namespace
= glbl
.namespace
1831 glbl_list
[-1].is_const
= glbl
.is_const
1840 text
+= self
.wtype
.gen_text() + " get_var_py_" + self
.name
+ "()"
1841 text
+= "\n\t{\n\t\t"
1842 if self
.wtype
.attr_type
== attr_types
.star
:
1843 text
+= "if(" + self
.namespace
+ "::" + self
.name
+ " == NULL)\n\t\t\t"
1844 text
+= "throw std::runtime_error(\"" + self
.namespace
+ "::" + self
.name
+ " is NULL\");\n\t\t"
1845 if self
.wtype
.name
in known_containers
:
1846 text
+= self
.wtype
.gen_text_cpp()
1850 text
+= self
.wtype
.gen_text()
1852 if self
.wtype
.name
in classnames
or (self
.wtype
.name
in known_containers
and self
.wtype
.attr_type
== attr_types
.star
):
1855 if self
.wtype
.name
in classnames
:
1856 text
+= self
.wtype
.name
+ "::get_py_obj("
1857 if self
.wtype
.attr_type
!= attr_types
.star
:
1859 text
+= self
.namespace
+ "::" + self
.name
1860 if self
.wtype
.name
in classnames
:
1864 if self
.wtype
.name
in classnames
:
1865 text
+= "\n\t\treturn *ret_;"
1866 elif self
.wtype
.name
in known_containers
:
1867 text
+= known_containers
[self
.wtype
.name
].translate_cpp("ret_", self
.wtype
.cont
.args
, "\n\t\t", self
.wtype
.attr_type
== attr_types
.star
)
1868 text
+= "\n\t\treturn ret____tmp;"
1870 text
+= "\n\t\treturn ret_;"
1876 ret
= Attribute(self
.wtype
, "rhs");
1878 if self
.wtype
.name
in classnames
:
1879 text
+= "\n\tvoid set_var_py_" + self
.name
+ "(" + self
.wtype
.gen_text() + " *rhs)"
1881 text
+= "\n\tvoid set_var_py_" + self
.name
+ "(" + self
.wtype
.gen_text() + " rhs)"
1883 text
+= ret
.gen_translation()
1884 text
+= "\n\t\t" + self
.namespace
+ "::" + self
.name
+ " = " + ret
.gen_call() + ";"
1889 def gen_boost_py(self
):
1890 text
= "\n\t\t\t.add_static_property(\"" + self
.name
+ "\", &" + "YOSYS_PYTHON::get_var_py_" + self
.name
1891 if not self
.is_const
:
1892 text
+= ", &YOSYS_PYTHON::set_var_py_" + self
.name
1896 def concat_namespace(tuple_list
):
1897 if len(tuple_list
) == 0:
1900 for namespace
in tuple_list
:
1901 ret
+= "::" + namespace
[0]
1904 def calc_ident(text
):
1905 if len(text
) == 0 or text
[0] != ' ':
1907 return calc_ident(text
[1:]) + 1
1909 def assure_length(text
, length
, left
= False):
1910 if len(text
) > length
:
1911 return text
[:length
]
1913 return text
+ " "*(length
- len(text
))
1914 return " "*(length
- len(text
)) + text
1916 def parse_header(source
):
1917 debug("Parsing " + source
.name
+ ".pyh",1)
1918 source_file
= open(source
.name
+ ".pyh", "r")
1921 in_line
= source_file
.readline()
1927 source_text
.append(in_line
.replace("char *", "char_p ").replace("char* ", "char_p "))
1928 in_line
= source_file
.readline()
1934 private_segment
= False
1936 while i
< len(source_text
):
1937 line
= source_text
[i
].replace("YOSYS_NAMESPACE_BEGIN", " namespace YOSYS_NAMESPACE{").replace("YOSYS_NAMESPACE_END"," }")
1938 ugly_line
= unpretty_string(line
)
1940 # for anonymous unions, ignore union enclosure by skipping start line and replacing end line with new line
1941 if 'union {' in line
:
1943 while j
< len(source_text
):
1944 union_line
= source_text
[j
]
1945 if '};' in union_line
:
1946 source_text
[j
] = '\n'
1949 if j
!= len(source_text
):
1953 if str.startswith(ugly_line
, "namespace "):# and ugly_line.find("std") == -1 and ugly_line.find("__") == -1:
1954 namespace_name
= ugly_line
[10:].replace("{","").strip()
1955 namespaces
.append((namespace_name
, ugly_line
.count("{")))
1956 debug("-----NAMESPACE " + concat_namespace(namespaces
) + "-----",3)
1960 if len(namespaces
) != 0:
1961 namespaces
[-1] = (namespaces
[-1][0], namespaces
[-1][1] + ugly_line
.count("{") - ugly_line
.count("}"))
1962 if namespaces
[-1][1] == 0:
1963 debug("-----END NAMESPACE " + concat_namespace(namespaces
) + "-----",3)
1968 if class_
== None and (str.startswith(ugly_line
, "struct ") or str.startswith(ugly_line
, "class")) and ugly_line
.count(";") == 0:
1970 struct_name
= ugly_line
.split(" ")[1].split("::")[-1]
1971 impl_namespaces
= ugly_line
.split(" ")[1].split("::")[:-1]
1972 complete_namespace
= concat_namespace(namespaces
)
1973 for namespace
in impl_namespaces
:
1974 complete_namespace
+= "::" + namespace
1975 debug("\tFound " + struct_name
+ " in " + complete_namespace
,2)
1977 base_class_name
= None
1978 if len(ugly_line
.split(" : ")) > 1: # class is derived
1979 deriv_str
= ugly_line
.split(" : ")[1]
1980 if len(deriv_str
.split("::")) > 1: # namespace of base class is given
1981 base_class_name
= deriv_str
.split("::", 1)[1]
1983 base_class_name
= deriv_str
.split(" ")[0]
1984 debug("\t " + struct_name
+ " is derived from " + base_class_name
,2)
1985 base_class
= class_by_name(base_class_name
)
1987 class_
= (class_by_name(struct_name
), ugly_line
.count("{"))#calc_ident(line))
1988 if struct_name
in classnames
:
1989 class_
[0].namespace
= complete_namespace
1990 class_
[0].base_class
= base_class
1995 class_
= (class_
[0], class_
[1] + ugly_line
.count("{") - ugly_line
.count("}"))
1997 if class_
[0] == None:
1998 debug("\tExiting unknown class", 3)
2000 debug("\tExiting class " + class_
[0].name
, 3)
2002 private_segment
= False
2006 if class_
!= None and (line
.find("private:") != -1 or line
.find("protected:") != -1):
2007 private_segment
= True
2010 if class_
!= None and line
.find("public:") != -1:
2011 private_segment
= False
2017 if private_segment
and class_
!= None and class_
[0] != None:
2018 candidate
= WConstructor
.from_string(ugly_line
, source
.name
, class_
[0], i
, True)
2019 if candidate
!= None:
2020 debug("\t\tFound constructor of class \"" + class_
[0].name
+ "\" in namespace " + concat_namespace(namespaces
),2)
2021 class_
[0].found_constrs
.append(candidate
)
2025 if not private_segment
and (class_
== None or class_
[0] != None):
2027 candidate
= WFunction
.from_string(ugly_line
, source
.name
, class_
[0], i
, concat_namespace(namespaces
))
2029 candidate
= WFunction
.from_string(ugly_line
, source
.name
, None, i
, concat_namespace(namespaces
))
2030 if candidate
!= None and candidate
.name
.find("::") == -1:
2032 debug("\tFound unowned function \"" + candidate
.name
+ "\" in namespace " + concat_namespace(namespaces
),2)
2033 unowned_functions
.append(candidate
)
2035 debug("\t\tFound function \"" + candidate
.name
+ "\" of class \"" + class_
[0].name
+ "\" in namespace " + concat_namespace(namespaces
),2)
2036 class_
[0].found_funs
.append(candidate
)
2038 candidate
= WEnum
.from_string(ugly_line
, concat_namespace(namespaces
), i
)
2039 if candidate
!= None:
2040 enums
.append(candidate
)
2041 debug("\tFound enum \"" + candidate
.name
+ "\" in namespace " + concat_namespace(namespaces
),2)
2042 elif class_
!= None and class_
[1] == 1:
2043 candidate
= WConstructor
.from_string(ugly_line
, source
.name
, class_
[0], i
)
2044 if candidate
!= None:
2045 debug("\t\tFound constructor of class \"" + class_
[0].name
+ "\" in namespace " + concat_namespace(namespaces
),2)
2046 class_
[0].found_constrs
.append(candidate
)
2048 candidate
= WMember
.from_string(ugly_line
, source
.name
, class_
[0], i
, concat_namespace(namespaces
))
2049 if candidate
!= None:
2050 if type(candidate
) == list:
2052 debug("\t\tFound member \"" + c
.name
+ "\" of class \"" + class_
[0].name
+ "\" of type \"" + c
.wtype
.name
+ "\"", 2)
2053 class_
[0].found_vars
.extend(candidate
)
2055 debug("\t\tFound member \"" + candidate
.name
+ "\" of class \"" + class_
[0].name
+ "\" of type \"" + candidate
.wtype
.name
+ "\"", 2)
2056 class_
[0].found_vars
.append(candidate
)
2057 if candidate
== None and class_
== None:
2058 candidate
= WGlobal
.from_string(ugly_line
, source
.name
, i
, concat_namespace(namespaces
))
2059 if candidate
!= None:
2060 if type(candidate
) == list:
2063 debug("\tFound global \"" + c
.name
+ "\" in namespace " + concat_namespace(namespaces
), 2)
2065 glbls
.append(candidate
)
2066 debug("\tFound global \"" + candidate
.name
+ "\" in namespace " + concat_namespace(namespaces
), 2)
2069 line
= unpretty_string(line
)
2070 while candidate
== None and j
+1 < len(source_text
) and line
.count(';') <= 1 and line
.count("(") >= line
.count(")"):
2072 line
= line
+ "\n" + unpretty_string(source_text
[j
])
2074 candidate
= WFunction
.from_string(ugly_line
, source
.name
, class_
[0], i
, concat_namespace(namespaces
))
2076 candidate
= WFunction
.from_string(ugly_line
, source
.name
, None, i
, concat_namespace(namespaces
))
2077 if candidate
!= None and candidate
.name
.find("::") == -1:
2079 debug("\tFound unowned function \"" + candidate
.name
+ "\" in namespace " + concat_namespace(namespaces
),2)
2080 unowned_functions
.append(candidate
)
2082 debug("\t\tFound function \"" + candidate
.name
+ "\" of class \"" + class_
[0].name
+ "\" in namespace " + concat_namespace(namespaces
),2)
2083 class_
[0].found_funs
.append(candidate
)
2085 candidate
= WEnum
.from_string(line
, concat_namespace(namespaces
), i
)
2086 if candidate
!= None:
2087 enums
.append(candidate
)
2088 debug("\tFound enum \"" + candidate
.name
+ "\" in namespace " + concat_namespace(namespaces
),2)
2091 candidate
= WConstructor
.from_string(line
, source
.name
, class_
[0], i
)
2092 if candidate
!= None:
2093 debug("\t\tFound constructor of class \"" + class_
[0].name
+ "\" in namespace " + concat_namespace(namespaces
),2)
2094 class_
[0].found_constrs
.append(candidate
)
2097 candidate
= WGlobal
.from_string(line
, source
.name
, i
, concat_namespace(namespaces
))
2098 if candidate
!= None:
2099 if type(candidate
) == list:
2102 debug("\tFound global \"" + c
.name
+ "\" in namespace " + concat_namespace(namespaces
), 2)
2104 glbls
.append(candidate
)
2105 debug("\tFound global \"" + candidate
.name
+ "\" in namespace " + concat_namespace(namespaces
), 2)
2107 if candidate
!= None:
2110 line
= source_text
[i
].replace("YOSYS_NAMESPACE_BEGIN", " namespace YOSYS_NAMESPACE{").replace("YOSYS_NAMESPACE_END"," }")
2111 ugly_line
= unpretty_string(line
)
2112 if len(namespaces
) != 0:
2113 namespaces
[-1] = (namespaces
[-1][0], namespaces
[-1][1] + ugly_line
.count("{") - ugly_line
.count("}"))
2114 if namespaces
[-1][1] == 0:
2115 debug("-----END NAMESPACE " + concat_namespace(namespaces
) + "-----",3)
2118 class_
= (class_
[0] , class_
[1] + ugly_line
.count("{") - ugly_line
.count("}"))
2120 if class_
[0] == None:
2121 debug("\tExiting unknown class", 3)
2123 debug("\tExiting class " + class_
[0].name
, 3)
2125 private_segment
= False
2130 def debug(message
, level
):
2131 if level
<= debug
.debug_level
:
2134 def expand_function(f
):
2138 if arg
.default_value
!= None and (arg
.wtype
.name
.split(" ")[-1] in primitive_types
or arg
.wtype
.name
in enum_names
or (arg
.wtype
.name
in classnames
and arg
.default_value
== "nullptr")):
2139 fi
= copy
.deepcopy(f
)
2140 fi
.args
= copy
.deepcopy(arg_list
)
2142 arg_list
.append(arg
)
2146 def expand_functions():
2147 global unowned_functions
2149 for fun
in unowned_functions
:
2150 new_funs
.extend(expand_function(fun
))
2151 unowned_functions
= new_funs
2152 for source
in sources
:
2153 for class_
in source
.classes
:
2155 for fun
in class_
.found_funs
:
2156 new_funs
.extend(expand_function(fun
))
2157 class_
.found_funs
= new_funs
2159 def inherit_members():
2160 for source
in sources
:
2161 for class_
in source
.classes
:
2162 if class_
.base_class
:
2163 base_funs
= copy
.deepcopy(class_
.base_class
.found_funs
)
2164 for fun
in base_funs
:
2165 fun
.member_of
= class_
2166 fun
.namespace
= class_
.namespace
2167 base_vars
= copy
.deepcopy(class_
.base_class
.found_vars
)
2168 for var
in base_vars
:
2169 var
.member_of
= class_
2170 var
.namespace
= class_
.namespace
2171 class_
.found_funs
.extend(base_funs
)
2172 class_
.found_vars
.extend(base_vars
)
2174 def clean_duplicates():
2175 for source
in sources
:
2176 for class_
in source
.classes
:
2178 for fun
in class_
.found_funs
:
2179 if fun
.gen_decl_hash_py() in known_decls
:
2180 debug("Multiple declarations of " + fun
.gen_decl_hash_py(),3)
2181 other
= known_decls
[fun
.gen_decl_hash_py()]
2184 if fun
.gen_decl_hash_py() == other
.gen_decl_hash_py():
2185 fun
.duplicate
= True
2186 debug("Disabled \"" + fun
.gen_decl_hash_py() + "\"", 3)
2188 known_decls
[fun
.gen_decl_hash_py()] = fun
2190 for con
in class_
.found_constrs
:
2191 if con
.gen_decl_hash_py() in known_decls
:
2192 debug("Multiple declarations of " + con
.gen_decl_hash_py(),3)
2193 con
.duplicate
= True
2195 known_decls
.append(con
.gen_decl_hash_py())
2197 for fun
in unowned_functions
:
2198 if fun
.gen_decl_hash_py() in known_decls
:
2199 debug("Multiple declarations of " + fun
.gen_decl_hash_py(),3)
2200 fun
.duplicate
= True
2202 known_decls
.append(fun
.gen_decl_hash_py())
2204 def gen_wrappers(filename
, debug_level_
= 0):
2205 debug
.debug_level
= debug_level_
2206 for source
in sources
:
2207 parse_header(source
)
2215 col
= shutil
.get_terminal_size((80,20)).columns
2217 debug("-"*math
.floor((col
-7)/2)+"SUMMARY"+"-"*math
.ceil((col
-7)/2), 1)
2219 for source
in sources
:
2220 for class_
in source
.classes
:
2221 debug("Class " + assure_length(class_
.name
, len(max(classnames
, key
=len)), True) + " contains " + assure_length(str(len(class_
.found_vars
)), 3, False) + " member variables, "+ assure_length(str(len(class_
.found_funs
)), 3, False) + " methods and " + assure_length(str(len(class_
.found_constrs
)), 2, False) + " constructors", 1)
2222 if len(class_
.found_constrs
) == 0:
2223 class_
.found_constrs
.append(WConstructor(source
.name
, class_
))
2224 debug(str(len(unowned_functions
)) + " functions are unowned", 1)
2225 debug(str(len(unowned_functions
)) + " global variables", 1)
2227 debug("Enum " + assure_length(enum
.name
, len(max(enum_names
, key
=len)), True) + " contains " + assure_length(str(len(enum
.values
)), 2, False) + " values", 1)
2229 wrapper_file
= open(filename
, "w+")
2232 * yosys -- Yosys Open SYnthesis Suite
2234 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
2236 * Permission to use, copy, modify, and/or distribute this software for any
2237 * purpose with or without fee is hereby granted, provided that the above
2238 * copyright notice and this permission notice appear in all copies.
2240 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2241 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2242 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2243 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2244 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2245 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2246 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2248 * This is a generated file and can be overwritten by make
2253 for source
in sources
:
2254 wrapper_file
.write("#include \""+source
.name
+".h\"\n")
2255 wrapper_file
.write("""
2256 #include <boost/python/module.hpp>
2257 #include <boost/python/class.hpp>
2258 #include <boost/python/wrapper.hpp>
2259 #include <boost/python/call.hpp>
2260 #include <boost/python.hpp>
2261 #include <iosfwd> // std::streamsize
2263 #include <boost/iostreams/concepts.hpp> // boost::iostreams::sink
2264 #include <boost/iostreams/stream.hpp>
2265 USING_YOSYS_NAMESPACE
2267 namespace YOSYS_PYTHON {
2269 struct YosysStatics{};
2272 for source
in sources
:
2273 for wclass
in source
.classes
:
2274 wrapper_file
.write("\n\tstruct " + wclass
.name
+ ";")
2276 wrapper_file
.write("\n")
2278 for source
in sources
:
2279 for wclass
in source
.classes
:
2280 wrapper_file
.write(wclass
.gen_decl(source
.name
))
2282 wrapper_file
.write("\n")
2284 for source
in sources
:
2285 for wclass
in source
.classes
:
2286 wrapper_file
.write(wclass
.gen_funs(source
.name
))
2288 for fun
in unowned_functions
:
2289 wrapper_file
.write(fun
.gen_def())
2292 wrapper_file
.write(glbl
.gen_def())
2294 wrapper_file
.write(""" struct Initializer
2297 if(!Yosys::yosys_already_setup())
2299 Yosys::log_streams.push_back(&std::cout);
2300 Yosys::log_error_stderr = true;
2301 Yosys::yosys_setup();
2305 Initializer(Initializer const &) {}
2308 Yosys::yosys_shutdown();
2313 /// source: https://stackoverflow.com/questions/26033781/converting-python-io-object-to-stdostream-when-using-boostpython?noredirect=1&lq=1
2314 /// @brief Type that implements the Boost.IOStream's Sink and Flushable
2315 /// concept for writing data to Python object that support:
2316 /// n = object.write(str) # n = None or bytes written
2317 /// object.flush() # if flush exists, then it is callable
2318 class PythonOutputDevice
2322 // This class models both the Sink and Flushable concepts.
2324 : boost::iostreams::sink_tag,
2325 boost::iostreams::flushable_tag
2329 PythonOutputDevice(boost::python::object object)
2336 typedef char char_type;
2338 std::streamsize write(const char* buffer, std::streamsize buffer_size)
2340 namespace python = boost::python;
2341 // Copy the buffer to a python string.
2342 python::str data(buffer, buffer_size);
2344 // Invoke write on the python object, passing in the data. The following
2345 // is equivalent to:
2346 // n = object_.write(data)
2347 python::extract<std::streamsize> bytes_written(
2348 object_.attr("write")(data));
2350 // Per the Sink concept, return the number of bytes written. If the
2351 // Python return value provides a numeric result, then use it. Otherwise,
2352 // such as the case of a File object, use the buffer_size.
2353 return bytes_written.check()
2358 // Flushable concept.
2363 // If flush exists, then call it.
2364 boost::python::object flush = object_.attr("flush");
2365 if (!flush.is_none())
2370 // Always return true. If an error occurs, an exception should be thrown.
2375 boost::python::object object_;
2378 /// @brief Use an auxiliary function to adapt the legacy function.
2379 void log_to_stream(boost::python::object object)
2381 // Create an ostream that delegates to the python object.
2382 boost::iostreams::stream<PythonOutputDevice>* output = new boost::iostreams::stream<PythonOutputDevice>(object);
2383 Yosys::log_streams.insert(Yosys::log_streams.begin(), output);
2387 BOOST_PYTHON_MODULE(libyosys)
2389 using namespace boost::python;
2391 class_<Initializer>("Initializer");
2392 scope().attr("_hidden") = new Initializer();
2394 def("log_to_stream", &log_to_stream);
2398 wrapper_file
.write(enum
.gen_boost_py())
2400 for source
in sources
:
2401 for wclass
in source
.classes
:
2402 wrapper_file
.write(wclass
.gen_boost_py())
2404 for fun
in unowned_functions
:
2405 wrapper_file
.write(fun
.gen_boost_py())
2407 wrapper_file
.write("\n\n\t\tclass_<YosysStatics>(\"Yosys\")\n")
2409 wrapper_file
.write(glbl
.gen_boost_py())
2410 wrapper_file
.write("\t\t;\n")
2412 wrapper_file
.write("\n\t}\n}\n#endif")
2414 def print_includes():
2415 for source
in sources
:
2416 print(source
.name
+ ".pyh")