ncgmem.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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 update_reference_reg_packed(reg:tregister;l:aint);virtual;
  56. procedure second_wideansistring;virtual;
  57. procedure second_dynamicarray;virtual;
  58. public
  59. procedure pass_2;override;
  60. end;
  61. implementation
  62. uses
  63. systems,
  64. cutils,verbose,globals,
  65. symconst,symdef,symsym,symtable,defutil,paramgr,
  66. aasmbase,aasmtai,aasmdata,
  67. procinfo,pass_2,parabase,
  68. pass_1,nld,ncon,nadd,nutils,
  69. cgutils,cgobj,
  70. tgobj,ncgutil
  71. ;
  72. {*****************************************************************************
  73. TCGLOADVMTADDRNODE
  74. *****************************************************************************}
  75. procedure tcgloadvmtaddrnode.pass_2;
  76. var
  77. href : treference;
  78. begin
  79. location_reset(location,LOC_REGISTER,OS_ADDR);
  80. if (left.nodetype=typen) then
  81. begin
  82. reference_reset_symbol(href,
  83. current_asmdata.RefAsmSymbol(tobjectdef(tclassrefdef(resulttype.def).pointertype.def).vmt_mangledname),0);
  84. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  85. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,href,location.register);
  86. end
  87. else
  88. begin
  89. { left contains self, load vmt from self }
  90. secondpass(left);
  91. gen_load_vmt_register(current_asmdata.CurrAsmList,tobjectdef(left.resulttype.def),left.location,location.register);
  92. end;
  93. end;
  94. {*****************************************************************************
  95. TCGLOADPARENTFPNODE
  96. *****************************************************************************}
  97. procedure tcgloadparentfpnode.pass_2;
  98. var
  99. currpi : tprocinfo;
  100. hsym : tparavarsym;
  101. href : treference;
  102. begin
  103. if (current_procinfo.procdef.parast.symtablelevel=parentpd.parast.symtablelevel) then
  104. begin
  105. location_reset(location,LOC_REGISTER,OS_ADDR);
  106. location.register:=current_procinfo.framepointer;
  107. end
  108. else
  109. begin
  110. currpi:=current_procinfo;
  111. location_reset(location,LOC_REGISTER,OS_ADDR);
  112. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  113. { load framepointer of current proc }
  114. hsym:=tparavarsym(currpi.procdef.parast.search('parentfp'));
  115. if not assigned(hsym) then
  116. internalerror(200309281);
  117. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,hsym.localloc,location.register);
  118. { walk parents }
  119. while (currpi.procdef.owner.symtablelevel>parentpd.parast.symtablelevel) do
  120. begin
  121. currpi:=currpi.parent;
  122. if not assigned(currpi) then
  123. internalerror(200311201);
  124. hsym:=tparavarsym(currpi.procdef.parast.search('parentfp'));
  125. if not assigned(hsym) then
  126. internalerror(200309282);
  127. if hsym.localloc.loc<>LOC_REFERENCE then
  128. internalerror(200309283);
  129. reference_reset_base(href,location.register,hsym.localloc.reference.offset);
  130. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,location.register);
  131. end;
  132. end;
  133. end;
  134. {*****************************************************************************
  135. TCGADDRNODE
  136. *****************************************************************************}
  137. procedure tcgaddrnode.pass_2;
  138. begin
  139. secondpass(left);
  140. location_reset(location,LOC_REGISTER,OS_ADDR);
  141. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  142. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  143. end;
  144. {*****************************************************************************
  145. TCGDEREFNODE
  146. *****************************************************************************}
  147. procedure tcgderefnode.pass_2;
  148. var
  149. paraloc1 : tcgpara;
  150. begin
  151. secondpass(left);
  152. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  153. case left.location.loc of
  154. LOC_CREGISTER,
  155. LOC_REGISTER:
  156. begin
  157. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,true);
  158. {$ifdef cpu_uses_separate_address_registers}
  159. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  160. begin
  161. location.reference.base := cg.getaddressregister(current_asmdata.CurrAsmList);
  162. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,
  163. location.reference.base);
  164. end
  165. else
  166. {$endif}
  167. location.reference.base := left.location.register;
  168. end;
  169. LOC_CREFERENCE,
  170. LOC_REFERENCE:
  171. begin
  172. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  173. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,left.location,location.reference.base);
  174. end;
  175. LOC_CONSTANT:
  176. begin
  177. location.reference.offset:=left.location.value;
  178. end;
  179. else
  180. internalerror(200507031);
  181. end;
  182. if (cs_use_heaptrc in aktglobalswitches) and
  183. (cs_checkpointer in aktlocalswitches) and
  184. not(cs_compilesystem in aktmoduleswitches) and
  185. not(tpointerdef(left.resulttype.def).is_far) and
  186. not(nf_no_checkpointer in flags) then
  187. begin
  188. paraloc1.init;
  189. paramanager.getintparaloc(pocall_default,1,paraloc1);
  190. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  191. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  192. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  193. paraloc1.done;
  194. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  195. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER');
  196. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  197. end;
  198. end;
  199. {*****************************************************************************
  200. TCGSUBSCRIPTNODE
  201. *****************************************************************************}
  202. procedure tcgsubscriptnode.pass_2;
  203. var
  204. paraloc1 : tcgpara;
  205. sref: tsubsetreference;
  206. begin
  207. secondpass(left);
  208. if codegenerror then
  209. exit;
  210. paraloc1.init;
  211. { classes and interfaces must be dereferenced implicit }
  212. if is_class_or_interface(left.resulttype.def) then
  213. begin
  214. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  215. case left.location.loc of
  216. LOC_CREGISTER,
  217. LOC_REGISTER:
  218. begin
  219. {$ifdef cpu_uses_separate_address_registers}
  220. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  221. begin
  222. location.reference.base:=rg.getaddressregister(current_asmdata.CurrAsmList);
  223. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  224. left.location.register,location.reference.base);
  225. end
  226. else
  227. {$endif}
  228. location.reference.base := left.location.register;
  229. end;
  230. LOC_CREFERENCE,
  231. LOC_REFERENCE:
  232. begin
  233. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  234. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,left.location,location.reference.base);
  235. end;
  236. end;
  237. { implicit deferencing }
  238. if (cs_use_heaptrc in aktglobalswitches) and
  239. (cs_checkpointer in aktlocalswitches) and
  240. not(cs_compilesystem in aktmoduleswitches) then
  241. begin
  242. paramanager.getintparaloc(pocall_default,1,paraloc1);
  243. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  244. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  245. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  246. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  247. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER');
  248. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  249. end;
  250. end
  251. else if is_interfacecom(left.resulttype.def) then
  252. begin
  253. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  254. tg.GetTempTyped(current_asmdata.CurrAsmList,left.resulttype.def,tt_normal,location.reference);
  255. cg.a_load_loc_ref(current_asmdata.CurrAsmList,OS_ADDR,left.location,location.reference);
  256. { implicit deferencing also for interfaces }
  257. if (cs_use_heaptrc in aktglobalswitches) and
  258. (cs_checkpointer in aktlocalswitches) and
  259. not(cs_compilesystem in aktmoduleswitches) then
  260. begin
  261. paramanager.getintparaloc(pocall_default,1,paraloc1);
  262. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  263. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  264. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  265. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  266. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER');
  267. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  268. end;
  269. end
  270. else
  271. begin
  272. location_copy(location,left.location);
  273. { some abi's require that functions return (some) records in }
  274. { registers }
  275. case location.loc of
  276. LOC_REFERENCE,
  277. LOC_CREFERENCE:
  278. ;
  279. LOC_REGISTER,
  280. LOC_CREGISTER:
  281. begin
  282. if (left.resulttype.def.size > sizeof(aint)) then
  283. location_force_mem(current_asmdata.CurrAsmList,location)
  284. else
  285. begin
  286. if (left.location.loc = LOC_REGISTER) then
  287. location.loc := LOC_SUBSETREG
  288. else
  289. location.loc := LOC_CSUBSETREG;
  290. location.size:=def_cgsize(resulttype.def);
  291. location.sreg.subsetreg := left.location.register;
  292. location.sreg.subsetregsize := left.location.size;
  293. if not is_packed_record_or_object(left.resulttype.def) then
  294. begin
  295. if (target_info.endian = ENDIAN_BIG) then
  296. location.sreg.startbit := (tcgsize2size[location.sreg.subsetregsize] - tcgsize2size[location.size] - vs.fieldoffset) * 8
  297. else
  298. location.sreg.startbit := (vs.fieldoffset * 8);
  299. location.sreg.bitlen := tcgsize2size[location.size] * 8;
  300. end
  301. else
  302. begin
  303. location.sreg.bitlen := resulttype.def.packedbitsize;
  304. if (target_info.endian = ENDIAN_BIG) then
  305. location.sreg.startbit := (tcgsize2size[location.sreg.subsetregsize]*8 - location.sreg.bitlen) - vs.fieldoffset
  306. else
  307. location.sreg.startbit := vs.fieldoffset;
  308. end;
  309. end;
  310. end;
  311. LOC_SUBSETREG,
  312. LOC_CSUBSETREG:
  313. begin
  314. location.size:=def_cgsize(resulttype.def);
  315. if (target_info.endian = ENDIAN_BIG) then
  316. inc(location.sreg.startbit, (left.resulttype.def.size - tcgsize2size[location.size] - vs.fieldoffset) * 8)
  317. else
  318. inc(location.sreg.startbit, vs.fieldoffset * 8);
  319. location.sreg.bitlen := tcgsize2size[location.size] * 8;
  320. end;
  321. else
  322. internalerror(2006031901);
  323. end;
  324. end;
  325. if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  326. begin
  327. if not is_packed_record_or_object(left.resulttype.def) then
  328. begin
  329. inc(location.reference.offset,vs.fieldoffset);
  330. {$ifdef SUPPORT_UNALIGNED}
  331. { packed? }
  332. {$warning unalignment check does not work since usefieldalignment is not stored in ppu}
  333. if (vs.owner.defowner.deftype in [recorddef,objectdef]) and
  334. (tabstractrecordsymtable(vs.owner).usefieldalignment=1) then
  335. location.reference.alignment:=1;
  336. {$endif SUPPORT_UNALIGNED}
  337. end
  338. else if (vs.fieldoffset mod 8 = 0) and
  339. (resulttype.def.packedbitsize mod 8 = 0) and
  340. { is different in case of e.g. packenum 2 and an enum }
  341. { which fits in 8 bits }
  342. (resulttype.def.size*8 = resulttype.def.packedbitsize) then
  343. begin
  344. inc(location.reference.offset,vs.fieldoffset div 8);
  345. if (resulttype.def.size*8 <> resulttype.def.packedbitsize) then
  346. internalerror(2006082013);
  347. { packed records always have an alignment of 1 }
  348. location.reference.alignment:=1;
  349. end
  350. else
  351. begin
  352. sref.ref:=location.reference;
  353. sref.ref.alignment:=1;
  354. sref.bitindexreg:=NR_NO;
  355. inc(sref.ref.offset,vs.fieldoffset div 8);
  356. sref.startbit:=vs.fieldoffset mod 8;
  357. sref.bitlen:=resulttype.def.packedbitsize;
  358. if (left.location.loc=LOC_REFERENCE) then
  359. location.loc:=LOC_SUBSETREF
  360. else
  361. location.loc:=LOC_CSUBSETREF;
  362. location.sref:=sref;
  363. end;
  364. { also update the size of the location }
  365. location.size:=def_cgsize(resulttype.def);
  366. end;
  367. paraloc1.done;
  368. end;
  369. {*****************************************************************************
  370. TCGWITHNODE
  371. *****************************************************************************}
  372. procedure tcgwithnode.pass_2;
  373. begin
  374. location_reset(location,LOC_VOID,OS_NO);
  375. if assigned(left) then
  376. secondpass(left);
  377. end;
  378. {*****************************************************************************
  379. TCGVECNODE
  380. *****************************************************************************}
  381. function tcgvecnode.get_mul_size : aint;
  382. begin
  383. if nf_memindex in flags then
  384. get_mul_size:=1
  385. else
  386. begin
  387. if (left.resulttype.def.deftype=arraydef) then
  388. if not is_packed_array(left.resulttype.def) then
  389. get_mul_size:=tarraydef(left.resulttype.def).elesize
  390. else
  391. get_mul_size:=tarraydef(left.resulttype.def).elepackedbitsize
  392. else
  393. get_mul_size:=resulttype.def.size;
  394. end
  395. end;
  396. procedure tcgvecnode.update_reference_reg_mul(reg:tregister;l:aint);
  397. var
  398. hreg: tregister;
  399. begin
  400. if location.reference.base=NR_NO then
  401. begin
  402. if l<>1 then
  403. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  404. location.reference.base:=reg;
  405. end
  406. else if location.reference.index=NR_NO then
  407. begin
  408. if l<>1 then
  409. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  410. location.reference.index:=reg;
  411. end
  412. else
  413. begin
  414. hreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  415. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,location.reference,hreg);
  416. reference_reset_base(location.reference,hreg,0);
  417. { insert new index register }
  418. if l<>1 then
  419. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  420. location.reference.index:=reg;
  421. end;
  422. end;
  423. procedure tcgvecnode.update_reference_reg_packed(reg:tregister;l:aint);
  424. var
  425. sref: tsubsetreference;
  426. offsetreg: tregister;
  427. byteoffs, bitoffs, alignpower: aint;
  428. temp : longint;
  429. begin
  430. if (l mod 8) = 0 then
  431. begin
  432. update_reference_reg_mul(reg,l div 8);
  433. exit;
  434. end;
  435. if (l > 8*sizeof(aint)) then
  436. internalerror(200608051);
  437. sref.ref := location.reference;
  438. offsetreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  439. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,tarraydef(left.resulttype.def).lowrange,reg);
  440. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_INT,l,reg);
  441. { keep alignment for index }
  442. sref.ref.alignment := left.resulttype.def.alignment;
  443. if not ispowerof2(sref.ref.alignment,temp) then
  444. internalerror(2006081201);
  445. alignpower:=temp;
  446. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_ADDR,3+alignpower,reg,offsetreg);
  447. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,alignpower,offsetreg);
  448. if (sref.ref.base = NR_NO) then
  449. sref.ref.base := offsetreg
  450. else if (sref.ref.index = NR_NO) then
  451. sref.ref.index := offsetreg
  452. else
  453. begin
  454. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,sref.ref.base,offsetreg);
  455. sref.ref.base := offsetreg;
  456. end;
  457. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,(1 shl (3+alignpower))-1,reg);
  458. sref.bitindexreg := reg;
  459. sref.startbit := 0;
  460. sref.bitlen := resulttype.def.packedbitsize;
  461. if (left.location.loc = LOC_REFERENCE) then
  462. location.loc := LOC_SUBSETREF
  463. else
  464. location.loc := LOC_CSUBSETREF;
  465. location.sref := sref;
  466. end;
  467. procedure tcgvecnode.second_wideansistring;
  468. begin
  469. end;
  470. procedure tcgvecnode.second_dynamicarray;
  471. begin
  472. end;
  473. procedure tcgvecnode.rangecheck_array;
  474. var
  475. hightree : tnode;
  476. poslabel,
  477. neglabel : tasmlabel;
  478. hreg : tregister;
  479. paraloc1,paraloc2 : tcgpara;
  480. begin
  481. paraloc1.init;
  482. paraloc2.init;
  483. if is_open_array(left.resulttype.def) or
  484. is_array_of_const(left.resulttype.def) then
  485. begin
  486. { cdecl functions don't have high() so we can not check the range }
  487. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  488. begin
  489. { Get high value }
  490. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  491. { it must be available }
  492. if not assigned(hightree) then
  493. internalerror(200212201);
  494. firstpass(hightree);
  495. secondpass(hightree);
  496. { generate compares }
  497. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  498. hreg:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_INT)
  499. else
  500. begin
  501. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  502. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_INT,right.location,hreg);
  503. end;
  504. current_asmdata.getjumplabel(neglabel);
  505. current_asmdata.getjumplabel(poslabel);
  506. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_LT,0,hreg,poslabel);
  507. cg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  508. cg.a_label(current_asmdata.CurrAsmList,poslabel);
  509. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RANGEERROR');
  510. cg.a_label(current_asmdata.CurrAsmList,neglabel);
  511. { release hightree }
  512. hightree.free;
  513. end;
  514. end
  515. else
  516. if is_dynamic_array(left.resulttype.def) then
  517. begin
  518. paramanager.getintparaloc(pocall_default,1,paraloc1);
  519. paramanager.getintparaloc(pocall_default,2,paraloc2);
  520. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  521. cg.a_param_loc(current_asmdata.CurrAsmList,right.location,paraloc2);
  522. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  523. cg.a_param_loc(current_asmdata.CurrAsmList,left.location,paraloc1);
  524. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  525. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  526. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  527. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DYNARRAY_RANGECHECK');
  528. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  529. end
  530. else
  531. cg.g_rangecheck(current_asmdata.CurrAsmList,right.location,right.resulttype.def,left.resulttype.def);
  532. paraloc1.done;
  533. paraloc2.done;
  534. end;
  535. procedure tcgvecnode.pass_2;
  536. var
  537. offsetdec,
  538. extraoffset : aint;
  539. t : tnode;
  540. href : treference;
  541. otl,ofl : tasmlabel;
  542. newsize : tcgsize;
  543. mulsize,
  544. bytemulsize,
  545. alignpow : aint;
  546. isjump : boolean;
  547. paraloc1,
  548. paraloc2 : tcgpara;
  549. subsetref : tsubsetreference;
  550. temp : longint;
  551. begin
  552. paraloc1.init;
  553. paraloc2.init;
  554. mulsize:=get_mul_size;
  555. if not is_packed_array(left.resulttype.def) then
  556. bytemulsize:=mulsize
  557. else
  558. bytemulsize:=mulsize div 8;
  559. newsize:=def_cgsize(resulttype.def);
  560. secondpass(left);
  561. if left.location.loc=LOC_CREFERENCE then
  562. location_reset(location,LOC_CREFERENCE,newsize)
  563. else
  564. location_reset(location,LOC_REFERENCE,newsize);
  565. { an ansistring needs to be dereferenced }
  566. if is_ansistring(left.resulttype.def) or
  567. is_widestring(left.resulttype.def) then
  568. begin
  569. if nf_callunique in flags then
  570. internalerror(200304236);
  571. {DM!!!!!}
  572. case left.location.loc of
  573. LOC_REGISTER,
  574. LOC_CREGISTER :
  575. location.reference.base:=left.location.register;
  576. LOC_CREFERENCE,
  577. LOC_REFERENCE :
  578. begin
  579. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  580. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.reference.base);
  581. end;
  582. else
  583. internalerror(2002032218);
  584. end;
  585. { check for a zero length string,
  586. we can use the ansistring routine here }
  587. if (cs_check_range in aktlocalswitches) then
  588. begin
  589. paramanager.getintparaloc(pocall_default,1,paraloc1);
  590. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  591. cg.a_param_reg(current_asmdata.CurrAsmList,OS_ADDR,location.reference.base,paraloc1);
  592. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  593. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  594. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_CHECKZERO');
  595. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  596. end;
  597. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  598. if is_ansistring(left.resulttype.def) then
  599. offsetdec:=1
  600. else
  601. offsetdec:=2;
  602. dec(location.reference.offset,offsetdec);
  603. end
  604. else if is_dynamic_array(left.resulttype.def) then
  605. begin
  606. case left.location.loc of
  607. LOC_REGISTER,
  608. LOC_CREGISTER :
  609. location.reference.base:=left.location.register;
  610. LOC_REFERENCE,
  611. LOC_CREFERENCE :
  612. begin
  613. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  614. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  615. left.location.reference,location.reference.base);
  616. end;
  617. else
  618. internalerror(2002032219);
  619. end;
  620. end
  621. else
  622. location_copy(location,left.location);
  623. { location must be memory }
  624. if not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  625. internalerror(200411013);
  626. { offset can only differ from 0 if arraydef }
  627. if (left.resulttype.def.deftype=arraydef) and
  628. not(is_dynamic_array(left.resulttype.def)) and
  629. (not(is_packed_array(left.resulttype.def)) or
  630. (mulsize mod 8 = 0)) then
  631. dec(location.reference.offset,bytemulsize*tarraydef(left.resulttype.def).lowrange);
  632. if right.nodetype=ordconstn then
  633. begin
  634. { offset can only differ from 0 if arraydef }
  635. case left.resulttype.def.deftype of
  636. arraydef :
  637. begin
  638. if not(is_open_array(left.resulttype.def)) and
  639. not(is_array_of_const(left.resulttype.def)) and
  640. not(is_dynamic_array(left.resulttype.def)) then
  641. begin
  642. if (tordconstnode(right).value>tarraydef(left.resulttype.def).highrange) or
  643. (tordconstnode(right).value<tarraydef(left.resulttype.def).lowrange) then
  644. begin
  645. { this should be caught in the resulttypepass! (JM) }
  646. if (cs_check_range in aktlocalswitches) then
  647. CGMessage(parser_e_range_check_error)
  648. else
  649. CGMessage(parser_w_range_check_error);
  650. end;
  651. end
  652. else
  653. begin
  654. { range checking for open and dynamic arrays needs
  655. runtime code }
  656. secondpass(right);
  657. if (cs_check_range in aktlocalswitches) then
  658. rangecheck_array;
  659. end;
  660. end;
  661. stringdef :
  662. begin
  663. if (cs_check_range in aktlocalswitches) then
  664. begin
  665. case tstringdef(left.resulttype.def).string_typ of
  666. { it's the same for ansi- and wide strings }
  667. st_widestring,
  668. st_ansistring:
  669. begin
  670. paramanager.getintparaloc(pocall_default,1,paraloc1);
  671. paramanager.getintparaloc(pocall_default,2,paraloc2);
  672. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  673. cg.a_param_const(current_asmdata.CurrAsmList,OS_INT,tordconstnode(right).value,paraloc2);
  674. href:=location.reference;
  675. dec(href.offset,sizeof(aint)-offsetdec);
  676. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  677. cg.a_param_ref(current_asmdata.CurrAsmList,OS_INT,href,paraloc1);
  678. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  679. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  680. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  681. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  682. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  683. end;
  684. st_shortstring:
  685. begin
  686. {!!!!!!!!!!!!!!!!!}
  687. { if this one is implemented making use of the high parameter for openshortstrings, update ncgutils.do_get_used_regvars() too (JM) }
  688. end;
  689. st_longstring:
  690. begin
  691. {!!!!!!!!!!!!!!!!!}
  692. end;
  693. end;
  694. end;
  695. end;
  696. end;
  697. if not(is_packed_array(left.resulttype.def)) or
  698. (mulsize mod 8 = 0) then
  699. inc(location.reference.offset,
  700. bytemulsize*tordconstnode(right).value)
  701. else
  702. begin
  703. subsetref.ref := location.reference;
  704. subsetref.ref.alignment := left.resulttype.def.alignment;
  705. if not ispowerof2(subsetref.ref.alignment,temp) then
  706. internalerror(2006081212);
  707. alignpow:=temp;
  708. inc(subsetref.ref.offset,((mulsize * (tordconstnode(right).value-tarraydef(left.resulttype.def).lowrange)) shr (3+alignpow)) shl alignpow);
  709. subsetref.bitindexreg := NR_NO;
  710. subsetref.startbit := (mulsize * (tordconstnode(right).value-tarraydef(left.resulttype.def).lowrange)) and ((1 shl (3+alignpow))-1);
  711. subsetref.bitlen := resulttype.def.packedbitsize;
  712. if (left.location.loc = LOC_REFERENCE) then
  713. location.loc := LOC_SUBSETREF
  714. else
  715. location.loc := LOC_CSUBSETREF;
  716. location.sref := subsetref;
  717. end;
  718. end
  719. else
  720. { not nodetype=ordconstn }
  721. begin
  722. if (cs_opt_level1 in aktoptimizerswitches) and
  723. { if we do range checking, we don't }
  724. { need that fancy code (it would be }
  725. { buggy) }
  726. not(cs_check_range in aktlocalswitches) and
  727. (left.resulttype.def.deftype=arraydef) and
  728. not is_packed_array(left.resulttype.def) then
  729. begin
  730. extraoffset:=0;
  731. if (right.nodetype=addn) then
  732. begin
  733. if taddnode(right).right.nodetype=ordconstn then
  734. begin
  735. extraoffset:=tordconstnode(taddnode(right).right).value;
  736. t:=taddnode(right).left;
  737. { First pass processed this with the assumption }
  738. { that there was an add node which may require an }
  739. { extra register. Fake it or die with IE10 (JM) }
  740. t.registersint := taddnode(right).registersint;
  741. taddnode(right).left:=nil;
  742. right.free;
  743. right:=t;
  744. end
  745. else if taddnode(right).left.nodetype=ordconstn then
  746. begin
  747. extraoffset:=tordconstnode(taddnode(right).left).value;
  748. t:=taddnode(right).right;
  749. t.registersint := right.registersint;
  750. taddnode(right).right:=nil;
  751. right.free;
  752. right:=t;
  753. end;
  754. end
  755. else if (right.nodetype=subn) then
  756. begin
  757. if taddnode(right).right.nodetype=ordconstn then
  758. begin
  759. extraoffset:=-tordconstnode(taddnode(right).right).value;
  760. t:=taddnode(right).left;
  761. t.registersint := right.registersint;
  762. taddnode(right).left:=nil;
  763. right.free;
  764. right:=t;
  765. end
  766. { You also have to negate right.right in this case! I can't add an
  767. unaryminusn without causing a crash, so I've disabled it (JM)
  768. else if right.left.nodetype=ordconstn then
  769. begin
  770. extraoffset:=right.left.value;
  771. t:=right.right;
  772. t^.registersint := right.registersint;
  773. putnode(right);
  774. putnode(right.left);
  775. right:=t;
  776. end;}
  777. end;
  778. inc(location.reference.offset,
  779. mulsize*extraoffset);
  780. end;
  781. { calculate from left to right }
  782. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  783. internalerror(200304237);
  784. isjump:=(right.expectloc=LOC_JUMP);
  785. if isjump then
  786. begin
  787. otl:=current_procinfo.CurrTrueLabel;
  788. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  789. ofl:=current_procinfo.CurrFalseLabel;
  790. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  791. end;
  792. secondpass(right);
  793. { if mulsize = 1, we won't have to modify the index }
  794. location_force_reg(current_asmdata.CurrAsmList,right.location,OS_ADDR,not is_packed_array(left.resulttype.def) and (mulsize = 1) );
  795. if isjump then
  796. begin
  797. current_procinfo.CurrTrueLabel:=otl;
  798. current_procinfo.CurrFalseLabel:=ofl;
  799. end
  800. else if (right.location.loc = LOC_JUMP) then
  801. internalerror(2006010801);
  802. { only range check now, we can't range check loc_flags/loc_jump }
  803. if cs_check_range in aktlocalswitches then
  804. begin
  805. if left.resulttype.def.deftype=arraydef then
  806. rangecheck_array;
  807. end;
  808. { produce possible range check code: }
  809. if cs_check_range in aktlocalswitches then
  810. begin
  811. if left.resulttype.def.deftype=arraydef then
  812. begin
  813. { done defore (PM) }
  814. end
  815. else if (left.resulttype.def.deftype=stringdef) then
  816. begin
  817. case tstringdef(left.resulttype.def).string_typ of
  818. { it's the same for ansi- and wide strings }
  819. st_widestring,
  820. st_ansistring:
  821. begin
  822. paramanager.getintparaloc(pocall_default,1,paraloc1);
  823. paramanager.getintparaloc(pocall_default,2,paraloc2);
  824. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  825. cg.a_param_reg(current_asmdata.CurrAsmList,OS_INT,right.location.register,paraloc2);
  826. href:=location.reference;
  827. dec(href.offset,sizeof(aint)-offsetdec);
  828. //dec(href.offset,7);
  829. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  830. cg.a_param_ref(current_asmdata.CurrAsmList,OS_INT,href,paraloc1);
  831. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  832. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  833. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  834. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  835. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  836. end;
  837. st_shortstring:
  838. begin
  839. {!!!!!!!!!!!!!!!!!}
  840. end;
  841. st_longstring:
  842. begin
  843. {!!!!!!!!!!!!!!!!!}
  844. end;
  845. end;
  846. end;
  847. end;
  848. { insert the register and the multiplication factor in the
  849. reference }
  850. if not is_packed_array(left.resulttype.def) then
  851. update_reference_reg_mul(right.location.register,mulsize)
  852. else
  853. update_reference_reg_packed(right.location.register,mulsize);
  854. end;
  855. location.size:=newsize;
  856. paraloc1.done;
  857. paraloc2.done;
  858. end;
  859. begin
  860. cloadvmtaddrnode:=tcgloadvmtaddrnode;
  861. cloadparentfpnode:=tcgloadparentfpnode;
  862. caddrnode:=tcgaddrnode;
  863. cderefnode:=tcgderefnode;
  864. csubscriptnode:=tcgsubscriptnode;
  865. cwithnode:=tcgwithnode;
  866. cvecnode:=tcgvecnode;
  867. end.