ncgcon.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. symtype,
  24. ncon;
  25. type
  26. tcgrealconstnode = class(trealconstnode)
  27. procedure pass_generate_code;override;
  28. end;
  29. tcgordconstnode = class(tordconstnode)
  30. procedure pass_generate_code;override;
  31. end;
  32. tcgpointerconstnode = class(tpointerconstnode)
  33. procedure pass_generate_code;override;
  34. end;
  35. tcgstringconstnode = class(tstringconstnode)
  36. procedure pass_generate_code;override;
  37. protected
  38. procedure load_dynstring(const strpointerdef: tdef; const elementdef: tdef; const winlikewidestring: boolean); virtual;
  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. aasmcnst,
  58. symconst,symdef,aasmtai,aasmdata,defutil,
  59. cpuinfo,cpubase,
  60. cgbase,cgutils,
  61. hlcgobj,cclasses
  62. ;
  63. {*****************************************************************************
  64. TCGREALCONSTNODE
  65. *****************************************************************************}
  66. procedure tcgrealconstnode.pass_generate_code;
  67. { I suppose the parser/pass_1 must make sure the generated real }
  68. { constants are actually supported by the target processor? (JM) }
  69. const
  70. floattype2ait:array[tfloattype] of tairealconsttype=
  71. (aitrealconst_s32bit,aitrealconst_s64bit,aitrealconst_s80bit,aitrealconst_s80bit,aitrealconst_s64comp,aitrealconst_s64comp,aitrealconst_s128bit);
  72. { Since the value is stored always as bestreal, we share a single pool
  73. between all float types. This requires type and hiloswapped flag to
  74. be matched along with the value }
  75. type
  76. tfloatkey = record
  77. value: bestreal;
  78. typ: tfloattype;
  79. swapped: boolean;
  80. end;
  81. var
  82. lastlabel : tasmlabel;
  83. realait : tairealconsttype;
  84. entry : PHashSetItem;
  85. key: tfloatkey;
  86. {$ifdef ARM}
  87. hiloswapped : boolean;
  88. {$endif ARM}
  89. begin
  90. location_reset_ref(location,LOC_CREFERENCE,def_cgsize(resultdef),const_align(resultdef.alignment),[]);
  91. lastlabel:=nil;
  92. realait:=floattype2ait[tfloatdef(resultdef).floattype];
  93. {$ifdef ARM}
  94. hiloswapped:=is_double_hilo_swapped;
  95. {$endif ARM}
  96. { const already used ? }
  97. if not assigned(lab_real) then
  98. begin
  99. { there may be gap between record fields, zero it out }
  100. fillchar(key,sizeof(key),0);
  101. key.value:=value_real;
  102. key.typ:=tfloatdef(resultdef).floattype;
  103. {$ifdef ARM}
  104. key.swapped:=hiloswapped;
  105. {$endif ARM}
  106. entry := current_asmdata.ConstPools[sp_floats].FindOrAdd(@key, sizeof(key));
  107. lab_real := TAsmLabel(entry^.Data); // is it needed anymore?
  108. { :-(, we must generate a new entry }
  109. if not(assigned(lab_real)) then
  110. begin
  111. current_asmdata.getglobaldatalabel(lastlabel);
  112. entry^.Data:=lastlabel;
  113. lab_real:=lastlabel;
  114. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  115. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,lastlabel.name,const_align(resultdef.alignment));
  116. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(lastlabel));
  117. case realait of
  118. aitrealconst_s32bit :
  119. begin
  120. current_asmdata.asmlists[al_typedconsts].concat(tai_realconst.create_s32real(ts32real(value_real)));
  121. end;
  122. aitrealconst_s64bit :
  123. begin
  124. {$ifdef ARM}
  125. if hiloswapped then
  126. current_asmdata.asmlists[al_typedconsts].concat(tai_realconst.create_s64real_hiloswapped(ts64real(value_real)))
  127. else
  128. {$endif ARM}
  129. current_asmdata.asmlists[al_typedconsts].concat(tai_realconst.create_s64real(ts64real(value_real)));
  130. end;
  131. aitrealconst_s80bit :
  132. begin
  133. current_asmdata.asmlists[al_typedconsts].concat(tai_realconst.create_s80real(value_real,tfloatdef(resultdef).size));
  134. end;
  135. {$ifdef cpufloat128}
  136. aitrealconst_s128bit :
  137. begin
  138. current_asmdata.asmlists[al_typedconsts].concat(tai_realconst.create_s128real(value_real));
  139. end;
  140. {$endif cpufloat128}
  141. { the round is necessary for native compilers where comp isn't a float }
  142. aitrealconst_s64comp :
  143. if (value_real>9223372036854775807.0) or (value_real<-9223372036854775808.0) then
  144. message(parser_e_range_check_error)
  145. else
  146. current_asmdata.asmlists[al_typedconsts].concat(tai_realconst.create_s64compreal(round(value_real)));
  147. {$ifndef cpufloat128}
  148. else
  149. internalerror(10120);
  150. {$endif not cpufloat128}
  151. end;
  152. end;
  153. end;
  154. location.reference.symbol:=lab_real;
  155. end;
  156. {*****************************************************************************
  157. TCGORDCONSTNODE
  158. *****************************************************************************}
  159. procedure tcgordconstnode.pass_generate_code;
  160. begin
  161. location_reset(location,LOC_CONSTANT,def_cgsize(resultdef));
  162. {$if defined(cpu64bitalu) or defined(cpuhighleveltarget)}
  163. location.value:=value.svalue;
  164. {$else cpu64bitalu or cpuhighleveltarget}
  165. location.value64:=value.svalue;
  166. {$endif cpu64bitalu or cpuhighleveltarget}
  167. end;
  168. {*****************************************************************************
  169. TCGPOINTERCONSTNODE
  170. *****************************************************************************}
  171. procedure tcgpointerconstnode.pass_generate_code;
  172. begin
  173. { an integer const. behaves as a memory reference }
  174. location_reset(location,LOC_CONSTANT,OS_ADDR);
  175. location.value:=aint(value);
  176. end;
  177. {*****************************************************************************
  178. TCGSTRINGCONSTNODE
  179. *****************************************************************************}
  180. procedure tcgstringconstnode.pass_generate_code;
  181. var
  182. lastlabel: tasmlabofs;
  183. pc: pchar;
  184. l: longint;
  185. pool: THashSet;
  186. entry: PHashSetItem;
  187. winlikewidestring: boolean;
  188. elementdef: tdef;
  189. strpointerdef: tdef;
  190. datatcb: ttai_typedconstbuilder;
  191. datadef: tdef;
  192. const
  193. PoolMap: array[tconststringtype] of TConstPoolType = (
  194. sp_conststr,
  195. sp_shortstr,
  196. sp_longstr,
  197. sp_ansistr,
  198. sp_widestr,
  199. sp_unicodestr
  200. );
  201. begin
  202. case cst_type of
  203. cst_shortstring,
  204. cst_conststring,
  205. cst_ansistring:
  206. begin
  207. elementdef:=cansichartype;
  208. strpointerdef:=charpointertype;
  209. end;
  210. cst_widestring,
  211. cst_unicodestring:
  212. begin
  213. elementdef:=cwidechartype;
  214. strpointerdef:=widecharpointertype;
  215. end;
  216. else
  217. internalerror(2014032803);
  218. end;
  219. { for empty ansistrings we could return a constant 0 }
  220. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) and (len=0) then
  221. begin
  222. location_reset(location,LOC_CONSTANT,def_cgsize(strpointerdef));
  223. location.value:=0;
  224. exit;
  225. end;
  226. winlikewidestring:=(cst_type=cst_widestring) and (tf_winlikewidestring in target_info.flags);
  227. { const already used ? }
  228. if not assigned(lab_str) then
  229. begin
  230. pool := current_asmdata.ConstPools[PoolMap[cst_type]];
  231. if cst_type in [cst_widestring, cst_unicodestring] then
  232. entry := pool.FindOrAdd(pcompilerwidestring(value_str)^.data,len*cwidechartype.size)
  233. else
  234. if cst_type = cst_ansistring then
  235. entry := PHashSetItem(TTagHashSet(pool).FindOrAdd(value_str,len,tstringdef(resultdef).encoding))
  236. else
  237. entry := pool.FindOrAdd(value_str,len);
  238. lab_str := TAsmLabel(entry^.Data); // is it needed anymore?
  239. { :-(, we must generate a new entry }
  240. if not assigned(entry^.Data) then
  241. begin
  242. datatcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable,tcalo_apply_constalign]);
  243. case cst_type of
  244. cst_ansistring:
  245. begin
  246. if len=0 then
  247. InternalError(2008032301) { empty string should be handled above }
  248. else
  249. begin
  250. lastlabel:=datatcb.emit_ansistring_const(current_asmdata.AsmLists[al_typedconsts],value_str,len,tstringdef(resultdef).encoding);
  251. { because we hardcode the offset below due to it
  252. not being stored in the hashset, check here }
  253. if lastlabel.ofs<>datatcb.get_string_symofs(st_ansistring,false) then
  254. internalerror(2012051703);
  255. end;
  256. { no contents of the datatcb itself to concatenate,
  257. as we will just return the address of the emitted
  258. ansistring constant record }
  259. end;
  260. cst_unicodestring,
  261. cst_widestring:
  262. begin
  263. if len=0 then
  264. InternalError(2008032302) { empty string should be handled above }
  265. else
  266. begin
  267. lastlabel:=datatcb.emit_unicodestring_const(current_asmdata.AsmLists[al_typedconsts],
  268. value_str,
  269. tstringdef(resultdef).encoding,
  270. winlikewidestring);
  271. { because we hardcode the offset below due to it
  272. not being stored in the hashset, check here }
  273. if lastlabel.ofs<>datatcb.get_string_symofs(tstringdef(resultdef).stringtype,winlikewidestring) then
  274. internalerror(2012051704);
  275. end;
  276. { no contents of the datatcb itself to concatenate,
  277. as we will just return the address of the emitted
  278. unicode/widestring constant record }
  279. end;
  280. cst_shortstring:
  281. begin
  282. current_asmdata.getglobaldatalabel(lastlabel.lab);
  283. { truncate strings larger than 255 chars }
  284. if len>255 then
  285. l:=255
  286. else
  287. l:=len;
  288. { include length and terminating zero for quick conversion to pchar }
  289. getmem(pc,l+2);
  290. move(value_str^,pc[1],l);
  291. pc[0]:=chr(l);
  292. pc[l+1]:=#0;
  293. datadef:=carraydef.getreusable(cansichartype,l+2);
  294. datatcb.maybe_begin_aggregate(datadef);
  295. datatcb.emit_tai(Tai_string.Create_pchar(pc,l+2),datadef);
  296. datatcb.maybe_end_aggregate(datadef);
  297. current_asmdata.asmlists[al_typedconsts].concatList(
  298. datatcb.get_final_asmlist(lastlabel.lab,datadef,sec_rodata_norel,lastlabel.lab.name,const_align(sizeof(pint)))
  299. );
  300. end;
  301. cst_conststring:
  302. begin
  303. current_asmdata.getglobaldatalabel(lastlabel.lab);
  304. { include terminating zero }
  305. getmem(pc,len+1);
  306. move(value_str^,pc[0],len);
  307. pc[len]:=#0;
  308. { the data includes the terminating #0 because this
  309. string can be used for pchar assignments (but it's
  310. also used for array-of-char assignments, in which
  311. case the terminating #0 is not part of the data) }
  312. datadef:=carraydef.getreusable(cansichartype,len+1);
  313. datatcb.maybe_begin_aggregate(datadef);
  314. datatcb.emit_tai(Tai_string.Create_pchar(pc,len+1),datadef);
  315. datatcb.maybe_end_aggregate(datadef);
  316. current_asmdata.asmlists[al_typedconsts].concatList(
  317. datatcb.get_final_asmlist(lastlabel.lab,datadef,sec_rodata_norel,lastlabel.lab.name,const_align(sizeof(pint)))
  318. );
  319. end;
  320. else
  321. internalerror(2013120103);
  322. end;
  323. datatcb.free;
  324. lab_str:=lastlabel.lab;
  325. entry^.Data:=lastlabel.lab;
  326. end;
  327. end;
  328. if cst_type in [cst_ansistring, cst_widestring, cst_unicodestring] then
  329. begin
  330. location_reset(location, LOC_REGISTER, def_cgsize(strpointerdef));
  331. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,strpointerdef);
  332. load_dynstring(strpointerdef, elementdef, winlikewidestring);
  333. end
  334. else
  335. begin
  336. location_reset_ref(location, LOC_CREFERENCE, def_cgsize(resultdef), const_align(strpointerdef.size), []);
  337. location.reference.symbol:=lab_str;
  338. end;
  339. end;
  340. procedure tcgstringconstnode.load_dynstring(const strpointerdef: tdef; const elementdef: tdef; const winlikewidestring: boolean);
  341. var
  342. href: treference;
  343. begin
  344. reference_reset_symbol(href, lab_str,
  345. ctai_typedconstbuilder.get_string_symofs(tstringdef(resultdef).stringtype, winlikewidestring),
  346. const_align(strpointerdef.size),[]);
  347. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList, elementdef, strpointerdef, href, location.register)
  348. end;
  349. {*****************************************************************************
  350. TCGSETCONSTNODE
  351. *****************************************************************************}
  352. function tcgsetconstnode.emitvarsetconst: tasmsymbol;
  353. type
  354. setbytes=array[0..31] of byte;
  355. Psetbytes=^setbytes;
  356. var
  357. lab: tasmlabel;
  358. i: longint;
  359. tcb: ttai_typedconstbuilder;
  360. begin
  361. current_asmdata.getglobaldatalabel(lab);
  362. result:=lab;
  363. lab_set:=lab;
  364. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable,tcalo_apply_constalign]);
  365. tcb.maybe_begin_aggregate(resultdef);
  366. if (source_info.endian=target_info.endian) then
  367. for i:=0 to resultdef.size-1 do
  368. tcb.emit_tai(tai_const.create_8bit(Psetbytes(value_set)^[i]),u8inttype)
  369. else
  370. for i:=0 to resultdef.size-1 do
  371. tcb.emit_tai(tai_const.create_8bit(reverse_byte(Psetbytes(value_set)^[i])),u8inttype);
  372. tcb.maybe_end_aggregate(resultdef);
  373. current_asmdata.asmlists[al_typedconsts].concatlist(tcb.get_final_asmlist(
  374. result,resultdef,sec_rodata_norel,result.name,const_align(8)));
  375. tcb.free;
  376. end;
  377. procedure tcgsetconstnode.handlevarsetconst;
  378. var
  379. entry : PHashSetItem;
  380. begin
  381. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(8),[]);
  382. { const already used ? }
  383. if not assigned(lab_set) then
  384. begin
  385. entry := current_asmdata.ConstPools[sp_varsets].FindOrAdd(value_set, resultdef.size);
  386. { :-(, we must generate a new entry }
  387. if not assigned(entry^.Data) then
  388. entry^.Data:=emitvarsetconst;
  389. lab_set := TAsmSymbol(entry^.Data);
  390. end;
  391. location.reference.symbol:=lab_set;
  392. end;
  393. procedure tcgsetconstnode.pass_generate_code;
  394. type
  395. setbytes=array[0..31] of byte;
  396. Psetbytes=^setbytes;
  397. procedure smallsetconst;
  398. begin
  399. location_reset(location,LOC_CONSTANT,int_cgsize(resultdef.size));
  400. if (source_info.endian=target_info.endian) then
  401. begin
  402. { not plongint, because that will "sign extend" the set on 64 bit platforms }
  403. { if changed to "paword", please also modify "32-resultdef.size*8" and }
  404. { cross-endian code below }
  405. { Extra aint type cast to avoid range errors }
  406. location.value:=aint(pCardinal(value_set)^)
  407. end
  408. else
  409. begin
  410. location.value:=aint(swapendian(Pcardinal(value_set)^));
  411. location.value:=aint(
  412. reverse_byte (location.value and $ff) or
  413. (reverse_byte((location.value shr 8) and $ff) shl 8) or
  414. (reverse_byte((location.value shr 16) and $ff) shl 16) or
  415. (reverse_byte((location.value shr 24) and $ff) shl 24)
  416. );
  417. end;
  418. if (target_info.endian=endian_big) then
  419. location.value:=location.value shr (32-resultdef.size*8);
  420. end;
  421. begin
  422. adjustforsetbase;
  423. { small sets are loaded as constants }
  424. if is_smallset(resultdef) then
  425. smallsetconst
  426. else
  427. handlevarsetconst;
  428. end;
  429. {*****************************************************************************
  430. TCGNILNODE
  431. *****************************************************************************}
  432. procedure tcgnilnode.pass_generate_code;
  433. begin
  434. location_reset(location,LOC_CONSTANT,OS_ADDR);
  435. location.value:=0;
  436. end;
  437. {*****************************************************************************
  438. TCGGUIDCONSTNODE
  439. *****************************************************************************}
  440. procedure tcgguidconstnode.pass_generate_code;
  441. var
  442. lastlabel : tasmlabel;
  443. entry : PHashSetItem;
  444. datatcb : ttai_typedconstbuilder;
  445. begin
  446. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(16),[]);
  447. lastlabel:=nil;
  448. { const already used ? }
  449. if not assigned(lab_set) then
  450. begin
  451. entry:=current_asmdata.ConstPools[sp_guids].FindOrAdd(@value,sizeof(value));
  452. lab_set:=TAsmLabel(entry^.Data); // is it needed anymore?
  453. { :-(, we must generate a new entry }
  454. if not assigned(entry^.Data) then
  455. begin
  456. current_asmdata.getglobaldatalabel(lastlabel);
  457. datatcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable,tcalo_apply_constalign]);
  458. datatcb.emit_guid_const(value);
  459. current_asmdata.asmlists[al_typedconsts].concatList(
  460. datatcb.get_final_asmlist(lastlabel,rec_tguid,sec_rodata_norel,lastlabel.name,const_align(16)));
  461. datatcb.free;
  462. lab_set:=lastlabel;
  463. entry^.Data:=lastlabel;
  464. end;
  465. end;
  466. location.reference.symbol:=lab_set;
  467. end;
  468. begin
  469. crealconstnode:=tcgrealconstnode;
  470. cordconstnode:=tcgordconstnode;
  471. cpointerconstnode:=tcgpointerconstnode;
  472. cstringconstnode:=tcgstringconstnode;
  473. csetconstnode:=tcgsetconstnode;
  474. cnilnode:=tcgnilnode;
  475. cguidconstnode:=tcgguidconstnode;
  476. end.