Extend Blitz3D

Started by Emil_halim, October 19, 2018, 15:51:24

Previous topic - Next topic

Emil_halim

Hi All,

Just want to let you know that , I am tring to extend Blitz3D language it self first by allowing inline asm.

Example
Quote
Global msg$ = "welcome "

Global body$ = " here "

Print msg 

Asm " sub esp,8 "
Asm " mov eax,_vbody "
Asm " mov [esp],eax "
Asm " call __bbStrLoad "
Asm " mov [esp],eax "
Asm " call _fprint "


yn$=Input$( "press Return key" )

End


It worked fine.

Also successfully integrated SmallerC with Blitz3D.

Example
Quote
Global msg$ = "welcome "
Global body$ = " here "
Global xx

Asm " mov eax , _vbody
Asm " add eax, 4       
Asm " push eax         
Asm " call _strlen     
Asm " add esp,4       
Asm " mov [_vxx],eax   
Print xx

yn$=Input$( "press Return key" )
End

Ccode
"  int strlen(char* mystr)
"  {
"      char* ch = mystr;
"      while(*ch!=0)ch++;   
"      return ch-mystr;
"  } 
CEndCode



Also i have added some new KeyWords such as ,AsmInc,AsmData,AsmEndData,NewType,End NewType,DefType,Statement,End Statement,IncBin.

As you see , some keywords are from Amiga Blitz2 syntax , it is a try to port it to Pc.

The IncBin Keyword include a binary file into your program , if the file name start with  'Fasm:' like "Fasm:filename.asm"
blitz3d compiler will auto execute NotePad++ to allow you edit your fasm code , waiting to close it , then compile it with fasm and finaly include the bin code into your code.

The same for AsmInc keyword.

but my gool is allow the compiler to preduce the asm code from Basic code , already availbale by using -a option , and let Fasm compiles it and link it with polink.

I am working in it, still in first step.

Hope wou will like the idea. 

Kippykip

Oh wow, too bad I don't know ASM whatsoever, that's why I use Blitz products in the first place :))

Emil_halim


Ofcourse that , it's for Expert  users only , so that they know what they do. :)

Here is a Example demo the point.

Blitz3D did not handle double type , in  bOGL-2 this helper function is done by

