1 /* foreign2.c
2  * Copyright 1984-2017 Cisco Systems, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <wchar.h>
19 
20 #ifdef _WIN32
21 #  define SCHEME_IMPORT
22 #  include "scheme.h"
23 #  undef EXPORT
24 #  define EXPORT extern __declspec (dllexport)
25 #else
26 #include "scheme.h"
27 #endif
28 
testten(int x0,int x1,int x2,int x3,int x4,int x5,int x6,int x7,int x8,int x9)29 EXPORT int testten(int x0,int x1,int x2,int x3,int x4,int x5,int x6,int x7,int x8,int x9) {
30    return  1 * x0 +
31            2 * x1 +
32            3 * x2 +
33            5 * x3 +
34            7 * x4 +
35           11 * x5 +
36           13 * x6 +
37           17 * x7 +
38           19 * x8 +
39           23 * x9;
40 }
41 
flsum8(double x1,double x2,double x3,double x4,double x5,double x6,double x7,double x8)42 EXPORT double flsum8(double x1,double x2,double x3,double x4,double x5,double x6,double x7,double x8) {
43     return (x1+x2+x3+x4+x5+x6+x7+x8);
44 }
45 
sparcfltest(int x1,int x2,int x3,int x4,int x5,double x6,int x7,double x8)46 EXPORT double sparcfltest(int x1,int x2,int x3,int x4,int x5,double x6,int x7,double x8) {
47     return (x1+x2+x3+x4+x5+x6+x7+x8);
48 }
49 
mipsfltest1(int x1,int x2,double x3)50 EXPORT double mipsfltest1(int x1,int x2,double x3) {
51     return (x1+x2+x3);
52 }
53 
mipsfltest2(int x1,double x2,double x3)54 EXPORT double mipsfltest2(int x1,double x2,double x3) {
55     return (x1+x2+x3);
56 }
57 
ppcfltest(int x1,double x2,int x3,double x4,int x5,double x6,int x7,double x8,double x9,double x10,double x11,double x12,double x13,double x14,double x15,double x16,double x17,double x18,double x19)58 EXPORT double ppcfltest(int x1,double x2,int x3,double x4,int x5,double x6,int x7,double x8,double x9,double x10,double x11,double x12,double x13,double x14,double x15,double x16,double x17,double x18,double x19) {
59     return x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12+x13+x14+x15+x16+x17+x18+x19;
60 }
61 
ppcfltest2(int x1,double x2,int x3,double x4,int x5,long long x5_5,double x6,int x7,double x8,long long x8_5,int x8_75,double x9,double x10,double x11,double x12,double x13,float x14,double x15,int x15_5,double x16,int x16_5,long long x17,double x18,int x18_5,double x19)62 EXPORT double ppcfltest2(int x1, double x2, int x3, double x4, int x5, long long x5_5, double x6, int x7, double x8, long long x8_5, int x8_75, double x9, double x10, double x11, double x12, double x13, float x14, double x15, int x15_5, double x16, int x16_5, long long x17, double x18, int x18_5, double x19) {
63     return x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12+x13+x14+x15+x16+x17+x18+x19 + x5_5 + x8_5 + x8_75 + x15_5 + x16_5 + x18_5;
64 }
65 
66 typedef char i8;
67 typedef unsigned char u8;
68 typedef short i16;
69 typedef unsigned short u16;
70 typedef int i32;
71 typedef unsigned int u32;
72 #ifdef _WIN32
73 typedef __int64 i64;
74 typedef unsigned __int64 u64;
75 typedef __int64 LONGLONG;
76 typedef unsigned __int64 UNSIGNED_LONGLONG;
77 #else
78 typedef long long i64;
79 typedef unsigned long long u64;
80 typedef long long LONGLONG;
81 typedef unsigned long long UNSIGNED_LONGLONG;
82 #endif
83 typedef float single_float;
84 typedef double double_float;
85 
check_types(int Bchar,int Bwchar,int Bshort,int Bint,int Blong,int Blonglong,int Bfloat,int Bdouble,int Bvoid_star)86 EXPORT int check_types(int Bchar, int Bwchar, int Bshort, int Bint, int Blong, int Blonglong, int Bfloat, int Bdouble, int Bvoid_star) {
87   int succ = 1;
88   if (sizeof(i8) != 1) {
89     fprintf(stderr,"sizeof(i8) [%ld] != 1\n", (long)sizeof(i8));
90     succ = 0;
91   }
92   if (sizeof(u8) != 1) {
93     fprintf(stderr,"sizeof(u8) [%ld] != 1\n", (long)sizeof(u8));
94     succ = 0;
95   }
96   if (sizeof(i16) != 2) {
97     fprintf(stderr,"sizeof(i16) [%ld] != 2\n", (long)sizeof(i16));
98     succ = 0;
99   }
100   if (sizeof(u16) != 2) {
101     fprintf(stderr,"sizeof(u16) [%ld] != 2\n", (long)sizeof(u16));
102     succ = 0;
103   }
104   if (sizeof(i32) != 4) {
105     fprintf(stderr,"sizeof(i32) [%ld] != 4\n", (long)sizeof(i32));
106     succ = 0;
107   }
108   if (sizeof(u32) != 4) {
109     fprintf(stderr,"sizeof(u32) [%ld] != 4\n", (long)sizeof(u32));
110     succ = 0;
111   }
112   if (sizeof(i64) != 8) {
113     fprintf(stderr,"sizeof(i64) [%ld] != 8\n", (long)sizeof(i64));
114     succ = 0;
115   }
116   if (sizeof(u64) != 8) {
117     fprintf(stderr,"sizeof(u64) [%ld] != 8\n", (long)sizeof(u64));
118     succ = 0;
119   }
120   if (sizeof(single_float) != 4) {
121     fprintf(stderr,"sizeof(single_float) [%ld] != 4\n", (long)sizeof(single_float));
122     succ = 0;
123   }
124   if (sizeof(double_float) != 8) {
125     fprintf(stderr,"sizeof(double_float) [%ld] != 8\n", (long)sizeof(double_float));
126     succ = 0;
127   }
128   if (sizeof(char) != Bchar) {
129     fprintf(stderr,"sizeof(char) [%ld] != %ld\n", (long)sizeof(char), (long)Bchar);
130     succ = 0;
131   }
132   if (sizeof(wchar_t) != Bwchar) {
133     fprintf(stderr,"sizeof(wchar_t) [%ld] != %ld\n", (long)sizeof(wchar_t), (long)Bwchar);
134     succ = 0;
135   }
136   if (sizeof(short) != Bshort) {
137     fprintf(stderr,"sizeof(short) [%ld] != %ld\n", (long)sizeof(short), (long)Bshort);
138     succ = 0;
139   }
140   if (sizeof(int) != Bint) {
141     fprintf(stderr,"sizeof(int) [%ld] != %ld\n", (long)sizeof(int), (long)Bint);
142     succ = 0;
143   }
144   if (sizeof(long) != Blong) {
145     fprintf(stderr,"sizeof(long) [%ld] != %ld\n", (long)sizeof(long), (long)Blong);
146     succ = 0;
147   }
148   if (sizeof(long long) != Blonglong) {
149     fprintf(stderr,"sizeof(long long) [%ld] != %ld\n", (long)sizeof(long long), (long)Blong);
150     succ = 0;
151   }
152   if (sizeof(float) != Bfloat) {
153     fprintf(stderr,"sizeof(float) [%ld] != %ld\n", (long)sizeof(float), (long)Bfloat);
154     succ = 0;
155   }
156   if (sizeof(double) != Bdouble) {
157     fprintf(stderr,"sizeof(double) [%ld] != %ld\n", (long)sizeof(double), (long)Bdouble);
158     succ = 0;
159   }
160   if (sizeof(void *) != Bvoid_star) {
161     fprintf(stderr,"sizeof(void *) [%ld] != %ld\n", (long)sizeof(void *), (long)Bvoid_star);
162     succ = 0;
163   }
164   return succ;
165 }
166 
i8_to_i8(i8 x,int k)167 EXPORT i8 i8_to_i8(i8 x, int k) {
168   return x + k;
169 }
170 
u8_to_u8(u8 x,int k)171 EXPORT u8 u8_to_u8(u8 x, int k) {
172   return x + k;
173 }
174 
call_i8(ptr code,i8 x,int m,int k)175 EXPORT i8 call_i8(ptr code, i8 x, int m, int k) {
176   return (*((i8 (*) (i8))Sforeign_callable_entry_point(code)))(x + m) + k;
177 }
178 
call_u8(ptr code,u8 x,int m,int k)179 EXPORT u8 call_u8(ptr code, u8 x, int m, int k) {
180   return (*((u8 (*) (u8))Sforeign_callable_entry_point(code)))(x + m) + k;
181 }
182 
i16_to_i16(i16 x,int k)183 EXPORT i16 i16_to_i16(i16 x, int k) {
184   return x + k;
185 }
186 
u16_to_u16(u16 x,int k)187 EXPORT u16 u16_to_u16(u16 x, int k) {
188   return x + k;
189 }
190 
call_i16(ptr code,i16 x,int m,int k)191 EXPORT i16 call_i16(ptr code, i16 x, int m, int k) {
192   return (*((i16 (*) (i16))Sforeign_callable_entry_point(code)))(x + m) + k;
193 }
194 
call_u16(ptr code,u16 x,int m,int k)195 EXPORT u16 call_u16(ptr code, u16 x, int m, int k) {
196   return (*((u16 (*) (u16))Sforeign_callable_entry_point(code)))(x + m) + k;
197 }
198 
i32_to_i32(i32 x,int k)199 EXPORT i32 i32_to_i32(i32 x, int k) {
200   return x + k;
201 }
202 
u32_to_u32(u32 x,int k)203 EXPORT u32 u32_to_u32(u32 x, int k) {
204   return x + k;
205 }
206 
call_i32(ptr code,i32 x,int m,int k)207 EXPORT i32 call_i32(ptr code, i32 x, int m, int k) {
208   return (*((i32 (*) (i32))Sforeign_callable_entry_point(code)))(x + m) + k;
209 }
210 
call_u32(ptr code,u32 x,int m,int k)211 EXPORT u32 call_u32(ptr code, u32 x, int m, int k) {
212   return (*((u32 (*) (u32))Sforeign_callable_entry_point(code)))(x + m) + k;
213 }
214 
i64_to_i64(u64 x,int k)215 EXPORT i64 i64_to_i64(u64 x, int k) {
216   return x + k;
217 }
218 
u64_to_u64(u64 x,int k)219 EXPORT u64 u64_to_u64(u64 x, int k) {
220   return x + k;
221 }
222 
call_i64(ptr code,i64 x,int m,int k)223 EXPORT i64 call_i64(ptr code, i64 x, int m, int k) {
224   return (*((i64 (*) (i64))Sforeign_callable_entry_point(code)))(x + m) + k;
225 }
226 
call_u64(ptr code,u64 x,int m,int k)227 EXPORT u64 call_u64(ptr code, u64 x, int m, int k) {
228   return (*((u64 (*) (u64))Sforeign_callable_entry_point(code)))(x + m) + k;
229 }
230 
sf_to_sf(single_float x)231 EXPORT single_float sf_to_sf(single_float x) {
232   return x + 1;
233 }
234 
call_sf(ptr code,single_float x,int m,int k)235 EXPORT single_float call_sf(ptr code, single_float x, int m, int k) {
236   return (*((single_float (*) (single_float))Sforeign_callable_entry_point(code)))(x + m) + k;
237 }
238 
df_to_df(double_float x)239 EXPORT double_float df_to_df(double_float x) {
240   return x + 1;
241 }
242 
call_df(ptr code,double_float x,int m,int k)243 EXPORT double_float call_df(ptr code, double_float x, int m, int k) {
244   return (*((double_float (*) (double_float))Sforeign_callable_entry_point(code)))(x + m) + k;
245 }
246 
u8_star_to_u8_star(u8 * s)247 EXPORT u8 *u8_star_to_u8_star(u8 *s) {
248   return s == (u8 *)0 ? (u8 *)0 : s + 1;
249 }
250 
call_u8_star(ptr code,u8 * s)251 EXPORT u8 *call_u8_star(ptr code, u8 *s) {
252   return (*((u8 *(*) (u8 *))Sforeign_callable_entry_point(code)))(s + 1) + 1;
253 }
254 
u16_star_to_u16_star(u16 * s)255 EXPORT u16 *u16_star_to_u16_star(u16 *s) {
256   return s == (u16 *)0 ? (u16 *)0 : s + 1;
257 }
258 
call_u16_star(ptr code,u16 * s)259 EXPORT u16 *call_u16_star(ptr code, u16 *s) {
260   return (*((u16 *(*) (u16 *))Sforeign_callable_entry_point(code)))(s + 1) + 1;
261 }
262 
u32_star_to_u32_star(u32 * s)263 EXPORT u32 *u32_star_to_u32_star(u32 *s) {
264   return s == (u32 *)0 ? (u32 *)0 : s + 1;
265 }
266 
call_u32_star(ptr code,u32 * s)267 EXPORT u32 *call_u32_star(ptr code, u32 *s) {
268   return (*((u32 *(*) (u32 *))Sforeign_callable_entry_point(code)))(s + 1) + 1;
269 }
270 
char_star_to_char_star(char * s)271 EXPORT char *char_star_to_char_star(char *s) {
272   return s == (char *)0 ? (char *)0 : s + 1;
273 }
274 
call_string(ptr code,char * s)275 EXPORT char *call_string(ptr code, char *s) {
276   return (*((char *(*) (char *))Sforeign_callable_entry_point(code)))(s + 1) + 1;
277 }
278 
wchar_star_to_wchar_star(wchar_t * s)279 EXPORT wchar_t *wchar_star_to_wchar_star(wchar_t *s) {
280   return s == (wchar_t *)0 ? (wchar_t *)0 : s + 1;
281 }
282 
call_wstring(ptr code,wchar_t * s)283 EXPORT wchar_t *call_wstring(ptr code, wchar_t *s) {
284   return (*((wchar_t *(*) (wchar_t *))Sforeign_callable_entry_point(code)))(s + 1) + 1;
285 }
286 
char_to_char(char x)287 EXPORT char char_to_char(char x) {
288   return x - 0x20;
289 }
290 
call_char(ptr code,char x,int m,int k)291 EXPORT char call_char(ptr code, char x, int m, int k) {
292   return (*((char (*) (char))Sforeign_callable_entry_point(code)))(x + m) + k;
293 }
294 
wchar_to_wchar(wchar_t x)295 EXPORT wchar_t wchar_to_wchar(wchar_t x) {
296   return x - 0x20;
297 }
298 
call_wchar(ptr code,wchar_t x,int m,int k)299 EXPORT wchar_t call_wchar(ptr code, wchar_t x, int m, int k) {
300   return (*((wchar_t (*) (wchar_t))Sforeign_callable_entry_point(code)))(x + m) + k;
301 }
302 
short_to_short(short x,int k)303 EXPORT short short_to_short(short x, int k) {
304   return x + k;
305 }
306 
unsigned_short_to_unsigned_short(unsigned short x,int k)307 EXPORT unsigned short unsigned_short_to_unsigned_short(unsigned short x, int k) {
308   return x + k;
309 }
310 
call_short(ptr code,short x,int m,int k)311 EXPORT short call_short(ptr code, short x, int m, int k) {
312   return (*((short (*) (short))Sforeign_callable_entry_point(code)))(x + m) + k;
313 }
314 
call_unsigned_short(ptr code,unsigned short x,int m,int k)315 EXPORT unsigned short call_unsigned_short(ptr code, unsigned short x, int m, int k) {
316   return (*((unsigned short (*) (unsigned short))Sforeign_callable_entry_point(code)))(x + m) + k;
317 }
318 
int_to_int(int x,int k)319 EXPORT int int_to_int(int x, int k) {
320   return x + k;
321 }
322 
unsigned_to_unsigned(int x,int k)323 EXPORT unsigned unsigned_to_unsigned(int x, int k) {
324   return x + k;
325 }
326 
call_int(ptr code,int x,int m,int k)327 EXPORT int call_int(ptr code, int x, int m, int k) {
328   return (*((int (*) (int))Sforeign_callable_entry_point(code)))(x + m) + k;
329 }
330 
call_unsigned(ptr code,unsigned x,int m,int k)331 EXPORT unsigned call_unsigned(ptr code, unsigned x, int m, int k) {
332   return (*((unsigned (*) (unsigned))Sforeign_callable_entry_point(code)))(x + m) + k;
333 }
334 
long_to_long(long x,int k)335 EXPORT long long_to_long(long x, int k) {
336   return x + k;
337 }
338 
unsigned_long_to_unsigned_long(unsigned long x,int k)339 EXPORT unsigned long unsigned_long_to_unsigned_long(unsigned long x, int k) {
340   return x + k;
341 }
342 
call_long(ptr code,long x,int m,int k)343 EXPORT long call_long(ptr code, long x, int m, int k) {
344   return (*((long (*) (long))Sforeign_callable_entry_point(code)))(x + m) + k;
345 }
346 
call_unsigned_long(ptr code,unsigned long x,int m,int k)347 EXPORT unsigned long call_unsigned_long(ptr code, unsigned long x, int m, int k) {
348   return (*((unsigned long (*) (unsigned long))Sforeign_callable_entry_point(code)))(x + m) + k;
349 }
350 
long_long_to_long_long(LONGLONG x,int k)351 EXPORT LONGLONG long_long_to_long_long(LONGLONG x, int k) {
352   return x + k;
353 }
354 
unsigned_long_long_to_unsigned_long_long(UNSIGNED_LONGLONG x,int k)355 EXPORT UNSIGNED_LONGLONG unsigned_long_long_to_unsigned_long_long(UNSIGNED_LONGLONG x, int k) {
356   return x + k;
357 }
358 
call_long_long(ptr code,LONGLONG x,int m,int k)359 EXPORT LONGLONG call_long_long(ptr code, LONGLONG x, int m, int k) {
360   return (*((LONGLONG (*) (LONGLONG))Sforeign_callable_entry_point(code)))(x + m) + k;
361 }
362 
call_unsigned_long_long(ptr code,UNSIGNED_LONGLONG x,int m,int k)363 EXPORT UNSIGNED_LONGLONG call_unsigned_long_long(ptr code, UNSIGNED_LONGLONG x, int m, int k) {
364   return (*((UNSIGNED_LONGLONG (*) (UNSIGNED_LONGLONG))Sforeign_callable_entry_point(code)))(x + m) + k;
365 }
366 
iptr_to_iptr(iptr x,int k)367 EXPORT iptr iptr_to_iptr(iptr x, int k) {
368   return x + k;
369 }
370 
uptr_to_uptr(uptr x,int k)371 EXPORT iptr uptr_to_uptr(uptr x, int k) {
372   return x + k;
373 }
374 
call_iptr(ptr code,iptr x,int m,int k)375 EXPORT iptr call_iptr(ptr code, iptr x, int m, int k) {
376   return (*((iptr (*) (iptr))Sforeign_callable_entry_point(code)))(x + m) + k;
377 }
378 
call_uptr(ptr code,uptr x,int m,int k)379 EXPORT iptr call_uptr(ptr code, uptr x, int m, int k) {
380   return (*((uptr (*) (uptr))Sforeign_callable_entry_point(code)))(x + m) + k;
381 }
382 
float_to_float(float x)383 EXPORT float float_to_float(float x) {
384   return x + 1;
385 }
386 
call_float(ptr code,float x,int m,int k)387 EXPORT float call_float(ptr code, float x, int m, int k) {
388   return (*((float (*) (float))Sforeign_callable_entry_point(code)))(x + m) + k;
389 }
390 
double_to_double(double x)391 EXPORT double double_to_double(double x) {
392   return x + 1;
393 }
394 
call_double(ptr code,double x,int m,int k)395 EXPORT double call_double(ptr code, double x, int m, int k) {
396   return (*((double (*) (double))Sforeign_callable_entry_point(code)))(x + m) + k;
397 }
398 
u32xu32_to_u64(u32 x,u32 y)399 EXPORT u64 u32xu32_to_u64(u32 x, u32 y) {
400   return (u64)x << 32 | (u64)y;
401 }
402 
i32xu32_to_i64(i32 x,u32 y)403 EXPORT i64 i32xu32_to_i64(i32 x, u32 y) {
404   return (i64)((u64)x << 32 | (u64)y);
405 }
406 
call_i32xu32_to_i64(ptr code,i32 x,u32 y,int k)407 EXPORT i64 call_i32xu32_to_i64(ptr code, i32 x, u32 y, int k) {
408   i64 q = (*((i64 (*) (i32, u32))Sforeign_callable_entry_point(code)))(x, y);
409   return q + k;
410 }
411 
ufoo64a(u64 a,u64 b,u64 c,u64 d,u64 e,u64 f,u64 g)412 EXPORT u64 ufoo64a(u64 a, u64 b, u64 c, u64 d, u64 e, u64 f, u64 g) {
413   return (a - b) + (c - d) + (e - f) + g;
414 }
415 
ufoo64b(u32 x,u64 a,u64 b,u64 c,u64 d,u64 e,u64 f,u64 g)416 EXPORT u64 ufoo64b(u32 x, u64 a, u64 b, u64 c, u64 d, u64 e, u64 f, u64 g) {
417   return (u64)x + (a - b) + (c - d) + (e - f) + g;
418 }
419 
ifoo64a(i64 a,i64 b,i64 c,i64 d,i64 e,i64 f,i64 g)420 EXPORT i64 ifoo64a(i64 a, i64 b, i64 c, i64 d, i64 e, i64 f, i64 g) {
421   return (a - b) + (c - d) + (e - f) + g;
422 }
423 
ifoo64b(i32 x,i64 a,i64 b,i64 c,i64 d,i64 e,i64 f,i64 g)424 EXPORT i64 ifoo64b(i32 x, i64 a, i64 b, i64 c, i64 d, i64 e, i64 f, i64 g) {
425   return (i64)x + (a - b) + (c - d) + (e - f) + g;
426 }
427 
call_many_times(void (* f)(iptr))428 EXPORT void call_many_times(void (*f)(iptr))
429 {
430   int x;
431   iptr a = 1, b = 3, c = 5, d = 7;
432   iptr e = 1, g = 3, h = 5, i = 7;
433   iptr j = 1, k = 3, l = 5, m = 7;
434   iptr big = (((iptr)1) << ((8 * sizeof(iptr)) - 2));
435 
436   /* The intent of the loop is to convince the C compiler to store
437      something in the same register used for CP (so, compile with
438      optimization). */
439   for (x = 0; x < 1000000; x++) {
440     f(big|(a+e+j));
441     a = b; b = c; c = d; d = e;
442     e = g; g = h; h = i; i = j;
443     j = k+2; k = l+2; l = m+2; m = m+2;
444   }
445 }
446 
call_many_times_bv(void (* f)(char * s))447 EXPORT void call_many_times_bv(void (*f)(char *s))
448 {
449   /* make this sensible as u8*, u16*, and u32* */
450   char buf[8] = { 1, 2, 3, 4, 0, 0, 0, 0 };
451   int x;
452 
453   for (x = 0; x < 1000000; x++) {
454     buf[0] = (x & 63) + 1;
455     f(buf);
456   }
457 }
458 
459 typedef void (*many_arg_callback_t)(int i, const char* s1, const char* s2, const char* s3,
460                                     const char* s4, int i2, const char* s6, const char* s7, int i3);
call_with_many_args(many_arg_callback_t callback)461 EXPORT void call_with_many_args(many_arg_callback_t callback)
462 {
463     callback(0, "this", "is", "working", "just", 1, "fine", "or does it?", 2);
464 }
465