ncgmem.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate assembler for memory related nodes which are
  5. the same for all (most?) processors
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. { This unit generate assembler for memory related nodes.
  20. }
  21. unit ncgmem;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. cginfo,cpuinfo,cpubase,
  26. node,nmem;
  27. type
  28. tcgloadvmtaddrnode = class(tloadvmtaddrnode)
  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 : longint;
  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:aword);virtual;
  55. procedure second_wideansistring;virtual;
  56. procedure second_dynamicarray;virtual;
  57. public
  58. procedure pass_2;override;
  59. end;
  60. implementation
  61. uses
  62. {$ifdef delphi}
  63. sysutils,
  64. {$else}
  65. strings,
  66. {$endif}
  67. {$ifdef GDB}
  68. gdb,
  69. {$endif GDB}
  70. globtype,systems,
  71. cutils,verbose,globals,
  72. symconst,symdef,symsym,symtable,defutil,paramgr,
  73. aasmbase,aasmtai,
  74. cgbase,pass_2,
  75. pass_1,nld,ncon,nadd,
  76. cgobj,tgobj,rgobj,ncgutil,symbase
  77. ;
  78. {*****************************************************************************
  79. TCGLOADNODE
  80. *****************************************************************************}
  81. procedure tcgloadvmtaddrnode.pass_2;
  82. var
  83. href : treference;
  84. begin
  85. location_reset(location,LOC_REGISTER,OS_ADDR);
  86. if (left.nodetype<>typen) then
  87. begin
  88. { left contains self, load vmt from self }
  89. secondpass(left);
  90. if is_object(left.resulttype.def) then
  91. begin
  92. case left.location.loc of
  93. LOC_CREFERENCE,
  94. LOC_REFERENCE:
  95. begin
  96. location_release(exprasmlist,left.location);
  97. reference_reset_base(href,rg.getaddressregister(exprasmlist),tobjectdef(left.resulttype.def).vmt_offset);
  98. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,href.base);
  99. end;
  100. else
  101. internalerror(200305056);
  102. end;
  103. end
  104. else
  105. begin
  106. case left.location.loc of
  107. LOC_REGISTER:
  108. begin
  109. if not rg.isaddressregister(left.location.register) then
  110. begin
  111. location_release(exprasmlist,left.location);
  112. reference_reset_base(href,rg.getaddressregister(exprasmlist),tobjectdef(left.resulttype.def).vmt_offset);
  113. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.register,href.base);
  114. end
  115. else
  116. reference_reset_base(href,left.location.register,tobjectdef(left.resulttype.def).vmt_offset);
  117. end;
  118. LOC_CREGISTER,
  119. LOC_CREFERENCE,
  120. LOC_REFERENCE:
  121. begin
  122. location_release(exprasmlist,left.location);
  123. reference_reset_base(href,rg.getaddressregister(exprasmlist),tobjectdef(left.resulttype.def).vmt_offset);
  124. cg.a_load_loc_reg(exprasmlist,OS_ADDR,left.location,href.base);
  125. end;
  126. else
  127. internalerror(200305057);
  128. end;
  129. end;
  130. reference_release(exprasmlist,href);
  131. location.register:=rg.getaddressregister(exprasmlist);
  132. cg.g_maybe_testself(exprasmlist,href.base);
  133. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,href,location.register);
  134. end
  135. else
  136. begin
  137. reference_reset_symbol(href,
  138. objectlibrary.newasmsymboldata(tobjectdef(tclassrefdef(resulttype.def).pointertype.def).vmt_mangledname),0);
  139. location.register:=rg.getaddressregister(exprasmlist);
  140. cg.a_loadaddr_ref_reg(exprasmlist,href,location.register);
  141. end;
  142. end;
  143. {*****************************************************************************
  144. TCGADDRNODE
  145. *****************************************************************************}
  146. procedure tcgaddrnode.pass_2;
  147. begin
  148. secondpass(left);
  149. { when loading procvar we do nothing with this node, so load the
  150. location of left }
  151. if nf_procvarload in flags then
  152. begin
  153. location_copy(location,left.location);
  154. exit;
  155. end;
  156. location_release(exprasmlist,left.location);
  157. location_reset(location,LOC_REGISTER,OS_ADDR);
  158. location.register:=rg.getaddressregister(exprasmlist);
  159. { @ on a procvar means returning an address to the procedure that
  160. is stored in it }
  161. if (m_tp_procvar in aktmodeswitches) and
  162. (left.nodetype=loadn) and
  163. (tloadnode(left).resulttype.def.deftype=procvardef) and
  164. assigned(tloadnode(left).symtableentry) and
  165. (tloadnode(left).symtableentry.typ=varsym) then
  166. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.reference,location.register)
  167. else
  168. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,location.register);
  169. end;
  170. {*****************************************************************************
  171. TCGDEREFNODE
  172. *****************************************************************************}
  173. procedure tcgderefnode.pass_2;
  174. begin
  175. secondpass(left);
  176. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  177. case left.location.loc of
  178. LOC_REGISTER:
  179. begin
  180. if not rg.isaddressregister(left.location.register) then
  181. begin
  182. location_release(exprasmlist,left.location);
  183. location.reference.base := rg.getaddressregister(exprasmlist);
  184. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.register,
  185. location.reference.base);
  186. end
  187. else
  188. location.reference.base := left.location.register;
  189. end;
  190. LOC_CREGISTER,
  191. LOC_CREFERENCE,
  192. LOC_REFERENCE:
  193. begin
  194. location_release(exprasmlist,left.location);
  195. location.reference.base:=rg.getaddressregister(exprasmlist);
  196. cg.a_load_loc_reg(exprasmlist,OS_ADDR,left.location,location.reference.base);
  197. end;
  198. end;
  199. if (cs_gdb_heaptrc in aktglobalswitches) and
  200. (cs_checkpointer in aktglobalswitches) and
  201. not(cs_compilesystem in aktmoduleswitches) and
  202. (not tpointerdef(left.resulttype.def).is_far) then
  203. begin
  204. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paramanager.getintparaloc(exprasmlist,1));
  205. paramanager.freeintparaloc(exprasmlist,1);
  206. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  207. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  208. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  209. end;
  210. end;
  211. {*****************************************************************************
  212. TCGSUBSCRIPTNODE
  213. *****************************************************************************}
  214. procedure tcgsubscriptnode.pass_2;
  215. begin
  216. secondpass(left);
  217. if codegenerror then
  218. exit;
  219. { classes and interfaces must be dereferenced implicit }
  220. if is_class_or_interface(left.resulttype.def) then
  221. begin
  222. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  223. case left.location.loc of
  224. LOC_CREGISTER,
  225. LOC_REGISTER:
  226. begin
  227. if not rg.isaddressregister(left.location.register) then
  228. begin
  229. location_release(exprasmlist,left.location);
  230. location.reference.base:=rg.getaddressregister(exprasmlist);
  231. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,
  232. left.location.register,location.reference.base);
  233. end
  234. else
  235. location.reference.base := left.location.register;
  236. end;
  237. LOC_CREFERENCE,
  238. LOC_REFERENCE:
  239. begin
  240. location_release(exprasmlist,left.location);
  241. location.reference.base:=rg.getaddressregister(exprasmlist);
  242. cg.a_load_loc_reg(exprasmlist,OS_ADDR,left.location,location.reference.base);
  243. end;
  244. end;
  245. { implicit deferencing }
  246. if (cs_gdb_heaptrc in aktglobalswitches) and
  247. (cs_checkpointer in aktglobalswitches) and
  248. not(cs_compilesystem in aktmoduleswitches) then
  249. begin
  250. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paramanager.getintparaloc(exprasmlist,1));
  251. paramanager.freeintparaloc(exprasmlist,1);
  252. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  253. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  254. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  255. end;
  256. end
  257. else if is_interfacecom(left.resulttype.def) then
  258. begin
  259. tg.GetTemp(exprasmlist,pointer_size,tt_interfacecom,location.reference);
  260. cg.a_load_loc_ref(exprasmlist,OS_ADDR,left.location,location.reference);
  261. { implicit deferencing also for interfaces }
  262. if (cs_gdb_heaptrc in aktglobalswitches) and
  263. (cs_checkpointer in aktglobalswitches) and
  264. not(cs_compilesystem in aktmoduleswitches) then
  265. begin
  266. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paramanager.getintparaloc(exprasmlist,1));
  267. paramanager.freeintparaloc(exprasmlist,1);
  268. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  269. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  270. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  271. end;
  272. end
  273. else
  274. location_copy(location,left.location);
  275. inc(location.reference.offset,vs.address);
  276. { also update the size of the location }
  277. location.size:=def_cgsize(resulttype.def);
  278. end;
  279. {*****************************************************************************
  280. TCGWITHNODE
  281. *****************************************************************************}
  282. procedure tcgwithnode.pass_2;
  283. {$ifdef GDB}
  284. const
  285. withlevel : longint = 0;
  286. var
  287. withstartlabel,withendlabel : tasmlabel;
  288. pp : pchar;
  289. mangled_length : longint;
  290. {$endif GDB}
  291. begin
  292. location_reset(location,LOC_VOID,OS_NO);
  293. {$ifdef GDB}
  294. if (cs_debuginfo in aktmoduleswitches) then
  295. begin
  296. { load reference }
  297. if (withrefnode.nodetype=derefn) and
  298. (tderefnode(withrefnode).left.nodetype=temprefn) then
  299. secondpass(withrefnode);
  300. inc(withlevel);
  301. objectlibrary.getaddrlabel(withstartlabel);
  302. objectlibrary.getaddrlabel(withendlabel);
  303. cg.a_label(exprasmlist,withstartlabel);
  304. withdebugList.concat(Tai_stabs.Create(strpnew(
  305. '"with'+tostr(withlevel)+':'+tostr(symtablestack.getnewtypecount)+
  306. '=*'+tstoreddef(left.resulttype.def).numberstring+'",'+
  307. tostr(N_LSYM)+',0,0,'+tostr(withrefnode.location.reference.offset))));
  308. mangled_length:=length(current_procinfo.procdef.mangledname);
  309. getmem(pp,mangled_length+50);
  310. strpcopy(pp,'192,0,0,'+withstartlabel.name);
  311. if (target_info.use_function_relative_addresses) then
  312. begin
  313. strpcopy(strend(pp),'-');
  314. strpcopy(strend(pp),current_procinfo.procdef.mangledname);
  315. end;
  316. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  317. end;
  318. {$endif GDB}
  319. if assigned(left) then
  320. secondpass(left);
  321. {$ifdef GDB}
  322. if (cs_debuginfo in aktmoduleswitches) then
  323. begin
  324. cg.a_label(exprasmlist,withendlabel);
  325. strpcopy(pp,'224,0,0,'+withendlabel.name);
  326. if (target_info.use_function_relative_addresses) then
  327. begin
  328. strpcopy(strend(pp),'-');
  329. strpcopy(strend(pp),current_procinfo.procdef.mangledname);
  330. end;
  331. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  332. freemem(pp,mangled_length+50);
  333. dec(withlevel);
  334. end;
  335. {$endif GDB}
  336. end;
  337. {*****************************************************************************
  338. TCGVECNODE
  339. *****************************************************************************}
  340. function tcgvecnode.get_mul_size : longint;
  341. begin
  342. if nf_memindex in flags then
  343. get_mul_size:=1
  344. else
  345. begin
  346. if (left.resulttype.def.deftype=arraydef) then
  347. get_mul_size:=tarraydef(left.resulttype.def).elesize
  348. else
  349. get_mul_size:=resulttype.def.size;
  350. end
  351. end;
  352. procedure tcgvecnode.update_reference_reg_mul(reg:tregister;l:aword);
  353. var
  354. hreg: tregister;
  355. begin
  356. if location.reference.base=NR_NO then
  357. begin
  358. cg.a_op_const_reg(exprasmlist,OP_IMUL,OS_ADDR,l,reg);
  359. location.reference.base:=reg;
  360. end
  361. else if location.reference.index=NR_NO then
  362. begin
  363. cg.a_op_const_reg(exprasmlist,OP_IMUL,OS_ADDR,l,reg);
  364. location.reference.index:=reg;
  365. end
  366. else
  367. begin
  368. rg.ungetreference(exprasmlist,location.reference);
  369. hreg := rg.getaddressregister(exprasmlist);
  370. cg.a_loadaddr_ref_reg(exprasmlist,location.reference,hreg);
  371. reference_reset_base(location.reference,hreg,0);
  372. { insert new index register }
  373. cg.a_op_const_reg(exprasmlist,OP_IMUL,OS_ADDR,l,reg);
  374. location.reference.index:=reg;
  375. end;
  376. end;
  377. procedure tcgvecnode.second_wideansistring;
  378. begin
  379. end;
  380. procedure tcgvecnode.second_dynamicarray;
  381. begin
  382. end;
  383. procedure tcgvecnode.rangecheck_array;
  384. var
  385. freereg : boolean;
  386. hightree : tnode;
  387. poslabel,
  388. neglabel : tasmlabel;
  389. hreg : tregister;
  390. begin
  391. if is_open_array(left.resulttype.def) or
  392. is_array_of_const(left.resulttype.def) then
  393. begin
  394. { cdecl functions don't have high() so we can not check the range }
  395. if not(current_procinfo.procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  396. begin
  397. { Get high value }
  398. hightree:=load_high_value_node(tvarsym(tloadnode(left).symtableentry));
  399. { it must be available }
  400. if not assigned(hightree) then
  401. internalerror(200212201);
  402. firstpass(hightree);
  403. secondpass(hightree);
  404. { generate compares }
  405. freereg:=false;
  406. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  407. hreg:=right.location.register
  408. else
  409. begin
  410. hreg:=rg.getregisterint(exprasmlist,OS_INT);
  411. freereg:=true;
  412. cg.a_load_loc_reg(exprasmlist,OS_INT,right.location,hreg);
  413. end;
  414. objectlibrary.getlabel(neglabel);
  415. objectlibrary.getlabel(poslabel);
  416. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_LT,0,hreg,poslabel);
  417. cg.a_cmp_loc_reg_label(exprasmlist,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  418. if freereg then
  419. rg.ungetregisterint(exprasmlist,hreg);
  420. cg.a_label(exprasmlist,poslabel);
  421. cg.a_call_name(exprasmlist,'FPC_RANGEERROR');
  422. cg.a_label(exprasmlist,neglabel);
  423. { release hightree }
  424. location_release(exprasmlist,hightree.location);
  425. hightree.free;
  426. end;
  427. end
  428. else
  429. if is_dynamic_array(left.resulttype.def) then
  430. begin
  431. cg.a_param_loc(exprasmlist,right.location,paramanager.getintparaloc(exprasmlist,2));
  432. cg.a_param_loc(exprasmlist,left.location,paramanager.getintparaloc(exprasmlist,1));
  433. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  434. cg.a_call_name(exprasmlist,'FPC_DYNARRAY_RANGECHECK');
  435. paramanager.freeintparaloc(exprasmlist,2);
  436. paramanager.freeintparaloc(exprasmlist,1);
  437. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  438. end
  439. else
  440. cg.g_rangecheck(exprasmlist,right.location,right.resulttype.def,left.resulttype.def);
  441. end;
  442. procedure tcgvecnode.pass_2;
  443. var
  444. extraoffset : longint;
  445. t : tnode;
  446. href : treference;
  447. otl,ofl : tasmlabel;
  448. newsize : tcgsize;
  449. mulsize: longint;
  450. isjump : boolean;
  451. begin
  452. mulsize := get_mul_size;
  453. newsize:=def_cgsize(resulttype.def);
  454. secondpass(left);
  455. if left.location.loc=LOC_CREFERENCE then
  456. location_reset(location,LOC_CREFERENCE,newsize)
  457. else
  458. location_reset(location,LOC_REFERENCE,newsize);
  459. { an ansistring needs to be dereferenced }
  460. if is_ansistring(left.resulttype.def) or
  461. is_widestring(left.resulttype.def) then
  462. begin
  463. if nf_callunique in flags then
  464. internalerror(200304236);
  465. case left.location.loc of
  466. LOC_REGISTER,
  467. LOC_CREGISTER :
  468. location.reference.base:=left.location.register;
  469. LOC_CREFERENCE,
  470. LOC_REFERENCE :
  471. begin
  472. location_release(exprasmlist,left.location);
  473. location.reference.base:=rg.getaddressregister(exprasmlist);
  474. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.reference,location.reference.base);
  475. end;
  476. else
  477. internalerror(2002032218);
  478. end;
  479. { check for a zero length string,
  480. we can use the ansistring routine here }
  481. if (cs_check_range in aktlocalswitches) then
  482. begin
  483. cg.a_param_reg(exprasmlist,OS_ADDR,location.reference.base,paramanager.getintparaloc(exprasmlist,1));
  484. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  485. cg.a_call_name(exprasmlist,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_CHECKZERO');
  486. paramanager.freeintparaloc(exprasmlist,1);
  487. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  488. end;
  489. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  490. if is_ansistring(left.resulttype.def) then
  491. dec(location.reference.offset)
  492. else
  493. dec(location.reference.offset,2);
  494. end
  495. else if is_dynamic_array(left.resulttype.def) then
  496. begin
  497. case left.location.loc of
  498. LOC_REGISTER,
  499. LOC_CREGISTER :
  500. location.reference.base:=left.location.register;
  501. LOC_REFERENCE,
  502. LOC_CREFERENCE :
  503. begin
  504. location_release(exprasmlist,left.location);
  505. location.reference.base:=rg.getaddressregister(exprasmlist);
  506. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,
  507. left.location.reference,location.reference.base);
  508. end;
  509. else
  510. internalerror(2002032219);
  511. end;
  512. end
  513. else
  514. location_copy(location,left.location);
  515. { offset can only differ from 0 if arraydef }
  516. if (left.resulttype.def.deftype=arraydef) and
  517. not(is_dynamic_array(left.resulttype.def)) then
  518. dec(location.reference.offset,mulsize*tarraydef(left.resulttype.def).lowrange);
  519. if right.nodetype=ordconstn then
  520. begin
  521. { offset can only differ from 0 if arraydef }
  522. case left.resulttype.def.deftype of
  523. arraydef :
  524. begin
  525. if not(is_open_array(left.resulttype.def)) and
  526. not(is_array_of_const(left.resulttype.def)) and
  527. not(is_dynamic_array(left.resulttype.def)) then
  528. begin
  529. if (tordconstnode(right).value>tarraydef(left.resulttype.def).highrange) or
  530. (tordconstnode(right).value<tarraydef(left.resulttype.def).lowrange) then
  531. begin
  532. { this should be caught in the resulttypepass! (JM) }
  533. if (cs_check_range in aktlocalswitches) then
  534. CGMessage(parser_e_range_check_error)
  535. else
  536. CGMessage(parser_w_range_check_error);
  537. end;
  538. end
  539. else
  540. begin
  541. { range checking for open and dynamic arrays needs
  542. runtime code }
  543. secondpass(right);
  544. if (cs_check_range in aktlocalswitches) then
  545. rangecheck_array;
  546. end;
  547. end;
  548. stringdef :
  549. begin
  550. if (cs_check_range in aktlocalswitches) then
  551. begin
  552. case tstringdef(left.resulttype.def).string_typ of
  553. { it's the same for ansi- and wide strings }
  554. st_widestring,
  555. st_ansistring:
  556. begin
  557. cg.a_param_const(exprasmlist,OS_INT,tordconstnode(right).value,paramanager.getintparaloc(exprasmlist,2));
  558. href:=location.reference;
  559. dec(href.offset,7);
  560. cg.a_param_ref(exprasmlist,OS_INT,href,paramanager.getintparaloc(exprasmlist,1));
  561. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  562. cg.a_call_name(exprasmlist,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  563. paramanager.freeintparaloc(exprasmlist,2);
  564. paramanager.freeintparaloc(exprasmlist,1);
  565. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  566. end;
  567. st_shortstring:
  568. begin
  569. {!!!!!!!!!!!!!!!!!}
  570. end;
  571. st_longstring:
  572. begin
  573. {!!!!!!!!!!!!!!!!!}
  574. end;
  575. end;
  576. end;
  577. end;
  578. end;
  579. inc(location.reference.offset,
  580. mulsize*tordconstnode(right).value);
  581. end
  582. else
  583. { not nodetype=ordconstn }
  584. begin
  585. if (cs_regvars in aktglobalswitches) and
  586. { if we do range checking, we don't }
  587. { need that fancy code (it would be }
  588. { buggy) }
  589. not(cs_check_range in aktlocalswitches) and
  590. (left.resulttype.def.deftype=arraydef) then
  591. begin
  592. extraoffset:=0;
  593. if (right.nodetype=addn) then
  594. begin
  595. if taddnode(right).right.nodetype=ordconstn then
  596. begin
  597. extraoffset:=tordconstnode(taddnode(right).right).value;
  598. t:=taddnode(right).left;
  599. { First pass processed this with the assumption }
  600. { that there was an add node which may require an }
  601. { extra register. Fake it or die with IE10 (JM) }
  602. t.registers32 := taddnode(right).registers32;
  603. taddnode(right).left:=nil;
  604. right.free;
  605. right:=t;
  606. end
  607. else if taddnode(right).left.nodetype=ordconstn then
  608. begin
  609. extraoffset:=tordconstnode(taddnode(right).left).value;
  610. t:=taddnode(right).right;
  611. t.registers32 := right.registers32;
  612. taddnode(right).right:=nil;
  613. right.free;
  614. right:=t;
  615. end;
  616. end
  617. else if (right.nodetype=subn) then
  618. begin
  619. if taddnode(right).right.nodetype=ordconstn then
  620. begin
  621. extraoffset:=-tordconstnode(taddnode(right).right).value;
  622. t:=taddnode(right).left;
  623. t.registers32 := right.registers32;
  624. taddnode(right).left:=nil;
  625. right.free;
  626. right:=t;
  627. end
  628. { You also have to negate right.right in this case! I can't add an
  629. unaryminusn without causing a crash, so I've disabled it (JM)
  630. else if right.left.nodetype=ordconstn then
  631. begin
  632. extraoffset:=right.left.value;
  633. t:=right.right;
  634. t^.registers32 := right.registers32;
  635. putnode(right);
  636. putnode(right.left);
  637. right:=t;
  638. end;}
  639. end;
  640. inc(location.reference.offset,
  641. mulsize*extraoffset);
  642. end;
  643. { calculate from left to right }
  644. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  645. internalerror(200304237);
  646. isjump:=(right.location.loc=LOC_JUMP);
  647. if isjump then
  648. begin
  649. otl:=truelabel;
  650. objectlibrary.getlabel(truelabel);
  651. ofl:=falselabel;
  652. objectlibrary.getlabel(falselabel);
  653. end;
  654. secondpass(right);
  655. if cs_check_range in aktlocalswitches then
  656. begin
  657. if left.resulttype.def.deftype=arraydef then
  658. rangecheck_array;
  659. end;
  660. { if mulsize = 1, we won't have to modify the index }
  661. location_force_reg(exprasmlist,right.location,OS_32,mulsize = 1);
  662. if isjump then
  663. begin
  664. truelabel:=otl;
  665. falselabel:=ofl;
  666. end;
  667. { produce possible range check code: }
  668. if cs_check_range in aktlocalswitches then
  669. begin
  670. if left.resulttype.def.deftype=arraydef then
  671. begin
  672. { done defore (PM) }
  673. end
  674. else if (left.resulttype.def.deftype=stringdef) then
  675. begin
  676. case tstringdef(left.resulttype.def).string_typ of
  677. { it's the same for ansi- and wide strings }
  678. st_widestring,
  679. st_ansistring:
  680. begin
  681. cg.a_param_reg(exprasmlist,OS_INT,right.location.register,paramanager.getintparaloc(exprasmlist,2));
  682. href:=location.reference;
  683. dec(href.offset,7);
  684. cg.a_param_ref(exprasmlist,OS_INT,href,paramanager.getintparaloc(exprasmlist,1));
  685. rg.allocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  686. cg.a_call_name(exprasmlist,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  687. paramanager.freeintparaloc(exprasmlist,2);
  688. paramanager.freeintparaloc(exprasmlist,1);
  689. rg.deallocexplicitregistersint(exprasmlist,VOLATILE_INTREGISTERS);
  690. end;
  691. st_shortstring:
  692. begin
  693. {!!!!!!!!!!!!!!!!!}
  694. end;
  695. st_longstring:
  696. begin
  697. {!!!!!!!!!!!!!!!!!}
  698. end;
  699. end;
  700. end;
  701. end;
  702. { insert the register and the multiplication factor in the
  703. reference }
  704. update_reference_reg_mul(right.location.register,mulsize);
  705. end;
  706. location.size:=newsize;
  707. end;
  708. begin
  709. cloadvmtaddrnode:=tcgloadvmtaddrnode;
  710. caddrnode:=tcgaddrnode;
  711. cderefnode:=tcgderefnode;
  712. csubscriptnode:=tcgsubscriptnode;
  713. cwithnode:=tcgwithnode;
  714. cvecnode:=tcgvecnode;
  715. end.
  716. {
  717. $Log$
  718. Revision 1.70 2003-09-03 15:55:00 peter
  719. * NEWRA branch merged
  720. Revision 1.69.2.1 2003/08/29 17:28:59 peter
  721. * next batch of updates
  722. Revision 1.69 2003/08/10 17:25:23 peter
  723. * fixed some reported bugs
  724. Revision 1.68 2003/08/09 18:56:54 daniel
  725. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  726. allocator
  727. * Some preventive changes to i386 spillinh code
  728. Revision 1.67 2003/07/23 11:01:14 jonas
  729. * several rg.allocexplicitregistersint/rg.deallocexplicitregistersint
  730. pairs round calls to helpers
  731. Revision 1.66 2003/07/06 21:50:33 jonas
  732. * fixed ppc compilation problems and changed VOLATILE_REGISTERS for x86
  733. so that it doesn't include ebp and esp anymore
  734. Revision 1.65 2003/07/06 15:31:20 daniel
  735. * Fixed register allocator. *Lots* of fixes.
  736. Revision 1.64 2003/06/17 19:24:08 jonas
  737. * fixed conversion of fpc_*str_unique to compilerproc
  738. Revision 1.63 2003/06/17 16:34:44 jonas
  739. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  740. * renamed all_intregisters to volatile_intregisters and made it
  741. processor dependent
  742. Revision 1.62 2003/06/13 21:19:30 peter
  743. * current_procdef removed, use current_procinfo.procdef instead
  744. Revision 1.61 2003/06/09 16:45:41 jonas
  745. * fixed update_reference_reg_mul() so that it won't modify CREGISTERs
  746. in a reference
  747. * cache value of get_mul_size()
  748. * if get_mul_size = 1, the index can be a CREGISTER since it won't be
  749. modified
  750. Revision 1.60 2003/06/07 18:57:04 jonas
  751. + added freeintparaloc
  752. * ppc get/freeintparaloc now check whether the parameter regs are
  753. properly allocated/deallocated (and get an extra list para)
  754. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  755. * fixed lot of missing pi_do_call's
  756. Revision 1.59 2003/06/03 21:11:09 peter
  757. * cg.a_load_* get a from and to size specifier
  758. * makeregsize only accepts newregister
  759. * i386 uses generic tcgnotnode,tcgunaryminus
  760. Revision 1.58 2003/06/03 13:01:59 daniel
  761. * Register allocator finished
  762. Revision 1.57 2003/06/02 22:35:45 florian
  763. * better handling of CREGISTER in subscript nodes
  764. Revision 1.56 2003/06/01 21:38:06 peter
  765. * getregisterfpu size parameter added
  766. * op_const_reg size parameter added
  767. * sparc updates
  768. Revision 1.55 2003/05/30 23:49:18 jonas
  769. * a_load_loc_reg now has an extra size parameter for the destination
  770. register (properly fixes what I worked around in revision 1.106 of
  771. ncgutil.pas)
  772. Revision 1.54 2003/05/15 16:10:37 florian
  773. * fixed getintparaloc call for ansi- and widestring range checking
  774. Revision 1.53 2003/05/11 21:37:03 peter
  775. * moved implicit exception frame from ncgutil to psub
  776. * constructor/destructor helpers moved from cobj/ncgutil to psub
  777. Revision 1.52 2003/05/11 14:45:12 peter
  778. * tloadnode does not support objectsymtable,withsymtable anymore
  779. * withnode cleanup
  780. * direct with rewritten to use temprefnode
  781. Revision 1.51 2003/05/09 17:47:02 peter
  782. * self moved to hidden parameter
  783. * removed hdisposen,hnewn,selfn
  784. Revision 1.50 2003/05/07 09:16:23 mazen
  785. - non used units removed from uses clause
  786. Revision 1.49 2003/04/27 11:21:33 peter
  787. * aktprocdef renamed to current_procinfo.procdef
  788. * procinfo renamed to current_procinfo
  789. * procinfo will now be stored in current_module so it can be
  790. cleaned up properly
  791. * gen_main_procsym changed to create_main_proc and release_main_proc
  792. to also generate a tprocinfo structure
  793. * fixed unit implicit initfinal
  794. Revision 1.48 2003/04/22 23:50:22 peter
  795. * firstpass uses expectloc
  796. * checks if there are differences between the expectloc and
  797. location.loc from secondpass in EXTDEBUG
  798. Revision 1.47 2003/04/22 13:47:08 peter
  799. * fixed C style array of const
  800. * fixed C array passing
  801. * fixed left to right with high parameters
  802. Revision 1.46 2003/04/22 10:09:35 daniel
  803. + Implemented the actual register allocator
  804. + Scratch registers unavailable when new register allocator used
  805. + maybe_save/maybe_restore unavailable when new register allocator used
  806. Revision 1.45 2003/04/06 21:11:23 olle
  807. * changed newasmsymbol to newasmsymboldata for data symbols
  808. Revision 1.44 2003/03/28 19:16:56 peter
  809. * generic constructor working for i386
  810. * remove fixed self register
  811. * esi added as address register for i386
  812. Revision 1.43 2003/03/12 22:43:38 jonas
  813. * more powerpc and generic fixes related to the new register allocator
  814. Revision 1.42 2003/02/19 22:00:14 daniel
  815. * Code generator converted to new register notation
  816. - Horribily outdated todo.txt removed
  817. Revision 1.41 2003/01/30 21:46:57 peter
  818. * self fixes for static methods (merged)
  819. Revision 1.40 2003/01/08 18:43:56 daniel
  820. * Tregister changed into a record
  821. Revision 1.39 2002/12/20 18:13:19 peter
  822. * no rangecheck for openarrays with cdecl
  823. Revision 1.38 2002/12/17 22:19:33 peter
  824. * fixed pushing of records>8 bytes with stdcall
  825. * simplified hightree loading
  826. Revision 1.37 2002/12/08 13:39:03 carl
  827. + some documentation added
  828. Revision 1.36 2002/12/07 14:14:19 carl
  829. * bugfix on invalid typecast
  830. Revision 1.35 2002/11/25 17:43:18 peter
  831. * splitted defbase in defutil,symutil,defcmp
  832. * merged isconvertable and is_equal into compare_defs(_ext)
  833. * made operator search faster by walking the list only once
  834. Revision 1.34 2002/11/24 18:19:20 carl
  835. + checkpointer for interfaces also
  836. Revision 1.33 2002/11/23 22:50:06 carl
  837. * some small speed optimizations
  838. + added several new warnings/hints
  839. Revision 1.32 2002/11/15 01:58:51 peter
  840. * merged changes from 1.0.7 up to 04-11
  841. - -V option for generating bug report tracing
  842. - more tracing for option parsing
  843. - errors for cdecl and high()
  844. - win32 import stabs
  845. - win32 records<=8 are returned in eax:edx (turned off by default)
  846. - heaptrc update
  847. - more info for temp management in .s file with EXTDEBUG
  848. Revision 1.31 2002/10/09 20:24:47 florian
  849. + range checking for dyn. arrays
  850. Revision 1.30 2002/10/07 21:30:45 peter
  851. * rangecheck for open arrays added
  852. Revision 1.29 2002/10/05 12:43:25 carl
  853. * fixes for Delphi 6 compilation
  854. (warning : Some features do not work under Delphi)
  855. Revision 1.28 2002/09/17 18:54:02 jonas
  856. * a_load_reg_reg() now has two size parameters: source and dest. This
  857. allows some optimizations on architectures that don't encode the
  858. register size in the register name.
  859. Revision 1.27 2002/09/07 15:25:03 peter
  860. * old logs removed and tabs fixed
  861. Revision 1.26 2002/09/01 18:46:01 peter
  862. * fixed generic tcgvecnode
  863. * move code that updates a reference with index register and multiplier
  864. to separate method so it can be overriden for scaled indexing
  865. * i386 uses generic tcgvecnode
  866. Revision 1.25 2002/08/23 16:14:48 peter
  867. * tempgen cleanup
  868. * tt_noreuse temp type added that will be used in genentrycode
  869. Revision 1.24 2002/08/15 08:13:54 carl
  870. - a_load_sym_ofs_reg removed
  871. * loadvmt now calls loadaddr_ref_reg instead
  872. Revision 1.23 2002/08/11 14:32:26 peter
  873. * renamed current_library to objectlibrary
  874. Revision 1.22 2002/08/11 13:24:12 peter
  875. * saving of asmsymbols in ppu supported
  876. * asmsymbollist global is removed and moved into a new class
  877. tasmlibrarydata that will hold the info of a .a file which
  878. corresponds with a single module. Added librarydata to tmodule
  879. to keep the library info stored for the module. In the future the
  880. objectfiles will also be stored to the tasmlibrarydata class
  881. * all getlabel/newasmsymbol and friends are moved to the new class
  882. Revision 1.21 2002/08/11 11:36:57 jonas
  883. * always first try to use base and only then index
  884. Revision 1.20 2002/08/11 06:14:40 florian
  885. * fixed powerpc compilation problems
  886. Revision 1.19 2002/08/10 14:46:29 carl
  887. + moved target_cpu_string to cpuinfo
  888. * renamed asmmode enum.
  889. * assembler reader has now less ifdef's
  890. * move from nppcmem.pas -> ncgmem.pas vec. node.
  891. Revision 1.18 2002/07/28 21:34:31 florian
  892. * more powerpc fixes
  893. + dummy tcgvecnode
  894. Revision 1.17 2002/07/11 14:41:28 florian
  895. * start of the new generic parameter handling
  896. Revision 1.16 2002/07/07 09:52:32 florian
  897. * powerpc target fixed, very simple units can be compiled
  898. * some basic stuff for better callparanode handling, far from being finished
  899. Revision 1.15 2002/07/01 18:46:23 peter
  900. * internal linker
  901. * reorganized aasm layer
  902. Revision 1.14 2002/07/01 16:23:53 peter
  903. * cg64 patch
  904. * basics for currency
  905. * asnode updates for class and interface (not finished)
  906. Revision 1.13 2002/05/20 13:30:40 carl
  907. * bugfix of hdisponen (base must be set, not index)
  908. * more portability fixes
  909. Revision 1.12 2002/05/18 13:34:09 peter
  910. * readded missing revisions
  911. Revision 1.11 2002/05/16 19:46:37 carl
  912. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  913. + try to fix temp allocation (still in ifdef)
  914. + generic constructor calls
  915. + start of tassembler / tmodulebase class cleanup
  916. Revision 1.9 2002/05/12 16:53:07 peter
  917. * moved entry and exitcode to ncgutil and cgobj
  918. * foreach gets extra argument for passing local data to the
  919. iterator function
  920. * -CR checks also class typecasts at runtime by changing them
  921. into as
  922. * fixed compiler to cycle with the -CR option
  923. * fixed stabs with elf writer, finally the global variables can
  924. be watched
  925. * removed a lot of routines from cga unit and replaced them by
  926. calls to cgobj
  927. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  928. u32bit then the other is typecasted also to u32bit without giving
  929. a rangecheck warning/error.
  930. * fixed pascal calling method with reversing also the high tree in
  931. the parast, detected by tcalcst3 test
  932. Revision 1.8 2002/04/20 21:32:23 carl
  933. + generic FPC_CHECKPOINTER
  934. + first parameter offset in stack now portable
  935. * rename some constants
  936. + move some cpu stuff to other units
  937. - remove unused constents
  938. * fix stacksize for some targets
  939. * fix generic size problems which depend now on EXTEND_SIZE constant
  940. Revision 1.7 2002/04/15 18:58:47 carl
  941. + target_info.size_of_pointer -> pointer_Size
  942. Revision 1.6 2002/04/04 19:05:57 peter
  943. * removed unused units
  944. * use tlocation.size in cg.a_*loc*() routines
  945. }