ncgmem.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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,
  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. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  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. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,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 (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  420. begin
  421. if not is_packed_record_or_object(left.resultdef) then
  422. begin
  423. inc(location.reference.offset,vs.fieldoffset);
  424. location.reference.alignment:=newalignment(location.reference.alignment,vs.fieldoffset);
  425. end
  426. else if (vs.fieldoffset mod 8 = 0) and
  427. (resultdef.packedbitsize mod 8 = 0) and
  428. { is different in case of e.g. packenum 2 and an enum }
  429. { which fits in 8 bits }
  430. (resultdef.size*8 = resultdef.packedbitsize) then
  431. begin
  432. inc(location.reference.offset,vs.fieldoffset div 8);
  433. location.reference.alignment:=newalignment(location.reference.alignment,vs.fieldoffset div 8);
  434. end
  435. else
  436. begin
  437. sref.ref:=location.reference;
  438. sref.ref.alignment:=1;
  439. sref.bitindexreg:=NR_NO;
  440. inc(sref.ref.offset,vs.fieldoffset div 8);
  441. sref.startbit:=vs.fieldoffset mod 8;
  442. sref.bitlen:=resultdef.packedbitsize;
  443. if (left.location.loc=LOC_REFERENCE) then
  444. location.loc:=LOC_SUBSETREF
  445. else
  446. location.loc:=LOC_CSUBSETREF;
  447. location.sref:=sref;
  448. end;
  449. { also update the size of the location }
  450. location.size:=def_cgsize(resultdef);
  451. end;
  452. paraloc1.done;
  453. end;
  454. {*****************************************************************************
  455. TCGWITHNODE
  456. *****************************************************************************}
  457. procedure tcgwithnode.pass_generate_code;
  458. begin
  459. location_reset(location,LOC_VOID,OS_NO);
  460. if assigned(left) then
  461. secondpass(left);
  462. end;
  463. {*****************************************************************************
  464. TCGVECNODE
  465. *****************************************************************************}
  466. function tcgvecnode.get_mul_size : aint;
  467. begin
  468. if nf_memindex in flags then
  469. get_mul_size:=1
  470. else
  471. begin
  472. if (left.resultdef.typ=arraydef) then
  473. if not is_packed_array(left.resultdef) then
  474. get_mul_size:=tarraydef(left.resultdef).elesize
  475. else
  476. get_mul_size:=tarraydef(left.resultdef).elepackedbitsize
  477. else
  478. get_mul_size:=resultdef.size;
  479. end
  480. end;
  481. { this routine must, like any other routine, not change the contents }
  482. { of base/index registers of references, as these may be regvars. }
  483. { The register allocator can coalesce one LOC_REGISTER being moved }
  484. { into another (as their live ranges won't overlap), but not a }
  485. { LOC_CREGISTER moved into a LOC_(C)REGISTER most of the time (as }
  486. { the live range of the LOC_CREGISTER will most likely overlap the }
  487. { the live range of the target LOC_(C)REGISTER) }
  488. { The passed register may be a LOC_CREGISTER as well. }
  489. procedure tcgvecnode.update_reference_reg_mul(maybe_const_reg:tregister;l:aint);
  490. var
  491. hreg: tregister;
  492. begin
  493. if l<>1 then
  494. begin
  495. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  496. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,maybe_const_reg,hreg);
  497. maybe_const_reg:=hreg;
  498. end;
  499. if location.reference.base=NR_NO then
  500. location.reference.base:=maybe_const_reg
  501. else if location.reference.index=NR_NO then
  502. location.reference.index:=maybe_const_reg
  503. else
  504. begin
  505. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  506. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,location.reference,hreg);
  507. reference_reset_base(location.reference,hreg,0,location.reference.alignment);
  508. { insert new index register }
  509. location.reference.index:=maybe_const_reg;
  510. end;
  511. { update alignment }
  512. if (location.reference.alignment=0) then
  513. internalerror(2009020704);
  514. location.reference.alignment:=newalignment(location.reference.alignment,l);
  515. end;
  516. { see remarks for tcgvecnode.update_reference_reg_mul above }
  517. procedure tcgvecnode.update_reference_reg_packed(maybe_const_reg:tregister;l:aint);
  518. var
  519. sref: tsubsetreference;
  520. offsetreg, hreg: tregister;
  521. alignpower: aint;
  522. temp : longint;
  523. begin
  524. { only orddefs are bitpacked. Even then we only need special code in }
  525. { case the bitpacked *byte size* is not a power of two, otherwise }
  526. { everything can be handled using the the regular array code. }
  527. if ((l mod 8) = 0) and
  528. (ispowerof2(l div 8,temp) or
  529. not is_ordinal(resultdef)
  530. {$ifndef cpu64bitalu}
  531. or is_64bitint(resultdef)
  532. {$endif not cpu64bitalu}
  533. ) then
  534. begin
  535. update_reference_reg_mul(maybe_const_reg,l div 8);
  536. exit;
  537. end;
  538. if (l > 8*sizeof(aint)) then
  539. internalerror(200608051);
  540. sref.ref := location.reference;
  541. hreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  542. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,tarraydef(left.resultdef).lowrange,maybe_const_reg,hreg);
  543. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_INT,l,hreg);
  544. { keep alignment for index }
  545. sref.ref.alignment := left.resultdef.alignment;
  546. if not ispowerof2(sref.ref.alignment,temp) then
  547. internalerror(2006081201);
  548. alignpower:=temp;
  549. offsetreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  550. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_ADDR,3+alignpower,hreg,offsetreg);
  551. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,alignpower,offsetreg);
  552. if (sref.ref.base = NR_NO) then
  553. sref.ref.base := offsetreg
  554. else if (sref.ref.index = NR_NO) then
  555. sref.ref.index := offsetreg
  556. else
  557. begin
  558. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,sref.ref.base,offsetreg);
  559. sref.ref.base := offsetreg;
  560. end;
  561. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,(1 shl (3+alignpower))-1,hreg);
  562. sref.bitindexreg := hreg;
  563. sref.startbit := 0;
  564. sref.bitlen := resultdef.packedbitsize;
  565. if (left.location.loc = LOC_REFERENCE) then
  566. location.loc := LOC_SUBSETREF
  567. else
  568. location.loc := LOC_CSUBSETREF;
  569. location.sref := sref;
  570. end;
  571. procedure tcgvecnode.second_wideansistring;
  572. begin
  573. end;
  574. procedure tcgvecnode.second_dynamicarray;
  575. begin
  576. end;
  577. procedure tcgvecnode.rangecheck_array;
  578. var
  579. hightree : tnode;
  580. poslabel,
  581. neglabel : tasmlabel;
  582. hreg : tregister;
  583. paraloc1,paraloc2 : tcgpara;
  584. begin
  585. { omit range checking when this is an array access to a pointer which has been
  586. typecasted from an array }
  587. if (ado_isconvertedpointer in tarraydef(left.resultdef).arrayoptions) then
  588. exit;
  589. paraloc1.init;
  590. paraloc2.init;
  591. if is_open_array(left.resultdef) or
  592. is_array_of_const(left.resultdef) then
  593. begin
  594. { cdecl functions don't have high() so we can not check the range }
  595. { (can't use current_procdef, since it may be a nested procedure) }
  596. if not(tprocdef(tparasymtable(tparavarsym(tloadnode(left).symtableentry).owner).defowner).proccalloption in cdecl_pocalls) then
  597. begin
  598. { Get high value }
  599. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  600. { it must be available }
  601. if not assigned(hightree) then
  602. internalerror(200212201);
  603. firstpass(hightree);
  604. secondpass(hightree);
  605. { generate compares }
  606. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  607. hreg:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_INT)
  608. else
  609. begin
  610. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  611. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_INT,right.location,hreg);
  612. end;
  613. current_asmdata.getjumplabel(neglabel);
  614. current_asmdata.getjumplabel(poslabel);
  615. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_LT,0,hreg,poslabel);
  616. cg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  617. cg.a_label(current_asmdata.CurrAsmList,poslabel);
  618. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RANGEERROR',false);
  619. cg.a_label(current_asmdata.CurrAsmList,neglabel);
  620. { release hightree }
  621. hightree.free;
  622. end;
  623. end
  624. else
  625. if is_dynamic_array(left.resultdef) then
  626. begin
  627. paramanager.getintparaloc(pocall_default,1,paraloc1);
  628. paramanager.getintparaloc(pocall_default,2,paraloc2);
  629. cg.a_load_loc_cgpara(current_asmdata.CurrAsmList,right.location,paraloc2);
  630. cg.a_load_loc_cgpara(current_asmdata.CurrAsmList,left.location,paraloc1);
  631. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  632. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc2);
  633. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  634. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DYNARRAY_RANGECHECK',false);
  635. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  636. end;
  637. { for regular arrays, we don't have to do anything because the index has been
  638. type converted to the index type, which already inserted a range check if
  639. necessary }
  640. paraloc1.done;
  641. paraloc2.done;
  642. end;
  643. procedure tcgvecnode.rangecheck_string;
  644. var
  645. paraloc1,
  646. paraloc2: tcgpara;
  647. begin
  648. paraloc1.init;
  649. paraloc2.init;
  650. case tstringdef(left.resultdef).stringtype of
  651. { it's the same for ansi- and wide strings }
  652. st_unicodestring,
  653. st_widestring,
  654. st_ansistring:
  655. begin
  656. paramanager.getintparaloc(pocall_default,1,paraloc1);
  657. paramanager.getintparaloc(pocall_default,2,paraloc2);
  658. cg.a_load_loc_cgpara(current_asmdata.CurrAsmList,left.location,paraloc1);
  659. cg.a_load_loc_cgpara(current_asmdata.CurrAsmList,right.location,paraloc2);
  660. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  661. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc2);
  662. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  663. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resultdef).stringtypname)+'_RANGECHECK',false);
  664. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  665. end;
  666. st_shortstring:
  667. begin
  668. {!!!!!!!!!!!!!!!!!}
  669. { if this one is implemented making use of the high parameter for openshortstrings, update ncgutils.do_get_used_regvars() too (JM) }
  670. end;
  671. st_longstring:
  672. begin
  673. {!!!!!!!!!!!!!!!!!}
  674. end;
  675. end;
  676. paraloc1.done;
  677. paraloc2.done;
  678. end;
  679. procedure tcgvecnode.pass_generate_code;
  680. var
  681. offsetdec,
  682. extraoffset : aint;
  683. t : tnode;
  684. otl,ofl : tasmlabel;
  685. newsize : tcgsize;
  686. mulsize,
  687. bytemulsize,
  688. alignpow : aint;
  689. isjump : boolean;
  690. paraloc1,
  691. paraloc2 : tcgpara;
  692. subsetref : tsubsetreference;
  693. temp : longint;
  694. begin
  695. paraloc1.init;
  696. paraloc2.init;
  697. mulsize:=get_mul_size;
  698. if not is_packed_array(left.resultdef) then
  699. bytemulsize:=mulsize
  700. else
  701. bytemulsize:=mulsize div 8;
  702. newsize:=def_cgsize(resultdef);
  703. secondpass(left);
  704. if left.location.loc=LOC_CREFERENCE then
  705. location_reset_ref(location,LOC_CREFERENCE,newsize,left.location.reference.alignment)
  706. else
  707. location_reset_ref(location,LOC_REFERENCE,newsize,left.location.reference.alignment);
  708. { an ansistring needs to be dereferenced }
  709. if is_ansistring(left.resultdef) or
  710. is_wide_or_unicode_string(left.resultdef) then
  711. begin
  712. if nf_callunique in flags then
  713. internalerror(200304236);
  714. {DM!!!!!}
  715. case left.location.loc of
  716. LOC_REGISTER,
  717. LOC_CREGISTER :
  718. begin
  719. {$ifdef m68k}
  720. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  721. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,location.reference.base);
  722. {$else m68k}
  723. location.reference.base:=left.location.register;
  724. {$endif m68k}
  725. end;
  726. LOC_CREFERENCE,
  727. LOC_REFERENCE :
  728. begin
  729. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  730. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.reference.base);
  731. end;
  732. else
  733. internalerror(2002032218);
  734. end;
  735. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  736. if is_ansistring(left.resultdef) then
  737. offsetdec:=1
  738. else
  739. offsetdec:=2;
  740. location.reference.alignment:=offsetdec;
  741. dec(location.reference.offset,offsetdec);
  742. end
  743. else if is_dynamic_array(left.resultdef) then
  744. begin
  745. case left.location.loc of
  746. LOC_REGISTER,
  747. LOC_CREGISTER :
  748. location.reference.base:=left.location.register;
  749. LOC_REFERENCE,
  750. LOC_CREFERENCE :
  751. begin
  752. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  753. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  754. left.location.reference,location.reference.base);
  755. end;
  756. else
  757. internalerror(2002032219);
  758. end;
  759. { a dynarray points to the start of a memory block, which
  760. we assume to be always aligned to a multiple of the
  761. pointer size
  762. }
  763. location.reference.alignment:=sizeof(pint);
  764. end
  765. else
  766. begin
  767. { may happen in case of function results }
  768. case left.location.loc of
  769. LOC_REGISTER,
  770. LOC_MMREGISTER:
  771. location_force_mem(current_asmdata.CurrAsmList,left.location);
  772. end;
  773. location_copy(location,left.location);
  774. end;
  775. { location must be memory }
  776. if not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  777. internalerror(200411013);
  778. { offset can only differ from 0 if arraydef }
  779. if (left.resultdef.typ=arraydef) and
  780. not(is_dynamic_array(left.resultdef)) and
  781. (not(is_packed_array(left.resultdef)) or
  782. ((mulsize mod 8 = 0) and
  783. ispowerof2(mulsize div 8,temp)) or
  784. { only orddefs are bitpacked }
  785. not is_ordinal(resultdef)
  786. {$ifndef cpu64bitalu}
  787. or is_64bitint(resultdef)
  788. {$endif not cpu64bitalu}
  789. ) then
  790. dec(location.reference.offset,bytemulsize*tarraydef(left.resultdef).lowrange);
  791. if right.nodetype=ordconstn then
  792. begin
  793. { offset can only differ from 0 if arraydef }
  794. if cs_check_range in current_settings.localswitches then
  795. begin
  796. secondpass(right);
  797. case left.resultdef.typ of
  798. arraydef :
  799. rangecheck_array;
  800. stringdef :
  801. rangecheck_string;
  802. end;
  803. end;
  804. if not(is_packed_array(left.resultdef)) or
  805. ((mulsize mod 8 = 0) and
  806. (ispowerof2(mulsize div 8,temp) or
  807. { only orddefs are bitpacked }
  808. not is_ordinal(resultdef))) then
  809. begin
  810. extraoffset:=bytemulsize*tordconstnode(right).value.svalue;
  811. inc(location.reference.offset,extraoffset);
  812. { adjust alignment after to this change }
  813. location.reference.alignment:=newalignment(location.reference.alignment,extraoffset);
  814. { don't do this for floats etc.; needed to properly set the }
  815. { size for bitpacked arrays (e.g. a bitpacked array of }
  816. { enums who are size 2 but fit in one byte -> in the array }
  817. { they will be one byte and have to be stored like that) }
  818. if is_packed_array(left.resultdef) and
  819. (tcgsize2size[newsize] <> bytemulsize) then
  820. newsize:=int_cgsize(bytemulsize);
  821. end
  822. else
  823. begin
  824. subsetref.ref := location.reference;
  825. subsetref.ref.alignment := left.resultdef.alignment;
  826. if not ispowerof2(subsetref.ref.alignment,temp) then
  827. internalerror(2006081212);
  828. alignpow:=temp;
  829. inc(subsetref.ref.offset,((mulsize * (tordconstnode(right).value.svalue-tarraydef(left.resultdef).lowrange)) shr (3+alignpow)) shl alignpow);
  830. subsetref.bitindexreg := NR_NO;
  831. subsetref.startbit := (mulsize * (tordconstnode(right).value.svalue-tarraydef(left.resultdef).lowrange)) and ((1 shl (3+alignpow))-1);
  832. subsetref.bitlen := resultdef.packedbitsize;
  833. if (left.location.loc = LOC_REFERENCE) then
  834. location.loc := LOC_SUBSETREF
  835. else
  836. location.loc := LOC_CSUBSETREF;
  837. location.sref := subsetref;
  838. end;
  839. end
  840. else
  841. { not nodetype=ordconstn }
  842. begin
  843. if (cs_opt_level1 in current_settings.optimizerswitches) and
  844. { if we do range checking, we don't }
  845. { need that fancy code (it would be }
  846. { buggy) }
  847. not(cs_check_range in current_settings.localswitches) and
  848. (left.resultdef.typ=arraydef) and
  849. not is_packed_array(left.resultdef) then
  850. begin
  851. extraoffset:=0;
  852. if (right.nodetype=addn) then
  853. begin
  854. if taddnode(right).right.nodetype=ordconstn then
  855. begin
  856. extraoffset:=tordconstnode(taddnode(right).right).value.svalue;
  857. t:=taddnode(right).left;
  858. taddnode(right).left:=nil;
  859. right.free;
  860. right:=t;
  861. end
  862. else if taddnode(right).left.nodetype=ordconstn then
  863. begin
  864. extraoffset:=tordconstnode(taddnode(right).left).value.svalue;
  865. t:=taddnode(right).right;
  866. taddnode(right).right:=nil;
  867. right.free;
  868. right:=t;
  869. end;
  870. end
  871. else if (right.nodetype=subn) then
  872. begin
  873. if taddnode(right).right.nodetype=ordconstn then
  874. begin
  875. extraoffset:=-tordconstnode(taddnode(right).right).value.svalue;
  876. t:=taddnode(right).left;
  877. taddnode(right).left:=nil;
  878. right.free;
  879. right:=t;
  880. end;
  881. end;
  882. inc(location.reference.offset,
  883. mulsize*extraoffset);
  884. end;
  885. { calculate from left to right }
  886. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  887. internalerror(200304237);
  888. isjump:=(right.expectloc=LOC_JUMP);
  889. if isjump then
  890. begin
  891. otl:=current_procinfo.CurrTrueLabel;
  892. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  893. ofl:=current_procinfo.CurrFalseLabel;
  894. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  895. end;
  896. secondpass(right);
  897. { if mulsize = 1, we won't have to modify the index }
  898. location_force_reg(current_asmdata.CurrAsmList,right.location,OS_ADDR,true);
  899. if isjump then
  900. begin
  901. current_procinfo.CurrTrueLabel:=otl;
  902. current_procinfo.CurrFalseLabel:=ofl;
  903. end
  904. else if (right.location.loc = LOC_JUMP) then
  905. internalerror(2006010801);
  906. { produce possible range check code: }
  907. if cs_check_range in current_settings.localswitches then
  908. begin
  909. if left.resultdef.typ=arraydef then
  910. rangecheck_array
  911. else if (left.resultdef.typ=stringdef) then
  912. rangecheck_string;
  913. end;
  914. { insert the register and the multiplication factor in the
  915. reference }
  916. if not is_packed_array(left.resultdef) then
  917. update_reference_reg_mul(right.location.register,mulsize)
  918. else
  919. update_reference_reg_packed(right.location.register,mulsize);
  920. end;
  921. location.size:=newsize;
  922. paraloc1.done;
  923. paraloc2.done;
  924. end;
  925. begin
  926. cloadvmtaddrnode:=tcgloadvmtaddrnode;
  927. cloadparentfpnode:=tcgloadparentfpnode;
  928. caddrnode:=tcgaddrnode;
  929. cderefnode:=tcgderefnode;
  930. csubscriptnode:=tcgsubscriptnode;
  931. cwithnode:=tcgwithnode;
  932. cvecnode:=tcgvecnode;
  933. end.