intrinsics.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. # State creation
  41. 'create_node' : tree_ir.CreateNodeInstruction,
  42. 'create_edge' : tree_ir.CreateEdgeInstruction,
  43. 'create_value' :
  44. lambda val:
  45. tree_ir.CreateNodeWithValueInstruction(
  46. tree_ir.ReadValueInstruction(val)),
  47. # State reads
  48. 'read_edge_src' :
  49. lambda e:
  50. tree_ir.LoadIndexInstruction(
  51. tree_ir.ReadEdgeInstruction(e),
  52. tree_ir.LiteralInstruction(0)),
  53. 'read_edge_dst' :
  54. lambda e:
  55. tree_ir.LoadIndexInstruction(
  56. tree_ir.ReadEdgeInstruction(e),
  57. tree_ir.LiteralInstruction(1)),
  58. 'is_edge' :
  59. lambda e:
  60. tree_ir.CreateNodeWithValueInstruction(
  61. tree_ir.BinaryInstruction(
  62. tree_ir.LoadIndexInstruction(
  63. tree_ir.ReadEdgeInstruction(e),
  64. tree_ir.LiteralInstruction(0)),
  65. 'is not',
  66. tree_ir.LiteralInstruction(None))),
  67. # Dictionary operations
  68. 'dict_read' :
  69. lambda dict_node, key:
  70. tree_ir.ReadDictionaryValueInstruction(
  71. dict_node, tree_ir.ReadValueInstruction(key)),
  72. 'dict_read_edge' :
  73. lambda dict_node, key:
  74. tree_ir.ReadDictionaryEdgeInstruction(
  75. dict_node, tree_ir.ReadValueInstruction(key))
  76. }
  77. def register_intrinsics(target_jit):
  78. """Registers all intrinsics in the module with the given JIT."""
  79. for (key, value) in BINARY_INTRINSICS.items():
  80. target_jit.register_binary_intrinsic(key, value)
  81. for (key, value) in UNARY_INTRINSICS.items():
  82. target_jit.register_unary_intrinsic(key, value)
  83. for (key, value) in MISC_INTRINSICS.items():
  84. target_jit.register_intrinsic(key, value)