Author Topic: PureBasic and GOSUB  (Read 13440 times)

0 Members and 1 Guest are viewing this topic.

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 935
  • User-Rate: +30/-4
    • it-berater
PureBasic and GOSUB
« on: October 21, 2013, 07:55:45 AM »
Got an intersting Info today, that i want to share.
Its about the GOSUB issue that regular PureBasic does not allow GOSUB in Procedures.
There is some sort of Workaround

- FASM-Gosub Replacement.
- and this "Call Macros"

Quote
The issue with the gosub implementation in procdures perhaps was due to the single pass
compilation, the specific issue would arise from select statements where PureBasic uses the stack
so when either a gosub or goto is used to exit from within a select statement it will either result in
reading the wrong local variables or produce an IMA on returning from the procedure.
So It appears Fred disallowed gosub within procedures.

I've provided macro replacements for goto and gosub with FASM preprocesor directives
to handle this issue, you just need to use a depth parameter if you're exiting a select

Must admit that it still looks a bit complicated to me compared to PowerBasic.
But if it works ...
« Last Edit: October 21, 2013, 08:55:52 AM by Theo Gottwald »

Offline Aslan Babakhanov

  • Newbie
  • *
  • Posts: 24
  • User-Rate: +5/-3
Re: PureBasic and GOSUB
« Reply #1 on: October 21, 2013, 12:28:29 PM »
pure solution in powerbasic :)
Code: [Select]
Function PBMain () As Long
    Local i As Long
    For i = 1 To 3
        ! jmp lbl_show
        lbl_ret:
    Next i
    Exit Function

lbl_show:
    ? Str$(i)
    ! jmp lbl_ret 
End Function

'
' and these macros
'
Macro p_return (lbl)
  ! jmp lbl_ret
End Macro

Macro p_gosub (lbl)
      ! jmp lbl_show
      lbl_ret:
End Macro
           
« Last Edit: October 21, 2013, 12:32:17 PM by Aslan Babakhanov »

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 935
  • User-Rate: +30/-4
    • it-berater
Re: PureBasic and GOSUB
« Reply #2 on: October 21, 2013, 02:39:57 PM »
Thats a poor sollution, Aslan :-) because using CALL you can use the Macro more then once and the return adress is saved.
Did you take a look on both solutions?

In Powerbasic its simpler as you know.

Offline Aslan Babakhanov

  • Newbie
  • *
  • Posts: 24
  • User-Rate: +5/-3
Re: PureBasic and GOSUB
« Reply #3 on: October 21, 2013, 10:28:15 PM »
They are good indeed, somehow complicated. I just don't understand the reason of why gosub wans't implemented.
There is something else rather than 'due to single pass compilation...'.
Powerbasic is single pass compiler too, but does a lot.
I suppose, internal Purebasic to fasm translator is not that complex and smart as it should be from the first sight.


Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2485
  • User-Rate: +204/-0
Re: PureBasic and GOSUB
« Reply #4 on: October 21, 2013, 10:39:48 PM »
> Powerbasic is single pass compiler too

Wrong. it is a three pass compiler.

Offline Brice Manuel

  • Full Member
  • ***
  • Posts: 154
  • User-Rate: +0/-0
Re: PureBasic and GOSUB
« Reply #5 on: October 21, 2013, 11:37:21 PM »
Purebasic to fasm translator is not that complex and smart as it should be from the first sight.

Long ago, there was a third-party optimizer for PureB that would optimize the ASM it generated.

Over the years, most of the optimization by Fred seems to have been in the libs as opposed to the compiler.

Offline Aslan Babakhanov

  • Newbie
  • *
  • Posts: 24
  • User-Rate: +5/-3
Re: PureBasic and GOSUB
« Reply #6 on: October 22, 2013, 06:02:33 AM »
Wrong. it is a three pass compiler.

Jose, thanks for information.
I always thought, that PB is a single pass compiler (read on forum long time ago).


Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2485
  • User-Rate: +204/-0
Re: PureBasic and GOSUB
« Reply #7 on: October 22, 2013, 06:43:12 AM »
Don't believe all what you read. Older versions of the compiler where two-passes. A third pass was added in PB9 to handle forward references. Most "indie" basics, like Pure Basic and FreeBasic, are single pass, which is inefficient.

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 935
  • User-Rate: +30/-4
    • it-berater
Re: PureBasic and GOSUB
« Reply #8 on: October 22, 2013, 07:58:38 AM »
Some people say they are "enhanced MACRO Assembler Scripts" compared to the PB Compiler. PureBasic for example can not make a FOR Loop with something else then Integer Data. No
Code: [Select]
FOR I=0.001 TO 0.78 STEP 0.01for that reason. Thats why i suggested in their wishlist thread a compiler modernization. In 32 bit compiling the PowerBasic Compiler is definitely 2 passes ahead.

Offline Brice Manuel

  • Full Member
  • ***
  • Posts: 154
  • User-Rate: +0/-0
Re: PureBasic and GOSUB
« Reply #9 on: October 22, 2013, 10:15:38 AM »
Don't believe all what you read. Older versions of the compiler where two-passes. A third pass was added in PB9 to handle forward references. Most "indie" basics, like Pure Basic and FreeBasic, are single pass, which is inefficient.

I would argue that PowerBASIC has a more inefficient compiler than PureBasic.  For PowerB to be a three-pass compiler and still produce the bloated EXEs it does not speak well for efficiency.  The last two versions of PowerB saw significant bloat added as the mottoes "smaller faster" and "say goodbye to bloatware" were apparently abandoned.

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 935
  • User-Rate: +30/-4
    • it-berater
