ncgmem.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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 (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. internalerror(2006031901);
  327. end;
  328. end;
  329. if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  330. begin
  331. if not is_packed_record_or_object(left.resultdef) then
  332. begin
  333. inc(location.reference.offset,vs.fieldoffset);
  334. {$ifdef SUPPORT_UNALIGNED}
  335. { packed? }
  336. if (vs.owner.defowner.typ in [recorddef,objectdef]) and
  337. (tabstractrecordsymtable(vs.owner).usefieldalignment=1) then
  338. location.reference.alignment:=1;
  339. {$endif SUPPORT_UNALIGNED}
  340. end
  341. else if (vs.fieldoffset mod 8 = 0) and
  342. (resultdef.packedbitsize mod 8 = 0) and
  343. { is different in case of e.g. packenum 2 and an enum }
  344. { which fits in 8 bits }
  345. (resultdef.size*8 = resultdef.packedbitsize) then
  346. begin
  347. inc(location.reference.offset,vs.fieldoffset div 8);
  348. if (resultdef.size*8 <> resultdef.packedbitsize) then
  349. internalerror(2006082013);
  350. { packed records always have an alignment of 1 }
  351. location.reference.alignment:=1;
  352. end
  353. else
  354. begin
  355. sref.ref:=location.reference;
  356. sref.ref.alignment:=1;
  357. sref.bitindexreg:=NR_NO;
  358. inc(sref.ref.offset,vs.fieldoffset div 8);
  359. sref.startbit:=vs.fieldoffset mod 8;
  360. sref.bitlen:=resultdef.packedbitsize;
  361. if (left.location.loc=LOC_REFERENCE) then
  362. location.loc:=LOC_SUBSETREF
  363. else
  364. location.loc:=LOC_CSUBSETREF;
  365. location.sref:=sref;
  366. end;
  367. { also update the size of the location }
  368. location.size:=def_cgsize(resultdef);
  369. end;
  370. paraloc1.done;
  371. end;
  372. {*****************************************************************************
  373. TCGWITHNODE
  374. *****************************************************************************}
  375. procedure tcgwithnode.pass_generate_code;
  376. begin
  377. location_reset(location,LOC_VOID,OS_NO);
  378. if assigned(left) then
  379. secondpass(left);
  380. end;
  381. {*****************************************************************************
  382. TCGVECNODE
  383. *****************************************************************************}
  384. function tcgvecnode.get_mul_size : aint;
  385. begin
  386. if nf_memindex in flags then
  387. get_mul_size:=1
  388. else
  389. begin
  390. if (left.resultdef.typ=arraydef) then
  391. if not is_packed_array(left.resultdef) then
  392. get_mul_size:=tarraydef(left.resultdef).elesize
  393. else
  394. get_mul_size:=tarraydef(left.resultdef).elepackedbitsize
  395. else
  396. get_mul_size:=resultdef.size;
  397. end
  398. end;
  399. procedure tcgvecnode.update_reference_reg_mul(reg:tregister;l:aint);
  400. var
  401. hreg: tregister;
  402. begin
  403. if location.reference.base=NR_NO then
  404. begin
  405. if l<>1 then
  406. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  407. location.reference.base:=reg;
  408. end
  409. else if location.reference.index=NR_NO then
  410. begin
  411. if l<>1 then
  412. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  413. location.reference.index:=reg;
  414. end
  415. else
  416. begin
  417. hreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  418. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,location.reference,hreg);
  419. reference_reset_base(location.reference,hreg,0);
  420. { insert new index register }
  421. if l<>1 then
  422. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  423. location.reference.index:=reg;
  424. end;
  425. end;
  426. procedure tcgvecnode.update_reference_reg_packed(reg:tregister;l:aint);
  427. var
  428. sref: tsubsetreference;
  429. offsetreg: tregister;
  430. byteoffs, bitoffs, alignpower: aint;
  431. temp : longint;
  432. begin
  433. { only orddefs are bitpacked. Even then we only need special code in }
  434. { case the bitpacked *byte size* is not a power of two, otherwise }
  435. { everything can be handled using the the regular array code. }
  436. if ((l mod 8) = 0) and
  437. (ispowerof2(l div 8,temp) or
  438. not is_ordinal(resultdef)) then
  439. begin
  440. update_reference_reg_mul(reg,l div 8);
  441. exit;
  442. end;
  443. if (l > 8*sizeof(aint)) then
  444. internalerror(200608051);
  445. sref.ref := location.reference;
  446. offsetreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  447. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,tarraydef(left.resultdef).lowrange,reg);
  448. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_INT,l,reg);
  449. { keep alignment for index }
  450. sref.ref.alignment := left.resultdef.alignment;
  451. if not ispowerof2(sref.ref.alignment,temp) then
  452. internalerror(2006081201);
  453. alignpower:=temp;
  454. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_ADDR,3+alignpower,reg,offsetreg);
  455. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,alignpower,offsetreg);
  456. if (sref.ref.base = NR_NO) then
  457. sref.ref.base := offsetreg
  458. else if (sref.ref.index = NR_NO) then
  459. sref.ref.index := offsetreg
  460. else
  461. begin
  462. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,sref.ref.base,offsetreg);
  463. sref.ref.base := offsetreg;
  464. end;
  465. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,(1 shl (3+alignpower))-1,reg);
  466. sref.bitindexreg := reg;
  467. sref.startbit := 0;
  468. sref.bitlen := resultdef.packedbitsize;
  469. if (left.location.loc = LOC_REFERENCE) then
  470. location.loc := LOC_SUBSETREF
  471. else
  472. location.loc := LOC_CSUBSETREF;
  473. location.sref := sref;
  474. end;
  475. procedure tcgvecnode.second_wideansistring;
  476. begin
  477. end;
  478. procedure tcgvecnode.second_dynamicarray;
  479. begin
  480. end;
  481. procedure tcgvecnode.rangecheck_array;
  482. var
  483. hightree : tnode;
  484. poslabel,
  485. neglabel : tasmlabel;
  486. hreg : tregister;
  487. paraloc1,paraloc2 : tcgpara;
  488. begin
  489. paraloc1.init;
  490. paraloc2.init;
  491. if is_open_array(left.resultdef) or
  492. is_array_of_const(left.resultdef) then
  493. begin
  494. { cdecl functions don't have high() so we can not check the range }
  495. { (can't use current_procdef, since it may be a nested procedure) }
  496. if not(tprocdef(tparasymtable(tparavarsym(tloadnode(left).symtableentry).owner).defowner).proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  497. begin
  498. { Get high value }
  499. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  500. { it must be available }
  501. if not assigned(hightree) then
  502. internalerror(200212201);
  503. firstpass(hightree);
  504. secondpass(hightree);
  505. { generate compares }
  506. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  507. hreg:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_INT)
  508. else
  509. begin
  510. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  511. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_INT,right.location,hreg);
  512. end;
  513. current_asmdata.getjumplabel(neglabel);
  514. current_asmdata.getjumplabel(poslabel);
  515. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_LT,0,hreg,poslabel);
  516. cg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  517. cg.a_label(current_asmdata.CurrAsmList,poslabel);
  518. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RANGEERROR');
  519. cg.a_label(current_asmdata.CurrAsmList,neglabel);
  520. { release hightree }
  521. hightree.free;
  522. end;
  523. end
  524. else
  525. if is_dynamic_array(left.resultdef) then
  526. begin
  527. paramanager.getintparaloc(pocall_default,1,paraloc1);
  528. paramanager.getintparaloc(pocall_default,2,paraloc2);
  529. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  530. cg.a_param_loc(current_asmdata.CurrAsmList,right.location,paraloc2);
  531. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  532. cg.a_param_loc(current_asmdata.CurrAsmList,left.location,paraloc1);
  533. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  534. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  535. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  536. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DYNARRAY_RANGECHECK');
  537. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  538. end
  539. else
  540. cg.g_rangecheck(current_asmdata.CurrAsmList,right.location,right.resultdef,left.resultdef);
  541. paraloc1.done;
  542. paraloc2.done;
  543. end;
  544. procedure tcgvecnode.pass_generate_code;
  545. var
  546. offsetdec,
  547. extraoffset : aint;
  548. t : tnode;
  549. href : treference;
  550. otl,ofl : tasmlabel;
  551. newsize : tcgsize;
  552. mulsize,
  553. bytemulsize,
  554. alignpow : aint;
  555. isjump : boolean;
  556. paraloc1,
  557. paraloc2 : tcgpara;
  558. subsetref : tsubsetreference;
  559. temp : longint;
  560. begin
  561. paraloc1.init;
  562. paraloc2.init;
  563. mulsize:=get_mul_size;
  564. if not is_packed_array(left.resultdef) then
  565. bytemulsize:=mulsize
  566. else
  567. bytemulsize:=mulsize div 8;
  568. newsize:=def_cgsize(resultdef);
  569. secondpass(left);
  570. if left.location.loc=LOC_CREFERENCE then
  571. location_reset(location,LOC_CREFERENCE,newsize)
  572. else
  573. location_reset(location,LOC_REFERENCE,newsize);
  574. { an ansistring needs to be dereferenced }
  575. if is_ansistring(left.resultdef) or
  576. is_widestring(left.resultdef) then
  577. begin
  578. if nf_callunique in flags then
  579. internalerror(200304236);
  580. {DM!!!!!}
  581. case left.location.loc of
  582. LOC_REGISTER,
  583. LOC_CREGISTER :
  584. begin
  585. {$ifdef m68k}
  586. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  587. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,location.reference.base);
  588. {$else m68k}
  589. location.reference.base:=left.location.register;
  590. {$endif m68k}
  591. end;
  592. LOC_CREFERENCE,
  593. LOC_REFERENCE :
  594. begin
  595. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  596. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.reference.base);
  597. end;
  598. else
  599. internalerror(2002032218);
  600. end;
  601. { check for a zero length string,
  602. we can use the ansistring routine here }
  603. if (cs_check_range in current_settings.localswitches) then
  604. begin
  605. paramanager.getintparaloc(pocall_default,1,paraloc1);
  606. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  607. cg.a_param_reg(current_asmdata.CurrAsmList,OS_ADDR,location.reference.base,paraloc1);
  608. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  609. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  610. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resultdef).stringtypname)+'_CHECKZERO');
  611. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  612. end;
  613. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  614. if is_ansistring(left.resultdef) then
  615. offsetdec:=1
  616. else
  617. offsetdec:=2;
  618. dec(location.reference.offset,offsetdec);
  619. end
  620. else if is_dynamic_array(left.resultdef) then
  621. begin
  622. case left.location.loc of
  623. LOC_REGISTER,
  624. LOC_CREGISTER :
  625. location.reference.base:=left.location.register;
  626. LOC_REFERENCE,
  627. LOC_CREFERENCE :
  628. begin
  629. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  630. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  631. left.location.reference,location.reference.base);
  632. end;
  633. else
  634. internalerror(2002032219);
  635. end;
  636. end
  637. else
  638. location_copy(location,left.location);
  639. { location must be memory }
  640. if not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  641. internalerror(200411013);
  642. { offset can only differ from 0 if arraydef }
  643. if (left.resultdef.typ=arraydef) and
  644. not(is_dynamic_array(left.resultdef)) and
  645. (not(is_packed_array(left.resultdef)) or
  646. ((mulsize mod 8 = 0) and
  647. ispowerof2(mulsize div 8,temp)) or
  648. { only orddefs are bitpacked }
  649. not is_ordinal(resultdef)) then
  650. dec(location.reference.offset,bytemulsize*tarraydef(left.resultdef).lowrange);
  651. if right.nodetype=ordconstn then
  652. begin
  653. { offset can only differ from 0 if arraydef }
  654. case left.resultdef.typ of
  655. arraydef :
  656. begin
  657. if not(is_open_array(left.resultdef)) and
  658. not(is_array_of_const(left.resultdef)) and
  659. not(is_dynamic_array(left.resultdef)) then
  660. begin
  661. if (tordconstnode(right).value>tarraydef(left.resultdef).highrange) or
  662. (tordconstnode(right).value<tarraydef(left.resultdef).lowrange) then
  663. begin
  664. { this should be caught in the typecheckpass! (JM) }
  665. if (cs_check_range in current_settings.localswitches) then
  666. CGMessage(parser_e_range_check_error)
  667. else
  668. CGMessage(parser_w_range_check_error);
  669. end;
  670. end
  671. else
  672. begin
  673. { range checking for open and dynamic arrays needs
  674. runtime code }
  675. secondpass(right);
  676. if (cs_check_range in current_settings.localswitches) then
  677. rangecheck_array;
  678. end;
  679. end;
  680. stringdef :
  681. begin
  682. if (cs_check_range in current_settings.localswitches) then
  683. begin
  684. case tstringdef(left.resultdef).stringtype of
  685. { it's the same for ansi- and wide strings }
  686. st_widestring,
  687. st_ansistring:
  688. begin
  689. paramanager.getintparaloc(pocall_default,1,paraloc1);
  690. paramanager.getintparaloc(pocall_default,2,paraloc2);
  691. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  692. cg.a_param_const(current_asmdata.CurrAsmList,OS_INT,tordconstnode(right).value,paraloc2);
  693. href:=location.reference;
  694. dec(href.offset,sizeof(aint)-offsetdec);
  695. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  696. cg.a_param_ref(current_asmdata.CurrAsmList,OS_INT,href,paraloc1);
  697. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  698. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  699. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  700. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resultdef).stringtypname)+'_RANGECHECK');
  701. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  702. end;
  703. st_shortstring:
  704. begin
  705. {!!!!!!!!!!!!!!!!!}
  706. { if this one is implemented making use of the high parameter for openshortstrings, update ncgutils.do_get_used_regvars() too (JM) }
  707. end;
  708. st_longstring:
  709. begin
  710. {!!!!!!!!!!!!!!!!!}
  711. end;
  712. end;
  713. end;
  714. end;
  715. end;
  716. if not(is_packed_array(left.resultdef)) or
  717. ((mulsize mod 8 = 0) and
  718. (ispowerof2(mulsize div 8,temp) or
  719. { only orddefs are bitpacked }
  720. not is_ordinal(resultdef))) then
  721. begin
  722. inc(location.reference.offset,
  723. bytemulsize*tordconstnode(right).value);
  724. { don't do this for floats etc.; needed to properly set the }
  725. { size for bitpacked arrays (e.g. a bitpacked array of }
  726. { enums who are size 2 but fit in one byte -> in the array }
  727. { they will be one byte and have to be stored like that) }
  728. if is_packed_array(left.resultdef) and
  729. (tcgsize2size[newsize] <> bytemulsize) then
  730. newsize:=int_cgsize(bytemulsize);
  731. end
  732. else
  733. begin
  734. subsetref.ref := location.reference;
  735. subsetref.ref.alignment := left.resultdef.alignment;
  736. if not ispowerof2(subsetref.ref.alignment,temp) then
  737. internalerror(2006081212);
  738. alignpow:=temp;
  739. inc(subsetref.ref.offset,((mulsize * (tordconstnode(right).value-tarraydef(left.resultdef).lowrange)) shr (3+alignpow)) shl alignpow);
  740. subsetref.bitindexreg := NR_NO;
  741. subsetref.startbit := (mulsize * (tordconstnode(right).value-tarraydef(left.resultdef).lowrange)) and ((1 shl (3+alignpow))-1);
  742. subsetref.bitlen := resultdef.packedbitsize;
  743. if (left.location.loc = LOC_REFERENCE) then
  744. location.loc := LOC_SUBSETREF
  745. else
  746. location.loc := LOC_CSUBSETREF;
  747. location.sref := subsetref;
  748. end;
  749. end
  750. else
  751. { not nodetype=ordconstn }
  752. begin
  753. if (cs_opt_level1 in current_settings.optimizerswitches) and
  754. { if we do range checking, we don't }
  755. { need that fancy code (it would be }
  756. { buggy) }
  757. not(cs_check_range in current_settings.localswitches) and
  758. (left.resultdef.typ=arraydef) and
  759. not is_packed_array(left.resultdef) then
  760. begin
  761. extraoffset:=0;
  762. if (right.nodetype=addn) then
  763. begin
  764. if taddnode(right).right.nodetype=ordconstn then
  765. begin
  766. extraoffset:=tordconstnode(taddnode(right).right).value;
  767. t:=taddnode(right).left;
  768. { First pass processed this with the assumption }
  769. { that there was an add node which may require an }
  770. { extra register. Fake it or die with IE10 (JM) }
  771. t.registersint := taddnode(right).registersint;
  772. taddnode(right).left:=nil;
  773. right.free;
  774. right:=t;
  775. end
  776. else if taddnode(right).left.nodetype=ordconstn then
  777. begin
  778. extraoffset:=tordconstnode(taddnode(right).left).value;
  779. t:=taddnode(right).right;
  780. t.registersint := right.registersint;
  781. taddnode(right).right:=nil;
  782. right.free;
  783. right:=t;
  784. end;
  785. end
  786. else if (right.nodetype=subn) then
  787. begin
  788. if taddnode(right).right.nodetype=ordconstn then
  789. begin
  790. extraoffset:=-tordconstnode(taddnode(right).right).value;
  791. t:=taddnode(right).left;
  792. t.registersint := right.registersint;
  793. taddnode(right).left:=nil;
  794. right.free;
  795. right:=t;
  796. end
  797. { You also have to negate right.right in this case! I can't add an
  798. unaryminusn without causing a crash, so I've disabled it (JM)
  799. else if right.left.nodetype=ordconstn then
  800. begin
  801. extraoffset:=right.left.value;
  802. t:=right.right;
  803. t^.registersint := right.registersint;
  804. putnode(right);
  805. putnode(right.left);
  806. right:=t;
  807. end;}
  808. end;
  809. inc(location.reference.offset,
  810. mulsize*extraoffset);
  811. end;
  812. { calculate from left to right }
  813. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  814. internalerror(200304237);
  815. isjump:=(right.expectloc=LOC_JUMP);
  816. if isjump then
  817. begin
  818. otl:=current_procinfo.CurrTrueLabel;
  819. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  820. ofl:=current_procinfo.CurrFalseLabel;
  821. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  822. end;
  823. secondpass(right);
  824. { if mulsize = 1, we won't have to modify the index }
  825. location_force_reg(current_asmdata.CurrAsmList,right.location,OS_ADDR,not is_packed_array(left.resultdef) and (mulsize = 1) );
  826. if isjump then
  827. begin
  828. current_procinfo.CurrTrueLabel:=otl;
  829. current_procinfo.CurrFalseLabel:=ofl;
  830. end
  831. else if (right.location.loc = LOC_JUMP) then
  832. internalerror(2006010801);
  833. { only range check now, we can't range check loc_flags/loc_jump }
  834. if cs_check_range in current_settings.localswitches then
  835. begin
  836. if left.resultdef.typ=arraydef then
  837. rangecheck_array;
  838. end;
  839. { produce possible range check code: }
  840. if cs_check_range in current_settings.localswitches then
  841. begin
  842. if left.resultdef.typ=arraydef then
  843. begin
  844. { done defore (PM) }
  845. end
  846. else if (left.resultdef.typ=stringdef) then
  847. begin
  848. case tstringdef(left.resultdef).stringtype of
  849. { it's the same for ansi- and wide strings }
  850. st_widestring,
  851. st_ansistring:
  852. begin
  853. paramanager.getintparaloc(pocall_default,1,paraloc1);
  854. paramanager.getintparaloc(pocall_default,2,paraloc2);
  855. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  856. cg.a_param_reg(current_asmdata.CurrAsmList,OS_INT,right.location.register,paraloc2);
  857. href:=location.reference;
  858. dec(href.offset,sizeof(aint)-offsetdec);
  859. //dec(href.offset,7);
  860. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  861. cg.a_param_ref(current_asmdata.CurrAsmList,OS_INT,href,paraloc1);
  862. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  863. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  864. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  865. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resultdef).stringtypname)+'_RANGECHECK');
  866. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  867. end;
  868. st_shortstring:
  869. begin
  870. {!!!!!!!!!!!!!!!!!}
  871. end;
  872. st_longstring:
  873. begin
  874. {!!!!!!!!!!!!!!!!!}
  875. end;
  876. end;
  877. end;
  878. end;
  879. { insert the register and the multiplication factor in the
  880. reference }
  881. if not is_packed_array(left.resultdef) then
  882. update_reference_reg_mul(right.location.register,mulsize)
  883. else
  884. update_reference_reg_packed(right.location.register,mulsize);
  885. end;
  886. location.size:=newsize;
  887. paraloc1.done;
  888. paraloc2.done;
  889. end;
  890. begin
  891. cloadvmtaddrnode:=tcgloadvmtaddrnode;
  892. cloadparentfpnode:=tcgloadparentfpnode;
  893. caddrnode:=tcgaddrnode;
  894. cderefnode:=tcgderefnode;
  895. csubscriptnode:=tcgsubscriptnode;
  896. cwithnode:=tcgwithnode;
  897. cvecnode:=tcgvecnode;
  898. end.