ncgmem.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. LOC_CONSTANT:
  215. begin
  216. location.reference.offset:=left.location.value;
  217. end;
  218. else
  219. internalerror(200507031);
  220. end;
  221. if (cs_gdb_heaptrc in aktglobalswitches) and
  222. (cs_checkpointer in aktlocalswitches) and
  223. not(cs_compilesystem in aktmoduleswitches) and
  224. not(tpointerdef(left.resulttype.def).is_far) and
  225. not(nf_no_checkpointer in flags) then
  226. begin
  227. paraloc1.init;
  228. paramanager.getintparaloc(pocall_default,1,paraloc1);
  229. paramanager.allocparaloc(exprasmlist,paraloc1);
  230. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paraloc1);
  231. paramanager.freeparaloc(exprasmlist,paraloc1);
  232. paraloc1.done;
  233. cg.allocallcpuregisters(exprasmlist);
  234. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  235. cg.deallocallcpuregisters(exprasmlist);
  236. end;
  237. end;
  238. {*****************************************************************************
  239. TCGSUBSCRIPTNODE
  240. *****************************************************************************}
  241. procedure tcgsubscriptnode.pass_2;
  242. var
  243. paraloc1 : tcgpara;
  244. begin
  245. secondpass(left);
  246. if codegenerror then
  247. exit;
  248. paraloc1.init;
  249. { classes and interfaces must be dereferenced implicit }
  250. if is_class_or_interface(left.resulttype.def) then
  251. begin
  252. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  253. case left.location.loc of
  254. LOC_CREGISTER,
  255. LOC_REGISTER:
  256. begin
  257. {$ifdef cpu_uses_separate_address_registers}
  258. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  259. begin
  260. location.reference.base:=rg.getaddressregister(exprasmlist);
  261. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,
  262. left.location.register,location.reference.base);
  263. end
  264. else
  265. {$endif}
  266. location.reference.base := left.location.register;
  267. end;
  268. LOC_CREFERENCE,
  269. LOC_REFERENCE:
  270. begin
  271. location.reference.base:=cg.getaddressregister(exprasmlist);
  272. cg.a_load_loc_reg(exprasmlist,OS_ADDR,left.location,location.reference.base);
  273. end;
  274. end;
  275. { implicit deferencing }
  276. if (cs_gdb_heaptrc in aktglobalswitches) and
  277. (cs_checkpointer in aktlocalswitches) and
  278. not(cs_compilesystem in aktmoduleswitches) then
  279. begin
  280. paramanager.getintparaloc(pocall_default,1,paraloc1);
  281. paramanager.allocparaloc(exprasmlist,paraloc1);
  282. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paraloc1);
  283. paramanager.freeparaloc(exprasmlist,paraloc1);
  284. cg.allocallcpuregisters(exprasmlist);
  285. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  286. cg.deallocallcpuregisters(exprasmlist);
  287. end;
  288. end
  289. else if is_interfacecom(left.resulttype.def) then
  290. begin
  291. tg.GetTempTyped(exprasmlist,left.resulttype.def,tt_normal,location.reference);
  292. cg.a_load_loc_ref(exprasmlist,OS_ADDR,left.location,location.reference);
  293. { implicit deferencing also for interfaces }
  294. if (cs_gdb_heaptrc in aktglobalswitches) and
  295. (cs_checkpointer in aktlocalswitches) and
  296. not(cs_compilesystem in aktmoduleswitches) then
  297. begin
  298. paramanager.getintparaloc(pocall_default,1,paraloc1);
  299. paramanager.allocparaloc(exprasmlist,paraloc1);
  300. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paraloc1);
  301. paramanager.freeparaloc(exprasmlist,paraloc1);
  302. cg.allocallcpuregisters(exprasmlist);
  303. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  304. cg.deallocallcpuregisters(exprasmlist);
  305. end;
  306. end
  307. else
  308. location_copy(location,left.location);
  309. inc(location.reference.offset,vs.fieldoffset);
  310. { also update the size of the location }
  311. location.size:=def_cgsize(resulttype.def);
  312. paraloc1.done;
  313. end;
  314. {*****************************************************************************
  315. TCGWITHNODE
  316. *****************************************************************************}
  317. procedure tcgwithnode.pass_2;
  318. {$ifdef WITHNODEDEBUG}
  319. const
  320. withlevel : longint = 0;
  321. var
  322. withstartlabel,withendlabel : tasmlabel;
  323. pp : pchar;
  324. mangled_length : longint;
  325. refnode : tnode;
  326. {$endif WITHNODEDEBUG}
  327. begin
  328. location_reset(location,LOC_VOID,OS_NO);
  329. {$ifdef WITHNODEDEBUG}
  330. if (cs_debuginfo in aktmoduleswitches) then
  331. begin
  332. { load reference }
  333. if (withrefnode.nodetype=derefn) and
  334. (tderefnode(withrefnode).left.nodetype=temprefn) then
  335. refnode:=tderefnode(withrefnode).left
  336. else
  337. refnode:=withrefnode;
  338. secondpass(refnode);
  339. location_freetemp(exprasmlist,refnode.location);
  340. if not(refnode.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  341. internalerror(2003092810);
  342. inc(withlevel);
  343. objectlibrary.getaddrlabel(withstartlabel);
  344. objectlibrary.getaddrlabel(withendlabel);
  345. cg.a_label(exprasmlist,withstartlabel);
  346. al_withdebug.concat(Tai_stabs.Create(strpnew(
  347. '"with'+tostr(withlevel)+':'+tostr(symtablestack.getnewtypecount)+
  348. '=*'+tstoreddef(left.resulttype.def).numberstring+'",'+
  349. tostr(N_LSYM)+',0,0,'+tostr(refnode.location.reference.offset))));
  350. mangled_length:=length(current_procinfo.procdef.mangledname);
  351. getmem(pp,mangled_length+50);
  352. strpcopy(pp,'192,0,0,'+withstartlabel.name);
  353. if (target_info.use_function_relative_addresses) then
  354. begin
  355. strpcopy(strend(pp),'-');
  356. strpcopy(strend(pp),current_procinfo.procdef.mangledname);
  357. end;
  358. al_withdebug.concat(Tai_stabn.Create(strnew(pp)));
  359. end;
  360. {$endif WITHNODEDEBUG}
  361. if assigned(left) then
  362. secondpass(left);
  363. {$ifdef WITHNODEDEBUG}
  364. if (cs_debuginfo in aktmoduleswitches) then
  365. begin
  366. cg.a_label(exprasmlist,withendlabel);
  367. strpcopy(pp,'224,0,0,'+withendlabel.name);
  368. if (target_info.use_function_relative_addresses) then
  369. begin
  370. strpcopy(strend(pp),'-');
  371. strpcopy(strend(pp),current_procinfo.procdef.mangledname);
  372. end;
  373. al_withdebug.concat(Tai_stabn.Create(strnew(pp)));
  374. freemem(pp,mangled_length+50);
  375. dec(withlevel);
  376. end;
  377. {$endif WITHNODEDEBUG}
  378. end;
  379. {*****************************************************************************
  380. TCGVECNODE
  381. *****************************************************************************}
  382. function tcgvecnode.get_mul_size : aint;
  383. begin
  384. if nf_memindex in flags then
  385. get_mul_size:=1
  386. else
  387. begin
  388. if (left.resulttype.def.deftype=arraydef) then
  389. get_mul_size:=tarraydef(left.resulttype.def).elesize
  390. else
  391. get_mul_size:=resulttype.def.size;
  392. end
  393. end;
  394. procedure tcgvecnode.update_reference_reg_mul(reg:tregister;l:aint);
  395. var
  396. hreg: tregister;
  397. begin
  398. if location.reference.base=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.base:=reg;
  403. end
  404. else if location.reference.index=NR_NO then
  405. begin
  406. if l<>1 then
  407. cg.a_op_const_reg(exprasmlist,OP_IMUL,OS_ADDR,l,reg);
  408. location.reference.index:=reg;
  409. end
  410. else
  411. begin
  412. hreg := cg.getaddressregister(exprasmlist);
  413. cg.a_loadaddr_ref_reg(exprasmlist,location.reference,hreg);
  414. reference_reset_base(location.reference,hreg,0);
  415. { insert new index register }
  416. if l<>1 then
  417. cg.a_op_const_reg(exprasmlist,OP_IMUL,OS_ADDR,l,reg);
  418. location.reference.index:=reg;
  419. end;
  420. end;
  421. procedure tcgvecnode.second_wideansistring;
  422. begin
  423. end;
  424. procedure tcgvecnode.second_dynamicarray;
  425. begin
  426. end;
  427. procedure tcgvecnode.rangecheck_array;
  428. var
  429. hightree : tnode;
  430. poslabel,
  431. neglabel : tasmlabel;
  432. hreg : tregister;
  433. paraloc1,paraloc2 : tcgpara;
  434. begin
  435. paraloc1.init;
  436. paraloc2.init;
  437. if is_open_array(left.resulttype.def) or
  438. is_array_of_const(left.resulttype.def) then
  439. begin
  440. { cdecl functions don't have high() so we can not check the range }
  441. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  442. begin
  443. { Get high value }
  444. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  445. { it must be available }
  446. if not assigned(hightree) then
  447. internalerror(200212201);
  448. firstpass(hightree);
  449. secondpass(hightree);
  450. { generate compares }
  451. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  452. hreg:=cg.makeregsize(exprasmlist,right.location.register,OS_INT)
  453. else
  454. begin
  455. hreg:=cg.getintregister(exprasmlist,OS_INT);
  456. cg.a_load_loc_reg(exprasmlist,OS_INT,right.location,hreg);
  457. end;
  458. objectlibrary.getlabel(neglabel);
  459. objectlibrary.getlabel(poslabel);
  460. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_LT,0,hreg,poslabel);
  461. cg.a_cmp_loc_reg_label(exprasmlist,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  462. cg.a_label(exprasmlist,poslabel);
  463. cg.a_call_name(exprasmlist,'FPC_RANGEERROR');
  464. cg.a_label(exprasmlist,neglabel);
  465. { release hightree }
  466. hightree.free;
  467. end;
  468. end
  469. else
  470. if is_dynamic_array(left.resulttype.def) then
  471. begin
  472. paramanager.getintparaloc(pocall_default,1,paraloc1);
  473. paramanager.getintparaloc(pocall_default,2,paraloc2);
  474. paramanager.allocparaloc(exprasmlist,paraloc2);
  475. cg.a_param_loc(exprasmlist,right.location,paraloc2);
  476. paramanager.allocparaloc(exprasmlist,paraloc1);
  477. cg.a_param_loc(exprasmlist,left.location,paraloc1);
  478. paramanager.freeparaloc(exprasmlist,paraloc1);
  479. paramanager.freeparaloc(exprasmlist,paraloc2);
  480. cg.allocallcpuregisters(exprasmlist);
  481. cg.a_call_name(exprasmlist,'FPC_DYNARRAY_RANGECHECK');
  482. cg.deallocallcpuregisters(exprasmlist);
  483. end
  484. else
  485. cg.g_rangecheck(exprasmlist,right.location,right.resulttype.def,left.resulttype.def);
  486. paraloc1.done;
  487. paraloc2.done;
  488. end;
  489. procedure tcgvecnode.pass_2;
  490. var
  491. offsetdec,
  492. extraoffset : aint;
  493. t : tnode;
  494. href : treference;
  495. otl,ofl : tasmlabel;
  496. newsize : tcgsize;
  497. mulsize : aint;
  498. isjump : boolean;
  499. paraloc1,
  500. paraloc2 : tcgpara;
  501. begin
  502. paraloc1.init;
  503. paraloc2.init;
  504. mulsize := get_mul_size;
  505. newsize:=def_cgsize(resulttype.def);
  506. secondpass(left);
  507. if left.location.loc=LOC_CREFERENCE then
  508. location_reset(location,LOC_CREFERENCE,newsize)
  509. else
  510. location_reset(location,LOC_REFERENCE,newsize);
  511. { an ansistring needs to be dereferenced }
  512. if is_ansistring(left.resulttype.def) or
  513. is_widestring(left.resulttype.def) then
  514. begin
  515. if nf_callunique in flags then
  516. internalerror(200304236);
  517. {DM!!!!!}
  518. case left.location.loc of
  519. LOC_REGISTER,
  520. LOC_CREGISTER :
  521. location.reference.base:=left.location.register;
  522. LOC_CREFERENCE,
  523. LOC_REFERENCE :
  524. begin
  525. location.reference.base:=cg.getaddressregister(exprasmlist);
  526. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.reference,location.reference.base);
  527. end;
  528. else
  529. internalerror(2002032218);
  530. end;
  531. { check for a zero length string,
  532. we can use the ansistring routine here }
  533. if (cs_check_range in aktlocalswitches) then
  534. begin
  535. paramanager.getintparaloc(pocall_default,1,paraloc1);
  536. paramanager.allocparaloc(exprasmlist,paraloc1);
  537. cg.a_param_reg(exprasmlist,OS_ADDR,location.reference.base,paraloc1);
  538. paramanager.freeparaloc(exprasmlist,paraloc1);
  539. cg.allocallcpuregisters(exprasmlist);
  540. cg.a_call_name(exprasmlist,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_CHECKZERO');
  541. cg.deallocallcpuregisters(exprasmlist);
  542. end;
  543. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  544. if is_ansistring(left.resulttype.def) then
  545. offsetdec:=1
  546. else
  547. offsetdec:=2;
  548. dec(location.reference.offset,offsetdec);
  549. end
  550. else if is_dynamic_array(left.resulttype.def) then
  551. begin
  552. case left.location.loc of
  553. LOC_REGISTER,
  554. LOC_CREGISTER :
  555. location.reference.base:=left.location.register;
  556. LOC_REFERENCE,
  557. LOC_CREFERENCE :
  558. begin
  559. location.reference.base:=cg.getaddressregister(exprasmlist);
  560. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,
  561. left.location.reference,location.reference.base);
  562. end;
  563. else
  564. internalerror(2002032219);
  565. end;
  566. end
  567. else
  568. location_copy(location,left.location);
  569. { location must be memory }
  570. if not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  571. internalerror(200411013);
  572. { offset can only differ from 0 if arraydef }
  573. if (left.resulttype.def.deftype=arraydef) and
  574. not(is_dynamic_array(left.resulttype.def)) then
  575. dec(location.reference.offset,mulsize*tarraydef(left.resulttype.def).lowrange);
  576. if right.nodetype=ordconstn then
  577. begin
  578. { offset can only differ from 0 if arraydef }
  579. case left.resulttype.def.deftype of
  580. arraydef :
  581. begin
  582. if not(is_open_array(left.resulttype.def)) and
  583. not(is_array_of_const(left.resulttype.def)) and
  584. not(is_dynamic_array(left.resulttype.def)) then
  585. begin
  586. if (tordconstnode(right).value>tarraydef(left.resulttype.def).highrange) or
  587. (tordconstnode(right).value<tarraydef(left.resulttype.def).lowrange) then
  588. begin
  589. { this should be caught in the resulttypepass! (JM) }
  590. if (cs_check_range in aktlocalswitches) then
  591. CGMessage(parser_e_range_check_error)
  592. else
  593. CGMessage(parser_w_range_check_error);
  594. end;
  595. end
  596. else
  597. begin
  598. { range checking for open and dynamic arrays needs
  599. runtime code }
  600. secondpass(right);
  601. if (cs_check_range in aktlocalswitches) then
  602. rangecheck_array;
  603. end;
  604. end;
  605. stringdef :
  606. begin
  607. if (cs_check_range in aktlocalswitches) then
  608. begin
  609. case tstringdef(left.resulttype.def).string_typ of
  610. { it's the same for ansi- and wide strings }
  611. st_widestring,
  612. {$ifdef ansistring_bits}
  613. st_ansistring16,st_ansistring32,st_ansistring64:
  614. {$else}
  615. st_ansistring:
  616. {$endif}
  617. begin
  618. paramanager.getintparaloc(pocall_default,1,paraloc1);
  619. paramanager.getintparaloc(pocall_default,2,paraloc2);
  620. paramanager.allocparaloc(exprasmlist,paraloc2);
  621. cg.a_param_const(exprasmlist,OS_INT,tordconstnode(right).value,paraloc2);
  622. href:=location.reference;
  623. dec(href.offset,sizeof(aint)-offsetdec);
  624. paramanager.allocparaloc(exprasmlist,paraloc1);
  625. cg.a_param_ref(exprasmlist,OS_INT,href,paraloc1);
  626. paramanager.freeparaloc(exprasmlist,paraloc1);
  627. paramanager.freeparaloc(exprasmlist,paraloc2);
  628. cg.allocallcpuregisters(exprasmlist);
  629. cg.a_call_name(exprasmlist,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  630. cg.deallocallcpuregisters(exprasmlist);
  631. end;
  632. st_shortstring:
  633. begin
  634. {!!!!!!!!!!!!!!!!!}
  635. end;
  636. st_longstring:
  637. begin
  638. {!!!!!!!!!!!!!!!!!}
  639. end;
  640. end;
  641. end;
  642. end;
  643. end;
  644. inc(location.reference.offset,
  645. mulsize*tordconstnode(right).value);
  646. end
  647. else
  648. { not nodetype=ordconstn }
  649. begin
  650. if (cs_regvars in aktglobalswitches) and
  651. { if we do range checking, we don't }
  652. { need that fancy code (it would be }
  653. { buggy) }
  654. not(cs_check_range in aktlocalswitches) and
  655. (left.resulttype.def.deftype=arraydef) then
  656. begin
  657. extraoffset:=0;
  658. if (right.nodetype=addn) then
  659. begin
  660. if taddnode(right).right.nodetype=ordconstn then
  661. begin
  662. extraoffset:=tordconstnode(taddnode(right).right).value;
  663. t:=taddnode(right).left;
  664. { First pass processed this with the assumption }
  665. { that there was an add node which may require an }
  666. { extra register. Fake it or die with IE10 (JM) }
  667. t.registersint := taddnode(right).registersint;
  668. taddnode(right).left:=nil;
  669. right.free;
  670. right:=t;
  671. end
  672. else if taddnode(right).left.nodetype=ordconstn then
  673. begin
  674. extraoffset:=tordconstnode(taddnode(right).left).value;
  675. t:=taddnode(right).right;
  676. t.registersint := right.registersint;
  677. taddnode(right).right:=nil;
  678. right.free;
  679. right:=t;
  680. end;
  681. end
  682. else if (right.nodetype=subn) then
  683. begin
  684. if taddnode(right).right.nodetype=ordconstn then
  685. begin
  686. extraoffset:=-tordconstnode(taddnode(right).right).value;
  687. t:=taddnode(right).left;
  688. t.registersint := right.registersint;
  689. taddnode(right).left:=nil;
  690. right.free;
  691. right:=t;
  692. end
  693. { You also have to negate right.right in this case! I can't add an
  694. unaryminusn without causing a crash, so I've disabled it (JM)
  695. else if right.left.nodetype=ordconstn then
  696. begin
  697. extraoffset:=right.left.value;
  698. t:=right.right;
  699. t^.registersint := right.registersint;
  700. putnode(right);
  701. putnode(right.left);
  702. right:=t;
  703. end;}
  704. end;
  705. inc(location.reference.offset,
  706. mulsize*extraoffset);
  707. end;
  708. { calculate from left to right }
  709. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  710. internalerror(200304237);
  711. isjump:=(right.location.loc=LOC_JUMP);
  712. if isjump then
  713. begin
  714. otl:=truelabel;
  715. objectlibrary.getlabel(truelabel);
  716. ofl:=falselabel;
  717. objectlibrary.getlabel(falselabel);
  718. end;
  719. secondpass(right);
  720. if cs_check_range in aktlocalswitches then
  721. begin
  722. if left.resulttype.def.deftype=arraydef then
  723. rangecheck_array;
  724. end;
  725. { if mulsize = 1, we won't have to modify the index }
  726. location_force_reg(exprasmlist,right.location,OS_ADDR,(mulsize = 1));
  727. if isjump then
  728. begin
  729. truelabel:=otl;
  730. falselabel:=ofl;
  731. end;
  732. { produce possible range check code: }
  733. if cs_check_range in aktlocalswitches then
  734. begin
  735. if left.resulttype.def.deftype=arraydef then
  736. begin
  737. { done defore (PM) }
  738. end
  739. else if (left.resulttype.def.deftype=stringdef) then
  740. begin
  741. case tstringdef(left.resulttype.def).string_typ of
  742. { it's the same for ansi- and wide strings }
  743. st_widestring,
  744. {$ifdef ansistring_bits}
  745. st_ansistring16,st_ansistring32,st_ansistring64:
  746. {$else}
  747. st_ansistring:
  748. {$endif}
  749. begin
  750. paramanager.getintparaloc(pocall_default,1,paraloc1);
  751. paramanager.getintparaloc(pocall_default,2,paraloc2);
  752. paramanager.allocparaloc(exprasmlist,paraloc2);
  753. cg.a_param_reg(exprasmlist,OS_INT,right.location.register,paraloc2);
  754. href:=location.reference;
  755. dec(href.offset,sizeof(aint)-offsetdec);
  756. //dec(href.offset,7);
  757. paramanager.allocparaloc(exprasmlist,paraloc1);
  758. cg.a_param_ref(exprasmlist,OS_INT,href,paraloc1);
  759. paramanager.freeparaloc(exprasmlist,paraloc1);
  760. paramanager.freeparaloc(exprasmlist,paraloc2);
  761. cg.allocallcpuregisters(exprasmlist);
  762. cg.a_call_name(exprasmlist,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  763. cg.deallocallcpuregisters(exprasmlist);
  764. end;
  765. st_shortstring:
  766. begin
  767. {!!!!!!!!!!!!!!!!!}
  768. end;
  769. st_longstring:
  770. begin
  771. {!!!!!!!!!!!!!!!!!}
  772. end;
  773. end;
  774. end;
  775. end;
  776. { insert the register and the multiplication factor in the
  777. reference }
  778. update_reference_reg_mul(right.location.register,mulsize);
  779. end;
  780. location.size:=newsize;
  781. paraloc1.done;
  782. paraloc2.done;
  783. end;
  784. begin
  785. cloadvmtaddrnode:=tcgloadvmtaddrnode;
  786. cloadparentfpnode:=tcgloadparentfpnode;
  787. caddrnode:=tcgaddrnode;
  788. cderefnode:=tcgderefnode;
  789. csubscriptnode:=tcgsubscriptnode;
  790. cwithnode:=tcgwithnode;
  791. cvecnode:=tcgvecnode;
  792. end.