jumptable PB 6.0 asm and c backend

Share your advanced PureBasic knowledge/code with the community.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

jumptable PB 6.0 asm and c backend

Post by idle »

jumptable code pattern for PB 6.0 x86/x64/arm Asm and C backends

edit: changed to pointer array, mk-soft

Code: Select all

Structure ArrayOfPointer
  *Index[0]
EndStructure

Procedure JumpTable_test(index)
  Protected *addr.ArrayOfPointer = ?jt 
  Protected result 
  
  DataSection : jt:
     Data.i ?l0 , ?l1 , ?l2 , ?l3 , ?l4
  EndDataSection 
  
  If index < 5 
    
    CompilerIf #PB_Compiler_Backend = #PB_Backend_C    
      !goto *p_addr->f_index[v_index];
    CompilerElse 
      EnableASM  
      jmp *addr\Index[index]
      DisableASM 
    CompilerEndIf 
    
  Else 
    Goto lE 
  EndIf  
    
  l0: 
  result =  0
  Goto lE 
  l1: 
  result = 1  
  Goto lE
  l2:
  result =  2  
  Goto lE 
  l3:
  result = 3 
  Goto lE 
  l4:
  result =  4  
  lE:
  
  ProcedureReturn result 
EndProcedure    

Debug JumpTable_test(3)


Jeromyal
Enthusiast
Enthusiast
Posts: 204
Joined: Wed Jul 17, 2013 8:49 am

Re: jumptable PB 6.0 asm and c backend

Post by Jeromyal »

Hey ! This is some ground work for some really clever stuff.

Thanks for sharing.
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: jumptable PB 6.0 asm and c backend

Post by Blue »

Smart and useful.
Thank you, idle
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
Post Reply