static struct expr_val cgasm_handle_ptr_add(struct cgasm_context *ctx, struct expr_val lhs, struct expr_val rhs) { lhs = cgasm_handle_deref_flag(ctx, lhs); rhs = cgasm_handle_deref_flag(ctx, rhs); assert(lhs.ctype->tag == T_PTR || rhs.ctype->tag == T_PTR); if (lhs.ctype->tag == T_PTR && rhs.ctype->tag == T_PTR) { panic("add ptr to ptr is not valid"); } if (lhs.ctype->tag != T_PTR) { struct expr_val tmp = lhs; lhs = rhs; rhs = tmp; } if (!is_integer_type(rhs.ctype)) { panic("ptr is only allowed to add with integer type"); } struct type *ptrtype = lhs.ctype; struct type *subtype = ptrtype->subtype; lhs.ctype = get_int_type(); // convert to int struct expr_val res; if (subtype->tag != T_VOID && type_get_size(subtype) != 1) { rhs = cgasm_handle_binary_op(ctx, TOK_STAR, rhs, int_const_expr_val(type_get_size(subtype))); } res = cgasm_handle_binary_op(ctx, TOK_ADD, lhs, rhs); res = cgasm_handle_deref_flag(ctx, res); res.ctype = ptrtype; return res; }
int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) { _cleanup_netlink_message_unref_ sd_netlink_message *m = NULL; const NLType *nl_type; size_t size; int r; r = type_system_get_type(&type_system_root, &nl_type, type); if (r < 0) return r; if (type_get_type(nl_type) != NETLINK_TYPE_NESTED) return -EINVAL; r = message_new_empty(rtnl, &m); if (r < 0) return r; size = NLMSG_SPACE(type_get_size(nl_type)); assert(size >= sizeof(struct nlmsghdr)); m->hdr = malloc0(size); if (!m->hdr) return -ENOMEM; m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; type_get_type_system(nl_type, &m->containers[0].type_system); m->hdr->nlmsg_len = size; m->hdr->nlmsg_type = type; *ret = m; m = NULL; return 0; }
static void gfortran_7_or_less_array_descriptor_info(_size_t *size, _size_t *align, int rank) { _size_t base_addr_size = CURRENT_CONFIGURATION->type_environment->sizeof_pointer; _size_t base_addr_align = CURRENT_CONFIGURATION->type_environment->alignof_pointer; _size_t offset_size = type_get_size(CURRENT_CONFIGURATION->type_environment->type_of_sizeof()); _size_t offset_align = type_get_alignment(CURRENT_CONFIGURATION->type_environment->type_of_sizeof()); _size_t dtype_size = type_get_size(CURRENT_CONFIGURATION->type_environment->type_of_ptrdiff_t()); _size_t dtype_align = type_get_alignment(CURRENT_CONFIGURATION->type_environment->type_of_ptrdiff_t()); _size_t descriptor_dimension_size = 3 * type_get_size(CURRENT_CONFIGURATION->type_environment->type_of_ptrdiff_t()); _size_t descriptor_dimension_align = type_get_alignment(CURRENT_CONFIGURATION->type_environment->type_of_ptrdiff_t()); _size_t dim_size = rank * descriptor_dimension_size; _size_t dim_align = descriptor_dimension_align; *size = base_addr_size + offset_size + dtype_size + dim_size; *align = MAX(base_addr_align, MAX(offset_align, MAX(dtype_align, MAX(dim_align, 0)))); }
unsigned int Type::get_size() const { unsigned int result; if (is_generic_vector_type(_type_info)) { result = this->basic_type().get_size(); } else { result = (unsigned int) type_get_size(_type_info); } return result; }
int sd_netlink_message_rewind(sd_netlink_message *m) { const NLType *nl_type; uint16_t type; size_t size; unsigned i; int r; assert_return(m, -EINVAL); /* don't allow appending to message once parsed */ if (!m->sealed) rtnl_message_seal(m); for (i = 1; i <= m->n_containers; i++) m->containers[i].attributes = mfree(m->containers[i].attributes); m->n_containers = 0; if (m->containers[0].attributes) /* top-level attributes have already been parsed */ return 0; assert(m->hdr); r = type_system_get_type(&type_system_root, &nl_type, m->hdr->nlmsg_type); if (r < 0) return r; type = type_get_type(nl_type); size = type_get_size(nl_type); if (type == NETLINK_TYPE_NESTED) { const NLTypeSystem *type_system; type_get_type_system(nl_type, &type_system); m->containers[0].type_system = type_system; r = netlink_container_parse(m, &m->containers[m->n_containers], type_system_get_count(type_system), (struct rtattr*)((uint8_t*)NLMSG_DATA(m->hdr) + NLMSG_ALIGN(size)), NLMSG_PAYLOAD(m->hdr, size)); if (r < 0) return r; } return 0; }
static struct expr_val cgasm_handle_ptr_sub(struct cgasm_context *ctx, struct expr_val lhs, struct expr_val rhs) { lhs = cgasm_handle_deref_flag(ctx, lhs); rhs = cgasm_handle_deref_flag(ctx, rhs); if (lhs.ctype->tag == T_PTR && is_integer_type(rhs.ctype)) { struct type *oldtype = lhs.ctype; struct type *subtype = lhs.ctype->subtype; if (subtype->tag != T_VOID && type_get_size(subtype) != 1) { panic("non unit ptr"); } lhs.ctype = get_int_type(); struct expr_val res = cgasm_handle_binary_op(ctx, TOK_SUB, lhs, rhs); res = cgasm_handle_deref_flag(ctx, res); res.ctype = oldtype; return res; } if (lhs.ctype->tag == T_PTR && rhs.ctype->tag == T_PTR) { if (!type_eq(lhs.ctype->subtype, rhs.ctype->subtype)) { panic("incompatible pointer types"); } struct type *elem_type = lhs.ctype->subtype; lhs.ctype = get_int_type(); rhs.ctype = get_int_type(); struct expr_val res = cgasm_handle_binary_op(ctx, TOK_SUB, lhs, rhs); int elem_size = type_get_size(elem_type); if (elem_type->tag != T_VOID && elem_size != 1) { res = cgasm_handle_binary_op(ctx, TOK_DIV, res, int_const_expr_val(elem_size)); } return res; } panic("invalid ptr subtraction"); }
static int message_attribute_has_type(sd_netlink_message *m, size_t *out_size, uint16_t attribute_type, uint16_t data_type) { const NLType *type; int r; assert(m); r = type_system_get_type(m->containers[m->n_containers].type_system, &type, attribute_type); if (r < 0) return r; if (type_get_type(type) != data_type) return -EINVAL; if (out_size) *out_size = type_get_size(type); return 0; }
const char* fortran_print_type_str(type_t* t) { t = no_ref(t); if (is_error_type(t)) { return "<error-type>"; } if (is_hollerith_type(t)) { return "HOLLERITH"; } const char* result = ""; char is_pointer = 0; if (is_pointer_type(t)) { is_pointer = 1; t = pointer_type_get_pointee_type(t); } struct array_spec_tag { nodecl_t lower; nodecl_t upper; char is_undefined; } array_spec_list[MCXX_MAX_ARRAY_SPECIFIER] = { { nodecl_null(), nodecl_null(), 0 } }; int array_spec_idx; for (array_spec_idx = MCXX_MAX_ARRAY_SPECIFIER - 1; fortran_is_array_type(t); array_spec_idx--) { if (array_spec_idx < 0) { internal_error("too many array dimensions %d\n", MCXX_MAX_ARRAY_SPECIFIER); } if (!array_type_is_unknown_size(t)) { array_spec_list[array_spec_idx].lower = array_type_get_array_lower_bound(t); array_spec_list[array_spec_idx].upper = array_type_get_array_upper_bound(t); } else { array_spec_list[array_spec_idx].is_undefined = 1; } t = array_type_get_element_type(t); } char is_array = (array_spec_idx != (MCXX_MAX_ARRAY_SPECIFIER - 1)); if (is_bool_type(t) || is_integer_type(t) || is_floating_type(t) || is_double_type(t) || is_complex_type(t)) { const char* type_name = NULL; char c[128] = { 0 }; if (is_bool_type(t)) { type_name = "LOGICAL"; } else if (is_integer_type(t)) { type_name = "INTEGER"; } else if (is_floating_type(t)) { type_name = "REAL"; } else if (is_complex_type(t)) { type_name = "COMPLEX"; } else { internal_error("unreachable code", 0); } size_t size = type_get_size(t); if (is_floating_type(t)) { // KIND of floats is their size in byes (using the bits as in IEEE754) size = (floating_type_get_info(t)->bits) / 8; } else if (is_complex_type(t)) { // KIND of a complex is the KIND of its component type type_t* f = complex_type_get_base_type(t); size = (floating_type_get_info(f)->bits) / 8; } snprintf(c, 127, "%s(%zd)", type_name, size); c[127] = '\0'; result = uniquestr(c); } else if (is_class_type(t)) { scope_entry_t* entry = named_type_get_symbol(t); char c[128] = { 0 }; snprintf(c, 127, "TYPE(%s)", entry->symbol_name); c[127] = '\0'; result = uniquestr(c); } else if (fortran_is_character_type(t)) { nodecl_t length = array_type_get_array_size_expr(t); char c[128] = { 0 }; snprintf(c, 127, "CHARACTER(LEN=%s)", nodecl_is_null(length) ? "*" : codegen_to_str(length, nodecl_retrieve_context(length))); c[127] = '\0'; result = uniquestr(c); } else if (is_function_type(t)) { result = "PROCEDURE"; } else { const char* non_printable = NULL; uniquestr_sprintf(&non_printable, "non-fortran type '%s'", print_declarator(t)); return non_printable; } if (is_pointer) { result = strappend(result, ", POINTER"); } if (is_array) { array_spec_idx++; result = strappend(result, ", DIMENSION("); while (array_spec_idx <= (MCXX_MAX_ARRAY_SPECIFIER - 1)) { if (!array_spec_list[array_spec_idx].is_undefined) { result = strappend(result, codegen_to_str(array_spec_list[array_spec_idx].lower, nodecl_retrieve_context(array_spec_list[array_spec_idx].lower))); result = strappend(result, ":"); result = strappend(result, codegen_to_str(array_spec_list[array_spec_idx].upper, nodecl_retrieve_context(array_spec_list[array_spec_idx].upper))); } else { result = strappend(result, ":"); } if ((array_spec_idx + 1) <= (MCXX_MAX_ARRAY_SPECIFIER - 1)) { result = strappend(result, ", "); } array_spec_idx++; } result = strappend(result, ")"); } return result; }
void cgasm_copy_bytes_to_temp(struct cgasm_context *ctx, int from_base_reg, int from_start_off, struct expr_val temp) { assert(temp.type == EXPR_VAL_TEMP); struct type *type = expr_val_get_type(temp); cgasm_copy_bytes(ctx, from_base_reg, from_start_off, REG_EBP, cgasm_get_temp_var_offset(ctx, temp.temp_var), type_get_size(type)); }
static Symbol create_new_function_opencl_allocate( Nodecl::NodeclBase expr_stmt, Symbol subscripted_symbol, Type element_type, int num_dimensions, bool is_allocatable) { std::string alloca_or_pointer = is_allocatable ? "ALLOCATABLE" : "POINTER"; TL::Source dummy_arguments_bounds, dimension_attr, allocate_dims; dimension_attr << "DIMENSION("; for (int i = 1; i <= num_dimensions; ++i) { if (i != 1) { allocate_dims << ", "; dummy_arguments_bounds <<", "; dimension_attr << ", "; } dummy_arguments_bounds <<"LB" << i <<", " << "UB" << i; dimension_attr << ":"; allocate_dims << "LB" << i << ":" << "UB" << i; } dimension_attr << ")"; size_t size_of_array_descriptor = fortran_size_of_array_descriptor( fortran_get_rank0_type(subscripted_symbol.get_type().get_internal_type()), fortran_get_rank_of_type(subscripted_symbol.get_type().get_internal_type())); TL::Source new_function_name; new_function_name << "NANOX_OPENCL_ALLOCATE_INTERNAL_" << (ptrdiff_t) subscripted_symbol.get_internal_symbol() ; Nodecl::NodeclBase nodecl_body; TL::Source new_function; new_function << "SUBROUTINE " << new_function_name << "(ARR, " << dummy_arguments_bounds << ")\n" << as_type(element_type) << ", " << dimension_attr << ", " << alloca_or_pointer << " :: ARR\n" << as_type(element_type) << ", " << dimension_attr << ", ALLOCATABLE :: TMP\n" << "INTEGER :: " << dummy_arguments_bounds << "\n" << "INTEGER :: ERR \n" << "ALLOCATE(TMP(" << allocate_dims << "))\n" << statement_placeholder(nodecl_body) << "DEALLOCATE(TMP)\n" << "END SUBROUTINE " << new_function_name << "\n" ; Nodecl::NodeclBase function_code = new_function.parse_global(expr_stmt.retrieve_context().get_global_scope()); TL::Scope inside_function = ReferenceScope(nodecl_body).get_scope(); TL::Symbol new_function_sym = inside_function.get_symbol_from_name(strtolower(new_function_name.get_source().c_str())); TL::Symbol arr_sym = inside_function.get_symbol_from_name("arr"); TL::Symbol tmp_sym = inside_function.get_symbol_from_name("tmp"); TL::Symbol ptr_of_arr_sym = get_function_ptr_of(arr_sym, inside_function); TL::Symbol ptr_of_tmp_sym = get_function_ptr_of(tmp_sym, inside_function); TL::Source aux; aux << "ERR = NANOS_MEMCPY(" << ptr_of_arr_sym.get_name() << "(ARR)," << ptr_of_tmp_sym.get_name() << "(TMP)," << "INT(" << size_of_array_descriptor << "," << type_get_size(get_ptrdiff_t_type()) << "))\n" << "CALL NANOS_OPENCL_ALLOCATE_FORTRAN(" << "SIZEOF(TMP)," << ptr_of_arr_sym.get_name() << "(ARR))\n" ; nodecl_body.replace(aux.parse_statement(inside_function)); Nodecl::Utils::prepend_to_enclosing_top_level_location(expr_stmt, function_code); return new_function_sym; }