ncgmem.pas 46 KB

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