1 /* ginsn.h - GAS instruction representation.
2 Copyright (C) 2023 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
26 #ifdef TARGET_USE_GINSN
28 static const char *const ginsn_type_names
[] =
30 #define _GINSN_TYPE_ITEM(NAME, STR) STR,
32 #undef _GINSN_TYPE_ITEM
38 ginsnS
*ginsn
= XCNEW (ginsnS
);
43 ginsn_init (enum ginsn_type type
, const symbolS
*sym
, bool real_p
)
45 ginsnS
*ginsn
= ginsn_alloc ();
49 ginsn
->flags
|= GINSN_F_INSN_REAL
;
54 ginsn_cleanup (ginsnS
**ginsnp
)
58 if (!ginsnp
|| !*ginsnp
)
64 scfi_ops_cleanup (ginsn
->scfi_ops
);
65 ginsn
->scfi_ops
= NULL
;
73 ginsn_set_src (struct ginsn_src
*src
, enum ginsn_src_type type
, unsigned int reg
,
80 /* Even when the use-case is SCFI, the value of reg may be > SCFI_MAX_REG_ID.
81 E.g., in AMD64, push fs etc. */
83 src
->immdisp
= immdisp
;
87 ginsn_set_dst (struct ginsn_dst
*dst
, enum ginsn_dst_type type
, unsigned int reg
,
96 if (type
== GINSN_DST_INDIRECT
)
101 ginsn_set_file_line (ginsnS
*ginsn
, const char *file
, unsigned int line
)
111 ginsn_get_src1 (ginsnS
*ginsn
)
113 return &ginsn
->src
[0];
117 ginsn_get_src2 (ginsnS
*ginsn
)
119 return &ginsn
->src
[1];
123 ginsn_get_dst (ginsnS
*ginsn
)
129 ginsn_get_src_reg (struct ginsn_src
*src
)
135 ginsn_get_src_type (struct ginsn_src
*src
)
141 ginsn_get_src_disp (struct ginsn_src
*src
)
147 ginsn_get_src_imm (struct ginsn_src
*src
)
153 ginsn_get_dst_reg (struct ginsn_dst
*dst
)
159 ginsn_get_dst_type (struct ginsn_dst
*dst
)
165 ginsn_get_dst_disp (struct ginsn_dst
*dst
)
171 label_ginsn_map_insert (const symbolS
*label
, ginsnS
*ginsn
)
173 const char *name
= S_GET_NAME (label
);
174 str_hash_insert (frchain_now
->frch_ginsn_data
->label_ginsn_map
,
175 name
, ginsn
, 0 /* noreplace. */);
179 label_ginsn_map_find (const symbolS
*label
)
181 const char *name
= S_GET_NAME (label
);
183 = (ginsnS
*) str_hash_find (frchain_now
->frch_ginsn_data
->label_ginsn_map
,
189 ginsn_new_phantom (const symbolS
*sym
)
191 ginsnS
*ginsn
= ginsn_alloc ();
192 ginsn
->type
= GINSN_TYPE_PHANTOM
;
194 /* By default, GINSN_F_INSN_REAL is not set in ginsn->flags. */
199 ginsn_new_symbol (const symbolS
*sym
, bool func_begin_p
)
201 ginsnS
*ginsn
= ginsn_alloc ();
202 ginsn
->type
= GINSN_TYPE_SYMBOL
;
205 ginsn
->flags
|= GINSN_F_FUNC_MARKER
;
210 ginsn_new_symbol_func_begin (const symbolS
*sym
)
212 return ginsn_new_symbol (sym
, true);
216 ginsn_new_symbol_func_end (const symbolS
*sym
)
218 return ginsn_new_symbol (sym
, false);
222 ginsn_new_symbol_user_label (const symbolS
*sym
)
224 ginsnS
*ginsn
= ginsn_new_symbol (sym
, false);
225 ginsn
->flags
|= GINSN_F_USER_LABEL
;
230 ginsn_new_add (const symbolS
*sym
, bool real_p
,
231 enum ginsn_src_type src1_type
, unsigned int src1_reg
, offsetT src1_disp
,
232 enum ginsn_src_type src2_type
, unsigned int src2_reg
, offsetT src2_disp
,
233 enum ginsn_dst_type dst_type
, unsigned int dst_reg
, offsetT dst_disp
)
235 ginsnS
*ginsn
= ginsn_init (GINSN_TYPE_ADD
, sym
, real_p
);
237 ginsn_set_src (&ginsn
->src
[0], src1_type
, src1_reg
, src1_disp
);
238 ginsn_set_src (&ginsn
->src
[1], src2_type
, src2_reg
, src2_disp
);
240 ginsn_set_dst (&ginsn
->dst
, dst_type
, dst_reg
, dst_disp
);
246 ginsn_new_and (const symbolS
*sym
, bool real_p
,
247 enum ginsn_src_type src1_type
, unsigned int src1_reg
, offsetT src1_disp
,
248 enum ginsn_src_type src2_type
, unsigned int src2_reg
, offsetT src2_disp
,
249 enum ginsn_dst_type dst_type
, unsigned int dst_reg
, offsetT dst_disp
)
251 ginsnS
*ginsn
= ginsn_init (GINSN_TYPE_AND
, sym
, real_p
);
253 ginsn_set_src (&ginsn
->src
[0], src1_type
, src1_reg
, src1_disp
);
254 ginsn_set_src (&ginsn
->src
[1], src2_type
, src2_reg
, src2_disp
);
256 ginsn_set_dst (&ginsn
->dst
, dst_type
, dst_reg
, dst_disp
);
262 ginsn_new_call (const symbolS
*sym
, bool real_p
,
263 enum ginsn_src_type src_type
, unsigned int src_reg
,
264 const symbolS
*src_text_sym
)
267 ginsnS
*ginsn
= ginsn_init (GINSN_TYPE_CALL
, sym
, real_p
);
269 ginsn_set_src (&ginsn
->src
[0], src_type
, src_reg
, 0);
271 if (src_type
== GINSN_SRC_SYMBOL
)
272 ginsn
->src
[0].sym
= src_text_sym
;
278 ginsn_new_jump (const symbolS
*sym
, bool real_p
,
279 enum ginsn_src_type src_type
, unsigned int src_reg
,
280 const symbolS
*src_ginsn_sym
)
282 ginsnS
*ginsn
= ginsn_init (GINSN_TYPE_JUMP
, sym
, real_p
);
284 ginsn_set_src (&ginsn
->src
[0], src_type
, src_reg
, 0);
286 if (src_type
== GINSN_SRC_SYMBOL
)
287 ginsn
->src
[0].sym
= src_ginsn_sym
;
293 ginsn_new_jump_cond (const symbolS
*sym
, bool real_p
,
294 enum ginsn_src_type src_type
, unsigned int src_reg
,
295 const symbolS
*src_ginsn_sym
)
297 ginsnS
*ginsn
= ginsn_init (GINSN_TYPE_JUMP_COND
, sym
, real_p
);
299 ginsn_set_src (&ginsn
->src
[0], src_type
, src_reg
, 0);
301 if (src_type
== GINSN_SRC_SYMBOL
)
302 ginsn
->src
[0].sym
= src_ginsn_sym
;
308 ginsn_new_mov (const symbolS
*sym
, bool real_p
,
309 enum ginsn_src_type src_type
, unsigned int src_reg
, offsetT src_disp
,
310 enum ginsn_dst_type dst_type
, unsigned int dst_reg
, offsetT dst_disp
)
312 ginsnS
*ginsn
= ginsn_init (GINSN_TYPE_MOV
, sym
, real_p
);
314 ginsn_set_src (&ginsn
->src
[0], src_type
, src_reg
, src_disp
);
316 ginsn_set_dst (&ginsn
->dst
, dst_type
, dst_reg
, dst_disp
);
322 ginsn_new_store (const symbolS
*sym
, bool real_p
,
323 enum ginsn_src_type src_type
, unsigned int src_reg
,
324 enum ginsn_dst_type dst_type
, unsigned int dst_reg
, offsetT dst_disp
)
326 ginsnS
*ginsn
= ginsn_init (GINSN_TYPE_STORE
, sym
, real_p
);
328 ginsn_set_src (&ginsn
->src
[0], src_type
, src_reg
, 0);
330 gas_assert (dst_type
== GINSN_DST_INDIRECT
);
331 ginsn_set_dst (&ginsn
->dst
, dst_type
, dst_reg
, dst_disp
);
337 ginsn_new_load (const symbolS
*sym
, bool real_p
,
338 enum ginsn_src_type src_type
, unsigned int src_reg
, offsetT src_disp
,
339 enum ginsn_dst_type dst_type
, unsigned int dst_reg
)
341 ginsnS
*ginsn
= ginsn_init (GINSN_TYPE_LOAD
, sym
, real_p
);
343 gas_assert (src_type
== GINSN_SRC_INDIRECT
);
344 ginsn_set_src (&ginsn
->src
[0], src_type
, src_reg
, src_disp
);
346 ginsn_set_dst (&ginsn
->dst
, dst_type
, dst_reg
, 0);
352 ginsn_new_sub (const symbolS
*sym
, bool real_p
,
353 enum ginsn_src_type src1_type
, unsigned int src1_reg
, offsetT src1_disp
,
354 enum ginsn_src_type src2_type
, unsigned int src2_reg
, offsetT src2_disp
,
355 enum ginsn_dst_type dst_type
, unsigned int dst_reg
, offsetT dst_disp
)
357 ginsnS
*ginsn
= ginsn_init (GINSN_TYPE_SUB
, sym
, real_p
);
359 ginsn_set_src (&ginsn
->src
[0], src1_type
, src1_reg
, src1_disp
);
360 ginsn_set_src (&ginsn
->src
[1], src2_type
, src2_reg
, src2_disp
);
362 ginsn_set_dst (&ginsn
->dst
, dst_type
, dst_reg
, dst_disp
);
367 /* PS: Note this API does not identify the displacement values of
368 src1/src2/dst. At this time, it is unnecessary for correctness to support
369 the additional argument. */
372 ginsn_new_other (const symbolS
*sym
, bool real_p
,
373 enum ginsn_src_type src1_type
, unsigned int src1_val
,
374 enum ginsn_src_type src2_type
, unsigned int src2_val
,
375 enum ginsn_dst_type dst_type
, unsigned int dst_reg
)
377 ginsnS
*ginsn
= ginsn_init (GINSN_TYPE_OTHER
, sym
, real_p
);
379 ginsn_set_src (&ginsn
->src
[0], src1_type
, src1_val
, src1_val
);
380 /* GINSN_SRC_INDIRECT src2_type is not expected. */
381 gas_assert (src2_type
!= GINSN_SRC_INDIRECT
);
382 ginsn_set_src (&ginsn
->src
[1], src2_type
, src2_val
, src2_val
);
384 ginsn_set_dst (&ginsn
->dst
, dst_type
, dst_reg
, 0);
390 ginsn_new_return (const symbolS
*sym
, bool real_p
)
392 ginsnS
*ginsn
= ginsn_init (GINSN_TYPE_RETURN
, sym
, real_p
);
397 ginsn_set_where (ginsnS
*ginsn
)
401 file
= as_where (&line
);
402 ginsn_set_file_line (ginsn
, file
, line
);
406 ginsn_link_next (ginsnS
*ginsn
, ginsnS
*next
)
410 /* Avoid data corruption by limiting the scope of the API. */
411 if (!ginsn
|| ginsn
->next
)
420 ginsn_track_reg_p (unsigned int dw2reg
, enum ginsn_gen_mode gmode
)
422 bool track_p
= false;
424 if (gmode
== GINSN_GEN_SCFI
&& dw2reg
<= SCFI_MAX_REG_ID
)
426 /* FIXME - rename this to tc_ ? */
427 track_p
|= SCFI_CALLEE_SAVED_REG_P (dw2reg
);
428 track_p
|= (dw2reg
== REG_FP
);
429 track_p
|= (dw2reg
== REG_SP
);
436 ginsn_indirect_jump_p (ginsnS
*ginsn
)
442 ret_p
= (ginsn
->type
== GINSN_TYPE_JUMP
443 && ginsn
->src
[0].type
== GINSN_SRC_REG
);
447 /* Return whether the GINSN is an unconditional jump to a label which is
448 defined locally in the scope of the block of insns, which are currently
449 being processed for GCFG creation. */
452 ginsn_direct_local_jump_p (ginsnS
*ginsn
)
454 bool local_p
= false;
455 const symbolS
*taken_label
;
460 if (ginsn
->type
== GINSN_TYPE_JUMP
461 && ginsn
->src
[0].type
== GINSN_SRC_SYMBOL
)
463 taken_label
= ginsn
->src
[0].sym
;
464 local_p
= (label_ginsn_map_find (taken_label
) != NULL
);
470 ginsn_src_print (struct ginsn_src
*src
)
473 char *src_str
= XNEWVEC (char, len
);
475 memset (src_str
, 0, len
);
480 snprintf (src_str
, len
, "%%r%d, ", ginsn_get_src_reg (src
));
483 snprintf (src_str
, len
, "%lld, ",
484 (long long int) ginsn_get_src_imm (src
));
486 case GINSN_SRC_INDIRECT
:
487 snprintf (src_str
, len
, "[%%r%d+%lld], ", ginsn_get_src_reg (src
),
488 (long long int) ginsn_get_src_disp (src
));
498 ginsn_dst_print (struct ginsn_dst
*dst
)
501 size_t len
= GINSN_LISTING_OPND_LEN
;
502 char *dst_str
= XNEWVEC (char, len
);
504 memset (dst_str
, 0, len
);
506 if (dst
->type
== GINSN_DST_REG
)
507 str_size
= snprintf (dst_str
, GINSN_LISTING_OPND_LEN
,
508 "%%r%d", ginsn_get_dst_reg (dst
));
509 else if (dst
->type
== GINSN_DST_INDIRECT
)
510 str_size
= snprintf (dst_str
, GINSN_LISTING_OPND_LEN
,
511 "[%%r%d+%lld]", ginsn_get_dst_reg (dst
),
512 (long long int) ginsn_get_dst_disp (dst
));
513 else if (dst
->type
!= GINSN_DST_UNKNOWN
)
514 /* Other dst types are unexpected. */
517 /* str_size will remain 0 when GINSN_DST_UNKNOWN. */
518 gas_assert (str_size
>= 0 && str_size
< GINSN_LISTING_OPND_LEN
);
524 ginsn_type_func_marker_print (ginsnS
*ginsn
)
527 static const char * const ginsn_sym_strs
[] =
528 { "", "FUNC_BEGIN", "FUNC_END" };
530 if (GINSN_F_FUNC_BEGIN_P (ginsn
))
532 else if (GINSN_F_FUNC_END_P (ginsn
))
535 return ginsn_sym_strs
[id
];
539 ginsn_print (ginsnS
*ginsn
)
541 struct ginsn_src
*src
;
542 struct ginsn_dst
*dst
;
544 size_t len
= GINSN_LISTING_LEN
;
545 char *ginsn_str
= XNEWVEC (char, len
);
547 memset (ginsn_str
, 0, len
);
549 str_size
= snprintf (ginsn_str
, GINSN_LISTING_LEN
, "ginsn: %s",
550 ginsn_type_names
[ginsn
->type
]);
551 gas_assert (str_size
>= 0 && str_size
< GINSN_LISTING_LEN
);
553 /* For some ginsn types, no further information is printed for now. */
554 if (ginsn
->type
== GINSN_TYPE_CALL
555 || ginsn
->type
== GINSN_TYPE_RETURN
)
557 else if (ginsn
->type
== GINSN_TYPE_SYMBOL
)
559 if (GINSN_F_USER_LABEL_P (ginsn
))
560 str_size
+= snprintf (ginsn_str
+ str_size
,
561 GINSN_LISTING_LEN
- str_size
,
562 " %s", S_GET_NAME (ginsn
->sym
));
564 str_size
+= snprintf (ginsn_str
+ str_size
,
565 GINSN_LISTING_LEN
- str_size
,
566 " %s", ginsn_type_func_marker_print (ginsn
));
571 src
= ginsn_get_src1 (ginsn
);
572 char *src_buf
= ginsn_src_print (src
);
573 str_size
+= snprintf (ginsn_str
+ str_size
, GINSN_LISTING_LEN
- str_size
,
576 gas_assert (str_size
>= 0 && str_size
< GINSN_LISTING_LEN
);
579 src
= ginsn_get_src2 (ginsn
);
580 src_buf
= ginsn_src_print (src
);
581 str_size
+= snprintf (ginsn_str
+ str_size
, GINSN_LISTING_LEN
- str_size
,
584 gas_assert (str_size
>= 0 && str_size
< GINSN_LISTING_LEN
);
587 dst
= ginsn_get_dst (ginsn
);
588 char *dst_buf
= ginsn_dst_print (dst
);
589 str_size
+= snprintf (ginsn_str
+ str_size
, GINSN_LISTING_LEN
- str_size
,
594 gas_assert (str_size
>= 0 && str_size
< GINSN_LISTING_LEN
);
599 gbb_cleanup (gbbS
**bbp
)
610 free (bb
->entry_state
);
611 bb
->entry_state
= NULL
;
615 free (bb
->exit_state
);
616 bb
->exit_state
= NULL
;
622 /* Add an edge from the source bb FROM_BB to the sink bb TO_BB. */
625 bb_add_edge (gbbS
* from_bb
, gbbS
*to_bb
)
627 gedgeS
*tmpedge
= NULL
;
631 if (!from_bb
|| !to_bb
)
634 /* Create a new edge object. */
635 gedge
= XCNEW (gedgeS
);
636 gedge
->dst_bb
= to_bb
;
638 gedge
->visited
= false;
641 if (from_bb
->out_gedges
== NULL
)
643 from_bb
->out_gedges
= gedge
;
644 from_bb
->num_out_gedges
++;
648 /* Get the head of the list. */
649 tmpedge
= from_bb
->out_gedges
;
652 /* Do not add duplicate edges. Duplicated edges will cause unwanted
653 failures in the forward and backward passes for SCFI. */
654 if (tmpedge
->dst_bb
== to_bb
)
660 tmpedge
= tmpedge
->next
;
667 tmpedge
->next
= gedge
;
668 from_bb
->num_out_gedges
++;
676 cfg_add_bb (gcfgS
*gcfg
, gbbS
*gbb
)
678 gbbS
*last_bb
= NULL
;
684 last_bb
= gcfg
->root_bb
;
685 while (last_bb
->next
)
686 last_bb
= last_bb
->next
;
692 gbb
->id
= gcfg
->num_gbbs
;
696 add_bb_at_ginsn (const symbolS
*func
, gcfgS
*gcfg
, ginsnS
*ginsn
, gbbS
*prev_bb
,
699 /* Return the already existing basic block (if present), which begins with
700 GINSN, in the given GCFG. Return NULL otherwise. */
703 find_bb (gcfgS
*gcfg
, ginsnS
*ginsn
)
705 gbbS
*found_bb
= NULL
;
713 cfg_for_each_bb (gcfg
, gbb
)
715 if (gbb
->first_ginsn
== ginsn
)
721 /* Must be found because ginsn is visited. */
722 gas_assert (found_bb
);
728 /* Get the basic block starting at GINSN in the GCFG.
730 If not already present, the function will make one, while adding an edge
731 from the PREV_BB to it. */
734 find_or_make_bb (const symbolS
*func
, gcfgS
*gcfg
, ginsnS
*ginsn
, gbbS
*prev_bb
,
737 gbbS
*found_bb
= NULL
;
739 found_bb
= find_bb (gcfg
, ginsn
);
741 found_bb
= add_bb_at_ginsn (func
, gcfg
, ginsn
, prev_bb
, errp
);
743 gas_assert (found_bb
);
744 gas_assert (found_bb
->first_ginsn
== ginsn
);
749 /* Add basic block(s) for all reachable, unvisited ginsns, starting from GINSN,
750 to the given GCFG. Also add an edge from the PREV_BB to the root of the
751 newly added basic block(s).
753 This is a recursive function which returns the root of the added basic
757 add_bb_at_ginsn (const symbolS
*func
, gcfgS
*gcfg
, ginsnS
*ginsn
, gbbS
*prev_bb
,
760 gbbS
*root_bb
= NULL
;
761 gbbS
*current_bb
= NULL
;
762 ginsnS
*target_ginsn
= NULL
;
763 const symbolS
*taken_label
;
765 /* Create a new bb. N.B. The caller must ensure bb with this ginsn does not
767 gas_assert (!find_bb (gcfg
, ginsn
));
768 root_bb
= XCNEW (gbbS
);
769 cfg_add_bb (gcfg
, root_bb
);
770 root_bb
->first_ginsn
= ginsn
;
772 current_bb
= root_bb
;
776 /* Skip these as they may be right after a GINSN_TYPE_RETURN.
777 For GINSN_TYPE_RETURN, we have already considered that as
778 end of bb, and a logical exit from function. */
779 if (GINSN_F_FUNC_END_P (ginsn
))
781 /* Dont mark them visited yet though, leaving the option of these
782 being visited via other control flows as applicable. */
789 /* If the ginsn has been visited earlier, the bb must exist by now
791 prev_bb
= current_bb
;
792 current_bb
= find_bb (gcfg
, ginsn
);
793 gas_assert (current_bb
);
794 /* Add edge from the prev_bb. */
796 bb_add_edge (prev_bb
, current_bb
);
799 else if (current_bb
&& current_bb
->first_ginsn
!= ginsn
800 && GINSN_F_USER_LABEL_P (ginsn
))
802 /* Create new bb starting at ginsn for (user-defined) label. This is
803 likely going to be a destination of a some control flow. */
804 prev_bb
= current_bb
;
805 current_bb
= find_or_make_bb (func
, gcfg
, ginsn
, prev_bb
, errp
);
806 bb_add_edge (prev_bb
, current_bb
);
810 if (current_bb
== NULL
)
812 current_bb
= XCNEW (gbbS
);
813 cfg_add_bb (gcfg
, current_bb
);
814 current_bb
->first_ginsn
= ginsn
;
815 /* Add edge for the Not Taken, or Fall-through path. */
817 bb_add_edge (prev_bb
, current_bb
);
820 ginsn
->visited
= true;
821 current_bb
->num_ginsns
++;
822 current_bb
->last_ginsn
= ginsn
;
824 /* Note that BB is _not_ split on ginsn of type GINSN_TYPE_CALL. */
825 if (ginsn
->type
== GINSN_TYPE_JUMP
826 || ginsn
->type
== GINSN_TYPE_JUMP_COND
827 || ginsn
->type
== GINSN_TYPE_RETURN
)
829 /* Indirect jumps must not be seen here. The caller must have
830 already checked for that. */
831 gas_assert (!ginsn_indirect_jump_p (ginsn
));
833 /* Handle direct jumps. For unconditional direct jumps, where the
834 target is not local to the function, treat them later as similar
835 to an exit from function (in the else block). */
836 if (ginsn
->type
== GINSN_TYPE_JUMP_COND
837 || ginsn_direct_local_jump_p (ginsn
))
839 gas_assert (ginsn
->src
[0].type
== GINSN_SRC_SYMBOL
);
840 taken_label
= ginsn
->src
[0].sym
;
841 gas_assert (taken_label
);
843 /* Preserve the prev_bb to be the source bb as we are going to
844 follow the taken path of the conditional branch soon. */
845 prev_bb
= current_bb
;
847 /* Follow the target on the taken path. */
848 target_ginsn
= label_ginsn_map_find (taken_label
);
849 /* Add the bb for the target of the taken branch. */
852 current_bb
= find_or_make_bb (func
, gcfg
, target_ginsn
,
854 gas_assert (prev_bb
);
855 bb_add_edge (prev_bb
, current_bb
);
860 *errp
= GCFG_JLABEL_NOT_PRESENT
;
861 as_warn_where (ginsn
->file
, ginsn
->line
,
862 _("missing label '%s' in func '%s' may result in imprecise cfg"),
863 S_GET_NAME (taken_label
), S_GET_NAME (func
));
866 if (ginsn
->type
== GINSN_TYPE_JUMP_COND
)
868 /* Add the bb for the fall through path. */
869 current_bb
= find_or_make_bb (func
, gcfg
, ginsn
->next
,
871 gas_assert (prev_bb
);
872 bb_add_edge (prev_bb
, current_bb
);
877 /* Unconditional jump. Current BB has been processed. */
879 /* We'll come back to the ginsns following these (local)
880 unconditional jmps from another path if they are indeed
887 gas_assert (ginsn
->type
== GINSN_TYPE_RETURN
888 || (ginsn
->type
== GINSN_TYPE_JUMP
889 && !ginsn_direct_local_jump_p (ginsn
)));
890 /* Current BB has been processed. */
893 /* We'll come back to the ginsns following GINSN_TYPE_RETURN or
894 other (non-local) unconditional jmps from another path if they
895 are indeed reachable code. */
907 gbbs_compare (const void *v1
, const void *v2
)
909 const gbbS
*bb1
= *(const gbbS
**) v1
;
910 const gbbS
*bb2
= *(const gbbS
**) v2
;
912 if (bb1
->first_ginsn
->id
< bb2
->first_ginsn
->id
)
914 else if (bb1
->first_ginsn
->id
> bb2
->first_ginsn
->id
)
916 else if (bb1
->first_ginsn
->id
== bb2
->first_ginsn
->id
)
922 /* Synthesize DWARF CFI and emit it. */
925 ginsn_pass_execute_scfi (const symbolS
*func
, gcfgS
*gcfg
, gbbS
*root_bb
)
927 int err
= scfi_synthesize_dw2cfi (func
, gcfg
, root_bb
);
929 scfi_emit_dw2cfi (func
);
934 /* Traverse the list of ginsns for the function and warn if some
935 ginsns are not visited.
937 FIXME - this code assumes the caller has already performed a pass over
938 ginsns such that the reachable ginsns are already marked. Revisit this - we
939 should ideally make this pass self-sufficient. */
942 ginsn_pass_warn_unreachable_code (const symbolS
*func
,
943 gcfgS
*gcfg ATTRIBUTE_UNUSED
,
947 bool unreach_p
= false;
949 if (!gcfg
|| !func
|| !root_ginsn
)
956 /* Some ginsns of type GINSN_TYPE_SYMBOL remain unvisited. Some
957 may even be excluded from the CFG as they are not reachable, given
958 their function, e.g., user labels after return machine insn. */
960 && !GINSN_F_FUNC_END_P (ginsn
)
961 && !GINSN_F_USER_LABEL_P (ginsn
))
970 as_warn_where (ginsn
->file
, ginsn
->line
,
971 _("GINSN: found unreachable code in func '%s'"),
978 gcfg_get_bbs_in_prog_order (gcfgS
*gcfg
, gbbS
**prog_order_bbs
)
986 cfg_for_each_bb (gcfg
, gbb
)
988 gas_assert (i
< gcfg
->num_gbbs
);
989 prog_order_bbs
[i
++] = gbb
;
992 qsort (prog_order_bbs
, gcfg
->num_gbbs
, sizeof (gbbS
*), gbbs_compare
);
995 /* Build the control flow graph for the ginsns of the function.
997 It is important that the target adds an appropriate ginsn:
999 - GINSN_TYPE_JUMP_COND,
1002 at the associated points in the function. The correctness of the CFG
1003 depends on the accuracy of these 'change of flow instructions'. */
1006 gcfg_build (const symbolS
*func
, int *errp
)
1009 ginsnS
*first_ginsn
;
1011 gcfg
= XCNEW (gcfgS
);
1012 first_ginsn
= frchain_now
->frch_ginsn_data
->gins_rootP
;
1013 add_bb_at_ginsn (func
, gcfg
, first_ginsn
, NULL
/* prev_bb. */, errp
);
1019 gcfg_cleanup (gcfgS
**gcfgp
)
1023 gedgeS
*edge
, *next_edge
;
1025 if (!gcfgp
|| !*gcfgp
)
1029 bb
= gcfg_get_rootbb (cfg
);
1035 /* Cleanup all the edges. */
1036 edge
= bb
->out_gedges
;
1039 next_edge
= edge
->next
;
1053 gcfg_get_rootbb (gcfgS
*gcfg
)
1055 gbbS
*rootbb
= NULL
;
1057 if (!gcfg
|| !gcfg
->num_gbbs
)
1060 rootbb
= gcfg
->root_bb
;
1066 gcfg_print (const gcfgS
*gcfg
, FILE *outfile
)
1069 gedgeS
*gedge
= NULL
;
1070 uint64_t total_ginsns
= 0;
1072 cfg_for_each_bb(gcfg
, gbb
)
1074 fprintf (outfile
, "BB [%" PRIu64
"] with num insns: %" PRIu64
,
1075 gbb
->id
, gbb
->num_ginsns
);
1076 fprintf (outfile
, " [insns: %u to %u]\n",
1077 gbb
->first_ginsn
->line
, gbb
->last_ginsn
->line
);
1078 total_ginsns
+= gbb
->num_ginsns
;
1079 bb_for_each_edge(gbb
, gedge
)
1080 fprintf (outfile
, " outgoing edge to %" PRIu64
"\n",
1083 fprintf (outfile
, "\nTotal ginsns in all GBBs = %" PRIu64
"\n",
1088 frch_ginsn_data_init (const symbolS
*func
, symbolS
*start_addr
,
1089 enum ginsn_gen_mode gmode
)
1091 /* FIXME - error out if prev object is not free'd ? */
1092 frchain_now
->frch_ginsn_data
= XCNEW (struct frch_ginsn_data
);
1094 frchain_now
->frch_ginsn_data
->mode
= gmode
;
1095 /* Annotate with the current function symbol. */
1096 frchain_now
->frch_ginsn_data
->func
= func
;
1097 /* Create a new start address symbol now. */
1098 frchain_now
->frch_ginsn_data
->start_addr
= start_addr
;
1099 /* Assume the set of ginsn are apt for CFG creation, by default. */
1100 frchain_now
->frch_ginsn_data
->gcfg_apt_p
= true;
1102 frchain_now
->frch_ginsn_data
->label_ginsn_map
= str_htab_create ();
1106 frch_ginsn_data_cleanup (void)
1108 ginsnS
*ginsn
= NULL
;
1109 ginsnS
*next_ginsn
= NULL
;
1111 ginsn
= frchain_now
->frch_ginsn_data
->gins_rootP
;
1114 next_ginsn
= ginsn
->next
;
1115 ginsn_cleanup (&ginsn
);
1119 if (frchain_now
->frch_ginsn_data
->label_ginsn_map
)
1120 htab_delete (frchain_now
->frch_ginsn_data
->label_ginsn_map
);
1122 free (frchain_now
->frch_ginsn_data
);
1123 frchain_now
->frch_ginsn_data
= NULL
;
1126 /* Append GINSN to the list of ginsns for the current function being
1130 frch_ginsn_data_append (ginsnS
*ginsn
)
1132 ginsnS
*last
= NULL
;
1133 ginsnS
*temp
= NULL
;
1139 if (frchain_now
->frch_ginsn_data
->gins_lastP
)
1140 id
= frchain_now
->frch_ginsn_data
->gins_lastP
->id
;
1142 /* Do the necessary preprocessing on the set of input GINSNs:
1143 - Update each ginsn with its ID.
1144 While you iterate, also keep gcfg_apt_p updated by checking whether any
1145 ginsn is inappropriate for GCFG creation. */
1151 if (ginsn_indirect_jump_p (temp
))
1152 frchain_now
->frch_ginsn_data
->gcfg_apt_p
= false;
1154 if (listing
& LISTING_GINSN_SCFI
)
1155 listing_newline (ginsn_print (temp
));
1157 /* The input GINSN may be a linked list of multiple ginsns chained
1158 together. Find the last ginsn in the input chain of ginsns. */
1164 /* Link in the ginsn to the tail. */
1165 if (!frchain_now
->frch_ginsn_data
->gins_rootP
)
1166 frchain_now
->frch_ginsn_data
->gins_rootP
= ginsn
;
1168 ginsn_link_next (frchain_now
->frch_ginsn_data
->gins_lastP
, ginsn
);
1170 frchain_now
->frch_ginsn_data
->gins_lastP
= last
;
1176 frch_ginsn_gen_mode (void)
1178 enum ginsn_gen_mode gmode
= GINSN_GEN_NONE
;
1180 if (frchain_now
->frch_ginsn_data
)
1181 gmode
= frchain_now
->frch_ginsn_data
->mode
;
1187 ginsn_data_begin (const symbolS
*func
)
1191 /* The previous block of asm must have been processed by now. */
1192 if (frchain_now
->frch_ginsn_data
)
1193 as_bad (_("GINSN process for prev func not done"));
1195 /* FIXME - hard code the mode to GINSN_GEN_SCFI.
1196 This can be changed later when other passes on ginsns are formalised. */
1197 frch_ginsn_data_init (func
, symbol_temp_new_now (), GINSN_GEN_SCFI
);
1199 /* Create and insert ginsn with function begin marker. */
1200 ginsn
= ginsn_new_symbol_func_begin (func
);
1201 frch_ginsn_data_append (ginsn
);
1207 ginsn_data_end (const symbolS
*label
)
1212 const symbolS
*func
;
1215 if (!frchain_now
->frch_ginsn_data
)
1218 /* Insert Function end marker. */
1219 ginsn
= ginsn_new_symbol_func_end (label
);
1220 frch_ginsn_data_append (ginsn
);
1222 func
= frchain_now
->frch_ginsn_data
->func
;
1224 /* Build the cfg of ginsn(s) of the function. */
1225 if (!frchain_now
->frch_ginsn_data
->gcfg_apt_p
)
1227 as_bad (_("untraceable control flow for func '%s'"),
1232 gcfg
= gcfg_build (func
, &err
);
1234 root_bb
= gcfg_get_rootbb (gcfg
);
1237 as_bad (_("Bad cfg of ginsn of func '%s'"), S_GET_NAME (func
));
1241 /* Execute the desired passes on ginsns. */
1242 err
= ginsn_pass_execute_scfi (func
, gcfg
, root_bb
);
1246 /* Other passes, e.g., warn for unreachable code can be enabled too. */
1247 ginsn
= frchain_now
->frch_ginsn_data
->gins_rootP
;
1248 err
= ginsn_pass_warn_unreachable_code (func
, gcfg
, ginsn
);
1252 gcfg_cleanup (&gcfg
);
1253 frch_ginsn_data_cleanup ();
1258 /* Add GINSN_TYPE_SYMBOL type ginsn for user-defined labels. These may be
1259 branch targets, and hence are necessary for control flow graph. */
1262 ginsn_frob_label (const symbolS
*label
)
1264 ginsnS
*label_ginsn
;
1268 if (frchain_now
->frch_ginsn_data
)
1270 /* PS: Note how we keep the actual LABEL symbol as ginsn->sym.
1271 Take care to avoid inadvertent updates or cleanups of symbols. */
1272 label_ginsn
= ginsn_new_symbol_user_label (label
);
1273 /* Keep the location updated. */
1274 file
= as_where (&line
);
1275 ginsn_set_file_line (label_ginsn
, file
, line
);
1277 frch_ginsn_data_append (label_ginsn
);
1279 label_ginsn_map_insert (label
, label_ginsn
);
1284 ginsn_data_func_symbol (void)
1286 const symbolS
*func
= NULL
;
1288 if (frchain_now
->frch_ginsn_data
)
1289 func
= frchain_now
->frch_ginsn_data
->func
;
1297 ginsn_data_begin (const symbolS
*func ATTRIBUTE_UNUSED
)
1299 as_bad (_("ginsn unsupported for target"));
1304 ginsn_data_end (const symbolS
*label ATTRIBUTE_UNUSED
)
1306 as_bad (_("ginsn unsupported for target"));
1311 ginsn_frob_label (const symbolS
*sym ATTRIBUTE_UNUSED
)
1317 ginsn_data_func_symbol (void)
1322 #endif /* TARGET_USE_GINSN. */