ncgcnv.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. {$elseif defined(sparcgen)}
  197. { Load left node into flag F_NE/F_E }
  198. resflags.Init(NR_ICC,F_NE);
  199. {$else}
  200. { Load left node into flag F_NE/F_E }
  201. resflags:=F_NE;
  202. {$endif defined(POWERPC) or defined(POWERPC64)}
  203. case left.location.loc of
  204. LOC_CREFERENCE,
  205. LOC_REFERENCE :
  206. begin
  207. if left.location.size in [OS_64,OS_S64] then
  208. begin
  209. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  210. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  211. href:=left.location.reference;
  212. inc(href.offset,4);
  213. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  214. end
  215. else
  216. begin
  217. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  218. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  219. end;
  220. end;
  221. LOC_FLAGS :
  222. begin
  223. resflags:=left.location.resflags;
  224. end;
  225. LOC_REGISTER,LOC_CREGISTER :
  226. begin
  227. {$if not defined(cpu64bitalu) and not defined(cpuhighleveltarget)}
  228. if left.location.size in [OS_64,OS_S64] then
  229. begin
  230. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  231. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  232. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  233. end
  234. else
  235. {$endif not cpu64bitalu and not cpuhighleveltarget}
  236. begin
  237. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  238. end;
  239. end;
  240. LOC_JUMP :
  241. begin
  242. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  243. current_asmdata.getjumplabel(hlabel);
  244. cg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  245. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  246. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  247. cg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  248. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  249. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  250. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  251. end;
  252. else
  253. internalerror(200311301);
  254. end;
  255. { load flags to register }
  256. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  257. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  258. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,location.register);
  259. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  260. if (is_cbool(resultdef)) then
  261. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,location.register,location.register);
  262. end;
  263. {$endif cpuflags}
  264. procedure tcgtypeconvnode.second_cstring_to_pchar;
  265. var
  266. hr : treference;
  267. begin
  268. if left.nodetype<>stringconstn then
  269. internalerror(200601131);
  270. if not is_pchar(resultdef) and not is_pwidechar(resultdef) then
  271. internalerror(2014032802);
  272. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  273. case tstringconstnode(left).cst_type of
  274. cst_conststring :
  275. begin
  276. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  277. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
  278. end;
  279. cst_shortstring :
  280. begin
  281. inc(left.location.reference.offset);
  282. location.reference.alignment:=1;
  283. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  284. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
  285. end;
  286. cst_widestring,
  287. cst_unicodestring,
  288. cst_ansistring :
  289. begin
  290. if tstringconstnode(left).len=0 then
  291. begin
  292. { FPC_EMPTYCHAR is a widechar -> 2 bytes }
  293. reference_reset(hr,2,[]);
  294. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR',AT_DATA,needs_indirect);
  295. current_module.add_extern_asmsym('FPC_EMPTYCHAR',AB_EXTERNAL,AT_DATA);
  296. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  297. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,cwidechartype,resultdef,hr,location.register);
  298. end
  299. else
  300. begin
  301. location_copy(location,left.location);
  302. end;
  303. end;
  304. cst_longstring:
  305. begin
  306. {!!!!!!!}
  307. internalerror(8888);
  308. end;
  309. else
  310. internalerror(200808241);
  311. end;
  312. end;
  313. procedure tcgtypeconvnode.second_cstring_to_int;
  314. begin
  315. { this can't happen because constants are already processed in
  316. pass 1 }
  317. internalerror(200510013);
  318. end;
  319. procedure tcgtypeconvnode.second_string_to_chararray;
  320. begin
  321. if is_chararray(left.resultdef) then
  322. begin
  323. location_copy(location,left.location);
  324. exit;
  325. end;
  326. { should be handled already in resultdef pass (JM) }
  327. internalerror(200108292);
  328. end;
  329. procedure tcgtypeconvnode.second_array_to_pointer;
  330. begin
  331. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  332. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  333. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
  334. end;
  335. procedure tcgtypeconvnode.second_pointer_to_array;
  336. begin
  337. { assume natural alignment, volatility of pointer has no effect on the volatility
  338. of the data it points to }
  339. location_reset_ref(location,LOC_REFERENCE,OS_NO,resultdef.alignment,[]);
  340. case left.location.loc of
  341. LOC_CREGISTER,
  342. LOC_REGISTER :
  343. begin
  344. {$ifdef cpu_uses_separate_address_registers}
  345. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  346. begin
  347. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  348. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  349. left.location.register,location.reference.base);
  350. end
  351. else
  352. {$endif}
  353. hlcg.reference_reset_base(location.reference,left.resultdef,left.location.register,0,ctempposinvalid,location.reference.alignment,location.reference.volatility);
  354. end;
  355. LOC_REFERENCE,
  356. LOC_CREFERENCE,
  357. { tricky type casting of parameters can cause these locations, see tb0593.pp on x86_64-linux }
  358. LOC_SUBSETREG,
  359. LOC_CSUBSETREG,
  360. LOC_SUBSETREF,
  361. LOC_CSUBSETREF:
  362. begin
  363. hlcg.reference_reset_base(location.reference,left.resultdef,
  364. hlcg.getaddressregister(current_asmdata.CurrAsmList,left.resultdef),0,ctempposinvalid,location.reference.alignment,[]);
  365. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,left.location,
  366. location.reference.base);
  367. if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  368. location_freetemp(current_asmdata.CurrAsmList,left.location);
  369. end;
  370. LOC_CONSTANT:
  371. begin
  372. location.reference.offset:=left.location.value;
  373. end
  374. else
  375. internalerror(2002032216);
  376. end;
  377. end;
  378. procedure tcgtypeconvnode.second_char_to_string;
  379. var
  380. tmpref: treference;
  381. begin
  382. location_reset_ref(location,LOC_REFERENCE,OS_NO,2,[]);
  383. case tstringdef(resultdef).stringtype of
  384. st_shortstring :
  385. begin
  386. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,resultdef.size,tt_normal,location.reference);
  387. tmpref:=location.reference;
  388. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,
  389. cpointerdef.getreusable(resultdef),
  390. cpointerdef.getreusable(left.resultdef),tmpref);
  391. hlcg.a_load_loc_ref(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,left.location,
  392. tmpref);
  393. location_freetemp(current_asmdata.CurrAsmList,left.location);
  394. end;
  395. { the rest is removed in the resultdef pass and converted to compilerprocs }
  396. else
  397. internalerror(4179);
  398. end;
  399. end;
  400. procedure tcgtypeconvnode.second_real_to_real;
  401. {$ifdef x86}
  402. var
  403. tr: treference;
  404. {$endif x86}
  405. begin
  406. location_reset(location,expectloc,def_cgsize(resultdef));
  407. {$ifdef x86}
  408. { extended types in memory which should be loaded into the sse unit
  409. must be converted by the fpu first, so force them to be loaded into
  410. the fpu }
  411. if (expectloc=LOC_MMREGISTER) and
  412. (left.location.size in [OS_F80,OS_C64]) then
  413. begin
  414. if (left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  415. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  416. { round them down to the proper precision }
  417. tg.gethltemp(current_asmdata.currasmlist,resultdef,resultdef.size,tt_normal,tr);
  418. hlcg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,tr);
  419. location_reset_ref(left.location,LOC_REFERENCE,location.size,tr.alignment,tr.volatility);
  420. left.location.reference:=tr;
  421. left.resultdef:=resultdef;
  422. end;
  423. {$endif x86}
  424. { ARM VFP values are in integer registers when they are function results }
  425. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  426. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  427. case left.location.loc of
  428. LOC_FPUREGISTER,
  429. LOC_CFPUREGISTER:
  430. begin
  431. case expectloc of
  432. LOC_FPUREGISTER:
  433. begin
  434. { on sparc a move from double -> single means from two to one register. }
  435. { On all other platforms it also needs rounding to avoid that }
  436. { single(double_regvar) = double_regvar is true in all cases }
  437. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  438. hlcg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
  439. end;
  440. LOC_MMREGISTER:
  441. begin
  442. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  443. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  444. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register,mms_movescalar);
  445. end
  446. else
  447. internalerror(2003012262);
  448. end;
  449. exit
  450. end;
  451. LOC_CREFERENCE,
  452. LOC_REFERENCE:
  453. begin
  454. if expectloc=LOC_MMREGISTER then
  455. begin
  456. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  457. hlcg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location,location.register,mms_movescalar)
  458. end
  459. else
  460. begin
  461. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  462. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  463. hlcg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
  464. end;
  465. location_freetemp(current_asmdata.CurrAsmList,left.location);
  466. end;
  467. LOC_MMREGISTER,
  468. LOC_CMMREGISTER:
  469. begin
  470. case expectloc of
  471. LOC_FPUREGISTER:
  472. begin
  473. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  474. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  475. hlcg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
  476. end;
  477. LOC_MMREGISTER:
  478. begin
  479. location.register:=hlcg.getmmregister(current_asmdata.CurrAsmList,resultdef);
  480. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register,mms_movescalar);
  481. end;
  482. else
  483. internalerror(2003012261);
  484. end;
  485. end;
  486. else
  487. internalerror(2002032215);
  488. end;
  489. end;
  490. procedure tcgtypeconvnode.second_cord_to_pointer;
  491. begin
  492. { this can't happen because constants are already processed in
  493. pass 1 }
  494. internalerror(47423985);
  495. end;
  496. procedure tcgtypeconvnode.second_proc_to_procvar;
  497. var
  498. href: treference;
  499. tmpreg: tregister;
  500. procvarrectype: trecorddef;
  501. procvarselfname: TIDString;
  502. begin
  503. if tabstractprocdef(resultdef).is_addressonly then
  504. begin
  505. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  506. { only a code pointer? (when taking the address of classtype.method
  507. we also only get a code pointer even though the resultdef is a
  508. procedure of object, and hence is_addressonly would return false)
  509. }
  510. if left.location.size = def_cgsize(tabstractprocdef(left.resultdef).address_type) then
  511. begin
  512. case left.location.loc of
  513. LOC_REFERENCE,LOC_CREFERENCE:
  514. begin
  515. { the procedure symbol is encoded in reference.symbol -> take address }
  516. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  517. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
  518. end;
  519. else
  520. internalerror(2013031501)
  521. end;
  522. end
  523. else
  524. begin
  525. { conversion from a procedure of object/nested procvar to plain procvar }
  526. case left.location.loc of
  527. LOC_REFERENCE,LOC_CREFERENCE:
  528. begin
  529. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  530. { code field is the first one }
  531. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,cpointerdef.getreusable(tprocvardef(tprocdef(left.resultdef).getcopyas(procvardef,pc_normal,''))),cpointerdef.getreusable(resultdef),left.location.reference);
  532. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,resultdef,resultdef,left.location.reference,location.register);
  533. end;
  534. LOC_REGISTER,LOC_CREGISTER:
  535. begin
  536. if target_info.endian=endian_little then
  537. location.register:=left.location.register
  538. else
  539. location.register:=left.location.registerhi;
  540. end;
  541. else
  542. internalerror(2013031502)
  543. end;
  544. end;
  545. end
  546. else
  547. begin
  548. if not tabstractprocdef(left.resultdef).is_addressonly then
  549. location_copy(location,left.location)
  550. else
  551. begin
  552. { assigning a global function to a nested procvar -> create
  553. tmethodpointer record and set the "frame pointer" to nil }
  554. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  555. internalerror(2013031503);
  556. location_reset_ref(location,LOC_REFERENCE,int_cgsize(resultdef.size),sizeof(pint),[]);
  557. tg.gethltemp(current_asmdata.CurrAsmList,resultdef,resultdef.size,tt_normal,location.reference);
  558. href:=location.reference;
  559. if is_nested_pd(tabstractprocdef(resultdef)) then
  560. begin
  561. procvarrectype:=trecorddef(nestedprocpointertype);
  562. procvarselfname:='parentfp';
  563. end
  564. else
  565. begin
  566. procvarrectype:=trecorddef(methodpointertype);
  567. procvarselfname:='self';
  568. end;
  569. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef),cpointerdef.getreusable(procvarrectype),href);
  570. tmpreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,voidcodepointertype);
  571. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,tprocdef(left.resultdef),voidcodepointertype,left.location.reference,tmpreg);
  572. hlcg.g_load_reg_field_by_name(current_asmdata.CurrAsmList,voidcodepointertype,trecorddef(procvarrectype),tmpreg,'proc',href);
  573. { setting the frame pointer to nil is not strictly necessary
  574. since the global procedure won't use it, but it can help with
  575. debugging }
  576. hlcg.g_load_const_field_by_name(current_asmdata.CurrAsmList,trecorddef(procvarrectype),0,procvarselfname,href);
  577. end;
  578. end;
  579. end;
  580. procedure Tcgtypeconvnode.second_nil_to_methodprocvar;
  581. begin
  582. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  583. location.registerhi:=hlcg.getaddressregister(current_asmdata.currasmlist,voidpointertype);
  584. hlcg.a_load_const_reg(current_asmdata.currasmlist,voidpointertype,0,location.registerhi);
  585. location.register:=hlcg.getaddressregister(current_asmdata.currasmlist,voidcodepointertype);
  586. hlcg.a_load_const_reg(current_asmdata.currasmlist,voidcodepointertype,0,location.register);
  587. end;
  588. procedure tcgtypeconvnode.second_bool_to_int;
  589. var
  590. newsize: tcgsize;
  591. begin
  592. secondpass(left);
  593. location_copy(location,left.location);
  594. newsize:=def_cgsize(resultdef);
  595. { byte(bytebool) or word(wordbool) or longint(longbool) must be }
  596. { accepted for var parameters and assignments, and must not }
  597. { change the ordinal value or value location. }
  598. { htypechk.valid_for_assign ensures that such locations with a }
  599. { size<sizeof(register) cannot be LOC_CREGISTER (they otherwise }
  600. { could be in case of a plain assignment), and LOC_REGISTER can }
  601. { never be an assignment target. The remaining LOC_REGISTER/ }
  602. { LOC_CREGISTER locations do have to be sign/zero-extended. }
  603. if not(nf_explicit in flags) or
  604. (location.loc in [LOC_FLAGS,LOC_JUMP]) or
  605. { change of size/signedness? Then we have to sign/ }
  606. { zero-extend in case of a loc_(c)register }
  607. ((newsize<>left.location.size) and
  608. ((left.resultdef.size<>resultdef.size) or
  609. not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]))) then
  610. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  611. else
  612. { may differ in sign, e.g. bytebool -> byte }
  613. location.size:=newsize;
  614. end;
  615. procedure tcgtypeconvnode.second_bool_to_bool;
  616. begin
  617. { we can reuse the conversion already available
  618. in bool_to_int to resize the value. But when the
  619. size of the new boolean is smaller we need to calculate
  620. the value as is done in int_to_bool. This is needed because
  621. the bits that define the true status can be outside the limits
  622. of the new size and truncating the register can result in a 0
  623. value }
  624. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) and
  625. { a cbool must be converted to -1/0 }
  626. not is_cbool(resultdef) then
  627. begin
  628. secondpass(left);
  629. if (left.location.loc <> left.expectloc) then
  630. internalerror(2010081601);
  631. location_copy(location,left.location);
  632. end
  633. else if (resultdef.size=left.resultdef.size) and
  634. (is_cbool(resultdef)=is_cbool(left.resultdef)) then
  635. second_bool_to_int
  636. else
  637. begin
  638. if (resultdef.size<>left.resultdef.size) then
  639. { remove nf_explicit to perform full conversion if boolean sizes are different }
  640. exclude(flags, nf_explicit);
  641. second_int_to_bool;
  642. end;
  643. end;
  644. procedure tcgtypeconvnode.second_ansistring_to_pchar;
  645. var
  646. l1 : tasmlabel;
  647. hr : treference;
  648. begin
  649. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  650. current_asmdata.getjumplabel(l1);
  651. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  652. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,
  653. left.location,location.register);
  654. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,resultdef,OC_NE,0,location.register,l1);
  655. { FPC_EMPTYCHAR is a widechar -> 2 bytes }
  656. reference_reset(hr,2,[]);
  657. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR',AT_DATA,needs_indirect);
  658. current_module.add_extern_asmsym('FPC_EMPTYCHAR',AB_EXTERNAL,AT_DATA);
  659. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,cwidechartype,resultdef,hr,location.register);
  660. hlcg.a_label(current_asmdata.CurrAsmList,l1);
  661. end;
  662. procedure tcgtypeconvnode.second_class_to_intf;
  663. var
  664. l1 : tasmlabel;
  665. hd : tobjectdef;
  666. ImplIntf : TImplementedInterface;
  667. begin
  668. l1:=nil;
  669. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  670. case left.location.loc of
  671. LOC_CREFERENCE,
  672. LOC_REFERENCE:
  673. begin
  674. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  675. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.reference,location.register);
  676. location_freetemp(current_asmdata.CurrAsmList,left.location);
  677. end;
  678. LOC_CREGISTER:
  679. begin
  680. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  681. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location.register,location.register);
  682. end;
  683. LOC_REGISTER:
  684. begin
  685. location.register:=left.location.register;
  686. hlcg.g_ptrtypecast_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,location.register);
  687. end;
  688. LOC_CONSTANT:
  689. begin
  690. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  691. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,resultdef,left.location.value,location.register);
  692. end
  693. else
  694. internalerror(121120001);
  695. end;
  696. hd:=tobjectdef(left.resultdef);
  697. while assigned(hd) do
  698. begin
  699. ImplIntf:=find_implemented_interface(hd,tobjectdef(resultdef));
  700. if assigned(ImplIntf) then
  701. begin
  702. case ImplIntf.IType of
  703. etStandard:
  704. begin
  705. current_asmdata.getjumplabel(l1);
  706. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,resultdef,OC_EQ,0,location.register,l1);
  707. hlcg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,resultdef,ImplIntf.ioffset,location.register);
  708. break;
  709. end;
  710. else
  711. internalerror(200802163);
  712. end;
  713. end;
  714. hd:=hd.childof;
  715. end;
  716. if hd=nil then
  717. internalerror(2002081301);
  718. if l1=nil then
  719. internalerror(2013120101);
  720. cg.a_label(current_asmdata.CurrAsmList,l1);
  721. end;
  722. procedure tcgtypeconvnode.second_char_to_char;
  723. begin
  724. internalerror(2007081202);
  725. end;
  726. procedure tcgtypeconvnode.second_elem_to_openarray;
  727. begin
  728. { nothing special to do by default }
  729. second_nothing;
  730. end;
  731. procedure tcgtypeconvnode.second_nothing;
  732. var
  733. newsize : tcgsize;
  734. begin
  735. { we reuse the old value }
  736. location_copy(location,left.location);
  737. { Floats should never be returned as LOC_CONSTANT, do the
  738. moving to memory before the new size is set.
  739. Also when converting from a float to a non-float
  740. move to memory first to prevent
  741. invalid LOC_(C)MM/FPUREGISTER locations }
  742. if (
  743. (resultdef.typ=floatdef) and
  744. (location.loc=LOC_CONSTANT)
  745. ) or
  746. ((resultdef.typ=floatdef) xor (location.loc in [LOC_CFPUREGISTER,LOC_FPUREGISTER,LOC_CMMREGISTER,LOC_MMREGISTER])) then
  747. hlcg.location_force_mem(current_asmdata.CurrAsmList,location,left.resultdef);
  748. { but use the new size, but we don't know the size of all arrays }
  749. newsize:=def_cgsize(resultdef);
  750. location.size:=newsize;
  751. end;
  752. {$ifdef TESTOBJEXT2}
  753. procedure tcgtypeconvnode.checkobject;
  754. begin
  755. { no checking by default }
  756. end;
  757. {$endif TESTOBJEXT2}
  758. procedure tcgtypeconvnode.pass_generate_code;
  759. begin
  760. { the boolean routines can be called with LOC_JUMP and
  761. call secondpass themselves in the helper }
  762. if not(convtype in [tc_bool_2_int,tc_bool_2_bool,tc_int_2_bool]) then
  763. begin
  764. secondpass(left);
  765. if codegenerror then
  766. exit;
  767. end;
  768. second_call_helper(convtype);
  769. {$ifdef TESTOBJEXT2}
  770. { Check explicit conversions to objects pointers !! }
  771. if p^.explizit and
  772. (p^.resultdef.typ=pointerdef) and
  773. (tpointerdef(p^.resultdef).definition.typ=objectdef) and not
  774. (tobjectdef(tpointerdef(p^.resultdef).definition).isclass) and
  775. ((tobjectdef(tpointerdef(p^.resultdef).definition).options and oo_hasvmt)<>0) and
  776. (cs_check_range in current_settings.localswitches) then
  777. checkobject;
  778. {$endif TESTOBJEXT2}
  779. end;
  780. procedure tcgasnode.pass_generate_code;
  781. begin
  782. secondpass(call);
  783. location_copy(location,call.location);
  784. end;
  785. begin
  786. ctypeconvnode := tcgtypeconvnode;
  787. casnode := tcgasnode;
  788. end.