123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- import jit
- import tree_ir
- BINARY_INTRINSICS = {
- 'value_eq' : '==',
- 'value_neq' : '!=',
- 'bool_and' : 'and',
- 'bool_or' : 'or',
- 'integer_addition' : '+',
- 'integer_subtraction' : '-',
- 'integer_multiplication' : '*',
- 'integer_division' : '/',
- 'integer_gt' : '>',
- 'integer_gte' : '>=',
- 'integer_lt' : '<',
- 'integer_lte' : '<=',
- 'float_addition' : '+',
- 'float_subtraction' : '-',
- 'float_multiplication' : '*',
- 'float_division' : '/',
- 'float_gt' : '>',
- 'float_gte' : '>=',
- 'float_lt' : '<',
- 'float_lte' : '<='
- }
- UNARY_INTRINSICS = {
- 'bool_not' : 'not',
- 'integer_neg' : '-',
- 'float_neg' : '-'
- }
- CAST_INTRINSICS = {
- 'cast_i2f' : float,
- 'cast_i2s' : str,
- 'cast_i2b' : bool,
- 'cast_f2i' : int,
- 'cast_f2s' : str,
- 'cast_f2b' : bool,
- 'cast_s2i' : int,
- 'cast_s2f' : float,
- 'cast_s2b' : bool,
- 'cast_b2i' : int,
- 'cast_b2f' : float,
- 'cast_b2s' : str
- }
- def create_get_length(expression):
- """Creates an expression that evaluates the given expression, and then
- computes the length of its result."""
- return tree_ir.CallInstruction(
- tree_ir.LoadGlobalInstruction('len'),
- [expression])
- # Don't compain about the variable names, pylint. It's important that we
- # get them right.
- # pylint: disable=I0011,C0103
- def __set_add(a, b):
- tmp = tree_ir.StoreLocalInstruction(None, a)
- return tree_ir.create_block(
- tmp,
- tree_ir.CreateEdgeInstruction(tmp.create_load(), b),
- tmp.create_load())
- def __dict_add(a, b, c):
- a_tmp = tree_ir.StoreLocalInstruction(None, a)
- b_tmp = tree_ir.StoreLocalInstruction(None, b)
- return tree_ir.create_block(
- a_tmp,
- b_tmp,
- tree_ir.CreateEdgeInstruction(
- tree_ir.CreateEdgeInstruction(a_tmp.create_load(), c),
- b_tmp.create_load()),
- a_tmp.create_load())
- def __list_read(a, b):
- # The statements in this function generate the following code:
- #
- # a_tmp = a # To make sure a is evaluated before b.
- # b_value, = yield [("RV", [b])]
- # result, = yield [("RD", [a_tmp, b_value])]
- # if result is None:
- # raise Exception("List read out of bounds: %s" % b_value)
- # result
- a_tmp = tree_ir.StoreLocalInstruction(None, a)
- b_val = tree_ir.StoreLocalInstruction(
- None,
- tree_ir.ReadValueInstruction(b))
- result = tree_ir.StoreLocalInstruction(
- None,
- tree_ir.ReadDictionaryValueInstruction(
- a_tmp.create_load(), b_val.create_load()))
- return tree_ir.create_block(
- a_tmp,
- b_val,
- result,
- tree_ir.SelectInstruction(
- tree_ir.BinaryInstruction(
- result.create_load(),
- 'is',
- tree_ir.LiteralInstruction(None)),
- tree_ir.RaiseInstruction(
- tree_ir.CallInstruction(
- tree_ir.LoadGlobalInstruction('Exception'),
- [tree_ir.BinaryInstruction(
- tree_ir.LiteralInstruction('List read out of bounds: %s'),
- '%',
- b_val.create_load())])),
- tree_ir.NopInstruction()),
- result.create_load())
- def __list_append(a, b):
- # We want to generate code that is more or less equivalent to:
- #
- # a_tmp = a
- # b_tmp = b
- # a_outgoing, = yield [("RO", [a_tmp])]
- # _ = yield [("CD", [a_tmp, len(a_outgoing), b_tmp])]
- # a
- a_tmp = tree_ir.StoreLocalInstruction(None, a)
- b_tmp = tree_ir.StoreLocalInstruction(None, b)
- return tree_ir.create_block(
- a_tmp,
- tree_ir.CreateDictionaryEdgeInstruction(
- a_tmp.create_load(),
- create_get_length(
- tree_ir.ReadOutgoingEdgesInstruction(
- a_tmp.create_load())),
- b_tmp),
- a_tmp.create_load())
- MISC_INTRINSICS = {
- # Reference equality
- 'element_eq' :
- lambda a, b:
- tree_ir.CreateNodeWithValueInstruction(
- tree_ir.BinaryInstruction(a, '==', b)),
- 'element_neq' :
- lambda a, b:
- tree_ir.CreateNodeWithValueInstruction(
- tree_ir.BinaryInstruction(a, '!=', b)),
- # Strings
- 'string_get' :
- lambda a, b:
- tree_ir.CreateNodeWithValueInstruction(
- tree_ir.LoadIndexInstruction(
- tree_ir.ReadValueInstruction(a),
- tree_ir.ReadValueInstruction(b))),
- 'string_len' :
- lambda a:
- tree_ir.CreateNodeWithValueInstruction(
- tree_ir.CallInstruction(
- tree_ir.LoadGlobalInstruction('len'),
- [tree_ir.ReadValueInstruction(a)])),
- 'string_join' :
- lambda a, b:
- tree_ir.CreateNodeWithValueInstruction(
- tree_ir.BinaryInstruction(
- tree_ir.CallInstruction(
- tree_ir.LoadGlobalInstruction('str'),
- [tree_ir.ReadValueInstruction(a)]),
- '+',
- tree_ir.CallInstruction(
- tree_ir.LoadGlobalInstruction('str'),
- [tree_ir.ReadValueInstruction(b)]))),
- 'string_startswith' :
- lambda a, b:
- tree_ir.CreateNodeWithValueInstruction(
- tree_ir.CallInstruction(
- tree_ir.LoadMemberInstruction(
- tree_ir.ReadValueInstruction(a),
- 'startswith'),
- [tree_ir.ReadValueInstruction(b)])),
- # State creation
- 'create_node' : tree_ir.CreateNodeInstruction,
- 'create_edge' :
- # Lambda is totally necessary here, pylint.
- # You totally dropped the ball on this one.
- # pylint: disable=I0011,W0108
- lambda a, b:
- tree_ir.CreateEdgeInstruction(a, b),
- 'create_value' :
- lambda a:
- tree_ir.CreateNodeWithValueInstruction(
- tree_ir.ReadValueInstruction(a)),
- # State reads
- 'read_edge_src' :
- lambda a:
- tree_ir.LoadIndexInstruction(
- tree_ir.ReadEdgeInstruction(a),
- tree_ir.LiteralInstruction(0)),
- 'read_edge_dst' :
- lambda a:
- tree_ir.LoadIndexInstruction(
- tree_ir.ReadEdgeInstruction(a),
- tree_ir.LiteralInstruction(1)),
- 'is_edge' :
- lambda a:
- tree_ir.CreateNodeWithValueInstruction(
- tree_ir.BinaryInstruction(
- tree_ir.LoadIndexInstruction(
- tree_ir.ReadEdgeInstruction(a),
- tree_ir.LiteralInstruction(0)),
- 'is not',
- tree_ir.LiteralInstruction(None))),
- # read_root
- 'read_root' :
- lambda:
- tree_ir.LoadIndexInstruction(
- tree_ir.LoadLocalInstruction(jit.KWARGS_PARAMETER_NAME),
- tree_ir.LiteralInstruction('root')),
- # # read_userroot
- 'read_userroot' :
- lambda:
- tree_ir.LoadIndexInstruction(
- tree_ir.LoadLocalInstruction(jit.KWARGS_PARAMETER_NAME),
- tree_ir.LiteralInstruction('user_root')),
- # Dictionary operations
- 'dict_read' :
- lambda a, b:
- tree_ir.ReadDictionaryValueInstruction(
- a, tree_ir.ReadValueInstruction(b)),
- 'dict_read_edge' :
- lambda a, b:
- tree_ir.ReadDictionaryEdgeInstruction(
- a, tree_ir.ReadValueInstruction(b)),
- 'dict_add' : __dict_add,
- # Set operations
- 'set_add' : __set_add,
- # List operations
- 'list_len' :
- lambda a:
- tree_ir.CreateNodeWithValueInstruction(
- create_get_length(tree_ir.ReadOutgoingEdgesInstruction(a))),
- 'list_read' : __list_read,
- 'list_append' : __list_append
- }
- def register_intrinsics(target_jit):
- """Registers all intrinsics in the module with the given JIT."""
- for (key, value) in BINARY_INTRINSICS.items():
- target_jit.register_binary_intrinsic(key, value)
- for (key, value) in UNARY_INTRINSICS.items():
- target_jit.register_unary_intrinsic(key, value)
- for (key, value) in CAST_INTRINSICS.items():
- target_jit.register_cast_intrinsic(key, value)
- for (key, value) in MISC_INTRINSICS.items():
- target_jit.register_intrinsic(key, value)
|