ncgmem.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate assembler for memory related nodes 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. { This unit generate assembler for memory related nodes.
  20. }
  21. unit ncgmem;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. cpuinfo,cpubase,
  26. node,nmem;
  27. type
  28. tcgloadvmtnode = class(tloadvmtnode)
  29. procedure pass_2;override;
  30. end;
  31. tcghnewnode = class(thnewnode)
  32. procedure pass_2;override;
  33. end;
  34. tcghdisposenode = class(thdisposenode)
  35. procedure pass_2;override;
  36. end;
  37. tcgaddrnode = class(taddrnode)
  38. procedure pass_2;override;
  39. end;
  40. tcgdoubleaddrnode = class(tdoubleaddrnode)
  41. procedure pass_2;override;
  42. end;
  43. tcgderefnode = class(tderefnode)
  44. procedure pass_2;override;
  45. end;
  46. tcgsubscriptnode = class(tsubscriptnode)
  47. procedure pass_2;override;
  48. end;
  49. tcgselfnode = class(tselfnode)
  50. procedure pass_2;override;
  51. end;
  52. tcgwithnode = class(twithnode)
  53. procedure pass_2;override;
  54. end;
  55. tcgvecnode = class(tvecnode)
  56. private
  57. procedure rangecheck_array;
  58. protected
  59. function get_mul_size : longint;
  60. {# This routine is used to calculate the address of the reference.
  61. On entry reg contains the index in the array,
  62. and l contains the size of each element in the array.
  63. This routine should update location.reference correctly,
  64. so it points to the correct address.
  65. }
  66. procedure update_reference_reg_mul(reg:tregister;l:aword);virtual;
  67. procedure second_wideansistring;virtual;
  68. procedure second_dynamicarray;virtual;
  69. public
  70. procedure pass_2;override;
  71. end;
  72. implementation
  73. uses
  74. {$ifdef delphi}
  75. sysutils,
  76. {$else}
  77. strings,
  78. {$endif}
  79. {$ifdef GDB}
  80. gdb,
  81. {$endif GDB}
  82. globtype,systems,
  83. cutils,verbose,globals,
  84. symconst,symtype,symdef,symsym,symtable,defutil,paramgr,
  85. aasmbase,aasmtai,aasmcpu,
  86. cginfo,cgbase,pass_2,
  87. pass_1,nld,ncon,nadd,
  88. cgobj,tgobj,rgobj,ncgutil,symbase
  89. ;
  90. {*****************************************************************************
  91. TCGLOADNODE
  92. *****************************************************************************}
  93. procedure tcgloadvmtnode.pass_2;
  94. var
  95. href : treference;
  96. begin
  97. location_reset(location,LOC_REGISTER,OS_ADDR);
  98. location.register:=rg.getaddressregister(exprasmlist);
  99. { on 80386, LEA is the same as mov imm32 }
  100. reference_reset_symbol(href,
  101. objectlibrary.newasmsymbol(tobjectdef(tclassrefdef(resulttype.def).pointertype.def).vmt_mangledname),0);
  102. cg.a_loadaddr_ref_reg(exprasmlist,href,location.register);
  103. end;
  104. {*****************************************************************************
  105. TCGHNEWNODE
  106. *****************************************************************************}
  107. procedure tcghnewnode.pass_2;
  108. begin
  109. { completely resolved in first pass now }
  110. end;
  111. {*****************************************************************************
  112. TCGHDISPOSENODE
  113. *****************************************************************************}
  114. procedure tcghdisposenode.pass_2;
  115. begin
  116. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  117. secondpass(left);
  118. if codegenerror then
  119. exit;
  120. case left.location.loc of
  121. LOC_REGISTER:
  122. begin
  123. if not rg.isaddressregister(left.location.register) then
  124. begin
  125. location_release(exprasmlist,left.location);
  126. location.reference.base := rg.getaddressregister(exprasmlist);
  127. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.register,
  128. location.reference.base);
  129. end
  130. else
  131. location.reference.base := left.location.register;
  132. end;
  133. LOC_CREGISTER,
  134. LOC_CREFERENCE,
  135. LOC_REFERENCE:
  136. begin
  137. location_release(exprasmlist,left.location);
  138. location.reference.base:=rg.getaddressregister(exprasmlist);
  139. cg.a_load_loc_reg(exprasmlist,left.location,location.reference.base);
  140. end;
  141. else
  142. internalerror(2002032217);
  143. end;
  144. end;
  145. {*****************************************************************************
  146. TCGADDRNODE
  147. *****************************************************************************}
  148. procedure tcgaddrnode.pass_2;
  149. begin
  150. secondpass(left);
  151. { when loading procvar we do nothing with this node, so load the
  152. location of left }
  153. if nf_procvarload in flags then
  154. begin
  155. location_copy(location,left.location);
  156. exit;
  157. end;
  158. location_release(exprasmlist,left.location);
  159. location_reset(location,LOC_REGISTER,OS_ADDR);
  160. location.register:=rg.getaddressregister(exprasmlist);
  161. {@ on a procvar means returning an address to the procedure that
  162. is stored in it.}
  163. { yes but left.symtableentry can be nil
  164. for example on self !! }
  165. { symtableentry can be also invalid, if left is no tree node }
  166. if (m_tp_procvar in aktmodeswitches) and
  167. (left.nodetype=loadn) and
  168. assigned(tloadnode(left).symtableentry) and
  169. (tloadnode(left).symtableentry.typ=varsym) and
  170. (tvarsym(tloadnode(left).symtableentry).vartype.def.deftype=procvardef) then
  171. cg.a_load_ref_reg(exprasmlist,OS_ADDR,left.location.reference,
  172. location.register)
  173. else
  174. begin
  175. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,
  176. location.register);
  177. end;
  178. end;
  179. {*****************************************************************************
  180. TCGDOUBLEADDRNODE
  181. *****************************************************************************}
  182. procedure tcgdoubleaddrnode.pass_2;
  183. begin
  184. secondpass(left);
  185. location_release(exprasmlist,left.location);
  186. location_reset(location,LOC_REGISTER,OS_ADDR);
  187. location.register:=rg.getaddressregister(exprasmlist);
  188. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,
  189. location.register);
  190. end;
  191. {*****************************************************************************
  192. TCGDEREFNODE
  193. *****************************************************************************}
  194. procedure tcgderefnode.pass_2;
  195. begin
  196. secondpass(left);
  197. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  198. case left.location.loc of
  199. LOC_REGISTER:
  200. begin
  201. if not rg.isaddressregister(left.location.register) then
  202. begin
  203. location_release(exprasmlist,left.location);
  204. location.reference.base := rg.getaddressregister(exprasmlist);
  205. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.register,
  206. location.reference.base);
  207. end
  208. else
  209. location.reference.base := left.location.register;
  210. end;
  211. LOC_CREGISTER,
  212. LOC_CREFERENCE,
  213. LOC_REFERENCE:
  214. begin
  215. location_release(exprasmlist,left.location);
  216. location.reference.base:=rg.getaddressregister(exprasmlist);
  217. cg.a_load_loc_reg(exprasmlist,left.location,location.reference.base);
  218. end;
  219. end;
  220. if (cs_gdb_heaptrc in aktglobalswitches) and
  221. (cs_checkpointer in aktglobalswitches) and
  222. not(cs_compilesystem in aktmoduleswitches) and
  223. (not tpointerdef(left.resulttype.def).is_far) then
  224. begin
  225. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paramanager.getintparaloc(1));
  226. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  227. end;
  228. end;
  229. {*****************************************************************************
  230. TCGSUBSCRIPTNODE
  231. *****************************************************************************}
  232. procedure tcgsubscriptnode.pass_2;
  233. begin
  234. secondpass(left);
  235. if codegenerror then
  236. exit;
  237. { classes and interfaces must be dereferenced implicit }
  238. if is_class_or_interface(left.resulttype.def) then
  239. begin
  240. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  241. case left.location.loc of
  242. LOC_REGISTER:
  243. begin
  244. if not rg.isaddressregister(left.location.register) then
  245. begin
  246. location_release(exprasmlist,left.location);
  247. location.reference.base:=rg.getaddressregister(exprasmlist);
  248. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,
  249. left.location.register,location.reference.base);
  250. end
  251. else
  252. location.reference.base := left.location.register;
  253. end;
  254. LOC_CREGISTER,
  255. LOC_CREFERENCE,
  256. LOC_REFERENCE:
  257. begin
  258. location_release(exprasmlist,left.location);
  259. location.reference.base:=rg.getaddressregister(exprasmlist);
  260. cg.a_load_loc_reg(exprasmlist,left.location,location.reference.base);
  261. end;
  262. end;
  263. { implicit deferencing }
  264. if (cs_gdb_heaptrc in aktglobalswitches) and
  265. (cs_checkpointer in aktglobalswitches) and
  266. not(cs_compilesystem in aktmoduleswitches) then
  267. begin
  268. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paramanager.getintparaloc(1));
  269. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  270. end;
  271. end
  272. else if is_interfacecom(left.resulttype.def) then
  273. begin
  274. tg.GetTemp(exprasmlist,pointer_size,tt_interfacecom,location.reference);
  275. cg.a_load_loc_ref(exprasmlist,left.location,location.reference);
  276. { implicit deferencing also for interfaces }
  277. if (cs_gdb_heaptrc in aktglobalswitches) and
  278. (cs_checkpointer in aktglobalswitches) and
  279. not(cs_compilesystem in aktmoduleswitches) then
  280. begin
  281. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paramanager.getintparaloc(1));
  282. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  283. end;
  284. end
  285. else
  286. location_copy(location,left.location);
  287. inc(location.reference.offset,vs.address);
  288. { also update the size of the location }
  289. location.size:=def_cgsize(resulttype.def);
  290. end;
  291. {*****************************************************************************
  292. TCGSELFNODE
  293. *****************************************************************************}
  294. procedure tcgselfnode.pass_2;
  295. begin
  296. rg.getexplicitregisterint(exprasmlist,SELF_POINTER_REG);
  297. if (resulttype.def.deftype=classrefdef) or
  298. is_class(resulttype.def) then
  299. begin
  300. location_reset(location,LOC_CREGISTER,OS_ADDR);
  301. location.register:=SELF_POINTER_REG;
  302. end
  303. else
  304. begin
  305. location_reset(location,LOC_CREFERENCE,OS_ADDR);
  306. location.reference.base:=SELF_POINTER_REG;
  307. end;
  308. end;
  309. {*****************************************************************************
  310. TCGWITHNODE
  311. *****************************************************************************}
  312. procedure tcgwithnode.pass_2;
  313. var
  314. tmpreg: tregister;
  315. usetemp,with_expr_in_temp : boolean;
  316. symtable : tsymtable;
  317. i : integer;
  318. {$ifdef GDB}
  319. withstartlabel,withendlabel : tasmlabel;
  320. pp : pchar;
  321. mangled_length : longint;
  322. const
  323. withlevel : longint = 0;
  324. {$endif GDB}
  325. begin
  326. if assigned(left) then
  327. begin
  328. secondpass(left);
  329. {$ifdef i386}
  330. if (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  331. (left.location.reference.segment<>R_NO) then
  332. message(parser_e_no_with_for_variable_in_other_segments);
  333. {$endif i386}
  334. reference_reset(withreference);
  335. usetemp:=false;
  336. if (left.nodetype=loadn) and
  337. (tloadnode(left).symtable=aktprocdef.localst) then
  338. begin
  339. { for locals use the local storage }
  340. withreference:=left.location.reference;
  341. include(flags,nf_islocal);
  342. end
  343. else
  344. { call can have happend with a property }
  345. begin
  346. usetemp:=true;
  347. if is_class_or_interface(left.resulttype.def) then
  348. begin
  349. tmpreg := cg.get_scratch_reg_int(exprasmlist);
  350. cg.a_load_loc_reg(exprasmlist,left.location,tmpreg)
  351. end
  352. else
  353. begin
  354. tmpreg := cg.get_scratch_reg_address(exprasmlist);
  355. cg.a_loadaddr_ref_reg(exprasmlist,
  356. left.location.reference,tmpreg);
  357. end;
  358. end;
  359. location_release(exprasmlist,left.location);
  360. symtable:=withsymtable;
  361. for i:=1 to tablecount do
  362. begin
  363. if (left.nodetype=loadn) and
  364. (tloadnode(left).symtable=aktprocdef.localst) then
  365. twithsymtable(symtable).direct_with:=true;
  366. twithsymtable(symtable).withnode:=self;
  367. symtable:=symtable.next;
  368. end;
  369. { if the with expression is stored in a temp }
  370. { area we must make it persistent and shouldn't }
  371. { release it (FK) }
  372. if (left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  373. tg.istemp(left.location.reference) then
  374. with_expr_in_temp:=tg.ChangeTempType(exprasmlist,left.location.reference,tt_persistant)
  375. else
  376. with_expr_in_temp:=false;
  377. { if usetemp is set the value must be in tmpreg }
  378. if usetemp then
  379. begin
  380. tg.GetTemp(exprasmlist,pointer_size,tt_persistant,withreference);
  381. { move to temp reference }
  382. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,withreference);
  383. cg.free_scratch_reg(exprasmlist,tmpreg);
  384. {$ifdef GDB}
  385. if (cs_debuginfo in aktmoduleswitches) then
  386. begin
  387. inc(withlevel);
  388. objectlibrary.getaddrlabel(withstartlabel);
  389. objectlibrary.getaddrlabel(withendlabel);
  390. cg.a_label(exprasmlist,withstartlabel);
  391. withdebugList.concat(Tai_stabs.Create(strpnew(
  392. '"with'+tostr(withlevel)+':'+tostr(symtablestack.getnewtypecount)+
  393. '=*'+tstoreddef(left.resulttype.def).numberstring+'",'+
  394. tostr(N_LSYM)+',0,0,'+tostr(withreference.offset))));
  395. mangled_length:=length(aktprocdef.mangledname);
  396. getmem(pp,mangled_length+50);
  397. strpcopy(pp,'192,0,0,'+withstartlabel.name);
  398. if (target_info.use_function_relative_addresses) then
  399. begin
  400. strpcopy(strend(pp),'-');
  401. strpcopy(strend(pp),aktprocdef.mangledname);
  402. end;
  403. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  404. end;
  405. {$endif GDB}
  406. end;
  407. { right can be optimize out !!! }
  408. if assigned(right) then
  409. secondpass(right);
  410. if usetemp then
  411. begin
  412. tg.UnGetTemp(exprasmlist,withreference);
  413. {$ifdef GDB}
  414. if (cs_debuginfo in aktmoduleswitches) then
  415. begin
  416. cg.a_label(exprasmlist,withendlabel);
  417. strpcopy(pp,'224,0,0,'+withendlabel.name);
  418. if (target_info.use_function_relative_addresses) then
  419. begin
  420. strpcopy(strend(pp),'-');
  421. strpcopy(strend(pp),aktprocdef.mangledname);
  422. end;
  423. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  424. freemem(pp,mangled_length+50);
  425. dec(withlevel);
  426. end;
  427. {$endif GDB}
  428. end;
  429. if with_expr_in_temp then
  430. tg.UnGetTemp(exprasmlist,left.location.reference);
  431. reference_reset(withreference);
  432. end;
  433. end;
  434. {*****************************************************************************
  435. TCGVECNODE
  436. *****************************************************************************}
  437. function tcgvecnode.get_mul_size : longint;
  438. begin
  439. if nf_memindex in flags then
  440. get_mul_size:=1
  441. else
  442. begin
  443. if (left.resulttype.def.deftype=arraydef) then
  444. get_mul_size:=tarraydef(left.resulttype.def).elesize
  445. else
  446. get_mul_size:=resulttype.def.size;
  447. end
  448. end;
  449. procedure tcgvecnode.update_reference_reg_mul(reg:tregister;l:aword);
  450. begin
  451. if location.reference.base=R_NO then
  452. begin
  453. cg.a_op_const_reg(exprasmlist,OP_IMUL,l,reg);
  454. location.reference.base:=reg;
  455. end
  456. else if location.reference.index=R_NO then
  457. begin
  458. cg.a_op_const_reg(exprasmlist,OP_IMUL,l,reg);
  459. location.reference.index:=reg;
  460. end
  461. else
  462. begin
  463. cg.a_loadaddr_ref_reg(exprasmlist,location.reference,location.reference.index);
  464. rg.ungetregisterint(exprasmlist,location.reference.base);
  465. reference_reset_base(location.reference,location.reference.index,0);
  466. { insert new index register }
  467. cg.a_op_const_reg(exprasmlist,OP_IMUL,l,reg);
  468. location.reference.index:=reg;
  469. end;
  470. end;
  471. procedure tcgvecnode.second_wideansistring;
  472. begin
  473. end;
  474. procedure tcgvecnode.second_dynamicarray;
  475. begin
  476. end;
  477. procedure tcgvecnode.rangecheck_array;
  478. var
  479. freereg : boolean;
  480. hightree : tnode;
  481. poslabel,
  482. neglabel : tasmlabel;
  483. hreg : tregister;
  484. pushed : tpushedsaved;
  485. begin
  486. if is_open_array(left.resulttype.def) or
  487. is_array_of_const(left.resulttype.def) then
  488. begin
  489. { cdecl functions don't have high() so we can not check the range }
  490. if not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  491. begin
  492. { Get high value }
  493. hightree:=load_high_value(tvarsym(tloadnode(left).symtableentry));
  494. { it must be available }
  495. if not assigned(hightree) then
  496. internalerror(200212201);
  497. firstpass(hightree);
  498. secondpass(hightree);
  499. { generate compares }
  500. freereg:=false;
  501. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  502. hreg:=right.location.register
  503. else
  504. begin
  505. hreg := cg.get_scratch_reg_int(exprasmlist);
  506. freereg:=true;
  507. cg.a_load_loc_reg(exprasmlist,right.location,hreg);
  508. end;
  509. objectlibrary.getlabel(neglabel);
  510. objectlibrary.getlabel(poslabel);
  511. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_LT,0,hreg,poslabel);
  512. cg.a_cmp_loc_reg_label(exprasmlist,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  513. if freereg then
  514. cg.free_scratch_reg(exprasmlist,hreg);
  515. cg.a_label(exprasmlist,poslabel);
  516. cg.a_call_name(exprasmlist,'FPC_RANGEERROR');
  517. cg.a_label(exprasmlist,neglabel);
  518. { release hightree }
  519. location_release(exprasmlist,hightree.location);
  520. hightree.free;
  521. end;
  522. end
  523. else
  524. if is_dynamic_array(left.resulttype.def) then
  525. begin
  526. rg.saveusedregisters(exprasmlist,pushed,all_registers);
  527. cg.a_param_loc(exprasmlist,right.location,paramanager.getintparaloc(2));
  528. cg.a_param_loc(exprasmlist,left.location,paramanager.getintparaloc(1));
  529. rg.saveregvars(exprasmlist,all_registers);
  530. cg.a_call_name(exprasmlist,'FPC_DYNARRAY_RANGECHECK');
  531. rg.restoreusedregisters(exprasmlist,pushed);
  532. cg.g_maybe_loadself(exprasmlist);
  533. end
  534. else
  535. cg.g_rangecheck(exprasmlist,right,left.resulttype.def);
  536. end;
  537. procedure tcgvecnode.pass_2;
  538. var
  539. extraoffset : longint;
  540. t : tnode;
  541. href : treference;
  542. pushed : tpushedsaved;
  543. isjump : boolean;
  544. otl,ofl : tasmlabel;
  545. newsize : tcgsize;
  546. pushedregs : tmaybesave;
  547. begin
  548. newsize:=def_cgsize(resulttype.def);
  549. location_reset(location,LOC_REFERENCE,newsize);
  550. secondpass(left);
  551. { we load the array reference to location }
  552. { an ansistring needs to be dereferenced }
  553. if is_ansistring(left.resulttype.def) or
  554. is_widestring(left.resulttype.def) then
  555. begin
  556. if nf_callunique in flags then
  557. begin
  558. if left.location.loc<>LOC_REFERENCE then
  559. begin
  560. CGMessage(cg_e_illegal_expression);
  561. exit;
  562. end;
  563. rg.saveusedregisters(exprasmlist,pushed,all_registers);
  564. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paramanager.getintparaloc(1));
  565. rg.saveregvars(exprasmlist,all_registers);
  566. cg.a_call_name(exprasmlist,'FPC_'+Upper(tstringdef(left.resulttype.def).stringtypname)+'_UNIQUE');
  567. cg.g_maybe_loadself(exprasmlist);
  568. rg.restoreusedregisters(exprasmlist,pushed);
  569. end;
  570. case left.location.loc of
  571. LOC_REGISTER,
  572. LOC_CREGISTER :
  573. location.reference.base:=left.location.register;
  574. LOC_CREFERENCE,
  575. LOC_REFERENCE :
  576. begin
  577. location_release(exprasmlist,left.location);
  578. location.reference.base:=rg.getregisterint(exprasmlist);
  579. cg.a_load_ref_reg(exprasmlist,OS_ADDR,left.location.reference,location.reference.base);
  580. end;
  581. else
  582. internalerror(2002032218);
  583. end;
  584. { check for a zero length string,
  585. we can use the ansistring routine here }
  586. if (cs_check_range in aktlocalswitches) then
  587. begin
  588. rg.saveusedregisters(exprasmlist,pushed,all_registers);
  589. cg.a_param_reg(exprasmlist,OS_ADDR,location.reference.base,paramanager.getintparaloc(1));
  590. rg.saveregvars(exprasmlist,all_registers);
  591. cg.a_call_name(exprasmlist,'FPC_'+Upper(tstringdef(left.resulttype.def).stringtypname)+'_CHECKZERO');
  592. cg.g_maybe_loadself(exprasmlist);
  593. rg.restoreusedregisters(exprasmlist,pushed);
  594. end;
  595. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  596. if is_ansistring(left.resulttype.def) then
  597. dec(location.reference.offset)
  598. else
  599. dec(location.reference.offset,2);
  600. end
  601. else if is_dynamic_array(left.resulttype.def) then
  602. begin
  603. case left.location.loc of
  604. LOC_REGISTER,
  605. LOC_CREGISTER :
  606. location.reference.base:=left.location.register;
  607. LOC_REFERENCE,
  608. LOC_CREFERENCE :
  609. begin
  610. location_release(exprasmlist,left.location);
  611. location.reference.base:=rg.getaddressregister(exprasmlist);
  612. cg.a_load_ref_reg(exprasmlist,OS_ADDR,
  613. left.location.reference,location.reference.base);
  614. end;
  615. else
  616. internalerror(2002032219);
  617. end;
  618. end
  619. else
  620. location_copy(location,left.location);
  621. { offset can only differ from 0 if arraydef }
  622. if (left.resulttype.def.deftype=arraydef) and
  623. not(is_dynamic_array(left.resulttype.def)) then
  624. dec(location.reference.offset,get_mul_size*tarraydef(left.resulttype.def).lowrange);
  625. if right.nodetype=ordconstn then
  626. begin
  627. { offset can only differ from 0 if arraydef }
  628. case left.resulttype.def.deftype of
  629. arraydef :
  630. begin
  631. if not(is_open_array(left.resulttype.def)) and
  632. not(is_array_of_const(left.resulttype.def)) and
  633. not(is_dynamic_array(left.resulttype.def)) then
  634. begin
  635. if (tordconstnode(right).value>tarraydef(left.resulttype.def).highrange) or
  636. (tordconstnode(right).value<tarraydef(left.resulttype.def).lowrange) then
  637. begin
  638. { this should be caught in the resulttypepass! (JM) }
  639. if (cs_check_range in aktlocalswitches) then
  640. CGMessage(parser_e_range_check_error)
  641. else
  642. CGMessage(parser_w_range_check_error);
  643. end;
  644. end
  645. else
  646. begin
  647. { range checking for open and dynamic arrays needs
  648. runtime code }
  649. secondpass(right);
  650. rangecheck_array;
  651. end;
  652. end;
  653. stringdef :
  654. begin
  655. if (cs_check_range in aktlocalswitches) then
  656. begin
  657. case tstringdef(left.resulttype.def).string_typ of
  658. { it's the same for ansi- and wide strings }
  659. st_widestring,
  660. st_ansistring:
  661. begin
  662. rg.saveusedregisters(exprasmlist,pushed,all_registers);
  663. cg.a_param_const(exprasmlist,OS_INT,tordconstnode(right).value,paramanager.getintparaloc(2));
  664. href:=location.reference;
  665. dec(href.offset,7);
  666. cg.a_param_ref(exprasmlist,OS_INT,href,paramanager.getintparaloc(1));
  667. rg.saveregvars(exprasmlist,all_registers);
  668. cg.a_call_name(exprasmlist,'FPC_'+Upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  669. rg.restoreusedregisters(exprasmlist,pushed);
  670. cg.g_maybe_loadself(exprasmlist);
  671. end;
  672. st_shortstring:
  673. begin
  674. {!!!!!!!!!!!!!!!!!}
  675. end;
  676. st_longstring:
  677. begin
  678. {!!!!!!!!!!!!!!!!!}
  679. end;
  680. end;
  681. end;
  682. end;
  683. end;
  684. inc(location.reference.offset,
  685. get_mul_size*tordconstnode(right).value);
  686. end
  687. else
  688. { not nodetype=ordconstn }
  689. begin
  690. if (cs_regalloc in aktglobalswitches) and
  691. { if we do range checking, we don't }
  692. { need that fancy code (it would be }
  693. { buggy) }
  694. not(cs_check_range in aktlocalswitches) and
  695. (left.resulttype.def.deftype=arraydef) then
  696. begin
  697. extraoffset:=0;
  698. if (right.nodetype=addn) then
  699. begin
  700. if taddnode(right).right.nodetype=ordconstn then
  701. begin
  702. extraoffset:=tordconstnode(taddnode(right).right).value;
  703. t:=taddnode(right).left;
  704. { First pass processed this with the assumption }
  705. { that there was an add node which may require an }
  706. { extra register. Fake it or die with IE10 (JM) }
  707. t.registers32 := taddnode(right).registers32;
  708. taddnode(right).left:=nil;
  709. right.free;
  710. right:=t;
  711. end
  712. else if taddnode(right).left.nodetype=ordconstn then
  713. begin
  714. extraoffset:=tordconstnode(taddnode(right).left).value;
  715. t:=taddnode(right).right;
  716. t.registers32 := right.registers32;
  717. taddnode(right).right:=nil;
  718. right.free;
  719. right:=t;
  720. end;
  721. end
  722. else if (right.nodetype=subn) then
  723. begin
  724. if taddnode(right).right.nodetype=ordconstn then
  725. begin
  726. extraoffset:=-tordconstnode(taddnode(right).right).value;
  727. t:=taddnode(right).left;
  728. t.registers32 := right.registers32;
  729. taddnode(right).left:=nil;
  730. right.free;
  731. right:=t;
  732. end
  733. { You also have to negate right.right in this case! I can't add an
  734. unaryminusn without causing a crash, so I've disabled it (JM)
  735. else if right.left.nodetype=ordconstn then
  736. begin
  737. extraoffset:=right.left.value;
  738. t:=right.right;
  739. t^.registers32 := right.registers32;
  740. putnode(right);
  741. putnode(right.left);
  742. right:=t;
  743. end;}
  744. end;
  745. inc(location.reference.offset,
  746. get_mul_size*extraoffset);
  747. end;
  748. { calculate from left to right }
  749. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  750. { should be internalerror! (JM) }
  751. CGMessage(cg_e_illegal_expression);
  752. isjump:=(right.location.loc=LOC_JUMP);
  753. if isjump then
  754. begin
  755. otl:=truelabel;
  756. objectlibrary.getlabel(truelabel);
  757. ofl:=falselabel;
  758. objectlibrary.getlabel(falselabel);
  759. end;
  760. maybe_save(exprasmlist,right.registers32,location,pushedregs);
  761. secondpass(right);
  762. maybe_restore(exprasmlist,location,pushedregs);
  763. if cs_check_range in aktlocalswitches then
  764. begin
  765. if left.resulttype.def.deftype=arraydef then
  766. rangecheck_array;
  767. end;
  768. location_force_reg(exprasmlist,right.location,OS_32,false);
  769. if isjump then
  770. begin
  771. truelabel:=otl;
  772. falselabel:=ofl;
  773. end;
  774. { produce possible range check code: }
  775. if cs_check_range in aktlocalswitches then
  776. begin
  777. if left.resulttype.def.deftype=arraydef then
  778. begin
  779. { done defore (PM) }
  780. end
  781. else if (left.resulttype.def.deftype=stringdef) then
  782. begin
  783. case tstringdef(left.resulttype.def).string_typ of
  784. { it's the same for ansi- and wide strings }
  785. st_widestring,
  786. st_ansistring:
  787. begin
  788. rg.saveusedregisters(exprasmlist,pushed,all_registers);
  789. cg.a_param_reg(exprasmlist,OS_INT,right.location.register,paramanager.getintparaloc(1));
  790. href:=location.reference;
  791. dec(href.offset,7);
  792. cg.a_param_ref(exprasmlist,OS_INT,href,paramanager.getintparaloc(1));
  793. rg.saveregvars(exprasmlist,all_registers);
  794. cg.a_call_name(exprasmlist,'FPC_'+Upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  795. rg.restoreusedregisters(exprasmlist,pushed);
  796. cg.g_maybe_loadself(exprasmlist);
  797. end;
  798. st_shortstring:
  799. begin
  800. {!!!!!!!!!!!!!!!!!}
  801. end;
  802. st_longstring:
  803. begin
  804. {!!!!!!!!!!!!!!!!!}
  805. end;
  806. end;
  807. end;
  808. end;
  809. { insert the register and the multiplication factor in the
  810. reference }
  811. update_reference_reg_mul(right.location.register,get_mul_size);
  812. end;
  813. location.size:=newsize;
  814. end;
  815. begin
  816. cloadvmtnode:=tcgloadvmtnode;
  817. chnewnode:=tcghnewnode;
  818. chdisposenode:=tcghdisposenode;
  819. caddrnode:=tcgaddrnode;
  820. cdoubleaddrnode:=tcgdoubleaddrnode;
  821. cderefnode:=tcgderefnode;
  822. csubscriptnode:=tcgsubscriptnode;
  823. cselfnode:=tcgselfnode;
  824. cwithnode:=tcgwithnode;
  825. cvecnode:=tcgvecnode;
  826. end.
  827. {
  828. $Log$
  829. Revision 1.39 2002-12-20 18:13:19 peter
  830. * no rangecheck for openarrays with cdecl
  831. Revision 1.38 2002/12/17 22:19:33 peter
  832. * fixed pushing of records>8 bytes with stdcall
  833. * simplified hightree loading
  834. Revision 1.37 2002/12/08 13:39:03 carl
  835. + some documentation added
  836. Revision 1.36 2002/12/07 14:14:19 carl
  837. * bugfix on invalid typecast
  838. Revision 1.35 2002/11/25 17:43:18 peter
  839. * splitted defbase in defutil,symutil,defcmp
  840. * merged isconvertable and is_equal into compare_defs(_ext)
  841. * made operator search faster by walking the list only once
  842. Revision 1.34 2002/11/24 18:19:20 carl
  843. + checkpointer for interfaces also
  844. Revision 1.33 2002/11/23 22:50:06 carl
  845. * some small speed optimizations
  846. + added several new warnings/hints
  847. Revision 1.32 2002/11/15 01:58:51 peter
  848. * merged changes from 1.0.7 up to 04-11
  849. - -V option for generating bug report tracing
  850. - more tracing for option parsing
  851. - errors for cdecl and high()
  852. - win32 import stabs
  853. - win32 records<=8 are returned in eax:edx (turned off by default)
  854. - heaptrc update
  855. - more info for temp management in .s file with EXTDEBUG
  856. Revision 1.31 2002/10/09 20:24:47 florian
  857. + range checking for dyn. arrays
  858. Revision 1.30 2002/10/07 21:30:45 peter
  859. * rangecheck for open arrays added
  860. Revision 1.29 2002/10/05 12:43:25 carl
  861. * fixes for Delphi 6 compilation
  862. (warning : Some features do not work under Delphi)
  863. Revision 1.28 2002/09/17 18:54:02 jonas
  864. * a_load_reg_reg() now has two size parameters: source and dest. This
  865. allows some optimizations on architectures that don't encode the
  866. register size in the register name.
  867. Revision 1.27 2002/09/07 15:25:03 peter
  868. * old logs removed and tabs fixed
  869. Revision 1.26 2002/09/01 18:46:01 peter
  870. * fixed generic tcgvecnode
  871. * move code that updates a reference with index register and multiplier
  872. to separate method so it can be overriden for scaled indexing
  873. * i386 uses generic tcgvecnode
  874. Revision 1.25 2002/08/23 16:14:48 peter
  875. * tempgen cleanup
  876. * tt_noreuse temp type added that will be used in genentrycode
  877. Revision 1.24 2002/08/15 08:13:54 carl
  878. - a_load_sym_ofs_reg removed
  879. * loadvmt now calls loadaddr_ref_reg instead
  880. Revision 1.23 2002/08/11 14:32:26 peter
  881. * renamed current_library to objectlibrary
  882. Revision 1.22 2002/08/11 13:24:12 peter
  883. * saving of asmsymbols in ppu supported
  884. * asmsymbollist global is removed and moved into a new class
  885. tasmlibrarydata that will hold the info of a .a file which
  886. corresponds with a single module. Added librarydata to tmodule
  887. to keep the library info stored for the module. In the future the
  888. objectfiles will also be stored to the tasmlibrarydata class
  889. * all getlabel/newasmsymbol and friends are moved to the new class
  890. Revision 1.21 2002/08/11 11:36:57 jonas
  891. * always first try to use base and only then index
  892. Revision 1.20 2002/08/11 06:14:40 florian
  893. * fixed powerpc compilation problems
  894. Revision 1.19 2002/08/10 14:46:29 carl
  895. + moved target_cpu_string to cpuinfo
  896. * renamed asmmode enum.
  897. * assembler reader has now less ifdef's
  898. * move from nppcmem.pas -> ncgmem.pas vec. node.
  899. Revision 1.18 2002/07/28 21:34:31 florian
  900. * more powerpc fixes
  901. + dummy tcgvecnode
  902. Revision 1.17 2002/07/11 14:41:28 florian
  903. * start of the new generic parameter handling
  904. Revision 1.16 2002/07/07 09:52:32 florian
  905. * powerpc target fixed, very simple units can be compiled
  906. * some basic stuff for better callparanode handling, far from being finished
  907. Revision 1.15 2002/07/01 18:46:23 peter
  908. * internal linker
  909. * reorganized aasm layer
  910. Revision 1.14 2002/07/01 16:23:53 peter
  911. * cg64 patch
  912. * basics for currency
  913. * asnode updates for class and interface (not finished)
  914. Revision 1.13 2002/05/20 13:30:40 carl
  915. * bugfix of hdisponen (base must be set, not index)
  916. * more portability fixes
  917. Revision 1.12 2002/05/18 13:34:09 peter
  918. * readded missing revisions
  919. Revision 1.11 2002/05/16 19:46:37 carl
  920. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  921. + try to fix temp allocation (still in ifdef)
  922. + generic constructor calls
  923. + start of tassembler / tmodulebase class cleanup
  924. Revision 1.9 2002/05/12 16:53:07 peter
  925. * moved entry and exitcode to ncgutil and cgobj
  926. * foreach gets extra argument for passing local data to the
  927. iterator function
  928. * -CR checks also class typecasts at runtime by changing them
  929. into as
  930. * fixed compiler to cycle with the -CR option
  931. * fixed stabs with elf writer, finally the global variables can
  932. be watched
  933. * removed a lot of routines from cga unit and replaced them by
  934. calls to cgobj
  935. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  936. u32bit then the other is typecasted also to u32bit without giving
  937. a rangecheck warning/error.
  938. * fixed pascal calling method with reversing also the high tree in
  939. the parast, detected by tcalcst3 test
  940. Revision 1.8 2002/04/20 21:32:23 carl
  941. + generic FPC_CHECKPOINTER
  942. + first parameter offset in stack now portable
  943. * rename some constants
  944. + move some cpu stuff to other units
  945. - remove unused constents
  946. * fix stacksize for some targets
  947. * fix generic size problems which depend now on EXTEND_SIZE constant
  948. Revision 1.7 2002/04/15 18:58:47 carl
  949. + target_info.size_of_pointer -> pointer_Size
  950. Revision 1.6 2002/04/04 19:05:57 peter
  951. * removed unused units
  952. * use tlocation.size in cg.a_*loc*() routines
  953. }