Wednesday, September 26, 2012

AdaTutor - Advanced Topics (8)


A pragma is a message to the compiler.  The pragmas that are predefined by Ada are all referenced in Annex L of the Ada 95 RM; we'll discuss the most important ones here.  A particular implementation of Ada need not honor all the predefined pragmas, and it may add some of its own.  (One implementation of Ada adds a pragma Time_Slice, used with tasking.)  Unlike representation clauses, unimplemented predefined pragmas do not cause error messages; the compiler simply ignores them.  This enhances program portability.  Any additional pragmas added by a particular implementation of Ada will be explained in the compiler documentation.  The most important predefined pragmas are these:

The statements pragma List(On); and pragma List(Off); turn on and off the compiler listing.  Also, pragma Page; will cause the compiler listing to start a new page, if the listing is turned on.  These pragmas are allowed almost anywhere in the program.

Within the declarative region we can write pragma Optimize(Time); or pragma Optimize(Space); to ask the compiler to optimize the program for minimum execution time or minimum memory usage.

We can write pragma Inline(...); with the name of a subprogram to ask the compiler to write inline code in place of every call to the subprogram.  Even implementations of Ada that honor this pragma will ignore it if the subprogram is recursive.

We can call a subprogram or reference an object written in another langauge with the Ada 95 pragma Import.  This pragma takes three parameters: the name of the language, the Ada name of the subprogram or object, and an optional external linker name for the subprogram or object.  Similarly, a program written in another language can call an Ada program with the Ada 95 pragma Export, taking the same three parameters.  The Ada 95 pragma Convention specifies that objects of a type should be stored using the conventions of another language.  Pragma Convention takes two parameters: the name of the language and the Ada name of the type whose storage convention is being specified.

Ada 83 provides only a pragma Interface.  It is similar to the Ada 95 pragma Import, but is useful only to call a subprogram written in another language, not to reference an object.  Pragma Interface takes only two parameters: the name of the language and the subprogram name.

We can ask the compiler to minimize memory occupied by a record or array by writing, after the type declaration, pragma Pack(...); with the name of the type.  Note that the specification for package Standard (in Annex A.1 of the Ada 95 RM) contains pragma Pack(String); after the definition of type String.

Package System defines a subtype of Integer called Priority.  We can assign a priority to a task by writing, in the specification, pragma Priority(...); with a parameter of subtype System.Priority.  Higher numbers denote greater urgency.

The pragma Suppress can be used to ask the compiler to turn off certain checks, such as Constraint_Error.  It's dangerous and shouldn't be used unless absolutely necessary because of time or memory constraints.


In the author's opinion, which one of these is not dangerous?
  1. Ada.Unchecked_Deallocation
  2. pragma Pack
  3. pragma Suppress

< prev   next >

No comments: