ncgmem.pas 46 KB

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