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. 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. else
  148. internalerror(10120);
  149. end;
  150. end;
  151. end;
  152. location.reference.symbol:=lab_real;
  153. end;
  154. {*****************************************************************************
  155. TCGORDCONSTNODE
  156. *****************************************************************************}
  157. procedure tcgordconstnode.pass_generate_code;
  158. begin
  159. location_reset(location,LOC_CONSTANT,def_cgsize(resultdef));
  160. {$ifdef cpu64bitalu}
  161. location.value:=value.svalue;
  162. {$else cpu64bitalu}
  163. location.value64:=value.svalue;
  164. {$endif cpu64bitalu}
  165. end;
  166. {*****************************************************************************
  167. TCGPOINTERCONSTNODE
  168. *****************************************************************************}
  169. procedure tcgpointerconstnode.pass_generate_code;
  170. begin
  171. { an integer const. behaves as a memory reference }
  172. location_reset(location,LOC_CONSTANT,OS_ADDR);
  173. location.value:=PInt(value);
  174. end;
  175. {*****************************************************************************
  176. TCGSTRINGCONSTNODE
  177. *****************************************************************************}
  178. procedure tcgstringconstnode.pass_generate_code;
  179. var
  180. lastlabel: tasmlabofs;
  181. pc: pchar;
  182. l: longint;
  183. pool: THashSet;
  184. entry: PHashSetItem;
  185. winlikewidestring: boolean;
  186. elementdef: tdef;
  187. strpointerdef: tdef;
  188. datatcb: ttai_typedconstbuilder;
  189. datadef: tdef;
  190. const
  191. PoolMap: array[tconststringtype] of TConstPoolType = (
  192. sp_conststr,
  193. sp_shortstr,
  194. sp_longstr,
  195. sp_ansistr,
  196. sp_widestr,
  197. sp_unicodestr
  198. );
  199. begin
  200. case cst_type of
  201. cst_shortstring,
  202. cst_conststring,
  203. cst_ansistring:
  204. begin
  205. elementdef:=cansichartype;
  206. strpointerdef:=charpointertype;
  207. end;
  208. cst_widestring,
  209. cst_unicodestring:
  210. begin
  211. elementdef:=cwidechartype;
  212. strpointerdef:=widecharpointertype;
  213. end;
  214. else
  215. internalerror(2014032803);
  216. end;
  217. { for empty ansistrings we could return a constant 0 }
  218. if (cst_type in [cst_ansistring,cst_widestring,cst_unicodestring]) and (len=0) then
  219. begin
  220. location_reset(location,LOC_CONSTANT,def_cgsize(strpointerdef));
  221. location.value:=0;
  222. exit;
  223. end;
  224. winlikewidestring:=(cst_type=cst_widestring) and (tf_winlikewidestring in target_info.flags);
  225. { const already used ? }
  226. if not assigned(lab_str) then
  227. begin
  228. pool := current_asmdata.ConstPools[PoolMap[cst_type]];
  229. if cst_type in [cst_widestring, cst_unicodestring] then
  230. entry := pool.FindOrAdd(pcompilerwidestring(value_str)^.data,len*cwidechartype.size)
  231. else
  232. if cst_type = cst_ansistring then
  233. entry := PHashSetItem(TTagHashSet(pool).FindOrAdd(value_str,len,tstringdef(resultdef).encoding))
  234. else
  235. entry := pool.FindOrAdd(value_str,len);
  236. lab_str := TAsmLabel(entry^.Data); // is it needed anymore?
  237. { :-(, we must generate a new entry }
  238. if not assigned(entry^.Data) then
  239. begin
  240. datatcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable,tcalo_apply_constalign]);
  241. case cst_type of
  242. cst_ansistring:
  243. begin
  244. if len=0 then
  245. InternalError(2008032301) { empty string should be handled above }
  246. else
  247. begin
  248. lastlabel:=datatcb.emit_ansistring_const(current_asmdata.AsmLists[al_typedconsts],value_str,len,tstringdef(resultdef).encoding);
  249. { because we hardcode the offset below due to it
  250. not being stored in the hashset, check here }
  251. if lastlabel.ofs<>datatcb.get_string_symofs(st_ansistring,false) then
  252. internalerror(2012051703);
  253. end;
  254. { no contents of the datatcb itself to concatenate,
  255. as we will just return the address of the emitted
  256. ansistring constant record }
  257. end;
  258. cst_unicodestring,
  259. cst_widestring:
  260. begin
  261. if len=0 then
  262. InternalError(2008032302) { empty string should be handled above }
  263. else
  264. begin
  265. lastlabel:=datatcb.emit_unicodestring_const(current_asmdata.AsmLists[al_typedconsts],
  266. value_str,
  267. tstringdef(resultdef).encoding,
  268. winlikewidestring);
  269. { because we hardcode the offset below due to it
  270. not being stored in the hashset, check here }
  271. if lastlabel.ofs<>datatcb.get_string_symofs(tstringdef(resultdef).stringtype,winlikewidestring) then
  272. internalerror(2012051704);
  273. end;
  274. { no contents of the datatcb itself to concatenate,
  275. as we will just return the address of the emitted
  276. unicode/widestring constant record }
  277. end;
  278. cst_shortstring:
  279. begin
  280. current_asmdata.getglobaldatalabel(lastlabel.lab);
  281. { truncate strings larger than 255 chars }
  282. if len>255 then
  283. l:=255
  284. else
  285. l:=len;
  286. { include length and terminating zero for quick conversion to pchar }
  287. getmem(pc,l+2);
  288. move(value_str^,pc[1],l);
  289. pc[0]:=chr(l);
  290. pc[l+1]:=#0;
  291. datadef:=carraydef.getreusable(cansichartype,l+2);
  292. datatcb.maybe_begin_aggregate(datadef);
  293. datatcb.emit_tai(Tai_string.Create_pchar(pc,l+2),datadef);
  294. datatcb.maybe_end_aggregate(datadef);
  295. current_asmdata.asmlists[al_typedconsts].concatList(
  296. datatcb.get_final_asmlist(lastlabel.lab,datadef,sec_rodata_norel,lastlabel.lab.name,const_align(sizeof(pint)))
  297. );
  298. end;
  299. cst_conststring:
  300. begin
  301. current_asmdata.getglobaldatalabel(lastlabel.lab);
  302. { include terminating zero }
  303. getmem(pc,len+1);
  304. move(value_str^,pc[0],len);
  305. pc[len]:=#0;
  306. { the data includes the terminating #0 because this
  307. string can be used for pchar assignments (but it's
  308. also used for array-of-char assignments, in which
  309. case the terminating #0 is not part of the data) }
  310. datadef:=carraydef.getreusable(cansichartype,len+1);
  311. datatcb.maybe_begin_aggregate(datadef);
  312. datatcb.emit_tai(Tai_string.Create_pchar(pc,len+1),datadef);
  313. datatcb.maybe_end_aggregate(datadef);
  314. current_asmdata.asmlists[al_typedconsts].concatList(
  315. datatcb.get_final_asmlist(lastlabel.lab,datadef,sec_rodata_norel,lastlabel.lab.name,const_align(sizeof(pint)))
  316. );
  317. end;
  318. else
  319. internalerror(2013120103);
  320. end;
  321. datatcb.free;
  322. lab_str:=lastlabel.lab;
  323. entry^.Data:=lastlabel.lab;
  324. end;
  325. end;
  326. if cst_type in [cst_ansistring, cst_widestring, cst_unicodestring] then
  327. begin
  328. location_reset(location, LOC_REGISTER, def_cgsize(strpointerdef));
  329. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,strpointerdef);
  330. load_dynstring(strpointerdef, elementdef, winlikewidestring);
  331. end
  332. else
  333. begin
  334. location_reset_ref(location, LOC_CREFERENCE, def_cgsize(resultdef), const_align(strpointerdef.size), []);
  335. location.reference.symbol:=lab_str;
  336. end;
  337. end;
  338. procedure tcgstringconstnode.load_dynstring(const strpointerdef: tdef; const elementdef: tdef; const winlikewidestring: boolean);
  339. var
  340. href: treference;
  341. begin
  342. reference_reset_symbol(href, lab_str,
  343. ctai_typedconstbuilder.get_string_symofs(tstringdef(resultdef).stringtype, winlikewidestring),
  344. const_align(strpointerdef.size),[]);
  345. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList, elementdef, strpointerdef, href, location.register)
  346. end;
  347. {*****************************************************************************
  348. TCGSETCONSTNODE
  349. *****************************************************************************}
  350. function tcgsetconstnode.emitvarsetconst: tasmsymbol;
  351. type
  352. setbytes=array[0..31] of byte;
  353. Psetbytes=^setbytes;
  354. var
  355. lab: tasmlabel;
  356. i: longint;
  357. tcb: ttai_typedconstbuilder;
  358. begin
  359. current_asmdata.getglobaldatalabel(lab);
  360. result:=lab;
  361. lab_set:=lab;
  362. tcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable,tcalo_apply_constalign]);
  363. tcb.maybe_begin_aggregate(resultdef);
  364. if (source_info.endian=target_info.endian) then
  365. for i:=0 to resultdef.size-1 do
  366. tcb.emit_tai(tai_const.create_8bit(Psetbytes(value_set)^[i]),u8inttype)
  367. else
  368. for i:=0 to resultdef.size-1 do
  369. tcb.emit_tai(tai_const.create_8bit(reverse_byte(Psetbytes(value_set)^[i])),u8inttype);
  370. tcb.maybe_end_aggregate(resultdef);
  371. current_asmdata.asmlists[al_typedconsts].concatlist(tcb.get_final_asmlist(
  372. result,resultdef,sec_rodata_norel,result.name,const_align(8)));
  373. tcb.free;
  374. end;
  375. procedure tcgsetconstnode.handlevarsetconst;
  376. var
  377. entry : PHashSetItem;
  378. begin
  379. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(8),[]);
  380. { const already used ? }
  381. if not assigned(lab_set) then
  382. begin
  383. entry := current_asmdata.ConstPools[sp_varsets].FindOrAdd(value_set, resultdef.size);
  384. { :-(, we must generate a new entry }
  385. if not assigned(entry^.Data) then
  386. entry^.Data:=emitvarsetconst;
  387. lab_set := TAsmSymbol(entry^.Data);
  388. end;
  389. location.reference.symbol:=lab_set;
  390. end;
  391. procedure tcgsetconstnode.pass_generate_code;
  392. type
  393. setbytes=array[0..31] of byte;
  394. Psetbytes=^setbytes;
  395. procedure smallsetconst;
  396. begin
  397. location_reset(location,LOC_CONSTANT,int_cgsize(resultdef.size));
  398. if (source_info.endian=target_info.endian) then
  399. begin
  400. { not plongint, because that will "sign extend" the set on 64 bit platforms }
  401. { if changed to "paword", please also modify "32-resultdef.size*8" and }
  402. { cross-endian code below }
  403. { Extra aint type cast to avoid range errors }
  404. location.value:=aint(pCardinal(value_set)^)
  405. end
  406. else
  407. begin
  408. location.value:=aint(swapendian(Pcardinal(value_set)^));
  409. location.value:=aint(
  410. reverse_byte (location.value and $ff) or
  411. (reverse_byte((location.value shr 8) and $ff) shl 8) or
  412. (reverse_byte((location.value shr 16) and $ff) shl 16) or
  413. (reverse_byte((location.value shr 24) and $ff) shl 24)
  414. );
  415. end;
  416. if (target_info.endian=endian_big) then
  417. location.value:=location.value shr (32-resultdef.size*8);
  418. end;
  419. begin
  420. adjustforsetbase;
  421. { small sets are loaded as constants }
  422. if is_smallset(resultdef) then
  423. smallsetconst
  424. else
  425. handlevarsetconst;
  426. end;
  427. {*****************************************************************************
  428. TCGNILNODE
  429. *****************************************************************************}
  430. procedure tcgnilnode.pass_generate_code;
  431. begin
  432. location_reset(location,LOC_CONSTANT,OS_ADDR);
  433. location.value:=0;
  434. end;
  435. {*****************************************************************************
  436. TCGGUIDCONSTNODE
  437. *****************************************************************************}
  438. procedure tcgguidconstnode.pass_generate_code;
  439. var
  440. lastlabel : tasmlabel;
  441. entry : PHashSetItem;
  442. datatcb : ttai_typedconstbuilder;
  443. begin
  444. location_reset_ref(location,LOC_CREFERENCE,OS_NO,const_align(16),[]);
  445. lastlabel:=nil;
  446. { const already used ? }
  447. if not assigned(lab_set) then
  448. begin
  449. entry:=current_asmdata.ConstPools[sp_guids].FindOrAdd(@value,sizeof(value));
  450. lab_set:=TAsmLabel(entry^.Data); // is it needed anymore?
  451. { :-(, we must generate a new entry }
  452. if not assigned(entry^.Data) then
  453. begin
  454. current_asmdata.getglobaldatalabel(lastlabel);
  455. datatcb:=ctai_typedconstbuilder.create([tcalo_is_lab,tcalo_make_dead_strippable,tcalo_apply_constalign]);
  456. datatcb.emit_guid_const(value);
  457. current_asmdata.asmlists[al_typedconsts].concatList(
  458. datatcb.get_final_asmlist(lastlabel,rec_tguid,sec_rodata_norel,lastlabel.name,const_align(16)));
  459. datatcb.free;
  460. lab_set:=lastlabel;
  461. entry^.Data:=lastlabel;
  462. end;
  463. end;
  464. location.reference.symbol:=lab_set;
  465. end;
  466. begin
  467. crealconstnode:=tcgrealconstnode;
  468. cordconstnode:=tcgordconstnode;
  469. cpointerconstnode:=tcgpointerconstnode;
  470. cstringconstnode:=tcgstringconstnode;
  471. csetconstnode:=tcgsetconstnode;
  472. cnilnode:=tcgnilnode;
  473. cguidconstnode:=tcgguidconstnode;
  474. end.