Re: [fpc-pascal] Nesting

2010-09-14 Thread Luiz Americo Pereira Camara

Juha Manninen (gmail) escreveu:

A new Lazarus review :
http://delphimax.wordpress.com/2010/09/13/freepascal-and-lazarus-success-or-
failure/

has this comment about Lazarus source:
---
Abundant use of the Exit() command instead of nesting code in If/then/else. It 
has been proven (last time in Delphi Informant Magazine) that allowing a 
method to nest itself out makes faster code. It is also easier to read and 
study. Exit should of course be used (it must be used in many situations) but 
prudently.

---

Does nesting really create faster code?

  


If i understand right what you mean by nesting, the produced code is 
equal at least in my simple test. See attached.


I dont know for more complex situations

Luiz

program template;
{$Mode ObjFpc}

procedure DoIt;
begin
  WriteLn('DoIt');
end;  

procedure UseExit(i: Integer);
begin
  if i = 0 then Exit;
  DoIt;
end;  

procedure DontUseExit(i: Integer);
begin
  if i <> 0 then
DoIt;  
end;

begin //Main 
  UseExit(1);
  DontUseExit(1); 
end..file "asmExit.pas"

.section .text

.section .text
.balign 4
.balign 4
# [asmExit.pas]
# [5] begin
.globl  P$TEMPLATE_DOIT
P$TEMPLATE_DOIT:
# Temps allocated between ebp-4 and ebp+0
pushl   %ebp
movl%esp,%ebp
subl$4,%esp
movl%ebx,-4(%ebp)
# [6] WriteLn('DoIt');
callfpc_get_output
movl%eax,%ebx
movl$_$PROGRAM$_L10,%ecx
movl%ebx,%edx
movl$0,%eax
callfpc_write_text_shortstr
callFPC_IOCHECK
movl%ebx,%eax
callfpc_writeln_end
callFPC_IOCHECK
# [7] end;
movl-4(%ebp),%ebx
leave
ret

.section .text
.balign 4
.balign 4
# [10] begin
.globl  P$TEMPLATE_USEEXIT$LONGINT
P$TEMPLATE_USEEXIT$LONGINT:
# Temps allocated between ebp-4 and ebp-4
pushl   %ebp
movl%esp,%ebp
subl$4,%esp
# Var i located at ebp-4
movl%eax,-4(%ebp)
# [11] if i = 0 then Exit;
testl   %eax,%eax
je  .L22
# [12] DoIt;
callP$TEMPLATE_DOIT
# [13] DoIt;
callP$TEMPLATE_DOIT
# [14] DoIt;
callP$TEMPLATE_DOIT
# [15] DoIt;
callP$TEMPLATE_DOIT
# [16] DoIt;
callP$TEMPLATE_DOIT
# [17] DoIt;
callP$TEMPLATE_DOIT
# [18] DoIt;
callP$TEMPLATE_DOIT
# [19] DoIt;
callP$TEMPLATE_DOIT
# [20] DoIt;
callP$TEMPLATE_DOIT
# [21] DoIt;
callP$TEMPLATE_DOIT
.L22:
# [22] end;
leave
ret

.section .text
.balign 4
.balign 4
# [25] begin
.globl  P$TEMPLATE_DONTUSEEXIT$LONGINT
P$TEMPLATE_DONTUSEEXIT$LONGINT:
# Temps allocated between ebp-4 and ebp-4
pushl   %ebp
movl%esp,%ebp
subl$4,%esp
# Var i located at ebp-4
movl%eax,-4(%ebp)
# [26] if i <> 0 then
testl   %eax,%eax
je  .L34
# [28] DoIt;
callP$TEMPLATE_DOIT
# [29] DoIt;
callP$TEMPLATE_DOIT
# [30] DoIt;
callP$TEMPLATE_DOIT
# [31] DoIt;
callP$TEMPLATE_DOIT
# [32] DoIt;
callP$TEMPLATE_DOIT
# [33] DoIt;
callP$TEMPLATE_DOIT
# [34] DoIt;
callP$TEMPLATE_DOIT
# [35] DoIt;
callP$TEMPLATE_DOIT
# [36] DoIt;
callP$TEMPLATE_DOIT
# [37] DoIt;
callP$TEMPLATE_DOIT
.L34:
# [39] end;
leave
ret

.section .text
.balign 4
.balign 4
# [41] begin //Main
.globl  PASCALMAIN
PASCALMAIN:
.globl  _main
_main:
# Temps allocated between ebp+0 and ebp+0
pushl   %ebp
movl%esp,%ebp
callFPC_INITIALIZEUNITS
# [42] UseExit(1);
movl$1,%eax
callP$TEMPLATE_USEEXIT$LONGINT
# [43] DontUseExit(1);
movl$1,%eax
callP$TEMPLATE_DONTUSEEXIT$LONGINT
# [44] end.
callFPC_DO_EXIT
leave
ret
.balign 4

