generic.inc 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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 then
  304. begin
  305. getmem(_self,pvmt(_vmt)^.size);
  306. { reset vmt needed for fail }
  307. _vmt:=pointer(-1);
  308. end;
  309. if _self<>nil then
  310. begin
  311. fillchar(_self^,pvmt(vmtcopy)^.size,#0);
  312. ppointer(_self+_vmt_pos)^:=vmtcopy;
  313. end;
  314. fpc_help_constructor:=_self;
  315. end;
  316. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  317. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  318. { Note: _self will not be reset, the compiler has to generate the reset }
  319. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  320. type
  321. ppointer = ^pointer;
  322. pvmt = ^tvmt;
  323. tvmt = packed record
  324. size,msize : longint;
  325. parent : pointer;
  326. end;
  327. var
  328. objectsize : longint;
  329. begin
  330. if (_self=nil) then
  331. exit;
  332. if (pvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  333. (pvmt(ppointer(_self+vmt_pos)^)^.size+pvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  334. RunError(210);
  335. if (_vmt = nil) then
  336. exit;
  337. objectsize:=pvmt(_vmt)^.size;
  338. { reset vmt to nil for protection }
  339. ppointer(_self+vmt_pos)^:=nil;
  340. freemem(_self,objectsize);
  341. end;
  342. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  343. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  344. { Note: _self will not be reset, the compiler has to generate the reset }
  345. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  346. type
  347. ppointer = ^pointer;
  348. pvmt = ^tvmt;
  349. tvmt = packed record
  350. size,msize : longint;
  351. parent : pointer;
  352. end;
  353. begin
  354. if _vmt=nil then
  355. exit;
  356. { vmt=-1 when memory was allocated }
  357. if longint(_vmt)=-1 then
  358. begin
  359. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  360. HandleError(210)
  361. else
  362. begin
  363. ppointer(_self+vmt_pos)^:=nil;
  364. freemem(_self);
  365. { reset _vmt to 0 so it will not be freed a
  366. second time }
  367. _vmt:=0;
  368. end;
  369. end
  370. else
  371. ppointer(_self+vmt_pos)^:=nil;
  372. end;
  373. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  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. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  396. procedure fpc_check_object(obj : pointer);[public,alias:'FPC_CHECK_OBJECT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  397. type
  398. pvmt = ^tvmt;
  399. tvmt = packed record
  400. size,msize : longint;
  401. parent : pointer;
  402. end;
  403. begin
  404. (* if (vmt=nil) or
  405. (pvmt(vmt)^.size=0) or
  406. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  407. RunError(210);*)
  408. end;
  409. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  410. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  411. { checks for a correct vmt pointer }
  412. { deeper check to see if the current object is }
  413. { really related to the true }
  414. procedure fpc_check_object_ext(vmt, expvmt : pointer);[public,alias:'FPC_CHECK_OBJECT_EXT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  415. type
  416. pvmt = ^tvmt;
  417. tvmt = packed record
  418. size,msize : longint;
  419. parent : pointer;
  420. end;
  421. begin
  422. if (vmt=nil) or
  423. (pvmt(vmt)^.size=0) or
  424. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  425. RunError(210);
  426. while assigned(vmt) do
  427. if vmt=expvmt then
  428. exit
  429. else
  430. vmt:=pvmt(vmt)^.parent;
  431. RunError(219);
  432. end;
  433. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  434. {****************************************************************************
  435. String
  436. ****************************************************************************}
  437. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  438. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  439. var
  440. slen : byte;
  441. begin
  442. { these are shortstrings, not pointers! (JM)
  443. if dstr=nil then
  444. exit;
  445. if sstr=nil then
  446. begin
  447. if dstr<>nil then
  448. pstring(dstr)^[0]:=#0;
  449. exit;
  450. end;
  451. }
  452. slen:=length(sstr);
  453. if slen<len then
  454. len:=slen;
  455. { don't forget the length character }
  456. if len <> 0 then
  457. move(sstr[0],result[0],len+1);
  458. end;
  459. {$ifdef interncopy}
  460. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  461. {$else}
  462. procedure fpc_shortstr_copy(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_COPY']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  463. {$endif}
  464. var
  465. slen : byte;
  466. type
  467. pstring = ^string;
  468. begin
  469. { these are shortstrings, not pointers! (JM)
  470. if dstr=nil then
  471. exit;
  472. if sstr=nil then
  473. begin
  474. if dstr<>nil then
  475. pstring(dstr)^[0]:=#0;
  476. exit;
  477. end;
  478. }
  479. slen:=length(pstring(sstr)^);
  480. if slen<len then
  481. len:=slen;
  482. { don't forget the length character }
  483. if len <> 0 then
  484. move(sstr^,dstr^,len+1);
  485. { already done by the move above (JM)
  486. pstring(dstr)^[0]:=chr(len);
  487. }
  488. end;
  489. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  490. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  491. { note: this routine is *DIFFERENT* from the routine in i386.inc and as such you }
  492. { cannot use it with the i386 compiler, unless you remove the }
  493. { ti386addnode.first_string method (JM) }
  494. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  495. var
  496. s1l, s2l : byte;
  497. begin
  498. { these are shortstrings, they can't be nil! (JM)
  499. if (s1=nil) or (s2=nil) then
  500. exit;
  501. }
  502. s1l:=length(s1);
  503. s2l:=length(s2);
  504. if s1l+s2l>255 then
  505. s2l:=255-s1l;
  506. fpc_shortstr_concat := s1;
  507. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  508. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  509. end;
  510. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  511. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  512. function fpc_shortstr_compare(const dstr,sstr:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  513. var
  514. s1,s2,max,i : byte;
  515. d : longint;
  516. begin
  517. s1:=length(dstr);
  518. s2:=length(sstr);
  519. if s1<s2 then
  520. max:=s1
  521. else
  522. max:=s2;
  523. for i:=1 to max do
  524. begin
  525. d:=byte(sstr[i])-byte(dstr[i]);
  526. if d>0 then
  527. exit(1)
  528. else if d<0 then
  529. exit(-1);
  530. end;
  531. if s1>s2 then
  532. exit(1)
  533. else if s1<s2 then
  534. exit(-1)
  535. else
  536. exit(0);
  537. end;
  538. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  539. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  540. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  541. var
  542. l : longint;
  543. s: shortstring;
  544. begin
  545. if p=nil then
  546. l:=0
  547. else
  548. l:=strlen(p);
  549. if l>255 then
  550. l:=255;
  551. if l>0 then
  552. move(p^,s[1],l);
  553. s[0]:=chr(l);
  554. fpc_pchar_to_shortstr := s;
  555. end;
  556. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  557. { also add a strpas alias for internal use in the system unit (JM) }
  558. function strpas(p:pchar):shortstring; [external name 'FPC_PCHAR_TO_SHORTSTR'];
  559. { also add a strlen alias for internal use in the system unit (JM) }
  560. function strlen(p:pchar):longint; [external name 'FPC_PCHAR_LENGTH'];
  561. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  562. {$ifdef hascompilerproc}
  563. function fpc_chararray_to_shortstr(const arr: array of char):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  564. var
  565. l: longint;
  566. {$else hascompilerproc}
  567. function fpc_chararray_to_shortstr(arr:pchar; l : longint):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR'];
  568. var
  569. {$endif hascompilerproc}
  570. index: longint;
  571. len: byte;
  572. begin
  573. {$ifdef hascompilerproc}
  574. l := high(arr)+1;
  575. {$endif hascompilerproc}
  576. if l>=256 then
  577. l:=255
  578. else if l<0 then
  579. l:=0;
  580. index:=IndexByte(arr[0],l,0);
  581. if (index < 0) then
  582. len := l
  583. else
  584. len := index;
  585. move(arr[0],fpc_chararray_to_shortstr[1],len);
  586. fpc_chararray_to_shortstr[0]:=chr(len);
  587. end;
  588. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  589. {$ifdef hascompilerproc}
  590. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  591. { inside the compiler, the resulttype is modified to that of the actual }
  592. { chararray we're converting to (JM) }
  593. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  594. var
  595. len: longint;
  596. begin
  597. len := length(src);
  598. if len > arraysize then
  599. len := arraysize;
  600. { make sure we don't access char 1 if length is 0 (JM) }
  601. if len > 0 then
  602. move(src[1],fpc_shortstr_to_chararray[0],len);
  603. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  604. end;
  605. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  606. {$else hascompilerproc}
  607. {$ifopt r+}
  608. {$define rangeon}
  609. {$r-}
  610. {$endif}
  611. {$ifndef FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  612. procedure fpc_str_to_chararray(strtyp, arraysize: longint; src,dest: pchar);[public,alias:'FPC_STR_TO_CHARARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  613. type
  614. plongint = ^longint;
  615. var
  616. len: longint;
  617. begin
  618. case strtyp of
  619. { shortstring }
  620. 0:
  621. begin
  622. len := byte(src[0]);
  623. inc(src);
  624. end;
  625. {$ifdef SUPPORT_ANSISTRING}
  626. { ansistring}
  627. 1: len := length(ansistring(pointer(src)));
  628. {$endif SUPPORT_ANSISTRING}
  629. { longstring }
  630. 2:;
  631. { widestring }
  632. 3: ;
  633. end;
  634. if len > arraysize then
  635. len := arraysize;
  636. { make sure we don't dereference src if it can be nil (JM) }
  637. if len > 0 then
  638. move(src^,dest^,len);
  639. fillchar(dest[len],arraysize-len,0);
  640. end;
  641. {$endif FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  642. {$ifdef rangeon}
  643. {$r+}
  644. {undef rangeon}
  645. {$endif rangeon}
  646. {$endif hascompilerproc}
  647. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  648. function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  649. var i : longint;
  650. begin
  651. i:=0;
  652. while p[i]<>#0 do inc(i);
  653. exit(i);
  654. end;
  655. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  656. {$ifdef HASWIDESTRING}
  657. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  658. function fpc_pwidechar_length(p:pwidechar):longint;[public,alias:'FPC_PWIDECHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  659. var i : longint;
  660. begin
  661. i:=0;
  662. while p[i]<>#0 do inc(i);
  663. exit(i);
  664. end;
  665. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  666. {$endif HASWIDESTRING}
  667. {****************************************************************************
  668. Caller/StackFrame Helpers
  669. ****************************************************************************}
  670. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  671. {_$error Get_frame must be defined for each processor }
  672. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  673. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  674. {_$error Get_caller_addr must be defined for each processor }
  675. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  676. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  677. {_$error Get_caller_frame must be defined for each processor }
  678. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  679. {****************************************************************************
  680. Math
  681. ****************************************************************************}
  682. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  683. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  684. begin
  685. if l<0 then
  686. abs:=-l
  687. else
  688. abs:=l;
  689. end;
  690. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  691. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  692. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  693. begin
  694. odd:=boolean(l and 1);
  695. end;
  696. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  697. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  698. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  699. begin
  700. odd:=boolean(l and 1);
  701. end;
  702. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  703. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  704. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  705. begin
  706. odd:=boolean(longint(l) and 1);
  707. end;
  708. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  709. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  710. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  711. begin
  712. odd:=boolean(longint(l) and 1);
  713. end;
  714. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  715. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  716. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  717. begin
  718. sqr:=l*l;
  719. end;
  720. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  721. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  722. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  723. begin
  724. if l < 0 then
  725. abs := -l
  726. else
  727. abs := l;
  728. end;
  729. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  730. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  731. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  732. begin
  733. sqr := l*l;
  734. end;
  735. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  736. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  737. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  738. begin
  739. sqr := l*l;
  740. end;
  741. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  742. {$ifndef FPC_SYSTEM_HAS_DECLOCKED}
  743. function declocked(var l:longint):boolean;
  744. begin
  745. Dec(l);
  746. declocked:=(l=0);
  747. end;
  748. {$endif FPC_SYSTEM_HAS_DECLOCKED}
  749. {$ifndef FPC_SYSTEM_HAS_INCLOCKED}
  750. procedure inclocked(var l:longint);
  751. begin
  752. Inc(l);
  753. end;
  754. {$endif FPC_SYSTEM_HAS_INCLOCKED}
  755. {$ifndef FPC_SYSTEM_HAS_SPTR}
  756. {_$error Sptr must be defined for each processor }
  757. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  758. {****************************************************************************
  759. Str()
  760. ****************************************************************************}
  761. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  762. procedure int_str(l : longint;var s : string);
  763. var
  764. value: longint;
  765. negative: boolean;
  766. begin
  767. negative := false;
  768. s:='';
  769. { Workaround: }
  770. if l=$80000000 then
  771. begin
  772. s:='-2147483648';
  773. exit;
  774. end;
  775. { handle case where l = 0 }
  776. if l = 0 then
  777. begin
  778. s:='0';
  779. exit;
  780. end;
  781. If l < 0 then
  782. begin
  783. negative := true;
  784. value:=abs(l);
  785. end
  786. else
  787. value:=l;
  788. { handle non-zero case }
  789. while value>0 do
  790. begin
  791. s:=char((value mod 10)+ord('0'))+s;
  792. value := value div 10;
  793. end;
  794. if negative then
  795. s := '-' + s;
  796. end;
  797. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  798. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  799. procedure int_str(l : longword;var s : string);
  800. begin
  801. s:='';
  802. if l = 0 then
  803. begin
  804. s := '0';
  805. exit;
  806. end;
  807. while l>0 do
  808. begin
  809. s:=char(ord('0')+(l mod 10))+s;
  810. l:=l div 10;
  811. end;
  812. end;
  813. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  814. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  815. procedure SysResetFpu;
  816. begin
  817. { nothing todo }
  818. end;
  819. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  820. {
  821. $Log$
  822. Revision 1.53 2003-04-02 14:05:45 peter
  823. * undo previous commit
  824. Revision 1.51 2003/03/26 00:17:34 peter
  825. * generic constructor/destructor fixes
  826. Revision 1.50 2003/02/18 17:56:06 jonas
  827. - removed buggy i386-specific FPC_CHARARRAY_TO_SHORTSTR
  828. * fixed generic FPC_CHARARRAY_TO_SHORTSTR (web bug 2382)
  829. * fixed some potential range errors in indexchar/word/dword
  830. Revision 1.49 2003/01/20 22:21:36 mazen
  831. * many stuff related to RTL fixed
  832. Revision 1.48 2003/01/09 20:14:20 florian
  833. * fixed helper declarations
  834. Revision 1.47 2003/01/07 22:04:12 mazen
  835. - space removed
  836. Revision 1.46 2003/01/06 23:04:21 mazen
  837. * functions headers modified in generic.inc to make it possible compiling sparc
  838. RTL based on generic code
  839. Revision 1.45 2003/01/05 21:32:35 mazen
  840. * fixing several bugs compiling the RTL
  841. Revision 1.44 2002/12/23 21:27:13 peter
  842. * fix wrong var names for shortstr_compare
  843. Revision 1.43 2002/10/20 11:51:54 carl
  844. * avoid crashes with negative len counts on fills/moves
  845. * movechar0 was wrong and did not do the behavior as
  846. described in docs
  847. Revision 1.42 2002/10/14 19:39:17 peter
  848. * threads unit added for thread support
  849. Revision 1.41 2002/10/12 20:32:41 carl
  850. * RunError 220 -> RunError 219 to be more consistent with as operator
  851. Revision 1.40 2002/10/10 16:08:50 florian
  852. + several widestring/pwidechar related helpers added
  853. Revision 1.39 2002/10/05 14:20:16 peter
  854. * fpc_pchar_length compilerproc and strlen alias
  855. Revision 1.38 2002/10/02 18:21:51 peter
  856. * Copy() changed to internal function calling compilerprocs
  857. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  858. new copy functions
  859. Revision 1.37 2002/09/27 21:10:40 carl
  860. * fix 2GB limit problem
  861. Revision 1.36 2002/09/13 19:13:06 carl
  862. * FPC_HELP_FAIL : reset _self to nil
  863. Revision 1.35 2002/09/10 21:29:44 jonas
  864. * added some missing compilerproc directives
  865. Revision 1.34 2002/09/07 21:08:42 carl
  866. * cardinal -> longword
  867. - remove generic boundcheck (does not exist in v1.1)
  868. Revision 1.33 2002/09/07 15:07:45 peter
  869. * old logs removed and tabs fixed
  870. Revision 1.32 2002/08/19 19:34:02 peter
  871. * SYSTEMINLINE define that will add inline directives for small
  872. functions and wrappers. This will be defined automaticly when
  873. the compiler defines the HASINLINE directive
  874. Revision 1.31 2002/07/29 21:28:16 florian
  875. * several fixes to get further with linux/ppc system unit compilation
  876. Revision 1.30 2002/07/29 09:23:11 jonas
  877. * fixed some datastructures > 2GB
  878. Revision 1.29 2002/07/28 21:39:28 florian
  879. * made abs a compiler proc if it is generic
  880. Revision 1.28 2002/07/28 20:43:47 florian
  881. * several fixes for linux/powerpc
  882. * several fixes to MT
  883. Revision 1.27 2002/06/16 08:19:03 carl
  884. * bugfix of FPC_NEW_CLASS (was not creating correct instance)
  885. + FPC_HELP_FAIL_CLASS now tested (no longer required)
  886. Revision 1.25 2002/05/16 19:58:05 carl
  887. * generic constructor implemented
  888. Revision 1.24 2002/03/30 13:08:54 carl
  889. * memory corruption bugfix in FPC_HELP_CONSTRUCTOR if object cannot be allocated
  890. Revision 1.23 2002/01/25 17:38:55 peter
  891. * add internconst for all overloaded types of Odd/Abs/Sqr
  892. Revision 1.22 2002/01/24 12:33:53 jonas
  893. * adapted ranges of native types to int64 (e.g. high cardinal is no
  894. longer longint($ffffffff), but just $fffffff in psystem)
  895. * small additional fix in 64bit rangecheck code generation for 32 bit
  896. processors
  897. * adaption of ranges required the matching talgorithm used for selecting
  898. which overloaded procedure to call to be adapted. It should now always
  899. select the closest match for ordinal parameters.
  900. + inttostr(qword) in sysstr.inc/sysstrh.inc
  901. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  902. fixes were required to be able to add them)
  903. * is_in_limit() moved from ncal to types unit, should always be used
  904. instead of direct comparisons of low/high values of orddefs because
  905. qword is a special case
  906. }