what exectly are macros?

Everything else that doesn't fall into one of the other PB categories.
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

what exectly are macros?

Post by Dreglor »

i been hear about macros being in 4.0,
and im wondering what exectly are they? what do they do?
~Dreglor
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: what exectly are macros?

Post by PB »

Here's how I know them:

viewtopic.php?t=2212

Don't know if there's any other major uses for them?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Re: what exectly are macros?

Post by Max.² »

Dreglor wrote:i been hear about macros being in 4.0,
and im wondering what exectly are they? what do they do?
It is even possible - FASM does that for example - to replace built-in commands with own macros. There was a tip like

Code: Select all

macro inc v 
{
pushf
add v,1
popf
}
macro dec v 
{
pushf
sub v,1
popf
} 

The above works even with PB, if you let generate commented output, put the macro into the asm code and then let PB recompile the asm code. BTW, it is meant to speed optimize inc & dec for P4 (claims are that add/dec commands are faster)


What is quite common too, is the precalculation of data. Like you need an array a(0)=0 to a(99)=99. Instead of dynamically calculating it in your program or typing by hand, a macro fills the array each time you compile.
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

Code: Select all

!macro inc v 
!{ 
!pushf 
!add v,1 
!popf 
!} 
!macro dec v 
!{ 
!pushf 
!sub v,1 
!popf 
!} 

foo = 4
Debug foo
!dec [v_foo]
Debug foo 
there is no need to recompile. many macro features from fasm can be used in PB.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Re: what exectly are macros?

Post by Kale »

Dreglor wrote:i been hear about macros being in 4.0,
and im wondering what exectly are they? what do they do?
Macros are a nice and convienient way to obfuscate and confuse code to make it unreadable by all that has not coded it! 8)
--Kale

Image
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

I have always thought of macros as a sort of "inline procedure" - but expanding out the code (putting the code in place of the macro) instead of calling the function. Thus marginally faster but marginally bloatier.

eg (and taking some liberties with Pure's yet to be seen syntax):

Code: Select all

MACRO r.l = myDumbMacro a.l, b.l
  local j.l
  j = a * 10
  r = j + b
EndMACRO

;-v-

Procedure myDumbProc(a.l, b.l)
  j = a * 10
  ProcedureReturn j + b
EndProcedure

result = myDumbMacro valueA, 2

; is effectively replaced with the machine code for
;  local_j = valueA * 10
;  result =local_ j + 2

-v-

result = MyDumbProc(valueA, 2)
Is this on track? Or totally wrong?

PS: I thought they improved readability! :)



EDIT: Lots of Apache errors happening in the forum.
@}--`--,-- A rose by any other name ..
Post Reply