blob: 12ea5b78755d8edfaf4f986ab0e1df966fa19fb4 [file] [log] [blame]
Austin Schuhdace2a62020-08-18 10:56:48 -07001/* x86 calling conventions checking. */
2
3/*
4Copyright 2000, 2001, 2010 Free Software Foundation, Inc.
5
6This file is part of the GNU MP Library test suite.
7
8The GNU MP Library test suite is free software; you can redistribute it
9and/or modify it under the terms of the GNU General Public License as
10published by the Free Software Foundation; either version 3 of the License,
11or (at your option) any later version.
12
13The GNU MP Library test suite is distributed in the hope that it will be
14useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16Public License for more details.
17
18You should have received a copy of the GNU General Public License along with
19the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
20
21#include <stdio.h>
22#include "gmp-impl.h"
23#include "tests.h"
24
25
26/* Vector if constants and register values. We use one vector to allow access
27 via a base pointer, very beneficial for the PIC-enabled amd64call.asm. */
28mp_limb_t calling_conventions_values[17] =
29{
30 CNST_LIMB(0x12345678), /* want_ebx */
31 CNST_LIMB(0x89ABCDEF), /* want_ebp */
32 CNST_LIMB(0xDEADBEEF), /* want_esi */
33 CNST_LIMB(0xFFEEDDCC), /* want_edi */
34
35 CNST_LIMB(0xFEEDABBA), /* JUNK_EAX */
36 CNST_LIMB(0xAB78DE89), /* JUNK_ECX */
37 CNST_LIMB(0x12389018) /* JUNK_EDX */
38
39 /* rest of array used for dynamic values. */
40};
41
42/* Index starts for various regions in above vector. */
43#define WANT 0
44#define JUNK 4
45#define SAVE 7
46#define RETADDR 11
47#define VAL 12
48#define EFLAGS 16
49
50
51/* values to check */
52#ifdef __cplusplus
53extern "C" {
54#endif
55struct {
56 unsigned control;
57 unsigned status;
58 unsigned tag;
59 unsigned other[4];
60} calling_conventions_fenv;
61#ifdef __cplusplus
62}
63#endif
64
65/* expected values, as per x86call.asm */
66#define VALUE_EBX 0x01234567
67#define VALUE_ESI 0x89ABCDEF
68#define VALUE_EDI 0xFEDCBA98
69#define VALUE_EBP 0x76543210
70
71
72const char *regname[] = {"ebx", "ebp", "esi", "edi"};
73
74#define DIR_BIT(eflags) (((eflags) & (1<<10)) != 0)
75
76
77/* Return 1 if ok, 0 if not */
78
79int
80calling_conventions_check (void)
81{
82 const char *header = "Violated calling conventions:\n";
83 int ret = 1;
84 int i;
85
86#define CHECK(callreg, regstr, value) \
87 if (callreg != value) \
88 { \
89 printf ("%s %s got 0x%08X want 0x%08X\n", \
90 header, regstr, callreg, value); \
91 header = ""; \
92 ret = 0; \
93 }
94
95 for (i = 0; i < 4; i++)
96 {
97 CHECK (calling_conventions_values[VAL+i], regname[i], calling_conventions_values[WANT+i]);
98 }
99
100 if (DIR_BIT (calling_conventions_values[EFLAGS]) != 0)
101 {
102 printf ("%s eflags dir bit got %d want 0\n",
103 header, DIR_BIT (calling_conventions_values[EFLAGS]));
104 header = "";
105 ret = 0;
106 }
107
108 if ((calling_conventions_fenv.tag & 0xFFFF) != 0xFFFF)
109 {
110 printf ("%s fpu tags got 0x%X want 0xFFFF\n",
111 header, calling_conventions_fenv.tag & 0xFFFF);
112 header = "";
113 ret = 0;
114 }
115
116 return ret;
117}