ncgcon.pas 22 KB

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