48a54b64306ac6bf7d5c84d72bb1fdfc59d14b8f
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 cont
.args
.append(candidate
)
259 #Translators between Python and C++ containers
263 typename
= "DefaultType"
264 orig_name
= "DefaultCpp"
267 def gen_type(c
, types
):
268 return "\nImplement a function that outputs the c++ type of this container here\n"
271 def translate(c
, varname
, types
, prefix
):
272 return "\nImplement a function translating a python container to a c++ container here\n"
275 def translate_cpp(c
, varname
, types
, prefix
, ref
):
276 return "\nImplement a function translating a c++ container to a python container here\n"
278 #Translates list-types (vector, pool, set), that only differ in their name and
279 #the name of the insertion function
280 class PythonListTranslator(Translator
):
281 typename
= "boost::python::list"
282 insert_name
= "Default"
284 #generate the c++ type string
286 def gen_type(c
, types
):
287 text
= c
.orig_name
+ "<"
288 if types
[0].name
in primitive_types
:
289 text
+= types
[0].name
290 elif types
[0].name
in known_containers
:
291 text
+= known_containers
[types
[0].name
].gen_type(types
[0].cont
.args
)
293 text
+= class_by_name(types
[0].name
).namespace
+ "::" + types
[0].name
294 if types
[0].attr_type
== attr_types
.star
:
299 #Generate C++ code to translate from a boost::python::list
301 def translate(c
, varname
, types
, prefix
):
302 text
= prefix
+ c
.gen_type(types
) + " " + varname
+ "___tmp;"
303 cntr_name
= "cntr_" + str(Translator
.tmp_cntr
)
304 Translator
.tmp_cntr
= Translator
.tmp_cntr
+ 1
305 text
+= prefix
+ "for(int " + cntr_name
+ " = 0; " + cntr_name
+ " < len(" + varname
+ "); " + cntr_name
+ "++)"
307 tmp_name
= "tmp_" + str(Translator
.tmp_cntr
)
308 Translator
.tmp_cntr
= Translator
.tmp_cntr
+ 1
309 if types
[0].name
in known_containers
:
310 text
+= prefix
+ "\t" + known_containers
[types
[0].name
].typename
+ " " + tmp_name
+ " = boost::python::extract<" + known_containers
[types
[0].name
].typename
+ ">(" + varname
+ "[" + cntr_name
+ "]);"
311 text
+= known_containers
[types
[0].name
].translate(tmp_name
, types
[0].cont
.args
, prefix
+"\t")
312 tmp_name
= tmp_name
+ "___tmp"
313 text
+= prefix
+ "\t" + varname
+ "___tmp." + c
.insert_name
+ "(" + tmp_name
+ ");"
314 elif types
[0].name
in classnames
:
315 text
+= prefix
+ "\t" + types
[0].name
+ "* " + tmp_name
+ " = boost::python::extract<" + types
[0].name
+ "*>(" + varname
+ "[" + cntr_name
+ "]);"
316 if types
[0].attr_type
== attr_types
.star
:
317 text
+= prefix
+ "\t" + varname
+ "___tmp." + c
.insert_name
+ "(" + tmp_name
+ "->get_cpp_obj());"
319 text
+= prefix
+ "\t" + varname
+ "___tmp." + c
.insert_name
+ "(*" + tmp_name
+ "->get_cpp_obj());"
321 text
+= prefix
+ "\t" + types
[0].name
+ " " + tmp_name
+ " = boost::python::extract<" + types
[0].name
+ ">(" + varname
+ "[" + cntr_name
+ "]);"
322 text
+= prefix
+ "\t" + varname
+ "___tmp." + c
.insert_name
+ "(" + tmp_name
+ ");"
326 #Generate C++ code to translate to a boost::python::list
328 def translate_cpp(c
, varname
, types
, prefix
, ref
):
329 text
= prefix
+ c
.typename
+ " " + varname
+ "___tmp;"
330 tmp_name
= "tmp_" + str(Translator
.tmp_cntr
)
331 Translator
.tmp_cntr
= Translator
.tmp_cntr
+ 1
333 text
+= prefix
+ "for(auto " + tmp_name
+ " : *" + varname
+ ")"
335 text
+= prefix
+ "for(auto " + tmp_name
+ " : " + varname
+ ")"
337 if types
[0].name
in classnames
:
338 if types
[0].attr_type
== attr_types
.star
:
339 text
+= prefix
+ "\t" + varname
+ "___tmp.append(" + types
[0].name
+ "::get_py_obj(" + tmp_name
+ "));"
341 text
+= prefix
+ "\t" + varname
+ "___tmp.append(*" + types
[0].name
+ "::get_py_obj(&" + tmp_name
+ "));"
342 elif types
[0].name
in known_containers
:
343 text
+= known_containers
[types
[0].name
].translate_cpp(tmp_name
, types
[0].cont
.args
, prefix
+ "\t", types
[0].attr_type
== attr_types
.star
)
344 text
+= prefix
+ "\t" + varname
+ "___tmp.append(" + tmp_name
+ "___tmp);"
346 text
+= prefix
+ "\t" + varname
+ "___tmp.append(" + tmp_name
+ ");"
350 #Sub-type for std::set
351 class SetTranslator(PythonListTranslator
):
352 insert_name
= "insert"
353 orig_name
= "std::set"
355 #Sub-type for std::vector
356 class VectorTranslator(PythonListTranslator
):
357 insert_name
= "push_back"
358 orig_name
= "std::vector"
361 class PoolTranslator(PythonListTranslator
):
362 insert_name
= "insert"
365 #Translates dict-types (dict, std::map), that only differ in their name and
366 #the name of the insertion function
367 class PythonDictTranslator(Translator
):
368 typename
= "boost::python::dict"
369 insert_name
= "Default"
372 def gen_type(c
, types
):
373 text
= c
.orig_name
+ "<"
374 if types
[0].name
in primitive_types
:
375 text
+= types
[0].name
376 elif types
[0].name
in known_containers
:
377 text
+= known_containers
[types
[0].name
].gen_type(types
[0].cont
.args
)
379 text
+= class_by_name(types
[0].name
).namespace
+ "::" + types
[0].name
380 if types
[0].attr_type
== attr_types
.star
:
383 if types
[1].name
in primitive_types
:
384 text
+= types
[1].name
385 elif types
[1].name
in known_containers
:
386 text
+= known_containers
[types
[1].name
].gen_type(types
[1].cont
.args
)
388 text
+= class_by_name(types
[1].name
).namespace
+ "::" + types
[1].name
389 if types
[1].attr_type
== attr_types
.star
:
394 #Generate c++ code to translate from a boost::python::dict
396 def translate(c
, varname
, types
, prefix
):
397 text
= prefix
+ c
.gen_type(types
) + " " + varname
+ "___tmp;"
398 text
+= prefix
+ "boost::python::list " + varname
+ "_keylist = " + varname
+ ".keys();"
399 cntr_name
= "cntr_" + str(Translator
.tmp_cntr
)
400 Translator
.tmp_cntr
= Translator
.tmp_cntr
+ 1
401 text
+= prefix
+ "for(int " + cntr_name
+ " = 0; " + cntr_name
+ " < len(" + varname
+ "_keylist); " + cntr_name
+ "++)"
403 key_tmp_name
= "key_tmp_" + str(Translator
.tmp_cntr
)
404 val_tmp_name
= "val_tmp_" + str(Translator
.tmp_cntr
)
405 Translator
.tmp_cntr
= Translator
.tmp_cntr
+ 1
407 if types
[0].name
in known_containers
:
408 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
+ " ]);"
409 text
+= known_containers
[types
[0].name
].translate(key_tmp_name
, types
[0].cont
.args
, prefix
+"\t")
410 key_tmp_name
= key_tmp_name
+ "___tmp"
411 elif types
[0].name
in classnames
:
412 text
+= prefix
+ "\t" + types
[0].name
+ "* " + key_tmp_name
+ " = boost::python::extract<" + types
[0].name
+ "*>(" + varname
+ "_keylist[ " + cntr_name
+ " ]);"
414 text
+= prefix
+ "\t" + types
[0].name
+ " " + key_tmp_name
+ " = boost::python::extract<" + types
[0].name
+ ">(" + varname
+ "_keylist[ " + cntr_name
+ " ]);"
416 if types
[1].name
in known_containers
:
417 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
+ " ]]);"
418 text
+= known_containers
[types
[1].name
].translate(val_tmp_name
, types
[1].cont
.args
, prefix
+"\t")
419 val_tmp_name
= val_tmp_name
+ "___tmp"
420 elif types
[1].name
in classnames
:
421 text
+= prefix
+ "\t" + types
[1].name
+ "* " + val_tmp_name
+ " = boost::python::extract<" + types
[1].name
+ "*>(" + varname
+ "[" + varname
+ "_keylist[ " + cntr_name
+ " ]]);"
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" + varname
+ "___tmp." + c
.insert_name
+ "(std::pair<" + types
[0].gen_text_cpp() + ", " + types
[1].gen_text_cpp() + ">("
427 if types
[0].name
not in classnames
:
430 if types
[0].attr_type
!= attr_types
.star
:
432 text
+= key_tmp_name
+ "->get_cpp_obj()"
435 if types
[1].name
not in classnames
:
438 if types
[1].attr_type
!= attr_types
.star
:
440 text
+= val_tmp_name
+ "->get_cpp_obj()"
441 text
+= "));\n" + prefix
+ "}"
444 #Generate c++ code to translate to a boost::python::dict
446 def translate_cpp(c
, varname
, types
, prefix
, ref
):
447 text
= prefix
+ c
.typename
+ " " + varname
+ "___tmp;"
448 tmp_name
= "tmp_" + str(Translator
.tmp_cntr
)
449 Translator
.tmp_cntr
= Translator
.tmp_cntr
+ 1
451 text
+= prefix
+ "for(auto " + tmp_name
+ " : *" + varname
+ ")"
453 text
+= prefix
+ "for(auto " + tmp_name
+ " : " + varname
+ ")"
455 if types
[1].name
in known_containers
:
456 text
+= prefix
+ "\tauto " + tmp_name
+ "_second = " + tmp_name
+ ".second;"
457 text
+= known_containers
[types
[1].name
].translate_cpp(tmp_name
+ "_second", types
[1].cont
.args
, prefix
+ "\t", types
[1].attr_type
== attr_types
.star
)
459 if types
[0].name
in classnames
:
460 text
+= prefix
+ "\t" + varname
+ "___tmp[" + types
[0].name
+ "::get_py_obj(" + tmp_name
+ ".first)] = "
461 elif types
[0].name
not in known_containers
:
462 text
+= prefix
+ "\t" + varname
+ "___tmp[" + tmp_name
+ ".first] = "
464 if types
[1].name
in classnames
:
465 if types
[1].attr_type
== attr_types
.star
:
466 text
+= types
[1].name
+ "::get_py_obj(" + tmp_name
+ ".second);"
468 text
+= "*" + types
[1].name
+ "::get_py_obj(&" + tmp_name
+ ".second);"
469 elif types
[1].name
in known_containers
:
470 text
+= tmp_name
+ "_second___tmp;"
472 text
+= tmp_name
+ ".second;"
477 class DictTranslator(PythonDictTranslator
):
478 insert_name
= "insert"
481 #Sub_type for std::map
482 class MapTranslator(PythonDictTranslator
):
483 insert_name
= "insert"
484 orig_name
= "std::map"
486 #Translator for std::pair. Derived from PythonDictTranslator because the
487 #gen_type function is the same (because both have two template parameters)
488 class TupleTranslator(PythonDictTranslator
):
489 typename
= "boost::python::tuple"
490 orig_name
= "std::pair"
492 #Generate c++ code to translate from a boost::python::tuple
494 def translate(c
, varname
, types
, prefix
):
495 text
= prefix
+ types
[0].name
+ " " + varname
+ "___tmp_0 = boost::python::extract<" + types
[0].name
+ ">(" + varname
+ "[0]);"
496 text
+= prefix
+ types
[1].name
+ " " + varname
+ "___tmp_1 = boost::python::extract<" + types
[1].name
+ ">(" + varname
+ "[1]);"
497 text
+= prefix
+ TupleTranslator
.gen_type(types
) + " " + varname
+ "___tmp("
498 if types
[0].name
.split(" ")[-1] in primitive_types
:
499 text
+= varname
+ "___tmp_0, "
501 text
+= varname
+ "___tmp_0.get_cpp_obj(), "
502 if types
[1].name
.split(" ")[-1] in primitive_types
:
503 text
+= varname
+ "___tmp_1);"
505 text
+= varname
+ "___tmp_1.get_cpp_obj());"
508 #Generate c++ code to translate to a boost::python::tuple
510 def translate_cpp(c
, varname
, types
, prefix
, ref
):
511 # if the tuple is a pair of SigSpecs (aka SigSig), then we need
512 # to call get_py_obj() on each item in the tuple
513 if types
[0].name
in classnames
:
514 first_var
= types
[0].name
+ "::get_py_obj(" + varname
+ ".first)"
516 first_var
= varname
+ ".first"
517 if types
[1].name
in classnames
:
518 second_var
= types
[1].name
+ "::get_py_obj(" + varname
+ ".second)"
520 second_var
= varname
+ ".second"
521 text
= prefix
+ TupleTranslator
.typename
+ " " + varname
+ "___tmp = boost::python::make_tuple(" + first_var
+ ", " + second_var
+ ");"
524 #Associate the Translators with their c++ type
526 "std::set" : SetTranslator
,
527 "std::vector" : VectorTranslator
,
528 "pool" : PoolTranslator
,
529 "dict" : DictTranslator
,
530 "std::pair" : TupleTranslator
,
531 "std::map" : MapTranslator
542 def __init__(self
, wtype
, varname
, is_const
= False, default_value
= None):
544 self
.varname
= varname
545 self
.is_const
= is_const
546 self
.default_value
= None
547 self
.container
= None
550 def from_string(str_def
, containing_file
, line_number
):
554 arg
= Attribute(None, None)
559 arg
.default_value
= None
561 if str.startswith(str_def
, "const "):
563 str_def
= str_def
[6:]
564 if str.startswith(str_def
, "unsigned "):
566 str_def
= str_def
[9:]
567 while str.startswith(str_def
, "long "):
568 prefix
= "long " + prefix
569 str_def
= str_def
[5:]
570 while str.startswith(str_def
, "short "):
571 prefix
= "short " + prefix
572 str_def
= str_def
[6:]
574 if str_def
.find("<") != -1 and str_def
.find("<") < str_def
.find(" "):
575 closing
= find_closing(str_def
[str_def
.find("<"):], "<", ">") + str_def
.find("<") + 1
576 arg
.wtype
= WType
.from_string(str_def
[:closing
].strip(), containing_file
, line_number
)
577 str_def
= str_def
[closing
+1:]
579 if str_def
.count(" ") > 0:
580 arg
.wtype
= WType
.from_string(prefix
+ str_def
[:str_def
.find(" ")].strip(), containing_file
, line_number
)
581 str_def
= str_def
[str_def
.find(" ")+1:]
583 arg
.wtype
= WType
.from_string(prefix
+ str_def
.strip(), containing_file
, line_number
)
587 if arg
.wtype
== None:
589 if str_def
.count("=") == 0:
590 arg
.varname
= str_def
.strip()
591 if arg
.varname
.find(" ") > 0:
594 arg
.varname
= str_def
[:str_def
.find("=")].strip()
595 if arg
.varname
.find(" ") > 0:
597 str_def
= str_def
[str_def
.find("=")+1:].strip()
598 arg
.default_value
= str_def
[arg
.varname
.find("=")+1:].strip()
599 if len(arg
.varname
) == 0:
602 if arg
.varname
[0] == '*':
603 arg
.wtype
.attr_type
= attr_types
.star
604 arg
.varname
= arg
.varname
[1:]
605 elif arg
.varname
[0] == '&':
606 if arg
.wtype
.attr_type
!= attr_types
.default
:
608 if arg
.varname
[1] == '&':
609 arg
.wtype
.attr_type
= attr_types
.ampamp
610 arg
.varname
= arg
.varname
[2:]
612 arg
.wtype
.attr_type
= attr_types
.amp
613 arg
.varname
= arg
.varname
[1:]
616 #Generates the varname. If the attribute has no name in the header file,
618 def gen_varname(self
):
619 if self
.varname
!= None:
621 if self
.wtype
.name
== "void":
624 self
.pos
= Attribute
.pos_counter
625 Attribute
.pos_counter
= Attribute
.pos_counter
+ 1
626 return "gen_varname_" + str(self
.pos
)
628 #Generates the text for the function headers with wrapper types
629 def gen_listitem(self
):
633 if self
.wtype
.name
in classnames
:
634 return prefix
+ self
.wtype
.name
+ "* " + self
.gen_varname()
635 if self
.wtype
.name
in known_containers
:
636 return prefix
+ known_containers
[self
.wtype
.name
].typename
+ " " + self
.gen_varname()
637 return prefix
+ self
.wtype
.name
+ " " + self
.gen_varname()
639 #Generates the test for the function headers with c++ types
640 def gen_listitem_cpp(self
):
645 if self
.wtype
.attr_type
== attr_types
.star
:
647 elif self
.wtype
.attr_type
== attr_types
.amp
:
649 elif self
.wtype
.attr_type
== attr_types
.ampamp
:
651 if self
.wtype
.name
in known_containers
:
652 return prefix
+ known_containers
[self
.wtype
.name
].gen_type(self
.wtype
.cont
.args
) + " " + infix
+ self
.gen_varname()
653 if self
.wtype
.name
in classnames
:
654 return prefix
+ class_by_name(self
.wtype
.name
).namespace
+ "::" + self
.wtype
.name
+ " " + infix
+ self
.gen_varname()
655 return prefix
+ self
.wtype
.name
+ " " + infix
+ self
.gen_varname()
657 #Generates the listitem withtout the varname, so the signature can be
659 def gen_listitem_hash(self
):
663 if self
.wtype
.name
in classnames
:
664 return prefix
+ self
.wtype
.name
+ "* "
665 if self
.wtype
.name
in known_containers
:
666 return known_containers
[self
.wtype
.name
].typename
667 return prefix
+ self
.wtype
.name
669 #Generate Translation code for the attribute
670 def gen_translation(self
):
671 if self
.wtype
.name
in known_containers
:
672 return known_containers
[self
.wtype
.name
].translate(self
.gen_varname(), self
.wtype
.cont
.args
, "\n\t\t")
675 #Generate Translation code from c++ for the attribute
676 def gen_translation_cpp(self
):
677 if self
.wtype
.name
in known_containers
:
678 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
)
681 #Generate Text for the call
683 ret
= self
.gen_varname()
684 if self
.wtype
.name
in known_containers
:
685 if self
.wtype
.attr_type
== attr_types
.star
:
686 return "&" + ret
+ "___tmp"
687 return ret
+ "___tmp"
688 if self
.wtype
.name
in classnames
:
689 if self
.wtype
.attr_type
!= attr_types
.star
:
691 return ret
+ "->get_cpp_obj()"
692 if self
.wtype
.name
== "char *" and self
.gen_varname() in ["format", "fmt"]:
693 return "\"%s\", " + self
.gen_varname()
694 if self
.wtype
.attr_type
== attr_types
.star
:
698 def gen_call_cpp(self
):
699 ret
= self
.gen_varname()
700 if self
.wtype
.name
.split(" ")[-1] in primitive_types
or self
.wtype
.name
in enum_names
:
701 if self
.wtype
.attr_type
== attr_types
.star
:
704 if self
.wtype
.name
not in classnames
:
705 if self
.wtype
.attr_type
== attr_types
.star
:
706 return "&" + ret
+ "___tmp"
707 return ret
+ "___tmp"
708 if self
.wtype
.attr_type
!= attr_types
.star
:
710 return self
.wtype
.name
+ "::get_py_obj(" + self
.gen_varname() + ")"
712 #Generate cleanup code
713 def gen_cleanup(self
):
714 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
):
716 return "\n\t\tdelete " + self
.gen_varname() + "___tmp;"
730 def __init__(self
, name
, link_type
, id_
, string_id
= None, hash_id
= None, needs_clone
= False):
732 self
.namespace
= None
733 self
.link_type
= link_type
735 self
.string_id
= string_id
736 self
.hash_id
= hash_id
737 self
.needs_clone
= needs_clone
740 self
.found_constrs
= []
742 def printable_constrs(self
):
744 for con
in self
.found_constrs
:
745 if not con
.protected
:
749 def gen_decl(self
, filename
):
750 long_name
= self
.namespace
+ "::" + self
.name
752 text
= "\n\t// WRAPPED from " + filename
753 text
+= "\n\tstruct " + self
.name
754 if self
.link_type
== link_types
.derive
:
755 text
+= " : public " + self
.namespace
+ "::" + self
.name
758 if self
.link_type
!= link_types
.derive
:
760 text
+= "\t\t" + long_name
+ "* ref_obj;\n"
762 if self
.link_type
== link_types
.ref_copy
or self
.link_type
== link_types
.pointer
:
763 text
+= "\n\t\t" + long_name
+ "* get_cpp_obj() const\n\t\t{\n\t\t\treturn ref_obj;\n\t\t}\n"
764 elif self
.link_type
== link_types
.global_list
:
765 text
+= "\t\t" + self
.id_
.wtype
.name
+ " " + self
.id_
.varname
+ ";\n"
766 text
+= "\n\t\t" + long_name
+ "* get_cpp_obj() const\n\t\t{"
767 text
+= "\n\t\t\t" + long_name
+ "* ret = " + long_name
+ "::get_all_" + self
.name
.lower() + "s()->at(this->" + self
.id_
.varname
+ ");"
768 text
+= "\n\t\t\tif(ret != NULL && ret == this->ref_obj)"
769 text
+= "\n\t\t\t\treturn ret;"
770 text
+= "\n\t\t\tthrow std::runtime_error(\"" + self
.name
+ "'s c++ object does not exist anymore.\");"
771 text
+= "\n\t\t\treturn NULL;"
774 #if self.link_type != link_types.pointer:
775 text
+= "\n\t\tstatic " + self
.name
+ "* get_py_obj(" + long_name
+ "* ref)\n\t\t{"
776 text
+= "\n\t\t\t" + self
.name
+ "* ret = (" + self
.name
+ "*)malloc(sizeof(" + self
.name
+ "));"
777 if self
.link_type
== link_types
.pointer
:
778 text
+= "\n\t\t\tret->ref_obj = ref;"
779 if self
.link_type
== link_types
.ref_copy
:
781 text
+= "\n\t\t\tret->ref_obj = ref->clone();"
783 text
+= "\n\t\t\tret->ref_obj = new "+long_name
+"(*ref);"
784 if self
.link_type
== link_types
.global_list
:
785 text
+= "\n\t\t\tret->ref_obj = ref;"
786 text
+= "\n\t\t\tret->" + self
.id_
.varname
+ " = ret->ref_obj->" + self
.id_
.varname
+ ";"
787 text
+= "\n\t\t\treturn ret;"
790 if self
.link_type
== link_types
.ref_copy
:
791 text
+= "\n\t\tstatic " + self
.name
+ "* get_py_obj(" + long_name
+ " ref)\n\t\t{"
792 text
+= "\n\t\t\t" + self
.name
+ "* ret = (" + self
.name
+ "*)malloc(sizeof(" + self
.name
+ "));"
794 text
+= "\n\t\t\tret->ref_obj = ref.clone();"
796 text
+= "\n\t\t\tret->ref_obj = new "+long_name
+"(ref);"
797 text
+= "\n\t\t\treturn ret;"
800 for con
in self
.found_constrs
:
801 text
+= con
.gen_decl()
802 for var
in self
.found_vars
:
803 text
+= var
.gen_decl()
804 for fun
in self
.found_funs
:
805 text
+= fun
.gen_decl()
808 if self
.link_type
== link_types
.derive
:
810 for fun
in self
.found_funs
:
811 if fun
.name
in duplicates
:
813 duplicates
[fun
.name
].gen_alias()
815 duplicates
[fun
.name
] = fun
817 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"
818 text
+= "\n\t\tstatic " + self
.name
+ "* get_py_obj(" + long_name
+ "* ref)\n\t\t{"
819 text
+= "\n\t\t\treturn (" + self
.name
+ "*)ref;"
822 for con
in self
.found_constrs
:
823 text
+= con
.gen_decl_derive()
824 for var
in self
.found_vars
:
825 text
+= var
.gen_decl()
826 for fun
in self
.found_funs
:
827 text
+= fun
.gen_decl_virtual()
829 if self
.hash_id
!= None:
830 text
+= "\n\t\tunsigned int get_hash_py()"
832 text
+= "\n\t\t\treturn get_cpp_obj()->" + self
.hash_id
+ ";"
837 if self
.link_type
== link_types
.derive
:
838 text
+= "\n\tstruct " + self
.name
+ "Wrap : " + self
.name
+ ", boost::python::wrapper<" + self
.name
+ ">"
841 for con
in self
.found_constrs
:
842 text
+= con
.gen_decl_wrapperclass()
843 for fun
in self
.found_funs
:
844 text
+= fun
.gen_default_impl()
848 text
+= "\n\tstd::ostream &operator<<(std::ostream &ostr, const " + self
.name
+ " &ref)"
850 text
+= "\n\t\tostr << \"" + self
.name
851 if self
.string_id
!= None:
853 text
+= " << ref.get_cpp_obj()->" + self
.string_id
854 text
+= " << \"\\\"\""
856 text
+= " at \" << ref.get_cpp_obj()"
858 text
+= "\n\t\treturn ostr;"
864 def gen_funs(self
, filename
):
866 if self
.link_type
!= link_types
.derive
:
867 for con
in self
.found_constrs
:
868 text
+= con
.gen_def()
869 for var
in self
.found_vars
:
870 text
+= var
.gen_def()
871 for fun
in self
.found_funs
:
872 text
+= fun
.gen_def()
874 for var
in self
.found_vars
:
875 text
+= var
.gen_def()
876 for fun
in self
.found_funs
:
877 text
+= fun
.gen_def_virtual()
880 def gen_boost_py(self
):
881 text
= "\n\t\tclass_<" + self
.name
882 if self
.link_type
== link_types
.derive
:
883 text
+= "Wrap, boost::noncopyable"
884 text
+= ">(\"" + self
.name
+ "\""
885 if self
.printable_constrs() == 0 or not self
.contains_default_constr():
888 text
+= "\n\t\t\t.def(boost::python::self_ns::str(boost::python::self_ns::self))"
889 text
+= "\n\t\t\t.def(boost::python::self_ns::repr(boost::python::self_ns::self))"
890 for con
in self
.found_constrs
:
891 text
+= con
.gen_boost_py()
892 for var
in self
.found_vars
:
893 text
+= var
.gen_boost_py()
895 for fun
in self
.found_funs
:
896 text
+= fun
.gen_boost_py()
897 if fun
.is_static
and fun
.alias
not in static_funs
:
898 static_funs
.append(fun
.alias
)
899 for fun
in static_funs
:
900 text
+= "\n\t\t\t.staticmethod(\"" + fun
+ "\")"
902 if self
.hash_id
!= None:
903 text
+= "\n\t\t\t.def(\"__hash__\", &" + self
.name
+ "::get_hash_py)"
904 text
+= "\n\t\t\t;\n"
907 def contains_default_constr(self
):
908 for c
in self
.found_constrs
:
913 #CONFIGURE HEADER-FILES TO BE PARSED AND CLASSES EXPECTED IN THEM HERE
916 Source("kernel/celltypes",[
917 WClass("CellType", link_types
.pointer
, None, None, "type.hash()", True),
918 WClass("CellTypes", link_types
.pointer
, None, None, None, True)
921 Source("kernel/consteval",[
922 WClass("ConstEval", link_types
.pointer
, None, None, None, True)
925 Source("kernel/log",[]),
926 Source("kernel/register",[
927 WClass("Pass", link_types
.derive
, None, None, None, True),
930 Source("kernel/rtlil",[
931 WClass("IdString", link_types
.ref_copy
, None, "str()", "hash()"),
932 WClass("Const", link_types
.ref_copy
, None, "as_string()", "hash()"),
933 WClass("AttrObject", link_types
.ref_copy
, None, None, None),
934 WClass("Selection", link_types
.ref_copy
, None, None, None),
935 WClass("Monitor", link_types
.derive
, None, None, None),
936 WClass("CaseRule",link_types
.ref_copy
, None, None, None, True),
937 WClass("SwitchRule",link_types
.ref_copy
, None, None, None, True),
938 WClass("SyncRule", link_types
.ref_copy
, None, None, None, True),
939 WClass("Process", link_types
.ref_copy
, None, "name.c_str()", "name.hash()"),
940 WClass("SigChunk", link_types
.ref_copy
, None, None, None),
941 WClass("SigBit", link_types
.ref_copy
, None, None, "hash()"),
942 WClass("SigSpec", link_types
.ref_copy
, None, None, "hash()"),
943 WClass("Cell", link_types
.global_list
, Attribute(WType("unsigned int"), "hashidx_"), "name.c_str()", "hash()"),
944 WClass("Wire", link_types
.global_list
, Attribute(WType("unsigned int"), "hashidx_"), "name.c_str()", "hash()"),
945 WClass("Memory", link_types
.global_list
, Attribute(WType("unsigned int"), "hashidx_"), "name.c_str()", "hash()"),
946 WClass("Module", link_types
.global_list
, Attribute(WType("unsigned int"), "hashidx_"), "name.c_str()", "hash()"),
947 WClass("Design", link_types
.global_list
, Attribute(WType("unsigned int"), "hashidx_"), "hashidx_", "hash()")
950 #Source("kernel/satgen",[
953 #Source("libs/ezsat/ezsat",[
956 #Source("libs/ezsat/ezminisat",[
959 Source("kernel/sigtools",[
960 WClass("SigMap", link_types
.pointer
, None, None, None, True)
963 Source("kernel/yosys",[
966 Source("kernel/cost",[])
969 blacklist_methods
= ["YOSYS_NAMESPACE::Pass::run_register", "YOSYS_NAMESPACE::Module::Pow", "YOSYS_NAMESPACE::Module::Bu0", "YOSYS_NAMESPACE::CaseRule::optimize"]
971 enum_names
= ["State","SyncType","ConstFlags"]
973 enums
= [] #Do not edit
975 unowned_functions
= []
978 for source
in sources
:
979 for wclass
in source
.classes
:
980 classnames
.append(wclass
.name
)
982 def class_by_name(name
):
983 for source
in sources
:
984 for wclass
in source
.classes
:
985 if wclass
.name
== name
:
989 def enum_by_name(name
):
995 def find_closing(text
, open_tok
, close_tok
):
996 if text
.find(open_tok
) == -1 or text
.find(close_tok
) <= text
.find(open_tok
):
997 return text
.find(close_tok
)
998 return text
.find(close_tok
) + find_closing(text
[text
.find(close_tok
)+1:], open_tok
, close_tok
) + 1
1000 def unpretty_string(s
):
1002 while s
.find(" ") != -1:
1003 s
= s
.replace(" "," ")
1004 while s
.find("\t") != -1:
1005 s
= s
.replace("\t"," ")
1006 s
= s
.replace(" (","(")
1014 def from_string(str_def
, namespace
, line_number
):
1015 str_def
= str_def
.strip()
1016 if not str.startswith(str_def
, "enum "):
1018 if str_def
.count(";") != 1:
1020 str_def
= str_def
[5:]
1022 split
= str_def
.split(":")
1023 if(len(split
) != 2):
1025 enum
.name
= split
[0].strip()
1026 if enum
.name
not in enum_names
:
1029 if str_def
.count("{") != str_def
.count("}") != 1:
1031 if len(str_def
) < str_def
.find("}")+2 or str_def
[str_def
.find("}")+1] != ';':
1033 str_def
= str_def
.split("{")[-1].split("}")[0]
1035 for val
in str_def
.split(','):
1036 enum
.values
.append(val
.strip().split('=')[0].strip())
1037 enum
.namespace
= namespace
1040 def gen_boost_py(self
):
1041 text
= "\n\t\tenum_<" + self
.namespace
+ "::" + self
.name
+ ">(\"" + self
.name
+ "\")\n"
1042 for value
in self
.values
:
1043 text
+= "\t\t\t.value(\"" + value
+ "\"," + self
.namespace
+ "::" + value
+ ")\n"
1048 ret
= "Enum " + self
.namespace
+ "::" + self
.name
+ "(\n"
1049 for val
in self
.values
:
1050 ret
= ret
+ "\t" + val
+ "\n"
1054 return __str__(self
)
1059 containing_file
= None
1064 def __init__(self
, containing_file
, class_
):
1065 self
.orig_text
= "Auto generated default constructor"
1067 self
.containing_file
= containing_file
1068 self
.member_of
= class_
1069 self
.protected
= False
1071 def from_string(str_def
, containing_file
, class_
, line_number
, protected
= False):
1074 if str_def
.count("delete;") > 0:
1076 con
= WConstructor(containing_file
, class_
)
1077 con
.orig_text
= str_def
1079 con
.duplicate
= False
1080 con
.protected
= protected
1081 if not str.startswith(str_def
, class_
.name
+ "("):
1083 str_def
= str_def
[len(class_
.name
)+1:]
1084 found
= find_closing(str_def
, "(", ")")
1087 str_def
= str_def
[0:found
].strip()
1088 if len(str_def
) == 0:
1090 for arg
in split_list(str_def
, ","):
1091 parsed
= Attribute
.from_string(arg
.strip(), containing_file
, line_number
)
1094 con
.args
.append(parsed
)
1098 if self
.duplicate
or self
.protected
:
1100 text
= "\n\t\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1101 text
+= "\n\t\t" + self
.member_of
.name
+ "("
1102 for arg
in self
.args
:
1103 text
+= arg
.gen_listitem() + ", "
1104 if len(self
.args
) > 0:
1109 def gen_decl_derive(self
):
1110 if self
.duplicate
or self
.protected
:
1112 text
= "\n\t\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1113 text
+= "\n\t\t" + self
.member_of
.name
+ "("
1114 for arg
in self
.args
:
1115 text
+= arg
.gen_listitem() + ", "
1116 if len(self
.args
) > 0:
1119 if len(self
.args
) == 0:
1122 text
+= self
.member_of
.namespace
+ "::" + self
.member_of
.name
+ "("
1123 for arg
in self
.args
:
1124 text
+= arg
.gen_call() + ", "
1125 if len(self
.args
) > 0:
1130 def gen_decl_wrapperclass(self
):
1131 if self
.duplicate
or self
.protected
:
1133 text
= "\n\t\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1134 text
+= "\n\t\t" + self
.member_of
.name
+ "Wrap("
1135 for arg
in self
.args
:
1136 text
+= arg
.gen_listitem() + ", "
1137 if len(self
.args
) > 0:
1140 if len(self
.args
) == 0:
1143 text
+= self
.member_of
.name
+ "("
1144 for arg
in self
.args
:
1145 text
+= arg
.gen_call() + ", "
1146 if len(self
.args
) > 0:
1151 def gen_decl_hash_py(self
):
1152 text
= self
.member_of
.name
+ "("
1153 for arg
in self
.args
:
1154 text
+= arg
.gen_listitem_hash() + ", "
1155 if len(self
.args
) > 0:
1161 if self
.duplicate
or self
.protected
:
1163 text
= "\n\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1164 text
+= "\n\t" + self
.member_of
.name
+ "::" + self
.member_of
.name
+ "("
1165 for arg
in self
.args
:
1166 text
+= arg
.gen_listitem() + ", "
1167 if len(self
.args
) > 0:
1170 for arg
in self
.args
:
1171 text
+= arg
.gen_translation()
1172 if self
.member_of
.link_type
!= link_types
.derive
:
1173 text
+= "\n\t\tthis->ref_obj = new " + self
.member_of
.namespace
+ "::" + self
.member_of
.name
+ "("
1174 for arg
in self
.args
:
1175 text
+= arg
.gen_call() + ", "
1176 if len(self
.args
) > 0:
1178 if self
.member_of
.link_type
!= link_types
.derive
:
1180 if self
.member_of
.link_type
== link_types
.global_list
:
1181 text
+= "\n\t\tthis->" + self
.member_of
.id_
.varname
+ " = this->ref_obj->" + self
.member_of
.id_
.varname
+ ";"
1182 for arg
in self
.args
:
1183 text
+= arg
.gen_cleanup()
1187 def gen_boost_py(self
):
1188 if self
.duplicate
or self
.protected
or len(self
.args
) == 0:
1190 text
= "\n\t\t\t.def(init"
1193 text
+= a
.gen_listitem_hash() + ", "
1194 text
= text
[0:-2] + ">())"
1202 ret_attr_type
= attr_types
.default
1208 containing_file
= None
1213 def from_string(str_def
, containing_file
, class_
, line_number
, namespace
):
1214 if str_def
.count("delete;") > 0:
1217 func
.is_static
= False
1218 func
.is_inline
= False
1219 func
.is_virtual
= False
1220 func
.ret_attr_type
= attr_types
.default
1221 func
.is_operator
= False
1222 func
.member_of
= None
1223 func
.orig_text
= str_def
1225 func
.containing_file
= containing_file
1226 func
.member_of
= class_
1227 func
.duplicate
= False
1228 func
.namespace
= namespace
1229 str_def
= str_def
.replace("operator ","operator")
1230 if str.startswith(str_def
, "static "):
1231 func
.is_static
= True
1232 str_def
= str_def
[7:]
1234 func
.is_static
= False
1235 if str.startswith(str_def
, "inline "):
1236 func
.is_inline
= True
1237 str_def
= str_def
[7:]
1239 func
.is_inline
= False
1240 if str.startswith(str_def
, "virtual "):
1241 func
.is_virtual
= True
1242 str_def
= str_def
[8:]
1244 func
.is_virtual
= False
1246 if str_def
.count(" ") == 0:
1249 parts
= split_list(str_def
.strip(), " ")
1254 if part
in ["unsigned", "long", "short"]:
1255 prefix
+= part
+ " "
1264 func
.ret_type
= WType
.from_string(prefix
+ parts
[0], containing_file
, line_number
)
1266 if func
.ret_type
== None:
1270 for part
in parts
[2:]:
1271 str_def
= str_def
+ " " + part
1273 found
= str_def
.find("(")
1274 if found
== -1 or (str_def
.find(" ") != -1 and found
> str_def
.find(" ")):
1276 func
.name
= str_def
[:found
]
1277 str_def
= str_def
[found
:]
1278 if func
.name
.find("operator") != -1 and str.startswith(str_def
, "()("):
1280 str_def
= str_def
[2:]
1281 str_def
= str_def
[1:]
1282 if func
.name
.find("operator") != -1:
1283 func
.is_operator
= True
1284 if func
.name
.find("*") == 0:
1285 func
.name
= func
.name
.replace("*", "")
1286 func
.ret_type
.attr_type
= attr_types
.star
1287 if func
.name
.find("&&") == 0:
1288 func
.name
= func
.name
.replace("&&", "")
1289 func
.ret_type
.attr_type
= attr_types
.ampamp
1290 if func
.name
.find("&") == 0:
1291 func
.name
= func
.name
.replace("&", "")
1292 func
.ret_type
.attr_type
= attr_types
.amp
1294 found
= find_closing(str_def
, "(", ")")
1297 str_def
= str_def
[0:found
]
1298 if func
.name
in blacklist_methods
:
1300 if func
.namespace
!= None and func
.namespace
!= "":
1301 if (func
.namespace
+ "::" + func
.name
) in blacklist_methods
:
1303 if func
.member_of
!= None:
1304 if (func
.namespace
+ "::" + func
.member_of
.name
+ "::" + func
.name
) in blacklist_methods
:
1306 if func
.is_operator
and func
.name
.replace(" ","").replace("operator","").split("::")[-1] not in wrappable_operators
:
1309 testname
= func
.name
1310 if func
.is_operator
:
1311 testname
= testname
[:testname
.find("operator")]
1312 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:
1315 func
.alias
= func
.name
1316 if func
.name
in keyword_aliases
:
1317 func
.alias
= keyword_aliases
[func
.name
]
1318 str_def
= str_def
[:found
].strip()
1319 if(len(str_def
) == 0):
1321 for arg
in split_list(str_def
, ","):
1322 if arg
.strip() == "...":
1324 parsed
= Attribute
.from_string(arg
.strip(), containing_file
, line_number
)
1327 func
.args
.append(parsed
)
1330 def gen_alias(self
):
1331 self
.alias
= self
.name
1332 for arg
in self
.args
:
1333 self
.alias
+= "__" + arg
.wtype
.gen_text_cpp().replace("::", "_").replace("<","_").replace(">","_").replace(" ","").replace("*","").replace(",","")
1338 text
= "\n\t\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1342 text
+= self
.ret_type
.gen_text() + " " + self
.alias
+ "("
1343 for arg
in self
.args
:
1344 text
+= arg
.gen_listitem()
1346 if len(self
.args
) > 0:
1351 def gen_decl_virtual(self
):
1354 if not self
.is_virtual
:
1355 return self
.gen_decl()
1356 text
= "\n\t\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1357 text
+= "\n\t\tvirtual "
1360 text
+= self
.ret_type
.gen_text() + " py_" + self
.alias
+ "("
1361 for arg
in self
.args
:
1362 text
+= arg
.gen_listitem()
1364 if len(self
.args
) > 0:
1367 if len(self
.args
) == 0:
1371 for arg
in self
.args
:
1372 text
+= "\n\t\t\t(void)" + arg
.gen_varname() + ";"
1374 text
+= "\n\t\tvirtual "
1377 text
+= self
.ret_type
.gen_text() + " " + self
.name
+ "("
1378 for arg
in self
.args
:
1379 text
+= arg
.gen_listitem_cpp()
1381 if len(self
.args
) > 0:
1383 text
+= ") YS_OVERRIDE;\n"
1386 def gen_decl_hash_py(self
):
1387 text
= self
.ret_type
.gen_text() + " " + self
.alias
+ "("
1388 for arg
in self
.args
:
1389 text
+= arg
.gen_listitem_hash() + ", "
1390 if len(self
.args
) > 0:
1398 text
= "\n\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1399 text
+= "\n\t" + self
.ret_type
.gen_text() + " "
1400 if self
.member_of
!= None:
1401 text
+= self
.member_of
.name
+ "::"
1402 text
+= self
.alias
+ "("
1403 for arg
in self
.args
:
1404 text
+= arg
.gen_listitem()
1406 if len(self
.args
) > 0:
1409 for arg
in self
.args
:
1410 text
+= arg
.gen_translation()
1412 if self
.ret_type
.name
!= "void":
1413 if self
.ret_type
.name
in known_containers
:
1414 text
+= self
.ret_type
.gen_text_cpp()
1416 text
+= self
.ret_type
.gen_text()
1417 if self
.ret_type
.name
in classnames
or (self
.ret_type
.name
in known_containers
and self
.ret_type
.attr_type
== attr_types
.star
):
1420 if self
.ret_type
.name
in classnames
:
1421 text
+= self
.ret_type
.name
+ "::get_py_obj("
1422 if self
.member_of
== None:
1423 text
+= "::" + self
.namespace
+ "::" + self
.alias
+ "("
1424 elif self
.is_static
:
1425 text
+= self
.member_of
.namespace
+ "::" + self
.member_of
.name
+ "::" + self
.name
+ "("
1427 text
+= "this->get_cpp_obj()->" + self
.name
+ "("
1428 for arg
in self
.args
:
1429 text
+= arg
.gen_call() + ", "
1430 if len(self
.args
) > 0:
1432 if self
.ret_type
.name
in classnames
:
1435 for arg
in self
.args
:
1436 text
+= arg
.gen_cleanup()
1437 if self
.ret_type
.name
!= "void":
1438 if self
.ret_type
.name
in classnames
:
1439 text
+= "\n\t\treturn *ret_;"
1440 elif self
.ret_type
.name
in known_containers
:
1441 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
)
1442 text
+= "\n\t\treturn ret____tmp;"
1444 text
+= "\n\t\treturn ret_;"
1448 def gen_def_virtual(self
):
1451 if not self
.is_virtual
:
1452 return self
.gen_def()
1453 text
= "\n\t// WRAPPED from \"" + self
.orig_text
.replace("\n"," ") + "\" in " + self
.containing_file
1457 text
+= self
.ret_type
.gen_text() + " " + self
.member_of
.name
+ "::" + self
.name
+ "("
1458 for arg
in self
.args
:
1459 text
+= arg
.gen_listitem_cpp()
1461 if len(self
.args
) > 0:
1464 for arg
in self
.args
:
1465 text
+= arg
.gen_translation_cpp()
1467 if self
.member_of
== None:
1468 text
+= "::" + self
.namespace
+ "::" + self
.alias
+ "("
1469 elif self
.is_static
:
1470 text
+= self
.member_of
.namespace
+ "::" + self
.member_of
.name
+ "::" + self
.name
+ "("
1472 text
+= "py_" + self
.alias
+ "("
1473 for arg
in self
.args
:
1474 text
+= arg
.gen_call_cpp() + ", "
1475 if len(self
.args
) > 0:
1477 if self
.ret_type
.name
in classnames
:
1480 for arg
in self
.args
:
1481 text
+= arg
.gen_cleanup()
1485 def gen_default_impl(self
):
1488 if not self
.is_virtual
:
1490 text
= "\n\n\t\t" + self
.ret_type
.gen_text() + " py_" + self
.alias
+ "("
1491 for arg
in self
.args
:
1492 text
+= arg
.gen_listitem() + ", "
1493 if len(self
.args
) > 0:
1496 call_string
= "py_" + self
.alias
+ "("
1497 for arg
in self
.args
:
1498 call_string
+= arg
.gen_varname() + ", "
1499 if len(self
.args
) > 0:
1500 call_string
= call_string
[0:-2]
1504 text
+= "\n\t\t\tif(boost::python::override py_" + self
.alias
+ " = this->get_override(\"py_" + self
.alias
+ "\"))"
1505 text
+= "\n\t\t\t\t" + call_string
1506 text
+= "\n\t\t\telse"
1507 text
+= "\n\t\t\t\t" + self
.member_of
.name
+ "::" + call_string
1510 text
+= "\n\n\t\t" + self
.ret_type
.gen_text() + " default_py_" + self
.alias
+ "("
1511 for arg
in self
.args
:
1512 text
+= arg
.gen_listitem() + ", "
1513 if len(self
.args
) > 0:
1516 text
+= "\n\t\t\tthis->" + self
.member_of
.name
+ "::" + call_string
1521 def gen_boost_py(self
):
1524 if self
.member_of
== None:
1527 text
= "\n\t\t\t.def"
1528 if len(self
.args
) > -1:
1529 if self
.ret_type
.name
in known_containers
:
1530 text
+= "<" + known_containers
[self
.ret_type
.name
].typename
+ " "
1532 text
+= "<" + self
.ret_type
.name
+ " "
1533 if self
.member_of
== None or self
.is_static
:
1536 text
+= "(" + self
.member_of
.name
+ "::*)("
1538 text
+= a
.gen_listitem_hash() + ", "
1539 if len(self
.args
) > 0:
1540 text
= text
[0:-2] + ")>"
1544 if self
.is_operator
:
1545 text
+= "(\"" + wrappable_operators
[self
.name
.replace("operator","")] + "\""
1547 if self
.member_of
!= None and self
.member_of
.link_type
== link_types
.derive
and self
.is_virtual
:
1548 text
+= "(\"py_" + self
.alias
+ "\""
1550 text
+= "(\"" + self
.alias
+ "\""
1551 if self
.member_of
!= None:
1552 text
+= ", &" + self
.member_of
.name
+ "::"
1553 if self
.member_of
.link_type
== link_types
.derive
and self
.is_virtual
:
1554 text
+= "py_" + self
.alias
1555 text
+= ", &" + self
.member_of
.name
+ "Wrap::default_py_" + self
.alias
1561 text
+= ", " + "YOSYS_PYTHON::" + self
.alias
+ ");"
1566 wtype
= attr_types
.default
1568 containing_file
= None
1573 def from_string(str_def
, containing_file
, class_
, line_number
, namespace
):
1575 member
.orig_text
= str_def
1578 member
.containing_file
= containing_file
1579 member
.member_of
= class_
1580 member
.namespace
= namespace
1581 member
.is_const
= False
1583 if str.startswith(str_def
, "const "):
1584 member
.is_const
= True
1585 str_def
= str_def
[6:]
1587 if str_def
.count(" ") == 0:
1590 parts
= split_list(str_def
.strip(), " ")
1595 if part
in ["unsigned", "long", "short"]:
1596 prefix
+= part
+ " "
1605 member
.wtype
= WType
.from_string(prefix
+ parts
[0], containing_file
, line_number
)
1607 if member
.wtype
== None:
1611 for part
in parts
[2:]:
1612 str_def
= str_def
+ " " + part
1614 if str_def
.find("(") != -1 or str_def
.find(")") != -1 or str_def
.find("{") != -1 or str_def
.find("}") != -1:
1617 found
= str_def
.find(";")
1621 found_eq
= str_def
.find("=")
1625 member
.name
= str_def
[:found
]
1626 str_def
= str_def
[found
+1:]
1627 if member
.name
.find("*") == 0:
1628 member
.name
= member
.name
.replace("*", "")
1629 member
.wtype
.attr_type
= attr_types
.star
1630 if member
.name
.find("&&") == 0:
1631 member
.name
= member
.name
.replace("&&", "")
1632 member
.wtype
.attr_type
= attr_types
.ampamp
1633 if member
.name
.find("&") == 0:
1634 member
.name
= member
.name
.replace("&", "")
1635 member
.wtype
.attr_type
= attr_types
.amp
1637 if(len(str_def
.strip()) != 0):
1640 if len(member
.name
.split(",")) > 1:
1642 for name
in member
.name
.split(","):
1643 name
= name
.strip();
1644 member_list
.append(WMember())
1645 member_list
[-1].orig_text
= member
.orig_text
1646 member_list
[-1].wtype
= member
.wtype
1647 member_list
[-1].name
= name
1648 member_list
[-1].containing_file
= member
.containing_file
1649 member_list
[-1].member_of
= member
.member_of
1650 member_list
[-1].namespace
= member
.namespace
1651 member_list
[-1].is_const
= member
.is_const
1657 text
= "\n\t\t" + self
.wtype
.gen_text() + " get_var_py_" + self
.name
+ "();\n"
1660 if self
.wtype
.name
in classnames
:
1661 text
+= "\n\t\tvoid set_var_py_" + self
.name
+ "(" + self
.wtype
.gen_text() + " *rhs);\n"
1663 text
+= "\n\t\tvoid set_var_py_" + self
.name
+ "(" + self
.wtype
.gen_text() + " rhs);\n"
1667 text
= "\n\t" + self
.wtype
.gen_text() + " " + self
.member_of
.name
+"::get_var_py_" + self
.name
+ "()"
1668 text
+= "\n\t{\n\t\t"
1669 if self
.wtype
.attr_type
== attr_types
.star
:
1670 text
+= "if(this->get_cpp_obj()->" + self
.name
+ " == NULL)\n\t\t\t"
1671 text
+= "throw std::runtime_error(\"Member \\\"" + self
.name
+ "\\\" is NULL\");\n\t\t"
1672 if self
.wtype
.name
in known_containers
:
1673 text
+= self
.wtype
.gen_text_cpp()
1675 text
+= self
.wtype
.gen_text()
1677 if self
.wtype
.name
in classnames
or (self
.wtype
.name
in known_containers
and self
.wtype
.attr_type
== attr_types
.star
):
1680 if self
.wtype
.name
in classnames
:
1681 text
+= self
.wtype
.name
+ "::get_py_obj("
1682 if self
.wtype
.attr_type
!= attr_types
.star
:
1684 text
+= "this->get_cpp_obj()->" + self
.name
1685 if self
.wtype
.name
in classnames
:
1689 if self
.wtype
.name
in classnames
:
1690 text
+= "\n\t\treturn *ret_;"
1691 elif self
.wtype
.name
in known_containers
:
1692 text
+= known_containers
[self
.wtype
.name
].translate_cpp("ret_", self
.wtype
.cont
.args
, "\n\t\t", self
.wtype
.attr_type
== attr_types
.star
)
1693 text
+= "\n\t\treturn ret____tmp;"
1695 text
+= "\n\t\treturn ret_;"
1701 ret
= Attribute(self
.wtype
, "rhs");
1703 if self
.wtype
.name
in classnames
:
1704 text
+= "\n\tvoid " + self
.member_of
.name
+ "::set_var_py_" + self
.name
+ "(" + self
.wtype
.gen_text() + " *rhs)"
1706 text
+= "\n\tvoid " + self
.member_of
.name
+ "::set_var_py_" + self
.name
+ "(" + self
.wtype
.gen_text() + " rhs)"
1708 text
+= ret
.gen_translation()
1709 text
+= "\n\t\tthis->get_cpp_obj()->" + self
.name
+ " = " + ret
.gen_call() + ";"
1714 def gen_boost_py(self
):
1715 text
= "\n\t\t\t.add_property(\"" + self
.name
+ "\", &" + self
.member_of
.name
+ "::get_var_py_" + self
.name
1716 if not self
.is_const
:
1717 text
+= ", &" + self
.member_of
.name
+ "::set_var_py_" + self
.name
1721 def concat_namespace(tuple_list
):
1722 if len(tuple_list
) == 0:
1725 for namespace
in tuple_list
:
1726 ret
+= "::" + namespace
[0]
1729 def calc_ident(text
):
1730 if len(text
) == 0 or text
[0] != ' ':
1732 return calc_ident(text
[1:]) + 1
1734 def assure_length(text
, length
, left
= False):
1735 if len(text
) > length
:
1736 return text
[:length
]
1738 return text
+ " "*(length
- len(text
))
1739 return " "*(length
- len(text
)) + text
1741 def parse_header(source
):
1742 debug("Parsing " + source
.name
+ ".pyh",1)
1743 source_file
= open(source
.name
+ ".pyh", "r")
1746 in_line
= source_file
.readline()
1752 source_text
.append(in_line
.replace("char *", "char_p ").replace("char* ", "char_p "))
1753 in_line
= source_file
.readline()
1759 private_segment
= False
1761 while i
< len(source_text
):
1762 line
= source_text
[i
].replace("YOSYS_NAMESPACE_BEGIN", " namespace YOSYS_NAMESPACE{").replace("YOSYS_NAMESPACE_END"," }")
1763 ugly_line
= unpretty_string(line
)
1765 if str.startswith(ugly_line
, "namespace "):# and ugly_line.find("std") == -1 and ugly_line.find("__") == -1:
1766 namespace_name
= ugly_line
[10:].replace("{","").strip()
1767 namespaces
.append((namespace_name
, ugly_line
.count("{")))
1768 debug("-----NAMESPACE " + concat_namespace(namespaces
) + "-----",3)
1772 if len(namespaces
) != 0:
1773 namespaces
[-1] = (namespaces
[-1][0], namespaces
[-1][1] + ugly_line
.count("{") - ugly_line
.count("}"))
1774 if namespaces
[-1][1] == 0:
1775 debug("-----END NAMESPACE " + concat_namespace(namespaces
) + "-----",3)
1780 if class_
== None and (str.startswith(ugly_line
, "struct ") or str.startswith(ugly_line
, "class")) and ugly_line
.count(";") == 0:
1782 struct_name
= ugly_line
.split(" ")[1].split("::")[-1]
1783 impl_namespaces
= ugly_line
.split(" ")[1].split("::")[:-1]
1784 complete_namespace
= concat_namespace(namespaces
)
1785 for namespace
in impl_namespaces
:
1786 complete_namespace
+= "::" + namespace
1787 debug("\tFound " + struct_name
+ " in " + complete_namespace
,2)
1788 class_
= (class_by_name(struct_name
), ugly_line
.count("{"))#calc_ident(line))
1789 if struct_name
in classnames
:
1790 class_
[0].namespace
= complete_namespace
1795 class_
= (class_
[0], class_
[1] + ugly_line
.count("{") - ugly_line
.count("}"))
1797 if class_
[0] == None:
1798 debug("\tExiting unknown class", 3)
1800 debug("\tExiting class " + class_
[0].name
, 3)
1802 private_segment
= False
1806 if class_
!= None and (line
.find("private:") != -1 or line
.find("protected:") != -1):
1807 private_segment
= True
1810 if class_
!= None and line
.find("public:") != -1:
1811 private_segment
= False
1817 if private_segment
and class_
!= None and class_
[0] != None:
1818 candidate
= WConstructor
.from_string(ugly_line
, source
.name
, class_
[0], i
, True)
1819 if candidate
!= None:
1820 debug("\t\tFound constructor of class \"" + class_
[0].name
+ "\" in namespace " + concat_namespace(namespaces
),2)
1821 class_
[0].found_constrs
.append(candidate
)
1825 if not private_segment
and (class_
== None or class_
[0] != None):
1827 candidate
= WFunction
.from_string(ugly_line
, source
.name
, class_
[0], i
, concat_namespace(namespaces
))
1829 candidate
= WFunction
.from_string(ugly_line
, source
.name
, None, i
, concat_namespace(namespaces
))
1830 if candidate
!= None and candidate
.name
.find("::") == -1:
1832 debug("\tFound unowned function \"" + candidate
.name
+ "\" in namespace " + concat_namespace(namespaces
),2)
1833 unowned_functions
.append(candidate
)
1835 debug("\t\tFound function \"" + candidate
.name
+ "\" of class \"" + class_
[0].name
+ "\" in namespace " + concat_namespace(namespaces
),2)
1836 class_
[0].found_funs
.append(candidate
)
1838 candidate
= WEnum
.from_string(ugly_line
, concat_namespace(namespaces
), i
)
1839 if candidate
!= None:
1840 enums
.append(candidate
)
1841 debug("\tFound enum \"" + candidate
.name
+ "\" in namespace " + concat_namespace(namespaces
),2)
1842 elif class_
!= None and class_
[1] == 1:
1843 candidate
= WConstructor
.from_string(ugly_line
, source
.name
, class_
[0], i
)
1844 if candidate
!= None:
1845 debug("\t\tFound constructor of class \"" + class_
[0].name
+ "\" in namespace " + concat_namespace(namespaces
),2)
1846 class_
[0].found_constrs
.append(candidate
)
1848 candidate
= WMember
.from_string(ugly_line
, source
.name
, class_
[0], i
, concat_namespace(namespaces
))
1849 if candidate
!= None:
1850 if type(candidate
) == list:
1852 debug("\t\tFound member \"" + c
.name
+ "\" of class \"" + class_
[0].name
+ "\" of type \"" + c
.wtype
.name
+ "\"", 2)
1853 class_
[0].found_vars
.extend(candidate
)
1855 debug("\t\tFound member \"" + candidate
.name
+ "\" of class \"" + class_
[0].name
+ "\" of type \"" + candidate
.wtype
.name
+ "\"", 2)
1856 class_
[0].found_vars
.append(candidate
)
1859 line
= unpretty_string(line
)
1860 while candidate
== None and j
+1 < len(source_text
) and line
.count(';') <= 1 and line
.count("(") >= line
.count(")"):
1862 line
= line
+ "\n" + unpretty_string(source_text
[j
])
1864 candidate
= WFunction
.from_string(ugly_line
, source
.name
, class_
[0], i
, concat_namespace(namespaces
))
1866 candidate
= WFunction
.from_string(ugly_line
, source
.name
, None, i
, concat_namespace(namespaces
))
1867 if candidate
!= None and candidate
.name
.find("::") == -1:
1869 debug("\tFound unowned function \"" + candidate
.name
+ "\" in namespace " + concat_namespace(namespaces
),2)
1870 unowned_functions
.append(candidate
)
1872 debug("\t\tFound function \"" + candidate
.name
+ "\" of class \"" + class_
[0].name
+ "\" in namespace " + concat_namespace(namespaces
),2)
1873 class_
[0].found_funs
.append(candidate
)
1875 candidate
= WEnum
.from_string(line
, concat_namespace(namespaces
), i
)
1876 if candidate
!= None:
1877 enums
.append(candidate
)
1878 debug("\tFound enum \"" + candidate
.name
+ "\" in namespace " + concat_namespace(namespaces
),2)
1881 candidate
= WConstructor
.from_string(line
, source
.name
, class_
[0], i
)
1882 if candidate
!= None:
1883 debug("\t\tFound constructor of class \"" + class_
[0].name
+ "\" in namespace " + concat_namespace(namespaces
),2)
1884 class_
[0].found_constrs
.append(candidate
)
1886 if candidate
!= None:
1889 line
= source_text
[i
].replace("YOSYS_NAMESPACE_BEGIN", " namespace YOSYS_NAMESPACE{").replace("YOSYS_NAMESPACE_END"," }")
1890 ugly_line
= unpretty_string(line
)
1891 if len(namespaces
) != 0:
1892 namespaces
[-1] = (namespaces
[-1][0], namespaces
[-1][1] + ugly_line
.count("{") - ugly_line
.count("}"))
1893 if namespaces
[-1][1] == 0:
1894 debug("-----END NAMESPACE " + concat_namespace(namespaces
) + "-----",3)
1897 class_
= (class_
[0] , class_
[1] + ugly_line
.count("{") - ugly_line
.count("}"))
1899 if class_
[0] == None:
1900 debug("\tExiting unknown class", 3)
1902 debug("\tExiting class " + class_
[0].name
, 3)
1904 private_segment
= False
1909 def debug(message
, level
):
1910 if level
<= debug
.debug_level
:
1913 def expand_function(f
):
1917 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")):
1918 fi
= copy
.deepcopy(f
)
1919 fi
.args
= copy
.deepcopy(arg_list
)
1921 arg_list
.append(arg
)
1925 def expand_functions():
1926 global unowned_functions
1928 for fun
in unowned_functions
:
1929 new_funs
.extend(expand_function(fun
))
1930 unowned_functions
= new_funs
1931 for source
in sources
:
1932 for class_
in source
.classes
:
1934 for fun
in class_
.found_funs
:
1935 new_funs
.extend(expand_function(fun
))
1936 class_
.found_funs
= new_funs
1938 def clean_duplicates():
1939 for source
in sources
:
1940 for class_
in source
.classes
:
1942 for fun
in class_
.found_funs
:
1943 if fun
.gen_decl_hash_py() in known_decls
:
1944 debug("Multiple declarations of " + fun
.gen_decl_hash_py(),3)
1945 other
= known_decls
[fun
.gen_decl_hash_py()]
1948 if fun
.gen_decl_hash_py() == other
.gen_decl_hash_py():
1949 fun
.duplicate
= True
1950 debug("Disabled \"" + fun
.gen_decl_hash_py() + "\"", 3)
1952 known_decls
[fun
.gen_decl_hash_py()] = fun
1954 for con
in class_
.found_constrs
:
1955 if con
.gen_decl_hash_py() in known_decls
:
1956 debug("Multiple declarations of " + con
.gen_decl_hash_py(),3)
1957 con
.duplicate
= True
1959 known_decls
.append(con
.gen_decl_hash_py())
1961 for fun
in unowned_functions
:
1962 if fun
.gen_decl_hash_py() in known_decls
:
1963 debug("Multiple declarations of " + fun
.gen_decl_hash_py(),3)
1964 fun
.duplicate
= True
1966 known_decls
.append(fun
.gen_decl_hash_py())
1968 def gen_wrappers(filename
, debug_level_
= 0):
1969 debug
.debug_level
= debug_level_
1970 for source
in sources
:
1971 parse_header(source
)
1978 col
= shutil
.get_terminal_size((80,20)).columns
1980 debug("-"*math
.floor((col
-7)/2)+"SUMMARY"+"-"*math
.ceil((col
-7)/2), 1)
1982 for source
in sources
:
1983 for class_
in source
.classes
:
1984 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)
1985 if len(class_
.found_constrs
) == 0:
1986 class_
.found_constrs
.append(WConstructor(source
.name
, class_
))
1987 debug(str(len(unowned_functions
)) + " functions are unowned", 1)
1989 debug("Enum " + assure_length(enum
.name
, len(max(enum_names
, key
=len)), True) + " contains " + assure_length(str(len(enum
.values
)), 2, False) + " values", 1)
1991 wrapper_file
= open(filename
, "w+")
1994 * yosys -- Yosys Open SYnthesis Suite
1996 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
1998 * Permission to use, copy, modify, and/or distribute this software for any
1999 * purpose with or without fee is hereby granted, provided that the above
2000 * copyright notice and this permission notice appear in all copies.
2002 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2003 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2004 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2005 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2006 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2007 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2008 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2010 * This is a generated file and can be overwritten by make
2015 for source
in sources
:
2016 wrapper_file
.write("#include \""+source
.name
+".h\"\n")
2017 wrapper_file
.write("""
2018 #include <boost/python/module.hpp>
2019 #include <boost/python/class.hpp>
2020 #include <boost/python/wrapper.hpp>
2021 #include <boost/python/call.hpp>
2022 #include <boost/python.hpp>
2024 USING_YOSYS_NAMESPACE
2026 namespace YOSYS_PYTHON {
2029 for source
in sources
:
2030 for wclass
in source
.classes
:
2031 wrapper_file
.write("\n\tstruct " + wclass
.name
+ ";")
2033 wrapper_file
.write("\n")
2035 for source
in sources
:
2036 for wclass
in source
.classes
:
2037 wrapper_file
.write(wclass
.gen_decl(source
.name
))
2039 wrapper_file
.write("\n")
2041 for source
in sources
:
2042 for wclass
in source
.classes
:
2043 wrapper_file
.write(wclass
.gen_funs(source
.name
))
2045 for fun
in unowned_functions
:
2046 wrapper_file
.write(fun
.gen_def())
2048 wrapper_file
.write(""" struct Initializer
2051 if(!Yosys::yosys_already_setup())
2053 Yosys::log_streams.push_back(&std::cout);
2054 Yosys::log_error_stderr = true;
2055 Yosys::yosys_setup();
2059 Initializer(Initializer const &) {}
2062 Yosys::yosys_shutdown();
2066 BOOST_PYTHON_MODULE(libyosys)
2068 using namespace boost::python;
2070 class_<Initializer>("Initializer");
2071 scope().attr("_hidden") = new Initializer();
2075 wrapper_file
.write(enum
.gen_boost_py())
2077 for source
in sources
:
2078 for wclass
in source
.classes
:
2079 wrapper_file
.write(wclass
.gen_boost_py())
2081 for fun
in unowned_functions
:
2082 wrapper_file
.write(fun
.gen_boost_py())
2084 wrapper_file
.write("\n\t}\n}\n#endif")
2086 def print_includes():
2087 for source
in sources
:
2088 print(source
.name
+ ".pyh")