tree_ir.py 76 KB

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