intrinsics.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. tmp = tree_ir.StoreLocalInstruction(None, a)
  58. return tree_ir.create_block(
  59. tmp,
  60. tree_ir.CreateEdgeInstruction(tmp.create_load(), b),
  61. tmp.create_load())
  62. def __dict_add(a, b, c):
  63. a_tmp = tree_ir.StoreLocalInstruction(None, a)
  64. b_tmp = tree_ir.StoreLocalInstruction(None, b)
  65. return tree_ir.create_block(
  66. a_tmp,
  67. b_tmp,
  68. tree_ir.CreateEdgeInstruction(
  69. tree_ir.CreateEdgeInstruction(a_tmp.create_load(), c),
  70. b_tmp.create_load()),
  71. a_tmp.create_load())
  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. a_tmp = tree_ir.StoreLocalInstruction(None, 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. a_tmp.create_load(), b_val.create_load()))
  89. return tree_ir.create_block(
  90. a_tmp,
  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. a_tmp = tree_ir.StoreLocalInstruction(None, a)
  116. b_tmp = tree_ir.StoreLocalInstruction(None, b)
  117. return tree_ir.create_block(
  118. a_tmp,
  119. tree_ir.CreateDictionaryEdgeInstruction(
  120. a_tmp.create_load(),
  121. create_get_length(
  122. tree_ir.ReadOutgoingEdgesInstruction(
  123. a_tmp.create_load())),
  124. b_tmp),
  125. a_tmp.create_load())
  126. def __log(a):
  127. # Original definition:
  128. #
  129. # def log(a, **remainder):
  130. # a_value, = yield [("RV", [a])]
  131. # print("== LOG == " + str(a_value))
  132. # raise PrimitiveFinished(a)
  133. a_tmp = tree_ir.StoreLocalInstruction(None, a)
  134. return tree_ir.CompoundInstruction(
  135. tree_ir.create_block(
  136. a_tmp,
  137. tree_ir.PrintInstruction(
  138. tree_ir.BinaryInstruction(
  139. tree_ir.LiteralInstruction("== LOG == "),
  140. '+',
  141. tree_ir.CallInstruction(
  142. tree_ir.LoadGlobalInstruction('str'),
  143. [tree_ir.ReadValueInstruction(a_tmp.create_load())])))),
  144. a_tmp.create_load())
  145. MISC_INTRINSICS = {
  146. # Reference equality
  147. 'element_eq' :
  148. lambda a, b:
  149. tree_ir.CreateNodeWithValueInstruction(
  150. tree_ir.BinaryInstruction(a, '==', b)),
  151. 'element_neq' :
  152. lambda a, b:
  153. tree_ir.CreateNodeWithValueInstruction(
  154. tree_ir.BinaryInstruction(a, '!=', b)),
  155. # Strings
  156. 'string_get' :
  157. lambda a, b:
  158. tree_ir.CreateNodeWithValueInstruction(
  159. tree_ir.LoadIndexInstruction(
  160. tree_ir.ReadValueInstruction(a),
  161. tree_ir.ReadValueInstruction(b))),
  162. 'string_len' :
  163. lambda a:
  164. tree_ir.CreateNodeWithValueInstruction(
  165. tree_ir.CallInstruction(
  166. tree_ir.LoadGlobalInstruction('len'),
  167. [tree_ir.ReadValueInstruction(a)])),
  168. 'string_join' :
  169. lambda a, b:
  170. tree_ir.CreateNodeWithValueInstruction(
  171. tree_ir.BinaryInstruction(
  172. tree_ir.CallInstruction(
  173. tree_ir.LoadGlobalInstruction('str'),
  174. [tree_ir.ReadValueInstruction(a)]),
  175. '+',
  176. tree_ir.CallInstruction(
  177. tree_ir.LoadGlobalInstruction('str'),
  178. [tree_ir.ReadValueInstruction(b)]))),
  179. 'string_startswith' :
  180. lambda a, b:
  181. tree_ir.CreateNodeWithValueInstruction(
  182. tree_ir.CallInstruction(
  183. tree_ir.LoadMemberInstruction(
  184. tree_ir.ReadValueInstruction(a),
  185. 'startswith'),
  186. [tree_ir.ReadValueInstruction(b)])),
  187. # State creation
  188. 'create_node' : tree_ir.CreateNodeInstruction,
  189. 'create_edge' :
  190. # Lambda is totally necessary here, pylint.
  191. # You totally dropped the ball on this one.
  192. # pylint: disable=I0011,W0108
  193. lambda a, b:
  194. tree_ir.CreateEdgeInstruction(a, b),
  195. 'create_value' :
  196. lambda a:
  197. tree_ir.CreateNodeWithValueInstruction(
  198. tree_ir.ReadValueInstruction(a)),
  199. # State reads
  200. 'read_edge_src' :
  201. lambda a:
  202. tree_ir.LoadIndexInstruction(
  203. tree_ir.ReadEdgeInstruction(a),
  204. tree_ir.LiteralInstruction(0)),
  205. 'read_edge_dst' :
  206. lambda a:
  207. tree_ir.LoadIndexInstruction(
  208. tree_ir.ReadEdgeInstruction(a),
  209. tree_ir.LiteralInstruction(1)),
  210. 'is_edge' :
  211. lambda a:
  212. tree_ir.CreateNodeWithValueInstruction(
  213. tree_ir.BinaryInstruction(
  214. tree_ir.LoadIndexInstruction(
  215. tree_ir.ReadEdgeInstruction(a),
  216. tree_ir.LiteralInstruction(0)),
  217. 'is not',
  218. tree_ir.LiteralInstruction(None))),
  219. # read_root
  220. 'read_root' :
  221. lambda:
  222. tree_ir.LoadIndexInstruction(
  223. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  224. tree_ir.LiteralInstruction('root')),
  225. # read_userroot
  226. 'read_userroot' :
  227. lambda:
  228. tree_ir.LoadIndexInstruction(
  229. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  230. tree_ir.LiteralInstruction('task_root')),
  231. # Dictionary operations
  232. 'dict_read' :
  233. lambda a, b:
  234. tree_ir.ReadDictionaryValueInstruction(
  235. a, tree_ir.ReadValueInstruction(b)),
  236. 'dict_read_edge' :
  237. lambda a, b:
  238. tree_ir.ReadDictionaryEdgeInstruction(
  239. a, tree_ir.ReadValueInstruction(b)),
  240. 'dict_add' : __dict_add,
  241. # Set operations
  242. 'set_add' : __set_add,
  243. # List operations
  244. 'list_len' :
  245. lambda a:
  246. tree_ir.CreateNodeWithValueInstruction(
  247. create_get_length(tree_ir.ReadOutgoingEdgesInstruction(a))),
  248. 'list_read' : __list_read,
  249. 'list_append' : __list_append,
  250. # log
  251. 'log' : __log
  252. }
  253. MISC_CFG_INTRINSICS = {
  254. # State creation
  255. 'create_node' :
  256. lambda original_def:
  257. original_def.redefine(
  258. cfg_ir.CreateNode(original_def.insert_before(cfg_ir.Literal(None)))),
  259. 'create_value' :
  260. lambda original_def, a:
  261. original_def.redefine(
  262. cfg_ir.CreateNode(original_def.insert_before(cfg_ir.Read(a))))
  263. }
  264. def register_time_intrinsic(target_jit):
  265. """Registers the time() intrinsic with the given JIT."""
  266. import_name = target_jit.import_value(time.time, 'time')
  267. target_jit.register_intrinsic(
  268. 'time',
  269. lambda: tree_ir.CreateNodeWithValueInstruction(
  270. tree_ir.CallInstruction(
  271. tree_ir.LoadGlobalInstruction(import_name),
  272. [])))
  273. def register_intrinsics(target_jit):
  274. """Registers all intrinsics in the module with the given JIT."""
  275. for (key, value) in BINARY_INTRINSICS.items():
  276. target_jit.register_binary_intrinsic(key, value)
  277. for (key, value) in UNARY_INTRINSICS.items():
  278. target_jit.register_unary_intrinsic(key, value)
  279. for (key, value) in CAST_INTRINSICS.items():
  280. target_jit.register_cast_intrinsic(key, value)
  281. for (key, value) in MISC_INTRINSICS.items():
  282. target_jit.register_intrinsic(key, value)
  283. for (key, value) in MISC_CFG_INTRINSICS.items():
  284. target_jit.register_cfg_intrinsic(key, value)
  285. register_time_intrinsic(target_jit)