Programming Newbie Compiler Question

New to FreeBASIC? Post your questions here.
MrSwiss
Posts: 2596
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Programming Newbie Compiler Question

Postby MrSwiss » Mar 01, 2018 15:31

@caseih,

precisely what I've been referring to: PowerBASIC DOS (ver. 2.1x) ... (I've never used QB).
caseih
Posts: 1230
Joined: Feb 26, 2007 5:32

Re: Programming Newbie Compiler Question

Postby caseih » Mar 01, 2018 15:41

It occurs to me that with FB you really don't need a build system to use separate .bas files. Just passing all the .bas files to FBC at once is sufficient. In fact for some big projects, "fbc -o my.exe *.bas" does the trick. Alternatively, "fbc -o my.exe *.o" if one wants to compile individual files. So there's no reason to continue including whole .bas files with FreeBasic, and should be strongly discouraged, even when not using a build system. Worst case, compiling *.bas is no slower than including the .bas files (nor faster), but is more future-proof if a person moves to an IDE with a build system in the future.
MrSwiss
Posts: 2596
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Programming Newbie Compiler Question

Postby MrSwiss » Mar 01, 2018 16:29

Well, as strongly as you seem to be in favour of build systems, by cons, there are reasons
to avoid them, too.

e.g. FBEdit Project:
- using a: not yet written or completed library, from "main.bas" [is a massive PITA] ...

versus, including the source of the: "to be library" (separate compile tests possible).
caseih
Posts: 1230
Joined: Feb 26, 2007 5:32

Re: Programming Newbie Compiler Question

Postby caseih » Mar 01, 2018 23:28

In my last post, I mentioned using separate .bas files without a build system, and without resorting to including them.
MrSwiss
Posts: 2596
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Programming Newbie Compiler Question

Postby MrSwiss » Mar 02, 2018 1:29

caseih wrote:... without resorting to including them.
Now, this is what I'd call: strange/highly unusual ...

As I'm keeping all sources in one Directory/Folder, your preferred option = impossible!
I don't understand your reasoning (since, it makes no sense) against including another
.bas file.
(I also don't particularly, like build systems, as you might have guessed by now! To me,
they tend to just add another layer of obfuscation, to the build process itself.)

To me it's the same as including a .bi, which then may be loading a lib (just a compiled
source) or the source itself. I'd call that: same difference!
caseih
Posts: 1230
Joined: Feb 26, 2007 5:32

Re: Programming Newbie Compiler Question

Postby caseih » Mar 02, 2018 3:29

