ncgcnv.pas 34 KB

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