C-Backend UCase Variables in Macros do not work! Bug or Feature?

Just starting out? Need help? Post your questions and find answers here.
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

C-Backend UCase Variables in Macros do not work! Bug or Feature?

Post by SMaag »

If I pass a variable , defined in UpCase characters, true a Macro directly
to the C-Compiler I get an error. If I pass the same variable in LoCase characters it works.
It seems like the PB-Compiler LoCase all variable names. But not in Macros!

Go to Lines 133, 140, 147 and switch to the BSwapVar(W), BSwapVar(L), BSwapVar(Q)
to see the problem! Only in C-Backend!!!


Is there any workarround to fix that?

I tested with PB 6.12 x64 Windows 10

Update: Bug x32 ASM Backend fixed!

Code: Select all

; ===========================================================================
; Universal BSwap Macro : BSwapVar to swap the bytes in a variable
; does not matter which size the variable has!
; ===========================================================================

EnableExplicit

; there is a problem in C-Backend. In C-Backend we have to 
; call BSwapVar always with variable name in lower case
; if we define L.l and call BSwapVar(L) we get an error
; if we call  BSwapVar(l) it works!
; it looks like the compler LoCase all variables but in a Macro not!

CompilerIf #PB_Compiler_Backend = #PB_Backend_C
  
  Macro BSwapVar(var)          
    CompilerSelect SizeOf(var)
      CompilerCase 2
        !v_#var = __builtin_bswap16(v_#var);                   
      CompilerCase 4
        !v_#var = __builtin_bswap32(v_#var);                 
      CompilerCase 8 
       !v_#var = __builtin_bswap64(v_#var);
    CompilerEndSelect        
  EndMacro

CompilerElse
  
  ; ASM Backend
  Macro BSwapVar(var)
    EnableASM   
    CompilerSelect SizeOf(var)
      CompilerCase 2         
        !XOR EAX, EAX
        MOV AX, var
        !XCHG AL, AH
        MOV var, AX         

      CompilerCase 4          
        MOV EAX, var
        !BSWAP EAX
        MOV var, EAX          
              
      CompilerCase 8
        
        CompilerIf #PB_Compiler_Processor = #PB_Processor_x64           
          MOV RAX, var
          !BSWAP RAX
          MOV var, RAX
                   
        CompilerElse            
          LEA ECX, var   ; load effective address of Value (:= @Value)
          !MOV EDX, DWORD [ECX]
          !MOV EAX, DWORD [ECX +4]
          !BSWAP EDX
          !BSWAP EAX
          !MOV DWORD [ECX], EAX
          !MOV DWORD [ECX +4], EDX 
                      
        CompilerEndIf    
          
      CompilerEndSelect
      DisableASM
  EndMacro
  
CompilerEndIf


