Mercurial > pub > dyncall > dyncall
annotate test/callf/main.c @ 393:60f851e35ef7
- todo cleanup
author | Tassilo Philipp |
---|---|
date | Fri, 22 Jan 2021 15:51:16 +0100 |
parents | 11483966a3ee |
children | 7608e34098b0 |
rev | line source |
---|---|
0 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: test | |
5 File: test/callf/main.c | |
379 | 6 Description: |
0 | 7 License: |
8 | |
387
11483966a3ee
- excluding syscall stuff from callf test for beos/haiku
Tassilo Philipp
parents:
379
diff
changeset
|
9 Copyright (c) 2007-2021 Daniel Adler <dadler@uni-goettingen.de>, |
0 | 10 Tassilo Philipp <tphilipp@potion-studios.com> |
11 | |
12 Permission to use, copy, modify, and distribute this software for any | |
13 purpose with or without fee is hereby granted, provided that the above | |
14 copyright notice and this permission notice appear in all copies. | |
15 | |
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
23 | |
24 */ | |
25 | |
26 | |
27 | |
28 /* test dcCallF API */ | |
29 | |
30 #include "../../dyncall/dyncall_callf.h" | |
31 #include "../common/platformInit.h" | |
32 #include "../common/platformInit.c" /* Impl. for functions only used in this translation unit */ | |
33 | |
358
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
34 #include <stdarg.h> |
387
11483966a3ee
- excluding syscall stuff from callf test for beos/haiku
Tassilo Philipp
parents:
379
diff
changeset
|
35 #if defined(DC_UNIX) && !defined(DC__OS_BeOS) |
379 | 36 #include <sys/syscall.h> |
362
78dfa2f9783a
- added helper function dcGetModeFromCCSigChar() mapping callconv sig chars to respective mode
Tassilo Philipp
parents:
358
diff
changeset
|
37 #endif |
358
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
38 |
0 | 39 |
40 /* sample void function */ | |
41 | |
42 int vf_iii(int x,int y,int z) | |
43 { | |
44 int r = (x == 1 && y == 2 && z == 3); | |
45 printf("%d %d %d: %d", x, y, z, r); | |
46 return r; | |
47 } | |
48 | |
49 int vf_ffiffiffi(float a, float b, int c, float d, float e, int f, float g, float h, int i) | |
50 { | |
51 int r = (a == 1.f && b == 2.f && c == 3 && d == 4.f && e == 5.f && f == 6 && g == 7.f && h == 8.f && i == 9); | |
52 printf("%f %f %d %f %f %d %f %f %d: %d", a, b, c, d, e, f, g, h, i, r); | |
53 return r; | |
54 } | |
55 | |
358
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
56 int vf_ffiV(float a, float b, int c, ...) |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
57 { |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
58 va_list ap; |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
59 double d, e, g, h; |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
60 int f, i; |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
61 int r; |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
62 |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
63 va_start(ap, c); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
64 d = va_arg(ap, double); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
65 e = va_arg(ap, double); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
66 f = va_arg(ap, int); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
67 g = va_arg(ap, double); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
68 h = va_arg(ap, double); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
69 i = va_arg(ap, int); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
70 va_end(ap); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
71 |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
72 r = (a == 1.f && b == 2.f && c == 3 && d == 4. && e == 5. && f == 6 && g == 7. && h == 8. && i == 9); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
73 printf("%f %f %d %f %f %d %f %f %d: %d", a, b, c, d, e, f, g, h, i, r); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
74 return r; |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
75 } |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
76 |
0 | 77 /* main */ |
78 | |
79 int main(int argc, char* argv[]) | |
80 { | |
81 DCCallVM* vm; | |
82 DCValue ret; | |
83 int r = 1; | |
84 | |
85 dcTest_initPlatform(); | |
86 | |
87 /* allocate call vm */ | |
88 vm = dcNewCallVM(4096); | |
89 | |
90 | |
91 /* calls using 'formatted' API */ | |
92 dcReset(vm); | |
93 printf("callf iii)i: "); | |
94 dcCallF(vm, &ret, (void*)&vf_iii, "iii)i", 1, 2, 3); | |
95 r = ret.i && r; | |
96 | |
97 dcReset(vm); | |
98 printf("\ncallf ffiffiffi)i: "); | |
99 dcCallF(vm, &ret, (void*)&vf_ffiffiffi, "ffiffiffi)i", 1.f, 2.f, 3, 4.f, 5.f, 6, 7.f, 8.f, 9); | |
100 r = ret.i && r; | |
101 | |
358
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
102 /* same but with calling convention prefix */ |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
103 dcReset(vm); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
104 printf("\ncallf _:ffiffiffi)i: "); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
105 dcCallF(vm, &ret, (void*)&vf_ffiffiffi, "_:ffiffiffi)i", 1.f, 2.f, 3, 4.f, 5.f, 6, 7.f, 8.f, 9); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
106 r = ret.i && r; |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
107 |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
108 /* vararg call */ |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
109 dcReset(vm); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
110 printf("\ncallf _effi_.ddiddi)i: "); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
111 dcCallF(vm, &ret, (void*)&vf_ffiV, "_effi_.ddiddi)i", 1.f, 2.f, 3, 4., 5., 6, 7., 8., 9); |
30aae7371373
- extended signature with calling convention mode switches for fastcall (gnu), default, cdecl, stdcall, arm (arm), arm (thumb), syscall
Tassilo Philipp
parents:
281
diff
changeset
|
112 r = ret.i && r; |
0 | 113 |
114 /* arg binding then call using 'formatted' API */ | |
115 dcReset(vm); | |
371
451299d50c1a
- windows arm64 support (dyncall, dyncallback, cmake support for armasm64), thanks Bernhard Urban-Forster!
Tassilo Philipp
parents:
363
diff
changeset
|
116 /* reset calling convention too */ |
451299d50c1a
- windows arm64 support (dyncall, dyncallback, cmake support for armasm64), thanks Bernhard Urban-Forster!
Tassilo Philipp
parents:
363
diff
changeset
|
117 dcMode(vm, DC_CALL_C_DEFAULT); |
0 | 118 printf("\nargf iii)i then call: "); |
119 dcArgF(vm, "iii)i", 1, 2, 3); | |
120 r = r && dcCallInt(vm, (void*)&vf_iii); | |
121 | |
122 dcReset(vm); | |
123 printf("\nargf iii then call: "); | |
124 dcArgF(vm, "iii", 1, 2, 3); | |
125 r = r && dcCallInt(vm, (void*)&vf_iii); | |
126 | |
127 dcReset(vm); | |
128 printf("\nargf ffiffiffi)i then call: "); | |
129 dcArgF(vm, "ffiffiffi)i", 1.f, 2.f, 3, 4.f, 5.f, 6, 7.f, 8.f, 9); | |
130 r = r && dcCallInt(vm, (void*)&vf_ffiffiffi); | |
131 | |
132 dcReset(vm); | |
133 printf("\nargf ffiffiffi then call: "); | |
134 dcArgF(vm, "ffiffiffi", 1.f, 2.f, 3, 4.f, 5.f, 6, 7.f, 8.f, 9); | |
135 r = r && dcCallInt(vm, (void*)&vf_ffiffiffi); | |
136 | |
387
11483966a3ee
- excluding syscall stuff from callf test for beos/haiku
Tassilo Philipp
parents:
379
diff
changeset
|
137 #if defined(DC_UNIX) && !defined(DC__OS_MacOSX) && !defined(DC__OS_SunOS) && !defined(DC__OS_BeOS) |
362
78dfa2f9783a
- added helper function dcGetModeFromCCSigChar() mapping callconv sig chars to respective mode
Tassilo Philipp
parents:
358
diff
changeset
|
138 /* testing syscall using calling convention prefix - not available on all platforms */ |
78dfa2f9783a
- added helper function dcGetModeFromCCSigChar() mapping callconv sig chars to respective mode
Tassilo Philipp
parents:
358
diff
changeset
|
139 dcReset(vm); |
363 | 140 printf("\ncallf _$iZi)i"); |
362
78dfa2f9783a
- added helper function dcGetModeFromCCSigChar() mapping callconv sig chars to respective mode
Tassilo Philipp
parents:
358
diff
changeset
|
141 fflush(NULL); /* needed before syscall write as it's immediate, or order might be incorrect */ |
363 | 142 dcCallF(vm, &ret, (DCpointer)(ptrdiff_t)SYS_write, "_$iZi)i", 1/*stdout*/, " = syscall: 1", 13); |
362
78dfa2f9783a
- added helper function dcGetModeFromCCSigChar() mapping callconv sig chars to respective mode
Tassilo Philipp
parents:
358
diff
changeset
|
143 r = ret.i == 13 && r; |
78dfa2f9783a
- added helper function dcGetModeFromCCSigChar() mapping callconv sig chars to respective mode
Tassilo Philipp
parents:
358
diff
changeset
|
144 #endif |
0 | 145 |
146 /* free vm */ | |
147 dcFree(vm); | |
148 | |
149 printf("\nresult: callf: %d\n", r); | |
150 | |
151 dcTest_deInitPlatform(); | |
379 | 152 |
0 | 153 return 0; |
154 } | |
155 |