Is it possible to compile a static library (.a) in PB

Just starting out? Need help? Post your questions and find answers here.
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Is it possible to compile a static library (.a) in PB

Post by Wolfram »

Is it possible to compile a static library (.a) in PB ?

Or do you have any other idea to include old PB assembler code in to the new C backend ?
macOS Catalina 10.15.7
User avatar
idle
Always Here
Always Here
Posts: 5039
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Is it possible to compile a static library (.a) in PB

Post by idle »

I'm looking into that myself but rather than making an archive we just need to compile to an object and use imports and modify the c file to tell the c compiler what pb lib functions we're using in our object file for linking. which are in the original asm source, so we only need to modify the generated assembly to produce the object file and then modify the c file adding in the required libs.
  • 1) strip the preamble from the generated asm, recompile it with fasm / nasm or yasm to an .o file
    2) import the .o file functions in your code importc
    3) modify the generated c file adding it to the required pb lib functions so the linker will extract and include the functions
    4) recompile.
This is something Fred could do with a bit of thought and automation adding a keyword like procedureASM or ComplierASM and CompilerEndASM blocks which would compile a function to an .o or a block of code to .o's
User avatar
idle
Always Here
Always Here
Posts: 5039
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Is it possible to compile a static library (.a) in PB

Post by idle »

I confirmed that it's easy enough to do here's a quick test on linux.

I've compiled a simple function with mult(x,y) with pbcompiler -c -so
stripped out extraneous stuff then called fasm -m 1048576 /home/idle/PB6/purebasic.asm pb.o
then imported the pb.o

Code: Select all

Import "pb.o"
  mult(x,y)
EndImport 

x = mult(2,3)

MessageRequester("test from c","mult 2 * 3 = " + Str(x))
and compiled with the c compiler pbcompilerc mixasmc.pb

the fasm code purebasic.asm

Code: Select all

format ELF64
public _Procedure0 as 'mult'
section '.text' executable align 4096

; ProcedureDLL mult(x,y)
_Procedure0:
  PUSH   r15
  PS0=80
  XOR    rax,rax
  PUSH   rax
  PUSH   rax
  PUSH   rax
  SUB    rsp,40
  MOV    rax,rdi
  MOV    [rsp+40],rax
  MOV    rax,rsi
  MOV    [rsp+48],rax
; ProcedureReturn x*y
  MOV    r15,qword [rsp+40]
  IMUL   r15,qword [rsp+48]
  MOV    rax,r15
  JMP   _EndProcedure1
; EndProcedure   
_EndProcedureZero1:
  XOR    rax,rax
_EndProcedure1:
  ADD    rsp,64
  POP    r15
  RET

juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Rescue inline ASM

Post by juergenkulow »

Code: Select all

; Rescue inline ASM 
; Compile with ASM-Backend pbcompiler /Commented /DLL file.pb or Linux pbcompiler --commented --sharedobject file.so file.pb
ProcedureDLL.l mybswap(l.l)
  EnableASM
  mov eax,[p.v_l]
  bswap eax
  ProcedureReturn
EndProcedure

Code: Select all

; fasm h:mybswapwinx64.asm h:mybswap.o
format MS64 COFF
public _Procedure0 as 'mybswap'; 

; ProcedureDLL.l mybswap(l.l)
_Procedure0:
  MOV    qword [rsp+8],rcx
  PS0=48
  SUB    rsp,40
; EnableASM
; mov eax,[p.v_l]
p.v_l equ rsp+PS0+0
mov eax,[p.v_l]
; bswap eax
bswap eax
; ProcedureReturn
  JMP   _EndProcedure1
; EndProcedure
_EndProcedureZero1:
  XOR    rax,rax
_EndProcedure1:
  ADD    rsp,40
  RET

Code: Select all

; fasm h:mybswapwinx86.asm h:mybswap.o
format MS COFF
public _Procedure0 as '_mybswap@4'; 
; ProcedureDLL.l mybswap(l.l)
_Procedure0:
  PS0=4
; EnableASM
; mov eax,[p.v_l]
p.v_l equ esp+PS0+0
mov eax,[p.v_l]
; bswap eax
bswap eax
; ProcedureReturn
  JMP   _EndProcedure1
; EndProcedure
_EndProcedureZero1:
  XOR    eax,eax
_EndProcedure1:
  RET    4
; 

Code: Select all

; fasm /tmp/mybswaplinuxx64.asm /tmp/mybswap.o 
format ELF64
public _Procedure0 as 'mybswap'

; ProcedureDLL.l mybswap(l.l)
_Procedure0:
  PS0=64
  XOR    rax,rax
  PUSH   rax
  PUSH   rax
  SUB    rsp,40
  MOV    rax,rdi
  MOV    [rsp+40],eax
; EnableASM
; mov eax,[p.v_l]
p.v_l equ rsp+40
mov eax,[p.v_l]
; bswap eax
bswap eax
; ProcedureReturn
  JMP   _EndProcedure1
; EndProcedure
_EndProcedureZero1:
  XOR    rax,rax
_EndProcedure1:
  ADD    rsp,56
  RET

Code: Select all

; use inline ASM  in C and ASM Backend
Import "H:\PB21_4\mybswap.o" ;Please adapt. 
;Import "/tmp/mybswap.o"
  mybswap(l.l)
EndImport

x.l = $FF000000 
Debug RSet(Hex(x,#PB_Long ),8,"0")+" "+ RSet(Hex(mybswap(x),#PB_Long),8,"0")
; ASM Backend: FF000000 000000FF
; C Backend:   FF000000 000000FF
User avatar
idle
Always Here
Always Here
Posts: 5039
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Is it possible to compile a static library (.a) in PB

Post by idle »

there are still issues to consider with this as you need a means to tell the compiler which libs you are using and unfortunately that can only be done on windows at the moment as the pbcompiler has no -reasm on linux and osx.
this means that you have to do things like new list foo.i() or Str(x) for instance to tell the compiler which libs your code object uses so it can extract the libs for the linker.
Then there's the issue of the data section to consider which will either need name spacing or merging. As soon as you use strings or Pb objects it starts to get a little messy.
Feasibility to write a compiler tool to do this hinges on having -reasm on linux and osx. Fred is taking a look at it so maybe it will get addressed at some stage.
Post Reply