generic.inc 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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 nil so it will not be freed a
  365. second time }
  366. _vmt:=nil;
  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. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  493. var
  494. s1l, s2l : byte;
  495. begin
  496. s1l:=length(s1);
  497. s2l:=length(s2);
  498. if s1l+s2l>255 then
  499. s2l:=255-s1l;
  500. move(s1[1],fpc_shortstr_concat[1],s1l);
  501. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  502. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  503. end;
  504. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  505. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  506. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);{$ifdef hascompilerproc} compilerproc; {$endif}
  507. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  508. var
  509. s1l, s2l : byte;
  510. begin
  511. s1l:=length(s1);
  512. s2l:=length(s2);
  513. if s1l+s2l>high(s1) then
  514. s2l:=high(s1)-s1l;
  515. move(s2[1],s1[s1l+1],s2l);
  516. s1[0]:=chr(s1l+s2l);
  517. end;
  518. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  519. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  520. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; {$ifdef hascompilerproc} compilerproc; {$endif}
  521. var
  522. s1,s2,max,i : byte;
  523. d : longint;
  524. begin
  525. s1:=length(left);
  526. s2:=length(right);
  527. if s1<s2 then
  528. max:=s1
  529. else
  530. max:=s2;
  531. for i:=1 to max do
  532. begin
  533. d:=byte(left[i])-byte(right[i]);
  534. if d>0 then
  535. exit(1)
  536. else if d<0 then
  537. exit(-1);
  538. end;
  539. if s1>s2 then
  540. exit(1)
  541. else if s1<s2 then
  542. exit(-1)
  543. else
  544. exit(0);
  545. end;
  546. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  547. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  548. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; {$ifdef hascompilerproc} compilerproc; {$endif}
  549. var
  550. l : longint;
  551. s: shortstring;
  552. begin
  553. if p=nil then
  554. l:=0
  555. else
  556. l:=strlen(p);
  557. if l>255 then
  558. l:=255;
  559. if l>0 then
  560. move(p^,s[1],l);
  561. s[0]:=chr(l);
  562. fpc_pchar_to_shortstr := s;
  563. end;
  564. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  565. { also add a strpas alias for internal use in the system unit (JM) }
  566. function strpas(p:pchar):shortstring; [external name 'FPC_PCHAR_TO_SHORTSTR'];
  567. { also add a strlen alias for internal use in the system unit (JM) }
  568. function strlen(p:pchar):longint; [external name 'FPC_PCHAR_LENGTH'];
  569. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  570. {$ifdef hascompilerproc}
  571. function fpc_chararray_to_shortstr(const arr: array of char):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  572. var
  573. l: longint;
  574. {$else hascompilerproc}
  575. function fpc_chararray_to_shortstr(arr:pchar; l : longint):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR'];
  576. var
  577. {$endif hascompilerproc}
  578. index: longint;
  579. len: byte;
  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. index:=IndexByte(arr[0],l,0);
  589. if (index < 0) then
  590. len := l
  591. else
  592. len := index;
  593. move(arr[0],fpc_chararray_to_shortstr[1],len);
  594. fpc_chararray_to_shortstr[0]:=chr(len);
  595. end;
  596. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  597. {$ifdef hascompilerproc}
  598. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  599. { inside the compiler, the resulttype is modified to that of the actual }
  600. { chararray we're converting to (JM) }
  601. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  602. var
  603. len: longint;
  604. begin
  605. len := length(src);
  606. if len > arraysize then
  607. len := arraysize;
  608. { make sure we don't access char 1 if length is 0 (JM) }
  609. if len > 0 then
  610. move(src[1],fpc_shortstr_to_chararray[0],len);
  611. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  612. end;
  613. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  614. {$else hascompilerproc}
  615. {$ifopt r+}
  616. {$define rangeon}
  617. {$r-}
  618. {$endif}
  619. {$ifndef FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  620. procedure fpc_str_to_chararray(strtyp, arraysize: longint; src,dest: pchar);[public,alias:'FPC_STR_TO_CHARARRAY']; {$ifdef hascompilerproc} compilerproc; {$endif}
  621. type
  622. plongint = ^longint;
  623. var
  624. len: longint;
  625. begin
  626. case strtyp of
  627. { shortstring }
  628. 0:
  629. begin
  630. len := byte(src[0]);
  631. inc(src);
  632. end;
  633. {$ifdef SUPPORT_ANSISTRING}
  634. { ansistring}
  635. 1: len := length(ansistring(pointer(src)));
  636. {$endif SUPPORT_ANSISTRING}
  637. { longstring }
  638. 2:;
  639. { widestring }
  640. 3: ;
  641. end;
  642. if len > arraysize then
  643. len := arraysize;
  644. { make sure we don't dereference src if it can be nil (JM) }
  645. if len > 0 then
  646. move(src^,dest^,len);
  647. fillchar(dest[len],arraysize-len,0);
  648. end;
  649. {$endif FPC_SYSTEM_HAS_FPC_STR_TO_CHARARRAY}
  650. {$ifdef rangeon}
  651. {$r+}
  652. {undef rangeon}
  653. {$endif rangeon}
  654. {$endif hascompilerproc}
  655. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  656. function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  657. var i : longint;
  658. begin
  659. i:=0;
  660. while p[i]<>#0 do inc(i);
  661. exit(i);
  662. end;
  663. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  664. {$ifdef HASWIDESTRING}
  665. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  666. function fpc_pwidechar_length(p:pwidechar):longint;[public,alias:'FPC_PWIDECHAR_LENGTH']; {$ifdef hascompilerproc} compilerproc; {$endif}
  667. var i : longint;
  668. begin
  669. i:=0;
  670. while p[i]<>#0 do inc(i);
  671. exit(i);
  672. end;
  673. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  674. {$endif HASWIDESTRING}
  675. {****************************************************************************
  676. Caller/StackFrame Helpers
  677. ****************************************************************************}
  678. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  679. {_$error Get_frame must be defined for each processor }
  680. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  681. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  682. {_$error Get_caller_addr must be defined for each processor }
  683. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  684. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  685. {_$error Get_caller_frame must be defined for each processor }
  686. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  687. {****************************************************************************
  688. Math
  689. ****************************************************************************}
  690. {****************************************************************************
  691. Software longint/dword division
  692. ****************************************************************************}
  693. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  694. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  695. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; {$ifdef hascompilerproc} compilerproc; {$endif}
  696. begin
  697. {!!!!}
  698. end;
  699. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  700. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  701. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; {$ifdef hascompilerproc} compilerproc; {$endif}
  702. begin
  703. {!!!!}
  704. end;
  705. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  706. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  707. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  708. begin
  709. {!!!!}
  710. end;
  711. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  712. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  713. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; {$ifdef hascompilerproc} compilerproc; {$endif}
  714. begin
  715. {!!!!}
  716. end;
  717. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  718. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  719. {****************************************************************************}
  720. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  721. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  722. begin
  723. if l<0 then
  724. abs:=-l
  725. else
  726. abs:=l;
  727. end;
  728. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  729. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  730. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  731. begin
  732. odd:=boolean(l and 1);
  733. end;
  734. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  735. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  736. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  737. begin
  738. odd:=boolean(l and 1);
  739. end;
  740. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  741. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  742. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  743. begin
  744. odd:=boolean(longint(l) and 1);
  745. end;
  746. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  747. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  748. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_odd];
  749. begin
  750. odd:=boolean(longint(l) and 1);
  751. end;
  752. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  753. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  754. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  755. begin
  756. sqr:=l*l;
  757. end;
  758. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  759. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  760. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_abs];
  761. begin
  762. if l < 0 then
  763. abs := -l
  764. else
  765. abs := l;
  766. end;
  767. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  768. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  769. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  770. begin
  771. sqr := l*l;
  772. end;
  773. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  774. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  775. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}[internconst:in_const_sqr];
  776. begin
  777. sqr := l*l;
  778. end;
  779. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  780. {$ifndef FPC_SYSTEM_HAS_DECLOCKED}
  781. function declocked(var l:longint):boolean;
  782. begin
  783. Dec(l);
  784. declocked:=(l=0);
  785. end;
  786. {$endif FPC_SYSTEM_HAS_DECLOCKED}
  787. {$ifndef FPC_SYSTEM_HAS_INCLOCKED}
  788. procedure inclocked(var l:longint);
  789. begin
  790. Inc(l);
  791. end;
  792. {$endif FPC_SYSTEM_HAS_INCLOCKED}
  793. {$ifndef FPC_SYSTEM_HAS_SPTR}
  794. {_$error Sptr must be defined for each processor }
  795. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  796. {****************************************************************************
  797. Str()
  798. ****************************************************************************}
  799. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  800. procedure int_str(l : longint;var s : string);
  801. var
  802. value: longint;
  803. negative: boolean;
  804. begin
  805. negative := false;
  806. s:='';
  807. { Workaround: }
  808. if l=longint($80000000) then
  809. begin
  810. s:='-2147483648';
  811. exit;
  812. end;
  813. { handle case where l = 0 }
  814. if l = 0 then
  815. begin
  816. s:='0';
  817. exit;
  818. end;
  819. If l < 0 then
  820. begin
  821. negative := true;
  822. value:=abs(l);
  823. end
  824. else
  825. value:=l;
  826. { handle non-zero case }
  827. while value>0 do
  828. begin
  829. s:=char((value mod 10)+ord('0'))+s;
  830. value := value div 10;
  831. end;
  832. if negative then
  833. s := '-' + s;
  834. end;
  835. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  836. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  837. procedure int_str(l : longword;var s : string);
  838. begin
  839. s:='';
  840. if l = 0 then
  841. begin
  842. s := '0';
  843. exit;
  844. end;
  845. while l>0 do
  846. begin
  847. s:=char(ord('0')+(l mod 10))+s;
  848. l:=l div 10;
  849. end;
  850. end;
  851. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  852. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  853. procedure SysResetFpu;
  854. begin
  855. { nothing todo }
  856. end;
  857. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  858. {
  859. $Log$
  860. Revision 1.63 2003-12-16 09:43:04 daniel
  861. * Use of 0 instead of nil fixed
  862. Revision 1.62 2003/12/06 13:25:30 jonas
  863. * fixed longint/cardinal comparison in int_str
  864. Revision 1.61 2003/09/03 14:09:37 florian
  865. * arm fixes to the common rtl code
  866. * some generic math code fixed
  867. * ...
  868. Revision 1.60 2003/06/01 14:50:17 jonas
  869. * fpc_shortstr_append_shortstr has to use high(s1) instead of 255 as
  870. maxlen
  871. + ppc version of fpc_shortstr_append_shortstr
  872. Revision 1.59 2003/05/26 21:18:13 peter
  873. * FPC_SHORTSTR_APPEND_SHORTSTR public added
  874. Revision 1.58 2003/05/26 19:36:46 peter
  875. * fpc_shortstr_concat is now the same for all targets
  876. * fpc_shortstr_append_shortstr added for optimized code generation
  877. Revision 1.57 2003/05/16 22:40:11 florian
  878. * fixed generic shortstr_compare
  879. Revision 1.56 2003/05/13 20:52:50 peter
  880. * extra check for self and empty objects
  881. Revision 1.55 2003/05/13 19:18:08 peter
  882. * fpc_help_fail compilerproc
  883. * fpc_new_class, fpc_dispose_class not needed by latest compiler
  884. Revision 1.54 2003/04/23 13:10:09 peter
  885. * remvoe objectsize loading from help_destructor
  886. * implement fpc_check_object
  887. * saveregistrers for check_object
  888. Revision 1.53 2003/04/02 14:05:45 peter
  889. * undo previous commit
  890. Revision 1.51 2003/03/26 00:17:34 peter
  891. * generic constructor/destructor fixes
  892. Revision 1.50 2003/02/18 17:56:06 jonas
  893. - removed buggy i386-specific FPC_CHARARRAY_TO_SHORTSTR
  894. * fixed generic FPC_CHARARRAY_TO_SHORTSTR (web bug 2382)
  895. * fixed some potential range errors in indexchar/word/dword
  896. Revision 1.49 2003/01/20 22:21:36 mazen
  897. * many stuff related to RTL fixed
  898. Revision 1.48 2003/01/09 20:14:20 florian
  899. * fixed helper declarations
  900. Revision 1.47 2003/01/07 22:04:12 mazen
  901. - space removed
  902. Revision 1.46 2003/01/06 23:04:21 mazen
  903. * functions headers modified in generic.inc to make it possible compiling sparc
  904. RTL based on generic code
  905. Revision 1.45 2003/01/05 21:32:35 mazen
  906. * fixing several bugs compiling the RTL
  907. Revision 1.44 2002/12/23 21:27:13 peter
  908. * fix wrong var names for shortstr_compare
  909. Revision 1.43 2002/10/20 11:51:54 carl
  910. * avoid crashes with negative len counts on fills/moves
  911. * movechar0 was wrong and did not do the behavior as
  912. described in docs
  913. Revision 1.42 2002/10/14 19:39:17 peter
  914. * threads unit added for thread support
  915. Revision 1.41 2002/10/12 20:32:41 carl
  916. * RunError 220 -> RunError 219 to be more consistent with as operator
  917. Revision 1.40 2002/10/10 16:08:50 florian
  918. + several widestring/pwidechar related helpers added
  919. Revision 1.39 2002/10/05 14:20:16 peter
  920. * fpc_pchar_length compilerproc and strlen alias
  921. Revision 1.38 2002/10/02 18:21:51 peter
  922. * Copy() changed to internal function calling compilerprocs
  923. * FPC_SHORTSTR_COPY renamed to FPC_SHORTSTR_ASSIGN because of the
  924. new copy functions
  925. Revision 1.37 2002/09/27 21:10:40 carl
  926. * fix 2GB limit problem
  927. Revision 1.36 2002/09/13 19:13:06 carl
  928. * FPC_HELP_FAIL : reset _self to nil
  929. Revision 1.35 2002/09/10 21:29:44 jonas
  930. * added some missing compilerproc directives
  931. Revision 1.34 2002/09/07 21:08:42 carl
  932. * cardinal -> longword
  933. - remove generic boundcheck (does not exist in v1.1)
  934. Revision 1.33 2002/09/07 15:07:45 peter
  935. * old logs removed and tabs fixed
  936. Revision 1.32 2002/08/19 19:34:02 peter
  937. * SYSTEMINLINE define that will add inline directives for small
  938. functions and wrappers. This will be defined automaticly when
  939. the compiler defines the HASINLINE directive
  940. Revision 1.31 2002/07/29 21:28:16 florian
  941. * several fixes to get further with linux/ppc system unit compilation
  942. Revision 1.30 2002/07/29 09:23:11 jonas
  943. * fixed some datastructures > 2GB
  944. Revision 1.29 2002/07/28 21:39:28 florian
  945. * made abs a compiler proc if it is generic
  946. Revision 1.28 2002/07/28 20:43:47 florian
  947. * several fixes for linux/powerpc
  948. * several fixes to MT
  949. Revision 1.27 2002/06/16 08:19:03 carl
  950. * bugfix of FPC_NEW_CLASS (was not creating correct instance)
  951. + FPC_HELP_FAIL_CLASS now tested (no longer required)
  952. Revision 1.25 2002/05/16 19:58:05 carl
  953. * generic constructor implemented
  954. Revision 1.24 2002/03/30 13:08:54 carl
  955. * memory corruption bugfix in FPC_HELP_CONSTRUCTOR if object cannot be allocated
  956. Revision 1.23 2002/01/25 17:38:55 peter
  957. * add internconst for all overloaded types of Odd/Abs/Sqr
  958. Revision 1.22 2002/01/24 12:33:53 jonas
  959. * adapted ranges of native types to int64 (e.g. high cardinal is no
  960. longer longint($ffffffff), but just $fffffff in psystem)
  961. * small additional fix in 64bit rangecheck code generation for 32 bit
  962. processors
  963. * adaption of ranges required the matching talgorithm used for selecting
  964. which overloaded procedure to call to be adapted. It should now always
  965. select the closest match for ordinal parameters.
  966. + inttostr(qword) in sysstr.inc/sysstrh.inc
  967. + abs(int64), sqr(int64), sqr(qword) in systemh.inc/generic.inc (previous
  968. fixes were required to be able to add them)
  969. * is_in_limit() moved from ncal to types unit, should always be used
  970. instead of direct comparisons of low/high values of orddefs because
  971. qword is a special case
  972. }