123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446 |
- {
- $Id$
- This file is part of the Free Pascal run time library.
- Copyright (c) 1999-2000 by the Free Pascal development team.
- Processor dependent implementation for the system unit for
- intel i386+
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- {$asmmode ATT}
- {****************************************************************************
- Primitives
- ****************************************************************************}
- function geteipasebx : pointer;assembler;[public,alias:'FPC_GETEIPINEBX'];
- asm
- movl (%esp),%ebx
- ret
- end;
- {$define FPC_SYSTEM_HAS_MOVE}
- procedure Move(const source;var dest;count:longint);assembler;
- var
- saveesi,saveedi : longint;
- asm
- movl %edi,saveedi
- movl %esi,saveesi
- movl dest,%edi
- movl source,%esi
- movl %edi,%eax
- movl count,%edx
- { check for zero or negative count }
- cmpl $0,%edx
- jle .LMoveEnd
- { Check for back or forward }
- sub %esi,%eax
- jz .LMoveEnd { Do nothing when source=dest }
- jc .LFMove { Do forward, dest<source }
- cmp %edx,%eax
- jb .LBMove { Dest is in range of move, do backward }
- { Forward Copy }
- .LFMove:
- cld
- cmpl $15,%edx
- jl .LFMove1
- movl %edi,%ecx { Align on 32bits }
- negl %ecx
- andl $3,%ecx
- subl %ecx,%edx
- rep
- movsb
- movl %edx,%ecx
- andl $3,%edx
- shrl $2,%ecx
- rep
- movsl
- .LFMove1:
- movl %edx,%ecx
- rep
- movsb
- jmp .LMoveEnd
- { Backward Copy }
- .LBMove:
- std
- addl %edx,%esi
- addl %edx,%edi
- movl %edi,%ecx
- decl %esi
- decl %edi
- cmpl $15,%edx
- jl .LBMove1
- negl %ecx { Align on 32bits }
- andl $3,%ecx
- subl %ecx,%edx
- rep
- movsb
- movl %edx,%ecx
- andl $3,%edx
- shrl $2,%ecx
- subl $3,%esi
- subl $3,%edi
- rep
- movsl
- addl $3,%esi
- addl $3,%edi
- .LBMove1:
- movl %edx,%ecx
- rep
- movsb
- cld
- .LMoveEnd:
- movl saveedi,%edi
- movl saveesi,%esi
- end;
- {$define FPC_SYSTEM_HAS_FILLCHAR}
- Procedure FillChar(var x;count:longint;value:byte);assembler;
- var
- saveedi : longint;
- asm
- movl %edi,saveedi
- cld
- movl x,%edi
- movb value,%al
- movl count,%ecx
- { check for zero or negative count }
- cmpl $0,%ecx
- jle .LFillEnd
- cmpl $7,%ecx
- jl .LFill1
- movb %al,%ah
- movl %eax,%edx
- shll $16,%eax
- movw %dx,%ax
- movl %ecx,%edx
- movl %edi,%ecx { Align on 32bits }
- negl %ecx
- andl $3,%ecx
- subl %ecx,%edx
- rep
- stosb
- movl %edx,%ecx
- andl $3,%edx
- shrl $2,%ecx
- rep
- stosl
- movl %edx,%ecx
- .LFill1:
- rep
- stosb
- .LFillEnd:
- movl saveedi,%edi
- end;
- {$define FPC_SYSTEM_HAS_FILLWORD}
- procedure fillword(var x;count : longint;value : word);assembler;
- var
- saveedi : longint;
- asm
- movl %edi,saveedi
- movl x,%edi
- movl count,%ecx
- { check for zero or negative count }
- cmpl $0,%ecx
- jle .LFillWordEnd
- movzwl value,%eax
- movl %eax,%edx
- shll $16,%eax
- orl %edx,%eax
- movl %ecx,%edx
- shrl $1,%ecx
- cld
- rep
- stosl
- movl %edx,%ecx
- andl $1,%ecx
- rep
- stosw
- .LFillWordEnd:
- movl saveedi,%edi
- end;
- {$define FPC_SYSTEM_HAS_FILLDWORD}
- procedure filldword(var x;count : longint;value : dword);assembler;
- var
- saveedi : longint;
- asm
- movl %edi,saveedi
- movl x,%edi
- movl count,%ecx
- { check for zero or negative count }
- cmpl $0,%ecx
- jle .LFillDWordEnd
- movl value,%eax
- cld
- rep
- stosl
- .LFillDWordEnd:
- movl saveedi,%edi
- end;
- {$define FPC_SYSTEM_HAS_INDEXBYTE}
- function IndexByte(Const buf;len:longint;b:byte):longint; assembler;
- var
- saveedi : longint;
- asm
- movl %edi,saveedi
- xorl %eax,%eax
- movl Len,%ecx // Load len
- movl Buf,%edi // Load String
- testl %ecx,%ecx
- jz .Lready
- cld
- movl %ecx,%edx // Copy for easy manipulation
- movb b,%al
- repne
- scasb
- jne .Lcharposnotfound
- incl %ecx
- subl %ecx,%edx
- movl %edx,%eax
- jmp .Lready
- .Lcharposnotfound:
- movl $-1,%eax
- .Lready:
- movl saveedi,%edi
- end;
- {$define FPC_SYSTEM_HAS_INDEXWORD}
- function Indexword(Const buf;len:longint;b:word):longint; assembler;
- var
- saveedi : longint;
- asm
- movl %edi,saveedi
- xorl %eax,%eax
- movl Len,%ecx // Load len
- movl Buf,%edi // Load String
- testl %ecx,%ecx
- jz .Lready
- cld
- movl %ecx,%edx // Copy for easy manipulation
- movw b,%ax
- repne
- scasw
- jne .Lcharposnotfound
- incl %ecx
- subl %ecx,%edx
- movl %edx,%eax
- jmp .Lready
- .Lcharposnotfound:
- movl $-1,%eax
- .Lready:
- movl saveedi,%edi
- end;
- {$define FPC_SYSTEM_HAS_INDEXDWORD}
- function IndexDWord(Const buf;len:longint;b:DWord):longint; assembler;
- var
- saveedi : longint;
- asm
- movl %edi,saveedi
- xorl %eax,%eax
- movl Len,%ecx // Load len
- movl Buf,%edi // Load String
- testl %ecx,%ecx
- jz .Lready
- cld
- movl %ecx,%edx // Copy for easy manipulation
- movl b,%eax
- repne
- scasl
- jne .Lcharposnotfound
- incl %ecx
- subl %ecx,%edx
- movl %edx,%eax
- jmp .Lready
- .Lcharposnotfound:
- movl $-1,%eax
- .Lready:
- movl saveedi,%edi
- end;
- {$define FPC_SYSTEM_HAS_COMPAREBYTE}
- function CompareByte(Const buf1,buf2;len:longint):longint; assembler;
- var
- saveesi,saveedi : longint;
- asm
- movl %edi,saveedi
- movl %esi,saveesi
- cld
- movl len,%eax
- movl buf2,%esi { Load params}
- movl buf1,%edi
- testl %eax,%eax {We address -1(%esi), so we have to deal with len=0}
- je .LCmpbyteExit
- cmpl $7,%eax {<7 not worth aligning and go through all trouble}
- jl .LCmpbyte2
- movl %edi,%ecx { Align on 32bits }
- negl %ecx { calc bytes to align (%edi and 3) xor 3= -%edi and 3}
- andl $3,%ecx
- subl %ecx,%eax { Subtract from number of bytes to go}
- orl %ecx,%ecx
- rep
- cmpsb {The actual 32-bit Aligning}
- jne .LCmpbyte3
- movl %eax,%ecx {bytes to do, divide by 4}
- andl $3,%eax {remainder}
- shrl $2,%ecx {The actual division}
- orl %ecx,%ecx {Sets zero flag if ecx=0 -> no cmp}
- rep
- cmpsl
- je .LCmpbyte2 { All equal? then to the left over bytes}
- movl $4,%eax { Not equal. Rescan the last 4 bytes bytewise}
- subl %eax,%esi
- subl %eax,%edi
- .LCmpbyte2:
- movl %eax,%ecx {bytes still to (re)scan}
- orl %eax,%eax {prevent disaster in case %eax=0}
- rep
- cmpsb
- .LCmpbyte3:
- movzbl -1(%esi),%ecx
- movzbl -1(%edi),%eax // Compare failing (or equal) position
- subl %ecx,%eax
- .LCmpbyteExit:
- movl saveedi,%edi
- movl saveesi,%esi
- end;
- {$define FPC_SYSTEM_HAS_COMPAREWORD}
- function CompareWord(Const buf1,buf2;len:longint):longint; assembler;
- var
- saveesi,saveedi,saveebx : longint;
- asm
- movl %edi,saveedi
- movl %esi,saveesi
- movl %ebx,saveebx
- cld
- movl len,%eax
- movl buf2,%esi { Load params}
- movl buf1,%edi
- testl %eax,%eax {We address -2(%esi), so we have to deal with len=0}
- je .LCmpwordExit
- cmpl $5,%eax {<5 (3 bytes align + 4 bytes cmpsl = 4 words}
- jl .LCmpword2 { not worth aligning and go through all trouble}
- movl (%edi),%ebx // Compare alignment bytes.
- cmpl (%esi),%ebx
- jne .LCmpword2 // Aligning will go wrong already. Max 2 words will be scanned Branch NOW
- shll $1,%eax {Convert word count to bytes}
- movl %edi,%edx { Align comparing is already done, so simply add}
- negl %edx { calc bytes to align -%edi and 3}
- andl $3,%edx
- addl %edx,%esi { Skip max 3 bytes alignment}
- addl %edx,%edi
- subl %edx,%eax { Subtract from number of bytes to go}
- movl %eax,%ecx { Make copy of bytes to go}
- andl $3,%eax { Calc remainder (mod 4) }
- andl $1,%edx { %edx is 1 if array not 2-aligned, 0 otherwise}
- shrl $2,%ecx { divide bytes to go by 4, DWords to go}
- orl %ecx,%ecx { Sets zero flag if ecx=0 -> no cmp}
- rep { Compare entire DWords}
- cmpsl
- je .LCmpword2a { All equal? then to the left over bytes}
- movl $4,%eax { Not equal. Rescan the last 4 bytes bytewise}
- subl %eax,%esi { Go back one DWord}
- subl %eax,%edi
- incl %eax {if not odd then this does nothing, else it makes
- sure that adding %edx increases from 2 to 3 words}
- .LCmpword2a:
- subl %edx,%esi { Subtract alignment}
- subl %edx,%edi
- addl %edx,%eax
- shrl $1,%eax
- .LCmpword2:
- movl %eax,%ecx {words still to (re)scan}
- orl %eax,%eax {prevent disaster in case %eax=0}
- rep
- cmpsw
- .LCmpword3:
- movzwl -2(%esi),%ecx
- movzwl -2(%edi),%eax // Compare failing (or equal) position
- subl %ecx,%eax // calculate end result.
- .LCmpwordExit:
- movl saveedi,%edi
- movl saveesi,%esi
- movl saveebx,%ebx
- end;
- {$define FPC_SYSTEM_HAS_COMPAREDWORD}
- function CompareDWord(Const buf1,buf2;len:longint):longint; assembler;
- var
- saveesi,saveedi,saveebx : longint;
- asm
- movl %edi,saveedi
- movl %esi,saveesi
- movl %ebx,saveebx
- cld
- movl len,%eax
- movl buf2,%esi { Load params}
- movl buf1,%edi
- testl %eax,%eax {We address -2(%esi), so we have to deal with len=0}
- je .LCmpDwordExit
- cmpl $3,%eax {<3 (3 bytes align + 4 bytes cmpsl) = 2 DWords}
- jl .LCmpDword2 { not worth aligning and go through all trouble}
- movl (%edi),%ebx // Compare alignment bytes.
- cmpl (%esi),%ebx
- jne .LCmpDword2 // Aligning will go wrong already. Max 2 words will be scanned Branch NOW
- shll $2,%eax {Convert word count to bytes}
- movl %edi,%edx { Align comparing is already done, so simply add}
- negl %edx { calc bytes to align -%edi and 3}
- andl $3,%edx
- addl %edx,%esi { Skip max 3 bytes alignment}
- addl %edx,%edi
- subl %edx,%eax { Subtract from number of bytes to go}
- movl %eax,%ecx { Make copy of bytes to go}
- andl $3,%eax { Calc remainder (mod 4) }
- shrl $2,%ecx { divide bytes to go by 4, DWords to go}
- orl %ecx,%ecx { Sets zero flag if ecx=0 -> no cmp}
- rep { Compare entire DWords}
- cmpsl
- je .LCmpDword2a { All equal? then to the left over bytes}
- movl $4,%eax { Not equal. Rescan the last 4 bytes bytewise}
- subl %eax,%esi { Go back one DWord}
- subl %eax,%edi
- addl $3,%eax {if align<>0 this causes repcount to be 2}
- .LCmpDword2a:
- subl %edx,%esi { Subtract alignment}
- subl %edx,%edi
- addl %edx,%eax
- shrl $2,%eax
- .LCmpDword2:
- movl %eax,%ecx {words still to (re)scan}
- orl %eax,%eax {prevent disaster in case %eax=0}
- rep
- cmpsl
- .LCmpDword3:
- movzwl -4(%esi),%ecx
- movzwl -4(%edi),%eax // Compare failing (or equal) position
- subl %ecx,%eax // calculate end result.
- .LCmpDwordExit:
- movl saveedi,%edi
- movl saveesi,%esi
- movl saveebx,%ebx
- end;
- {$define FPC_SYSTEM_HAS_INDEXCHAR0}
- function IndexChar0(Const buf;len:longint;b:Char):longint; assembler;
- var
- saveesi,saveebx : longint;
- asm
- movl %esi,saveesi
- movl %ebx,saveebx
- // Can't use scasb, or will have to do it twice, think this
- // is faster for small "len"
- movl Buf,%esi // Load address
- movl len,%edx // load maximal searchdistance
- movzbl b,%ebx // Load searchpattern
- testl %edx,%edx
- je .LFound
- xorl %ecx,%ecx // zero index in Buf
- xorl %eax,%eax // To make DWord compares possible
- .LLoop:
- movb (%esi),%al // Load byte
- cmpb %al,%bl
- je .LFound // byte the same?
- incl %ecx
- incl %esi
- cmpl %edx,%ecx // Maximal distance reached?
- je .LNotFound
- testl %eax,%eax // Nullchar = end of search?
- jne .LLoop
- .LNotFound:
- movl $-1,%ecx // Not found return -1
- .LFound:
- movl %ecx,%eax
- movl saveesi,%esi
- movl saveebx,%ebx
- end;
- {****************************************************************************
- Object Helpers
- ****************************************************************************}
- {$ifndef HAS_GENERICCONSTRUCTOR}
- {$define FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
- procedure fpc_help_constructor; assembler; [public,alias:'FPC_HELP_CONSTRUCTOR']; {$ifdef hascompilerproc} compilerproc; {$endif}
- asm
- { Entry without preamble, since we need the ESP of the constructor
- Stack (relative to %ebp):
- 12 Self
- 8 VMT-Address
- 4 main programm-Addr
- 0 %ebp
- edi contains the vmt position
- }
- { eax isn't touched anywhere, so it doesn't have to reloaded }
- movl 8(%ebp),%eax
- { initialise self ? }
- orl %esi,%esi
- jne .LHC_4
- { get memory, but save register first temporary variable }
- subl $4,%esp
- movl %esp,%esi
- { Save Register}
- pushal
- {$ifdef valuegetmem}
- { esi can be destroyed in fpc_getmem!!! (JM) }
- pushl %esi
- {$endif valuegetmem}
- { Memory size }
- pushl (%eax)
- {$ifdef valuegetmem}
- call fpc_getmem
- popl %esi
- movl %eax,(%esi)
- {$else valuegetmem}
- pushl %esi
- call AsmGetMem
- {$endif valuegetmem}
- movl $-1,8(%ebp)
- popal
- { Avoid 80386DX bug }
- nop
- { Memory position to %esi }
- movl (%esi),%esi
- addl $4,%esp
- { If no memory available : fail() }
- orl %esi,%esi
- jz .LHC_5
- { init self for the constructor }
- movl %esi,12(%ebp)
- { jmp not necessary anymore because next instruction is disabled (JM)
- jmp .LHC_6 }
- { Why was the VMT reset to zero here ????
- I need it fail to know if I should
- zero the VMT field in static objects PM }
- .LHC_4:
- { movl $0,8(%ebp) }
- .LHC_6:
- { is there a VMT address ? }
- orl %eax,%eax
- jnz .LHC_7
- { In case the constructor doesn't do anything, the Zero-Flag }
- { can't be put, because this calls Fail() }
- incl %eax
- ret
- .LHC_7:
- { set zero inside the object }
- pushal
- cld
- movl (%eax),%ecx
- movl %esi,%edi
- movl %ecx,%ebx
- xorl %eax,%eax
- shrl $2,%ecx
- andl $3,%ebx
- rep
- stosl
- movl %ebx,%ecx
- rep
- stosb
- popal
- { avoid the 80386DX bug }
- nop
- { set the VMT address for the new created object }
- { the offset is in %edi since the calling and has not been changed !! }
- movl %eax,(%esi,%edi,1)
- testl %eax,%eax
- .LHC_5:
- end;
- {$define FPC_SYSTEM_HAS_FPC_HELP_FAIL}
- procedure fpc_help_fail;assembler;[public,alias:'FPC_HELP_FAIL']; {$ifdef hascompilerproc} compilerproc; {$endif}
- { should be called with a object that needs to be
- freed if VMT field is at -1
- %edi contains VMT offset in object again }
- asm
- testl %esi,%esi
- je .LHF_1
- cmpl $-1,8(%ebp)
- je .LHF_2
- { reset vmt field to zero for static instances }
- cmpl $0,8(%ebp)
- je .LHF_3
- { main constructor, we can zero the VMT field now }
- movl $0,(%esi,%edi,1)
- .LHF_3:
- { we zero esi to indicate failure }
- xorl %esi,%esi
- jmp .LHF_1
- .LHF_2:
- { get vmt address in eax }
- movl (%esi,%edi,1),%eax
- movl %esi,12(%ebp)
- { push object position }
- {$ifdef valuefreemem}
- pushl %esi
- call fpc_freemem
- {$else valuefreemem}
- leal 12(%ebp),%eax
- pushl %eax
- call AsmFreeMem
- {$endif valuefreemem}
- { set both object places to zero }
- xorl %esi,%esi
- movl %esi,12(%ebp)
- .LHF_1:
- end;
- {$define FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
- procedure fpc_help_destructor;assembler;[public,alias:'FPC_HELP_DESTRUCTOR']; {$ifdef hascompilerproc} compilerproc; {$endif}
- asm
- { Stack (relative to %ebp):
- 12 Self
- 8 VMT-Address
- 4 Main program-Addr
- 0 %ebp
- edi contains the vmt position
- }
- pushal
- { Should the object be resolved ? }
- movl 8(%ebp),%eax
- orl %eax,%eax
- jz .LHD_3
- { Yes, get size from SELF! }
- movl 12(%ebp),%eax
- { get VMT-pointer (from Self) to %ebx }
- { the offset is in %edi since the calling and has not been changed !! }
- movl (%eax,%edi,1),%ebx
- { I think for precaution }
- { that we should clear the VMT here }
- movl $0,(%eax,%edi,1)
- {$ifdef valuefreemem}
- { Freemem }
- pushl %eax
- call fpc_freemem
- {$else valuefreemem}
- { temporary Variable }
- subl $4,%esp
- movl %esp,%edi
- { SELF }
- movl %eax,(%edi)
- pushl %edi
- call AsmFreeMem
- addl $4,%esp
- {$endif valuefreemem}
- .LHD_3:
- popal
- { avoid the 80386DX bug }
- nop
- end;
- {$define FPC_SYSTEM_HAS_FPC_NEW_CLASS}
- procedure fpc_new_class;assembler;[public,alias:'FPC_NEW_CLASS']; {$ifdef hascompilerproc} compilerproc; {$endif}
- asm
- { to be sure in the future, we save also edit }
- pushl %edi
- { create class ? }
- movl 8(%ebp),%edi
- { if we test eax later without calling newinstance }
- { it must have a value <>0 }
- movl $1,%eax
- testl %edi,%edi
- jz .LNEW_CLASS1
- { save registers !! }
- pushl %ebx
- pushl %ecx
- pushl %edx
- { esi contains the vmt }
- pushl %esi
- { call newinstance (class method!) }
- call *52{vmtNewInstance}(%esi)
- popl %edx
- popl %ecx
- popl %ebx
- { newinstance returns a pointer to the new created }
- { instance in eax }
- { load esi and insert self }
- movl %eax,%esi
- .LNEW_CLASS1:
- movl %esi,8(%ebp)
- testl %eax,%eax
- popl %edi
- end;
- { Internal alias that can be reference from asm code }
- procedure int_dispose_class;external name 'FPC_DISPOSE_CLASS';
- {$define FPC_SYSTEM_HAS_FPC_DISPOSE_CLASS}
- procedure fpc_dispose_class;assembler;[public,alias:'FPC_DISPOSE_CLASS']; {$ifdef hascompilerproc} compilerproc; {$endif}
- asm
- { to be sure in the future, we save also edit }
- pushl %edi
- { destroy class ? }
- movl 12(%ebp),%edi
- testl %edi,%edi
- jz .LDISPOSE_CLASS1
- { no inherited call }
- movl (%esi),%edi
- { save registers !! }
- pushl %eax
- pushl %ebx
- pushl %ecx
- pushl %edx
- { push self }
- pushl %esi
- { call freeinstance }
- call *56{vmtFreeInstance}(%edi)
- popl %edx
- popl %ecx
- popl %ebx
- popl %eax
- .LDISPOSE_CLASS1:
- popl %edi
- end;
- {$define FPC_SYSTEM_HAS_FPC_HELP_FAIL_CLASS}
- procedure fpc_help_fail_class;assembler;[public,alias:'FPC_HELP_FAIL_CLASS']; {$ifdef hascompilerproc} compilerproc; {$endif}
- { a non zero class must allways be disposed
- VMT is allways at pos 0 }
- asm
- testl %esi,%esi
- je .LHFC_1
- { can't use the compilerproc version as that will generate a
- reference instead of a symbol }
- call int_dispose_class
- { set both object places to zero }
- xorl %esi,%esi
- movl %esi,8(%ebp)
- .LHFC_1:
- end;
- {$define FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
- { we want the stack for debugging !! PM }
- procedure fpc_check_object(obj : pointer);[public,alias:'FPC_CHECK_OBJECT']; {$ifdef hascompilerproc} compilerproc; {$endif}
- begin
- asm
- pushl %edi
- movl obj,%edi
- pushl %eax
- { Here we must check if the VMT pointer is nil before }
- { accessing it... }
- testl %edi,%edi
- jz .Lco_re
- movl (%edi),%eax
- addl 4(%edi),%eax
- jz .Lco_ok
- .Lco_re:
- pushl $210
- call HandleError
- .Lco_ok:
- popl %eax
- popl %edi
- { the adress is pushed : it needs to be removed from stack !! PM }
- end;{ of asm }
- end;
- {$define FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
- procedure fpc_check_object_ext;assembler;[public,alias:'FPC_CHECK_OBJECT_EXT']; {$ifdef hascompilerproc} compilerproc; {$endif}
- { checks for a correct vmt pointer }
- { deeper check to see if the current object is }
- { really related to the true }
- asm
- pushl %ebp
- movl %esp,%ebp
- pushl %edi
- movl 8(%ebp),%edi
- pushl %ebx
- movl 12(%ebp),%ebx
- pushl %eax
- { Here we must check if the VMT pointer is nil before }
- { accessing it... }
- .Lcoext_obj:
- testl %edi,%edi
- jz .Lcoext_re
- movl (%edi),%eax
- addl 4(%edi),%eax
- jnz .Lcoext_re
- cmpl %edi,%ebx
- je .Lcoext_ok
- .Lcoext_vmt:
- movl 8(%edi),%eax
- cmpl %ebx,%eax
- je .Lcoext_ok
- movl %eax,%edi
- jmp .Lcoext_obj
- .Lcoext_re:
- pushl $219
- call HandleError
- .Lcoext_ok:
- popl %eax
- popl %ebx
- popl %edi
- { the adress and vmt were pushed : it needs to be removed from stack !! PM }
- popl %ebp
- ret $8
- end;
- {$endif HAS_GENERICCONSTRUCTOR}
- {****************************************************************************
- String
- ****************************************************************************}
- {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
- function fpc_shortstr_to_shortstr(len:longint; const sstr: shortstring): shortstring; [public,alias: 'FPC_SHORTSTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
- begin
- asm
- cld
- movl __RESULT,%edi
- movl sstr,%esi
- xorl %eax,%eax
- movl len,%ecx
- lodsb
- cmpl %ecx,%eax
- jbe .LStrCopy1
- movl %ecx,%eax
- .LStrCopy1:
- stosb
- cmpl $7,%eax
- jl .LStrCopy2
- movl %edi,%ecx { Align on 32bits }
- negl %ecx
- andl $3,%ecx
- subl %ecx,%eax
- rep
- movsb
- movl %eax,%ecx
- andl $3,%eax
- shrl $2,%ecx
- rep
- movsl
- .LStrCopy2:
- movl %eax,%ecx
- rep
- movsb
- end ['ESI','EDI','EAX','ECX'];
- end;
- {$ifdef interncopy}
- procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN'];
- {$else}
- procedure fpc_shortstr_copy(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_COPY'];
- {$endif}
- begin
- asm
- pushl %eax
- pushl %ecx
- cld
- movl dstr,%edi
- movl sstr,%esi
- xorl %eax,%eax
- movl len,%ecx
- lodsb
- cmpl %ecx,%eax
- jbe .LStrCopy1
- movl %ecx,%eax
- .LStrCopy1:
- stosb
- cmpl $7,%eax
- jl .LStrCopy2
- movl %edi,%ecx { Align on 32bits }
- negl %ecx
- andl $3,%ecx
- subl %ecx,%eax
- rep
- movsb
- movl %eax,%ecx
- andl $3,%eax
- shrl $2,%ecx
- rep
- movsl
- .LStrCopy2:
- movl %eax,%ecx
- rep
- movsb
- popl %ecx
- popl %eax
- end ['ESI','EDI'];
- end;
- {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
- function fpc_shortstr_concat(const s1,s2:shortstring):shortstring;{$ifdef hascompilerproc}compilerproc;{$endif}
- begin
- asm
- movl __RESULT,%edi
- movl %edi,%ebx
- movl s1,%esi { first string }
- lodsb
- andl $0x0ff,%eax
- stosb
- cmpl $7,%eax
- jl .LStrConcat1
- movl %edi,%ecx { Align on 32bits }
- negl %ecx
- andl $3,%ecx
- subl %ecx,%eax
- rep
- movsb
- movl %eax,%ecx
- andl $3,%eax
- shrl $2,%ecx
- rep
- movsl
- .LStrConcat1:
- movl %eax,%ecx
- rep
- movsb
- movl s2,%esi { second string }
- movzbl (%ebx),%ecx
- negl %ecx
- addl $0x0ff,%ecx
- lodsb
- cmpl %ecx,%eax
- jbe .LStrConcat2
- movl %ecx,%eax
- .LStrConcat2:
- addb %al,(%ebx)
- cmpl $7,%eax
- jl .LStrConcat3
- movl %edi,%ecx { Align on 32bits }
- negl %ecx
- andl $3,%ecx
- subl %ecx,%eax
- rep
- movsb
- movl %eax,%ecx
- andl $3,%eax
- shrl $2,%ecx
- rep
- movsl
- .LStrConcat3:
- movl %eax,%ecx
- rep
- movsb
- end ['EBX','ECX','EAX','ESI','EDI'];
- end;
- {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
- {$ifdef hascompilerproc}
- procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
- [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
- begin
- asm
- movl s1,%edi
- movl s2,%esi
- movl %edi,%ebx
- movzbl (%edi),%ecx
- movl __HIGH(s1),%eax
- lea 1(%edi,%ecx),%edi
- negl %ecx
- addl %eax,%ecx
- // no need to zero eax, high(s1) <= 255
- lodsb
- cmpl %ecx,%eax
- jbe .LStrConcat1
- movl %ecx,%eax
- .LStrConcat1:
- addb %al,(%ebx)
- cmpl $7,%eax
- jl .LStrConcat2
- movl %edi,%ecx { Align on 32bits }
- negl %ecx
- andl $3,%ecx
- subl %ecx,%eax
- rep
- movsb
- movl %eax,%ecx
- andl $3,%eax
- shrl $2,%ecx
- rep
- movsl
- .LStrConcat2:
- movl %eax,%ecx
- rep
- movsb
- end ['EBX','ECX','EAX','ESI','EDI'];
- end;
- {$else hascompilerproc}
- procedure fpc_shortstr_concat_int(const s1,s2:shortstring);[public,alias:'FPC_SHORTSTR_CONCAT'];
- begin
- asm
- movl s1,%esi
- movl s2,%edi
- movl %edi,%ebx
- movzbl (%edi),%ecx
- xor %eax,%eax
- lea 1(%edi,%ecx),%edi
- negl %ecx
- addl $0x0ff,%ecx
- lodsb
- cmpl %ecx,%eax
- jbe .LStrConcat1
- movl %ecx,%eax
- .LStrConcat1:
- addb %al,(%ebx)
- cmpl $7,%eax
- jl .LStrConcat2
- movl %edi,%ecx { Align on 32bits }
- negl %ecx
- andl $3,%ecx
- subl %ecx,%eax
- rep
- movsb
- movl %eax,%ecx
- andl $3,%eax
- shrl $2,%ecx
- rep
- movsl
- .LStrConcat2:
- movl %eax,%ecx
- rep
- movsb
- end ['EBX','ECX','EAX','ESI','EDI'];
- end;
- {$endif hascompilerproc}
- {$define FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
- function fpc_shortstr_compare(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
- begin
- asm
- cld
- xorl %ebx,%ebx
- xorl %eax,%eax
- movl right,%esi
- movl left,%edi
- movb (%esi),%al
- movb (%edi),%bl
- movl %eax,%edx
- incl %esi
- incl %edi
- cmpl %ebx,%eax
- jbe .LStrCmp1
- movl %ebx,%eax
- .LStrCmp1:
- cmpl $7,%eax
- jl .LStrCmp2
- movl %edi,%ecx { Align on 32bits }
- negl %ecx
- andl $3,%ecx
- subl %ecx,%eax
- orl %ecx,%ecx
- rep
- cmpsb
- jne .LStrCmp3
- movl %eax,%ecx
- andl $3,%eax
- shrl $2,%ecx
- orl %ecx,%ecx
- rep
- cmpsl
- je .LStrCmp2
- movl $4,%eax
- sub %eax,%esi
- sub %eax,%edi
- .LStrCmp2:
- movl %eax,%ecx
- orl %eax,%eax
- rep
- cmpsb
- jne .LStrCmp3
- cmp %ebx,%edx
- .LStrCmp3:
- end ['EDX','ECX','EBX','EAX','ESI','EDI'];
- end;
- {$define FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
- function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
- {$include strpas.inc}
- {$define FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
- function fpc_pchar_length(p:pchar):longint;assembler;[public,alias:'FPC_PCHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
- {$include strlen.inc}
- {$define FPC_SYSTEM_HAS_GET_FRAME}
- function get_frame:pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
- asm
- movl %ebp,%eax
- end ['EAX'];
- {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
- function get_caller_addr(framebp:pointer):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
- asm
- movl framebp,%eax
- orl %eax,%eax
- jz .Lg_a_null
- movl 4(%eax),%eax
- .Lg_a_null:
- end ['EAX'];
- {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
- function get_caller_frame(framebp:pointer):pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
- asm
- movl framebp,%eax
- orl %eax,%eax
- jz .Lgnf_null
- movl (%eax),%eax
- .Lgnf_null:
- end ['EAX'];
- {****************************************************************************
- Math
- ****************************************************************************}
- {$define FPC_SYSTEM_HAS_ABS_LONGINT}
- function abs(l:longint):longint; assembler;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
- asm
- movl l,%eax
- cltd
- xorl %edx,%eax
- subl %edx,%eax
- end ['EAX','EDX'];
- {$define FPC_SYSTEM_HAS_ODD_LONGINT}
- function odd(l:longint):boolean;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
- asm
- movl l,%eax
- andl $1,%eax
- setnz %al
- end ['EAX'];
- {$define FPC_SYSTEM_HAS_SQR_LONGINT}
- function sqr(l:longint):longint;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
- asm
- mov l,%eax
- imull %eax,%eax
- end ['EAX'];
- {$define FPC_SYSTEM_HAS_SPTR}
- Function Sptr : Pointer;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
- asm
- movl %esp,%eax
- end;
- {****************************************************************************
- Str()
- ****************************************************************************}
- {$define FPC_SYSTEM_HAS_INT_STR_LONGINT}
- procedure int_str(l : longint;var s : string);
- var
- buffer : array[0..15] of byte;
- isneg : byte;
- begin
- { Workaround: }
- if l=longint($80000000) then
- begin
- s:='-2147483648';
- exit;
- end;
- asm
- movl l,%eax // load Integer
- xorl %ecx,%ecx // String length=0
- leal buffer,%ebx
- movl $0x0a,%esi // load 10 as dividing constant.
- movb $0,isneg
- orl %eax,%eax // Sign ?
- jns .LM2
- movb $1,isneg
- negl %eax
- .LM2:
- cltd
- idivl %esi
- addb $0x30,%dl // convert Rest to ASCII.
- movb %dl,(%ebx)
- incl %ecx
- incl %ebx
- cmpl $0,%eax
- jnz .LM2
- { now copy the string }
- movl s,%edi // Load String address
- cmpb $0,isneg
- je .LM3
- movb $0x2d,(%ebx)
- incl %ecx
- incl %ebx
- .LM3:
- movb %cl,(%edi) // Copy String length
- incl %edi
- .LM4:
- decl %ebx
- movb (%ebx),%al
- stosb
- decl %ecx
- jnz .LM4
- end ['eax','ecx','edx','ebx','esi','edi'];
- end;
- {$define FPC_SYSTEM_HAS_INT_STR_LONGWORD}
- procedure int_str(c : longword;var s : string);
- var
- buffer : array[0..15] of byte;
- begin
- asm
- movl c,%eax // load CARDINAL
- xorl %ecx,%ecx // String length=0
- leal buffer,%ebx
- movl $0x0a,%esi // load 10 as dividing constant.
- .LM4:
- xorl %edx,%edx
- divl %esi
- addb $0x30,%dl // convert Rest to ASCII.
- movb %dl,(%ebx)
- incl %ecx
- incl %ebx
- cmpl $0,%eax
- jnz .LM4
- { now copy the string }
- movl s,%edi // Load String address
- movb %cl,(%edi) // Copy String length
- incl %edi
- .LM5:
- decl %ebx
- movb (%ebx),%al
- stosb
- decl %ecx
- jnz .LM5
- end ['eax','ecx','edx','ebx','esi','edi'];
- end;
- {****************************************************************************
- Bounds Check
- ****************************************************************************}
- {$ifndef NOBOUNDCHECK}
- procedure int_boundcheck;assembler;[public,alias: 'FPC_BOUNDCHECK'];
- var dummy_to_force_stackframe_generation_for_trace: Longint;
- {
- called with:
- %ecx - value
- %edi - pointer to the ranges
- }
- asm
- cmpl (%edi),%ecx
- jl .Lbc_err
- cmpl 4(%edi),%ecx
- jle .Lbc_ok
- .Lbc_err:
- pushl %ebp
- pushl $201
- call HandleErrorFrame
- .Lbc_ok:
- end;
- {$endif NOBOUNDCHECK}
- { do a thread save inc/dec }
- {$define FPC_SYSTEM_HAS_DECLOCKED}
- function declocked(var l : longint) : boolean;assembler;
- asm
- movl l,%eax
- { this check should be done because a lock takes a lot }
- { of time! }
- cmpb $0,IsMultithread
- jz .Ldeclockednolock
- lock
- decl (%eax)
- jmp .Ldeclockedend
- .Ldeclockednolock:
- decl (%eax);
- .Ldeclockedend:
- setzb %al
- end;
- {$define FPC_SYSTEM_HAS_INCLOCKED}
- procedure inclocked(var l : longint);assembler;
- asm
- movl l,%eax
- { this check should be done because a lock takes a lot }
- { of time! }
- cmpb $0,IsMultithread
- jz .Linclockednolock
- lock
- incl (%eax)
- jmp .Linclockedend
- .Linclockednolock:
- incl (%eax)
- .Linclockedend:
- end;
- {****************************************************************************
- FPU
- ****************************************************************************}
- const
- fpucw : word = $1332;
- { Internal constants for use in system unit }
- FPU_Invalid = 1;
- FPU_Denormal = 2;
- FPU_DivisionByZero = 4;
- FPU_Overflow = 8;
- FPU_Underflow = $10;
- FPU_StackUnderflow = $20;
- FPU_StackOverflow = $40;
- FPU_ExceptionMask = $ff;
- {$define FPC_SYSTEM_HAS_SYSRESETFPU}
- Procedure SysResetFPU;assembler;{$ifdef SYSTEMINLINE}inline;{$endif}
- asm
- fninit
- fldcw fpucw
- end;
- {
- $Log$
- Revision 1.52 2003-11-03 09:42:27 marco
- * Peter's Cardinal<->Longint fixes patch
- Revision 1.51 2003/10/27 09:16:57 marco
- * fix from peter i386.inc to circumvent ebx destroying
- Revision 1.50 2003/10/23 17:01:27 peter
- * save edi,ebx,esi in int_str
- Revision 1.49 2003/10/16 21:28:40 peter
- * use __HIGH()
- Revision 1.48 2003/10/14 00:57:48 florian
- + some code for PIC support added
- Revision 1.47 2003/09/14 11:34:13 peter
- * moved int64 asm code to int64p.inc
- * save ebx,esi
- Revision 1.46 2003/09/08 18:21:37 peter
- * save edi,esi,ebx
- Revision 1.45 2003/06/01 14:50:17 jonas
- * fpc_shortstr_append_shortstr has to use high(s1) instead of 255 as
- maxlen
- + ppc version of fpc_shortstr_append_shortstr
- Revision 1.44 2003/05/26 21:18:13 peter
- * FPC_SHORTSTR_APPEND_SHORTSTR public added
- Revision 1.43 2003/05/26 19:36:46 peter
- * fpc_shortstr_concat is now the same for all targets
- * fpc_shortstr_append_shortstr added for optimized code generation
- Revision 1.42 2003/05/16 22:40:11 florian
- * fixed generic shortstr_compare
- Revision 1.41 2003/03/26 00:19:10 peter
- * ifdef HAS_GENERICCONSTRUCTOR
- Revision 1.40 2003/03/17 14:30:11 peter
- * changed address parameter/return values to pointer instead
- of longint
- Revision 1.39 2003/02/18 17:56:06 jonas
- - removed buggy i386-specific FPC_CHARARRAY_TO_SHORTSTR
- * fixed generic FPC_CHARARRAY_TO_SHORTSTR (web bug 2382)
- * fixed some potential range errors in indexchar/word/dword
- Revision 1.38 2003/01/06 23:03:13 mazen
- + defining FPC_SYSTEM_HAS_DECLOCKED and FPC_SYSTEM_HAS_INCLOCKED to avoid
- compilation error on generic.inc
- Revision 1.37 2003/01/03 17:14:54 peter
- * fix possible overflow when array len > 255 when converting to
- shortstring
- Revision 1.36 2002/12/15 22:32:25 peter
- * fixed return value when len=0 for indexchar,indexword
- Revision 1.35 2002/10/20 11:50:57 carl
- * avoid crashes with negative len counts on fills/moves
- Revision 1.34 2002/10/15 19:24:47 carl
- * Replace 220 -> 219
- Revision 1.33 2002/10/14 19:39:16 peter
- * threads unit added for thread support
- Revision 1.32 2002/10/05 14:20:16 peter
- * fpc_pchar_length compilerproc and strlen alias
- Revision 1.31 2002/10/02 18:21:51 peter
- * Copy() changed to internal function calling compilerprocs
- * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
- new copy functions
- Revision 1.30 2002/09/07 21:33:35 carl
- - removed unused defines
- Revision 1.29 2002/09/07 16:01:19 peter
- * old logs removed and tabs fixed
- Revision 1.28 2002/09/03 15:43:36 peter
- * add alias for fpc_dispose_class so it can be called from
- fpc_help_fail_class
- Revision 1.27 2002/08/19 19:34:02 peter
- * SYSTEMINLINE define that will add inline directives for small
- functions and wrappers. This will be defined automaticly when
- the compiler defines the HASINLINE directive
- Revision 1.26 2002/07/26 15:45:33 florian
- * changed multi threading define: it's MT instead of MTRTL
- Revision 1.25 2002/07/06 20:31:59 carl
- + added TEST_GENERIC to test generic version
- Revision 1.24 2002/06/16 08:21:26 carl
- + TEST_GENERIC to test generic versions of code
- Revision 1.23 2002/06/09 12:54:37 jonas
- * fixed memory corruption bug in fpc_help_constructor
- Revision 1.22 2002/04/21 18:56:59 peter
- * fpc_freemem and fpc_getmem compilerproc
- Revision 1.21 2002/04/01 14:23:17 carl
- - no need for runerror 203, already fixed!
- Revision 1.20 2002/03/30 14:52:04 carl
- * cause runtime error 203 on failed class creation
- }
|