ncgcnv.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Generate assembler for nodes that handle type conversions which are
  4. the same for 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 ncgcnv;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,ncnv,defutil,defcmp;
  23. type
  24. { tcgtypeconvnode }
  25. tcgtypeconvnode = class(ttypeconvnode)
  26. protected
  27. {$ifdef cpuflags}
  28. { CPUs without flags need a specific implementation of int -> bool }
  29. procedure second_int_to_bool;override;
  30. {$endif cpuflags}
  31. procedure second_int_to_int;override;
  32. procedure second_cstring_to_pchar;override;
  33. procedure second_cstring_to_int;override;
  34. procedure second_string_to_chararray;override;
  35. procedure second_array_to_pointer;override;
  36. procedure second_pointer_to_array;override;
  37. procedure second_char_to_string;override;
  38. procedure second_real_to_real;override;
  39. procedure second_cord_to_pointer;override;
  40. procedure second_proc_to_procvar;override;
  41. procedure second_nil_to_methodprocvar;override;
  42. procedure second_bool_to_int;override;
  43. procedure second_bool_to_bool;override;
  44. procedure second_ansistring_to_pchar;override;
  45. procedure second_class_to_intf;override;
  46. procedure second_char_to_char;override;
  47. procedure second_elem_to_openarray;override;
  48. procedure second_nothing;override;
  49. public
  50. procedure pass_generate_code;override;
  51. end;
  52. tcgasnode = class(tasnode)
  53. procedure pass_generate_code;override;
  54. end;
  55. implementation
  56. uses
  57. cutils,verbose,globtype,globals,
  58. aasmbase,aasmtai,aasmdata,aasmcpu,symconst,symdef,paramgr,
  59. nutils,ncon,ncal,
  60. cpubase,systems,
  61. procinfo,pass_2,
  62. cgbase,
  63. cgutils,cgobj,hlcgobj,
  64. ncgutil,
  65. tgobj
  66. ;
  67. procedure tcgtypeconvnode.second_int_to_int;
  68. var
  69. orgsize,
  70. newsize : tcgsize;
  71. ressize,
  72. leftsize : longint;
  73. begin
  74. newsize:=def_cgsize(resultdef);
  75. { insert range check if not explicit conversion }
  76. if not(nf_explicit in flags) then
  77. hlcg.g_rangecheck(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef);
  78. { is the result size smaller? when typecasting from void
  79. we always reuse the current location, because there is
  80. nothing that we can load in a register }
  81. ressize := resultdef.size;
  82. leftsize := left.resultdef.size;
  83. if ((ressize<>leftsize) or
  84. is_bitpacked_access(left)) and
  85. not is_void(left.resultdef) then
  86. begin
  87. location_copy(location,left.location);
  88. { reuse a loc_reference when the newsize is smaller than
  89. than the original, else load it to a register }
  90. if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  91. (ressize<leftsize) then
  92. begin
  93. location.size:=newsize;
  94. if (target_info.endian = ENDIAN_BIG) then
  95. begin
  96. inc(location.reference.offset,leftsize-ressize);
  97. location.reference.alignment:=newalignment(location.reference.alignment,leftsize-ressize);
  98. end;
  99. end
  100. else
  101. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,false);
  102. end
  103. else
  104. begin
  105. { no special loading is required, reuse current location }
  106. { that's not true, if you go from signed to unsiged or }
  107. { vice versa, you need sign extension/removal if the }
  108. { value is already in a register (at least for archs }
  109. { which don't have 8bit register components etc) (JM) }
  110. location_copy(location,left.location);
  111. location.size:=newsize;
  112. orgsize := def_cgsize(left.resultdef);
  113. if (ressize < sizeof(aint)) and
  114. (location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  115. (orgsize <> newsize) then
  116. begin
  117. location.register := cg.getintregister(current_asmdata.CurrAsmList,newsize);
  118. location.loc := LOC_REGISTER;
  119. cg.a_load_reg_reg(current_asmdata.CurrAsmList,orgsize,newsize,left.location.register,location.register);
  120. end;
  121. end;
  122. end;
  123. {$ifdef cpuflags}
  124. procedure tcgtypeconvnode.second_int_to_bool;
  125. var
  126. hregister : tregister;
  127. href : treference;
  128. resflags : tresflags;
  129. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  130. newsize : tcgsize;
  131. begin
  132. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  133. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  134. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  135. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  136. secondpass(left);
  137. if codegenerror then
  138. exit;
  139. { Explicit typecasts from any ordinal type to a boolean type }
  140. { must not change the ordinal value }
  141. if (nf_explicit in flags) and
  142. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  143. begin
  144. location_copy(location,left.location);
  145. newsize:=def_cgsize(resultdef);
  146. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  147. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  148. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  149. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  150. else
  151. location.size:=newsize;
  152. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  153. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  154. exit;
  155. end;
  156. { though ppc/ppc64 doesn't use the generic code, we need to ifdef here
  157. because the code is included into the powerpc compilers }
  158. {$if defined(POWERPC) or defined(POWERPC64)}
  159. resflags.cr := RS_CR0;
  160. resflags.flag:=F_NE;
  161. {$else defined(POWERPC) or defined(POWERPC64)}
  162. { Load left node into flag F_NE/F_E }
  163. resflags:=F_NE;
  164. {$endif defined(POWERPC) or defined(POWERPC64)}
  165. case left.location.loc of
  166. LOC_CREFERENCE,
  167. LOC_REFERENCE :
  168. begin
  169. if left.location.size in [OS_64,OS_S64] then
  170. begin
  171. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  172. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  173. href:=left.location.reference;
  174. inc(href.offset,4);
  175. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  176. end
  177. else
  178. begin
  179. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  180. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  181. end;
  182. end;
  183. LOC_FLAGS :
  184. begin
  185. resflags:=left.location.resflags;
  186. end;
  187. LOC_REGISTER,LOC_CREGISTER :
  188. begin
  189. {$ifndef cpu64bitalu}
  190. if left.location.size in [OS_64,OS_S64] then
  191. begin
  192. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  193. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  194. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  195. end
  196. else
  197. {$endif cpu64bitalu}
  198. begin
  199. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  200. end;
  201. end;
  202. LOC_JUMP :
  203. begin
  204. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  205. current_asmdata.getjumplabel(hlabel);
  206. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  207. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  208. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  209. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  210. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  211. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  212. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  213. end;
  214. else
  215. internalerror(200311301);
  216. end;
  217. { load flags to register }
  218. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  219. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  220. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,location.register);
  221. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  222. if (is_cbool(resultdef)) then
  223. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,location.register,location.register);
  224. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  225. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  226. end;
  227. {$endif cpuflags}
  228. procedure tcgtypeconvnode.second_cstring_to_pchar;
  229. var
  230. hr : treference;
  231. begin
  232. if left.nodetype<>stringconstn then
  233. internalerror(200601131);
  234. location_reset(location,LOC_REGISTER,OS_ADDR);
  235. case tstringconstnode(left).cst_type of
  236. cst_conststring :
  237. begin
  238. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  239. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  240. end;
  241. cst_shortstring :
  242. begin
  243. inc(left.location.reference.offset);
  244. location.reference.alignment:=1;
  245. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  246. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  247. end;
  248. cst_widestring,
  249. cst_unicodestring,
  250. cst_ansistring :
  251. begin
  252. if tstringconstnode(left).len=0 then
  253. begin
  254. { FPC_EMPTYCHAR is a widechar -> 2 bytes }
  255. reference_reset(hr,2);
  256. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR');
  257. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  258. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hr,location.register);
  259. end
  260. else
  261. begin
  262. location_copy(location,left.location);
  263. end;
  264. end;
  265. cst_longstring:
  266. begin
  267. {!!!!!!!}
  268. internalerror(8888);
  269. end;
  270. else
  271. internalerror(200808241);
  272. end;
  273. end;
  274. procedure tcgtypeconvnode.second_cstring_to_int;
  275. begin
  276. { this can't happen because constants are already processed in
  277. pass 1 }
  278. internalerror(200510013);
  279. end;
  280. procedure tcgtypeconvnode.second_string_to_chararray;
  281. begin
  282. if is_chararray(left.resultdef) then
  283. begin
  284. location_copy(location,left.location);
  285. exit;
  286. end;
  287. { should be handled already in resultdef pass (JM) }
  288. internalerror(200108292);
  289. end;
  290. procedure tcgtypeconvnode.second_array_to_pointer;
  291. begin
  292. location_reset(location,LOC_REGISTER,OS_ADDR);
  293. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  294. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
  295. end;
  296. procedure tcgtypeconvnode.second_pointer_to_array;
  297. begin
  298. { assume natural alignment }
  299. location_reset_ref(location,LOC_REFERENCE,OS_NO,resultdef.alignment);
  300. case left.location.loc of
  301. LOC_CREGISTER,
  302. LOC_REGISTER :
  303. begin
  304. {$ifdef cpu_uses_separate_address_registers}
  305. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  306. begin
  307. location.reference.base:=rg.getaddressregister(current_asmdata.CurrAsmList);
  308. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  309. left.location.register,location.reference.base);
  310. end
  311. else
  312. {$endif}
  313. location.reference.base := left.location.register;
  314. end;
  315. LOC_REFERENCE,
  316. LOC_CREFERENCE,
  317. { tricky type casting of parameters can cause these locations, see tb0593.pp on x86_64-linux }
  318. LOC_SUBSETREG,
  319. LOC_CSUBSETREG,
  320. LOC_SUBSETREF,
  321. LOC_CSUBSETREF:
  322. begin
  323. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  324. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,left.location,
  325. location.reference.base);
  326. if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  327. location_freetemp(current_asmdata.CurrAsmList,left.location);
  328. end;
  329. else
  330. internalerror(2002032216);
  331. end;
  332. end;
  333. procedure tcgtypeconvnode.second_char_to_string;
  334. begin
  335. location_reset_ref(location,LOC_REFERENCE,OS_NO,2);
  336. case tstringdef(resultdef).stringtype of
  337. st_shortstring :
  338. begin
  339. tg.gethltemp(current_asmdata.CurrAsmList,cshortstringtype,256,tt_normal,location.reference);
  340. hlcg.a_load_loc_ref(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,left.location,
  341. location.reference);
  342. location_freetemp(current_asmdata.CurrAsmList,left.location);
  343. end;
  344. { the rest is removed in the resultdef pass and converted to compilerprocs }
  345. else
  346. internalerror(4179);
  347. end;
  348. end;
  349. procedure tcgtypeconvnode.second_real_to_real;
  350. {$ifdef x86}
  351. var
  352. tr: treference;
  353. {$endif x86}
  354. begin
  355. location_reset(location,expectloc,def_cgsize(resultdef));
  356. {$ifdef x86}
  357. { extended types in memory which should be loaded into the sse unit
  358. must be converted by the fpu first, so force them to be loaded into
  359. the fpu }
  360. if (expectloc=LOC_MMREGISTER) and
  361. (left.location.size in [OS_F80,OS_C64]) then
  362. begin
  363. if (left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  364. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false);
  365. { round them down to the proper precision }
  366. tg.gethltemp(current_asmdata.currasmlist,resultdef,resultdef.size,tt_normal,tr);
  367. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,tr);
  368. location_reset_ref(left.location,LOC_REFERENCE,location.size,tr.alignment);
  369. left.location.reference:=tr;
  370. end;
  371. {$endif x86}
  372. { ARM VFP values are in integer registers when they are function results }
  373. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  374. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  375. case left.location.loc of
  376. LOC_FPUREGISTER,
  377. LOC_CFPUREGISTER:
  378. begin
  379. case expectloc of
  380. LOC_FPUREGISTER:
  381. begin
  382. { on sparc a move from double -> single means from two to one register. }
  383. { On all other platforms it also needs rounding to avoid that }
  384. { single(double_regvar) = double_regvar is true in all cases }
  385. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  386. hlcg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
  387. end;
  388. LOC_MMREGISTER:
  389. begin
  390. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  391. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  392. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,location.register,mms_movescalar);
  393. end
  394. else
  395. internalerror(2003012262);
  396. end;
  397. exit
  398. end;
  399. LOC_CREFERENCE,
  400. LOC_REFERENCE:
  401. begin
  402. if expectloc=LOC_MMREGISTER then
  403. begin
  404. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  405. hlcg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location,location.register,mms_movescalar)
  406. end
  407. else
  408. begin
  409. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  410. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  411. hlcg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
  412. end;
  413. location_freetemp(current_asmdata.CurrAsmList,left.location);
  414. end;
  415. LOC_MMREGISTER,
  416. LOC_CMMREGISTER:
  417. begin
  418. case expectloc of
  419. LOC_FPUREGISTER:
  420. begin
  421. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false);
  422. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  423. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,location.register);
  424. end;
  425. LOC_MMREGISTER:
  426. begin
  427. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  428. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,location.register,mms_movescalar);
  429. end;
  430. else
  431. internalerror(2003012261);
  432. end;
  433. end;
  434. else
  435. internalerror(2002032215);
  436. end;
  437. end;
  438. procedure tcgtypeconvnode.second_cord_to_pointer;
  439. begin
  440. { this can't happen because constants are already processed in
  441. pass 1 }
  442. internalerror(47423985);
  443. end;
  444. procedure tcgtypeconvnode.second_proc_to_procvar;
  445. var
  446. tmpreg: tregister;
  447. begin
  448. if tabstractprocdef(resultdef).is_addressonly then
  449. begin
  450. location_reset(location,LOC_REGISTER,OS_ADDR);
  451. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  452. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  453. end
  454. else
  455. begin
  456. if not tabstractprocdef(left.resultdef).is_addressonly then
  457. location_copy(location,left.location)
  458. else
  459. begin
  460. { assigning a global function to a nested procvar -> create
  461. tmethodpointer record and set the "frame pointer" to nil }
  462. location_reset_ref(location,LOC_REFERENCE,int_cgsize(sizeof(pint)*2),sizeof(pint));
  463. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,resultdef.size,tt_normal,location.reference);
  464. tmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  465. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,tmpreg);
  466. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,tmpreg,location.reference);
  467. { setting the frame pointer to nil is not strictly necessary
  468. since the global procedure won't use it, but it can help with
  469. debugging }
  470. inc(location.reference.offset,sizeof(pint));
  471. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_ADDR,0,location.reference);
  472. dec(location.reference.offset,sizeof(pint));
  473. end;
  474. end;
  475. end;
  476. procedure Tcgtypeconvnode.second_nil_to_methodprocvar;
  477. {$ifdef jvm}
  478. var r:Treference;
  479. {$endif}
  480. begin
  481. {$ifdef jvm}
  482. {$ifndef nounsupported}
  483. tg.gethltemp(current_asmdata.currasmlist,java_jlobject,java_jlobject.size,tt_normal,r);
  484. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,java_jlobject,0,r);
  485. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),1);
  486. location.reference:=r;
  487. exit;
  488. {$endif}
  489. {$endif}
  490. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  491. location.registerhi:=cg.getaddressregister(current_asmdata.currasmlist);
  492. cg.a_load_const_reg(current_asmdata.currasmlist,OS_ADDR,0,location.registerhi);
  493. location.register:=cg.getaddressregister(current_asmdata.currasmlist);
  494. cg.a_load_const_reg(current_asmdata.currasmlist,OS_ADDR,0,location.register);
  495. end;
  496. procedure tcgtypeconvnode.second_bool_to_int;
  497. var
  498. newsize: tcgsize;
  499. oldTrueLabel,oldFalseLabel : tasmlabel;
  500. begin
  501. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  502. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  503. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  504. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  505. secondpass(left);
  506. location_copy(location,left.location);
  507. newsize:=def_cgsize(resultdef);
  508. { byte(bytebool) or word(wordbool) or longint(longbool) must be }
  509. { accepted for var parameters and assignments, and must not }
  510. { change the ordinal value or value location. }
  511. { htypechk.valid_for_assign ensures that such locations with a }
  512. { size<sizeof(register) cannot be LOC_CREGISTER (they otherwise }
  513. { could be in case of a plain assignment), and LOC_REGISTER can }
  514. { never be an assignment target. The remaining LOC_REGISTER/ }
  515. { LOC_CREGISTER locations do have to be sign/zero-extended. }
  516. if not(nf_explicit in flags) or
  517. (location.loc in [LOC_FLAGS,LOC_JUMP]) or
  518. { change of size/signedness? Then we have to sign/ }
  519. { zero-extend in case of a loc_(c)register }
  520. ((newsize<>left.location.size) and
  521. ((left.resultdef.size<>resultdef.size) or
  522. not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]))) then
  523. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  524. else
  525. { may differ in sign, e.g. bytebool -> byte }
  526. location.size:=newsize;
  527. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  528. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  529. end;
  530. procedure tcgtypeconvnode.second_bool_to_bool;
  531. begin
  532. { we can reuse the conversion already available
  533. in bool_to_int to resize the value. But when the
  534. size of the new boolean is smaller we need to calculate
  535. the value as is done in int_to_bool. This is needed because
  536. the bits that define the true status can be outside the limits
  537. of the new size and truncating the register can result in a 0
  538. value }
  539. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  540. { a cbool must be converted to -1/0 }
  541. not is_cbool(resultdef) then
  542. begin
  543. secondpass(left);
  544. if (left.location.loc <> left.expectloc) then
  545. internalerror(2010081601);
  546. location_copy(location,left.location);
  547. end
  548. else if (resultdef.size=left.resultdef.size) and
  549. (is_cbool(resultdef)=is_cbool(left.resultdef)) then
  550. second_bool_to_int
  551. else
  552. begin
  553. if (resultdef.size<>left.resultdef.size) then
  554. { remove nf_explicit to perform full conversion if boolean sizes are different }
  555. exclude(flags, nf_explicit);
  556. second_int_to_bool;
  557. end;
  558. end;
  559. procedure tcgtypeconvnode.second_ansistring_to_pchar;
  560. var
  561. l1 : tasmlabel;
  562. hr : treference;
  563. begin
  564. location_reset(location,LOC_REGISTER,OS_ADDR);
  565. current_asmdata.getjumplabel(l1);
  566. case left.location.loc of
  567. LOC_CREGISTER,LOC_REGISTER:
  568. begin
  569. {$ifdef cpu_uses_separate_address_registers}
  570. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  571. begin
  572. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  573. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  574. left.location.register,location.register);
  575. end
  576. else
  577. {$endif}
  578. location.register := left.location.register;
  579. end;
  580. LOC_CREFERENCE,LOC_REFERENCE:
  581. begin
  582. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  583. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.register);
  584. end;
  585. else
  586. internalerror(2002032214);
  587. end;
  588. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_NE,0,location.register,l1);
  589. { FPC_EMPTYCHAR is a widechar -> 2 bytes }
  590. reference_reset(hr,2);
  591. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR');
  592. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hr,location.register);
  593. cg.a_label(current_asmdata.CurrAsmList,l1);
  594. end;
  595. procedure tcgtypeconvnode.second_class_to_intf;
  596. var
  597. l1 : tasmlabel;
  598. hd : tobjectdef;
  599. ImplIntf : TImplementedInterface;
  600. begin
  601. location_reset(location,LOC_REGISTER,OS_ADDR);
  602. case left.location.loc of
  603. LOC_CREFERENCE,
  604. LOC_REFERENCE:
  605. begin
  606. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  607. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.register);
  608. location_freetemp(current_asmdata.CurrAsmList,left.location);
  609. end;
  610. LOC_CREGISTER:
  611. begin
  612. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  613. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,location.register);
  614. end;
  615. LOC_REGISTER:
  616. location.register:=left.location.register;
  617. else
  618. internalerror(121120001);
  619. end;
  620. hd:=tobjectdef(left.resultdef);
  621. while assigned(hd) do
  622. begin
  623. ImplIntf:=hd.find_implemented_interface(tobjectdef(resultdef));
  624. if assigned(ImplIntf) then
  625. begin
  626. case ImplIntf.IType of
  627. etStandard:
  628. begin
  629. current_asmdata.getjumplabel(l1);
  630. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_EQ,0,location.register,l1);
  631. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,ImplIntf.ioffset,location.register);
  632. break;
  633. end;
  634. else
  635. internalerror(200802163);
  636. end;
  637. end;
  638. hd:=hd.childof;
  639. end;
  640. if hd=nil then
  641. internalerror(2002081301);
  642. cg.a_label(current_asmdata.CurrAsmList,l1);
  643. end;
  644. procedure tcgtypeconvnode.second_char_to_char;
  645. begin
  646. internalerror(2007081202);
  647. end;
  648. procedure tcgtypeconvnode.second_elem_to_openarray;
  649. begin
  650. { nothing special to do by default }
  651. second_nothing;
  652. end;
  653. procedure tcgtypeconvnode.second_nothing;
  654. var
  655. newsize : tcgsize;
  656. begin
  657. { we reuse the old value }
  658. location_copy(location,left.location);
  659. { Floats should never be returned as LOC_CONSTANT, do the
  660. moving to memory before the new size is set.
  661. Also when converting from a float to a non-float
  662. or the other way round, move to memory first to prevent
  663. invalid LOC_FPUREGISTER locations }
  664. if (
  665. (resultdef.typ=floatdef) and
  666. (location.loc=LOC_CONSTANT)
  667. ) or
  668. (
  669. (left.resultdef.typ=floatdef) xor
  670. (resultdef.typ=floatdef)
  671. ) then
  672. hlcg.location_force_mem(current_asmdata.CurrAsmList,location,left.resultdef);
  673. { but use the new size, but we don't know the size of all arrays }
  674. newsize:=def_cgsize(resultdef);
  675. location.size:=newsize;
  676. end;
  677. {$ifdef TESTOBJEXT2}
  678. procedure tcgtypeconvnode.checkobject;
  679. begin
  680. { no checking by default }
  681. end;
  682. {$endif TESTOBJEXT2}
  683. procedure tcgtypeconvnode.pass_generate_code;
  684. begin
  685. { the boolean routines can be called with LOC_JUMP and
  686. call secondpass themselves in the helper }
  687. if not(convtype in [tc_bool_2_int,tc_bool_2_bool,tc_int_2_bool]) then
  688. begin
  689. secondpass(left);
  690. if codegenerror then
  691. exit;
  692. end;
  693. second_call_helper(convtype);
  694. {$ifdef TESTOBJEXT2}
  695. { Check explicit conversions to objects pointers !! }
  696. if p^.explizit and
  697. (p^.resultdef.typ=pointerdef) and
  698. (tpointerdef(p^.resultdef).definition.typ=objectdef) and not
  699. (tobjectdef(tpointerdef(p^.resultdef).definition).isclass) and
  700. ((tobjectdef(tpointerdef(p^.resultdef).definition).options and oo_hasvmt)<>0) and
  701. (cs_check_range in current_settings.localswitches) then
  702. checkobject;
  703. {$endif TESTOBJEXT2}
  704. end;
  705. procedure tcgasnode.pass_generate_code;
  706. begin
  707. secondpass(call);
  708. location_copy(location,call.location);
  709. end;
  710. begin
  711. ctypeconvnode := tcgtypeconvnode;
  712. casnode := tcgasnode;
  713. end.