Re: PureBasic and GOSUB
« Reply #10 on: October 22, 2013, 10:39:28 AM »
Brice, you have wrong informations.
Compare the ASM Output, then you will see that in fact the output of the powerbasic compiler is better oprimized then the PureBasic Output.

For more details, take a look here:

PureBasic stack-based Compiler Output
and compare it with the equivalent of the PowerBasic 32 bit compiler.

The pure size of the smallest executabel depends on what sort of code you are doing and what runtime is included.
The size of a "Hello world" doesn't help so much if its within a range.

Having said this, i must add that the differences will be not noticeable in 99.9% of all normal usage cases.


Offline Brice Manuel

  • Full Member
  • ***
  • Posts: 154
  • User-Rate: +0/-0
Re: PureBasic and GOSUB
« Reply #11 on: October 22, 2013, 10:45:14 AM »
Brice, you have wrong informations.

No, I don't.  I am merely suggesting there is more than one way to look at the efficiency of a compiler. 

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 935
  • User-Rate: +30/-4
    • it-berater
Re: PureBasic and GOSUB
« Reply #12 on: October 22, 2013, 10:50:19 AM »
What else do you look at, if not the ASM Output?
The IDE is definitely better with PureBasic (and it is multiplatform), and it has tons of more features.

But we are talking about the x32- Compiler-Core and about this PowerBasic is two evolution-steps ahead.
Tell me something PowerBasic x32 can not do, you can do with PureBasic.

I have told you several things here already that is possible with PowerBasic, that PureBasic can not do.

Its clearly understandable if you look into history.
When Bob took the "Turbo Basic" he started with something that was already an stable compiler.
That was created from people with university background on information technology.

Looking at PureBasic you can see that Fred started as some sort of "Hacker" and made an adventure building his first PureBasic.
At that time Fred just started. Just like Charles started his compiler.
They did not visit the university "on compiler building" before.

The knowledge on Compiler Technology was not there. He just did it. I have that old PureBasic versions still here.

Of course he improved step-by-step.
But i believe, if he would start "from scratch today" he would make something that would possibly have all that features.

But now he has the problem that you can not put "old constructs easily from top to bottom".
He would not make the thing " Stack based". Also VC is not "Stack based" in this way.

Quote
most of the optimization by Fred seems to have been in the libs as opposed to the compiler.

This is just the point. If i add some libs, i can say "Look Ma'm i have new FEATURES". If i optimize the compiler inside, it takes awful lots of time, and it sells rather bad.
Anyway, if you use Purebasic as some sort of "Brickbox", calling ready-made libs, you get most out of it.
And there are several libs that make life really easy, and make PureBasic the tool of choice.

To work with complicated data structures and make hi-speed stuff, currently i see PowerBasic ahead.
 
« Last Edit: October 22, 2013, 11:09:07 AM by Theo Gottwald »

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 677
  • User-Rate: +27/-1
    • Charles Pegge
Re: PureBasic and GOSUB
« Reply #13 on: October 22, 2013, 12:44:32 PM »
Gosub is very easy to implement, so the decision to leave it out of a compiler, is perhaps, political rather than technical :)

An inner function that can access all the parental-scoped variables would be a good replacement, and have  the advantage of being able to return values. - I'm currently testing this idea.

PS:

OxygenBasic uses up to 8 passes and  still counting - good for dumping redundant code as early as possible.
« Last Edit: October 22, 2013, 12:55:11 PM by Charles Pegge »

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 935
  • User-Rate: +30/-4
    • it-berater
Re: PureBasic and GOSUB
« Reply #14 on: October 22, 2013, 12:51:31 PM »
Quote
, as dependence on a single operating system, restrictions in the inline assembler, limited graphics capabilities, higher development costs, longer development cycles,

The only restriction in PowerBasic that applies to me is the missing x64. The other points can be resolved with libraries. The inline Assembler is weaker then an FASM, anyway its good for most uses, and i wonder if you will need more.

Today i came to anotehr PureBasic speciality: Procedure Parameters.

In PowerBasic you can say "BYREF" or "BYVAL". For any Procedure or Function Parameter.

In Purebasic there is nothing like that. Some datatypes are by default BYREF and others are always BYVAL.
Of course it works, and you can solve your problems with it, but it does not have the completeness and elegance of PowerBasic. And i did not yet start with the PowerBasic string-engine or the COM implementation.

Of course this has needed a long developement time, yet this is not my problem.
About this PowerBasic can compete C-Language while PureBasic does not (yet) offer ALL the possibilities. At a closer look it looks more like a Toolbox - and thats nothing bad.

Its just not "state-of-the-art" in compiler science. If you would ask me whats a "state-of-the-art" sort of-compiler, i would recommend that you get the book "Metafont" and "TEX" from Donald Knuth.
This is "Information Technology" at its best.

@Charles, PureBasic has "Gosub". The Point is that it is forbidden inside Procedures, because there is a danger that it would leave an unbalanced STACK. For details you will need to look at the Links in previouse posts. Just from Speed a GOSUB/RETURN, that is in fact a single CALL/RET mnemonic, is unbeatable and i use it very often.
The code may not look so great - but its structured and fast.
About your Oxygen Basic: Using a lot of passes is a good idea, charles and shows that your compiler goes the right way. Of course it will resolve forward references.
« Last Edit: October 22, 2013, 12:59:27 PM by Theo Gottwald »