aboutsummaryrefslogtreecommitdiff
path: root/gas/ginsn.c
blob: 20e83c5d51e23883cbde9b7c27be3c0a6ab2edc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
/* ginsn.h - GAS instruction representation.
   Copyright (C) 2023 Free Software Foundation, Inc.

   This file is part of GAS, the GNU Assembler.

   GAS 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.

   GAS 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 GAS; see the file COPYING.  If not, write to the Free
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */

#include "as.h"
#include "subsegs.h"
#include "ginsn.h"
#include "scfi.h"

#ifdef TARGET_USE_GINSN

static const char *const ginsn_type_names[] =
{
#define _GINSN_TYPE_ITEM(NAME, STR) STR,
  _GINSN_TYPES
#undef _GINSN_TYPE_ITEM
};

static ginsnS *
ginsn_alloc (void)
{
  ginsnS *ginsn = XCNEW (ginsnS);
  return ginsn;
}

static ginsnS *
ginsn_init (enum ginsn_type type, const symbolS *sym, bool real_p)
{
  ginsnS *ginsn = ginsn_alloc ();
  ginsn->type = type;
  ginsn->sym = sym;
  if (real_p)
    ginsn->flags |= GINSN_F_INSN_REAL;
  return ginsn;
}

static void
ginsn_cleanup (ginsnS **ginsnp)
{
  ginsnS *ginsn;

  if (!ginsnp || !*ginsnp)
    return;

  ginsn = *ginsnp;
  if (ginsn->scfi_ops)
    {
      scfi_ops_cleanup (ginsn->scfi_ops);
      ginsn->scfi_ops = NULL;
    }

  free (ginsn);
  *ginsnp = NULL;
}

static void
ginsn_set_src (struct ginsn_src *src, enum ginsn_src_type type, unsigned int reg,
	       offsetT immdisp)
{
  if (!src)
    return;

  src->type = type;
  /* Even when the use-case is SCFI, the value of reg may be > SCFI_MAX_REG_ID.
     E.g., in AMD64, push fs etc.  */
  src->reg = reg;
  src->immdisp = immdisp;
}

static void
ginsn_set_dst (struct ginsn_dst *dst, enum ginsn_dst_type type, unsigned int reg,
	       offsetT disp)
{
  if (!dst)
    return;

  dst->type = type;
  dst->reg = reg;

  if (type == GINSN_DST_INDIRECT)
    dst->disp = disp;
}

static void
ginsn_set_file_line (ginsnS *ginsn, const char *file, unsigned int line)
{
  if (!ginsn)
    return;

  ginsn->file = file;
  ginsn->line = line;
}

struct ginsn_src *
ginsn_get_src1 (ginsnS *ginsn)
{
  return &ginsn->src[0];
}

struct ginsn_src *
ginsn_get_src2 (ginsnS *ginsn)
{
  return &ginsn->src[1];
}

struct ginsn_dst *
ginsn_get_dst (ginsnS *ginsn)
{
  return &ginsn->dst;
}

unsigned int
ginsn_get_src_reg (struct ginsn_src *src)
{
  return src->reg;
}

enum ginsn_src_type
ginsn_get_src_type (struct ginsn_src *src)
{
  return src->type;
}

offsetT
ginsn_get_src_disp (struct ginsn_src *src)
{
  return src->immdisp;
}

offsetT
ginsn_get_src_imm (struct ginsn_src *src)
{
  return src->immdisp;
}

unsigned int
ginsn_get_dst_reg (struct ginsn_dst *dst)
{
  return dst->reg;
}

enum ginsn_dst_type
ginsn_get_dst_type (struct ginsn_dst *dst)
{
  return dst->type;
}

offsetT
ginsn_get_dst_disp (struct ginsn_dst *dst)
{
  return dst->disp;
}

void
label_ginsn_map_insert (const symbolS *label, ginsnS *ginsn)
{
  const char *name = S_GET_NAME (label);
  str_hash_insert (frchain_now->frch_ginsn_data->label_ginsn_map,
		   name, ginsn, 0 /* noreplace.  */);
}

ginsnS *
label_ginsn_map_find (const symbolS *label)
{
  const char *name = S_GET_NAME (label);
  ginsnS *ginsn
    = (ginsnS *) str_hash_find (frchain_now->frch_ginsn_data->label_ginsn_map,
				name);
  return ginsn;
}

ginsnS *
ginsn_new_phantom (const symbolS *sym)
{
  ginsnS *ginsn = ginsn_alloc ();
  ginsn->type = GINSN_TYPE_PHANTOM;
  ginsn->sym = sym;
  /* By default, GINSN_F_INSN_REAL is not set in ginsn->flags.  */
  return ginsn;
}

ginsnS *
ginsn_new_symbol (const symbolS *sym, bool func_begin_p)
{
  ginsnS *ginsn = ginsn_alloc ();
  ginsn->type = GINSN_TYPE_SYMBOL;
  ginsn->sym = sym;
  if (func_begin_p)
    ginsn->flags |= GINSN_F_FUNC_MARKER;
  return ginsn;
}

ginsnS *
ginsn_new_symbol_func_begin (const symbolS *sym)
{
  return ginsn_new_symbol (sym, true);
}

