This patch applies the rule that functions defined in FOO.c must be declared in FOO.h. One of the worst offenders in the code base is tree.h, unsurprisingly. The patch creates several new headers: attribs.h calls.h fold-const.h gcc-symtab.h print-rtl.h print-tree.h stmt.h stor-layout.h stringpool.h tree-nested.h tree-object-size.h varasm.h. Functions in each corresponding .c file got moved to those headers and others that already existed. I wanted to make this patch as mechanical as possible, so I made no attempt to fix problems like having build_addr defined in tree-inline.c. I left that for later. There were some declarations that I could not move out of tree.h because of header poisoning or the use of target macros. We forbid the inclusion of things like expr.h from FE files. While that's a reasonable idea, the FE file *still* manage to at expr.c functionality because the declarations they want to use were defined in tree.h. The affected files are builtins.h, emit-rtl.h and expr.h. If that functionality is allowed to be accessed from the FEs, then I will later move those functions out of expr.c into tree.c. I have moved these declarations to the bottom of tree.h so they are easy to identify later. There is a namespace collision with libcpp. The file gcc/symtab.c cannot use gcc/symtab.h because the #include command picks up libcpp/include/symtab.h first. So I named this file gcc-symtab.h for now. Finally, I added a new header to PLUGIN_HEADERS to account for the tree.h refactoring. I did not add all headers factored out of tree.h because it is unclear (and impossible to tell) what plugins need. This adds the one header used by the plugins in the testsuite. This will be changing quite dramatically as we progress with the header refactoring. This patch should offer some minimal incremental build advantages by reducing the size of tree.h. Changes that would otherwise affected tree.h, will now go to other headers which are less frequently included. * tree.h: Include fold-const.h. (aggregate_value_p): Moved to function.h. (alloca_call_p): Moved to calls.h. (allocate_struct_function): Moved to function.h. (apply_tm_attr): Moved to attribs.h. (array_at_struct_end_p): Moved to expr.h. (array_ref_element_size): Moved to tree-dfa.h. (array_ref_low_bound): Moved to tree-dfa.h. (array_ref_up_bound): Moved to tree.h. (assemble_alias): Moved to cgraph.h. (bit_from_pos): Moved to stor-layout.h. (build_addr): Moved to tree-nested.h. (build_duplicate_type): Moved to tree-inline.h. (build_fold_addr_expr): Moved to fold-const.h. (build_fold_addr_expr_with_type): Moved to fold-const.h. (build_fold_addr_expr_with_type_loc): Moved to fold-const.h. (build_fold_indirect_ref): Moved to fold-const.h. (build_fold_indirect_ref_loc): Moved to fold-const.h. (build_personality_function): Moved to tree.h. (build_range_check): Moved to fold-const.h. (build_simple_mem_ref): Moved to fold-const.h. (build_simple_mem_ref_loc): Moved to fold-const.h. (build_tm_abort_call): Moved to trans-mem.h. (byte_from_pos): Moved to stor-layout.h. (call_expr_flags): Moved to calls.h. (can_move_by_pieces): Moved to expr.h. (categorize_ctor_elements): Moved to expr.h. (change_decl_assembler_name): Moved to gcc-symtab.h. (combine_comparisons): Moved to fold-const.h. (complete_ctor_at_level_p): Moved to tree.h. (component_ref_field_offset): Moved to tree-dfa.h. (compute_builtin_object_size): Moved to tree-object-size.h. (compute_record_mode): Moved to stor-layout.h. (constant_boolean_node): Moved to fold-const.h. (constructor_static_from_elts_p): Moved to varasm.h. (cxx11_attribute_p): Moved to attribs.h. (debug_body): Moved to print-tree.h. (debug_find_tree): Moved to tree-inline.h. (debug_fold_checksum): Moved to fold-const.h. (debug_head): Moved to print-tree.h. (debug_head): Moved to print-tree.h. (debug_raw): Moved to print-tree.h. (debug_tree): Moved to print-tree.h. (debug_vec_tree): Moved to print-tree.h. (debug_verbose): Moved to print-tree.h. (debug_verbose): Moved to print-tree.h. (decl_attributes): Moved to attribs.h. (decl_binds_to_current_def_p): Moved to varasm.h. (decl_default_tls_model): Moved to varasm.h. (decl_replaceable_p): Moved to varasm.h. (div_if_zero_remainder): Moved to fold-const.h. (double_int mem_ref_offset): Moved to fold-const.h. (dump_addr): Moved to print-tree.h. (element_precision): Moved to machmode.h. (expand_dummy_function_end): Moved to function.h. (expand_function_end): Moved to function.h. (expand_function_start): Moved to function.h. (expand_label): Moved to stmt.h. (expr_first): Moved to tree-iterator.h. (expr_last): Moved to tree-iterator.h. (finalize_size_functions): Moved to stor-layout.h. (finish_builtin_struct): Moved to stor-layout.h. (finish_record_layout): Moved to stor-layout.h. (fixup_signed_type): Moved to stor-layout.h. (fixup_unsigned_type): Moved to stor-layout.h. (flags_from_decl_or_type): Moved to calls.h. (fold): Moved to fold-const.h. (fold_abs_const): Moved to fold-const.h. (fold_binary): Moved to fold-const.h. (fold_binary_loc): Moved to fold-const.h. (fold_binary_to_constant): Moved to fold-const.h. (fold_build1): Moved to fold-const.h. (fold_build1_initializer_loc): Moved to fold-const.h. (fold_build1_loc): Moved to fold-const.h. (fold_build1_stat_loc): Moved to fold-const.h. (fold_build2): Moved to fold-const.h. (fold_build2_initializer_loc): Moved to fold-const.h. (fold_build2_loc): Moved to fold-const.h. (fold_build2_stat_loc): Moved to fold-const.h. (fold_build3): Moved to fold-const.h. (fold_build3_loc): Moved to fold-const.h. (fold_build3_stat_loc): Moved to fold-const.h. (fold_build_call_array): Moved to fold-const.h. (fold_build_call_array_initializer): Moved to fold-const.h. (fold_build_call_array_initializer_loc): Moved to fold-const.h. (fold_build_call_array_loc): Moved to fold-const.h. (fold_build_cleanup_point_expr): Moved to fold-const.h. (fold_convert): Moved to fold-const.h. (fold_convert_loc): Moved to fold-const.h. (fold_convertible_p): Moved to fold-const.h. (fold_defer_overflow_warnings): Moved to fold-const.h. (fold_deferring_overflow_warnings_p): Moved to fold-const.h. (fold_fma): Moved to fold-const.h. (fold_ignored_result): Moved to fold-const.h. (fold_indirect_ref): Moved to fold-const.h. (fold_indirect_ref_1): Moved to fold-const.h. (fold_indirect_ref_loc): Moved to fold-const.h. (fold_read_from_constant_string): Moved to fold-const.h. (fold_real_zero_addition_p): Moved to fold-const.h. (fold_single_bit_test): Moved to fold-const.h. (fold_strip_sign_ops): Moved to fold-const.h. (fold_ternary): Moved to fold-const.h. (fold_ternary_loc): Moved to fold-const.h. (fold_unary): Moved to tree-data-ref.h. (fold_unary_ignore_overflow): Moved to fold-const.h. (fold_unary_ignore_overflow_loc): Moved to fold-const.h. (fold_unary_loc): Moved to fold-const.h. (fold_unary_to_constant): Moved to fold-const.h. (fold_undefer_and_ignore_overflow_warnings): Moved to fold-const.h. (fold_undefer_overflow_warnings): Moved to fold-const.h. (folding_initializer): Moved to fold-const.h. (free_temp_slots): Moved to function.h. (generate_setjmp_warnings): Moved to function.h. (get_attribute_name): Moved to attribs.h. (get_identifier): Moved to stringpool.h. (get_identifier_with_length): Moved to stringpool.h. (get_inner_reference): Moved to tree.h. (gimple_alloca_call_p): Moved to calls.h. (gimplify_parameters): Moved to function.h. (highest_pow2_factor): Moved to expr.h. (indent_to): Moved to print-tree.h. (init_attributes): Moved to attribs.h. (init_dummy_function_start): Moved to function.h. (init_function_start): Moved to function.h. (init_inline_once): Moved to tree-inline.h. (init_object_sizes): Moved to tree-object-size.h. (init_temp_slots): Moved to function.h. (init_tree_optimization_optabs): Moved to optabs.h. (initialize_sizetypes): Moved to stor-layout.h. (initializer_constant_valid_for_bitfield_p): Moved to varasm.h. (initializer_constant_valid_p): Moved to varasm.h. (int_const_binop): Moved to fold-const.h. (internal_reference_types): Moved to stor-layout.h. (invert_tree_comparison): Moved to fold-const.h. (invert_truthvalue): Moved to fold-const.h. (invert_truthvalue_loc): Moved to fold-const.h. (is_tm_ending_fndecl): Moved to trans-mem.h. (is_tm_may_cancel_outer): Moved to trans-mem.h. (is_tm_pure): Moved to trans-mem.h. (is_tm_safe): Moved to trans-mem.h. (layout_decl): Moved to stor-layout.h. (layout_type): Moved to stor-layout.h. (lookup_attribute_spec): Moved to attribs.h. (make_accum_type): Moved to stor-layout.h. (make_decl_one_only): Moved to varasm.h. (make_decl_rtl): Moved to tree.h. (make_decl_rtl_for_debug): Moved to varasm.h. (make_fract_type): Moved to stor-layout.h. (make_or_reuse_sat_signed_accum_type): Moved to stor-layout.h. (make_or_reuse_sat_signed_fract_type): Moved to stor-layout.h. (make_or_reuse_sat_unsigned_accum_type): Moved to stor-layout.h. (make_or_reuse_sat_unsigned_fract_type): Moved to stor-layout.h. (make_or_reuse_signed_accum_type): Moved to stor-layout.h. (make_or_reuse_signed_fract_type): Moved to stor-layout.h. (make_or_reuse_unsigned_accum_type): Moved to stor-layout.h. (make_or_reuse_unsigned_fract_type): Moved to stor-layout.h. (make_range): Moved to fold-const.h. (make_range_step): Moved to fold-const.h. (make_sat_signed_accum_type): Moved to stor-layout.h. (make_sat_signed_fract_type): Moved to stor-layout.h. (make_sat_unsigned_accum_type): Moved to stor-layout.h. (make_sat_unsigned_fract_type): Moved to stor-layout.h. (make_signed_accum_type): Moved to stor-layout.h. (make_signed_fract_type): Moved to stor-layout.h. (make_signed_type): Moved to stor-layout.h. (make_unsigned_accum_type): Moved to stor-layout.h. (make_unsigned_fract_type): Moved to stor-layout.h. (make_unsigned_type): Moved to stor-layout.h. (mark_decl_referenced): Moved to varasm.h. (mark_referenced): Moved to varasm.h. (may_negate_without_overflow_p): Moved to fold-const.h. (maybe_get_identifier): Moved to stringpool.h. (merge_ranges): Moved to fold-const.h. (merge_weak): Moved to varasm.h. (mode_for_size_tree): Moved to stor-layout.h. (multiple_of_p): Moved to fold-const.h. (must_pass_in_stack_var_size): Moved to calls.h. (must_pass_in_stack_var_size_or_pad): Moved to calls.h. (native_encode_expr): Moved to fold-const.h. (native_interpret_expr): Moved to fold-const.h. (non_lvalue): Moved to fold-const.h. (non_lvalue_loc): Moved to fold-const.h. (normalize_offset): Moved to stor-layout.h. (normalize_rli): Moved to stor-layout.h. (notice_global_symbol): Moved to varasm.h. (omit_one_operand): Moved to fold-const.h. (omit_one_operand_loc): Moved to fold-const.h. (omit_two_operands): Moved to fold-const.h. (omit_two_operands_loc): Moved to fold-const.h. (operand_equal_p): Moved to tree-data-ref.h. (parse_input_constraint): Moved to stmt.h. (parse_output_constraint): Moved to stmt.h. (place_field): Moved to stor-layout.h. (pop_function_context): Moved to function.h. (pop_temp_slots): Moved to function.h. (pos_from_bit): Moved to stor-layout.h. (preserve_temp_slots): Moved to function.h. (print_node): Moved to print-tree.h. (print_node_brief): Moved to print-tree.h. (print_rtl): Moved to rtl.h. (process_pending_assemble_externals): Moved to varasm.h. (ptr_difference_const): Moved to fold-const.h. (push_function_context): Moved to function.h. (push_struct_function): Moved to function.h. (push_temp_slots): Moved to function.h. (record_tm_replacement): Moved to trans-mem.h. (relayout_decl): Moved to stor-layout.h. (resolve_asm_operand_names): Moved to stmt.h. (resolve_unique_section): Moved to varasm.h. (rli_size_so_far): Moved to stor-layout.h. (rli_size_unit_so_far): Moved to stor-layout.h. (round_down): Moved to fold-const.h. (round_down_loc): Moved to fold-const.h. (round_up): Moved to fold-const.h. (round_up_loc): Moved to fold-const.h. (set_decl_incoming_rtl): Moved to emit-rtl.h. (set_decl_rtl): Moved to tree.h. (set_min_and_max_values_for_integral_type): Moved to stor-layout.h. (set_user_assembler_name): Moved to varasm.h. (setjmp_call_p): Moved to calls.h. (size_binop): Moved to fold-const.h. (size_binop_loc): Moved to fold-const.h. (size_diffop): Moved to fold-const.h. (size_diffop_loc): Moved to fold-const.h. (size_int_kind): Moved to fold-const.h. (stack_protect_epilogue): Moved to function.h. (start_record_layout): Moved to stor-layout.h. (supports_one_only): Moved to varasm.h. (swap_tree_comparison): Moved to fold-const.h. (tm_malloc_replacement): Moved to trans-mem.h. (tree build_fold_addr_expr_loc): Moved to fold-const.h. (tree build_invariant_address): Moved to fold-const.h. (tree_binary_nonnegative_warnv_p): Moved to fold-const.h. (tree_binary_nonzero_warnv_p): Moved to fold-const.h. (tree_call_nonnegative_warnv_p): Moved to fold-const.h. (tree_expr_nonnegative_p): Moved to fold-const.h. (tree_expr_nonnegative_warnv_p): Moved to fold-const.h. (tree_output_constant_def): Moved to varasm.h. (tree_overlaps_hard_reg_set): Moved to stmt.h. (tree_single_nonnegative_warnv_p): Moved to fold-const.h. (tree_single_nonzero_warnv_p): Moved to fold-const.h. (tree_swap_operands_p): Moved to fold-const.h. (tree_unary_nonnegative_warnv_p): Moved to fold-const.h. (tree_unary_nonzero_warnv_p): Moved to fold-const.h. (update_alignment_for_field): Moved to stor-layout.h. (use_register_for_decl): Moved to function.h. (variable_size): Moved to rtl.h. (vector_type_mode): Moved to stor-layout.h. * cgraph.h: Corresponding changes. * emit-rtl.h: Corresponding changes. * expr.h: Corresponding changes. * function.h: Corresponding changes. * optabs.h: Corresponding changes. * trans-mem.h: Corresponding changes. Protect against multiple inclusion. * tree-inline.h: Corresponding changes. * tree-iterator.h: Corresponding changes. * tree-dfa.h: Include expr.h. * tree-ssanames.h: Include stringpool.h. * attribs.h: New file. * calls.h: New file. * fold-const.h: New file. * gcc-symtab.h: New file. * print-rtl.h: New file. * print-tree.h: New file. * stmt.h: New file. * stor-layout.h: New file. * strinpool.h: New file. * tree-nested.h: New file * tree-object-size.h: New file. * varasm.h: New file. * Makefile.in (PLUGIN_HEADERS): Add stringpool.h. * alias.c: Include varasm.h. Include expr.h. * asan.c: Include calls.h. Include stor-layout.h. Include varasm.h. * attribs.c: Include stringpool.h. Include attribs.h. Include stor-layout.h. * builtins.c: Include stringpool.h. Include stor-layout.h. Include calls.h. Include varasm.h. Include tree-object-size.h. * calls.c: Include stor-layout.h. Include varasm.h. Include stringpool.h. Include attribs.h. * cfgexpand.c: Include stringpool.h. Include varasm.h. Include stor-layout.h. Include stmt.h. Include print-tree.h. * cgraph.c: Include varasm.h. Include calls.h. Include print-tree.h. * cgraphclones.c: Include stringpool.h. Include function.h. Include emit-rtl.h. Move inclusion of rtl.h earlier in the file. * cgraphunit.c: Include varasm.h. Include stor-layout.h. Include stringpool.h. * cilk-common.c: Include stringpool.h. Include stor-layout.h. * combine.c: Include stor-layout.h. * config/aarch64/aarch64-builtins.c: Include stor-layout.h. Include stringpool.h. Include calls.h. * config/aarch64/aarch64.c: Include stringpool.h. Include stor-layout.h. Include calls.h. Include varasm.h. * config/alpha/alpha.c: Include stor-layout.h. Include calls.h. Include varasm.h. * config/arc/arc.c: Include varasm.h. Include stor-layout.h. Include stringpool.h. Include calls.h. * config/arm/arm.c: Include stringpool.h. Include stor-layout.h. Include calls.h. Include varasm.h. * config/avr/avr-c.c: Include stor-layout.h. * config/avr/avr-log.c: Include print-tree.h. * config/avr/avr.c: Include print-tree.h. Include calls.h. Include stor-layout.h. Include stringpool.h. * config/bfin/bfin.c: Include varasm.h. Include calls.h. * config/c6x/c6x.c: Include stor-layout.h. Include varasm.h. Include calls.h. Include stringpool.h. * config/cr16/cr16.c: Include stor-layout.h. Include calls.h. * config/cris/cris.c: Include varasm.h. Include stor-layout.h. Include calls.h. Include stmt.h. * config/darwin.c: Include stringpool.h. Include varasm.h. Include stor-layout.h. * config/epiphany/epiphany.c: Include stor-layout.h. Include varasm.h. Include calls.h. Include stringpool.h. * config/fr30/fr30.c: Include stor-layout.h. Include varasm.h. * config/frv/frv.c: Include varasm.h. Include stor-layout.h. Include stringpool.h. * config/h8300/h8300.c: Include stor-layout.h. Include varasm.h. Include calls.h. Include stringpool.h. * config/i386/i386.c: Include stringpool.h. Include attribs.h. Include calls.h. Include stor-layout.h. Include varasm.h. * config/i386/winnt-cxx.c: Include stringpool.h. Include attribs.h. * config/i386/winnt.c: Include stringpool.h. Include varasm.h. * config/ia64/ia64-c.c: Include stringpool.h. * config/ia64/ia64.c: Include stringpool.h. Include stor-layout.h. Include calls.h. Include varasm.h. * config/iq2000/iq2000.c: Include stor-layout.h. Include calls.h. Include varasm.h. * config/lm32/lm32.c: Include calls.h. * config/m32c/m32c.c: Include stor-layout.h. Include varasm.h. Include calls.h. * config/m32r/m32r.c: Include stor-layout.h. Include varasm.h. Include stringpool.h. Include calls.h. * config/m68k/m68k.c: Include calls.h. Include stor-layout.h. Include varasm.h. * config/mcore/mcore.c: Include stor-layout.h. Include varasm.h. Include stringpool.h. Include calls.h. * config/mep/mep.c: Include varasm.h. Include calls.h. Include stringpool.h. Include stor-layout.h. * config/microblaze/microblaze.c: Include varasm.h. Include stor-layout.h. Include calls.h. * config/mips/mips.c: Include varasm.h. Include stringpool.h. Include stor-layout.h. Include calls.h. * config/mmix/mmix.c: Include varasm.h. Include stor-layout.h. Include calls.h. * config/mn10300/mn10300.c: Include stor-layout.h. Include varasm.h. Include calls.h. * config/moxie/moxie.c: Include stor-layout.h. Include varasm.h. Include calls.h. * config/msp430/msp430.c: Include stor-layout.h. Include calls.h. * config/nds32/nds32.c: Include stor-layout.h. Include varasm.h. Include calls.h. * config/pa/pa.c: Include stor-layout.h. Include stringpool.h. Include varasm.h. Include calls.h. * config/pdp11/pdp11.c: Include stor-layout.h. Include varasm.h. Include calls.h. * config/picochip/picochip.c: Include calls.h. Include stor-layout.h. Include stringpool.h. Include varasm.h. * config/rl78/rl78.c: Include varasm.h. Include stor-layout.h. Include calls.h. * config/rs6000/rs6000-c.c: Include stor-layout.h. Include stringpool.h. * config/rs6000/rs6000.c: Include stringpool.h. Include stor-layout.h. Include calls.h. Include print-tree.h. Include varasm.h. * config/rx/rx.c: Include varasm.h. Include stor-layout.h. Include calls.h. * config/s390/s390.c: Include print-tree.h. Include stringpool.h. Include stor-layout.h. Include varasm.h. Include calls.h. * config/score/score.c: Include stringpool.h. Include calls.h. Include varasm.h. Include stor-layout.h. * config/sh/sh-c.c: Include stringpool.h. Include attribs.h.h. * config/sh/sh.c: Include stringpool.h. Include stor-layout.h. Include calls.h. Include varasm.h. * config/sol2-c.c: Include stringpool.h. Include attribs.h. * config/sol2-cxx.c: Include stringpool.h. * config/sol2.c: Include stringpool.h. Include varasm.h. * config/sparc/sparc.c: Include stringpool.h. Include stor-layout.h. Include calls.h. Include varasm.h. * config/spu/spu-c.c: Include stringpool.h. * config/spu/spu.c: Include stringpool.h. Include stor-layout.h. Include calls.h. Include varasm.h. * config/stormy16/stormy16.c: Include stringpool.h. Include stor-layout.h. Include varasm.h. Include calls.h. * config/tilegx/tilegx.c: Include stringpool.h. Include stor-layout.h. Include varasm.h. Include calls.h. * config/tilepro/tilepro.c: Include stringpool.h. Include stor-layout.h. Include varasm.h. Include calls.h. * config/v850/v850-c.c: Include stringpool.h. Include attribs.h. * config/v850/v850.c: Include stringpool.h. Include stor-layout.h. Include varasm.h. Include calls.h. * config/vax/vax.c: Include calls.h. Include varasm.h. * config/vms/vms.c: Include stringpool.h. * config/vxworks.c: Include stringpool.h. * config/xtensa/xtensa.c: Include stringpool.h. Include stor-layout.h. Include calls.h. Include varasm.h. * convert.c: Include stor-layout.h. * coverage.c: Include stringpool.h. Include stor-layout.h. * dbxout.c: Include varasm.h. Include stor-layout.h. * dojump.c: Include stor-layout.h. * dse.c: Include stor-layout.h. * dwarf2asm.c: Include stringpool.h. Include varasm.h. * dwarf2cfi.c: Include stor-layout.h. * dwarf2out.c: Include rtl.h. Include stringpool.h. Include stor-layout.h. Include varasm.h. Include function.h. Include emit-rtl.h. Move inclusion of rtl.h earlier in the file. * emit-rtl.c: Include varasm.h. * except.c: Include stringpool.h. Include stor-layout.h. * explow.c: Include stor-layout.h. * expmed.c: Include stor-layout.h. * expr.c: Include stringpool.h. Include stor-layout.h. Include attribs.h. Include varasm.h. * final.c: Include varasm.h. * fold-const.c: Include stor-layout.h. Include calls.h. Include tree-iterator.h. * function.c: Include stor-layout.h. Include varasm.h. Include stringpool.h. * genattrtab.c (write_header): Emit includes for varasm.h, stor-layout.h and calls.h. * genautomata.c (main): Likewise. * genemit.c: Likewise. * genopinit.c: Likewise. * genoutput.c (output_prologue): Likewise. * genpeep.c: Likewise. * genpreds.c (write_insn_preds_c): Likewise. * gengtype.c (open_base_files): Add stringpool.h. * gimple-expr.c: Include stringpool.h. Include stor-layout.h. * gimple-fold.c: Include stringpool.h. Include expr.h. Include stmt.h. Include stor-layout.h. * gimple-low.c: Include tree-nested.h. Include calls.h. * gimple-pretty-print.c: Include stringpool.h. * gimple-ssa-strength-reduction.c: Include stor-layout.h. Include expr.h. * gimple-walk.c: Include stmt.h. * gimple.c: Include calls.h. Include stmt.h. Include stor-layout.h. * gimplify.c: Include stringpool.h. Include calls.h. Include varasm.h. Include stor-layout.h. Include stmt.h. Include print-tree.h. Include expr.h. * gimplify-me.c: Include stmt.h Include stor-layout.h * internal-fn.c: Include stor-layout.h. * ipa-devirt.c: Include print-tree.h. Include calls.h. * ipa-inline-analysis.c: Include stor-layout.h. Include stringpool.h. Include print-tree.h. * ipa-inline.c: Include trans-mem.h. Include calls.h. * ipa-prop.c: Include expr.h. Include stor-layout.h. Include print-tree.h. * ipa-pure-const.c: Include print-tree.h. Include calls.h. * ipa-reference.c: Include calls.h. * ipa-split.c: Include stringpool.h. Include expr.h. Include calls.h. * ipa.c: Include calls.h. Include stringpool.h. * langhooks.c: Include stringpool.h. Include attribs.h. * lto-cgraph.c: Include stringpool.h. * lto-streamer-in.c: Include stringpool.h. * lto-streamer-out.c: Include stor-layout.h. Include stringpool.h. * omp-low.c: Include stringpool.h. Include stor-layout.h. Include expr.h. * optabs.c: Include stor-layout.h. Include stringpool.h. Include varasm.h. * passes.c: Include varasm.h. * predict.c: Include calls.h. * print-rtl.c: Include print-tree.h. * print-tree.c: Include varasm.h. Include print-rtl.h. Include stor-layout.h. * realmpfr.c: Include stor-layout.h. * reg-stack.c: Include varasm.h. * sdbout.c: Include varasm.h. Include stor-layout.h. * simplify-rtx.c: Include varasm.h. * stmt.c: Include varasm.h. Include stor-layout.h. * stor-layout.c: Include stor-layout.h. Include stringpool.h. Include varasm.h. Include print-tree.h. * symtab.c: Include rtl.h. Include print-tree.h. Include varasm.h. Include function.h. Include emit-rtl.h. * targhooks.c: Include stor-layout.h. Include varasm.h. * toplev.c: Include varasm.h. Include tree-inline.h. * trans-mem.c: Include calls.h. Include function.h. Include rtl.h. Include emit-rtl.h. * tree-affine.c: Include expr.h. * tree-browser.c: Include print-tree.h. * tree-call-cdce.c: Include stor-layout.h. * tree-cfg.c: Include trans-mem.h. Include stor-layout.h. Include print-tree.h. * tree-complex.c: Include stor-layout.h. * tree-data-ref.c: Include expr.h. * tree-dfa.c: Include stor-layout.h. * tree-eh.c: Include expr.h. Include calls.h. * tree-emutls.c: Include stor-layout.h. Include varasm.h. * tree-if-conv.c: Include stor-layout.h. * tree-inline.c: Include stor-layout.h. Include calls.h. * tree-loop-distribution.c: Include stor-layout.h. * tree-nested.c: Include stringpool.h. Include stor-layout.h. * tree-object-size.c: Include tree-object-size.h. * tree-outof-ssa.c: Include stor-layout.h. * tree-parloops.c: Include stor-layout.h. Include tree-nested.h. * tree-pretty-print.c: Include stor-layout.h. Include expr.h. * tree-profile.c: Include varasm.h. Include tree-nested.h. * tree-scalar-evolution.c: Include expr.h. * tree-sra.c: Include stor-layout.h. * tree-ssa-address.c: Include stor-layout.h. * tree-ssa-ccp.c: Include stor-layout.h. * tree-ssa-dce.c: Include calls.h. * tree-ssa-dom.c: Include stor-layout.h. * tree-ssa-forwprop.c: Include stor-layout.h. * tree-ssa-ifcombine.c: Include stor-layout.h. * tree-ssa-loop-ivopts.c: Include stor-layout.h. * tree-ssa-loop-niter.c: Include calls.h. Include expr.h. * tree-ssa-loop-prefetch.c: Include stor-layout.h. * tree-ssa-math-opts.c: Include stor-layout.h. * tree-ssa-operands.c: Include stmt.h. Include print-tree.h. * tree-ssa-phiopt.c: Include stor-layout.h. * tree-ssa-reassoc.c: Include stor-layout.h. * tree-ssa-sccvn.c: Include stor-layout.h. * tree-ssa-sink.c: Include stor-layout.h. * tree-ssa-strlen.c: Include stor-layout.h. * tree-ssa-structalias.c: Include stor-layout.h. Include stmt.h. * tree-ssa-tail-merge.c: Include stor-layout.h. Include trans-mem.h. * tree-ssa-uncprop.c: Include stor-layout.h. * tree-ssa.c: Include stor-layout.h. * tree-ssanames.c: Include stor-layout.h. * tree-streamer-in.c: Include stringpool.h. * tree-streamer-out.c: Include stor-layout.h. * tree-switch-conversion.c: Include varasm.h. Include stor-layout.h. * tree-tailcall.c: Include stor-layout.h. * tree-vect-data-refs.c: Include stor-layout.h. * tree-vect-generic.c: Include stor-layout.h. * tree-vect-loop.c: Include stor-layout.h. * tree-vect-patterns.c: Include stor-layout.h. * tree-vect-slp.c: Include stor-layout.h. * tree-vect-stmts.c: Include stor-layout.h. * tree-vectorizer.c: Include stor-layout.h. * tree-vrp.c: Include stor-layout.h. Include calls.h. * tree.c: Include stor-layout.h. Include calls.h. Include attribs.h. Include varasm.h. * tsan.c: Include expr.h. * ubsan.c: Include stor-layout.h. Include stringpool.h. * value-prof.c: Include tree-nested.h. Include calls.h. * var-tracking.c: Include varasm.h. Include stor-layout.h. * varasm.c: Include stor-layout.h. Include stringpool.h. Include gcc-symtab.h. Include varasm.h. * varpool.c: Include varasm.h. * vmsdbgout.c: Include varasm.h. * xcoffout.c: Include varasm.h. ada/ChangeLog * gcc-interface/decl.c: Include stringpool.h Include stor-layout.h * gcc-interface/misc.c: Include stor-layout.h Include print-tree.h * gcc-interface/trans.c: Include stringpool.h Include stor-layout.h Include stmt.h Include varasm.h * gcc-interface/utils.c: Include stringpool.h Include stor-layout.h Include attribs.h Include varasm.h * gcc-interface/utils2.c: Include stringpool.h Include stor-layout.h Include attribs.h Include varasm.h c-family/ChangeLog * c-common.c: Include fold-const.h. Include stor-layout.h. Include calls.h. Include stringpool.h. Include attribs.h. Include varasm.h. Include trans-mem.h. * c-cppbuiltin.c: Include stor-layout.h. Include stringpool.h. * c-format.c: Include stringpool.h. * c-lex.c: Include stringpool.h. Include stor-layout.h. * c-pragma.c: Include stringpool.h. Include attribs.h. Include varasm.h. Include gcc-symtab.h. * c-pretty-print.c: Include stor-layout.h. Include attribs.h. * cilk.c: Include stringpool.h. Include calls.h. c/ChangeLog * c-decl.c: Include print-tree.h. Include stor-layout.h. Include varasm.h. Include attribs.h. Include stringpool.h. * c-lang.c: Include fold-const.h. * c-parser.c: Include stringpool.h. Include attribs.h. Include stor-layout.h. Include varasm.h. Include trans-mem.h. * c-typeck.c: Include stor-layout.h. Include trans-mem.h. Include varasm.h. Include stmt.h. cp/ChangeLog * call.c: Include stor-layout.h. Include trans-mem.h. Include stringpool.h. * class.c: Include stringpool.h. Include stor-layout.h. Include attribs.h. * cp-gimplify.c: Include stor-layout.h. * cvt.c: Include stor-layout.h. * decl.c: Include stringpool.h. Include stor-layout.h. Include varasm.h. Include attribs.h. Include calls.h. * decl2.c: Include stringpool.h. Include varasm.h. Include attribs.h. Include stor-layout.h. Include calls.h. * error.c: Include stringpool.h. * except.c: Include stringpool.h. Include trans-mem.h. Include attribs.h. * init.c: Include stringpool.h. Include varasm.h. * lambda.c: Include stringpool.h. * lex.c: Include stringpool.h. * mangle.c: Include stor-layout.h. Include stringpool.h. * method.c: Include stringpool.h. Include varasm.h. * name-lookup.c: Include stringpool.h. Include print-tree.h. Include attribs.h. * optimize.c: Include stringpool.h. * parser.c: Include print-tree.h. Include stringpool.h. Include attribs.h. Include trans-mem.h. * pt.c: Include stringpool.h. Include varasm.h. Include attribs.h. Include stor-layout.h. * ptree.c: Include print-tree.h. * repo.c: Include stringpool.h. * rtti.c: Include stringpool.h. Include stor-layout.h. * semantics.c: Include stmt.h. Include varasm.h. Include stor-layout.h. Include stringpool.h. * tree.c: Include stor-layout.h. Include print-tree.h. Include tree-iterator.h. * typeck.c: Include stor-layout.h. Include varasm.h. * typeck2.c: Include stor-layout.h. Include varasm.h. * vtable-class-hierarchy.c: Include stringpool.h. Include stor-layout.h. fortran/ChangeLog * decl.c: Include stringpool.h. * iresolve.c: Include stringpool.h. * match.c: Include stringpool.h. * module.c: Include stringpool.h. * target-memory.c: Include stor-layout.h. * trans-common.c: Include stringpool.h. Include stor-layout.h. Include varasm.h. * trans-const.c: Include stor-layout.h. * trans-decl.c: Include stringpool.h. Include stor-layout.h. Include varasm.h. Include attribs.h. * trans-expr.c: Include stringpool.h. * trans-intrinsic.c: Include stringpool.h. Include tree-nested.h. Include stor-layout.h. * trans-io.c: Include stringpool.h. Include stor-layout.h. * trans-openmp.c: Include stringpool.h. * trans-stmt.c: Include stringpool.h. * trans-types.c: Include stor-layout.h. Include stringpool.h. * trans.c: Include stringpool.h. go/ChangeLog * go-backend.c: Include stor-layout.h. * go-gcc.cc: Include stringpool.h. Include stor-layout.h. Include varasm.h. * go-lang.c: Include stor-layout.h. java/ChangeLog * builtins.c: Include stor-layout.h. Include stringpool.h. * class.c: Include stringpool.h. Include stor-layout.h. Include varasm.h. * constants.c: Include stringpool.h. Include stor-layout.h. * decl.c: Include stor-layout.h. Include stringpool.h. Include varasm.h. * except.c: Include stringpool.h. Include stor-layout.h. * expr.c: Include stringpool.h. Include stor-layout.h. * jcf-parse.c: Include stringpool.h. * mangle.c: Include stringpool.h. * resource.c: Include stringpool.h. Include stor-layout.h. * typeck.c: Include stor-layout.h. Include stringpool.h. * verify-glue.c: Include stringpool.h. lto/ChangeLog * lto-lang.c: Include stringpool.h. Include stor-layout.h. * lto-partition.c: Include gcc-symtab.h. * lto.c: Include stor-layout.h. objc/ChangeLog * objc-act.c: Include stringpool.h. Include stor-layout.h. Include attribs.h. * objc-encoding.c: Include stringpool.h. Include stor-layout.h. * objc-gnu-runtime-abi-01.c: Include stringpool.h. * objc-next-runtime-abi-01.c: Include stringpool.h. * objc-next-runtime-abi-02.c: Include stringpool.h. * objc-runtime-shared-support.c: Include stringpool.h. testsuite/ChangeLog * gcc.dg/plugin/selfassign.c: Include stringpool.h. * gcc.dg/plugin/start_unit_plugin.c: Likewise. From-SVN: r205023
1272 lines
36 KiB
C
1272 lines
36 KiB
C
/* Common block and equivalence list handling
|
|
Copyright (C) 2000-2013 Free Software Foundation, Inc.
|
|
Contributed by Canqun Yang <canqun@nudt.edu.cn>
|
|
|
|
This file is part of GCC.
|
|
|
|
GCC is free software; you can redistribute it and/or modify it under
|
|
the terms of the GNU General Public License as published by the Free
|
|
Software Foundation; either version 3, or (at your option) any later
|
|
version.
|
|
|
|
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with GCC; see the file COPYING3. If not see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
/* The core algorithm is based on Andy Vaught's g95 tree. Also the
|
|
way to build UNION_TYPE is borrowed from Richard Henderson.
|
|
|
|
Transform common blocks. An integral part of this is processing
|
|
equivalence variables. Equivalenced variables that are not in a
|
|
common block end up in a private block of their own.
|
|
|
|
Each common block or local equivalence list is declared as a union.
|
|
Variables within the block are represented as a field within the
|
|
block with the proper offset.
|
|
|
|
So if two variables are equivalenced, they just point to a common
|
|
area in memory.
|
|
|
|
Mathematically, laying out an equivalence block is equivalent to
|
|
solving a linear system of equations. The matrix is usually a
|
|
sparse matrix in which each row contains all zero elements except
|
|
for a +1 and a -1, a sort of a generalized Vandermonde matrix. The
|
|
matrix is usually block diagonal. The system can be
|
|
overdetermined, underdetermined or have a unique solution. If the
|
|
system is inconsistent, the program is not standard conforming.
|
|
The solution vector is integral, since all of the pivots are +1 or -1.
|
|
|
|
How we lay out an equivalence block is a little less complicated.
|
|
In an equivalence list with n elements, there are n-1 conditions to
|
|
be satisfied. The conditions partition the variables into what we
|
|
will call segments. If A and B are equivalenced then A and B are
|
|
in the same segment. If B and C are equivalenced as well, then A,
|
|
B and C are in a segment and so on. Each segment is a block of
|
|
memory that has one or more variables equivalenced in some way. A
|
|
common block is made up of a series of segments that are joined one
|
|
after the other. In the linear system, a segment is a block
|
|
diagonal.
|
|
|
|
To lay out a segment we first start with some variable and
|
|
determine its length. The first variable is assumed to start at
|
|
offset one and extends to however long it is. We then traverse the
|
|
list of equivalences to find an unused condition that involves at
|
|
least one of the variables currently in the segment.
|
|
|
|
Each equivalence condition amounts to the condition B+b=C+c where B
|
|
and C are the offsets of the B and C variables, and b and c are
|
|
constants which are nonzero for array elements, substrings or
|
|
structure components. So for
|
|
|
|
EQUIVALENCE(B(2), C(3))
|
|
we have
|
|
B + 2*size of B's elements = C + 3*size of C's elements.
|
|
|
|
If B and C are known we check to see if the condition already
|
|
holds. If B is known we can solve for C. Since we know the length
|
|
of C, we can see if the minimum and maximum extents of the segment
|
|
are affected. Eventually, we make a full pass through the
|
|
equivalence list without finding any new conditions and the segment
|
|
is fully specified.
|
|
|
|
At this point, the segment is added to the current common block.
|
|
Since we know the minimum extent of the segment, everything in the
|
|
segment is translated to its position in the common block. The
|
|
usual case here is that there are no equivalence statements and the
|
|
common block is series of segments with one variable each, which is
|
|
a diagonal matrix in the matrix formulation.
|
|
|
|
Each segment is described by a chain of segment_info structures. Each
|
|
segment_info structure describes the extents of a single variable within
|
|
the segment. This list is maintained in the order the elements are
|
|
positioned within the segment. If two elements have the same starting
|
|
offset the smaller will come first. If they also have the same size their
|
|
ordering is undefined.
|
|
|
|
Once all common blocks have been created, the list of equivalences
|
|
is examined for still-unused equivalence conditions. We create a
|
|
block for each merged equivalence list. */
|
|
|
|
#include <map>
|
|
#include "config.h"
|
|
#include "system.h"
|
|
#include "coretypes.h"
|
|
#include "tm.h"
|
|
#include "tree.h"
|
|
#include "stringpool.h"
|
|
#include "stor-layout.h"
|
|
#include "varasm.h"
|
|
#include "gfortran.h"
|
|
#include "trans.h"
|
|
#include "trans-types.h"
|
|
#include "trans-const.h"
|
|
#include "target-memory.h"
|
|
|
|
|
|
/* Holds a single variable in an equivalence set. */
|
|
typedef struct segment_info
|
|
{
|
|
gfc_symbol *sym;
|
|
HOST_WIDE_INT offset;
|
|
HOST_WIDE_INT length;
|
|
/* This will contain the field type until the field is created. */
|
|
tree field;
|
|
struct segment_info *next;
|
|
} segment_info;
|
|
|
|
static segment_info * current_segment;
|
|
|
|
/* Store decl of all common blocks in this translation unit; the first
|
|
tree is the identifier. */
|
|
static std::map<tree, tree> gfc_map_of_all_commons;
|
|
|
|
|
|
/* Make a segment_info based on a symbol. */
|
|
|
|
static segment_info *
|
|
get_segment_info (gfc_symbol * sym, HOST_WIDE_INT offset)
|
|
{
|
|
segment_info *s;
|
|
|
|
/* Make sure we've got the character length. */
|
|
if (sym->ts.type == BT_CHARACTER)
|
|
gfc_conv_const_charlen (sym->ts.u.cl);
|
|
|
|
/* Create the segment_info and fill it in. */
|
|
s = XCNEW (segment_info);
|
|
s->sym = sym;
|
|
/* We will use this type when building the segment aggregate type. */
|
|
s->field = gfc_sym_type (sym);
|
|
s->length = int_size_in_bytes (s->field);
|
|
s->offset = offset;
|
|
|
|
return s;
|
|
}
|
|
|
|
|
|
/* Add a copy of a segment list to the namespace. This is specifically for
|
|
equivalence segments, so that dependency checking can be done on
|
|
equivalence group members. */
|
|
|
|
static void
|
|
copy_equiv_list_to_ns (segment_info *c)
|
|
{
|
|
segment_info *f;
|
|
gfc_equiv_info *s;
|
|
gfc_equiv_list *l;
|
|
|
|
l = XCNEW (gfc_equiv_list);
|
|
|
|
l->next = c->sym->ns->equiv_lists;
|
|
c->sym->ns->equiv_lists = l;
|
|
|
|
for (f = c; f; f = f->next)
|
|
{
|
|
s = XCNEW (gfc_equiv_info);
|
|
s->next = l->equiv;
|
|
l->equiv = s;
|
|
s->sym = f->sym;
|
|
s->offset = f->offset;
|
|
s->length = f->length;
|
|
}
|
|
}
|
|
|
|
|
|
/* Add combine segment V and segment LIST. */
|
|
|
|
static segment_info *
|
|
add_segments (segment_info *list, segment_info *v)
|
|
{
|
|
segment_info *s;
|
|
segment_info *p;
|
|
segment_info *next;
|
|
|
|
p = NULL;
|
|
s = list;
|
|
|
|
while (v)
|
|
{
|
|
/* Find the location of the new element. */
|
|
while (s)
|
|
{
|
|
if (v->offset < s->offset)
|
|
break;
|
|
if (v->offset == s->offset
|
|
&& v->length <= s->length)
|
|
break;
|
|
|
|
p = s;
|
|
s = s->next;
|
|
}
|
|
|
|
/* Insert the new element in between p and s. */
|
|
next = v->next;
|
|
v->next = s;
|
|
if (p == NULL)
|
|
list = v;
|
|
else
|
|
p->next = v;
|
|
|
|
p = v;
|
|
v = next;
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
|
|
/* Construct mangled common block name from symbol name. */
|
|
|
|
/* We need the bind(c) flag to tell us how/if we should mangle the symbol
|
|
name. There are few calls to this function, so few places that this
|
|
would need to be added. At the moment, there is only one call, in
|
|
build_common_decl(). We can't attempt to look up the common block
|
|
because we may be building it for the first time and therefore, it won't
|
|
be in the common_root. We also need the binding label, if it's bind(c).
|
|
Therefore, send in the pointer to the common block, so whatever info we
|
|
have so far can be used. All of the necessary info should be available
|
|
in the gfc_common_head by now, so it should be accurate to test the
|
|
isBindC flag and use the binding label given if it is bind(c).
|
|
|
|
We may NOT know yet if it's bind(c) or not, but we can try at least.
|
|
Will have to figure out what to do later if it's labeled bind(c)
|
|
after this is called. */
|
|
|
|
static tree
|
|
gfc_sym_mangled_common_id (gfc_common_head *com)
|
|
{
|
|
int has_underscore;
|
|
char mangled_name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
|
|
char name[GFC_MAX_SYMBOL_LEN + 1];
|
|
|
|
/* Get the name out of the common block pointer. */
|
|
strcpy (name, com->name);
|
|
|
|
/* If we're suppose to do a bind(c). */
|
|
if (com->is_bind_c == 1 && com->binding_label)
|
|
return get_identifier (com->binding_label);
|
|
|
|
if (strcmp (name, BLANK_COMMON_NAME) == 0)
|
|
return get_identifier (name);
|
|
|
|
if (gfc_option.flag_underscoring)
|
|
{
|
|
has_underscore = strchr (name, '_') != 0;
|
|
if (gfc_option.flag_second_underscore && has_underscore)
|
|
snprintf (mangled_name, sizeof mangled_name, "%s__", name);
|
|
else
|
|
snprintf (mangled_name, sizeof mangled_name, "%s_", name);
|
|
|
|
return get_identifier (mangled_name);
|
|
}
|
|
else
|
|
return get_identifier (name);
|
|
}
|
|
|
|
|
|
/* Build a field declaration for a common variable or a local equivalence
|
|
object. */
|
|
|
|
static void
|
|
build_field (segment_info *h, tree union_type, record_layout_info rli)
|
|
{
|
|
tree field;
|
|
tree name;
|
|
HOST_WIDE_INT offset = h->offset;
|
|
unsigned HOST_WIDE_INT desired_align, known_align;
|
|
|
|
name = get_identifier (h->sym->name);
|
|
field = build_decl (h->sym->declared_at.lb->location,
|
|
FIELD_DECL, name, h->field);
|
|
known_align = (offset & -offset) * BITS_PER_UNIT;
|
|
if (known_align == 0 || known_align > BIGGEST_ALIGNMENT)
|
|
known_align = BIGGEST_ALIGNMENT;
|
|
|
|
desired_align = update_alignment_for_field (rli, field, known_align);
|
|
if (desired_align > known_align)
|
|
DECL_PACKED (field) = 1;
|
|
|
|
DECL_FIELD_CONTEXT (field) = union_type;
|
|
DECL_FIELD_OFFSET (field) = size_int (offset);
|
|
DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
|
|
SET_DECL_OFFSET_ALIGN (field, known_align);
|
|
|
|
rli->offset = size_binop (MAX_EXPR, rli->offset,
|
|
size_binop (PLUS_EXPR,
|
|
DECL_FIELD_OFFSET (field),
|
|
DECL_SIZE_UNIT (field)));
|
|
/* If this field is assigned to a label, we create another two variables.
|
|
One will hold the address of target label or format label. The other will
|
|
hold the length of format label string. */
|
|
if (h->sym->attr.assign)
|
|
{
|
|
tree len;
|
|
tree addr;
|
|
|
|
gfc_allocate_lang_decl (field);
|
|
GFC_DECL_ASSIGN (field) = 1;
|
|
len = gfc_create_var_np (gfc_charlen_type_node,h->sym->name);
|
|
addr = gfc_create_var_np (pvoid_type_node, h->sym->name);
|
|
TREE_STATIC (len) = 1;
|
|
TREE_STATIC (addr) = 1;
|
|
DECL_INITIAL (len) = build_int_cst (gfc_charlen_type_node, -2);
|
|
gfc_set_decl_location (len, &h->sym->declared_at);
|
|
gfc_set_decl_location (addr, &h->sym->declared_at);
|
|
GFC_DECL_STRING_LEN (field) = pushdecl_top_level (len);
|
|
GFC_DECL_ASSIGN_ADDR (field) = pushdecl_top_level (addr);
|
|
}
|
|
|
|
/* If this field is volatile, mark it. */
|
|
if (h->sym->attr.volatile_)
|
|
{
|
|
tree new_type;
|
|
TREE_THIS_VOLATILE (field) = 1;
|
|
TREE_SIDE_EFFECTS (field) = 1;
|
|
new_type = build_qualified_type (TREE_TYPE (field), TYPE_QUAL_VOLATILE);
|
|
TREE_TYPE (field) = new_type;
|
|
}
|
|
|
|
h->field = field;
|
|
}
|
|
|
|
|
|
/* Get storage for local equivalence. */
|
|
|
|
static tree
|
|
build_equiv_decl (tree union_type, bool is_init, bool is_saved)
|
|
{
|
|
tree decl;
|
|
char name[15];
|
|
static int serial = 0;
|
|
|
|
if (is_init)
|
|
{
|
|
decl = gfc_create_var (union_type, "equiv");
|
|
TREE_STATIC (decl) = 1;
|
|
GFC_DECL_COMMON_OR_EQUIV (decl) = 1;
|
|
return decl;
|
|
}
|
|
|
|
snprintf (name, sizeof (name), "equiv.%d", serial++);
|
|
decl = build_decl (input_location,
|
|
VAR_DECL, get_identifier (name), union_type);
|
|
DECL_ARTIFICIAL (decl) = 1;
|
|
DECL_IGNORED_P (decl) = 1;
|
|
|
|
if (!gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
|
|
|| is_saved)
|
|
TREE_STATIC (decl) = 1;
|
|
|
|
TREE_ADDRESSABLE (decl) = 1;
|
|
TREE_USED (decl) = 1;
|
|
GFC_DECL_COMMON_OR_EQUIV (decl) = 1;
|
|
|
|
/* The source location has been lost, and doesn't really matter.
|
|
We need to set it to something though. */
|
|
gfc_set_decl_location (decl, &gfc_current_locus);
|
|
|
|
gfc_add_decl_to_function (decl);
|
|
|
|
return decl;
|
|
}
|
|
|
|
|
|
/* Get storage for common block. */
|
|
|
|
static tree
|
|
build_common_decl (gfc_common_head *com, tree union_type, bool is_init)
|
|
{
|
|
tree decl, identifier;
|
|
|
|
identifier = gfc_sym_mangled_common_id (com);
|
|
decl = gfc_map_of_all_commons.count(identifier)
|
|
? gfc_map_of_all_commons[identifier] : NULL_TREE;
|
|
|
|
/* Update the size of this common block as needed. */
|
|
if (decl != NULL_TREE)
|
|
{
|
|
tree size = TYPE_SIZE_UNIT (union_type);
|
|
|
|
/* Named common blocks of the same name shall be of the same size
|
|
in all scoping units of a program in which they appear, but
|
|
blank common blocks may be of different sizes. */
|
|
if (!tree_int_cst_equal (DECL_SIZE_UNIT (decl), size)
|
|
&& strcmp (com->name, BLANK_COMMON_NAME))
|
|
gfc_warning ("Named COMMON block '%s' at %L shall be of the "
|
|
"same size as elsewhere (%lu vs %lu bytes)", com->name,
|
|
&com->where,
|
|
(unsigned long) TREE_INT_CST_LOW (size),
|
|
(unsigned long) TREE_INT_CST_LOW (DECL_SIZE_UNIT (decl)));
|
|
|
|
if (tree_int_cst_lt (DECL_SIZE_UNIT (decl), size))
|
|
{
|
|
DECL_SIZE (decl) = TYPE_SIZE (union_type);
|
|
DECL_SIZE_UNIT (decl) = size;
|
|
DECL_MODE (decl) = TYPE_MODE (union_type);
|
|
TREE_TYPE (decl) = union_type;
|
|
layout_decl (decl, 0);
|
|
}
|
|
}
|
|
|
|
/* If this common block has been declared in a previous program unit,
|
|
and either it is already initialized or there is no new initialization
|
|
for it, just return. */
|
|
if ((decl != NULL_TREE) && (!is_init || DECL_INITIAL (decl)))
|
|
return decl;
|
|
|
|
/* If there is no backend_decl for the common block, build it. */
|
|
if (decl == NULL_TREE)
|
|
{
|
|
if (com->is_bind_c == 1 && com->binding_label)
|
|
decl = build_decl (input_location, VAR_DECL, identifier, union_type);
|
|
else
|
|
{
|
|
decl = build_decl (input_location, VAR_DECL, get_identifier (com->name),
|
|
union_type);
|
|
gfc_set_decl_assembler_name (decl, identifier);
|
|
}
|
|
|
|
TREE_PUBLIC (decl) = 1;
|
|
TREE_STATIC (decl) = 1;
|
|
DECL_IGNORED_P (decl) = 1;
|
|
if (!com->is_bind_c)
|
|
DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
|
|
else
|
|
{
|
|
/* Do not set the alignment for bind(c) common blocks to
|
|
BIGGEST_ALIGNMENT because that won't match what C does. Also,
|
|
for common blocks with one element, the alignment must be
|
|
that of the field within the common block in order to match
|
|
what C will do. */
|
|
tree field = NULL_TREE;
|
|
field = TYPE_FIELDS (TREE_TYPE (decl));
|
|
if (DECL_CHAIN (field) == NULL_TREE)
|
|
DECL_ALIGN (decl) = TYPE_ALIGN (TREE_TYPE (field));
|
|
}
|
|
DECL_USER_ALIGN (decl) = 0;
|
|
GFC_DECL_COMMON_OR_EQUIV (decl) = 1;
|
|
|
|
gfc_set_decl_location (decl, &com->where);
|
|
|
|
if (com->threadprivate)
|
|
DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
|
|
|
|
/* Place the back end declaration for this common block in
|
|
GLOBAL_BINDING_LEVEL. */
|
|
gfc_map_of_all_commons[identifier] = pushdecl_top_level (decl);
|
|
}
|
|
|
|
/* Has no initial values. */
|
|
if (!is_init)
|
|
{
|
|
DECL_INITIAL (decl) = NULL_TREE;
|
|
DECL_COMMON (decl) = 1;
|
|
DECL_DEFER_OUTPUT (decl) = 1;
|
|
}
|
|
else
|
|
{
|
|
DECL_INITIAL (decl) = error_mark_node;
|
|
DECL_COMMON (decl) = 0;
|
|
DECL_DEFER_OUTPUT (decl) = 0;
|
|
}
|
|
return decl;
|
|
}
|
|
|
|
|
|
/* Return a field that is the size of the union, if an equivalence has
|
|
overlapping initializers. Merge the initializers into a single
|
|
initializer for this new field, then free the old ones. */
|
|
|
|
static tree
|
|
get_init_field (segment_info *head, tree union_type, tree *field_init,
|
|
record_layout_info rli)
|
|
{
|
|
segment_info *s;
|
|
HOST_WIDE_INT length = 0;
|
|
HOST_WIDE_INT offset = 0;
|
|
unsigned HOST_WIDE_INT known_align, desired_align;
|
|
bool overlap = false;
|
|
tree tmp, field;
|
|
tree init;
|
|
unsigned char *data, *chk;
|
|
vec<constructor_elt, va_gc> *v = NULL;
|
|
|
|
tree type = unsigned_char_type_node;
|
|
int i;
|
|
|
|
/* Obtain the size of the union and check if there are any overlapping
|
|
initializers. */
|
|
for (s = head; s; s = s->next)
|
|
{
|
|
HOST_WIDE_INT slen = s->offset + s->length;
|
|
if (s->sym->value)
|
|
{
|
|
if (s->offset < offset)
|
|
overlap = true;
|
|
offset = slen;
|
|
}
|
|
length = length < slen ? slen : length;
|
|
}
|
|
|
|
if (!overlap)
|
|
return NULL_TREE;
|
|
|
|
/* Now absorb all the initializer data into a single vector,
|
|
whilst checking for overlapping, unequal values. */
|
|
data = XCNEWVEC (unsigned char, (size_t)length);
|
|
chk = XCNEWVEC (unsigned char, (size_t)length);
|
|
|
|
/* TODO - change this when default initialization is implemented. */
|
|
memset (data, '\0', (size_t)length);
|
|
memset (chk, '\0', (size_t)length);
|
|
for (s = head; s; s = s->next)
|
|
if (s->sym->value)
|
|
gfc_merge_initializers (s->sym->ts, s->sym->value,
|
|
&data[s->offset],
|
|
&chk[s->offset],
|
|
(size_t)s->length);
|
|
|
|
for (i = 0; i < length; i++)
|
|
CONSTRUCTOR_APPEND_ELT (v, NULL, build_int_cst (type, data[i]));
|
|
|
|
free (data);
|
|
free (chk);
|
|
|
|
/* Build a char[length] array to hold the initializers. Much of what
|
|
follows is borrowed from build_field, above. */
|
|
|
|
tmp = build_int_cst (gfc_array_index_type, length - 1);
|
|
tmp = build_range_type (gfc_array_index_type,
|
|
gfc_index_zero_node, tmp);
|
|
tmp = build_array_type (type, tmp);
|
|
field = build_decl (gfc_current_locus.lb->location,
|
|
FIELD_DECL, NULL_TREE, tmp);
|
|
|
|
known_align = BIGGEST_ALIGNMENT;
|
|
|
|
desired_align = update_alignment_for_field (rli, field, known_align);
|
|
if (desired_align > known_align)
|
|
DECL_PACKED (field) = 1;
|
|
|
|
DECL_FIELD_CONTEXT (field) = union_type;
|
|
DECL_FIELD_OFFSET (field) = size_int (0);
|
|
DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
|
|
SET_DECL_OFFSET_ALIGN (field, known_align);
|
|
|
|
rli->offset = size_binop (MAX_EXPR, rli->offset,
|
|
size_binop (PLUS_EXPR,
|
|
DECL_FIELD_OFFSET (field),
|
|
DECL_SIZE_UNIT (field)));
|
|
|
|
init = build_constructor (TREE_TYPE (field), v);
|
|
TREE_CONSTANT (init) = 1;
|
|
|
|
*field_init = init;
|
|
|
|
for (s = head; s; s = s->next)
|
|
{
|
|
if (s->sym->value == NULL)
|
|
continue;
|
|
|
|
gfc_free_expr (s->sym->value);
|
|
s->sym->value = NULL;
|
|
}
|
|
|
|
return field;
|
|
}
|
|
|
|
|
|
/* Declare memory for the common block or local equivalence, and create
|
|
backend declarations for all of the elements. */
|
|
|
|
static void
|
|
create_common (gfc_common_head *com, segment_info *head, bool saw_equiv)
|
|
{
|
|
segment_info *s, *next_s;
|
|
tree union_type;
|
|
tree *field_link;
|
|
tree field;
|
|
tree field_init = NULL_TREE;
|
|
record_layout_info rli;
|
|
tree decl;
|
|
bool is_init = false;
|
|
bool is_saved = false;
|
|
|
|
/* Declare the variables inside the common block.
|
|
If the current common block contains any equivalence object, then
|
|
make a UNION_TYPE node, otherwise RECORD_TYPE. This will let the
|
|
alias analyzer work well when there is no address overlapping for
|
|
common variables in the current common block. */
|
|
if (saw_equiv)
|
|
union_type = make_node (UNION_TYPE);
|
|
else
|
|
union_type = make_node (RECORD_TYPE);
|
|
|
|
rli = start_record_layout (union_type);
|
|
field_link = &TYPE_FIELDS (union_type);
|
|
|
|
/* Check for overlapping initializers and replace them with a single,
|
|
artificial field that contains all the data. */
|
|
if (saw_equiv)
|
|
field = get_init_field (head, union_type, &field_init, rli);
|
|
else
|
|
field = NULL_TREE;
|
|
|
|
if (field != NULL_TREE)
|
|
{
|
|
is_init = true;
|
|
*field_link = field;
|
|
field_link = &DECL_CHAIN (field);
|
|
}
|
|
|
|
for (s = head; s; s = s->next)
|
|
{
|
|
build_field (s, union_type, rli);
|
|
|
|
/* Link the field into the type. */
|
|
*field_link = s->field;
|
|
field_link = &DECL_CHAIN (s->field);
|
|
|
|
/* Has initial value. */
|
|
if (s->sym->value)
|
|
is_init = true;
|
|
|
|
/* Has SAVE attribute. */
|
|
if (s->sym->attr.save)
|
|
is_saved = true;
|
|
}
|
|
|
|
finish_record_layout (rli, true);
|
|
|
|
if (com)
|
|
decl = build_common_decl (com, union_type, is_init);
|
|
else
|
|
decl = build_equiv_decl (union_type, is_init, is_saved);
|
|
|
|
if (is_init)
|
|
{
|
|
tree ctor, tmp;
|
|
vec<constructor_elt, va_gc> *v = NULL;
|
|
|
|
if (field != NULL_TREE && field_init != NULL_TREE)
|
|
CONSTRUCTOR_APPEND_ELT (v, field, field_init);
|
|
else
|
|
for (s = head; s; s = s->next)
|
|
{
|
|
if (s->sym->value)
|
|
{
|
|
/* Add the initializer for this field. */
|
|
tmp = gfc_conv_initializer (s->sym->value, &s->sym->ts,
|
|
TREE_TYPE (s->field),
|
|
s->sym->attr.dimension,
|
|
s->sym->attr.pointer
|
|
|| s->sym->attr.allocatable, false);
|
|
|
|
CONSTRUCTOR_APPEND_ELT (v, s->field, tmp);
|
|
}
|
|
}
|
|
|
|
gcc_assert (!v->is_empty ());
|
|
ctor = build_constructor (union_type, v);
|
|
TREE_CONSTANT (ctor) = 1;
|
|
TREE_STATIC (ctor) = 1;
|
|
DECL_INITIAL (decl) = ctor;
|
|
|
|
#ifdef ENABLE_CHECKING
|
|
{
|
|
tree field, value;
|
|
unsigned HOST_WIDE_INT idx;
|
|
FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, field, value)
|
|
gcc_assert (TREE_CODE (field) == FIELD_DECL);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/* Build component reference for each variable. */
|
|
for (s = head; s; s = next_s)
|
|
{
|
|
tree var_decl;
|
|
|
|
var_decl = build_decl (s->sym->declared_at.lb->location,
|
|
VAR_DECL, DECL_NAME (s->field),
|
|
TREE_TYPE (s->field));
|
|
TREE_STATIC (var_decl) = TREE_STATIC (decl);
|
|
/* Mark the variable as used in order to avoid warnings about
|
|
unused variables. */
|
|
TREE_USED (var_decl) = 1;
|
|
if (s->sym->attr.use_assoc)
|
|
DECL_IGNORED_P (var_decl) = 1;
|
|
if (s->sym->attr.target)
|
|
TREE_ADDRESSABLE (var_decl) = 1;
|
|
/* Fake variables are not visible from other translation units. */
|
|
TREE_PUBLIC (var_decl) = 0;
|
|
|
|
/* To preserve identifier names in COMMON, chain to procedure
|
|
scope unless at top level in a module definition. */
|
|
if (com
|
|
&& s->sym->ns->proc_name
|
|
&& s->sym->ns->proc_name->attr.flavor == FL_MODULE)
|
|
var_decl = pushdecl_top_level (var_decl);
|
|
else
|
|
gfc_add_decl_to_function (var_decl);
|
|
|
|
SET_DECL_VALUE_EXPR (var_decl,
|
|
fold_build3_loc (input_location, COMPONENT_REF,
|
|
TREE_TYPE (s->field),
|
|
decl, s->field, NULL_TREE));
|
|
DECL_HAS_VALUE_EXPR_P (var_decl) = 1;
|
|
GFC_DECL_COMMON_OR_EQUIV (var_decl) = 1;
|
|
|
|
if (s->sym->attr.assign)
|
|
{
|
|
gfc_allocate_lang_decl (var_decl);
|
|
GFC_DECL_ASSIGN (var_decl) = 1;
|
|
GFC_DECL_STRING_LEN (var_decl) = GFC_DECL_STRING_LEN (s->field);
|
|
GFC_DECL_ASSIGN_ADDR (var_decl) = GFC_DECL_ASSIGN_ADDR (s->field);
|
|
}
|
|
|
|
s->sym->backend_decl = var_decl;
|
|
|
|
next_s = s->next;
|
|
free (s);
|
|
}
|
|
}
|
|
|
|
|
|
/* Given a symbol, find it in the current segment list. Returns NULL if
|
|
not found. */
|
|
|
|
static segment_info *
|
|
find_segment_info (gfc_symbol *symbol)
|
|
{
|
|
segment_info *n;
|
|
|
|
for (n = current_segment; n; n = n->next)
|
|
{
|
|
if (n->sym == symbol)
|
|
return n;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
|
|
/* Given an expression node, make sure it is a constant integer and return
|
|
the mpz_t value. */
|
|
|
|
static mpz_t *
|
|
get_mpz (gfc_expr *e)
|
|
{
|
|
|
|
if (e->expr_type != EXPR_CONSTANT)
|
|
gfc_internal_error ("get_mpz(): Not an integer constant");
|
|
|
|
return &e->value.integer;
|
|
}
|
|
|
|
|
|
/* Given an array specification and an array reference, figure out the
|
|
array element number (zero based). Bounds and elements are guaranteed
|
|
to be constants. If something goes wrong we generate an error and
|
|
return zero. */
|
|
|
|
static HOST_WIDE_INT
|
|
element_number (gfc_array_ref *ar)
|
|
{
|
|
mpz_t multiplier, offset, extent, n;
|
|
gfc_array_spec *as;
|
|
HOST_WIDE_INT i, rank;
|
|
|
|
as = ar->as;
|
|
rank = as->rank;
|
|
mpz_init_set_ui (multiplier, 1);
|
|
mpz_init_set_ui (offset, 0);
|
|
mpz_init (extent);
|
|
mpz_init (n);
|
|
|
|
for (i = 0; i < rank; i++)
|
|
{
|
|
if (ar->dimen_type[i] != DIMEN_ELEMENT)
|
|
gfc_internal_error ("element_number(): Bad dimension type");
|
|
|
|
mpz_sub (n, *get_mpz (ar->start[i]), *get_mpz (as->lower[i]));
|
|
|
|
mpz_mul (n, n, multiplier);
|
|
mpz_add (offset, offset, n);
|
|
|
|
mpz_sub (extent, *get_mpz (as->upper[i]), *get_mpz (as->lower[i]));
|
|
mpz_add_ui (extent, extent, 1);
|
|
|
|
if (mpz_sgn (extent) < 0)
|
|
mpz_set_ui (extent, 0);
|
|
|
|
mpz_mul (multiplier, multiplier, extent);
|
|
}
|
|
|
|
i = mpz_get_ui (offset);
|
|
|
|
mpz_clear (multiplier);
|
|
mpz_clear (offset);
|
|
mpz_clear (extent);
|
|
mpz_clear (n);
|
|
|
|
return i;
|
|
}
|
|
|
|
|
|
/* Given a single element of an equivalence list, figure out the offset
|
|
from the base symbol. For simple variables or full arrays, this is
|
|
simply zero. For an array element we have to calculate the array
|
|
element number and multiply by the element size. For a substring we
|
|
have to calculate the further reference. */
|
|
|
|
static HOST_WIDE_INT
|
|
calculate_offset (gfc_expr *e)
|
|
{
|
|
HOST_WIDE_INT n, element_size, offset;
|
|
gfc_typespec *element_type;
|
|
gfc_ref *reference;
|
|
|
|
offset = 0;
|
|
element_type = &e->symtree->n.sym->ts;
|
|
|
|
for (reference = e->ref; reference; reference = reference->next)
|
|
switch (reference->type)
|
|
{
|
|
case REF_ARRAY:
|
|
switch (reference->u.ar.type)
|
|
{
|
|
case AR_FULL:
|
|
break;
|
|
|
|
case AR_ELEMENT:
|
|
n = element_number (&reference->u.ar);
|
|
if (element_type->type == BT_CHARACTER)
|
|
gfc_conv_const_charlen (element_type->u.cl);
|
|
element_size =
|
|
int_size_in_bytes (gfc_typenode_for_spec (element_type));
|
|
offset += n * element_size;
|
|
break;
|
|
|
|
default:
|
|
gfc_error ("Bad array reference at %L", &e->where);
|
|
}
|
|
break;
|
|
case REF_SUBSTRING:
|
|
if (reference->u.ss.start != NULL)
|
|
offset += mpz_get_ui (*get_mpz (reference->u.ss.start)) - 1;
|
|
break;
|
|
default:
|
|
gfc_error ("Illegal reference type at %L as EQUIVALENCE object",
|
|
&e->where);
|
|
}
|
|
return offset;
|
|
}
|
|
|
|
|
|
/* Add a new segment_info structure to the current segment. eq1 is already
|
|
in the list, eq2 is not. */
|
|
|
|
static void
|
|
new_condition (segment_info *v, gfc_equiv *eq1, gfc_equiv *eq2)
|
|
{
|
|
HOST_WIDE_INT offset1, offset2;
|
|
segment_info *a;
|
|
|
|
offset1 = calculate_offset (eq1->expr);
|
|
offset2 = calculate_offset (eq2->expr);
|
|
|
|
a = get_segment_info (eq2->expr->symtree->n.sym,
|
|
v->offset + offset1 - offset2);
|
|
|
|
current_segment = add_segments (current_segment, a);
|
|
}
|
|
|
|
|
|
/* Given two equivalence structures that are both already in the list, make
|
|
sure that this new condition is not violated, generating an error if it
|
|
is. */
|
|
|
|
static void
|
|
confirm_condition (segment_info *s1, gfc_equiv *eq1, segment_info *s2,
|
|
gfc_equiv *eq2)
|
|
{
|
|
HOST_WIDE_INT offset1, offset2;
|
|
|
|
offset1 = calculate_offset (eq1->expr);
|
|
offset2 = calculate_offset (eq2->expr);
|
|
|
|
if (s1->offset + offset1 != s2->offset + offset2)
|
|
gfc_error ("Inconsistent equivalence rules involving '%s' at %L and "
|
|
"'%s' at %L", s1->sym->name, &s1->sym->declared_at,
|
|
s2->sym->name, &s2->sym->declared_at);
|
|
}
|
|
|
|
|
|
/* Process a new equivalence condition. eq1 is know to be in segment f.
|
|
If eq2 is also present then confirm that the condition holds.
|
|
Otherwise add a new variable to the segment list. */
|
|
|
|
static void
|
|
add_condition (segment_info *f, gfc_equiv *eq1, gfc_equiv *eq2)
|
|
{
|
|
segment_info *n;
|
|
|
|
n = find_segment_info (eq2->expr->symtree->n.sym);
|
|
|
|
if (n == NULL)
|
|
new_condition (f, eq1, eq2);
|
|
else
|
|
confirm_condition (f, eq1, n, eq2);
|
|
}
|
|
|
|
|
|
/* Given a segment element, search through the equivalence lists for unused
|
|
conditions that involve the symbol. Add these rules to the segment. */
|
|
|
|
static bool
|
|
find_equivalence (segment_info *n)
|
|
{
|
|
gfc_equiv *e1, *e2, *eq;
|
|
bool found;
|
|
|
|
found = FALSE;
|
|
|
|
for (e1 = n->sym->ns->equiv; e1; e1 = e1->next)
|
|
{
|
|
eq = NULL;
|
|
|
|
/* Search the equivalence list, including the root (first) element
|
|
for the symbol that owns the segment. */
|
|
for (e2 = e1; e2; e2 = e2->eq)
|
|
{
|
|
if (!e2->used && e2->expr->symtree->n.sym == n->sym)
|
|
{
|
|
eq = e2;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* Go to the next root element. */
|
|
if (eq == NULL)
|
|
continue;
|
|
|
|
eq->used = 1;
|
|
|
|
/* Now traverse the equivalence list matching the offsets. */
|
|
for (e2 = e1; e2; e2 = e2->eq)
|
|
{
|
|
if (!e2->used && e2 != eq)
|
|
{
|
|
add_condition (n, eq, e2);
|
|
e2->used = 1;
|
|
found = TRUE;
|
|
}
|
|
}
|
|
}
|
|
return found;
|
|
}
|
|
|
|
|
|
/* Add all symbols equivalenced within a segment. We need to scan the
|
|
segment list multiple times to include indirect equivalences. Since
|
|
a new segment_info can inserted at the beginning of the segment list,
|
|
depending on its offset, we have to force a final pass through the
|
|
loop by demanding that completion sees a pass with no matches; i.e.,
|
|
all symbols with equiv_built set and no new equivalences found. */
|
|
|
|
static void
|
|
add_equivalences (bool *saw_equiv)
|
|
{
|
|
segment_info *f;
|
|
bool seen_one, more;
|
|
|
|
seen_one = false;
|
|
more = TRUE;
|
|
while (more)
|
|
{
|
|
more = FALSE;
|
|
for (f = current_segment; f; f = f->next)
|
|
{
|
|
if (!f->sym->equiv_built)
|
|
{
|
|
f->sym->equiv_built = 1;
|
|
seen_one = find_equivalence (f);
|
|
if (seen_one)
|
|
{
|
|
*saw_equiv = true;
|
|
more = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Add a copy of this segment list to the namespace. */
|
|
copy_equiv_list_to_ns (current_segment);
|
|
}
|
|
|
|
|
|
/* Returns the offset necessary to properly align the current equivalence.
|
|
Sets *palign to the required alignment. */
|
|
|
|
static HOST_WIDE_INT
|
|
align_segment (unsigned HOST_WIDE_INT *palign)
|
|
{
|
|
segment_info *s;
|
|
unsigned HOST_WIDE_INT offset;
|
|
unsigned HOST_WIDE_INT max_align;
|
|
unsigned HOST_WIDE_INT this_align;
|
|
unsigned HOST_WIDE_INT this_offset;
|
|
|
|
max_align = 1;
|
|
offset = 0;
|
|
for (s = current_segment; s; s = s->next)
|
|
{
|
|
this_align = TYPE_ALIGN_UNIT (s->field);
|
|
if (s->offset & (this_align - 1))
|
|
{
|
|
/* Field is misaligned. */
|
|
this_offset = this_align - ((s->offset + offset) & (this_align - 1));
|
|
if (this_offset & (max_align - 1))
|
|
{
|
|
/* Aligning this field would misalign a previous field. */
|
|
gfc_error ("The equivalence set for variable '%s' "
|
|
"declared at %L violates alignment requirements",
|
|
s->sym->name, &s->sym->declared_at);
|
|
}
|
|
offset += this_offset;
|
|
}
|
|
max_align = this_align;
|
|
}
|
|
if (palign)
|
|
*palign = max_align;
|
|
return offset;
|
|
}
|
|
|
|
|
|
/* Adjust segment offsets by the given amount. */
|
|
|
|
static void
|
|
apply_segment_offset (segment_info *s, HOST_WIDE_INT offset)
|
|
{
|
|
for (; s; s = s->next)
|
|
s->offset += offset;
|
|
}
|
|
|
|
|
|
/* Lay out a symbol in a common block. If the symbol has already been seen
|
|
then check the location is consistent. Otherwise create segments
|
|
for that symbol and all the symbols equivalenced with it. */
|
|
|
|
/* Translate a single common block. */
|
|
|
|
static void
|
|
translate_common (gfc_common_head *common, gfc_symbol *var_list)
|
|
{
|
|
gfc_symbol *sym;
|
|
segment_info *s;
|
|
segment_info *common_segment;
|
|
HOST_WIDE_INT offset;
|
|
HOST_WIDE_INT current_offset;
|
|
unsigned HOST_WIDE_INT align;
|
|
bool saw_equiv;
|
|
|
|
common_segment = NULL;
|
|
offset = 0;
|
|
current_offset = 0;
|
|
align = 1;
|
|
saw_equiv = false;
|
|
|
|
/* Add symbols to the segment. */
|
|
for (sym = var_list; sym; sym = sym->common_next)
|
|
{
|
|
current_segment = common_segment;
|
|
s = find_segment_info (sym);
|
|
|
|
/* Symbol has already been added via an equivalence. Multiple
|
|
use associations of the same common block result in equiv_built
|
|
being set but no information about the symbol in the segment. */
|
|
if (s && sym->equiv_built)
|
|
{
|
|
/* Ensure the current location is properly aligned. */
|
|
align = TYPE_ALIGN_UNIT (s->field);
|
|
current_offset = (current_offset + align - 1) &~ (align - 1);
|
|
|
|
/* Verify that it ended up where we expect it. */
|
|
if (s->offset != current_offset)
|
|
{
|
|
gfc_error ("Equivalence for '%s' does not match ordering of "
|
|
"COMMON '%s' at %L", sym->name,
|
|
common->name, &common->where);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* A symbol we haven't seen before. */
|
|
s = current_segment = get_segment_info (sym, current_offset);
|
|
|
|
/* Add all objects directly or indirectly equivalenced with this
|
|
symbol. */
|
|
add_equivalences (&saw_equiv);
|
|
|
|
if (current_segment->offset < 0)
|
|
gfc_error ("The equivalence set for '%s' cause an invalid "
|
|
"extension to COMMON '%s' at %L", sym->name,
|
|
common->name, &common->where);
|
|
|
|
if (gfc_option.flag_align_commons)
|
|
offset = align_segment (&align);
|
|
|
|
if (offset)
|
|
{
|
|
/* The required offset conflicts with previous alignment
|
|
requirements. Insert padding immediately before this
|
|
segment. */
|
|
if (gfc_option.warn_align_commons)
|
|
{
|
|
if (strcmp (common->name, BLANK_COMMON_NAME))
|
|
gfc_warning ("Padding of %d bytes required before '%s' in "
|
|
"COMMON '%s' at %L; reorder elements or use "
|
|
"-fno-align-commons", (int)offset,
|
|
s->sym->name, common->name, &common->where);
|
|
else
|
|
gfc_warning ("Padding of %d bytes required before '%s' in "
|
|
"COMMON at %L; reorder elements or use "
|
|
"-fno-align-commons", (int)offset,
|
|
s->sym->name, &common->where);
|
|
}
|
|
}
|
|
|
|
/* Apply the offset to the new segments. */
|
|
apply_segment_offset (current_segment, offset);
|
|
current_offset += offset;
|
|
|
|
/* Add the new segments to the common block. */
|
|
common_segment = add_segments (common_segment, current_segment);
|
|
}
|
|
|
|
/* The offset of the next common variable. */
|
|
current_offset += s->length;
|
|
}
|
|
|
|
if (common_segment == NULL)
|
|
{
|
|
gfc_error ("COMMON '%s' at %L does not exist",
|
|
common->name, &common->where);
|
|
return;
|
|
}
|
|
|
|
if (common_segment->offset != 0 && gfc_option.warn_align_commons)
|
|
{
|
|
if (strcmp (common->name, BLANK_COMMON_NAME))
|
|
gfc_warning ("COMMON '%s' at %L requires %d bytes of padding; "
|
|
"reorder elements or use -fno-align-commons",
|
|
common->name, &common->where, (int)common_segment->offset);
|
|
else
|
|
gfc_warning ("COMMON at %L requires %d bytes of padding; "
|
|
"reorder elements or use -fno-align-commons",
|
|
&common->where, (int)common_segment->offset);
|
|
}
|
|
|
|
create_common (common, common_segment, saw_equiv);
|
|
}
|
|
|
|
|
|
/* Create a new block for each merged equivalence list. */
|
|
|
|
static void
|
|
finish_equivalences (gfc_namespace *ns)
|
|
{
|
|
gfc_equiv *z, *y;
|
|
gfc_symbol *sym;
|
|
gfc_common_head * c;
|
|
HOST_WIDE_INT offset;
|
|
unsigned HOST_WIDE_INT align;
|
|
bool dummy;
|
|
|
|
for (z = ns->equiv; z; z = z->next)
|
|
for (y = z->eq; y; y = y->eq)
|
|
{
|
|
if (y->used)
|
|
continue;
|
|
sym = z->expr->symtree->n.sym;
|
|
current_segment = get_segment_info (sym, 0);
|
|
|
|
/* All objects directly or indirectly equivalenced with this
|
|
symbol. */
|
|
add_equivalences (&dummy);
|
|
|
|
/* Align the block. */
|
|
offset = align_segment (&align);
|
|
|
|
/* Ensure all offsets are positive. */
|
|
offset -= current_segment->offset & ~(align - 1);
|
|
|
|
apply_segment_offset (current_segment, offset);
|
|
|
|
/* Create the decl. If this is a module equivalence, it has a
|
|
unique name, pointed to by z->module. This is written to a
|
|
gfc_common_header to push create_common into using
|
|
build_common_decl, so that the equivalence appears as an
|
|
external symbol. Otherwise, a local declaration is built using
|
|
build_equiv_decl. */
|
|
if (z->module)
|
|
{
|
|
c = gfc_get_common_head ();
|
|
/* We've lost the real location, so use the location of the
|
|
enclosing procedure. */
|
|
c->where = ns->proc_name->declared_at;
|
|
strcpy (c->name, z->module);
|
|
}
|
|
else
|
|
c = NULL;
|
|
|
|
create_common (c, current_segment, true);
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
/* Work function for translating a named common block. */
|
|
|
|
static void
|
|
named_common (gfc_symtree *st)
|
|
{
|
|
translate_common (st->n.common, st->n.common->head);
|
|
}
|
|
|
|
|
|
/* Translate the common blocks in a namespace. Unlike other variables,
|
|
these have to be created before code, because the backend_decl depends
|
|
on the rest of the common block. */
|
|
|
|
void
|
|
gfc_trans_common (gfc_namespace *ns)
|
|
{
|
|
gfc_common_head *c;
|
|
|
|
/* Translate the blank common block. */
|
|
if (ns->blank_common.head != NULL)
|
|
{
|
|
c = gfc_get_common_head ();
|
|
c->where = ns->blank_common.head->common_head->where;
|
|
strcpy (c->name, BLANK_COMMON_NAME);
|
|
translate_common (c, ns->blank_common.head);
|
|
}
|
|
|
|
/* Translate all named common blocks. */
|
|
gfc_traverse_symtree (ns->common_root, named_common);
|
|
|
|
/* Translate local equivalence. */
|
|
finish_equivalences (ns);
|
|
|
|
/* Commit the newly created symbols for common blocks and module
|
|
equivalences. */
|
|
gfc_commit_symbols ();
|
|
}
|