ncgcon.pas 27 KB

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