Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: how to break out early of a case ?  (Read 44515 times)

BrunoK

• Sr. Member
• Posts: 275
• Retired programmer
Re: how to break out early of a case ?
« Reply #60 on: May 04, 2020, 03:29:52 pm »
Yes, like Thaddy said, a loop is inefficient. It requires a conditional jump and a full case walk-over each time, whereas a fall instruction is just one unconditional jump. I addition, each fall can be made conditional:
Code: Text  [Select][+][-]
1. var n: int = 0;
2.
3. main
4. do
5.   when true is 1 > n do
6.     when n
7.       is 0 do
8.         n = 1;
9.         if n > 0 do
10.           fall;
11.         end;
12.       is 1 do
13.         n = 2;
14.     end;
15.   end;
16. end;
Please, can you comment what this bit of code is supposed to do or where documentation can be found on such instructions.

munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
Re: how to break out early of a case ?
« Reply #61 on: May 04, 2020, 03:44:24 pm »
Please, can you comment what this bit of code is supposed to do or where documentation can be found on such instructions.

This code is just a simple example of fall-through. I used it as a test to see if the fall instruction works in SharpBASIC. I started its compiler project about half a year ago. The compiler is far from finished so there is no documentation published yet. But you can follow the latest development on the website sharpbasic.com.

My presence on this forum among other things is to exchange ideas. Pascal as a programming language is great to use as example, but it has its flaws, which I try to avoid in SharpBASIC.
« Last Edit: May 04, 2020, 03:46:23 pm by munair »
keep it simple

BrunoK

• Sr. Member
• Posts: 275
• Retired programmer
Re: how to break out early of a case ?
« Reply #62 on: May 04, 2020, 04:07:31 pm »
Code: Pascal  [Select][+][-]
1.  when true is 1 > n do
Just explain what this line means please.

jamie

• Hero Member
• Posts: 5135
Re: how to break out early of a case ?
« Reply #63 on: May 04, 2020, 05:02:04 pm »
While n < 1 do.
Rpn
The only true wisdom is knowing you know nothing

munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
Re: how to break out early of a case ?
« Reply #64 on: May 04, 2020, 05:05:46 pm »
Code: Pascal  [Select][+][-]
1.  when true is 1 > n do
Just explain what this line means please.

It means:

Code: Pascal  [Select][+][-]
1. if n < 1 then

In SharpBASIC one would usually write:

Code: Pascal  [Select][+][-]
1. if n < 1 do

As I said earlier, the when-switch should fulfill both Ifs and INs for most variable types, including boolean and string. In simple cases like above IF is preferred, but for range checks I prefer:

Code: Text  [Select][+][-]
1. when n is 1 to 10 do
2. // ..
3. end;

which produces (for 32 bits):

Code: ASM  [Select][+][-]
1. ; when n is <expr> to <expr> do
2.         movsx   eax, byte [_C5]
3.         push    eax
4.         movsx   eax, byte [_C4]
5.         push    eax
7.         mov     eax, dword [_I26]
8.         pop     edx
9.         cmp     eax, edx
10.         jg      ._L1
11.         pop     edx
12.         cmp     eax, edx
13.         jl      ._L1
14. ._L2:
15.         ; ...
16. ._L1:
17.   ; ...

Code: Text  [Select][+][-]
1. if n >= 1 and n <= 10 do
2. // ..
3. end;

which produces:

Code: ASM  [Select][+][-]
2.         mov     eax, dword [_I26]
3.         push    eax
4.         movsx   eax, byte [_C3]
5.         pop     edx
6.         cmp     edx, eax
7.         cmovge  eax, [_sb_true]
8.         cmovl   eax, [_sb_false]
9.         push    eax
11.         mov     eax, dword [_I26]
12.         push    eax
13.         movsx   eax, byte [_C4]
14.         pop     edx
15.         cmp     edx, eax
16.         cmovle  eax, [_sb_true]
17.         cmovg   eax, [_sb_false]
18.         pop     edx
19.         and     eax, edx
20. ; convert to boolean
21.         call    _sb_cbool
22.
23.         test    eax, -1
24.         je      ._L0
25.   ; ...
26. ._L0:
27.   ; ...
« Last Edit: May 04, 2020, 05:25:43 pm by munair »
keep it simple

munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
Re: how to break out early of a case ?
« Reply #65 on: May 04, 2020, 05:06:14 pm »
keep it simple

munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
Re: how to break out early of a case ?
« Reply #66 on: November 23, 2021, 10:35:50 am »
Code: Text  [Select][+][-]
1. var n: int = 0;
2.
3. main do
4.
5.   when n
6.     is 0 do
7.       n = 1;
8.       fall;
9.     is 1 do
10.       n = 2;
11.       fall;
12.     is 2 do
13.       n = 3;
14.       fall;
15.     is 3 do
16.       n = 4;
17.     end;
18.
19.   print n;      ' 4
20.
21. end;
That looks good.

Documentation and development stages are now on a dedicated forum. I thank everyone here on the FP forum for their input and ideas.
keep it simple

440bx

• Hero Member
• Posts: 2623
Re: how to break out early of a case ?
« Reply #67 on: November 23, 2021, 04:58:47 pm »
I thank everyone here on the FP forum for their input and ideas.
And thank you for implementing ideas that are mostly the removal of a wrinkle/deficiency in an existing concept that has proven useful.

I'm totally overwhelmed with things to do as it is but, I'll definitely check SharpBasic out.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
Re: how to break out early of a case ?
« Reply #68 on: November 27, 2021, 01:04:21 pm »
Reading the history of FPC again and its struggles to be TP and Delphi compatible, after some 5 months effectively working on the SharpBASIC compiler (SBC), I realize how constraining it must be to model a new compiler after an existing language. Honestly, I admire the FPC and also FBC (FreeBASIC) developers for honouring the restrictions/limitations and also the ambiguities of the original language for maximum compatibility. It certainly wouldn't be my cup of tea! I say this as a frequent FPC and FBC user; even a trivial thing such as declaring and initializing multiple variables can be hard/impossible to implement:

Code: Pascal  [Select][+][-]
1. var a,b,c:integer = 0; // error

See here one of the many reasons for SBC. It stems from a desire coming from years of experience with FBC and FPC and could be considered sort of an amalgamation of the two. Using Crenshaw's approach, i.e. without producing a ST, development of the compiler so far has been prosperous, considering I haven't been able to work on it for one-and-a-half year. Here's an example with pointers: https://sharpbasic.com/forum/viewtopic.php?p=27#p27, which tells you that the Crenshaw model, isn't necessarily for toy compilers.
« Last Edit: November 27, 2021, 01:10:46 pm by munair »
keep it simple

440bx

• Hero Member
• Posts: 2623
Re: how to break out early of a case ?
« Reply #69 on: November 27, 2021, 01:33:18 pm »
years of experience with FBC and FPC and could be considered sort of an amalgamation of the two.
Curious... what programming language are you using to write the SharpBasic compiler ?

The other question I have is, do you have plans to making it opensource ?
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

marcov

• Hero Member
• Posts: 9778
• FPC developer.
Re: how to break out early of a case ?
« Reply #70 on: November 27, 2021, 01:49:32 pm »
Reading the history of FPC again and its struggles to be TP and Delphi compatible, after some 5 months effectively working on the SharpBASIC compiler (SBC), I realize how constraining it must be to model a new compiler after an existing language

Well, if you phrase it as "struggles", the mind seems to be already made up before you get to the end of the sentence :-)

I think it is more a difference in goals. Making a widely used suitable for production compiler is simply hard and iterative.  There is also a significant difference in general between a one person team, and a multi person team. With multiple people there are also multiple opinions, directions etc.

On the other hand, compatibility also provides an Occam's razor to test random ideas and alternate directions with. Often it is also very clear which feature to tackle next (from the compatibility list) and that is very rewarding since that often makes a whole new class of applications and reusing existing code possible.  I can still remember how I excited I felt in the beta period before 2.0 when more and more Delphi code ran without significant modifications

