ncgcnv.pas 33 KB

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