main.py 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. import modelverse_kernel.primitives as primitive_functions
  2. import modelverse_kernel.compiled as compiled_functions
  3. from modelverse_kernel.request_handler import RequestHandler
  4. import modelverse_jit.jit as jit
  5. import modelverse_jit.intrinsics as jit_intrinsics
  6. import modelverse_jit.jit_primitives as jit_primitives
  7. import modelverse_jit.runtime as jit_runtime
  8. from collections import defaultdict
  9. import sys
  10. import time
  11. if sys.version > '3': # pragma: no cover
  12. string_types = (str,)
  13. else:
  14. string_types = (str, unicode)
  15. class ModelverseKernel(object):
  16. counter = 0
  17. def __init__(self, root):
  18. self.root = root
  19. self.returnvalue = None
  20. # request_handlers is a dictionary of tasknames to dictionaries of operations
  21. # to request handlers. In generics notation:
  22. #
  23. # Dictionary<
  24. # Username,
  25. # Dictionary<
  26. # Operation,
  27. # RequestHandler>>
  28. #
  29. self.request_handlers = {}
  30. self.allow_compiled = True
  31. #self.allow_compiled = False
  32. # Set `self.suggest_function_names` to True to associate global function names
  33. # with their function bodies.
  34. self.suggest_function_names = True
  35. # `self.jit` handles most JIT-related functionality.
  36. self.jit = jit.ModelverseJit()
  37. if self.allow_compiled:
  38. self.jit.compiled_function_lookup = lambda func_name: \
  39. getattr(compiled_functions, func_name, None)
  40. jit_intrinsics.register_intrinsics(self.jit)
  41. # To disable the JIT, uncomment the line below:
  42. #
  43. # self.jit.set_jit_enabled(False)
  44. #
  45. # To disable direct calls in the JIT, uncomment the line below:
  46. #
  47. # self.jit.allow_direct_calls(False)
  48. #
  49. # To disable thunks in the JIT, uncomment the line below:
  50. #
  51. # self.jit.enable_thunks(False)
  52. #
  53. # To make the JIT compile 'input' instructions as calls to
  54. # modelverse_jit.runtime.get_input, uncomment the line below:
  55. #
  56. # self.jit.use_input_function()
  57. #
  58. # To disable source maps in the JIT, uncomment the line below:
  59. #
  60. # self.jit.enable_source_maps(False)
  61. #
  62. # To enable tracing in the JIT (for debugging purposes), uncomment
  63. # the line below:
  64. #
  65. # self.jit.enable_tracing()
  66. #
  67. # To make the JIT print JIT successes and errors to the command-line,
  68. # uncomment the line below:
  69. #
  70. # self.jit.set_jit_success_log()
  71. #
  72. # If you want, you can use a custom logging function:
  73. #
  74. # self.jit.set_jit_success_log(logging_function)
  75. #
  76. # To make the JIT print jitted code to the command-line, uncomment the
  77. # line below:
  78. #
  79. # self.jit.set_jit_code_log()
  80. #
  81. # If you want, you can use a custom logging function:
  82. #
  83. # self.jit.set_jit_code_log(logging_function)
  84. #
  85. self.debug_info = defaultdict(list)
  86. def execute_yields(self, taskname, operation, params, reply):
  87. self.taskname = taskname
  88. if taskname not in self.request_handlers:
  89. self.request_handlers[taskname] = {}
  90. if operation not in self.request_handlers[taskname]:
  91. # Create the generator for the function to execute
  92. self.request_handlers[taskname][operation] = RequestHandler()
  93. handler = self.request_handlers[taskname][operation]
  94. if not handler.is_active():
  95. handler.push_generator(getattr(self, operation)(taskname, *params))
  96. return handler.handle_request(reply)
  97. def execute_rule(self, taskname):
  98. task_root, = yield [("RD", [self.root, taskname])]
  99. if task_root is None:
  100. yield None
  101. else:
  102. task_frame, = yield [("RD", [task_root, "frame"])]
  103. self.inst, phase = yield [("RD", [task_frame, "IP"]),
  104. ("RD", [task_frame, "phase"]),
  105. ]
  106. self.new_debug, self.phase_v, inst_v = \
  107. yield [("RD", [self.inst, "__debug"]),
  108. ("RV", [phase]),
  109. ("RV", [self.inst]),
  110. ]
  111. if self.new_debug is not None:
  112. if self.debug_info[taskname]:
  113. self.debug_info[taskname][-1], = yield [("RV", [self.new_debug])]
  114. if self.phase_v == "finish":
  115. gen = self.helper_init(task_root)
  116. elif self.inst is None:
  117. print("No instruction pointer found...")
  118. print(locals())
  119. print("Phase: " + str(self.phase_v))
  120. print("Debug: " + str(self.debug_info[taskname]))
  121. raise Exception("Instruction pointer could not be found!")
  122. elif isinstance(self.phase_v, string_types):
  123. if self.phase_v == "init" and self.jit.is_jittable_entry_point(self.inst):
  124. #print("%-30s(%s)" % ("COMPILED " + str(self.jit.jitted_entry_points[self.inst]), phase_v))
  125. gen = self.execute_jit(task_root, self.inst, taskname)
  126. elif inst_v is None:
  127. raise Exception("%s: error understanding command (%s, %s)" % (self.debug_info[taskname], inst_v, self.phase_v))
  128. else:
  129. #print("%-30s(%s) -- %s" % (inst_v["value"], self.phase_v, taskname))
  130. gen = self.get_inst_phase_generator(inst_v, self.phase_v, task_root)
  131. elif inst_v is None:
  132. raise Exception("%s: error understanding command (%s, %s)" % (self.debug_info[taskname], inst_v, self.phase_v))
  133. elif inst_v["value"] == "call":
  134. #print("%-30s(%s)" % ("call", "param"))
  135. gen = self.call_param(task_root)
  136. else:
  137. raise Exception("%s: error understanding command (%s, %s)" % (self.debug_info[taskname], inst_v, self.phase_v))
  138. def handle_jit_failed(exception):
  139. # Try again, but this time without the JIT.
  140. gen = self.get_inst_phase_generator(inst_v, self.phase_v, task_root)
  141. yield [("TAIL_CALL", [gen])]
  142. yield [("TRY", [])]
  143. yield [("CATCH", [jit.JitCompilationFailedException, handle_jit_failed])]
  144. yield [("CALL", [gen])]
  145. yield [("END_TRY", [])]
  146. def get_inst_phase_generator(self, inst_v, phase_v, task_root):
  147. """Gets a generator for the given instruction in the given phase,
  148. for the specified task root."""
  149. #print("%-30s(%s) -- %s" % (inst_v["value"], phase_v, taskname))
  150. return getattr(self, "%s_%s" % (inst_v["value"], phase_v))(task_root)
  151. ##########################
  152. ### Process primitives ###
  153. ##########################
  154. def load_primitives(self, taskname):
  155. yield [("CALL_ARGS",
  156. [self.load_primitives_from, (taskname, 'primitives', primitive_functions)])]
  157. yield [("CALL_ARGS",
  158. [self.load_primitives_from, (taskname, 'jit', jit_primitives)])]
  159. def load_primitives_from(self, taskname, source_name, source):
  160. hierarchy, = yield [("RD", [self.root, "__hierarchy"])]
  161. primitives, = yield [("RD", [hierarchy, source_name])]
  162. keys, = yield [("RDK", [primitives])]
  163. function_names = yield [("RV", [f]) for f in keys]
  164. signatures = yield [("RDN", [primitives, f]) for f in keys]
  165. bodies = yield [("RD", [f, "body"]) for f in signatures]
  166. for i in range(len(keys)):
  167. self.jit.register_compiled(
  168. bodies[i],
  169. getattr(source, function_names[i]),
  170. function_names[i])
  171. def print_instruction(self, inst, indent, nested_indent=None):
  172. """
  173. intrinsics = {"integer_addition": (lambda x, y: "(%s + %s)" % (x, y)),
  174. "string_join": (lambda x, y: "(str(%s) + str(%s))" % (x, y)),
  175. }
  176. """
  177. intrinsics = {}
  178. if nested_indent is None:
  179. nested_indent = indent
  180. inst_type, = yield [("RV", [inst])]
  181. instruction = "(no_printer_for_%s)" % inst_type["value"]
  182. prev = ""
  183. if inst_type["value"] == "if":
  184. cond, true, false = yield [("RD", [inst, "cond"]),
  185. ("RD", [inst, "then"]),
  186. ("RD", [inst, "else"])]
  187. (prev_cond, instruction_cond), = yield [("CALL_ARGS", [self.print_instruction, (cond, 0, indent)])]
  188. (prev_true, instruction_true), = yield [("CALL_ARGS", [self.print_instruction, (true, indent+1)])]
  189. if false:
  190. (prev_false, instruction_false), = yield [("CALL_ARGS", [self.print_instruction, (false, indent+1)])]
  191. false = (" " * indent + "else:\n%s%s") % (prev_false, instruction_false)
  192. else:
  193. false = ""
  194. instruction = prev_cond + " " * indent + "if (" + instruction_cond + "):\n" + prev_true + instruction_true + false
  195. elif inst_type["value"] == "constant":
  196. node, = yield [("RD", [inst, "node"])]
  197. node_value, = yield [("RV", [node])]
  198. if node_value is not None:
  199. # There is a value to the node, so replicate the value
  200. if isinstance(node_value, str):
  201. value = '"%s"' % node_value
  202. else:
  203. value = str(node_value)
  204. instruction = "constant_" + str(ModelverseKernel.counter)
  205. ModelverseKernel.counter += 1
  206. prev = " " * nested_indent + instruction + ", = yield [('CNV', [" + value + "])]\n"
  207. else:
  208. # Node is None, meaning that it was not about the value, but the node itself...
  209. instruction = str(node)
  210. elif inst_type["value"] == "return":
  211. value, = yield [("RD", [inst, "value"])]
  212. if value:
  213. (prev_value, instruction_value), = yield [("CALL_ARGS", [self.print_instruction, (value, 0, indent)])]
  214. instruction = prev_value + " " * indent + "return %s\n" % instruction_value
  215. else:
  216. instruction = " " * indent + "return\n"
  217. elif inst_type["value"] == "declare":
  218. instruction = ""
  219. elif inst_type["value"] == "resolve":
  220. value, = yield [("RD", [inst, "var"])]
  221. str_value, = yield [("RV", [value])]
  222. if str_value:
  223. instruction = str_value
  224. else:
  225. instruction = "var_%s" % value
  226. elif inst_type["value"] == "assign":
  227. var, val = yield [("RD", [inst, "var"]),
  228. ("RD", [inst, "value"])]
  229. (prev_var, instruction_var), (prev_val, instruction_val) = \
  230. yield [("CALL_ARGS", [self.print_instruction, (var, 0, indent)]),
  231. ("CALL_ARGS", [self.print_instruction, (val, 0, indent)])]
  232. instruction = prev_val + " " * indent + instruction_var + " = " + instruction_val + "\n"
  233. elif inst_type["value"] == "call":
  234. func_name, = yield [("RD", [inst, "func"])]
  235. (prev_func_name, func_name), = yield [("CALL_ARGS", [self.print_instruction, (func_name, 0, nested_indent)])]
  236. param_list = []
  237. param, = yield [("RD", [inst, "params"])]
  238. computation = ""
  239. while param:
  240. value, = yield [("RD", [param, "value"])]
  241. (prev_res, instruction_res), param = \
  242. yield [("CALL_ARGS", [self.print_instruction, (value, 0, nested_indent)]),
  243. ("RD", [param, "next_param"])]
  244. computation += prev_res
  245. param_list.append(instruction_res)
  246. if func_name in intrinsics:
  247. #instruction = " " * indent + intrinsics[func_name](*param_list)
  248. # TODO
  249. pass
  250. else:
  251. value = "func_result_" + str(ModelverseKernel.counter)
  252. ModelverseKernel.counter += 1
  253. if len(param_list) == 1:
  254. actual_computation = "%s, = yield [('CALL_ARGS', [%s, (%s,)])]\n" % (value, func_name, param_list[0])
  255. else:
  256. actual_computation = "%s, = yield [('CALL_ARGS', [%s, (%s)])]\n" % (value, func_name, ", ".join(param_list))
  257. if indent == 0:
  258. # No indent, meaning that we use it inline
  259. # Therefore, we output the prev and value individually
  260. prev, instruction = computation + " " * nested_indent + actual_computation, value
  261. else:
  262. # Some indentation, meaning that we don't even use the return value
  263. # Therefore, we only do the yield
  264. prev, instruction = computation, " " * indent + actual_computation
  265. elif inst_type["value"] == "access":
  266. value, = yield [("RD", [inst, "var"])]
  267. (prev_instruction, instruction), = yield [("CALL_ARGS", [self.print_instruction, (value, 0, indent)])]
  268. elif inst_type["value"] == "while":
  269. cond, body = yield [("RD", [inst, "cond"]),
  270. ("RD", [inst, "body"])]
  271. (prev_cond, instruction_cond), (prev_body, instruction_body) = \
  272. yield [("CALL_ARGS", [self.print_instruction, (cond, 0, indent+1)]),
  273. ("CALL_ARGS", [self.print_instruction, (body, indent+1)])]
  274. instruction = " " * indent + "while 1:\n" + prev_cond + \
  275. " " * (indent + 1) + "if not (" + instruction_cond + "):\n" + \
  276. " " * (indent + 2) + "break\n" + \
  277. prev_body + instruction_body
  278. next_inst, = yield [("RD", [inst, "next"])]
  279. if next_inst:
  280. (prev_next, inst_next), = yield [("CALL_ARGS", [self.print_instruction, (next_inst, indent)])]
  281. next_inst = prev_next + inst_next
  282. else:
  283. next_inst = ""
  284. raise primitive_functions.PrimitiveFinished((prev, instruction + next_inst))
  285. def read_function(self, inst):
  286. initial_instruction = inst
  287. suggested_name = self.jit.get_global_name(inst)
  288. (params, _, is_mutable), = yield [("CALL_ARGS", [self.jit.jit_signature, (inst,)])]
  289. if suggested_name is None or is_mutable:
  290. print("Ignoring mutable or unreadable: %s" % suggested_name)
  291. raise jit.JitCompilationFailedException("FAIL")
  292. print("Reading function: %s" % suggested_name)
  293. (_, printed), = yield [("CALL_ARGS", [self.print_instruction, (inst, 1)])]
  294. print("Total printed function: ")
  295. func = "def " + suggested_name + "(" + ", ".join(["var_%s" % param for param in params]) + "):\n" + printed
  296. print(func)
  297. raise primitive_functions.PrimitiveFinished(func)
  298. def jit_compile(self, task_root, inst):
  299. # Try to retrieve the suggested name.
  300. suggested_name = self.jit.get_global_name(inst)
  301. # Have the JIT compile the function.
  302. #return self.jit.jit_compile(task_root, inst, suggested_name)
  303. if inst is None:
  304. raise ValueError('body_id cannot be None: ' + str(suggested_name))
  305. elif inst in self.jit.jitted_entry_points:
  306. raise primitive_functions.PrimitiveFinished(
  307. self.jit.jit_globals[self.jit.jitted_entry_points[inst]])
  308. compiled_func = self.jit.lookup_compiled_body(inst)
  309. if compiled_func is None:
  310. name = self.jit.get_global_name(inst)
  311. compiled_func, = yield [("CALL_ARGS", [self.read_function, (inst,)])]
  312. exec(str(compiled_func), self.jit.jit_globals)
  313. print(self.jit.jit_globals)
  314. compiled_func = self.jit.jit_globals[name]
  315. self.jit.register_compiled(inst, compiled_func, name)
  316. #raise jit.JitCompilationFailedException("FAIL")
  317. try:
  318. raise primitive_functions.PrimitiveFinished(compiled_func)
  319. except Exception as e:
  320. print("Exception: " + str(e))
  321. raise jit.JitCompilationFailedException("FAIL")
  322. def execute_jit(self, task_root, inst, taskname):
  323. # execute_jit
  324. task_frame, = yield [("RD", [task_root, "frame"])]
  325. symbols, = yield [("RD", [task_frame, "symbols"])]
  326. dict_keys_ref, = yield [("RDK", [symbols])]
  327. dict_keys_ref_n = yield [("RD", [i, "name"]) for i in dict_keys_ref]
  328. dict_keys = yield [("RV", [i]) for i in dict_keys_ref_n]
  329. dict_values_elem = yield [("RDN", [symbols, i]) for i in dict_keys_ref]
  330. dict_values = yield [("RD", [i, "value"]) for i in dict_values_elem]
  331. parameters = dict(list(zip(dict_keys, dict_values)))
  332. parameters["root"] = self.root
  333. parameters["task_root"] = task_root
  334. parameters["taskname"] = taskname
  335. parameters["mvk"] = self
  336. # Have the JIT compile the function.
  337. compiled_func, = yield [("CALL_ARGS", [self.jit_compile, (task_root, inst)])]
  338. # Run the compiled function.
  339. results = yield [("CALL_KWARGS", [compiled_func, parameters])]
  340. if results is None:
  341. raise Exception(
  342. "%s: primitive finished without returning a value!" % (self.debug_info[taskname]))
  343. else:
  344. result, = results
  345. # Clean up the current stack, as if a return happened
  346. old_frame, exception_return = yield [
  347. ("RD", [task_frame, "prev"]),
  348. ("RD", [task_frame, primitive_functions.EXCEPTION_RETURN_KEY])]
  349. if self.debug_info[self.taskname]:
  350. self.debug_info[self.taskname].pop()
  351. if exception_return is not None:
  352. # The caller has requested that we throw an exception instead of injecting
  353. # the return value into the caller's frame. Read the comment at
  354. # primitive_functions.EXCEPTION_RETURN_KEY for the rationale behind this design.
  355. yield [("CD", [task_root, "frame", old_frame]),
  356. ("DN", [task_frame])]
  357. raise primitive_functions.InterpretedFunctionFinished(result)
  358. else:
  359. lnk, = yield [("RDE", [old_frame, "returnvalue"])]
  360. _, _, _, _ = yield [("CD", [old_frame, "returnvalue", result]),
  361. ("CD", [task_root, "frame", old_frame]),
  362. ("DE", [lnk]),
  363. ("DN", [task_frame]),
  364. ]
  365. ########################################
  366. ### Execute input and output methods ###
  367. ########################################
  368. def get_output(self, taskname):
  369. task_root, = yield [("RD", [self.root, taskname])]
  370. first_output, = yield [("RD", [task_root, "output"])]
  371. next_output, rv = yield [("RD", [first_output, "next"]),
  372. ("RD", [first_output, "value"]),
  373. ]
  374. if next_output is None:
  375. self.success = False
  376. self.returnvalue = None
  377. else:
  378. rv_value, _, _ = \
  379. yield [("RV", [rv]),
  380. ("CD", [task_root, "output", next_output]),
  381. ("DN", [first_output]),
  382. ]
  383. self.returnvalue = rv_value
  384. self.success = True
  385. def set_input(self, taskname, value):
  386. task_root, = yield [("RD", [self.root, taskname])]
  387. old_input, link, new_input, new_value = \
  388. yield [("RD", [task_root, "last_input"]),
  389. ("RDE", [task_root, "last_input"]),
  390. ("CN", []),
  391. ("CNV", [value]),
  392. ]
  393. _, _, _, _ = yield [("CD", [task_root, "last_input", new_input]),
  394. ("CD", [old_input, "next", new_input]),
  395. ("CD", [old_input, "value", new_value]),
  396. ("DE", [link])
  397. ]
  398. self.returnvalue = {"id": 100, "value": "success"}
  399. #############################################
  400. ### Transformation rules for instructions ###
  401. #############################################
  402. def continue_init(self, task_root):
  403. task_frame, = yield [("RD", [task_root, "frame"])]
  404. inst, = yield [("RD", [task_frame, "IP"])]
  405. while_inst, = yield [("RD", [inst, "while"])]
  406. old_evalstack_link, old_phase_link, evalstack_roots = \
  407. yield [("RDE", [task_frame, "evalstack"]),
  408. ("RDE", [task_frame, "phase"]),
  409. ("RRD", [while_inst, self.taskname]),
  410. ]
  411. if len(evalstack_roots) == 1:
  412. evalstack_root = evalstack_roots[0]
  413. else:
  414. print("Got roots: " + str(evalstack_roots))
  415. raise Exception("Could not process continue statement!")
  416. prev_evalstack_roots, old_evalstack_phase_link = \
  417. yield [("RRD", [evalstack_root, "prev"]),
  418. ("RDE", [evalstack_root, "phase"]),
  419. ]
  420. if len(prev_evalstack_roots) == 1:
  421. prev_evalstack_root = prev_evalstack_roots[0]
  422. else:
  423. raise Exception("Could not process continue statement!")
  424. new_evalstack_root, new_phase_while, new_phase_inst, prev_evalstack_root_link = \
  425. yield [("CN", []),
  426. ("CNV", ["init"]),
  427. ("CNV", ["finish"]),
  428. ("RDE", [prev_evalstack_root, "prev"]),
  429. ]
  430. _, _, _, _, _, _, _, _ = \
  431. yield [("CD", [task_frame, "evalstack", new_evalstack_root]),
  432. ("CD", [new_evalstack_root, "prev", evalstack_root]),
  433. ("CD", [task_frame, "phase", new_phase_inst]),
  434. ("CD", [evalstack_root, "phase", new_phase_while]),
  435. ("DE", [old_evalstack_link]),
  436. ("DE", [prev_evalstack_root_link]),
  437. ("DE", [old_phase_link]),
  438. ("DE", [old_evalstack_phase_link]),
  439. ]
  440. def break_init(self, task_root):
  441. task_frame, = yield [("RD", [task_root, "frame"])]
  442. inst, = yield [("RD", [task_frame, "IP"])]
  443. while_inst, = yield [("RD", [inst, "while"])]
  444. old_evalstack_link, old_phase_link, evalstack_roots = \
  445. yield [("RDE", [task_frame, "evalstack"]),
  446. ("RDE", [task_frame, "phase"]),
  447. ("RRD", [while_inst, self.taskname]),
  448. ]
  449. if len(evalstack_roots) == 1:
  450. evalstack_root = evalstack_roots[0]
  451. else:
  452. raise Exception("Could not process break statement!")
  453. prev_evalstack_roots, old_evalstack_phase_link = \
  454. yield [("RRD", [evalstack_root, "prev"]),
  455. ("RDE", [evalstack_root, "phase"]),
  456. ]
  457. if len(prev_evalstack_roots) == 1:
  458. prev_evalstack_root = prev_evalstack_roots[0]
  459. else:
  460. raise Exception("Could not process break statement!")
  461. new_evalstack_root, new_phase_while, new_phase_inst, prev_evalstack_root_link = \
  462. yield [("CN", []),
  463. ("CNV", ["finish"]),
  464. ("CNV", ["finish"]),
  465. ("RDE", [prev_evalstack_root, "prev"]),
  466. ]
  467. _, _, _, _, _, _, _, _ = \
  468. yield [("CD", [task_frame, "evalstack", new_evalstack_root]),
  469. ("CD", [new_evalstack_root, "prev", evalstack_root]),
  470. ("CD", [task_frame, "phase", new_phase_inst]),
  471. ("CD", [evalstack_root, "phase", new_phase_while]),
  472. ("DE", [old_evalstack_link]),
  473. ("DE", [prev_evalstack_root_link]),
  474. ("DE", [old_phase_link]),
  475. ("DE", [old_evalstack_phase_link]),
  476. ]
  477. def if_init(self, task_root):
  478. task_frame, = yield [("RD", [task_root, "frame"])]
  479. evalstack, evalstack_link = \
  480. yield [("RD", [task_frame, "evalstack"]),
  481. ("RDE", [task_frame, "evalstack"]),
  482. ]
  483. inst, ip_link = yield [("RD", [task_frame, "IP"]),
  484. ("RDE", [task_frame, "IP"]),
  485. ]
  486. cond, = yield [("RD", [inst, "cond"])]
  487. new_evalstack, new_phase = \
  488. yield [("CN", []),
  489. ("CNV", ["cond"]),
  490. ]
  491. _, _, _, _, _, _, _ = \
  492. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  493. ("CD", [new_evalstack, "prev", evalstack]),
  494. ("CD", [task_frame, "IP", cond]),
  495. ("CD", [evalstack, "inst", inst]),
  496. ("CD", [evalstack, "phase", new_phase]),
  497. ("DE", [evalstack_link]),
  498. ("DE", [ip_link]),
  499. ]
  500. def if_cond(self, task_root):
  501. task_frame, = yield [("RD", [task_root, "frame"])]
  502. returnvalue, inst = yield [("RD", [task_frame, "returnvalue"]),
  503. ("RD", [task_frame, "IP"]),
  504. ]
  505. returnvalue_v, = yield [("RV", [returnvalue])]
  506. _else, = yield [("RD", [inst, "else"])]
  507. if returnvalue_v:
  508. phase_link, evalstack, evalstack_link, ip_link, _then, new_evalstack, evalstack_phase, new_phase = \
  509. yield [("RDE", [task_frame, "phase"]),
  510. ("RD", [task_frame, "evalstack"]),
  511. ("RDE", [task_frame, "evalstack"]),
  512. ("RDE", [task_frame, "IP"]),
  513. ("RD", [inst, "then"]),
  514. ("CN", []),
  515. ("CNV", ["finish"]),
  516. ("CNV", ["init"]),
  517. ]
  518. _, _, _, _, _, _, _, _, _ = \
  519. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  520. ("CD", [task_frame, "IP", _then]),
  521. ("CD", [new_evalstack, "prev", evalstack]),
  522. ("CD", [evalstack, "inst", inst]),
  523. ("CD", [evalstack, "phase", evalstack_phase]),
  524. ("CD", [task_frame, "phase", new_phase]),
  525. ("DE", [evalstack_link]),
  526. ("DE", [ip_link]),
  527. ("DE", [phase_link]),
  528. ]
  529. elif _else is None:
  530. phase_link, new_phase = \
  531. yield [("RDE", [task_frame, "phase"]),
  532. ("CNV", ["finish"]),
  533. ]
  534. _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  535. ("DE", [phase_link]),
  536. ]
  537. else:
  538. phase_link, evalstack, evalstack_link, ip_link = \
  539. yield [("RDE", [task_frame, "phase"]),
  540. ("RD", [task_frame, "evalstack"]),
  541. ("RDE", [task_frame, "evalstack"]),
  542. ("RDE", [task_frame, "IP"]),
  543. ]
  544. new_evalstack, new_phase, evalstack_phase = \
  545. yield [("CN", []),
  546. ("CNV", ["init"]),
  547. ("CNV", ["finish"]),
  548. ]
  549. _, _, _, _, _, _, _, _, _ = \
  550. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  551. ("CD", [task_frame, "IP", _else]),
  552. ("CD", [new_evalstack, "prev", evalstack]),
  553. ("CD", [evalstack, "inst", inst]),
  554. ("CD", [evalstack, "phase", evalstack_phase]),
  555. ("CD", [task_frame, "phase", new_phase]),
  556. ("DE", [evalstack_link]),
  557. ("DE", [ip_link]),
  558. ("DE", [phase_link]),
  559. ]
  560. def while_init(self, task_root):
  561. task_frame, = yield [("RD", [task_root, "frame"])]
  562. evalstack, evalstack_link, ip_link, inst = \
  563. yield [("RD", [task_frame, "evalstack"]),
  564. ("RDE", [task_frame, "evalstack"]),
  565. ("RDE", [task_frame, "IP"]),
  566. ("RD", [task_frame, "IP"]),
  567. ]
  568. cond, new_evalstack, new_phase = \
  569. yield [("RD", [inst, "cond"]),
  570. ("CN", []),
  571. ("CNV", ["cond"]),
  572. ]
  573. _, _, _, _, _, _, _ = \
  574. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  575. ("CD", [new_evalstack, "prev", evalstack]),
  576. ("CD", [task_frame, "IP", cond]),
  577. ("CD", [evalstack, "phase", new_phase]),
  578. ("CD", [evalstack, "inst", inst]),
  579. ("DE", [evalstack_link]),
  580. ("DE", [ip_link]),
  581. ]
  582. def while_cond(self, task_root):
  583. task_frame, = yield [("RD", [task_root, "frame"])]
  584. returnvalue, = yield [("RD", [task_frame, "returnvalue"])]
  585. returnvalue_v, = yield [("RV", [returnvalue])]
  586. if returnvalue_v:
  587. phase_link, evalstack, evalstack_link, ip_link, inst = \
  588. yield [("RDE", [task_frame, "phase"]),
  589. ("RD", [task_frame, "evalstack"]),
  590. ("RDE", [task_frame, "evalstack"]),
  591. ("RDE", [task_frame, "IP"]),
  592. ("RD", [task_frame, "IP"]),
  593. ]
  594. body, = yield [("RD", [inst, "body"])]
  595. new_evalstack, new_phase, evalstack_phase = \
  596. yield [("CN", []),
  597. ("CNV", ["init"]),
  598. ("CNV", ["init"]),
  599. ]
  600. _, _, _, _, _, _, _, _, _ = \
  601. yield [("CD", [task_frame, "IP", body]),
  602. ("CD", [task_frame, "phase", new_phase]),
  603. ("CD", [task_frame, "evalstack", new_evalstack]),
  604. ("CD", [new_evalstack, "prev", evalstack]),
  605. ("CD", [evalstack, "inst", inst]),
  606. ("CD", [evalstack, "phase", evalstack_phase]),
  607. ("DE", [evalstack_link]),
  608. ("DE", [ip_link]),
  609. ("DE", [phase_link]),
  610. ]
  611. # Check if we already have a taskname link to the evalstack
  612. links, = yield [("RD", [evalstack, self.taskname])]
  613. if links is None:
  614. yield [("CD", [evalstack, self.taskname, inst])]
  615. else:
  616. phase_link, new_phase = \
  617. yield [("RDE", [task_frame, "phase"]),
  618. ("CNV", ["finish"]),
  619. ]
  620. _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  621. ("DE", [phase_link])
  622. ]
  623. def access_init(self, task_root):
  624. task_frame, = yield [("RD", [task_root, "frame"])]
  625. evalstack, evalstack_link, inst, ip_link = \
  626. yield [("RD", [task_frame, "evalstack"]),
  627. ("RDE", [task_frame, "evalstack"]),
  628. ("RD", [task_frame, "IP"]),
  629. ("RDE", [task_frame, "IP"]),
  630. ]
  631. var, new_evalstack, new_phase = \
  632. yield [("RD", [inst, "var"]),
  633. ("CN", []),
  634. ("CNV", ["eval"]),
  635. ]
  636. _, _, _, _, _, _, _ = \
  637. yield [("CD", [task_frame, "IP", var]),
  638. ("CD", [task_frame, "evalstack", new_evalstack]),
  639. ("CD", [new_evalstack, "prev", evalstack]),
  640. ("CD", [evalstack, "inst", inst]),
  641. ("CD", [evalstack, "phase", new_phase]),
  642. ("DE", [evalstack_link]),
  643. ("DE", [ip_link]),
  644. ]
  645. def access_eval(self, task_root):
  646. task_frame, = yield [("RD", [task_root, "frame"])]
  647. phase_link, returnvalue_link, returnvalue = \
  648. yield [("RDE", [task_frame, "phase"]),
  649. ("RDE", [task_frame, "returnvalue"]),
  650. ("RD", [task_frame, "returnvalue"]),
  651. ]
  652. value, new_phase = yield [("RD", [returnvalue, "value"]),
  653. ("CNV", ["finish"]),
  654. ]
  655. _, _, _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  656. ("CD", [task_frame, "returnvalue", value]),
  657. ("DE", [phase_link]),
  658. ("DE", [returnvalue_link]),
  659. ]
  660. def resolve_init(self, task_root):
  661. task_frame, = yield [("RD", [task_root, "frame"])]
  662. symbols, evalstack, evalstack_link, ip_link, inst = \
  663. yield [("RD", [task_frame, "symbols"]),
  664. ("RD", [task_frame, "evalstack"]),
  665. ("RDE", [task_frame, "evalstack"]),
  666. ("RDE", [task_frame, "IP"]),
  667. ("RD", [task_frame, "IP"]),
  668. ]
  669. var, = yield [("RD", [inst, "var"])]
  670. variable, = yield [("RDN", [symbols, var])]
  671. if variable is None:
  672. phase_link, returnvalue_link, _globals, var_name = \
  673. yield [("RDE", [task_frame, "phase"]),
  674. ("RDE", [task_frame, "returnvalue"]),
  675. ("RD", [task_root, "globals"]),
  676. ("RV", [var]),
  677. ]
  678. variable, new_phase = \
  679. yield [("RD", [_globals, var_name]),
  680. ("CNV", ["finish"]),
  681. ]
  682. if variable is None:
  683. raise Exception(jit_runtime.GLOBAL_NOT_FOUND_MESSAGE_FORMAT % var_name)
  684. # Resolved a global, so this is a string
  685. # Potentially, this might even be a function that we have precompiled already!
  686. # So check whether this is the case or not
  687. if self.allow_compiled:
  688. compiled_function = getattr(compiled_functions, var_name, None)
  689. if compiled_function is not None:
  690. # We have a compiled function ready!
  691. # Now we have to bind the ID to the compiled functions
  692. # For this, we read out the body of the resolved data
  693. compiler_val, = yield [("RD", [variable, "value"])]
  694. compiler_body, = yield [("RD", [compiler_val, "body"])]
  695. self.jit.register_compiled(compiler_body, compiled_function, var_name)
  696. # If we're dealing with a function, then we might want to figure out what its body id
  697. # is now so we can suggest a name to the JIT later.
  698. if self.suggest_function_names and self.jit.get_global_body_id(var_name) is None:
  699. compiler_val, = yield [("RD", [variable, "value"])]
  700. if compiler_val is not None:
  701. compiler_body, = yield [("RD", [compiler_val, "body"])]
  702. if compiler_body is not None:
  703. self.jit.register_global(compiler_body, var_name)
  704. else:
  705. phase_link, returnvalue_link, new_phase = \
  706. yield [("RDE", [task_frame, "phase"]),
  707. ("RDE", [task_frame, "returnvalue"]),
  708. ("CNV", ["finish"]),
  709. ]
  710. _, _, _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  711. ("CD", [task_frame, "returnvalue", variable]),
  712. ("DE", [phase_link]),
  713. ("DE", [returnvalue_link]),
  714. ]
  715. def assign_init(self, task_root):
  716. task_frame, = yield [("RD", [task_root, "frame"])]
  717. evalstack, evalstack_link, ip_link, inst = \
  718. yield [("RD", [task_frame, "evalstack"]),
  719. ("RDE", [task_frame, "evalstack"]),
  720. ("RDE", [task_frame, "IP"]),
  721. ("RD", [task_frame, "IP"]),
  722. ]
  723. var, new_evalstack, new_phase = \
  724. yield [("RD", [inst, "var"]),
  725. ("CN", []),
  726. ("CNV", ["value"]),
  727. ]
  728. _, _, _, _, _, _, _ = \
  729. yield [("CD", [task_frame, "IP", var]),
  730. ("CD", [task_frame, "evalstack", new_evalstack]),
  731. ("CD", [new_evalstack, "prev", evalstack]),
  732. ("CD", [evalstack, "inst", inst]),
  733. ("CD", [evalstack, "phase", new_phase]),
  734. ("DE", [evalstack_link]),
  735. ("DE", [ip_link]),
  736. ]
  737. def assign_value(self, task_root):
  738. task_frame, = yield [("RD", [task_root, "frame"])]
  739. phase_link, evalstack, returnvalue, evalstack_link, ip_link, inst = \
  740. yield [("RDE", [task_frame, "phase"]),
  741. ("RD", [task_frame, "evalstack"]),
  742. ("RD", [task_frame, "returnvalue"]),
  743. ("RDE", [task_frame, "evalstack"]),
  744. ("RDE", [task_frame, "IP"]),
  745. ("RD", [task_frame, "IP"]),
  746. ]
  747. value, new_evalstack, new_phase, evalstack_phase = \
  748. yield [("RD", [inst, "value"]),
  749. ("CN", []),
  750. ("CNV", ["init"]),
  751. ("CNV", ["assign"]),
  752. ]
  753. _, _, _, _, _, _, _, _, _, _ = \
  754. yield [("CD", [task_frame, "variable", returnvalue]),
  755. ("CD", [task_frame, "phase", new_phase]),
  756. ("CD", [task_frame, "evalstack", new_evalstack]),
  757. ("CD", [new_evalstack, "prev", evalstack]),
  758. ("CD", [evalstack, "inst", inst]),
  759. ("CD", [evalstack, "phase", evalstack_phase]),
  760. ("CD", [task_frame, "IP", value]),
  761. ("DE", [evalstack_link]),
  762. ("DE", [phase_link]),
  763. ("DE", [ip_link]),
  764. ]
  765. def assign_assign(self, task_root):
  766. task_frame, = yield [("RD", [task_root, "frame"])]
  767. phase_link, returnvalue, variable_link, variable = \
  768. yield [("RDE", [task_frame, "phase"]),
  769. ("RD", [task_frame, "returnvalue"]),
  770. ("RDE", [task_frame, "variable"]),
  771. ("RD", [task_frame, "variable"]),
  772. ]
  773. value_link, new_phase = \
  774. yield [("RDE", [variable, "value"]),
  775. ("CNV", ["finish"]),
  776. ]
  777. _, _, _, _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  778. ("CD", [variable, "value", returnvalue]),
  779. ("DE", [variable_link]),
  780. ("DE", [value_link]),
  781. ("DE", [phase_link]),
  782. ]
  783. def return_init(self, task_root):
  784. task_frame, = yield [("RD", [task_root, "frame"])]
  785. inst, = yield [("RD", [task_frame, "IP"])]
  786. value, = yield [("RD", [inst, "value"])]
  787. if value is None:
  788. prev_frame, = yield [("RD", [task_frame, "prev"])]
  789. # If the callee's frame is marked with the '__exception_return' key, then
  790. # we need to throw an exception instead of just finishing here. This design
  791. # gives us O(1) state reads per jit-interpreter transition.
  792. exception_return, = yield [("RD", [task_frame, primitive_functions.EXCEPTION_RETURN_KEY])]
  793. if prev_frame is None:
  794. _, = yield [("DN", [task_root])]
  795. del self.debug_info[self.taskname]
  796. #print("Cleanup task " + str(self.taskname))
  797. else:
  798. if self.debug_info[self.taskname]:
  799. self.debug_info[self.taskname].pop()
  800. _, _ = yield [("CD", [task_root, "frame", prev_frame]),
  801. ("DN", [task_frame]),
  802. ]
  803. if exception_return is not None:
  804. raise primitive_functions.InterpretedFunctionFinished(None)
  805. else:
  806. evalstack, evalstack_link, ip_link, new_evalstack, evalstack_phase = \
  807. yield [("RD", [task_frame, "evalstack"]),
  808. ("RDE", [task_frame, "evalstack"]),
  809. ("RDE", [task_frame, "IP"]),
  810. ("CN", []),
  811. ("CNV", ["eval"]),
  812. ]
  813. _, _, _, _, _, _, _ = \
  814. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  815. ("CD", [new_evalstack, "prev", evalstack]),
  816. ("CD", [evalstack, "inst", inst]),
  817. ("CD", [evalstack, "phase", evalstack_phase]),
  818. ("CD", [task_frame, "IP", value]),
  819. ("DE", [evalstack_link]),
  820. ("DE", [ip_link]),
  821. ]
  822. def return_eval(self, task_root):
  823. if self.debug_info[self.taskname]:
  824. self.debug_info[self.taskname].pop()
  825. task_frame, = yield [("RD", [task_root, "frame"])]
  826. prev_frame, = yield [("RD", [task_frame, "prev"])]
  827. if prev_frame is None:
  828. _, = yield [("DN", [task_root])]
  829. del self.debug_info[self.taskname]
  830. exception_return, returnvalue = yield [
  831. ("RD", [task_frame, primitive_functions.EXCEPTION_RETURN_KEY]),
  832. ("RD", [task_frame, "returnvalue"])]
  833. # If the callee's frame is marked with the '__exception_return' key, then
  834. # we need to throw an exception instead of just finishing here. This design
  835. # gives us O(1) state reads per jit-interpreter transition.
  836. if exception_return is not None:
  837. yield [
  838. ("CD", [task_root, "frame", prev_frame]),
  839. ("DN", [task_frame])]
  840. raise primitive_functions.InterpretedFunctionFinished(returnvalue)
  841. else:
  842. old_returnvalue_link, = yield [("RDE", [prev_frame, "returnvalue"])]
  843. yield [
  844. ("CD", [task_root, "frame", prev_frame]),
  845. ("CD", [prev_frame, "returnvalue", returnvalue]),
  846. ("DE", [old_returnvalue_link]),
  847. ("DN", [task_frame])]
  848. def constant_init(self, task_root):
  849. task_frame, = yield [("RD", [task_root, "frame"])]
  850. phase_link, returnvalue_link, inst = \
  851. yield [("RDE", [task_frame, "phase"]),
  852. ("RDE", [task_frame, "returnvalue"]),
  853. ("RD", [task_frame, "IP"]),
  854. ]
  855. node, new_phase = yield [("RD", [inst, "node"]),
  856. ("CNV", ["finish"]),
  857. ]
  858. _, _, _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  859. ("CD", [task_frame, "returnvalue", node]),
  860. ("DE", [returnvalue_link]),
  861. ("DE", [phase_link]),
  862. ]
  863. def helper_init(self, task_root):
  864. task_frame, = yield [("RD", [task_root, "frame"])]
  865. inst, = yield [("RD", [task_frame, "IP"])]
  866. next, = yield [("RD", [inst, "next"])]
  867. if next is None:
  868. ip_link, phase_link, evalstack_top = \
  869. yield [("RDE", [task_frame, "IP"]),
  870. ("RDE", [task_frame, "phase"]),
  871. ("RD", [task_frame, "evalstack"]),
  872. ]
  873. evalstack, = yield [("RD", [evalstack_top, "prev"])]
  874. evalstack_inst, evalstack_phase, evalstack_inst_link, evalstack_phase_link = \
  875. yield [("RD", [evalstack, "inst"]),
  876. ("RD", [evalstack, "phase"]),
  877. ("RDE", [evalstack, "inst"]),
  878. ("RDE", [evalstack, "phase"]),
  879. ]
  880. _, _, _, _, _, _, _, _ = \
  881. yield [("CD", [task_frame, "evalstack", evalstack]),
  882. ("CD", [task_frame, "IP", evalstack_inst]),
  883. ("CD", [task_frame, "phase", evalstack_phase]),
  884. ("DE", [ip_link]),
  885. ("DE", [phase_link]),
  886. ("DE", [evalstack_inst_link]),
  887. ("DE", [evalstack_phase_link]),
  888. ("DN", [evalstack_top]),
  889. ]
  890. else:
  891. ip_link, phase_link, new_phase = \
  892. yield [("RDE", [task_frame, "IP"]),
  893. ("RDE", [task_frame, "phase"]),
  894. ("CNV", ["init"]),
  895. ]
  896. _, _, _, _ = yield [("CD", [task_frame, "IP", next]),
  897. ("CD", [task_frame, "phase", new_phase]),
  898. ("DE", [ip_link]),
  899. ("DE", [phase_link]),
  900. ]
  901. def call_init(self, task_root):
  902. task_frame, = yield [("RD", [task_root, "frame"])]
  903. symbols, evalstack, evalstack_link, ip_link, inst = \
  904. yield [("RD", [task_frame, "symbols"]),
  905. ("RD", [task_frame, "evalstack"]),
  906. ("RDE", [task_frame, "evalstack"]),
  907. ("RDE", [task_frame, "IP"]),
  908. ("RD", [task_frame, "IP"]),
  909. ]
  910. func, params = yield [("RD", [inst, "func"]),
  911. ("RD", [inst, "params"]),
  912. ]
  913. if params is None:
  914. new_evalstack, evalstack_phase = \
  915. yield [("CN", []),
  916. ("CNV", ["call"]),
  917. ]
  918. _, _, _, _, _, _, _ = \
  919. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  920. ("CD", [new_evalstack, "prev", evalstack]),
  921. ("CD", [evalstack, "inst", inst]),
  922. ("CD", [evalstack, "phase", evalstack_phase]),
  923. ("CD", [task_frame, "IP", func]),
  924. ("DE", [evalstack_link]),
  925. ("DE", [ip_link]),
  926. ]
  927. else:
  928. new_evalstack,= yield [("CN", [])]
  929. _, _, _, _, _, _, _ = \
  930. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  931. ("CD", [new_evalstack, "prev", evalstack]),
  932. ("CD", [evalstack, "inst", inst]),
  933. ("CD", [evalstack, "phase", params]),
  934. ("CD", [task_frame, "IP", func]),
  935. ("DE", [evalstack_link]),
  936. ("DE", [ip_link]),
  937. ]
  938. def call_call(self, task_root):
  939. self.debug_info[self.taskname].append("None")
  940. task_frame, = yield [("RD", [task_root, "frame"])]
  941. inst, = yield [("RD", [task_frame, "IP"])]
  942. param, = yield [("RD", [inst, "last_param"])]
  943. if param is None:
  944. returnvalue, = yield [("RD", [task_frame, "returnvalue"])]
  945. body, = yield [("RD", [returnvalue, "body"])]
  946. self.jit.mark_entry_point(body)
  947. phase_link, frame_link, prev_phase, new_phase, new_frame, new_evalstack, new_symbols, new_returnvalue = \
  948. yield [("RDE", [task_frame, "phase"]),
  949. ("RDE", [task_root, "frame"]),
  950. ("CNV", ["finish"]),
  951. ("CNV", ["init"]),
  952. ("CN", []),
  953. ("CN", []),
  954. ("CN", []),
  955. ("CN", []),
  956. ]
  957. _, _, _, _, _, _, _, _, _, _, _ = \
  958. yield [("CD", [task_root, "frame", new_frame]),
  959. ("CD", [new_frame, "evalstack", new_evalstack]),
  960. ("CD", [new_frame, "symbols", new_symbols]),
  961. ("CD", [new_frame, "returnvalue", new_returnvalue]),
  962. ("CD", [new_frame, "caller", inst]),
  963. ("CD", [new_frame, "phase", new_phase]),
  964. ("CD", [new_frame, "IP", body]),
  965. ("CD", [new_frame, "prev", task_frame]),
  966. ("CD", [task_frame, "phase", prev_phase]),
  967. ("DE", [phase_link]),
  968. ("DE", [frame_link]),
  969. ]
  970. else:
  971. newer_frames, invoking_frames = \
  972. yield [("RRD", [task_frame, "prev"]),
  973. ("RRD", [inst, "caller"]),
  974. ]
  975. new_frame = self.find_overlapping(newer_frames, invoking_frames)
  976. phase_link, frame_link, new_symbols, new_IP = \
  977. yield [("RDE", [task_frame, "phase"]),
  978. ("RDE", [task_root, "frame"]),
  979. ("RD", [new_frame, "symbols"]),
  980. ("RD", [new_frame, "IP"]),
  981. ]
  982. signature, = yield [("RRD", [new_IP, "body"])]
  983. signature = signature[0]
  984. sig_params, last_param = \
  985. yield [("RD", [signature, "params"]),
  986. ("RD", [inst, "last_param"]),
  987. ]
  988. self.jit.mark_entry_point(new_IP)
  989. name, = yield [("RD", [last_param, "name"])]
  990. name_value, = yield [("RV", [name])]
  991. returnvalue, formal_parameter, new_phase, variable = \
  992. yield [("RD", [task_frame, "returnvalue"]),
  993. ("RD", [sig_params, name_value]),
  994. ("CNV", ["finish"]),
  995. ("CN", []),
  996. ]
  997. _, _, _, t1 = yield [("CD", [task_root, "frame", new_frame]),
  998. ("CD", [task_frame, "phase", new_phase]),
  999. ("CD", [variable, "value", returnvalue]),
  1000. ("CE", [new_symbols, variable]),
  1001. ]
  1002. _, _, _ = yield [("CE", [t1, formal_parameter]),
  1003. ("DE", [frame_link]),
  1004. ("DE", [phase_link]),
  1005. ]
  1006. def find_overlapping(self, a, b):
  1007. newer_frames = set(a)
  1008. invoking_frames = set(b)
  1009. matches = list(newer_frames.intersection(invoking_frames))
  1010. if len(matches) == 1:
  1011. return matches[0]
  1012. elif len(matches) > 1:
  1013. raise Exception("Error: multiple overlapping elements")
  1014. else:
  1015. raise Exception("Error: could not find any overlap")
  1016. def call_param(self, task_root):
  1017. task_frame, = yield [("RD", [task_root, "frame"])]
  1018. inst, phase = yield [("RD", [task_frame, "IP"]),
  1019. ("RD", [task_frame, "phase"]),
  1020. ]
  1021. params, last_param = \
  1022. yield [("RD", [inst, "params"]),
  1023. ("RD", [inst, "last_param"]),
  1024. ]
  1025. next_param, = yield [("RD", [params, "next_param"])]
  1026. if params == phase:
  1027. phase_link, ip_link, returnvalue, param_value, evalstack, evalstack_link = \
  1028. yield [("RDE", [task_frame, "phase"]),
  1029. ("RDE", [task_frame, "IP"]),
  1030. ("RD", [task_frame, "returnvalue"]),
  1031. ("RD", [params, "value"]),
  1032. ("RD", [task_frame, "evalstack"]),
  1033. ("RDE", [task_frame, "evalstack"]),
  1034. ]
  1035. body, = yield [("RD", [returnvalue, "body"])]
  1036. new_frame, prev_evalstack, new_phase, prev_phase, new_evalstack, new_symbols, new_returnvalue = \
  1037. yield [("CN", []),
  1038. ("CN", []),
  1039. ("CNV", ["init"]),
  1040. ("CNV", ["init"]),
  1041. ("CN", []),
  1042. ("CN", []),
  1043. ("CN", []),
  1044. ]
  1045. _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = \
  1046. yield [("CD", [new_frame, "evalstack", new_evalstack]),
  1047. ("CD", [new_frame, "symbols", new_symbols]),
  1048. ("CD", [new_frame, "returnvalue", new_returnvalue]),
  1049. ("CD", [new_frame, "caller", inst]),
  1050. ("CD", [new_frame, "phase", new_phase]),
  1051. ("CD", [new_frame, "IP", body]),
  1052. ("CD", [new_frame, "prev", task_frame]),
  1053. ("CD", [task_frame, "phase", prev_phase]),
  1054. ("CD", [task_frame, "IP", param_value]),
  1055. ("CD", [prev_evalstack, "prev", evalstack]),
  1056. ("CD", [evalstack, "inst", inst]),
  1057. ("CD", [task_frame, "evalstack", prev_evalstack]),
  1058. ("DE", [evalstack_link]),
  1059. ("DE", [ip_link]),
  1060. ("DE", [phase_link]),
  1061. ]
  1062. if next_param is not None:
  1063. _ = yield [("CD", [evalstack, "phase", next_param])]
  1064. else:
  1065. evalstack_phase, = \
  1066. yield [("CNV", ["call"])]
  1067. _ = yield [("CD", [evalstack, "phase", evalstack_phase])]
  1068. else:
  1069. frame_link, phase_link, newer_frames, invoking_frames = \
  1070. yield [("RDE", [task_root, "frame"]),
  1071. ("RDE", [task_frame, "phase"]),
  1072. ("RRD", [task_frame, "prev"]),
  1073. ("RRD", [inst, "caller"]),
  1074. ]
  1075. new_frame = self.find_overlapping(newer_frames, invoking_frames)
  1076. ip_link, evalstack, evalstack_link, new_symbols, new_IP = \
  1077. yield [("RDE", [task_frame, "IP"]),
  1078. ("RD", [task_frame, "evalstack"]),
  1079. ("RDE", [task_frame, "evalstack"]),
  1080. ("RD", [new_frame, "symbols"]),
  1081. ("RD", [new_frame, "IP"]),
  1082. ]
  1083. signature, = yield [("RRD", [new_IP, "body"])]
  1084. signature = signature[0]
  1085. sig_params, = yield [("RD", [signature, "params"])]
  1086. if last_param == phase:
  1087. prev_param, = \
  1088. yield [("RRD", [last_param, "next_param"])]
  1089. prev_param = prev_param[0]
  1090. name, = yield [("RD", [prev_param, "name"])]
  1091. name_value, = \
  1092. yield [("RV", [name])]
  1093. evalstack_phase, = \
  1094. yield [("CNV", ["call"])]
  1095. _ = yield [("CD", [evalstack, "phase", evalstack_phase])]
  1096. formal_parameter, param_value = \
  1097. yield [("RD", [sig_params, name_value]),
  1098. ("RD", [last_param, "value"]),
  1099. ]
  1100. else:
  1101. param_b, = yield [("RD", [task_frame, "phase"])]
  1102. param_c, param_a = \
  1103. yield [("RD", [param_b, "next_param"]),
  1104. ("RRD", [param_b, "next_param"]),
  1105. ]
  1106. param_a = param_a[0]
  1107. name, param_value = \
  1108. yield [("RD", [param_a, "name"]),
  1109. ("RD", [param_b, "value"]),
  1110. ]
  1111. name_value, = \
  1112. yield [("RV", [name])]
  1113. formal_parameter, _ = \
  1114. yield [("RD", [sig_params, name_value]),
  1115. ("CD", [evalstack, "phase", param_c]),
  1116. ]
  1117. new_phase, new_evalstack, variable, returnvalue = \
  1118. yield [("CNV", ["init"]),
  1119. ("CN", []),
  1120. ("CN", []),
  1121. ("RD", [task_frame, "returnvalue"]),
  1122. ]
  1123. _, _, _, _, _, _ = \
  1124. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  1125. ("CD", [new_evalstack, "prev", evalstack]),
  1126. ("CD", [evalstack, "inst", inst]),
  1127. ("CD", [task_frame, "phase", new_phase]),
  1128. ("CD", [task_frame, "IP", param_value]),
  1129. ("CD", [variable, "value", returnvalue]),
  1130. ]
  1131. t1, = yield [("CE", [new_symbols, variable])]
  1132. _, _, _, _ = \
  1133. yield [("CE", [t1, formal_parameter]),
  1134. ("DE", [phase_link]),
  1135. ("DE", [ip_link]),
  1136. ("DE", [evalstack_link]),
  1137. ]
  1138. def input_init(self, task_root):
  1139. task_frame, = yield [("RD", [task_root, "frame"])]
  1140. returnvalue_link, _input = \
  1141. yield [("RDE", [task_frame, "returnvalue"]),
  1142. ("RD", [task_root, "input"]),
  1143. ]
  1144. value, next, phase_link = \
  1145. yield [("RD", [_input, "value"]),
  1146. ("RD", [_input, "next"]),
  1147. ("RDE", [task_frame, "phase"]),
  1148. ]
  1149. if value is not None:
  1150. v = yield [("RV", [value])]
  1151. _, _, finish = \
  1152. yield [("CD", [task_frame, "returnvalue", value]),
  1153. ("CD", [task_root, "input", next]),
  1154. ("CNV", ["finish"]),
  1155. ]
  1156. _, _, _, _ = \
  1157. yield [("CD", [task_frame, "phase", finish]),
  1158. ("DN", [_input]),
  1159. ("DE", [returnvalue_link]),
  1160. ("DE", [phase_link]),
  1161. ]
  1162. self.input_value = value
  1163. else:
  1164. # No input yet, so just wait and don't advance IP or phase
  1165. self.input_value = None
  1166. ex = primitive_functions.SleepKernel(0.1, True)
  1167. raise ex
  1168. def output_init(self, task_root):
  1169. task_frame, = yield [("RD", [task_root, "frame"])]
  1170. evalstack, evalstack_link, ip_link, inst = \
  1171. yield [("RD", [task_frame, "evalstack"]),
  1172. ("RDE", [task_frame, "evalstack"]),
  1173. ("RDE", [task_frame, "IP"]),
  1174. ("RD", [task_frame, "IP"]),
  1175. ]
  1176. value, new_evalstack, evalstack_phase = \
  1177. yield [("RD", [inst, "value"]),
  1178. ("CN", []),
  1179. ("CNV", ["output"]),
  1180. ]
  1181. _, _, _, _, _, _, _ = \
  1182. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  1183. ("CD", [new_evalstack, "prev", evalstack]),
  1184. ("CD", [evalstack, "inst", inst]),
  1185. ("CD", [evalstack, "phase", evalstack_phase]),
  1186. ("CD", [task_frame, "IP", value]),
  1187. ("DE", [evalstack_link]),
  1188. ("DE", [ip_link]),
  1189. ]
  1190. def output_output(self, task_root):
  1191. task_frame, = yield [("RD", [task_root, "frame"])]
  1192. returnvalue_link, returnvalue, last_output, phase_link, last_output_link, new_last_output, finish = \
  1193. yield [("RDE", [task_frame, "returnvalue"]),
  1194. ("RD", [task_frame, "returnvalue"]),
  1195. ("RD", [task_root, "last_output"]),
  1196. ("RDE", [task_frame, "phase"]),
  1197. ("RDE", [task_root, "last_output"]),
  1198. ("CN", []),
  1199. ("CNV", ["finish"]),
  1200. ]
  1201. _, _, _, _, _, _ = \
  1202. yield [("CD", [last_output, "value", returnvalue]),
  1203. ("CD", [last_output, "next", new_last_output]),
  1204. ("CD", [task_root, "last_output", new_last_output]),
  1205. ("CD", [task_frame, "phase", finish]),
  1206. ("DE", [last_output_link]),
  1207. ("DE", [phase_link]),
  1208. ]
  1209. def declare_init(self, task_root):
  1210. task_frame, = yield [("RD", [task_root, "frame"])]
  1211. inst, = yield [("RD", [task_frame, "IP"])]
  1212. new_var, symbols, phase_link, empty_node, new_phase = \
  1213. yield [("RD", [inst, "var"]),
  1214. ("RD", [task_frame, "symbols"]),
  1215. ("RDE", [task_frame, "phase"]),
  1216. ("CN", []),
  1217. ("CNV", ["finish"]),
  1218. ]
  1219. exists, = yield [("RDN", [symbols, new_var])]
  1220. if exists is None:
  1221. new_edge, = yield [("CE", [symbols, empty_node])]
  1222. _ = yield [("CE", [new_edge, new_var])]
  1223. _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  1224. ("DE", [phase_link]),
  1225. ]
  1226. def global_init(self, task_root):
  1227. task_frame, = yield [("RD", [task_root, "frame"])]
  1228. inst, = yield [("RD", [task_frame, "IP"])]
  1229. new_var, global_symbols, phase_link, empty_node, new_phase = \
  1230. yield [("RD", [inst, "var"]),
  1231. ("RD", [task_root, "globals"]),
  1232. ("RDE", [task_frame, "phase"]),
  1233. ("CN", []),
  1234. ("CNV", ["finish"]),
  1235. ]
  1236. value, = yield [("RV", [new_var])]
  1237. exists, = yield [("RDE", [global_symbols, value])]
  1238. if exists is not None:
  1239. yield [("DE", [exists])]
  1240. yield [("CD", [global_symbols, value, empty_node])]
  1241. _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  1242. ("DE", [phase_link])
  1243. ]