ncgmem.pas 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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_generate_code;override;
  27. end;
  28. tcgloadparentfpnode = class(tloadparentfpnode)
  29. procedure pass_generate_code;override;
  30. end;
  31. tcgaddrnode = class(taddrnode)
  32. procedure pass_generate_code;override;
  33. end;
  34. tcgderefnode = class(tderefnode)
  35. procedure pass_generate_code;override;
  36. end;
  37. tcgsubscriptnode = class(tsubscriptnode)
  38. procedure pass_generate_code;override;
  39. end;
  40. tcgwithnode = class(twithnode)
  41. procedure pass_generate_code;override;
  42. end;
  43. tcgvecnode = class(tvecnode)
  44. function get_mul_size : aint;
  45. private
  46. procedure rangecheck_array;
  47. protected
  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(maybe_const_reg:tregister;l:aint);virtual;
  55. procedure update_reference_reg_packed(maybe_const_reg:tregister;l:aint);virtual;
  56. procedure second_wideansistring;virtual;
  57. procedure second_dynamicarray;virtual;
  58. public
  59. procedure pass_generate_code;override;
  60. end;
  61. implementation
  62. uses
  63. systems,
  64. cutils,verbose,globals,constexp,
  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_generate_code;
  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(resultdef).pointeddef).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.resultdef),left.location,location.register);
  92. end;
  93. end;
  94. {*****************************************************************************
  95. TCGLOADPARENTFPNODE
  96. *****************************************************************************}
  97. procedure tcgloadparentfpnode.pass_generate_code;
  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.Find('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.Find('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_generate_code;
  138. begin
  139. secondpass(left);
  140. location_reset(location,LOC_REGISTER,OS_ADDR);
  141. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  142. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  143. internalerror(2006111510);
  144. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  145. end;
  146. {*****************************************************************************
  147. TCGDEREFNODE
  148. *****************************************************************************}
  149. procedure tcgderefnode.pass_generate_code;
  150. var
  151. paraloc1 : tcgpara;
  152. begin
  153. secondpass(left);
  154. location_reset(location,LOC_REFERENCE,def_cgsize(resultdef));
  155. case left.location.loc of
  156. LOC_CREGISTER,
  157. LOC_REGISTER:
  158. begin
  159. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,true);
  160. {$ifdef cpu_uses_separate_address_registers}
  161. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  162. begin
  163. location.reference.base := cg.getaddressregister(current_asmdata.CurrAsmList);
  164. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,
  165. location.reference.base);
  166. end
  167. else
  168. {$endif}
  169. location.reference.base := left.location.register;
  170. end;
  171. LOC_CREFERENCE,
  172. LOC_REFERENCE:
  173. begin
  174. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  175. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,left.location,location.reference.base);
  176. end;
  177. LOC_CONSTANT:
  178. begin
  179. location.reference.offset:=left.location.value;
  180. end;
  181. else
  182. internalerror(200507031);
  183. end;
  184. if (cs_use_heaptrc in current_settings.globalswitches) and
  185. (cs_checkpointer in current_settings.localswitches) and
  186. not(cs_compilesystem in current_settings.moduleswitches) and
  187. not(tpointerdef(left.resultdef).is_far) and
  188. not(nf_no_checkpointer in flags) then
  189. begin
  190. paraloc1.init;
  191. paramanager.getintparaloc(pocall_default,1,paraloc1);
  192. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  193. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  194. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  195. paraloc1.done;
  196. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  197. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER');
  198. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  199. end;
  200. end;
  201. {*****************************************************************************
  202. TCGSUBSCRIPTNODE
  203. *****************************************************************************}
  204. procedure tcgsubscriptnode.pass_generate_code;
  205. var
  206. paraloc1 : tcgpara;
  207. sref: tsubsetreference;
  208. begin
  209. secondpass(left);
  210. if codegenerror then
  211. exit;
  212. paraloc1.init;
  213. { classes and interfaces must be dereferenced implicit }
  214. if is_class_or_interface(left.resultdef) then
  215. begin
  216. location_reset(location,LOC_REFERENCE,def_cgsize(resultdef));
  217. case left.location.loc of
  218. LOC_CREGISTER,
  219. LOC_REGISTER:
  220. begin
  221. {$ifdef cpu_uses_separate_address_registers}
  222. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  223. begin
  224. location.reference.base:=rg.getaddressregister(current_asmdata.CurrAsmList);
  225. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  226. left.location.register,location.reference.base);
  227. end
  228. else
  229. {$endif}
  230. location.reference.base := left.location.register;
  231. end;
  232. LOC_CREFERENCE,
  233. LOC_REFERENCE:
  234. begin
  235. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  236. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,left.location,location.reference.base);
  237. end;
  238. end;
  239. { implicit deferencing }
  240. if (cs_use_heaptrc in current_settings.globalswitches) and
  241. (cs_checkpointer in current_settings.localswitches) and
  242. not(cs_compilesystem in current_settings.moduleswitches) then
  243. begin
  244. paramanager.getintparaloc(pocall_default,1,paraloc1);
  245. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  246. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  247. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  248. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  249. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER');
  250. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  251. end;
  252. end
  253. else if is_interfacecom(left.resultdef) then
  254. begin
  255. location_reset(location,LOC_REFERENCE,def_cgsize(resultdef));
  256. tg.GetTempTyped(current_asmdata.CurrAsmList,left.resultdef,tt_normal,location.reference);
  257. cg.a_load_loc_ref(current_asmdata.CurrAsmList,OS_ADDR,left.location,location.reference);
  258. { implicit deferencing also for interfaces }
  259. if (cs_use_heaptrc in current_settings.globalswitches) and
  260. (cs_checkpointer in current_settings.localswitches) and
  261. not(cs_compilesystem in current_settings.moduleswitches) then
  262. begin
  263. paramanager.getintparaloc(pocall_default,1,paraloc1);
  264. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  265. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  266. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  267. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  268. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER');
  269. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  270. end;
  271. end
  272. else
  273. begin
  274. location_copy(location,left.location);
  275. { some abi's require that functions return (some) records in }
  276. { registers }
  277. case location.loc of
  278. LOC_REFERENCE,
  279. LOC_CREFERENCE:
  280. ;
  281. LOC_REGISTER,
  282. LOC_CREGISTER:
  283. begin
  284. if (left.resultdef.size > sizeof(pint)) then
  285. location_force_mem(current_asmdata.CurrAsmList,location)
  286. else
  287. begin
  288. if (left.location.loc = LOC_REGISTER) then
  289. location.loc := LOC_SUBSETREG
  290. else
  291. location.loc := LOC_CSUBSETREG;
  292. location.size:=def_cgsize(resultdef);
  293. location.sreg.subsetreg := left.location.register;
  294. location.sreg.subsetregsize := left.location.size;
  295. if not is_packed_record_or_object(left.resultdef) then
  296. begin
  297. if (target_info.endian = ENDIAN_BIG) then
  298. location.sreg.startbit := (tcgsize2size[location.sreg.subsetregsize] - tcgsize2size[location.size] - vs.fieldoffset) * 8
  299. else
  300. location.sreg.startbit := (vs.fieldoffset * 8);
  301. location.sreg.bitlen := tcgsize2size[location.size] * 8;
  302. end
  303. else
  304. begin
  305. location.sreg.bitlen := resultdef.packedbitsize;
  306. if (target_info.endian = ENDIAN_BIG) then
  307. location.sreg.startbit := (tcgsize2size[location.sreg.subsetregsize]*8 - location.sreg.bitlen) - vs.fieldoffset
  308. else
  309. location.sreg.startbit := vs.fieldoffset;
  310. end;
  311. end;
  312. end;
  313. LOC_SUBSETREG,
  314. LOC_CSUBSETREG:
  315. begin
  316. location.size:=def_cgsize(resultdef);
  317. if not is_packed_record_or_object(left.resultdef) then
  318. begin
  319. if (target_info.endian = ENDIAN_BIG) then
  320. inc(location.sreg.startbit, (left.resultdef.size - tcgsize2size[location.size] - vs.fieldoffset) * 8)
  321. else
  322. inc(location.sreg.startbit, vs.fieldoffset * 8);
  323. location.sreg.bitlen := tcgsize2size[location.size] * 8;
  324. end
  325. else
  326. begin
  327. location.sreg.bitlen := resultdef.packedbitsize;
  328. if (target_info.endian = ENDIAN_BIG) then
  329. inc(location.sreg.startbit, left.location.sreg.bitlen - location.sreg.bitlen - vs.fieldoffset)
  330. else
  331. inc(location.sreg.startbit, vs.fieldoffset);
  332. end;
  333. end;
  334. else
  335. internalerror(2006031901);
  336. end;
  337. end;
  338. if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  339. begin
  340. if not is_packed_record_or_object(left.resultdef) then
  341. begin
  342. inc(location.reference.offset,vs.fieldoffset);
  343. {$ifdef SUPPORT_UNALIGNED}
  344. { packed? }
  345. if (vs.owner.defowner.typ in [recorddef,objectdef]) and
  346. (tabstractrecordsymtable(vs.owner).usefieldalignment=1) then
  347. location.reference.alignment:=1;
  348. {$endif SUPPORT_UNALIGNED}
  349. end
  350. else if (vs.fieldoffset mod 8 = 0) and
  351. (resultdef.packedbitsize mod 8 = 0) and
  352. { is different in case of e.g. packenum 2 and an enum }
  353. { which fits in 8 bits }
  354. (resultdef.size*8 = resultdef.packedbitsize) then
  355. begin
  356. inc(location.reference.offset,vs.fieldoffset div 8);
  357. if (resultdef.size*8 <> resultdef.packedbitsize) then
  358. internalerror(2006082013);
  359. { packed records always have an alignment of 1 }
  360. location.reference.alignment:=1;
  361. end
  362. else
  363. begin
  364. sref.ref:=location.reference;
  365. sref.ref.alignment:=1;
  366. sref.bitindexreg:=NR_NO;
  367. inc(sref.ref.offset,vs.fieldoffset div 8);
  368. sref.startbit:=vs.fieldoffset mod 8;
  369. sref.bitlen:=resultdef.packedbitsize;
  370. if (left.location.loc=LOC_REFERENCE) then
  371. location.loc:=LOC_SUBSETREF
  372. else
  373. location.loc:=LOC_CSUBSETREF;
  374. location.sref:=sref;
  375. end;
  376. { also update the size of the location }
  377. location.size:=def_cgsize(resultdef);
  378. end;
  379. paraloc1.done;
  380. end;
  381. {*****************************************************************************
  382. TCGWITHNODE
  383. *****************************************************************************}
  384. procedure tcgwithnode.pass_generate_code;
  385. begin
  386. location_reset(location,LOC_VOID,OS_NO);
  387. if assigned(left) then
  388. secondpass(left);
  389. end;
  390. {*****************************************************************************
  391. TCGVECNODE
  392. *****************************************************************************}
  393. function tcgvecnode.get_mul_size : aint;
  394. begin
  395. if nf_memindex in flags then
  396. get_mul_size:=1
  397. else
  398. begin
  399. if (left.resultdef.typ=arraydef) then
  400. if not is_packed_array(left.resultdef) then
  401. get_mul_size:=tarraydef(left.resultdef).elesize
  402. else
  403. get_mul_size:=tarraydef(left.resultdef).elepackedbitsize
  404. else
  405. get_mul_size:=resultdef.size;
  406. end
  407. end;
  408. { this routine must, like any other routine, not change the contents }
  409. { of base/index registers of references, as these may be regvars. }
  410. { The register allocator can coalesce one LOC_REGISTER being moved }
  411. { into another (as their live ranges won't overlap), but not a }
  412. { LOC_CREGISTER moved into a LOC_(C)REGISTER most of the time (as }
  413. { the live range of the LOC_CREGISTER will most likely overlap the }
  414. { the live range of the target LOC_(C)REGISTER) }
  415. { The passed register may be a LOC_CREGISTER as well. }
  416. procedure tcgvecnode.update_reference_reg_mul(maybe_const_reg:tregister;l:aint);
  417. var
  418. hreg: tregister;
  419. begin
  420. if l<>1 then
  421. begin
  422. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  423. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,maybe_const_reg,hreg);
  424. maybe_const_reg:=hreg;
  425. end;
  426. if location.reference.base=NR_NO then
  427. location.reference.base:=maybe_const_reg
  428. else if location.reference.index=NR_NO then
  429. location.reference.index:=maybe_const_reg
  430. else
  431. begin
  432. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  433. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,location.reference,hreg);
  434. reference_reset_base(location.reference,hreg,0);
  435. { insert new index register }
  436. location.reference.index:=maybe_const_reg;
  437. end;
  438. end;
  439. { see remarks for tcgvecnode.update_reference_reg_mul above }
  440. procedure tcgvecnode.update_reference_reg_packed(maybe_const_reg:tregister;l:aint);
  441. var
  442. sref: tsubsetreference;
  443. offsetreg, hreg: tregister;
  444. alignpower: aint;
  445. temp : longint;
  446. begin
  447. { only orddefs are bitpacked. Even then we only need special code in }
  448. { case the bitpacked *byte size* is not a power of two, otherwise }
  449. { everything can be handled using the the regular array code. }
  450. if ((l mod 8) = 0) and
  451. (ispowerof2(l div 8,temp) or
  452. not is_ordinal(resultdef)) then
  453. begin
  454. update_reference_reg_mul(maybe_const_reg,l div 8);
  455. exit;
  456. end;
  457. if (l > 8*sizeof(aint)) then
  458. internalerror(200608051);
  459. sref.ref := location.reference;
  460. hreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  461. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,tarraydef(left.resultdef).lowrange,maybe_const_reg,hreg);
  462. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_INT,l,hreg);
  463. { keep alignment for index }
  464. sref.ref.alignment := left.resultdef.alignment;
  465. if not ispowerof2(sref.ref.alignment,temp) then
  466. internalerror(2006081201);
  467. alignpower:=temp;
  468. offsetreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  469. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_ADDR,3+alignpower,hreg,offsetreg);
  470. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,alignpower,offsetreg);
  471. if (sref.ref.base = NR_NO) then
  472. sref.ref.base := offsetreg
  473. else if (sref.ref.index = NR_NO) then
  474. sref.ref.index := offsetreg
  475. else
  476. begin
  477. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,sref.ref.base,offsetreg);
  478. sref.ref.base := offsetreg;
  479. end;
  480. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,(1 shl (3+alignpower))-1,hreg);
  481. sref.bitindexreg := hreg;
  482. sref.startbit := 0;
  483. sref.bitlen := resultdef.packedbitsize;
  484. if (left.location.loc = LOC_REFERENCE) then
  485. location.loc := LOC_SUBSETREF
  486. else
  487. location.loc := LOC_CSUBSETREF;
  488. location.sref := sref;
  489. end;
  490. procedure tcgvecnode.second_wideansistring;
  491. begin
  492. end;
  493. procedure tcgvecnode.second_dynamicarray;
  494. begin
  495. end;
  496. procedure tcgvecnode.rangecheck_array;
  497. var
  498. hightree : tnode;
  499. poslabel,
  500. neglabel : tasmlabel;
  501. hreg : tregister;
  502. paraloc1,paraloc2 : tcgpara;
  503. begin
  504. paraloc1.init;
  505. paraloc2.init;
  506. if is_open_array(left.resultdef) or
  507. is_array_of_const(left.resultdef) then
  508. begin
  509. { cdecl functions don't have high() so we can not check the range }
  510. { (can't use current_procdef, since it may be a nested procedure) }
  511. if not(tprocdef(tparasymtable(tparavarsym(tloadnode(left).symtableentry).owner).defowner).proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  512. begin
  513. { Get high value }
  514. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  515. { it must be available }
  516. if not assigned(hightree) then
  517. internalerror(200212201);
  518. firstpass(hightree);
  519. secondpass(hightree);
  520. { generate compares }
  521. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  522. hreg:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_INT)
  523. else
  524. begin
  525. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  526. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_INT,right.location,hreg);
  527. end;
  528. current_asmdata.getjumplabel(neglabel);
  529. current_asmdata.getjumplabel(poslabel);
  530. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_LT,0,hreg,poslabel);
  531. cg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  532. cg.a_label(current_asmdata.CurrAsmList,poslabel);
  533. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RANGEERROR');
  534. cg.a_label(current_asmdata.CurrAsmList,neglabel);
  535. { release hightree }
  536. hightree.free;
  537. end;
  538. end
  539. else
  540. if is_dynamic_array(left.resultdef) then
  541. begin
  542. paramanager.getintparaloc(pocall_default,1,paraloc1);
  543. paramanager.getintparaloc(pocall_default,2,paraloc2);
  544. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  545. cg.a_param_loc(current_asmdata.CurrAsmList,right.location,paraloc2);
  546. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  547. cg.a_param_loc(current_asmdata.CurrAsmList,left.location,paraloc1);
  548. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  549. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  550. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  551. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DYNARRAY_RANGECHECK');
  552. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  553. end
  554. else
  555. cg.g_rangecheck(current_asmdata.CurrAsmList,right.location,right.resultdef,left.resultdef);
  556. paraloc1.done;
  557. paraloc2.done;
  558. end;
  559. procedure tcgvecnode.pass_generate_code;
  560. var
  561. offsetdec,
  562. extraoffset : aint;
  563. t : tnode;
  564. href : treference;
  565. otl,ofl : tasmlabel;
  566. newsize : tcgsize;
  567. mulsize,
  568. bytemulsize,
  569. alignpow : aint;
  570. isjump : boolean;
  571. paraloc1,
  572. paraloc2 : tcgpara;
  573. subsetref : tsubsetreference;
  574. temp : longint;
  575. begin
  576. paraloc1.init;
  577. paraloc2.init;
  578. mulsize:=get_mul_size;
  579. if not is_packed_array(left.resultdef) then
  580. bytemulsize:=mulsize
  581. else
  582. bytemulsize:=mulsize div 8;
  583. newsize:=def_cgsize(resultdef);
  584. secondpass(left);
  585. if left.location.loc=LOC_CREFERENCE then
  586. location_reset(location,LOC_CREFERENCE,newsize)
  587. else
  588. location_reset(location,LOC_REFERENCE,newsize);
  589. { an ansistring needs to be dereferenced }
  590. if is_ansistring(left.resultdef) or
  591. is_widestring(left.resultdef) or
  592. is_unicodestring(left.resultdef) then
  593. begin
  594. if nf_callunique in flags then
  595. internalerror(200304236);
  596. {DM!!!!!}
  597. case left.location.loc of
  598. LOC_REGISTER,
  599. LOC_CREGISTER :
  600. begin
  601. {$ifdef m68k}
  602. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  603. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,location.reference.base);
  604. {$else m68k}
  605. location.reference.base:=left.location.register;
  606. {$endif m68k}
  607. end;
  608. LOC_CREFERENCE,
  609. LOC_REFERENCE :
  610. begin
  611. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  612. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.reference.base);
  613. end;
  614. else
  615. internalerror(2002032218);
  616. end;
  617. { check for a zero length string,
  618. we can use the ansistring routine here }
  619. if (cs_check_range in current_settings.localswitches) then
  620. begin
  621. paramanager.getintparaloc(pocall_default,1,paraloc1);
  622. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  623. cg.a_param_reg(current_asmdata.CurrAsmList,OS_ADDR,location.reference.base,paraloc1);
  624. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  625. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  626. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resultdef).stringtypname)+'_CHECKZERO');
  627. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  628. end;
  629. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  630. if is_ansistring(left.resultdef) then
  631. offsetdec:=1
  632. else
  633. offsetdec:=2;
  634. dec(location.reference.offset,offsetdec);
  635. end
  636. else if is_dynamic_array(left.resultdef) then
  637. begin
  638. case left.location.loc of
  639. LOC_REGISTER,
  640. LOC_CREGISTER :
  641. location.reference.base:=left.location.register;
  642. LOC_REFERENCE,
  643. LOC_CREFERENCE :
  644. begin
  645. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  646. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  647. left.location.reference,location.reference.base);
  648. end;
  649. else
  650. internalerror(2002032219);
  651. end;
  652. end
  653. else
  654. location_copy(location,left.location);
  655. { location must be memory }
  656. if not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  657. internalerror(200411013);
  658. { offset can only differ from 0 if arraydef }
  659. if (left.resultdef.typ=arraydef) and
  660. not(is_dynamic_array(left.resultdef)) and
  661. (not(is_packed_array(left.resultdef)) or
  662. ((mulsize mod 8 = 0) and
  663. ispowerof2(mulsize div 8,temp)) or
  664. { only orddefs are bitpacked }
  665. not is_ordinal(resultdef)) then
  666. dec(location.reference.offset,bytemulsize*tarraydef(left.resultdef).lowrange);
  667. if right.nodetype=ordconstn then
  668. begin
  669. { offset can only differ from 0 if arraydef }
  670. case left.resultdef.typ of
  671. arraydef :
  672. begin
  673. { do not do any range checking when this is an array access to a pointer which has been
  674. typecasted from an array }
  675. if (not (ado_isconvertedpointer in tarraydef(left.resultdef).arrayoptions)) then
  676. begin
  677. if not(is_open_array(left.resultdef)) and
  678. not(is_array_of_const(left.resultdef)) and
  679. not(is_dynamic_array(left.resultdef)) then
  680. begin
  681. if (tordconstnode(right).value.svalue>tarraydef(left.resultdef).highrange) or
  682. (tordconstnode(right).value.svalue<tarraydef(left.resultdef).lowrange) then
  683. begin
  684. { this should be caught in the typecheckpass! (JM) }
  685. if (cs_check_range in current_settings.localswitches) then
  686. CGMessage(parser_e_range_check_error)
  687. else
  688. CGMessage(parser_w_range_check_error);
  689. end;
  690. end
  691. else
  692. begin
  693. { range checking for open and dynamic arrays needs
  694. runtime code }
  695. secondpass(right);
  696. if (cs_check_range in current_settings.localswitches) then
  697. rangecheck_array;
  698. end;
  699. end;
  700. end;
  701. stringdef :
  702. begin
  703. if (cs_check_range in current_settings.localswitches) then
  704. begin
  705. case tstringdef(left.resultdef).stringtype of
  706. { it's the same for ansi- and wide strings }
  707. st_unicodestring,
  708. st_widestring,
  709. st_ansistring:
  710. begin
  711. paramanager.getintparaloc(pocall_default,1,paraloc1);
  712. paramanager.getintparaloc(pocall_default,2,paraloc2);
  713. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  714. cg.a_param_const(current_asmdata.CurrAsmList,OS_INT,tordconstnode(right).value.svalue,paraloc2);
  715. href:=location.reference;
  716. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  717. if not(tf_winlikewidestring in target_info.flags) or
  718. (tstringdef(left.resultdef).stringtype<>st_widestring) then
  719. begin
  720. dec(href.offset,sizeof(pint)-offsetdec);
  721. cg.a_param_ref(current_asmdata.CurrAsmList,OS_ADDR,href,paraloc1);
  722. end
  723. else
  724. begin
  725. { winlike widestrings have a 4 byte length }
  726. dec(href.offset,4-offsetdec);
  727. cg.a_param_ref(current_asmdata.CurrAsmList,OS_32,href,paraloc1);
  728. end;
  729. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  730. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  731. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  732. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resultdef).stringtypname)+'_RANGECHECK');
  733. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  734. end;
  735. st_shortstring:
  736. begin
  737. {!!!!!!!!!!!!!!!!!}
  738. { if this one is implemented making use of the high parameter for openshortstrings, update ncgutils.do_get_used_regvars() too (JM) }
  739. end;
  740. st_longstring:
  741. begin
  742. {!!!!!!!!!!!!!!!!!}
  743. end;
  744. end;
  745. end;
  746. end;
  747. end;
  748. if not(is_packed_array(left.resultdef)) or
  749. ((mulsize mod 8 = 0) and
  750. (ispowerof2(mulsize div 8,temp) or
  751. { only orddefs are bitpacked }
  752. not is_ordinal(resultdef))) then
  753. begin
  754. inc(location.reference.offset,
  755. bytemulsize*tordconstnode(right).value.svalue);
  756. { don't do this for floats etc.; needed to properly set the }
  757. { size for bitpacked arrays (e.g. a bitpacked array of }
  758. { enums who are size 2 but fit in one byte -> in the array }
  759. { they will be one byte and have to be stored like that) }
  760. if is_packed_array(left.resultdef) and
  761. (tcgsize2size[newsize] <> bytemulsize) then
  762. newsize:=int_cgsize(bytemulsize);
  763. end
  764. else
  765. begin
  766. subsetref.ref := location.reference;
  767. subsetref.ref.alignment := left.resultdef.alignment;
  768. if not ispowerof2(subsetref.ref.alignment,temp) then
  769. internalerror(2006081212);
  770. alignpow:=temp;
  771. inc(subsetref.ref.offset,((mulsize * (tordconstnode(right).value.svalue-tarraydef(left.resultdef).lowrange)) shr (3+alignpow)) shl alignpow);
  772. subsetref.bitindexreg := NR_NO;
  773. subsetref.startbit := (mulsize * (tordconstnode(right).value.svalue-tarraydef(left.resultdef).lowrange)) and ((1 shl (3+alignpow))-1);
  774. subsetref.bitlen := resultdef.packedbitsize;
  775. if (left.location.loc = LOC_REFERENCE) then
  776. location.loc := LOC_SUBSETREF
  777. else
  778. location.loc := LOC_CSUBSETREF;
  779. location.sref := subsetref;
  780. end;
  781. end
  782. else
  783. { not nodetype=ordconstn }
  784. begin
  785. if (cs_opt_level1 in current_settings.optimizerswitches) and
  786. { if we do range checking, we don't }
  787. { need that fancy code (it would be }
  788. { buggy) }
  789. not(cs_check_range in current_settings.localswitches) and
  790. (left.resultdef.typ=arraydef) and
  791. not is_packed_array(left.resultdef) then
  792. begin
  793. extraoffset:=0;
  794. if (right.nodetype=addn) then
  795. begin
  796. if taddnode(right).right.nodetype=ordconstn then
  797. begin
  798. extraoffset:=tordconstnode(taddnode(right).right).value.svalue;
  799. t:=taddnode(right).left;
  800. taddnode(right).left:=nil;
  801. right.free;
  802. right:=t;
  803. end
  804. else if taddnode(right).left.nodetype=ordconstn then
  805. begin
  806. extraoffset:=tordconstnode(taddnode(right).left).value.svalue;
  807. t:=taddnode(right).right;
  808. taddnode(right).right:=nil;
  809. right.free;
  810. right:=t;
  811. end;
  812. end
  813. else if (right.nodetype=subn) then
  814. begin
  815. if taddnode(right).right.nodetype=ordconstn then
  816. begin
  817. extraoffset:=-tordconstnode(taddnode(right).right).value.svalue;
  818. t:=taddnode(right).left;
  819. taddnode(right).left:=nil;
  820. right.free;
  821. right:=t;
  822. end;
  823. end;
  824. inc(location.reference.offset,
  825. mulsize*extraoffset);
  826. end;
  827. { calculate from left to right }
  828. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  829. internalerror(200304237);
  830. isjump:=(right.expectloc=LOC_JUMP);
  831. if isjump then
  832. begin
  833. otl:=current_procinfo.CurrTrueLabel;
  834. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  835. ofl:=current_procinfo.CurrFalseLabel;
  836. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  837. end;
  838. secondpass(right);
  839. { if mulsize = 1, we won't have to modify the index }
  840. location_force_reg(current_asmdata.CurrAsmList,right.location,OS_ADDR,true);
  841. if isjump then
  842. begin
  843. current_procinfo.CurrTrueLabel:=otl;
  844. current_procinfo.CurrFalseLabel:=ofl;
  845. end
  846. else if (right.location.loc = LOC_JUMP) then
  847. internalerror(2006010801);
  848. { only range check now, we can't range check loc_flags/loc_jump }
  849. if cs_check_range in current_settings.localswitches then
  850. begin
  851. if left.resultdef.typ=arraydef then
  852. rangecheck_array;
  853. end;
  854. { produce possible range check code: }
  855. if cs_check_range in current_settings.localswitches then
  856. begin
  857. if left.resultdef.typ=arraydef then
  858. begin
  859. { done defore (PM) }
  860. end
  861. else if (left.resultdef.typ=stringdef) then
  862. begin
  863. case tstringdef(left.resultdef).stringtype of
  864. { it's the same for ansi- and wide strings }
  865. st_unicodestring,
  866. st_widestring,
  867. st_ansistring:
  868. begin
  869. paramanager.getintparaloc(pocall_default,1,paraloc1);
  870. paramanager.getintparaloc(pocall_default,2,paraloc2);
  871. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  872. cg.a_param_reg(current_asmdata.CurrAsmList,OS_INT,right.location.register,paraloc2);
  873. href:=location.reference;
  874. dec(href.offset,sizeof(pint)-offsetdec);
  875. href:=location.reference;
  876. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  877. if not(tf_winlikewidestring in target_info.flags) or
  878. (tstringdef(left.resultdef).stringtype<>st_widestring) then
  879. begin
  880. dec(href.offset,sizeof(pint)-offsetdec);
  881. cg.a_param_ref(current_asmdata.CurrAsmList,OS_ADDR,href,paraloc1);
  882. end
  883. else
  884. begin
  885. { winlike widestrings have a 4 byte length }
  886. dec(href.offset,4-offsetdec);
  887. cg.a_param_ref(current_asmdata.CurrAsmList,OS_32,href,paraloc1);
  888. end;
  889. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  890. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  891. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  892. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resultdef).stringtypname)+'_RANGECHECK');
  893. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  894. end;
  895. st_shortstring:
  896. begin
  897. {!!!!!!!!!!!!!!!!!}
  898. end;
  899. st_longstring:
  900. begin
  901. {!!!!!!!!!!!!!!!!!}
  902. end;
  903. end;
  904. end;
  905. end;
  906. { insert the register and the multiplication factor in the
  907. reference }
  908. if not is_packed_array(left.resultdef) then
  909. update_reference_reg_mul(right.location.register,mulsize)
  910. else
  911. update_reference_reg_packed(right.location.register,mulsize);
  912. end;
  913. location.size:=newsize;
  914. paraloc1.done;
  915. paraloc2.done;
  916. end;
  917. begin
  918. cloadvmtaddrnode:=tcgloadvmtaddrnode;
  919. cloadparentfpnode:=tcgloadparentfpnode;
  920. caddrnode:=tcgaddrnode;
  921. cderefnode:=tcgderefnode;
  922. csubscriptnode:=tcgsubscriptnode;
  923. cwithnode:=tcgwithnode;
  924. cvecnode:=tcgvecnode;
  925. end.