ncgcon.pas 20 KB

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