ginsnS *
ginsn_new_symbol_func_end (const symbolS *sym)
{
  return ginsn_new_symbol (sym, false);
}

ginsnS *
ginsn_new_symbol_user_label (const symbolS *sym)
{
  ginsnS *ginsn = ginsn_new_symbol (sym, false);
  ginsn->flags |= GINSN_F_USER_LABEL;
  return ginsn;
}

ginsnS *
ginsn_new_add (const symbolS *sym, bool real_p,
	       enum ginsn_src_type src1_type, unsigned int src1_reg, offsetT src1_disp,
	       enum ginsn_src_type src2_type, unsigned int src2_reg, offsetT src2_disp,
	       enum ginsn_dst_type dst_type, unsigned int dst_reg, offsetT dst_disp)
{
  ginsnS *ginsn = ginsn_init (GINSN_TYPE_ADD, sym, real_p);
  /* src info.  */
  ginsn_set_src (&ginsn->src[0], src1_type, src1_reg, src1_disp);
  ginsn_set_src (&ginsn->src[1], src2_type, src2_reg, src2_disp);
  /* dst info.  */
  ginsn_set_dst (&ginsn->dst, dst_type, dst_reg, dst_disp);

  return ginsn;
}

ginsnS *
ginsn_new_and (const symbolS *sym, bool real_p,
	       enum ginsn_src_type src1_type, unsigned int src1_reg, offsetT src1_disp,
	       enum ginsn_src_type src2_type, unsigned int src2_reg, offsetT src2_disp,
	       enum ginsn_dst_type dst_type, unsigned int dst_reg, offsetT dst_disp)
{
  ginsnS *ginsn = ginsn_init (GINSN_TYPE_AND, sym, real_p);
  /* src info.  */
  ginsn_set_src (&ginsn->src[0], src1_type, src1_reg, src1_disp);
  ginsn_set_src (&ginsn->src[1], src2_type, src2_reg, src2_disp);
  /* dst info.  */
  ginsn_set_dst (&ginsn->dst, dst_type, dst_reg, dst_disp);

  return ginsn;
}

ginsnS *
ginsn_new_call (const symbolS *sym, bool real_p,
		enum ginsn_src_type src_type, unsigned int src_reg,
		const symbolS *src_text_sym)

{
  ginsnS *ginsn = ginsn_init (GINSN_TYPE_CALL, sym, real_p);
  /* src info.  */
  ginsn_set_src (&ginsn->src[0], src_type, src_reg, 0);

  if (src_type == GINSN_SRC_SYMBOL)
    ginsn->src[0].sym = src_text_sym;

  return ginsn;
}

ginsnS *
ginsn_new_jump (const symbolS *sym, bool real_p,
		enum ginsn_src_type src_type, unsigned int src_reg,
		const symbolS *src_ginsn_sym)
{
  ginsnS *ginsn = ginsn_init (GINSN_TYPE_JUMP, sym, real_p);
  /* src info.  */
  ginsn_set_src (&ginsn->src[0], src_type, src_reg, 0);

  if (src_type == GINSN_SRC_SYMBOL)
    ginsn->src[0].sym = src_ginsn_sym;

  return ginsn;
}

ginsnS *
ginsn_new_jump_cond (const symbolS *sym, bool real_p,
		     enum ginsn_src_type src_type, unsigned int src_reg,
		     const symbolS *src_ginsn_sym)
{
  ginsnS *ginsn = ginsn_init (GINSN_TYPE_JUMP_COND, sym, real_p);
  /* src info.  */
  ginsn_set_src (&ginsn->src[0], src_type, src_reg, 0);

  if (src_type == GINSN_SRC_SYMBOL)
    ginsn->src[0].sym = src_ginsn_sym;

  return ginsn;
}

ginsnS *
ginsn_new_mov (const symbolS *sym, bool real_p,
	       enum ginsn_src_type src_type, unsigned int src_reg, offsetT src_disp,
	       enum ginsn_dst_type dst_type, unsigned int dst_reg, offsetT dst_disp)
{
  ginsnS *ginsn = ginsn_init (GINSN_TYPE_MOV, sym, real_p);
  /* src info.  */
  ginsn_set_src (&ginsn->src[0], src_type, src_reg, src_disp);
  /* dst info.  */
  ginsn_set_dst (&ginsn->dst, dst_type, dst_reg, dst_disp);

  return ginsn;
}

ginsnS *
ginsn_new_store (const symbolS *sym, bool real_p,
		 enum ginsn_src_type src_type, unsigned int src_reg,
		 enum ginsn_dst_type dst_type, unsigned int dst_reg, offsetT dst_disp)
{
  ginsnS *ginsn = ginsn_init (GINSN_TYPE_STORE, sym, real_p);
  /* src info.  */
  ginsn_set_src (&ginsn->src[0], src_type, src_reg, 0);
  /* dst info.  */
  gas_assert (dst_type == GINSN_DST_INDIRECT);
  ginsn_set_dst (&ginsn->dst, dst_type, dst_reg, dst_disp);

  return ginsn;
}

