intrinsics.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 lhs, rhs:
  34. tree_ir.CreateNodeWithValueInstruction(
  35. tree_ir.BinaryInstruction(lhs, '==', rhs)),
  36. 'element_neq' :
  37. lambda lhs, rhs:
  38. tree_ir.CreateNodeWithValueInstruction(
  39. tree_ir.BinaryInstruction(lhs, '!=', rhs)),
  40. # Strings
  41. 'string_get' :
  42. lambda str_node, index_node:
  43. tree_ir.CreateNodeWithValueInstruction(
  44. tree_ir.LoadIndexInstruction(
  45. tree_ir.ReadValueInstruction(str_node),
  46. tree_ir.ReadValueInstruction(index_node))),
  47. 'string_len' :
  48. lambda str_node:
  49. tree_ir.CreateNodeWithValueInstruction(
  50. tree_ir.CallInstruction(
  51. tree_ir.LoadGlobalInstruction('len'),
  52. [tree_ir.ReadValueInstruction(str_node)])),
  53. 'string_join' :
  54. lambda lhs, rhs:
  55. tree_ir.CreateNodeWithValueInstruction(
  56. tree_ir.BinaryInstruction(
  57. tree_ir.CallInstruction(
  58. tree_ir.LoadGlobalInstruction('str'),
  59. [tree_ir.ReadValueInstruction(lhs)]),
  60. '+',
  61. tree_ir.CallInstruction(
  62. tree_ir.LoadGlobalInstruction('str'),
  63. [tree_ir.ReadValueInstruction(rhs)]))),
  64. # State creation
  65. 'create_node' : tree_ir.CreateNodeInstruction,
  66. 'create_edge' : tree_ir.CreateEdgeInstruction,
  67. 'create_value' :
  68. lambda val:
  69. tree_ir.CreateNodeWithValueInstruction(
  70. tree_ir.ReadValueInstruction(val)),
  71. # State reads
  72. 'read_edge_src' :
  73. lambda e:
  74. tree_ir.LoadIndexInstruction(
  75. tree_ir.ReadEdgeInstruction(e),
  76. tree_ir.LiteralInstruction(0)),
  77. 'read_edge_dst' :
  78. lambda e:
  79. tree_ir.LoadIndexInstruction(
  80. tree_ir.ReadEdgeInstruction(e),
  81. tree_ir.LiteralInstruction(1)),
  82. 'is_edge' :
  83. lambda e:
  84. tree_ir.CreateNodeWithValueInstruction(
  85. tree_ir.BinaryInstruction(
  86. tree_ir.LoadIndexInstruction(
  87. tree_ir.ReadEdgeInstruction(e),
  88. tree_ir.LiteralInstruction(0)),
  89. 'is not',
  90. tree_ir.LiteralInstruction(None))),
  91. # Dictionary operations
  92. 'dict_read' :
  93. lambda dict_node, key:
  94. tree_ir.ReadDictionaryValueInstruction(
  95. dict_node, tree_ir.ReadValueInstruction(key)),
  96. 'dict_read_edge' :
  97. lambda dict_node, key:
  98. tree_ir.ReadDictionaryEdgeInstruction(
  99. dict_node, tree_ir.ReadValueInstruction(key))
  100. }
  101. def register_intrinsics(target_jit):
  102. """Registers all intrinsics in the module with the given JIT."""
  103. for (key, value) in BINARY_INTRINSICS.items():
  104. target_jit.register_binary_intrinsic(key, value)
  105. for (key, value) in UNARY_INTRINSICS.items():
  106. target_jit.register_unary_intrinsic(key, value)
  107. for (key, value) in MISC_INTRINSICS.items():
  108. target_jit.register_intrinsic(key, value)