main.py 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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 + "raise PrimitiveFinished(%s)\n" % instruction_value
  215. else:
  216. instruction = " " * indent + "raise PrimitiveFinished(None)\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. name, = yield [("RD", [param, "name"])]
  242. name, = yield [("RV", [name])]
  243. (prev_res, instruction_res), param = \
  244. yield [("CALL_ARGS", [self.print_instruction, (value, 0, nested_indent)]),
  245. ("RD", [param, "next_param"])]
  246. computation += prev_res
  247. param_list[name] = instruction_res
  248. if func_name in intrinsics:
  249. #instruction = " " * indent + intrinsics[func_name](*param_list)
  250. # TODO
  251. pass
  252. else:
  253. value = "func_result_" + str(ModelverseKernel.counter)
  254. ModelverseKernel.counter += 1
  255. param_list = "{" + ", ".join(["'%s': %s" % (k, v) for k, v in param_list.items()]) + "}"
  256. actual_computation = "%s, = yield [('CALL_KWARGS', [%s, dict(%s.items() + kwargs.items())])]\n" % (value, func_name, 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. if params:
  296. func = "def " + suggested_name + "(" + ", ".join(["var_%s" % param for param in params]) + ", **kwargs):\n" + printed
  297. else:
  298. func = "def " + suggested_name + "(**kwargs):\n" + printed
  299. print(func)
  300. raise primitive_functions.PrimitiveFinished(func)
  301. def jit_compile(self, task_root, inst):
  302. # Try to retrieve the suggested name.
  303. suggested_name = self.jit.get_global_name(inst)
  304. # Have the JIT compile the function.
  305. #return self.jit.jit_compile(task_root, inst, suggested_name)
  306. if inst is None:
  307. raise ValueError('body_id cannot be None: ' + str(suggested_name))
  308. elif inst in self.jit.jitted_entry_points:
  309. raise primitive_functions.PrimitiveFinished(
  310. self.jit.jit_globals[self.jit.jitted_entry_points[inst]])
  311. compiled_func = self.jit.lookup_compiled_body(inst)
  312. if compiled_func is None:
  313. name = self.jit.get_global_name(inst)
  314. compiled_func, = yield [("CALL_ARGS", [self.read_function, (inst,)])]
  315. exec(str(compiled_func), self.jit.jit_globals)
  316. compiled_func = self.jit.jit_globals[name]
  317. self.jit.register_compiled(inst, compiled_func, name)
  318. #raise jit.JitCompilationFailedException("FAIL")
  319. raise primitive_functions.PrimitiveFinished(compiled_func)
  320. """
  321. try:
  322. raise primitive_functions.PrimitiveFinished(compiled_func)
  323. except Exception as e:
  324. print("Exception: " + str(e))
  325. raise jit.JitCompilationFailedException("FAIL")
  326. """
  327. def execute_jit(self, task_root, inst, taskname):
  328. # execute_jit
  329. task_frame, = yield [("RD", [task_root, "frame"])]
  330. symbols, = yield [("RD", [task_frame, "symbols"])]
  331. dict_keys_ref, = yield [("RDK", [symbols])]
  332. dict_keys_ref_n = yield [("RD", [i, "name"]) for i in dict_keys_ref]
  333. dict_keys = yield [("RV", [i]) for i in dict_keys_ref_n]
  334. dict_values_elem = yield [("RDN", [symbols, i]) for i in dict_keys_ref]
  335. dict_values = yield [("RD", [i, "value"]) for i in dict_values_elem]
  336. parameters = dict(list(zip(dict_keys, dict_values)))
  337. parameters["root"] = self.root
  338. parameters["task_root"] = task_root
  339. parameters["taskname"] = taskname
  340. parameters["mvk"] = self
  341. # Have the JIT compile the function.
  342. compiled_func, = yield [("CALL_ARGS", [self.jit_compile, (task_root, inst)])]
  343. # Run the compiled function.
  344. results = yield [("CALL_KWARGS", [compiled_func, parameters])]
  345. if results is None:
  346. raise Exception(
  347. "%s: primitive finished without returning a value!" % (self.debug_info[taskname]))
  348. else:
  349. result, = results
  350. # Clean up the current stack, as if a return happened
  351. old_frame, exception_return = yield [
  352. ("RD", [task_frame, "prev"]),
  353. ("RD", [task_frame, primitive_functions.EXCEPTION_RETURN_KEY])]
  354. if self.debug_info[self.taskname]:
  355. self.debug_info[self.taskname].pop()
  356. if exception_return is not None:
  357. # The caller has requested that we throw an exception instead of injecting
  358. # the return value into the caller's frame. Read the comment at
  359. # primitive_functions.EXCEPTION_RETURN_KEY for the rationale behind this design.
  360. yield [("CD", [task_root, "frame", old_frame]),
  361. ("DN", [task_frame])]
  362. raise primitive_functions.InterpretedFunctionFinished(result)
  363. else:
  364. lnk, = yield [("RDE", [old_frame, "returnvalue"])]
  365. _, _, _, _ = yield [("CD", [old_frame, "returnvalue", result]),
  366. ("CD", [task_root, "frame", old_frame]),
  367. ("DE", [lnk]),
  368. ("DN", [task_frame]),
  369. ]
  370. ########################################
  371. ### Execute input and output methods ###
  372. ########################################
  373. def get_output(self, taskname):
  374. task_root, = yield [("RD", [self.root, taskname])]
  375. first_output, = yield [("RD", [task_root, "output"])]
  376. next_output, rv = yield [("RD", [first_output, "next"]),
  377. ("RD", [first_output, "value"]),
  378. ]
  379. if next_output is None:
  380. self.success = False
  381. self.returnvalue = None
  382. else:
  383. rv_value, _, _ = \
  384. yield [("RV", [rv]),
  385. ("CD", [task_root, "output", next_output]),
  386. ("DN", [first_output]),
  387. ]
  388. self.returnvalue = rv_value
  389. self.success = True
  390. def set_input(self, taskname, value):
  391. task_root, = yield [("RD", [self.root, taskname])]
  392. old_input, link, new_input, new_value = \
  393. yield [("RD", [task_root, "last_input"]),
  394. ("RDE", [task_root, "last_input"]),
  395. ("CN", []),
  396. ("CNV", [value]),
  397. ]
  398. _, _, _, _ = yield [("CD", [task_root, "last_input", new_input]),
  399. ("CD", [old_input, "next", new_input]),
  400. ("CD", [old_input, "value", new_value]),
  401. ("DE", [link])
  402. ]
  403. self.returnvalue = {"id": 100, "value": "success"}
  404. #############################################
  405. ### Transformation rules for instructions ###
  406. #############################################
  407. def continue_init(self, task_root):
  408. task_frame, = yield [("RD", [task_root, "frame"])]
  409. inst, = yield [("RD", [task_frame, "IP"])]
  410. while_inst, = yield [("RD", [inst, "while"])]
  411. old_evalstack_link, old_phase_link, evalstack_roots = \
  412. yield [("RDE", [task_frame, "evalstack"]),
  413. ("RDE", [task_frame, "phase"]),
  414. ("RRD", [while_inst, self.taskname]),
  415. ]
  416. if len(evalstack_roots) == 1:
  417. evalstack_root = evalstack_roots[0]
  418. else:
  419. print("Got roots: " + str(evalstack_roots))
  420. raise Exception("Could not process continue statement!")
  421. prev_evalstack_roots, old_evalstack_phase_link = \
  422. yield [("RRD", [evalstack_root, "prev"]),
  423. ("RDE", [evalstack_root, "phase"]),
  424. ]
  425. if len(prev_evalstack_roots) == 1:
  426. prev_evalstack_root = prev_evalstack_roots[0]
  427. else:
  428. raise Exception("Could not process continue statement!")
  429. new_evalstack_root, new_phase_while, new_phase_inst, prev_evalstack_root_link = \
  430. yield [("CN", []),
  431. ("CNV", ["init"]),
  432. ("CNV", ["finish"]),
  433. ("RDE", [prev_evalstack_root, "prev"]),
  434. ]
  435. _, _, _, _, _, _, _, _ = \
  436. yield [("CD", [task_frame, "evalstack", new_evalstack_root]),
  437. ("CD", [new_evalstack_root, "prev", evalstack_root]),
  438. ("CD", [task_frame, "phase", new_phase_inst]),
  439. ("CD", [evalstack_root, "phase", new_phase_while]),
  440. ("DE", [old_evalstack_link]),
  441. ("DE", [prev_evalstack_root_link]),
  442. ("DE", [old_phase_link]),
  443. ("DE", [old_evalstack_phase_link]),
  444. ]
  445. def break_init(self, task_root):
  446. task_frame, = yield [("RD", [task_root, "frame"])]
  447. inst, = yield [("RD", [task_frame, "IP"])]
  448. while_inst, = yield [("RD", [inst, "while"])]
  449. old_evalstack_link, old_phase_link, evalstack_roots = \
  450. yield [("RDE", [task_frame, "evalstack"]),
  451. ("RDE", [task_frame, "phase"]),
  452. ("RRD", [while_inst, self.taskname]),
  453. ]
  454. if len(evalstack_roots) == 1:
  455. evalstack_root = evalstack_roots[0]
  456. else:
  457. raise Exception("Could not process break statement!")
  458. prev_evalstack_roots, old_evalstack_phase_link = \
  459. yield [("RRD", [evalstack_root, "prev"]),
  460. ("RDE", [evalstack_root, "phase"]),
  461. ]
  462. if len(prev_evalstack_roots) == 1:
  463. prev_evalstack_root = prev_evalstack_roots[0]
  464. else:
  465. raise Exception("Could not process break statement!")
  466. new_evalstack_root, new_phase_while, new_phase_inst, prev_evalstack_root_link = \
  467. yield [("CN", []),
  468. ("CNV", ["finish"]),
  469. ("CNV", ["finish"]),
  470. ("RDE", [prev_evalstack_root, "prev"]),
  471. ]
  472. _, _, _, _, _, _, _, _ = \
  473. yield [("CD", [task_frame, "evalstack", new_evalstack_root]),
  474. ("CD", [new_evalstack_root, "prev", evalstack_root]),
  475. ("CD", [task_frame, "phase", new_phase_inst]),
  476. ("CD", [evalstack_root, "phase", new_phase_while]),
  477. ("DE", [old_evalstack_link]),
  478. ("DE", [prev_evalstack_root_link]),
  479. ("DE", [old_phase_link]),
  480. ("DE", [old_evalstack_phase_link]),
  481. ]
  482. def if_init(self, task_root):
  483. task_frame, = yield [("RD", [task_root, "frame"])]
  484. evalstack, evalstack_link = \
  485. yield [("RD", [task_frame, "evalstack"]),
  486. ("RDE", [task_frame, "evalstack"]),
  487. ]
  488. inst, ip_link = yield [("RD", [task_frame, "IP"]),
  489. ("RDE", [task_frame, "IP"]),
  490. ]
  491. cond, = yield [("RD", [inst, "cond"])]
  492. new_evalstack, new_phase = \
  493. yield [("CN", []),
  494. ("CNV", ["cond"]),
  495. ]
  496. _, _, _, _, _, _, _ = \
  497. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  498. ("CD", [new_evalstack, "prev", evalstack]),
  499. ("CD", [task_frame, "IP", cond]),
  500. ("CD", [evalstack, "inst", inst]),
  501. ("CD", [evalstack, "phase", new_phase]),
  502. ("DE", [evalstack_link]),
  503. ("DE", [ip_link]),
  504. ]
  505. def if_cond(self, task_root):
  506. task_frame, = yield [("RD", [task_root, "frame"])]
  507. returnvalue, inst = yield [("RD", [task_frame, "returnvalue"]),
  508. ("RD", [task_frame, "IP"]),
  509. ]
  510. returnvalue_v, = yield [("RV", [returnvalue])]
  511. _else, = yield [("RD", [inst, "else"])]
  512. if returnvalue_v:
  513. phase_link, evalstack, evalstack_link, ip_link, _then, new_evalstack, evalstack_phase, new_phase = \
  514. yield [("RDE", [task_frame, "phase"]),
  515. ("RD", [task_frame, "evalstack"]),
  516. ("RDE", [task_frame, "evalstack"]),
  517. ("RDE", [task_frame, "IP"]),
  518. ("RD", [inst, "then"]),
  519. ("CN", []),
  520. ("CNV", ["finish"]),
  521. ("CNV", ["init"]),
  522. ]
  523. _, _, _, _, _, _, _, _, _ = \
  524. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  525. ("CD", [task_frame, "IP", _then]),
  526. ("CD", [new_evalstack, "prev", evalstack]),
  527. ("CD", [evalstack, "inst", inst]),
  528. ("CD", [evalstack, "phase", evalstack_phase]),
  529. ("CD", [task_frame, "phase", new_phase]),
  530. ("DE", [evalstack_link]),
  531. ("DE", [ip_link]),
  532. ("DE", [phase_link]),
  533. ]
  534. elif _else is None:
  535. phase_link, new_phase = \
  536. yield [("RDE", [task_frame, "phase"]),
  537. ("CNV", ["finish"]),
  538. ]
  539. _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  540. ("DE", [phase_link]),
  541. ]
  542. else:
  543. phase_link, evalstack, evalstack_link, ip_link = \
  544. yield [("RDE", [task_frame, "phase"]),
  545. ("RD", [task_frame, "evalstack"]),
  546. ("RDE", [task_frame, "evalstack"]),
  547. ("RDE", [task_frame, "IP"]),
  548. ]
  549. new_evalstack, new_phase, evalstack_phase = \
  550. yield [("CN", []),
  551. ("CNV", ["init"]),
  552. ("CNV", ["finish"]),
  553. ]
  554. _, _, _, _, _, _, _, _, _ = \
  555. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  556. ("CD", [task_frame, "IP", _else]),
  557. ("CD", [new_evalstack, "prev", evalstack]),
  558. ("CD", [evalstack, "inst", inst]),
  559. ("CD", [evalstack, "phase", evalstack_phase]),
  560. ("CD", [task_frame, "phase", new_phase]),
  561. ("DE", [evalstack_link]),
  562. ("DE", [ip_link]),
  563. ("DE", [phase_link]),
  564. ]
  565. def while_init(self, task_root):
  566. task_frame, = yield [("RD", [task_root, "frame"])]
  567. evalstack, evalstack_link, ip_link, inst = \
  568. yield [("RD", [task_frame, "evalstack"]),
  569. ("RDE", [task_frame, "evalstack"]),
  570. ("RDE", [task_frame, "IP"]),
  571. ("RD", [task_frame, "IP"]),
  572. ]
  573. cond, new_evalstack, new_phase = \
  574. yield [("RD", [inst, "cond"]),
  575. ("CN", []),
  576. ("CNV", ["cond"]),
  577. ]
  578. _, _, _, _, _, _, _ = \
  579. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  580. ("CD", [new_evalstack, "prev", evalstack]),
  581. ("CD", [task_frame, "IP", cond]),
  582. ("CD", [evalstack, "phase", new_phase]),
  583. ("CD", [evalstack, "inst", inst]),
  584. ("DE", [evalstack_link]),
  585. ("DE", [ip_link]),
  586. ]
  587. def while_cond(self, task_root):
  588. task_frame, = yield [("RD", [task_root, "frame"])]
  589. returnvalue, = yield [("RD", [task_frame, "returnvalue"])]
  590. returnvalue_v, = yield [("RV", [returnvalue])]
  591. if returnvalue_v:
  592. phase_link, evalstack, evalstack_link, ip_link, inst = \
  593. yield [("RDE", [task_frame, "phase"]),
  594. ("RD", [task_frame, "evalstack"]),
  595. ("RDE", [task_frame, "evalstack"]),
  596. ("RDE", [task_frame, "IP"]),
  597. ("RD", [task_frame, "IP"]),
  598. ]
  599. body, = yield [("RD", [inst, "body"])]
  600. new_evalstack, new_phase, evalstack_phase = \
  601. yield [("CN", []),
  602. ("CNV", ["init"]),
  603. ("CNV", ["init"]),
  604. ]
  605. _, _, _, _, _, _, _, _, _ = \
  606. yield [("CD", [task_frame, "IP", body]),
  607. ("CD", [task_frame, "phase", new_phase]),
  608. ("CD", [task_frame, "evalstack", new_evalstack]),
  609. ("CD", [new_evalstack, "prev", evalstack]),
  610. ("CD", [evalstack, "inst", inst]),
  611. ("CD", [evalstack, "phase", evalstack_phase]),
  612. ("DE", [evalstack_link]),
  613. ("DE", [ip_link]),
  614. ("DE", [phase_link]),
  615. ]
  616. # Check if we already have a taskname link to the evalstack
  617. links, = yield [("RD", [evalstack, self.taskname])]
  618. if links is None:
  619. yield [("CD", [evalstack, self.taskname, inst])]
  620. else:
  621. phase_link, new_phase = \
  622. yield [("RDE", [task_frame, "phase"]),
  623. ("CNV", ["finish"]),
  624. ]
  625. _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  626. ("DE", [phase_link])
  627. ]
  628. def access_init(self, task_root):
  629. task_frame, = yield [("RD", [task_root, "frame"])]
  630. evalstack, evalstack_link, inst, ip_link = \
  631. yield [("RD", [task_frame, "evalstack"]),
  632. ("RDE", [task_frame, "evalstack"]),
  633. ("RD", [task_frame, "IP"]),
  634. ("RDE", [task_frame, "IP"]),
  635. ]
  636. var, new_evalstack, new_phase = \
  637. yield [("RD", [inst, "var"]),
  638. ("CN", []),
  639. ("CNV", ["eval"]),
  640. ]
  641. _, _, _, _, _, _, _ = \
  642. yield [("CD", [task_frame, "IP", var]),
  643. ("CD", [task_frame, "evalstack", new_evalstack]),
  644. ("CD", [new_evalstack, "prev", evalstack]),
  645. ("CD", [evalstack, "inst", inst]),
  646. ("CD", [evalstack, "phase", new_phase]),
  647. ("DE", [evalstack_link]),
  648. ("DE", [ip_link]),
  649. ]
  650. def access_eval(self, task_root):
  651. task_frame, = yield [("RD", [task_root, "frame"])]
  652. phase_link, returnvalue_link, returnvalue = \
  653. yield [("RDE", [task_frame, "phase"]),
  654. ("RDE", [task_frame, "returnvalue"]),
  655. ("RD", [task_frame, "returnvalue"]),
  656. ]
  657. value, new_phase = yield [("RD", [returnvalue, "value"]),
  658. ("CNV", ["finish"]),
  659. ]
  660. _, _, _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  661. ("CD", [task_frame, "returnvalue", value]),
  662. ("DE", [phase_link]),
  663. ("DE", [returnvalue_link]),
  664. ]
  665. def resolve_init(self, task_root):
  666. task_frame, = yield [("RD", [task_root, "frame"])]
  667. symbols, evalstack, evalstack_link, ip_link, inst = \
  668. yield [("RD", [task_frame, "symbols"]),
  669. ("RD", [task_frame, "evalstack"]),
  670. ("RDE", [task_frame, "evalstack"]),
  671. ("RDE", [task_frame, "IP"]),
  672. ("RD", [task_frame, "IP"]),
  673. ]
  674. var, = yield [("RD", [inst, "var"])]
  675. variable, = yield [("RDN", [symbols, var])]
  676. if variable is None:
  677. phase_link, returnvalue_link, _globals, var_name = \
  678. yield [("RDE", [task_frame, "phase"]),
  679. ("RDE", [task_frame, "returnvalue"]),
  680. ("RD", [task_root, "globals"]),
  681. ("RV", [var]),
  682. ]
  683. variable, new_phase = \
  684. yield [("RD", [_globals, var_name]),
  685. ("CNV", ["finish"]),
  686. ]
  687. if variable is None:
  688. globs, = yield [("RDK", [_globals])]
  689. print("Globals: " + str(globs))
  690. globs = yield [("RV", [i]) for i in globs]
  691. print("Resolved globals: " + str(globs))
  692. raise Exception(jit_runtime.GLOBAL_NOT_FOUND_MESSAGE_FORMAT % var_name)
  693. # Resolved a global, so this is a string
  694. # Potentially, this might even be a function that we have precompiled already!
  695. # So check whether this is the case or not
  696. if self.allow_compiled:
  697. compiled_function = getattr(compiled_functions, var_name, None)
  698. if compiled_function is not None:
  699. # We have a compiled function ready!
  700. # Now we have to bind the ID to the compiled functions
  701. # For this, we read out the body of the resolved data
  702. compiler_val, = yield [("RD", [variable, "value"])]
  703. compiler_body, = yield [("RD", [compiler_val, "body"])]
  704. self.jit.register_compiled(compiler_body, compiled_function, var_name)
  705. # If we're dealing with a function, then we might want to figure out what its body id
  706. # is now so we can suggest a name to the JIT later.
  707. if self.suggest_function_names and self.jit.get_global_body_id(var_name) is None:
  708. compiler_val, = yield [("RD", [variable, "value"])]
  709. if compiler_val is not None:
  710. compiler_body, = yield [("RD", [compiler_val, "body"])]
  711. if compiler_body is not None:
  712. self.jit.register_global(compiler_body, var_name)
  713. else:
  714. phase_link, returnvalue_link, new_phase = \
  715. yield [("RDE", [task_frame, "phase"]),
  716. ("RDE", [task_frame, "returnvalue"]),
  717. ("CNV", ["finish"]),
  718. ]
  719. _, _, _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  720. ("CD", [task_frame, "returnvalue", variable]),
  721. ("DE", [phase_link]),
  722. ("DE", [returnvalue_link]),
  723. ]
  724. def assign_init(self, task_root):
  725. task_frame, = yield [("RD", [task_root, "frame"])]
  726. evalstack, evalstack_link, ip_link, inst = \
  727. yield [("RD", [task_frame, "evalstack"]),
  728. ("RDE", [task_frame, "evalstack"]),
  729. ("RDE", [task_frame, "IP"]),
  730. ("RD", [task_frame, "IP"]),
  731. ]
  732. var, new_evalstack, new_phase = \
  733. yield [("RD", [inst, "var"]),
  734. ("CN", []),
  735. ("CNV", ["value"]),
  736. ]
  737. _, _, _, _, _, _, _ = \
  738. yield [("CD", [task_frame, "IP", var]),
  739. ("CD", [task_frame, "evalstack", new_evalstack]),
  740. ("CD", [new_evalstack, "prev", evalstack]),
  741. ("CD", [evalstack, "inst", inst]),
  742. ("CD", [evalstack, "phase", new_phase]),
  743. ("DE", [evalstack_link]),
  744. ("DE", [ip_link]),
  745. ]
  746. def assign_value(self, task_root):
  747. task_frame, = yield [("RD", [task_root, "frame"])]
  748. phase_link, evalstack, returnvalue, evalstack_link, ip_link, inst = \
  749. yield [("RDE", [task_frame, "phase"]),
  750. ("RD", [task_frame, "evalstack"]),
  751. ("RD", [task_frame, "returnvalue"]),
  752. ("RDE", [task_frame, "evalstack"]),
  753. ("RDE", [task_frame, "IP"]),
  754. ("RD", [task_frame, "IP"]),
  755. ]
  756. value, new_evalstack, new_phase, evalstack_phase = \
  757. yield [("RD", [inst, "value"]),
  758. ("CN", []),
  759. ("CNV", ["init"]),
  760. ("CNV", ["assign"]),
  761. ]
  762. _, _, _, _, _, _, _, _, _, _ = \
  763. yield [("CD", [task_frame, "variable", returnvalue]),
  764. ("CD", [task_frame, "phase", new_phase]),
  765. ("CD", [task_frame, "evalstack", new_evalstack]),
  766. ("CD", [new_evalstack, "prev", evalstack]),
  767. ("CD", [evalstack, "inst", inst]),
  768. ("CD", [evalstack, "phase", evalstack_phase]),
  769. ("CD", [task_frame, "IP", value]),
  770. ("DE", [evalstack_link]),
  771. ("DE", [phase_link]),
  772. ("DE", [ip_link]),
  773. ]
  774. def assign_assign(self, task_root):
  775. task_frame, = yield [("RD", [task_root, "frame"])]
  776. phase_link, returnvalue, variable_link, variable = \
  777. yield [("RDE", [task_frame, "phase"]),
  778. ("RD", [task_frame, "returnvalue"]),
  779. ("RDE", [task_frame, "variable"]),
  780. ("RD", [task_frame, "variable"]),
  781. ]
  782. value_link, new_phase = \
  783. yield [("RDE", [variable, "value"]),
  784. ("CNV", ["finish"]),
  785. ]
  786. _, _, _, _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  787. ("CD", [variable, "value", returnvalue]),
  788. ("DE", [variable_link]),
  789. ("DE", [value_link]),
  790. ("DE", [phase_link]),
  791. ]
  792. def return_init(self, task_root):
  793. task_frame, = yield [("RD", [task_root, "frame"])]
  794. inst, = yield [("RD", [task_frame, "IP"])]
  795. value, = yield [("RD", [inst, "value"])]
  796. if value is None:
  797. prev_frame, = yield [("RD", [task_frame, "prev"])]
  798. # If the callee's frame is marked with the '__exception_return' key, then
  799. # we need to throw an exception instead of just finishing here. This design
  800. # gives us O(1) state reads per jit-interpreter transition.
  801. exception_return, = yield [("RD", [task_frame, primitive_functions.EXCEPTION_RETURN_KEY])]
  802. if prev_frame is None:
  803. _, = yield [("DN", [task_root])]
  804. del self.debug_info[self.taskname]
  805. #print("Cleanup task " + str(self.taskname))
  806. else:
  807. if self.debug_info[self.taskname]:
  808. self.debug_info[self.taskname].pop()
  809. _, _ = yield [("CD", [task_root, "frame", prev_frame]),
  810. ("DN", [task_frame]),
  811. ]
  812. if exception_return is not None:
  813. raise primitive_functions.InterpretedFunctionFinished(None)
  814. else:
  815. evalstack, evalstack_link, ip_link, new_evalstack, evalstack_phase = \
  816. yield [("RD", [task_frame, "evalstack"]),
  817. ("RDE", [task_frame, "evalstack"]),
  818. ("RDE", [task_frame, "IP"]),
  819. ("CN", []),
  820. ("CNV", ["eval"]),
  821. ]
  822. _, _, _, _, _, _, _ = \
  823. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  824. ("CD", [new_evalstack, "prev", evalstack]),
  825. ("CD", [evalstack, "inst", inst]),
  826. ("CD", [evalstack, "phase", evalstack_phase]),
  827. ("CD", [task_frame, "IP", value]),
  828. ("DE", [evalstack_link]),
  829. ("DE", [ip_link]),
  830. ]
  831. def return_eval(self, task_root):
  832. if self.debug_info[self.taskname]:
  833. self.debug_info[self.taskname].pop()
  834. task_frame, = yield [("RD", [task_root, "frame"])]
  835. prev_frame, = yield [("RD", [task_frame, "prev"])]
  836. if prev_frame is None:
  837. _, = yield [("DN", [task_root])]
  838. del self.debug_info[self.taskname]
  839. exception_return, returnvalue = yield [
  840. ("RD", [task_frame, primitive_functions.EXCEPTION_RETURN_KEY]),
  841. ("RD", [task_frame, "returnvalue"])]
  842. # If the callee's frame is marked with the '__exception_return' key, then
  843. # we need to throw an exception instead of just finishing here. This design
  844. # gives us O(1) state reads per jit-interpreter transition.
  845. if exception_return is not None:
  846. yield [
  847. ("CD", [task_root, "frame", prev_frame]),
  848. ("DN", [task_frame])]
  849. raise primitive_functions.InterpretedFunctionFinished(returnvalue)
  850. else:
  851. old_returnvalue_link, = yield [("RDE", [prev_frame, "returnvalue"])]
  852. yield [
  853. ("CD", [task_root, "frame", prev_frame]),
  854. ("CD", [prev_frame, "returnvalue", returnvalue]),
  855. ("DE", [old_returnvalue_link]),
  856. ("DN", [task_frame])]
  857. def constant_init(self, task_root):
  858. task_frame, = yield [("RD", [task_root, "frame"])]
  859. phase_link, returnvalue_link, inst = \
  860. yield [("RDE", [task_frame, "phase"]),
  861. ("RDE", [task_frame, "returnvalue"]),
  862. ("RD", [task_frame, "IP"]),
  863. ]
  864. node, new_phase = yield [("RD", [inst, "node"]),
  865. ("CNV", ["finish"]),
  866. ]
  867. _, _, _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  868. ("CD", [task_frame, "returnvalue", node]),
  869. ("DE", [returnvalue_link]),
  870. ("DE", [phase_link]),
  871. ]
  872. def helper_init(self, task_root):
  873. task_frame, = yield [("RD", [task_root, "frame"])]
  874. inst, = yield [("RD", [task_frame, "IP"])]
  875. next, = yield [("RD", [inst, "next"])]
  876. if next is None:
  877. ip_link, phase_link, evalstack_top = \
  878. yield [("RDE", [task_frame, "IP"]),
  879. ("RDE", [task_frame, "phase"]),
  880. ("RD", [task_frame, "evalstack"]),
  881. ]
  882. evalstack, = yield [("RD", [evalstack_top, "prev"])]
  883. evalstack_inst, evalstack_phase, evalstack_inst_link, evalstack_phase_link = \
  884. yield [("RD", [evalstack, "inst"]),
  885. ("RD", [evalstack, "phase"]),
  886. ("RDE", [evalstack, "inst"]),
  887. ("RDE", [evalstack, "phase"]),
  888. ]
  889. _, _, _, _, _, _, _, _ = \
  890. yield [("CD", [task_frame, "evalstack", evalstack]),
  891. ("CD", [task_frame, "IP", evalstack_inst]),
  892. ("CD", [task_frame, "phase", evalstack_phase]),
  893. ("DE", [ip_link]),
  894. ("DE", [phase_link]),
  895. ("DE", [evalstack_inst_link]),
  896. ("DE", [evalstack_phase_link]),
  897. ("DN", [evalstack_top]),
  898. ]
  899. else:
  900. ip_link, phase_link, new_phase = \
  901. yield [("RDE", [task_frame, "IP"]),
  902. ("RDE", [task_frame, "phase"]),
  903. ("CNV", ["init"]),
  904. ]
  905. _, _, _, _ = yield [("CD", [task_frame, "IP", next]),
  906. ("CD", [task_frame, "phase", new_phase]),
  907. ("DE", [ip_link]),
  908. ("DE", [phase_link]),
  909. ]
  910. def call_init(self, task_root):
  911. task_frame, = yield [("RD", [task_root, "frame"])]
  912. symbols, evalstack, evalstack_link, ip_link, inst = \
  913. yield [("RD", [task_frame, "symbols"]),
  914. ("RD", [task_frame, "evalstack"]),
  915. ("RDE", [task_frame, "evalstack"]),
  916. ("RDE", [task_frame, "IP"]),
  917. ("RD", [task_frame, "IP"]),
  918. ]
  919. func, params = yield [("RD", [inst, "func"]),
  920. ("RD", [inst, "params"]),
  921. ]
  922. if params is None:
  923. new_evalstack, evalstack_phase = \
  924. yield [("CN", []),
  925. ("CNV", ["call"]),
  926. ]
  927. _, _, _, _, _, _, _ = \
  928. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  929. ("CD", [new_evalstack, "prev", evalstack]),
  930. ("CD", [evalstack, "inst", inst]),
  931. ("CD", [evalstack, "phase", evalstack_phase]),
  932. ("CD", [task_frame, "IP", func]),
  933. ("DE", [evalstack_link]),
  934. ("DE", [ip_link]),
  935. ]
  936. else:
  937. new_evalstack,= yield [("CN", [])]
  938. _, _, _, _, _, _, _ = \
  939. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  940. ("CD", [new_evalstack, "prev", evalstack]),
  941. ("CD", [evalstack, "inst", inst]),
  942. ("CD", [evalstack, "phase", params]),
  943. ("CD", [task_frame, "IP", func]),
  944. ("DE", [evalstack_link]),
  945. ("DE", [ip_link]),
  946. ]
  947. def call_call(self, task_root):
  948. self.debug_info[self.taskname].append("None")
  949. task_frame, = yield [("RD", [task_root, "frame"])]
  950. inst, = yield [("RD", [task_frame, "IP"])]
  951. param, = yield [("RD", [inst, "last_param"])]
  952. if param is None:
  953. returnvalue, = yield [("RD", [task_frame, "returnvalue"])]
  954. body, = yield [("RD", [returnvalue, "body"])]
  955. self.jit.mark_entry_point(body)
  956. phase_link, frame_link, prev_phase, new_phase, new_frame, new_evalstack, new_symbols, new_returnvalue = \
  957. yield [("RDE", [task_frame, "phase"]),
  958. ("RDE", [task_root, "frame"]),
  959. ("CNV", ["finish"]),
  960. ("CNV", ["init"]),
  961. ("CN", []),
  962. ("CN", []),
  963. ("CN", []),
  964. ("CN", []),
  965. ]
  966. _, _, _, _, _, _, _, _, _, _, _ = \
  967. yield [("CD", [task_root, "frame", new_frame]),
  968. ("CD", [new_frame, "evalstack", new_evalstack]),
  969. ("CD", [new_frame, "symbols", new_symbols]),
  970. ("CD", [new_frame, "returnvalue", new_returnvalue]),
  971. ("CD", [new_frame, "caller", inst]),
  972. ("CD", [new_frame, "phase", new_phase]),
  973. ("CD", [new_frame, "IP", body]),
  974. ("CD", [new_frame, "prev", task_frame]),
  975. ("CD", [task_frame, "phase", prev_phase]),
  976. ("DE", [phase_link]),
  977. ("DE", [frame_link]),
  978. ]
  979. else:
  980. newer_frames, invoking_frames = \
  981. yield [("RRD", [task_frame, "prev"]),
  982. ("RRD", [inst, "caller"]),
  983. ]
  984. new_frame = self.find_overlapping(newer_frames, invoking_frames)
  985. phase_link, frame_link, new_symbols, new_IP = \
  986. yield [("RDE", [task_frame, "phase"]),
  987. ("RDE", [task_root, "frame"]),
  988. ("RD", [new_frame, "symbols"]),
  989. ("RD", [new_frame, "IP"]),
  990. ]
  991. signature, = yield [("RRD", [new_IP, "body"])]
  992. signature = signature[0]
  993. sig_params, last_param = \
  994. yield [("RD", [signature, "params"]),
  995. ("RD", [inst, "last_param"]),
  996. ]
  997. self.jit.mark_entry_point(new_IP)
  998. name, = yield [("RD", [last_param, "name"])]
  999. name_value, = yield [("RV", [name])]
  1000. returnvalue, formal_parameter, new_phase, variable = \
  1001. yield [("RD", [task_frame, "returnvalue"]),
  1002. ("RD", [sig_params, name_value]),
  1003. ("CNV", ["finish"]),
  1004. ("CN", []),
  1005. ]
  1006. _, _, _, t1 = yield [("CD", [task_root, "frame", new_frame]),
  1007. ("CD", [task_frame, "phase", new_phase]),
  1008. ("CD", [variable, "value", returnvalue]),
  1009. ("CE", [new_symbols, variable]),
  1010. ]
  1011. _, _, _ = yield [("CE", [t1, formal_parameter]),
  1012. ("DE", [frame_link]),
  1013. ("DE", [phase_link]),
  1014. ]
  1015. def find_overlapping(self, a, b):
  1016. newer_frames = set(a)
  1017. invoking_frames = set(b)
  1018. matches = list(newer_frames.intersection(invoking_frames))
  1019. if len(matches) == 1:
  1020. return matches[0]
  1021. elif len(matches) > 1:
  1022. raise Exception("Error: multiple overlapping elements")
  1023. else:
  1024. raise Exception("Error: could not find any overlap")
  1025. def call_param(self, task_root):
  1026. task_frame, = yield [("RD", [task_root, "frame"])]
  1027. inst, phase = yield [("RD", [task_frame, "IP"]),
  1028. ("RD", [task_frame, "phase"]),
  1029. ]
  1030. params, last_param = \
  1031. yield [("RD", [inst, "params"]),
  1032. ("RD", [inst, "last_param"]),
  1033. ]
  1034. next_param, = yield [("RD", [params, "next_param"])]
  1035. if params == phase:
  1036. phase_link, ip_link, returnvalue, param_value, evalstack, evalstack_link = \
  1037. yield [("RDE", [task_frame, "phase"]),
  1038. ("RDE", [task_frame, "IP"]),
  1039. ("RD", [task_frame, "returnvalue"]),
  1040. ("RD", [params, "value"]),
  1041. ("RD", [task_frame, "evalstack"]),
  1042. ("RDE", [task_frame, "evalstack"]),
  1043. ]
  1044. body, = yield [("RD", [returnvalue, "body"])]
  1045. new_frame, prev_evalstack, new_phase, prev_phase, new_evalstack, new_symbols, new_returnvalue = \
  1046. yield [("CN", []),
  1047. ("CN", []),
  1048. ("CNV", ["init"]),
  1049. ("CNV", ["init"]),
  1050. ("CN", []),
  1051. ("CN", []),
  1052. ("CN", []),
  1053. ]
  1054. _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = \
  1055. yield [("CD", [new_frame, "evalstack", new_evalstack]),
  1056. ("CD", [new_frame, "symbols", new_symbols]),
  1057. ("CD", [new_frame, "returnvalue", new_returnvalue]),
  1058. ("CD", [new_frame, "caller", inst]),
  1059. ("CD", [new_frame, "phase", new_phase]),
  1060. ("CD", [new_frame, "IP", body]),
  1061. ("CD", [new_frame, "prev", task_frame]),
  1062. ("CD", [task_frame, "phase", prev_phase]),
  1063. ("CD", [task_frame, "IP", param_value]),
  1064. ("CD", [prev_evalstack, "prev", evalstack]),
  1065. ("CD", [evalstack, "inst", inst]),
  1066. ("CD", [task_frame, "evalstack", prev_evalstack]),
  1067. ("DE", [evalstack_link]),
  1068. ("DE", [ip_link]),
  1069. ("DE", [phase_link]),
  1070. ]
  1071. if next_param is not None:
  1072. _ = yield [("CD", [evalstack, "phase", next_param])]
  1073. else:
  1074. evalstack_phase, = \
  1075. yield [("CNV", ["call"])]
  1076. _ = yield [("CD", [evalstack, "phase", evalstack_phase])]
  1077. else:
  1078. frame_link, phase_link, newer_frames, invoking_frames = \
  1079. yield [("RDE", [task_root, "frame"]),
  1080. ("RDE", [task_frame, "phase"]),
  1081. ("RRD", [task_frame, "prev"]),
  1082. ("RRD", [inst, "caller"]),
  1083. ]
  1084. new_frame = self.find_overlapping(newer_frames, invoking_frames)
  1085. ip_link, evalstack, evalstack_link, new_symbols, new_IP = \
  1086. yield [("RDE", [task_frame, "IP"]),
  1087. ("RD", [task_frame, "evalstack"]),
  1088. ("RDE", [task_frame, "evalstack"]),
  1089. ("RD", [new_frame, "symbols"]),
  1090. ("RD", [new_frame, "IP"]),
  1091. ]
  1092. signature, = yield [("RRD", [new_IP, "body"])]
  1093. signature = signature[0]
  1094. sig_params, = yield [("RD", [signature, "params"])]
  1095. if last_param == phase:
  1096. prev_param, = \
  1097. yield [("RRD", [last_param, "next_param"])]
  1098. prev_param = prev_param[0]
  1099. name, = yield [("RD", [prev_param, "name"])]
  1100. name_value, = \
  1101. yield [("RV", [name])]
  1102. evalstack_phase, = \
  1103. yield [("CNV", ["call"])]
  1104. _ = yield [("CD", [evalstack, "phase", evalstack_phase])]
  1105. formal_parameter, param_value = \
  1106. yield [("RD", [sig_params, name_value]),
  1107. ("RD", [last_param, "value"]),
  1108. ]
  1109. else:
  1110. param_b, = yield [("RD", [task_frame, "phase"])]
  1111. param_c, param_a = \
  1112. yield [("RD", [param_b, "next_param"]),
  1113. ("RRD", [param_b, "next_param"]),
  1114. ]
  1115. param_a = param_a[0]
  1116. name, param_value = \
  1117. yield [("RD", [param_a, "name"]),
  1118. ("RD", [param_b, "value"]),
  1119. ]
  1120. name_value, = \
  1121. yield [("RV", [name])]
  1122. formal_parameter, _ = \
  1123. yield [("RD", [sig_params, name_value]),
  1124. ("CD", [evalstack, "phase", param_c]),
  1125. ]
  1126. new_phase, new_evalstack, variable, returnvalue = \
  1127. yield [("CNV", ["init"]),
  1128. ("CN", []),
  1129. ("CN", []),
  1130. ("RD", [task_frame, "returnvalue"]),
  1131. ]
  1132. _, _, _, _, _, _ = \
  1133. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  1134. ("CD", [new_evalstack, "prev", evalstack]),
  1135. ("CD", [evalstack, "inst", inst]),
  1136. ("CD", [task_frame, "phase", new_phase]),
  1137. ("CD", [task_frame, "IP", param_value]),
  1138. ("CD", [variable, "value", returnvalue]),
  1139. ]
  1140. t1, = yield [("CE", [new_symbols, variable])]
  1141. _, _, _, _ = \
  1142. yield [("CE", [t1, formal_parameter]),
  1143. ("DE", [phase_link]),
  1144. ("DE", [ip_link]),
  1145. ("DE", [evalstack_link]),
  1146. ]
  1147. def input_init(self, task_root):
  1148. task_frame, = yield [("RD", [task_root, "frame"])]
  1149. returnvalue_link, _input = \
  1150. yield [("RDE", [task_frame, "returnvalue"]),
  1151. ("RD", [task_root, "input"]),
  1152. ]
  1153. value, next, phase_link = \
  1154. yield [("RD", [_input, "value"]),
  1155. ("RD", [_input, "next"]),
  1156. ("RDE", [task_frame, "phase"]),
  1157. ]
  1158. if value is not None:
  1159. v = yield [("RV", [value])]
  1160. _, _, finish = \
  1161. yield [("CD", [task_frame, "returnvalue", value]),
  1162. ("CD", [task_root, "input", next]),
  1163. ("CNV", ["finish"]),
  1164. ]
  1165. _, _, _, _ = \
  1166. yield [("CD", [task_frame, "phase", finish]),
  1167. ("DN", [_input]),
  1168. ("DE", [returnvalue_link]),
  1169. ("DE", [phase_link]),
  1170. ]
  1171. self.input_value = value
  1172. else:
  1173. # No input yet, so just wait and don't advance IP or phase
  1174. self.input_value = None
  1175. ex = primitive_functions.SleepKernel(0.1, True)
  1176. raise ex
  1177. def output_init(self, task_root):
  1178. task_frame, = yield [("RD", [task_root, "frame"])]
  1179. evalstack, evalstack_link, ip_link, inst = \
  1180. yield [("RD", [task_frame, "evalstack"]),
  1181. ("RDE", [task_frame, "evalstack"]),
  1182. ("RDE", [task_frame, "IP"]),
  1183. ("RD", [task_frame, "IP"]),
  1184. ]
  1185. value, new_evalstack, evalstack_phase = \
  1186. yield [("RD", [inst, "value"]),
  1187. ("CN", []),
  1188. ("CNV", ["output"]),
  1189. ]
  1190. _, _, _, _, _, _, _ = \
  1191. yield [("CD", [task_frame, "evalstack", new_evalstack]),
  1192. ("CD", [new_evalstack, "prev", evalstack]),
  1193. ("CD", [evalstack, "inst", inst]),
  1194. ("CD", [evalstack, "phase", evalstack_phase]),
  1195. ("CD", [task_frame, "IP", value]),
  1196. ("DE", [evalstack_link]),
  1197. ("DE", [ip_link]),
  1198. ]
  1199. def output_output(self, task_root):
  1200. task_frame, = yield [("RD", [task_root, "frame"])]
  1201. returnvalue_link, returnvalue, last_output, phase_link, last_output_link, new_last_output, finish = \
  1202. yield [("RDE", [task_frame, "returnvalue"]),
  1203. ("RD", [task_frame, "returnvalue"]),
  1204. ("RD", [task_root, "last_output"]),
  1205. ("RDE", [task_frame, "phase"]),
  1206. ("RDE", [task_root, "last_output"]),
  1207. ("CN", []),
  1208. ("CNV", ["finish"]),
  1209. ]
  1210. _, _, _, _, _, _ = \
  1211. yield [("CD", [last_output, "value", returnvalue]),
  1212. ("CD", [last_output, "next", new_last_output]),
  1213. ("CD", [task_root, "last_output", new_last_output]),
  1214. ("CD", [task_frame, "phase", finish]),
  1215. ("DE", [last_output_link]),
  1216. ("DE", [phase_link]),
  1217. ]
  1218. def declare_init(self, task_root):
  1219. task_frame, = yield [("RD", [task_root, "frame"])]
  1220. inst, = yield [("RD", [task_frame, "IP"])]
  1221. new_var, symbols, phase_link, empty_node, new_phase = \
  1222. yield [("RD", [inst, "var"]),
  1223. ("RD", [task_frame, "symbols"]),
  1224. ("RDE", [task_frame, "phase"]),
  1225. ("CN", []),
  1226. ("CNV", ["finish"]),
  1227. ]
  1228. exists, = yield [("RDN", [symbols, new_var])]
  1229. if exists is None:
  1230. new_edge, = yield [("CE", [symbols, empty_node])]
  1231. _ = yield [("CE", [new_edge, new_var])]
  1232. _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  1233. ("DE", [phase_link]),
  1234. ]
  1235. def global_init(self, task_root):
  1236. task_frame, = yield [("RD", [task_root, "frame"])]
  1237. inst, = yield [("RD", [task_frame, "IP"])]
  1238. new_var, global_symbols, phase_link, empty_node, new_phase = \
  1239. yield [("RD", [inst, "var"]),
  1240. ("RD", [task_root, "globals"]),
  1241. ("RDE", [task_frame, "phase"]),
  1242. ("CN", []),
  1243. ("CNV", ["finish"]),
  1244. ]
  1245. value, = yield [("RV", [new_var])]
  1246. exists, = yield [("RDE", [global_symbols, value])]
  1247. if exists is not None:
  1248. yield [("DE", [exists])]
  1249. yield [("CD", [global_symbols, value, empty_node])]
  1250. _, _ = yield [("CD", [task_frame, "phase", new_phase]),
  1251. ("DE", [phase_link])
  1252. ]