ncgmem.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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_2;override;
  27. end;
  28. tcgloadparentfpnode = class(tloadparentfpnode)
  29. procedure pass_2;override;
  30. end;
  31. tcgaddrnode = class(taddrnode)
  32. procedure pass_2;override;
  33. end;
  34. tcgderefnode = class(tderefnode)
  35. procedure pass_2;override;
  36. end;
  37. tcgsubscriptnode = class(tsubscriptnode)
  38. procedure pass_2;override;
  39. end;
  40. tcgwithnode = class(twithnode)
  41. procedure pass_2;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_2;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_2;
  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(resulttype.def).pointertype.def).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.resulttype.def),left.location,location.register);
  92. end;
  93. end;
  94. {*****************************************************************************
  95. TCGLOADPARENTFPNODE
  96. *****************************************************************************}
  97. procedure tcgloadparentfpnode.pass_2;
  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.search('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.search('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_2;
  138. begin
  139. secondpass(left);
  140. location_reset(location,LOC_REGISTER,OS_ADDR);
  141. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  142. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  143. end;
  144. {*****************************************************************************
  145. TCGDEREFNODE
  146. *****************************************************************************}
  147. procedure tcgderefnode.pass_2;
  148. var
  149. paraloc1 : tcgpara;
  150. begin
  151. secondpass(left);
  152. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  153. case left.location.loc of
  154. LOC_CREGISTER,
  155. LOC_REGISTER:
  156. begin
  157. {$ifdef cpu_uses_separate_address_registers}
  158. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  159. begin
  160. location.reference.base := cg.getaddressregister(current_asmdata.CurrAsmList);
  161. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,
  162. location.reference.base);
  163. end
  164. else
  165. {$endif}
  166. location.reference.base := left.location.register;
  167. end;
  168. LOC_CREFERENCE,
  169. LOC_REFERENCE:
  170. begin
  171. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  172. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,left.location,location.reference.base);
  173. end;
  174. LOC_CONSTANT:
  175. begin
  176. location.reference.offset:=left.location.value;
  177. end;
  178. else
  179. internalerror(200507031);
  180. end;
  181. if (cs_use_heaptrc in aktglobalswitches) and
  182. (cs_checkpointer in aktlocalswitches) and
  183. not(cs_compilesystem in aktmoduleswitches) and
  184. not(tpointerdef(left.resulttype.def).is_far) and
  185. not(nf_no_checkpointer in flags) then
  186. begin
  187. paraloc1.init;
  188. paramanager.getintparaloc(pocall_default,1,paraloc1);
  189. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  190. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  191. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  192. paraloc1.done;
  193. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  194. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER');
  195. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  196. end;
  197. end;
  198. {*****************************************************************************
  199. TCGSUBSCRIPTNODE
  200. *****************************************************************************}
  201. procedure tcgsubscriptnode.pass_2;
  202. var
  203. paraloc1 : tcgpara;
  204. begin
  205. secondpass(left);
  206. if codegenerror then
  207. exit;
  208. paraloc1.init;
  209. { classes and interfaces must be dereferenced implicit }
  210. if is_class_or_interface(left.resulttype.def) then
  211. begin
  212. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  213. case left.location.loc of
  214. LOC_CREGISTER,
  215. LOC_REGISTER:
  216. begin
  217. {$ifdef cpu_uses_separate_address_registers}
  218. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  219. begin
  220. location.reference.base:=rg.getaddressregister(current_asmdata.CurrAsmList);
  221. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  222. left.location.register,location.reference.base);
  223. end
  224. else
  225. {$endif}
  226. location.reference.base := left.location.register;
  227. end;
  228. LOC_CREFERENCE,
  229. LOC_REFERENCE:
  230. begin
  231. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  232. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,left.location,location.reference.base);
  233. end;
  234. end;
  235. { implicit deferencing }
  236. if (cs_use_heaptrc in aktglobalswitches) and
  237. (cs_checkpointer in aktlocalswitches) and
  238. not(cs_compilesystem in aktmoduleswitches) then
  239. begin
  240. paramanager.getintparaloc(pocall_default,1,paraloc1);
  241. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  242. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  243. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  244. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  245. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER');
  246. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  247. end;
  248. end
  249. else if is_interfacecom(left.resulttype.def) then
  250. begin
  251. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  252. tg.GetTempTyped(current_asmdata.CurrAsmList,left.resulttype.def,tt_normal,location.reference);
  253. cg.a_load_loc_ref(current_asmdata.CurrAsmList,OS_ADDR,left.location,location.reference);
  254. { implicit deferencing also for interfaces }
  255. if (cs_use_heaptrc in aktglobalswitches) and
  256. (cs_checkpointer in aktlocalswitches) and
  257. not(cs_compilesystem in aktmoduleswitches) then
  258. begin
  259. paramanager.getintparaloc(pocall_default,1,paraloc1);
  260. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  261. cg.a_param_reg(current_asmdata.CurrAsmList, OS_ADDR,location.reference.base,paraloc1);
  262. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  263. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  264. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_CHECKPOINTER');
  265. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  266. end;
  267. end
  268. else
  269. begin
  270. location_copy(location,left.location);
  271. { some abi's require that functions return (some) records in }
  272. { registers }
  273. case location.loc of
  274. LOC_REFERENCE,
  275. LOC_CREFERENCE:
  276. ;
  277. LOC_REGISTER,
  278. LOC_CREGISTER:
  279. begin
  280. if (left.resulttype.def.size > sizeof(aint)) then
  281. location_force_mem(current_asmdata.CurrAsmList,location)
  282. else
  283. begin
  284. if (left.location.loc = LOC_REGISTER) then
  285. location.loc := LOC_SUBSETREG
  286. else
  287. location.loc := LOC_CSUBSETREG;
  288. location.size:=def_cgsize(resulttype.def);
  289. location.sreg.subsetreg := left.location.register;
  290. location.sreg.subsetregsize := left.location.size;
  291. if (target_info.endian = ENDIAN_BIG) then
  292. location.sreg.startbit := (tcgsize2size[location.sreg.subsetregsize] - tcgsize2size[location.size] - vs.fieldoffset) * 8
  293. else
  294. location.sreg.startbit := (vs.fieldoffset * 8);
  295. location.sreg.bitlen := tcgsize2size[location.size] * 8;
  296. end;
  297. end;
  298. LOC_SUBSETREG,
  299. LOC_CSUBSETREG:
  300. begin
  301. location.size:=def_cgsize(resulttype.def);
  302. if (target_info.endian = ENDIAN_BIG) then
  303. inc(location.sreg.startbit, (left.resulttype.def.size - tcgsize2size[location.size] - vs.fieldoffset) * 8)
  304. else
  305. inc(location.sreg.startbit, vs.fieldoffset * 8);
  306. location.sreg.bitlen := tcgsize2size[location.size] * 8;
  307. end;
  308. else
  309. internalerror(2006031901);
  310. end;
  311. end;
  312. if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  313. begin
  314. inc(location.reference.offset,vs.fieldoffset);
  315. {$ifdef SUPPORT_UNALIGNED}
  316. { packed? }
  317. if (vs.owner.defowner.deftype in [recorddef,objectdef]) and
  318. (tabstractrecordsymtable(vs.owner).usefieldalignment=1) then
  319. location.reference.alignment:=1;
  320. {$endif SUPPORT_UNALIGNED}
  321. { also update the size of the location }
  322. location.size:=def_cgsize(resulttype.def);
  323. end;
  324. paraloc1.done;
  325. end;
  326. {*****************************************************************************
  327. TCGWITHNODE
  328. *****************************************************************************}
  329. procedure tcgwithnode.pass_2;
  330. begin
  331. location_reset(location,LOC_VOID,OS_NO);
  332. if assigned(left) then
  333. secondpass(left);
  334. end;
  335. {*****************************************************************************
  336. TCGVECNODE
  337. *****************************************************************************}
  338. function tcgvecnode.get_mul_size : aint;
  339. begin
  340. if nf_memindex in flags then
  341. get_mul_size:=1
  342. else
  343. begin
  344. if (left.resulttype.def.deftype=arraydef) then
  345. if not is_packed_array(left.resulttype.def) then
  346. get_mul_size:=tarraydef(left.resulttype.def).elesize
  347. else
  348. get_mul_size:=tarraydef(left.resulttype.def).elepackedbitsize
  349. else
  350. get_mul_size:=resulttype.def.size;
  351. end
  352. end;
  353. procedure tcgvecnode.update_reference_reg_mul(reg:tregister;l:aint);
  354. var
  355. hreg: tregister;
  356. begin
  357. if location.reference.base=NR_NO then
  358. begin
  359. if l<>1 then
  360. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  361. location.reference.base:=reg;
  362. end
  363. else if location.reference.index=NR_NO then
  364. begin
  365. if l<>1 then
  366. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  367. location.reference.index:=reg;
  368. end
  369. else
  370. begin
  371. hreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  372. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,location.reference,hreg);
  373. reference_reset_base(location.reference,hreg,0);
  374. { insert new index register }
  375. if l<>1 then
  376. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  377. location.reference.index:=reg;
  378. end;
  379. end;
  380. procedure tcgvecnode.update_reference_reg_packed(reg:tregister;l:aint);
  381. var
  382. sref: tsubsetreference;
  383. offsetreg: tregister;
  384. byteoffs, bitoffs, alignpower: aint;
  385. temp : longint;
  386. begin
  387. if (l mod 8) = 0 then
  388. begin
  389. update_reference_reg_mul(reg,l div 8);
  390. exit;
  391. end;
  392. if (l > 8*sizeof(aint)) then
  393. internalerror(200608051);
  394. sref.ref := location.reference;
  395. offsetreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  396. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,tarraydef(left.resulttype.def).lowrange,reg);
  397. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_INT,l,reg);
  398. { keep alignment for index }
  399. sref.ref.alignment := left.resulttype.def.alignment;
  400. if not ispowerof2(sref.ref.alignment,temp) then
  401. internalerror(2006081201);
  402. alignpower:=temp;
  403. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_ADDR,3+alignpower,reg,offsetreg);
  404. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,alignpower,offsetreg);
  405. if (sref.ref.base = NR_NO) then
  406. sref.ref.base := offsetreg
  407. else if (sref.ref.index = NR_NO) then
  408. sref.ref.index := offsetreg
  409. else
  410. begin
  411. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,sref.ref.base,offsetreg);
  412. sref.ref.base := offsetreg;
  413. end;
  414. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,(1 shl (3+alignpower))-1,reg);
  415. sref.bitindexreg := reg;
  416. sref.startbit := 0;
  417. sref.bitlen := resulttype.def.packedbitsize;
  418. location.loc := LOC_SUBSETREF;
  419. location.sref := sref;
  420. end;
  421. procedure tcgvecnode.second_wideansistring;
  422. begin
  423. end;
  424. procedure tcgvecnode.second_dynamicarray;
  425. begin
  426. end;
  427. procedure tcgvecnode.rangecheck_array;
  428. var
  429. hightree : tnode;
  430. poslabel,
  431. neglabel : tasmlabel;
  432. hreg : tregister;
  433. paraloc1,paraloc2 : tcgpara;
  434. begin
  435. paraloc1.init;
  436. paraloc2.init;
  437. if is_open_array(left.resulttype.def) or
  438. is_array_of_const(left.resulttype.def) then
  439. begin
  440. { cdecl functions don't have high() so we can not check the range }
  441. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  442. begin
  443. { Get high value }
  444. hightree:=load_high_value_node(tparavarsym(tloadnode(left).symtableentry));
  445. { it must be available }
  446. if not assigned(hightree) then
  447. internalerror(200212201);
  448. firstpass(hightree);
  449. secondpass(hightree);
  450. { generate compares }
  451. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  452. hreg:=cg.makeregsize(current_asmdata.CurrAsmList,right.location.register,OS_INT)
  453. else
  454. begin
  455. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  456. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_INT,right.location,hreg);
  457. end;
  458. current_asmdata.getjumplabel(neglabel);
  459. current_asmdata.getjumplabel(poslabel);
  460. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_LT,0,hreg,poslabel);
  461. cg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  462. cg.a_label(current_asmdata.CurrAsmList,poslabel);
  463. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_RANGEERROR');
  464. cg.a_label(current_asmdata.CurrAsmList,neglabel);
  465. { release hightree }
  466. hightree.free;
  467. end;
  468. end
  469. else
  470. if is_dynamic_array(left.resulttype.def) then
  471. begin
  472. paramanager.getintparaloc(pocall_default,1,paraloc1);
  473. paramanager.getintparaloc(pocall_default,2,paraloc2);
  474. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  475. cg.a_param_loc(current_asmdata.CurrAsmList,right.location,paraloc2);
  476. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  477. cg.a_param_loc(current_asmdata.CurrAsmList,left.location,paraloc1);
  478. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  479. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  480. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  481. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_DYNARRAY_RANGECHECK');
  482. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  483. end
  484. else
  485. cg.g_rangecheck(current_asmdata.CurrAsmList,right.location,right.resulttype.def,left.resulttype.def);
  486. paraloc1.done;
  487. paraloc2.done;
  488. end;
  489. procedure tcgvecnode.pass_2;
  490. var
  491. offsetdec,
  492. extraoffset : aint;
  493. t : tnode;
  494. href : treference;
  495. otl,ofl : tasmlabel;
  496. newsize : tcgsize;
  497. mulsize,
  498. bytemulsize,
  499. alignpow : aint;
  500. isjump : boolean;
  501. paraloc1,
  502. paraloc2 : tcgpara;
  503. subsetref : tsubsetreference;
  504. temp : longint;
  505. begin
  506. paraloc1.init;
  507. paraloc2.init;
  508. mulsize:=get_mul_size;
  509. if not is_packed_array(left.resulttype.def) then
  510. bytemulsize:=mulsize
  511. else
  512. bytemulsize:=mulsize div 8;
  513. newsize:=def_cgsize(resulttype.def);
  514. secondpass(left);
  515. if left.location.loc=LOC_CREFERENCE then
  516. location_reset(location,LOC_CREFERENCE,newsize)
  517. else
  518. location_reset(location,LOC_REFERENCE,newsize);
  519. { an ansistring needs to be dereferenced }
  520. if is_ansistring(left.resulttype.def) or
  521. is_widestring(left.resulttype.def) then
  522. begin
  523. if nf_callunique in flags then
  524. internalerror(200304236);
  525. {DM!!!!!}
  526. case left.location.loc of
  527. LOC_REGISTER,
  528. LOC_CREGISTER :
  529. location.reference.base:=left.location.register;
  530. LOC_CREFERENCE,
  531. LOC_REFERENCE :
  532. begin
  533. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  534. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.reference.base);
  535. end;
  536. else
  537. internalerror(2002032218);
  538. end;
  539. { check for a zero length string,
  540. we can use the ansistring routine here }
  541. if (cs_check_range in aktlocalswitches) then
  542. begin
  543. paramanager.getintparaloc(pocall_default,1,paraloc1);
  544. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  545. cg.a_param_reg(current_asmdata.CurrAsmList,OS_ADDR,location.reference.base,paraloc1);
  546. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  547. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  548. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_CHECKZERO');
  549. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  550. end;
  551. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  552. if is_ansistring(left.resulttype.def) then
  553. offsetdec:=1
  554. else
  555. offsetdec:=2;
  556. dec(location.reference.offset,offsetdec);
  557. end
  558. else if is_dynamic_array(left.resulttype.def) then
  559. begin
  560. case left.location.loc of
  561. LOC_REGISTER,
  562. LOC_CREGISTER :
  563. location.reference.base:=left.location.register;
  564. LOC_REFERENCE,
  565. LOC_CREFERENCE :
  566. begin
  567. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  568. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  569. left.location.reference,location.reference.base);
  570. end;
  571. else
  572. internalerror(2002032219);
  573. end;
  574. end
  575. else
  576. location_copy(location,left.location);
  577. { location must be memory }
  578. if not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  579. internalerror(200411013);
  580. { offset can only differ from 0 if arraydef }
  581. if (left.resulttype.def.deftype=arraydef) and
  582. not(is_dynamic_array(left.resulttype.def)) and
  583. (not(is_packed_array(left.resulttype.def)) or
  584. (mulsize mod 8 = 0)) then
  585. dec(location.reference.offset,bytemulsize*tarraydef(left.resulttype.def).lowrange);
  586. if right.nodetype=ordconstn then
  587. begin
  588. { offset can only differ from 0 if arraydef }
  589. case left.resulttype.def.deftype of
  590. arraydef :
  591. begin
  592. if not(is_open_array(left.resulttype.def)) and
  593. not(is_array_of_const(left.resulttype.def)) and
  594. not(is_dynamic_array(left.resulttype.def)) then
  595. begin
  596. if (tordconstnode(right).value>tarraydef(left.resulttype.def).highrange) or
  597. (tordconstnode(right).value<tarraydef(left.resulttype.def).lowrange) then
  598. begin
  599. { this should be caught in the resulttypepass! (JM) }
  600. if (cs_check_range in aktlocalswitches) then
  601. CGMessage(parser_e_range_check_error)
  602. else
  603. CGMessage(parser_w_range_check_error);
  604. end;
  605. end
  606. else
  607. begin
  608. { range checking for open and dynamic arrays needs
  609. runtime code }
  610. secondpass(right);
  611. if (cs_check_range in aktlocalswitches) then
  612. rangecheck_array;
  613. end;
  614. end;
  615. stringdef :
  616. begin
  617. if (cs_check_range in aktlocalswitches) then
  618. begin
  619. case tstringdef(left.resulttype.def).string_typ of
  620. { it's the same for ansi- and wide strings }
  621. st_widestring,
  622. st_ansistring:
  623. begin
  624. paramanager.getintparaloc(pocall_default,1,paraloc1);
  625. paramanager.getintparaloc(pocall_default,2,paraloc2);
  626. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  627. cg.a_param_const(current_asmdata.CurrAsmList,OS_INT,tordconstnode(right).value,paraloc2);
  628. href:=location.reference;
  629. dec(href.offset,sizeof(aint)-offsetdec);
  630. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  631. cg.a_param_ref(current_asmdata.CurrAsmList,OS_INT,href,paraloc1);
  632. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  633. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  634. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  635. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  636. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  637. end;
  638. st_shortstring:
  639. begin
  640. {!!!!!!!!!!!!!!!!!}
  641. { if this one is implemented making use of the high parameter for openshortstrings, update ncgutils.do_get_used_regvars() too (JM) }
  642. end;
  643. st_longstring:
  644. begin
  645. {!!!!!!!!!!!!!!!!!}
  646. end;
  647. end;
  648. end;
  649. end;
  650. end;
  651. if not(is_packed_array(left.resulttype.def)) or
  652. (mulsize mod 8 = 0) then
  653. inc(location.reference.offset,
  654. bytemulsize*tordconstnode(right).value)
  655. else
  656. begin
  657. subsetref.ref := location.reference;
  658. subsetref.ref.alignment := left.resulttype.def.alignment;
  659. if not ispowerof2(subsetref.ref.alignment,temp) then
  660. internalerror(2006081212);
  661. alignpow:=temp;
  662. inc(subsetref.ref.offset,((mulsize * (tordconstnode(right).value-tarraydef(left.resulttype.def).lowrange)) shr (3+alignpow)) shl alignpow);
  663. subsetref.bitindexreg := NR_NO;
  664. subsetref.startbit := (mulsize * (tordconstnode(right).value-tarraydef(left.resulttype.def).lowrange)) and ((1 shl (3+alignpow))-1);
  665. subsetref.bitlen := resulttype.def.packedbitsize;
  666. location.loc := LOC_SUBSETREF;
  667. location.sref := subsetref;
  668. end;
  669. end
  670. else
  671. { not nodetype=ordconstn }
  672. begin
  673. if (cs_opt_level1 in aktoptimizerswitches) and
  674. { if we do range checking, we don't }
  675. { need that fancy code (it would be }
  676. { buggy) }
  677. not(cs_check_range in aktlocalswitches) and
  678. (left.resulttype.def.deftype=arraydef) and
  679. not is_packed_array(left.resulttype.def) then
  680. begin
  681. extraoffset:=0;
  682. if (right.nodetype=addn) then
  683. begin
  684. if taddnode(right).right.nodetype=ordconstn then
  685. begin
  686. extraoffset:=tordconstnode(taddnode(right).right).value;
  687. t:=taddnode(right).left;
  688. { First pass processed this with the assumption }
  689. { that there was an add node which may require an }
  690. { extra register. Fake it or die with IE10 (JM) }
  691. t.registersint := taddnode(right).registersint;
  692. taddnode(right).left:=nil;
  693. right.free;
  694. right:=t;
  695. end
  696. else if taddnode(right).left.nodetype=ordconstn then
  697. begin
  698. extraoffset:=tordconstnode(taddnode(right).left).value;
  699. t:=taddnode(right).right;
  700. t.registersint := right.registersint;
  701. taddnode(right).right:=nil;
  702. right.free;
  703. right:=t;
  704. end;
  705. end
  706. else if (right.nodetype=subn) then
  707. begin
  708. if taddnode(right).right.nodetype=ordconstn then
  709. begin
  710. extraoffset:=-tordconstnode(taddnode(right).right).value;
  711. t:=taddnode(right).left;
  712. t.registersint := right.registersint;
  713. taddnode(right).left:=nil;
  714. right.free;
  715. right:=t;
  716. end
  717. { You also have to negate right.right in this case! I can't add an
  718. unaryminusn without causing a crash, so I've disabled it (JM)
  719. else if right.left.nodetype=ordconstn then
  720. begin
  721. extraoffset:=right.left.value;
  722. t:=right.right;
  723. t^.registersint := right.registersint;
  724. putnode(right);
  725. putnode(right.left);
  726. right:=t;
  727. end;}
  728. end;
  729. inc(location.reference.offset,
  730. mulsize*extraoffset);
  731. end;
  732. { calculate from left to right }
  733. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  734. internalerror(200304237);
  735. isjump:=(right.expectloc=LOC_JUMP);
  736. if isjump then
  737. begin
  738. otl:=current_procinfo.CurrTrueLabel;
  739. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  740. ofl:=current_procinfo.CurrFalseLabel;
  741. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  742. end;
  743. secondpass(right);
  744. { if mulsize = 1, we won't have to modify the index }
  745. location_force_reg(current_asmdata.CurrAsmList,right.location,OS_ADDR,not is_packed_array(left.resulttype.def) and (mulsize = 1) );
  746. if isjump then
  747. begin
  748. current_procinfo.CurrTrueLabel:=otl;
  749. current_procinfo.CurrFalseLabel:=ofl;
  750. end
  751. else if (right.location.loc = LOC_JUMP) then
  752. internalerror(2006010801);
  753. { only range check now, we can't range check loc_flags/loc_jump }
  754. if cs_check_range in aktlocalswitches then
  755. begin
  756. if left.resulttype.def.deftype=arraydef then
  757. rangecheck_array;
  758. end;
  759. { produce possible range check code: }
  760. if cs_check_range in aktlocalswitches then
  761. begin
  762. if left.resulttype.def.deftype=arraydef then
  763. begin
  764. { done defore (PM) }
  765. end
  766. else if (left.resulttype.def.deftype=stringdef) then
  767. begin
  768. case tstringdef(left.resulttype.def).string_typ of
  769. { it's the same for ansi- and wide strings }
  770. st_widestring,
  771. st_ansistring:
  772. begin
  773. paramanager.getintparaloc(pocall_default,1,paraloc1);
  774. paramanager.getintparaloc(pocall_default,2,paraloc2);
  775. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc2);
  776. cg.a_param_reg(current_asmdata.CurrAsmList,OS_INT,right.location.register,paraloc2);
  777. href:=location.reference;
  778. dec(href.offset,sizeof(aint)-offsetdec);
  779. //dec(href.offset,7);
  780. paramanager.allocparaloc(current_asmdata.CurrAsmList,paraloc1);
  781. cg.a_param_ref(current_asmdata.CurrAsmList,OS_INT,href,paraloc1);
  782. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc1);
  783. paramanager.freeparaloc(current_asmdata.CurrAsmList,paraloc2);
  784. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  785. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  786. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  787. end;
  788. st_shortstring:
  789. begin
  790. {!!!!!!!!!!!!!!!!!}
  791. end;
  792. st_longstring:
  793. begin
  794. {!!!!!!!!!!!!!!!!!}
  795. end;
  796. end;
  797. end;
  798. end;
  799. { insert the register and the multiplication factor in the
  800. reference }
  801. if not is_packed_array(left.resulttype.def) then
  802. update_reference_reg_mul(right.location.register,mulsize)
  803. else
  804. update_reference_reg_packed(right.location.register,mulsize);
  805. end;
  806. location.size:=newsize;
  807. paraloc1.done;
  808. paraloc2.done;
  809. end;
  810. begin
  811. cloadvmtaddrnode:=tcgloadvmtaddrnode;
  812. cloadparentfpnode:=tcgloadparentfpnode;
  813. caddrnode:=tcgaddrnode;
  814. cderefnode:=tcgderefnode;
  815. csubscriptnode:=tcgsubscriptnode;
  816. cwithnode:=tcgwithnode;
  817. cvecnode:=tcgvecnode;
  818. end.