main.py 72 KB

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