ncgmem.pas 46 KB

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