comp.lang.ada
 help / color / mirror / Atom feed
* Re: RISC - largish (code listed)
  2001-03-20 15:45                     ` RISC Ted Dennison
@ 2001-03-22  9:16                       ` Martin Dowie
  2001-03-22  9:34                         ` Martin Dowie
  0 siblings, 1 reply; 5+ messages in thread
From: Martin Dowie @ 2001-03-22  9:16 UTC (permalink / raw)


Something like this? Seems _well_ dangerous but also seems to produce very
little code...

Ted Dennison <dennison@telepath.com> wrote in message
news:LWKt6.1865$94.2578@www.newsranger.com...
> That's why if the object is largish I typically use unchecked_conversion
on
> pointers to the object instead. Depending on the smarts of the compiler,
that
> might not generate any code at all.
>
> I don't like "for use at" overlays. For one thing, initializations get
(re)done
> when the overlay happens, which could wipe out important stuff. For
another, the
> aliasing is (generally) given a much wider scope than is achievable with
> unchecked conversion.


-- Unchecked_Accessors Spec:
with System;

generic
   type Object1 (<>) is limited private;
   type Object1_Pointer is access all Object1;
   with function To_Object1_Pointer (Address : System.Address) return
Object1_Pointer;
   with function To_Object1_Address (Obj1_Ptr : Object1_Pointer) return
System.Address;

   type Object2 (<>) is limited private;
   type Object2_Pointer is access all Object2;
   with function To_Object2_Pointer (Address : System.Address) return
Object2_Pointer;
   with function To_Object2_Address (Obj2_Ptr : Object2_Pointer) return
System.Address;
package Unchecked_Accessors is
   function To_Object1_Pointer (Obj2_Ptr : Object2_Pointer) return
Object1_Pointer;
   function To_Object2_Pointer (Obj1_Ptr : Object1_Pointer) return
Object2_Pointer;
   pragma Inline (To_Object1_Pointer);
   pragma Inline (To_Object2_Pointer);
end Unchecked_Accessors;


-- Unchecked_Accessors Body:
package body Unchecked_Accessors is

   function To_Object1_Pointer (Obj2_Ptr : Object2_Pointer) return
Object1_Pointer is
   begin
       return To_Object1_Pointer (Address => To_Object2_Address (Obj2_Ptr =>
Obj2_Ptr));
   end To_Object1_Pointer;

   function To_Object2_Pointer (Obj1_Ptr : Object1_Pointer) return
Object2_Pointer is
   begin
       return To_Object2_Pointer (Address => To_Object1_Address (Obj1_Ptr =>
Obj1_Ptr));
   end To_Object2_Pointer;

end Unchecked_Accessors;


-- Test Harness:
with Ada.Text_IO; use Ada.Text_IO;
with System.Address_To_Access_Conversions;
with Unchecked_Accessors;

procedure Test_Unchecked_Access_Conversion is

  type A_Record (Variant : Boolean := False) is record
    Item1 : Integer;
    case Variant is
      when False =>
        Item2 : Float;
      when True =>
        Item3 : Boolean;
        Item4 : String (1 .. 10);
      end case;
  end record;

  package Record_Conversions is
    new System.Address_To_Access_Conversions (Object => A_Record);

  My_Object : aliased A_Record := (Variant => True,
                                   Item1 => Integer'Last,
                                   Item3 => True,
                                   Item4 => "0123456789");

  type A_String is new String (1 .. My_Object'Size / 8);

  package String_Conversions is
    new System.Address_To_Access_Conversions (Object => A_String);

  package My_Conversions is
    new Unchecked_Accessors (Object1 => A_Record,
                             Object1_Pointer =>
Record_Conversions.Object_Pointer,
                             To_Object1_Pointer =>
Record_Conversions.To_Pointer,
                             To_Object1_Address =>
Record_Conversions.To_Address,

                             Object2 => A_String,
                             Object2_Pointer =>
String_Conversions.Object_Pointer,
                             To_Object2_Pointer =>
String_Conversions.To_Pointer,
                             To_Object2_Address =>
String_Conversions.To_Address);


  My_String_Access : String_Conversions.Object_Pointer;

begin
  My_String_Access := My_Conversions.To_Object2_Pointer (Obj1_Ptr =>
My_Object'Access);
  Put_Line (String (My_String_Access.all));
end Test_Unchecked_Access_Conversion;


Assempler for Unchecked_Accessors:
 .file "unchecked_accessors.adb"
gcc2_compiled.:
___gnu_compiled_ada:
.stabs "W:/Personal/Ada/Utilities/",100,0,0,Ltext0
.stabs "w:/personal/ada/utilit~1/unchecked_accessors.adb",100,0,0,Ltext0
.text
Ltext0:
.stabs "long int:t(0,1)=r(0,1);-2147483648;2147483647;",128,0,0,0
.stabs "unsigned char:t(0,2)=@s8;r(0,2);0;255;",128,0,0,0
.stabs "boolean___XDLU_0__1:t(0,3)=@s8;efalse:0,true:1,;",128,0,1,0
.stabs "character:t(0,4)=@s8;r(0,4);0;255;",128,0,1,0
.stabs "natural___XDLU_0__2147483647:t(0,5)=r(0,1);0;2147483647;",128,0,1,0
.stabs "access_character:t(0,6)=*(0,4)",128,0,1,0
.stabs "integer:t(0,7)=r(0,1);-2147483648;2147483647;",128,0,1,0
.stabs
"exception:T(0,8)=s20not_handled_by_others:(0,3),0,8;c1:(0,4),8,8;c2:(0,4),1
6,8;c3:(0,4),24,8;name_length:(0,5),32,32;full_name:(0,6),64,32;htable_ptr:(
0,6),96,32;import_code:(0,7),128,32;;",128,0,0,0
.stabs "exception:t(0,8)",128,0,1,0
.stabs "long_long_float:t(0,9)=r(0,1);12;0;",128,0,1,0
.globl _unchecked_accessors_E
.data
.stabs "unchecked_accessors_E:G(0,3)",32,0,13,0
_unchecked_accessors_E:
 .byte 0
.text
 .align 4
.stabs
"unchecked_accessors___elabb:F(0,10)=(0,10)",36,0,1,_unchecked_accessors___e
labb
.globl _unchecked_accessors___elabb
_unchecked_accessors___elabb:
.stabn 68,0,1,LM1-_unchecked_accessors___elabb
LM1:
 pushl %ebp
 movl %esp,%ebp
.stabn 68,0,1,LM2-_unchecked_accessors___elabb
LM2:
 movb $1,_unchecked_accessors_E
.stabn 68,0,1,LM3-_unchecked_accessors___elabb
LM3:
L1:
 movl %ebp,%esp
 popl %ebp
 ret
Lscope0:
.stabs "",36,0,0,Lscope0-_unchecked_accessors___elabb
 .text
 .stabs "",100,0,0,Letext
Letext:


Test Results:
?      ??0123456789







^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: RISC - largish (code listed)
  2001-03-22  9:16                       ` RISC - largish (code listed) Martin Dowie
