tree_ir.py 73 KB

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