generic.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team.
  5. Processor independent implementation for the system unit
  6. (adapted for intel i386.inc file)
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {****************************************************************************
  14. Primitives
  15. ****************************************************************************}
  16. type
  17. pstring = ^shortstring;
  18. {$ifndef FPC_SYSTEM_HAS_MOVE}
  19. procedure Move(const source;var dest;count:longint);
  20. type
  21. bytearray = array [0..maxlongint-1] of byte;
  22. var
  23. i,size : longint;
  24. begin
  25. if count <= 0 then exit;
  26. Dec(count);
  27. for i:=0 to count do
  28. bytearray(dest)[i]:=bytearray(source)[i];
  29. end;
  30. {$endif not FPC_SYSTEM_HAS_MOVE}
  31. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  32. Procedure FillChar(var x;count:longint;value:byte);
  33. type
  34. longintarray = array [0..maxlongint div 4] of longint;
  35. bytearray = array [0..maxlongint-1] of byte;
  36. var
  37. i,v : longint;
  38. begin
  39. if count <= 0 then exit;
  40. v := 0;
  41. v:=(value shl 8) or (value and $FF);
  42. v:=(v shl 16) or (v and $ffff);
  43. for i:=0 to (count div 4) -1 do
  44. longintarray(x)[i]:=v;
  45. for i:=(count div 4)*4 to count-1 do
  46. bytearray(x)[i]:=value;
  47. end;
  48. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  49. {$ifndef FPC_SYSTEM_HAS_FILLBYTE}
  50. procedure FillByte (var x;count : longint;value : byte );
  51. begin
  52. FillChar (X,Count,CHR(VALUE));
  53. end;
  54. {$endif not FPC_SYSTEM_HAS_FILLBYTE}
  55. {$ifndef FPC_SYSTEM_HAS_FILLWORD}
  56. procedure fillword(var x;count : longint;value : word);
  57. type
  58. longintarray = array [0..maxlongint div 4] of longint;
  59. wordarray = array [0..maxlongint div 2] of word;
  60. var
  61. i,v : longint;
  62. begin
  63. if Count <= 0 then exit;
  64. v:=value*$10000+value;
  65. for i:=0 to (count div 2) -1 do
  66. longintarray(x)[i]:=v;
  67. for i:=(count div 2)*2 to count-1 do
  68. wordarray(x)[i]:=value;
  69. end;
  70. {$endif not FPC_SYSTEM_HAS_FILLWORD}
  71. {$ifndef FPC_SYSTEM_HAS_FILLDWORD}
  72. procedure FillDWord(var x;count : longint;value : DWord);
  73. type
  74. longintarray = array [0..maxlongint div 4] of longint;
  75. begin
  76. if count <= 0 then exit;
  77. while Count<>0 do
  78. begin
  79. { range checking must be disabled here }
  80. longintarray(x)[count-1]:=longint(value);
  81. Dec(count);
  82. end;
  83. end;
  84. {$endif FPC_SYSTEM_HAS_FILLDWORD}
  85. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR}
  86. function IndexChar(Const buf;len:longint;b:char):longint;
  87. begin
  88. IndexChar:=IndexByte(Buf,Len,byte(B));
  89. end;
  90. {$endif not FPC_SYSTEM_HAS_INDEXCHAR}
  91. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  92. function IndexByte(Const buf;len:longint;b:byte):longint;
  93. type
  94. bytearray = array [0..maxlongint-1] of byte;
  95. var
  96. I : longint;
  97. begin
  98. I:=0;
  99. while (bytearray(buf)[I]<>b) and (I<Len) do
  100. inc(I);
  101. if (i=Len) then
  102. i:=-1; {Can't use 0, since it is a possible value}
  103. IndexByte:=I;
  104. end;
  105. {$endif not FPC_SYSTEM_HAS_INDEXBYTE}
  106. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  107. function Indexword(Const buf;len:longint;b:word):longint;
  108. type
  109. wordarray = array [0..maxlongint div 2] of word;
  110. var
  111. I : longint;
  112. begin
  113. I:=0;
  114. while (wordarray(buf)[I]<>b) and (I<Len) do
  115. inc(I);
  116. if (i=Len) then
  117. i:=-1; {Can't use 0, since it is a possible value for index}
  118. Indexword:=I;
  119. end;
  120. {$endif not FPC_SYSTEM_HAS_INDEXWORD}
  121. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  122. function IndexDWord(Const buf;len:longint;b:DWord):longint;
  123. type
  124. longintarray = array [0..maxlongint div 4] of longint;
  125. var
  126. I : longint;
  127. begin
  128. I:=0;
  129. while (longintarray(buf)[I]<>b) and (I<Len) do inc(I);
  130. if (i=Len) then
  131. i:=-1; {Can't use 0, since it is a possible value for index}
  132. IndexDWord:=I;
  133. end;
  134. {$endif not FPC_SYSTEM_HAS_INDEXDWORD}
  135. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR}
  136. function CompareChar(Const buf1,buf2;len:longint):longint;
  137. begin
  138. CompareChar:=CompareByte(buf1,buf2,len);
  139. end;
  140. {$endif not FPC_SYSTEM_HAS_COMPARECHAR}
  141. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  142. function CompareByte(Const buf1,buf2;len:longint):longint;
  143. type
  144. bytearray = array [0..maxlongint-1] of byte;
  145. var
  146. I,J : longint;
  147. begin
  148. I:=0;
  149. if (Len<>0) and (@Buf1<>@Buf2) then
  150. begin
  151. while (bytearray(Buf1)[I]=bytearray(Buf2)[I]) and (I<Len) do
  152. inc(I);
  153. if I=Len then {No difference}
  154. I:=0
  155. else
  156. begin
  157. I:=bytearray(Buf1)[I]-bytearray(Buf2)[I];
  158. if I>0 then
  159. I:=1
  160. else
  161. if I<0 then
  162. I:=-1;
  163. end;
  164. end;
  165. CompareByte:=I;
  166. end;
  167. {$endif not FPC_SYSTEM_HAS_COMPAREBYTE}
  168. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  169. function CompareWord(Const buf1,buf2;len:longint):longint;
  170. type
  171. wordarray = array [0..maxlongint div 2] of word;
  172. var
  173. I,J : longint;
  174. begin
  175. I:=0;
  176. if (Len<>0) and (@Buf1<>@Buf2) then
  177. begin
  178. while (wordarray(Buf1)[I]=wordarray(Buf2)[I]) and (I<Len) do
  179. inc(I);
  180. if I=Len then {No difference}
  181. I:=0
  182. else
  183. begin
  184. I:=wordarray(Buf1)[I]-wordarray(Buf2)[I];
  185. if I>0 then
  186. I:=1
  187. else
  188. if I<0 then
  189. I:=-1;
  190. end;
  191. end;
  192. CompareWord:=I;
  193. end;
  194. {$endif not FPC_SYSTEM_HAS_COMPAREWORD}
  195. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  196. function CompareDWord(Const buf1,buf2;len:longint):longint;
  197. type
  198. longintarray = array [0..maxlongint div 4] of longint;
  199. var
  200. I,J : longint;
  201. begin
  202. I:=0;
  203. if (Len<>0) and (@Buf1<>@Buf2) then
  204. begin
  205. while (longintarray(Buf1)[I]=longintarray(Buf2)[I]) and (I<Len) do
  206. inc(I);
  207. if I=Len then {No difference}
  208. I:=0
  209. else
  210. begin
  211. I:=longintarray(Buf1)[I]-longintarray(Buf2)[I];
  212. if I>0 then
  213. I:=1
  214. else
  215. if I<0 then
  216. I:=-1;
  217. end;
  218. end;
  219. CompareDWord:=I;
  220. end;
  221. {$endif ndef FPC_SYSTEM_HAS_COMPAREDWORD}
  222. {$ifndef FPC_SYSTEM_HAS_MOVECHAR0}
  223. procedure MoveChar0(Const buf1;var buf2;len:longint);
  224. var
  225. I : longint;
  226. begin
  227. if Len = 0 then exit;
  228. I:=IndexByte(Buf1,Len,0);
  229. if I<>-1 then
  230. Move(Buf1,Buf2,I)
  231. else
  232. Move(Buf1,Buf2,len);
  233. end;
  234. {$endif ndef FPC_SYSTEM_HAS_MOVECHAR0}
  235. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  236. function IndexChar0(Const buf;len:longint;b:Char):longint;
  237. var
  238. I : longint;
  239. begin
  240. if Len<>0 then
  241. begin
  242. I:=IndexByte(Buf,Len,0);
  243. IndexChar0:=IndexByte(Buf,I,0);
  244. end
  245. else
  246. IndexChar0:=0;
  247. end;
  248. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR0}
  249. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  250. function CompareChar0(Const buf1,buf2;len:longint):longint;
  251. type
  252. bytearray = array [0..maxlongint-1] of byte;
  253. Var i : longint;
  254. begin
  255. I:=0;
  256. if (Len<>0) and (@Buf1<>@Buf2) then
  257. begin
  258. while (I<Len) And
  259. ((Pbyte(@Buf1)[i]<>0) and (PByte(@buf2)[i]<>0)) and
  260. (pbyte(@Buf1)[I]=pbyte(@Buf2)[I]) do
  261. inc(I);
  262. if (I=Len) or
  263. (PByte(@Buf1)[i]=0) or
  264. (PByte(@buf2)[I]=0) then {No difference or 0 reached }
  265. I:=0
  266. else
  267. begin
  268. I:=bytearray(Buf1)[I]-bytearray(Buf2)[I];
  269. if I>0 then
  270. I:=1
  271. else
  272. if I<0 then
  273. I:=-1;
  274. end;
  275. end;
  276. CompareChar0:=I;
  277. end;
  278. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  279. {****************************************************************************
  280. Object Helpers
  281. ****************************************************************************}
  282. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  283. { Generic code does not set the register used for self !
  284. So this needs to be done by the compiler after calling
  285. FPC_HELP_CONSTRUCTOR : generic allways means aa little less efficient (PM) }
  286. { I don't think we really need to save any registers here }
  287. { since this is called at the start of the constructor (CEC) }
  288. function fpc_help_constructor(var _self : pointer; var vmt : pointer; vmt_pos : cardinal) : pointer; [public,alias:'FPC_HELP_CONSTRUCTOR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  289. type
  290. ppointer = ^pointer;
  291. pvmt = ^tvmt;
  292. tvmt = packed record
  293. size,msize : longint;
  294. parent : pointer;
  295. end;
  296. var
  297. objectsize : longint;
  298. vmtcopy : pointer;
  299. begin
  300. if vmt=nil then
  301. begin
  302. fpc_help_constructor:=_self;
  303. exit;
  304. end;
  305. vmtcopy:=vmt;
  306. objectsize:=pvmt(vmtcopy)^.size;
  307. if _self=nil then
  308. begin
  309. getmem(_self,objectsize);
  310. longint(vmt):=-1; { needed for fail }
  311. end;
  312. if _self<>nil then
  313. begin
  314. fillchar(_self^,objectsize,#0);
  315. ppointer(_self+vmt_pos)^:=vmtcopy;
  316. end;
  317. fpc_help_constructor:=_self;
  318. end;
  319. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  320. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  321. procedure fpc_help_destructor(var _self : pointer; vmt : pointer; vmt_pos : cardinal);saveregisters;[public,alias:'FPC_HELP_DESTRUCTOR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  322. type
  323. ppointer = ^pointer;
  324. pvmt = ^tvmt;
  325. tvmt = packed record
  326. size,msize : longint;
  327. parent : pointer;
  328. end;
  329. var
  330. objectsize : longint;
  331. begin
  332. if (_self=nil) then
  333. exit;
  334. if (pvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  335. (pvmt(ppointer(_self+vmt_pos)^)^.size+pvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  336. RunError(210);
  337. if (vmt = nil) then
  338. exit;
  339. objectsize:=pvmt(vmt)^.size;
  340. { reset vmt to nil for protection }
  341. ppointer(_self+vmt_pos)^:=nil;
  342. freemem(_self,objectsize);
  343. _self:=nil;
  344. end;
  345. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  346. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  347. procedure fpc_help_fail(var _self : pointer; var vmt : pointer; vmt_pos : cardinal);safecall; [public,alias:'FPC_HELP_FAIL'];
  348. type
  349. ppointer = ^pointer;
  350. pvmt = ^tvmt;
  351. tvmt = packed record
  352. size,msize : longint;
  353. parent : pointer;
  354. end;
  355. var
  356. objectsize : longint;
  357. begin
  358. if vmt=nil then
  359. exit;
  360. if longint(vmt)=-1 then
  361. begin
  362. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  363. HandleError(210)
  364. else
  365. begin
  366. ppointer(_self+vmt_pos)^:=nil;
  367. freemem(_self);
  368. _self:=nil;
  369. vmt:=nil;
  370. end;
  371. end
  372. else
  373. ppointer(_self+vmt_pos)^:=nil;
  374. _self := nil;
  375. end;
  376. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  377. {$ifndef FPC_SYSTEM_HAS_FPC_NEW_CLASS}
  378. { the constructor receives as first parameter a pointer }
  379. { to the vmt or nil, if called when class already instanciated }
  380. { RETURNS SELF }
  381. { IMPORTANT: SELF REGISTER should be pre-loaded before call to }
  382. { constructor for this to work! }
  383. function fpc_new_class(_vmt: pointer; _self : pointer):pointer;saveregisters;[public,alias:'FPC_NEW_CLASS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  384. begin
  385. if _vmt <> nil then
  386. begin
  387. fpc_new_class := tclass(_vmt).NewInstance;
  388. end
  389. else
  390. begin
  391. { calling when class already instanciated }
  392. { then simply returna a boolean value <> 0 }
  393. fpc_new_class := _self;
  394. end;
  395. end;
  396. {$endif FPC_SYSTEM_HAS_FPC_NEW_CLASS}
  397. {$ifndef FPC_SYSTEM_HAS_FPC_DISPOSE_CLASS}
  398. procedure fpc_dispose_class(_self: tobject; flag : longint);saveregisters;[public,alias:'FPC_DISPOSE_CLASS'];
  399. begin
  400. if (_self <> nil) and (flag = 1) then
  401. _self.FreeInstance;
  402. end;
  403. {$endif ndef FPC_SYSTEM_HAS_FPC_DISPOSE_CLASS}
  404. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  405. procedure fpc_check_object(obj : pointer);saveregisters;[public,alias:'FPC_CHECK_OBJECT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  406. type
  407. pvmt = ^tvmt;
  408. tvmt = packed record
  409. size,msize : longint;
  410. parent : pointer;
  411. end;
  412. begin
  413. (* if (vmt=nil) or
  414. (pvmt(vmt)^.size=0) or
  415. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  416. RunError(210);*)
  417. end;
  418. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  419. { checks for a correct vmt pointer }
  420. { deeper check to see if the current object is }
  421. { really related to the true }
  422. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  423. procedure fpc_check_object_ext(vmt, expvmt : pointer);saveregisters;[public,alias:'FPC_CHECK_OBJECT_EXT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  424. type
  425. pvmt = ^tvmt;
  426. tvmt = packed record
  427. size,msize : longint;
  428. parent : pointer;
  429. end;
  430. begin
  431. if (vmt=nil) or
  432. (pvmt(vmt)^.size=0) or
  433. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  434. RunError(210);
  435. while assigned(vmt) do
  436. if vmt=expvmt then
  437. exit
  438. else
  439. vmt:=pvmt(vmt)^.parent;
  440. RunError(219);
  441. end;
  442. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  443. {****************************************************************************
  444. String
  445. ****************************************************************************}
  446. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  447. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  448. var
  449. slen : byte;
  450. begin
  451. { these are shortstrings, not pointers! (JM)
  452. if dstr=nil then
  453. exit;
  454. if sstr=nil then
  455. begin
  456. if dstr<>nil then
  457. pstring(dstr)^[0]:=#0;
  458. exit;
  459. end;
  460. }
  461. slen:=length(sstr);
  462. if slen<len then
  463. len:=slen;
  464. { don't forget the length character }
  465. if len <> 0 then
  466. move(sstr[0],result[0],len+1);
  467. end;
  468. {$ifdef interncopy}
  469. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  470. {$else}
  471. procedure fpc_shortstr_copy(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_COPY']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  472. {$endif}
  473. var
  474. slen : byte;
  475. type
  476. pstring = ^string;
  477. begin
  478. { these are shortstrings, not pointers! (JM)
  479. if dstr=nil then
  480. exit;
  481. if sstr=nil then
  482. begin
  483. if dstr<>nil then
  484. pstring(dstr)^[0]:=#0;
  485. exit;
  486. end;
  487. }
  488. slen:=length(pstring(sstr)^);
  489. if slen<len then
  490. len:=slen;
  491. { don't forget the length character }
  492. if len <> 0 then
  493. move(sstr^,dstr^,len+1);
  494. { already done by the move above (JM)
  495. pstring(dstr)^[0]:=chr(len);
  496. }
  497. end;
  498. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  499. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  500. { note: this routine is *DIFFERENT* from the routine in i386.inc and as such you }
  501. { cannot use it with the i386 compiler, unless you remove the }
  502. { ti386addnode.first_string method (JM) }
  503. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  504. var
  505. s1l, s2l : byte;
  506. begin
  507. { these are shortstrings, they can't be nil! (JM)
  508. if (s1=nil) or (s2=nil) then
  509. exit;
  510. }
  511. s1l:=length(s1);
  512. s2l:=length(s2);
  513. if s1l+s2l>255 then
  514. s2l:=255-s1l;
  515. fpc_shortstr_concat := s1;
  516. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  517. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  518. end;
  519. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  520. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  521. function fpc_shortstr_compare(const rightstr,leftstr:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  522. var
  523. s1,s2,max,i : byte;
  524. d : longint;
  525. begin
  526. s1:=length(rightstr);
  527. s2:=length(leftstr);
  528. if s1<s2 then
  529. max:=s1
  530. else
  531. max:=s2;
  532. for i:=1 to max do
  533. begin
  534. d:=byte(leftstr[i])-byte(rightstr[i]);
  535. if d>0 then
  536. exit(1)
  537. else if d<0 then
  538. exit(-1);
  539. end;
  540. if s1>s2 then
  541. exit(1)
  542. else if s1<s2 then
  543. exit(-1)
  544. else
  545. exit(0);
  546. end;
  547. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  548. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  549. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  550. var
  551. l : longint;
  552. s: shortstring;
  553. begin
  554. if p=nil then
  555. l:=0
  556. else
  557. l:=strlen(p);
  558. if l>255 then
  559. l:=255;
  560. if l>0 then
  561. move(p^,s[1],l);
  562. s[0]:=chr(l);
  563. fpc_pchar_to_shortstr := s;
  564. end;
  565. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  566. { also add a strpas alias for internal use in the system unit (JM) }
  567. function strpas(p:pchar):shortstring; [external name 'FPC_PCHAR_TO_SHORTSTR'];
  568. { also add a strlen alias for internal use in the system unit (JM) }
  569. function strlen(p:pchar):longint; [external name 'FPC_PCHAR_LENGTH'];
  570. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  571. {$ifdef hascompilerproc}
  572. function fpc_chararray_to_shortstr(const arr: array of char):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  573. var
  574. l: longint;
  575. {$else hascompilerproc}
  576. function fpc_chararray_to_shortstr(p:pchar; l : longint):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR'];
  577. {$endif hascompilerproc}
  578. begin
  579. {$ifdef hascompilerproc}
  580. l := high(arr)+1;
  581. {$endif hascompilerproc}
  582. if l>=256 then
  583. l:=255
  584. else if l<0 then
  585. l:=0;
  586. move(arr[0],fpc_chararray_to_shortstr[1],l);
  587. fpc_chararray_to_shortstr[0]:=chr(l);
  588. end;
  589. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  590. {$ifdef hascompilerproc}
  591. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  592. { inside the compiler, the resulttype is modified to that of the actual }
  593. { chararray we're converting to (JM) }
  594. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  595. var
  596. len: longint;
  597. begin
  598. len := length(src);
  599. if len > arraysize then
  600. len := arraysize;
  601. { make sure we don't access char 1 if length is 0 (JM) }
  602. if len > 0 then
  603. move(src[1],fpc_shortstr_to_chararray[0],len);
  604. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  605. end;
  606. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  607. {$else hascompilerproc}
  608. {$ifopt r+}
  609. {$define rangeon}
  610. {$r-}
  611. {$endif}
  612. {$ifndef FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  613. procedure fpc_str_to_chararray(strtyp, arraysize: longint; src,dest: pchar);[public,alias:'FPC_STR_TO_CHARARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  614. type
  615. plongint = ^longint;
  616. var
  617. len: longint;
  618. begin
  619. case strtyp of
  620. { shortstring }
  621. 0:
  622. begin
  623. len := byte(src[0]);
  624. inc(src);
  625. end;
  626. {$ifdef SUPPORT_ANSISTRING}
  627. { ansistring}
  628. 1: len := length(ansistring(pointer(src)));
  629. {$endif SUPPORT_ANSISTRING}
  630. { longstring }
  631. 2:;
  632. { widestring }
  633. 3: ;
  634. end;
  635. if len > arraysize then
  636. len := arraysize;
  637. { make sure we don't dereference src if it can be nil (JM) }
  638. if len > 0 then
  639. move(src^,dest^,len);
  640. fillchar(dest[len],arraysize-len,0);
  641. end;
  642. {$endif FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  643. {$ifdef rangeon}
  644. {$r+}
  645. {undef rangeon}
  646. {$endif rangeon}
  647. {$endif hascompilerproc}
  648. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  649. function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  650. var i : longint;
  651. begin
  652. i:=0;
  653. while p[i]<>#0 do inc(i);
  654. exit(i);
  655. end;
  656. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  657. {$ifdef HASWIDESTRING}
  658. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  659. function fpc_pwidechar_length(p:pwidechar):longint;[public,alias:'FPC_PWIDECHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  660. var i : longint;
  661. begin
  662. i:=0;
  663. while p[i]<>#0 do inc(i);
  664. exit(i);
  665. end;
  666. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  667. {$endif HASWIDESTRING}
  668. {****************************************************************************
  669. Caller/StackFrame Helpers
  670. ****************************************************************************}
  671. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  672. {$error Get_frame must be defined for each processor }
  673. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  674. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  675. {$error Get_caller_addr must be defined for each processor }
  676. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  677. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  678. {$error Get_caller_frame must be defined for each processor }
  679. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  680. {****************************************************************************
  681. Math
  682. ****************************************************************************}
  683. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  684. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  685. begin
  686. if l<0 then
  687. abs:=-l
  688. else
  689. abs:=l;
  690. end;
  691. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  692. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  693. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  694. begin
  695. odd:=boolean(l and 1);
  696. end;
  697. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  698. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  699. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  700. begin
  701. odd:=boolean(l and 1);
  702. end;
  703. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  704. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  705. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  706. begin
  707. odd:=boolean(longint(l) and 1);
  708. end;
  709. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  710. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  711. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  712. begin
  713. odd:=boolean(longint(l) and 1);
  714. end;
  715. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  716. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  717. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  718. begin
  719. sqr:=l*l;
  720. end;
  721. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  722. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  723. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  724. begin
  725. if l < 0 then
  726. abs := -l
  727. else
  728. abs := l;
  729. end;
  730. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  731. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  732. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  733. begin
  734. sqr := l*l;
  735. end;
  736. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  737. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  738. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  739. begin
  740. sqr := l*l;
  741. end;
  742. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  743. {$ifndef FPC_SYSTEM_HAS_SPTR}
  744. {$error Sptr must be defined for each processor }
  745. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  746. {****************************************************************************
  747. Str()
  748. ****************************************************************************}
  749. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  750. procedure int_str(l : longint;var s : string);
  751. var
  752. value: longint;
  753. negative: boolean;
  754. begin
  755. negative := false;
  756. s:='';
  757. { Workaround: }
  758. if l=$80000000 then
  759. begin
  760. s:='-2147483648';
  761. exit;
  762. end;
  763. { handle case where l = 0 }
  764. if l = 0 then
  765. begin
  766. s:='0';
  767. exit;
  768. end;
  769. If l < 0 then
  770. begin
  771. negative := true;
  772. value:=abs(l);
  773. end
  774. else
  775. value:=l;
  776. { handle non-zero case }
  777. while value>0 do
  778. begin
  779. s:=char((value mod 10)+ord('0'))+s;
  780. value := value div 10;
  781. end;
  782. if negative then
  783. s := '-' + s;
  784. end;
  785. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  786. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  787. procedure int_str(l : longword;var s : string);
  788. begin
  789. s:='';
  790. if l = 0 then
  791. begin
  792. s := '0';
  793. exit;
  794. end;
  795. while l>0 do
  796. begin
  797. s:=char(ord('0')+(l mod 10))+s;
  798. l:=l div 10;
  799. end;
  800. end;
  801. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  802. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  803. procedure SysResetFpu;
  804. begin
  805. { nothing todo }
  806. end;
  807. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  808. {
  809. $Log$
  810. Revision 1.43 2002-10-20 11:51:54 carl
  811. * avoid crashes with negative len counts on fills/moves
  812. * movechar0 was wrong and did not do the behavior as
  813. described in docs
  814. Revision 1.42 2002/10/14 19:39:17 peter
  815. * threads unit added for thread support
  816. Revision 1.41 2002/10/12 20:32:41 carl
  817. * RunError 220 -> RunError 219 to be more consistent with as operator
  818. Revision 1.40 2002/10/10 16:08:50 florian
  819. + several widestring/pwidechar related helpers added
  820. Revision 1.39 2002/10/05 14:20:16 peter
  821. * fpc_pchar_length compilerproc and strlen alias
  822. Revision 1.38 2002/10/02 18:21:51 peter
  823. * Copy() changed to internal function calling compilerprocs
  824. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  825. new copy functions
  826. Revision 1.37 2002/09/27 21:10:40 carl
  827. * fix 2GB limit problem
  828. Revision 1.36 2002/09/13 19:13:06 carl
  829. * FPC_HELP_FAIL : reset _self to nil
  830. Revision 1.35 2002/09/10 21:29:44 jonas
  831. * added some missing compilerproc directives
  832. Revision 1.34 2002/09/07 21:08:42 carl
  833. * cardinal -> longword
  834. - remove generic boundcheck (does not exist in v1.1)
  835. Revision 1.33 2002/09/07 15:07:45 peter
  836. * old logs removed and tabs fixed
  837. Revision 1.32 2002/08/19 19:34:02 peter
  838. * SYSTEMINLINE define that will add inline directives for small
  839. functions and wrappers. This will be defined automaticly when
  840. the compiler defines the HASINLINE directive
  841. Revision 1.31 2002/07/29 21:28:16 florian
  842. * several fixes to get further with linux/ppc system unit compilation
  843. Revision 1.30 2002/07/29 09:23:11 jonas
  844. * fixed some datastructures > 2GB
  845. Revision 1.29 2002/07/28 21:39:28 florian
  846. * made abs a compiler proc if it is generic
  847. Revision 1.28 2002/07/28 20:43:47 florian
  848. * several fixes for linux/powerpc
  849. * several fixes to MT
  850. Revision 1.27 2002/06/16 08:19:03 carl
  851. * bugfix of FPC_NEW_CLASS (was not creating correct instance)
  852. + FPC_HELP_FAIL_CLASS now tested (no longer required)
  853. Revision 1.25 2002/05/16 19:58:05 carl
  854. * generic constructor implemented
  855. Revision 1.24 2002/03/30 13:08:54 carl
  856. * memory corruption bugfix in FPC_HELP_CONSTRUCTOR if object cannot be allocated
  857. Revision 1.23 2002/01/25 17:38:55 peter
  858. * add internconst for all overloaded types of Odd/Abs/Sqr
  859. Revision 1.22 2002/01/24 12:33:53 jonas
  860. * adapted ranges of native types to int64 (e.g. high cardinal is no
  861. longer longint($ffffffff), but just $fffffff in psystem)
  862. * small additional fix in 64bit rangecheck code generation for 32 bit
  863. processors
  864. * adaption of ranges required the matching talgorithm used for selecting
  865. which overloaded procedure to call to be adapted. It should now always
  866. select the closest match for ordinal parameters.
  867. + inttostr(qword) in sysstr.inc/sysstrh.inc
  868. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  869. fixes were required to be able to add them)
  870. * is_in_limit() moved from ncal to types unit, should always be used
  871. instead of direct comparisons of low/high values of orddefs because
  872. qword is a special case
  873. }