2
0

ncgcon.pas 24 KB

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