]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/ABI/SysV-hexagon/ABISysV_hexagon.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / ABI / SysV-hexagon / ABISysV_hexagon.cpp
1 //===-- ABISysV_hexagon.cpp -------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "ABISysV_hexagon.h"
11
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/IR/DerivedTypes.h"
17
18 // Project includes
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/PluginManager.h"
21 #include "lldb/Core/RegisterValue.h"
22 #include "lldb/Core/Value.h"
23 #include "lldb/Core/ValueObjectConstResult.h"
24 #include "lldb/Core/ValueObjectMemory.h"
25 #include "lldb/Core/ValueObjectRegister.h"
26 #include "lldb/Symbol/UnwindPlan.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/RegisterContext.h"
29 #include "lldb/Target/StackFrame.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/Thread.h"
32 #include "lldb/Utility/ConstString.h"
33 #include "lldb/Utility/DataExtractor.h"
34 #include "lldb/Utility/Log.h"
35 #include "lldb/Utility/Status.h"
36
37 using namespace lldb;
38 using namespace lldb_private;
39
40 static RegisterInfo g_register_infos[] = {
41     // hexagon-core.xml
42     {"r00",
43      "",
44      4,
45      0,
46      eEncodingUint,
47      eFormatAddressInfo,
48      {0, 0, LLDB_INVALID_REGNUM, 0, 0},
49      nullptr,
50      nullptr,
51      nullptr,
52      0},
53     {"r01",
54      "",
55      4,
56      0,
57      eEncodingUint,
58      eFormatAddressInfo,
59      {1, 1, LLDB_INVALID_REGNUM, 1, 1},
60      nullptr,
61      nullptr,
62      nullptr,
63      0},
64     {"r02",
65      "",
66      4,
67      0,
68      eEncodingUint,
69      eFormatAddressInfo,
70      {2, 2, LLDB_INVALID_REGNUM, 2, 2},
71      nullptr,
72      nullptr,
73      nullptr,
74      0},
75     {"r03",
76      "",
77      4,
78      0,
79      eEncodingUint,
80      eFormatAddressInfo,
81      {3, 3, LLDB_INVALID_REGNUM, 3, 3},
82      nullptr,
83      nullptr,
84      nullptr,
85      0},
86     {"r04",
87      "",
88      4,
89      0,
90      eEncodingUint,
91      eFormatAddressInfo,
92      {4, 4, LLDB_INVALID_REGNUM, 4, 4},
93      nullptr,
94      nullptr,
95      nullptr,
96      0},
97     {"r05",
98      "",
99      4,
100      0,
101      eEncodingUint,
102      eFormatAddressInfo,
103      {5, 5, LLDB_INVALID_REGNUM, 5, 5},
104      nullptr,
105      nullptr,
106      nullptr,
107      0},
108     {"r06",
109      "",
110      4,
111      0,
112      eEncodingUint,
113      eFormatAddressInfo,
114      {6, 6, LLDB_INVALID_REGNUM, 6, 6},
115      nullptr,
116      nullptr,
117      nullptr,
118      0},
119     {"r07",
120      "",
121      4,
122      0,
123      eEncodingUint,
124      eFormatAddressInfo,
125      {7, 7, LLDB_INVALID_REGNUM, 7, 7},
126      nullptr,
127      nullptr,
128      nullptr,
129      0},
130     {"r08",
131      "",
132      4,
133      0,
134      eEncodingUint,
135      eFormatAddressInfo,
136      {8, 8, LLDB_INVALID_REGNUM, 8, 8},
137      nullptr,
138      nullptr,
139      nullptr,
140      0},
141     {"r09",
142      "",
143      4,
144      0,
145      eEncodingUint,
146      eFormatAddressInfo,
147      {9, 9, LLDB_INVALID_REGNUM, 9, 9},
148      nullptr,
149      nullptr,
150      nullptr,
151      0},
152     {"r10",
153      "",
154      4,
155      0,
156      eEncodingUint,
157      eFormatAddressInfo,
158      {10, 10, LLDB_INVALID_REGNUM, 10, 10},
159      nullptr,
160      nullptr,
161      nullptr,
162      0},
163     {"r11",
164      "",
165      4,
166      0,
167      eEncodingUint,
168      eFormatAddressInfo,
169      {11, 11, LLDB_INVALID_REGNUM, 11, 11},
170      nullptr,
171      nullptr,
172      nullptr,
173      0},
174     {"r12",
175      "",
176      4,
177      0,
178      eEncodingUint,
179      eFormatAddressInfo,
180      {12, 12, LLDB_INVALID_REGNUM, 12, 12},
181      nullptr,
182      nullptr,
183      nullptr,
184      0},
185     {"r13",
186      "",
187      4,
188      0,
189      eEncodingUint,
190      eFormatAddressInfo,
191      {13, 13, LLDB_INVALID_REGNUM, 13, 13},
192      nullptr,
193      nullptr,
194      nullptr,
195      0},
196     {"r14",
197      "",
198      4,
199      0,
200      eEncodingUint,
201      eFormatAddressInfo,
202      {14, 14, LLDB_INVALID_REGNUM, 14, 14},
203      nullptr,
204      nullptr,
205      nullptr,
206      0},
207     {"r15",
208      "",
209      4,
210      0,
211      eEncodingUint,
212      eFormatAddressInfo,
213      {15, 15, LLDB_INVALID_REGNUM, 15, 15},
214      nullptr,
215      nullptr,
216      nullptr,
217      0},
218     {"r16",
219      "",
220      4,
221      0,
222      eEncodingUint,
223      eFormatAddressInfo,
224      {16, 16, LLDB_INVALID_REGNUM, 16, 16},
225      nullptr,
226      nullptr,
227      nullptr,
228      0},
229     {"r17",
230      "",
231      4,
232      0,
233      eEncodingUint,
234      eFormatAddressInfo,
235      {17, 17, LLDB_INVALID_REGNUM, 17, 17},
236      nullptr,
237      nullptr,
238      nullptr,
239      0},
240     {"r18",
241      "",
242      4,
243      0,
244      eEncodingUint,
245      eFormatAddressInfo,
246      {18, 18, LLDB_INVALID_REGNUM, 18, 18},
247      nullptr,
248      nullptr,
249      nullptr,
250      0},
251     {"r19",
252      "",
253      4,
254      0,
255      eEncodingUint,
256      eFormatAddressInfo,
257      {19, 19, LLDB_INVALID_REGNUM, 19, 19},
258      nullptr,
259      nullptr,
260      nullptr,
261      0},
262     {"r20",
263      "",
264      4,
265      0,
266      eEncodingUint,
267      eFormatAddressInfo,
268      {20, 20, LLDB_INVALID_REGNUM, 20, 20},
269      nullptr,
270      nullptr,
271      nullptr,
272      0},
273     {"r21",
274      "",
275      4,
276      0,
277      eEncodingUint,
278      eFormatAddressInfo,
279      {21, 21, LLDB_INVALID_REGNUM, 21, 21},
280      nullptr,
281      nullptr,
282      nullptr,
283      0},
284     {"r22",
285      "",
286      4,
287      0,
288      eEncodingUint,
289      eFormatAddressInfo,
290      {22, 22, LLDB_INVALID_REGNUM, 22, 22},
291      nullptr,
292      nullptr,
293      nullptr,
294      0},
295     {"r23",
296      "",
297      4,
298      0,
299      eEncodingUint,
300      eFormatAddressInfo,
301      {23, 23, LLDB_INVALID_REGNUM, 23, 23},
302      nullptr,
303      nullptr,
304      nullptr,
305      0},
306     {"r24",
307      "",
308      4,
309      0,
310      eEncodingUint,
311      eFormatAddressInfo,
312      {24, 24, LLDB_INVALID_REGNUM, 24, 24},
313      nullptr,
314      nullptr,
315      nullptr,
316      0},
317     {"r25",
318      "",
319      4,
320      0,
321      eEncodingUint,
322      eFormatAddressInfo,
323      {25, 25, LLDB_INVALID_REGNUM, 25, 25},
324      nullptr,
325      nullptr,
326      nullptr,
327      0},
328     {"r26",
329      "",
330      4,
331      0,
332      eEncodingUint,
333      eFormatAddressInfo,
334      {26, 26, LLDB_INVALID_REGNUM, 26, 26},
335      nullptr,
336      nullptr,
337      nullptr,
338      0},
339     {"r27",
340      "",
341      4,
342      0,
343      eEncodingUint,
344      eFormatAddressInfo,
345      {27, 27, LLDB_INVALID_REGNUM, 27, 27},
346      nullptr,
347      nullptr,
348      nullptr,
349      0},
350     {"r28",
351      "",
352      4,
353      0,
354      eEncodingUint,
355      eFormatAddressInfo,
356      {28, 28, LLDB_INVALID_REGNUM, 28, 28},
357      nullptr,
358      nullptr,
359      nullptr,
360      0},
361     {"sp",
362      "r29",
363      4,
364      0,
365      eEncodingUint,
366      eFormatAddressInfo,
367      {29, 29, LLDB_REGNUM_GENERIC_SP, 29, 29},
368      nullptr,
369      nullptr,
370      nullptr,
371      0},
372     {"fp",
373      "r30",
374      4,
375      0,
376      eEncodingUint,
377      eFormatAddressInfo,
378      {30, 30, LLDB_REGNUM_GENERIC_FP, 30, 30},
379      nullptr,
380      nullptr,
381      nullptr,
382      0},
383     {"lr",
384      "r31",
385      4,
386      0,
387      eEncodingUint,
388      eFormatAddressInfo,
389      {31, 31, LLDB_REGNUM_GENERIC_RA, 31, 31},
390      nullptr,
391      nullptr,
392      nullptr,
393      0},
394     {"sa0",
395      "",
396      4,
397      0,
398      eEncodingUint,
399      eFormatAddressInfo,
400      {32, 32, LLDB_INVALID_REGNUM, 32, 32},
401      nullptr,
402      nullptr,
403      nullptr,
404      0},
405     {"lc0",
406      "",
407      4,
408      0,
409      eEncodingUint,
410      eFormatAddressInfo,
411      {33, 33, LLDB_INVALID_REGNUM, 33, 33},
412      nullptr,
413      nullptr,
414      nullptr,
415      0},
416     {"sa1",
417      "",
418      4,
419      0,
420      eEncodingUint,
421      eFormatAddressInfo,
422      {34, 34, LLDB_INVALID_REGNUM, 34, 34},
423      nullptr,
424      nullptr,
425      nullptr,
426      0},
427     {"lc1",
428      "",
429      4,
430      0,
431      eEncodingUint,
432      eFormatAddressInfo,
433      {35, 35, LLDB_INVALID_REGNUM, 35, 35},
434      nullptr,
435      nullptr,
436      nullptr,
437      0},
438     // --> hexagon-v4/5/55/56-sim.xml
439     {"p3_0",
440      "",
441      4,
442      0,
443      eEncodingUint,
444      eFormatAddressInfo,
445      {36, 36, LLDB_INVALID_REGNUM, 36, 36},
446      nullptr,
447      nullptr,
448      nullptr,
449      0},
450     // PADDING {
451     {"p00",
452      "",
453      4,
454      0,
455      eEncodingInvalid,
456      eFormatInvalid,
457      {37, 37, LLDB_INVALID_REGNUM, 37, 37},
458      nullptr,
459      nullptr,
460      nullptr,
461      0},
462     // }
463     {"m0",
464      "",
465      4,
466      0,
467      eEncodingUint,
468      eFormatAddressInfo,
469      {38, 38, LLDB_INVALID_REGNUM, 38, 38},
470      nullptr,
471      nullptr,
472      nullptr,
473      0},
474     {"m1",
475      "",
476      4,
477      0,
478      eEncodingUint,
479      eFormatAddressInfo,
480      {39, 39, LLDB_INVALID_REGNUM, 39, 39},
481      nullptr,
482      nullptr,
483      nullptr,
484      0},
485     {"usr",
486      "",
487      4,
488      0,
489      eEncodingUint,
490      eFormatAddressInfo,
491      {40, 40, LLDB_INVALID_REGNUM, 40, 40},
492      nullptr,
493      nullptr,
494      nullptr,
495      0},
496     {"pc",
497      "",
498      4,
499      0,
500      eEncodingUint,
501      eFormatAddressInfo,
502      {41, 41, LLDB_REGNUM_GENERIC_PC, 41, 41},
503      nullptr,
504      nullptr,
505      nullptr,
506      0},
507     {"ugp",
508      "",
509      4,
510      0,
511      eEncodingUint,
512      eFormatAddressInfo,
513      {42, 42, LLDB_INVALID_REGNUM, 42, 42},
514      nullptr,
515      nullptr,
516      nullptr,
517      0},
518     {"gp",
519      "",
520      4,
521      0,
522      eEncodingUint,
523      eFormatAddressInfo,
524      {43, 43, LLDB_INVALID_REGNUM, 43, 43},
525      nullptr,
526      nullptr,
527      nullptr,
528      0},
529     {"cs0",
530      "",
531      4,
532      0,
533      eEncodingUint,
534      eFormatAddressInfo,
535      {44, 44, LLDB_INVALID_REGNUM, 44, 44},
536      nullptr,
537      nullptr,
538      nullptr,
539      0},
540     {"cs1",
541      "",
542      4,
543      0,
544      eEncodingUint,
545      eFormatAddressInfo,
546      {45, 45, LLDB_INVALID_REGNUM, 45, 45},
547      nullptr,
548      nullptr,
549      nullptr,
550      0},
551     // PADDING {
552     {"p01",
553      "",
554      4,
555      0,
556      eEncodingInvalid,
557      eFormatInvalid,
558      {46, 46, LLDB_INVALID_REGNUM, 46, 46},
559      nullptr,
560      nullptr,
561      nullptr,
562      0},
563     {"p02",
564      "",
565      4,
566      0,
567      eEncodingInvalid,
568      eFormatInvalid,
569      {47, 47, LLDB_INVALID_REGNUM, 47, 47},
570      nullptr,
571      nullptr,
572      nullptr,
573      0},
574     {"p03",
575      "",
576      4,
577      0,
578      eEncodingInvalid,
579      eFormatInvalid,
580      {48, 48, LLDB_INVALID_REGNUM, 48, 48},
581      nullptr,
582      nullptr,
583      nullptr,
584      0},
585     {"p04",
586      "",
587      4,
588      0,
589      eEncodingInvalid,
590      eFormatInvalid,
591      {49, 49, LLDB_INVALID_REGNUM, 49, 49},
592      nullptr,
593      nullptr,
594      nullptr,
595      0},
596     {"p05",
597      "",
598      4,
599      0,
600      eEncodingInvalid,
601      eFormatInvalid,
602      {50, 50, LLDB_INVALID_REGNUM, 50, 50},
603      nullptr,
604      nullptr,
605      nullptr,
606      0},
607     {"p06",
608      "",
609      4,
610      0,
611      eEncodingInvalid,
612      eFormatInvalid,
613      {51, 51, LLDB_INVALID_REGNUM, 51, 51},
614      nullptr,
615      nullptr,
616      nullptr,
617      0},
618     {"p07",
619      "",
620      4,
621      0,
622      eEncodingInvalid,
623      eFormatInvalid,
624      {52, 52, LLDB_INVALID_REGNUM, 52, 52},
625      nullptr,
626      nullptr,
627      nullptr,
628      0},
629     {"p08",
630      "",
631      4,
632      0,
633      eEncodingInvalid,
634      eFormatInvalid,
635      {53, 53, LLDB_INVALID_REGNUM, 53, 53},
636      nullptr,
637      nullptr,
638      nullptr,
639      0},
640     {"p09",
641      "",
642      4,
643      0,
644      eEncodingInvalid,
645      eFormatInvalid,
646      {54, 54, LLDB_INVALID_REGNUM, 54, 54},
647      nullptr,
648      nullptr,
649      nullptr,
650      0},
651     {"p10",
652      "",
653      4,
654      0,
655      eEncodingInvalid,
656      eFormatInvalid,
657      {55, 55, LLDB_INVALID_REGNUM, 55, 55},
658      nullptr,
659      nullptr,
660      nullptr,
661      0},
662     {"p11",
663      "",
664      4,
665      0,
666      eEncodingInvalid,
667      eFormatInvalid,
668      {56, 56, LLDB_INVALID_REGNUM, 56, 56},
669      nullptr,
670      nullptr,
671      nullptr,
672      0},
673     {"p12",
674      "",
675      4,
676      0,
677      eEncodingInvalid,
678      eFormatInvalid,
679      {57, 57, LLDB_INVALID_REGNUM, 57, 57},
680      nullptr,
681      nullptr,
682      nullptr,
683      0},
684     {"p13",
685      "",
686      4,
687      0,
688      eEncodingInvalid,
689      eFormatInvalid,
690      {58, 58, LLDB_INVALID_REGNUM, 58, 58},
691      nullptr,
692      nullptr,
693      nullptr,
694      0},
695     {"p14",
696      "",
697      4,
698      0,
699      eEncodingInvalid,
700      eFormatInvalid,
701      {59, 59, LLDB_INVALID_REGNUM, 59, 59},
702      nullptr,
703      nullptr,
704      nullptr,
705      0},
706     {"p15",
707      "",
708      4,
709      0,
710      eEncodingInvalid,
711      eFormatInvalid,
712      {60, 60, LLDB_INVALID_REGNUM, 60, 60},
713      nullptr,
714      nullptr,
715      nullptr,
716      0},
717     {"p16",
718      "",
719      4,
720      0,
721      eEncodingInvalid,
722      eFormatInvalid,
723      {61, 61, LLDB_INVALID_REGNUM, 61, 61},
724      nullptr,
725      nullptr,
726      nullptr,
727      0},
728     {"p17",
729      "",
730      4,
731      0,
732      eEncodingInvalid,
733      eFormatInvalid,
734      {62, 62, LLDB_INVALID_REGNUM, 62, 62},
735      nullptr,
736      nullptr,
737      nullptr,
738      0},
739     {"p18",
740      "",
741      4,
742      0,
743      eEncodingInvalid,
744      eFormatInvalid,
745      {63, 63, LLDB_INVALID_REGNUM, 63, 63},
746      nullptr,
747      nullptr,
748      nullptr,
749      0},
750     // }
751     {"sgp0",
752      "",
753      4,
754      0,
755      eEncodingUint,
756      eFormatAddressInfo,
757      {64, 64, LLDB_INVALID_REGNUM, 64, 64},
758      nullptr,
759      nullptr,
760      nullptr,
761      0},
762     // PADDING {
763     {"p19",
764      "",
765      4,
766      0,
767      eEncodingInvalid,
768      eFormatInvalid,
769      {65, 65, LLDB_INVALID_REGNUM, 65, 65},
770      nullptr,
771      nullptr,
772      nullptr,
773      0},
774     // }
775     {"stid",
776      "",
777      4,
778      0,
779      eEncodingUint,
780      eFormatAddressInfo,
781      {66, 66, LLDB_INVALID_REGNUM, 66, 66},
782      nullptr,
783      nullptr,
784      nullptr,
785      0},
786     {"elr",
787      "",
788      4,
789      0,
790      eEncodingUint,
791      eFormatAddressInfo,
792      {67, 67, LLDB_INVALID_REGNUM, 67, 67},
793      nullptr,
794      nullptr,
795      nullptr,
796      0},
797     {"badva0",
798      "",
799      4,
800      0,
801      eEncodingUint,
802      eFormatAddressInfo,
803      {68, 68, LLDB_INVALID_REGNUM, 68, 68},
804      nullptr,
805      nullptr,
806      nullptr,
807      0},
808     {"badva1",
809      "",
810      4,
811      0,
812      eEncodingUint,
813      eFormatAddressInfo,
814      {69, 69, LLDB_INVALID_REGNUM, 69, 69},
815      nullptr,
816      nullptr,
817      nullptr,
818      0},
819     {"ssr",
820      "",
821      4,
822      0,
823      eEncodingUint,
824      eFormatAddressInfo,
825      {70, 70, LLDB_INVALID_REGNUM, 70, 70},
826      nullptr,
827      nullptr,
828      nullptr,
829      0},
830     {"ccr",
831      "",
832      4,
833      0,
834      eEncodingUint,
835      eFormatAddressInfo,
836      {71, 71, LLDB_INVALID_REGNUM, 71, 71},
837      nullptr,
838      nullptr,
839      nullptr,
840      0},
841     {"htid",
842      "",
843      4,
844      0,
845      eEncodingUint,
846      eFormatAddressInfo,
847      {72, 72, LLDB_INVALID_REGNUM, 72, 72},
848      nullptr,
849      nullptr,
850      nullptr,
851      0},
852     // PADDING {
853     {"p20",
854      "",
855      4,
856      0,
857      eEncodingInvalid,
858      eFormatInvalid,
859      {73, 73, LLDB_INVALID_REGNUM, 73, 73},
860      nullptr,
861      nullptr,
862      nullptr,
863      0},
864     // }
865     {"imask",
866      "",
867      4,
868      0,
869      eEncodingUint,
870      eFormatAddressInfo,
871      {74, 74, LLDB_INVALID_REGNUM, 74, 74},
872      nullptr,
873      nullptr,
874      nullptr,
875      0},
876     // PADDING {
877     {"p21",
878      "",
879      4,
880      0,
881      eEncodingInvalid,
882      eFormatInvalid,
883      {75, 75, LLDB_INVALID_REGNUM, 75, 75},
884      nullptr,
885      nullptr,
886      nullptr,
887      0},
888     {"p22",
889      "",
890      4,
891      0,
892      eEncodingInvalid,
893      eFormatInvalid,
894      {76, 76, LLDB_INVALID_REGNUM, 76, 76},
895      nullptr,
896      nullptr,
897      nullptr,
898      0},
899     {"p23",
900      "",
901      4,
902      0,
903      eEncodingInvalid,
904      eFormatInvalid,
905      {77, 77, LLDB_INVALID_REGNUM, 77, 77},
906      nullptr,
907      nullptr,
908      nullptr,
909      0},
910     {"p24",
911      "",
912      4,
913      0,
914      eEncodingInvalid,
915      eFormatInvalid,
916      {78, 78, LLDB_INVALID_REGNUM, 78, 78},
917      nullptr,
918      nullptr,
919      nullptr,
920      0},
921     {"p25",
922      "",
923      4,
924      0,
925      eEncodingInvalid,
926      eFormatInvalid,
927      {79, 79, LLDB_INVALID_REGNUM, 79, 79},
928      nullptr,
929      nullptr,
930      nullptr,
931      0},
932     // }
933     {"g0",
934      "",
935      4,
936      0,
937      eEncodingUint,
938      eFormatAddressInfo,
939      {80, 80, LLDB_INVALID_REGNUM, 80, 80},
940      nullptr,
941      nullptr,
942      nullptr,
943      0},
944     {"g1",
945      "",
946      4,
947      0,
948      eEncodingUint,
949      eFormatAddressInfo,
950      {81, 81, LLDB_INVALID_REGNUM, 81, 81},
951      nullptr,
952      nullptr,
953      nullptr,
954      0},
955     {"g2",
956      "",
957      4,
958      0,
959      eEncodingUint,
960      eFormatAddressInfo,
961      {82, 82, LLDB_INVALID_REGNUM, 82, 82},
962      nullptr,
963      nullptr,
964      nullptr,
965      0},
966     {"g3",
967      "",
968      4,
969      0,
970      eEncodingUint,
971      eFormatAddressInfo,
972      {83, 83, LLDB_INVALID_REGNUM, 83, 83},
973      nullptr,
974      nullptr,
975      nullptr,
976      0}};
977
978 static const uint32_t k_num_register_infos =
979     sizeof(g_register_infos) / sizeof(RegisterInfo);
980 static bool g_register_info_names_constified = false;
981
982 const lldb_private::RegisterInfo *
983 ABISysV_hexagon::GetRegisterInfoArray(uint32_t &count) {
984   // Make the C-string names and alt_names for the register infos into const
985   // C-string values by having the ConstString unique the names in the global
986   // constant C-string pool.
987   if (!g_register_info_names_constified) {
988     g_register_info_names_constified = true;
989     for (uint32_t i = 0; i < k_num_register_infos; ++i) {
990       if (g_register_infos[i].name)
991         g_register_infos[i].name =
992             ConstString(g_register_infos[i].name).GetCString();
993       if (g_register_infos[i].alt_name)
994         g_register_infos[i].alt_name =
995             ConstString(g_register_infos[i].alt_name).GetCString();
996     }
997   }
998   count = k_num_register_infos;
999   return g_register_infos;
1000 }
1001
1002 /*
1003     http://en.wikipedia.org/wiki/Red_zone_%28computing%29
1004
1005     In computing, a red zone is a fixed size area in memory beyond the stack
1006    pointer that has not been
1007     "allocated". This region of memory is not to be modified by
1008    interrupt/exception/signal handlers.
1009     This allows the space to be used for temporary data without the extra
1010    overhead of modifying the
1011     stack pointer. The x86-64 ABI mandates a 128 byte red zone.[1] The OpenRISC
1012    toolchain assumes a
1013     128 byte red zone though it is not documented.
1014 */
1015 size_t ABISysV_hexagon::GetRedZoneSize() const { return 0; }
1016
1017 //------------------------------------------------------------------
1018 // Static Functions
1019 //------------------------------------------------------------------
1020
1021 ABISP
1022 ABISysV_hexagon::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) {
1023   static ABISP g_abi_sp;
1024   if (arch.GetTriple().getArch() == llvm::Triple::hexagon) {
1025     if (!g_abi_sp)
1026       g_abi_sp.reset(new ABISysV_hexagon(process_sp));
1027     return g_abi_sp;
1028   }
1029   return ABISP();
1030 }
1031
1032 bool ABISysV_hexagon::PrepareTrivialCall(Thread &thread, lldb::addr_t sp,
1033                                          lldb::addr_t pc, lldb::addr_t ra,
1034                                          llvm::ArrayRef<addr_t> args) const {
1035   // we don't use the traditional trivial call specialized for jit
1036   return false;
1037 }
1038
1039 /*
1040
1041 // AD:
1042 //  . safeguard the current stack
1043 //  . how can we know that the called function will create its own frame
1044 properly?
1045 //  . we could manually make a new stack first:
1046 //      2. push RA
1047 //      3. push FP
1048 //      4. FP = SP
1049 //      5. SP = SP ( since no locals in our temp frame )
1050
1051 // AD 6/05/2014
1052 //  . variable argument list parameters are not passed via registers, they are
1053 passed on
1054 //    the stack.  This presents us with a problem, since we need to know when
1055 the valist
1056 //    starts.  Currently I can find out if a function is varg, but not how many
1057 //    real parameters it takes.  Thus I don't know when to start spilling the
1058 vargs.  For
1059 //    the time being, to progress, I will assume that it takes on real parameter
1060 before
1061 //    the vargs list starts.
1062
1063 // AD 06/05/2014
1064 //  . how do we adhere to the stack alignment requirements
1065
1066 // AD 06/05/2014
1067 //  . handle 64bit values and their register / stack requirements
1068
1069 */
1070 #define HEX_ABI_DEBUG 0
1071 bool ABISysV_hexagon::PrepareTrivialCall(
1072     Thread &thread, lldb::addr_t sp, lldb::addr_t pc, lldb::addr_t ra,
1073     llvm::Type &prototype, llvm::ArrayRef<ABI::CallArgument> args) const {
1074   // default number of register passed arguments for varg functions
1075   const int nVArgRegParams = 1;
1076   Status error;
1077
1078   // grab the process so we have access to the memory for spilling
1079   lldb::ProcessSP proc = thread.GetProcess();
1080
1081   // get the register context for modifying all of the registers
1082   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
1083   if (!reg_ctx)
1084     return false;
1085
1086   uint32_t pc_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(
1087       eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
1088   if (pc_reg == LLDB_INVALID_REGNUM)
1089     return false;
1090
1091   uint32_t ra_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(
1092       eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA);
1093   if (ra_reg == LLDB_INVALID_REGNUM)
1094     return false;
1095
1096   uint32_t sp_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(
1097       eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
1098   if (sp_reg == LLDB_INVALID_REGNUM)
1099     return false;
1100
1101   // push host data onto target
1102   for (size_t i = 0; i < args.size(); i++) {
1103     const ABI::CallArgument &arg = args[i];
1104     // skip over target values
1105     if (arg.type == ABI::CallArgument::TargetValue)
1106       continue;
1107     // round up to 8 byte multiple
1108     size_t argSize = (arg.size | 0x7) + 1;
1109
1110     // create space on the stack for this data
1111     sp -= argSize;
1112
1113     // write this argument onto the stack of the host process
1114     proc->WriteMemory(sp, arg.data_ap.get(), arg.size, error);
1115     if (error.Fail())
1116       return false;
1117
1118     // update the argument with the target pointer
1119     // XXX: This is a gross hack for getting around the const
1120     *const_cast<lldb::addr_t *>(&arg.value) = sp;
1121   }
1122
1123 #if HEX_ABI_DEBUG
1124   // print the original stack pointer
1125   printf("sp : %04" PRIx64 " \n", sp);
1126 #endif
1127
1128   // make sure number of parameters matches prototype
1129   assert(prototype.getFunctionNumParams() == args.size());
1130
1131   // check if this is a variable argument function
1132   bool isVArg = prototype.isFunctionVarArg();
1133
1134   // number of arguments passed by register
1135   int nRegArgs = nVArgRegParams;
1136   if (!isVArg) {
1137     // number of arguments is limited by [R0 : R5] space
1138     nRegArgs = args.size();
1139     if (nRegArgs > 6)
1140       nRegArgs = 6;
1141   }
1142
1143   // pass arguments that are passed via registers
1144   for (int i = 0; i < nRegArgs; i++) {
1145     // get the parameter as a u32
1146     uint32_t param = (uint32_t)args[i].value;
1147     // write argument into register
1148     if (!reg_ctx->WriteRegisterFromUnsigned(i, param))
1149       return false;
1150   }
1151
1152   // number of arguments to spill onto stack
1153   int nSpillArgs = args.size() - nRegArgs;
1154   // make space on the stack for arguments
1155   sp -= 4 * nSpillArgs;
1156   // align stack on an 8 byte boundary
1157   if (sp & 7)
1158     sp -= 4;
1159
1160   // arguments that are passed on the stack
1161   for (size_t i = nRegArgs, offs = 0; i < args.size(); i++) {
1162     // get the parameter as a u32
1163     uint32_t param = (uint32_t)args[i].value;
1164     // write argument to stack
1165     proc->WriteMemory(sp + offs, (void *)&param, sizeof(param), error);
1166     if (!error.Success())
1167       return false;
1168     //
1169     offs += 4;
1170   }
1171
1172   // update registers with current function call state
1173   reg_ctx->WriteRegisterFromUnsigned(pc_reg, pc);
1174   reg_ctx->WriteRegisterFromUnsigned(ra_reg, ra);
1175   reg_ctx->WriteRegisterFromUnsigned(sp_reg, sp);
1176
1177 #if HEX_ABI_DEBUG
1178   // quick and dirty stack dumper for debugging
1179   for (int i = -8; i < 8; i++) {
1180     uint32_t data = 0;
1181     lldb::addr_t addr = sp + i * 4;
1182     proc->ReadMemory(addr, (void *)&data, sizeof(data), error);
1183     printf("\n0x%04" PRIx64 " 0x%08x ", addr, data);
1184     if (i == 0)
1185       printf("<<-- sp");
1186   }
1187   printf("\n");
1188 #endif
1189
1190   return true;
1191 }
1192
1193 bool ABISysV_hexagon::GetArgumentValues(Thread &thread,
1194                                         ValueList &values) const {
1195   return false;
1196 }
1197
1198 Status
1199 ABISysV_hexagon::SetReturnValueObject(lldb::StackFrameSP &frame_sp,
1200                                       lldb::ValueObjectSP &new_value_sp) {
1201   Status error;
1202   return error;
1203 }
1204
1205 ValueObjectSP ABISysV_hexagon::GetReturnValueObjectSimple(
1206     Thread &thread, CompilerType &return_compiler_type) const {
1207   ValueObjectSP return_valobj_sp;
1208   return return_valobj_sp;
1209 }
1210
1211 ValueObjectSP ABISysV_hexagon::GetReturnValueObjectImpl(
1212     Thread &thread, CompilerType &return_compiler_type) const {
1213   ValueObjectSP return_valobj_sp;
1214   return return_valobj_sp;
1215 }
1216
1217 // called when we are on the first instruction of a new function for hexagon
1218 // the return address is in RA (R31)
1219 bool ABISysV_hexagon::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
1220   unwind_plan.Clear();
1221   unwind_plan.SetRegisterKind(eRegisterKindGeneric);
1222   unwind_plan.SetReturnAddressRegister(LLDB_REGNUM_GENERIC_RA);
1223
1224   UnwindPlan::RowSP row(new UnwindPlan::Row);
1225
1226   // Our Call Frame Address is the stack pointer value
1227   row->GetCFAValue().SetIsRegisterPlusOffset(LLDB_REGNUM_GENERIC_SP, 4);
1228   row->SetOffset(0);
1229
1230   // The previous PC is in the LR
1231   row->SetRegisterLocationToRegister(LLDB_REGNUM_GENERIC_PC,
1232                                      LLDB_REGNUM_GENERIC_RA, true);
1233   unwind_plan.AppendRow(row);
1234
1235   unwind_plan.SetSourceName("hexagon at-func-entry default");
1236   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
1237   return true;
1238 }
1239
1240 bool ABISysV_hexagon::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
1241   unwind_plan.Clear();
1242   unwind_plan.SetRegisterKind(eRegisterKindGeneric);
1243
1244   uint32_t fp_reg_num = LLDB_REGNUM_GENERIC_FP;
1245   uint32_t sp_reg_num = LLDB_REGNUM_GENERIC_SP;
1246   uint32_t pc_reg_num = LLDB_REGNUM_GENERIC_PC;
1247
1248   UnwindPlan::RowSP row(new UnwindPlan::Row);
1249
1250   row->GetCFAValue().SetIsRegisterPlusOffset(LLDB_REGNUM_GENERIC_FP, 8);
1251
1252   row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, -8, true);
1253   row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, -4, true);
1254   row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
1255
1256   unwind_plan.AppendRow(row);
1257   unwind_plan.SetSourceName("hexagon default unwind plan");
1258   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
1259   unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
1260   return true;
1261 }
1262
1263 /*
1264     Register            Usage                                   Saved By
1265
1266     R0  - R5            parameters(a)                   -
1267     R6  - R15           Scratch(b)                              Caller
1268     R16 - R27           Scratch                                 Callee
1269     R28                         Scratch(b)                              Caller
1270     R29 - R31           Stack Frames                    Callee(c)
1271     P3:0                        Processor State                 Caller
1272
1273     a = the caller can change parameter values
1274     b = R14 - R15 and R28 are used by the procedure linkage table
1275     c = R29 - R31 are saved and restored by allocframe() and deallocframe()
1276 */
1277 bool ABISysV_hexagon::RegisterIsVolatile(const RegisterInfo *reg_info) {
1278   return !RegisterIsCalleeSaved(reg_info);
1279 }
1280
1281 bool ABISysV_hexagon::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {
1282   int reg = ((reg_info->byte_offset) / 4);
1283
1284   bool save = (reg >= 16) && (reg <= 27);
1285   save |= (reg >= 29) && (reg <= 32);
1286
1287   return save;
1288 }
1289
1290 void ABISysV_hexagon::Initialize() {
1291   PluginManager::RegisterPlugin(GetPluginNameStatic(),
1292                                 "System V ABI for hexagon targets",
1293                                 CreateInstance);
1294 }
1295
1296 void ABISysV_hexagon::Terminate() {
1297   PluginManager::UnregisterPlugin(CreateInstance);
1298 }
1299
1300 lldb_private::ConstString ABISysV_hexagon::GetPluginNameStatic() {
1301   static ConstString g_name("sysv-hexagon");
1302   return g_name;
1303 }
1304
1305 //------------------------------------------------------------------
1306 // PluginInterface protocol
1307 //------------------------------------------------------------------
1308
1309 lldb_private::ConstString ABISysV_hexagon::GetPluginName() {
1310   return GetPluginNameStatic();
1311 }
1312
1313 uint32_t ABISysV_hexagon::GetPluginVersion() { return 1; }
1314
1315 // get value object specialized to work with llvm IR types
1316 lldb::ValueObjectSP
1317 ABISysV_hexagon::GetReturnValueObjectImpl(lldb_private::Thread &thread,
1318                                           llvm::Type &retType) const {
1319   Value value;
1320   ValueObjectSP vObjSP;
1321
1322   // get the current register context
1323   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
1324   if (!reg_ctx)
1325     return vObjSP;
1326
1327   // for now just pop R0 to find the return value
1328   const lldb_private::RegisterInfo *r0_info =
1329       reg_ctx->GetRegisterInfoAtIndex(0);
1330   if (r0_info == nullptr)
1331     return vObjSP;
1332
1333   // void return type
1334   if (retType.isVoidTy()) {
1335     value.GetScalar() = 0;
1336   }
1337   // integer / pointer return type
1338   else if (retType.isIntegerTy() || retType.isPointerTy()) {
1339     // read r0 register value
1340     lldb_private::RegisterValue r0_value;
1341     if (!reg_ctx->ReadRegister(r0_info, r0_value))
1342       return vObjSP;
1343
1344     // push r0 into value
1345     uint32_t r0_u32 = r0_value.GetAsUInt32();
1346
1347     // account for integer size
1348     if (retType.isIntegerTy() && retType.isSized()) {
1349       uint64_t size = retType.getScalarSizeInBits();
1350       uint64_t mask = (1ull << size) - 1;
1351       // mask out higher order bits then the type we expect
1352       r0_u32 &= mask;
1353     }
1354
1355     value.GetScalar() = r0_u32;
1356   }
1357   // unsupported return type
1358   else
1359     return vObjSP;
1360
1361   // pack the value into a ValueObjectSP
1362   vObjSP = ValueObjectConstResult::Create(thread.GetStackFrameAtIndex(0).get(),
1363                                           value, ConstString(""));
1364   return vObjSP;
1365 }