ncgcon.pas 23 KB

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