Author Topic: Some Simple macros  (Read 3353 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Some Simple macros
« on: June 10, 2012, 02:04:32 PM »

Macros may be deployed in place of small functions and subs. They produce significantly faster code,  and the ones shown here are not bloaters.

In the last few weeks, I have made the namespace inside the macros 'leak proof' so that parameter names cannot clash with symbols outside the macros.

Charles

Code: OxygenBasic
  1. macro hiWord(w)
  2. (w>>16 and 0xffff)
  3. end macro
  4.  
  5. macro loWord(w)
  6. (w and 0xffff)
  7. end macro
  8.  
  9. macro bit(w,n)
  10. (w>>n and 1)
  11. end macro
  12.  
  13. macro splitBits4(w,b0,b1,b2,b3)
  14. b0=w and 1
  15. b1=w>>1 and 1
  16. b2=w>>2 and 1
  17. b3=w>>3 and 1
  18. end macro
  19.  
  20. macro SplitBytes4(w,b1,b2,b3,b4)
  21. b1=w and 0xff
  22. b2=w>>8 and 0xff
  23. b3=w>>16 and 0xff
  24. b4=w>>24 and 0xff
  25. end macro
  26.  

kryton9

  • Guest
Re: Some Simple macros
« Reply #1 on: June 10, 2012, 03:39:41 PM »
Can we use macros as methods in a class, this would be like using inline in c++ class methods?

JRS

  • Guest
Re: Some Simple macros
« Reply #2 on: June 10, 2012, 07:11:58 PM »
Are you still looking at the O2 interface for SB? I bet converting some of the SB macros would be a good test.  ;)

Charles Pegge

  • Guest
Re: Some Simple macros
« Reply #3 on: June 11, 2012, 12:07:23 AM »
With a small tweak, macros of this style can be used as private methods. I don't know what constraints apply to C# inline functions/methods but making them 'public' in Oxygen would be quite difficult.

The bes macros for ScriptBasic, of course use the #define preprocessor form of macro. OxygenBasic also supports this syntax. The format here is better suited for multi-line macros, and very simple editing is all that is needed to convert procedures into macros, and vice-versa. Does SB have a FreeGlut module, John?

Peter

  • Guest
Re: Some Simple macros
« Reply #4 on: June 11, 2012, 01:23:09 AM »
Hi Charles,
Code: [Select]
macro SplitBytes4(w,b1,b2,b3,b4) 
b1=w and 0xff 
b2=w>>8 and 0xff 
b3=w>>16 and 0xff 
b4=w>>24 and 0xff 
end macro 

sys w=0x80642010, byte b1,b2,b3,b4

mov eax,w
and eax,255
mov b1,eax

mov eax,w
shr eax,8
mov b2,al

mov eax,w
shr eax,16
mov b3,al

mov eax,w
shr eax,24
mov b4,al

print b1 + " " + b2 + " " + b3 + " " + b4

'Why 0xFF ?

JRS

  • Guest
Re: Some Simple macros
« Reply #5 on: June 11, 2012, 02:06:25 AM »
Quote from: Charles Pegge
With a small tweak, macros of this style can be used as private methods. I don't know what constraints apply to C# inline functions/methods but making them 'public' in Oxygen would be quite difficult.

Dynamic Macros   8)

Quote from: Charles Pegge
The bes macros for ScriptBasic, of course use the #define preprocessor form of macro. OxygenBasic also supports this syntax. The format here is better suited for multi-line macros, and very simple editing is all that is needed to convert procedures into macros, and vice-versa. Does SB have a FreeGlut module, John?

How much of the current SB C header files are usable in O2?

I just started (and now sitting on the back burner since the Android port) a IUP CD (Canvas Draw) extension module as an attempt to bring graphics to SB. I would love to see the Windows version of SB take advantage of features that platform provides.

  • FreeGlut extension module
  • O2 JIT extension module
  • Better FFI (DYC)
  • OLE Automation
  • IUP (GUI forms based GUI) in O2 as an extension module.
  • Complete SQLite3 extension module
  • O2 embedded SB as and IDE/Debugger and IUP forms layout tool integration.

I will be spending most of my time on the Android/SL4A port of SB for the next couple of months at the minimal. It would be great to see a group of homeless Windows Basic programmers rally and bring SB for Windows up to it's potential.

Charles Pegge

  • Guest
Re: Some Simple macros
« Reply #6 on: June 11, 2012, 02:29:22 AM »
Hi Peter,
Code: [Select]
macro SplitBytes4(w,b1,b2,b3,b4)  
b1=w and 0xff  
b2=w>>8 and 0xff  
b3=w>>16 and 0xff  
b4=w>>24 and 0xff  
end macro


The macro does not assume b1,b2,b3,b4 are bytes, so the & 0xff bit mask is required.

Ideally macros like this should work with all types.

Charles

Charles Pegge

  • Guest
Re: Some Simple macros
« Reply #7 on: June 11, 2012, 10:19:31 AM »
I had to fix a little problem with bit shifting:

bit shifting by number worked correctly but bit shifting by variable did not.

example:
a=4
n=2
print a>>n


The latest Oxygen-in-progress fixes this problem.

Using macros as private methods:
Code: OxygenBasic
  1.  
  2. class TightData
  3.  
  4. sys dat
  5.  
  6. macro mGet(w,n)
  7. ((dat>>n) and w)
  8. end macro
  9.  
  10. macro mClear(w,n)
  11. mov edx,w
  12. mov cl,n
  13. shl edx,cl
  14. not edx
  15. dat and=edx
  16. end macro
  17.  
  18. macro mSet(w,n)
  19. mov edx,w
  20. mov cl,n
  21. shl edx,cl
  22. dat or=edx
  23. end macro
  24.  
  25. macro mSave(b,w,n)
  26. mClear w,n
  27. b and=w
  28. if b then mSet b,n
  29. end macro
  30.  
  31. '===
  32.  
  33. method GetBit(sys n) as sys
  34. return mGet 1,n
  35. end method
  36.  
  37. method PutBit(sys b,n)
  38. mSave b,1,n
  39. end method
  40.  
  41. method GetNybble(sys n) as sys
  42. return mGet 15,n
  43. end method
  44.  
  45. method PutNybble(sys b,n)
  46. mSave b,15,n
  47. end method
  48.  
  49. method GetByte(sys n) as sys
  50. return mGet 255,n
  51. end method
  52.  
  53. method PutByte(sys b,n)
  54. mSave b,255,n
  55. end method
  56.  
  57. method GetBits(sys w,n) as sys
  58. return mGet w,n
  59. end method
  60.  
  61. method PutBits(sys b,w,n)
  62. mSave b,w,n
  63. end method
  64.  
  65. end class
  66.  
  67. '#recordof TightData
  68.  
  69. TightData t
  70. t.dat=0x44332211
  71.  
  72. print hex t.getNybble 16
  73. t.PutNybble 0xA,16
  74. print hex t.getNybble 16
  75. print hex t.dat
  76.  
  77.  

Charles