@ 2001-03-22  9:34                         ` Martin Dowie
  0 siblings, 0 replies; 5+ messages in thread
From: Martin Dowie @ 2001-03-22  9:34 UTC (permalink / raw)


oops forgot the assembler for the instantiation.. still not a lot extra for
these 2 routines.

Martin Dowie <martin.dowie@baesystems.com> wrote in message
news:3ab9c0e9$1@pull.gecm.com...
> Something like this? Seems _well_ dangerous but also seems to produce very
> little code...
>
> Ted Dennison <dennison@telepath.com> wrote in message
> news:LWKt6.1865$94.2578@www.newsranger.com...
> > That's why if the object is largish I typically use unchecked_conversion
> on
> > pointers to the object instead. Depending on the smarts of the compiler,
> that
> > might not generate any code at all.
> >
> > I don't like "for use at" overlays. For one thing, initializations get
> (re)done
> > when the overlay happens, which could wipe out important stuff. For
> another, the
> > aliasing is (generally) given a much wider scope than is achievable with
> > unchecked conversion.

Ltext2:
.stabn
68,0,3,LM19-_test_unchecked_access_conversion__my_conversions__to_object1_po
inter$2.8
LM19:
 pushl %ebp
 movl %esp,%ebp
 subl $4,%esp
 movl %ecx,-4(%ebp)
.stabn
68,0,5,LM20-_test_unchecked_access_conversion__my_conversions__to_object1_po
inter$2.8
LM20:
 movl 8(%ebp),%eax
 pushl %eax
 movl -4(%ebp),%ecx
 call _test_unchecked_access_conversion__string_conversions__to_address.7
 addl $4,%esp
 movl %eax,%eax
 pushl %eax
 movl -4(%ebp),%ecx
 call _test_unchecked_access_conversion__record_conversions__to_pointer.4
 addl $4,%esp
 movl %eax,%edx
 movl %edx,%eax
 jmp L10
 .align 2,0x90
.stabn
68,0,5,LM21-_test_unchecked_access_conversion__my_conversions__to_object1_po
inter$2.8
LM21:
L10:
 movl %ebp,%esp
 popl %ebp
 ret
Lscope4:
.stabs
"",36,0,0,Lscope4-_test_unchecked_access_conversion__my_conversions__to_obje
ct1_pointer$2.8
 .align 4
.stabs
"test_unchecked_access_conversion__my_conversions__to_object2_pointer$2:f(0,
46),test_unchecked_access_conversion__my_conversions__to_object2_pointer$2,t
est_unchecked_access_conversion",36,0,8,_test_unchecked_access_conversion__m
y_conversions__to_object2_pointer$2.9
.stabs "obj1_ptr:p(0,29)",160,0,15,8
_test_unchecked_access_conversion__my_conversions__to_object2_pointer$2.9:
.stabn
68,0,8,LM22-_test_unchecked_access_conversion__my_conversions__to_object2_po
inter$2.9
LM22:
 pushl %ebp
 movl %esp,%ebp
 subl $4,%esp
 movl %ecx,-4(%ebp)
.stabn
68,0,10,LM23-_test_unchecked_access_conversion__my_conversions__to_object2_p
ointer$2.9
LM23:
 movl 8(%ebp),%eax
 pushl %eax
 movl -4(%ebp),%ecx
 call _test_unchecked_access_conversion__record_conversions__to_address.5
 addl $4,%esp
 movl %eax,%eax
 pushl %eax
 movl -4(%ebp),%ecx
 call _test_unchecked_access_conversion__string_conversions__to_pointer.6
 addl $4,%esp
 movl %eax,%edx
 movl %edx,%eax
 jmp L11
 .align 2,0x90
.stabn
68,0,10,LM24-_test_unchecked_access_conversion__my_conversions__to_object2_p
ointer$2.9
LM24:
L11:
 movl %ebp,%esp
 popl %ebp
 ret






^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: RISC - largish (code listed)
@ 2001-03-22 18:31 Beard, Frank
  2001-03-23  8:36 ` Martin Dowie
  0 siblings, 1 reply; 5+ messages in thread
