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