ncgmem.pas 40 KB

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