intrinsics.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import jit
  2. import tree_ir
  3. BINARY_INTRINSICS = {
  4. 'value_eq' : '==',
  5. 'value_neq' : '!=',
  6. 'bool_and' : 'and',
  7. 'bool_or' : 'or',
  8. 'integer_addition' : '+',
  9. 'integer_subtraction' : '-',
  10. 'integer_multiplication' : '*',
  11. 'integer_division' : '/',
  12. 'integer_gt' : '>',
  13. 'integer_gte' : '>=',
  14. 'integer_lt' : '<',
  15. 'integer_lte' : '<=',
  16. 'float_addition' : '+',
  17. 'float_subtraction' : '-',
  18. 'float_multiplication' : '*',
  19. 'float_division' : '/',
  20. 'float_gt' : '>',
  21. 'float_gte' : '>=',
  22. 'float_lt' : '<',
  23. 'float_lte' : '<='
  24. }
  25. UNARY_INTRINSICS = {
  26. 'bool_not' : 'not',
  27. 'integer_neg' : '-',
  28. 'float_neg' : '-'
  29. }
  30. MISC_INTRINSICS = {
  31. # Reference equality
  32. 'element_eq' :
  33. lambda a, b:
  34. tree_ir.CreateNodeWithValueInstruction(
  35. tree_ir.BinaryInstruction(a, '==', b)),
  36. 'element_neq' :
  37. lambda a, b:
  38. tree_ir.CreateNodeWithValueInstruction(
  39. tree_ir.BinaryInstruction(a, '!=', b)),
  40. # Strings
  41. 'string_get' :
  42. lambda a, b:
  43. tree_ir.CreateNodeWithValueInstruction(
  44. tree_ir.LoadIndexInstruction(
  45. tree_ir.ReadValueInstruction(a),
  46. tree_ir.ReadValueInstruction(b))),
  47. 'string_len' :
  48. lambda a:
  49. tree_ir.CreateNodeWithValueInstruction(
  50. tree_ir.CallInstruction(
  51. tree_ir.LoadGlobalInstruction('len'),
  52. [tree_ir.ReadValueInstruction(a)])),
  53. 'string_join' :
  54. lambda a, b:
  55. tree_ir.CreateNodeWithValueInstruction(
  56. tree_ir.BinaryInstruction(
  57. tree_ir.CallInstruction(
  58. tree_ir.LoadGlobalInstruction('str'),
  59. [tree_ir.ReadValueInstruction(a)]),
  60. '+',
  61. tree_ir.CallInstruction(
  62. tree_ir.LoadGlobalInstruction('str'),
  63. [tree_ir.ReadValueInstruction(b)]))),
  64. 'string_startswith' :
  65. lambda a, b:
  66. tree_ir.CreateNodeWithValueInstruction(
  67. tree_ir.CallInstruction(
  68. tree_ir.LoadMemberInstruction(
  69. tree_ir.ReadValueInstruction(a),
  70. 'startswith'),
  71. [tree_ir.ReadValueInstruction(b)])),
  72. # State creation
  73. 'create_node' : tree_ir.CreateNodeInstruction,
  74. 'create_edge' :
  75. # Lambda is totally necessary here, pylint.
  76. # You totally dropped the ball on this one.
  77. # pylint: disable=I0011,W0108
  78. lambda a, b:
  79. tree_ir.CreateEdgeInstruction(a, b),
  80. 'create_value' :
  81. lambda a:
  82. tree_ir.CreateNodeWithValueInstruction(
  83. tree_ir.ReadValueInstruction(a)),
  84. # State reads
  85. 'read_edge_src' :
  86. lambda a:
  87. tree_ir.LoadIndexInstruction(
  88. tree_ir.ReadEdgeInstruction(a),
  89. tree_ir.LiteralInstruction(0)),
  90. 'read_edge_dst' :
  91. lambda a:
  92. tree_ir.LoadIndexInstruction(
  93. tree_ir.ReadEdgeInstruction(a),
  94. tree_ir.LiteralInstruction(1)),
  95. 'is_edge' :
  96. lambda a:
  97. tree_ir.CreateNodeWithValueInstruction(
  98. tree_ir.BinaryInstruction(
  99. tree_ir.LoadIndexInstruction(
  100. tree_ir.ReadEdgeInstruction(a),
  101. tree_ir.LiteralInstruction(0)),
  102. 'is not',
  103. tree_ir.LiteralInstruction(None))),
  104. # Dictionary operations
  105. 'dict_read' :
  106. lambda a, b:
  107. tree_ir.ReadDictionaryValueInstruction(
  108. a, tree_ir.ReadValueInstruction(b)),
  109. 'dict_read_edge' :
  110. lambda a, b:
  111. tree_ir.ReadDictionaryEdgeInstruction(
  112. a, tree_ir.ReadValueInstruction(b))
  113. }
  114. def register_intrinsics(target_jit):
  115. """Registers all intrinsics in the module with the given JIT."""
  116. for (key, value) in BINARY_INTRINSICS.items():
  117. target_jit.register_binary_intrinsic(key, value)
  118. for (key, value) in UNARY_INTRINSICS.items():
  119. target_jit.register_unary_intrinsic(key, value)
  120. for (key, value) in MISC_INTRINSICS.items():
  121. target_jit.register_intrinsic(key, value)