From: Beard, Frank @ 2001-03-22 18:31 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

Any particular reason you don't use "System.Address_To_Access_Conversions"?

Frank

-----Original Message-----
From: Martin Dowie [mailto:martin.dowie@baesystems.com]
Sent: Thursday, March 22, 2001 4:16 AM
To: comp.lang.ada@ada.eu.org
Subject: Re: RISC - largish (code listed)


Something like this? Seems _well_ dangerous but also seems to produce very
little code...

Ted Dennison <dennison@telepath.com> wrote in message
news:LWKt6.1865$94.2578@www.newsranger.com...
> That's why if the object is largish I typically use unchecked_conversion
on
> pointers to the object instead. Depending on the smarts of the compiler,
that
> might not generate any code at all.
>
> I don't like "for use at" overlays. For one thing, initializations get
(re)done
> when the overlay happens, which could wipe out important stuff. For
another, the
> aliasing is (generally) given a much wider scope than is achievable with
> unchecked conversion.


-- Unchecked_Accessors Spec:
with System;

generic
   type Object1 (<>) is limited private;
   type Object1_Pointer is access all Object1;
   with function To_Object1_Pointer (Address : System.Address) return
Object1_Pointer;
   with function To_Object1_Address (Obj1_Ptr : Object1_Pointer) return
System.Address;

   type Object2 (<>) is limited private;
   type Object2_Pointer is access all Object2;
   with function To_Object2_Pointer (Address : System.Address) return
Object2_Pointer;
   with function To_Object2_Address (Obj2_Ptr : Object2_Pointer) return