Specially in the early days this keeps language experimentation and the associated discussions to a minimum, and keep your focus on a certain target (like self-compiling/bootstrapping the compiler)

Quote
Honestly, I admire the FPC and also FBC (FreeBASIC) developers for honouring the restrictions/limitations and also the ambiguities of the original language for maximum compatibility. It certainly wouldn't be my cup of tea! I say this as a frequent FPC and FBC user; even a trivial thing such as declaring and initializing multiple variables can be hard/impossible to implement:

If such micro syntax is the biggest problem in a language, you'd be very happy. Keep in mind that the Pascal parsing model was ingrained into the fabric of the project. Problems with foreign language constructs not matching the model might have been considered a small or even negligible sacrifice.

Anyway don't fall for the micro syntax trap, it takes more to make a good language than simply implementing a superset of all shorthands of all languages.

In the end what attracts users is visible ability/proof to complete certain tasks, like sizeable programs in the project's target domain.  It is not simply a matter of bullet lists of micro syntax comparisons.
« Last Edit: November 27, 2021, 01:57:48 pm by marcov »

munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
Re: how to break out early of a case ?
« Reply #71 on: November 27, 2021, 02:23:12 pm »
Well, if you phrase it as "struggles", the mind seems to be already made up before you get to the end of the sentence :-)
Wikipedia:
Quote
The principal problems were that adding processors meant rewriting the code generator, and that the register allocation was based on the principle of always keeping three free registers between building blocks, which was inflexible and difficult to maintain.

For these reasons, the 1.1.x series branched off from the 1.0.x main branch in December 1999. At first, changes were mostly clean-ups and rewrite-redesigns to all parts of the compiler. The code generator and register allocator were also rewritten. Any remaining missing Delphi compatibility was added.

I think it is more a difference in goals. Making a widely used suitable for production compiler is simply hard and iterative.  There is also a significant difference in general between a one person team, and a multi person team. With multiple people there are also multiple opinions, directions etc.
I agree that it is a long way to a compiler successfully targeting multiple platforms. If I read correctly, initial development of the FPC compiler 1993-1995 was also done by one person and the compiler took off after Florian Klämpfl released it. A firm codebase is essential, otherwise, with different opinions, a compiler may actually never be born.

Quote
Honestly, I admire the FPC and also FBC (FreeBASIC) developers for honouring the restrictions/limitations and also the ambiguities of the original language for maximum compatibility. It certainly wouldn't be my cup of tea! I say this as a frequent FPC and FBC user; even a trivial thing such as declaring and initializing multiple variables can be hard/impossible to implement:

If such micro syntax is the biggest problem in a language, you'd be very happy. Keep in mind that the Pascal parsing model was ingrained into the fabric of the project. Foreign constructs not matching the model might not have been considered a small or even negligible sacrifice.

Anyway don't fall for the micro syntax trap, it takes more to make a good language than simply implementing a superset of all shorthands of all languages.

This is not about deliberate micro-syntax (although less verbosity might be a goal), but rather about flexibility. I've seen feature request both for FPC and FBC that appear difficult to implement because of the compiler's design. When starting development of a new compiler, implementing such features right at the start is much easier. SBC is not the first project started due to a new language design wish.

In the end what attracts users is visible ability/proof to complete certain tasks, like sizeable programs in the project's target domain.  It is not simply a matter of bullet lists of micro syntax comparisons.
Agreed.
keep it simple

munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
Re: how to break out early of a case ?
« Reply #72 on: November 27, 2021, 02:28:55 pm »
years of experience with FBC and FPC and could be considered sort of an amalgamation of the two.
Curious... what programming language are you using to write the SharpBasic compiler ?

The other question I have is, do you have plans to making it opensource ?

Initial/current development is in FreeBASIC, simply because I'm a bit more comfortable with the language (I did extensive programming with QuickBASIC in the 80-90s). But I could have chosen FPC as well. It doesn't matter all that much because eventually, when SBC is up to it, I want it to be self-hosting, which is also a great way to extensively test the compiler.

