ncgcon.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. aasmbase,
  23. node,ncon;
  24. type
  25. tcgdataconstnode = class(tdataconstnode)
  26. procedure pass_generate_code;override;
  27. end;
  28. tcgrealconstnode = class(trealconstnode)
  29. procedure pass_generate_code;override;
  30. end;
  31. tcgordconstnode = class(tordconstnode)
  32. procedure pass_generate_code;override;
  33. end;
  34. tcgpointerconstnode = class(tpointerconstnode)
  35. procedure pass_generate_code;override;
  36. end;
  37. tcgstringconstnode = class(tstringconstnode)
  38. procedure pass_generate_code;override;
  39. end;
  40. tcgsetconstnode = class(tsetconstnode)
  41. protected
  42. function emitvarsetconst: tasmsymbol; virtual;
  43. procedure handlevarsetconst;
  44. public
  45. procedure pass_generate_code;override;
  46. end;
  47. tcgnilnode = class(tnilnode)
  48. procedure pass_generate_code;override;
  49. end;
  50. tcgguidconstnode = class(tguidconstnode)
  51. procedure pass_generate_code;override;
  52. end;
  53. implementation
  54. uses
  55. globtype,widestr,systems,
  56. verbose,globals,cutils,
  57. symconst,symdef,aasmtai,aasmdata,aasmcpu,defutil,
  58. cpuinfo,cpubase,
  59. cgbase,cgobj,cgutils,
  60. ncgutil,hlcgobj,symtype,cclasses,asmutils,tgobj
  61. ;
  62. {*****************************************************************************
  63. TCGDATACONSTNODE
  64. *****************************************************************************}
  65. procedure tcgdataconstnode.pass_generate_code;
  66. var
  67. l : tasmlabel;
  68. i : longint;
  69. b : byte;
  70. begin
  71. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(maxalign));
  72. current_asmdata.getdatalabel(l);
  73. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  74. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l.name,const_align(maxalign));
  75. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l));
  76. data.seek(0);
  77. for i:=0 to data.size-1 do
  78. begin
  79. data.read(b,1);
  80. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(b));
  81. end;
  82. location.reference.symbol:=l;
  83. end;
  84. {*****************************************************************************
  85. TCGREALCONSTNODE
  86. *****************************************************************************}
  87. procedure tcgrealconstnode.pass_generate_code;
  88. { I suppose the parser/pass_1 must make sure the generated real }
  89. { constants are actually supported by the target processor? (JM) }
  90. const
  91. floattype2ait:array[tfloattype] of taitype=
  92. (ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_real_80bit,ait_comp_64bit,ait_comp_64bit,ait_real_128bit);
  93. { Since the value is stored always as bestreal, we share a single pool
  94. between all float types. This requires type and hiloswapped flag to
  95. be matched along with the value }
  96. type
  97. tfloatkey = record
  98. value: bestreal;
  99. typ: tfloattype;
  100. swapped: boolean;
  101. end;
  102. var
  103. lastlabel : tasmlabel;
  104. realait : taitype;
  105. entry : PHashSetItem;
  106. key: tfloatkey;
  107. {$ifdef ARM}
  108. hiloswapped : boolean;
  109. {$endif ARM}
  110. begin
  111. location_reset_ref(location,LOC_CREFERENCE,def_cgsize(resultdef),const_align(resultdef.alignment));
  112. lastlabel:=nil;
  113. realait:=floattype2ait[tfloatdef(resultdef).floattype];
  114. {$ifdef ARM}
  115. hiloswapped:=is_double_hilo_swapped;
  116. {$endif ARM}
  117. { const already used ? }
  118. if not assigned(lab_real) then
  119. begin
  120. { there may be gap between record fields, zero it out }
  121. fillchar(key,sizeof(key),0);
  122. key.value:=value_real;
  123. key.typ:=tfloatdef(resultdef).floattype;
  124. {$ifdef ARM}
  125. key.swapped:=hiloswapped;
  126. {$endif ARM}
  127. entry := current_asmdata.ConstPools[sp_floats].FindOrAdd(@key, sizeof(key));
  128. lab_real := TAsmLabel(entry^.Data); // is it needed anymore?
  129. { :-(, we must generate a new entry }
  130. if not(assigned(lab_real)) then
  131. begin
  132. current_asmdata.getdatalabel(lastlabel);
  133. entry^.Data:=lastlabel;
  134. lab_real:=lastlabel;
  135. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  136. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,lastlabel.name,const_align(resultdef.alignment));
  137. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lastlabel));
  138. case realait of
  139. ait_real_32bit :
  140. begin
  141. current_asmdata.asmlists[al_typedconsts].concat(Tai_real_32bit.Create(ts32real(value_real)));
  142. { range checking? }
  143. if floating_point_range_check_error and
  144. (tai_real_32bit(current_asmdata.asmlists[al_typedconsts].last).value=MathInf.Value) then
  145. Message(parser_e_range_check_error);
  146. end;
  147. ait_real_64bit :
  148. begin
  149. {$ifdef ARM}
  150. if hiloswapped then
  151. current_asmdata.asmlists[al_typedconsts].concat(Tai_real_64bit.Create_hiloswapped(ts64real(value_real)))
  152. else
  153. {$endif ARM}
  154. current_asmdata.asmlists[al_typedconsts].concat(Tai_real_64bit.Create(ts64real(value_real)));
  155. { range checking? }
  156. if floating_point_range_check_error and
  157. (tai_real_64bit(current_asmdata.asmlists[al_typedconsts].last).value=MathInf.Value) then
  158. Message(parser_e_range_check_error);
  159. end;
  160. ait_real_80bit :
  161. begin
  162. current_asmdata.asmlists[al_typedconsts].concat(Tai_real_80bit.Create(value_real,resultdef.size));
  163. { range checking? }
  164. if floating_point_range_check_error and
  165. (tai_real_80bit(current_asmdata.asmlists[al_typedconsts].last).value=MathInf.Value) then
  166. Message(parser_e_range_check_error);
  167. end;
  168. {$ifdef cpufloat128}
  169. ait_real_128bit :
  170. begin
  171. current_asmdata.asmlists[al_typedconsts].concat(Tai_real_128bit.Create(value_real));
  172. { range checking? }
  173. if floating_point_range_check_error and
  174. (tai_real_128bit(current_asmdata.asmlists[al_typedconsts].last).value=MathInf.Value) then
  175. Message(parser_e_range_check_error);
  176. end;
  177. {$endif cpufloat128}
  178. { the round is necessary for native compilers where comp isn't a float }
  179. ait_comp_64bit :
  180. if (value_real>9223372036854775807.0) or (value_real<-9223372036854775808.0) then
  181. message(parser_e_range_check_error)
  182. else
  183. current_asmdata.asmlists[al_typedconsts].concat(Tai_comp_64bit.Create(round(value_real)));
  184. else
  185. internalerror(10120);
  186. end;
  187. end;
  188. end;
  189. location.reference.symbol:=lab_real;
  190. end;
  191. {*****************************************************************************
  192. TCGORDCONSTNODE
  193. *****************************************************************************}
  194. procedure tcgordconstnode.pass_generate_code;
  195. begin
  196. location_reset(location,LOC_CONSTANT,def_cgsize(resultdef));
  197. {$ifdef cpu64bitalu}
  198. location.value:=value.svalue;
  199. {$else cpu64bitalu}
  200. location.value64:=value.svalue;
  201. {$endif cpu64bitalu}
  202. end;
  203. {*****************************************************************************
  204. TCGPOINTERCONSTNODE
  205. *****************************************************************************}
  206. procedure tcgpointerconstnode.pass_generate_code;
  207. begin
  208. { an integer const. behaves as a memory reference }
  209. location_reset(location,LOC_CONSTANT,OS_ADDR);
  210. location.value:=aint(value);
  211. end;
  212. {*****************************************************************************
  213. TCGSTRINGCONSTNODE
  214. *****************************************************************************}
  215. procedure tcgstringconstnode.pass_generate_code;
  216. var
  217. lastlabel: tasmlabofs;
  218. pc: pchar;
  219. l: longint;
  220. href: treference;
  221. pool: THashSet;
  222. entry: PHashSetItem;
  223. winlikewidestring: boolean;
  224. elementdef: tdef;
  225. strpointerdef: tdef;
  226. const
  227. PoolMap: array[tconststringtype] of TConstPoolType = (
  228. sp_conststr,
  229. sp_shortstr,
  230. sp_longstr,
  231. sp_ansistr,
  232. sp_widestr,
  233. sp_unicodestr
  234. );
  235. begin
  236. case cst_type of
  237. cst_shortstring,
  238. cst_conststring,
  239. cst_ansistring:
  240. begin
  241. elementdef:=cansichartype;
  242. strpointerdef:=charpointertype;
  243. end;
  244. cst_widestring,
  245. cst_unicodestring:
  246. begin
  247. elementdef:=cwidechartype;
  248. strpointerdef:=widecharpointertype;
  249. end;
  250. else
  251. internalerror(2014032803);
  252. end;
  253. { for empty ansistrings we could return a constant 0 }
  254. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) and (len=0) then
  255. begin
  256. location_reset(location,LOC_CONSTANT,def_cgsize(strpointerdef));
  257. location.value:=0;
  258. exit;
  259. end;
  260. winlikewidestring:=(cst_type=cst_widestring) and (tf_winlikewidestring in target_info.flags);
  261. { const already used ? }
  262. if not assigned(lab_str) then
  263. begin
  264. pool := current_asmdata.ConstPools[PoolMap[cst_type]];
  265. if cst_type in [cst_widestring, cst_unicodestring] then
  266. entry := pool.FindOrAdd(pcompilerwidestring(value_str)^.data,len*cwidechartype.size)
  267. else
  268. if cst_type = cst_ansistring then
  269. entry := PHashSetItem(TTagHashSet(pool).FindOrAdd(value_str,len,tstringdef(resultdef).encoding))
  270. else
  271. entry := pool.FindOrAdd(value_str,len);
  272. lab_str := TAsmLabel(entry^.Data); // is it needed anymore?
  273. { :-(, we must generate a new entry }
  274. if not assigned(entry^.Data) then
  275. begin
  276. case cst_type of
  277. cst_ansistring:
  278. begin
  279. if len=0 then
  280. InternalError(2008032301) { empty string should be handled above }
  281. else
  282. begin
  283. lastlabel:=emit_ansistring_const(current_asmdata.AsmLists[al_typedconsts],value_str,len,tstringdef(resultdef).encoding);
  284. { because we hardcode the offset below due to it
  285. not being stored in the hashset, check here }
  286. if lastlabel.ofs<>get_string_symofs(st_ansistring,false) then
  287. internalerror(2012051703);
  288. end;
  289. end;
  290. cst_unicodestring,
  291. cst_widestring:
  292. begin
  293. if len=0 then
  294. InternalError(2008032302) { empty string should be handled above }
  295. else
  296. begin
  297. lastlabel := emit_unicodestring_const(current_asmdata.AsmLists[al_typedconsts],
  298. value_str,
  299. tstringdef(resultdef).encoding,
  300. winlikewidestring);
  301. { because we hardcode the offset below due to it
  302. not being stored in the hashset, check here }
  303. if lastlabel.ofs<>get_string_symofs(tstringdef(resultdef).stringtype,winlikewidestring) then
  304. internalerror(2012051704);
  305. end;
  306. end;
  307. cst_shortstring:
  308. begin
  309. current_asmdata.getdatalabel(lastlabel.lab);
  310. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  311. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,lastlabel.lab.name,const_align(sizeof(pint)));
  312. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lastlabel.lab));
  313. { truncate strings larger than 255 chars }
  314. if len>255 then
  315. l:=255
  316. else
  317. l:=len;
  318. { include length and terminating zero for quick conversion to pchar }
  319. getmem(pc,l+2);
  320. move(value_str^,pc[1],l);
  321. pc[0]:=chr(l);
  322. pc[l+1]:=#0;
  323. current_asmdata.asmlists[al_typedconsts].concat(Tai_string.Create_pchar(pc,l+2));
  324. end;
  325. cst_conststring:
  326. begin
  327. current_asmdata.getdatalabel(lastlabel.lab);
  328. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  329. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,lastlabel.lab.name,const_align(sizeof(pint)));
  330. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lastlabel.lab));
  331. { include terminating zero }
  332. getmem(pc,len+1);
  333. move(value_str^,pc[0],len);
  334. pc[len]:=#0;
  335. current_asmdata.asmlists[al_typedconsts].concat(Tai_string.Create_pchar(pc,len+1));
  336. end;
  337. else
  338. internalerror(2013120103);
  339. end;
  340. lab_str:=lastlabel.lab;
  341. entry^.Data:=lastlabel.lab;
  342. end;
  343. end;
  344. if cst_type in [cst_ansistring, cst_widestring, cst_unicodestring] then
  345. begin
  346. location_reset(location, LOC_REGISTER, def_cgsize(strpointerdef));
  347. reference_reset_symbol(href, lab_str,
  348. get_string_symofs(tstringdef(resultdef).stringtype,winlikewidestring),
  349. const_align(strpointerdef.size));
  350. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,strpointerdef);
  351. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,elementdef,strpointerdef,href,location.register)
  352. end
  353. else
  354. begin
  355. location_reset_ref(location, LOC_CREFERENCE, def_cgsize(resultdef), const_align(strpointerdef.size));
  356. location.reference.symbol:=lab_str;
  357. end;
  358. end;
  359. {*****************************************************************************
  360. TCGSETCONSTNODE
  361. *****************************************************************************}
  362. function tcgsetconstnode.emitvarsetconst: tasmsymbol;
  363. type
  364. setbytes=array[0..31] of byte;
  365. Psetbytes=^setbytes;
  366. var
  367. lab: tasmlabel;
  368. i: longint;
  369. begin
  370. current_asmdata.getdatalabel(lab);
  371. result:=lab;
  372. lab_set:=lab;
  373. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  374. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,result.name,const_align(8));
  375. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lab));
  376. if (source_info.endian=target_info.endian) then
  377. for i:=0 to 31 do
  378. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(Psetbytes(value_set)^[i]))
  379. else
  380. for i:=0 to 31 do
  381. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(reverse_byte(Psetbytes(value_set)^[i])));
  382. end;
  383. procedure tcgsetconstnode.handlevarsetconst;
  384. var
  385. entry : PHashSetItem;
  386. begin
  387. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(8));
  388. { const already used ? }
  389. if not assigned(lab_set) then
  390. begin
  391. entry := current_asmdata.ConstPools[sp_varsets].FindOrAdd(value_set, 32);
  392. { :-(, we must generate a new entry }
  393. if not assigned(entry^.Data) then
  394. entry^.Data:=emitvarsetconst;
  395. lab_set := TAsmSymbol(entry^.Data);
  396. end;
  397. location.reference.symbol:=lab_set;
  398. end;
  399. procedure tcgsetconstnode.pass_generate_code;
  400. type
  401. setbytes=array[0..31] of byte;
  402. Psetbytes=^setbytes;
  403. procedure smallsetconst;
  404. begin
  405. location_reset(location,LOC_CONSTANT,int_cgsize(resultdef.size));
  406. if (source_info.endian=target_info.endian) then
  407. begin
  408. { not plongint, because that will "sign extend" the set on 64 bit platforms }
  409. { if changed to "paword", please also modify "32-resultdef.size*8" and }
  410. { cross-endian code below }
  411. { Extra aint type cast to avoid range errors }
  412. location.value:=aint(pCardinal(value_set)^)
  413. end
  414. else
  415. begin
  416. location.value:=aint(swapendian(Pcardinal(value_set)^));
  417. location.value:=aint(
  418. reverse_byte (location.value and $ff) or
  419. (reverse_byte((location.value shr 8) and $ff) shl 8) or
  420. (reverse_byte((location.value shr 16) and $ff) shl 16) or
  421. (reverse_byte((location.value shr 24) and $ff) shl 24)
  422. );
  423. end;
  424. if (target_info.endian=endian_big) then
  425. location.value:=location.value shr (32-resultdef.size*8);
  426. end;
  427. procedure varsetconst;
  428. var
  429. lastlabel : tasmlabel;
  430. i : longint;
  431. entry : PHashSetItem;
  432. begin
  433. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(8));
  434. lastlabel:=nil;
  435. { const already used ? }
  436. if not assigned(lab_set) then
  437. begin
  438. entry := current_asmdata.ConstPools[sp_varsets].FindOrAdd(value_set, 32);
  439. lab_set := TAsmLabel(entry^.Data); // is it needed anymore?
  440. { :-(, we must generate a new entry }
  441. if not assigned(entry^.Data) then
  442. begin
  443. current_asmdata.getdatalabel(lastlabel);
  444. lab_set:=lastlabel;
  445. entry^.Data:=lastlabel;
  446. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  447. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,lastlabel.name,const_align(8));
  448. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lastlabel));
  449. if (source_info.endian=target_info.endian) then
  450. for i:=0 to 31 do
  451. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(Psetbytes(value_set)^[i]))
  452. else
  453. for i:=0 to 31 do
  454. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(reverse_byte(Psetbytes(value_set)^[i])));
  455. end;
  456. end;
  457. location.reference.symbol:=lab_set;
  458. end;
  459. begin
  460. adjustforsetbase;
  461. { small sets are loaded as constants }
  462. if is_smallset(resultdef) then
  463. smallsetconst
  464. else
  465. handlevarsetconst;
  466. end;
  467. {*****************************************************************************
  468. TCGNILNODE
  469. *****************************************************************************}
  470. procedure tcgnilnode.pass_generate_code;
  471. begin
  472. location_reset(location,LOC_CONSTANT,OS_ADDR);
  473. location.value:=0;
  474. end;
  475. {*****************************************************************************
  476. TCGGUIDCONSTNODE
  477. *****************************************************************************}
  478. procedure tcgguidconstnode.pass_generate_code;
  479. var
  480. lastlabel : tasmlabel;
  481. i : longint;
  482. entry : PHashSetItem;
  483. begin
  484. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(16));
  485. lastlabel:=nil;
  486. { const already used ? }
  487. if not assigned(lab_set) then
  488. begin
  489. entry := current_asmdata.ConstPools[sp_guids].FindOrAdd(@value,sizeof(value));
  490. lab_set := TAsmLabel(entry^.Data); // is it needed anymore?
  491. { :-(, we must generate a new entry }
  492. if not assigned(entry^.Data) then
  493. begin
  494. current_asmdata.getdatalabel(lastlabel);
  495. lab_set:=lastlabel;
  496. entry^.Data:=lastlabel;
  497. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  498. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,lastlabel.name,const_align(16));
  499. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lastlabel));
  500. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(longint(value.D1)));
  501. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(value.D2));
  502. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(value.D3));
  503. for i:=low(value.D4) to high(value.D4) do
  504. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(value.D4[i]));
  505. end;
  506. end;
  507. location.reference.symbol:=lab_set;
  508. end;
  509. begin
  510. cdataconstnode:=tcgdataconstnode;
  511. crealconstnode:=tcgrealconstnode;
  512. cordconstnode:=tcgordconstnode;
  513. cpointerconstnode:=tcgpointerconstnode;
  514. cstringconstnode:=tcgstringconstnode;
  515. csetconstnode:=tcgsetconstnode;
  516. cnilnode:=tcgnilnode;
  517. cguidconstnode:=tcgguidconstnode;
  518. end.