ncgmem.pas 43 KB

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