ncgmem.pas 46 KB

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