System.Address;
package Unchecked_Accessors is
   function To_Object1_Pointer (Obj2_Ptr : Object2_Pointer) return
Object1_Pointer;
   function To_Object2_Pointer (Obj1_Ptr : Object1_Pointer) return
Object2_Pointer;
   pragma Inline (To_Object1_Pointer);
   pragma Inline (To_Object2_Pointer);
end Unchecked_Accessors;


-- Unchecked_Accessors Body:
package body Unchecked_Accessors is

   function To_Object1_Pointer (Obj2_Ptr : Object2_Pointer) return
Object1_Pointer is
   begin
       return To_Object1_Pointer (Address => To_Object2_Address (Obj2_Ptr =>
Obj2_Ptr));
   end To_Object1_Pointer;

   function To_Object2_Pointer (Obj1_Ptr : Object1_Pointer) return
Object2_Pointer is
   begin
       return To_Object2_Pointer (Address => To_Object1_Address (Obj1_Ptr =>
Obj1_Ptr));
   end To_Object2_Pointer;

end Unchecked_Accessors;


-- Test Harness:
with Ada.Text_IO; use Ada.Text_IO;
with System.Address_To_Access_Conversions;
with Unchecked_Accessors;

procedure Test_Unchecked_Access_Conversion is

  type A_Record (Variant : Boolean := False) is record
    Item1 : Integer;
    case Variant is
      when False =>
        Item2 : Float;
      when True =>
        Item3 : Boolean;
        Item4 : String (1 .. 10);
      end case;
  end record;

  package Record_Conversions is
    new System.Address_To_Access_Conversions (Object => A_Record);

  My_Object : aliased A_Record := (Variant => True,
                                   Item1 => Integer'Last,
                                   Item3 => True,
                                   Item4 => "0123456789");

  type A_String is new String (1 .. My_Object'Size / 8);

  package String_Conversions is
    new System.Address_To_Access_Conversions (Object => A_String);

  package My_Conversions is
    new Unchecked_Accessors (Object1 => A_Record,
                             Object1_Pointer =>
Record_Conversions.Object_Pointer,
                             To_Object1_Pointer =>
Record_Conversions.To_Pointer,
                             To_Object1_Address =>
Record_Conversions.To_Address,

                             Object2 => A_String,
                             Object2_Pointer =>
String_Conversions.Object_Pointer,
                             To_Object2_Pointer =>
String_Conversions.To_Pointer,
                             To_Object2_Address =>
String_Conversions.To_Address);


  My_String_Access : String_Conversions.Object_Pointer;

begin
  My_String_Access := My_Conversions.To_Object2_Pointer (Obj1_Ptr =>
My_Object'Access);
  Put_Line (String (My_String_Access.all));
end Test_Unchecked_Access_Conversion;


Assempler for Unchecked_Accessors:
 .file "unchecked_accessors.adb"
gcc2_compiled.:
___gnu_compiled_ada:
.stabs "W:/Personal/Ada/Utilities/",100,0,0,Ltext0
.stabs "w:/personal/ada/utilit~1/unchecked_accessors.adb",100,0,0,Ltext0
.text
Ltext0:
.stabs "long int:t(0,1)=r(0,1);-2147483648;2147483647;",128,0,0,0
.stabs "unsigned char:t(0,2)=@s8;r(0,2);0;255;",128,0,0,0
.stabs "boolean___XDLU_0__1:t(0,3)=@s8;efalse:0,true:1,;",128,0,1,0
.stabs "character:t(0,4)=@s8;r(0,4);0;255;",128,0,1,0
.stabs "natural___XDLU_0__2147483647:t(0,5)=r(0,1);0;2147483647;",128,0,1,0
.stabs "access_character:t(0,6)=*(0,4)",128,0,1,0
.stabs "integer:t(0,7)=r(0,1);-2147483648;2147483647;",128,0,1,0
.stabs
"exception:T(0,8)=s20not_handled_by_others:(0,3),0,8;c1:(0,4),8,8;c2:(0,4),1
6,8;c3:(0,4),24,8;name_length:(0,5),32,32;full_name:(0,6),64,32;htable_ptr:(
0,6),96,32;import_code:(0,7),128,32;;",128,0,0,0
.stabs "exception:t(0,8)",128,0,1,0
.stabs "long_long_float:t(0,9)=r(0,1);12;0;",128,0,1,0
.globl _unchecked_accessors_E
.data
.stabs "unchecked_accessors_E:G(0,3)",32,0,13,0
_unchecked_accessors_E:
 .byte 0
.text
 .align 4
.stabs
"unchecked_accessors___elabb:F(0,10)=(0,10)",36,0,1,_unchecked_accessors___e
labb
.globl _unchecked_accessors___elabb
_unchecked_accessors___elabb:
.stabn 68,0,1,LM1-_unchecked_accessors___elabb
LM1:
 pushl %ebp
 movl %esp,%ebp
.stabn 68,0,1,LM2-_unchecked_accessors___elabb
LM2:
 movb $1,_unchecked_accessors_E
.stabn 68,0,1,LM3-_unchecked_accessors___elabb
LM3:
L1:
 movl %ebp,%esp
 popl %ebp
 ret
Lscope0:
.stabs "",36,0,0,Lscope0-_unchecked_accessors___elabb
 .text
 .stabs "",100,0,0,Letext
Letext:


Test Results:
?      ??0123456789




_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: RISC - largish (code listed)
  2001-03-22 18:31 RISC - largish (code listed) Beard, Frank
@ 2001-03-23  8:36 ` Martin Dowie
  2001-03-23 12:21   ` David C. Hoos, Sr.
  0 siblings, 1 reply; 5+ messages in thread
From: Martin Dowie @ 2001-03-23  8:36 UTC (permalink / raw)


Beard, Frank <beardf@spawar.navy.mil> wrote in message
news:mailman.985286058.25350.comp.lang.ada@ada.eu.org...
> Any particular reason you don't use
"System.Address_To_Access_Conversions"?

we do - in the body. This package converts one access type to another access
type by converting to system.address on route. So you can do dangerous
things
in a type safe manner.

If there is a simpler way of doing this (type safely) then please let me
know! :-)







^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: RISC - largish (code listed)
  2001-03-23  8:36 ` Martin Dowie
@ 2001-03-23 12:21   ` David C. Hoos, Sr.
  0 siblings, 0 replies; 5+ messages in thread
From: David C. Hoos, Sr. @ 2001-03-23 12:21 UTC (permalink / raw)



"Martin Dowie" <martin.dowie@baesystems.com> wrote in message
news:3abb0937$1@pull.gecm.com...
> Beard, Frank <beardf@spawar.navy.mil> wrote in message
> news:mailman.985286058.25350.comp.lang.ada@ada.eu.org...
> > Any particular reason you don't use
> "System.Address_To_Access_Conversions"?
>
> we do - in the body. This package converts one access type to another
access
> type by converting to system.address on route. So you can do dangerous
> things
> in a type safe manner.
>
> If there is a simpler way of doing this (type safely) then please let me
> know! :-)

