ncgcnv.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Generate assembler for nodes that handle type conversions which are
  5. the same for all (most) processors
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit ncgcnv;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. node,ncnv,types;
  24. type
  25. tcgtypeconvnode = class(ttypeconvnode)
  26. procedure second_int_to_int;override;
  27. procedure second_cstring_to_pchar;override;
  28. procedure second_string_to_chararray;override;
  29. procedure second_array_to_pointer;override;
  30. procedure second_pointer_to_array;override;
  31. procedure second_char_to_string;override;
  32. procedure second_real_to_real;override;
  33. procedure second_cord_to_pointer;override;
  34. procedure second_proc_to_procvar;override;
  35. procedure second_bool_to_int;override;
  36. procedure second_bool_to_bool;override;
  37. procedure second_ansistring_to_pchar;override;
  38. procedure second_class_to_intf;override;
  39. procedure second_char_to_char;override;
  40. procedure second_nothing;override;
  41. {$ifdef TESTOBJEXT2}
  42. procedure checkobject;virtual;
  43. {$endif TESTOBJEXT2}
  44. procedure second_call_helper(c : tconverttype);virtual;abstract;
  45. procedure pass_2;override;
  46. end;
  47. tcgasnode = class(tasnode)
  48. procedure pass_2;override;
  49. end;
  50. implementation
  51. uses
  52. cutils,verbose,
  53. aasmbase,aasmtai,aasmcpu,symconst,symdef,
  54. ncon,ncal,
  55. cpubase,cpuinfo,
  56. pass_2,
  57. cginfo,cgbase,
  58. cgobj,cgcpu,
  59. ncgutil,
  60. tgobj,rgobj
  61. ;
  62. procedure tcgtypeconvnode.second_int_to_int;
  63. var
  64. newsize : tcgsize;
  65. begin
  66. newsize:=def_cgsize(resulttype.def);
  67. { insert range check if not explicit conversion }
  68. if not(nf_explizit in flags) then
  69. cg.g_rangecheck(exprasmlist,left,resulttype.def);
  70. { is the result size smaller ? }
  71. if resulttype.def.size<>left.resulttype.def.size then
  72. begin
  73. { reuse the left location by default }
  74. location_copy(location,left.location);
  75. location_force_reg(exprasmlist,location,newsize,false);
  76. end
  77. else
  78. begin
  79. { no special loading is required, reuse current location }
  80. location_copy(location,left.location);
  81. location.size:=newsize;
  82. end;
  83. end;
  84. procedure tcgtypeconvnode.second_cstring_to_pchar;
  85. var
  86. hr : treference;
  87. begin
  88. location_release(exprasmlist,left.location);
  89. location_reset(location,LOC_REGISTER,OS_ADDR);
  90. case tstringdef(left.resulttype.def).string_typ of
  91. st_shortstring :
  92. begin
  93. inc(left.location.reference.offset);
  94. location.register:=rg.getregisterint(exprasmlist);
  95. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,location.register);
  96. end;
  97. st_ansistring :
  98. begin
  99. if (left.nodetype=stringconstn) and
  100. (str_length(left)=0) then
  101. begin
  102. reference_reset(hr);
  103. hr.symbol:=newasmsymbol('FPC_EMPTYCHAR');
  104. location.register:=rg.getregisterint(exprasmlist);
  105. cg.a_loadaddr_ref_reg(exprasmlist,hr,location.register);
  106. end
  107. else
  108. begin
  109. location.register:=rg.getregisterint(exprasmlist);
  110. cg.a_load_ref_reg(exprasmlist,OS_ADDR,left.location.reference,location.register);
  111. end;
  112. end;
  113. st_longstring:
  114. begin
  115. {!!!!!!!}
  116. internalerror(8888);
  117. end;
  118. st_widestring:
  119. begin
  120. if (left.nodetype=stringconstn) and
  121. (str_length(left)=0) then
  122. begin
  123. reference_reset(hr);
  124. hr.symbol:=newasmsymbol('FPC_EMPTYCHAR');
  125. location.register:=rg.getregisterint(exprasmlist);
  126. cg.a_loadaddr_ref_reg(exprasmlist,hr,location.register);
  127. end
  128. else
  129. begin
  130. location.register:=rg.getregisterint(exprasmlist);
  131. {$warning Todo: convert widestrings to ascii when typecasting them to pchars}
  132. cg.a_load_ref_reg(exprasmlist,OS_ADDR,left.location.reference,
  133. location.register);
  134. end;
  135. end;
  136. end;
  137. end;
  138. procedure tcgtypeconvnode.second_string_to_chararray;
  139. var
  140. arrsize: longint;
  141. begin
  142. with tarraydef(resulttype.def) do
  143. arrsize := highrange-lowrange+1;
  144. if (left.nodetype = stringconstn) and
  145. { left.length+1 since there's always a terminating #0 character (JM) }
  146. (tstringconstnode(left).len+1 >= arrsize) and
  147. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  148. begin
  149. location_copy(location,left.location);
  150. inc(location.reference.offset);
  151. exit;
  152. end
  153. else
  154. { should be handled already in resulttype pass (JM) }
  155. internalerror(200108292);
  156. end;
  157. procedure tcgtypeconvnode.second_array_to_pointer;
  158. begin
  159. location_release(exprasmlist,left.location);
  160. location_reset(location,LOC_REGISTER,OS_ADDR);
  161. location.register:=rg.getregisterint(exprasmlist);
  162. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,location.register);
  163. end;
  164. procedure tcgtypeconvnode.second_pointer_to_array;
  165. begin
  166. location_reset(location,LOC_REFERENCE,OS_NO);
  167. case left.location.loc of
  168. LOC_REGISTER :
  169. location.reference.base:=left.location.register;
  170. LOC_CREGISTER :
  171. begin
  172. location.reference.base:=rg.getregisterint(exprasmlist);
  173. cg.a_load_reg_reg(exprasmlist,OS_ADDR,left.location.register,
  174. location.reference.base);
  175. end;
  176. LOC_REFERENCE,
  177. LOC_CREFERENCE :
  178. begin
  179. location_release(exprasmlist,left.location);
  180. location.reference.base:=rg.getregisterint(exprasmlist);
  181. cg.a_load_ref_reg(exprasmlist,OS_ADDR,left.location.reference,
  182. location.reference.base);
  183. end;
  184. else
  185. internalerror(2002032216);
  186. end;
  187. end;
  188. procedure tcgtypeconvnode.second_char_to_string;
  189. begin
  190. location_reset(location,LOC_REFERENCE,OS_NO);
  191. case tstringdef(resulttype.def).string_typ of
  192. st_shortstring :
  193. begin
  194. tg.gettempofsizereference(exprasmlist,256,location.reference);
  195. cg.a_load_loc_ref(exprasmlist,left.location,
  196. location.reference);
  197. location_release(exprasmlist,left.location);
  198. end;
  199. { the rest is removed in the resulttype pass and converted to compilerprocs }
  200. else
  201. internalerror(4179);
  202. end;
  203. end;
  204. procedure tcgtypeconvnode.second_real_to_real;
  205. begin
  206. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  207. case left.location.loc of
  208. LOC_FPUREGISTER,
  209. LOC_CFPUREGISTER:
  210. begin
  211. location_copy(location,left.location);
  212. location.size:=def_cgsize(resulttype.def);
  213. exit;
  214. end;
  215. LOC_CREFERENCE,
  216. LOC_REFERENCE:
  217. begin
  218. location_release(exprasmlist,left.location);
  219. location.register:=rg.getregisterfpu(exprasmlist);
  220. cg.a_loadfpu_loc_reg(exprasmlist,left.location,location.register);
  221. end;
  222. else
  223. internalerror(2002032215);
  224. end;
  225. end;
  226. procedure tcgtypeconvnode.second_cord_to_pointer;
  227. begin
  228. { this can't happen because constants are already processed in
  229. pass 1 }
  230. internalerror(47423985);
  231. end;
  232. procedure tcgtypeconvnode.second_proc_to_procvar;
  233. begin
  234. { method pointer ? }
  235. if assigned(tunarynode(left).left) then
  236. begin
  237. location_copy(location,left.location);
  238. end
  239. else
  240. begin
  241. location_release(exprasmlist,left.location);
  242. location_reset(location,LOC_REGISTER,OS_ADDR);
  243. location.register:=rg.getregisterint(exprasmlist);
  244. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,location.register);
  245. end;
  246. end;
  247. procedure tcgtypeconvnode.second_bool_to_int;
  248. var
  249. oldtruelabel,oldfalselabel : tasmlabel;
  250. begin
  251. oldtruelabel:=truelabel;
  252. oldfalselabel:=falselabel;
  253. getlabel(truelabel);
  254. getlabel(falselabel);
  255. secondpass(left);
  256. location_copy(location,left.location);
  257. { byte(boolean) or word(wordbool) or longint(longbool) must }
  258. { be accepted for var parameters }
  259. if not((nf_explizit in flags) and
  260. (left.resulttype.def.size=resulttype.def.size) and
  261. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER])) then
  262. location_force_reg(exprasmlist,location,def_cgsize(resulttype.def),false);
  263. truelabel:=oldtruelabel;
  264. falselabel:=oldfalselabel;
  265. end;
  266. procedure tcgtypeconvnode.second_bool_to_bool;
  267. begin
  268. { we can reuse the conversion already available
  269. in bool_to_int to resize the value. But when the
  270. size of the new boolean is smaller we need to calculate
  271. the value as is done in int_to_bool. This is needed because
  272. the bits that define the true status can be outside the limits
  273. of the new size and truncating the register can result in a 0
  274. value }
  275. if resulttype.def.size<left.resulttype.def.size then
  276. second_int_to_bool
  277. else
  278. second_bool_to_int;
  279. end;
  280. procedure tcgtypeconvnode.second_ansistring_to_pchar;
  281. var
  282. l1 : tasmlabel;
  283. hr : treference;
  284. begin
  285. location_reset(location,LOC_REGISTER,OS_ADDR);
  286. getlabel(l1);
  287. case left.location.loc of
  288. LOC_CREGISTER,LOC_REGISTER:
  289. location.register:=left.location.register;
  290. LOC_CREFERENCE,LOC_REFERENCE:
  291. begin
  292. location_release(exprasmlist,left.location);
  293. location.register:=rg.getregisterint(exprasmlist);
  294. cg.a_load_ref_reg(exprasmlist,OS_32,left.location.reference,location.register);
  295. end;
  296. else
  297. internalerror(2002032214);
  298. end;
  299. cg.a_cmp_const_reg_label(exprasmlist,OS_32,OC_NE,0,location.register,l1);
  300. reference_reset(hr);
  301. hr.symbol:=newasmsymbol('FPC_EMPTYCHAR');
  302. cg.a_loadaddr_ref_reg(exprasmlist,hr,location.register);
  303. cg.a_label(exprasmlist,l1);
  304. end;
  305. procedure tcgtypeconvnode.second_class_to_intf;
  306. var
  307. l1 : tasmlabel;
  308. begin
  309. location_reset(location,LOC_REGISTER,OS_ADDR);
  310. case left.location.loc of
  311. LOC_CREFERENCE,
  312. LOC_REFERENCE:
  313. begin
  314. location_release(exprasmlist,left.location);
  315. location.register:=rg.getregisterint(exprasmlist);
  316. cg.a_load_ref_reg(exprasmlist,OS_ADDR,left.location.reference,location.register);
  317. end;
  318. LOC_CREGISTER:
  319. begin
  320. location.register:=rg.getregisterint(exprasmlist);
  321. cg.a_load_reg_reg(exprasmlist,OS_ADDR,left.location.register,location.register);
  322. end;
  323. LOC_REGISTER:
  324. location.register:=left.location.register;
  325. else
  326. internalerror(121120001);
  327. end;
  328. getlabel(l1);
  329. cg.a_cmp_const_reg_label(exprasmlist,OS_ADDR,OC_EQ,0,location.register,l1);
  330. cg.a_op_const_reg(exprasmlist,OP_ADD,aword(
  331. tobjectdef(left.resulttype.def).implementedinterfaces.ioffsets(
  332. tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(
  333. resulttype.def))^),location.register);
  334. cg.a_label(exprasmlist,l1);
  335. end;
  336. procedure tcgtypeconvnode.second_char_to_char;
  337. begin
  338. {$warning todo: add RTL routine for widechar-char conversion }
  339. { Quick hack to atleast generate 'working' code (PFV) }
  340. second_int_to_int;
  341. end;
  342. procedure tcgtypeconvnode.second_nothing;
  343. begin
  344. { we reuse the old value }
  345. location_copy(location,left.location);
  346. { Floats should never be returned as LOC_CONSTANT, do the
  347. moving to memory before the new size is set }
  348. if (resulttype.def.deftype=floatdef) and
  349. (location.loc=LOC_CONSTANT) then
  350. location_force_mem(exprasmlist,location);
  351. { but use the new size, but we don't know the size of all arrays }
  352. location.size:=def_cgsize(resulttype.def);
  353. end;
  354. {$ifdef TESTOBJEXT2}
  355. procedure tcgtypeconvnode.checkobject;
  356. begin
  357. { no checking by default }
  358. end;
  359. {$endif TESTOBJEXT2}
  360. procedure tcgtypeconvnode.pass_2;
  361. begin
  362. { the boolean routines can be called with LOC_JUMP and
  363. call secondpass themselves in the helper }
  364. if not(convtype in [tc_bool_2_int,tc_bool_2_bool,tc_int_2_bool]) then
  365. begin
  366. secondpass(left);
  367. if codegenerror then
  368. exit;
  369. end;
  370. second_call_helper(convtype);
  371. {$ifdef TESTOBJEXT2}
  372. { Check explicit conversions to objects pointers !! }
  373. if p^.explizit and
  374. (p^.resulttype.def.deftype=pointerdef) and
  375. (tpointerdef(p^.resulttype.def).definition.deftype=objectdef) and not
  376. (tobjectdef(tpointerdef(p^.resulttype.def).definition).isclass) and
  377. ((tobjectdef(tpointerdef(p^.resulttype.def).definition).options and oo_hasvmt)<>0) and
  378. (cs_check_range in aktlocalswitches) then
  379. checkobject;
  380. {$endif TESTOBJEXT2}
  381. end;
  382. procedure tcgasnode.pass_2;
  383. var
  384. pushed : tpushedsaved;
  385. begin
  386. if (right.nodetype=guidconstn) then
  387. begin
  388. {$warning need to push a third parameter}
  389. { instance to check }
  390. secondpass(left);
  391. rg.saveusedregisters(exprasmlist,pushed,all_registers);
  392. cg.a_param_loc(exprasmlist,left.location,2);
  393. { type information }
  394. secondpass(right);
  395. cg.a_paramaddr_ref(exprasmlist,right.location.reference,1);
  396. location_release(exprasmlist,right.location);
  397. { call helper }
  398. if is_class(left.resulttype.def) then
  399. cg.a_call_name(exprasmlist,'FPC_CLASS_AS_INTF')
  400. else
  401. cg.a_call_name(exprasmlist,'FPC_INTF_AS');
  402. cg.g_maybe_loadself(exprasmlist);
  403. rg.restoreusedregisters(exprasmlist,pushed);
  404. end
  405. else
  406. begin
  407. { instance to check }
  408. secondpass(left);
  409. rg.saveusedregisters(exprasmlist,pushed,all_registers);
  410. cg.a_param_loc(exprasmlist,left.location,2);
  411. { type information }
  412. secondpass(right);
  413. cg.a_param_loc(exprasmlist,right.location,1);
  414. location_release(exprasmlist,right.location);
  415. { call helper }
  416. cg.a_call_name(exprasmlist,'FPC_DO_AS');
  417. cg.g_maybe_loadself(exprasmlist);
  418. rg.restoreusedregisters(exprasmlist,pushed);
  419. end;
  420. location_copy(location,left.location);
  421. end;
  422. begin
  423. ctypeconvnode := tcgtypeconvnode;
  424. casnode := tcgasnode;
  425. end.
  426. {
  427. $Log$
  428. Revision 1.17 2002-07-01 18:46:22 peter
  429. * internal linker
  430. * reorganized aasm layer
  431. Revision 1.16 2002/07/01 16:23:53 peter
  432. * cg64 patch
  433. * basics for currency
  434. * asnode updates for class and interface (not finished)
  435. Revision 1.15 2002/05/18 13:34:09 peter
  436. * readded missing revisions
  437. Revision 1.14 2002/05/16 19:46:37 carl
  438. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  439. + try to fix temp allocation (still in ifdef)
  440. + generic constructor calls
  441. + start of tassembler / tmodulebase class cleanup
  442. Revision 1.12 2002/05/12 16:53:07 peter
  443. * moved entry and exitcode to ncgutil and cgobj
  444. * foreach gets extra argument for passing local data to the
  445. iterator function
  446. * -CR checks also class typecasts at runtime by changing them
  447. into as
  448. * fixed compiler to cycle with the -CR option
  449. * fixed stabs with elf writer, finally the global variables can
  450. be watched
  451. * removed a lot of routines from cga unit and replaced them by
  452. calls to cgobj
  453. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  454. u32bit then the other is typecasted also to u32bit without giving
  455. a rangecheck warning/error.
  456. * fixed pascal calling method with reversing also the high tree in
  457. the parast, detected by tcalcst3 test
  458. Revision 1.11 2002/04/21 19:02:03 peter
  459. * removed newn and disposen nodes, the code is now directly
  460. inlined from pexpr
  461. * -an option that will write the secondpass nodes to the .s file, this
  462. requires EXTDEBUG define to actually write the info
  463. * fixed various internal errors and crashes due recent code changes
  464. Revision 1.10 2002/04/19 15:39:34 peter
  465. * removed some more routines from cga
  466. * moved location_force_reg/mem to ncgutil
  467. * moved arrayconstructnode secondpass to ncgld
  468. Revision 1.9 2002/04/15 19:44:19 peter
  469. * fixed stackcheck that would be called recursively when a stack
  470. error was found
  471. * generic changeregsize(reg,size) for i386 register resizing
  472. * removed some more routines from cga unit
  473. * fixed returnvalue handling
  474. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  475. Revision 1.8 2002/04/06 18:10:42 jonas
  476. * several powerpc-related additions and fixes
  477. Revision 1.7 2002/04/04 19:05:57 peter
  478. * removed unused units
  479. * use tlocation.size in cg.a_*loc*() routines
  480. Revision 1.6 2002/04/02 17:11:28 peter
  481. * tlocation,treference update
  482. * LOC_CONSTANT added for better constant handling
  483. * secondadd splitted in multiple routines
  484. * location_force_reg added for loading a location to a register
  485. of a specified size
  486. * secondassignment parses now first the right and then the left node
  487. (this is compatible with Kylix). This saves a lot of push/pop especially
  488. with string operations
  489. * adapted some routines to use the new cg methods
  490. Revision 1.5 2002/03/31 20:26:34 jonas
  491. + a_loadfpu_* and a_loadmm_* methods in tcg
  492. * register allocation is now handled by a class and is mostly processor
  493. independent (+rgobj.pas and i386/rgcpu.pas)
  494. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  495. * some small improvements and fixes to the optimizer
  496. * some register allocation fixes
  497. * some fpuvaroffset fixes in the unary minus node
  498. * push/popusedregisters is now called rg.save/restoreusedregisters and
  499. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  500. also better optimizable)
  501. * fixed and optimized register saving/restoring for new/dispose nodes
  502. * LOC_FPU locations now also require their "register" field to be set to
  503. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  504. - list field removed of the tnode class because it's not used currently
  505. and can cause hard-to-find bugs
  506. }