main.py 67 KB

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