ncgcon.pas 24 KB

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