ncgmem.pas 45 KB

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