ncgmem.pas 44 KB

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