tree_ir.py 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798
  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_result_temporary():
  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. return [val for _, val in self.key_value_pairs]
  685. def create(self, new_children):
  686. """Creates a new instruction of this type from the given sequence of child instructions."""
  687. keys = [k for k, _ in self.key_value_pairs]
  688. return DictionaryLiteralInstruction(zip(keys, new_children))
  689. def has_definition_impl(self):
  690. """Tells if this instruction requires a definition."""
  691. return any(
  692. [key.has_definition() or val.has_definition()
  693. for key, val in self.key_value_pairs])
  694. def has_result_temporary(self):
  695. """Tells if this instruction stores its result in a temporary."""
  696. return False
  697. def simplify(self):
  698. """Applies basic simplification to this instruction and its children."""
  699. return DictionaryLiteralInstruction(
  700. [(key.simplify(), val.simplify()) for key, val in self.key_value_pairs])
  701. def generate_python_def(self, code_generator):
  702. """Generates a Python statement that executes this instruction.
  703. The statement is appended immediately to the code generator."""
  704. for key, val in self.key_value_pairs:
  705. if key.has_definition():
  706. key.generate_python_def(code_generator)
  707. if val.has_definition():
  708. val.generate_python_def(code_generator)
  709. def generate_python_use(self, code_generator):
  710. """Generates a Python expression that retrieves this instruction's
  711. result. The expression is returned as a string."""
  712. return '{%s}' % ', '.join(
  713. ['%s : %s' % (
  714. key.generate_python_use(code_generator),
  715. val.generate_python_use(code_generator))
  716. for key, val in self.key_value_pairs])
  717. def __repr__(self):
  718. return "DictionaryLiteralInstruction(%r)" % self.key_value_pairs
  719. class ListSliceInstruction(Instruction):
  720. """Slices a list."""
  721. def __init__(self, seq, start, end, step):
  722. Instruction.__init__(self)
  723. self.seq = seq
  724. self.start = start
  725. self.end = end
  726. self.step = step
  727. def get_children(self):
  728. """Gets this instruction's sequence of child instructions."""
  729. all_items = (self.seq, self.start, self.end, self.step)
  730. return [item for item in all_items if item is not None]
  731. def create(self, new_children):
  732. """Creates a new instruction of this type from the given sequence of child instructions."""
  733. # pylint: disable=I0011,E1120
  734. args = []
  735. i = 0
  736. for old_item in (self.seq, self.start, self.end, self.step):
  737. if old_item is None:
  738. args.append(None)
  739. else:
  740. args.append(new_children[i])
  741. i += 1
  742. assert len(new_children) == i
  743. assert len(args) == 4
  744. return ListSliceInstruction(*args)
  745. def has_definition_impl(self):
  746. """Tells if this instruction requires a definition."""
  747. return any([item.has_definition() for item in self.get_children()])
  748. def has_result_temporary(self):
  749. """Tells if this instruction stores its result in a temporary."""
  750. return False
  751. def generate_python_def(self, code_generator):
  752. """Generates a Python statement that executes this instruction.
  753. The statement is appended immediately to the code generator."""
  754. for item in self.get_children():
  755. if item.has_definition():
  756. item.generate_python_def(code_generator)
  757. def generate_python_use(self, code_generator):
  758. """Generates a Python expression that retrieves this instruction's
  759. result. The expression is returned as a string."""
  760. return '%s[%s:%s:%s]' % (
  761. self.seq.generate_python_use(code_generator),
  762. '' if self.start is None else self.start.generate_python_use(code_generator),
  763. '' if self.end is None else self.end.generate_python_use(code_generator),
  764. '' if self.step is None else self.step.generate_python_use(code_generator))
  765. def __repr__(self):
  766. return "ListSliceInstruction(%r, %r, %r, %r)" % (self.seq, self.start, self.end, self.step)
  767. class StateInstruction(Instruction):
  768. """An instruction that accesses the modelverse state."""
  769. def get_result_type_impl(self):
  770. """Gets the type of value produced by this instruction."""
  771. return NODE_RESULT_TYPE
  772. def get_opcode(self):
  773. """Gets the opcode for this state instruction."""
  774. raise NotImplementedError()
  775. def get_arguments(self):
  776. """Gets this state instruction's argument list."""
  777. raise NotImplementedError()
  778. def get_children(self):
  779. """Gets this instruction's sequence of child instructions."""
  780. return self.get_arguments()
  781. def create(self, new_children):
  782. """Creates a new instruction of this type from the given sequence of child instructions."""
  783. return type(self)(*new_children)
  784. def generate_python_def(self, code_generator):
  785. """Generates a Python statement that executes this instruction.
  786. The statement is appended immediately to the code generator."""
  787. args = self.get_arguments()
  788. for arg_i in args:
  789. if arg_i.has_definition():
  790. arg_i.generate_python_def(code_generator)
  791. code_generator.append_state_definition(self, self.get_opcode(), args)
  792. def __repr__(self):
  793. return "StateInstruction(%s)" % ', '.join(map(repr, self.get_arguments()))
  794. class RunGeneratorFunctionInstruction(StateInstruction):
  795. """An instruction that runs a generator function."""
  796. def __init__(self, function, argument_dict, result_type=PRIMITIVE_RESULT_TYPE):
  797. StateInstruction.__init__(self)
  798. self.function = function
  799. self.argument_dict = argument_dict
  800. self.result_type_cache = result_type
  801. def get_opcode(self):
  802. """Gets the opcode for this state instruction."""
  803. return "CALL_KWARGS"
  804. def get_arguments(self):
  805. """Gets this state instruction's argument list."""
  806. return [self.function, self.argument_dict]
  807. def create(self, new_children):
  808. """Creates a new instruction of this type from the given sequence of child instructions."""
  809. func, arg_dict = new_children
  810. return RunGeneratorFunctionInstruction(func, arg_dict, self.get_result_type())
  811. class RunTailGeneratorFunctionInstruction(StateInstruction):
  812. """An instruction that runs a generator function."""
  813. def __init__(self, function, argument_dict):
  814. StateInstruction.__init__(self)
  815. self.function = function
  816. self.argument_dict = argument_dict
  817. def get_result_type_impl(self):
  818. """Gets the type of value produced by this instruction."""
  819. return NO_RESULT_TYPE
  820. def get_opcode(self):
  821. """Gets the opcode for this state instruction."""
  822. return "TAIL_CALL_KWARGS"
  823. def get_arguments(self):
  824. """Gets this state instruction's argument list."""
  825. return [self.function, self.argument_dict]
  826. class VariableName(object):
  827. """A data structure that unifies names across instructions that access the
  828. same variable."""
  829. def __init__(self, name):
  830. self.name = name
  831. def get_result_name_override(self, _):
  832. """Gets a value that overrides the code generator's result name for this
  833. instruction if it is not None."""
  834. return self.name
  835. def __str__(self):
  836. return self.name
  837. def __repr__(self):
  838. return 'VariableName(%r)' % self.name
  839. class VariableInstruction(Instruction):
  840. """A base class for instructions that access variables."""
  841. def __init__(self, name):
  842. Instruction.__init__(self)
  843. if isinstance(name, str) or isinstance(name, unicode) or name is None:
  844. self.name = VariableName(name)
  845. else:
  846. self.name = name
  847. def get_children(self):
  848. """Gets this instruction's sequence of child instructions."""
  849. raise NotImplementedError()
  850. def create(self, new_children):
  851. """Creates a new instruction of this type from the given sequence of child instructions."""
  852. raise NotImplementedError()
  853. def get_result_name_override(self, code_generator):
  854. """Gets a value that overrides the code generator's result name for this
  855. instruction if it is not None."""
  856. return code_generator.get_result_name(self.name)
  857. class LocalInstruction(VariableInstruction):
  858. """A base class for instructions that access local variables."""
  859. def get_children(self):
  860. """Gets this instruction's sequence of child instructions."""
  861. raise NotImplementedError()
  862. def create(self, new_children):
  863. """Creates a new instruction of this type from the given sequence of child instructions."""
  864. raise NotImplementedError()
  865. def create_load(self):
  866. """Creates an instruction that loads the variable referenced by this instruction."""
  867. return LoadLocalInstruction(self.name)
  868. def create_store(self, value):
  869. """Creates an instruction that stores the given value in the variable referenced
  870. by this instruction."""
  871. return StoreLocalInstruction(self.name, value)
  872. class StoreLocalInstruction(LocalInstruction):
  873. """An instruction that stores a value in a local variable."""
  874. def __init__(self, name, value):
  875. LocalInstruction.__init__(self, name)
  876. self.value = value
  877. def get_children(self):
  878. """Gets this instruction's sequence of child instructions."""
  879. return [self.value]
  880. def create(self, new_children):
  881. """Creates a new instruction of this type from the given sequence of child instructions."""
  882. val, = new_children
  883. return StoreLocalInstruction(self.name, val)
  884. def generate_python_def(self, code_generator):
  885. """Generates a Python statement that executes this instruction.
  886. The statement is appended immediately to the code generator."""
  887. code_generator.append_move_definition(self, self.value)
  888. def __repr__(self):
  889. return 'StoreLocalInstruction(%r, %r)' % (self.name, self.value)
  890. class TupleStoreLocalInstruction(VoidInstruction):
  891. """Assigns a number of values to a number of variables."""
  892. def __init__(self, name_value_pairs):
  893. VoidInstruction.__init__(self)
  894. self.name_value_pairs = name_value_pairs
  895. def get_children(self):
  896. """Gets this instruction's sequence of child instructions."""
  897. return [val for _, val in self.name_value_pairs]
  898. def has_result_temporary(self):
  899. """Tells if this instruction stores its result in a temporary."""
  900. return False
  901. def create(self, new_children):
  902. """Creates a new instruction of this type from the given sequence of child instructions."""
  903. new_name_value_pairs = [
  904. (name, new_value) for (name, _), new_value in zip(self.name_value_pairs, new_children)]
  905. return TupleStoreLocalInstruction(new_name_value_pairs)
  906. def generate_python_def(self, code_generator):
  907. """Generates a Python statement that executes this instruction.
  908. The statement is appended immediately to the code generator."""
  909. tuple_lhs = []
  910. tuple_rhs = []
  911. for name, value in self.name_value_pairs:
  912. if isinstance(name, str) or isinstance(name, unicode) or name is None:
  913. variable = VariableName(name)
  914. else:
  915. variable = name
  916. # Retrieve the result name for the variable.
  917. var_result_name = code_generator.get_result_name(variable)
  918. # Encourage the value to take on the same result name as the variable.
  919. value_result_name = code_generator.get_result_name(value, var_result_name)
  920. if value.has_definition():
  921. # Generate the value' definition.
  922. value.generate_python_def(code_generator)
  923. # Only perform an assignment if it's truly necessary.
  924. if var_result_name != value_result_name or not value.has_result_temporary():
  925. tuple_lhs.append(var_result_name)
  926. tuple_rhs.append(value.generate_python_use(code_generator))
  927. if len(tuple_lhs) > 0:
  928. code_generator.append_line(
  929. '%s = %s' % (', '.join(tuple_lhs), ', '.join(tuple_rhs)))
  930. def __repr__(self):
  931. return 'TupleStoreLocalInstruction(%r)' % (self.name_value_pairs)
  932. class LoadLocalInstruction(LocalInstruction):
  933. """An instruction that loads a value from a local variable."""
  934. def has_definition_impl(self):
  935. """Tells if this instruction requires a definition."""
  936. return False
  937. def get_children(self):
  938. """Gets this instruction's sequence of child instructions."""
  939. return []
  940. def create(self, new_children):
  941. """Creates a new instruction of this type from the given sequence of child instructions."""
  942. return self
  943. def __repr__(self):
  944. return 'LoadLocalInstruction(%r)' % self.name
  945. class DefineFunctionInstruction(VariableInstruction):
  946. """An instruction that defines a function."""
  947. def __init__(self, name, parameter_list, body):
  948. VariableInstruction.__init__(self, name)
  949. self.parameter_list = parameter_list
  950. self.body = body
  951. def get_children(self):
  952. """Gets this instruction's sequence of child instructions."""
  953. return [self.body]
  954. def create(self, new_children):
  955. """Creates a new instruction of this type from the given sequence of child instructions."""
  956. body, = new_children
  957. return DefineFunctionInstruction(self.name, self.parameter_list, body)
  958. def generate_python_def(self, code_generator):
  959. """Generates a Python statement that executes this instruction.
  960. The statement is appended immediately to the code generator."""
  961. code_generator.append_line('def %s(%s):' % (
  962. code_generator.get_result_name(self), ', '.join(self.parameter_list)))
  963. code_generator.increase_indentation()
  964. self.body.generate_python_def(code_generator)
  965. code_generator.decrease_indentation()
  966. def __repr__(self):
  967. return 'DefineFunctionInstruction(%r, %r, %r)' % (self.name, self.parameter_list, self.body)
  968. class LocalExistsInstruction(LocalInstruction):
  969. """An instruction that checks if a local variable exists."""
  970. def has_definition_impl(self):
  971. """Tells if this instruction requires a definition."""
  972. return False
  973. def get_children(self):
  974. """Gets this instruction's sequence of child instructions."""
  975. return []
  976. def create(self, new_children):
  977. """Creates a new instruction of this type from the given sequence of child instructions."""
  978. return self
  979. def generate_python_use(self, code_generator):
  980. """Generates a Python expression that retrieves this instruction's
  981. result. The expression is returned as a string."""
  982. return "'%s' in locals()" % self.get_result_name_override(code_generator)
  983. def __repr__(self):
  984. return 'LocalExistsInstruction(%r)' % self.name
  985. class LoadGlobalInstruction(VariableInstruction):
  986. """An instruction that loads a value from a global variable."""
  987. def has_definition_impl(self):
  988. """Tells if this instruction requires a definition."""
  989. return False
  990. def get_children(self):
  991. """Gets this instruction's sequence of child instructions."""
  992. return []
  993. def create(self, new_children):
  994. """Creates a new instruction of this type from the given sequence of child instructions."""
  995. return self
  996. def __repr__(self):
  997. return 'LoadGlobalInstruction(%r)' % self.name
  998. class LoadIndexInstruction(Instruction):
  999. """An instruction that produces a value by indexing a specified expression with
  1000. a given key."""
  1001. def __init__(self, indexed, key):
  1002. Instruction.__init__(self)
  1003. self.indexed = indexed
  1004. self.key = key
  1005. def has_definition_impl(self):
  1006. """Tells if this instruction requires a definition."""
  1007. return self.indexed.has_definition() or self.key.has_definition()
  1008. def get_children(self):
  1009. """Gets this instruction's sequence of child instructions."""
  1010. return [self.indexed, self.key]
  1011. def has_result_temporary(self):
  1012. """Tells if this instruction stores its result in a temporary."""
  1013. return False
  1014. def create(self, new_children):
  1015. """Creates a new instruction of this type from the given sequence of child instructions."""
  1016. indexed, key = new_children
  1017. return LoadIndexInstruction(indexed, key)
  1018. def generate_python_use(self, code_generator):
  1019. """Generates a Python expression that retrieves this instruction's
  1020. result. The expression is returned as a string."""
  1021. return "%s[%s]" % (
  1022. self.indexed.generate_python_use(code_generator),
  1023. self.key.generate_python_use(code_generator))
  1024. def generate_python_def(self, code_generator):
  1025. """Generates a Python statement that executes this instruction.
  1026. The statement is appended immediately to the code generator."""
  1027. indexed_has_def, key_has_def = self.indexed.has_definition(), self.key.has_definition()
  1028. if indexed_has_def:
  1029. self.indexed.generate_python_def(code_generator)
  1030. if key_has_def:
  1031. self.key.generate_python_def(code_generator)
  1032. if not indexed_has_def and not key_has_def:
  1033. code_generator.append_line('pass')
  1034. def __repr__(self):
  1035. return 'LoadIndexInstruction(%r, %r)' % (self.indexed, self.key)
  1036. class LoadMemberInstruction(Instruction):
  1037. """An instruction that produces a value by loading a member from a container."""
  1038. def __init__(self, container, member_name):
  1039. Instruction.__init__(self)
  1040. self.container = container
  1041. self.member_name = member_name
  1042. def has_definition_impl(self):
  1043. """Tells if this instruction requires a definition."""
  1044. return self.container.has_definition()
  1045. def get_children(self):
  1046. """Gets this instruction's sequence of child instructions."""
  1047. return [self.container]
  1048. def has_result_temporary(self):
  1049. """Tells if this instruction stores its result in a temporary."""
  1050. return False
  1051. def create(self, new_children):
  1052. """Creates a new instruction of this type from the given sequence of child instructions."""
  1053. container, = new_children
  1054. return LoadMemberInstruction(container, self.member_name)
  1055. def generate_python_def(self, code_generator):
  1056. """Generates a Python statement that executes this instruction.
  1057. The statement is appended immediately to the code generator."""
  1058. self.container.generate_python_def(code_generator)
  1059. def generate_python_use(self, code_generator):
  1060. """Generates a Python expression that retrieves this instruction's
  1061. result. The expression is returned as a string."""
  1062. return "%s.%s" % (
  1063. self.container.generate_python_use(code_generator),
  1064. self.member_name)
  1065. def __repr__(self):
  1066. return 'LoadMemberInstruction(%r, %r)' % (self.container, self.member_name)
  1067. class StoreMemberInstruction(VoidInstruction):
  1068. """An instruction that stores a value in a container member."""
  1069. def __init__(self, container, member_name, value):
  1070. VoidInstruction.__init__(self)
  1071. self.container = container
  1072. self.member_name = member_name
  1073. self.value = value
  1074. def has_definition_impl(self):
  1075. """Tells if this instruction requires a definition."""
  1076. return True
  1077. def get_children(self):
  1078. """Gets this instruction's sequence of child instructions."""
  1079. return [self.container, self.value]
  1080. def create(self, new_children):
  1081. """Creates a new instruction of this type from the given sequence of child instructions."""
  1082. container, value = new_children
  1083. return StoreMemberInstruction(container, self.member_name, value)
  1084. def generate_python_def(self, code_generator):
  1085. """Generates a Python statement that executes this instruction.
  1086. The statement is appended immediately to the code generator."""
  1087. if self.container.has_definition():
  1088. self.container.generate_python_def(code_generator)
  1089. code_generator.append_line('%s.%s = %s' % (
  1090. self.container.generate_python_use(code_generator),
  1091. self.member_name,
  1092. self.value.generate_python_use(code_generator)))
  1093. def __repr__(self):
  1094. return 'StoreMemberInstruction(%r, %r, %r)' % (self.container, self.member_name, self.value)
  1095. class NopInstruction(VoidInstruction):
  1096. """A nop instruction, which allows for the kernel's thread of execution to be interrupted."""
  1097. def generate_python_def(self, code_generator):
  1098. """Generates a Python statement that executes this instruction.
  1099. The statement is appended immediately to the code generator."""
  1100. code_generator.append_line('yield %s' % repr(NOP_LITERAL))
  1101. def __repr__(self):
  1102. return 'NopInstruction()'
  1103. class ReadValueInstruction(StateInstruction):
  1104. """An instruction that reads a value from a node."""
  1105. def __init__(self, node_id):
  1106. StateInstruction.__init__(self)
  1107. self.node_id = node_id
  1108. def get_result_type_impl(self):
  1109. """Gets the type of value produced by this instruction."""
  1110. return PRIMITIVE_RESULT_TYPE
  1111. def simplify_node(self):
  1112. """Applies basic simplification to this instruction only."""
  1113. if isinstance(self.node_id, CreateNodeWithValueInstruction):
  1114. return self.node_id.value
  1115. else:
  1116. return self
  1117. def get_opcode(self):
  1118. """Gets the opcode for this state instruction."""
  1119. return "RV"
  1120. def get_arguments(self):
  1121. """Gets this state instruction's argument list."""
  1122. return [self.node_id]
  1123. class ReadDictionaryValueInstruction(StateInstruction):
  1124. """An instruction that reads a dictionary value."""
  1125. def __init__(self, node_id, key):
  1126. StateInstruction.__init__(self)
  1127. self.node_id = node_id
  1128. self.key = key
  1129. def get_opcode(self):
  1130. """Gets the opcode for this state instruction."""
  1131. return "RD"
  1132. def get_arguments(self):
  1133. """Gets this state instruction's argument list."""
  1134. return [self.node_id, self.key]
  1135. class ReadDictionaryEdgeInstruction(StateInstruction):
  1136. """An instruction that reads a dictionary edge."""
  1137. def __init__(self, node_id, key):
  1138. StateInstruction.__init__(self)
  1139. self.node_id = node_id
  1140. self.key = key
  1141. def get_opcode(self):
  1142. """Gets the opcode for this state instruction."""
  1143. return "RDE"
  1144. def get_arguments(self):
  1145. """Gets this state instruction's argument list."""
  1146. return [self.node_id, self.key]
  1147. class ReadDictionaryKeysInstruction(StateInstruction):
  1148. """An instruction that reads all keys from a dictionary."""
  1149. def __init__(self, node_id):
  1150. StateInstruction.__init__(self)
  1151. self.node_id = node_id
  1152. def get_opcode(self):
  1153. """Gets the opcode for this state instruction."""
  1154. return "RDK"
  1155. def get_arguments(self):
  1156. """Gets this state instruction's argument list."""
  1157. return [self.node_id]
  1158. class ReadEdgeInstruction(StateInstruction):
  1159. """An instruction that reads an edge."""
  1160. def __init__(self, node_id):
  1161. StateInstruction.__init__(self)
  1162. self.node_id = node_id
  1163. def get_opcode(self):
  1164. """Gets the opcode for this state instruction."""
  1165. return "RE"
  1166. def get_arguments(self):
  1167. """Gets this state instruction's argument list."""
  1168. return [self.node_id]
  1169. class ReadOutgoingEdgesInstruction(StateInstruction):
  1170. """An instruction that reads all outgoing edges for a node."""
  1171. def __init__(self, node_id):
  1172. StateInstruction.__init__(self)
  1173. self.node_id = node_id
  1174. def get_result_type_impl(self):
  1175. """Gets the type of value produced by this instruction."""
  1176. return PRIMITIVE_RESULT_TYPE
  1177. def get_opcode(self):
  1178. """Gets the opcode for this state instruction."""
  1179. return "RO"
  1180. def get_arguments(self):
  1181. """Gets this state instruction's argument list."""
  1182. return [self.node_id]
  1183. class ReadIncomingEdgesInstruction(StateInstruction):
  1184. """An instruction that reads all incoming edges for a node."""
  1185. def __init__(self, node_id):
  1186. StateInstruction.__init__(self)
  1187. self.node_id = node_id
  1188. def get_result_type_impl(self):
  1189. """Gets the type of value produced by this instruction."""
  1190. return PRIMITIVE_RESULT_TYPE
  1191. def get_opcode(self):
  1192. """Gets the opcode for this state instruction."""
  1193. return "RI"
  1194. def get_arguments(self):
  1195. """Gets this state instruction's argument list."""
  1196. return [self.node_id]
  1197. class CreateNodeInstruction(StateInstruction):
  1198. """An instruction that creates an empty node."""
  1199. def get_opcode(self):
  1200. """Gets the opcode for this state instruction."""
  1201. return "CN"
  1202. def get_arguments(self):
  1203. """Gets this state instruction's argument list."""
  1204. return []
  1205. class CreateNodeWithValueInstruction(StateInstruction):
  1206. """An instruction that creates a node with a given value."""
  1207. def __init__(self, value):
  1208. StateInstruction.__init__(self)
  1209. self.value = value
  1210. def get_opcode(self):
  1211. """Gets the opcode for this state instruction."""
  1212. return "CNV"
  1213. def get_arguments(self):
  1214. """Gets this state instruction's argument list."""
  1215. return [self.value]
  1216. class CreateEdgeInstruction(StateInstruction):
  1217. """An instruction that creates an edge."""
  1218. def __init__(self, source_id, target_id):
  1219. StateInstruction.__init__(self)
  1220. self.source_id = source_id
  1221. self.target_id = target_id
  1222. def get_opcode(self):
  1223. """Gets the opcode for this state instruction."""
  1224. return "CE"
  1225. def get_arguments(self):
  1226. """Gets this state instruction's argument list."""
  1227. return [self.source_id, self.target_id]
  1228. class CreateDictionaryEdgeInstruction(StateInstruction):
  1229. """An instruction that creates a dictionary edge."""
  1230. def __init__(self, source_id, key, target_id):
  1231. StateInstruction.__init__(self)
  1232. self.source_id = source_id
  1233. self.key = key
  1234. self.target_id = target_id
  1235. def get_opcode(self):
  1236. """Gets the opcode for this state instruction."""
  1237. return "CD"
  1238. def get_arguments(self):
  1239. """Gets this state instruction's argument list."""
  1240. return [self.source_id, self.key, self.target_id]
  1241. class DeleteNodeInstruction(StateInstruction):
  1242. """An instruction that deletes a node."""
  1243. def __init__(self, node_id):
  1244. StateInstruction.__init__(self)
  1245. self.node_id = node_id
  1246. def get_result_type_impl(self):
  1247. """Gets the type of value produced by this instruction."""
  1248. return NO_RESULT_TYPE
  1249. def get_opcode(self):
  1250. """Gets the opcode for this state instruction."""
  1251. return "DN"
  1252. def get_arguments(self):
  1253. """Gets this state instruction's argument list."""
  1254. return [self.node_id]
  1255. class DeleteEdgeInstruction(StateInstruction):
  1256. """An instruction that deletes an edge."""
  1257. def __init__(self, edge_id):
  1258. StateInstruction.__init__(self)
  1259. self.edge_id = edge_id
  1260. def get_result_type_impl(self):
  1261. """Gets the type of value produced by this instruction."""
  1262. return NO_RESULT_TYPE
  1263. def get_opcode(self):
  1264. """Gets the opcode for this state instruction."""
  1265. return "DE"
  1266. def get_arguments(self):
  1267. """Gets this state instruction's argument list."""
  1268. return [self.edge_id]
  1269. def create_block(*statements):
  1270. """Creates a block-statement from the given list of statements."""
  1271. length = len(statements)
  1272. if length == 0:
  1273. return EmptyInstruction()
  1274. elif length == 1:
  1275. return statements[0]
  1276. else:
  1277. return CompoundInstruction(
  1278. statements[0],
  1279. create_block(*statements[1:]))
  1280. def create_jit_call(target, named_arguments, kwargs):
  1281. """Creates a call that abides by the JIT's calling convention."""
  1282. # A JIT call looks like this:
  1283. #
  1284. # target = ...
  1285. # arg_dict = { ... }
  1286. # arg_dict.update(kwargs)
  1287. # result, = yield [("CALL_KWARGS", [target, arg_dict])]
  1288. results = []
  1289. if target.has_definition():
  1290. target_tmp = StoreLocalInstruction(None, target)
  1291. results.append(target_tmp)
  1292. target = target_tmp.create_load()
  1293. arg_dict = StoreLocalInstruction(
  1294. None,
  1295. DictionaryLiteralInstruction(
  1296. [(LiteralInstruction(key), val) for key, val in named_arguments]))
  1297. results.append(arg_dict)
  1298. results.append(
  1299. CallInstruction(
  1300. LoadMemberInstruction(arg_dict.create_load(), 'update'),
  1301. [kwargs]))
  1302. return CompoundInstruction(
  1303. create_block(*results),
  1304. RunGeneratorFunctionInstruction(
  1305. target, arg_dict.create_load(), NODE_RESULT_TYPE))
  1306. def evaluate_and_load(value):
  1307. """Creates a statement that evaluates the given tree, and creates
  1308. an expression that loads the result. These instructions are returned
  1309. as a pair of trees."""
  1310. if isinstance(value, (LoadLocalInstruction, LiteralInstruction)):
  1311. return EmptyInstruction(), value
  1312. else:
  1313. store = StoreLocalInstruction(None, value)
  1314. return IgnoreInstruction(store), store.create_load()
  1315. def create_new_local_node(local_variable, connected_node, edge_variable=None):
  1316. """Creates a local node that is the backing storage for a local variable.
  1317. This node is connected to a given node to make sure it's not perceived
  1318. as dead by the GC. The newly created node is stored in the given
  1319. local variable. The edge's id can also optionally be stored in a variable."""
  1320. local_store = StoreLocalInstruction(local_variable, CreateNodeInstruction())
  1321. create_edge = CreateEdgeInstruction(connected_node, local_store.create_load())
  1322. if edge_variable is not None:
  1323. create_edge = StoreLocalInstruction(edge_variable, create_edge)
  1324. return create_block(IgnoreInstruction(local_store), IgnoreInstruction(create_edge))
  1325. def map_instruction_tree_top_down(function, instruction):
  1326. """Applies the given mapping function to every instruction in the tree
  1327. that has the given instruction as root. The map is applied in a top-down
  1328. fashion."""
  1329. mapped_instruction = function(instruction)
  1330. return mapped_instruction.create(
  1331. [map_instruction_tree_top_down(function, child)
  1332. for child in mapped_instruction.get_children()])
  1333. def map_and_simplify(function, instruction):
  1334. """Applies the given mapping function to every instruction in the tree
  1335. that has the given instruction as root, and simplifies it on-the-fly.
  1336. The map is applied in a bottom-up fashion.
  1337. This is at least as powerful as first mapping and then simplifying, as
  1338. maps and simplifications are interspersed."""
  1339. return function(
  1340. instruction.create(
  1341. [map_and_simplify(function, child)
  1342. for child in instruction.get_children()])).simplify_node()
  1343. def iterate_as_stack(instruction, stack_iterator):
  1344. """Iterates over the given instruction and its children in the order in which temporaries are
  1345. 'pushed' on and 'popped' from a virtual evaluation stack."""
  1346. if isinstance(instruction, SelectInstruction):
  1347. iterate_as_stack(instruction.condition, stack_iterator)
  1348. stack_iterator.pop()
  1349. if_iterator, else_iterator = stack_iterator.branch(), stack_iterator.branch()
  1350. iterate_as_stack(instruction.if_clause, if_iterator)
  1351. iterate_as_stack(instruction.else_clause, else_iterator)
  1352. stack_iterator.merge(if_iterator, else_iterator)
  1353. elif isinstance(instruction, CompoundInstruction):
  1354. iterate_as_stack(instruction.first, stack_iterator)
  1355. if instruction.second.has_result():
  1356. stack_iterator.pop()
  1357. iterate_as_stack(instruction.second, stack_iterator)
  1358. if not instruction.second.has_result():
  1359. stack_iterator.pop()
  1360. else:
  1361. children = instruction.get_children()
  1362. for child in children:
  1363. # Push all children onto the stack.
  1364. iterate_as_stack(child, stack_iterator)
  1365. for child in children:
  1366. # Pop all children from the stack.
  1367. stack_iterator.pop()
  1368. # Push the instruction.
  1369. stack_iterator.push(instruction)
  1370. class StackIterator(object):
  1371. """A base class for stack iterators."""
  1372. def __init__(self, stack=None):
  1373. self.stack = [] if stack is None else stack
  1374. def pop(self):
  1375. """Pops an instruction from the stack."""
  1376. self.stack.pop()
  1377. def push(self, instruction):
  1378. """Pushes an instruction onto the stack."""
  1379. self.stack.append(set([instruction]))
  1380. def branch(self):
  1381. """Creates a copy of this stack iterator."""
  1382. return self.create(self.copy_stack())
  1383. def merge(self, *branches):
  1384. """Sets this stack iterator's stack to the union of the given branches."""
  1385. self.__init__(self.merge_stacks(*branches))
  1386. def copy_stack(self):
  1387. """Creates a copy of this stack iterator's stack."""
  1388. return [set(vals) for vals in self.stack]
  1389. def create(self, new_stack):
  1390. """Creates a stack iterator from the given stack"""
  1391. return type(self)(new_stack)
  1392. def merge_stacks(self, *branches):
  1393. """Computes the union of the stacks of the given branches."""
  1394. results = None
  1395. for branch in branches:
  1396. if results is None:
  1397. results = branch.copy_stack()
  1398. else:
  1399. assert len(branch.stack) == len(results)
  1400. results = [set.union(*t) for t in zip(branch.stack, results)]
  1401. return results
  1402. def protect_temporaries_from_gc(instruction, connected_node):
  1403. """Protects temporaries from the garbage collector by connecting them to the given node."""
  1404. # # The reasoning behind this function
  1405. #
  1406. # A nop instruction (`yield None`) may trigger the garbage collector, which will delete
  1407. # unreachable ("dead") vertices and edges. Now take into account that a bare temporary node
  1408. # is actually unreachable from the root node. The consequence is that temporary nodes
  1409. # may be garbage-collected if a nop instruction is executed while they are on the evaluation
  1410. # "stack." This is _never_ what we want.
  1411. #
  1412. # To counter this, we can connect temporary nodes to a node that is reachable from the root.
  1413. # However, we only want to create edges between edges and a known reachable node if we really
  1414. # have to, because creating edges incurs some overhead.
  1415. #
  1416. # We will create an edge between a temporary and the known reachable node if and only if the
  1417. # temporary is on the "stack" when either a nop or a call instruction is executed.
  1418. class GCStackIterator(StackIterator):
  1419. """A stack iterator that detects which instructions might be at risk of getting garbage
  1420. collected."""
  1421. def __init__(self, stack=None, gc_temporaries=None):
  1422. StackIterator.__init__(self, stack)
  1423. self.gc_temporaries = set() if gc_temporaries is None else gc_temporaries
  1424. def push(self, instruction):
  1425. """Pushes an instruction onto the stack."""
  1426. if isinstance(instruction, (
  1427. NopInstruction,
  1428. RunGeneratorFunctionInstruction,
  1429. RunTailGeneratorFunctionInstruction)):
  1430. # All values currently on the stack are at risk. Mark them as such.
  1431. for instruction_set in self.stack:
  1432. self.gc_temporaries.update(instruction_set)
  1433. # Proceed.
  1434. StackIterator.push(self, instruction)
  1435. def merge(self, *branches):
  1436. """Sets this stack iterator's stack to the union of the given branches."""
  1437. self.__init__(
  1438. self.merge_stacks(*branches),
  1439. set.union(*[br.gc_temporaries for br in branches]))
  1440. def create(self, new_stack):
  1441. """Creates a stack iterator from the given stack"""
  1442. return GCStackIterator(new_stack, self.gc_temporaries)
  1443. # Find out which instructions are at risk.
  1444. gc_iterator = GCStackIterator()
  1445. iterate_as_stack(instruction, gc_iterator)
  1446. # These temporaries need to be protected from the GC.
  1447. gc_temporaries = gc_iterator.gc_temporaries
  1448. def protect_result(instruction):
  1449. """Protects the given instruction's (temporary) result."""
  1450. if instruction in gc_temporaries and instruction.get_result_type() == NODE_RESULT_TYPE:
  1451. gc_temporaries.remove(instruction)
  1452. store_instr = StoreLocalInstruction(None, instruction)
  1453. return CompoundInstruction(
  1454. store_instr,
  1455. CompoundInstruction(
  1456. SelectInstruction(
  1457. BinaryInstruction(
  1458. store_instr.create_load(), 'is not', LiteralInstruction(None)),
  1459. CreateEdgeInstruction(connected_node, store_instr.create_load()),
  1460. EmptyInstruction()),
  1461. store_instr.create_load()))
  1462. else:
  1463. return instruction
  1464. return map_instruction_tree_top_down(protect_result, instruction)
  1465. if __name__ == "__main__":
  1466. example_tree = SelectInstruction(
  1467. LiteralInstruction(True),
  1468. LoopInstruction(
  1469. CompoundInstruction(
  1470. BreakInstruction(),
  1471. CompoundInstruction(
  1472. EmptyInstruction(),
  1473. ContinueInstruction()
  1474. )
  1475. )
  1476. ),
  1477. ReturnInstruction(
  1478. EmptyInstruction()))
  1479. print(example_tree.simplify())