main.py 69 KB

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