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