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
33using namespace lldb;
34using namespace lldb_private;
35
36enum 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
100static 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
1056static const uint32_t k_num_register_infos =
1057    llvm::array_lengthof(g_register_infos);
1058static bool g_register_info_names_constified = false;
1059
1060const lldb_private::RegisterInfo *
1061ABIWindows_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
1080bool ABIWindows_x86_64::GetPointerReturnRegister(const char *&name) {
1081  name = "rax";
1082  return true;
1083}
1084
1085size_t ABIWindows_x86_64::GetRedZoneSize() const { return 0; }
1086
1087//------------------------------------------------------------------
1088// Static Functions
1089//------------------------------------------------------------------
1090
1091ABISP
1092ABIWindows_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(
1096        new ABIWindows_x86_64(std::move(process_sp), MakeMCRegisterInfo(arch)));
1097  }
1098  return ABISP();
1099}
1100
1101bool ABIWindows_x86_64::PrepareTrivialCall(Thread &thread, addr_t sp,
1102                                           addr_t func_addr, addr_t return_addr,
1103                                           llvm::ArrayRef<addr_t> args) const {
1104  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1105
1106  if (log) {
1107    StreamString s;
1108    s.Printf("ABIWindows_x86_64::PrepareTrivialCall (tid = 0x%" PRIx64
1109             ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
1110             ", return_addr = 0x%" PRIx64,
1111             thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
1112             (uint64_t)return_addr);
1113
1114    for (size_t i = 0; i < args.size(); ++i)
1115      s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),
1116               args[i]);
1117    s.PutCString(")");
1118    log->PutString(s.GetString());
1119  }
1120
1121  RegisterContext *reg_ctx = thread.GetRegisterContext().get();
1122  if (!reg_ctx)
1123    return false;
1124
1125  const RegisterInfo *reg_info = nullptr;
1126
1127  if (args.size() > 4) // Windows x64 only put first 4 arguments into registers
1128    return false;
1129
1130  for (size_t i = 0; i < args.size(); ++i) {
1131    reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
1132                                        LLDB_REGNUM_GENERIC_ARG1 + i);
1133    LLDB_LOGF(log, "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  LLDB_LOGF(log, "16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64,
1142            (uint64_t)sp, (uint64_t)(sp & ~0xfull));
1143
1144  sp &= ~(0xfull); // 16-byte alignment
1145
1146  sp -= 8; // return address
1147
1148  Status error;
1149  const RegisterInfo *pc_reg_info =
1150      reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
1151  const RegisterInfo *sp_reg_info =
1152      reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
1153  ProcessSP process_sp(thread.GetProcess());
1154
1155  RegisterValue reg_value;
1156  LLDB_LOGF(log,
1157            "Pushing the return address onto the stack: 0x%" PRIx64
1158            ": 0x%" PRIx64,
1159            (uint64_t)sp, (uint64_t)return_addr);
1160
1161  // Save return address onto the stack
1162  if (!process_sp->WritePointerToMemory(sp, return_addr, error))
1163    return false;
1164
1165  // %rsp is set to the actual stack value.
1166
1167  LLDB_LOGF(log, "Writing SP: 0x%" PRIx64, (uint64_t)sp);
1168
1169  if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp))
1170    return false;
1171
1172  // %rip is set to the address of the called function.
1173
1174  LLDB_LOGF(log, "Writing IP: 0x%" PRIx64, (uint64_t)func_addr);
1175
1176  if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr))
1177    return false;
1178
1179  return true;
1180}
1181
1182static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,
1183                                bool is_signed, Thread &thread,
1184                                uint32_t *argument_register_ids,
1185                                unsigned int &current_argument_register,
1186                                addr_t &current_stack_argument) {
1187  if (bit_width > 64)
1188    return false; // Scalar can't hold large integer arguments
1189
1190  if (current_argument_register < 4) { // Windows pass first 4 arguments to register
1191    scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
1192        argument_register_ids[current_argument_register], 0);
1193    current_argument_register++;
1194    if (is_signed)
1195      scalar.SignExtend(bit_width);
1196  	return true;
1197  }
1198  uint32_t byte_size = (bit_width + (CHAR_BIT - 1)) / CHAR_BIT;
1199  Status error;
1200  if (thread.GetProcess()->ReadScalarIntegerFromMemory(
1201          current_stack_argument, byte_size, is_signed, scalar, error)) {
1202    current_stack_argument += byte_size;
1203    return true;
1204  }
1205  return false;
1206}
1207
1208bool ABIWindows_x86_64::GetArgumentValues(Thread &thread,
1209                                       ValueList &values) const {
1210  unsigned int num_values = values.GetSize();
1211  unsigned int value_index;
1212
1213  // Extract the register context so we can read arguments from registers
1214
1215  RegisterContext *reg_ctx = thread.GetRegisterContext().get();
1216
1217  if (!reg_ctx)
1218    return false;
1219
1220  // Get the pointer to the first stack argument so we have a place to start
1221  // when reading data
1222
1223  addr_t sp = reg_ctx->GetSP(0);
1224
1225  if (!sp)
1226    return false;
1227
1228  addr_t current_stack_argument = sp + 8; // jump over return address
1229
1230  uint32_t argument_register_ids[4];
1231
1232  argument_register_ids[0] =
1233      reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1)
1234          ->kinds[eRegisterKindLLDB];
1235  argument_register_ids[1] =
1236      reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2)
1237          ->kinds[eRegisterKindLLDB];
1238  argument_register_ids[2] =
1239      reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3)
1240          ->kinds[eRegisterKindLLDB];
1241  argument_register_ids[3] =
1242      reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4)
1243          ->kinds[eRegisterKindLLDB];
1244
1245  unsigned int current_argument_register = 0;
1246
1247  for (value_index = 0; value_index < num_values; ++value_index) {
1248    Value *value = values.GetValueAtIndex(value_index);
1249
1250    if (!value)
1251      return false;
1252
1253    CompilerType compiler_type = value->GetCompilerType();
1254    llvm::Optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread);
1255    if (!bit_size)
1256      return false;
1257    bool is_signed;
1258
1259    if (compiler_type.IsIntegerOrEnumerationType(is_signed)) {
1260      ReadIntegerArgument(value->GetScalar(), *bit_size, is_signed, thread,
1261                          argument_register_ids, current_argument_register,
1262                          current_stack_argument);
1263    } else if (compiler_type.IsPointerType()) {
1264      ReadIntegerArgument(value->GetScalar(), *bit_size, false, thread,
1265                          argument_register_ids, current_argument_register,
1266                          current_stack_argument);
1267    }
1268  }
1269
1270  return true;
1271}
1272
1273Status ABIWindows_x86_64::SetReturnValueObject(lldb::StackFrameSP &frame_sp,
1274                                            lldb::ValueObjectSP &new_value_sp) {
1275  Status error;
1276  if (!new_value_sp) {
1277    error.SetErrorString("Empty value object for return value.");
1278    return error;
1279  }
1280
1281  CompilerType compiler_type = new_value_sp->GetCompilerType();
1282  if (!compiler_type) {
1283    error.SetErrorString("Null clang type for return value.");
1284    return error;
1285  }
1286
1287  Thread *thread = frame_sp->GetThread().get();
1288
1289  bool is_signed;
1290  uint32_t count;
1291  bool is_complex;
1292
1293  RegisterContext *reg_ctx = thread->GetRegisterContext().get();
1294
1295  bool set_it_simple = false;
1296  if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||
1297      compiler_type.IsPointerType()) {
1298    const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("rax", 0);
1299
1300    DataExtractor data;
1301    Status data_error;
1302    size_t num_bytes = new_value_sp->GetData(data, data_error);
1303    if (data_error.Fail()) {
1304      error.SetErrorStringWithFormat(
1305          "Couldn't convert return value to raw data: %s",
1306          data_error.AsCString());
1307      return error;
1308    }
1309    lldb::offset_t offset = 0;
1310    if (num_bytes <= 8) {
1311      uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
1312
1313      if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value))
1314        set_it_simple = true;
1315    } else {
1316      error.SetErrorString("We don't support returning longer than 64 bit "
1317                           "integer values at present.");
1318    }
1319  } else if (compiler_type.IsFloatingPointType(count, is_complex)) {
1320    if (is_complex)
1321      error.SetErrorString(
1322          "We don't support returning complex values at present");
1323    else {
1324      llvm::Optional<uint64_t> bit_width =
1325          compiler_type.GetBitSize(frame_sp.get());
1326      if (!bit_width) {
1327        error.SetErrorString("can't get type size");
1328        return error;
1329      }
1330      if (*bit_width <= 64) {
1331        const RegisterInfo *xmm0_info =
1332            reg_ctx->GetRegisterInfoByName("xmm0", 0);
1333        RegisterValue xmm0_value;
1334        DataExtractor data;
1335        Status data_error;
1336        size_t num_bytes = new_value_sp->GetData(data, data_error);
1337        if (data_error.Fail()) {
1338          error.SetErrorStringWithFormat(
1339              "Couldn't convert return value to raw data: %s",
1340              data_error.AsCString());
1341          return error;
1342        }
1343
1344        unsigned char buffer[16];
1345        ByteOrder byte_order = data.GetByteOrder();
1346
1347        data.CopyByteOrderedData(0, num_bytes, buffer, 16, byte_order);
1348        xmm0_value.SetBytes(buffer, 16, byte_order);
1349        reg_ctx->WriteRegister(xmm0_info, xmm0_value);
1350        set_it_simple = true;
1351      } else {
1352        // Windows doesn't support 80 bit FP
1353        error.SetErrorString(
1354            "Windows-x86_64 doesn't allow FP larger than 64 bits.");
1355      }
1356    }
1357  }
1358
1359  if (!set_it_simple) {
1360    // Okay we've got a structure or something that doesn't fit in a simple
1361    // register.
1362    // TODO(wanyi): On Windows, if the return type is a struct:
1363    // 1) smaller that 64 bits and return by value -> RAX
1364    // 2) bigger than 64 bits, the caller will allocate memory for that struct
1365    // and pass the struct pointer in RCX then return the pointer in RAX
1366    error.SetErrorString("We only support setting simple integer and float "
1367                         "return types at present.");
1368  }
1369
1370  return error;
1371}
1372
1373ValueObjectSP ABIWindows_x86_64::GetReturnValueObjectSimple(
1374    Thread &thread, CompilerType &return_compiler_type) const {
1375  ValueObjectSP return_valobj_sp;
1376  Value value;
1377
1378  if (!return_compiler_type)
1379    return return_valobj_sp;
1380
1381  value.SetCompilerType(return_compiler_type);
1382
1383  RegisterContext *reg_ctx = thread.GetRegisterContext().get();
1384  if (!reg_ctx)
1385    return return_valobj_sp;
1386
1387  const uint32_t type_flags = return_compiler_type.GetTypeInfo();
1388  if (type_flags & eTypeIsScalar) {
1389    value.SetValueType(Value::eValueTypeScalar);
1390
1391    bool success = false;
1392    if (type_flags & eTypeIsInteger) {
1393      // Extract the register context so we can read arguments from registers
1394      llvm::Optional<uint64_t> byte_size =
1395          return_compiler_type.GetByteSize(nullptr);
1396      if (!byte_size)
1397        return return_valobj_sp;
1398      uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
1399          reg_ctx->GetRegisterInfoByName("rax", 0), 0);
1400      const bool is_signed = (type_flags & eTypeIsSigned) != 0;
1401      switch (*byte_size) {
1402      default:
1403        break;
1404
1405      case sizeof(uint64_t):
1406        if (is_signed)
1407          value.GetScalar() = (int64_t)(raw_value);
1408        else
1409          value.GetScalar() = (uint64_t)(raw_value);
1410        success = true;
1411        break;
1412
1413      case sizeof(uint32_t):
1414        if (is_signed)
1415          value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
1416        else
1417          value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
1418        success = true;
1419        break;
1420
1421      case sizeof(uint16_t):
1422        if (is_signed)
1423          value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
1424        else
1425          value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
1426        success = true;
1427        break;
1428
1429      case sizeof(uint8_t):
1430        if (is_signed)
1431          value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
1432        else
1433          value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
1434        success = true;
1435        break;
1436      }
1437    } else if (type_flags & eTypeIsFloat) {
1438      if (type_flags & eTypeIsComplex) {
1439        // Don't handle complex yet.
1440      } else {
1441        llvm::Optional<uint64_t> byte_size =
1442            return_compiler_type.GetByteSize(nullptr);
1443        if (byte_size && *byte_size <= sizeof(long double)) {
1444          const RegisterInfo *xmm0_info =
1445              reg_ctx->GetRegisterInfoByName("xmm0", 0);
1446          RegisterValue xmm0_value;
1447          if (reg_ctx->ReadRegister(xmm0_info, xmm0_value)) {
1448            DataExtractor data;
1449            if (xmm0_value.GetData(data)) {
1450              lldb::offset_t offset = 0;
1451              if (*byte_size == sizeof(float)) {
1452                value.GetScalar() = (float)data.GetFloat(&offset);
1453                success = true;
1454              } else if (*byte_size == sizeof(double)) {
1455                // double and long double are the same on windows
1456                value.GetScalar() = (double)data.GetDouble(&offset);
1457                success = true;
1458              }
1459            }
1460          }
1461        }
1462      }
1463    }
1464
1465    if (success)
1466      return_valobj_sp = ValueObjectConstResult::Create(
1467          thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
1468  } else if ((type_flags & eTypeIsPointer) ||
1469             (type_flags & eTypeInstanceIsPointer)) {
1470    unsigned rax_id =
1471        reg_ctx->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB];
1472    value.GetScalar() =
1473        (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id,
1474                                                                      0);
1475    value.SetValueType(Value::eValueTypeScalar);
1476    return_valobj_sp = ValueObjectConstResult::Create(
1477        thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
1478  } else if (type_flags & eTypeIsVector) {
1479    llvm::Optional<uint64_t> byte_size =
1480        return_compiler_type.GetByteSize(nullptr);
1481    if (byte_size && *byte_size > 0) {
1482      const RegisterInfo *xmm_reg =
1483          reg_ctx->GetRegisterInfoByName("xmm0", 0);
1484      if (xmm_reg == nullptr)
1485        xmm_reg = reg_ctx->GetRegisterInfoByName("mm0", 0);
1486
1487      if (xmm_reg) {
1488        if (*byte_size <= xmm_reg->byte_size) {
1489          ProcessSP process_sp(thread.GetProcess());
1490          if (process_sp) {
1491            std::unique_ptr<DataBufferHeap> heap_data_up(
1492                new DataBufferHeap(*byte_size, 0));
1493            const ByteOrder byte_order = process_sp->GetByteOrder();
1494            RegisterValue reg_value;
1495            if (reg_ctx->ReadRegister(xmm_reg, reg_value)) {
1496              Status error;
1497              if (reg_value.GetAsMemoryData(
1498                      xmm_reg, heap_data_up->GetBytes(),
1499                      heap_data_up->GetByteSize(), byte_order, error)) {
1500                DataExtractor data(DataBufferSP(heap_data_up.release()),
1501                                   byte_order,
1502                                   process_sp->GetTarget()
1503                                       .GetArchitecture()
1504                                       .GetAddressByteSize());
1505                return_valobj_sp = ValueObjectConstResult::Create(
1506                    &thread, return_compiler_type, ConstString(""), data);
1507              }
1508            }
1509          }
1510        }
1511      }
1512    }
1513  }
1514
1515  return return_valobj_sp;
1516}
1517
1518// The compiler will flatten the nested aggregate type into single
1519// layer and push the value to stack
1520// This helper function will flatten an aggregate type
1521// and return true if it can be returned in register(s) by value
1522// return false if the aggregate is in memory
1523static bool FlattenAggregateType(
1524    Thread &thread, ExecutionContext &exe_ctx,
1525    CompilerType &return_compiler_type,
1526    uint32_t data_byte_offset,
1527    std::vector<uint32_t> &aggregate_field_offsets,
1528    std::vector<CompilerType> &aggregate_compiler_types) {
1529
1530  const uint32_t num_children = return_compiler_type.GetNumFields();
1531  for (uint32_t idx = 0; idx < num_children; ++idx) {
1532    std::string name;
1533    bool is_signed;
1534    uint32_t count;
1535    bool is_complex;
1536
1537    uint64_t field_bit_offset = 0;
1538    CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex(
1539        idx, name, &field_bit_offset, nullptr, nullptr);
1540    llvm::Optional<uint64_t> field_bit_width =
1541          field_compiler_type.GetBitSize(&thread);
1542
1543    // if we don't know the size of the field (e.g. invalid type), exit
1544    if (!field_bit_width || *field_bit_width == 0) {
1545      return false;
1546    }
1547    // If there are any unaligned fields, this is stored in memory.
1548    if (field_bit_offset % *field_bit_width != 0) {
1549      return false;
1550    }
1551
1552    // add overall offset
1553    uint32_t field_byte_offset = field_bit_offset / 8 + data_byte_offset;
1554
1555    const uint32_t field_type_flags = field_compiler_type.GetTypeInfo();
1556    if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
1557        field_compiler_type.IsPointerType() ||
1558        field_compiler_type.IsFloatingPointType(count, is_complex)) {
1559      aggregate_field_offsets.push_back(field_byte_offset);
1560      aggregate_compiler_types.push_back(field_compiler_type);
1561    } else if (field_type_flags & eTypeHasChildren) {
1562      if (!FlattenAggregateType(thread, exe_ctx, field_compiler_type,
1563                                field_byte_offset, aggregate_field_offsets,
1564                                aggregate_compiler_types)) {
1565        return false;
1566      }
1567    }
1568  }
1569  return true;
1570}
1571
1572ValueObjectSP ABIWindows_x86_64::GetReturnValueObjectImpl(
1573    Thread &thread, CompilerType &return_compiler_type) const {
1574  ValueObjectSP return_valobj_sp;
1575
1576  if (!return_compiler_type) {
1577    return return_valobj_sp;
1578  }
1579
1580  // try extract value as if it's a simple type
1581  return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type);
1582  if (return_valobj_sp) {
1583    return return_valobj_sp;
1584  }
1585
1586  RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();
1587  if (!reg_ctx_sp) {
1588    return return_valobj_sp;
1589  }
1590
1591  llvm::Optional<uint64_t> bit_width = return_compiler_type.GetBitSize(&thread);
1592  if (!bit_width) {
1593    return return_valobj_sp;
1594  }
1595
1596  // if it's not simple or aggregate type, then we don't know how to handle it
1597  if (!return_compiler_type.IsAggregateType()) {
1598    return return_valobj_sp;
1599  }
1600
1601  ExecutionContext exe_ctx(thread.shared_from_this());
1602  Target *target = exe_ctx.GetTargetPtr();
1603  uint32_t max_register_value_bit_width = 64;
1604
1605  // The scenario here is to have a struct/class which is POD
1606  // if the return struct/class size is larger than 64 bits,
1607  // the caller will allocate memory for it and pass the return addr in RCX
1608  // then return the address in RAX
1609
1610  // if the struct is returned by value in register (RAX)
1611  // its size has to be: 1, 2, 4, 8, 16, 32, or 64 bits (aligned)
1612  // for floating point, the return value will be copied over to RAX
1613  bool is_memory = *bit_width > max_register_value_bit_width ||
1614                   *bit_width & (*bit_width - 1);
1615  std::vector<uint32_t> aggregate_field_offsets;
1616  std::vector<CompilerType> aggregate_compiler_types;
1617  if (!is_memory &&
1618      FlattenAggregateType(thread, exe_ctx, return_compiler_type,
1619                           0, aggregate_field_offsets,
1620                           aggregate_compiler_types)) {
1621    ByteOrder byte_order = target->GetArchitecture().GetByteOrder();
1622    DataBufferSP data_sp(
1623        new DataBufferHeap(max_register_value_bit_width / 8, 0));
1624    DataExtractor return_ext(data_sp, byte_order,
1625        target->GetArchitecture().GetAddressByteSize());
1626
1627    // The only register used to return struct/class by value
1628    const RegisterInfo *rax_info =
1629        reg_ctx_sp->GetRegisterInfoByName("rax", 0);
1630    RegisterValue rax_value;
1631    reg_ctx_sp->ReadRegister(rax_info, rax_value);
1632    DataExtractor rax_data;
1633    rax_value.GetData(rax_data);
1634
1635    uint32_t used_bytes =
1636        0; // Tracks how much of the rax registers we've consumed so far
1637
1638    // in case of the returned type is a subclass of non-abstract-base class
1639    // it will have a padding to skip the base content
1640    if (aggregate_field_offsets.size())
1641      used_bytes = aggregate_field_offsets[0];
1642
1643    const uint32_t num_children = aggregate_compiler_types.size();
1644    for (uint32_t idx = 0; idx < num_children; idx++) {
1645      bool is_signed;
1646      bool is_complex;
1647      uint32_t count;
1648
1649      CompilerType field_compiler_type = aggregate_compiler_types[idx];
1650      uint32_t field_byte_width = (uint32_t) (*field_compiler_type.GetByteSize(&thread));
1651      uint32_t field_byte_offset = aggregate_field_offsets[idx];
1652
1653      // this is unlikely w/o the overall size being greater than 8 bytes
1654      // For now, return a nullptr return value object.
1655      if (used_bytes >= 8 || used_bytes + field_byte_width > 8) {
1656        return return_valobj_sp;
1657      }
1658
1659      DataExtractor *copy_from_extractor = nullptr;
1660      uint32_t copy_from_offset = 0;
1661      if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
1662          field_compiler_type.IsPointerType() ||
1663          field_compiler_type.IsFloatingPointType(count, is_complex)) {
1664        copy_from_extractor = &rax_data;
1665        copy_from_offset = used_bytes;
1666        used_bytes += field_byte_width;
1667      }
1668      // These two tests are just sanity checks.  If I somehow get the type
1669      // calculation wrong above it is better to just return nothing than to
1670      // assert or crash.
1671      if (!copy_from_extractor) {
1672        return return_valobj_sp;
1673      }
1674      if (copy_from_offset + field_byte_width >
1675          copy_from_extractor->GetByteSize()) {
1676        return return_valobj_sp;
1677      }
1678      copy_from_extractor->CopyByteOrderedData(copy_from_offset,
1679          field_byte_width, data_sp->GetBytes() + field_byte_offset,
1680          field_byte_width, byte_order);
1681    }
1682    if (!is_memory) {
1683      // The result is in our data buffer.  Let's make a variable object out
1684      // of it:
1685      return_valobj_sp = ValueObjectConstResult::Create(
1686          &thread, return_compiler_type, ConstString(""), return_ext);
1687    }
1688  }
1689
1690  // The Windows x86_64 ABI specifies that the return address for MEMORY
1691  // objects be placed in rax on exit from the function.
1692
1693  // FIXME: This is just taking a guess, rax may very well no longer hold the
1694  // return storage location.
1695  // If we are going to do this right, when we make a new frame we should
1696  // check to see if it uses a memory return, and if we are at the first
1697  // instruction and if so stash away the return location.  Then we would
1698  // only return the memory return value if we know it is valid.
1699  if (is_memory) {
1700    unsigned rax_id =
1701        reg_ctx_sp->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB];
1702    lldb::addr_t storage_addr =
1703        (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id,
1704                                                                      0);
1705    return_valobj_sp = ValueObjectMemory::Create(
1706        &thread, "", Address(storage_addr, nullptr), return_compiler_type);
1707  }
1708  return return_valobj_sp;
1709}
1710
1711// This defines the CFA as rsp+8
1712// the saved pc is at CFA-8 (i.e. rsp+0)
1713// The saved rsp is CFA+0
1714
1715bool ABIWindows_x86_64::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
1716  unwind_plan.Clear();
1717  unwind_plan.SetRegisterKind(eRegisterKindDWARF);
1718
1719  uint32_t sp_reg_num = dwarf_rsp;
1720  uint32_t pc_reg_num = dwarf_rip;
1721
1722  UnwindPlan::RowSP row(new UnwindPlan::Row);
1723  row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 8);
1724  row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, -8, false);
1725  row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
1726  unwind_plan.AppendRow(row);
1727  unwind_plan.SetSourceName("x86_64 at-func-entry default");
1728  unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
1729  return true;
1730}
1731
1732// Windows-x86_64 doesn't use %rbp
1733// No available Unwind information for Windows-x86_64 (section .pdata)
1734// Let's use SysV-x86_64 one for now
1735bool ABIWindows_x86_64::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
1736  unwind_plan.Clear();
1737  unwind_plan.SetRegisterKind(eRegisterKindDWARF);
1738
1739  uint32_t fp_reg_num = dwarf_rbp;
1740  uint32_t sp_reg_num = dwarf_rsp;
1741  uint32_t pc_reg_num = dwarf_rip;
1742
1743  UnwindPlan::RowSP row(new UnwindPlan::Row);
1744
1745  const int32_t ptr_size = 8;
1746  row->GetCFAValue().SetIsRegisterPlusOffset(dwarf_rbp, 2 * ptr_size);
1747  row->SetOffset(0);
1748
1749  row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, ptr_size * -2, true);
1750  row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * -1, true);
1751  row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
1752
1753  unwind_plan.AppendRow(row);
1754  unwind_plan.SetSourceName("x86_64 default unwind plan");
1755  unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
1756  unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
1757
1758  return true;
1759}
1760
1761bool ABIWindows_x86_64::RegisterIsVolatile(const RegisterInfo *reg_info) {
1762  return !RegisterIsCalleeSaved(reg_info);
1763}
1764
1765bool ABIWindows_x86_64::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {
1766  if (!reg_info)
1767    return false;
1768  assert(reg_info->name != nullptr && "unnamed register?");
1769  std::string Name = std::string(reg_info->name);
1770  bool IsCalleeSaved =
1771      llvm::StringSwitch<bool>(Name)
1772          .Cases("rbx", "ebx", "rbp", "ebp", "rdi", "edi", "rsi", "esi", true)
1773          .Cases("rsp", "esp", "r12", "r13", "r14", "r15", "sp", "fp", true)
1774          .Cases("xmm6", "xmm7", "xmm8", "xmm9", "xmm10", "xmm11", "xmm12",
1775                 "xmm13", "xmm14", "xmm15", true)
1776          .Default(false);
1777  return IsCalleeSaved;
1778}
1779
1780void ABIWindows_x86_64::Initialize() {
1781  PluginManager::RegisterPlugin(
1782      GetPluginNameStatic(), "Windows ABI for x86_64 targets", CreateInstance);
1783}
1784
1785void ABIWindows_x86_64::Terminate() {
1786  PluginManager::UnregisterPlugin(CreateInstance);
1787}
1788
1789lldb_private::ConstString ABIWindows_x86_64::GetPluginNameStatic() {
1790  static ConstString g_name("windows-x86_64");
1791  return g_name;
1792}
1793
1794//------------------------------------------------------------------
1795// PluginInterface protocol
1796//------------------------------------------------------------------
1797
1798lldb_private::ConstString ABIWindows_x86_64::GetPluginName() {
1799  return GetPluginNameStatic();
1800}
1801
1802uint32_t ABIWindows_x86_64::GetPluginVersion() { return 1; }
1803