generic.inc 27 KB

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