comp.lang.ada
 help / color / mirror / Atom feed
* Code Statement
@ 2010-05-18 13:12 johnjohn
  2010-05-18 13:22 ` Ludovic Brenta
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: johnjohn @ 2010-05-18 13:12 UTC (permalink / raw)


Trying to use the "Code Statement" for a special project, by passing the 
ASM proc/func. So, can Gnat process a true "Code Statement" statement?

When I compile the following example using 
>gnat make temp.adb -gnatpg 
gcc -c -gnatpg temp.adb
temp.adb:10:07: incorrect type for code statement
gnatmake: "temp.adb" compilation error


Source code:

   package Machine_Code is

       type Asm_Insn is new Integer;  --  type required by gant

       x86_NOP    : constant Asm_Insn := 16#90#;

       type M_Code_0  is record
                         Opcode : Asm_Insn;
       end record;
       pragma Pack (M_Code_0);
   end Machine_Code;

Source code:

   with Machine_Code;

   procedure temp is

       procedure Test;

       procedure Test is
          use Machine_Code;
       begin
          M_Code_0'(Opcode => x86_NOP);  -- line 10
       end Test;

   begin
      null;
   end temp;




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

* Re: Code Statement
  2010-05-18 13:12 Code Statement johnjohn
@ 2010-05-18 13:22 ` Ludovic Brenta
  2010-05-18 13:29 ` AdaMagica
  2010-05-18 15:05 ` sjw
  2 siblings, 0 replies; 8+ messages in thread
From: Ludovic Brenta @ 2010-05-18 13:22 UTC (permalink / raw)


johnj...@cox.net wrote on comp.lang.ada:
> Trying to use the "Code Statement" for a special project, by passing the
> ASM proc/func. So, can Gnat process a true "Code Statement" statement?
>
> When I compile the following example using>gnat make temp.adb -gnatpg
>
> gcc -c -gnatpg temp.adb
> temp.adb:10:07: incorrect type for code statement
> gnatmake: "temp.adb" compilation error
>
> Source code:
>
>    package Machine_Code is
>        type Asm_Insn is new Integer;  --  type required by gant
>        x86_NOP    : constant Asm_Insn := 16#90#;
>        type M_Code_0  is record
>                          Opcode : Asm_Insn;
>        end record;
>        pragma Pack (M_Code_0);
>    end Machine_Code;
>
> Source code:
>
>    with Machine_Code;
>    procedure temp is
>        procedure Test;
>        procedure Test is
>           use Machine_Code;
>        begin
>           M_Code_0'(Opcode => x86_NOP);  -- line 10
>        end Test;
>    begin
>       null;
>    end temp;

Line 10 is not a statement, it is a qualified expression and only
statements (and pragmas) are accepted between "begin" and "end".
Machine code insertions are very, very special; they bend the language
rules. If you want to do machine code insertions with GNAT, you must
follow the GNAT rules and use the package System.Machine_Code package
provided by the compiler, not a user-written package.

It seems you are trying to port machine code from the IBM Rational
compiler to GNAT? The only way to do that is by rewriting the machine
code in GNU assembly language. See http://gcc.gnu.org/onlinedocs/gnat_ugn_unw/Inline-Assembler.html

HTH

--
Ludovic Brenta.



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

* Re: Code Statement
  2010-05-18 13:12 Code Statement johnjohn
  2010-05-18 13:22 ` Ludovic Brenta
@ 2010-05-18 13:29 ` AdaMagica
  2010-05-18 15:05 ` sjw
  2 siblings, 0 replies; 8+ messages in thread
From: AdaMagica @ 2010-05-18 13:29 UTC (permalink / raw)


>    package Machine_Code is
>
>        type Asm_Insn is new Integer;  --  type required by gant
>
>        x86_NOP    : constant Asm_Insn := 16#90#;
>
>        type M_Code_0  is record
>                          Opcode : Asm_Insn;
>        end record;
>        pragma Pack (M_Code_0);
>    end Machine_Code;

This is a package that you have written yourself. You must use the
package System.Machine_Code. See RM 13.8.



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

* Re: Code Statement
  2010-05-18 13:12 Code Statement johnjohn
  2010-05-18 13:22 ` Ludovic Brenta
  2010-05-18 13:29 ` AdaMagica
@ 2010-05-18 15:05 ` sjw
  2010-05-19  4:37   ` anon
  2 siblings, 1 reply; 8+ messages in thread
From: sjw @ 2010-05-18 15:05 UTC (permalink / raw)


On May 18, 2:12 pm, johnj...@cox.net wrote:
> Trying to use the "Code Statement" for a special project, by passing the
> ASM proc/func. So, can Gnat process a true "Code Statement" statement?
>
> When I compile the following example using>gnat make temp.adb -gnatpg
>
> gcc -c -gnatpg temp.adb
> temp.adb:10:07: incorrect type for code statement
> gnatmake: "temp.adb" compilation error
>
> Source code:
>
>    package Machine_Code is
>
>        type Asm_Insn is new Integer;  --  type required by gant
>
>        x86_NOP    : constant Asm_Insn := 16#90#;
>
>        type M_Code_0  is record
>                          Opcode : Asm_Insn;
>        end record;
>        pragma Pack (M_Code_0);
>    end Machine_Code;
>
> Source code:
>
>    with Machine_Code;
>
>    procedure temp is
>
>        procedure Test;
>
>        procedure Test is
>           use Machine_Code;
>        begin
>           M_Code_0'(Opcode => x86_NOP);  -- line 10
>        end Test;
>
>    begin
>       null;
>    end temp;

See
http://booch95.svn.sourceforge.net/viewvc/booch95/trunk/src/bc-support-high_resolution_time-clock.adb-pentium?revision=1415&view=markup
for a working i586/x86_64 example.

Best not to use -gnatpg: -gnatp says 'suppress all checks', -gnatg
says 'GNAT implementation mode (used for compiling GNAT units)' -- I'm
pretty sure that that mode is like -Werror.



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

* Re: Code Statement
  2010-05-18 15:05 ` sjw