; parts from blitz_gl.bb file
Function gluPerspective(fovy#, aspect#, zNear#, zFar#)
SngToDbl fovy,BlitzGL_DblBank
Local fovy_l = PeekInt(BlitzGL_DblBank, 0), fovy_r = PeekInt(BlitzGL_DblBank, 4)

SngToDbl aspect,BlitzGL_DblBank
Local aspect_l = PeekInt(BlitzGL_DblBank, 0), aspect_r = PeekInt(BlitzGL_DblBank, 4)

SngToDbl zNear,BlitzGL_DblBank
Local zNear_l = PeekInt(BlitzGL_DblBank, 0), zNear_r = PeekInt(BlitzGL_DblBank, 4)

SngToDbl zFar,BlitzGL_DblBank
Local zFar_l = PeekInt(BlitzGL_DblBank, 0), zFar_r = PeekInt(BlitzGL_DblBank, 4)

blitz_gluPerspective fovy_l, fovy_r, aspect_l, aspect_r, zNear_l, zNear_r, zFar_l, zFar_r
End Function

; Thanks to Floyd for this one. His comments:
; This should convert all ordinary floats correctly.
; The extreme cases +Infinity, -Infinity, NaN would require special handling.
Function SngToDbl(x#, bank)
Local s, e, m, Lo, Hi, n

PokeFloat bank, 0, x
n = PeekInt( bank, 0 )  ; raw bits of x

s = n And %10000000000000000000000000000000  ; sign bit
e = n And %01111111100000000000000000000000  ; 8-bit exponent
e = (e Shr 3) + %00111000000000000000000000000000  ; 11-bit exponent
m = n And %00000000011111111111111111111111  ; 23-bit mantissa
Lo = m Shl 29  ; final three bits of mantissa
Hi = s Or e Or (m Shr 3 ) ; sign, exponent, first twenty bits of m

PokeInt bank, 0, Lo
PokeInt bank, 4, Hi
End Function



so the function blitz_gluPerspective  ,'it's user lib function' , takes double args , before calling it the in above code they convert
the args to double from float by call SngToDbl.

Here is my approach with my extension using Fasm.

Statement gluPerspective { fovy.f, aspect.f, zNear.f, zFar.f }

      Fasm             
           Sub esp,4*8
           fld  dword [ebp+20]   ;Arg1
           fstp qword [esp]
 
           fld  dword [ebp+24]   ;Arg2   
           fstp qword [esp+8]
 
           fld  dword [ebp+28]   ;Arg3 
           fstp qword [esp+16]
 
           fld  dword [ebp+32]   ;Arg4 
           fstp qword [esp+24]
     
      End Fasm                       

      Call [blitz_gluPerspective]
       
End Statement


The Statement differs from Function that when return it does not make eax register with 0.
So this code collects whatever between Fasm and End Fasm keywords in a file then compile it with Fasm then reinserts the bin code in the Blitz3D code.

Hope that Explain the point.

Emil_halim

Hi All

To allow fasm to access to data in the Bank and process it's data in fasm . i created new runtime function 'BankBuffer' , that returns the adderss of data in the bank.

Here is code demo the new function.

; test of new function BankBuffer
; -----------------------------------

bnkTest=CreateBank(12)

PokeByte bnkTest,0,Rand(255)
PokeShort bnkTest,1,Rand(65535)
PokeInt bnkTest,3,Rand(-2147483648,2147483647)
PokeFloat bnkTest,7,0.5

Print PeekByte(bnkTest,0)
Print PeekShort(bnkTest,1)
Print PeekInt(bnkTest,3)
Print PeekFloat(bnkTest,7)

Global ptr = BankBuffer(bnkTest) ; get the address of data in bank

Mov eax,[ptr]                    ; put the adderss in eax register so that move it to fasm code
Fasm
     
   Mov byte[eax],5               ; changes the first byte in the bank

   Mov word[eax+1],1

   Mov dword[eax+3],200

   Mov dword[eax+7],1.0

End Fasm

Print PeekByte(bnkTest,0)       ; print 5
Print PeekShort(bnkTest,1)      ; print 1
Print PeekInt(bnkTest,3)        ; print 200
Print PeekFloat(bnkTest,7)      ; print 1.0


WaitKey

FreeBank bnkTest


It will be uesful when need to process data in a Bank fastly , the poke peek is very slow.

See you with next demo.

Emil_halim


Here is an other demo , also taken from  bOGL-2


bOGL-2  AmbientLight
==============

Function AmbientLight(red, green, blue)
PokeFloat bOGL_AmbientLight_, 0, red / 255.0
PokeFloat bOGL_AmbientLight_, 4, green / 255.0
PokeFloat bOGL_AmbientLight_, 8, blue / 255.0
End Function


And this is mine
===========

Function AmbientLight(red, green, blue)
    Mov eax,[pbOGL_AmbientLight_]
    Mov ebx,_d255
    Fasm
          fild  dword [ebp+20]
          fld   dword [ebx]
          fdivp st1,st0
          fstp  dword [eax+0]
               
          fild  dword [ebp+24]
          fld   dword [ebx]
          fdivp st1,st0
          fstp  dword [eax+4]

          fild  dword [ebp+28]
          fld   dword [ebx]
          fdivp st1,st0
          fstp  dword [eax+8]         
    End Fasm

;  MessageBox(0,Str(PeekFloat(bOGL_AmbientLight_,8)),"",0) ; display for debug
   AsmData
     "_d255 .dd 255.0
   AsmEndData
End Function


Amanda Dearheart

Great work,

I can understand the reason for assembly if you want to increase the speed of a process,  (are there any other reasons to use assembly), but why SmallC
Prepare to be assimilated !  Resistance is futile!

Emil_halim

#6
Hi bsisko,

  Actually using asm for the sake of speed and allows Blitz3D to extend to make more modern tasks that did not allowed with the original one , without change the Blitz3D compiler a lot for backword compatibility.

  Sometimes , we need some complex task that can done with c , so i think that it's better to include it.

but, smallerc does not support float math as in pc , so i will replace it with an other one.

All that is under testing.

Hope you find it usefull.

Emil_halim

H all

I have Add another feature , use Register as a Variable.

Example

; Rgister As Variable Example
;============================

Global  eax = 10 * Sin( 60 )

Print  eax

  Fasm
    Mov  eax,100
  End Fasm

Print  eax  ; print 100

WaitKey
End



This is a fast way to pass data from Blitz3D expression to Fasm code.