cfg_to_tree.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. """Lowers CFG-IR to tree-IR."""
  2. import modelverse_jit.cfg_ir as cfg_ir
  3. import modelverse_jit.cfg_optimization as cfg_optimization
  4. import modelverse_jit.tree_ir as tree_ir
  5. import modelverse_jit.runtime as jit_runtime
  6. import modelverse_jit.bytecode_to_tree as bytecode_to_tree
  7. # The CFG deconstruction code here is based on the relooper algorithm
  8. # as detailed in https://github.com/kripken/Relooper/blob/master/paper.pdf
  9. class FlowGraphComponent(object):
  10. """Represents a control-flow graph component."""
  11. def __init__(self, entry_blocks, blocks, reachable):
  12. self.entry_blocks = entry_blocks
  13. self.blocks = blocks
  14. self.reachable = reachable
  15. def get_entry_reachable_blocks(self):
  16. """Gets the set of all blocks that are reachable from the entry points."""
  17. return set.union(*[self.get_reachable_blocks(block) for block in self.entry_blocks])
  18. def get_reachable_blocks(self, block):
  19. """Gets all blocks in this component that are reachable from the given block."""
  20. return set.intersection(self.reachable[block], self.blocks)
  21. def get_directly_reachable_blocks(self, block):
  22. """Gets all blocks in this component that are reachable from the given block by taking
  23. exactly one branch."""
  24. return set.intersection(cfg_optimization.get_directly_reachable_blocks(block), self.blocks)
  25. def can_reach(self, source_block, target_block):
  26. """Tests if the first block can reach the second."""
  27. return target_block in self.reachable[source_block] and target_block in self.blocks
  28. def sub_component(self, new_entry_points):
  29. """Creates a sub-component of this component, with the given new entry points."""
  30. result = FlowGraphComponent(new_entry_points, self.blocks, self.reachable)
  31. result.blocks = result.get_entry_reachable_blocks()
  32. return result
  33. def create_graph_component(entry_point):
  34. """Creates a flow graph component from the given entry point."""
  35. reachable = cfg_optimization.get_all_reachable_blocks(entry_point)
  36. all_blocks = set(reachable[entry_point])
  37. all_blocks.add(entry_point)
  38. return FlowGraphComponent([entry_point], all_blocks, reachable)
  39. class SimpleBlock(object):
  40. """A 'simple' block in the relooper algorithm."""
  41. def __init__(self, body, next_block):
  42. self.body = body
  43. self.next_block = next_block
  44. def lower(self, state):
  45. """Lowers this 'simple' block to a tree."""
  46. return tree_ir.create_block(
  47. state.lower_block(self.body),
  48. self.next_block.lower(state))
  49. class EmptyBlock(object):
  50. """An empty relooper block."""
  51. def lower(self, _):
  52. """Lowers this empty block to a tree."""
  53. return tree_ir.EmptyInstruction()
  54. class LoopBlock(object):
  55. """A 'loop' block in the relooper algorithm."""
  56. def __init__(self, inner, next_block):
  57. self.inner = inner
  58. self.next_block = next_block
  59. def lower(self, state):
  60. """Lowers this 'loop' block to a tree."""
  61. return tree_ir.create_block(
  62. tree_ir.LoopInstruction(self.inner.lower(state)),
  63. self.next_block.lower(state))
  64. class MultipleBlock(object):
  65. """A 'multiple' block in the relooper algorithm that does _not_ require a loop."""
  66. def __init__(self, handled_blocks, next_block):
  67. self.handled_blocks = handled_blocks
  68. self.next_block = next_block
  69. def lower_handled_blocks(self, state):
  70. """Lowers the handled blocks of this 'multiple' block to a tree."""
  71. result = tree_ir.EmptyInstruction()
  72. for entry, block in self.handled_blocks:
  73. result = tree_ir.SelectInstruction(
  74. tree_ir.BinaryInstruction(
  75. tree_ir.LoadLocalInstruction(state.label_variable),
  76. '==',
  77. tree_ir.LiteralInstruction(entry.index)),
  78. block.lower(state),
  79. result)
  80. return result
  81. def lower(self, state):
  82. """Lowers this 'multiple' block to a tree."""
  83. return tree_ir.create_block(
  84. self.lower_handled_blocks(state),
  85. self.next_block.lower(state))
  86. class MultipleLoopBlock(MultipleBlock):
  87. """A 'multiple' block in the relooper algorithm."""
  88. def __init__(self, handled_blocks, next_block):
  89. MultipleBlock.__init__(self, handled_blocks, next_block)
  90. def lower(self, state):
  91. """Lowers this 'multiple' block to a tree."""
  92. return tree_ir.create_block(
  93. tree_ir.LoopInstruction(self.lower_handled_blocks(state)),
  94. self.next_block.lower(state))
  95. class ContinueFlow(cfg_ir.FlowInstruction):
  96. """Represents a control flow instruction which continues to the next loop iteration."""
  97. def __init__(self, loop):
  98. cfg_ir.FlowInstruction.__init__(self)
  99. self.loop = loop
  100. def get_dependencies(self):
  101. """Gets all definitions and instructions on which this instruction depends."""
  102. return []
  103. def branches(self):
  104. """Gets a list of basic blocks targeted by this flow instruction."""
  105. return []
  106. def __str__(self):
  107. return 'continue'
  108. class BreakFlow(cfg_ir.FlowInstruction):
  109. """Represents a control flow instruction which breaks out of a loop."""
  110. def __init__(self, loop):
  111. cfg_ir.FlowInstruction.__init__(self)
  112. self.loop = loop
  113. def get_dependencies(self):
  114. """Gets all definitions and instructions on which this instruction depends."""
  115. return []
  116. def branches(self):
  117. """Gets a list of basic blocks targeted by this flow instruction."""
  118. return []
  119. def __str__(self):
  120. return 'break'
  121. def solipsize(graph_component, loop, entry_blocks, next_blocks):
  122. """Replaces branches to entry blocks and next blocks by jumps to 'continue' and 'break'
  123. blocks, respectively."""
  124. all_blocks = set()
  125. reachable_from_inner = set()
  126. for block in graph_component.blocks:
  127. if block in next_blocks:
  128. continue
  129. all_blocks.add(block)
  130. for branch in block.flow.branches():
  131. if branch.block in entry_blocks:
  132. # Create a 'continue' block, and point to that.
  133. continue_block = cfg_ir.BasicBlock(block.counter)
  134. for param in branch.block.parameters:
  135. continue_block.append_parameter(param)
  136. continue_block.flow = ContinueFlow(loop)
  137. branch.block = continue_block
  138. all_blocks.add(continue_block)
  139. elif branch.block in next_blocks:
  140. # Create a 'break' block, and point to that.
  141. break_block = cfg_ir.BasicBlock(block.counter)
  142. for param in branch.block.parameters:
  143. break_block.append_parameter(param)
  144. break_block.flow = BreakFlow(loop)
  145. branch.block = break_block
  146. all_blocks.add(break_block)
  147. reachable_from_inner.add(branch.block)
  148. return reachable_from_inner, FlowGraphComponent(
  149. graph_component.entry_blocks,
  150. all_blocks,
  151. {
  152. block : cfg_optimization.get_reachable_blocks(block)
  153. for block in all_blocks
  154. })
  155. def to_relooper_loop(graph_component):
  156. """Converts the given graph component to a relooper 'loop'."""
  157. entry_blocks = graph_component.entry_blocks
  158. result = LoopBlock(None, None)
  159. inner_blocks = []
  160. next_blocks = []
  161. for block in graph_component.blocks:
  162. if any([graph_component.can_reach(block, ep) for ep in entry_blocks]):
  163. inner_blocks.append(block)
  164. else:
  165. next_blocks.append(block)
  166. reachable_from_inner, inner_component = solipsize(
  167. graph_component, result, entry_blocks, next_blocks)
  168. result.inner = reloop(inner_component)
  169. next_component = FlowGraphComponent(
  170. reachable_from_inner, next_blocks, graph_component.reachable)
  171. result.next_block = reloop(next_component)
  172. return result
  173. def to_relooper_multiple_or_loop(graph_component):
  174. """Converts the given graph component to a relooper 'multiple' or 'loop'."""
  175. # From the Emscripten paper:
  176. #
  177. # If we have more than one entry, try to create a Multiple block:
  178. # For each entry, find all the labels it reaches that
  179. # cannot be reached by any other entry. If at least one entry
  180. # has such labels, return a Multiple block, whose Handled
  181. # blocks are blocks for those labels (and whose entries are
  182. # those labels), and whose Next block is all the rest. Entries
  183. # for the next block are entries that did not become part of
  184. # the Handled blocks, and also labels that can be reached
  185. # from the Handled blocks.
  186. entry_blocks = graph_component.entry_blocks
  187. if len(entry_blocks) <= 1:
  188. return to_relooper_loop(graph_component)
  189. entry_reachables = {ep : graph_component.get_reachable_blocks(ep) for ep in entry_blocks}
  190. exclusive_entries = {}
  191. for entry in entry_blocks:
  192. exclusive_blocks = set(entry_reachables[entry])
  193. for other_entry in entry_blocks:
  194. if other_entry != entry:
  195. exclusive_blocks.difference_update(entry_reachables[other_entry])
  196. if len(exclusive_blocks) > 0:
  197. exclusive_entries[entry] = exclusive_blocks
  198. if len(exclusive_entries) == 0:
  199. return to_relooper_loop(graph_component)
  200. next_entries = set(graph_component.entry_blocks)
  201. for block_set in exclusive_entries.values():
  202. for elem in block_set:
  203. directly_reachable = graph_component.get_directly_reachable_blocks(elem)
  204. directly_reachable.remove(elem)
  205. next_entries.update(directly_reachable)
  206. next_entries.difference_update(exclusive_entries.keys())
  207. result = MultipleLoopBlock({}, None)
  208. for entry, exclusive_blocks in exclusive_entries.items():
  209. other_blocks = set(graph_component.blocks)
  210. other_blocks.difference_update(exclusive_blocks)
  211. result.handled_blocks[entry] = reloop(
  212. solipsize(graph_component, result, set([entry]), other_blocks))
  213. result.next_block = reloop(graph_component.sub_component(next_entries))
  214. return result
  215. def reloop(graph_component):
  216. """Applies the relooper algorithm to the given graph component."""
  217. entry_blocks = graph_component.entry_blocks
  218. if len(entry_blocks) == 0:
  219. return EmptyBlock()
  220. reachable_set = graph_component.get_entry_reachable_blocks()
  221. if len(entry_blocks) == 1 and entry_blocks[0] not in reachable_set:
  222. graph_component.blocks.remove(entry_blocks[0])
  223. return SimpleBlock(
  224. entry_blocks[0],
  225. reloop(
  226. FlowGraphComponent(
  227. graph_component.get_directly_reachable_blocks(entry_blocks[0]),
  228. graph_component.blocks,
  229. graph_component.reachable)))
  230. elif all([block in reachable_set for block in entry_blocks]):
  231. return to_relooper_loop(graph_component)
  232. else:
  233. return to_relooper_multiple_or_loop(graph_component)
  234. def reloop_trivial(graph_component):
  235. """Converts the given control-flow graph to a 'multiple' block that contains only 'simple'
  236. blocks."""
  237. return MultipleLoopBlock(
  238. [(block, SimpleBlock(block, EmptyBlock())) for block in graph_component.blocks],
  239. EmptyBlock())
  240. def reloop_function_body(entry_point):
  241. """Reloops the control-flow graph defined by the given entry point."""
  242. return reloop_trivial(create_graph_component(entry_point))
  243. class LoweringState(object):
  244. """Stores information related to the relooper->tree conversion."""
  245. def __init__(self, jit):
  246. self.jit = jit
  247. self.label_variable = tree_ir.VariableName('__label')
  248. self.definition_loads = {}
  249. self.local_name_map = bytecode_to_tree.LocalNameMap()
  250. self.root_edge_names = {}
  251. def __get_root_edge_name(self, root_node):
  252. """Gets the name of the given root edge's variable."""
  253. return self.__get_root_node_name(root_node) + '_edge'
  254. def __get_root_node_name(self, root_node):
  255. """Gets the name of the given root node's variable."""
  256. if isinstance(root_node, cfg_ir.Definition):
  257. return self.__get_root_edge_name(root_node.value)
  258. if root_node in self.root_edge_names:
  259. return self.root_edge_names[root_node]
  260. result = 'jit_locals%d' % len(self.root_edge_names)
  261. self.root_edge_names[root_node] = result
  262. return result
  263. def __create_value_load(self, value):
  264. """Creates a tree that loads the given value."""
  265. if value.has_value():
  266. if isinstance(value, cfg_ir.Literal):
  267. return self.lower_literal(value)
  268. else:
  269. return tree_ir.LoadLocalInstruction(None)
  270. else:
  271. return tree_ir.LiteralInstruction(None)
  272. def load_definition(self, definition):
  273. """Loads the given definition's variable."""
  274. if definition in self.definition_loads:
  275. return self.definition_loads[definition]
  276. if isinstance(definition.value, cfg_ir.Definition):
  277. result = self.load_definition(definition.value)
  278. else:
  279. result = self.__create_value_load(definition.value)
  280. self.definition_loads[definition] = result
  281. return result
  282. def lower_block(self, block):
  283. """Lowers the given (relooped) block to a tree."""
  284. statements = []
  285. for definition in block.definitions:
  286. statements.append(self.lower_definition(definition))
  287. statements.append(self.lower_flow(block.flow))
  288. return tree_ir.create_block(*statements)
  289. def lower_definition(self, definition):
  290. """Lowers the given definition to a tree."""
  291. if isinstance(definition.value, cfg_ir.Definition):
  292. return tree_ir.EmptyInstruction()
  293. instruction = definition.value
  294. tree_instruction = self.lower_value(instruction)
  295. def_load = self.load_definition(definition)
  296. if isinstance(def_load, tree_ir.LocalInstruction):
  297. return def_load.create_store(tree_instruction)
  298. else:
  299. return tree_instruction
  300. def lower_value(self, value):
  301. """Lowers the given instruction to a tree."""
  302. value_type = type(value)
  303. if value_type in LoweringState.value_lowerings:
  304. return LoweringState.value_lowerings[value_type](self, value)
  305. else:
  306. raise jit_runtime.JitCompilationFailedException(
  307. "Unknown CFG instruction: '%s'" % value)
  308. def lower_literal(self, value):
  309. """Lowers the given literal value."""
  310. return tree_ir.LiteralInstruction(value.literal)
  311. def lower_check_local_exists(self, value):
  312. """Lowers a 'check-value-exists' value."""
  313. return tree_ir.LocalExistsInstruction(
  314. self.local_name_map.get_local_name(value.variable.node_id))
  315. def lower_declare_local(self, value):
  316. """Lowers a 'declare-local' value."""
  317. local_name = self.local_name_map.get_local_name(value.variable.node_id)
  318. return tree_ir.create_block(
  319. tree_ir.create_new_local_node(
  320. local_name,
  321. self.load_definition(value.root_node)),
  322. tree_ir.LoadLocalInstruction(local_name))
  323. def lower_resolve_local(self, value):
  324. """Lowers a 'resolve-local' value."""
  325. return tree_ir.LoadLocalInstruction(
  326. self.local_name_map.get_local_name(value.variable.node_id))
  327. def lower_declare_global(self, value):
  328. """Lowers a 'declare-global' value."""
  329. #
  330. # global_var, = yield [("CN", [])]
  331. # _globals, = yield [("RD", [task_root, "globals"])]
  332. # yield [("CD", [_globals, var_name, global_var])]
  333. #
  334. task_root = bytecode_to_tree.retrieve_task_root()
  335. global_var = tree_ir.StoreLocalInstruction(None, tree_ir.CreateNodeInstruction())
  336. return tree_ir.create_block(
  337. global_var.create_store(
  338. tree_ir.CreateNodeInstruction()),
  339. tree_ir.CreateDictionaryEdgeInstruction(
  340. tree_ir.ReadDictionaryValueInstruction(
  341. task_root.create_load(),
  342. tree_ir.LiteralInstruction('globals')),
  343. tree_ir.LiteralInstruction(
  344. value.variable.name),
  345. global_var.create_load()),
  346. global_var.create_load())
  347. def lower_resolve_global(self, value):
  348. """Lowers a 'resolve-global' value."""
  349. #
  350. # _globals, = yield [("RD", [task_root, "globals"])]
  351. # global_var, = yield [("RD", [_globals, var_name])]
  352. #
  353. task_root = bytecode_to_tree.retrieve_task_root()
  354. return tree_ir.ReadDictionaryValueInstruction(
  355. tree_ir.ReadDictionaryValueInstruction(
  356. task_root.create_load(),
  357. tree_ir.LiteralInstruction('globals')),
  358. tree_ir.LiteralInstruction(value.variable.name))
  359. def lower_function_parameter(self, value):
  360. """Lowers a 'function-parameter' value."""
  361. return tree_ir.LoadLocalInstruction(value.name)
  362. def lower_alloc_root_node(self, value):
  363. """Lowers an 'alloc-root-node' value."""
  364. local_name = tree_ir.VariableName(self.__get_root_node_name(value))
  365. return tree_ir.create_block(
  366. tree_ir.create_new_local_node(
  367. local_name,
  368. tree_ir.LoadIndexInstruction(
  369. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  370. tree_ir.LiteralInstruction('task_root')),
  371. self.__get_root_edge_name(value)),
  372. tree_ir.LoadLocalInstruction(local_name))
  373. def lower_free_root_node(self, value):
  374. """Lowers a 'free-root-node' value."""
  375. return tree_ir.DeleteEdgeInstruction(
  376. tree_ir.LoadLocalInstruction(self.__get_root_edge_name(value.root_node)))
  377. def lower_load_pointer(self, value):
  378. """Lowers a 'load' value."""
  379. return bytecode_to_tree.create_access(self.load_definition(value.pointer))
  380. def lower_store_pointer(self, value):
  381. """Lowers a 'store' value."""
  382. return bytecode_to_tree.create_assign(
  383. self.load_definition(value.pointer), self.load_definition(value.value))
  384. def lower_read(self, value):
  385. """Lowers a 'read' value."""
  386. return tree_ir.ReadValueInstruction(
  387. self.load_definition(value.node))
  388. def lower_create_node(self, value):
  389. """Lowers a 'create-node' value."""
  390. if value.value.has_value():
  391. return tree_ir.CreateNodeWithValueInstruction(
  392. self.load_definition(value.value))
  393. else:
  394. return tree_ir.CreateNodeInstruction()
  395. def lower_binary(self, value):
  396. """Lowers a 'binary' value."""
  397. return tree_ir.BinaryInstruction(
  398. self.load_definition(value.lhs),
  399. value.operator,
  400. self.load_definition(value.rhs))
  401. def lower_input(self, _):
  402. """Lowers an 'input' value."""
  403. return bytecode_to_tree.create_input(self.jit.use_input_function)
  404. def lower_output(self, value):
  405. """Lowers an 'output' value."""
  406. return bytecode_to_tree.create_output(self.load_definition(value.value))
  407. def lower_direct_call(self, value):
  408. """Lowers a direct function call."""
  409. calling_convention = value.calling_convention
  410. if calling_convention in LoweringState.call_lowerings:
  411. return LoweringState.call_lowerings[calling_convention](self, value)
  412. else:
  413. raise jit_runtime.JitCompilationFailedException(
  414. "Unknown calling convention: '%s' in instruction '%s'" %
  415. (calling_convention, value))
  416. def lower_indirect_call(self, value):
  417. """Lowers an indirect function call."""
  418. return bytecode_to_tree.create_indirect_call(
  419. self.load_definition(value.target),
  420. [(name, self.load_definition(arg)) for name, arg in value.argument_list])
  421. value_lowerings = {
  422. cfg_ir.Literal : lower_literal,
  423. cfg_ir.CheckLocalExists : lower_check_local_exists,
  424. cfg_ir.DeclareLocal : lower_declare_local,
  425. cfg_ir.ResolveLocal : lower_resolve_local,
  426. cfg_ir.DeclareGlobal : lower_declare_global,
  427. cfg_ir.ResolveGlobal : lower_resolve_global,
  428. cfg_ir.FunctionParameter : lower_function_parameter,
  429. cfg_ir.AllocateRootNode : lower_alloc_root_node,
  430. cfg_ir.DeallocateRootNode : lower_free_root_node,
  431. cfg_ir.LoadPointer : lower_load_pointer,
  432. cfg_ir.StoreAtPointer : lower_store_pointer,
  433. cfg_ir.Read : lower_read,
  434. cfg_ir.CreateNode : lower_create_node,
  435. cfg_ir.Binary : lower_binary,
  436. cfg_ir.Input : lower_input,
  437. cfg_ir.Output : lower_output,
  438. cfg_ir.DirectFunctionCall : lower_direct_call,
  439. cfg_ir.IndirectFunctionCall : lower_indirect_call
  440. }
  441. def lower_simple_positional_call(self, value):
  442. """Lowers a direct call that uses the 'simple-positional' calling convention."""
  443. return tree_ir.CallInstruction(
  444. tree_ir.LoadGlobalInstruction(value.target_name),
  445. [self.load_definition(arg) for _, arg in value.argument_list])
  446. def lower_jit_call(self, value):
  447. """Lowers a direct call that uses the 'jit' calling convention."""
  448. arg_list = [(name, self.load_definition(arg)) for name, arg in value.argument_list]
  449. intrinsic = self.jit.get_intrinsic(value.target_name)
  450. if intrinsic is not None:
  451. return bytecode_to_tree.apply_intrinsic(intrinsic, arg_list)
  452. else:
  453. return tree_ir.create_jit_call(
  454. tree_ir.LoadGlobalInstruction(value.target_name),
  455. arg_list,
  456. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME))
  457. call_lowerings = {
  458. cfg_ir.SIMPLE_POSITIONAL_CALLING_CONVENTION : lower_simple_positional_call,
  459. cfg_ir.JIT_CALLING_CONVENTION : lower_jit_call
  460. }
  461. def lower_flow(self, flow):
  462. """Lowers the given (relooped) flow instruction to a tree."""
  463. flow_type = type(flow)
  464. if flow_type in LoweringState.flow_lowerings:
  465. return LoweringState.flow_lowerings[flow_type](self, flow)
  466. else:
  467. raise jit_runtime.JitCompilationFailedException(
  468. "Unknown CFG flow instruction: '%s'" % flow)
  469. def lower_jump(self, flow):
  470. """Lowers the given 'jump' flow instruction to a tree."""
  471. return self.lower_branch(flow.branch)
  472. def lower_select(self, flow):
  473. """Lowers the given 'select' flow instruction to a tree."""
  474. return tree_ir.SelectInstruction(
  475. self.load_definition(flow.condition),
  476. self.lower_branch(flow.if_branch),
  477. self.lower_branch(flow.else_branch))
  478. def lower_return(self, flow):
  479. """Lowers the given 'return' flow instruction to a tree."""
  480. return tree_ir.ReturnInstruction(self.load_definition(flow.value))
  481. def lower_throw(self, flow):
  482. """Lowers the given 'throw' flow instruction to a tree."""
  483. return tree_ir.RaiseInstruction(self.load_definition(flow.exception))
  484. def lower_unreachable(self, _):
  485. """Lowers the given 'unreachable' flow instruction to a tree."""
  486. return tree_ir.EmptyInstruction()
  487. def lower_break(self, _):
  488. """Lowers the given 'break' flow instruction to a tree."""
  489. return tree_ir.BreakInstruction()
  490. def lower_continue(self, _):
  491. """Lowers the given 'continue' flow instruction to a tree."""
  492. return tree_ir.ContinueInstruction()
  493. def lower_branch(self, branch):
  494. """Lowers the given (relooped) branch to a tree."""
  495. for param, arg in zip(branch.block.parameters, branch.arguments):
  496. self.load_definition(param).create_store(self.load_definition(arg))
  497. return tree_ir.StoreLocalInstruction(
  498. self.label_variable,
  499. tree_ir.LiteralInstruction(branch.block.index))
  500. flow_lowerings = {
  501. cfg_ir.JumpFlow : lower_jump,
  502. cfg_ir.SelectFlow : lower_select,
  503. cfg_ir.ReturnFlow : lower_return,
  504. cfg_ir.ThrowFlow : lower_throw,
  505. cfg_ir.UnreachableFlow : lower_unreachable,
  506. BreakFlow : lower_break,
  507. ContinueFlow : lower_continue
  508. }