ginsnS *
ginsn_new_load (const symbolS *sym, bool real_p,
		enum ginsn_src_type src_type, unsigned int src_reg, offsetT src_disp,
		enum ginsn_dst_type dst_type, unsigned int dst_reg)
{
  ginsnS *ginsn = ginsn_init (GINSN_TYPE_LOAD, sym, real_p);
  /* src info.  */
  gas_assert (src_type == GINSN_SRC_INDIRECT);
  ginsn_set_src (&ginsn->src[0], src_type, src_reg, src_disp);
  /* dst info.  */
  ginsn_set_dst (&ginsn->dst, dst_type, dst_reg, 0);

  return ginsn;
}

ginsnS *
ginsn_new_sub (const symbolS *sym, bool real_p,
	       enum ginsn_src_type src1_type, unsigned int src1_reg, offsetT src1_disp,
	       enum ginsn_src_type src2_type, unsigned int src2_reg, offsetT src2_disp,
	       enum ginsn_dst_type dst_type, unsigned int dst_reg, offsetT dst_disp)
{
  ginsnS *ginsn = ginsn_init (GINSN_TYPE_SUB, sym, real_p);
  /* src info.  */
  ginsn_set_src (&ginsn->src[0], src1_type, src1_reg, src1_disp);
  ginsn_set_src (&ginsn->src[1], src2_type, src2_reg, src2_disp);
  /* dst info.  */
  ginsn_set_dst (&ginsn->dst, dst_type, dst_reg, dst_disp);

  return ginsn;
}

/* PS: Note this API does not identify the displacement values of
   src1/src2/dst.  At this time, it is unnecessary for correctness to support
   the additional argument.  */

ginsnS *
ginsn_new_other (const symbolS *sym, bool real_p,
		 enum ginsn_src_type src1_type, unsigned int src1_val,
		 enum ginsn_src_type src2_type, unsigned int src2_val,
		 enum ginsn_dst_type dst_type, unsigned int dst_reg)
{
  ginsnS *ginsn = ginsn_init (GINSN_TYPE_OTHER, sym, real_p);
  /* src info.  */
  ginsn_set_src (&ginsn->src[0], src1_type, src1_val, src1_val);
  /* GINSN_SRC_INDIRECT src2_type is not expected.  */
  gas_assert (src2_type != GINSN_SRC_INDIRECT);
  ginsn_set_src (&ginsn->src[1], src2_type, src2_val, src2_val);
  /* dst info.  */
  ginsn_set_dst (&ginsn->dst, dst_type, dst_reg, 0);

  return ginsn;
}

ginsnS *
ginsn_new_return (const symbolS *sym, bool real_p)
{
  ginsnS *ginsn = ginsn_init (GINSN_TYPE_RETURN, sym, real_p);
  return ginsn;
}

void
ginsn_set_where (ginsnS *ginsn)
{
  const char *file;
  unsigned int line;
  file = as_where (&line);
  ginsn_set_file_line (ginsn, file, line);
}

int
ginsn_link_next (ginsnS *ginsn, ginsnS *next)
{
  int ret = 0;

  /* Avoid data corruption by limiting the scope of the API.  */
  if (!ginsn || ginsn->next)
    return 1;

  ginsn->next = next;

  return ret;
}

bool
ginsn_track_reg_p (unsigned int dw2reg, enum ginsn_gen_mode gmode)
{
  bool track_p = false;

  if (gmode == GINSN_GEN_SCFI && dw2reg <= SCFI_MAX_REG_ID)
    {
      /* FIXME - rename this to tc_ ? */
      track_p |= SCFI_CALLEE_SAVED_REG_P (dw2reg);
      track_p |= (dw2reg == REG_FP);
      track_p |= (dw2reg == REG_SP);
    }

  return track_p;
}

static bool
ginsn_indirect_jump_p (ginsnS *ginsn)
{
  bool ret_p = false;
  if (!ginsn)
    return ret_p;

  ret_p = (ginsn->type == GINSN_TYPE_JUMP
	   && ginsn->src[0].type == GINSN_SRC_REG);
  return ret_p;
}

/* Return whether the GINSN is an unconditional jump to a label which is
   defined locally in the scope of the block of insns, which are currently
   being processed for GCFG creation.  */

static bool
ginsn_direct_local_jump_p (ginsnS *ginsn)
{
  bool local_p = false;
  const symbolS *taken_label;

  if (!ginsn)
    return local_p;

  if (ginsn->type == GINSN_TYPE_JUMP
      && ginsn->src[0].type == GINSN_SRC_SYMBOL)
    {
      taken_label = ginsn->src[0].sym;
      local_p = (label_ginsn_map_find (taken_label) != NULL);
    }
  return local_p;
}

static char *
ginsn_src_print (struct ginsn_src *src)
{
  size_t len = 40;
  char *src_str = XNEWVEC (char, len);

  memset (src_str, 0, len);

  switch (src->type)
    {
    case GINSN_SRC_REG:
      snprintf (src_str, len, "%%r%d, ", ginsn_get_src_reg (src));
      break;
    case GINSN_SRC_IMM:
      snprintf (src_str, len, "%lld, ",
		(long long int) ginsn_get_src_imm (src));
      break;
    case GINSN_SRC_INDIRECT:
      snprintf (src_str, len, "[%%r%d+%lld], ", ginsn_get_src_reg (src),
		(long long int) ginsn_get_src_disp (src));
      break;
    default:
      break;
    }

  return src_str;
}

