ncgmem.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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,symtable,defutil,paramgr,
  65. aasmbase,aasmtai,aasmdata,
  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. current_asmdata.RefAsmSymbol(tobjectdef(tclassrefdef(resulttype.def).pointertype.def).vmt_mangledname),0);
  83. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  84. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,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(current_asmdata.CurrAsmList,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(current_asmdata.CurrAsmList);
  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(current_asmdata.CurrAsmList,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(current_asmdata.CurrAsmList,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(current_asmdata.CurrAsmList);
  141. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,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_CREGISTER,
  154. LOC_REGISTER:
  155. begin
  156. {$ifdef cpu_uses_separate_address_registers}
  157. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  158. begin
  159. location.reference.base := cg.getaddressregister(current_asmdata.CurrAsmList);
  160. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,
  161. location.reference.base);
  162. end
  163. else
  164. {$endif}
  165. location.reference.base := left.location.register;
  166. end;
  167. LOC_CREFERENCE,
  168. LOC_REFERENCE:
  169. begin
  170. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  171. cg.a_load_loc_reg(current_asmdata.CurrAsmList,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_use_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(current_asmdata.CurrAsmList,paraloc1);
  189. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  190. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  191. paraloc1.done;
  192. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  193. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER');
  194. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  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(current_asmdata.CurrAsmList);
  220. cg.a_load_reg_reg(current_asmdata.CurrAsmList,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(current_asmdata.CurrAsmList);
  231. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,left.location,location.reference.base);
  232. end;
  233. end;
  234. { implicit deferencing }
  235. if (cs_use_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(current_asmdata.CurrAsmList,paraloc1);
  241. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  242. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  243. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  244. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER');
  245. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  246. end;
  247. end
  248. else if is_interfacecom(left.resulttype.def) then
  249. begin
  250. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  251. tg.GetTempTyped(current_asmdata.CurrAsmList,left.resulttype.def,tt_normal,location.reference);
  252. cg.a_load_loc_ref(current_asmdata.CurrAsmList,OS_ADDR,left.location,location.reference);
  253. { implicit deferencing also for interfaces }
  254. if (cs_use_heaptrc in aktglobalswitches) and
  255. (cs_checkpointer in aktlocalswitches) and
  256. not(cs_compilesystem in aktmoduleswitches) then
  257. begin
  258. paramanager.getintparaloc(pocall_default,1,paraloc1);
  259. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  260. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  261. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  262. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  263. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER');
  264. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  265. end;
  266. end
  267. else
  268. begin
  269. location_copy(location,left.location);
  270. { some abi's require that functions return (some) records in }
  271. { registers }
  272. case location.loc of
  273. LOC_REFERENCE,
  274. LOC_CREFERENCE:
  275. ;
  276. LOC_REGISTER,
  277. LOC_CREGISTER:
  278. begin
  279. if (left.resulttype.def.size > sizeof(aint)) then
  280. location_force_mem(current_asmdata.CurrAsmList,location)
  281. else
  282. begin
  283. if (left.location.loc = LOC_REGISTER) then
  284. location.loc := LOC_SUBSETREG
  285. else
  286. location.loc := LOC_CSUBSETREG;
  287. location.size:=def_cgsize(resulttype.def);
  288. location.sreg.subsetreg := left.location.register;
  289. location.sreg.subsetregsize := left.location.size;
  290. if (target_info.endian = ENDIAN_BIG) then
  291. location.sreg.startbit := (tcgsize2size[location.sreg.subsetregsize] - tcgsize2size[location.size] - vs.fieldoffset) * 8
  292. else
  293. location.sreg.startbit := (vs.fieldoffset * 8);
  294. location.sreg.bitlen := tcgsize2size[location.size] * 8;
  295. end;
  296. end;
  297. LOC_SUBSETREG,
  298. LOC_CSUBSETREG:
  299. begin
  300. location.size:=def_cgsize(resulttype.def);
  301. if (target_info.endian = ENDIAN_BIG) then
  302. inc(location.sreg.startbit, (left.resulttype.def.size - tcgsize2size[location.size] - vs.fieldoffset) * 8)
  303. else
  304. inc(location.sreg.startbit, vs.fieldoffset * 8);
  305. location.sreg.bitlen := tcgsize2size[location.size] * 8;
  306. end;
  307. else
  308. internalerror(2006031901);
  309. end;
  310. end;
  311. if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  312. begin
  313. inc(location.reference.offset,vs.fieldoffset);
  314. {$ifdef SUPPORT_UNALIGNED}
  315. { packed? }
  316. if (vs.owner.defowner.deftype in [recorddef,objectdef]) and
  317. (tabstractrecordsymtable(vs.owner).usefieldalignment=1) then
  318. location.reference.alignment:=1;
  319. {$endif SUPPORT_UNALIGNED}
  320. { also update the size of the location }
  321. location.size:=def_cgsize(resulttype.def);
  322. end;
  323. paraloc1.done;
  324. end;
  325. {*****************************************************************************
  326. TCGWITHNODE
  327. *****************************************************************************}
  328. procedure tcgwithnode.pass_2;
  329. begin
  330. location_reset(location,LOC_VOID,OS_NO);
  331. if assigned(left) then
  332. secondpass(left);
  333. end;
  334. {*****************************************************************************
  335. TCGVECNODE
  336. *****************************************************************************}
  337. function tcgvecnode.get_mul_size : aint;
  338. begin
  339. if nf_memindex in flags then
  340. get_mul_size:=1
  341. else
  342. begin
  343. if (left.resulttype.def.deftype=arraydef) then
  344. get_mul_size:=tarraydef(left.resulttype.def).elesize
  345. else
  346. get_mul_size:=resulttype.def.size;
  347. end
  348. end;
  349. procedure tcgvecnode.update_reference_reg_mul(reg:tregister;l:aint);
  350. var
  351. hreg: tregister;
  352. begin
  353. if location.reference.base=NR_NO then
  354. begin
  355. if l<>1 then
  356. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  357. location.reference.base:=reg;
  358. end
  359. else if location.reference.index=NR_NO then
  360. begin
  361. if l<>1 then
  362. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  363. location.reference.index:=reg;
  364. end
  365. else
  366. begin
  367. hreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  368. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,location.reference,hreg);
  369. reference_reset_base(location.reference,hreg,0);
  370. { insert new index register }
  371. if l<>1 then
  372. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  373. location.reference.index:=reg;
  374. end;
  375. end;
  376. procedure tcgvecnode.second_wideansistring;
  377. begin
  378. end;
  379. procedure tcgvecnode.second_dynamicarray;
  380. begin
  381. end;
  382. procedure tcgvecnode.rangecheck_array;
  383. var
  384. hightree : tnode;
  385. poslabel,
  386. neglabel : tasmlabel;
  387. hreg : tregister;
  388. paraloc1,paraloc2 : tcgpara;
  389. begin
  390. paraloc1.init;
  391. paraloc2.init;
  392. if is_open_array(left.resulttype.def) or
  393. is_array_of_const(left.resulttype.def) then
  394. begin
  395. { cdecl functions don't have high() so we can not check the range }
  396. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  397. begin
  398. { Get high value }
  399. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  400. { it must be available }
  401. if not assigned(hightree) then
  402. internalerror(200212201);
  403. firstpass(hightree);
  404. secondpass(hightree);
  405. { generate compares }
  406. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  407. hreg:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_INT)
  408. else
  409. begin
  410. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  411. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_INT,right.location,hreg);
  412. end;
  413. current_asmdata.getjumplabel(neglabel);
  414. current_asmdata.getjumplabel(poslabel);
  415. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_LT,0,hreg,poslabel);
  416. cg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  417. cg.a_label(current_asmdata.CurrAsmList,poslabel);
  418. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RANGEERROR');
  419. cg.a_label(current_asmdata.CurrAsmList,neglabel);
  420. { release hightree }
  421. hightree.free;
  422. end;
  423. end
  424. else
  425. if is_dynamic_array(left.resulttype.def) then
  426. begin
  427. paramanager.getintparaloc(pocall_default,1,paraloc1);
  428. paramanager.getintparaloc(pocall_default,2,paraloc2);
  429. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  430. cg.a_param_loc(current_asmdata.CurrAsmList,right.location,paraloc2);
  431. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  432. cg.a_param_loc(current_asmdata.CurrAsmList,left.location,paraloc1);
  433. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  434. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  435. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  436. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DYNARRAY_RANGECHECK');
  437. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  438. end
  439. else
  440. cg.g_rangecheck(current_asmdata.CurrAsmList,right.location,right.resulttype.def,left.resulttype.def);
  441. paraloc1.done;
  442. paraloc2.done;
  443. end;
  444. procedure tcgvecnode.pass_2;
  445. var
  446. offsetdec,
  447. extraoffset : aint;
  448. t : tnode;
  449. href : treference;
  450. otl,ofl : tasmlabel;
  451. newsize : tcgsize;
  452. mulsize : aint;
  453. isjump : boolean;
  454. paraloc1,
  455. paraloc2 : tcgpara;
  456. begin
  457. paraloc1.init;
  458. paraloc2.init;
  459. mulsize := get_mul_size;
  460. newsize:=def_cgsize(resulttype.def);
  461. secondpass(left);
  462. if left.location.loc=LOC_CREFERENCE then
  463. location_reset(location,LOC_CREFERENCE,newsize)
  464. else
  465. location_reset(location,LOC_REFERENCE,newsize);
  466. { an ansistring needs to be dereferenced }
  467. if is_ansistring(left.resulttype.def) or
  468. is_widestring(left.resulttype.def) then
  469. begin
  470. if nf_callunique in flags then
  471. internalerror(200304236);
  472. {DM!!!!!}
  473. case left.location.loc of
  474. LOC_REGISTER,
  475. LOC_CREGISTER :
  476. location.reference.base:=left.location.register;
  477. LOC_CREFERENCE,
  478. LOC_REFERENCE :
  479. begin
  480. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  481. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.reference.base);
  482. end;
  483. else
  484. internalerror(2002032218);
  485. end;
  486. { check for a zero length string,
  487. we can use the ansistring routine here }
  488. if (cs_check_range in aktlocalswitches) then
  489. begin
  490. paramanager.getintparaloc(pocall_default,1,paraloc1);
  491. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  492. cg.a_param_reg(current_asmdata.CurrAsmList,OS_ADDR,location.reference.base,paraloc1);
  493. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  494. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  495. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_CHECKZERO');
  496. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  497. end;
  498. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  499. if is_ansistring(left.resulttype.def) then
  500. offsetdec:=1
  501. else
  502. offsetdec:=2;
  503. dec(location.reference.offset,offsetdec);
  504. end
  505. else if is_dynamic_array(left.resulttype.def) then
  506. begin
  507. case left.location.loc of
  508. LOC_REGISTER,
  509. LOC_CREGISTER :
  510. location.reference.base:=left.location.register;
  511. LOC_REFERENCE,
  512. LOC_CREFERENCE :
  513. begin
  514. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  515. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  516. left.location.reference,location.reference.base);
  517. end;
  518. else
  519. internalerror(2002032219);
  520. end;
  521. end
  522. else
  523. location_copy(location,left.location);
  524. { location must be memory }
  525. if not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  526. internalerror(200411013);
  527. { offset can only differ from 0 if arraydef }
  528. if (left.resulttype.def.deftype=arraydef) and
  529. not(is_dynamic_array(left.resulttype.def)) then
  530. dec(location.reference.offset,mulsize*tarraydef(left.resulttype.def).lowrange);
  531. if right.nodetype=ordconstn then
  532. begin
  533. { offset can only differ from 0 if arraydef }
  534. case left.resulttype.def.deftype of
  535. arraydef :
  536. begin
  537. if not(is_open_array(left.resulttype.def)) and
  538. not(is_array_of_const(left.resulttype.def)) and
  539. not(is_dynamic_array(left.resulttype.def)) then
  540. begin
  541. if (tordconstnode(right).value>tarraydef(left.resulttype.def).highrange) or
  542. (tordconstnode(right).value<tarraydef(left.resulttype.def).lowrange) then
  543. begin
  544. { this should be caught in the resulttypepass! (JM) }
  545. if (cs_check_range in aktlocalswitches) then
  546. CGMessage(parser_e_range_check_error)
  547. else
  548. CGMessage(parser_w_range_check_error);
  549. end;
  550. end
  551. else
  552. begin
  553. { range checking for open and dynamic arrays needs
  554. runtime code }
  555. secondpass(right);
  556. if (cs_check_range in aktlocalswitches) then
  557. rangecheck_array;
  558. end;
  559. end;
  560. stringdef :
  561. begin
  562. if (cs_check_range in aktlocalswitches) then
  563. begin
  564. case tstringdef(left.resulttype.def).string_typ of
  565. { it's the same for ansi- and wide strings }
  566. st_widestring,
  567. st_ansistring:
  568. begin
  569. paramanager.getintparaloc(pocall_default,1,paraloc1);
  570. paramanager.getintparaloc(pocall_default,2,paraloc2);
  571. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  572. cg.a_param_const(current_asmdata.CurrAsmList,OS_INT,tordconstnode(right).value,paraloc2);
  573. href:=location.reference;
  574. dec(href.offset,sizeof(aint)-offsetdec);
  575. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  576. cg.a_param_ref(current_asmdata.CurrAsmList,OS_INT,href,paraloc1);
  577. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  578. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  579. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  580. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  581. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  582. end;
  583. st_shortstring:
  584. begin
  585. {!!!!!!!!!!!!!!!!!}
  586. { if this one is implemented making use of the high parameter for openshortstrings, update ncgutils.do_get_used_regvars() too (JM) }
  587. end;
  588. st_longstring:
  589. begin
  590. {!!!!!!!!!!!!!!!!!}
  591. end;
  592. end;
  593. end;
  594. end;
  595. end;
  596. inc(location.reference.offset,
  597. mulsize*tordconstnode(right).value);
  598. end
  599. else
  600. { not nodetype=ordconstn }
  601. begin
  602. if (cs_opt_regvar in aktoptimizerswitches) and
  603. { if we do range checking, we don't }
  604. { need that fancy code (it would be }
  605. { buggy) }
  606. not(cs_check_range in aktlocalswitches) and
  607. (left.resulttype.def.deftype=arraydef) then
  608. begin
  609. extraoffset:=0;
  610. if (right.nodetype=addn) then
  611. begin
  612. if taddnode(right).right.nodetype=ordconstn then
  613. begin
  614. extraoffset:=tordconstnode(taddnode(right).right).value;
  615. t:=taddnode(right).left;
  616. { First pass processed this with the assumption }
  617. { that there was an add node which may require an }
  618. { extra register. Fake it or die with IE10 (JM) }
  619. t.registersint := taddnode(right).registersint;
  620. taddnode(right).left:=nil;
  621. right.free;
  622. right:=t;
  623. end
  624. else if taddnode(right).left.nodetype=ordconstn then
  625. begin
  626. extraoffset:=tordconstnode(taddnode(right).left).value;
  627. t:=taddnode(right).right;
  628. t.registersint := right.registersint;
  629. taddnode(right).right:=nil;
  630. right.free;
  631. right:=t;
  632. end;
  633. end
  634. else if (right.nodetype=subn) then
  635. begin
  636. if taddnode(right).right.nodetype=ordconstn then
  637. begin
  638. extraoffset:=-tordconstnode(taddnode(right).right).value;
  639. t:=taddnode(right).left;
  640. t.registersint := right.registersint;
  641. taddnode(right).left:=nil;
  642. right.free;
  643. right:=t;
  644. end
  645. { You also have to negate right.right in this case! I can't add an
  646. unaryminusn without causing a crash, so I've disabled it (JM)
  647. else if right.left.nodetype=ordconstn then
  648. begin
  649. extraoffset:=right.left.value;
  650. t:=right.right;
  651. t^.registersint := right.registersint;
  652. putnode(right);
  653. putnode(right.left);
  654. right:=t;
  655. end;}
  656. end;
  657. inc(location.reference.offset,
  658. mulsize*extraoffset);
  659. end;
  660. { calculate from left to right }
  661. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  662. internalerror(200304237);
  663. isjump:=(right.expectloc=LOC_JUMP);
  664. if isjump then
  665. begin
  666. otl:=current_procinfo.CurrTrueLabel;
  667. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  668. ofl:=current_procinfo.CurrFalseLabel;
  669. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  670. end;
  671. secondpass(right);
  672. { if mulsize = 1, we won't have to modify the index }
  673. location_force_reg(current_asmdata.CurrAsmList,right.location,OS_ADDR,(mulsize = 1));
  674. if isjump then
  675. begin
  676. current_procinfo.CurrTrueLabel:=otl;
  677. current_procinfo.CurrFalseLabel:=ofl;
  678. end
  679. else if (right.location.loc = LOC_JUMP) then
  680. internalerror(2006010801);
  681. { only range check now, we can't range check loc_flags/loc_jump }
  682. if cs_check_range in aktlocalswitches then
  683. begin
  684. if left.resulttype.def.deftype=arraydef then
  685. rangecheck_array;
  686. end;
  687. { produce possible range check code: }
  688. if cs_check_range in aktlocalswitches then
  689. begin
  690. if left.resulttype.def.deftype=arraydef then
  691. begin
  692. { done defore (PM) }
  693. end
  694. else if (left.resulttype.def.deftype=stringdef) then
  695. begin
  696. case tstringdef(left.resulttype.def).string_typ of
  697. { it's the same for ansi- and wide strings }
  698. st_widestring,
  699. st_ansistring:
  700. begin
  701. paramanager.getintparaloc(pocall_default,1,paraloc1);
  702. paramanager.getintparaloc(pocall_default,2,paraloc2);
  703. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  704. cg.a_param_reg(current_asmdata.CurrAsmList,OS_INT,right.location.register,paraloc2);
  705. href:=location.reference;
  706. dec(href.offset,sizeof(aint)-offsetdec);
  707. //dec(href.offset,7);
  708. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  709. cg.a_param_ref(current_asmdata.CurrAsmList,OS_INT,href,paraloc1);
  710. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  711. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  712. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  713. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  714. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  715. end;
  716. st_shortstring:
  717. begin
  718. {!!!!!!!!!!!!!!!!!}
  719. end;
  720. st_longstring:
  721. begin
  722. {!!!!!!!!!!!!!!!!!}
  723. end;
  724. end;
  725. end;
  726. end;
  727. { insert the register and the multiplication factor in the
  728. reference }
  729. update_reference_reg_mul(right.location.register,mulsize);
  730. end;
  731. location.size:=newsize;
  732. paraloc1.done;
  733. paraloc2.done;
  734. end;
  735. begin
  736. cloadvmtaddrnode:=tcgloadvmtaddrnode;
  737. cloadparentfpnode:=tcgloadparentfpnode;
  738. caddrnode:=tcgaddrnode;
  739. cderefnode:=tcgderefnode;
  740. csubscriptnode:=tcgsubscriptnode;
  741. cwithnode:=tcgwithnode;
  742. cvecnode:=tcgvecnode;
  743. end.