Macro Preprocessor Documentation

Latest version can be downloaded from here: http://x.pgy.hu/~worm/het/hp
Suggested Catalyst ver: 12.10, also works with 13.4 (new ELF format), but except the disassembler. Later Catalyst versions aren’t tested with.

Hi and Happy New Year!

As my script/asm compiler is getting more complicated, it is time to summarize everything that helps to code in GCN ASM easier. The macro preprocessor is working right before the pascal interpreter and it has a syntax that is close to the C Macro Preprocessor.

Defining a macro: #define, #assign, #undef

#define something               //just define it, equals ""

#define add(a,b) ((a)+(b))      //define a simple macro
#define add(a,b,c) ((a)+(b)+(c))//overload it with 3 parameters

#define multiline 1+  \
                  2             //multiline macro

{#define multiline2 1+
2 }
         //another form of multiline macro

#macro ensureRange(x, min, max) //multiline macro, best syntax
  if(x<min)then
    x:=min
  else if(x>max)then
    x:=max;
#endm

#assign two 1+1                 //'assign' evaluates the expression

#undef something                //undefine an existing macro

var x:=-0;                      //test some of the macroes
ensureRange(x,1,5);
writeln(x, ',', add(1,2,3), ',', two);    //the output should be 1,6,2

The # operator

# converts the contents of tho following identifier to string.
## concatenates its two operands.
Using whitespace is not allowed near the #, ## operators.

Predefined macros:

There are some predefined macros: __file__, __line__, __date__, __time__

Conditional code generating can be done with __ifdef__(), __ifndef__(), __if__(). First parameter is the condition, and the rest of the parameters are the code which will be compiled if the condition is met.  Must ensure proper use of brackets() otherwise use a macro.

#define a
__ifdef__(a, writeln('a is defined'));
__ifndef__(b, writeln('b is undefined'));
#define a 15
__if__(a>=10, writeln('a>=10'));

Iterations can be written with the __for__() macro:

__for__(i in [1, 2, 5..8], write(i); ) //125678
__for__(i:=1 to 8 where i not in [3, 4], write(i); ) //125678
__for__(i:=1 to 8 step 2, write(i); ) //1357

Special macros:

#include pastes a file into the source code. No “” or <> needed, just the filename. First it will search for the file in the directory of the current .hpas file. If it can’t find there, it will try in the include\ directory (which is resides in the hetpas.exe’s directory).

It is possible to generate program code with actual program code. In the #script directive you can place a pascal script. Everything passed to write() and writeln() will be pasted into the source code:

{#script writeln('writeln("this is scripted");');}

Because this type of macro is originated back when I did a preprocessor for Delphi, this macro must enclosed in {} comment brackets.

ASM macros:

There are two macros that help nesting assembly code into the pascal script: asm_il(), asm_isa(). They work exactly like -marks when defining string literals. They are to isolate pascal script code from assembly code, so the IDE can provide proper syntax highlighting and code-insight functionality.
When including a file these macros are stripped down automatically so they can be included inside an asm() block while the included file has proper syntax highlighting.
There is a way to insert evaluated scripts into the asm block: If the preprocessor find something like this ![expression] in the asm block, then it evaluates the expression and inserts its result into the code.

writeln(asm_isa(This code is displayed in !["GCN"+" ISA"] syntax s_endpgm));
//output is: This code is displayed in GCN ISA syntax s_endpgm

Advertisements
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s