static char*
ginsn_dst_print (struct ginsn_dst *dst)
{
  int str_size = 0;
  size_t len = GINSN_LISTING_OPND_LEN;
  char *dst_str = XNEWVEC (char, len);

  memset (dst_str, 0, len);

  if (dst->type == GINSN_DST_REG)
    str_size = snprintf (dst_str, GINSN_LISTING_OPND_LEN,
			 "%%r%d", ginsn_get_dst_reg (dst));
  else if (dst->type == GINSN_DST_INDIRECT)
    str_size = snprintf (dst_str, GINSN_LISTING_OPND_LEN,
			 "[%%r%d+%lld]", ginsn_get_dst_reg (dst),
			 (long long int) ginsn_get_dst_disp (dst));
  else if (dst->type != GINSN_DST_UNKNOWN)
    /* Other dst types are unexpected.  */
    gas_assert (false);

  /* str_size will remain 0 when GINSN_DST_UNKNOWN.  */
  gas_assert (str_size >= 0 && str_size < GINSN_LISTING_OPND_LEN);

  return dst_str;
}

static const char*
ginsn_type_func_marker_print (ginsnS *ginsn)
{
  int id = 0;
  static const char * const ginsn_sym_strs[] =
    { "", "FUNC_BEGIN", "FUNC_END" };

  if (GINSN_F_FUNC_BEGIN_P (ginsn))
    id = 1;
  else if (GINSN_F_FUNC_END_P (ginsn))
    id = 2;

  return ginsn_sym_strs[id];
}

static char*
ginsn_print (ginsnS *ginsn)
{
  struct ginsn_src *src;
  struct ginsn_dst *dst;
  int str_size = 0;
  size_t len = GINSN_LISTING_LEN;
  char *ginsn_str = XNEWVEC (char, len);

  memset (ginsn_str, 0, len);

  str_size = snprintf (ginsn_str, GINSN_LISTING_LEN, "ginsn: %s",
		       ginsn_type_names[ginsn->type]);
  gas_assert (str_size >= 0 && str_size < GINSN_LISTING_LEN);

  /* For some ginsn types, no further information is printed for now.  */
  if (ginsn->type == GINSN_TYPE_CALL
      || ginsn->type == GINSN_TYPE_RETURN)
    goto end;
  else if (ginsn->type == GINSN_TYPE_SYMBOL)
    {
      if (GINSN_F_USER_LABEL_P (ginsn))
	str_size += snprintf (ginsn_str + str_size,
			      GINSN_LISTING_LEN - str_size,
			      " %s", S_GET_NAME (ginsn->sym));
      else
	str_size += snprintf (ginsn_str + str_size,
			      GINSN_LISTING_LEN - str_size,
			      " %s", ginsn_type_func_marker_print (ginsn));
      goto end;
    }

  /* src 1.  */
  src = ginsn_get_src1 (ginsn);
  char *src_buf = ginsn_src_print (src);
  str_size += snprintf (ginsn_str + str_size, GINSN_LISTING_LEN - str_size,
			" %s", src_buf);
  free (src_buf);
  gas_assert (str_size >= 0 && str_size < GINSN_LISTING_LEN);

  /* src 2.  */
  src = ginsn_get_src2 (ginsn);
  src_buf = ginsn_src_print (src);
  str_size += snprintf (ginsn_str + str_size, GINSN_LISTING_LEN - str_size,
			"%s", src_buf);
  free (src_buf);
  gas_assert (str_size >= 0 && str_size < GINSN_LISTING_LEN);

  /* dst.  */
  dst = ginsn_get_dst (ginsn);
  char *dst_buf = ginsn_dst_print (dst);
  str_size += snprintf (ginsn_str + str_size, GINSN_LISTING_LEN - str_size,
			"%s", dst_buf);
  free (dst_buf);

end:
  gas_assert (str_size >= 0 && str_size < GINSN_LISTING_LEN);
  return ginsn_str;
}

static void
gbb_cleanup (gbbS **bbp)
{
  gbbS *bb = NULL;

  if (!bbp && !*bbp)
    return;

  bb = *bbp;

  if (bb->entry_state)
    {
      free (bb->entry_state);
      bb->entry_state = NULL;
    }
  if (bb->exit_state)
    {
      free (bb->exit_state);
      bb->exit_state = NULL;
    }
  free (bb);
  *bbp = NULL;
}

/* Add an edge from the source bb FROM_BB to the sink bb TO_BB.  */