@ 2010-05-19  4:37   ` anon
  2010-05-19  6:07     ` Simon Wright
  0 siblings, 1 reply; 8+ messages in thread
From: anon @ 2010-05-19  4:37 UTC (permalink / raw)


In <8242a424-5b09-4444-a3ed-7b45e159c46a@z17g2000vbd.googlegroups.com>, sjw <simon.j.wright@mac.com> writes:
>On May 18, 2:12=A0pm, johnj...@cox.net wrote:
>> Trying to use the "Code Statement" for a special project, by passing the
>> ASM proc/func. So, can Gnat process a true "Code Statement" statement?
>>
>> When I compile the following example using>gnat make temp.adb -gnatpg
>>
>> gcc -c -gnatpg temp.adb
>> temp.adb:10:07: incorrect type for code statement
>> gnatmake: "temp.adb" compilation error
>>
>> Source code:
>>
>> =A0 =A0package Machine_Code is
>>
>> =A0 =A0 =A0 =A0type Asm_Insn is new Integer; =A0-- =A0type required by ga=
>nt
>>
>> =A0 =A0 =A0 =A0x86_NOP =A0 =A0: constant Asm_Insn :=3D 16#90#;
>>
>> =A0 =A0 =A0 =A0type M_Code_0 =A0is record
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Opcode : Asm_Insn;
>> =A0 =A0 =A0 =A0end record;
>> =A0 =A0 =A0 =A0pragma Pack (M_Code_0);
>> =A0 =A0end Machine_Code;
>>
>> Source code:
>>
>> =A0 =A0with Machine_Code;
>>
>> =A0 =A0procedure temp is
>>
>> =A0 =A0 =A0 =A0procedure Test;
>>
>> =A0 =A0 =A0 =A0procedure Test is
>> =A0 =A0 =A0 =A0 =A0 use Machine_Code;
>> =A0 =A0 =A0 =A0begin
>> =A0 =A0 =A0 =A0 =A0 M_Code_0'(Opcode =3D> x86_NOP); =A0-- line 10
>> =A0 =A0 =A0 =A0end Test;
>>
>> =A0 =A0begin
>> =A0 =A0 =A0 null;
>> =A0 =A0end temp;
>
>See
>http://booch95.svn.sourceforge.net/viewvc/booch95/trunk/src/bc-support-high=
>_resolution_time-clock.adb-pentium?revision=3D1415&view=3Dmarkup
>for a working i586/x86_64 example.
>
>Best not to use -gnatpg: -gnatp says 'suppress all checks', -gnatg
>says 'GNAT implementation mode (used for compiling GNAT units)' -- I'm
>pretty sure that that mode is like -Werror.




As for the example supplied I decide use the intel "nop" for simplicity
instead of using the codes for the attached processor.

I prefer to use the Code Statement like the System.Machine_Code in SUN, 
instead of using the inline-assembler of gnat\gcc. which forces the 
following type of statements for non-native processors:

    ASM ( " .byte 0x90" ) ; -- intel x86 nop
    ASM ( " .byte 0x02" ) ; -- attach processor instruction "Load R2"
    ...

And using the code statement allows the Ada compiler to emulate a small 
limited assembler without all of the work\time in rewriting\expanding 
the gcc "AS" to include the new processor.

So, I see no problem in using code statement except for creating or 
expand and recompiling the "system.machine_code" package. Unless Adacore 
has crippled gnat's code statement routines.

