From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c32f54b2230c68d7 X-Google-Attributes: gid103376,public From: Simon Wright Subject: Re: Ada version of C's 'static' Date: 1999/07/20 Message-ID: #1/1 X-Deja-AN: 503253442 X-NNTP-Posting-Host: pogner.demon.co.uk:158.152.70.98 References: <7n03us$862$1@nnrp1.deja.com> X-Trace: news.demon.co.uk 932501564 nnrp-09:9394 NO-IDENT pogner.demon.co.uk:158.152.70.98 Organization: At Home Newsgroups: comp.lang.ada X-Complaints-To: abuse@demon.net Date: 1999-07-20T00:00:00+00:00 List-Id: Craig Allen 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