static void
bb_add_edge (gbbS* from_bb, gbbS *to_bb)
{
  gedgeS *tmpedge = NULL;
  gedgeS *gedge;
  bool exists = false;

  if (!from_bb || !to_bb)
    return;

  /* Create a new edge object.  */
  gedge = XCNEW (gedgeS);
  gedge->dst_bb = to_bb;
  gedge->next = NULL;
  gedge->visited = false;

  /* Add it in.  */
  if (from_bb->out_gedges == NULL)
    {
      from_bb->out_gedges = gedge;
      from_bb->num_out_gedges++;
    }
  else
    {
      /* Get the head of the list.  */
      tmpedge = from_bb->out_gedges;
      while (tmpedge)
	{
	  /* Do not add duplicate edges.  Duplicated edges will cause unwanted
	     failures in the forward and backward passes for SCFI.  */
	  if (tmpedge->dst_bb == to_bb)
	    {
	      exists = true;
	      break;
	    }
	  if (tmpedge->next)
	    tmpedge = tmpedge->next;
	  else
	    break;
	}

      if (!exists)
	{
	  tmpedge->next = gedge;
	  from_bb->num_out_gedges++;
	}
      else
	free (gedge);
    }
}

static void
cfg_add_bb (gcfgS *gcfg, gbbS *gbb)
{
  gbbS *last_bb = NULL;

  if (!gcfg->root_bb)
    gcfg->root_bb = gbb;
  else
    {
      last_bb = gcfg->root_bb;
      while (last_bb->next)
	last_bb = last_bb->next;

      last_bb->next = gbb;
    }
  gcfg->num_gbbs++;

  gbb->id = gcfg->num_gbbs;
}

static gbbS *
add_bb_at_ginsn (const symbolS *func, gcfgS *gcfg, ginsnS *ginsn, gbbS *prev_bb,
		 int *errp);

/* Return the already existing basic block (if present), which begins with
   GINSN, in the given GCFG.  Return NULL otherwise.  */

static gbbS *
find_bb (gcfgS *gcfg, ginsnS *ginsn)
{
  gbbS *found_bb = NULL;
  gbbS *gbb = NULL;

  if (!ginsn)
    return found_bb;

  if (ginsn->visited)
    {
      cfg_for_each_bb (gcfg, gbb)
	{
	  if (gbb->first_ginsn == ginsn)
	    {
	      found_bb = gbb;
	      break;
	    }
	}
      /* Must be found because ginsn is visited.  */
      gas_assert (found_bb);
    }

  return found_bb;
}

/* Get the basic block starting at GINSN in the GCFG.

   If not already present, the function will make one, while adding an edge
   from the PREV_BB to it.  */

static gbbS *
find_or_make_bb (const symbolS *func, gcfgS *gcfg, ginsnS *ginsn, gbbS *prev_bb,
		 int *errp)
{
  gbbS *found_bb = NULL;

  found_bb = find_bb (gcfg, ginsn);
  if (!found_bb)
    found_bb = add_bb_at_ginsn (func, gcfg, ginsn, prev_bb, errp);

  gas_assert (found_bb);
  gas_assert (found_bb->first_ginsn == ginsn);

  return found_bb;
}

/* Add basic block(s) for all reachable, unvisited ginsns, starting from GINSN,
   to the given GCFG.  Also add an edge from the PREV_BB to the root of the
   newly added basic block(s).

   This is a recursive function which returns the root of the added basic
   blocks.  */

static gbbS *
add_bb_at_ginsn (const symbolS *func, gcfgS *gcfg, ginsnS *ginsn, gbbS *prev_bb,
		 int *errp)
{
  gbbS *root_bb = NULL;
  gbbS *current_bb = NULL;
  ginsnS *target_ginsn = NULL;
  const symbolS *taken_label;

  /* Create a new bb.  N.B. The caller must ensure bb with this ginsn does not
     already exist.  */
  gas_assert (!find_bb (gcfg, ginsn));
  root_bb = XCNEW (gbbS);
  cfg_add_bb (gcfg, root_bb);
  root_bb->first_ginsn = ginsn;

  current_bb = root_bb;

  while (ginsn)
    {
      /* Skip these as they may be right after a GINSN_TYPE_RETURN.
	 For GINSN_TYPE_RETURN, we have already considered that as
	 end of bb, and a logical exit from function.  */
      if (GINSN_F_FUNC_END_P (ginsn))
	{
	  /* Dont mark them visited yet though, leaving the option of these
	     being visited via other control flows as applicable.  */
	  ginsn = ginsn->next;
	  continue;
	}

      if (ginsn->visited)
	{
	  /* If the ginsn has been visited earlier, the bb must exist by now
	     in the cfg.  */
	  prev_bb = current_bb;
	  current_bb = find_bb (gcfg, ginsn);
	  gas_assert (current_bb);
	  /* Add edge from the prev_bb.  */
	  if (prev_bb)
	    bb_add_edge (prev_bb, current_bb);
	  break;
	}
      else if (current_bb && current_bb->first_ginsn != ginsn
	       && GINSN_F_USER_LABEL_P (ginsn))
	{
	  /* Create new bb starting at ginsn for (user-defined) label.  This is
	     likely going to be a destination of a some control flow.  */
	  prev_bb = current_bb;
	  current_bb = find_or_make_bb (func, gcfg, ginsn, prev_bb, errp);
	  bb_add_edge (prev_bb, current_bb);
	  break;
	}

      if (current_bb == NULL)
	{
	  current_bb = XCNEW (gbbS);
	  cfg_add_bb (gcfg, current_bb);
	  current_bb->first_ginsn = ginsn;
	  /* Add edge for the Not Taken, or Fall-through path.  */
	  if (prev_bb)
	    bb_add_edge (prev_bb, current_bb);
	}

      ginsn->visited = true;
      current_bb->num_ginsns++;
      current_bb->last_ginsn = ginsn;

      /* Note that BB is _not_ split on ginsn of type GINSN_TYPE_CALL.  */
      if (ginsn->type == GINSN_TYPE_JUMP
	  || ginsn->type == GINSN_TYPE_JUMP_COND
	  || ginsn->type == GINSN_TYPE_RETURN)
	{
	  /* Indirect jumps must not be seen here.  The caller must have
	     already checked for that.  */
	  gas_assert (!ginsn_indirect_jump_p (ginsn));

	  /* Handle direct jumps.  For unconditional direct jumps, where the
	     target is not local to the function, treat them later as similar
	     to an exit from function (in the else block).  */
	  if (ginsn->type == GINSN_TYPE_JUMP_COND
	      || ginsn_direct_local_jump_p (ginsn))
	    {
	      gas_assert (ginsn->src[0].type == GINSN_SRC_SYMBOL);
	      taken_label = ginsn->src[0].sym;
	      gas_assert (taken_label);

	      /* Preserve the prev_bb to be the source bb as we are going to
		 follow the taken path of the conditional branch soon.  */
	      prev_bb = current_bb;

	      /* Follow the target on the taken path.  */
	      target_ginsn = label_ginsn_map_find (taken_label);
	      /* Add the bb for the target of the taken branch.  */
	      if (target_ginsn)
		{
		  current_bb = find_or_make_bb (func, gcfg, target_ginsn,
						prev_bb, errp);
		  gas_assert (prev_bb);
		  bb_add_edge (prev_bb, current_bb);
		  current_bb = NULL;
		}
	      else
		{
		  *errp = GCFG_JLABEL_NOT_PRESENT;
		  as_warn_where (ginsn->file, ginsn->line,
				 _("missing label '%s' in func '%s' may result in imprecise cfg"),
				 S_GET_NAME (taken_label), S_GET_NAME (func));
		}

	      if (ginsn->type == GINSN_TYPE_JUMP_COND)
		{
		  /* Add the bb for the fall through path.  */
		  current_bb = find_or_make_bb (func, gcfg, ginsn->next,
						prev_bb, errp);
		  gas_assert (prev_bb);
		  bb_add_edge (prev_bb, current_bb);
		  current_bb = NULL;
		}
	      else
		{
		  /* Unconditional jump.  Current BB has been processed.  */
		  current_bb = NULL;
		  /* We'll come back to the ginsns following these (local)
		     unconditional jmps from another path if they are indeed
		     reachable code.  */
		  break;
		}
	    }
	 else
	   {
	     gas_assert (ginsn->type == GINSN_TYPE_RETURN
			 || (ginsn->type == GINSN_TYPE_JUMP
			     && !ginsn_direct_local_jump_p (ginsn)));
	     /* Current BB has been processed.  */
	     current_bb = NULL;

	     /* We'll come back to the ginsns following GINSN_TYPE_RETURN or
		other (non-local) unconditional jmps from another path if they
		are indeed reachable code.  */
	     break;
	   }
	}

      ginsn = ginsn->next;
    }

  return root_bb;
}

static int
gbbs_compare (const void *v1, const void *v2)
{
  const gbbS *bb1 = *(const gbbS **) v1;
  const gbbS *bb2 = *(const gbbS **) v2;

  if (bb1->first_ginsn->id < bb2->first_ginsn->id)
    return -1;
  else if (bb1->first_ginsn->id > bb2->first_ginsn->id)
    return 1;
  else if (bb1->first_ginsn->id == bb2->first_ginsn->id)
    return 0;

  return 0;
}

/* Synthesize DWARF CFI and emit it.  */

static int
ginsn_pass_execute_scfi (const symbolS *func, gcfgS *gcfg, gbbS *root_bb)
{
  int err = scfi_synthesize_dw2cfi (func, gcfg, root_bb);
  if (!err)
    scfi_emit_dw2cfi (func);

  return err;
}

/* Traverse the list of ginsns for the function and warn if some
   ginsns are not visited.

   FIXME - this code assumes the caller has already performed a pass over
   ginsns such that the reachable ginsns are already marked.  Revisit this - we
   should ideally make this pass self-sufficient.  */

static int
ginsn_pass_warn_unreachable_code (const symbolS *func,
				  gcfgS *gcfg ATTRIBUTE_UNUSED,
				  ginsnS *root_ginsn)
{
  ginsnS *ginsn;
  bool unreach_p = false;

  if (!gcfg || !func || !root_ginsn)
    return 0;

  ginsn = root_ginsn;

  while (ginsn)
    {
      /* Some ginsns of type GINSN_TYPE_SYMBOL remain unvisited.  Some
	 may even be excluded from the CFG as they are not reachable, given
	 their function, e.g., user labels after return machine insn.  */
      if (!ginsn->visited
	  && !GINSN_F_FUNC_END_P (ginsn)
	  && !GINSN_F_USER_LABEL_P (ginsn))
	{
	  unreach_p = true;
	  break;
	}
      ginsn = ginsn->next;
    }

  if (unreach_p)
    as_warn_where (ginsn->file, ginsn->line,
		   _("GINSN: found unreachable code in func '%s'"),
		   S_GET_NAME (func));

  return unreach_p;
}