Now, the RM 13.8 (2), says that the code statement is qualified_expression 
so what the problem.  In Gnat, the ASM routines are handled by the 
"Expand_Asm_Call" procedure ("exp_code" package) which expands the ASM 
statement into a qualified_expression.  Also, "P_Code_Statement" ("par-ch13" 
package) directly processes code_statements as an qualified_expression.  
Afterwards, the "Analyze_Code_Statement" ("sem-ch13" package)  analyzes 
should handle the code statements but this routine is where the error 
message is generated.

Used "-gnatpg" since machine_code is a gnat system package.



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

* Re: Code Statement
  2010-05-19  4:37   ` anon
@ 2010-05-19  6:07     ` Simon Wright
  2010-05-19  9:56       ` sjw
  0 siblings, 1 reply; 8+ messages in thread
From: Simon Wright @ 2010-05-19  6:07 UTC (permalink / raw)


anon@att.net writes:

> As for the example supplied I decide use the intel "nop" for simplicity
> instead of using the codes for the attached processor.

Ah! I see what you meant by "special project".

> I prefer to use the Code Statement like the System.Machine_Code in SUN, 
> instead of using the inline-assembler of gnat\gcc. which forces the 
> following type of statements for non-native processors:
>
>     ASM ( " .byte 0x90" ) ; -- intel x86 nop
>     ASM ( " .byte 0x02" ) ; -- attach processor instruction "Load R2"
>     ...
>
> And using the code statement allows the Ada compiler to emulate a small 
> limited assembler without all of the work\time in rewriting\expanding 
> the gcc "AS" to include the new processor.
>
> So, I see no problem in using code statement except for creating or 
> expand and recompiling the "system.machine_code" package. Unless Adacore 
> has crippled gnat's code statement routines.

I'm not sure what http://www.adaic.com/standards/05rm/html/RM-13-8.html
(2) means; does it give you a licence to use any type you lke? seems
unlikely.

> Now, the RM 13.8 (2), says that the code statement is qualified_expression 
> so what the problem.  In Gnat, the ASM routines are handled by the 
> "Expand_Asm_Call" procedure ("exp_code" package) which expands the ASM 
> statement into a qualified_expression.  Also, "P_Code_Statement" ("par-ch13" 
> package) directly processes code_statements as an qualified_expression.  
> Afterwards, the "Analyze_Code_Statement" ("sem-ch13" package)  analyzes 
> should handle the code statements but this routine is where the error 
> message is generated.

I expect the compiler is written to expect System.Machine_Code.Asm_Insn
of the specific type defined in System.Machine_Code.

You could write
   Asm_Insn'(Asm (".byte 0x90"));
but it hardly seems worth the trouble. AdaCore have provided this as a 
"feature" I think -- see the GNAT RM, 
http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gnat_rm/Machine-Code-Insertions.html



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

* Re: Code Statement
  2010-05-19  6:07     ` Simon Wright
@ 2010-05-19  9:56       ` sjw
  2010-05-19 10:20         ` AdaMagica
  0 siblings, 1 reply; 8+ messages in thread
From: sjw @ 2010-05-19  9:56 UTC (permalink / raw)


On May 19, 7:07 am, Simon Wright <si...@pushface.org> wrote:

> I'm not sure what http://www.adaic.com/standards/05rm/html/RM-13-8.html
> (2) means; does it give you a licence to use any type you lke? seems
> unlikely.

Sorry, I meant (4): "The qualified_expression is expected to be of any
type."

Compare with (5): "The qualified_expression shall be of a type
declared in package System.Machine_Code."




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

* Re: Code Statement
  2010-05-19  9:56       ` sjw
@ 2010-05-19 10:20         ` AdaMagica
  0 siblings, 0 replies; 8+ messages in thread
From: AdaMagica @ 2010-05-19 10:20 UTC (permalink / raw)


On 19 Mai, 11:56, sjw <simon.j.wri...@mac.com> wrote:
> On May 19, 7:07 am, Simon Wright <si...@pushface.org> wrote:
>
> > I'm not sure whathttp://www.adaic.com/standards/05rm/html/RM-13-8.html
> > (2) means; does it give you a licence to use any type you lke? seems
> > unlikely.
>
> Sorry, I meant (4): "The qualified_expression is expected to be of any
> type."

This is a Name Resolution Rule used for overload resolution. As far as
overloading is concernded, any type can do.

> Compare with (5): "The qualified_expression shall be of a type
> declared in package System.Machine_Code."

This is a Legality Rule and applied only after overload resolution. So
this is where code becomes illegal when other types are used.



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

end of thread, other threads:[~2010-05-19 10:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-18 13:12 Code Statement johnjohn
2010-05-18 13:22 ` Ludovic Brenta
2010-05-18 13:29 ` AdaMagica
2010-05-18 15:05 ` sjw
2010-05-19  4:37   ` anon
2010-05-19  6:07     ` Simon Wright
2010-05-19  9:56       ` sjw
2010-05-19 10:20         ` AdaMagica

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