ncgcon.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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, 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. const
  225. PoolMap: array[tconststringtype] of TConstPoolType = (
  226. sp_conststr,
  227. sp_shortstr,
  228. sp_longstr,
  229. sp_ansistr,
  230. sp_widestr,
  231. sp_unicodestr
  232. );
  233. begin
  234. { for empty ansistrings we could return a constant 0 }
  235. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) and (len=0) then
  236. begin
  237. location_reset(location,LOC_CONSTANT,OS_ADDR);
  238. location.value:=0;
  239. exit;
  240. end;
  241. winlikewidestring:=(cst_type=cst_widestring) and (tf_winlikewidestring in target_info.flags);
  242. { const already used ? }
  243. if not assigned(lab_str) then
  244. begin
  245. pool := current_asmdata.ConstPools[PoolMap[cst_type]];
  246. if cst_type in [cst_widestring, cst_unicodestring] then
  247. entry := pool.FindOrAdd(pcompilerwidestring(value_str)^.data,len*cwidechartype.size)
  248. else
  249. if cst_type = cst_ansistring then
  250. entry := PHashSetItem(TTagHashSet(pool).FindOrAdd(value_str,len,tstringdef(resultdef).encoding))
  251. else
  252. entry := pool.FindOrAdd(value_str,len);
  253. lab_str := TAsmLabel(entry^.Data); // is it needed anymore?
  254. { :-(, we must generate a new entry }
  255. if not assigned(entry^.Data) then
  256. begin
  257. case cst_type of
  258. cst_ansistring:
  259. begin
  260. if len=0 then
  261. InternalError(2008032301) { empty string should be handled above }
  262. else
  263. begin
  264. lastlabel:=emit_ansistring_const(current_asmdata.AsmLists[al_typedconsts],value_str,len,tstringdef(resultdef).encoding);
  265. { because we hardcode the offset below due to it
  266. not being stored in the hashset, check here }
  267. if lastlabel.ofs<>get_string_symofs(st_ansistring,false) then
  268. internalerror(2012051703);
  269. end;
  270. end;
  271. cst_unicodestring,
  272. cst_widestring:
  273. begin
  274. if len=0 then
  275. InternalError(2008032302) { empty string should be handled above }
  276. else
  277. begin
  278. lastlabel := emit_unicodestring_const(current_asmdata.AsmLists[al_typedconsts],
  279. value_str,
  280. tstringdef(resultdef).encoding,
  281. winlikewidestring);
  282. { because we hardcode the offset below due to it
  283. not being stored in the hashset, check here }
  284. if lastlabel.ofs<>get_string_symofs(tstringdef(resultdef).stringtype,winlikewidestring) then
  285. internalerror(2012051704);
  286. end;
  287. end;
  288. cst_shortstring:
  289. begin
  290. current_asmdata.getdatalabel(lastlabel.lab);
  291. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  292. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,lastlabel.lab.name,const_align(sizeof(pint)));
  293. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lastlabel.lab));
  294. { truncate strings larger than 255 chars }
  295. if len>255 then
  296. l:=255
  297. else
  298. l:=len;
  299. { include length and terminating zero for quick conversion to pchar }
  300. getmem(pc,l+2);
  301. move(value_str^,pc[1],l);
  302. pc[0]:=chr(l);
  303. pc[l+1]:=#0;
  304. current_asmdata.asmlists[al_typedconsts].concat(Tai_string.Create_pchar(pc,l+2));
  305. end;
  306. cst_conststring:
  307. begin
  308. current_asmdata.getdatalabel(lastlabel.lab);
  309. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  310. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,lastlabel.lab.name,const_align(sizeof(pint)));
  311. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lastlabel.lab));
  312. { include terminating zero }
  313. getmem(pc,len+1);
  314. move(value_str^,pc[0],len);
  315. pc[len]:=#0;
  316. current_asmdata.asmlists[al_typedconsts].concat(Tai_string.Create_pchar(pc,len+1));
  317. end;
  318. end;
  319. lab_str:=lastlabel.lab;
  320. entry^.Data:=lastlabel.lab;
  321. end;
  322. end;
  323. if cst_type in [cst_ansistring, cst_widestring, cst_unicodestring] then
  324. begin
  325. location_reset(location, LOC_REGISTER, OS_ADDR);
  326. reference_reset_symbol(href, lab_str,
  327. get_string_symofs(tstringdef(resultdef).stringtype,winlikewidestring),
  328. const_align(sizeof(pint)));
  329. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  330. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,location.register);
  331. end
  332. else
  333. begin
  334. location_reset_ref(location, LOC_CREFERENCE, def_cgsize(resultdef), const_align(sizeof(pint)));
  335. location.reference.symbol:=lab_str;
  336. end;
  337. end;
  338. {*****************************************************************************
  339. TCGSETCONSTNODE
  340. *****************************************************************************}
  341. function tcgsetconstnode.emitvarsetconst: tasmsymbol;
  342. type
  343. setbytes=array[0..31] of byte;
  344. Psetbytes=^setbytes;
  345. var
  346. lab: tasmlabel;
  347. i: longint;
  348. begin
  349. current_asmdata.getdatalabel(lab);
  350. result:=lab;
  351. lab_set:=lab;
  352. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  353. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,result.name,const_align(8));
  354. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lab));
  355. if (source_info.endian=target_info.endian) then
  356. for i:=0 to 31 do
  357. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(Psetbytes(value_set)^[i]))
  358. else
  359. for i:=0 to 31 do
  360. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(reverse_byte(Psetbytes(value_set)^[i])));
  361. end;
  362. procedure tcgsetconstnode.handlevarsetconst;
  363. var
  364. entry : PHashSetItem;
  365. begin
  366. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(8));
  367. { const already used ? }
  368. if not assigned(lab_set) then
  369. begin
  370. entry := current_asmdata.ConstPools[sp_varsets].FindOrAdd(value_set, 32);
  371. { :-(, we must generate a new entry }
  372. if not assigned(entry^.Data) then
  373. entry^.Data:=emitvarsetconst;
  374. lab_set := TAsmSymbol(entry^.Data);
  375. end;
  376. location.reference.symbol:=lab_set;
  377. end;
  378. procedure tcgsetconstnode.pass_generate_code;
  379. type
  380. setbytes=array[0..31] of byte;
  381. Psetbytes=^setbytes;
  382. procedure smallsetconst;
  383. begin
  384. location_reset(location,LOC_CONSTANT,int_cgsize(resultdef.size));
  385. if (source_info.endian=target_info.endian) then
  386. begin
  387. { not plongint, because that will "sign extend" the set on 64 bit platforms }
  388. { if changed to "paword", please also modify "32-resultdef.size*8" and }
  389. { cross-endian code below }
  390. { Extra aint type cast to avoid range errors }
  391. location.value:=aint(pCardinal(value_set)^)
  392. end
  393. else
  394. begin
  395. location.value:=swapendian(Pcardinal(value_set)^);
  396. location.value:=aint(
  397. reverse_byte (location.value and $ff) or
  398. (reverse_byte((location.value shr 8) and $ff) shl 8) or
  399. (reverse_byte((location.value shr 16) and $ff) shl 16) or
  400. (reverse_byte((location.value shr 24) and $ff) shl 24)
  401. );
  402. end;
  403. if (target_info.endian=endian_big) then
  404. location.value:=location.value shr (32-resultdef.size*8);
  405. end;
  406. procedure varsetconst;
  407. var
  408. lastlabel : tasmlabel;
  409. i : longint;
  410. entry : PHashSetItem;
  411. begin
  412. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(8));
  413. lastlabel:=nil;
  414. { const already used ? }
  415. if not assigned(lab_set) then
  416. begin
  417. entry := current_asmdata.ConstPools[sp_varsets].FindOrAdd(value_set, 32);
  418. lab_set := TAsmLabel(entry^.Data); // is it needed anymore?
  419. { :-(, we must generate a new entry }
  420. if not assigned(entry^.Data) then
  421. begin
  422. current_asmdata.getdatalabel(lastlabel);
  423. lab_set:=lastlabel;
  424. entry^.Data:=lastlabel;
  425. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  426. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,lastlabel.name,const_align(8));
  427. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lastlabel));
  428. if (source_info.endian=target_info.endian) then
  429. for i:=0 to 31 do
  430. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(Psetbytes(value_set)^[i]))
  431. else
  432. for i:=0 to 31 do
  433. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(reverse_byte(Psetbytes(value_set)^[i])));
  434. end;
  435. end;
  436. location.reference.symbol:=lab_set;
  437. end;
  438. begin
  439. adjustforsetbase;
  440. { small sets are loaded as constants }
  441. if is_smallset(resultdef) then
  442. smallsetconst
  443. else
  444. handlevarsetconst;
  445. end;
  446. {*****************************************************************************
  447. TCGNILNODE
  448. *****************************************************************************}
  449. procedure tcgnilnode.pass_generate_code;
  450. begin
  451. location_reset(location,LOC_CONSTANT,OS_ADDR);
  452. location.value:=0;
  453. end;
  454. {*****************************************************************************
  455. TCGGUIDCONSTNODE
  456. *****************************************************************************}
  457. procedure tcgguidconstnode.pass_generate_code;
  458. var
  459. tmplabel : TAsmLabel;
  460. i : integer;
  461. begin
  462. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(16));
  463. { label for GUID }
  464. current_asmdata.getdatalabel(tmplabel);
  465. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  466. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,tmplabel.name,const_align(16));
  467. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(tmplabel));
  468. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(longint(value.D1)));
  469. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(value.D2));
  470. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(value.D3));
  471. for i:=low(value.D4) to high(value.D4) do
  472. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(value.D4[i]));
  473. location.reference.symbol:=tmplabel;
  474. end;
  475. begin
  476. cdataconstnode:=tcgdataconstnode;
  477. crealconstnode:=tcgrealconstnode;
  478. cordconstnode:=tcgordconstnode;
  479. cpointerconstnode:=tcgpointerconstnode;
  480. cstringconstnode:=tcgstringconstnode;
  481. csetconstnode:=tcgsetconstnode;
  482. cnilnode:=tcgnilnode;
  483. cguidconstnode:=tcgguidconstnode;
  484. end.