intrinsics.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. import time
  2. import modelverse_jit.jit as jit
  3. import modelverse_jit.tree_ir as tree_ir
  4. import modelverse_jit.cfg_ir as cfg_ir
  5. import modelverse_jit.runtime as jit_runtime
  6. BINARY_INTRINSICS = {
  7. 'value_eq' : '==',
  8. 'value_neq' : '!=',
  9. 'bool_and' : 'and',
  10. 'bool_or' : 'or',
  11. 'integer_addition' : '+',
  12. 'integer_subtraction' : '-',
  13. 'integer_multiplication' : '*',
  14. 'integer_division' : '/',
  15. 'integer_gt' : '>',
  16. 'integer_gte' : '>=',
  17. 'integer_lt' : '<',
  18. 'integer_lte' : '<=',
  19. 'float_addition' : '+',
  20. 'float_subtraction' : '-',
  21. 'float_multiplication' : '*',
  22. 'float_division' : '/',
  23. 'float_gt' : '>',
  24. 'float_gte' : '>=',
  25. 'float_lt' : '<',
  26. 'float_lte' : '<='
  27. }
  28. UNARY_INTRINSICS = {
  29. 'bool_not' : 'not',
  30. 'integer_neg' : '-',
  31. 'float_neg' : '-'
  32. }
  33. CAST_INTRINSICS = {
  34. 'cast_i2f' : float,
  35. 'cast_i2s' : str,
  36. 'cast_i2b' : bool,
  37. 'cast_f2i' : int,
  38. 'cast_f2s' : str,
  39. 'cast_f2b' : bool,
  40. 'cast_s2i' : int,
  41. 'cast_s2f' : float,
  42. 'cast_s2b' : bool,
  43. 'cast_b2i' : int,
  44. 'cast_b2f' : float,
  45. 'cast_b2s' : str
  46. }
  47. def create_get_length(expression):
  48. """Creates an expression that evaluates the given expression, and then
  49. computes the length of its result."""
  50. return tree_ir.CallInstruction(
  51. tree_ir.LoadGlobalInstruction('len'),
  52. [expression])
  53. # Don't compain about the variable names, pylint. It's important that we
  54. # get them right.
  55. # pylint: disable=I0011,C0103
  56. def __set_add(a, b):
  57. store_a, load_a = tree_ir.evaluate_and_load(a)
  58. return tree_ir.create_block(
  59. store_a,
  60. tree_ir.CreateEdgeInstruction(load_a, b),
  61. load_a)
  62. def __dict_add(a, b, c):
  63. store_a, load_a = tree_ir.evaluate_and_load(a)
  64. store_b, load_b = tree_ir.evaluate_and_load(b)
  65. return tree_ir.create_block(
  66. store_a,
  67. store_b,
  68. tree_ir.CreateEdgeInstruction(
  69. tree_ir.CreateEdgeInstruction(load_a, c),
  70. load_b),
  71. load_a)
  72. def __dict_add_fast(a, b, c):
  73. # TODO This might be possible to optimize slightly?
  74. store_a, load_a = tree_ir.evaluate_and_load(a)
  75. b_val = tree_ir.StoreLocalInstruction(
  76. None,
  77. tree_ir.ReadValueInstruction(b))
  78. store_c, load_c = tree_ir.evaluate_and_load(c)
  79. return tree_ir.create_block(
  80. store_a,
  81. b_val,
  82. store_c,
  83. tree_ir.CreateDictionaryEdgeInstruction(
  84. load_a,
  85. b_val.create_load(),
  86. load_c),
  87. load_a)
  88. def __list_read(a, b):
  89. # The statements in this function generate the following code:
  90. #
  91. # a_tmp = a # To make sure a is evaluated before b.
  92. # b_value, = yield [("RV", [b])]
  93. # result, = yield [("RD", [a_tmp, b_value])]
  94. # if result is None:
  95. # raise Exception("List read out of bounds: %s" % b_value)
  96. # result
  97. store_a, load_a = tree_ir.evaluate_and_load(a)
  98. b_val = tree_ir.StoreLocalInstruction(
  99. None,
  100. tree_ir.ReadValueInstruction(b))
  101. result = tree_ir.StoreLocalInstruction(
  102. None,
  103. tree_ir.ReadDictionaryValueInstruction(
  104. load_a.create_load(), b_val.create_load()))
  105. return tree_ir.create_block(
  106. store_a,
  107. b_val,
  108. result,
  109. tree_ir.SelectInstruction(
  110. tree_ir.BinaryInstruction(
  111. result.create_load(),
  112. 'is',
  113. tree_ir.LiteralInstruction(None)),
  114. tree_ir.RaiseInstruction(
  115. tree_ir.CallInstruction(
  116. tree_ir.LoadGlobalInstruction('Exception'),
  117. [tree_ir.BinaryInstruction(
  118. tree_ir.LiteralInstruction('List read out of bounds: %s'),
  119. '%',
  120. b_val.create_load())])),
  121. tree_ir.EmptyInstruction()),
  122. result.create_load())
  123. def __list_append(a, b):
  124. # We want to generate code that is more or less equivalent to:
  125. #
  126. # a_tmp = a
  127. # b_tmp = b
  128. # a_outgoing, = yield [("RO", [a_tmp])]
  129. # _ = yield [("CD", [a_tmp, len(a_outgoing), b_tmp])]
  130. # a
  131. store_a, load_a = tree_ir.evaluate_and_load(a)
  132. store_b, load_b = tree_ir.evaluate_and_load(b)
  133. return tree_ir.create_block(
  134. store_a,
  135. store_b,
  136. tree_ir.CreateDictionaryEdgeInstruction(
  137. load_a,
  138. create_get_length(
  139. tree_ir.ReadOutgoingEdgesInstruction(
  140. load_a)),
  141. load_b),
  142. load_a)
  143. def __log(a):
  144. # Original definition:
  145. #
  146. # def log(a, **remainder):
  147. # a_value, = yield [("RV", [a])]
  148. # print("== LOG == " + str(a_value))
  149. # raise PrimitiveFinished(a)
  150. store_a, load_a = tree_ir.evaluate_and_load(a)
  151. return tree_ir.CompoundInstruction(
  152. tree_ir.create_block(
  153. store_a,
  154. tree_ir.PrintInstruction(
  155. tree_ir.BinaryInstruction(
  156. tree_ir.LiteralInstruction("== LOG == "),
  157. '+',
  158. tree_ir.CallInstruction(
  159. tree_ir.LoadGlobalInstruction('str'),
  160. [tree_ir.ReadValueInstruction(load_a)])))),
  161. load_a)
  162. def __read_nr_out(a):
  163. # Original definition:
  164. #
  165. # def read_nr_out(a, **remainder):
  166. # outgoing, = yield [("RO", [a])]
  167. # result, = yield [("CNV", [len(outgoing)])]
  168. # raise PrimitiveFinished(result)
  169. return tree_ir.CreateNodeWithValueInstruction(
  170. create_get_length(tree_ir.ReadOutgoingEdgesInstruction(a)))
  171. MISC_INTRINSICS = {
  172. # Reference equality
  173. 'element_eq' :
  174. lambda a, b:
  175. tree_ir.CreateNodeWithValueInstruction(
  176. tree_ir.BinaryInstruction(a, '==', b)),
  177. 'element_neq' :
  178. lambda a, b:
  179. tree_ir.CreateNodeWithValueInstruction(
  180. tree_ir.BinaryInstruction(a, '!=', b)),
  181. # Strings
  182. 'string_get' :
  183. lambda a, b:
  184. tree_ir.CreateNodeWithValueInstruction(
  185. tree_ir.LoadIndexInstruction(
  186. tree_ir.ReadValueInstruction(a),
  187. tree_ir.ReadValueInstruction(b))),
  188. 'string_len' :
  189. lambda a:
  190. tree_ir.CreateNodeWithValueInstruction(
  191. tree_ir.CallInstruction(
  192. tree_ir.LoadGlobalInstruction('len'),
  193. [tree_ir.ReadValueInstruction(a)])),
  194. 'string_join' :
  195. lambda a, b:
  196. tree_ir.CreateNodeWithValueInstruction(
  197. tree_ir.BinaryInstruction(
  198. tree_ir.CallInstruction(
  199. tree_ir.LoadGlobalInstruction('str'),
  200. [tree_ir.ReadValueInstruction(a)]),
  201. '+',
  202. tree_ir.CallInstruction(
  203. tree_ir.LoadGlobalInstruction('str'),
  204. [tree_ir.ReadValueInstruction(b)]))),
  205. 'string_startswith' :
  206. lambda a, b:
  207. tree_ir.CreateNodeWithValueInstruction(
  208. tree_ir.CallInstruction(
  209. tree_ir.LoadMemberInstruction(
  210. tree_ir.ReadValueInstruction(a),
  211. 'startswith'),
  212. [tree_ir.ReadValueInstruction(b)])),
  213. # State creation
  214. 'create_node' : tree_ir.CreateNodeInstruction,
  215. 'create_edge' :
  216. # Lambda is totally necessary here, pylint.
  217. # You totally dropped the ball on this one.
  218. # pylint: disable=I0011,W0108
  219. lambda a, b:
  220. tree_ir.CreateEdgeInstruction(a, b),
  221. 'create_value' :
  222. lambda a:
  223. tree_ir.CreateNodeWithValueInstruction(
  224. tree_ir.ReadValueInstruction(a)),
  225. # State reads
  226. 'read_edge_src' :
  227. lambda a:
  228. tree_ir.LoadIndexInstruction(
  229. tree_ir.ReadEdgeInstruction(a),
  230. tree_ir.LiteralInstruction(0)),
  231. 'read_edge_dst' :
  232. lambda a:
  233. tree_ir.LoadIndexInstruction(
  234. tree_ir.ReadEdgeInstruction(a),
  235. tree_ir.LiteralInstruction(1)),
  236. 'is_edge' :
  237. lambda a:
  238. tree_ir.CreateNodeWithValueInstruction(
  239. tree_ir.BinaryInstruction(
  240. tree_ir.LoadIndexInstruction(
  241. tree_ir.ReadEdgeInstruction(a),
  242. tree_ir.LiteralInstruction(0)),
  243. 'is not',
  244. tree_ir.LiteralInstruction(None))),
  245. 'read_nr_out' : __read_nr_out,
  246. # read_root
  247. 'read_root' :
  248. lambda:
  249. tree_ir.LoadIndexInstruction(
  250. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  251. tree_ir.LiteralInstruction('root')),
  252. # read_taskroot
  253. 'read_taskroot' :
  254. lambda:
  255. tree_ir.LoadIndexInstruction(
  256. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  257. tree_ir.LiteralInstruction('task_root')),
  258. # Dictionary operations
  259. 'dict_read' :
  260. lambda a, b:
  261. tree_ir.ReadDictionaryValueInstruction(
  262. a, tree_ir.ReadValueInstruction(b)),
  263. 'dict_read_edge' :
  264. lambda a, b:
  265. tree_ir.ReadDictionaryEdgeInstruction(
  266. a, tree_ir.ReadValueInstruction(b)),
  267. 'dict_add' : __dict_add,
  268. 'dict_add_fast' : __dict_add_fast,
  269. 'dict_len' : __read_nr_out,
  270. # Set operations
  271. 'set_add' : __set_add,
  272. # List operations
  273. 'list_len' : __read_nr_out,
  274. 'list_read' : __list_read,
  275. 'list_append' : __list_append,
  276. # log
  277. 'log' : __log
  278. }
  279. def __read_nr_out_cfg(original_def, a):
  280. # Original definition:
  281. #
  282. # def read_nr_out(a, **remainder):
  283. # outgoing, = yield [("RO", [a])]
  284. # result, = yield [("CNV", [len(outgoing)])]
  285. # raise PrimitiveFinished(result)
  286. original_def.redefine(
  287. cfg_ir.CreateNode(
  288. original_def.insert_before(
  289. cfg_ir.create_pure_simple_call(
  290. 'len',
  291. original_def.insert_before(
  292. cfg_ir.create_read_outgoing_edges(a))))))
  293. def __dict_in_cfg(original_def, a, b):
  294. # Original definition:
  295. #
  296. # def dict_in(a, b, **remainder):
  297. # b_value, = yield [("RV", [b])]
  298. # value, = yield [("RD", [a, b_value])]
  299. # is_in = value is not None
  300. # result, = yield [("CNV", [is_in])]
  301. # raise PrimitiveFinished(result)
  302. original_def.redefine(
  303. cfg_ir.CreateNode(
  304. original_def.insert_before(
  305. cfg_ir.Binary(
  306. original_def.insert_before(
  307. cfg_ir.create_read_dict_value(
  308. a, original_def.insert_before(cfg_ir.Read(b)))),
  309. 'is not',
  310. original_def.insert_before(cfg_ir.Literal(None))))))
  311. def __dict_in_node_cfg(original_def, a, b):
  312. # Original definition:
  313. #
  314. # def dict_in_node(a, b, **remainder):
  315. # value, = yield [("RDN", [a, b])]
  316. # result, = yield [("CNV", [value is not None])]
  317. # raise PrimitiveFinished(result)
  318. original_def.redefine(
  319. cfg_ir.CreateNode(
  320. original_def.insert_before(
  321. cfg_ir.Binary(
  322. original_def.insert_before(cfg_ir.create_read_dict_node(a, b)),
  323. 'is not',
  324. original_def.insert_before(cfg_ir.Literal(None))))))
  325. def __dict_read_cfg(original_def, a, b):
  326. # Original definition:
  327. #
  328. # def dict_read(a, b, **remainder):
  329. # b_value, = yield [("RV", [b])]
  330. # result, = yield [("RD", [a, b_value])]
  331. # raise PrimitiveFinished(result)
  332. original_def.redefine(
  333. cfg_ir.create_read_dict_value(
  334. a,
  335. original_def.insert_before(cfg_ir.Read(b))))
  336. MISC_CFG_INTRINSICS = {
  337. # Reference equality
  338. 'element_eq' :
  339. lambda original_def, a, b:
  340. original_def.redefine(
  341. cfg_ir.CreateNode(
  342. original_def.insert_before(
  343. cfg_ir.Binary(a, '==', b)))),
  344. 'element_neq' :
  345. lambda original_def, a, b:
  346. original_def.redefine(
  347. cfg_ir.CreateNode(
  348. original_def.insert_before(
  349. cfg_ir.Binary(a, '!=', b)))),
  350. # String operations
  351. 'string_get' :
  352. lambda original_def, a, b:
  353. original_def.redefine(
  354. cfg_ir.CreateNode(
  355. original_def.insert_before(
  356. cfg_ir.create_index(
  357. original_def.insert_before(cfg_ir.Read(a)),
  358. original_def.insert_before(cfg_ir.Read(b)))))),
  359. 'string_len' :
  360. lambda original_def, a:
  361. original_def.redefine(
  362. cfg_ir.CreateNode(
  363. original_def.insert_before(
  364. cfg_ir.create_pure_simple_call(
  365. 'len',
  366. original_def.insert_before(cfg_ir.Read(a)))))),
  367. 'string_join' :
  368. lambda original_def, a, b:
  369. original_def.redefine(
  370. cfg_ir.CreateNode(
  371. original_def.insert_before(
  372. cfg_ir.Binary(
  373. original_def.insert_before(
  374. cfg_ir.create_pure_simple_call(
  375. 'str',
  376. original_def.insert_before(cfg_ir.Read(a)))),
  377. '+',
  378. original_def.insert_before(
  379. cfg_ir.create_pure_simple_call(
  380. 'str',
  381. original_def.insert_before(cfg_ir.Read(b)))))))),
  382. 'string_startswith' :
  383. lambda original_def, a, b:
  384. original_def.redefine(
  385. cfg_ir.CreateNode(
  386. original_def.insert_before(
  387. cfg_ir.DirectFunctionCall(
  388. 'startswith',
  389. [('self', original_def.insert_before(cfg_ir.Read(a))),
  390. ('substring', original_def.insert_before(cfg_ir.Read(b)))],
  391. calling_convention=cfg_ir.SELF_POSITIONAL_CALLING_CONVENTION,
  392. has_value=True, has_side_effects=False)))),
  393. # State creation
  394. 'create_node' :
  395. lambda original_def:
  396. original_def.redefine(
  397. cfg_ir.CreateNode(original_def.insert_before(cfg_ir.Literal(None)))),
  398. 'create_value' :
  399. lambda original_def, a:
  400. original_def.redefine(
  401. cfg_ir.CreateNode(original_def.insert_before(cfg_ir.Read(a)))),
  402. # State reads
  403. 'read_nr_out' : __read_nr_out_cfg,
  404. # Dictionary operations
  405. 'dict_len' : __read_nr_out_cfg,
  406. 'dict_read' : __dict_read_cfg,
  407. 'dict_in' : __dict_in_cfg,
  408. 'dict_in_node' : __dict_in_node_cfg,
  409. # List operations
  410. 'list_len' : __read_nr_out_cfg
  411. }
  412. def register_time_intrinsic(target_jit):
  413. """Registers the time() intrinsic with the given JIT."""
  414. import_name = target_jit.import_value(time.time, 'time')
  415. target_jit.register_intrinsic(
  416. 'time',
  417. lambda: tree_ir.CreateNodeWithValueInstruction(
  418. tree_ir.CallInstruction(
  419. tree_ir.LoadGlobalInstruction(import_name),
  420. [])))
  421. def register_intrinsics(target_jit):
  422. """Registers all intrinsics in the module with the given JIT."""
  423. for (key, value) in BINARY_INTRINSICS.items():
  424. target_jit.register_binary_intrinsic(key, value)
  425. for (key, value) in UNARY_INTRINSICS.items():
  426. target_jit.register_unary_intrinsic(key, value)
  427. for (key, value) in CAST_INTRINSICS.items():
  428. target_jit.register_cast_intrinsic(key, value)
  429. for (key, value) in MISC_INTRINSICS.items():
  430. target_jit.register_intrinsic(key, value)
  431. for (key, value) in MISC_CFG_INTRINSICS.items():
  432. target_jit.register_cfg_intrinsic(key, value)
  433. register_time_intrinsic(target_jit)