ncgmem.pas 45 KB

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