]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/source/Plugins/ABI/Windows-x86_64/ABIWindows_x86_64.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / source / Plugins / ABI / Windows-x86_64 / ABIWindows_x86_64.cpp
1 //===-- ABIWindows_x86_64.cpp --------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "ABIWindows_x86_64.h"
10
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/ADT/Triple.h"
14
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/Value.h"
18 #include "lldb/Core/ValueObjectConstResult.h"
19 #include "lldb/Core/ValueObjectMemory.h"
20 #include "lldb/Core/ValueObjectRegister.h"
21 #include "lldb/Symbol/UnwindPlan.h"
22 #include "lldb/Target/Process.h"
23 #include "lldb/Target/RegisterContext.h"
24 #include "lldb/Target/StackFrame.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Target/Thread.h"
27 #include "lldb/Utility/ConstString.h"
28 #include "lldb/Utility/DataExtractor.h"
29 #include "lldb/Utility/Log.h"
30 #include "lldb/Utility/RegisterValue.h"
31 #include "lldb/Utility/Status.h"
32
33 using namespace lldb;
34 using namespace lldb_private;
35
36 enum dwarf_regnums {
37   dwarf_rax = 0,
38   dwarf_rdx,
39   dwarf_rcx,
40   dwarf_rbx,
41   dwarf_rsi,
42   dwarf_rdi,
43   dwarf_rbp,
44   dwarf_rsp,
45   dwarf_r8,
46   dwarf_r9,
47   dwarf_r10,
48   dwarf_r11,
49   dwarf_r12,
50   dwarf_r13,
51   dwarf_r14,
52   dwarf_r15,
53   dwarf_rip,
54   dwarf_xmm0,
55   dwarf_xmm1,
56   dwarf_xmm2,
57   dwarf_xmm3,
58   dwarf_xmm4,
59   dwarf_xmm5,
60   dwarf_xmm6,
61   dwarf_xmm7,
62   dwarf_xmm8,
63   dwarf_xmm9,
64   dwarf_xmm10,
65   dwarf_xmm11,
66   dwarf_xmm12,
67   dwarf_xmm13,
68   dwarf_xmm14,
69   dwarf_xmm15,
70   dwarf_stmm0,
71   dwarf_stmm1,
72   dwarf_stmm2,
73   dwarf_stmm3,
74   dwarf_stmm4,
75   dwarf_stmm5,
76   dwarf_stmm6,
77   dwarf_stmm7,
78   dwarf_ymm0,
79   dwarf_ymm1,
80   dwarf_ymm2,
81   dwarf_ymm3,
82   dwarf_ymm4,
83   dwarf_ymm5,
84   dwarf_ymm6,
85   dwarf_ymm7,
86   dwarf_ymm8,
87   dwarf_ymm9,
88   dwarf_ymm10,
89   dwarf_ymm11,
90   dwarf_ymm12,
91   dwarf_ymm13,
92   dwarf_ymm14,
93   dwarf_ymm15,
94   dwarf_bnd0 = 126,
95   dwarf_bnd1,
96   dwarf_bnd2,
97   dwarf_bnd3
98 };
99
100 static RegisterInfo g_register_infos[] = {
101     //  NAME      ALT      SZ OFF ENCODING         FORMAT              EH_FRAME
102     //  DWARF                 GENERIC                     PROCESS PLUGIN
103     //  LLDB NATIVE
104     //  ========  =======  == === =============    ===================
105     //  ======================= =====================
106     //  =========================== ===================== ======================
107     {"rax",
108      nullptr,
109      8,
110      0,
111      eEncodingUint,
112      eFormatHex,
113      {dwarf_rax, dwarf_rax, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
114       LLDB_INVALID_REGNUM},
115      nullptr,
116      nullptr,
117      nullptr,
118      0},
119     {"rbx",
120      nullptr,
121      8,
122      0,
123      eEncodingUint,
124      eFormatHex,
125      {dwarf_rbx, dwarf_rbx, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
126       LLDB_INVALID_REGNUM},
127      nullptr,
128      nullptr,
129      nullptr,
130      0},
131     {"rcx",
132      "arg1",
133      8,
134      0,
135      eEncodingUint,
136      eFormatHex,
137      {dwarf_rcx, dwarf_rcx, LLDB_REGNUM_GENERIC_ARG1, LLDB_INVALID_REGNUM,
138       LLDB_INVALID_REGNUM},
139      nullptr,
140      nullptr,
141      nullptr,
142      0},
143     {"rdx",
144      "arg2",
145      8,
146      0,
147      eEncodingUint,
148      eFormatHex,
149      {dwarf_rdx, dwarf_rdx, LLDB_REGNUM_GENERIC_ARG2, LLDB_INVALID_REGNUM,
150       LLDB_INVALID_REGNUM},
151      nullptr,
152      nullptr,
153      nullptr,
154      0},
155     {"rsi",
156      nullptr,
157      8,
158      0,
159      eEncodingUint,
160      eFormatHex,
161      {dwarf_rsi, dwarf_rsi, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
162       LLDB_INVALID_REGNUM},
163      nullptr,
164      nullptr,
165      nullptr,
166      0},
167     {"rdi",
168      nullptr,
169      8,
170      0,
171      eEncodingUint,
172      eFormatHex,
173      {dwarf_rdi, dwarf_rdi, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
174       LLDB_INVALID_REGNUM},
175      nullptr,
176      nullptr,
177      nullptr,
178      0},
179     {"rbp",
180      "fp",
181      8,
182      0,
183      eEncodingUint,
184      eFormatHex,
185      {dwarf_rbp, dwarf_rbp, LLDB_REGNUM_GENERIC_FP, LLDB_INVALID_REGNUM,
186       LLDB_INVALID_REGNUM},
187      nullptr,
188      nullptr,
189      nullptr,
190      0},
191     {"rsp",
192      "sp",
193      8,
194      0,
195      eEncodingUint,
196      eFormatHex,
197      {dwarf_rsp, dwarf_rsp, LLDB_REGNUM_GENERIC_SP, LLDB_INVALID_REGNUM,
198       LLDB_INVALID_REGNUM},
199      nullptr,
200      nullptr,
201      nullptr,
202      0},
203     {"r8",
204      "arg3",
205      8,
206      0,
207      eEncodingUint,
208      eFormatHex,
209      {dwarf_r8, dwarf_r8, LLDB_REGNUM_GENERIC_ARG3, LLDB_INVALID_REGNUM,
210       LLDB_INVALID_REGNUM},
211      nullptr,
212      nullptr,
213      nullptr,
214      0},
215     {"r9",
216      "arg4",
217      8,
218      0,
219      eEncodingUint,
220      eFormatHex,
221      {dwarf_r9, dwarf_r9, LLDB_REGNUM_GENERIC_ARG4, LLDB_INVALID_REGNUM,
222       LLDB_INVALID_REGNUM},
223      nullptr,
224      nullptr,
225      nullptr,
226      0},
227     {"r10",
228      nullptr,
229      8,
230      0,
231      eEncodingUint,
232      eFormatHex,
233      {dwarf_r10, dwarf_r10, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
234       LLDB_INVALID_REGNUM},
235      nullptr,
236      nullptr,
237      nullptr,
238      0},
239     {"r11",
240      nullptr,
241      8,
242      0,
243      eEncodingUint,
244      eFormatHex,
245      {dwarf_r11, dwarf_r11, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
246       LLDB_INVALID_REGNUM},
247      nullptr,
248      nullptr,
249      nullptr,
250      0},
251     {"r12",
252      nullptr,
253      8,
254      0,
255      eEncodingUint,
256      eFormatHex,
257      {dwarf_r12, dwarf_r12, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
258       LLDB_INVALID_REGNUM},
259      nullptr,
260      nullptr,
261      nullptr,
262      0},
263     {"r13",
264      nullptr,
265      8,
266      0,
267      eEncodingUint,
268      eFormatHex,
269      {dwarf_r13, dwarf_r13, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
270       LLDB_INVALID_REGNUM},
271      nullptr,
272      nullptr,
273      nullptr,
274      0},
275     {"r14",
276      nullptr,
277      8,
278      0,
279      eEncodingUint,
280      eFormatHex,
281      {dwarf_r14, dwarf_r14, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
282       LLDB_INVALID_REGNUM},
283      nullptr,
284      nullptr,
285      nullptr,
286      0},
287     {"r15",
288      nullptr,
289      8,
290      0,
291      eEncodingUint,
292      eFormatHex,
293      {dwarf_r15, dwarf_r15, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
294       LLDB_INVALID_REGNUM},
295      nullptr,
296      nullptr,
297      nullptr,
298      0},
299     {"rip",
300      "pc",
301      8,
302      0,
303      eEncodingUint,
304      eFormatHex,
305      {dwarf_rip, dwarf_rip, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_REGNUM,
306       LLDB_INVALID_REGNUM},
307      nullptr,
308      nullptr,
309      nullptr,
310      0},
311     {"rflags",
312      nullptr,
313      4,
314      0,
315      eEncodingUint,
316      eFormatHex,
317      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_REGNUM_GENERIC_FLAGS,
318       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
319      nullptr,
320      nullptr,
321      nullptr,
322      0},
323     {"cs",
324      nullptr,
325      4,
326      0,
327      eEncodingUint,
328      eFormatHex,
329      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
330       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
331      nullptr,
332      nullptr,
333      nullptr,
334      0},
335     {"ss",
336      nullptr,
337      4,
338      0,
339      eEncodingUint,
340      eFormatHex,
341      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
342       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
343      nullptr,
344      nullptr,
345      nullptr,
346      0},
347     {"ds",
348      nullptr,
349      4,
350      0,
351      eEncodingUint,
352      eFormatHex,
353      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
354       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
355      nullptr,
356      nullptr,
357      nullptr,
358      0},
359     {"es",
360      nullptr,
361      4,
362      0,
363      eEncodingUint,
364      eFormatHex,
365      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
366       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
367      nullptr,
368      nullptr,
369      nullptr,
370      0},
371     {"fs",
372      nullptr,
373      4,
374      0,
375      eEncodingUint,
376      eFormatHex,
377      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
378       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
379      nullptr,
380      nullptr,
381      nullptr,
382      0},
383     {"gs",
384      nullptr,
385      4,
386      0,
387      eEncodingUint,
388      eFormatHex,
389      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
390       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
391      nullptr,
392      nullptr,
393      nullptr,
394      0},
395     {"stmm0",
396      nullptr,
397      10,
398      0,
399      eEncodingVector,
400      eFormatVectorOfUInt8,
401      {dwarf_stmm0, dwarf_stmm0, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
402       LLDB_INVALID_REGNUM},
403      nullptr,
404      nullptr,
405      nullptr,
406      0},
407     {"stmm1",
408      nullptr,
409      10,
410      0,
411      eEncodingVector,
412      eFormatVectorOfUInt8,
413      {dwarf_stmm1, dwarf_stmm1, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
414       LLDB_INVALID_REGNUM},
415      nullptr,
416      nullptr,
417      nullptr,
418      0},
419     {"stmm2",
420      nullptr,
421      10,
422      0,
423      eEncodingVector,
424      eFormatVectorOfUInt8,
425      {dwarf_stmm2, dwarf_stmm2, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
426       LLDB_INVALID_REGNUM},
427      nullptr,
428      nullptr,
429      nullptr,
430      0},
431     {"stmm3",
432      nullptr,
433      10,
434      0,
435      eEncodingVector,
436      eFormatVectorOfUInt8,
437      {dwarf_stmm3, dwarf_stmm3, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
438       LLDB_INVALID_REGNUM},
439      nullptr,
440      nullptr,
441      nullptr,
442      0},
443     {"stmm4",
444      nullptr,
445      10,
446      0,
447      eEncodingVector,
448      eFormatVectorOfUInt8,
449      {dwarf_stmm4, dwarf_stmm4, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
450       LLDB_INVALID_REGNUM},
451      nullptr,
452      nullptr,
453      nullptr,
454      0},
455     {"stmm5",
456      nullptr,
457      10,
458      0,
459      eEncodingVector,
460      eFormatVectorOfUInt8,
461      {dwarf_stmm5, dwarf_stmm5, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
462       LLDB_INVALID_REGNUM},
463      nullptr,
464      nullptr,
465      nullptr,
466      0},
467     {"stmm6",
468      nullptr,
469      10,
470      0,
471      eEncodingVector,
472      eFormatVectorOfUInt8,
473      {dwarf_stmm6, dwarf_stmm6, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
474       LLDB_INVALID_REGNUM},
475      nullptr,
476      nullptr,
477      nullptr,
478      0},
479     {"stmm7",
480      nullptr,
481      10,
482      0,
483      eEncodingVector,
484      eFormatVectorOfUInt8,
485      {dwarf_stmm7, dwarf_stmm7, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
486       LLDB_INVALID_REGNUM},
487      nullptr,
488      nullptr,
489      nullptr,
490      0},
491     {"fctrl",
492      nullptr,
493      4,
494      0,
495      eEncodingUint,
496      eFormatHex,
497      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
498       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
499      nullptr,
500      nullptr,
501      nullptr,
502      0},
503     {"fstat",
504      nullptr,
505      4,
506      0,
507      eEncodingUint,
508      eFormatHex,
509      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
510       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
511      nullptr,
512      nullptr,
513      nullptr,
514      0},
515     {"ftag",
516      nullptr,
517      4,
518      0,
519      eEncodingUint,
520      eFormatHex,
521      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
522       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
523      nullptr,
524      nullptr,
525      nullptr,
526      0},
527     {"fiseg",
528      nullptr,
529      4,
530      0,
531      eEncodingUint,
532      eFormatHex,
533      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
534       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
535      nullptr,
536      nullptr,
537      nullptr,
538      0},
539     {"fioff",
540      nullptr,
541      4,
542      0,
543      eEncodingUint,
544      eFormatHex,
545      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
546       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
547      nullptr,
548      nullptr,
549      nullptr,
550      0},
551     {"foseg",
552      nullptr,
553      4,
554      0,
555      eEncodingUint,
556      eFormatHex,
557      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
558       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
559      nullptr,
560      nullptr,
561      nullptr,
562      0},
563     {"fooff",
564      nullptr,
565      4,
566      0,
567      eEncodingUint,
568      eFormatHex,
569      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
570       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
571      nullptr,
572      nullptr,
573      nullptr,
574      0},
575     {"fop",
576      nullptr,
577      4,
578      0,
579      eEncodingUint,
580      eFormatHex,
581      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
582       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
583      nullptr,
584      nullptr,
585      nullptr,
586      0},
587     {"xmm0",
588      nullptr,
589      16,
590      0,
591      eEncodingVector,
592      eFormatVectorOfUInt8,
593      {dwarf_xmm0, dwarf_xmm0, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
594       LLDB_INVALID_REGNUM},
595      nullptr,
596      nullptr,
597      nullptr,
598      0},
599     {"xmm1",
600      nullptr,
601      16,
602      0,
603      eEncodingVector,
604      eFormatVectorOfUInt8,
605      {dwarf_xmm1, dwarf_xmm1, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
606       LLDB_INVALID_REGNUM},
607      nullptr,
608      nullptr,
609      nullptr,
610      0},
611     {"xmm2",
612      nullptr,
613      16,
614      0,
615      eEncodingVector,
616      eFormatVectorOfUInt8,
617      {dwarf_xmm2, dwarf_xmm2, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
618       LLDB_INVALID_REGNUM},
619      nullptr,
620      nullptr,
621      nullptr,
622      0},
623     {"xmm3",
624      nullptr,
625      16,
626      0,
627      eEncodingVector,
628      eFormatVectorOfUInt8,
629      {dwarf_xmm3, dwarf_xmm3, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
630       LLDB_INVALID_REGNUM},
631      nullptr,
632      nullptr,
633      nullptr,
634      0},
635     {"xmm4",
636      nullptr,
637      16,
638      0,
639      eEncodingVector,
640      eFormatVectorOfUInt8,
641      {dwarf_xmm4, dwarf_xmm4, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
642       LLDB_INVALID_REGNUM},
643      nullptr,
644      nullptr,
645      nullptr,
646      0},
647     {"xmm5",
648      nullptr,
649      16,
650      0,
651      eEncodingVector,
652      eFormatVectorOfUInt8,
653      {dwarf_xmm5, dwarf_xmm5, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
654       LLDB_INVALID_REGNUM},
655      nullptr,
656      nullptr,
657      nullptr,
658      0},
659     {"xmm6",
660      nullptr,
661      16,
662      0,
663      eEncodingVector,
664      eFormatVectorOfUInt8,
665      {dwarf_xmm6, dwarf_xmm6, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
666       LLDB_INVALID_REGNUM},
667      nullptr,
668      nullptr,
669      nullptr,
670      0},
671     {"xmm7",
672      nullptr,
673      16,
674      0,
675      eEncodingVector,
676      eFormatVectorOfUInt8,
677      {dwarf_xmm7, dwarf_xmm7, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
678       LLDB_INVALID_REGNUM},
679      nullptr,
680      nullptr,
681      nullptr,
682      0},
683     {"xmm8",
684      nullptr,
685      16,
686      0,
687      eEncodingVector,
688      eFormatVectorOfUInt8,
689      {dwarf_xmm8, dwarf_xmm8, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
690       LLDB_INVALID_REGNUM},
691      nullptr,
692      nullptr,
693      nullptr,
694      0},
695     {"xmm9",
696      nullptr,
697      16,
698      0,
699      eEncodingVector,
700      eFormatVectorOfUInt8,
701      {dwarf_xmm9, dwarf_xmm9, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
702       LLDB_INVALID_REGNUM},
703      nullptr,
704      nullptr,
705      nullptr,
706      0},
707     {"xmm10",
708      nullptr,
709      16,
710      0,
711      eEncodingVector,
712      eFormatVectorOfUInt8,
713      {dwarf_xmm10, dwarf_xmm10, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
714       LLDB_INVALID_REGNUM},
715      nullptr,
716      nullptr,
717      nullptr,
718      0},
719     {"xmm11",
720      nullptr,
721      16,
722      0,
723      eEncodingVector,
724      eFormatVectorOfUInt8,
725      {dwarf_xmm11, dwarf_xmm11, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
726       LLDB_INVALID_REGNUM},
727      nullptr,
728      nullptr,
729      nullptr,
730      0},
731     {"xmm12",
732      nullptr,
733      16,
734      0,
735      eEncodingVector,
736      eFormatVectorOfUInt8,
737      {dwarf_xmm12, dwarf_xmm12, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
738       LLDB_INVALID_REGNUM},
739      nullptr,
740      nullptr,
741      nullptr,
742      0},
743     {"xmm13",
744      nullptr,
745      16,
746      0,
747      eEncodingVector,
748      eFormatVectorOfUInt8,
749      {dwarf_xmm13, dwarf_xmm13, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
750       LLDB_INVALID_REGNUM},
751      nullptr,
752      nullptr,
753      nullptr,
754      0},
755     {"xmm14",
756      nullptr,
757      16,
758      0,
759      eEncodingVector,
760      eFormatVectorOfUInt8,
761      {dwarf_xmm14, dwarf_xmm14, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
762       LLDB_INVALID_REGNUM},
763      nullptr,
764      nullptr,
765      nullptr,
766      0},
767     {"xmm15",
768      nullptr,
769      16,
770      0,
771      eEncodingVector,
772      eFormatVectorOfUInt8,
773      {dwarf_xmm15, dwarf_xmm15, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
774       LLDB_INVALID_REGNUM},
775      nullptr,
776      nullptr,
777      nullptr,
778      0},
779     {"mxcsr",
780      nullptr,
781      4,
782      0,
783      eEncodingUint,
784      eFormatHex,
785      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
786       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
787      nullptr,
788      nullptr,
789      nullptr,
790      0},
791     {"ymm0",
792      nullptr,
793      32,
794      0,
795      eEncodingVector,
796      eFormatVectorOfUInt8,
797      {dwarf_ymm0, dwarf_ymm0, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
798       LLDB_INVALID_REGNUM},
799      nullptr,
800      nullptr,
801      nullptr,
802      0},
803     {"ymm1",
804      nullptr,
805      32,
806      0,
807      eEncodingVector,
808      eFormatVectorOfUInt8,
809      {dwarf_ymm1, dwarf_ymm1, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
810       LLDB_INVALID_REGNUM},
811      nullptr,
812      nullptr,
813      nullptr,
814      0},
815     {"ymm2",
816      nullptr,
817      32,
818      0,
819      eEncodingVector,
820      eFormatVectorOfUInt8,
821      {dwarf_ymm2, dwarf_ymm2, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
822       LLDB_INVALID_REGNUM},
823      nullptr,
824      nullptr,
825      nullptr,
826      0},
827     {"ymm3",
828      nullptr,
829      32,
830      0,
831      eEncodingVector,
832      eFormatVectorOfUInt8,
833      {dwarf_ymm3, dwarf_ymm3, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
834       LLDB_INVALID_REGNUM},
835      nullptr,
836      nullptr,
837      nullptr,
838      0},
839     {"ymm4",
840      nullptr,
841      32,
842      0,
843      eEncodingVector,
844      eFormatVectorOfUInt8,
845      {dwarf_ymm4, dwarf_ymm4, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
846       LLDB_INVALID_REGNUM},
847      nullptr,
848      nullptr,
849      nullptr,
850      0},
851     {"ymm5",
852      nullptr,
853      32,
854      0,
855      eEncodingVector,
856      eFormatVectorOfUInt8,
857      {dwarf_ymm5, dwarf_ymm5, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
858       LLDB_INVALID_REGNUM},
859      nullptr,
860      nullptr,
861      nullptr,
862      0},
863     {"ymm6",
864      nullptr,
865      32,
866      0,
867      eEncodingVector,
868      eFormatVectorOfUInt8,
869      {dwarf_ymm6, dwarf_ymm6, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
870       LLDB_INVALID_REGNUM},
871      nullptr,
872      nullptr,
873      nullptr,
874      0},
875     {"ymm7",
876      nullptr,
877      32,
878      0,
879      eEncodingVector,
880      eFormatVectorOfUInt8,
881      {dwarf_ymm7, dwarf_ymm7, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
882       LLDB_INVALID_REGNUM},
883      nullptr,
884      nullptr,
885      nullptr,
886      0},
887     {"ymm8",
888      nullptr,
889      32,
890      0,
891      eEncodingVector,
892      eFormatVectorOfUInt8,
893      {dwarf_ymm8, dwarf_ymm8, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
894       LLDB_INVALID_REGNUM},
895      nullptr,
896      nullptr,
897      nullptr,
898      0},
899     {"ymm9",
900      nullptr,
901      32,
902      0,
903      eEncodingVector,
904      eFormatVectorOfUInt8,
905      {dwarf_ymm9, dwarf_ymm9, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
906       LLDB_INVALID_REGNUM},
907      nullptr,
908      nullptr,
909      nullptr,
910      0},
911     {"ymm10",
912      nullptr,
913      32,
914      0,
915      eEncodingVector,
916      eFormatVectorOfUInt8,
917      {dwarf_ymm10, dwarf_ymm10, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
918       LLDB_INVALID_REGNUM},
919      nullptr,
920      nullptr,
921      nullptr,
922      0},
923     {"ymm11",
924      nullptr,
925      32,
926      0,
927      eEncodingVector,
928      eFormatVectorOfUInt8,
929      {dwarf_ymm11, dwarf_ymm11, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
930       LLDB_INVALID_REGNUM},
931      nullptr,
932      nullptr,
933      nullptr,
934      0},
935     {"ymm12",
936      nullptr,
937      32,
938      0,
939      eEncodingVector,
940      eFormatVectorOfUInt8,
941      {dwarf_ymm12, dwarf_ymm12, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
942       LLDB_INVALID_REGNUM},
943      nullptr,
944      nullptr,
945      nullptr,
946      0},
947     {"ymm13",
948      nullptr,
949      32,
950      0,
951      eEncodingVector,
952      eFormatVectorOfUInt8,
953      {dwarf_ymm13, dwarf_ymm13, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
954       LLDB_INVALID_REGNUM},
955      nullptr,
956      nullptr,
957      nullptr,
958      0},
959     {"ymm14",
960      nullptr,
961      32,
962      0,
963      eEncodingVector,
964      eFormatVectorOfUInt8,
965      {dwarf_ymm14, dwarf_ymm14, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
966       LLDB_INVALID_REGNUM},
967      nullptr,
968      nullptr,
969      nullptr,
970      0},
971     {"ymm15",
972      nullptr,
973      32,
974      0,
975      eEncodingVector,
976      eFormatVectorOfUInt8,
977      {dwarf_ymm15, dwarf_ymm15, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
978       LLDB_INVALID_REGNUM},
979      nullptr,
980      nullptr,
981      nullptr,
982      0},
983     {"bnd0",
984      nullptr,
985      16,
986      0,
987      eEncodingVector,
988      eFormatVectorOfUInt64,
989      {dwarf_bnd0, dwarf_bnd0, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
990       LLDB_INVALID_REGNUM},
991      nullptr,
992      nullptr,
993      nullptr,
994      0},
995     {"bnd1",
996      nullptr,
997      16,
998      0,
999      eEncodingVector,
1000      eFormatVectorOfUInt64,
1001      {dwarf_bnd1, dwarf_bnd1, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
1002       LLDB_INVALID_REGNUM},
1003      nullptr,
1004      nullptr,
1005      nullptr,
1006      0},
1007     {"bnd2",
1008      nullptr,
1009      16,
1010      0,
1011      eEncodingVector,
1012      eFormatVectorOfUInt64,
1013      {dwarf_bnd2, dwarf_bnd2, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
1014       LLDB_INVALID_REGNUM},
1015      nullptr,
1016      nullptr,
1017      nullptr,
1018      0},
1019     {"bnd3",
1020      nullptr,
1021      16,
1022      0,
1023      eEncodingVector,
1024      eFormatVectorOfUInt64,
1025      {dwarf_bnd3, dwarf_bnd3, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
1026       LLDB_INVALID_REGNUM},
1027      nullptr,
1028      nullptr,
1029      nullptr,
1030      0},
1031     {"bndcfgu",
1032      nullptr,
1033      8,
1034      0,
1035      eEncodingVector,
1036      eFormatVectorOfUInt8,
1037      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
1038       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
1039      nullptr,
1040      nullptr,
1041      nullptr,
1042      0},
1043     {"bndstatus",
1044      nullptr,
1045      8,
1046      0,
1047      eEncodingVector,
1048      eFormatVectorOfUInt8,
1049      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
1050       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
1051      nullptr,
1052      nullptr,
1053      nullptr,
1054      0}};
1055
1056 static const uint32_t k_num_register_infos =
1057     llvm::array_lengthof(g_register_infos);
1058 static bool g_register_info_names_constified = false;
1059
1060 const lldb_private::RegisterInfo *
1061 ABIWindows_x86_64::GetRegisterInfoArray(uint32_t &count) {
1062   // Make the C-string names and alt_names for the register infos into const
1063   // C-string values by having the ConstString unique the names in the global
1064   // constant C-string pool.
1065   if (!g_register_info_names_constified) {
1066     g_register_info_names_constified = true;
1067     for (uint32_t i = 0; i < k_num_register_infos; ++i) {
1068       if (g_register_infos[i].name)
1069         g_register_infos[i].name =
1070             ConstString(g_register_infos[i].name).GetCString();
1071       if (g_register_infos[i].alt_name)
1072         g_register_infos[i].alt_name =
1073             ConstString(g_register_infos[i].alt_name).GetCString();
1074     }
1075   }
1076   count = k_num_register_infos;
1077   return g_register_infos;
1078 }
1079
1080 bool ABIWindows_x86_64::GetPointerReturnRegister(const char *&name) {
1081   name = "rax";
1082   return true;
1083 }
1084
1085 size_t ABIWindows_x86_64::GetRedZoneSize() const { return 0; }
1086
1087 //------------------------------------------------------------------
1088 // Static Functions
1089 //------------------------------------------------------------------
1090
1091 ABISP
1092 ABIWindows_x86_64::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) {
1093   if (arch.GetTriple().getArch() == llvm::Triple::x86_64 &&
1094       arch.GetTriple().isOSWindows()) {
1095     return ABISP(new ABIWindows_x86_64(process_sp));
1096   }
1097   return ABISP();
1098 }
1099
1100 bool ABIWindows_x86_64::PrepareTrivialCall(Thread &thread, addr_t sp,
1101                                            addr_t func_addr, addr_t return_addr,
1102                                            llvm::ArrayRef<addr_t> args) const {
1103   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1104
1105   if (log) {
1106     StreamString s;
1107     s.Printf("ABIWindows_x86_64::PrepareTrivialCall (tid = 0x%" PRIx64
1108              ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
1109              ", return_addr = 0x%" PRIx64,
1110              thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
1111              (uint64_t)return_addr);
1112
1113     for (size_t i = 0; i < args.size(); ++i)
1114       s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),
1115                args[i]);
1116     s.PutCString(")");
1117     log->PutString(s.GetString());
1118   }
1119
1120   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
1121   if (!reg_ctx)
1122     return false;
1123
1124   const RegisterInfo *reg_info = nullptr;
1125
1126   if (args.size() > 4) // Windows x64 only put first 4 arguments into registers
1127     return false;
1128
1129   for (size_t i = 0; i < args.size(); ++i) {
1130     reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
1131                                         LLDB_REGNUM_GENERIC_ARG1 + i);
1132     if (log)
1133       log->Printf("About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s",
1134                   static_cast<uint64_t>(i + 1), args[i], reg_info->name);
1135     if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i]))
1136       return false;
1137   }
1138
1139   // First, align the SP
1140
1141   if (log)
1142     log->Printf("16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64,
1143                 (uint64_t)sp, (uint64_t)(sp & ~0xfull));
1144
1145   sp &= ~(0xfull); // 16-byte alignment
1146
1147   sp -= 8; // return address
1148
1149   Status error;
1150   const RegisterInfo *pc_reg_info =
1151       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
1152   const RegisterInfo *sp_reg_info =
1153       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
1154   ProcessSP process_sp(thread.GetProcess());
1155
1156   RegisterValue reg_value;
1157   if (log)
1158     log->Printf("Pushing the return address onto the stack: 0x%" PRIx64
1159                 ": 0x%" PRIx64,
1160                 (uint64_t)sp, (uint64_t)return_addr);
1161
1162   // Save return address onto the stack
1163   if (!process_sp->WritePointerToMemory(sp, return_addr, error))
1164     return false;
1165
1166   // %rsp is set to the actual stack value.
1167
1168   if (log)
1169     log->Printf("Writing SP: 0x%" PRIx64, (uint64_t)sp);
1170
1171   if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp))
1172     return false;
1173
1174   // %rip is set to the address of the called function.
1175
1176   if (log)
1177     log->Printf("Writing IP: 0x%" PRIx64, (uint64_t)func_addr);
1178
1179   if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr))
1180     return false;
1181
1182   return true;
1183 }
1184
1185 static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,
1186                                 bool is_signed, Thread &thread,
1187                                 uint32_t *argument_register_ids,
1188                                 unsigned int &current_argument_register,
1189                                 addr_t &current_stack_argument) {
1190   if (bit_width > 64)
1191     return false; // Scalar can't hold large integer arguments
1192
1193   if (current_argument_register < 4) { // Windows pass first 4 arguments to register
1194     scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
1195         argument_register_ids[current_argument_register], 0);
1196     current_argument_register++;
1197     if (is_signed)
1198       scalar.SignExtend(bit_width);
1199         return true;
1200   }
1201   uint32_t byte_size = (bit_width + (CHAR_BIT - 1)) / CHAR_BIT;
1202   Status error;
1203   if (thread.GetProcess()->ReadScalarIntegerFromMemory(
1204           current_stack_argument, byte_size, is_signed, scalar, error)) {
1205     current_stack_argument += byte_size;
1206     return true;
1207   }
1208   return false;
1209 }
1210
1211 bool ABIWindows_x86_64::GetArgumentValues(Thread &thread,
1212                                        ValueList &values) const {
1213   unsigned int num_values = values.GetSize();
1214   unsigned int value_index;
1215
1216   // Extract the register context so we can read arguments from registers
1217
1218   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
1219
1220   if (!reg_ctx)
1221     return false;
1222
1223   // Get the pointer to the first stack argument so we have a place to start
1224   // when reading data
1225
1226   addr_t sp = reg_ctx->GetSP(0);
1227
1228   if (!sp)
1229     return false;
1230
1231   addr_t current_stack_argument = sp + 8; // jump over return address
1232
1233   uint32_t argument_register_ids[4];
1234
1235   argument_register_ids[0] =
1236       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1)
1237           ->kinds[eRegisterKindLLDB];
1238   argument_register_ids[1] =
1239       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2)
1240           ->kinds[eRegisterKindLLDB];
1241   argument_register_ids[2] =
1242       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3)
1243           ->kinds[eRegisterKindLLDB];
1244   argument_register_ids[3] =
1245       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4)
1246           ->kinds[eRegisterKindLLDB];
1247
1248   unsigned int current_argument_register = 0;
1249
1250   for (value_index = 0; value_index < num_values; ++value_index) {
1251     Value *value = values.GetValueAtIndex(value_index);
1252
1253     if (!value)
1254       return false;
1255
1256     CompilerType compiler_type = value->GetCompilerType();
1257     llvm::Optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread);
1258     if (!bit_size)
1259       return false;
1260     bool is_signed;
1261
1262     if (compiler_type.IsIntegerOrEnumerationType(is_signed)) {
1263       ReadIntegerArgument(value->GetScalar(), *bit_size, is_signed, thread,
1264                           argument_register_ids, current_argument_register,
1265                           current_stack_argument);
1266     } else if (compiler_type.IsPointerType()) {
1267       ReadIntegerArgument(value->GetScalar(), *bit_size, false, thread,
1268                           argument_register_ids, current_argument_register,
1269                           current_stack_argument);
1270     }
1271   }
1272
1273   return true;
1274 }
1275
1276 Status ABIWindows_x86_64::SetReturnValueObject(lldb::StackFrameSP &frame_sp,
1277                                             lldb::ValueObjectSP &new_value_sp) {
1278   Status error;
1279   if (!new_value_sp) {
1280     error.SetErrorString("Empty value object for return value.");
1281     return error;
1282   }
1283
1284   CompilerType compiler_type = new_value_sp->GetCompilerType();
1285   if (!compiler_type) {
1286     error.SetErrorString("Null clang type for return value.");
1287     return error;
1288   }
1289
1290   Thread *thread = frame_sp->GetThread().get();
1291
1292   bool is_signed;
1293   uint32_t count;
1294   bool is_complex;
1295
1296   RegisterContext *reg_ctx = thread->GetRegisterContext().get();
1297
1298   bool set_it_simple = false;
1299   if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||
1300       compiler_type.IsPointerType()) {
1301     const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("rax", 0);
1302
1303     DataExtractor data;
1304     Status data_error;
1305     size_t num_bytes = new_value_sp->GetData(data, data_error);
1306     if (data_error.Fail()) {
1307       error.SetErrorStringWithFormat(
1308           "Couldn't convert return value to raw data: %s",
1309           data_error.AsCString());
1310       return error;
1311     }
1312     lldb::offset_t offset = 0;
1313     if (num_bytes <= 8) {
1314       uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
1315
1316       if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value))
1317         set_it_simple = true;
1318     } else {
1319       error.SetErrorString("We don't support returning longer than 64 bit "
1320                            "integer values at present.");
1321     }
1322   } else if (compiler_type.IsFloatingPointType(count, is_complex)) {
1323     if (is_complex)
1324       error.SetErrorString(
1325           "We don't support returning complex values at present");
1326     else {
1327       llvm::Optional<uint64_t> bit_width =
1328           compiler_type.GetBitSize(frame_sp.get());
1329       if (!bit_width) {
1330         error.SetErrorString("can't get type size");
1331         return error;
1332       }
1333       if (*bit_width <= 64) {
1334         const RegisterInfo *xmm0_info =
1335             reg_ctx->GetRegisterInfoByName("xmm0", 0);
1336         RegisterValue xmm0_value;
1337         DataExtractor data;
1338         Status data_error;
1339         size_t num_bytes = new_value_sp->GetData(data, data_error);
1340         if (data_error.Fail()) {
1341           error.SetErrorStringWithFormat(
1342               "Couldn't convert return value to raw data: %s",
1343               data_error.AsCString());
1344           return error;
1345         }
1346
1347         unsigned char buffer[16];
1348         ByteOrder byte_order = data.GetByteOrder();
1349
1350         data.CopyByteOrderedData(0, num_bytes, buffer, 16, byte_order);
1351         xmm0_value.SetBytes(buffer, 16, byte_order);
1352         reg_ctx->WriteRegister(xmm0_info, xmm0_value);
1353         set_it_simple = true;
1354       } else {
1355         // Windows doesn't support 80 bit FP
1356         error.SetErrorString(
1357             "Windows-x86_64 doesn't allow FP larger than 64 bits.");
1358       }
1359     }
1360   }
1361
1362   if (!set_it_simple) {
1363     // Okay we've got a structure or something that doesn't fit in a simple
1364     // register.
1365     // TODO(wanyi): On Windows, if the return type is a struct:
1366     // 1) smaller that 64 bits and return by value -> RAX
1367     // 2) bigger than 64 bits, the caller will allocate memory for that struct
1368     // and pass the struct pointer in RCX then return the pointer in RAX
1369     error.SetErrorString("We only support setting simple integer and float "
1370                          "return types at present.");
1371   }
1372
1373   return error;
1374 }
1375
1376 ValueObjectSP ABIWindows_x86_64::GetReturnValueObjectSimple(
1377     Thread &thread, CompilerType &return_compiler_type) const {
1378   ValueObjectSP return_valobj_sp;
1379   Value value;
1380
1381   if (!return_compiler_type)
1382     return return_valobj_sp;
1383
1384   value.SetCompilerType(return_compiler_type);
1385
1386   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
1387   if (!reg_ctx)
1388     return return_valobj_sp;
1389
1390   const uint32_t type_flags = return_compiler_type.GetTypeInfo();
1391   if (type_flags & eTypeIsScalar) {
1392     value.SetValueType(Value::eValueTypeScalar);
1393
1394     bool success = false;
1395     if (type_flags & eTypeIsInteger) {
1396       // Extract the register context so we can read arguments from registers
1397       llvm::Optional<uint64_t> byte_size =
1398           return_compiler_type.GetByteSize(nullptr);
1399       if (!byte_size)
1400         return return_valobj_sp;
1401       uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
1402           reg_ctx->GetRegisterInfoByName("rax", 0), 0);
1403       const bool is_signed = (type_flags & eTypeIsSigned) != 0;
1404       switch (*byte_size) {
1405       default:
1406         break;
1407
1408       case sizeof(uint64_t):
1409         if (is_signed)
1410           value.GetScalar() = (int64_t)(raw_value);
1411         else
1412           value.GetScalar() = (uint64_t)(raw_value);
1413         success = true;
1414         break;
1415
1416       case sizeof(uint32_t):
1417         if (is_signed)
1418           value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
1419         else
1420           value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
1421         success = true;
1422         break;
1423
1424       case sizeof(uint16_t):
1425         if (is_signed)
1426           value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
1427         else
1428           value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
1429         success = true;
1430         break;
1431
1432       case sizeof(uint8_t):
1433         if (is_signed)
1434           value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
1435         else
1436           value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
1437         success = true;
1438         break;
1439       }
1440     } else if (type_flags & eTypeIsFloat) {
1441       if (type_flags & eTypeIsComplex) {
1442         // Don't handle complex yet.
1443       } else {
1444         llvm::Optional<uint64_t> byte_size =
1445             return_compiler_type.GetByteSize(nullptr);
1446         if (byte_size && *byte_size <= sizeof(long double)) {
1447           const RegisterInfo *xmm0_info =
1448               reg_ctx->GetRegisterInfoByName("xmm0", 0);
1449           RegisterValue xmm0_value;
1450           if (reg_ctx->ReadRegister(xmm0_info, xmm0_value)) {
1451             DataExtractor data;
1452             if (xmm0_value.GetData(data)) {
1453               lldb::offset_t offset = 0;
1454               if (*byte_size == sizeof(float)) {
1455                 value.GetScalar() = (float)data.GetFloat(&offset);
1456                 success = true;
1457               } else if (*byte_size == sizeof(double)) {
1458                 // double and long double are the same on windows
1459                 value.GetScalar() = (double)data.GetDouble(&offset);
1460                 success = true;
1461               }
1462             }
1463           }
1464         }
1465       }
1466     }
1467
1468     if (success)
1469       return_valobj_sp = ValueObjectConstResult::Create(
1470           thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
1471   } else if ((type_flags & eTypeIsPointer) ||
1472              (type_flags & eTypeInstanceIsPointer)) {
1473     unsigned rax_id =
1474         reg_ctx->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB];
1475     value.GetScalar() =
1476         (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id,
1477                                                                       0);
1478     value.SetValueType(Value::eValueTypeScalar);
1479     return_valobj_sp = ValueObjectConstResult::Create(
1480         thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
1481   } else if (type_flags & eTypeIsVector) {
1482     llvm::Optional<uint64_t> byte_size =
1483         return_compiler_type.GetByteSize(nullptr);
1484     if (byte_size && *byte_size > 0) {
1485       const RegisterInfo *xmm_reg =
1486           reg_ctx->GetRegisterInfoByName("xmm0", 0);
1487       if (xmm_reg == nullptr)
1488         xmm_reg = reg_ctx->GetRegisterInfoByName("mm0", 0);
1489
1490       if (xmm_reg) {
1491         if (*byte_size <= xmm_reg->byte_size) {
1492           ProcessSP process_sp(thread.GetProcess());
1493           if (process_sp) {
1494             std::unique_ptr<DataBufferHeap> heap_data_up(
1495                 new DataBufferHeap(*byte_size, 0));
1496             const ByteOrder byte_order = process_sp->GetByteOrder();
1497             RegisterValue reg_value;
1498             if (reg_ctx->ReadRegister(xmm_reg, reg_value)) {
1499               Status error;
1500               if (reg_value.GetAsMemoryData(
1501                       xmm_reg, heap_data_up->GetBytes(),
1502                       heap_data_up->GetByteSize(), byte_order, error)) {
1503                 DataExtractor data(DataBufferSP(heap_data_up.release()),
1504                                    byte_order,
1505                                    process_sp->GetTarget()
1506                                        .GetArchitecture()
1507                                        .GetAddressByteSize());
1508                 return_valobj_sp = ValueObjectConstResult::Create(
1509                     &thread, return_compiler_type, ConstString(""), data);
1510               }
1511             }
1512           }
1513         }
1514       }
1515     }
1516   }
1517
1518   return return_valobj_sp;
1519 }
1520
1521 // The compiler will flatten the nested aggregate type into single
1522 // layer and push the value to stack
1523 // This helper function will flatten an aggregate type
1524 // and return true if it can be returned in register(s) by value
1525 // return false if the aggregate is in memory
1526 static bool FlattenAggregateType(
1527     Thread &thread, ExecutionContext &exe_ctx,
1528     CompilerType &return_compiler_type,
1529     uint32_t data_byte_offset,
1530     std::vector<uint32_t> &aggregate_field_offsets,
1531     std::vector<CompilerType> &aggregate_compiler_types) {
1532
1533   const uint32_t num_children = return_compiler_type.GetNumFields();
1534   for (uint32_t idx = 0; idx < num_children; ++idx) {
1535     std::string name;
1536     bool is_signed;
1537     uint32_t count;
1538     bool is_complex;
1539
1540     uint64_t field_bit_offset = 0;
1541     CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex(
1542         idx, name, &field_bit_offset, nullptr, nullptr);
1543     llvm::Optional<uint64_t> field_bit_width =
1544           field_compiler_type.GetBitSize(&thread);
1545
1546     // if we don't know the size of the field (e.g. invalid type), exit
1547     if (!field_bit_width || *field_bit_width == 0) {
1548       return false;
1549     }
1550     // If there are any unaligned fields, this is stored in memory.
1551     if (field_bit_offset % *field_bit_width != 0) {
1552       return false;
1553     }
1554
1555     // add overall offset
1556     uint32_t field_byte_offset = field_bit_offset / 8 + data_byte_offset;
1557
1558     const uint32_t field_type_flags = field_compiler_type.GetTypeInfo();
1559     if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
1560         field_compiler_type.IsPointerType() ||
1561         field_compiler_type.IsFloatingPointType(count, is_complex)) {
1562       aggregate_field_offsets.push_back(field_byte_offset);
1563       aggregate_compiler_types.push_back(field_compiler_type);
1564     } else if (field_type_flags & eTypeHasChildren) {
1565       if (!FlattenAggregateType(thread, exe_ctx, field_compiler_type,
1566                                 field_byte_offset, aggregate_field_offsets,
1567                                 aggregate_compiler_types)) {
1568         return false;
1569       }
1570     }
1571   }
1572   return true;
1573 }
1574
1575 ValueObjectSP ABIWindows_x86_64::GetReturnValueObjectImpl(
1576     Thread &thread, CompilerType &return_compiler_type) const {
1577   ValueObjectSP return_valobj_sp;
1578
1579   if (!return_compiler_type) {
1580     return return_valobj_sp;
1581   }
1582
1583   // try extract value as if it's a simple type
1584   return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type);
1585   if (return_valobj_sp) {
1586     return return_valobj_sp;
1587   }
1588
1589   RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();
1590   if (!reg_ctx_sp) {
1591     return return_valobj_sp;
1592   }
1593
1594   llvm::Optional<uint64_t> bit_width = return_compiler_type.GetBitSize(&thread);
1595   if (!bit_width) {
1596     return return_valobj_sp;
1597   }
1598
1599   // if it's not simple or aggregate type, then we don't know how to handle it
1600   if (!return_compiler_type.IsAggregateType()) {
1601     return return_valobj_sp;
1602   }
1603
1604   ExecutionContext exe_ctx(thread.shared_from_this());
1605   Target *target = exe_ctx.GetTargetPtr();
1606   uint32_t max_register_value_bit_width = 64;
1607
1608   // The scenario here is to have a struct/class which is POD
1609   // if the return struct/class size is larger than 64 bits,
1610   // the caller will allocate memory for it and pass the return addr in RCX
1611   // then return the address in RAX
1612
1613   // if the struct is returned by value in register (RAX)
1614   // its size has to be: 1, 2, 4, 8, 16, 32, or 64 bits (aligned)
1615   // for floating point, the return value will be copied over to RAX
1616   bool is_memory = *bit_width > max_register_value_bit_width ||
1617                    *bit_width & (*bit_width - 1);
1618   std::vector<uint32_t> aggregate_field_offsets;
1619   std::vector<CompilerType> aggregate_compiler_types;
1620   if (!is_memory &&
1621       FlattenAggregateType(thread, exe_ctx, return_compiler_type,
1622                            0, aggregate_field_offsets,
1623                            aggregate_compiler_types)) {
1624     ByteOrder byte_order = target->GetArchitecture().GetByteOrder();
1625     DataBufferSP data_sp(
1626         new DataBufferHeap(max_register_value_bit_width / 8, 0));
1627     DataExtractor return_ext(data_sp, byte_order,
1628         target->GetArchitecture().GetAddressByteSize());
1629
1630     // The only register used to return struct/class by value
1631     const RegisterInfo *rax_info =
1632         reg_ctx_sp->GetRegisterInfoByName("rax", 0);
1633     RegisterValue rax_value;
1634     reg_ctx_sp->ReadRegister(rax_info, rax_value);
1635     DataExtractor rax_data;
1636     rax_value.GetData(rax_data);
1637
1638     uint32_t used_bytes =
1639         0; // Tracks how much of the rax registers we've consumed so far
1640
1641     // in case of the returned type is a subclass of non-abstract-base class
1642     // it will have a padding to skip the base content
1643     if (aggregate_field_offsets.size())
1644       used_bytes = aggregate_field_offsets[0];
1645
1646     const uint32_t num_children = aggregate_compiler_types.size();
1647     for (uint32_t idx = 0; idx < num_children; idx++) {
1648       bool is_signed;
1649       bool is_complex;
1650       uint32_t count;
1651
1652       CompilerType field_compiler_type = aggregate_compiler_types[idx];
1653       uint32_t field_byte_width = (uint32_t) (*field_compiler_type.GetByteSize(&thread));
1654       uint32_t field_byte_offset = aggregate_field_offsets[idx];
1655
1656       // this is unlikely w/o the overall size being greater than 8 bytes
1657       // For now, return a nullptr return value object.
1658       if (used_bytes >= 8 || used_bytes + field_byte_width > 8) {
1659         return return_valobj_sp;
1660       }
1661
1662       DataExtractor *copy_from_extractor = nullptr;
1663       uint32_t copy_from_offset = 0;
1664       if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
1665           field_compiler_type.IsPointerType() ||
1666           field_compiler_type.IsFloatingPointType(count, is_complex)) {
1667         copy_from_extractor = &rax_data;
1668         copy_from_offset = used_bytes;
1669         used_bytes += field_byte_width;
1670       }
1671       // These two tests are just sanity checks.  If I somehow get the type
1672       // calculation wrong above it is better to just return nothing than to
1673       // assert or crash.
1674       if (!copy_from_extractor) {
1675         return return_valobj_sp;
1676       }
1677       if (copy_from_offset + field_byte_width >
1678           copy_from_extractor->GetByteSize()) {
1679         return return_valobj_sp;
1680       }
1681       copy_from_extractor->CopyByteOrderedData(copy_from_offset,
1682           field_byte_width, data_sp->GetBytes() + field_byte_offset,
1683           field_byte_width, byte_order);
1684     }
1685     if (!is_memory) {
1686       // The result is in our data buffer.  Let's make a variable object out
1687       // of it:
1688       return_valobj_sp = ValueObjectConstResult::Create(
1689           &thread, return_compiler_type, ConstString(""), return_ext);
1690     }
1691   }
1692
1693   // The Windows x86_64 ABI specifies that the return address for MEMORY
1694   // objects be placed in rax on exit from the function.
1695
1696   // FIXME: This is just taking a guess, rax may very well no longer hold the
1697   // return storage location.
1698   // If we are going to do this right, when we make a new frame we should
1699   // check to see if it uses a memory return, and if we are at the first
1700   // instruction and if so stash away the return location.  Then we would
1701   // only return the memory return value if we know it is valid.
1702   if (is_memory) {
1703     unsigned rax_id =
1704         reg_ctx_sp->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB];
1705     lldb::addr_t storage_addr =
1706         (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id,
1707                                                                       0);
1708     return_valobj_sp = ValueObjectMemory::Create(
1709         &thread, "", Address(storage_addr, nullptr), return_compiler_type);
1710   }
1711   return return_valobj_sp;
1712 }
1713
1714 // This defines the CFA as rsp+8
1715 // the saved pc is at CFA-8 (i.e. rsp+0)
1716 // The saved rsp is CFA+0
1717
1718 bool ABIWindows_x86_64::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
1719   unwind_plan.Clear();
1720   unwind_plan.SetRegisterKind(eRegisterKindDWARF);
1721
1722   uint32_t sp_reg_num = dwarf_rsp;
1723   uint32_t pc_reg_num = dwarf_rip;
1724
1725   UnwindPlan::RowSP row(new UnwindPlan::Row);
1726   row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 8);
1727   row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, -8, false);
1728   row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
1729   unwind_plan.AppendRow(row);
1730   unwind_plan.SetSourceName("x86_64 at-func-entry default");
1731   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
1732   return true;
1733 }
1734
1735 // Windows-x86_64 doesn't use %rbp
1736 // No available Unwind information for Windows-x86_64 (section .pdata)
1737 // Let's use SysV-x86_64 one for now
1738 bool ABIWindows_x86_64::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
1739   unwind_plan.Clear();
1740   unwind_plan.SetRegisterKind(eRegisterKindDWARF);
1741
1742   uint32_t fp_reg_num = dwarf_rbp;
1743   uint32_t sp_reg_num = dwarf_rsp;
1744   uint32_t pc_reg_num = dwarf_rip;
1745
1746   UnwindPlan::RowSP row(new UnwindPlan::Row);
1747
1748   const int32_t ptr_size = 8;
1749   row->GetCFAValue().SetIsRegisterPlusOffset(dwarf_rbp, 2 * ptr_size);
1750   row->SetOffset(0);
1751
1752   row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, ptr_size * -2, true);
1753   row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * -1, true);
1754   row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
1755
1756   unwind_plan.AppendRow(row);
1757   unwind_plan.SetSourceName("x86_64 default unwind plan");
1758   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
1759   unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
1760
1761   return true;
1762 }
1763
1764 bool ABIWindows_x86_64::RegisterIsVolatile(const RegisterInfo *reg_info) {
1765   return !RegisterIsCalleeSaved(reg_info);
1766 }
1767
1768 bool ABIWindows_x86_64::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {
1769   if (!reg_info)
1770     return false;
1771   assert(reg_info->name != nullptr && "unnamed register?");
1772   std::string Name = std::string(reg_info->name);
1773   bool IsCalleeSaved =
1774       llvm::StringSwitch<bool>(Name)
1775           .Cases("rbx", "ebx", "rbp", "ebp", "rdi", "edi", "rsi", "esi", true)
1776           .Cases("rsp", "esp", "r12", "r13", "r14", "r15", "sp", "fp", true)
1777           .Cases("xmm6", "xmm7", "xmm8", "xmm9", "xmm10", "xmm11", "xmm12",
1778                  "xmm13", "xmm14", "xmm15", true)
1779           .Default(false);
1780   return IsCalleeSaved;
1781 }
1782
1783 void ABIWindows_x86_64::Initialize() {
1784   PluginManager::RegisterPlugin(
1785       GetPluginNameStatic(), "Windows ABI for x86_64 targets", CreateInstance);
1786 }
1787
1788 void ABIWindows_x86_64::Terminate() {
1789   PluginManager::UnregisterPlugin(CreateInstance);
1790 }
1791
1792 lldb_private::ConstString ABIWindows_x86_64::GetPluginNameStatic() {
1793   static ConstString g_name("windows-x86_64");
1794   return g_name;
1795 }
1796
1797 //------------------------------------------------------------------
1798 // PluginInterface protocol
1799 //------------------------------------------------------------------
1800
1801 lldb_private::ConstString ABIWindows_x86_64::GetPluginName() {
1802   return GetPluginNameStatic();
1803 }
1804
1805 uint32_t ABIWindows_x86_64::GetPluginVersion() { return 1; }