However, key routines such as copying strings, allocating memory etc are all in assembly and part of the sys library. For this reason SBC supported inline ASM early, because it makes it convenient to write these routines. For example:
Code: [Select]
`sub _sb_copystr(p_src: ptr, p_dest: ptr, length: uint)do  asm do    push     esi    push     edi    push     eax    push     ecx    mov      esi, [ebp + 16]              ; source    mov      edi, [ebp + 12]              ; destination    mov      ecx, [ebp + 8]               ; length    cmp      ecx, 0                       ; larger than 0 ?    jle      .out    .copy:    mov      al,[esi]                     ; from source    mov      [edi],al                     ; to destination    inc      esi                          ; increment pointers    inc      edi    dec      ecx                          ; decrement counter    cmp      ecx, 0                       ; done?    jne      .copy    .out:    pop      ecx    pop      eax    pop      edi    pop      esi  end;end;`
I haven't made up my mind yet about the compiler being open-source. If successful, it might be as an example of a compiler without producing a ST.
« Last Edit: November 27, 2021, 03:04:48 pm by munair »
keep it simple

marcov

• Hero Member
• Posts: 9778
• FPC developer.
Re: how to break out early of a case ?
« Reply #73 on: November 27, 2021, 03:31:52 pm »
Well, if you phrase it as "struggles", the mind seems to be already made up before you get to the end of the sentence :-)
Wikipedia:
Quote
The principal problems were that adding processors meant rewriting the code generator, and that the register allocation was based on the principle of always keeping three free registers between building blocks, which was inflexible and difficult to maintain.

For these reasons, the 1.1.x series branched off from the 1.0.x main branch in December 1999. At first, changes were mostly clean-ups and rewrite-redesigns to all parts of the compiler. The code generator and register allocator were also rewritten. Any remaining missing Delphi compatibility was added.

Most of this was compiler architectural and backend rewrite, not due to language compatibility issues. The old backend was hard to stabilize after changes (either bugfixes, new features, new optimizations etc), since there were certain assumptions in the initial codegenerator (a minimum of scratch registers) that were constantly violated with changing codepaths. One could argue the 1.0.x series was only meta-stable.

Due to the length of this rewriting (1999-2003, and another 2  (2003-2005) to bring it to production), the newer language features were implemented in parallel, quite some only in the later stages (the betas, 2003-2005) of the development, after the main rewrite.

I agree that it is a long way to a compiler successfully targeting multiple platforms. If I read correctly, initial development of the FPC compiler 1993-1995 was also done by one person and the compiler took off after Florian Klämpfl released it. A firm codebase is essential, otherwise, with different opinions, a compiler may actually never be born.

All true.

This is not about deliberate micro-syntax (although less verbosity might be a goal), but rather about flexibility.

This is. You more or less explain it here:

I've seen feature request both for FPC and FBC that appear difficult to implement because of the compiler's design. When starting development of a new compiler, implementing such features right at the start is much easier. SBC is not the first project started due to a new language design wish.

It is expanding existing Pascal syntax with unplanned features stolen from other languages, and they were never on the table on the start when a good portion of those languages didn't even exist. Even C++ was still on shaky ground in 93-94.

Agreed.

So what domain do you think Sharpbasic will excel in? What is the big picture?
« Last Edit: November 27, 2021, 05:23:24 pm by marcov »

munair

• Hero Member
• Posts: 781
• compiler developer @SharpBASIC
Re: how to break out early of a case ?
« Reply #74 on: November 27, 2021, 03:54:18 pm »
So what domain do you think Sharpbasic will excel in? What is the big picture?
I haven't given it too much thought yet. The first goal is a stable compiler that generates at least 32 and 64 bit programs for Linux and Windows (GUI bindings might be added at that point). Possibly, programmers that see a use for the compiler on other architectures might want to add the specific code generation to the compiler. There's also the question of OOP with classes or only interfaces as Go has. But SBC will certainly support structures/types (NASM supports them natively). Right now I'm working on strings, then arrays, then types, all needed to make the compiler self-hosting.
keep it simple