void
gcfg_get_bbs_in_prog_order (gcfgS *gcfg, gbbS **prog_order_bbs)
{
  uint64_t i = 0;
  gbbS *gbb;

  if (!prog_order_bbs)
    return;

  cfg_for_each_bb (gcfg, gbb)
    {
      gas_assert (i < gcfg->num_gbbs);
      prog_order_bbs[i++] = gbb;
    }

  qsort (prog_order_bbs, gcfg->num_gbbs, sizeof (gbbS *), gbbs_compare);
}

/* Build the control flow graph for the ginsns of the function.

   It is important that the target adds an appropriate ginsn:
     - GINSN_TYPE_JUMP,
     - GINSN_TYPE_JUMP_COND,
     - GINSN_TYPE_CALL,
     - GINSN_TYPE_RET
  at the associated points in the function.  The correctness of the CFG
  depends on the accuracy of these 'change of flow instructions'.  */

gcfgS *
gcfg_build (const symbolS *func, int *errp)
{
  gcfgS *gcfg;
  ginsnS *first_ginsn;

  gcfg = XCNEW (gcfgS);
  first_ginsn = frchain_now->frch_ginsn_data->gins_rootP;
  add_bb_at_ginsn (func, gcfg, first_ginsn, NULL /* prev_bb.  */, errp);

  return gcfg;
}

void
gcfg_cleanup (gcfgS **gcfgp)
{
  gcfgS *cfg;
  gbbS *bb, *next_bb;
  gedgeS *edge, *next_edge;

  if (!gcfgp || !*gcfgp)
    return;

  cfg = *gcfgp;
  bb = gcfg_get_rootbb (cfg);

  while (bb)
    {
      next_bb = bb->next;

      /* Cleanup all the edges.  */
      edge = bb->out_gedges;
      while (edge)
	{
	  next_edge = edge->next;
	  free (edge);
	  edge = next_edge;
	}

      gbb_cleanup (&bb);
      bb = next_bb;
    }

  free (cfg);
  *gcfgp = NULL;
}

gbbS *
gcfg_get_rootbb (gcfgS *gcfg)
{
  gbbS *rootbb = NULL;

  if (!gcfg || !gcfg->num_gbbs)
    return NULL;

  rootbb = gcfg->root_bb;

  return rootbb;
}

void
gcfg_print (const gcfgS *gcfg, FILE *outfile)
{
  gbbS *gbb = NULL;
  gedgeS *gedge = NULL;
  uint64_t total_ginsns = 0;

  cfg_for_each_bb(gcfg, gbb)
    {
      fprintf (outfile, "BB [%" PRIu64 "] with num insns: %" PRIu64,
	       gbb->id, gbb->num_ginsns);
      fprintf (outfile, " [insns: %u to %u]\n",
	       gbb->first_ginsn->line, gbb->last_ginsn->line);
      total_ginsns += gbb->num_ginsns;
      bb_for_each_edge(gbb, gedge)
	fprintf (outfile, "  outgoing edge to %" PRIu64 "\n",
		 gedge->dst_bb->id);
    }
  fprintf (outfile, "\nTotal ginsns in all GBBs = %" PRIu64 "\n",
	   total_ginsns);
}

void
frch_ginsn_data_init (const symbolS *func, symbolS *start_addr,
		      enum ginsn_gen_mode gmode)
{
  /* FIXME - error out if prev object is not free'd ?  */
  frchain_now->frch_ginsn_data = XCNEW (struct frch_ginsn_data);

  frchain_now->frch_ginsn_data->mode = gmode;
  /* Annotate with the current function symbol.  */
  frchain_now->frch_ginsn_data->func = func;
  /* Create a new start address symbol now.  */
  frchain_now->frch_ginsn_data->start_addr = start_addr;
  /* Assume the set of ginsn are apt for CFG creation, by default.  */
  frchain_now->frch_ginsn_data->gcfg_apt_p = true;

  frchain_now->frch_ginsn_data->label_ginsn_map = str_htab_create ();
}

void
frch_ginsn_data_cleanup (void)
{
  ginsnS *ginsn = NULL;
  ginsnS *next_ginsn = NULL;

  ginsn = frchain_now->frch_ginsn_data->gins_rootP;
  while (ginsn)
    {
      next_ginsn = ginsn->next;
      ginsn_cleanup (&ginsn);
      ginsn = next_ginsn;
    }

  if (frchain_now->frch_ginsn_data->label_ginsn_map)
    htab_delete (frchain_now->frch_ginsn_data->label_ginsn_map);

  free (frchain_now->frch_ginsn_data);
  frchain_now->frch_ginsn_data = NULL;
}

/* Append GINSN to the list of ginsns for the current function being
   assembled.  */

