ncgcnv.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. { only a code pointer? (when taking the address of classtype.method
  452. we also only get a code pointer even though the resultdef is a
  453. procedure of object, and hence is_addressonly would return false)
  454. }
  455. if left.location.size = OS_ADDR then
  456. begin
  457. case left.location.loc of
  458. LOC_REFERENCE,LOC_CREFERENCE:
  459. begin
  460. { the procedure symbol is encoded in reference.symbol -> take address }
  461. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  462. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  463. end;
  464. else
  465. internalerror(2013031501)
  466. end;
  467. end
  468. else
  469. begin
  470. { conversion from a procedure of object/nested procvar to plain procvar }
  471. case left.location.loc of
  472. LOC_REFERENCE,LOC_CREFERENCE:
  473. begin
  474. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  475. { code field is the first one }
  476. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.register);
  477. end;
  478. LOC_REGISTER,LOC_CREGISTER:
  479. begin
  480. if target_info.endian=endian_little then
  481. location.register:=left.location.register
  482. else
  483. location.register:=left.location.registerhi;
  484. end;
  485. else
  486. internalerror(2013031502)
  487. end;
  488. end;
  489. end
  490. else
  491. begin
  492. if not tabstractprocdef(left.resultdef).is_addressonly then
  493. location_copy(location,left.location)
  494. else
  495. begin
  496. { assigning a global function to a nested procvar -> create
  497. tmethodpointer record and set the "frame pointer" to nil }
  498. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  499. internalerror(2013031503);
  500. location_reset_ref(location,LOC_REFERENCE,int_cgsize(sizeof(pint)*2),sizeof(pint));
  501. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,resultdef.size,tt_normal,location.reference);
  502. tmpreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  503. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,tmpreg);
  504. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,tmpreg,location.reference);
  505. { setting the frame pointer to nil is not strictly necessary
  506. since the global procedure won't use it, but it can help with
  507. debugging }
  508. inc(location.reference.offset,sizeof(pint));
  509. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_ADDR,0,location.reference);
  510. dec(location.reference.offset,sizeof(pint));
  511. end;
  512. end;
  513. end;
  514. procedure Tcgtypeconvnode.second_nil_to_methodprocvar;
  515. {$ifdef jvm}
  516. var r:Treference;
  517. {$endif}
  518. begin
  519. {$ifdef jvm}
  520. {$ifndef nounsupported}
  521. tg.gethltemp(current_asmdata.currasmlist,java_jlobject,java_jlobject.size,tt_normal,r);
  522. hlcg.a_load_const_ref(current_asmdata.CurrAsmList,java_jlobject,0,r);
  523. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),1);
  524. location.reference:=r;
  525. exit;
  526. {$endif}
  527. {$endif}
  528. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  529. location.registerhi:=cg.getaddressregister(current_asmdata.currasmlist);
  530. cg.a_load_const_reg(current_asmdata.currasmlist,OS_ADDR,0,location.registerhi);
  531. location.register:=cg.getaddressregister(current_asmdata.currasmlist);
  532. cg.a_load_const_reg(current_asmdata.currasmlist,OS_ADDR,0,location.register);
  533. end;
  534. procedure tcgtypeconvnode.second_bool_to_int;
  535. var
  536. newsize: tcgsize;
  537. oldTrueLabel,oldFalseLabel : tasmlabel;
  538. begin
  539. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  540. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  541. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  542. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  543. secondpass(left);
  544. location_copy(location,left.location);
  545. newsize:=def_cgsize(resultdef);
  546. { byte(bytebool) or word(wordbool) or longint(longbool) must be }
  547. { accepted for var parameters and assignments, and must not }
  548. { change the ordinal value or value location. }
  549. { htypechk.valid_for_assign ensures that such locations with a }
  550. { size<sizeof(register) cannot be LOC_CREGISTER (they otherwise }
  551. { could be in case of a plain assignment), and LOC_REGISTER can }
  552. { never be an assignment target. The remaining LOC_REGISTER/ }
  553. { LOC_CREGISTER locations do have to be sign/zero-extended. }
  554. if not(nf_explicit in flags) or
  555. (location.loc in [LOC_FLAGS,LOC_JUMP]) or
  556. { change of size/signedness? Then we have to sign/ }
  557. { zero-extend in case of a loc_(c)register }
  558. ((newsize<>left.location.size) and
  559. ((left.resultdef.size<>resultdef.size) or
  560. not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]))) then
  561. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  562. else
  563. { may differ in sign, e.g. bytebool -> byte }
  564. location.size:=newsize;
  565. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  566. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  567. end;
  568. procedure tcgtypeconvnode.second_bool_to_bool;
  569. begin
  570. { we can reuse the conversion already available
  571. in bool_to_int to resize the value. But when the
  572. size of the new boolean is smaller we need to calculate
  573. the value as is done in int_to_bool. This is needed because
  574. the bits that define the true status can be outside the limits
  575. of the new size and truncating the register can result in a 0
  576. value }
  577. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  578. { a cbool must be converted to -1/0 }
  579. not is_cbool(resultdef) then
  580. begin
  581. secondpass(left);
  582. if (left.location.loc <> left.expectloc) then
  583. internalerror(2010081601);
  584. location_copy(location,left.location);
  585. end
  586. else if (resultdef.size=left.resultdef.size) and
  587. (is_cbool(resultdef)=is_cbool(left.resultdef)) then
  588. second_bool_to_int
  589. else
  590. begin
  591. if (resultdef.size<>left.resultdef.size) then
  592. { remove nf_explicit to perform full conversion if boolean sizes are different }
  593. exclude(flags, nf_explicit);
  594. second_int_to_bool;
  595. end;
  596. end;
  597. procedure tcgtypeconvnode.second_ansistring_to_pchar;
  598. var
  599. l1 : tasmlabel;
  600. hr : treference;
  601. begin
  602. location_reset(location,LOC_REGISTER,OS_ADDR);
  603. current_asmdata.getjumplabel(l1);
  604. case left.location.loc of
  605. LOC_CREGISTER,LOC_REGISTER:
  606. begin
  607. {$ifdef cpu_uses_separate_address_registers}
  608. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  609. begin
  610. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  611. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  612. left.location.register,location.register);
  613. end
  614. else
  615. {$endif}
  616. location.register := left.location.register;
  617. end;
  618. LOC_CREFERENCE,LOC_REFERENCE:
  619. begin
  620. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  621. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.register);
  622. end;
  623. else
  624. internalerror(2002032214);
  625. end;
  626. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_NE,0,location.register,l1);
  627. { FPC_EMPTYCHAR is a widechar -> 2 bytes }
  628. reference_reset(hr,2);
  629. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR');
  630. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hr,location.register);
  631. cg.a_label(current_asmdata.CurrAsmList,l1);
  632. end;
  633. procedure tcgtypeconvnode.second_class_to_intf;
  634. var
  635. l1 : tasmlabel;
  636. hd : tobjectdef;
  637. ImplIntf : TImplementedInterface;
  638. begin
  639. location_reset(location,LOC_REGISTER,OS_ADDR);
  640. case left.location.loc of
  641. LOC_CREFERENCE,
  642. LOC_REFERENCE:
  643. begin
  644. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  645. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.register);
  646. location_freetemp(current_asmdata.CurrAsmList,left.location);
  647. end;
  648. LOC_CREGISTER:
  649. begin
  650. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  651. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,location.register);
  652. end;
  653. LOC_REGISTER:
  654. location.register:=left.location.register;
  655. else
  656. internalerror(121120001);
  657. end;
  658. hd:=tobjectdef(left.resultdef);
  659. while assigned(hd) do
  660. begin
  661. ImplIntf:=hd.find_implemented_interface(tobjectdef(resultdef));
  662. if assigned(ImplIntf) then
  663. begin
  664. case ImplIntf.IType of
  665. etStandard:
  666. begin
  667. current_asmdata.getjumplabel(l1);
  668. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_EQ,0,location.register,l1);
  669. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,ImplIntf.ioffset,location.register);
  670. break;
  671. end;
  672. else
  673. internalerror(200802163);
  674. end;
  675. end;
  676. hd:=hd.childof;
  677. end;
  678. if hd=nil then
  679. internalerror(2002081301);
  680. cg.a_label(current_asmdata.CurrAsmList,l1);
  681. end;
  682. procedure tcgtypeconvnode.second_char_to_char;
  683. begin
  684. internalerror(2007081202);
  685. end;
  686. procedure tcgtypeconvnode.second_elem_to_openarray;
  687. begin
  688. { nothing special to do by default }
  689. second_nothing;
  690. end;
  691. procedure tcgtypeconvnode.second_nothing;
  692. var
  693. newsize : tcgsize;
  694. begin
  695. { we reuse the old value }
  696. location_copy(location,left.location);
  697. { Floats should never be returned as LOC_CONSTANT, do the
  698. moving to memory before the new size is set.
  699. Also when converting from a float to a non-float
  700. or the other way round, move to memory first to prevent
  701. invalid LOC_FPUREGISTER locations }
  702. if (
  703. (resultdef.typ=floatdef) and
  704. (location.loc=LOC_CONSTANT)
  705. ) or
  706. (
  707. (left.resultdef.typ=floatdef) xor
  708. (resultdef.typ=floatdef)
  709. ) then
  710. hlcg.location_force_mem(current_asmdata.CurrAsmList,location,left.resultdef);
  711. { but use the new size, but we don't know the size of all arrays }
  712. newsize:=def_cgsize(resultdef);
  713. location.size:=newsize;
  714. end;
  715. {$ifdef TESTOBJEXT2}
  716. procedure tcgtypeconvnode.checkobject;
  717. begin
  718. { no checking by default }
  719. end;
  720. {$endif TESTOBJEXT2}
  721. procedure tcgtypeconvnode.pass_generate_code;
  722. begin
  723. { the boolean routines can be called with LOC_JUMP and
  724. call secondpass themselves in the helper }
  725. if not(convtype in [tc_bool_2_int,tc_bool_2_bool,tc_int_2_bool]) then
  726. begin
  727. secondpass(left);
  728. if codegenerror then
  729. exit;
  730. end;
  731. second_call_helper(convtype);
  732. {$ifdef TESTOBJEXT2}
  733. { Check explicit conversions to objects pointers !! }
  734. if p^.explizit and
  735. (p^.resultdef.typ=pointerdef) and
  736. (tpointerdef(p^.resultdef).definition.typ=objectdef) and not
  737. (tobjectdef(tpointerdef(p^.resultdef).definition).isclass) and
  738. ((tobjectdef(tpointerdef(p^.resultdef).definition).options and oo_hasvmt)<>0) and
  739. (cs_check_range in current_settings.localswitches) then
  740. checkobject;
  741. {$endif TESTOBJEXT2}
  742. end;
  743. procedure tcgasnode.pass_generate_code;
  744. begin
  745. secondpass(call);
  746. location_copy(location,call.location);
  747. end;
  748. begin
  749. ctypeconvnode := tcgtypeconvnode;
  750. casnode := tcgasnode;
  751. end.