main.py 72 KB

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