intrinsics.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. # State creation
  65. 'create_node' : tree_ir.CreateNodeInstruction,
  66. 'create_edge' :
  67. # Lambda is totally necessary here, pylint.
  68. # You totally dropped the ball on this one.
  69. # pylint: disable=I0011,W0108
  70. lambda a, b:
  71. tree_ir.CreateEdgeInstruction(a, b),
  72. 'create_value' :
  73. lambda a:
  74. tree_ir.CreateNodeWithValueInstruction(
  75. tree_ir.ReadValueInstruction(a)),
  76. # State reads
  77. 'read_edge_src' :
  78. lambda a:
  79. tree_ir.LoadIndexInstruction(
  80. tree_ir.ReadEdgeInstruction(a),
  81. tree_ir.LiteralInstruction(0)),
  82. 'read_edge_dst' :
  83. lambda a:
  84. tree_ir.LoadIndexInstruction(
  85. tree_ir.ReadEdgeInstruction(a),
  86. tree_ir.LiteralInstruction(1)),
  87. 'is_edge' :
  88. lambda a:
  89. tree_ir.CreateNodeWithValueInstruction(
  90. tree_ir.BinaryInstruction(
  91. tree_ir.LoadIndexInstruction(
  92. tree_ir.ReadEdgeInstruction(a),
  93. tree_ir.LiteralInstruction(0)),
  94. 'is not',
  95. tree_ir.LiteralInstruction(None))),
  96. # Dictionary operations
  97. 'dict_read' :
  98. lambda a, b:
  99. tree_ir.ReadDictionaryValueInstruction(
  100. a, tree_ir.ReadValueInstruction(b)),
  101. 'dict_read_edge' :
  102. lambda a, b:
  103. tree_ir.ReadDictionaryEdgeInstruction(
  104. a, tree_ir.ReadValueInstruction(b))
  105. }
  106. def register_intrinsics(target_jit):
  107. """Registers all intrinsics in the module with the given JIT."""
  108. for (key, value) in BINARY_INTRINSICS.items():
  109. target_jit.register_binary_intrinsic(key, value)
  110. for (key, value) in UNARY_INTRINSICS.items():
  111. target_jit.register_unary_intrinsic(key, value)
  112. for (key, value) in MISC_INTRINSICS.items():
  113. target_jit.register_intrinsic(key, value)