As a matter of good style (regardless of simplicity) it's always a good idea
to
hide implementation details from the specification, even when the
implementation is only an instantiation of a language-defined generic unit.
This allows refinement of the implementation without changing the
specification and thereby forcing recompilation of all of the client units.









^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2001-03-23 12:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-22 18:31 RISC - largish (code listed) Beard, Frank
2001-03-23  8:36 ` Martin Dowie
2001-03-23 12:21   ` David C. Hoos, Sr.
  -- strict thread matches above, loose matches on Subject: below --
2001-03-14 20:23 RISC chris.danx
2001-03-15 12:37 ` RISC chris.danx
2001-03-15 14:40   ` RISC Ted Dennison
2001-03-15 14:49     ` RISC Robert A Duff
2001-03-15 17:37       ` RISC Marin David Condic
2001-03-15 18:28         ` RISC Robert A Duff
2001-03-15 19:16           ` RISC Marin David Condic
2001-03-16  8:44             ` RISC Martin Dowie
2001-03-16 14:40               ` RISC Marin David Condic
2001-03-20 10:17                 ` RISC Martin Dowie
2001-03-20 14:34                   ` RISC Marin David Condic
2001-03-20 15:45                     ` RISC Ted Dennison
2001-03-22  9:16                       ` RISC - largish (code listed) Martin Dowie
2001-03-22  9:34                         ` Martin Dowie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox