intrinsics.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 __list_read(a, b):
  73. # The statements in this function generate the following code:
  74. #
  75. # a_tmp = a # To make sure a is evaluated before b.
  76. # b_value, = yield [("RV", [b])]
  77. # result, = yield [("RD", [a_tmp, b_value])]
  78. # if result is None:
  79. # raise Exception("List read out of bounds: %s" % b_value)
  80. # result
  81. store_a, load_a = tree_ir.evaluate_and_load(a)
  82. b_val = tree_ir.StoreLocalInstruction(
  83. None,
  84. tree_ir.ReadValueInstruction(b))
  85. result = tree_ir.StoreLocalInstruction(
  86. None,
  87. tree_ir.ReadDictionaryValueInstruction(
  88. load_a.create_load(), b_val.create_load()))
  89. return tree_ir.create_block(
  90. store_a,
  91. b_val,
  92. result,
  93. tree_ir.SelectInstruction(
  94. tree_ir.BinaryInstruction(
  95. result.create_load(),
  96. 'is',
  97. tree_ir.LiteralInstruction(None)),
  98. tree_ir.RaiseInstruction(
  99. tree_ir.CallInstruction(
  100. tree_ir.LoadGlobalInstruction('Exception'),
  101. [tree_ir.BinaryInstruction(
  102. tree_ir.LiteralInstruction('List read out of bounds: %s'),
  103. '%',
  104. b_val.create_load())])),
  105. tree_ir.NopInstruction()),
  106. result.create_load())
  107. def __list_append(a, b):
  108. # We want to generate code that is more or less equivalent to:
  109. #
  110. # a_tmp = a
  111. # b_tmp = b
  112. # a_outgoing, = yield [("RO", [a_tmp])]
  113. # _ = yield [("CD", [a_tmp, len(a_outgoing), b_tmp])]
  114. # a
  115. store_a, load_a = tree_ir.evaluate_and_load(a)
  116. store_b, load_b = tree_ir.evaluate_and_load(b)
  117. return tree_ir.create_block(
  118. store_a,
  119. store_b,
  120. tree_ir.CreateDictionaryEdgeInstruction(
  121. load_a,
  122. create_get_length(
  123. tree_ir.ReadOutgoingEdgesInstruction(
  124. load_a)),
  125. load_b),
  126. load_a)
  127. def __log(a):
  128. # Original definition:
  129. #
  130. # def log(a, **remainder):
  131. # a_value, = yield [("RV", [a])]
  132. # print("== LOG == " + str(a_value))
  133. # raise PrimitiveFinished(a)
  134. store_a, load_a = tree_ir.evaluate_and_load(a)
  135. return tree_ir.CompoundInstruction(
  136. tree_ir.create_block(
  137. store_a,
  138. tree_ir.PrintInstruction(
  139. tree_ir.BinaryInstruction(
  140. tree_ir.LiteralInstruction("== LOG == "),
  141. '+',
  142. tree_ir.CallInstruction(
  143. tree_ir.LoadGlobalInstruction('str'),
  144. [tree_ir.ReadValueInstruction(load_a)])))),
  145. load_a)
  146. MISC_INTRINSICS = {
  147. # Reference equality
  148. 'element_eq' :
  149. lambda a, b:
  150. tree_ir.CreateNodeWithValueInstruction(
  151. tree_ir.BinaryInstruction(a, '==', b)),
  152. 'element_neq' :
  153. lambda a, b:
  154. tree_ir.CreateNodeWithValueInstruction(
  155. tree_ir.BinaryInstruction(a, '!=', b)),
  156. # Strings
  157. 'string_get' :
  158. lambda a, b:
  159. tree_ir.CreateNodeWithValueInstruction(
  160. tree_ir.LoadIndexInstruction(
  161. tree_ir.ReadValueInstruction(a),
  162. tree_ir.ReadValueInstruction(b))),
  163. 'string_len' :
  164. lambda a:
  165. tree_ir.CreateNodeWithValueInstruction(
  166. tree_ir.CallInstruction(
  167. tree_ir.LoadGlobalInstruction('len'),
  168. [tree_ir.ReadValueInstruction(a)])),
  169. 'string_join' :
  170. lambda a, b:
  171. tree_ir.CreateNodeWithValueInstruction(
  172. tree_ir.BinaryInstruction(
  173. tree_ir.CallInstruction(
  174. tree_ir.LoadGlobalInstruction('str'),
  175. [tree_ir.ReadValueInstruction(a)]),
  176. '+',
  177. tree_ir.CallInstruction(
  178. tree_ir.LoadGlobalInstruction('str'),
  179. [tree_ir.ReadValueInstruction(b)]))),
  180. 'string_startswith' :
  181. lambda a, b:
  182. tree_ir.CreateNodeWithValueInstruction(
  183. tree_ir.CallInstruction(
  184. tree_ir.LoadMemberInstruction(
  185. tree_ir.ReadValueInstruction(a),
  186. 'startswith'),
  187. [tree_ir.ReadValueInstruction(b)])),
  188. # State creation
  189. 'create_node' : tree_ir.CreateNodeInstruction,
  190. 'create_edge' :
  191. # Lambda is totally necessary here, pylint.
  192. # You totally dropped the ball on this one.
  193. # pylint: disable=I0011,W0108
  194. lambda a, b:
  195. tree_ir.CreateEdgeInstruction(a, b),
  196. 'create_value' :
  197. lambda a:
  198. tree_ir.CreateNodeWithValueInstruction(
  199. tree_ir.ReadValueInstruction(a)),
  200. # State reads
  201. 'read_edge_src' :
  202. lambda a:
  203. tree_ir.LoadIndexInstruction(
  204. tree_ir.ReadEdgeInstruction(a),
  205. tree_ir.LiteralInstruction(0)),
  206. 'read_edge_dst' :
  207. lambda a:
  208. tree_ir.LoadIndexInstruction(
  209. tree_ir.ReadEdgeInstruction(a),
  210. tree_ir.LiteralInstruction(1)),
  211. 'is_edge' :
  212. lambda a:
  213. tree_ir.CreateNodeWithValueInstruction(
  214. tree_ir.BinaryInstruction(
  215. tree_ir.LoadIndexInstruction(
  216. tree_ir.ReadEdgeInstruction(a),
  217. tree_ir.LiteralInstruction(0)),
  218. 'is not',
  219. tree_ir.LiteralInstruction(None))),
  220. # read_root
  221. 'read_root' :
  222. lambda:
  223. tree_ir.LoadIndexInstruction(
  224. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  225. tree_ir.LiteralInstruction('root')),
  226. # read_userroot
  227. 'read_userroot' :
  228. lambda:
  229. tree_ir.LoadIndexInstruction(
  230. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  231. tree_ir.LiteralInstruction('task_root')),
  232. # Dictionary operations
  233. 'dict_read' :
  234. lambda a, b:
  235. tree_ir.ReadDictionaryValueInstruction(
  236. a, tree_ir.ReadValueInstruction(b)),
  237. 'dict_read_edge' :
  238. lambda a, b:
  239. tree_ir.ReadDictionaryEdgeInstruction(
  240. a, tree_ir.ReadValueInstruction(b)),
  241. 'dict_add' : __dict_add,
  242. # Set operations
  243. 'set_add' : __set_add,
  244. # List operations
  245. 'list_len' :
  246. lambda a:
  247. tree_ir.CreateNodeWithValueInstruction(
  248. create_get_length(tree_ir.ReadOutgoingEdgesInstruction(a))),
  249. 'list_read' : __list_read,
  250. 'list_append' : __list_append,
  251. # log
  252. 'log' : __log
  253. }
  254. MISC_CFG_INTRINSICS = {
  255. # State creation
  256. 'create_node' :
  257. lambda original_def:
  258. original_def.redefine(
  259. cfg_ir.CreateNode(original_def.insert_before(cfg_ir.Literal(None)))),
  260. 'create_value' :
  261. lambda original_def, a:
  262. original_def.redefine(
  263. cfg_ir.CreateNode(original_def.insert_before(cfg_ir.Read(a))))
  264. }
  265. def register_time_intrinsic(target_jit):
  266. """Registers the time() intrinsic with the given JIT."""
  267. import_name = target_jit.import_value(time.time, 'time')
  268. target_jit.register_intrinsic(
  269. 'time',
  270. lambda: tree_ir.CreateNodeWithValueInstruction(
  271. tree_ir.CallInstruction(
  272. tree_ir.LoadGlobalInstruction(import_name),
  273. [])))
  274. def register_intrinsics(target_jit):
  275. """Registers all intrinsics in the module with the given JIT."""
  276. for (key, value) in BINARY_INTRINSICS.items():
  277. target_jit.register_binary_intrinsic(key, value)
  278. for (key, value) in UNARY_INTRINSICS.items():
  279. target_jit.register_unary_intrinsic(key, value)
  280. for (key, value) in CAST_INTRINSICS.items():
  281. target_jit.register_cast_intrinsic(key, value)
  282. for (key, value) in MISC_INTRINSICS.items():
  283. target_jit.register_intrinsic(key, value)
  284. for (key, value) in MISC_CFG_INTRINSICS.items():
  285. target_jit.register_cfg_intrinsic(key, value)
  286. register_time_intrinsic(target_jit)