ncgcon.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. if current_asmdata.ConstPools[sp_floats] = nil then
  121. current_asmdata.ConstPools[sp_floats] := THashSet.Create(64, True, False);
  122. { there may be gap between record fields, zero it out }
  123. fillchar(key,sizeof(key),0);
  124. key.value:=value_real;
  125. key.typ:=tfloatdef(resultdef).floattype;
  126. {$ifdef ARM}
  127. key.swapped:=hiloswapped;
  128. {$endif ARM}
  129. entry := current_asmdata.ConstPools[sp_floats].FindOrAdd(@key, sizeof(key));
  130. lab_real := TAsmLabel(entry^.Data); // is it needed anymore?
  131. { :-(, we must generate a new entry }
  132. if not assigned(lab_real) then
  133. begin
  134. current_asmdata.getdatalabel(lastlabel);
  135. entry^.Data:=lastlabel;
  136. lab_real:=lastlabel;
  137. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  138. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,lastlabel.name,const_align(resultdef.alignment));
  139. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lastlabel));
  140. case realait of
  141. ait_real_32bit :
  142. begin
  143. current_asmdata.asmlists[al_typedconsts].concat(Tai_real_32bit.Create(ts32real(value_real)));
  144. { range checking? }
  145. if floating_point_range_check_error and
  146. (tai_real_32bit(current_asmdata.asmlists[al_typedconsts].last).value=MathInf.Value) then
  147. Message(parser_e_range_check_error);
  148. end;
  149. ait_real_64bit :
  150. begin
  151. {$ifdef ARM}
  152. if hiloswapped then
  153. current_asmdata.asmlists[al_typedconsts].concat(Tai_real_64bit.Create_hiloswapped(ts64real(value_real)))
  154. else
  155. {$endif ARM}
  156. current_asmdata.asmlists[al_typedconsts].concat(Tai_real_64bit.Create(ts64real(value_real)));
  157. { range checking? }
  158. if floating_point_range_check_error and
  159. (tai_real_64bit(current_asmdata.asmlists[al_typedconsts].last).value=MathInf.Value) then
  160. Message(parser_e_range_check_error);
  161. end;
  162. ait_real_80bit :
  163. begin
  164. current_asmdata.asmlists[al_typedconsts].concat(Tai_real_80bit.Create(value_real,resultdef.size));
  165. { range checking? }
  166. if floating_point_range_check_error and
  167. (tai_real_80bit(current_asmdata.asmlists[al_typedconsts].last).value=MathInf.Value) then
  168. Message(parser_e_range_check_error);
  169. end;
  170. {$ifdef cpufloat128}
  171. ait_real_128bit :
  172. begin
  173. current_asmdata.asmlists[al_typedconsts].concat(Tai_real_128bit.Create(value_real));
  174. { range checking? }
  175. if floating_point_range_check_error and
  176. (tai_real_128bit(current_asmdata.asmlists[al_typedconsts].last).value=MathInf.Value) then
  177. Message(parser_e_range_check_error);
  178. end;
  179. {$endif cpufloat128}
  180. { the round is necessary for native compilers where comp isn't a float }
  181. ait_comp_64bit :
  182. if (value_real>9223372036854775807.0) or (value_real<-9223372036854775808.0) then
  183. message(parser_e_range_check_error)
  184. else
  185. current_asmdata.asmlists[al_typedconsts].concat(Tai_comp_64bit.Create(round(value_real)));
  186. else
  187. internalerror(10120);
  188. end;
  189. end;
  190. end;
  191. location.reference.symbol:=lab_real;
  192. end;
  193. {*****************************************************************************
  194. TCGORDCONSTNODE
  195. *****************************************************************************}
  196. procedure tcgordconstnode.pass_generate_code;
  197. begin
  198. location_reset(location,LOC_CONSTANT,def_cgsize(resultdef));
  199. {$ifdef cpu64bitalu}
  200. location.value:=value.svalue;
  201. {$else cpu64bitalu}
  202. location.value64:=value.svalue;
  203. {$endif cpu64bitalu}
  204. end;
  205. {*****************************************************************************
  206. TCGPOINTERCONSTNODE
  207. *****************************************************************************}
  208. procedure tcgpointerconstnode.pass_generate_code;
  209. begin
  210. { an integer const. behaves as a memory reference }
  211. location_reset(location,LOC_CONSTANT,OS_ADDR);
  212. location.value:=aint(value);
  213. end;
  214. {*****************************************************************************
  215. TCGSTRINGCONSTNODE
  216. *****************************************************************************}
  217. procedure tcgstringconstnode.pass_generate_code;
  218. var
  219. lastlabel : tasmlabel;
  220. pc : pchar;
  221. l: longint;
  222. href: treference;
  223. pooltype: TConstPoolType;
  224. pool: THashSet;
  225. entry: PHashSetItem;
  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. { for empty ansistrings we could return a constant 0 }
  237. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) and (len=0) then
  238. begin
  239. location_reset(location,LOC_CONSTANT,OS_ADDR);
  240. location.value:=0;
  241. exit;
  242. end;
  243. { const already used ? }
  244. if not assigned(lab_str) then
  245. begin
  246. pooltype := PoolMap[cst_type];
  247. if current_asmdata.ConstPools[pooltype] = nil then
  248. current_asmdata.ConstPools[pooltype] := THashSet.Create(64, True, False);
  249. pool := current_asmdata.ConstPools[pooltype];
  250. if cst_type in [cst_widestring, cst_unicodestring] then
  251. entry := pool.FindOrAdd(pcompilerwidestring(value_str)^.data, len*cwidechartype.size)
  252. else
  253. entry := pool.FindOrAdd(value_str, len);
  254. lab_str := TAsmLabel(entry^.Data); // is it needed anymore?
  255. { :-(, we must generate a new entry }
  256. if not assigned(entry^.Data) then
  257. begin
  258. case cst_type of
  259. cst_ansistring:
  260. begin
  261. if len=0 then
  262. InternalError(2008032301) { empty string should be handled above }
  263. else
  264. lastlabel:=emit_ansistring_const(current_asmdata.AsmLists[al_typedconsts],value_str,len);
  265. end;
  266. cst_unicodestring,
  267. cst_widestring:
  268. begin
  269. if len=0 then
  270. InternalError(2008032302) { empty string should be handled above }
  271. else
  272. lastlabel := emit_unicodestring_const(current_asmdata.AsmLists[al_typedconsts],
  273. value_str,
  274. (cst_type=cst_widestring) and (tf_winlikewidestring in target_info.flags));
  275. end;
  276. cst_shortstring:
  277. begin
  278. current_asmdata.getdatalabel(lastlabel);
  279. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  280. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,lastlabel.name,const_align(sizeof(pint)));
  281. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lastlabel));
  282. { truncate strings larger than 255 chars }
  283. if len>255 then
  284. l:=255
  285. else
  286. l:=len;
  287. { include length and terminating zero for quick conversion to pchar }
  288. getmem(pc,l+2);
  289. move(value_str^,pc[1],l);
  290. pc[0]:=chr(l);
  291. pc[l+1]:=#0;
  292. current_asmdata.asmlists[al_typedconsts].concat(Tai_string.Create_pchar(pc,l+2));
  293. end;
  294. cst_conststring:
  295. begin
  296. current_asmdata.getdatalabel(lastlabel);
  297. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  298. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,lastlabel.name,const_align(sizeof(pint)));
  299. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lastlabel));
  300. { include terminating zero }
  301. getmem(pc,len+1);
  302. move(value_str^,pc[0],len);
  303. pc[len]:=#0;
  304. current_asmdata.asmlists[al_typedconsts].concat(Tai_string.Create_pchar(pc,len+1));
  305. end;
  306. end;
  307. lab_str:=lastlabel;
  308. entry^.Data:=lastlabel;
  309. end;
  310. end;
  311. if cst_type in [cst_ansistring, cst_widestring, cst_unicodestring] then
  312. begin
  313. location_reset(location, LOC_REGISTER, OS_ADDR);
  314. reference_reset_symbol(href, lab_str, 0, const_align(sizeof(pint)));
  315. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  316. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,location.register);
  317. end
  318. else
  319. begin
  320. location_reset_ref(location, LOC_CREFERENCE, def_cgsize(resultdef), const_align(sizeof(pint)));
  321. location.reference.symbol:=lab_str;
  322. end;
  323. end;
  324. {*****************************************************************************
  325. TCGSETCONSTNODE
  326. *****************************************************************************}
  327. function tcgsetconstnode.emitvarsetconst: tasmsymbol;
  328. type
  329. setbytes=array[0..31] of byte;
  330. Psetbytes=^setbytes;
  331. var
  332. lab: tasmlabel;
  333. i: longint;
  334. begin
  335. current_asmdata.getdatalabel(lab);
  336. result:=lab;
  337. lab_set:=lab;
  338. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  339. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,result.name,const_align(8));
  340. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lab));
  341. if (source_info.endian=target_info.endian) then
  342. for i:=0 to 31 do
  343. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(Psetbytes(value_set)^[i]))
  344. else
  345. for i:=0 to 31 do
  346. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(reverse_byte(Psetbytes(value_set)^[i])));
  347. end;
  348. procedure tcgsetconstnode.handlevarsetconst;
  349. var
  350. i : longint;
  351. entry : PHashSetItem;
  352. begin
  353. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(8));
  354. { const already used ? }
  355. if not assigned(lab_set) then
  356. begin
  357. if current_asmdata.ConstPools[sp_varsets] = nil then
  358. current_asmdata.ConstPools[sp_varsets] := THashSet.Create(64, True, False);
  359. entry := current_asmdata.ConstPools[sp_varsets].FindOrAdd(value_set, 32);
  360. { :-(, we must generate a new entry }
  361. if not assigned(entry^.Data) then
  362. entry^.Data:=emitvarsetconst;
  363. lab_set := TAsmSymbol(entry^.Data);
  364. end;
  365. location.reference.symbol:=lab_set;
  366. end;
  367. procedure tcgsetconstnode.pass_generate_code;
  368. procedure smallsetconst;
  369. begin
  370. location_reset(location,LOC_CONSTANT,int_cgsize(resultdef.size));
  371. if (source_info.endian=target_info.endian) then
  372. begin
  373. { not plongint, because that will "sign extend" the set on 64 bit platforms }
  374. { if changed to "paword", please also modify "32-resultdef.size*8" and }
  375. { cross-endian code below }
  376. { Extra aint type cast to avoid range errors }
  377. location.value:=aint(pCardinal(value_set)^)
  378. end
  379. else
  380. begin
  381. location.value:=swapendian(Pcardinal(value_set)^);
  382. location.value:=aint(
  383. reverse_byte (location.value and $ff) or
  384. (reverse_byte((location.value shr 8) and $ff) shl 8) or
  385. (reverse_byte((location.value shr 16) and $ff) shl 16) or
  386. (reverse_byte((location.value shr 24) and $ff) shl 24)
  387. );
  388. end;
  389. if (target_info.endian=endian_big) then
  390. location.value:=location.value shr (32-resultdef.size*8);
  391. end;
  392. begin
  393. adjustforsetbase;
  394. { small sets are loaded as constants }
  395. if is_smallset(resultdef) then
  396. smallsetconst
  397. else
  398. handlevarsetconst;
  399. end;
  400. {*****************************************************************************
  401. TCGNILNODE
  402. *****************************************************************************}
  403. procedure tcgnilnode.pass_generate_code;
  404. begin
  405. location_reset(location,LOC_CONSTANT,OS_ADDR);
  406. location.value:=0;
  407. end;
  408. {*****************************************************************************
  409. TCGPOINTERCONSTNODE
  410. *****************************************************************************}
  411. procedure tcgguidconstnode.pass_generate_code;
  412. var
  413. tmplabel : TAsmLabel;
  414. i : integer;
  415. begin
  416. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(16));
  417. { label for GUID }
  418. current_asmdata.getdatalabel(tmplabel);
  419. current_asmdata.asmlists[al_typedconsts].concat(tai_align.create(const_align(16)));
  420. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(tmplabel));
  421. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(longint(value.D1)));
  422. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(value.D2));
  423. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_16bit(value.D3));
  424. for i:=low(value.D4) to high(value.D4) do
  425. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_8bit(value.D4[i]));
  426. location.reference.symbol:=tmplabel;
  427. end;
  428. begin
  429. cdataconstnode:=tcgdataconstnode;
  430. crealconstnode:=tcgrealconstnode;
  431. cordconstnode:=tcgordconstnode;
  432. cpointerconstnode:=tcgpointerconstnode;
  433. cstringconstnode:=tcgstringconstnode;
  434. csetconstnode:=tcgsetconstnode;
  435. cnilnode:=tcgnilnode;
  436. cguidconstnode:=tcgguidconstnode;
  437. end.