1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
{****************************************************************}
{ CODE GENERATOR TEST PROGRAM }
{****************************************************************}
{ NODE TESTED : secondcalln() }
{****************************************************************}
{ PRE-REQUISITES: secondload() }
{ secondassign() }
{ secondcalln() }
{ secondadd() }
{ secondtypeconv() }
{****************************************************************}
{ DEFINES: }
{****************************************************************}
{ REMARKS: This tests a subset of the secondcalln() , it }
{ verifies the usage of external cdecl }
{ modules compiled with C compilers. }
{****************************************************************}
{$ifndef USE_PASCAL_OBJECT}
{$MODE OBJFPC}
{$R+}
uses strings,ctypes;
{$L ctest.o}
{$endif USE_PASCAL_OBJECT}
{$ifdef FPC_HAS_TYPE_EXTENDED}
{$define test_longdouble}
{$endif}
{ Use C alignment of records }
{$PACKRECORDS C}
const
RESULT_U8BIT = $55;
RESULT_U16BIT = $500F;
RESULT_U32BIT = $500F0000;
RESULT_U64BIT = $1BCDABCD;
RESULT_S16BIT = -12;
RESULT_S32BIT = -120;
RESULT_S64BIT = -12000;
RESULT_FLOAT = 14.54;
RESULT_DOUBLE = 15.54;
RESULT_LONGDOUBLE = 16.54;
RESULT_PCHAR = 'Hello world';
type
_1byte_ = record
u8 : byte;
end;
_3byte_ = record
u8 : byte;
u16 : word;
end;
_3byte_s = record
u16 : word;
w8 : byte;
end;
_5byte_ = record
u8 : byte;
u32 : cardinal;
end;
_7byte_ = record
u8: byte;
s64: int64;
u16: word;
end;
byte_array = array [0..1] of byte;
word_array = array [0..1] of word;
cardinal_array = array [0..1] of cardinal;
qword_array = array [0..1] of qword;
smallint_array = array [0..1] of smallint;
longint_array = array [0..1] of longint;
int64_array = array [0..1] of int64;
single_array = array [0..1] of single;
double_array = array [0..1] of double;
clongdouble_array = array [0..1] of clongdouble;
{ simple parameter passing }
procedure test_param_u8(x: byte); cdecl; external;
procedure test_param_u16(x : word); cdecl; external;
procedure test_param_u32(x: cardinal); cdecl; external;
procedure test_param_u64(x: qword); cdecl; external;
procedure test_param_s16(x : smallint); cdecl; external;
procedure test_param_s32(x: longint); cdecl; external;
procedure test_param_s64(x: int64); cdecl; external;
procedure test_param_float(x : single); cdecl; external;
procedure test_param_double(x: double); cdecl; external;
procedure test_param_longdouble(x: clongdouble); cdecl; external;
procedure test_param_var_u8(var x: byte); cdecl; external;
{ array parameter passing }
procedure test_array_param_u8(x: byte_array); cdecl; external;
procedure test_array_param_u16(x : word_array); cdecl; external;
procedure test_array_param_u32(x: cardinal_array); cdecl; external;
procedure test_array_param_u64(x: qword_array); cdecl; external;
procedure test_array_param_s16(x :smallint_array); cdecl; external;
procedure test_array_param_s32(x: longint_array); cdecl; external;
procedure test_array_param_s64(x: int64_array); cdecl; external;
procedure test_array_param_float(x : single_array); cdecl; external;
procedure test_array_param_double(x: double_array); cdecl; external;
procedure test_array_param_longdouble(x: clongdouble_array); cdecl; external;
{ mixed parameter passing }
procedure test_param_mixed_u16(z: byte; x : word; y :byte); cdecl; external;
procedure test_param_mixed_u32(z: byte; x: cardinal; y: byte); cdecl; external;
procedure test_param_mixed_s64(z: byte; x: int64; y: byte); cdecl; external;
procedure test_param_mixed_float(x: single; y: byte); cdecl; external;
procedure test_param_mixed_double(x: double; y: byte); cdecl; external;
procedure test_param_mixed_long_double(x: clongdouble; y: byte); cdecl; external;
procedure test_param_mixed_var_u8(var x: byte;y:byte); cdecl; external;
{ structure parameter testing }
procedure test_param_struct_tiny(buffer : _1BYTE_); cdecl; external;
procedure test_param_struct_small(buffer : _3BYTE_); cdecl; external;
procedure test_param_struct_small_s(buffer : _3BYTE_S); cdecl; external;
procedure test_param_struct_medium(buffer : _5BYTE_); cdecl; external;
procedure test_param_struct_large(buffer : _7BYTE_); cdecl; external;
{ mixed with structure parameter testing }
procedure test_param_mixed_struct_tiny(buffer : _1BYTE_; y :byte); cdecl; external;
procedure test_param_mixed_struct_small(buffer : _3BYTE_; y :byte); cdecl; external;
procedure test_param_mixed_struct_small_s(buffer : _3BYTE_S; y :byte); cdecl; external;
procedure test_param_mixed_struct_medium(buffer : _5BYTE_; y :byte); cdecl; external;
procedure test_param_mixed_struct_large(buffer : _7BYTE_; y :byte); cdecl; external;
{ function result value testing }
function test_function_u8: byte; cdecl; external;
function test_function_u16: word; cdecl; external;
function test_function_u32: cardinal; cdecl; external;
function test_function_u64: qword; cdecl; external;
function test_function_s16: smallint; cdecl; external;
function test_function_s32: longint; cdecl; external;
function test_function_s64: int64; cdecl; external;
function test_function_pchar: pchar; cdecl; external;
function test_function_float : single; cdecl; external;
function test_function_double : double; cdecl; external;
function test_function_longdouble: clongdouble; cdecl; external;
function test_function_tiny_struct : _1byte_; cdecl; external;
function test_function_small_struct : _3byte_; cdecl; external;
function test_function_small_struct_s : _3byte_s; cdecl; external;
function test_function_medium_struct : _5byte_; cdecl; external;
function test_function_struct : _7byte_; cdecl; external;
var
global_u8bit : byte; cvar; external;
global_u16bit : word; cvar; external;
global_u32bit : cardinal; cvar;external;
global_u64bit : qword; cvar; external;
global_s16bit : smallint; cvar; external;
global_s32bit : longint; cvar;external;
global_s64bit : int64; cvar; external;
global_float : single; cvar;external;
global_double : double; cvar;external;
global_long_double : clongdouble; cvar; external;
value_u8bit : byte;
value_s16bit : smallint;
value_s32bit : longint;
value_s64bit : int64;
value_u16bit : word;
value_u32bit : cardinal;
value_u64bit : qword;
value_float : single;
value_double : double;
value_long_double : extended;
array_u8bit : array [0..1] of byte;
array_s16bit : array [0..1] of smallint;
array_s32bit : array [0..1] of longint;
array_s64bit : array [0..1] of int64;
array_u16bit : array [0..1] of word;
array_u32bit : array [0..1] of cardinal;
array_u64bit : array [0..1] of qword;
array_float : array [0..1] of single;
array_double : array [0..1] of double;
array_long_double : array [0..1] of clongdouble;
procedure clear_globals;
begin
global_u8bit := 0;
global_u16bit := 0;
global_u32bit := 0;
global_u64bit := 0;
global_s16bit := 0;
global_s32bit := 0;
global_s64bit := 0;
global_float := 0.0;
global_double := 0.0;
global_long_double := 0.0;
end;
procedure clear_values;
begin
value_u8bit := 0;
value_u16bit := 0;
value_u32bit := 0;
value_u64bit := 0;
value_s16bit := 0;
value_s32bit := 0;
value_s64bit := 0;
value_float := 0.0;
value_double := 0.0;
value_long_double := 0.0;
end;
{ in sub procedure to detect stack corruption when exiting }
procedure dotest;
const
has_errors : boolean = false;
procedure fail;
begin
WriteLn('Failed!');
has_errors:=true;
end;
var failed : boolean;
tinystruct : _1BYTE_;
smallstruct : _3BYTE_;
smallstruct_s : _3BYTE_S;
mediumstruct : _5BYTE_;
bigstruct : _7BYTE_;
pc: pchar;
begin
Write('External simple parameter testing...');
failed := false;
clear_values;
clear_globals;
value_u8bit := RESULT_U8BIT;
test_param_u8(value_u8bit);
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
value_u16bit := RESULT_U16BIT;
test_param_u16(value_u16bit);
if global_u16bit <> RESULT_U16BIT then
failed := true;
clear_values;
clear_globals;
value_u32bit := RESULT_U32BIT;
test_param_u32(value_u32bit);
if global_u32bit <> RESULT_U32BIT then
failed := true;
clear_values;
clear_globals;
value_u64bit := RESULT_U64BIT;
test_param_u64(value_u64bit);
if global_u64bit <> RESULT_U64BIT then
failed := true;
clear_values;
clear_globals;
value_s16bit := RESULT_S16BIT;
test_param_s16(value_s16bit);
if global_s16bit <> RESULT_S16BIT then
failed := true;
clear_values;
clear_globals;
value_s32bit := RESULT_S32BIT;
test_param_s32(value_s32bit);
if global_s32bit <> RESULT_S32BIT then
failed := true;
clear_values;
clear_globals;
value_s64bit := RESULT_S64BIT;
test_param_s64(value_s64bit);
if global_s64bit <> RESULT_S64BIT then
failed := true;
clear_values;
clear_globals;
value_float := RESULT_FLOAT;
test_param_float(value_float);
if trunc(global_float) <> trunc(RESULT_FLOAT) then
failed := true;
clear_values;
clear_globals;
value_double := RESULT_DOUBLE;
test_param_double(value_double);
if trunc(global_double) <> trunc(RESULT_DOUBLE) then
failed := true;
{$ifdef test_longdouble}
clear_values;
clear_globals;
value_long_double := RESULT_LONGDOUBLE;
test_param_longdouble(value_long_double);
if trunc(global_long_double) <> trunc(RESULT_LONGDOUBLE) then
failed := true;
{$endif}
{ var parameter testing }
clear_values;
clear_globals;
test_param_var_u8(value_u8bit);
if value_u8bit <> RESULT_U8BIT then
failed := true;
If failed then
fail
else
WriteLn('Passed!');
Write('External array parameter testing...');
failed := false;
clear_values;
clear_globals;
array_u8bit[1] := RESULT_U8BIT;
test_array_param_u8(array_u8bit);
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
array_u16bit[1] := RESULT_U16BIT;
test_array_param_u16(array_u16bit);
if global_u16bit <> RESULT_U16BIT then
failed := true;
clear_values;
clear_globals;
array_u32bit[1] := RESULT_U32BIT;
test_array_param_u32(array_u32bit);
if global_u32bit <> RESULT_U32BIT then
failed := true;
clear_values;
clear_globals;
array_u64bit[1] := RESULT_U64BIT;
test_array_param_u64(array_u64bit);
if global_u64bit <> RESULT_U64BIT then
failed := true;
clear_values;
clear_globals;
array_s16bit[1] := RESULT_S16BIT;
test_array_param_s16(array_s16bit);
if global_s16bit <> RESULT_S16BIT then
failed := true;
clear_values;
clear_globals;
array_s32bit[1] := RESULT_S32BIT;
test_array_param_s32(array_s32bit);
if global_s32bit <> RESULT_S32BIT then
failed := true;
clear_values;
clear_globals;
array_s64bit[1] := RESULT_S64BIT;
test_array_param_s64(array_s64bit);
if global_s64bit <> RESULT_S64BIT then
failed := true;
clear_values;
clear_globals;
array_float[1] := RESULT_FLOAT;
test_array_param_float(array_float);
if trunc(global_float) <> trunc(RESULT_FLOAT) then
failed := true;
clear_values;
clear_globals;
array_double[1] := RESULT_DOUBLE;
test_array_param_double(array_double);
if trunc(global_double) <> trunc(RESULT_DOUBLE) then
failed := true;
clear_values;
clear_globals;
{$ifdef test_longdouble}
array_long_double[1] := RESULT_LONGDOUBLE;
test_array_param_longdouble(array_long_double);
if trunc(global_long_double) <> trunc(RESULT_LONGDOUBLE) then
failed := true;
{$endif test_longdouble}
If failed then
fail
else
WriteLn('Passed!');
Write('External mixed parameter testing...');
failed := false;
clear_values;
clear_globals;
test_param_mixed_var_u8(value_u8bit,RESULT_U8BIT);
if value_u8bit <> RESULT_U8BIT then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
value_u8bit := RESULT_U8BIT;
value_u16bit := RESULT_U16BIT;
test_param_mixed_u16(value_u8bit, value_u16bit, value_u8bit);
if global_u16bit <> RESULT_U16BIT then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
value_u8bit := RESULT_U8BIT;
value_u32bit := RESULT_U32BIT;
test_param_mixed_u32(value_u8bit, value_u32bit, value_u8bit);
if global_u32bit <> RESULT_U32BIT then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
value_u8bit := RESULT_U8BIT;
value_s64bit := RESULT_S64BIT;
test_param_mixed_s64(value_u8bit, value_s64bit, value_u8bit);
if global_s64bit <> RESULT_S64BIT then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
value_u8bit := RESULT_U8BIT;
value_float := RESULT_FLOAT;
test_param_mixed_float(value_float, value_u8bit);
if global_float <> value_float then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
If failed then
fail
else
WriteLn('Passed!');
Write('External mixed parameter testing with floating values...');
failed := false;
clear_values;
clear_globals;
value_u8bit := RESULT_U8BIT;
value_double := RESULT_DOUBLE;
test_param_mixed_double(value_double, value_u8bit);
if global_double <> value_double then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
{$ifdef test_longdouble}
value_u8bit := RESULT_U8BIT;
value_long_double := RESULT_LONGDOUBLE;
test_param_mixed_long_double(value_long_double, value_u8bit);
if global_long_double <> value_long_double then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
{$endif}
If failed then
fail
else
WriteLn('Passed!');
Write('External struct parameter testing...');
failed := false;
clear_values;
clear_globals;
tinystruct.u8 := RESULT_U8BIT;
test_param_struct_tiny(tinystruct);
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
smallstruct.u8 := RESULT_U8BIT;
smallstruct.u16 := RESULT_u16BIT;
test_param_struct_small(smallstruct);
if global_u16bit <> RESULT_U16BIT then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
smallstruct_s.u16 := RESULT_U16BIT;
smallstruct_s.w8 := RESULT_U8BIT;
test_param_struct_small_s(smallstruct_s);
if global_u16bit <> RESULT_U16BIT then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
mediumstruct.u8 := RESULT_U8BIT;
mediumstruct.u32 := RESULT_U32BIT;
test_param_struct_medium(mediumstruct);
if global_u32bit <> RESULT_U32BIT then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
bigstruct.u8 := RESULT_U8BIT;
bigstruct.u16 := RESULT_U16BIT;
bigstruct.s64 := RESULT_S64BIT;
test_param_struct_large(bigstruct);
if global_s64bit <> RESULT_S64BIT then
failed := true;
if global_u16bit <> RESULT_U16BIT then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
If failed then
fail
else
WriteLn('Passed!');
Write('External mixed struct/byte parameter testing...');
failed := false;
clear_values;
clear_globals;
test_param_mixed_struct_tiny(tinystruct,RESULT_U8BIT);
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
smallstruct.u16 := RESULT_u16BIT;
test_param_mixed_struct_small(smallstruct,RESULT_U8BIT);
if global_u16bit <> RESULT_U16BIT then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
smallstruct_s.u16 := RESULT_U16BIT;
test_param_mixed_struct_small_s(smallstruct_s,RESULT_U8BIT);
if global_u16bit <> RESULT_U16BIT then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
mediumstruct.u32 := RESULT_U32BIT;
test_param_mixed_struct_medium(mediumstruct,RESULT_U8BIT);
if global_u32bit <> RESULT_U32BIT then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
bigstruct.u16 := RESULT_U16BIT;
bigstruct.s64 := RESULT_S64BIT;
test_param_mixed_struct_large(bigstruct,RESULT_U8BIT);
if global_s64bit <> RESULT_S64BIT then
failed := true;
if global_u16bit <> RESULT_U16BIT then
failed := true;
if global_u8bit <> RESULT_U8BIT then
failed := true;
If failed then
fail
else
WriteLn('Passed!');
Write('Integer function result testing...');
failed := false;
clear_values;
clear_globals;
value_u8bit := test_function_u8;
if value_u8bit <> RESULT_U8BIT then
failed := true;
clear_values;
clear_globals;
value_u16bit := test_function_u16;
if value_u16bit <> RESULT_U16BIT then
failed := true;
clear_values;
clear_globals;
value_u32bit := test_function_u32;
if value_u32bit <> RESULT_U32BIT then
failed := true;
clear_values;
clear_globals;
value_u64bit := test_function_u64;
if value_u64bit <> RESULT_U64BIT then
failed := true;
clear_values;
clear_globals;
value_s16bit := test_function_s16;
if value_s16bit <> RESULT_S16BIT then
failed := true;
clear_values;
clear_globals;
value_s32bit := test_function_s32;
if value_s32bit <> RESULT_S32BIT then
failed := true;
clear_values;
clear_globals;
value_s64bit := test_function_s64;
if value_s64bit <> RESULT_S64BIT then
failed := true;
clear_values;
clear_globals;
If failed then
fail
else
WriteLn('Passed!');
Write('pchar function result testing...');
failed := false;
{ verify if the contents both strings are equal }
pc := test_function_pchar;
if strcomp(pc, RESULT_PCHAR) <> 0 then
failed := true;
clear_values;
clear_globals;
If failed then
fail
else
WriteLn('Passed!');
Write('Real function result testing...');
failed := false;
value_float := test_function_float;
if trunc(value_float) <> trunc(RESULT_FLOAT) then
failed := true;
clear_values;
clear_globals;
value_double := test_function_double;
if trunc(value_double) <> trunc(RESULT_DOUBLE) then
failed := true;
{$ifdef test_longdouble}
clear_values;
clear_globals;
value_long_double := test_function_longdouble;
if trunc(value_long_double) <> trunc(RESULT_LONGDOUBLE) then
failed := true;
{$endif}
clear_values;
clear_globals;
If failed then
fail
else
WriteLn('Passed!');
Write('Function result testing for struct...');
failed := false;
tinystruct := test_function_tiny_struct;
if tinystruct.u8 <> RESULT_U8BIT then
failed := true;
smallstruct := test_function_small_struct;
if smallstruct.u8 <> RESULT_U8BIT then
failed := true;
if smallstruct.u16 <> RESULT_U16BIT then
failed := true;
smallstruct_s := test_function_small_struct_s;
if smallstruct_s.u16 <> RESULT_U16BIT then
failed := true;
if smallstruct_s.w8 <> RESULT_U8BIT then
failed := true;
mediumstruct := test_function_medium_struct;
if mediumstruct.u8 <> RESULT_U8BIT then
failed := true;
if mediumstruct.u32 <> RESULT_U32BIT then
failed := true;
bigstruct := test_function_struct;
if bigstruct.u8 <> RESULT_U8BIT then
failed := true;
if bigstruct.s64 <> RESULT_S64BIT then
failed := true;
if bigstruct.u16 <> RESULT_U16BIT then
failed := true;
If failed then
fail
else
WriteLn('Passed!');
if has_errors then
Halt(1);
end;
begin
dotest;
end.
|