tree_ir.py 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. # NOTE: NOP_LITERAL abuses a mechanic of the modelverse kernel. Specifically,
  2. # whenever the `ModelverseKernel.execute_yields` method returns `None`, then
  3. # the server built around it takes that as a hint that an instruction's phase
  4. # has been completed. The server interrupts the kernel's thread of execution
  5. # when it remarks that an instruction has completed a phase (i.e., when `None`
  6. # is returned by `ModelverseKernel.execute_yields`) and proceeds to check for
  7. # input and output.
  8. #
  9. # In assembly language, a nop is usually used as a point at which a thread of
  10. # execution can be terminated. It follows from the paragraph above that what
  11. # the interpreter does is more or less equivalent to placing nops after every
  12. # instruction. It is worthwhile to remark that JIT-compiled code cannot rely
  13. # on the kernel to interrupt the thread of execution automatically during a
  14. # jitted function's execution -- jitted functions are considered equivalent
  15. # to a single instruction as far as the kernel is concerned. A nop will be
  16. # inserted _after_ the function call (if it is called from interpreted code)
  17. # but that does not suffice for IO, which needs the input/output processing
  18. # to be performed during function execution.
  19. #
  20. # For this reason, the JIT must strategically interrupt the execution of the
  21. # functions it compiles. In other words, it must insert its own nops.
  22. # Here comes the interesting part: a nop is equivalent to `yield None`,
  23. # because that will persuade `ModelverseKernel.execute_yields` to relay the
  24. # `None` marker value to the server, without terminating the current
  25. # generator.
  26. # Let's just agree to disagree on map vs list comprehensions, pylint.
  27. # pylint: disable=I0011,W0141
  28. NOP_LITERAL = None
  29. """A literal that results in a nop during which execution may be interrupted
  30. when yielded."""
  31. UNKNOWN_RESULT_TYPE = 'unknown'
  32. """The result type for instructions that produce either primitive values or
  33. references to nodes."""
  34. PRIMITIVE_RESULT_TYPE = 'primitive'
  35. """The result type for instructions that produce primitive values."""
  36. NODE_RESULT_TYPE = 'node'
  37. """The result type for instructions that produce references to nodes."""
  38. NO_RESULT_TYPE = 'nothing'
  39. """The result type for instructions that no result."""
  40. def result_type_intersection(first_type, second_type):
  41. """Computes the intersection of the given result types."""
  42. if first_type == second_type:
  43. return first_type
  44. elif ((first_type == PRIMITIVE_RESULT_TYPE and second_type == UNKNOWN_RESULT_TYPE)
  45. or (first_type == UNKNOWN_RESULT_TYPE and second_type == PRIMITIVE_RESULT_TYPE)):
  46. return PRIMITIVE_RESULT_TYPE
  47. elif ((first_type == NODE_RESULT_TYPE and second_type == UNKNOWN_RESULT_TYPE)
  48. or (first_type == UNKNOWN_RESULT_TYPE and second_type == NODE_RESULT_TYPE)):
  49. return NODE_RESULT_TYPE
  50. else:
  51. return NO_RESULT_TYPE
  52. def result_type_union(first_type, second_type):
  53. """Computes the union of the given result types."""
  54. if first_type == second_type:
  55. return first_type
  56. elif ((first_type == PRIMITIVE_RESULT_TYPE and second_type == NO_RESULT_TYPE)
  57. or (first_type == NO_RESULT_TYPE and second_type == PRIMITIVE_RESULT_TYPE)):
  58. return PRIMITIVE_RESULT_TYPE
  59. elif ((first_type == NODE_RESULT_TYPE and second_type == NO_RESULT_TYPE)
  60. or (first_type == NO_RESULT_TYPE and second_type == NODE_RESULT_TYPE)):
  61. return NODE_RESULT_TYPE
  62. else:
  63. return UNKNOWN_RESULT_TYPE
  64. class Instruction(object):
  65. """A base class for instructions. An instruction is essentially a syntax
  66. node that must first be defined, and can only then be used."""
  67. def __init__(self):
  68. self.result_type_cache = None
  69. self.has_definition_cache = None
  70. def get_result_type(self):
  71. """Gets this instruction's result type."""
  72. if self.result_type_cache is None:
  73. self.result_type_cache = self.get_result_type_impl()
  74. return self.result_type_cache
  75. def has_result(self):
  76. """Tells if this instruction computes a result."""
  77. return self.get_result_type() != NO_RESULT_TYPE
  78. def has_definition(self):
  79. """Tells if this instruction requires a definition."""
  80. if self.has_definition_cache is None:
  81. self.has_definition_cache = self.has_definition_impl()
  82. return self.has_definition_cache
  83. def get_result_type_impl(self):
  84. """Gets this instruction's result type."""
  85. return PRIMITIVE_RESULT_TYPE
  86. def has_definition_impl(self):
  87. """Tells if this instruction requires a definition."""
  88. return True
  89. def get_result_name_override(self, code_generator):
  90. """Gets a value that overrides the code generator's result name for this
  91. instruction if it is not None."""
  92. return None
  93. def has_result_temporary(self):
  94. """Tells if this instruction stores its result in a temporary."""
  95. return True
  96. def generate_python_def(self, code_generator):
  97. """Generates a Python statement that executes this instruction.
  98. The statement is appended immediately to the code generator."""
  99. if self.has_definition():
  100. raise NotImplementedError()
  101. else:
  102. code_generator.append_line('pass')
  103. def generate_python_use(self, code_generator):
  104. """Generates a Python expression that retrieves this instruction's
  105. result. The expression is returned as a string."""
  106. if self.has_result():
  107. return code_generator.get_result_name(self)
  108. else:
  109. return 'None'
  110. def get_children(self):
  111. """Gets this instruction's sequence of child instructions."""
  112. raise NotImplementedError()
  113. def create(self, new_children):
  114. """Creates a new instruction of this type from the given sequence of child instructions."""
  115. raise NotImplementedError()
  116. def simplify_node(self):
  117. """Applies basic simplification to this instruction only."""
  118. return self
  119. def simplify(self):
  120. """Applies basic simplification to this instruction and all of its children."""
  121. # This fairly convoluted one-liner first simplifies all children, then creates
  122. # an instruction from the simplified children, and finally simplifies that top-level
  123. # instruction.
  124. return self.create([c.simplify() for c in self.get_children()]).simplify_node()
  125. def __str__(self):
  126. code_generator = PythonGenerator()
  127. self.generate_python_def(code_generator)
  128. return str(code_generator)
  129. class PythonGenerator(object):
  130. """Generates Python code from instructions."""
  131. def __init__(self, combine_state_definitions=True):
  132. self.code = []
  133. self.state_definitions = []
  134. self.state_definition_names = set()
  135. self.indentation_string = ' ' * 4
  136. self.indentation = 0
  137. self.result_name_dict = {}
  138. self.combine_state_definitions = combine_state_definitions
  139. def append(self, text):
  140. """Appends the given string to this code generator."""
  141. self.flush_state_definitions()
  142. self.code.append(text)
  143. def append_indentation(self):
  144. """Appends indentation to the code generator."""
  145. self.append(self.indentation_string * self.indentation)
  146. def append_line(self, line=None):
  147. """Appends the indentation string followed by the given string (if any)
  148. and a newline to the code generator."""
  149. self.append_indentation()
  150. if line is not None:
  151. self.append(line)
  152. self.append('\n')
  153. def increase_indentation(self):
  154. """Increases the code generator's indentation by one indent."""
  155. self.flush_state_definitions()
  156. self.indentation += 1
  157. def decrease_indentation(self):
  158. """Decreases the code generator's indentation by one indent."""
  159. self.flush_state_definitions()
  160. self.indentation -= 1
  161. def get_result_name(self, instruction, advised_name=None):
  162. """Gets the name of the given instruction's result variable."""
  163. if instruction not in self.result_name_dict:
  164. override_name = instruction.get_result_name_override(self)
  165. if override_name is not None:
  166. self.result_name_dict[instruction] = override_name
  167. elif advised_name is not None:
  168. self.result_name_dict[instruction] = advised_name
  169. else:
  170. self.result_name_dict[instruction] = \
  171. 'tmp' + str(len(self.result_name_dict))
  172. result = self.result_name_dict[instruction]
  173. if result in self.state_definition_names:
  174. # This might mean that we're trying to access a name that is
  175. # defined by the current block of state definitions. So we need
  176. # to flush the state definitions now.
  177. self.flush_state_definitions()
  178. return result
  179. def append_definition(self, lhs, rhs):
  180. """Defines the first instruction's result variable as the second
  181. instruction's result."""
  182. self.append_line(
  183. self.get_result_name(lhs) + ' = ' + rhs.generate_python_use(self))
  184. def append_move_definition(self, lhs, rhs):
  185. """First defines the second instruction, then defines the first
  186. instruction as the result of the second."""
  187. if rhs.has_definition():
  188. # Retrieve the result name for the lhs.
  189. lhs_result_name = self.get_result_name(lhs)
  190. # Encourage the rhs to take on the same result name as the lhs.
  191. rhs_result_name = self.get_result_name(rhs, lhs_result_name)
  192. # Generate the rhs' definition.
  193. rhs.generate_python_def(self)
  194. # Only perform an assignment if it's truly necessary.
  195. if lhs_result_name != rhs_result_name or not rhs.has_result_temporary():
  196. self.append_definition(lhs, rhs)
  197. else:
  198. self.append_definition(lhs, rhs)
  199. def append_state_definition(self, lhs, opcode, args):
  200. """Appends a definition that queries the modelverse state."""
  201. result_name = self.get_result_name(lhs)
  202. request_tuple = '(%s, [%s])' % (
  203. repr(opcode),
  204. ', '.join([arg_i.generate_python_use(self) for arg_i in args]))
  205. self.state_definitions.append((result_name, request_tuple))
  206. self.state_definition_names.add(result_name)
  207. if not self.combine_state_definitions:
  208. self.flush_state_definitions()
  209. def flush_state_definitions(self):
  210. """Flushes the list of state-access definitions to the generated code."""
  211. state_defs = self.state_definitions
  212. if len(state_defs) > 0:
  213. # Clear state_definitions _before_ calling append_line, because append_line
  214. # will call flush_state_definitions. Clearing state_definitions afterward
  215. # will result in infinite recursion.
  216. self.state_definitions = []
  217. self.state_definition_names = set()
  218. if len(state_defs) == 1:
  219. self.append_line('%s, = yield [%s]' % state_defs[0])
  220. else:
  221. self.append_line(
  222. "%s = yield [%s]" % (
  223. ', '.join([name for name, _ in state_defs]),
  224. ', '.join([def_val for _, def_val in state_defs])))
  225. def __str__(self):
  226. self.flush_state_definitions()
  227. return ''.join(self.code)
  228. class VoidInstruction(Instruction):
  229. """A base class for instructions that do not return a value."""
  230. def get_result_type_impl(self):
  231. """Gets this instruction's result type."""
  232. return NO_RESULT_TYPE
  233. def get_children(self):
  234. """Gets this instruction's sequence of child instructions."""
  235. return []
  236. def create(self, new_children):
  237. """Creates a new instruction of this type from the given sequence of child instructions."""
  238. return self
  239. class EmptyInstruction(VoidInstruction):
  240. """Represents the empty instruction, which does nothing."""
  241. def has_definition_impl(self):
  242. """Tells if this instruction requires a definition."""
  243. return False
  244. def __repr__(self):
  245. return "EmptyInstruction()"
  246. class SelectInstruction(Instruction):
  247. """Represents a select-instruction: an instruction that defines one of two
  248. child instructions, and sets its result to the defined child's result."""
  249. def __init__(self, condition, if_clause, else_clause):
  250. Instruction.__init__(self)
  251. self.condition = condition
  252. self.if_clause = if_clause
  253. self.else_clause = else_clause
  254. def get_result_type_impl(self):
  255. """Gets this instruction's result type."""
  256. return result_type_intersection(
  257. self.if_clause.get_result_type(),
  258. self.else_clause.get_result_type())
  259. def simplify_node(self):
  260. """Applies basic simplification to this instruction only."""
  261. if isinstance(self.condition, LiteralInstruction):
  262. return self.if_clause if self.condition.literal else self.else_clause
  263. else:
  264. return SelectInstruction(self.condition, self.if_clause, self.else_clause)
  265. def get_children(self):
  266. """Gets this instruction's sequence of child instructions."""
  267. return [self.condition, self.if_clause, self.else_clause]
  268. def create(self, new_children):
  269. """Creates a new instruction of this type from the given sequence of child instructions."""
  270. condition, if_clause, else_clause = new_children
  271. return SelectInstruction(condition, if_clause, else_clause)
  272. def generate_python_if(self, code_generator, is_elif=False):
  273. """Generates Python code for this instruction."""
  274. if_has_result = self.has_result()
  275. if self.condition.has_definition():
  276. self.condition.generate_python_def(code_generator)
  277. code_generator.append_line(
  278. ('elif ' if is_elif else 'if ') +
  279. self.condition.generate_python_use(code_generator) + ':')
  280. code_generator.increase_indentation()
  281. if if_has_result:
  282. code_generator.append_move_definition(self, self.if_clause)
  283. else:
  284. self.if_clause.generate_python_def(code_generator)
  285. code_generator.decrease_indentation()
  286. else_has_def = self.else_clause.has_definition()
  287. if else_has_def or if_has_result:
  288. if (isinstance(self.else_clause, SelectInstruction) and
  289. not self.else_clause.condition.has_definition()):
  290. self.else_clause.generate_python_if(code_generator, True)
  291. if if_has_result:
  292. code_generator.increase_indentation()
  293. code_generator.append_definition(self, self.else_clause)
  294. code_generator.decrease_indentation()
  295. else:
  296. code_generator.append_line('else:')
  297. code_generator.increase_indentation()
  298. if if_has_result:
  299. code_generator.append_move_definition(self, self.else_clause)
  300. else:
  301. self.else_clause.generate_python_def(code_generator)
  302. code_generator.decrease_indentation()
  303. def generate_python_def(self, code_generator):
  304. """Generates Python code for this instruction."""
  305. return self.generate_python_if(code_generator)
  306. def __repr__(self):
  307. return "SelectInstruction(%r, %r, %r)" % (self.condition, self.if_clause, self.else_clause)
  308. class ReturnInstruction(VoidInstruction):
  309. """Represents a return-instruction."""
  310. def __init__(self, value):
  311. VoidInstruction.__init__(self)
  312. self.value = value
  313. def get_children(self):
  314. """Gets this instruction's sequence of child instructions."""
  315. return [self.value]
  316. def create(self, new_children):
  317. """Creates a new instruction of this type from the given sequence of child instructions."""
  318. value, = new_children
  319. return ReturnInstruction(value)
  320. def simplify_node(self):
  321. """Applies basic simplification to this instruction only."""
  322. # If we have a return whose value is a call, then we can rewrite
  323. # that as a tail call and save us a stack frame.
  324. def rewrite_call(instruction):
  325. """Rewrites the given instruction in tail-call form, or returns
  326. None."""
  327. if isinstance(instruction, RunGeneratorFunctionInstruction):
  328. return RunTailGeneratorFunctionInstruction(*instruction.get_children())
  329. elif isinstance(instruction, CompoundInstruction):
  330. snd_rewritten = rewrite_call(instruction.second)
  331. if snd_rewritten is not None:
  332. return CompoundInstruction(instruction.first, snd_rewritten)
  333. return None
  334. rewritten_value = rewrite_call(self.value)
  335. if rewritten_value is None:
  336. return self
  337. else:
  338. # We don't even need to create a return here, because tail calls terminate
  339. # the current stack frame anyway.
  340. return rewritten_value
  341. def generate_python_def(self, code_generator):
  342. """Generates Python code for this instruction."""
  343. if self.value.has_definition():
  344. self.value.generate_python_def(code_generator)
  345. code_generator.append_line(
  346. 'raise PrimitiveFinished(' +
  347. self.value.generate_python_use(code_generator) +
  348. ')')
  349. def __repr__(self):
  350. return "ReturnInstruction(%r)" % self.value
  351. class RaiseInstruction(VoidInstruction):
  352. """An instruction that raises an error."""
  353. def __init__(self, value):
  354. VoidInstruction.__init__(self)
  355. self.value = value
  356. def get_children(self):
  357. """Gets this instruction's sequence of child instructions."""
  358. return [self.value]
  359. def create(self, new_children):
  360. """Creates a new instruction of this type from the given sequence of child instructions."""
  361. value, = new_children
  362. return RaiseInstruction(value)
  363. def generate_python_def(self, code_generator):
  364. """Generates Python code for this instruction."""
  365. self.value.generate_python_def(code_generator)
  366. code_generator.append_line(
  367. 'raise ' + self.value.generate_python_use(code_generator))
  368. def __repr__(self):
  369. return "RaiseInstruction(%r)" % self.value
  370. class CallInstruction(Instruction):
  371. """An instruction that performs a simple call."""
  372. def __init__(self, target, argument_list):
  373. Instruction.__init__(self)
  374. self.target = target
  375. self.argument_list = argument_list
  376. def get_children(self):
  377. """Gets this instruction's sequence of child instructions."""
  378. return [self.target] + self.argument_list
  379. def create(self, new_children):
  380. """Creates a new instruction of this type from the given sequence of child instructions."""
  381. return CallInstruction(new_children[0], new_children[1:])
  382. def generate_python_def(self, code_generator):
  383. """Generates Python code for this instruction."""
  384. if self.target.has_definition():
  385. self.target.generate_python_def(code_generator)
  386. for arg in self.argument_list:
  387. if arg.has_definition():
  388. arg.generate_python_def(code_generator)
  389. code_generator.append_line(
  390. '%s = %s(%s)' % (
  391. code_generator.get_result_name(self),
  392. self.target.generate_python_use(code_generator),
  393. ', '.join([arg.generate_python_use(code_generator) for arg in self.argument_list])))
  394. def __repr__(self):
  395. return "CallInstruction(%r, %r)" % (self.target, self.argument_list)
  396. class PrintInstruction(VoidInstruction):
  397. """An instruction that prints a value."""
  398. def __init__(self, argument):
  399. VoidInstruction.__init__(self)
  400. self.argument = argument
  401. def get_children(self):
  402. """Gets this instruction's sequence of child instructions."""
  403. return [self.argument]
  404. def create(self, new_children):
  405. """Creates a new instruction of this type from the given sequence of child instructions."""
  406. arg, = new_children
  407. return PrintInstruction(arg)
  408. def generate_python_def(self, code_generator):
  409. """Generates Python code for this instruction."""
  410. if self.argument.has_definition():
  411. self.argument.generate_python_def(code_generator)
  412. code_generator.append_line(
  413. 'print(%s)' % (
  414. self.argument.generate_python_use(code_generator)))
  415. def __repr__(self):
  416. return "PrintInstruction(%r)" % self.argument
  417. class BinaryInstruction(Instruction):
  418. """An instruction that performs a binary operation."""
  419. def __init__(self, lhs, operator, rhs):
  420. Instruction.__init__(self)
  421. self.lhs = lhs
  422. self.operator = operator
  423. self.rhs = rhs
  424. def has_definition_impl(self):
  425. """Tells if this instruction requires a definition."""
  426. return self.lhs.has_definition() or self.rhs.has_definition()
  427. def get_children(self):
  428. """Gets this instruction's sequence of child instructions."""
  429. return [self.lhs, self.rhs]
  430. def create(self, new_children):
  431. """Creates a new instruction of this type from the given sequence of child instructions."""
  432. lhs, rhs, = new_children
  433. return BinaryInstruction(lhs, self.operator, rhs)
  434. def simplify_node(self):
  435. """Applies basic simplification to this instruction only."""
  436. if isinstance(self.lhs, LiteralInstruction) and isinstance(self.rhs, LiteralInstruction):
  437. # TODO: there's probably a better way to do this than with eval.
  438. return LiteralInstruction(
  439. eval('%s %s %s' % (repr(self.lhs.literal), self.operator, repr(self.rhs.literal))))
  440. else:
  441. return self
  442. def has_result_temporary(self):
  443. """Tells if this instruction stores its result in a temporary."""
  444. return False
  445. def generate_python_use(self, code_generator):
  446. """Generates a Python expression that retrieves this instruction's
  447. result. The expression is returned as a string."""
  448. return '(%s %s %s)' % (
  449. self.lhs.generate_python_use(code_generator),
  450. self.operator,
  451. self.rhs.generate_python_use(code_generator))
  452. def generate_python_def(self, code_generator):
  453. """Generates a Python statement that executes this instruction.
  454. The statement is appended immediately to the code generator."""
  455. lhs_has_def, rhs_has_def = self.lhs.has_definition(), self.rhs.has_definition()
  456. if lhs_has_def:
  457. self.lhs.generate_python_def(code_generator)
  458. if rhs_has_def:
  459. self.rhs.generate_python_def(code_generator)
  460. elif rhs_has_def:
  461. self.rhs.generate_python_def(code_generator)
  462. else:
  463. code_generator.append_line('pass')
  464. def __repr__(self):
  465. return "BinaryInstruction(%r, %r, %r)" % (self.lhs, self.operator, self.rhs)
  466. class UnaryInstruction(Instruction):
  467. """An instruction that performs a unary operation."""
  468. def __init__(self, operator, operand):
  469. Instruction.__init__(self)
  470. self.operator = operator
  471. self.operand = operand
  472. def has_definition_impl(self):
  473. """Tells if this instruction requires a definition."""
  474. return self.operand.has_definition()
  475. def get_children(self):
  476. """Gets this instruction's sequence of child instructions."""
  477. return [self.operand]
  478. def has_result_temporary(self):
  479. """Tells if this instruction stores its result in a temporary."""
  480. return False
  481. def create(self, new_children):
  482. """Creates a new instruction of this type from the given sequence of child instructions."""
  483. operand, = new_children
  484. return UnaryInstruction(self.operator, operand)
  485. def simplify_node(self):
  486. """Applies basic simplification to this instruction only."""
  487. if isinstance(self.operand, LiteralInstruction):
  488. # TODO: there's probably a better way to do this than with eval.
  489. return LiteralInstruction(
  490. eval('%s %s' % (self.operator, repr(self.operand.literal))))
  491. else:
  492. return self
  493. def generate_python_use(self, code_generator):
  494. """Generates a Python expression that retrieves this instruction's
  495. result. The expression is returned as a string."""
  496. return '(%s %s)' % (
  497. self.operator,
  498. self.operand.generate_python_use(code_generator))
  499. def generate_python_def(self, code_generator):
  500. """Generates a Python statement that executes this instruction.
  501. The statement is appended immediately to the code generator."""
  502. if self.operand.has_definition():
  503. self.operand.generate_python_def(code_generator)
  504. else:
  505. code_generator.append_line('pass')
  506. def __repr__(self):
  507. return "UnaryInstruction(%r, %r)" % (self.operator, self.operand)
  508. class LoopInstruction(VoidInstruction):
  509. """Represents a loop-instruction, which loops until broken."""
  510. def __init__(self, body):
  511. VoidInstruction.__init__(self)
  512. self.body = body
  513. def get_children(self):
  514. """Gets this instruction's sequence of child instructions."""
  515. return [self.body]
  516. def create(self, new_children):
  517. """Creates a new instruction of this type from the given sequence of child instructions."""
  518. body, = new_children
  519. return LoopInstruction(body)
  520. def generate_python_def(self, code_generator):
  521. """Generates Python code for this instruction."""
  522. code_generator.append_line('while 1:')
  523. code_generator.increase_indentation()
  524. self.body.generate_python_def(code_generator)
  525. code_generator.decrease_indentation()
  526. def __repr__(self):
  527. return "LoopInstruction(%r)" % self.body
  528. class BreakInstruction(VoidInstruction):
  529. """Represents a break-instruction."""
  530. def generate_python_def(self, code_generator):
  531. """Generates Python code for this instruction."""
  532. code_generator.append_line('break')
  533. def __repr__(self):
  534. return "BreakInstruction()"
  535. class ContinueInstruction(VoidInstruction):
  536. """Represents a continue-instruction."""
  537. def generate_python_def(self, code_generator):
  538. """Generates Python code for this instruction."""
  539. code_generator.append_line('continue')
  540. def __repr__(self):
  541. return "ContinueInstruction()"
  542. class IgnoreInstruction(VoidInstruction):
  543. """Represents an instruction that evaluates its operand, and then discards
  544. the result."""
  545. def __init__(self, value):
  546. VoidInstruction.__init__(self)
  547. self.value = value
  548. def has_definition_impl(self):
  549. """Tells if this instruction requires a definition."""
  550. return self.value.has_definition()
  551. def get_children(self):
  552. """Gets this instruction's sequence of child instructions."""
  553. return [self.value]
  554. def create(self, new_children):
  555. """Creates a new instruction of this type from the given sequence of child instructions."""
  556. value, = new_children
  557. return IgnoreInstruction(value)
  558. def simplify_node(self):
  559. """Applies basic simplification to this instruction and its children."""
  560. if not self.value.has_result():
  561. return self.value
  562. else:
  563. return self
  564. def generate_python_def(self, code_generator):
  565. """Generates Python code for this instruction."""
  566. self.value.generate_python_def(code_generator)
  567. def __repr__(self):
  568. return "IgnoreInstruction(%r)" % self.value
  569. class CompoundInstruction(Instruction):
  570. """Represents an instruction that evaluates two other instructions
  571. in order, and returns the second instruction's result."""
  572. def __init__(self, first, second):
  573. Instruction.__init__(self)
  574. self.first = first
  575. self.second = second
  576. def get_result_type_impl(self):
  577. """Gets this instruction's result type."""
  578. if self.second.has_result():
  579. return self.second.get_result_type()
  580. else:
  581. return self.first.get_result_type()
  582. def get_children(self):
  583. """Gets this instruction's sequence of child instructions."""
  584. return [self.first, self.second]
  585. def create(self, new_children):
  586. """Creates a new instruction of this type from the given sequence of child instructions."""
  587. first, second = new_children
  588. return CompoundInstruction(first, second)
  589. def simplify_node(self):
  590. """Applies basic simplification to this instruction and its children."""
  591. if not self.first.has_definition() and (
  592. not self.first.has_result() or self.second.has_result()):
  593. return self.second
  594. elif (not self.second.has_definition()) and (not self.second.has_result()):
  595. return self.first
  596. else:
  597. return self
  598. def generate_python_def(self, code_generator):
  599. """Generates Python code for this instruction."""
  600. if self.second.has_result():
  601. self.first.generate_python_def(code_generator)
  602. code_generator.append_move_definition(self, self.second)
  603. elif self.first.has_result():
  604. code_generator.append_move_definition(self, self.first)
  605. self.second.generate_python_def(code_generator)
  606. else:
  607. self.first.generate_python_def(code_generator)
  608. self.second.generate_python_def(code_generator)
  609. def __repr__(self):
  610. return "CompoundInstruction(%r, %r)" % (self.first, self.second)
  611. class LiteralInstruction(Instruction):
  612. """Represents an integer, floating-point, string or Boolean literal."""
  613. def __init__(self, literal):
  614. Instruction.__init__(self)
  615. self.literal = literal
  616. def has_definition_impl(self):
  617. """Tells if this instruction requires a definition."""
  618. return False
  619. def has_result_temporary(self):
  620. """Tells if this instruction stores its result in a temporary."""
  621. return False
  622. def get_children(self):
  623. """Gets this instruction's sequence of child instructions."""
  624. return []
  625. def create(self, new_children):
  626. """Creates a new instruction of this type from the given sequence of child instructions."""
  627. return self
  628. def generate_python_use(self, code_generator):
  629. """Generates a Python expression that retrieves this instruction's
  630. result. The expression is returned as a string."""
  631. return repr(self.literal)
  632. def __repr__(self):
  633. return "LiteralInstruction(%r)" % self.literal
  634. class DictionaryLiteralInstruction(Instruction):
  635. """Constructs a dictionary literal."""
  636. def __init__(self, key_value_pairs):
  637. Instruction.__init__(self)
  638. self.key_value_pairs = key_value_pairs
  639. def get_children(self):
  640. """Gets this instruction's sequence of child instructions."""
  641. return [val for _, val in self.key_value_pairs]
  642. def create(self, new_children):
  643. """Creates a new instruction of this type from the given sequence of child instructions."""
  644. keys = [k for k, _ in self.key_value_pairs]
  645. return DictionaryLiteralInstruction(zip(keys, new_children))
  646. def has_definition_impl(self):
  647. """Tells if this instruction requires a definition."""
  648. return any(
  649. [key.has_definition() or val.has_definition()
  650. for key, val in self.key_value_pairs])
  651. def has_result_temporary(self):
  652. """Tells if this instruction stores its result in a temporary."""
  653. return False
  654. def simplify(self):
  655. """Applies basic simplification to this instruction and its children."""
  656. return DictionaryLiteralInstruction(
  657. [(key.simplify(), val.simplify()) for key, val in self.key_value_pairs])
  658. def generate_python_def(self, code_generator):
  659. """Generates a Python statement that executes this instruction.
  660. The statement is appended immediately to the code generator."""
  661. for key, val in self.key_value_pairs:
  662. if key.has_definition():
  663. key.generate_python_def(code_generator)
  664. if val.has_definition():
  665. val.generate_python_def(code_generator)
  666. def generate_python_use(self, code_generator):
  667. """Generates a Python expression that retrieves this instruction's
  668. result. The expression is returned as a string."""
  669. return '{%s}' % ', '.join(
  670. ['%s : %s' % (
  671. key.generate_python_use(code_generator),
  672. val.generate_python_use(code_generator))
  673. for key, val in self.key_value_pairs])
  674. def __repr__(self):
  675. return "DictionaryLiteralInstruction(%r)" % self.key_value_pairs
  676. class StateInstruction(Instruction):
  677. """An instruction that accesses the modelverse state."""
  678. def get_result_type_impl(self):
  679. """Gets the type of value produced by this instruction."""
  680. return NODE_RESULT_TYPE
  681. def get_opcode(self):
  682. """Gets the opcode for this state instruction."""
  683. raise NotImplementedError()
  684. def get_arguments(self):
  685. """Gets this state instruction's argument list."""
  686. raise NotImplementedError()
  687. def get_children(self):
  688. """Gets this instruction's sequence of child instructions."""
  689. return self.get_arguments()
  690. def create(self, new_children):
  691. """Creates a new instruction of this type from the given sequence of child instructions."""
  692. return type(self)(*new_children)
  693. def generate_python_def(self, code_generator):
  694. """Generates a Python statement that executes this instruction.
  695. The statement is appended immediately to the code generator."""
  696. args = self.get_arguments()
  697. for arg_i in args:
  698. if arg_i.has_definition():
  699. arg_i.generate_python_def(code_generator)
  700. code_generator.append_state_definition(self, self.get_opcode(), args)
  701. def __repr__(self):
  702. return "StateInstruction(%s)" % ', '.join(map(repr, self.get_arguments()))
  703. class RunGeneratorFunctionInstruction(StateInstruction):
  704. """An instruction that runs a generator function."""
  705. def __init__(self, function, argument_dict, result_type=PRIMITIVE_RESULT_TYPE):
  706. StateInstruction.__init__(self)
  707. self.function = function
  708. self.argument_dict = argument_dict
  709. self.result_type_cache = result_type
  710. def get_opcode(self):
  711. """Gets the opcode for this state instruction."""
  712. return "CALL_KWARGS"
  713. def get_arguments(self):
  714. """Gets this state instruction's argument list."""
  715. return [self.function, self.argument_dict]
  716. def create(self, new_children):
  717. """Creates a new instruction of this type from the given sequence of child instructions."""
  718. func, arg_dict = new_children
  719. return RunGeneratorFunctionInstruction(func, arg_dict, self.get_result_type())
  720. class RunTailGeneratorFunctionInstruction(StateInstruction):
  721. """An instruction that runs a generator function."""
  722. def __init__(self, function, argument_dict):
  723. StateInstruction.__init__(self)
  724. self.function = function
  725. self.argument_dict = argument_dict
  726. def get_result_type_impl(self):
  727. """Gets the type of value produced by this instruction."""
  728. return NO_RESULT_TYPE
  729. def get_opcode(self):
  730. """Gets the opcode for this state instruction."""
  731. return "TAIL_CALL_KWARGS"
  732. def get_arguments(self):
  733. """Gets this state instruction's argument list."""
  734. return [self.function, self.argument_dict]
  735. class VariableName(object):
  736. """A data structure that unifies names across instructions that access the
  737. same variable."""
  738. def __init__(self, name):
  739. self.name = name
  740. def get_result_name_override(self, _):
  741. """Gets a value that overrides the code generator's result name for this
  742. instruction if it is not None."""
  743. return self.name
  744. def __str__(self):
  745. return self.name
  746. def __repr__(self):
  747. return 'VariableName(%r)' % self.name
  748. class VariableInstruction(Instruction):
  749. """A base class for instructions that access variables."""
  750. def __init__(self, name):
  751. Instruction.__init__(self)
  752. if isinstance(name, str) or isinstance(name, unicode) or name is None:
  753. self.name = VariableName(name)
  754. else:
  755. self.name = name
  756. def get_children(self):
  757. """Gets this instruction's sequence of child instructions."""
  758. raise NotImplementedError()
  759. def create(self, new_children):
  760. """Creates a new instruction of this type from the given sequence of child instructions."""
  761. raise NotImplementedError()
  762. def get_result_name_override(self, code_generator):
  763. """Gets a value that overrides the code generator's result name for this
  764. instruction if it is not None."""
  765. return code_generator.get_result_name(self.name)
  766. class LocalInstruction(VariableInstruction):
  767. """A base class for instructions that access local variables."""
  768. def get_children(self):
  769. """Gets this instruction's sequence of child instructions."""
  770. raise NotImplementedError()
  771. def create(self, new_children):
  772. """Creates a new instruction of this type from the given sequence of child instructions."""
  773. raise NotImplementedError()
  774. def create_load(self):
  775. """Creates an instruction that loads the variable referenced by this instruction."""
  776. return LoadLocalInstruction(self.name)
  777. def create_store(self, value):
  778. """Creates an instruction that stores the given value in the variable referenced
  779. by this instruction."""
  780. return StoreLocalInstruction(self.name, value)
  781. class StoreLocalInstruction(LocalInstruction):
  782. """An instruction that stores a value in a local variable."""
  783. def __init__(self, name, value):
  784. LocalInstruction.__init__(self, name)
  785. self.value = value
  786. def get_children(self):
  787. """Gets this instruction's sequence of child instructions."""
  788. return [self.value]
  789. def create(self, new_children):
  790. """Creates a new instruction of this type from the given sequence of child instructions."""
  791. val, = new_children
  792. return StoreLocalInstruction(self.name, val)
  793. def generate_python_def(self, code_generator):
  794. """Generates a Python statement that executes this instruction.
  795. The statement is appended immediately to the code generator."""
  796. code_generator.append_move_definition(self, self.value)
  797. def __repr__(self):
  798. return 'StoreLocalInstruction(%r, %r)' % (self.name, self.value)
  799. class LoadLocalInstruction(LocalInstruction):
  800. """An instruction that loads a value from a local variable."""
  801. def has_definition_impl(self):
  802. """Tells if this instruction requires a definition."""
  803. return False
  804. def get_children(self):
  805. """Gets this instruction's sequence of child instructions."""
  806. return []
  807. def create(self, new_children):
  808. """Creates a new instruction of this type from the given sequence of child instructions."""
  809. return self
  810. def __repr__(self):
  811. return 'LoadLocalInstruction(%r)' % self.name
  812. class DefineFunctionInstruction(VariableInstruction):
  813. """An instruction that defines a function."""
  814. def __init__(self, name, parameter_list, body):
  815. VariableInstruction.__init__(self, name)
  816. self.parameter_list = parameter_list
  817. self.body = body
  818. def get_children(self):
  819. """Gets this instruction's sequence of child instructions."""
  820. return [self.body]
  821. def create(self, new_children):
  822. """Creates a new instruction of this type from the given sequence of child instructions."""
  823. body, = new_children
  824. return DefineFunctionInstruction(self.name, self.parameter_list, body)
  825. def generate_python_def(self, code_generator):
  826. """Generates a Python statement that executes this instruction.
  827. The statement is appended immediately to the code generator."""
  828. code_generator.append_line('def %s(%s):' % (
  829. code_generator.get_result_name(self), ', '.join(self.parameter_list)))
  830. code_generator.increase_indentation()
  831. self.body.generate_python_def(code_generator)
  832. code_generator.decrease_indentation()
  833. def __repr__(self):
  834. return 'DefineFunctionInstruction(%r, %r, %r)' % (self.name, self.parameter_list, self.body)
  835. class LocalExistsInstruction(LocalInstruction):
  836. """An instruction that checks if a local variable exists."""
  837. def has_definition_impl(self):
  838. """Tells if this instruction requires a definition."""
  839. return False
  840. def get_children(self):
  841. """Gets this instruction's sequence of child instructions."""
  842. return []
  843. def create(self, new_children):
  844. """Creates a new instruction of this type from the given sequence of child instructions."""
  845. return self
  846. def generate_python_use(self, code_generator):
  847. """Generates a Python expression that retrieves this instruction's
  848. result. The expression is returned as a string."""
  849. return "'%s' in locals()" % self.get_result_name_override(code_generator)
  850. def __repr__(self):
  851. return 'LocalExistsInstruction(%r)' % self.name
  852. class LoadGlobalInstruction(VariableInstruction):
  853. """An instruction that loads a value from a global variable."""
  854. def has_definition_impl(self):
  855. """Tells if this instruction requires a definition."""
  856. return False
  857. def get_children(self):
  858. """Gets this instruction's sequence of child instructions."""
  859. return []
  860. def create(self, new_children):
  861. """Creates a new instruction of this type from the given sequence of child instructions."""
  862. return self
  863. def __repr__(self):
  864. return 'LoadGlobalInstruction(%r)' % self.name
  865. class LoadIndexInstruction(Instruction):
  866. """An instruction that produces a value by indexing a specified expression with
  867. a given key."""
  868. def __init__(self, indexed, key):
  869. Instruction.__init__(self)
  870. self.indexed = indexed
  871. self.key = key
  872. def has_definition_impl(self):
  873. """Tells if this instruction requires a definition."""
  874. return self.indexed.has_definition() or self.key.has_definition()
  875. def get_children(self):
  876. """Gets this instruction's sequence of child instructions."""
  877. return [self.indexed, self.key]
  878. def has_result_temporary(self):
  879. """Tells if this instruction stores its result in a temporary."""
  880. return False
  881. def create(self, new_children):
  882. """Creates a new instruction of this type from the given sequence of child instructions."""
  883. indexed, key = new_children
  884. return LoadIndexInstruction(indexed, key)
  885. def generate_python_use(self, code_generator):
  886. """Generates a Python expression that retrieves this instruction's
  887. result. The expression is returned as a string."""
  888. return "%s[%s]" % (
  889. self.indexed.generate_python_use(code_generator),
  890. self.key.generate_python_use(code_generator))
  891. def generate_python_def(self, code_generator):
  892. """Generates a Python statement that executes this instruction.
  893. The statement is appended immediately to the code generator."""
  894. indexed_has_def, key_has_def = self.indexed.has_definition(), self.key.has_definition()
  895. if indexed_has_def:
  896. self.indexed.generate_python_def(code_generator)
  897. if key_has_def:
  898. self.key.generate_python_def(code_generator)
  899. if not indexed_has_def and not key_has_def:
  900. code_generator.append_line('pass')
  901. def __repr__(self):
  902. return 'LoadIndexInstruction(%r, %r)' % (self.indexed, self.key)
  903. class LoadMemberInstruction(Instruction):
  904. """An instruction that produces a value by loading a member from a container."""
  905. def __init__(self, container, member_name):
  906. Instruction.__init__(self)
  907. self.container = container
  908. self.member_name = member_name
  909. def has_definition_impl(self):
  910. """Tells if this instruction requires a definition."""
  911. return self.container.has_definition()
  912. def get_children(self):
  913. """Gets this instruction's sequence of child instructions."""
  914. return [self.container]
  915. def has_result_temporary(self):
  916. """Tells if this instruction stores its result in a temporary."""
  917. return False
  918. def create(self, new_children):
  919. """Creates a new instruction of this type from the given sequence of child instructions."""
  920. container, = new_children
  921. return LoadMemberInstruction(container, self.member_name)
  922. def generate_python_def(self, code_generator):
  923. """Generates a Python statement that executes this instruction.
  924. The statement is appended immediately to the code generator."""
  925. self.container.generate_python_def(code_generator)
  926. def generate_python_use(self, code_generator):
  927. """Generates a Python expression that retrieves this instruction's
  928. result. The expression is returned as a string."""
  929. return "%s.%s" % (
  930. self.container.generate_python_use(code_generator),
  931. self.member_name)
  932. def __repr__(self):
  933. return 'LoadMemberInstruction(%r, %r)' % (self.container, self.member_name)
  934. class StoreMemberInstruction(VoidInstruction):
  935. """An instruction that stores a value in a container member."""
  936. def __init__(self, container, member_name, value):
  937. VoidInstruction.__init__(self)
  938. self.container = container
  939. self.member_name = member_name
  940. self.value = value
  941. def has_definition_impl(self):
  942. """Tells if this instruction requires a definition."""
  943. return True
  944. def get_children(self):
  945. """Gets this instruction's sequence of child instructions."""
  946. return [self.container, self.value]
  947. def create(self, new_children):
  948. """Creates a new instruction of this type from the given sequence of child instructions."""
  949. container, value = new_children
  950. return StoreMemberInstruction(container, self.member_name, value)
  951. def generate_python_def(self, code_generator):
  952. """Generates a Python statement that executes this instruction.
  953. The statement is appended immediately to the code generator."""
  954. if self.container.has_definition():
  955. self.container.generate_python_def(code_generator)
  956. code_generator.append_line('%s.%s = %s' % (
  957. self.container.generate_python_use(code_generator),
  958. self.member_name,
  959. self.value.generate_python_use(code_generator)))
  960. def __repr__(self):
  961. return 'StoreMemberInstruction(%r, %r, %r)' % (self.container, self.member_name, self.value)
  962. class NopInstruction(VoidInstruction):
  963. """A nop instruction, which allows for the kernel's thread of execution to be interrupted."""
  964. def generate_python_def(self, code_generator):
  965. """Generates a Python statement that executes this instruction.
  966. The statement is appended immediately to the code generator."""
  967. code_generator.append_line('yield %s' % repr(NOP_LITERAL))
  968. def __repr__(self):
  969. return 'NopInstruction()'
  970. class ReadValueInstruction(StateInstruction):
  971. """An instruction that reads a value from a node."""
  972. def __init__(self, node_id):
  973. StateInstruction.__init__(self)
  974. self.node_id = node_id
  975. def get_result_type_impl(self):
  976. """Gets the type of value produced by this instruction."""
  977. return PRIMITIVE_RESULT_TYPE
  978. def simplify_node(self):
  979. """Applies basic simplification to this instruction only."""
  980. if isinstance(self.node_id, CreateNodeWithValueInstruction):
  981. return self.node_id.value
  982. else:
  983. return self
  984. def get_opcode(self):
  985. """Gets the opcode for this state instruction."""
  986. return "RV"
  987. def get_arguments(self):
  988. """Gets this state instruction's argument list."""
  989. return [self.node_id]
  990. class ReadDictionaryValueInstruction(StateInstruction):
  991. """An instruction that reads a dictionary value."""
  992. def __init__(self, node_id, key):
  993. StateInstruction.__init__(self)
  994. self.node_id = node_id
  995. self.key = key
  996. def get_opcode(self):
  997. """Gets the opcode for this state instruction."""
  998. return "RD"
  999. def get_arguments(self):
  1000. """Gets this state instruction's argument list."""
  1001. return [self.node_id, self.key]
  1002. class ReadDictionaryEdgeInstruction(StateInstruction):
  1003. """An instruction that reads a dictionary edge."""
  1004. def __init__(self, node_id, key):
  1005. StateInstruction.__init__(self)
  1006. self.node_id = node_id
  1007. self.key = key
  1008. def get_opcode(self):
  1009. """Gets the opcode for this state instruction."""
  1010. return "RDE"
  1011. def get_arguments(self):
  1012. """Gets this state instruction's argument list."""
  1013. return [self.node_id, self.key]
  1014. class ReadEdgeInstruction(StateInstruction):
  1015. """An instruction that reads an edge."""
  1016. def __init__(self, node_id):
  1017. StateInstruction.__init__(self)
  1018. self.node_id = node_id
  1019. def get_opcode(self):
  1020. """Gets the opcode for this state instruction."""
  1021. return "RE"
  1022. def get_arguments(self):
  1023. """Gets this state instruction's argument list."""
  1024. return [self.node_id]
  1025. class ReadOutgoingEdgesInstruction(StateInstruction):
  1026. """An instruction that reads all outgoing edges for a node."""
  1027. def __init__(self, node_id):
  1028. StateInstruction.__init__(self)
  1029. self.node_id = node_id
  1030. def get_result_type_impl(self):
  1031. """Gets the type of value produced by this instruction."""
  1032. return PRIMITIVE_RESULT_TYPE
  1033. def get_opcode(self):
  1034. """Gets the opcode for this state instruction."""
  1035. return "RO"
  1036. def get_arguments(self):
  1037. """Gets this state instruction's argument list."""
  1038. return [self.node_id]
  1039. class ReadIncomingEdgesInstruction(StateInstruction):
  1040. """An instruction that reads all incoming edges for a node."""
  1041. def __init__(self, node_id):
  1042. StateInstruction.__init__(self)
  1043. self.node_id = node_id
  1044. def get_result_type_impl(self):
  1045. """Gets the type of value produced by this instruction."""
  1046. return PRIMITIVE_RESULT_TYPE
  1047. def get_opcode(self):
  1048. """Gets the opcode for this state instruction."""
  1049. return "RI"
  1050. def get_arguments(self):
  1051. """Gets this state instruction's argument list."""
  1052. return [self.node_id]
  1053. class CreateNodeInstruction(StateInstruction):
  1054. """An instruction that creates an empty node."""
  1055. def get_opcode(self):
  1056. """Gets the opcode for this state instruction."""
  1057. return "CN"
  1058. def get_arguments(self):
  1059. """Gets this state instruction's argument list."""
  1060. return []
  1061. class CreateNodeWithValueInstruction(StateInstruction):
  1062. """An instruction that creates a node with a given value."""
  1063. def __init__(self, value):
  1064. StateInstruction.__init__(self)
  1065. self.value = value
  1066. def get_opcode(self):
  1067. """Gets the opcode for this state instruction."""
  1068. return "CNV"
  1069. def get_arguments(self):
  1070. """Gets this state instruction's argument list."""
  1071. return [self.value]
  1072. class CreateEdgeInstruction(StateInstruction):
  1073. """An instruction that creates an edge."""
  1074. def __init__(self, source_id, target_id):
  1075. StateInstruction.__init__(self)
  1076. self.source_id = source_id
  1077. self.target_id = target_id
  1078. def get_opcode(self):
  1079. """Gets the opcode for this state instruction."""
  1080. return "CE"
  1081. def get_arguments(self):
  1082. """Gets this state instruction's argument list."""
  1083. return [self.source_id, self.target_id]
  1084. class CreateDictionaryEdgeInstruction(StateInstruction):
  1085. """An instruction that creates a dictionary edge."""
  1086. def __init__(self, source_id, key, target_id):
  1087. StateInstruction.__init__(self)
  1088. self.source_id = source_id
  1089. self.key = key
  1090. self.target_id = target_id
  1091. def get_opcode(self):
  1092. """Gets the opcode for this state instruction."""
  1093. return "CD"
  1094. def get_arguments(self):
  1095. """Gets this state instruction's argument list."""
  1096. return [self.source_id, self.key, self.target_id]
  1097. class DeleteNodeInstruction(StateInstruction):
  1098. """An instruction that deletes a node."""
  1099. def __init__(self, node_id):
  1100. StateInstruction.__init__(self)
  1101. self.node_id = node_id
  1102. def get_result_type_impl(self):
  1103. """Gets the type of value produced by this instruction."""
  1104. return NO_RESULT_TYPE
  1105. def get_opcode(self):
  1106. """Gets the opcode for this state instruction."""
  1107. return "DN"
  1108. def get_arguments(self):
  1109. """Gets this state instruction's argument list."""
  1110. return [self.node_id]
  1111. class DeleteEdgeInstruction(StateInstruction):
  1112. """An instruction that deletes an edge."""
  1113. def __init__(self, edge_id):
  1114. StateInstruction.__init__(self)
  1115. self.edge_id = edge_id
  1116. def get_result_type_impl(self):
  1117. """Gets the type of value produced by this instruction."""
  1118. return NO_RESULT_TYPE
  1119. def get_opcode(self):
  1120. """Gets the opcode for this state instruction."""
  1121. return "DE"
  1122. def get_arguments(self):
  1123. """Gets this state instruction's argument list."""
  1124. return [self.edge_id]
  1125. def create_block(*statements):
  1126. """Creates a block-statement from the given list of statements."""
  1127. length = len(statements)
  1128. if length == 0:
  1129. return EmptyInstruction()
  1130. elif length == 1:
  1131. return statements[0]
  1132. else:
  1133. return CompoundInstruction(
  1134. statements[0],
  1135. create_block(*statements[1:]))
  1136. def create_jit_call(target, named_arguments, kwargs):
  1137. """Creates a call that abides by the JIT's calling convention."""
  1138. # A JIT call looks like this:
  1139. #
  1140. # target = ...
  1141. # arg_dict = { ... }
  1142. # arg_dict.update(kwargs)
  1143. # result, = yield [("CALL_KWARGS", [target, arg_dict])]
  1144. results = []
  1145. if target.has_definition():
  1146. target_tmp = StoreLocalInstruction(None, target)
  1147. results.append(target_tmp)
  1148. target = target_tmp.create_load()
  1149. arg_dict = StoreLocalInstruction(
  1150. None,
  1151. DictionaryLiteralInstruction(
  1152. [(LiteralInstruction(key), val) for key, val in named_arguments]))
  1153. results.append(arg_dict)
  1154. results.append(
  1155. CallInstruction(
  1156. LoadMemberInstruction(arg_dict.create_load(), 'update'),
  1157. [kwargs]))
  1158. return CompoundInstruction(
  1159. create_block(*results),
  1160. RunGeneratorFunctionInstruction(
  1161. target, arg_dict.create_load(), NODE_RESULT_TYPE))
  1162. def create_new_local_node(local_variable, connected_node, edge_variable=None):
  1163. """Creates a local node that is the backing storage for a local variable.
  1164. This node is connected to a given node to make sure it's not perceived
  1165. as dead by the GC. The newly created node is stored in the given
  1166. local variable. The edge's id can also optionally be stored in a variable."""
  1167. local_store = StoreLocalInstruction(local_variable, CreateNodeInstruction())
  1168. create_edge = CreateEdgeInstruction(connected_node, local_store.create_load())
  1169. if edge_variable is not None:
  1170. create_edge = StoreLocalInstruction(edge_variable, create_edge)
  1171. return create_block(local_store, create_edge)
  1172. def map_instruction_tree_top_down(function, instruction):
  1173. """Applies the given mapping function to every instruction in the tree
  1174. that has the given instruction as root. The map is applied in a top-down
  1175. fashion."""
  1176. mapped_instruction = function(instruction)
  1177. return mapped_instruction.create(
  1178. [map_instruction_tree_top_down(function, child)
  1179. for child in mapped_instruction.get_children()])
  1180. def map_and_simplify(function, instruction):
  1181. """Applies the given mapping function to every instruction in the tree
  1182. that has the given instruction as root, and simplifies it on-the-fly.
  1183. The map is applied in a bottom-up fashion.
  1184. This is at least as powerful as first mapping and then simplifying, as
  1185. maps and simplifications are interspersed."""
  1186. return function(
  1187. instruction.create(
  1188. [map_and_simplify(function, child)
  1189. for child in instruction.get_children()])).simplify_node()
  1190. def iterate_as_stack(instruction, stack_iterator):
  1191. """Iterates over the given instruction and its children in the order in which temporaries are
  1192. 'pushed' on and 'popped' from a virtual evaluation stack."""
  1193. if isinstance(instruction, SelectInstruction):
  1194. iterate_as_stack(instruction.condition, stack_iterator)
  1195. stack_iterator.pop()
  1196. if_iterator, else_iterator = stack_iterator.branch(), stack_iterator.branch()
  1197. iterate_as_stack(instruction.if_clause, if_iterator)
  1198. iterate_as_stack(instruction.else_clause, else_iterator)
  1199. stack_iterator.merge(if_iterator, else_iterator)
  1200. elif isinstance(instruction, CompoundInstruction):
  1201. iterate_as_stack(instruction.first, stack_iterator)
  1202. if instruction.second.has_result():
  1203. stack_iterator.pop()
  1204. iterate_as_stack(instruction.second, stack_iterator)
  1205. if not instruction.second.has_result():
  1206. stack_iterator.pop()
  1207. else:
  1208. children = instruction.get_children()
  1209. for child in children:
  1210. # Push all children onto the stack.
  1211. iterate_as_stack(child, stack_iterator)
  1212. for child in children:
  1213. # Pop all children from the stack.
  1214. stack_iterator.pop()
  1215. # Push the instruction.
  1216. stack_iterator.push(instruction)
  1217. class StackIterator(object):
  1218. """A base class for stack iterators."""
  1219. def __init__(self, stack=None):
  1220. self.stack = [] if stack is None else stack
  1221. def pop(self):
  1222. """Pops an instruction from the stack."""
  1223. self.stack.pop()
  1224. def push(self, instruction):
  1225. """Pushes an instruction onto the stack."""
  1226. self.stack.append(set([instruction]))
  1227. def branch(self):
  1228. """Creates a copy of this stack iterator."""
  1229. return self.create(self.copy_stack())
  1230. def merge(self, *branches):
  1231. """Sets this stack iterator's stack to the union of the given branches."""
  1232. self.__init__(self.merge_stacks(*branches))
  1233. def copy_stack(self):
  1234. """Creates a copy of this stack iterator's stack."""
  1235. return [set(vals) for vals in self.stack]
  1236. def create(self, new_stack):
  1237. """Creates a stack iterator from the given stack"""
  1238. return type(self)(new_stack)
  1239. def merge_stacks(self, *branches):
  1240. """Computes the union of the stacks of the given branches."""
  1241. results = None
  1242. for branch in branches:
  1243. if results is None:
  1244. results = branch.copy_stack()
  1245. else:
  1246. assert len(branch.stack) == len(results)
  1247. results = [set.union(*t) for t in zip(branch.stack, results)]
  1248. return results
  1249. def protect_temporaries_from_gc(instruction, connected_node):
  1250. """Protects temporaries from the garbage collector by connecting them to the given node."""
  1251. # # The reasoning behind this function
  1252. #
  1253. # A nop instruction (`yield None`) may trigger the garbage collector, which will delete
  1254. # unreachable ("dead") vertices and edges. Now take into account that a bare temporary node
  1255. # is actually unreachable from the root node. The consequence is that temporary nodes
  1256. # may be garbage-collected if a nop instruction is executed while they are on the evaluation
  1257. # "stack." This is _never_ what we want.
  1258. #
  1259. # To counter this, we can connect temporary nodes to a node that is reachable from the root.
  1260. # However, we only want to create edges between edges and a known reachable node if we really
  1261. # have to, because creating edges incurs some overhead.
  1262. #
  1263. # We will create an edge between a temporary and the known reachable node if and only if the
  1264. # temporary is on the "stack" when either a nop or a call instruction is executed.
  1265. class GCStackIterator(StackIterator):
  1266. """A stack iterator that detects which instructions might be at risk of getting garbage
  1267. collected."""
  1268. def __init__(self, stack=None, gc_temporaries=None):
  1269. StackIterator.__init__(self, stack)
  1270. self.gc_temporaries = set() if gc_temporaries is None else gc_temporaries
  1271. def push(self, instruction):
  1272. """Pushes an instruction onto the stack."""
  1273. if isinstance(instruction, (
  1274. NopInstruction,
  1275. RunGeneratorFunctionInstruction,
  1276. RunTailGeneratorFunctionInstruction)):
  1277. # All values currently on the stack are at risk. Mark them as such.
  1278. for instruction_set in self.stack:
  1279. self.gc_temporaries.update(instruction_set)
  1280. # Proceed.
  1281. StackIterator.push(self, instruction)
  1282. def merge(self, *branches):
  1283. """Sets this stack iterator's stack to the union of the given branches."""
  1284. self.__init__(
  1285. self.merge_stacks(*branches),
  1286. set.union(*[br.gc_temporaries for br in branches]))
  1287. def create(self, new_stack):
  1288. """Creates a stack iterator from the given stack"""
  1289. return GCStackIterator(new_stack, self.gc_temporaries)
  1290. # Find out which instructions are at risk.
  1291. gc_iterator = GCStackIterator()
  1292. iterate_as_stack(instruction, gc_iterator)
  1293. # These temporaries need to be protected from the GC.
  1294. gc_temporaries = gc_iterator.gc_temporaries
  1295. def protect_result(instruction):
  1296. """Protects the given instruction's (temporary) result."""
  1297. if instruction in gc_temporaries and instruction.get_result_type() == NODE_RESULT_TYPE:
  1298. gc_temporaries.remove(instruction)
  1299. store_instr = StoreLocalInstruction(None, instruction)
  1300. return CompoundInstruction(
  1301. store_instr,
  1302. CompoundInstruction(
  1303. SelectInstruction(
  1304. BinaryInstruction(
  1305. store_instr.create_load(), 'is not', LiteralInstruction(None)),
  1306. CreateEdgeInstruction(connected_node, store_instr.create_load()),
  1307. EmptyInstruction()),
  1308. store_instr.create_load()))
  1309. else:
  1310. return instruction
  1311. return map_instruction_tree_top_down(protect_result, instruction)
  1312. if __name__ == "__main__":
  1313. example_tree = SelectInstruction(
  1314. LiteralInstruction(True),
  1315. LoopInstruction(
  1316. CompoundInstruction(
  1317. BreakInstruction(),
  1318. CompoundInstruction(
  1319. EmptyInstruction(),
  1320. ContinueInstruction()
  1321. )
  1322. )
  1323. ),
  1324. ReturnInstruction(
  1325. EmptyInstruction()))
  1326. print(example_tree.simplify())