ncgmem.pas 43 KB

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