.section .data
.ascii  "FPC 2.0.4 [2006/08/21] for i386 - Win32"
.balign 16
.balign 16
.globl  THREADVARLIST_P$TEMPLATE
THREADVARLIST_P$TEMPLATE:
.long   0
.balign 4
.globl  FPC_THREADVARTABLES
FPC_THREADVARTABLES:
.long   3
.long   THREADVARLIST_SYSTEM
.long   THREADVARLIST_OBJPAS
.long   THREADVARLIST_P$TEMPLATE
.balign 4
.globl  FPC_RESOURCESTRINGTABLES
FPC_RESOURCESTRINGTABLES:
.long   0
.balign 4
.globl  INITFINAL
INITFINAL:
.long   2,0
.long   INIT$_SYSTEM
.long   FINALIZE$_SYSTEM
.long   INIT$_OBJPAS
.long   FINALIZE$_OBJPAS

.section .data
.balign 4
.globl  __stklen
__stklen:
.long   262144

.section .data
.balign 4
.globl  __heapsize
__heapsize:
.long   0

.section .data

.section .data
.balign 4
.globl  _$PROGRAM$_L10
_$PROGRAM$_L10:
.ascii  "\004DoIt\000"

.section .data

.section .data

.section .bss

__

[fpc-pascal] Interface with NASM

2010-09-14 Thread leledumbo

I need to teach assembler, and the easiest way would be to create a driver
and simple I/O library in high level language that can be called from
assembler so that students can focus in programming with assembler more than
dealing with system specific features. It's been quite a while since I go
down to this level, so I might miss something.

Here's the IO library (asmio.pas):

unit asmio;

interface

procedure printc(const c: Char); pascal;
procedure printi(const i: LongInt); pascal;

implementation

procedure printc(const c: Char); pascal; [public, alias: 'printc'];
begin
  Write(c);
end;

procedure printi(const i: LongInt); pascal; [public, alias: 'printi'];
begin
  Write(i);
end;

end.

The driver program (driver.pas):

program driver;

{$L asmio.o}
{$L test.o}

procedure tes; pascal; external name 'tes';

begin
  tes;
end.

And example assembler program which suppose to print 'A' (test.asm):

[bits 32]
extern printc,printi

segment .text
global tes
tes:
  push ebp
  mov  ebp,esp
  push 65
  call printc
  leave
  ret

build command:
nasm -f coff test.asm
fpc asmio.pas
fpc -o test driver.pas

As you can see, I didn't use any optimizations because AFAIR FPC would
change parameter passing mechanism when a certain optimizations are
performed. Of course if it works I won't ask here, but it doesn't. I got
access violation instead. However changing the driver program to:

procedure printc(const c: Char); pascal; external name 'printc';

begin
  printc('A');
end.

generates the same instruction sequence for calling printc:

push 65
call printc

I've tried omitting the stackframe (with the corresponding modifier in
driver program) but no effect. I wonder where my mistake is...

Additional information:
I've made it to work for non IO functions

-- 
View this message in context: 
http://free-pascal-general.1045716.n5.nabble.com/Interface-with-NASM-tp2838671p2838671.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Basics of profiling -- some comments on fpprofiler bugs

2010-09-14 Thread Darius Blaszyk
On Mon, 2010-09-06 at 16:58 +0300, Žilvinas Ledas wrote:
> On 2010-09-06 15:48, Darius Blaszyk wrote:
> > On Mon, 2010-09-06 at 13:01 +0200, Graeme Geldenhuys wrote:
> >> Recently I took a look at Darius's old project 'fpprofiler. Links
> >> below.  It was over 2 years since anybody worked on it, and riddled
> >> with bugs. I fixed all memory leaks, added some improvements, and used
> >> the new fcl-passrc code included with FPC Trunk (though it only uses
> >> the tokenizer for speed reasons, so FPC 2.4.x should work to - maybe
> >> with minor one line code change).
> > I've tested fpprofiler on FPC 2.4 and it works, provided the fcl-passrc
> > is from 2.5.1. I'll add this folder as external to the SVN repository.
> Few notes (parts that need some improvement):
> 1) fpp.pp needs at least tkAsm in "case tokenlist[i].token of" in 
> "procedure ModifyCode(AFileName: string; tokenlist: TPasTokenList);"
> 2) it works wrong with ifdef's (it leaves only ELSE code) -- (I modified 
> my copy to output comments as well and had to "modify" pscanner.pp to 
> have directives as simple comments);
> 3) in "procedure TPasTokenList.SaveToFile(const AFileName: string);" no 
> need for writeLN's - writes are enough;
> 4) Asm procedures are handeled wrong (fpprof_exit_profile; is inserted 
> before end;)
> function IsCPUID_Available : Boolean; register; assembler;
> asm
>...
> end;
> 5) records with cases (or some other type) is handeled wrong 
> (fpprof_exit_profile; is inserted before end;) -- I don't remember what 
> type it was exactly.
> 
> In my opinion 2) is the biggest problem now :)
> 
> P. S. These comments are with Graemes changes.
> P. P. S. Good luck with improvements and updates!

Could you please create patches or at least some simple testcases for
each described issue? I will add them to SVN to prevent regressions in
the future. Please post them on mantis.