To each his own! I probably don't go as far back as you do, but I've never seen include used that way (where include means literally include the file's content) in any language besides BASIC here, and maybe PHP (interpreted language, however). It's definitely strange and highly unusual in C, and almost all other compiled languages I can think of that require a linking step.
marcov
Posts: 2563
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: Programming Newbie Compiler Question

Postby marcov » Mar 02, 2018 8:47

caseih wrote:To each his own! I probably don't go as far back as you do, but I've never seen include used that way (where include means literally include the file's content) in any language besides BASIC here, and maybe PHP (interpreted language, however). It's definitely strange and highly unusual in C, and almost all other compiled languages I can think of that require a linking step.


FreePascal, Delphi and Turbo Pascal can do it (has a typical extension for such cases even; .INC, which can be headers or code). However the modular unit system is far more widely used.

FPC/Lazarus uses it more than Delphi/TP, mostly because its multi platform and widgetset concepts. It is mostly used in the deeper parts of the RTS, and the Lazarus LCL (to build with varying backend widgetsets, the widgetspecific units include common parts). It is also used to break up very large units by topic. (easier with SVN)
Last edited by marcov on Mar 02, 2018 17:09, edited 1 time in total.
MrSwiss
Posts: 2596
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Programming Newbie Compiler Question

Postby MrSwiss » Mar 02, 2018 15:30

Thanks marcov,

that reminds me: .INC was also used in PowerBASIC, ask: José Roca / Paul Squires ...
(they're using it extensively, even in FB -- WinFBE (IDE) also supports it).

(and, probably other late PowerBASIC users, that migrated to FB ...)
caseih
Posts: 1230
Joined: Feb 26, 2007 5:32

Re: Programming Newbie Compiler Question

Postby caseih » Mar 02, 2018 23:50

marcov wrote:However the modular unit system is far more widely used.

Exactly. In languages like C and FB, the .bi file forms the interface of a modular unit and is used in a similar way to Pascal's units (has the same effect). FP's unit's appear to consist of a single file, unlike a C-like language, which works well given the strict separation of interface and implementation in Pascal. I'm not not sure how one interfaces with dlls... are there interface-only files in FP? I'd check the GTK bindings to see, but apparently Sourceforge is down right now.

Anyway, like I said, to each his own. I will admit there are use cases, but from experience on the list in recent days, one has to be very careful when doing it.
Josep Roca
Posts: 329
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Programming Newbie Compiler Question

Postby Josep Roca » Mar 03, 2018 2:29

MrSwiss wrote:Thanks marcov,

that reminds me: .INC was also used in PowerBASIC, ask: José Roca / Paul Squires ...
(they're using it extensively, even in FB -- WinFBE (IDE) also supports it).

(and, probably other late PowerBASIC users, that migrated to FB ...)


We are using include files instead of libraries or modules mainly because it is the only way to have dead code removal with FreeBasic. See: viewtopic.php?t=25237
deltarho[1859]
Posts: 1238
Joined: Jan 02, 2017 0:34
Location: UK

Re: Programming Newbie Compiler Question

Postby deltarho[1859] » Mar 03, 2018 3:25

JosepRoca wrote:We are using include files instead of libraries or modules mainly because it is the only way to have dead code removal with FreeBasic.

I remember the conversation you provided a link to.

Regards the quote above.: Can you prove that?

I tried all sorts of combinations at the time of the conversation but could not prove it. It seemed to me that your explanation was just a weeny bit short of a full explanation and it was that weeny bit short that stopped me from proving it.

Some working code to prove that an exe size does change with your method may persuade folk to use it because I reckon that nobody is.
Josep Roca
Posts: 329
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Programming Newbie Compiler Question

Postby Josep Roca » Mar 03, 2018 12:15

Take for example this code and compile it with the -rr option to generate the intermediate asm file:

Code: Select all

'#CONSOLE ON

SUB Foo1 (BYVAL x AS LONG)
   ? x
END SUB

SUB Foo2 (BYVAL x AS LONG)
   ? x
END SUB

SUB Foo3 (BYVAL x AS LONG)
   ? x
END SUB

Foo1 123

PRINT
PRINT "Press any key..."
SLEEP


The asm file will contain:

Code: Select all

   .intel_syntax noprefix

.section .text
.balign 16

.globl _FOO1@4
_FOO1@4:
push ebp
mov ebp, esp
.Lt_0004:
push 1
push dword ptr [ebp+8]
push 0
call _fb_PrintInt@12
.Lt_0005:
mov esp, ebp
pop ebp
ret 4
.balign 16

.globl _FOO2@4
_FOO2@4:
push ebp
mov ebp, esp
.Lt_0006:
push 1
push dword ptr [ebp+8]
push 0
call _fb_PrintInt@12
.Lt_0007:
mov esp, ebp
pop ebp
ret 4
.balign 16

.globl _FOO3@4
_FOO3@4:
push ebp
mov ebp, esp
.Lt_0008:
push 1
push dword ptr [ebp+8]
push 0
call _fb_PrintInt@12
.Lt_0009:
mov esp, ebp
pop ebp
ret 4
.balign 16
_fb_ctor__1:
.Lt_0002:
push 123
call _FOO1@4
push 1
push 0
call _fb_PrintVoid@8
push 1
push 16
push offset _Lt_000A
call _fb_StrAllocTempDescZEx@8
push eax
push 0
call _fb_PrintString@12
push -1
call _fb_Sleep@4
.Lt_0003:
ret

.section .data
.balign 4
_Lt_000A:   .ascii   "Press any key...\0"

.section .ctors
.int _fb_ctor__1


That is, code for the three SUBs is generated, although we are only calling Foo1.

Now add PRIVATE to the subs:

Code: Select all

'#CONSOLE ON

PRIVATE SUB Foo1 (BYVAL x AS LONG)
   ? x
END SUB

PRIVATE SUB Foo2 (BYVAL x AS LONG)
   ? x
END SUB

PRIVATE SUB Foo3 (BYVAL x AS LONG)
   ? x
END SUB

Foo1 123

PRINT
PRINT "Press any key..."
SLEEP


And the output is:

Code: Select all

   .intel_syntax noprefix

.section .text
.balign 16
_fb_ctor__1:
.Lt_0002:
push 123
call _FOO1@4
push 1
push 0
call _fb_PrintVoid@8
push 1
push 16
push offset _Lt_000A
call _fb_StrAllocTempDescZEx@8
push eax
push 0
call _fb_PrintString@12
push -1
call _fb_Sleep@4
.Lt_0003:
ret
.balign 16
_FOO1@4:
push ebp
mov ebp, esp
.Lt_0004:
push 1
push dword ptr [ebp+8]
push 0
call _fb_PrintInt@12
.Lt_0005:
mov esp, ebp
pop ebp
ret 4

.section .data
.balign 4
_Lt_000A:   .ascii   "Press any key...\0"

.section .ctors
.int _fb_ctor__1


And you can see that only code for Foo1 has been generated.

If you use my WinFBX framework, you will notice that it does not generate very big files even though it contains lots of classes and many hundreds of procedures.
deltarho[1859]
Posts: 1238
Joined: Jan 02, 2017 0:34
Location: UK

Re: Programming Newbie Compiler Question

Postby deltarho[1859] » Mar 03, 2018 12:54

JosepRoca wrote:And you can see that only code for Foo1 has been generated.

Well, it certainly looks that way.

However, when I compile to exe I get 32,256 bytes for private and 32,256 bytes for not private. OK, the Subs are small but I would have expected some difference. Maybe not - maybe we need some larger Subs to see a difference in exe sizes.
Josep Roca
Posts: 329
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Programming Newbie Compiler Question

Postby Josep Roca » Mar 03, 2018 13:54

Well, the size of the exe on disk depends also of the cluster size. Although with this small test the size on disk is the same, the second will have more unused space in the cluster. But if instead of three tiny subs you include a file with many procedures, the difference in size will be very noticeable.
marcov
Posts: 2563
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: Programming Newbie Compiler Question

Postby marcov » Mar 03, 2018 14:04

Josep Roca wrote:We are using include files instead of libraries or modules mainly because it is the only way to have dead code removal with FreeBasic. See: viewtopic.php?t=25237


Using the assembler backend? Maybe FB doesn't emit section data, or you must instruct it to do so?

Does section based smartlinking work on Windows at all with LD? (FPC dropped LD as default over 10 years ago, so I don't know the current situation)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 2 guests