Appendix A. Examples of Generated Code

Table of Contents
A.1. The Sieve of Eratosthenes
A.2. Ackermann's Function

In this chapter we present examples of code generated by the Version 1.7 compiler.

A.1. The Sieve of Eratosthenes

Compiler writers use the Sieve of Eratosthenes benchmark to check code quality and to compare run-time performance among compilers, languages and computers.

The benchmark uses the sieve method to compute the number of odd primes between 3 and 16383.

Example A-1. Source Code for Sieve


procedure Sieve (Result : out Integer) is
   Size : constant := 8190;
   k, Prime : Natural;
   Count : Integer;

   type Ftype is array (0 .. Size) of Boolean;
   Flags : Ftype;
begin
   for Iter in 1 .. 10 loop
      Count := 0;

      for i in 0 .. Size loop
         Flags (i) := True;
      end loop;

      for i in 0 .. Size loop
         if Flags (i) then
            Prime := i + i + 3;
            k := i + Prime;
            while k <= Size loop
               Flags (k) := False;
               k := k + Prime;
            end loop;
            Count := Count + 1;
         end if;
      end loop;
   end loop;

   Result := Count;
end Sieve;

The generated code is given in Example A-2. The code was generated at optimization level 2 with checks suppressed.

Example A-2. Generated Code for Sieve


   1                            .file  "sieve.adb"
   2                    gcc2_compiled.:
   3                    __gnu_compiled_ada:
   4                            .text
   5                            .align  4
   6                            .global _ada_sieve
   7                            .proc   04
   8                    _ada_sieve:
   9 0000 9DE3BF98              save    %sp,-104,%sp
  10 0004 80A38007              cmp     %sp,%g7
  11 0008 89D02009              tleu    9
  12 000c 11000008              sethi   %hi(8198),%o0
  13 0010 90122006              or      %o0,%lo(8198),%o0
  14 0014 90022063              add     %o0,99,%o0
  15 0018 900A3FF8              and     %o0,-8,%o0
  16 001c 90238008              sub     %sp,%o0,%o0
  17 0020 80A20007              cmp     %o0,%g7
  18 0024 89D02009              tleu    9
  19 0028 9C100008              mov     %o0,%sp
  20 002c 9A03A060              add     %sp,96,%o5
  21 0030 84102001              mov     1,%g2
  22 0034 11000007              sethi   %hi(8190),%o0
  23 0038 981223FE              or      %o0,%lo(8190),%o4
  24 003c 86100002              mov     %g2,%g3
  25 0040 B0102000              mov     0,%i0
  26                    .L33:
  27 0044 90100018              mov     %i0,%o0
  28 0048 C62B4008              stb     %g3,[%o5+%o0]
  29                    .L29:
  30 004c 90022001              add     %o0,1,%o0
  31 0050 80A2000C              cmp     %o0,%o4
  32 0054 24BFFFFE              ble,a   .L29
  33 0058 C62B4008              stb     %g3,[%o5+%o0]
  34 005c 96102000              mov     0,%o3
  35 0060 D00B400B              ldub    [%o5+%o3],%o0
  36                    .L32:
  37 0064 80A22000              cmp     %o0,0
  38 0068 22800012              be,a    .L30
  39 006c 9602E001              add     %o3,1,%o3
  40 0070 912AE001              sll     %o3,1,%o0
  41 0074 90022003              add     %o0,3,%o0
  42 0078 9202C008              add     %o3,%o0,%o1
  43 007c 80A2400C              cmp     %o1,%o4
  44 0080 3480000B              bg,a    .L31
  45 0084 B0062001              add     %i0,1,%i0
  46 0088 80A2400C              cmp     %o1,%o4
  47                    .L34:
  48 008c 08800003              bleu    .L21
  49 0090 94100009              mov     %o1,%o2
  50 0094 91D02005              ta      5
  51                    .L21:
  52 0098 92024008              add     %o1,%o0,%o1
  53 009c 80A2400C              cmp     %o1,%o4
  54 00a0 04BFFFFB              ble     .L34
  55 00a4 C02B400A              stb     %g0,[%o5+%o2]
  56 00a8 B0062001              add     %i0,1,%i0
  57                    .L31:
  58 00ac 9602E001              add     %o3,1,%o3
  59                    .L30:
  60 00b0 80A2C00C              cmp     %o3,%o4
  61 00b4 24BFFFEC              ble,a   .L32
  62 00b8 D00B400B              ldub    [%o5+%o3],%o0
  63 00bc 8400A001              add     %g2,1,%g2
  64 00c0 80A0A00A              cmp     %g2,10
  65 00c4 24BFFFE0              ble,a   .L33
  66 00c8 B0102000              mov     0,%i0
  67 00cc 81C7E008              ret
  68 00d0 81E80000              restore