ncgmem.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate assembler for memory related nodes 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 ncgmem;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,cgbase,cpuinfo,cpubase,
  23. node,nmem;
  24. type
  25. tcgloadvmtaddrnode = class(tloadvmtaddrnode)
  26. procedure pass_2;override;
  27. end;
  28. tcgloadparentfpnode = class(tloadparentfpnode)
  29. procedure pass_2;override;
  30. end;
  31. tcgaddrnode = class(taddrnode)
  32. procedure pass_2;override;
  33. end;
  34. tcgderefnode = class(tderefnode)
  35. procedure pass_2;override;
  36. end;
  37. tcgsubscriptnode = class(tsubscriptnode)
  38. procedure pass_2;override;
  39. end;
  40. tcgwithnode = class(twithnode)
  41. procedure pass_2;override;
  42. end;
  43. tcgvecnode = class(tvecnode)
  44. private
  45. procedure rangecheck_array;
  46. protected
  47. function get_mul_size : aint;
  48. {# This routine is used to calculate the address of the reference.
  49. On entry reg contains the index in the array,
  50. and l contains the size of each element in the array.
  51. This routine should update location.reference correctly,
  52. so it points to the correct address.
  53. }
  54. procedure update_reference_reg_mul(reg:tregister;l:aint);virtual;
  55. procedure second_wideansistring;virtual;
  56. procedure second_dynamicarray;virtual;
  57. public
  58. procedure pass_2;override;
  59. end;
  60. implementation
  61. uses
  62. systems,
  63. cutils,verbose,globals,
  64. symconst,symdef,symsym,defutil,paramgr,
  65. aasmbase,aasmtai,
  66. procinfo,pass_2,parabase,
  67. pass_1,nld,ncon,nadd,nutils,
  68. cgutils,cgobj,
  69. tgobj,ncgutil
  70. ;
  71. {*****************************************************************************
  72. TCGLOADVMTADDRNODE
  73. *****************************************************************************}
  74. procedure tcgloadvmtaddrnode.pass_2;
  75. var
  76. href : treference;
  77. begin
  78. location_reset(location,LOC_REGISTER,OS_ADDR);
  79. if (left.nodetype<>typen) then
  80. begin
  81. { left contains self, load vmt from self }
  82. secondpass(left);
  83. if is_object(left.resulttype.def) then
  84. begin
  85. case left.location.loc of
  86. LOC_CREFERENCE,
  87. LOC_REFERENCE:
  88. begin
  89. reference_reset_base(href,cg.getaddressregister(exprasmlist),tobjectdef(left.resulttype.def).vmt_offset);
  90. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,href.base);
  91. end;
  92. else
  93. internalerror(200305056);
  94. end;
  95. end
  96. else
  97. begin
  98. case left.location.loc of
  99. LOC_REGISTER:
  100. begin
  101. {$ifdef cpu_uses_separate_address_registers}
  102. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  103. begin
  104. reference_reset_base(href,cg.getaddressregister(exprasmlist),tobjectdef(left.resulttype.def).vmt_offset);
  105. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.register,href.base);
  106. end
  107. else
  108. {$endif}
  109. reference_reset_base(href,left.location.register,tobjectdef(left.resulttype.def).vmt_offset);
  110. end;
  111. LOC_CREGISTER,
  112. LOC_CREFERENCE,
  113. LOC_REFERENCE:
  114. begin
  115. reference_reset_base(href,cg.getaddressregister(exprasmlist),tobjectdef(left.resulttype.def).vmt_offset);
  116. cg.a_load_loc_reg(exprasmlist,OS_ADDR,left.location,href.base);
  117. end;
  118. else
  119. internalerror(200305057);
  120. end;
  121. end;
  122. location.register:=cg.getaddressregister(exprasmlist);
  123. cg.g_maybe_testself(exprasmlist,href.base);
  124. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,href,location.register);
  125. end
  126. else
  127. begin
  128. reference_reset_symbol(href,
  129. objectlibrary.newasmsymbol(tobjectdef(tclassrefdef(resulttype.def).pointertype.def).vmt_mangledname,AB_EXTERNAL,AT_DATA),0);
  130. location.register:=cg.getaddressregister(exprasmlist);
  131. cg.a_loadaddr_ref_reg(exprasmlist,href,location.register);
  132. end;
  133. end;
  134. {*****************************************************************************
  135. TCGLOADPARENTFPNODE
  136. *****************************************************************************}
  137. procedure tcgloadparentfpnode.pass_2;
  138. var
  139. currpi : tprocinfo;
  140. hsym : tparavarsym;
  141. href : treference;
  142. begin
  143. if (current_procinfo.procdef.parast.symtablelevel=parentpd.parast.symtablelevel) then
  144. begin
  145. location_reset(location,LOC_REGISTER,OS_ADDR);
  146. location.register:=current_procinfo.framepointer;
  147. end
  148. else
  149. begin
  150. currpi:=current_procinfo;
  151. location_reset(location,LOC_REGISTER,OS_ADDR);
  152. location.register:=cg.getaddressregister(exprasmlist);
  153. { load framepointer of current proc }
  154. hsym:=tparavarsym(currpi.procdef.parast.search('parentfp'));
  155. if not assigned(hsym) then
  156. internalerror(200309281);
  157. cg.a_load_loc_reg(exprasmlist,OS_ADDR,hsym.localloc,location.register);
  158. { walk parents }
  159. while (currpi.procdef.owner.symtablelevel>parentpd.parast.symtablelevel) do
  160. begin
  161. currpi:=currpi.parent;
  162. if not assigned(currpi) then
  163. internalerror(200311201);
  164. hsym:=tparavarsym(currpi.procdef.parast.search('parentfp'));
  165. if not assigned(hsym) then
  166. internalerror(200309282);
  167. if hsym.localloc.loc<>LOC_REFERENCE then
  168. internalerror(200309283);
  169. reference_reset_base(href,location.register,hsym.localloc.reference.offset);
  170. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,href,location.register);
  171. end;
  172. end;
  173. end;
  174. {*****************************************************************************
  175. TCGADDRNODE
  176. *****************************************************************************}
  177. procedure tcgaddrnode.pass_2;
  178. begin
  179. secondpass(left);
  180. location_reset(location,LOC_REGISTER,OS_ADDR);
  181. location.register:=cg.getaddressregister(exprasmlist);
  182. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,location.register);
  183. end;
  184. {*****************************************************************************
  185. TCGDEREFNODE
  186. *****************************************************************************}
  187. procedure tcgderefnode.pass_2;
  188. var
  189. paraloc1 : tcgpara;
  190. begin
  191. secondpass(left);
  192. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  193. case left.location.loc of
  194. LOC_REGISTER:
  195. begin
  196. {$ifdef cpu_uses_separate_address_registers}
  197. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  198. begin
  199. location.reference.base := cg.getaddressregister(exprasmlist);
  200. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.register,
  201. location.reference.base);
  202. end
  203. else
  204. {$endif}
  205. location.reference.base := left.location.register;
  206. end;
  207. LOC_CREGISTER,
  208. LOC_CREFERENCE,
  209. LOC_REFERENCE:
  210. begin
  211. location.reference.base:=cg.getaddressregister(exprasmlist);
  212. cg.a_load_loc_reg(exprasmlist,OS_ADDR,left.location,location.reference.base);
  213. end;
  214. end;
  215. if (cs_gdb_heaptrc in aktglobalswitches) and
  216. (cs_checkpointer in aktlocalswitches) and
  217. not(cs_compilesystem in aktmoduleswitches) and
  218. not(tpointerdef(left.resulttype.def).is_far) and
  219. not(nf_no_checkpointer in flags) then
  220. begin
  221. paraloc1.init;
  222. paramanager.getintparaloc(pocall_default,1,paraloc1);
  223. paramanager.allocparaloc(exprasmlist,paraloc1);
  224. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paraloc1);
  225. paramanager.freeparaloc(exprasmlist,paraloc1);
  226. paraloc1.done;
  227. cg.allocallcpuregisters(exprasmlist);
  228. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  229. cg.deallocallcpuregisters(exprasmlist);
  230. end;
  231. end;
  232. {*****************************************************************************
  233. TCGSUBSCRIPTNODE
  234. *****************************************************************************}
  235. procedure tcgsubscriptnode.pass_2;
  236. var
  237. paraloc1 : tcgpara;
  238. begin
  239. secondpass(left);
  240. if codegenerror then
  241. exit;
  242. paraloc1.init;
  243. { classes and interfaces must be dereferenced implicit }
  244. if is_class_or_interface(left.resulttype.def) then
  245. begin
  246. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  247. case left.location.loc of
  248. LOC_CREGISTER,
  249. LOC_REGISTER:
  250. begin
  251. {$ifdef cpu_uses_separate_address_registers}
  252. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  253. begin
  254. location.reference.base:=rg.getaddressregister(exprasmlist);
  255. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,
  256. left.location.register,location.reference.base);
  257. end
  258. else
  259. {$endif}
  260. location.reference.base := left.location.register;
  261. end;
  262. LOC_CREFERENCE,
  263. LOC_REFERENCE:
  264. begin
  265. location.reference.base:=cg.getaddressregister(exprasmlist);
  266. cg.a_load_loc_reg(exprasmlist,OS_ADDR,left.location,location.reference.base);
  267. end;
  268. end;
  269. { implicit deferencing }
  270. if (cs_gdb_heaptrc in aktglobalswitches) and
  271. (cs_checkpointer in aktlocalswitches) and
  272. not(cs_compilesystem in aktmoduleswitches) then
  273. begin
  274. paramanager.getintparaloc(pocall_default,1,paraloc1);
  275. paramanager.allocparaloc(exprasmlist,paraloc1);
  276. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paraloc1);
  277. paramanager.freeparaloc(exprasmlist,paraloc1);
  278. cg.allocallcpuregisters(exprasmlist);
  279. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  280. cg.deallocallcpuregisters(exprasmlist);
  281. end;
  282. end
  283. else if is_interfacecom(left.resulttype.def) then
  284. begin
  285. tg.GetTempTyped(exprasmlist,left.resulttype.def,tt_normal,location.reference);
  286. cg.a_load_loc_ref(exprasmlist,OS_ADDR,left.location,location.reference);
  287. { implicit deferencing also for interfaces }
  288. if (cs_gdb_heaptrc in aktglobalswitches) and
  289. (cs_checkpointer in aktlocalswitches) and
  290. not(cs_compilesystem in aktmoduleswitches) then
  291. begin
  292. paramanager.getintparaloc(pocall_default,1,paraloc1);
  293. paramanager.allocparaloc(exprasmlist,paraloc1);
  294. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paraloc1);
  295. paramanager.freeparaloc(exprasmlist,paraloc1);
  296. cg.allocallcpuregisters(exprasmlist);
  297. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  298. cg.deallocallcpuregisters(exprasmlist);
  299. end;
  300. end
  301. else
  302. location_copy(location,left.location);
  303. inc(location.reference.offset,vs.fieldoffset);
  304. { also update the size of the location }
  305. location.size:=def_cgsize(resulttype.def);
  306. paraloc1.done;
  307. end;
  308. {*****************************************************************************
  309. TCGWITHNODE
  310. *****************************************************************************}
  311. procedure tcgwithnode.pass_2;
  312. {$ifdef WITHNODEDEBUG}
  313. const
  314. withlevel : longint = 0;
  315. var
  316. withstartlabel,withendlabel : tasmlabel;
  317. pp : pchar;
  318. mangled_length : longint;
  319. refnode : tnode;
  320. {$endif WITHNODEDEBUG}
  321. begin
  322. location_reset(location,LOC_VOID,OS_NO);
  323. {$ifdef WITHNODEDEBUG}
  324. if (cs_debuginfo in aktmoduleswitches) then
  325. begin
  326. { load reference }
  327. if (withrefnode.nodetype=derefn) and
  328. (tderefnode(withrefnode).left.nodetype=temprefn) then
  329. refnode:=tderefnode(withrefnode).left
  330. else
  331. refnode:=withrefnode;
  332. secondpass(refnode);
  333. location_freetemp(exprasmlist,refnode.location);
  334. if not(refnode.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  335. internalerror(2003092810);
  336. inc(withlevel);
  337. objectlibrary.getaddrlabel(withstartlabel);
  338. objectlibrary.getaddrlabel(withendlabel);
  339. cg.a_label(exprasmlist,withstartlabel);
  340. withdebugList.concat(Tai_stabs.Create(strpnew(
  341. '"with'+tostr(withlevel)+':'+tostr(symtablestack.getnewtypecount)+
  342. '=*'+tstoreddef(left.resulttype.def).numberstring+'",'+
  343. tostr(N_LSYM)+',0,0,'+tostr(refnode.location.reference.offset))));
  344. mangled_length:=length(current_procinfo.procdef.mangledname);
  345. getmem(pp,mangled_length+50);
  346. strpcopy(pp,'192,0,0,'+withstartlabel.name);
  347. if (target_info.use_function_relative_addresses) then
  348. begin
  349. strpcopy(strend(pp),'-');
  350. strpcopy(strend(pp),current_procinfo.procdef.mangledname);
  351. end;
  352. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  353. end;
  354. {$endif WITHNODEDEBUG}
  355. if assigned(left) then
  356. secondpass(left);
  357. {$ifdef WITHNODEDEBUG}
  358. if (cs_debuginfo in aktmoduleswitches) then
  359. begin
  360. cg.a_label(exprasmlist,withendlabel);
  361. strpcopy(pp,'224,0,0,'+withendlabel.name);
  362. if (target_info.use_function_relative_addresses) then
  363. begin
  364. strpcopy(strend(pp),'-');
  365. strpcopy(strend(pp),current_procinfo.procdef.mangledname);
  366. end;
  367. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  368. freemem(pp,mangled_length+50);
  369. dec(withlevel);
  370. end;
  371. {$endif WITHNODEDEBUG}
  372. end;
  373. {*****************************************************************************
  374. TCGVECNODE
  375. *****************************************************************************}
  376. function tcgvecnode.get_mul_size : aint;
  377. begin
  378. if nf_memindex in flags then
  379. get_mul_size:=1
  380. else
  381. begin
  382. if (left.resulttype.def.deftype=arraydef) then
  383. get_mul_size:=tarraydef(left.resulttype.def).elesize
  384. else
  385. get_mul_size:=resulttype.def.size;
  386. end
  387. end;
  388. procedure tcgvecnode.update_reference_reg_mul(reg:tregister;l:aint);
  389. var
  390. hreg: tregister;
  391. begin
  392. if location.reference.base=NR_NO then
  393. begin
  394. if l<>1 then
  395. cg.a_op_const_reg(exprasmlist,OP_IMUL,OS_ADDR,l,reg);
  396. location.reference.base:=reg;
  397. end
  398. else if location.reference.index=NR_NO then
  399. begin
  400. if l<>1 then
  401. cg.a_op_const_reg(exprasmlist,OP_IMUL,OS_ADDR,l,reg);
  402. location.reference.index:=reg;
  403. end
  404. else
  405. begin
  406. hreg := cg.getaddressregister(exprasmlist);
  407. cg.a_loadaddr_ref_reg(exprasmlist,location.reference,hreg);
  408. reference_reset_base(location.reference,hreg,0);
  409. { insert new index register }
  410. if l<>1 then
  411. cg.a_op_const_reg(exprasmlist,OP_IMUL,OS_ADDR,l,reg);
  412. location.reference.index:=reg;
  413. end;
  414. end;
  415. procedure tcgvecnode.second_wideansistring;
  416. begin
  417. end;
  418. procedure tcgvecnode.second_dynamicarray;
  419. begin
  420. end;
  421. procedure tcgvecnode.rangecheck_array;
  422. var
  423. hightree : tnode;
  424. poslabel,
  425. neglabel : tasmlabel;
  426. hreg : tregister;
  427. paraloc1,paraloc2 : tcgpara;
  428. begin
  429. paraloc1.init;
  430. paraloc2.init;
  431. if is_open_array(left.resulttype.def) or
  432. is_array_of_const(left.resulttype.def) then
  433. begin
  434. { cdecl functions don't have high() so we can not check the range }
  435. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  436. begin
  437. { Get high value }
  438. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  439. { it must be available }
  440. if not assigned(hightree) then
  441. internalerror(200212201);
  442. firstpass(hightree);
  443. secondpass(hightree);
  444. { generate compares }
  445. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  446. hreg:=cg.makeregsize(exprasmlist,right.location.register,OS_INT)
  447. else
  448. begin
  449. hreg:=cg.getintregister(exprasmlist,OS_INT);
  450. cg.a_load_loc_reg(exprasmlist,OS_INT,right.location,hreg);
  451. end;
  452. objectlibrary.getlabel(neglabel);
  453. objectlibrary.getlabel(poslabel);
  454. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_LT,0,hreg,poslabel);
  455. cg.a_cmp_loc_reg_label(exprasmlist,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  456. cg.a_label(exprasmlist,poslabel);
  457. cg.a_call_name(exprasmlist,'FPC_RANGEERROR');
  458. cg.a_label(exprasmlist,neglabel);
  459. { release hightree }
  460. hightree.free;
  461. end;
  462. end
  463. else
  464. if is_dynamic_array(left.resulttype.def) then
  465. begin
  466. paramanager.getintparaloc(pocall_default,1,paraloc1);
  467. paramanager.getintparaloc(pocall_default,2,paraloc2);
  468. paramanager.allocparaloc(exprasmlist,paraloc2);
  469. cg.a_param_loc(exprasmlist,right.location,paraloc2);
  470. paramanager.allocparaloc(exprasmlist,paraloc1);
  471. cg.a_param_loc(exprasmlist,left.location,paraloc1);
  472. paramanager.freeparaloc(exprasmlist,paraloc1);
  473. paramanager.freeparaloc(exprasmlist,paraloc2);
  474. cg.allocallcpuregisters(exprasmlist);
  475. cg.a_call_name(exprasmlist,'FPC_DYNARRAY_RANGECHECK');
  476. cg.deallocallcpuregisters(exprasmlist);
  477. end
  478. else
  479. cg.g_rangecheck(exprasmlist,right.location,right.resulttype.def,left.resulttype.def);
  480. paraloc1.done;
  481. paraloc2.done;
  482. end;
  483. procedure tcgvecnode.pass_2;
  484. var
  485. offsetdec,
  486. extraoffset : aint;
  487. t : tnode;
  488. href : treference;
  489. otl,ofl : tasmlabel;
  490. newsize : tcgsize;
  491. mulsize : aint;
  492. isjump : boolean;
  493. paraloc1,
  494. paraloc2 : tcgpara;
  495. begin
  496. paraloc1.init;
  497. paraloc2.init;
  498. mulsize := get_mul_size;
  499. newsize:=def_cgsize(resulttype.def);
  500. secondpass(left);
  501. if left.location.loc=LOC_CREFERENCE then
  502. location_reset(location,LOC_CREFERENCE,newsize)
  503. else
  504. location_reset(location,LOC_REFERENCE,newsize);
  505. { an ansistring needs to be dereferenced }
  506. if is_ansistring(left.resulttype.def) or
  507. is_widestring(left.resulttype.def) then
  508. begin
  509. if nf_callunique in flags then
  510. internalerror(200304236);
  511. {DM!!!!!}
  512. case left.location.loc of
  513. LOC_REGISTER,
  514. LOC_CREGISTER :
  515. location.reference.base:=left.location.register;
  516. LOC_CREFERENCE,
  517. LOC_REFERENCE :
  518. begin
  519. location.reference.base:=cg.getaddressregister(exprasmlist);
  520. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.reference,location.reference.base);
  521. end;
  522. else
  523. internalerror(2002032218);
  524. end;
  525. { check for a zero length string,
  526. we can use the ansistring routine here }
  527. if (cs_check_range in aktlocalswitches) then
  528. begin
  529. paramanager.getintparaloc(pocall_default,1,paraloc1);
  530. paramanager.allocparaloc(exprasmlist,paraloc1);
  531. cg.a_param_reg(exprasmlist,OS_ADDR,location.reference.base,paraloc1);
  532. paramanager.freeparaloc(exprasmlist,paraloc1);
  533. cg.allocallcpuregisters(exprasmlist);
  534. cg.a_call_name(exprasmlist,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_CHECKZERO');
  535. cg.deallocallcpuregisters(exprasmlist);
  536. end;
  537. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  538. if is_ansistring(left.resulttype.def) then
  539. offsetdec:=1
  540. else
  541. offsetdec:=2;
  542. dec(location.reference.offset,offsetdec);
  543. end
  544. else if is_dynamic_array(left.resulttype.def) then
  545. begin
  546. case left.location.loc of
  547. LOC_REGISTER,
  548. LOC_CREGISTER :
  549. location.reference.base:=left.location.register;
  550. LOC_REFERENCE,
  551. LOC_CREFERENCE :
  552. begin
  553. location.reference.base:=cg.getaddressregister(exprasmlist);
  554. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,
  555. left.location.reference,location.reference.base);
  556. end;
  557. else
  558. internalerror(2002032219);
  559. end;
  560. end
  561. else
  562. location_copy(location,left.location);
  563. { location must be memory }
  564. if not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  565. internalerror(200411013);
  566. { offset can only differ from 0 if arraydef }
  567. if (left.resulttype.def.deftype=arraydef) and
  568. not(is_dynamic_array(left.resulttype.def)) then
  569. dec(location.reference.offset,mulsize*tarraydef(left.resulttype.def).lowrange);
  570. if right.nodetype=ordconstn then
  571. begin
  572. { offset can only differ from 0 if arraydef }
  573. case left.resulttype.def.deftype of
  574. arraydef :
  575. begin
  576. if not(is_open_array(left.resulttype.def)) and
  577. not(is_array_of_const(left.resulttype.def)) and
  578. not(is_dynamic_array(left.resulttype.def)) then
  579. begin
  580. if (tordconstnode(right).value>tarraydef(left.resulttype.def).highrange) or
  581. (tordconstnode(right).value<tarraydef(left.resulttype.def).lowrange) then
  582. begin
  583. { this should be caught in the resulttypepass! (JM) }
  584. if (cs_check_range in aktlocalswitches) then
  585. CGMessage(parser_e_range_check_error)
  586. else
  587. CGMessage(parser_w_range_check_error);
  588. end;
  589. end
  590. else
  591. begin
  592. { range checking for open and dynamic arrays needs
  593. runtime code }
  594. secondpass(right);
  595. if (cs_check_range in aktlocalswitches) then
  596. rangecheck_array;
  597. end;
  598. end;
  599. stringdef :
  600. begin
  601. if (cs_check_range in aktlocalswitches) then
  602. begin
  603. case tstringdef(left.resulttype.def).string_typ of
  604. { it's the same for ansi- and wide strings }
  605. st_widestring,
  606. {$ifdef ansistring_bits}
  607. st_ansistring16,st_ansistring32,st_ansistring64:
  608. {$else}
  609. st_ansistring:
  610. {$endif}
  611. begin
  612. paramanager.getintparaloc(pocall_default,1,paraloc1);
  613. paramanager.getintparaloc(pocall_default,2,paraloc2);
  614. paramanager.allocparaloc(exprasmlist,paraloc2);
  615. cg.a_param_const(exprasmlist,OS_INT,tordconstnode(right).value,paraloc2);
  616. href:=location.reference;
  617. dec(href.offset,sizeof(aint)-offsetdec);
  618. paramanager.allocparaloc(exprasmlist,paraloc1);
  619. cg.a_param_ref(exprasmlist,OS_INT,href,paraloc1);
  620. paramanager.freeparaloc(exprasmlist,paraloc1);
  621. paramanager.freeparaloc(exprasmlist,paraloc2);
  622. cg.allocallcpuregisters(exprasmlist);
  623. cg.a_call_name(exprasmlist,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  624. cg.deallocallcpuregisters(exprasmlist);
  625. end;
  626. st_shortstring:
  627. begin
  628. {!!!!!!!!!!!!!!!!!}
  629. end;
  630. st_longstring:
  631. begin
  632. {!!!!!!!!!!!!!!!!!}
  633. end;
  634. end;
  635. end;
  636. end;
  637. end;
  638. inc(location.reference.offset,
  639. mulsize*tordconstnode(right).value);
  640. end
  641. else
  642. { not nodetype=ordconstn }
  643. begin
  644. if (cs_regvars in aktglobalswitches) and
  645. { if we do range checking, we don't }
  646. { need that fancy code (it would be }
  647. { buggy) }
  648. not(cs_check_range in aktlocalswitches) and
  649. (left.resulttype.def.deftype=arraydef) then
  650. begin
  651. extraoffset:=0;
  652. if (right.nodetype=addn) then
  653. begin
  654. if taddnode(right).right.nodetype=ordconstn then
  655. begin
  656. extraoffset:=tordconstnode(taddnode(right).right).value;
  657. t:=taddnode(right).left;
  658. { First pass processed this with the assumption }
  659. { that there was an add node which may require an }
  660. { extra register. Fake it or die with IE10 (JM) }
  661. t.registersint := taddnode(right).registersint;
  662. taddnode(right).left:=nil;
  663. right.free;
  664. right:=t;
  665. end
  666. else if taddnode(right).left.nodetype=ordconstn then
  667. begin
  668. extraoffset:=tordconstnode(taddnode(right).left).value;
  669. t:=taddnode(right).right;
  670. t.registersint := right.registersint;
  671. taddnode(right).right:=nil;
  672. right.free;
  673. right:=t;
  674. end;
  675. end
  676. else if (right.nodetype=subn) then
  677. begin
  678. if taddnode(right).right.nodetype=ordconstn then
  679. begin
  680. extraoffset:=-tordconstnode(taddnode(right).right).value;
  681. t:=taddnode(right).left;
  682. t.registersint := right.registersint;
  683. taddnode(right).left:=nil;
  684. right.free;
  685. right:=t;
  686. end
  687. { You also have to negate right.right in this case! I can't add an
  688. unaryminusn without causing a crash, so I've disabled it (JM)
  689. else if right.left.nodetype=ordconstn then
  690. begin
  691. extraoffset:=right.left.value;
  692. t:=right.right;
  693. t^.registersint := right.registersint;
  694. putnode(right);
  695. putnode(right.left);
  696. right:=t;
  697. end;}
  698. end;
  699. inc(location.reference.offset,
  700. mulsize*extraoffset);
  701. end;
  702. { calculate from left to right }
  703. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  704. internalerror(200304237);
  705. isjump:=(right.location.loc=LOC_JUMP);
  706. if isjump then
  707. begin
  708. otl:=truelabel;
  709. objectlibrary.getlabel(truelabel);
  710. ofl:=falselabel;
  711. objectlibrary.getlabel(falselabel);
  712. end;
  713. secondpass(right);
  714. if cs_check_range in aktlocalswitches then
  715. begin
  716. if left.resulttype.def.deftype=arraydef then
  717. rangecheck_array;
  718. end;
  719. { if mulsize = 1, we won't have to modify the index }
  720. location_force_reg(exprasmlist,right.location,OS_ADDR,(mulsize = 1));
  721. if isjump then
  722. begin
  723. truelabel:=otl;
  724. falselabel:=ofl;
  725. end;
  726. { produce possible range check code: }
  727. if cs_check_range in aktlocalswitches then
  728. begin
  729. if left.resulttype.def.deftype=arraydef then
  730. begin
  731. { done defore (PM) }
  732. end
  733. else if (left.resulttype.def.deftype=stringdef) then
  734. begin
  735. case tstringdef(left.resulttype.def).string_typ of
  736. { it's the same for ansi- and wide strings }
  737. st_widestring,
  738. {$ifdef ansistring_bits}
  739. st_ansistring16,st_ansistring32,st_ansistring64:
  740. {$else}
  741. st_ansistring:
  742. {$endif}
  743. begin
  744. paramanager.getintparaloc(pocall_default,1,paraloc1);
  745. paramanager.getintparaloc(pocall_default,2,paraloc2);
  746. paramanager.allocparaloc(exprasmlist,paraloc2);
  747. cg.a_param_reg(exprasmlist,OS_INT,right.location.register,paraloc2);
  748. href:=location.reference;
  749. dec(href.offset,sizeof(aint)-offsetdec);
  750. //dec(href.offset,7);
  751. paramanager.allocparaloc(exprasmlist,paraloc1);
  752. cg.a_param_ref(exprasmlist,OS_INT,href,paraloc1);
  753. paramanager.freeparaloc(exprasmlist,paraloc1);
  754. paramanager.freeparaloc(exprasmlist,paraloc2);
  755. cg.allocallcpuregisters(exprasmlist);
  756. cg.a_call_name(exprasmlist,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  757. cg.deallocallcpuregisters(exprasmlist);
  758. end;
  759. st_shortstring:
  760. begin
  761. {!!!!!!!!!!!!!!!!!}
  762. end;
  763. st_longstring:
  764. begin
  765. {!!!!!!!!!!!!!!!!!}
  766. end;
  767. end;
  768. end;
  769. end;
  770. { insert the register and the multiplication factor in the
  771. reference }
  772. update_reference_reg_mul(right.location.register,mulsize);
  773. end;
  774. location.size:=newsize;
  775. paraloc1.done;
  776. paraloc2.done;
  777. end;
  778. begin
  779. cloadvmtaddrnode:=tcgloadvmtaddrnode;
  780. cloadparentfpnode:=tcgloadparentfpnode;
  781. caddrnode:=tcgaddrnode;
  782. cderefnode:=tcgderefnode;
  783. csubscriptnode:=tcgsubscriptnode;
  784. cwithnode:=tcgwithnode;
  785. cvecnode:=tcgvecnode;
  786. end.