Darius

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Help translating C++ to Object Pascal

2010-09-14 Thread Reimar Grabowski
On Tue, 14 Sep 2010 11:50:31 +0200
Graeme Geldenhuys  wrote:

> Hi,
> 
> In a C++ method as shown below, does the '... return True;' immediately
> exit the HandleClientMessage method
Yes.

R.
-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Help translating C++ to Object Pascal

2010-09-14 Thread David W Noon
On Tue, 14 Sep 2010 11:50:31 +0200, Graeme Geldenhuys wrote about
[fpc-pascal] Help translating C++ to Object Pascal:

> In a C++ method as shown below, does the '... return True;'
> immediately exit the HandleClientMessage method, or will it still
> continue processing the remainder of the method (just exiting the
> current code block)?

It will return directly to the calling routine.  The following
statement:

>   return OXCompositeFrame::HandleClientMessage(event);

will be ignored.
-- 
Regards,

Dave  [RLU #314465]
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
david.w.n...@ntlworld.com (David W Noon)
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


signature.asc
Description: PGP signature
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

[fpc-pascal] heap size & free heap

2010-09-14 Thread Juha Manninen (gmail)
Hi

Maybe someone here knows...

I built Lazarus with "-gh", started it and closed without doing anything 
else.

---
Heap dump by heaptrc unit
717866 memory blocks allocated : 81646458/83984784
717866 memory blocks freed : 81646458/83984784
0 unfreed memory blocks : 0
True heap size : 851968
True free heap : 942784
Should be : 851968
---

What are the 3 last lines about?

I am running on 64-bit Linux.

Juha
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Help translating C++ to Object Pascal

2010-09-14 Thread Graeme Geldenhuys
Hi,

In a C++ method as shown below, does the '... return True;' immediately
exit the HandleClientMessage method, or will it still continue processing
the remainder of the method (just exiting the current code block)?



int OXMainFrame::HandleClientMessage(XClientMessageEvent *event) {

  if (_dndManager) {
if (_dndManager->HandleClientMessage(event)) return True;
  }

  if ((event->format == 32) && (event->data.l[0] == WM_DELETE_WINDOW)) {
CloseWindow();
return True;
  }

  return OXCompositeFrame::HandleClientMessage(event);
}




Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Re: Nesting

2010-09-14 Thread leledumbo

I sometimes use that kind of Exit because it's cheaper in code, though it
makes the code flow less structured. Probably because C family bad coding
doctrine (my university is a C family slave). But when I'm in full
consciousness, I use if/else.
-- 
View this message in context: 
http://free-pascal-general.1045716.n5.nabble.com/Nesting-tp2837567p2838663.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Question mark?

2010-09-14 Thread Felipe Monteiro de Carvalho
Does it work without {$ Codepage UTF8} ?

-- 
Felipe Monteiro de Carvalho
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Basics of profiling -- some comments on fpprofiler bugs

2010-09-14 Thread Darius Blaszyk
On Mon, 2010-09-06 at 16:58 +0300, Žilvinas Ledas wrote:
> 3) in "procedure TPasTokenList.SaveToFile(const AFileName: string);"
> no 
> need for writeLN's - writes are enough; 

Fixed in rev2492

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Interface with NASM

2010-09-14 Thread Matthias K.
Hi,
I've played around with your code and didn't get usefull results
either at first glance. Instead of putting the
printc test into driver, I've wrote a pure pascal unit which does the
same as your test.asm, compiled it, disassembled it and modfied the
nasm program. That worked.

I'm on a different target machine and pascal calling convention
doesn't work at all so this may not be usefull for you, but at least
there is a chance it may be, so here is the pure pascal test.asm. If
this doesn't link correctly it may be another Problem (!so Compiler
Version/svn revision and Target would be good to know!)

---
unit testp;

interface

procedure tes; pascal;

implementation

procedure printchar( c: Char ); pascal; external name 'printc';

procedure tes; pascal; [public, alias: 'tes'];
begin
  printchar( 'A' );
end;

end.
---

Btw. It would be very interesting to know the solution for your
Problem if its already fixed :)
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Help translating C++ to Object Pascal

2010-09-14 Thread Sven Barth

Am 14.09.2010 11:50, schrieb Graeme Geldenhuys:

Hi,

In a C++ method as shown below, does the '... return True;' immediately
exit the HandleClientMessage method, or will it still continue processing
the remainder of the method (just exiting the current code block)?



int OXMainFrame::HandleClientMessage(XClientMessageEvent *event) {

   if (_dndManager) {
 if (_dndManager->HandleClientMessage(event)) return True;
   }

   if ((event->format == 32)&&  (event->data.l[0] == WM_DELETE_WINDOW)) {
 CloseWindow();
 return True;
   }

   return OXCompositeFrame::HandleClientMessage(event);
}


"return Something;" in C/C++ is equivalent to "Exit(Something);" in 
FPC's Pascal.


Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Help translating C++ to Object Pascal

2010-09-14 Thread Graeme Geldenhuys
Thanks to all that replied. My C/C++ is a bit rusty. :)


Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal