From: Simon Wright <simon@pogner.demon.co.uk>
Subject: Re: Ada version of C's 'static'
Date: 1999/07/20
Date: 1999-07-20T00:00:00+00:00 [thread overview]
Message-ID: <x7vvhbfkt04.fsf@pogner.moho> (raw)
In-Reply-To: 7n03us$862$1@nnrp1.deja.com
Craig Allen <cpallen@my-deja.com> writes:
> I've written some code in a procedure that operates on a table of
> constants. The table is used only by that procedure. Right now, I have
> that table defined locally to that procedure. I believe the table is
> regenerated every time the procedure is called.
>
> In C, I'd declare this table static in that function. This would give
> me static duration and block scope for that function.
>
> I don't want to define this table at 'package scope', as only 1
> procedure uses this table (and the values would be removed from the
> code that uses them). I would prefer not to build the table at
> elaboration time, because I'd like the definition to be close to the
> code that's using it, not at the bottom of the package.
>
> In short, this is exactly what I want, except in Ada.
>
> void foo(void)
> {
> static const int bar[] = {1, 2, 3, 4, 5};
> ...
> }
>
> Or is there a 'more Ada' way of doing this?
You can do pretty much exactly that in Ada. I've made it into a
function and filled in the content so that optimisation won't remove
the entire contents ..
function Foo (Input : Integer) return Integer is
type Integer_Array is array (Natural range <>) of Integer;
Bar : constant Integer_Array := (1, 2, 3, 4, 5);
begin
if Input in Bar'Range then
return Bar (Input);
else
return -1;
end if;
end Foo;
cf
int foo(int input)
{
static const int bar[] = {1, 2, 3, 4, 5};
if (input >= 0 && input < sizeof(bar) / sizeof(bar[0]))
return bar[input];
else
return -1;
}
Using gcc -O3 -S gives (Ada on the left, C on the right)
[i486-pc-linux-gnulibc1, gnat 3.11p, gcc 2.8.1]
.file "foo.adb" .file "foo.c"
.version "01.01" .version "01.01"
gcc2_compiled.: gcc2_compiled.:
.section .rodata .section .rodata
.align 4 .align 4
.type bar.0,@object .type bar.2,@object
.size bar.0,20 bar.2:
bar.0: .long 1
.long 1 .long 2
.long 2 .long 3
.long 3 .long 4
.long 4 .long 5
.long 5 .text
.text .align 16
.align 16 .globl foo
.globl _ada_foo .type foo,@function
.type _ada_foo,@function foo:
_ada_foo: pushl %ebp
pushl %ebp movl %esp,%ebp
movl %esp,%ebp movl 8(%ebp),%eax
movl 8(%ebp),%eax cmpl $4,%eax
cmpl $4,%eax jbe .L2
ja .L2 movl $-1,%eax
movl bar.0(,%eax,4),%eax jmp .L4
jmp .L6 .align 16
.align 16 .L2:
.L2: movl bar.2(,%eax,4),%eax
movl $-1,%eax .L4:
.L6: movl %ebp,%esp
movl %ebp,%esp popl %ebp
popl %ebp ret
ret .Lfe1:
.Lfe1: .size foo,.Lfe1-foo
.size _ada_foo,.Lfe1-_ada_foo .ident "GCC: (GNU) 2.8.1"
.ident "GCC: (GNU) 2.8.1"
which is pretty close!
-Simon
next prev parent reply other threads:[~1999-07-20 0:00 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-07-19 0:00 Ada version of C's 'static' Craig Allen
1999-07-19 0:00 ` David Botton
1999-07-20 0:00 ` Simon Wright [this message]
1999-07-23 0:00 ` Tucker Taft
1999-07-30 0:00 ` Craig Allen
1999-08-03 0:00 ` Compilers for VAX Was: " Erdelyi Gaspar
1999-08-03 0:00 ` Larry Kilgallen
1999-08-04 0:00 ` Steve Doiel
1999-08-05 0:00 ` Robert Dewar
1999-08-05 0:00 ` Larry Kilgallen
1999-08-06 0:00 ` Robert A Duff
1999-08-06 0:00 ` Larry Kilgallen
1999-08-09 0:00 ` Robert A Duff
1999-08-09 0:00 ` Richard D Riehle
1999-08-09 0:00 ` Tucker Taft
1999-08-10 0:00 ` Robert A Duff
1999-08-08 0:00 ` Robert Dewar
1999-08-04 0:00 ` Robert Dewar
1999-08-04 0:00 ` Rod Chapman
1999-08-04 0:00 ` Larry Kilgallen
1999-08-04 0:00 ` Robert Dewar
1999-08-04 0:00 ` Marin David Condic
1999-08-04 0:00 ` Robert Dewar
1999-08-04 0:00 ` Larry Kilgallen
1999-08-04 0:00 ` Robert Dewar
1999-08-04 0:00 ` Chris Miller
1999-08-05 0:00 ` Robert Dewar
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox