ncgcnv.pas 36 KB

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