def test_dict_set_item(self) -> None: self.assert_emit( CallC(dict_set_item_op.c_function_name, [self.d, self.o, self.o2], dict_set_item_op.return_type, dict_set_item_op.steals, dict_set_item_op.is_borrowed, dict_set_item_op.error_kind, 1), """cpy_r_r0 = CPyDict_SetItem(cpy_r_d, cpy_r_o, cpy_r_o2);""")
def test_list_set_item(self) -> None: self.assert_emit( CallC(list_set_item_op.c_function_name, [self.l, self.n, self.o], list_set_item_op.return_type, list_set_item_op.steals, list_set_item_op.is_borrowed, list_set_item_op.error_kind, 55), """cpy_r_r0 = CPyList_SetItem(cpy_r_l, cpy_r_n, cpy_r_o);""")
def assert_emit_binary_op(self, op: str, dest: Value, left: Value, right: Value, expected: str) -> None: # TODO: merge this if op in c_binary_ops: c_ops = c_binary_ops[op] for c_desc in c_ops: if (is_subtype(left.type, c_desc.arg_types[0]) and is_subtype(right.type, c_desc.arg_types[1])): args = [left, right] if c_desc.ordering is not None: args = [args[i] for i in c_desc.ordering] self.assert_emit(CallC(c_desc.c_function_name, args, c_desc.return_type, c_desc.steals, c_desc.error_kind, 55), expected) return ops = binary_ops[op] for desc in ops: if (is_subtype(left.type, desc.arg_types[0]) and is_subtype(right.type, desc.arg_types[1])): self.assert_emit(PrimitiveOp([left, right], desc, 55), expected) break else: assert False, 'Could not find matching op'
def call_c(self, desc: CFunctionDescription, args: List[Value], line: int, result_type: Optional[RType] = None) -> Value: # handle void function via singleton RVoid instance coerced = [] # coerce fixed number arguments for i in range(min(len(args), len(desc.arg_types))): formal_type = desc.arg_types[i] arg = args[i] arg = self.coerce(arg, formal_type, line) coerced.append(arg) # coerce any var_arg var_arg_idx = -1 if desc.var_arg_type is not None: var_arg_idx = len(desc.arg_types) for i in range(len(desc.arg_types), len(args)): arg = args[i] arg = self.coerce(arg, desc.var_arg_type, line) coerced.append(arg) target = self.add( CallC(desc.c_function_name, coerced, desc.return_type, desc.steals, desc.error_kind, line, var_arg_idx)) if result_type and not is_runtime_subtype(target.type, result_type): if is_none_rprimitive(result_type): # Special case None return. The actual result may actually be a bool # and so we can't just coerce it. target = self.none() else: target = self.coerce(target, result_type, line) return target
def call_c(self, desc: CFunctionDescription, args: List[Value], line: int, result_type: Optional[RType] = None) -> Value: # handle void function via singleton RVoid instance coerced = [] # coerce fixed number arguments for i in range(min(len(args), len(desc.arg_types))): formal_type = desc.arg_types[i] arg = args[i] arg = self.coerce(arg, formal_type, line) coerced.append(arg) # reorder args if necessary if desc.ordering is not None: assert desc.var_arg_type is None coerced = [coerced[i] for i in desc.ordering] # coerce any var_arg var_arg_idx = -1 if desc.var_arg_type is not None: var_arg_idx = len(desc.arg_types) for i in range(len(desc.arg_types), len(args)): arg = args[i] arg = self.coerce(arg, desc.var_arg_type, line) coerced.append(arg) # add extra integer constant if any for item in desc.extra_int_constants: val, typ = item extra_int_constant = self.add(LoadInt(val, line, rtype=typ)) coerced.append(extra_int_constant) target = self.add( CallC(desc.c_function_name, coerced, desc.return_type, desc.steals, desc.is_borrowed, desc.error_kind, line, var_arg_idx)) if desc.truncated_type is None: result = target else: truncate = self.add( Truncate(target, desc.return_type, desc.truncated_type)) result = truncate if result_type and not is_runtime_subtype(result.type, result_type): if is_none_rprimitive(result_type): # Special case None return. The actual result may actually be a bool # and so we can't just coerce it. result = self.none() else: result = self.coerce(target, result_type, line) return result
def assert_emit_binary_op(self, op: str, dest: Value, left: Value, right: Value, expected: str) -> None: if op in binary_ops: ops = binary_ops[op] for desc in ops: if (is_subtype(left.type, desc.arg_types[0]) and is_subtype(right.type, desc.arg_types[1])): args = [left, right] if desc.ordering is not None: args = [args[i] for i in desc.ordering] self.assert_emit( CallC(desc.c_function_name, args, desc.return_type, desc.steals, desc.is_borrowed, desc.error_kind, 55), expected) return else: assert False, 'Could not find matching op'
def call_c(self, desc: CFunctionDescription, args: List[Value], line: int, result_type: Optional[RType] = None) -> Value: # handle void function via singleton RVoid instance coerced = [] for i, arg in enumerate(args): formal_type = desc.arg_types[i] arg = self.coerce(arg, formal_type, line) coerced.append(arg) target = self.add(CallC(desc.c_function_name, coerced, desc.return_type, desc.steals, desc.error_kind, line)) if result_type and not is_runtime_subtype(target.type, result_type): if is_none_rprimitive(result_type): # Special case None return. The actual result may actually be a bool # and so we can't just coerce it. target = self.none() else: target = self.coerce(target, result_type, line) return target
def test_new_dict(self) -> None: self.assert_emit( CallC(dict_new_op.c_function_name, [], dict_new_op.return_type, dict_new_op.steals, dict_new_op.is_borrowed, dict_new_op.error_kind, 1), """cpy_r_r0 = PyDict_New();""")
def test_dict_update(self) -> None: self.assert_emit( CallC(dict_update_op.c_function_name, [self.d, self.o], dict_update_op.return_type, dict_update_op.steals, dict_update_op.is_borrowed, dict_update_op.error_kind, 1), """cpy_r_r0 = CPyDict_Update(cpy_r_d, cpy_r_o);""")
def test_list_append(self) -> None: self.assert_emit( CallC(list_append_op.c_function_name, [self.l, self.o], list_append_op.return_type, list_append_op.steals, list_append_op.is_borrowed, list_append_op.error_kind, 1), """cpy_r_r0 = PyList_Append(cpy_r_l, cpy_r_o);""")
def test_list_get_item(self) -> None: self.assert_emit( CallC(list_get_item_op.c_function_name, [self.m, self.k], list_get_item_op.return_type, list_get_item_op.steals, list_get_item_op.is_borrowed, list_get_item_op.error_kind, 55), """cpy_r_r0 = CPyList_GetItem(cpy_r_m, cpy_r_k);""")
def test_int_neg(self) -> None: self.assert_emit( CallC(int_neg_op.c_function_name, [self.m], int_neg_op.return_type, int_neg_op.steals, int_neg_op.is_borrowed, int_neg_op.is_borrowed, int_neg_op.error_kind, 55), "cpy_r_r0 = CPyTagged_Negate(cpy_r_m);")
def call_c(self, function_name: str, args: List[Value], line: int, result_type: Optional[RType]) -> Value: # handle void function via singleton RVoid instance ret_type = void_rtype if result_type is None else result_type target = self.add(CallC(function_name, args, ret_type, line)) return target