On 01/08/2018 03:10 AM, Martin Liška wrote:
> On 01/07/2018 11:59 PM, H.J. Lu wrote:
>> +static void
>> +output_indirect_thunk_function (bool need_bnd_p, int regno)
>> +{
>> + char name[32];
>> + tree decl;
>> +
>> + /* Create __x86_indirect_thunk/__x86_indirect_thunk_bnd. */
>> + indirect_thunk_name (name, regno, need_bnd_p);
>> + decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
>> + get_identifier (name),
>> + build_function_type_list (void_type_node, NULL_TREE));
>> + DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
>> + NULL_TREE, void_type_node);
>> + TREE_PUBLIC (decl) = 1;
>> + TREE_STATIC (decl) = 1;
>> + DECL_IGNORED_P (decl) = 1;
>> +
>> +#if TARGET_MACHO
>> + if (TARGET_MACHO)
>> + {
>> + switch_to_section (darwin_sections[picbase_thunk_section]);
>> + fputs ("\t.weak_definition\t", asm_out_file);
>> + assemble_name (asm_out_file, name);
>> + fputs ("\n\t.private_extern\t", asm_out_file);
>> + assemble_name (asm_out_file, name);
>> + putc ('\n', asm_out_file);
>> + ASM_OUTPUT_LABEL (asm_out_file, name);
>> + DECL_WEAK (decl) = 1;
>> + }
>> + else
>> +#endif
>> + if (USE_HIDDEN_LINKONCE)
>> + {
>> + cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME
>> (decl));
>> +
>> + targetm.asm_out.unique_section (decl, 0);
>> + switch_to_section (get_named_section (decl, NULL, 0));
>> +
>> + targetm.asm_out.globalize_label (asm_out_file, name);
>> + fputs ("\t.hidden\t", asm_out_file);
>> + assemble_name (asm_out_file, name);
>> + putc ('\n', asm_out_file);
>> + ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
>> + }
>> + else
>> + {
>> + switch_to_section (text_section);
>> + ASM_OUTPUT_LABEL (asm_out_file, name);
>> + }
>> +
>> + DECL_INITIAL (decl) = make_node (BLOCK);
>> + current_function_decl = decl;
>> + allocate_struct_function (decl, false);
>> + init_function_start (decl);
>> + /* We're about to hide the function body from callees of final_* by
>> + emitting it directly; tell them we're a thunk, if they care. */
>> + cfun->is_thunk = true;
>> + first_function_block_is_cold = false;
>> + /* Make sure unwind info is emitted for the thunk if needed. */
>> + final_start_function (emit_barrier (), asm_out_file, 1);
>> +
>> + output_indirect_thunk (need_bnd_p, regno);
>> +
>> + final_end_function ();
>> + init_insn_lengths ();
>> + free_after_compilation (cfun);
>> + set_cfun (NULL);
>> + current_function_decl = NULL;
>> +}
>> +
>
> I'm wondering whether thunk creation can be a good target-independent
> generalization? I guess
> we can emit the function declaration without direct writes to asm_out_file?
> And the emission
> of function body can be potentially a target hook?
>
> What about emitting body of the function with RTL instructions instead of
> direct assembly write?
> My knowledge of RTL is quite small, but maybe it can bring some
> generalization and reusability
> for other targets?
That's the key point I'm trying to make. We should be looking at
generalizing this stuff where it makes sense.
jeff