[ Pobierz całość w formacie PDF ]
put32( u32 ); stdout.newln();
put32( i32 ); stdout.newln();
Beta Draft - Do not distribute © 2001, By Randall Hyde Page 993
Chapter Eight Volume Four
end put32Demo;
Program 8.8 Procedure Overloading Based on Operand Type
You can easily extend the macro above to output eight and sixteen-bit operands as well as 32-bit operands.
That is left as an exercise.
The number of actual parameters is another way to resolve which overloaded procedure to call. If you
specify a variable number of macro parameters (using the "[ ]" syntax, see Macros with a Variable Number
of Parameters on page 974) you can use the @ELEMENTS compile-time function to determine exactly
how many parameters are present and call the appropriate routine. The following sample program uses this
trick to determine whether it should call stdout.puti32 or stdout.puti32Size:
// puti32.hla
//
// This program demonstrates procedure overloading via macros.
//
// It defines a "puti32" macro that calls stdout.puti32 or stdout.puti32size
// depending on the number of parameters present.
program puti32Demo;
#include( "stdlib.hhf" )
// puti32-
//
// Automatically decides whether we have an int32, uns32, or dword
// operand and calls the appropriate stdout.putX routine to
// output this value.
macro puti32( operand[] );
// If we have a single operand, call stdout.puti32:
#if( @elements( operand ) = 1 )
stdout.puti32( @text(operand[0]) );
// If we have two operands, call stdout.puti32size and
// supply a default value of ' ' for the padding character:
#elseif( @elements( operand ) = 2 )
stdout.puti32Size( @text(operand[0]), @text(operand[1]), ' ' );
// If we have three parameters, then pass all three of them
// along to puti32size:
#elseif( @elements( operand ) = 3 )
stdout.puti32Size
(
@text(operand[0]),
Page 994 © 2001, By Randall Hyde Beta Draft - Do not distribute
The HLA Compile-Time Language
@text(operand[1]),
@text(operand[2])
);
// If we don't have one, two, or three operands, report an error:
#else
#error( "Expected one, two, or three operands" )
#endif
endmacro;
// A sample variable declaration so we can test the macro above.
static
i32: int32 := -32;
begin puti32Demo;
// Demo the put32 macro:
puti32( i32 ); stdout.newln();
puti32( i32, 5 ); stdout.newln();
puti32( i32, 5, '*' ); stdout.newln();
end puti32Demo;
Program 8.9 Using the Number of Parameters to Resolve Overloaded Procedures
All the examples up to this point provide procedure overloading for Standard Library routines (specifi-
cally, the integer output routines). Of course, you are not limited to overloading procedures in the HLA
Standard Library. You can create your own overloaded procedures as well. All you ve got to do is write a set
of procedures, all with unique names, and then use a single macro to decide which routine to actually call
based on the macro s parameters. Rather than call the individual routines, invoke the common macro and let
it decide which procedure to actually call.
8.3 Writing Compile-Time "Programs"
The HLA compile-time language provides a powerful facility with which to write "programs" that exe-
cute while HLA is compiling your assembly language programs. Although it is possible to write some gen-
eral purpose programs using the HLA compile-time language, the real purpose of the HLA compile-time
language is to allow you to write short programs that write other programs. In particular, the primary pur-
pose of the HLA compile-time language is to automate the creation of large or complex assembly language
sequences. The following subsections provide some simple examples of such compile-time programs.
Beta Draft - Do not distribute © 2001, By Randall Hyde Page 995
Chapter Eight Volume Four
8.3.1 Constructing Data Tables at Compile Time
Earlier, this text suggested that you could write programs to generate large, complex, lookup tables for
your assembly language programs (see Generating Tables on page 651). That chapter provided examples
in HLA but suggested that writing a separate program was unnecessary. This is true, you can generate most
look-up tables you ll need using nothing more than the HLA compile-time language facilities. Indeed, fill-
ing in table entries is one of the principle uses of the HLA compile-time language. In this section we will
take a look at using the HLA compile-time language to construct data tables during compilation.
In the section on generating tables, this text gave an example of an HLA program that writes a text file
containing a lookup table for the trigonometric sine function. The table contains 360 entries with the index
into the table specifying an angle in degrees. Each int32 entry in the table contained the value
[ Pobierz całość w formacie PDF ]