intrinsics.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }
  59. def register_intrinsics(target_jit):
  60. """Registers all intrinsics in the module with the given JIT."""
  61. for (key, value) in BINARY_INTRINSICS.items():
  62. target_jit.register_binary_intrinsic(key, value)
  63. for (key, value) in UNARY_INTRINSICS.items():
  64. target_jit.register_unary_intrinsic(key, value)
  65. for (key, value) in MISC_INTRINSICS.items():
  66. target_jit.register_intrinsic(key, value)