Procedure Test()
  Protected Qp.q, Lp.l, Wp.w
  
  Debug ""
  Debug "----------------------------------------"
  Debug "BSwap inside Procedure"
  Debug "----------------------------------------"
  Debug ""
  Debug "Varsizes"
  Debug SizeOf(Wp)
  Debug SizeOf(Lp)
  Debug SizeOf(Qp)
  
  Wp=$1122
  Lp=$11223344
  Qp=$1122334455667788
  
  Debug ""
  Debug "BSwap16"
  Debug Hex(wp, #PB_Word)
  BSwapVar(wp)
  Debug Hex(wp, #PB_Word)
  
  ; This do not work in C-Backend 
  Debug ""
  Debug "BSwap32"
  Debug Hex(lp, #PB_Long)
  BSwapVar(lp)
  Debug Hex(lp, #PB_Long)
  
  ; This do not work in C-Backend
  Debug ""
  Debug "BSwap64"
  Debug Hex(qp, #PB_Quad)
  BSwapVar(qp)
  Debug Hex(qp, #PB_Quad)

EndProcedure

Define Q.q, L.l, W.w
Debug "----------------------------------------"
Debug "BSwap outside of Procedure"
Debug "----------------------------------------"
Debug ""
Debug "Varsizes"
Debug SizeOf(W)
Debug SizeOf(L)
Debug SizeOf(Q)

W=$1122
L=$11223344
Q=$1122334455667788

Debug ""
Debug "BSwap16"
Debug Hex(w, #PB_Word)
BSwapVar(w)   ; in LoCase it works
; BSwapVar(W)   ; in UpCase it do not work in C-Backen!
Debug Hex(w, #PB_Word)

Debug ""
Debug "BSwap32"
Debug Hex(l, #PB_Long)
BSwapVar(l)
; BSwapVar(L)   ; don't work with UCase character
Debug Hex(l, #PB_Long)

Debug ""
Debug "BSwap64"
Debug Hex(q, #PB_Quad)
BSwapVar(q)
; BSwapVar(Q)   ; don't work with UCase character
Debug Hex(q, #PB_Quad)

Test()

breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: C-Backend UCase Variables in Macros do not work! Bug or Feature?

Post by breeze4me »

The C language is case-sensitive for variable names. So v_q and v_Q are considered two different variables.
PB uses the variable named Q in the C backend as v_q, so the v_Q variable name in the macro will not work correctly.
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: C-Backend UCase Variables in Macros do not work! Bug or Feature?

Post by SMaag »

The C language is case-sensitive for variable names. So v_q and v_Q are considered two different variables.
PB uses the variable named Q in the C backend as v_q, so the v_Q variable name in the macro will not work correctly.
Yes seems to be like that! It's not a real bug! It is exactly how Macros work! But this feature cause bugs at the end!

I think there is no solution for that issue at the moment. Maybe it can be solved in the future directly in the compiler.

Maybe we should add this to feature request or bug report!
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: C-Backend UCase Variables in Macros do not work! Bug or Feature?

Post by Fred »

Unfortunately, it can't be solved by the compiler as PureBasic is case insensitive while C is case sentisive.
User avatar
skywalk
Addict
Addict
Posts: 4241
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: C-Backend UCase Variables in Macros do not work! Bug or Feature?

Post by skywalk »

Hi Fred,
Any chance for EnableCaseSensitive?
I prefer case sensitive for my own use. It is easier to parse my files and throw errors when not exactly my intended command.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: C-Backend UCase Variables in Macros do not work! Bug or Feature?

Post by Quin »

skywalk wrote: Mon Feb 03, 2025 11:56 pm Hi Fred,
Any chance for EnableCaseSensitive?
I prefer case sensitive for my own use. It is easier to parse my files and throw errors when not exactly my intended command.
+1, I'd love a case-sensative PB :)
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: C-Backend UCase Variables in Macros do not work! Bug or Feature?

Post by idle »

Quin wrote: Tue Feb 04, 2025 12:44 am
skywalk wrote: Mon Feb 03, 2025 11:56 pm Hi Fred,
Any chance for EnableCaseSensitive?
I prefer case sensitive for my own use. It is easier to parse my files and throw errors when not exactly my intended command.
+1, I'd love a case-sensative PB :)
NOOO! That's why I code in PB, Dyslexic can't see the difference! :lol:

come to think about it, how on earth do you know the difference between upper case and lower case with a screen reader?
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: C-Backend UCase Variables in Macros do not work! Bug or Feature?

Post by SMaag »

Unfortunately, it can't be solved by the compiler as PureBasic is case insensitive while C is case sentisive.
Yes! That's what I expected. I have only one solution approche in mind. what do not produce other problems: a 'Compiler LCase' Function.

If parsing Macros for direct C Code '!' and 'v_#' for variables and LCase it, this may cause other problems.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: C-Backend UCase Variables in Macros do not work! Bug or Feature?

Post by Quin »

idle wrote: Tue Feb 04, 2025 7:09 am come to think about it, how on earth do you know the difference between upper case and lower case with a screen reader?
It's configurable, I can either make it beep for capitals, change the pitch, or say "cap". I prefer changing the pitch, it hurts my ears the least and doesn't add any extra time to my speech reading things..
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: C-Backend UCase Variables in Macros do not work! Bug or Feature?

Post by idle »

SMaag wrote: Tue Feb 04, 2025 11:07 am
Unfortunately, it can't be solved by the compiler as PureBasic is case insensitive while C is case sentisive.
Yes! That's what I expected. I have only one solution approche in mind. what do not produce other problems: a 'Compiler LCase' Function.

If parsing Macros for direct C Code '!' and 'v_#' for variables and LCase it, this may cause other problems.
it could be done with a compiler tool but that's currently the only option unless fred adds a lcase parse for the c backend in the compiler.

@quin
I would have thought pitch would slow you down a little but must be better than beep.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: C-Backend UCase Variables in Macros do not work! Bug or Feature?

Post by Quin »

idle wrote: Tue Feb 04, 2025 8:39 pm @quin
I would have thought pitch would slow you down a little but must be better than beep.
No, the pitch actually doesn't slow me down at all, nor does the beep, but it's a 2000 HZ square wave for some fucked up reason, so I refuse to use it. What does slow me down is it saying cap, like "cap I".
Post Reply