ncgcon.pas 22 KB

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