ncgmem.pas 44 KB

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