ncgcnv.pas 30 KB

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