ncgmem.pas 48 KB

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