int
frch_ginsn_data_append (ginsnS *ginsn)
{
  ginsnS *last = NULL;
  ginsnS *temp = NULL;
  uint64_t id = 0;

  if (!ginsn)
    return 1;

  if (frchain_now->frch_ginsn_data->gins_lastP)
    id = frchain_now->frch_ginsn_data->gins_lastP->id;

  /* Do the necessary preprocessing on the set of input GINSNs:
       - Update each ginsn with its ID.
     While you iterate, also keep gcfg_apt_p updated by checking whether any
     ginsn is inappropriate for GCFG creation.  */
  temp = ginsn;
  while (temp)
    {
      temp->id = ++id;

      if (ginsn_indirect_jump_p (temp))
	frchain_now->frch_ginsn_data->gcfg_apt_p = false;

      if (listing & LISTING_GINSN_SCFI)
	listing_newline (ginsn_print (temp));

      /* The input GINSN may be a linked list of multiple ginsns chained
	 together.  Find the last ginsn in the input chain of ginsns.  */
      last = temp;

      temp = temp->next;
    }

  /* Link in the ginsn to the tail.  */
  if (!frchain_now->frch_ginsn_data->gins_rootP)
    frchain_now->frch_ginsn_data->gins_rootP = ginsn;
  else
    ginsn_link_next (frchain_now->frch_ginsn_data->gins_lastP, ginsn);

  frchain_now->frch_ginsn_data->gins_lastP = last;

  return 0;
}

enum ginsn_gen_mode
frch_ginsn_gen_mode (void)
{
  enum ginsn_gen_mode gmode = GINSN_GEN_NONE;

  if (frchain_now->frch_ginsn_data)
    gmode = frchain_now->frch_ginsn_data->mode;

  return gmode;
}

int
ginsn_data_begin (const symbolS *func)
{
  ginsnS *ginsn;

  /* The previous block of asm must have been processed by now.  */
  if (frchain_now->frch_ginsn_data)
    as_bad (_("GINSN process for prev func not done"));

  /* FIXME - hard code the mode to GINSN_GEN_SCFI.
     This can be changed later when other passes on ginsns are formalised.  */
  frch_ginsn_data_init (func, symbol_temp_new_now (), GINSN_GEN_SCFI);

  /* Create and insert ginsn with function begin marker.  */
  ginsn = ginsn_new_symbol_func_begin (func);
  frch_ginsn_data_append (ginsn);

  return 0;
}

int
ginsn_data_end (const symbolS *label)
{
  ginsnS *ginsn;
  gbbS *root_bb;
  gcfgS *gcfg = NULL;
  const symbolS *func;
  int err = 0;

  if (!frchain_now->frch_ginsn_data)
    return err;

  /* Insert Function end marker.  */
  ginsn = ginsn_new_symbol_func_end (label);
  frch_ginsn_data_append (ginsn);

  func = frchain_now->frch_ginsn_data->func;

  /* Build the cfg of ginsn(s) of the function.  */
  if (!frchain_now->frch_ginsn_data->gcfg_apt_p)
    {
      as_bad (_("untraceable control flow for func '%s'"),
	      S_GET_NAME (func));
      goto end;
    }

  gcfg = gcfg_build (func, &err);

  root_bb = gcfg_get_rootbb (gcfg);
  if (!root_bb)
    {
      as_bad (_("Bad cfg of ginsn of func '%s'"), S_GET_NAME (func));
      goto end;
    }

  /* Execute the desired passes on ginsns.  */
  err = ginsn_pass_execute_scfi (func, gcfg, root_bb);
  if (err)
    goto end;

  /* Other passes, e.g., warn for unreachable code can be enabled too.  */
  ginsn = frchain_now->frch_ginsn_data->gins_rootP;
  err = ginsn_pass_warn_unreachable_code (func, gcfg, ginsn);

end:
  if (gcfg)
    gcfg_cleanup (&gcfg);
  frch_ginsn_data_cleanup ();

  return err;
}

/* Add GINSN_TYPE_SYMBOL type ginsn for user-defined labels.  These may be
   branch targets, and hence are necessary for control flow graph.  */

void
ginsn_frob_label (const symbolS *label)
{
  ginsnS *label_ginsn;
  const char *file;
  unsigned int line;

  if (frchain_now->frch_ginsn_data)
    {
      /* PS: Note how we keep the actual LABEL symbol as ginsn->sym.
	 Take care to avoid inadvertent updates or cleanups of symbols.  */
      label_ginsn = ginsn_new_symbol_user_label (label);
      /* Keep the location updated.  */
      file = as_where (&line);
      ginsn_set_file_line (label_ginsn, file, line);

      frch_ginsn_data_append (label_ginsn);

      label_ginsn_map_insert (label, label_ginsn);
    }
}

const symbolS *
ginsn_data_func_symbol (void)
{
  const symbolS *func = NULL;

  if (frchain_now->frch_ginsn_data)
    func = frchain_now->frch_ginsn_data->func;

  return func;
}

#else

int
ginsn_data_begin (const symbolS *func ATTRIBUTE_UNUSED)
{
  as_bad (_("ginsn unsupported for target"));
  return 1;
}

int
ginsn_data_end (const symbolS *label ATTRIBUTE_UNUSED)
{
  as_bad (_("ginsn unsupported for target"));
  return 1;
}

void
ginsn_frob_label (const symbolS *sym ATTRIBUTE_UNUSED)
{
  return;
}

const symbolS *
ginsn_data_func_symbol (void)
{
  return NULL;
}

#endif  /* TARGET_USE_GINSN.  */