ncgmem.pas 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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)) or
  262. (target_info.system in systems_garbage_collected_managed_types) then
  263. begin
  264. { the contents of a class are aligned to a sizeof(pointer) }
  265. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),sizeof(pint));
  266. case left.location.loc of
  267. LOC_CREGISTER,
  268. LOC_REGISTER:
  269. begin
  270. {$ifdef cpu_uses_separate_address_registers}
  271. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  272. begin
  273. location.reference.base:=rg.getaddressregister(current_asmdata.CurrAsmList);
  274. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,
  275. left.location.register,location.reference.base);
  276. end
  277. else
  278. {$endif}
  279. location.reference.base := left.location.register;
  280. end;
  281. LOC_CREFERENCE,
  282. LOC_REFERENCE:
  283. begin
  284. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  285. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,left.resultdef,left.location,location.reference.base);
  286. end;
  287. LOC_CONSTANT:
  288. begin
  289. { can happen with @classtype(pointerconst).field }
  290. location.reference.offset:=left.location.value;
  291. end;
  292. else
  293. internalerror(2009092401);
  294. end;
  295. { implicit deferencing }
  296. if (cs_use_heaptrc in current_settings.globalswitches) and
  297. (cs_checkpointer in current_settings.localswitches) and
  298. not(cs_compilesystem in current_settings.moduleswitches) then
  299. begin
  300. paramanager.getintparaloc(pocall_default,1,paraloc1);
  301. cg.a_load_reg_cgpara(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  302. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  303. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  304. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER',false);
  305. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  306. end;
  307. end
  308. else
  309. { reference-counted implicit pointer object types don't have
  310. fields -> cannot be subscripted (calls are handled via call
  311. nodes) }
  312. internalerror(2011011901);
  313. end
  314. else
  315. begin
  316. location_copy(location,left.location);
  317. { some abi's require that functions return (some) records in }
  318. { registers }
  319. case location.loc of
  320. LOC_REFERENCE,
  321. LOC_CREFERENCE:
  322. ;
  323. LOC_REGISTER,
  324. LOC_CREGISTER,
  325. LOC_MMREGISTER,
  326. LOC_FPUREGISTER:
  327. begin
  328. // in case the result is not something that can be put
  329. // into an integer register (e.g.
  330. // function_returning_record().non_regable_field, or
  331. // a function returning a value > sizeof(intreg))
  332. // -> force to memory
  333. if not tstoreddef(left.resultdef).is_intregable or
  334. not tstoreddef(resultdef).is_intregable or
  335. (location.loc in [LOC_MMREGISTER,LOC_FPUREGISTER]) then
  336. location_force_mem(current_asmdata.CurrAsmList,location)
  337. else
  338. begin
  339. if (left.location.loc = LOC_REGISTER) then
  340. location.loc := LOC_SUBSETREG
  341. else
  342. location.loc := LOC_CSUBSETREG;
  343. location.size:=def_cgsize(resultdef);
  344. location.sreg.subsetreg := left.location.register;
  345. location.sreg.subsetregsize := left.location.size;
  346. if not is_packed_record_or_object(left.resultdef) then
  347. begin
  348. if (target_info.endian = ENDIAN_BIG) then
  349. location.sreg.startbit := (tcgsize2size[location.sreg.subsetregsize] - tcgsize2size[location.size] - vs.fieldoffset) * 8
  350. else
  351. location.sreg.startbit := (vs.fieldoffset * 8);
  352. location.sreg.bitlen := tcgsize2size[location.size] * 8;
  353. end
  354. else
  355. begin
  356. location.sreg.bitlen := resultdef.packedbitsize;
  357. if (target_info.endian = ENDIAN_BIG) then
  358. location.sreg.startbit := (tcgsize2size[location.sreg.subsetregsize]*8 - location.sreg.bitlen) - vs.fieldoffset
  359. else
  360. location.sreg.startbit := vs.fieldoffset;
  361. end;
  362. end;
  363. end;
  364. LOC_SUBSETREG,
  365. LOC_CSUBSETREG:
  366. begin
  367. location.size:=def_cgsize(resultdef);
  368. if not is_packed_record_or_object(left.resultdef) then
  369. begin
  370. if (target_info.endian = ENDIAN_BIG) then
  371. inc(location.sreg.startbit, (left.resultdef.size - tcgsize2size[location.size] - vs.fieldoffset) * 8)
  372. else
  373. inc(location.sreg.startbit, vs.fieldoffset * 8);
  374. location.sreg.bitlen := tcgsize2size[location.size] * 8;
  375. end
  376. else
  377. begin
  378. location.sreg.bitlen := resultdef.packedbitsize;
  379. if (target_info.endian = ENDIAN_BIG) then
  380. inc(location.sreg.startbit, left.location.sreg.bitlen - location.sreg.bitlen - vs.fieldoffset)
  381. else
  382. inc(location.sreg.startbit, vs.fieldoffset);
  383. end;
  384. end;
  385. else
  386. internalerror(2006031901);
  387. end;
  388. end;
  389. if is_objc_class_or_protocol(left.resultdef) and
  390. (target_info.system in systems_objc_nfabi) then
  391. begin
  392. if (location.loc<>LOC_REFERENCE) or
  393. (location.reference.index<>NR_NO) then
  394. internalerror(2009092402);
  395. { the actual field offset is stored in memory (to solve the
  396. "fragile base class" problem: this way the layout of base
  397. classes can be changed without breaking programs compiled against
  398. earlier versions)
  399. }
  400. hreg:=cg.g_indirect_sym_load(current_asmdata.CurrAsmList,vs.mangledname,false);
  401. { TODO: clean up. g_indirect_sym_load cannot perform
  402. a plain load for targets that don't need an indirect load
  403. because it's also used in ncgld, but this is not very nice...
  404. }
  405. if (hreg=NR_NO) then
  406. begin
  407. sym:=current_asmdata.RefAsmSymbol(vs.mangledname);
  408. reference_reset_symbol(tmpref,sym,0,sizeof(pint));
  409. location.reference.index:=cg.getaddressregister(current_asmdata.CurrAsmList);
  410. end
  411. else
  412. begin
  413. reference_reset_base(tmpref,hreg,0,sizeof(pint));
  414. location.reference.index:=hreg;
  415. end;
  416. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,tmpref,location.reference.index);
  417. { always packrecords C -> natural alignment }
  418. location.reference.alignment:=vs.vardef.alignment;
  419. end
  420. else if is_java_class_or_interface(left.resultdef) or
  421. ((target_info.system=system_jvm_java32) and
  422. (left.resultdef.typ=recorddef)) then
  423. begin
  424. if (location.loc<>LOC_REFERENCE) or
  425. (location.reference.index<>NR_NO) or
  426. assigned(location.reference.symbol) then
  427. internalerror(2011011301);
  428. location.reference.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname);
  429. end
  430. else if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  431. begin
  432. if not is_packed_record_or_object(left.resultdef) then
  433. begin
  434. inc(location.reference.offset,vs.fieldoffset);
  435. location.reference.alignment:=newalignment(location.reference.alignment,vs.fieldoffset);
  436. end
  437. else if (vs.fieldoffset mod 8 = 0) and
  438. (resultdef.packedbitsize mod 8 = 0) and
  439. { is different in case of e.g. packenum 2 and an enum }
  440. { which fits in 8 bits }
  441. (resultdef.size*8 = resultdef.packedbitsize) then
  442. begin
  443. inc(location.reference.offset,vs.fieldoffset div 8);
  444. location.reference.alignment:=newalignment(location.reference.alignment,vs.fieldoffset div 8);
  445. end
  446. else
  447. begin
  448. sref.ref:=location.reference;
  449. sref.ref.alignment:=1;
  450. sref.bitindexreg:=NR_NO;
  451. inc(sref.ref.offset,vs.fieldoffset div 8);
  452. sref.startbit:=vs.fieldoffset mod 8;
  453. sref.bitlen:=resultdef.packedbitsize;
  454. if (left.location.loc=LOC_REFERENCE) then
  455. location.loc:=LOC_SUBSETREF
  456. else
  457. location.loc:=LOC_CSUBSETREF;
  458. location.sref:=sref;
  459. end;
  460. { also update the size of the location }
  461. location.size:=def_cgsize(resultdef);
  462. end;
  463. paraloc1.done;
  464. end;
  465. {*****************************************************************************
  466. TCGWITHNODE
  467. *****************************************************************************}
  468. procedure tcgwithnode.pass_generate_code;
  469. begin
  470. location_reset(location,LOC_VOID,OS_NO);
  471. if assigned(left) then
  472. secondpass(left);
  473. end;
  474. {*****************************************************************************
  475. TCGVECNODE
  476. *****************************************************************************}
  477. function tcgvecnode.get_mul_size : aint;
  478. begin
  479. if nf_memindex in flags then
  480. get_mul_size:=1
  481. else
  482. begin
  483. if (left.resultdef.typ=arraydef) then
  484. if not is_packed_array(left.resultdef) then
  485. get_mul_size:=tarraydef(left.resultdef).elesize
  486. else
  487. get_mul_size:=tarraydef(left.resultdef).elepackedbitsize
  488. else
  489. get_mul_size:=resultdef.size;
  490. end
  491. end;
  492. { this routine must, like any other routine, not change the contents }
  493. { of base/index registers of references, as these may be regvars. }
  494. { The register allocator can coalesce one LOC_REGISTER being moved }
  495. { into another (as their live ranges won't overlap), but not a }
  496. { LOC_CREGISTER moved into a LOC_(C)REGISTER most of the time (as }
  497. { the live range of the LOC_CREGISTER will most likely overlap the }
  498. { the live range of the target LOC_(C)REGISTER) }
  499. { The passed register may be a LOC_CREGISTER as well. }
  500. procedure tcgvecnode.update_reference_reg_mul(maybe_const_reg:tregister;l:aint);
  501. var
  502. hreg: tregister;
  503. begin
  504. if l<>1 then
  505. begin
  506. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  507. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,maybe_const_reg,hreg);
  508. maybe_const_reg:=hreg;
  509. end;
  510. if location.reference.base=NR_NO then
  511. location.reference.base:=maybe_const_reg
  512. else if location.reference.index=NR_NO then
  513. location.reference.index:=maybe_const_reg
  514. else
  515. begin
  516. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  517. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,location.reference,hreg);
  518. reference_reset_base(location.reference,hreg,0,location.reference.alignment);
  519. { insert new index register }
  520. location.reference.index:=maybe_const_reg;
  521. end;
  522. { update alignment }
  523. if (location.reference.alignment=0) then
  524. internalerror(2009020704);
  525. location.reference.alignment:=newalignment(location.reference.alignment,l);
  526. end;
  527. { see remarks for tcgvecnode.update_reference_reg_mul above }
  528. procedure tcgvecnode.update_reference_reg_packed(maybe_const_reg:tregister;l:aint);
  529. var
  530. sref: tsubsetreference;
  531. offsetreg, hreg: tregister;
  532. alignpower: aint;
  533. temp : longint;
  534. begin
  535. { only orddefs are bitpacked. Even then we only need special code in }
  536. { case the bitpacked *byte size* is not a power of two, otherwise }
  537. { everything can be handled using the the regular array code. }
  538. if ((l mod 8) = 0) and
  539. (ispowerof2(l div 8,temp) or
  540. not is_ordinal(resultdef)
  541. {$ifndef cpu64bitalu}
  542. or is_64bitint(resultdef)
  543. {$endif not cpu64bitalu}
  544. ) then
  545. begin
  546. update_reference_reg_mul(maybe_const_reg,l div 8);
  547. exit;
  548. end;
  549. if (l > 8*sizeof(aint)) then
  550. internalerror(200608051);
  551. sref.ref := location.reference;
  552. hreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  553. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,tarraydef(left.resultdef).lowrange,maybe_const_reg,hreg);
  554. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_INT,l,hreg);
  555. { keep alignment for index }
  556. sref.ref.alignment := left.resultdef.alignment;
  557. if not ispowerof2(sref.ref.alignment,temp) then
  558. internalerror(2006081201);
  559. alignpower:=temp;
  560. offsetreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  561. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_ADDR,3+alignpower,hreg,offsetreg);
  562. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,alignpower,offsetreg);
  563. if (sref.ref.base = NR_NO) then
  564. sref.ref.base := offsetreg
  565. else if (sref.ref.index = NR_NO) then
  566. sref.ref.index := offsetreg
  567. else
  568. begin
  569. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,sref.ref.base,offsetreg);
  570. sref.ref.base := offsetreg;
  571. end;
  572. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,(1 shl (3+alignpower))-1,hreg);
  573. sref.bitindexreg := hreg;
  574. sref.startbit := 0;
  575. sref.bitlen := resultdef.packedbitsize;
  576. if (left.location.loc = LOC_REFERENCE) then
  577. location.loc := LOC_SUBSETREF
  578. else
  579. location.loc := LOC_CSUBSETREF;
  580. location.sref := sref;
  581. end;
  582. procedure tcgvecnode.second_wideansistring;
  583. begin
  584. end;
  585. procedure tcgvecnode.second_dynamicarray;
  586. begin
  587. end;
  588. procedure tcgvecnode.rangecheck_array;
  589. var
  590. hightree : tnode;
  591. poslabel,
  592. neglabel : tasmlabel;
  593. hreg : tregister;
  594. paraloc1,paraloc2 : tcgpara;
  595. begin
  596. { omit range checking when this is an array access to a pointer which has been
  597. typecasted from an array }
  598. if (ado_isconvertedpointer in tarraydef(left.resultdef).arrayoptions) then
  599. exit;
  600. paraloc1.init;
  601. paraloc2.init;
  602. if is_open_array(left.resultdef) or
  603. is_array_of_const(left.resultdef) then
  604. begin
  605. { cdecl functions don't have high() so we can not check the range }
  606. { (can't use current_procdef, since it may be a nested procedure) }
  607. if not(tprocdef(tparasymtable(tparavarsym(tloadnode(left).symtableentry).owner).defowner).proccalloption in cdecl_pocalls) then
  608. begin
  609. { Get high value }
  610. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  611. { it must be available }
  612. if not assigned(hightree) then
  613. internalerror(200212201);
  614. firstpass(hightree);
  615. secondpass(hightree);
  616. { generate compares }
  617. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  618. hreg:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_INT)
  619. else
  620. begin
  621. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  622. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_INT,right.location,hreg);
  623. end;
  624. current_asmdata.getjumplabel(neglabel);
  625. current_asmdata.getjumplabel(poslabel);
  626. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_LT,0,hreg,poslabel);
  627. cg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  628. cg.a_label(current_asmdata.CurrAsmList,poslabel);
  629. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RANGEERROR',false);
  630. cg.a_label(current_asmdata.CurrAsmList,neglabel);
  631. { release hightree }
  632. hightree.free;
  633. end;
  634. end
  635. else
  636. if is_dynamic_array(left.resultdef) then
  637. begin
  638. paramanager.getintparaloc(pocall_default,1,paraloc1);
  639. paramanager.getintparaloc(pocall_default,2,paraloc2);
  640. cg.a_load_loc_cgpara(current_asmdata.CurrAsmList,right.location,paraloc2);
  641. cg.a_load_loc_cgpara(current_asmdata.CurrAsmList,left.location,paraloc1);
  642. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  643. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc2);
  644. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  645. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DYNARRAY_RANGECHECK',false);
  646. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  647. end;
  648. { for regular arrays, we don't have to do anything because the index has been
  649. type converted to the index type, which already inserted a range check if
  650. necessary }
  651. paraloc1.done;
  652. paraloc2.done;
  653. end;
  654. procedure tcgvecnode.rangecheck_string;
  655. var
  656. paraloc1,
  657. paraloc2: tcgpara;
  658. begin
  659. paraloc1.init;
  660. paraloc2.init;
  661. case tstringdef(left.resultdef).stringtype of
  662. { it's the same for ansi- and wide strings }
  663. st_unicodestring,
  664. st_widestring,
  665. st_ansistring:
  666. begin
  667. paramanager.getintparaloc(pocall_default,1,paraloc1);
  668. paramanager.getintparaloc(pocall_default,2,paraloc2);
  669. cg.a_load_loc_cgpara(current_asmdata.CurrAsmList,left.location,paraloc1);
  670. cg.a_load_loc_cgpara(current_asmdata.CurrAsmList,right.location,paraloc2);
  671. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  672. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc2);
  673. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  674. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resultdef).stringtypname)+'_RANGECHECK',false);
  675. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  676. end;
  677. st_shortstring:
  678. begin
  679. {!!!!!!!!!!!!!!!!!}
  680. { if this one is implemented making use of the high parameter for openshortstrings, update ncgutils.do_get_used_regvars() too (JM) }
  681. end;
  682. st_longstring:
  683. begin
  684. {!!!!!!!!!!!!!!!!!}
  685. end;
  686. end;
  687. paraloc1.done;
  688. paraloc2.done;
  689. end;
  690. procedure tcgvecnode.pass_generate_code;
  691. var
  692. offsetdec,
  693. extraoffset : aint;
  694. t : tnode;
  695. otl,ofl : tasmlabel;
  696. newsize : tcgsize;
  697. mulsize,
  698. bytemulsize,
  699. alignpow : aint;
  700. isjump : boolean;
  701. paraloc1,
  702. paraloc2 : tcgpara;
  703. subsetref : tsubsetreference;
  704. temp : longint;
  705. begin
  706. paraloc1.init;
  707. paraloc2.init;
  708. mulsize:=get_mul_size;
  709. if not is_packed_array(left.resultdef) then
  710. bytemulsize:=mulsize
  711. else
  712. bytemulsize:=mulsize div 8;
  713. newsize:=def_cgsize(resultdef);
  714. secondpass(left);
  715. if left.location.loc=LOC_CREFERENCE then
  716. location_reset_ref(location,LOC_CREFERENCE,newsize,left.location.reference.alignment)
  717. else
  718. location_reset_ref(location,LOC_REFERENCE,newsize,left.location.reference.alignment);
  719. { an ansistring needs to be dereferenced }
  720. if is_ansistring(left.resultdef) or
  721. is_wide_or_unicode_string(left.resultdef) then
  722. begin
  723. if nf_callunique in flags then
  724. internalerror(200304236);
  725. {DM!!!!!}
  726. case left.location.loc of
  727. LOC_REGISTER,
  728. LOC_CREGISTER :
  729. begin
  730. {$ifdef m68k}
  731. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  732. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,location.reference.base);
  733. {$else m68k}
  734. location.reference.base:=left.location.register;
  735. {$endif m68k}
  736. end;
  737. LOC_CREFERENCE,
  738. LOC_REFERENCE :
  739. begin
  740. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  741. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.reference.base);
  742. end;
  743. else
  744. internalerror(2002032218);
  745. end;
  746. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  747. if is_ansistring(left.resultdef) then
  748. offsetdec:=1
  749. else
  750. offsetdec:=2;
  751. location.reference.alignment:=offsetdec;
  752. dec(location.reference.offset,offsetdec);
  753. end
  754. else if is_dynamic_array(left.resultdef) then
  755. begin
  756. case left.location.loc of
  757. LOC_REGISTER,
  758. LOC_CREGISTER :
  759. location.reference.base:=left.location.register;
  760. LOC_REFERENCE,
  761. LOC_CREFERENCE :
  762. begin
  763. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  764. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  765. left.location.reference,location.reference.base);
  766. end;
  767. else
  768. internalerror(2002032219);
  769. end;
  770. { a dynarray points to the start of a memory block, which
  771. we assume to be always aligned to a multiple of the
  772. pointer size
  773. }
  774. location.reference.alignment:=sizeof(pint);
  775. end
  776. else
  777. begin
  778. { may happen in case of function results }
  779. case left.location.loc of
  780. LOC_REGISTER,
  781. LOC_MMREGISTER:
  782. location_force_mem(current_asmdata.CurrAsmList,left.location);
  783. end;
  784. location_copy(location,left.location);
  785. end;
  786. { location must be memory }
  787. if not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  788. internalerror(200411013);
  789. { offset can only differ from 0 if arraydef }
  790. if (left.resultdef.typ=arraydef) and
  791. not(is_dynamic_array(left.resultdef)) and
  792. (not(is_packed_array(left.resultdef)) or
  793. ((mulsize mod 8 = 0) and
  794. ispowerof2(mulsize div 8,temp)) or
  795. { only orddefs are bitpacked }
  796. not is_ordinal(resultdef)
  797. {$ifndef cpu64bitalu}
  798. or is_64bitint(resultdef)
  799. {$endif not cpu64bitalu}
  800. ) then
  801. dec(location.reference.offset,bytemulsize*tarraydef(left.resultdef).lowrange);
  802. if right.nodetype=ordconstn then
  803. begin
  804. { offset can only differ from 0 if arraydef }
  805. if cs_check_range in current_settings.localswitches then
  806. begin
  807. secondpass(right);
  808. case left.resultdef.typ of
  809. arraydef :
  810. rangecheck_array;
  811. stringdef :
  812. rangecheck_string;
  813. end;
  814. end;
  815. if not(is_packed_array(left.resultdef)) or
  816. ((mulsize mod 8 = 0) and
  817. (ispowerof2(mulsize div 8,temp) or
  818. { only orddefs are bitpacked }
  819. not is_ordinal(resultdef))) then
  820. begin
  821. extraoffset:=bytemulsize*tordconstnode(right).value.svalue;
  822. inc(location.reference.offset,extraoffset);
  823. { adjust alignment after to this change }
  824. location.reference.alignment:=newalignment(location.reference.alignment,extraoffset);
  825. { don't do this for floats etc.; needed to properly set the }
  826. { size for bitpacked arrays (e.g. a bitpacked array of }
  827. { enums who are size 2 but fit in one byte -> in the array }
  828. { they will be one byte and have to be stored like that) }
  829. if is_packed_array(left.resultdef) and
  830. (tcgsize2size[newsize] <> bytemulsize) then
  831. newsize:=int_cgsize(bytemulsize);
  832. end
  833. else
  834. begin
  835. subsetref.ref := location.reference;
  836. subsetref.ref.alignment := left.resultdef.alignment;
  837. if not ispowerof2(subsetref.ref.alignment,temp) then
  838. internalerror(2006081212);
  839. alignpow:=temp;
  840. inc(subsetref.ref.offset,((mulsize * (tordconstnode(right).value.svalue-tarraydef(left.resultdef).lowrange)) shr (3+alignpow)) shl alignpow);
  841. subsetref.bitindexreg := NR_NO;
  842. subsetref.startbit := (mulsize * (tordconstnode(right).value.svalue-tarraydef(left.resultdef).lowrange)) and ((1 shl (3+alignpow))-1);
  843. subsetref.bitlen := resultdef.packedbitsize;
  844. if (left.location.loc = LOC_REFERENCE) then
  845. location.loc := LOC_SUBSETREF
  846. else
  847. location.loc := LOC_CSUBSETREF;
  848. location.sref := subsetref;
  849. end;
  850. end
  851. else
  852. { not nodetype=ordconstn }
  853. begin
  854. if (cs_opt_level1 in current_settings.optimizerswitches) and
  855. { if we do range checking, we don't }
  856. { need that fancy code (it would be }
  857. { buggy) }
  858. not(cs_check_range in current_settings.localswitches) and
  859. (left.resultdef.typ=arraydef) and
  860. not is_packed_array(left.resultdef) then
  861. begin
  862. extraoffset:=0;
  863. if (right.nodetype=addn) then
  864. begin
  865. if taddnode(right).right.nodetype=ordconstn then
  866. begin
  867. extraoffset:=tordconstnode(taddnode(right).right).value.svalue;
  868. t:=taddnode(right).left;
  869. taddnode(right).left:=nil;
  870. right.free;
  871. right:=t;
  872. end
  873. else if taddnode(right).left.nodetype=ordconstn then
  874. begin
  875. extraoffset:=tordconstnode(taddnode(right).left).value.svalue;
  876. t:=taddnode(right).right;
  877. taddnode(right).right:=nil;
  878. right.free;
  879. right:=t;
  880. end;
  881. end
  882. else if (right.nodetype=subn) then
  883. begin
  884. if taddnode(right).right.nodetype=ordconstn then
  885. begin
  886. extraoffset:=-tordconstnode(taddnode(right).right).value.svalue;
  887. t:=taddnode(right).left;
  888. taddnode(right).left:=nil;
  889. right.free;
  890. right:=t;
  891. end;
  892. end;
  893. inc(location.reference.offset,
  894. mulsize*extraoffset);
  895. end;
  896. { calculate from left to right }
  897. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  898. internalerror(200304237);
  899. isjump:=(right.expectloc=LOC_JUMP);
  900. if isjump then
  901. begin
  902. otl:=current_procinfo.CurrTrueLabel;
  903. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  904. ofl:=current_procinfo.CurrFalseLabel;
  905. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  906. end;
  907. secondpass(right);
  908. { if mulsize = 1, we won't have to modify the index }
  909. location_force_reg(current_asmdata.CurrAsmList,right.location,OS_ADDR,true);
  910. if isjump then
  911. begin
  912. current_procinfo.CurrTrueLabel:=otl;
  913. current_procinfo.CurrFalseLabel:=ofl;
  914. end
  915. else if (right.location.loc = LOC_JUMP) then
  916. internalerror(2006010801);
  917. { produce possible range check code: }
  918. if cs_check_range in current_settings.localswitches then
  919. begin
  920. if left.resultdef.typ=arraydef then
  921. rangecheck_array
  922. else if (left.resultdef.typ=stringdef) then
  923. rangecheck_string;
  924. end;
  925. { insert the register and the multiplication factor in the
  926. reference }
  927. if not is_packed_array(left.resultdef) then
  928. update_reference_reg_mul(right.location.register,mulsize)
  929. else
  930. update_reference_reg_packed(right.location.register,mulsize);
  931. end;
  932. location.size:=newsize;
  933. paraloc1.done;
  934. paraloc2.done;
  935. end;
  936. begin
  937. cloadvmtaddrnode:=tcgloadvmtaddrnode;
  938. cloadparentfpnode:=tcgloadparentfpnode;
  939. caddrnode:=tcgaddrnode;
  940. cderefnode:=tcgderefnode;
  941. csubscriptnode:=tcgsubscriptnode;
  942. cwithnode:=tcgwithnode;
  943. cvecnode:=tcgvecnode;
  944. end.