generic.inc 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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: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 (I<Len) and (bytearray(buf)[I]<>b) 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 (I<Len) and (wordarray(buf)[I]<>b) 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 (I<Len) and (longintarray(buf)[I]<>b) 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 : 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 : 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 : 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. { Note: _vmt will be reset to -1 when memory is allocated,
  284. this is needed for fpc_help_fail }
  285. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];{$ifdef hascompilerproc}compilerproc;{$endif}
  286. type
  287. ppointer = ^pointer;
  288. pvmt = ^tvmt;
  289. tvmt=packed record
  290. size,msize:longint;
  291. parent:pointer;
  292. end;
  293. var
  294. vmtcopy : pointer;
  295. begin
  296. { Inherited call? }
  297. if _vmt=nil then
  298. begin
  299. fpc_help_constructor:=_self;
  300. exit;
  301. end;
  302. vmtcopy:=_vmt;
  303. if (_self=nil) and
  304. (pvmt(_vmt)^.size>0) then
  305. begin
  306. getmem(_self,pvmt(_vmt)^.size);
  307. { reset vmt needed for fail }
  308. _vmt:=pointer(-1);
  309. end;
  310. if _self<>nil then
  311. begin
  312. fillchar(_self^,pvmt(vmtcopy)^.size,#0);
  313. ppointer(_self+_vmt_pos)^:=vmtcopy;
  314. end;
  315. fpc_help_constructor:=_self;
  316. end;
  317. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  318. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  319. { Note: _self will not be reset, the compiler has to generate the reset }
  320. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  321. type
  322. ppointer = ^pointer;
  323. pvmt = ^tvmt;
  324. tvmt = packed record
  325. size,msize : longint;
  326. parent : pointer;
  327. end;
  328. begin
  329. { already released? }
  330. if (_self=nil) or
  331. (_vmt=nil) or
  332. (ppointer(_self+vmt_pos)^=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. { reset vmt to nil for protection }
  338. ppointer(_self+vmt_pos)^:=nil;
  339. freemem(_self);
  340. end;
  341. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  342. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  343. { Note: _self will not be reset, the compiler has to generate the reset }
  344. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  345. type
  346. ppointer = ^pointer;
  347. pvmt = ^tvmt;
  348. tvmt = packed record
  349. size,msize : longint;
  350. parent : pointer;
  351. end;
  352. begin
  353. if (_self=nil) or (_vmt=nil) then
  354. exit;
  355. { vmt=-1 when memory was allocated }
  356. if longint(_vmt)=-1 then
  357. begin
  358. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  359. HandleError(210)
  360. else
  361. begin
  362. ppointer(_self+vmt_pos)^:=nil;
  363. freemem(_self);
  364. { reset _vmt to 0 so it will not be freed a
  365. second time }
  366. _vmt:=0;
  367. end;
  368. end
  369. else
  370. ppointer(_self+vmt_pos)^:=nil;
  371. end;
  372. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  373. {$ifndef NOCLASSHELPERS}
  374. {$ifndef FPC_SYSTEM_HAS_FPC_NEW_CLASS}
  375. function fpc_new_class(_self,_vmt:pointer):pointer;[public,alias:'FPC_NEW_CLASS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  376. begin
  377. { Inherited call? }
  378. if _vmt=nil then
  379. begin
  380. fpc_new_class:=_self;
  381. exit;
  382. end;
  383. fpc_new_class := tclass(_vmt).NewInstance
  384. end;
  385. {$endif FPC_SYSTEM_HAS_FPC_NEW_CLASS}
  386. {$ifndef FPC_SYSTEM_HAS_FPC_DISPOSE_CLASS}
  387. procedure fpc_dispose_class(_self: pointer; flag : longint);[public,alias:'FPC_DISPOSE_CLASS'];compilerproc;
  388. begin
  389. { inherited -> flag = 0 -> no destroy }
  390. { normal -> flag = 1 -> destroy }
  391. if (_self <> nil) and (flag = 1) then
  392. tobject(_self).FreeInstance;
  393. end;
  394. {$endif ndef FPC_SYSTEM_HAS_FPC_DISPOSE_CLASS}
  395. {$endif NOCLASSHELPERS}
  396. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  397. procedure fpc_check_object(_vmt : pointer);saveregisters;[public,alias:'FPC_CHECK_OBJECT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  398. type
  399. pvmt = ^tvmt;
  400. tvmt = packed record
  401. size,msize : longint;
  402. parent : pointer;
  403. end;
  404. begin
  405. if (_vmt=nil) or
  406. (pvmt(_vmt)^.size=0) or
  407. (pvmt(_vmt)^.size+pvmt(_vmt)^.msize<>0) then
  408. RunError(210);
  409. end;
  410. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  411. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  412. { checks for a correct vmt pointer }
  413. { deeper check to see if the current object is }
  414. { really related to the true }
  415. procedure fpc_check_object_ext(vmt, expvmt : pointer);saveregisters;[public,alias:'FPC_CHECK_OBJECT_EXT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  416. type
  417. pvmt = ^tvmt;
  418. tvmt = packed record
  419. size,msize : longint;
  420. parent : pointer;
  421. end;
  422. begin
  423. if (vmt=nil) or
  424. (pvmt(vmt)^.size=0) or
  425. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  426. RunError(210);
  427. while assigned(vmt) do
  428. if vmt=expvmt then
  429. exit
  430. else
  431. vmt:=pvmt(vmt)^.parent;
  432. RunError(219);
  433. end;
  434. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  435. {****************************************************************************
  436. String
  437. ****************************************************************************}
  438. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  439. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  440. var
  441. slen : byte;
  442. begin
  443. { these are shortstrings, not pointers! (JM)
  444. if dstr=nil then
  445. exit;
  446. if sstr=nil then
  447. begin
  448. if dstr<>nil then
  449. pstring(dstr)^[0]:=#0;
  450. exit;
  451. end;
  452. }
  453. slen:=length(sstr);
  454. if slen<len then
  455. len:=slen;
  456. { don't forget the length character }
  457. if len <> 0 then
  458. move(sstr[0],result[0],len+1);
  459. end;
  460. {$ifdef interncopy}
  461. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  462. {$else}
  463. procedure fpc_shortstr_copy(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_COPY']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  464. {$endif}
  465. var
  466. slen : byte;
  467. type
  468. pstring = ^string;
  469. begin
  470. { these are shortstrings, not pointers! (JM)
  471. if dstr=nil then
  472. exit;
  473. if sstr=nil then
  474. begin
  475. if dstr<>nil then
  476. pstring(dstr)^[0]:=#0;
  477. exit;
  478. end;
  479. }
  480. slen:=length(pstring(sstr)^);
  481. if slen<len then
  482. len:=slen;
  483. { don't forget the length character }
  484. if len <> 0 then
  485. move(sstr^,dstr^,len+1);
  486. { already done by the move above (JM)
  487. pstring(dstr)^[0]:=chr(len);
  488. }
  489. end;
  490. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  491. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  492. { note: this routine is *DIFFERENT* from the routine in i386.inc and as such you }
  493. { cannot use it with the i386 compiler, unless you remove the }
  494. { ti386addnode.first_string method (JM) }
  495. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  496. var
  497. s1l, s2l : byte;
  498. begin
  499. { these are shortstrings, they can't be nil! (JM)
  500. if (s1=nil) or (s2=nil) then
  501. exit;
  502. }
  503. s1l:=length(s1);
  504. s2l:=length(s2);
  505. if s1l+s2l>255 then
  506. s2l:=255-s1l;
  507. fpc_shortstr_concat := s1;
  508. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  509. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  510. end;
  511. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  512. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  513. function fpc_shortstr_compare(const dstr,sstr:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  514. var
  515. s1,s2,max,i : byte;
  516. d : longint;
  517. begin
  518. s1:=length(dstr);
  519. s2:=length(sstr);
  520. if s1<s2 then
  521. max:=s1
  522. else
  523. max:=s2;
  524. for i:=1 to max do
  525. begin
  526. d:=byte(sstr[i])-byte(dstr[i]);
  527. if d>0 then
  528. exit(1)
  529. else if d<0 then
  530. exit(-1);
  531. end;
  532. if s1>s2 then
  533. exit(1)
  534. else if s1<s2 then
  535. exit(-1)
  536. else
  537. exit(0);
  538. end;
  539. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  540. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  541. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  542. var
  543. l : longint;
  544. s: shortstring;
  545. begin
  546. if p=nil then
  547. l:=0
  548. else
  549. l:=strlen(p);
  550. if l>255 then
  551. l:=255;
  552. if l>0 then
  553. move(p^,s[1],l);
  554. s[0]:=chr(l);
  555. fpc_pchar_to_shortstr := s;
  556. end;
  557. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  558. { also add a strpas alias for internal use in the system unit (JM) }
  559. function strpas(p:pchar):shortstring; [external name 'FPC_PCHAR_TO_SHORTSTR'];
  560. { also add a strlen alias for internal use in the system unit (JM) }
  561. function strlen(p:pchar):longint; [external name 'FPC_PCHAR_LENGTH'];
  562. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  563. {$ifdef hascompilerproc}
  564. function fpc_chararray_to_shortstr(const arr: array of char):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  565. var
  566. l: longint;
  567. {$else hascompilerproc}
  568. function fpc_chararray_to_shortstr(arr:pchar; l : longint):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR'];
  569. var
  570. {$endif hascompilerproc}
  571. index: longint;
  572. len: byte;
  573. begin
  574. {$ifdef hascompilerproc}
  575. l := high(arr)+1;
  576. {$endif hascompilerproc}
  577. if l>=256 then
  578. l:=255
  579. else if l<0 then
  580. l:=0;
  581. index:=IndexByte(arr[0],l,0);
  582. if (index < 0) then
  583. len := l
  584. else
  585. len := index;
  586. move(arr[0],fpc_chararray_to_shortstr[1],len);
  587. fpc_chararray_to_shortstr[0]:=chr(len);
  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_DECLOCKED}
  744. function declocked(var l:longint):boolean;
  745. begin
  746. Dec(l);
  747. declocked:=(l=0);
  748. end;
  749. {$endif FPC_SYSTEM_HAS_DECLOCKED}
  750. {$ifndef FPC_SYSTEM_HAS_INCLOCKED}
  751. procedure inclocked(var l:longint);
  752. begin
  753. Inc(l);
  754. end;
  755. {$endif FPC_SYSTEM_HAS_INCLOCKED}
  756. {$ifndef FPC_SYSTEM_HAS_SPTR}
  757. {_$error Sptr must be defined for each processor }
  758. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  759. {****************************************************************************
  760. Str()
  761. ****************************************************************************}
  762. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  763. procedure int_str(l : longint;var s : string);
  764. var
  765. value: longint;
  766. negative: boolean;
  767. begin
  768. negative := false;
  769. s:='';
  770. { Workaround: }
  771. if l=$80000000 then
  772. begin
  773. s:='-2147483648';
  774. exit;
  775. end;
  776. { handle case where l = 0 }
  777. if l = 0 then
  778. begin
  779. s:='0';
  780. exit;
  781. end;
  782. If l < 0 then
  783. begin
  784. negative := true;
  785. value:=abs(l);
  786. end
  787. else
  788. value:=l;
  789. { handle non-zero case }
  790. while value>0 do
  791. begin
  792. s:=char((value mod 10)+ord('0'))+s;
  793. value := value div 10;
  794. end;
  795. if negative then
  796. s := '-' + s;
  797. end;
  798. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  799. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  800. procedure int_str(l : longword;var s : string);
  801. begin
  802. s:='';
  803. if l = 0 then
  804. begin
  805. s := '0';
  806. exit;
  807. end;
  808. while l>0 do
  809. begin
  810. s:=char(ord('0')+(l mod 10))+s;
  811. l:=l div 10;
  812. end;
  813. end;
  814. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  815. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  816. procedure SysResetFpu;
  817. begin
  818. { nothing todo }
  819. end;
  820. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  821. {
  822. $Log$
  823. Revision 1.56 2003-05-13 20:52:50 peter
  824. * extra check for self and empty objects
  825. Revision 1.55 2003/05/13 19:18:08 peter
  826. * fpc_help_fail compilerproc
  827. * fpc_new_class, fpc_dispose_class not needed by latest compiler
  828. Revision 1.54 2003/04/23 13:10:09 peter
  829. * remvoe objectsize loading from help_destructor
  830. * implement fpc_check_object
  831. * saveregistrers for check_object
  832. Revision 1.53 2003/04/02 14:05:45 peter
  833. * undo previous commit
  834. Revision 1.51 2003/03/26 00:17:34 peter
  835. * generic constructor/destructor fixes
  836. Revision 1.50 2003/02/18 17:56:06 jonas
  837. - removed buggy i386-specific FPC_CHARARRAY_TO_SHORTSTR
  838. * fixed generic FPC_CHARARRAY_TO_SHORTSTR (web bug 2382)
  839. * fixed some potential range errors in indexchar/word/dword
  840. Revision 1.49 2003/01/20 22:21:36 mazen
  841. * many stuff related to RTL fixed
  842. Revision 1.48 2003/01/09 20:14:20 florian
  843. * fixed helper declarations
  844. Revision 1.47 2003/01/07 22:04:12 mazen
  845. - space removed
  846. Revision 1.46 2003/01/06 23:04:21 mazen
  847. * functions headers modified in generic.inc to make it possible compiling sparc
  848. RTL based on generic code
  849. Revision 1.45 2003/01/05 21:32:35 mazen
  850. * fixing several bugs compiling the RTL
  851. Revision 1.44 2002/12/23 21:27:13 peter
  852. * fix wrong var names for shortstr_compare
  853. Revision 1.43 2002/10/20 11:51:54 carl
  854. * avoid crashes with negative len counts on fills/moves
  855. * movechar0 was wrong and did not do the behavior as
  856. described in docs
  857. Revision 1.42 2002/10/14 19:39:17 peter
  858. * threads unit added for thread support
  859. Revision 1.41 2002/10/12 20:32:41 carl
  860. * RunError 220 -> RunError 219 to be more consistent with as operator
  861. Revision 1.40 2002/10/10 16:08:50 florian
  862. + several widestring/pwidechar related helpers added
  863. Revision 1.39 2002/10/05 14:20:16 peter
  864. * fpc_pchar_length compilerproc and strlen alias
  865. Revision 1.38 2002/10/02 18:21:51 peter
  866. * Copy() changed to internal function calling compilerprocs
  867. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  868. new copy functions
  869. Revision 1.37 2002/09/27 21:10:40 carl
  870. * fix 2GB limit problem
  871. Revision 1.36 2002/09/13 19:13:06 carl
  872. * FPC_HELP_FAIL : reset _self to nil
  873. Revision 1.35 2002/09/10 21:29:44 jonas
  874. * added some missing compilerproc directives
  875. Revision 1.34 2002/09/07 21:08:42 carl
  876. * cardinal -> longword
  877. - remove generic boundcheck (does not exist in v1.1)
  878. Revision 1.33 2002/09/07 15:07:45 peter
  879. * old logs removed and tabs fixed
  880. Revision 1.32 2002/08/19 19:34:02 peter
  881. * SYSTEMINLINE define that will add inline directives for small
  882. functions and wrappers. This will be defined automaticly when
  883. the compiler defines the HASINLINE directive
  884. Revision 1.31 2002/07/29 21:28:16 florian
  885. * several fixes to get further with linux/ppc system unit compilation
  886. Revision 1.30 2002/07/29 09:23:11 jonas
  887. * fixed some datastructures > 2GB
  888. Revision 1.29 2002/07/28 21:39:28 florian
  889. * made abs a compiler proc if it is generic
  890. Revision 1.28 2002/07/28 20:43:47 florian
  891. * several fixes for linux/powerpc
  892. * several fixes to MT
  893. Revision 1.27 2002/06/16 08:19:03 carl
  894. * bugfix of FPC_NEW_CLASS (was not creating correct instance)
  895. + FPC_HELP_FAIL_CLASS now tested (no longer required)
  896. Revision 1.25 2002/05/16 19:58:05 carl
  897. * generic constructor implemented
  898. Revision 1.24 2002/03/30 13:08:54 carl
  899. * memory corruption bugfix in FPC_HELP_CONSTRUCTOR if object cannot be allocated
  900. Revision 1.23 2002/01/25 17:38:55 peter
  901. * add internconst for all overloaded types of Odd/Abs/Sqr
  902. Revision 1.22 2002/01/24 12:33:53 jonas
  903. * adapted ranges of native types to int64 (e.g. high cardinal is no
  904. longer longint($ffffffff), but just $fffffff in psystem)
  905. * small additional fix in 64bit rangecheck code generation for 32 bit
  906. processors
  907. * adaption of ranges required the matching talgorithm used for selecting
  908. which overloaded procedure to call to be adapted. It should now always
  909. select the closest match for ordinal parameters.
  910. + inttostr(qword) in sysstr.inc/sysstrh.inc
  911. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  912. fixes were required to be able to add them)
  913. * is_in_limit() moved from ncal to types unit, should always be used
  914. instead of direct comparisons of low/high values of orddefs because
  915. qword is a special case
  916. }