ncgcon.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate assembler for constant nodes which are the same for
  4. all (most) processors
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  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. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncgcon;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,ncon;
  23. type
  24. tcgrealconstnode = class(trealconstnode)
  25. procedure pass_2;override;
  26. end;
  27. tcgordconstnode = class(tordconstnode)
  28. procedure pass_2;override;
  29. end;
  30. tcgpointerconstnode = class(tpointerconstnode)
  31. procedure pass_2;override;
  32. end;
  33. tcgstringconstnode = class(tstringconstnode)
  34. procedure pass_2;override;
  35. end;
  36. tcgsetconstnode = class(tsetconstnode)
  37. procedure pass_2;override;
  38. end;
  39. tcgnilnode = class(tnilnode)
  40. procedure pass_2;override;
  41. end;
  42. tcgguidconstnode = class(tguidconstnode)
  43. procedure pass_2;override;
  44. end;
  45. implementation
  46. uses
  47. globtype,widestr,systems,
  48. verbose,globals,
  49. symconst,symdef,aasmbase,aasmtai,aasmcpu,defutil,
  50. cpuinfo,cpubase,
  51. cgbase,cgobj,cgutils,
  52. ncgutil
  53. ;
  54. {*****************************************************************************
  55. TCGREALCONSTNODE
  56. *****************************************************************************}
  57. procedure tcgrealconstnode.pass_2;
  58. { I suppose the parser/pass_1 must make sure the generated real }
  59. { constants are actually supported by the target processor? (JM) }
  60. const
  61. floattype2ait:array[tfloattype] of taitype=
  62. (ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_comp_64bit,ait_real_128bit);
  63. var
  64. hp1 : tai;
  65. lastlabel : tasmlabel;
  66. realait : taitype;
  67. value_real_sign, hp1_sign: pbyte;
  68. {$ifdef ARM}
  69. hiloswapped : boolean;
  70. {$endif ARM}
  71. begin
  72. location_reset(location,LOC_CREFERENCE,def_cgsize(resulttype.def));
  73. lastlabel:=nil;
  74. realait:=floattype2ait[tfloatdef(resulttype.def).typ];
  75. {$ifdef ARM}
  76. hiloswapped:=aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11];
  77. {$endif ARM}
  78. { const already used ? }
  79. if not assigned(lab_real) then
  80. begin
  81. { tries to find an old entry }
  82. hp1:=tai(asmlist[al_typedconsts].first);
  83. while assigned(hp1) do
  84. begin
  85. if hp1.typ=ait_label then
  86. lastlabel:=tai_label(hp1).l
  87. else
  88. begin
  89. if (hp1.typ=realait) and (lastlabel<>nil) then
  90. begin
  91. if is_number_float(value_real) and
  92. (
  93. ((realait=ait_real_32bit) and (tai_real_32bit(hp1).value=value_real) and is_number_float(tai_real_32bit(hp1).value) and (get_real_sign(value_real) = get_real_sign(tai_real_32bit(hp1).value))) or
  94. ((realait=ait_real_64bit) and
  95. {$ifdef ARM}
  96. ((tai_real_64bit(hp1).formatoptions=fo_hiloswapped)=hiloswapped) and
  97. {$endif ARM}
  98. (tai_real_64bit(hp1).value=value_real) and is_number_float(tai_real_64bit(hp1).value) and (get_real_sign(value_real) = get_real_sign(tai_real_64bit(hp1).value))) or
  99. ((realait=ait_real_80bit) and (tai_real_80bit(hp1).value=value_real) and is_number_float(tai_real_80bit(hp1).value) and (get_real_sign(value_real) = get_real_sign(tai_real_80bit(hp1).value))) or
  100. {$ifdef cpufloat128}
  101. ((realait=ait_real_128bit) and (tai_real_128bit(hp1).value=value_real) and is_number_float(tai_real_128bit(hp1).value) and (get_real_sign(value_real) = get_real_sign(tai_real_128bit(hp1).value))) or
  102. {$endif cpufloat128}
  103. ((realait=ait_comp_64bit) and (tai_comp_64bit(hp1).value=value_real) and is_number_float(tai_comp_64bit(hp1).value) and (get_real_sign(value_real) = get_real_sign(tai_comp_64bit(hp1).value)))
  104. ) then
  105. begin
  106. { found! }
  107. lab_real:=lastlabel;
  108. break;
  109. end;
  110. end;
  111. lastlabel:=nil;
  112. end;
  113. hp1:=tai(hp1.next);
  114. end;
  115. { :-(, we must generate a new entry }
  116. if not assigned(lab_real) then
  117. begin
  118. objectlibrary.getdatalabel(lastlabel);
  119. lab_real:=lastlabel;
  120. maybe_new_object_file(asmlist[al_typedconsts]);
  121. new_section(asmlist[al_typedconsts],sec_rodata,lastlabel.name,const_align(resulttype.def.size));
  122. asmlist[al_typedconsts].concat(Tai_label.Create(lastlabel));
  123. case realait of
  124. ait_real_32bit :
  125. begin
  126. asmlist[al_typedconsts].concat(Tai_real_32bit.Create(ts32real(value_real)));
  127. { range checking? }
  128. if ((cs_check_range in aktlocalswitches) or
  129. (cs_check_overflow in aktlocalswitches)) and
  130. (tai_real_32bit(asmlist[al_typedconsts].last).value=double(MathInf)) then
  131. Message(parser_e_range_check_error);
  132. end;
  133. ait_real_64bit :
  134. begin
  135. {$ifdef ARM}
  136. if hiloswapped then
  137. asmlist[al_typedconsts].concat(Tai_real_64bit.Create_hiloswapped(ts64real(value_real)))
  138. else
  139. {$endif ARM}
  140. asmlist[al_typedconsts].concat(Tai_real_64bit.Create(ts64real(value_real)));
  141. { range checking? }
  142. if ((cs_check_range in aktlocalswitches) or
  143. (cs_check_overflow in aktlocalswitches)) and
  144. (tai_real_64bit(asmlist[al_typedconsts].last).value=double(MathInf)) then
  145. Message(parser_e_range_check_error);
  146. end;
  147. ait_real_80bit :
  148. begin
  149. asmlist[al_typedconsts].concat(Tai_real_80bit.Create(value_real));
  150. { range checking? }
  151. if ((cs_check_range in aktlocalswitches) or
  152. (cs_check_overflow in aktlocalswitches)) and
  153. (tai_real_80bit(asmlist[al_typedconsts].last).value=double(MathInf)) then
  154. Message(parser_e_range_check_error);
  155. end;
  156. {$ifdef cpufloat128}
  157. ait_real_128bit :
  158. begin
  159. asmlist[al_typedconsts].concat(Tai_real_128bit.Create(value_real));
  160. { range checking? }
  161. if ((cs_check_range in aktlocalswitches) or
  162. (cs_check_overflow in aktlocalswitches)) and
  163. (tai_real_128bit(asmlist[al_typedconsts].last).value=double(MathInf)) then
  164. Message(parser_e_range_check_error);
  165. end;
  166. {$endif cpufloat128}
  167. { the round is necessary for native compilers where comp isn't a float }
  168. ait_comp_64bit :
  169. if (value_real>9223372036854775807.0) or (value_real<-9223372036854775808.0) then
  170. message(parser_e_range_check_error)
  171. else
  172. asmlist[al_typedconsts].concat(Tai_comp_64bit.Create(round(value_real)));
  173. else
  174. internalerror(10120);
  175. end;
  176. end;
  177. end;
  178. location.reference.symbol:=lab_real;
  179. end;
  180. {*****************************************************************************
  181. TCGORDCONSTNODE
  182. *****************************************************************************}
  183. procedure tcgordconstnode.pass_2;
  184. begin
  185. location_reset(location,LOC_CONSTANT,def_cgsize(resulttype.def));
  186. {$ifdef cpu64bit}
  187. location.value:=value;
  188. {$else cpu64bit}
  189. location.value64:=int64(value);
  190. {$endif cpu64bit}
  191. end;
  192. {*****************************************************************************
  193. TCGPOINTERCONSTNODE
  194. *****************************************************************************}
  195. procedure tcgpointerconstnode.pass_2;
  196. begin
  197. { an integer const. behaves as a memory reference }
  198. location_reset(location,LOC_CONSTANT,OS_ADDR);
  199. location.value:=aint(value);
  200. end;
  201. {*****************************************************************************
  202. TCGSTRINGCONSTNODE
  203. *****************************************************************************}
  204. procedure tcgstringconstnode.pass_2;
  205. var
  206. hp1,hp2 : tai;
  207. l1,l2,
  208. lastlabel : tasmlabel;
  209. lastlabelhp : tai;
  210. pc : pchar;
  211. same_string : boolean;
  212. l,j,
  213. i,mylength : longint;
  214. begin
  215. { for empty ansistrings we could return a constant 0 }
  216. if (st_type in [st_ansistring,st_widestring]) and (len=0) then
  217. begin
  218. location_reset(location,LOC_CONSTANT,OS_ADDR);
  219. location.value:=0;
  220. exit;
  221. end;
  222. { return a constant reference in memory }
  223. location_reset(location,LOC_CREFERENCE,def_cgsize(resulttype.def));
  224. { const already used ? }
  225. lastlabel:=nil;
  226. lastlabelhp:=nil;
  227. if not assigned(lab_str) then
  228. begin
  229. if is_shortstring(resulttype.def) then
  230. mylength:=len+2
  231. else
  232. mylength:=len+1;
  233. { widestrings can't be reused yet }
  234. if not(is_widestring(resulttype.def)) then
  235. begin
  236. { tries to find an old entry }
  237. hp1:=tai(asmlist[al_typedconsts].first);
  238. while assigned(hp1) do
  239. begin
  240. if hp1.typ=ait_label then
  241. begin
  242. lastlabel:=tai_label(hp1).l;
  243. lastlabelhp:=hp1;
  244. end
  245. else
  246. begin
  247. same_string:=false;
  248. if (hp1.typ=ait_string) and
  249. (lastlabel<>nil) and
  250. (tai_string(hp1).len=mylength) then
  251. begin
  252. case st_type of
  253. st_conststring:
  254. begin
  255. j:=0;
  256. same_string:=true;
  257. if len>0 then
  258. begin
  259. for i:=0 to len-1 do
  260. begin
  261. if tai_string(hp1).str[j]<>value_str[i] then
  262. begin
  263. same_string:=false;
  264. break;
  265. end;
  266. inc(j);
  267. end;
  268. end;
  269. end;
  270. st_shortstring:
  271. begin
  272. { if shortstring then check the length byte first and
  273. set the start index to 1 }
  274. if len=ord(tai_string(hp1).str[0]) then
  275. begin
  276. j:=1;
  277. same_string:=true;
  278. if len>0 then
  279. begin
  280. for i:=0 to len-1 do
  281. begin
  282. if tai_string(hp1).str[j]<>value_str[i] then
  283. begin
  284. same_string:=false;
  285. break;
  286. end;
  287. inc(j);
  288. end;
  289. end;
  290. end;
  291. end;
  292. st_ansistring,
  293. st_widestring :
  294. begin
  295. { before the string the following sequence must be found:
  296. <label>
  297. constsymbol <datalabel>
  298. constint -1
  299. constint <len>
  300. we must then return <label> to reuse
  301. }
  302. hp2:=tai(lastlabelhp.previous);
  303. if assigned(hp2) and
  304. (hp2.typ=ait_const_aint) and
  305. (tai_const(hp2).value=-1) and
  306. assigned(hp2.previous) and
  307. (tai(hp2.previous).typ=ait_const_aint) and
  308. (tai_const(hp2.previous).value=len) and
  309. assigned(hp2.previous.previous) and
  310. (tai(hp2.previous.previous).typ=ait_const_ptr) and
  311. assigned(hp2.previous.previous.previous) and
  312. (tai(hp2.previous.previous.previous).typ=ait_label) then
  313. begin
  314. lastlabel:=tai_label(hp2.previous.previous.previous).l;
  315. same_string:=true;
  316. j:=0;
  317. if len>0 then
  318. begin
  319. for i:=0 to len-1 do
  320. begin
  321. if tai_string(hp1).str[j]<>value_str[i] then
  322. begin
  323. same_string:=false;
  324. break;
  325. end;
  326. inc(j);
  327. end;
  328. end;
  329. end;
  330. end;
  331. end;
  332. { found ? }
  333. if same_string then
  334. begin
  335. lab_str:=lastlabel;
  336. break;
  337. end;
  338. end;
  339. lastlabel:=nil;
  340. end;
  341. hp1:=tai(hp1.next);
  342. end;
  343. end;
  344. { :-(, we must generate a new entry }
  345. if not assigned(lab_str) then
  346. begin
  347. objectlibrary.getdatalabel(lastlabel);
  348. lab_str:=lastlabel;
  349. maybe_new_object_file(asmlist[al_typedconsts]);
  350. new_section(asmlist[al_typedconsts],sec_rodata,lastlabel.name,const_align(sizeof(aint)));
  351. asmlist[al_typedconsts].concat(Tai_label.Create(lastlabel));
  352. { generate an ansi string ? }
  353. case st_type of
  354. st_ansistring:
  355. begin
  356. { an empty ansi string is nil! }
  357. if len=0 then
  358. asmlist[al_typedconsts].concat(Tai_const.Create_sym(nil))
  359. else
  360. begin
  361. objectlibrary.getdatalabel(l1);
  362. objectlibrary.getdatalabel(l2);
  363. asmlist[al_typedconsts].concat(Tai_label.Create(l2));
  364. asmlist[al_typedconsts].concat(Tai_const.Create_sym(l1));
  365. asmlist[al_typedconsts].concat(Tai_const.Create_aint(-1));
  366. asmlist[al_typedconsts].concat(Tai_const.Create_aint(len));
  367. asmlist[al_typedconsts].concat(Tai_label.Create(l1));
  368. { include also terminating zero }
  369. getmem(pc,len+1);
  370. move(value_str^,pc^,len);
  371. pc[len]:=#0;
  372. asmlist[al_typedconsts].concat(Tai_string.Create_pchar(pc,len+1));
  373. { return the offset of the real string }
  374. lab_str:=l2;
  375. end;
  376. end;
  377. st_widestring:
  378. begin
  379. { an empty wide string is nil! }
  380. if len=0 then
  381. asmlist[al_typedconsts].concat(Tai_const.Create_sym(nil))
  382. else
  383. begin
  384. objectlibrary.getdatalabel(l1);
  385. objectlibrary.getdatalabel(l2);
  386. asmlist[al_typedconsts].concat(Tai_label.Create(l2));
  387. asmlist[al_typedconsts].concat(Tai_const.Create_sym(l1));
  388. { we use always UTF-16 coding for constants }
  389. { at least for now }
  390. { Consts.concat(Tai_const.Create_8bit(2)); }
  391. asmlist[al_typedconsts].concat(Tai_const.Create_aint(-1));
  392. asmlist[al_typedconsts].concat(Tai_const.Create_aint(len*cwidechartype.def.size));
  393. asmlist[al_typedconsts].concat(Tai_label.Create(l1));
  394. for i:=0 to len-1 do
  395. asmlist[al_typedconsts].concat(Tai_const.Create_16bit(pcompilerwidestring(value_str)^.data[i]));
  396. { terminating zero }
  397. asmlist[al_typedconsts].concat(Tai_const.Create_16bit(0));
  398. { return the offset of the real string }
  399. lab_str:=l2;
  400. end;
  401. end;
  402. st_shortstring:
  403. begin
  404. { truncate strings larger than 255 chars }
  405. if len>255 then
  406. l:=255
  407. else
  408. l:=len;
  409. { include length and terminating zero for quick conversion to pchar }
  410. getmem(pc,l+2);
  411. move(value_str^,pc[1],l);
  412. pc[0]:=chr(l);
  413. pc[l+1]:=#0;
  414. asmlist[al_typedconsts].concat(Tai_string.Create_pchar(pc,l+2));
  415. end;
  416. st_conststring:
  417. begin
  418. { include terminating zero }
  419. getmem(pc,len+1);
  420. move(value_str^,pc[0],len);
  421. pc[len]:=#0;
  422. asmlist[al_typedconsts].concat(Tai_string.Create_pchar(pc,len+1));
  423. end;
  424. end;
  425. end;
  426. end;
  427. location.reference.symbol:=lab_str;
  428. end;
  429. {*****************************************************************************
  430. TCGSETCONSTNODE
  431. *****************************************************************************}
  432. procedure tcgsetconstnode.pass_2;
  433. var
  434. hp1 : tai;
  435. lastlabel : tasmlabel;
  436. i : longint;
  437. neededtyp : taitype;
  438. indexadjust : longint;
  439. type
  440. setbytes=array[0..31] of byte;
  441. Psetbytes=^setbytes;
  442. begin
  443. { xor indexadjust with indexes in a set typecasted to an array of }
  444. { bytes to get the correct locations, also when endianess of source }
  445. { and destiantion differs (JM) }
  446. if (source_info.endian = target_info.endian) then
  447. indexadjust := 0
  448. else
  449. indexadjust := 3;
  450. { small sets are loaded as constants }
  451. if tsetdef(resulttype.def).settype=smallset then
  452. begin
  453. location_reset(location,LOC_CONSTANT,OS_32);
  454. location.value:=pLongint(value_set)^;
  455. exit;
  456. end;
  457. location_reset(location,LOC_CREFERENCE,OS_NO);
  458. neededtyp:=ait_const_8bit;
  459. lastlabel:=nil;
  460. { const already used ? }
  461. if not assigned(lab_set) then
  462. begin
  463. { tries to found an old entry }
  464. hp1:=tai(asmlist[al_typedconsts].first);
  465. while assigned(hp1) do
  466. begin
  467. if hp1.typ=ait_label then
  468. lastlabel:=tai_label(hp1).l
  469. else
  470. begin
  471. if (lastlabel<>nil) and (hp1.typ=neededtyp) then
  472. begin
  473. if (hp1.typ=ait_const_8bit) then
  474. begin
  475. { compare normal set }
  476. i:=0;
  477. while assigned(hp1) and (i<32) do
  478. begin
  479. if tai_const(hp1).value<>Psetbytes(value_set)^[i xor indexadjust] then
  480. break;
  481. inc(i);
  482. hp1:=tai(hp1.next);
  483. end;
  484. if i=32 then
  485. begin
  486. { found! }
  487. lab_set:=lastlabel;
  488. break;
  489. end;
  490. { leave when the end of consts is reached, so no
  491. hp1.next is done }
  492. if not assigned(hp1) then
  493. break;
  494. end
  495. else
  496. begin
  497. { compare small set }
  498. if paint(value_set)^=tai_const(hp1).value then
  499. begin
  500. { found! }
  501. lab_set:=lastlabel;
  502. break;
  503. end;
  504. end;
  505. end;
  506. lastlabel:=nil;
  507. end;
  508. hp1:=tai(hp1.next);
  509. end;
  510. { :-(, we must generate a new entry }
  511. if not assigned(lab_set) then
  512. begin
  513. objectlibrary.getdatalabel(lastlabel);
  514. lab_set:=lastlabel;
  515. maybe_new_object_file(asmlist[al_typedconsts]);
  516. new_section(asmlist[al_typedconsts],sec_rodata,lastlabel.name,const_align(sizeof(aint)));
  517. asmlist[al_typedconsts].concat(Tai_label.Create(lastlabel));
  518. { already handled at the start of this method?? (JM)
  519. if tsetdef(resulttype.def).settype=smallset then
  520. begin
  521. move(value_set^,i,sizeof(longint));
  522. Consts.concat(Tai_const.Create_32bit(i));
  523. end
  524. else
  525. }
  526. begin
  527. for i:=0 to 31 do
  528. asmlist[al_typedconsts].concat(Tai_const.Create_8bit(Psetbytes(value_set)^[i xor indexadjust]));
  529. end;
  530. end;
  531. end;
  532. location.reference.symbol:=lab_set;
  533. end;
  534. {*****************************************************************************
  535. TCGNILNODE
  536. *****************************************************************************}
  537. procedure tcgnilnode.pass_2;
  538. begin
  539. location_reset(location,LOC_CONSTANT,OS_ADDR);
  540. location.value:=0;
  541. end;
  542. {*****************************************************************************
  543. TCGPOINTERCONSTNODE
  544. *****************************************************************************}
  545. procedure tcgguidconstnode.pass_2;
  546. var
  547. tmplabel : TAsmLabel;
  548. i : integer;
  549. begin
  550. location_reset(location,LOC_CREFERENCE,OS_NO);
  551. { label for GUID }
  552. objectlibrary.getdatalabel(tmplabel);
  553. asmlist[al_typedconsts].concat(tai_align.create(const_align(16)));
  554. asmlist[al_typedconsts].concat(Tai_label.Create(tmplabel));
  555. asmlist[al_typedconsts].concat(Tai_const.Create_32bit(longint(value.D1)));
  556. asmlist[al_typedconsts].concat(Tai_const.Create_16bit(value.D2));
  557. asmlist[al_typedconsts].concat(Tai_const.Create_16bit(value.D3));
  558. for i:=low(value.D4) to high(value.D4) do
  559. asmlist[al_typedconsts].concat(Tai_const.Create_8bit(value.D4[i]));
  560. location.reference.symbol:=tmplabel;
  561. end;
  562. begin
  563. crealconstnode:=tcgrealconstnode;
  564. cordconstnode:=tcgordconstnode;
  565. cpointerconstnode:=tcgpointerconstnode;
  566. cstringconstnode:=tcgstringconstnode;
  567. csetconstnode:=tcgsetconstnode;
  568. cnilnode:=tcgnilnode;
  569. cguidconstnode:=tcgguidconstnode;
  570. end.