bytecode_to_tree.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. """Naively converts bytecode IR to tree IR."""
  2. import modelverse_jit.bytecode_ir as bytecode_ir
  3. import modelverse_jit.tree_ir as tree_ir
  4. import modelverse_jit.runtime as jit_runtime
  5. import modelverse_kernel.primitives as primitive_functions
  6. def get_parameter_names(compiled_function):
  7. """Gets the given compiled function's parameter names."""
  8. if hasattr(compiled_function, '__code__'):
  9. return compiled_function.__code__.co_varnames[
  10. :compiled_function.__code__.co_argcount]
  11. elif hasattr(compiled_function, '__init__'):
  12. return get_parameter_names(compiled_function.__init__)[1:]
  13. else:
  14. raise ValueError("'compiled_function' must be a function or a type.")
  15. def apply_intrinsic(intrinsic_function, named_args):
  16. """Applies the given intrinsic to the given sequence of named arguments."""
  17. param_names = get_parameter_names(intrinsic_function)
  18. if tuple(param_names) == tuple([n for n, _ in named_args]):
  19. # Perfect match. Yay!
  20. return intrinsic_function(**dict(named_args))
  21. else:
  22. # We'll have to store the arguments into locals to preserve
  23. # the order of evaluation.
  24. stored_args = [(name, tree_ir.StoreLocalInstruction(None, arg)) for name, arg in named_args]
  25. arg_value_dict = dict([(name, arg.create_load()) for name, arg in stored_args])
  26. store_instructions = [instruction for _, instruction in stored_args]
  27. return tree_ir.CompoundInstruction(
  28. tree_ir.create_block(*store_instructions),
  29. intrinsic_function(**arg_value_dict))
  30. class LocalNameMap(object):
  31. """A map that converts local variable nodes to identifiers."""
  32. def __init__(self, local_mapping=None):
  33. if local_mapping is None:
  34. local_mapping = {}
  35. self.local_mapping = local_mapping
  36. def get_local_name(self, local_variable_id):
  37. """Gets the name for the local variable node with the given id."""
  38. if local_variable_id not in self.local_mapping:
  39. self.local_mapping[local_variable_id] = 'local%d' % local_variable_id
  40. return self.local_mapping[local_variable_id]
  41. class AnalysisState(object):
  42. """The state of a bytecode analysis call graph."""
  43. def __init__(self, jit, body_id, task_root, local_mapping, max_instructions=None):
  44. self.analyzed_instructions = set()
  45. self.function_vars = set()
  46. self.local_vars = set()
  47. self.body_id = body_id
  48. self.max_instructions = max_instructions
  49. self.task_root = task_root
  50. self.jit = jit
  51. self.local_name_map = LocalNameMap(local_mapping)
  52. self.function_name = jit.jitted_entry_points[body_id]
  53. self.enclosing_loop_instruction = None
  54. def register_local_var(self, local_id):
  55. """Registers the given variable node id as a local."""
  56. if local_id in self.function_vars:
  57. raise jit_runtime.JitCompilationFailedException(
  58. "Local is used as target of function call.")
  59. self.local_vars.add(local_id)
  60. def register_function_var(self, local_id):
  61. """Registers the given variable node id as a function."""
  62. if local_id in self.local_vars:
  63. raise jit_runtime.JitCompilationFailedException(
  64. "Local is used as target of function call.")
  65. self.function_vars.add(local_id)
  66. def retrieve_task_root(self):
  67. """Creates an instruction that stores the task_root variable
  68. in a local."""
  69. return tree_ir.StoreLocalInstruction(
  70. 'task_root',
  71. tree_ir.LoadIndexInstruction(
  72. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  73. tree_ir.LiteralInstruction('task_root')))
  74. def load_kernel(self):
  75. """Creates an instruction that loads the Modelverse kernel."""
  76. return tree_ir.LoadIndexInstruction(
  77. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  78. tree_ir.LiteralInstruction('mvk'))
  79. def analyze(self, instruction):
  80. """Tries to build an intermediate representation from the instruction with the
  81. given id."""
  82. # Check the analyzed_instructions set for instruction_id to avoid
  83. # infinite loops.
  84. if instruction in self.analyzed_instructions:
  85. raise jit_runtime.JitCompilationFailedException(
  86. 'Cannot jit non-tree instruction graph.')
  87. elif (self.max_instructions is not None and
  88. len(self.analyzed_instructions) > self.max_instructions):
  89. raise jit_runtime.JitCompilationFailedException(
  90. 'Maximum number of instructions exceeded.')
  91. self.analyzed_instructions.add(instruction)
  92. instruction_type = type(instruction)
  93. if instruction_type in self.instruction_analyzers:
  94. # Analyze the instruction itself.
  95. outer_result, = yield [
  96. ("CALL_ARGS", [self.instruction_analyzers[instruction_type], (self, instruction)])]
  97. if self.jit.tracing_enabled and instruction.debug_information is not None:
  98. outer_result = tree_ir.with_debug_info_trace(
  99. outer_result, instruction.debug_information, self.function_name)
  100. # Check if the instruction has a 'next' instruction.
  101. if instruction.next_instruction is None:
  102. raise primitive_functions.PrimitiveFinished(outer_result)
  103. else:
  104. next_result, = yield [
  105. ("CALL_ARGS", [self.analyze, (instruction.next_instruction,)])]
  106. raise primitive_functions.PrimitiveFinished(
  107. tree_ir.CompoundInstruction(
  108. outer_result,
  109. next_result))
  110. else:
  111. raise jit_runtime.JitCompilationFailedException(
  112. "Unknown instruction type: '%s'" % type(instruction))
  113. def analyze_all(self, instruction_ids):
  114. """Tries to compile a list of IR trees from the given list of instruction ids."""
  115. results = []
  116. for inst in instruction_ids:
  117. analyzed_inst, = yield [("CALL_ARGS", [self.analyze, (inst,)])]
  118. results.append(analyzed_inst)
  119. raise primitive_functions.PrimitiveFinished(results)
  120. def analyze_return(self, instruction):
  121. """Tries to analyze the given 'return' instruction."""
  122. def create_return(return_value):
  123. return tree_ir.ReturnInstruction(
  124. tree_ir.CompoundInstruction(
  125. return_value,
  126. tree_ir.DeleteEdgeInstruction(
  127. tree_ir.LoadLocalInstruction(jit_runtime.LOCALS_EDGE_NAME))))
  128. if instruction.value is None:
  129. raise primitive_functions.PrimitiveFinished(
  130. create_return(
  131. tree_ir.EmptyInstruction()))
  132. else:
  133. retval, = yield [("CALL_ARGS", [self.analyze, (instruction.value,)])]
  134. raise primitive_functions.PrimitiveFinished(
  135. create_return(retval))
  136. def analyze_if(self, instruction):
  137. """Tries to analyze the given 'if' instruction."""
  138. if instruction.else_clause is None:
  139. (cond_r, true_r), = yield [
  140. ("CALL_ARGS",
  141. [self.analyze_all,
  142. ([instruction.condition, instruction.if_clause],)])]
  143. false_r = tree_ir.EmptyInstruction()
  144. else:
  145. (cond_r, true_r, false_r), = yield [
  146. ("CALL_ARGS",
  147. [self.analyze_all,
  148. ([instruction.condition, instruction.if_clause, instruction.else_clause],)])]
  149. raise primitive_functions.PrimitiveFinished(
  150. tree_ir.SelectInstruction(
  151. tree_ir.ReadValueInstruction(cond_r),
  152. true_r,
  153. false_r))
  154. def analyze_while(self, instruction):
  155. """Tries to analyze the given 'while' instruction."""
  156. # Analyze the condition.
  157. cond_r, = yield [("CALL_ARGS", [self.analyze, (instruction.condition,)])]
  158. # Store the old enclosing loop on the stack, and make this loop the
  159. # new enclosing loop.
  160. old_loop_instruction = self.enclosing_loop_instruction
  161. self.enclosing_loop_instruction = instruction
  162. body_r, = yield [("CALL_ARGS", [self.analyze, (instruction.body,)])]
  163. # Restore hte old enclosing loop.
  164. self.enclosing_loop_instruction = old_loop_instruction
  165. if self.jit.nop_insertion_enabled:
  166. create_loop_body = lambda check, body: tree_ir.create_block(
  167. check,
  168. body_r,
  169. tree_ir.NopInstruction())
  170. else:
  171. create_loop_body = tree_ir.CompoundInstruction
  172. raise primitive_functions.PrimitiveFinished(
  173. tree_ir.LoopInstruction(
  174. create_loop_body(
  175. tree_ir.SelectInstruction(
  176. tree_ir.ReadValueInstruction(cond_r),
  177. tree_ir.EmptyInstruction(),
  178. tree_ir.BreakInstruction()),
  179. body_r)))
  180. def analyze_constant(self, instruction):
  181. """Tries to analyze the given 'constant' (literal) instruction."""
  182. raise primitive_functions.PrimitiveFinished(
  183. tree_ir.LiteralInstruction(instruction.constant_id))
  184. def analyze_output(self, instruction):
  185. """Tries to analyze the given 'output' instruction."""
  186. # The plan is to basically generate this tree:
  187. #
  188. # value = <some tree>
  189. # last_output, last_output_link, new_last_output = \
  190. # yield [("RD", [task_root, "last_output"]),
  191. # ("RDE", [task_root, "last_output"]),
  192. # ("CN", []),
  193. # ]
  194. # _, _, _, _ = \
  195. # yield [("CD", [last_output, "value", value]),
  196. # ("CD", [last_output, "next", new_last_output]),
  197. # ("CD", [task_root, "last_output", new_last_output]),
  198. # ("DE", [last_output_link])
  199. # ]
  200. # yield None
  201. value_val, = yield [("CALL_ARGS", [self.analyze, (instruction.value,)])]
  202. value_local = tree_ir.StoreLocalInstruction('value', value_val)
  203. store_task_root = self.retrieve_task_root()
  204. last_output = tree_ir.StoreLocalInstruction(
  205. 'last_output',
  206. tree_ir.ReadDictionaryValueInstruction(
  207. store_task_root.create_load(),
  208. tree_ir.LiteralInstruction('last_output')))
  209. last_output_link = tree_ir.StoreLocalInstruction(
  210. 'last_output_link',
  211. tree_ir.ReadDictionaryEdgeInstruction(
  212. store_task_root.create_load(),
  213. tree_ir.LiteralInstruction('last_output')))
  214. new_last_output = tree_ir.StoreLocalInstruction(
  215. 'new_last_output',
  216. tree_ir.CreateNodeInstruction())
  217. result = tree_ir.create_block(
  218. value_local,
  219. store_task_root,
  220. last_output,
  221. last_output_link,
  222. new_last_output,
  223. tree_ir.CreateDictionaryEdgeInstruction(
  224. last_output.create_load(),
  225. tree_ir.LiteralInstruction('value'),
  226. value_local.create_load()),
  227. tree_ir.CreateDictionaryEdgeInstruction(
  228. last_output.create_load(),
  229. tree_ir.LiteralInstruction('next'),
  230. new_last_output.create_load()),
  231. tree_ir.CreateDictionaryEdgeInstruction(
  232. store_task_root.create_load(),
  233. tree_ir.LiteralInstruction('last_output'),
  234. new_last_output.create_load()),
  235. tree_ir.DeleteEdgeInstruction(last_output_link.create_load()),
  236. tree_ir.NopInstruction())
  237. raise primitive_functions.PrimitiveFinished(result)
  238. def analyze_input(self, _):
  239. """Tries to analyze the given 'input' instruction."""
  240. # Possible alternative to the explicit syntax tree:
  241. if self.jit.input_function_enabled:
  242. raise primitive_functions.PrimitiveFinished(
  243. tree_ir.create_jit_call(
  244. tree_ir.LoadGlobalInstruction(jit_runtime.GET_INPUT_FUNCTION_NAME),
  245. [],
  246. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME)))
  247. # The plan is to generate this tree:
  248. #
  249. # value = None
  250. # while True:
  251. # _input = yield [("RD", [task_root, "input"])]
  252. # value = yield [("RD", [_input, "value"])]
  253. #
  254. # if value is None:
  255. # kwargs['mvk'].success = False # to avoid blocking
  256. # yield None # nop/interrupt
  257. # else:
  258. # break
  259. #
  260. # _next = yield [("RD", [_input, "next"])]
  261. # yield [("CD", [task_root, "input", _next])]
  262. # yield [("CE", [jit_locals, value])]
  263. # yield [("DN", [_input])]
  264. task_root = self.retrieve_task_root()
  265. _input = tree_ir.StoreLocalInstruction(
  266. None,
  267. tree_ir.ReadDictionaryValueInstruction(
  268. task_root.create_load(),
  269. tree_ir.LiteralInstruction('input')))
  270. value = tree_ir.StoreLocalInstruction(
  271. None,
  272. tree_ir.ReadDictionaryValueInstruction(
  273. _input.create_load(),
  274. tree_ir.LiteralInstruction('value')))
  275. raise primitive_functions.PrimitiveFinished(
  276. tree_ir.CompoundInstruction(
  277. tree_ir.create_block(
  278. task_root,
  279. value.create_store(tree_ir.LiteralInstruction(None)),
  280. tree_ir.LoopInstruction(
  281. tree_ir.create_block(
  282. _input,
  283. value,
  284. tree_ir.SelectInstruction(
  285. tree_ir.BinaryInstruction(
  286. value.create_load(),
  287. 'is',
  288. tree_ir.LiteralInstruction(None)),
  289. tree_ir.create_block(
  290. tree_ir.StoreMemberInstruction(
  291. self.load_kernel(),
  292. 'success',
  293. tree_ir.LiteralInstruction(False)),
  294. tree_ir.NopInstruction()),
  295. tree_ir.BreakInstruction()))),
  296. tree_ir.CreateDictionaryEdgeInstruction(
  297. task_root.create_load(),
  298. tree_ir.LiteralInstruction('input'),
  299. tree_ir.ReadDictionaryValueInstruction(
  300. _input.create_load(),
  301. tree_ir.LiteralInstruction('next'))),
  302. tree_ir.CreateEdgeInstruction(
  303. tree_ir.LoadLocalInstruction(jit_runtime.LOCALS_NODE_NAME),
  304. value.create_load()),
  305. tree_ir.DeleteNodeInstruction(_input.create_load())),
  306. value.create_load()))
  307. def analyze_resolve(self, instruction):
  308. """Tries to analyze the given 'resolve' instruction."""
  309. # To resolve a variable, we'll do something along the
  310. # lines of:
  311. #
  312. # if 'local_var' in locals():
  313. # tmp = local_var
  314. # else:
  315. # _globals, = yield [("RD", [task_root, "globals"])]
  316. # global_var, = yield [("RD", [_globals, var_name])]
  317. #
  318. # if global_var is None:
  319. # raise Exception("Not found as global: %s" % (var_name))
  320. #
  321. # tmp = global_var
  322. name = self.local_name_map.get_local_name(instruction.variable.node_id)
  323. if instruction.variable.name is None:
  324. raise primitive_functions.PrimitiveFinished(
  325. tree_ir.LoadLocalInstruction(name))
  326. task_root = self.retrieve_task_root()
  327. global_var = tree_ir.StoreLocalInstruction(
  328. 'global_var',
  329. tree_ir.ReadDictionaryValueInstruction(
  330. tree_ir.ReadDictionaryValueInstruction(
  331. task_root.create_load(),
  332. tree_ir.LiteralInstruction('globals')),
  333. tree_ir.LiteralInstruction(instruction.variable.name)))
  334. err_block = tree_ir.SelectInstruction(
  335. tree_ir.BinaryInstruction(
  336. global_var.create_load(),
  337. 'is',
  338. tree_ir.LiteralInstruction(None)),
  339. tree_ir.RaiseInstruction(
  340. tree_ir.CallInstruction(
  341. tree_ir.LoadGlobalInstruction('Exception'),
  342. [tree_ir.LiteralInstruction(
  343. "Not found as global: %s" % instruction.variable.name)
  344. ])),
  345. tree_ir.EmptyInstruction())
  346. raise primitive_functions.PrimitiveFinished(
  347. tree_ir.SelectInstruction(
  348. tree_ir.LocalExistsInstruction(name),
  349. tree_ir.LoadLocalInstruction(name),
  350. tree_ir.CompoundInstruction(
  351. tree_ir.create_block(
  352. task_root,
  353. global_var,
  354. err_block),
  355. global_var.create_load())))
  356. def analyze_declare(self, instruction):
  357. """Tries to analyze the given 'declare' function."""
  358. self.register_local_var(instruction.variable.node_id)
  359. name = self.local_name_map.get_local_name(instruction.variable.node_id)
  360. # The following logic declares a local:
  361. #
  362. # if 'local_name' not in locals():
  363. # local_name, = yield [("CN", [])]
  364. # yield [("CE", [LOCALS_NODE_NAME, local_name])]
  365. raise primitive_functions.PrimitiveFinished(
  366. tree_ir.SelectInstruction(
  367. tree_ir.LocalExistsInstruction(name),
  368. tree_ir.EmptyInstruction(),
  369. tree_ir.create_new_local_node(
  370. name,
  371. tree_ir.LoadLocalInstruction(jit_runtime.LOCALS_NODE_NAME))))
  372. def analyze_global(self, instruction):
  373. """Tries to analyze the given 'global' (declaration) instruction."""
  374. # To resolve a variable, we'll do something along the
  375. # lines of:
  376. #
  377. # _globals, = yield [("RD", [task_root, "globals"])]
  378. # global_var = yield [("RD", [_globals, var_name])]
  379. #
  380. # if global_var is None:
  381. # global_var, = yield [("CN", [])]
  382. # yield [("CD", [_globals, var_name, global_var])]
  383. #
  384. # tmp = global_var
  385. task_root = self.retrieve_task_root()
  386. _globals = tree_ir.StoreLocalInstruction(
  387. '_globals',
  388. tree_ir.ReadDictionaryValueInstruction(
  389. task_root.create_load(),
  390. tree_ir.LiteralInstruction('globals')))
  391. global_var = tree_ir.StoreLocalInstruction(
  392. 'global_var',
  393. tree_ir.ReadDictionaryValueInstruction(
  394. _globals.create_load(),
  395. tree_ir.LiteralInstruction(instruction.variable.name)))
  396. raise primitive_functions.PrimitiveFinished(
  397. tree_ir.CompoundInstruction(
  398. tree_ir.create_block(
  399. task_root,
  400. _globals,
  401. global_var,
  402. tree_ir.SelectInstruction(
  403. tree_ir.BinaryInstruction(
  404. global_var.create_load(),
  405. 'is',
  406. tree_ir.LiteralInstruction(None)),
  407. tree_ir.create_block(
  408. global_var.create_store(
  409. tree_ir.CreateNodeInstruction()),
  410. tree_ir.CreateDictionaryEdgeInstruction(
  411. _globals.create_load(),
  412. tree_ir.LiteralInstruction(
  413. instruction.variable.name),
  414. global_var.create_load())),
  415. tree_ir.EmptyInstruction())),
  416. global_var.create_load()))
  417. def analyze_assign(self, instruction):
  418. """Tries to analyze the given 'assign' instruction."""
  419. (var_r, value_r), = yield [
  420. ("CALL_ARGS", [self.analyze_all, ([instruction.pointer, instruction.value],)])]
  421. # Assignments work like this:
  422. #
  423. # value_link = yield [("RDE", [variable, "value"])]
  424. # _, _ = yield [("CD", [variable, "value", value]),
  425. # ("DE", [value_link])]
  426. variable = tree_ir.StoreLocalInstruction(None, var_r)
  427. value = tree_ir.StoreLocalInstruction(None, value_r)
  428. value_link = tree_ir.StoreLocalInstruction(
  429. 'value_link',
  430. tree_ir.ReadDictionaryEdgeInstruction(
  431. variable.create_load(),
  432. tree_ir.LiteralInstruction('value')))
  433. raise primitive_functions.PrimitiveFinished(
  434. tree_ir.create_block(
  435. variable,
  436. value,
  437. value_link,
  438. tree_ir.CreateDictionaryEdgeInstruction(
  439. variable.create_load(),
  440. tree_ir.LiteralInstruction('value'),
  441. value.create_load()),
  442. tree_ir.DeleteEdgeInstruction(
  443. value_link.create_load())))
  444. def analyze_access(self, instruction):
  445. """Tries to analyze the given 'access' instruction."""
  446. var_r, = yield [("CALL_ARGS", [self.analyze, (instruction.pointer,)])]
  447. # Accessing a variable is pretty easy. It really just boils
  448. # down to reading the value corresponding to the 'value' key
  449. # of the variable.
  450. #
  451. # value, = yield [("RD", [returnvalue, "value"])]
  452. raise primitive_functions.PrimitiveFinished(
  453. tree_ir.ReadDictionaryValueInstruction(
  454. var_r,
  455. tree_ir.LiteralInstruction('value')))
  456. def analyze_direct_call(self, callee_id, callee_name, argument_list):
  457. """Tries to analyze a direct 'call' instruction."""
  458. body_id, = yield [("RD", [callee_id, jit_runtime.FUNCTION_BODY_KEY])]
  459. # Make this function dependent on the callee.
  460. if body_id in self.jit.compilation_dependencies:
  461. self.jit.compilation_dependencies[body_id].add(self.body_id)
  462. # Figure out if the function might be an intrinsic.
  463. intrinsic = self.jit.get_intrinsic(callee_name)
  464. if intrinsic is None:
  465. if callee_name is not None:
  466. self.jit.register_global(body_id, callee_name)
  467. compiled_func = self.jit.lookup_compiled_function(callee_name)
  468. else:
  469. compiled_func = None
  470. if compiled_func is None:
  471. # Compile the callee.
  472. yield [
  473. ("CALL_ARGS", [self.jit.jit_compile, (self.task_root, body_id, callee_name)])]
  474. # Get the callee's name.
  475. compiled_func_name = self.jit.get_compiled_name(body_id)
  476. # This handles the corner case where a constant node is called, like
  477. # 'call(constant(9), ...)'. In this case, `callee_name` is `None`
  478. # because 'constant(9)' doesn't give us a name. However, we can look up
  479. # the name of the function at a specific node. If that turns out to be
  480. # an intrinsic, then we still want to pick the intrinsic over a call.
  481. intrinsic = self.jit.get_intrinsic(compiled_func_name)
  482. # Analyze the argument dictionary.
  483. named_args, = yield [("CALL_ARGS", [self.analyze_arguments, (argument_list,)])]
  484. if intrinsic is not None:
  485. raise primitive_functions.PrimitiveFinished(
  486. apply_intrinsic(intrinsic, named_args))
  487. else:
  488. raise primitive_functions.PrimitiveFinished(
  489. tree_ir.create_jit_call(
  490. tree_ir.LoadGlobalInstruction(compiled_func_name),
  491. named_args,
  492. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME)))
  493. def analyze_arguments(self, argument_list):
  494. """Analyzes the given parameter-to-value mapping."""
  495. named_args = []
  496. for param_name, arg in argument_list:
  497. param_val, = yield [("CALL_ARGS", [self.analyze, (arg,)])]
  498. named_args.append((param_name, param_val))
  499. raise primitive_functions.PrimitiveFinished(named_args)
  500. def analyze_indirect_call(self, target, argument_list):
  501. """Analyzes a call to an unknown function."""
  502. # First off, let's analyze the callee and the argument list.
  503. func_val, = yield [("CALL_ARGS", [self.analyze, (target,)])]
  504. named_args, = yield [("CALL_ARGS", [self.analyze_arguments, (argument_list,)])]
  505. # Call the __call_function function to run the interpreter, like so:
  506. #
  507. # __call_function(function_id, { first_param_name : first_param_val, ... }, **kwargs)
  508. #
  509. dict_literal = tree_ir.DictionaryLiteralInstruction(
  510. [(tree_ir.LiteralInstruction(key), val) for key, val in named_args])
  511. raise primitive_functions.PrimitiveFinished(
  512. tree_ir.create_jit_call(
  513. tree_ir.LoadGlobalInstruction(jit_runtime.CALL_FUNCTION_NAME),
  514. [('function_id', func_val), ('named_arguments', dict_literal)],
  515. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME)))
  516. def try_analyze_direct_call(self, target, argument_list):
  517. """Tries to analyze the given 'call' instruction as a direct call."""
  518. if not self.jit.direct_calls_allowed:
  519. raise jit_runtime.JitCompilationFailedException(
  520. 'Direct calls are not allowed by the JIT.')
  521. # Figure out what the 'func' instruction's type is.
  522. if isinstance(target, bytecode_ir.AccessInstruction):
  523. # 'access(resolve(var))' instructions are translated to direct calls.
  524. if isinstance(target.pointer, bytecode_ir.ResolveInstruction):
  525. self.register_function_var(target.pointer.variable.node_id)
  526. resolved_var_name = target.pointer.variable.name
  527. if self.jit.thunks_enabled:
  528. # Analyze the argument dictionary.
  529. named_args, = yield [("CALL_ARGS", [self.analyze_arguments, (argument_list,)])]
  530. # Try to resolve the callee as an intrinsic.
  531. intrinsic = self.jit.get_intrinsic(resolved_var_name)
  532. if intrinsic is not None:
  533. raise primitive_functions.PrimitiveFinished(
  534. apply_intrinsic(intrinsic, named_args))
  535. # Otherwise, build a thunk.
  536. thunk_name = self.jit.jit_thunk_global(target.pointer.variable.name)
  537. raise primitive_functions.PrimitiveFinished(
  538. tree_ir.create_jit_call(
  539. tree_ir.LoadGlobalInstruction(thunk_name),
  540. named_args,
  541. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME)))
  542. else:
  543. # Try to look up the name as a global.
  544. _globals, = yield [("RD", [self.task_root, "globals"])]
  545. global_var, = yield [("RD", [_globals, resolved_var_name])]
  546. global_val, = yield [("RD", [global_var, "value"])]
  547. if global_val is not None:
  548. result, = yield [("CALL_ARGS", [self.analyze_direct_call, (
  549. global_val, resolved_var_name, argument_list)])]
  550. raise primitive_functions.PrimitiveFinished(result)
  551. elif isinstance(target, bytecode_ir.ConstantInstruction):
  552. # 'const(func_id)' instructions are also translated to direct calls.
  553. result, = yield [("CALL_ARGS", [self.analyze_direct_call, (
  554. target.constant_id, None, argument_list)])]
  555. raise primitive_functions.PrimitiveFinished(result)
  556. raise jit_runtime.JitCompilationFailedException(
  557. "Cannot JIT function calls that target an unknown value as direct calls.")
  558. def analyze_call(self, instruction):
  559. """Tries to analyze the given 'call' instruction."""
  560. def handle_exception(_):
  561. # Looks like we'll have to compile it as an indirect call.
  562. gen = self.analyze_indirect_call(instruction.target, instruction.argument_list)
  563. result, = yield [("CALL", [gen])]
  564. raise primitive_functions.PrimitiveFinished(result)
  565. # Try to analyze the call as a direct call.
  566. yield [("TRY", [])]
  567. yield [("CATCH", [jit_runtime.JitCompilationFailedException, handle_exception])]
  568. result, = yield [
  569. ("CALL_ARGS",
  570. [self.try_analyze_direct_call, (instruction.target, instruction.argument_list)])]
  571. yield [("END_TRY", [])]
  572. raise primitive_functions.PrimitiveFinished(result)
  573. def analyze_break(self, instruction):
  574. """Tries to analyze the given 'break' instruction."""
  575. if instruction.loop == self.enclosing_loop_instruction:
  576. raise primitive_functions.PrimitiveFinished(tree_ir.BreakInstruction())
  577. else:
  578. raise jit_runtime.JitCompilationFailedException(
  579. "Multilevel 'break' is not supported by the baseline JIT.")
  580. def analyze_continue(self, instruction):
  581. """Tries to analyze the given 'continue' instruction."""
  582. if instruction.loop == self.enclosing_loop_instruction:
  583. raise primitive_functions.PrimitiveFinished(tree_ir.ContinueInstruction())
  584. else:
  585. raise jit_runtime.JitCompilationFailedException(
  586. "Multilevel 'continue' is not supported by the baseline JIT.")
  587. instruction_analyzers = {
  588. bytecode_ir.SelectInstruction : analyze_if,
  589. bytecode_ir.WhileInstruction : analyze_while,
  590. bytecode_ir.ReturnInstruction : analyze_return,
  591. bytecode_ir.ConstantInstruction : analyze_constant,
  592. bytecode_ir.ResolveInstruction : analyze_resolve,
  593. bytecode_ir.DeclareInstruction : analyze_declare,
  594. bytecode_ir.GlobalInstruction : analyze_global,
  595. bytecode_ir.AssignInstruction : analyze_assign,
  596. bytecode_ir.AccessInstruction : analyze_access,
  597. bytecode_ir.OutputInstruction : analyze_output,
  598. bytecode_ir.InputInstruction : analyze_input,
  599. bytecode_ir.CallInstruction : analyze_call,
  600. bytecode_ir.BreakInstruction : analyze_break,
  601. bytecode_ir.ContinueInstruction : analyze_continue
  602. }