* Added a simple version of printf()
[fw/sdcc] / support / tests / dhrystone / dhry.c
1 /** Sanatised version of the dhrystone-2.1 test.
2
3     Modifications Michael Hope <michaelh@earthling.net>
4
5     This is derrived from the dhrystone-2.1.tar.gz tarball available
6     all over the net.  I sourced it from the Red Hat src rpm.
7
8     Modifications:
9     * Made it more ANSI compliant.
10     * Changed any array function arguments to pointers to make my 
11       compiler work.
12     * Removed malloc's for a couple of static arrays.
13     * Into one file.
14
15     Notes:
16     * The comment at the start of Func_1 about the input being
17       H, R on the first call is wrong - Func_1 is first called
18       from Func_2 where the input is R, Y.  The test still succeeds.
19
20     I couldnt find a copyright in the original - the most relevent
21     part follows:
22
23     A SPECIAL THANKS
24     I didn't write the DHRYSTONE benchmark.  Rheinhold Weicker did. He has
25     certainly provided us with a useful tool for benchmarking, and is
26     to be congratulated.
27
28     Rick Richardson
29     PC Research, Inc.
30     (201) 389-8963 (9-17 EST)
31     (201) 542-3734 (7-9,17-24 EST)
32     ...!uunet!pcrat!rick        (normal mail)
33     ...!uunet!pcrat!dry2        (results only)
34 */
35
36 #include "dhry.h"
37 /* Temporary definitions.  Remove soon :) */
38 char *strcpy(char *dest, const char *src);
39 void *memcpy(void *dest, const char *src, int wLen);
40 int strcmp(const char *s1, const char *s2);
41
42 Rec_Pointer     Ptr_Glob,
43                 Next_Ptr_Glob;
44 int             Int_Glob;
45 Boolean         Bool_Glob;
46 char            Ch_1_Glob,
47                 Ch_2_Glob;
48 int             Arr_1_Glob [50];
49 int             Arr_2_Glob [50] [50];
50
51 /* Used instead of malloc() */
52 static Rec_Type _r[2];
53
54 void Proc_1 (REG Rec_Pointer Ptr_Val_Par);
55 void Proc_2 (One_Fifty *Int_Par_Ref);
56 void Proc_5 (void);
57 void Proc_4 (void);
58 void Proc_7 (One_Fifty Int_1_Par_Val, One_Fifty Int_2_Par_Val, One_Fifty *Int_Par_Ref);
59 void Proc_8 (int **Arr_1_Par_Ref, int **Arr_2_Par_Ref, 
60              int Int_1_Par_Val, int Int_2_Par_Val);
61 void Proc_6 (Enumeration Enum_Val_Par, Enumeration *Enum_Ref_Par);
62 void Proc_3 (Rec_Pointer *Ptr_Ref_Par);
63 Enumeration Func_1 (Capital_Letter Ch_1_Par_Val, Capital_Letter Ch_2_Par_Val);
64 Boolean Func_2 (char *Str_1_Par_Ref, char *Str_2_Par_Ref);
65 Boolean Func_3 (Enumeration Enum_Par_Val);
66
67 void printf(const char *format, ...);
68 void exit(int val);
69
70 int _main(void) 
71 {
72     One_Fifty       Int_1_Loc;
73     REG   One_Fifty       Int_2_Loc;
74     One_Fifty       Int_3_Loc;
75     REG   Capital_Letter Ch_Index;
76     Enumeration     Enum_Loc;
77     Str_30          Str_1_Loc;
78     Str_30          Str_2_Loc;
79     REG   int             Run_Index;
80     REG   int             Number_Of_Runs;
81
82     printf("[dhry]\n");
83
84     Next_Ptr_Glob = &_r[0];
85     Ptr_Glob = &_r[1];
86
87     Ptr_Glob->Ptr_Comp                    = Next_Ptr_Glob;
88     Ptr_Glob->Discr                       = Ident_1;
89     Ptr_Glob->variant.var_1.Enum_Comp     = Ident_3;
90     Ptr_Glob->variant.var_1.Int_Comp      = 40;
91
92     strcpy (Ptr_Glob->variant.var_1.Str_Comp, 
93             "DHRYSTONE PROGRAM, SOME STRING");
94     strcpy (Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
95
96     Arr_2_Glob [8][7] = 10;
97     /* Was missing in published program. Without this statement,    */
98     /* Arr_2_Glob [8][7] would have an undefined value.             */
99     /* Warning: With 16-Bit processors and Number_Of_Runs > 32000,  */
100     /* overflow may occur for this array element.                   */
101
102     Number_Of_Runs = 5;
103
104     /* Main test loop */
105     for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index) {
106         printf("Proc_5\n");
107         Proc_5();
108         Proc_4();
109         /* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
110         Int_1_Loc = 2;
111         Int_2_Loc = 3;
112         strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
113         Enum_Loc = Ident_2;
114         printf("Func_2\n");
115         Bool_Glob = ! Func_2 (Str_1_Loc, Str_2_Loc);
116         /* Bool_Glob == 1 */
117         while (Int_1_Loc < Int_2_Loc)  /* loop body executed once */
118             {
119                 printf("m.1 Executes once.\n");
120                 Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
121                 /* Int_3_Loc == 7 */
122                 printf("Proc_7\n");
123                 Proc_7 (Int_1_Loc, Int_2_Loc, &Int_3_Loc);
124                 /* Int_3_Loc == 7 */
125                 Int_1_Loc += 1;
126             } /* while */
127         printf("m.2 Check above executed once.\n");
128         /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
129         printf("Proc_8\n");
130         Proc_8 (Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
131         /* Int_Glob == 5 */
132         printf("Proc_1\n");
133         Proc_1 (Ptr_Glob);
134         for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index)
135             /* loop body executed twice */
136             {
137                 printf("Func_1\n");
138                 if (Enum_Loc == Func_1 (Ch_Index, 'C'))
139                     /* then, not executed */
140                     {
141                         printf("Proc_6\n");
142                         Proc_6 (Ident_1, &Enum_Loc);
143                         strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 3'RD STRING");
144                         Int_2_Loc = Run_Index;
145                         Int_Glob = Run_Index;
146                     }
147             }
148         /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
149         Int_2_Loc = Int_2_Loc * Int_1_Loc;
150         Int_1_Loc = Int_2_Loc / Int_3_Loc;
151         Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;
152         /* Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7 */
153         printf("Proc_2\n");
154         Proc_2 (&Int_1_Loc);
155         /* Int_1_Loc == 5 */
156         printf("Looping.\n");
157     } /* loop "for Run_Index" */
158
159     printf("Done.\n");
160 #if !SDCC
161     printf ("Execution ends\n");
162     printf ("\n");
163     printf ("Final values of the variables used in the benchmark:\n");
164     printf ("\n");
165     printf ("Int_Glob:            %d\n", Int_Glob);
166     printf ("        should be:   %d\n", 5);
167     printf ("Bool_Glob:           %d\n", Bool_Glob);
168     printf ("        should be:   %d\n", 1);
169     printf ("Ch_1_Glob:           %c\n", Ch_1_Glob);
170     printf ("        should be:   %c\n", 'A');
171     printf ("Ch_2_Glob:           %c\n", Ch_2_Glob);
172     printf ("        should be:   %c\n", 'B');
173     printf ("Arr_1_Glob[8]:       %d\n", Arr_1_Glob[8]);
174     printf ("        should be:   %d\n", 7);
175     printf ("Arr_2_Glob[8][7]:    %d\n", Arr_2_Glob[8][7]);
176     printf ("        should be:   Number_Of_Runs + 10\n");
177     printf ("Ptr_Glob->\n");
178     printf ("  Ptr_Comp:          %d\n", (int) Ptr_Glob->Ptr_Comp);
179     printf ("        should be:   (implementation-dependent)\n");
180     printf ("  Discr:             %d\n", Ptr_Glob->Discr);
181     printf ("        should be:   %d\n", 0);
182     printf ("  Enum_Comp:         %d\n", Ptr_Glob->variant.var_1.Enum_Comp);
183     printf ("        should be:   %d\n", 2);
184     printf ("  Int_Comp:          %d\n", Ptr_Glob->variant.var_1.Int_Comp);
185     printf ("        should be:   %d\n", 17);
186     printf ("  Str_Comp:          %s\n", Ptr_Glob->variant.var_1.Str_Comp);
187     printf ("        should be:   DHRYSTONE PROGRAM, SOME STRING\n");
188     printf ("Next_Ptr_Glob->\n");
189     printf ("  Ptr_Comp:          %d\n", (int) Next_Ptr_Glob->Ptr_Comp);
190     printf ("        should be:   (implementation-dependent), same as above\n");
191     printf ("  Discr:             %d\n", Next_Ptr_Glob->Discr);
192     printf ("        should be:   %d\n", 0);
193     printf ("  Enum_Comp:         %d\n", Next_Ptr_Glob->variant.var_1.Enum_Comp);
194     printf ("        should be:   %d\n", 1);
195     printf ("  Int_Comp:          %d\n", Next_Ptr_Glob->variant.var_1.Int_Comp);
196     printf ("        should be:   %d\n", 18);
197     printf ("  Str_Comp:          %s\n",
198             Next_Ptr_Glob->variant.var_1.Str_Comp);
199     printf ("        should be:   DHRYSTONE PROGRAM, SOME STRING\n");
200     printf ("Int_1_Loc:           %d\n", Int_1_Loc);
201     printf ("        should be:   %d\n", 5);
202     printf ("Int_2_Loc:           %d\n", Int_2_Loc);
203     printf ("        should be:   %d\n", 13);
204     printf ("Int_3_Loc:           %d\n", Int_3_Loc);
205     printf ("        should be:   %d\n", 7);
206     printf ("Enum_Loc:            %d\n", Enum_Loc);
207     printf ("        should be:   %d\n", 1);
208     printf ("Str_1_Loc:           %s\n", Str_1_Loc);
209     printf ("        should be:   DHRYSTONE PROGRAM, 1'ST STRING\n");
210     printf ("Str_2_Loc:           %s\n", Str_2_Loc);
211     printf ("        should be:   DHRYSTONE PROGRAM, 2'ND STRING\n");
212     printf ("\n");
213 #endif
214 }
215
216 void Proc_1 (REG Rec_Pointer Ptr_Val_Par)
217 /* executed once */
218 {
219     REG Rec_Pointer Next_Record = Ptr_Val_Par->Ptr_Comp;  
220     /* == Ptr_Glob_Next */
221     /* Local variable, initialized with Ptr_Val_Par->Ptr_Comp,    */
222     /* corresponds to "rename" in Ada, "with" in Pascal           */
223   
224     structassign (*Ptr_Val_Par->Ptr_Comp, *Ptr_Glob); 
225     Ptr_Val_Par->variant.var_1.Int_Comp = 5;
226     Next_Record->variant.var_1.Int_Comp 
227         = Ptr_Val_Par->variant.var_1.Int_Comp;
228     Next_Record->Ptr_Comp = Ptr_Val_Par->Ptr_Comp;
229     Proc_3 (&Next_Record->Ptr_Comp);
230     /* Ptr_Val_Par->Ptr_Comp->Ptr_Comp 
231        == Ptr_Glob->Ptr_Comp */
232     if (Next_Record->Discr == Ident_1)
233         /* then, executed */
234         {
235             Next_Record->variant.var_1.Int_Comp = 6;
236             Proc_6 (Ptr_Val_Par->variant.var_1.Enum_Comp, 
237                     &Next_Record->variant.var_1.Enum_Comp);
238             Next_Record->Ptr_Comp = Ptr_Glob->Ptr_Comp;
239             Proc_7 (Next_Record->variant.var_1.Int_Comp, 10, 
240                     &Next_Record->variant.var_1.Int_Comp);
241         }
242     else /* not executed */
243         structassign (*Ptr_Val_Par, *Ptr_Val_Par->Ptr_Comp);
244 } /* Proc_1 */
245
246
247 void Proc_2 (One_Fifty *Int_Par_Ref)
248     /******************/
249     /* executed once */
250     /* *Int_Par_Ref == 1, becomes 4 */
251 {
252     One_Fifty  Int_Loc;  
253     Enumeration   Enum_Loc;
254
255     printf("-> Proc_2\n");
256
257     Int_Loc = *Int_Par_Ref + 10;
258     do {
259         printf("1\n");
260         /* executed once */
261         if (Ch_1_Glob == 'A')
262             /* then, executed */
263             {
264                 printf("2\n");
265                 Int_Loc -= 1;
266                 *Int_Par_Ref = Int_Loc - Int_Glob;
267                 Enum_Loc = Ident_1;
268             } /* if */
269         printf("3\n");
270     } while (Enum_Loc != Ident_1); /* true */
271     printf("Proc_2 done.\n");
272 } /* Proc_2 */
273
274
275 void Proc_3 (Rec_Pointer *Ptr_Ref_Par)
276     /******************/
277     /* executed once */
278     /* Ptr_Ref_Par becomes Ptr_Glob */
279 {
280     if (Ptr_Glob != Null)
281         /* then, executed */
282         *Ptr_Ref_Par = Ptr_Glob->Ptr_Comp;
283     Proc_7 (10, Int_Glob, &Ptr_Glob->variant.var_1.Int_Comp);
284 } /* Proc_3 */
285
286 void Proc_4 (void) /* without parameters */
287     /*******/
288     /* executed once */
289 {
290     Boolean Bool_Loc;
291
292     printf("-> Proc_4\n");
293     Bool_Loc = Ch_1_Glob == 'A';
294     Bool_Glob = Bool_Loc | Bool_Glob;
295     Ch_2_Glob = 'B';
296     printf("Expect Ch_1_Glob '%c' == 'A'\n", (char)Ch_1_Glob);
297     printf("       Ch_2_Glob '%c' == 'B'\n", (char)Ch_2_Glob);
298     printf("       Bool_Loc %d == 1\n", (unsigned)Bool_Loc);
299     printf("       Bool_Glob %d == 1\n", (unsigned)Bool_Glob);
300 } /* Proc_4 */
301
302
303 void Proc_5 () /* without parameters */
304     /*******/
305     /* executed once */
306 {
307     Ch_1_Glob = 'A';
308     Bool_Glob = false;
309 } /* Proc_5 */
310
311 void Proc_6 (Enumeration Enum_Val_Par, Enumeration *Enum_Ref_Par)
312     /*********************************/
313     /* executed once */
314     /* Enum_Val_Par == Ident_3, Enum_Ref_Par becomes Ident_2 */
315
316 {
317     *Enum_Ref_Par = Enum_Val_Par;
318     if (! Func_3 (Enum_Val_Par))
319         /* then, not executed */
320         *Enum_Ref_Par = Ident_4;
321     switch (Enum_Val_Par)
322         {
323         case Ident_1: 
324             *Enum_Ref_Par = Ident_1;
325             break;
326         case Ident_2: 
327             if (Int_Glob > 100)
328                 /* then */
329                 *Enum_Ref_Par = Ident_1;
330             else *Enum_Ref_Par = Ident_4;
331             break;
332         case Ident_3: /* executed */
333             *Enum_Ref_Par = Ident_2;
334             break;
335         case Ident_4: break;
336         case Ident_5: 
337             *Enum_Ref_Par = Ident_3;
338             break;
339         } /* switch */
340 } /* Proc_6 */
341
342
343 /*************************************************/
344 /* executed three times                                         */
345 /* first call:      Ch_1_Par_Val == 'H', Ch_2_Par_Val == 'R'    */
346 /* second call:     Ch_1_Par_Val == 'A', Ch_2_Par_Val == 'C'    */
347 /* third call:      Ch_1_Par_Val == 'B', Ch_2_Par_Val == 'C'    */
348 Enumeration Func_1 (Capital_Letter Ch_1_Par_Val, Capital_Letter Ch_2_Par_Val)
349 {
350     Capital_Letter        Ch_1_Loc;
351     Capital_Letter        Ch_2_Loc;
352
353     printf("-> Func_1\n");
354     printf("  Inputs: Ch_1_Par_Val '%c' == { H, A, B }\n", (char)Ch_1_Par_Val);
355     printf("          Ch_2_Par_Val '%c' == { R, C, C }\n", (char)Ch_2_Par_Val);
356
357     Ch_1_Loc = Ch_1_Par_Val;
358     Ch_2_Loc = Ch_1_Loc;
359     if (Ch_2_Loc != Ch_2_Par_Val) {
360         /* then, executed */
361         return (Ident_1);
362     }
363     else  /* not executed */
364         {
365             Ch_1_Glob = Ch_1_Loc;
366             return (Ident_2);
367         }
368 } /* Func_1 */
369
370 Boolean Func_3 (Enumeration Enum_Par_Val)
371     /***************************/
372     /* executed once        */
373     /* Enum_Par_Val == Ident_3 */
374 {
375     Enumeration Enum_Loc;
376
377     Enum_Loc = Enum_Par_Val;
378     if (Enum_Loc == Ident_3)
379         /* then, executed */
380         return (true);
381     else /* not executed */
382         return (false);
383 } /* Func_3 */
384
385 void Proc_7 (One_Fifty Int_1_Par_Val, One_Fifty Int_2_Par_Val, One_Fifty *Int_Par_Ref)
386     /**********************************************/
387     /* executed three times                                      */ 
388     /* first call:      Int_1_Par_Val == 2, Int_2_Par_Val == 3,  */
389     /*                  Int_Par_Ref becomes 7                    */
390     /* second call:     Int_1_Par_Val == 10, Int_2_Par_Val == 5, */
391     /*                  Int_Par_Ref becomes 17                   */
392     /* third call:      Int_1_Par_Val == 6, Int_2_Par_Val == 10, */
393     /*                  Int_Par_Ref becomes 18                   */
394 {
395     One_Fifty Int_Loc;
396
397     Int_Loc = Int_1_Par_Val + 2;
398     *Int_Par_Ref = Int_2_Par_Val + Int_Loc;
399 } /* Proc_7 */
400
401 /*void Proc_8 (Arr_1_Dim Arr_1_Par_Ref, Arr_2_Dim Arr_2_Par_Ref, 
402   int Int_1_Par_Val, int Int_2_Par_Val)*/
403 void Proc_8 (int **Arr_1_Par_Ref, int **Arr_2_Par_Ref, 
404              int Int_1_Par_Val, int Int_2_Par_Val)
405 {
406     REG One_Fifty Int_Index;
407     REG One_Fifty Int_Loc;
408
409     Int_Loc = Int_1_Par_Val + 5;
410     Arr_1_Par_Ref [Int_Loc] = Int_2_Par_Val;
411     Arr_1_Par_Ref [Int_Loc+1] = Arr_1_Par_Ref [Int_Loc];
412     Arr_1_Par_Ref [Int_Loc+30] = Int_Loc;
413     for (Int_Index = Int_Loc; Int_Index <= Int_Loc+1; ++Int_Index)
414         Arr_2_Par_Ref [Int_Loc] [Int_Index] = Int_Loc;
415     Arr_2_Par_Ref [Int_Loc] [Int_Loc-1] += 1;
416     Arr_2_Par_Ref [Int_Loc+20] [Int_Loc] = Arr_1_Par_Ref [Int_Loc];
417     Int_Glob = 5;
418 }
419
420 /*********************************************************************/
421 /* executed once      */
422 /* Int_Par_Val_1 == 3 */
423 /* Int_Par_Val_2 == 7 */
424 Boolean Func_2 (char *Str_1_Par_Ref, char *Str_2_Par_Ref)
425     /*************************************************/
426     /* executed once */
427     /* Str_1_Par_Ref == "DHRYSTONE PROGRAM, 1'ST STRING" */
428     /* Str_2_Par_Ref == "DHRYSTONE PROGRAM, 2'ND STRING" */
429 {
430     REG One_Thirty        Int_Loc;
431     Capital_Letter    Ch_Loc;
432
433     printf("-> Func_2\n");
434     printf("  Inputs Str_1_Par_Ref \"%s\" = \"DHRYSTONE PROGRAM, 1'ST STRING\"\n", Str_1_Par_Ref);
435     printf("         Str_2_Par_Ref \"%s\" = \"DHRYSTONE PROGRAM, 2'ND STRING\"\n", Str_2_Par_Ref);
436
437     Int_Loc = 2;
438
439     while (Int_Loc <= 2) {
440 #if BROKEN_SDCC
441         char a, b;
442         printf("  2.1 Runs once Int_Loc %u = 2.\n", (unsigned)Int_Loc);
443         /* loop body executed once */
444         a = Str_1_Par_Ref[Int_Loc];
445         b = Str_2_Par_Ref[Int_Loc+1];
446         if (Func_1 (a, b) == Ident_1)
447 #else
448         if (Func_1 (Str_1_Par_Ref[Int_Loc],
449                     Str_2_Par_Ref[Int_Loc+1]) == Ident_1)
450 #endif
451             /* then, executed */
452             {
453                 printf("  2.3 Then OK.\n");
454                 Ch_Loc = 'A';
455                 Int_Loc += 1;
456             } /* if, while */
457         else {
458             printf("  2.2 Error.\n");
459         }
460     }
461     if (Ch_Loc >= 'W' && Ch_Loc < 'Z')
462         /* then, not executed */
463         Int_Loc = 7;
464     if (Ch_Loc == 'R')
465         /* then, not executed */
466         return (true);
467     else /* executed */
468         {
469             if (strcmp (Str_1_Par_Ref, Str_2_Par_Ref) > 0)
470                 /* then, not executed */
471                 {
472                     Int_Loc += 7;
473                     Int_Glob = Int_Loc;
474                     return (true);
475                 }
476             else /* executed */
477                 return (false);
478         } /* if Ch_Loc */
479 } /* Func_2 */