ncgmem.pas 33 KB

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