ncgmem.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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,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,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,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,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(1));
  227. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  228. end;
  229. end;
  230. {*****************************************************************************
  231. TCGSUBSCRIPTNODE
  232. *****************************************************************************}
  233. procedure tcgsubscriptnode.pass_2;
  234. begin
  235. secondpass(left);
  236. if codegenerror then
  237. exit;
  238. { classes and interfaces must be dereferenced implicit }
  239. if is_class_or_interface(left.resulttype.def) then
  240. begin
  241. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  242. case left.location.loc of
  243. LOC_REGISTER:
  244. begin
  245. if not rg.isaddressregister(left.location.register) then
  246. begin
  247. location_release(exprasmlist,left.location);
  248. location.reference.base:=rg.getaddressregister(exprasmlist);
  249. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,
  250. left.location.register,location.reference.base);
  251. end
  252. else
  253. location.reference.base := left.location.register;
  254. end;
  255. LOC_CREGISTER,
  256. LOC_CREFERENCE,
  257. LOC_REFERENCE:
  258. begin
  259. location_release(exprasmlist,left.location);
  260. location.reference.base:=rg.getaddressregister(exprasmlist);
  261. cg.a_load_loc_reg(exprasmlist,left.location,location.reference.base);
  262. end;
  263. end;
  264. { implicit deferencing }
  265. if (cs_gdb_heaptrc in aktglobalswitches) and
  266. (cs_checkpointer in aktglobalswitches) and
  267. not(cs_compilesystem in aktmoduleswitches) then
  268. begin
  269. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paramanager.getintparaloc(1));
  270. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  271. end;
  272. end
  273. else if is_interfacecom(left.resulttype.def) then
  274. begin
  275. tg.GetTemp(exprasmlist,pointer_size,tt_interfacecom,location.reference);
  276. cg.a_load_loc_ref(exprasmlist,left.location,location.reference);
  277. { implicit deferencing also for interfaces }
  278. if (cs_gdb_heaptrc in aktglobalswitches) and
  279. (cs_checkpointer in aktglobalswitches) and
  280. not(cs_compilesystem in aktmoduleswitches) then
  281. begin
  282. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paramanager.getintparaloc(1));
  283. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  284. end;
  285. end
  286. else
  287. location_copy(location,left.location);
  288. inc(location.reference.offset,vs.address);
  289. { also update the size of the location }
  290. location.size:=def_cgsize(resulttype.def);
  291. end;
  292. {*****************************************************************************
  293. TCGWITHNODE
  294. *****************************************************************************}
  295. procedure tcgwithnode.pass_2;
  296. {$ifdef GDB}
  297. const
  298. withlevel : longint = 0;
  299. var
  300. withstartlabel,withendlabel : tasmlabel;
  301. pp : pchar;
  302. mangled_length : longint;
  303. {$endif GDB}
  304. begin
  305. location_reset(location,LOC_VOID,OS_NO);
  306. {$ifdef GDB}
  307. if (cs_debuginfo in aktmoduleswitches) then
  308. begin
  309. { load reference }
  310. if (withrefnode.nodetype=derefn) and
  311. (tderefnode(withrefnode).left.nodetype=temprefn) then
  312. secondpass(withrefnode);
  313. inc(withlevel);
  314. objectlibrary.getaddrlabel(withstartlabel);
  315. objectlibrary.getaddrlabel(withendlabel);
  316. cg.a_label(exprasmlist,withstartlabel);
  317. withdebugList.concat(Tai_stabs.Create(strpnew(
  318. '"with'+tostr(withlevel)+':'+tostr(symtablestack.getnewtypecount)+
  319. '=*'+tstoreddef(left.resulttype.def).numberstring+'",'+
  320. tostr(N_LSYM)+',0,0,'+tostr(withrefnode.location.reference.offset))));
  321. mangled_length:=length(current_procdef.mangledname);
  322. getmem(pp,mangled_length+50);
  323. strpcopy(pp,'192,0,0,'+withstartlabel.name);
  324. if (target_info.use_function_relative_addresses) then
  325. begin
  326. strpcopy(strend(pp),'-');
  327. strpcopy(strend(pp),current_procdef.mangledname);
  328. end;
  329. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  330. end;
  331. {$endif GDB}
  332. if assigned(left) then
  333. secondpass(left);
  334. {$ifdef GDB}
  335. if (cs_debuginfo in aktmoduleswitches) then
  336. begin
  337. cg.a_label(exprasmlist,withendlabel);
  338. strpcopy(pp,'224,0,0,'+withendlabel.name);
  339. if (target_info.use_function_relative_addresses) then
  340. begin
  341. strpcopy(strend(pp),'-');
  342. strpcopy(strend(pp),current_procdef.mangledname);
  343. end;
  344. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  345. freemem(pp,mangled_length+50);
  346. dec(withlevel);
  347. end;
  348. {$endif GDB}
  349. end;
  350. {*****************************************************************************
  351. TCGVECNODE
  352. *****************************************************************************}
  353. function tcgvecnode.get_mul_size : longint;
  354. begin
  355. if nf_memindex in flags then
  356. get_mul_size:=1
  357. else
  358. begin
  359. if (left.resulttype.def.deftype=arraydef) then
  360. get_mul_size:=tarraydef(left.resulttype.def).elesize
  361. else
  362. get_mul_size:=resulttype.def.size;
  363. end
  364. end;
  365. procedure tcgvecnode.update_reference_reg_mul(reg:tregister;l:aword);
  366. begin
  367. if location.reference.base.number=NR_NO then
  368. begin
  369. cg.a_op_const_reg(exprasmlist,OP_IMUL,l,reg);
  370. location.reference.base:=reg;
  371. end
  372. else if location.reference.index.number=NR_NO then
  373. begin
  374. cg.a_op_const_reg(exprasmlist,OP_IMUL,l,reg);
  375. location.reference.index:=reg;
  376. end
  377. else
  378. begin
  379. cg.a_loadaddr_ref_reg(exprasmlist,location.reference,location.reference.index);
  380. rg.ungetregisterint(exprasmlist,location.reference.base);
  381. reference_reset_base(location.reference,location.reference.index,0);
  382. { insert new index register }
  383. cg.a_op_const_reg(exprasmlist,OP_IMUL,l,reg);
  384. location.reference.index:=reg;
  385. end;
  386. end;
  387. procedure tcgvecnode.second_wideansistring;
  388. begin
  389. end;
  390. procedure tcgvecnode.second_dynamicarray;
  391. begin
  392. end;
  393. procedure tcgvecnode.rangecheck_array;
  394. var
  395. freereg : boolean;
  396. hightree : tnode;
  397. poslabel,
  398. neglabel : tasmlabel;
  399. hreg : tregister;
  400. pushed : tpushedsavedint;
  401. begin
  402. if is_open_array(left.resulttype.def) or
  403. is_array_of_const(left.resulttype.def) then
  404. begin
  405. { cdecl functions don't have high() so we can not check the range }
  406. if not(current_procdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  407. begin
  408. { Get high value }
  409. hightree:=load_high_value(tvarsym(tloadnode(left).symtableentry));
  410. { it must be available }
  411. if not assigned(hightree) then
  412. internalerror(200212201);
  413. firstpass(hightree);
  414. secondpass(hightree);
  415. { generate compares }
  416. freereg:=false;
  417. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  418. hreg:=right.location.register
  419. else
  420. begin
  421. {$ifdef newra}
  422. hreg:=rg.getregisterint(exprasmlist,OS_INT);
  423. {$else}
  424. hreg := cg.get_scratch_reg_int(exprasmlist,OS_INT);
  425. {$endif}
  426. freereg:=true;
  427. cg.a_load_loc_reg(exprasmlist,right.location,hreg);
  428. end;
  429. objectlibrary.getlabel(neglabel);
  430. objectlibrary.getlabel(poslabel);
  431. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_LT,0,hreg,poslabel);
  432. cg.a_cmp_loc_reg_label(exprasmlist,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  433. {$ifdef newra}
  434. if freereg then
  435. rg.ungetregisterint(exprasmlist,hreg);
  436. {$else}
  437. if freereg then
  438. cg.free_scratch_reg(exprasmlist,hreg);
  439. {$endif}
  440. cg.a_label(exprasmlist,poslabel);
  441. cg.a_call_name(exprasmlist,'FPC_RANGEERROR');
  442. cg.a_label(exprasmlist,neglabel);
  443. { release hightree }
  444. location_release(exprasmlist,hightree.location);
  445. hightree.free;
  446. end;
  447. end
  448. else
  449. if is_dynamic_array(left.resulttype.def) then
  450. begin
  451. rg.saveusedintregisters(exprasmlist,pushed,all_intregisters);
  452. cg.a_param_loc(exprasmlist,right.location,paramanager.getintparaloc(2));
  453. cg.a_param_loc(exprasmlist,left.location,paramanager.getintparaloc(1));
  454. rg.saveintregvars(exprasmlist,all_intregisters);
  455. cg.a_call_name(exprasmlist,'FPC_DYNARRAY_RANGECHECK');
  456. rg.restoreusedintregisters(exprasmlist,pushed);
  457. end
  458. else
  459. cg.g_rangecheck(exprasmlist,right,left.resulttype.def);
  460. end;
  461. procedure tcgvecnode.pass_2;
  462. var
  463. extraoffset : longint;
  464. t : tnode;
  465. href : treference;
  466. pushed : tpushedsavedint;
  467. isjump : boolean;
  468. otl,ofl : tasmlabel;
  469. newsize : tcgsize;
  470. pushedregs : tmaybesave;
  471. begin
  472. newsize:=def_cgsize(resulttype.def);
  473. secondpass(left);
  474. if left.location.loc=LOC_CREFERENCE then
  475. location_reset(location,LOC_CREFERENCE,newsize)
  476. else
  477. location_reset(location,LOC_REFERENCE,newsize);
  478. { an ansistring needs to be dereferenced }
  479. if is_ansistring(left.resulttype.def) or
  480. is_widestring(left.resulttype.def) then
  481. begin
  482. if nf_callunique in flags then
  483. begin
  484. if left.location.loc<>LOC_REFERENCE then
  485. internalerror(200304236);
  486. rg.saveusedintregisters(exprasmlist,pushed,all_intregisters);
  487. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paramanager.getintparaloc(1));
  488. rg.saveintregvars(exprasmlist,all_intregisters);
  489. cg.a_call_name(exprasmlist,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_UNIQUE');
  490. rg.restoreusedintregisters(exprasmlist,pushed);
  491. end;
  492. case left.location.loc of
  493. LOC_REGISTER,
  494. LOC_CREGISTER :
  495. location.reference.base:=left.location.register;
  496. LOC_CREFERENCE,
  497. LOC_REFERENCE :
  498. begin
  499. location_release(exprasmlist,left.location);
  500. location.reference.base:=rg.getregisterint(exprasmlist,OS_INT);
  501. cg.a_load_ref_reg(exprasmlist,OS_ADDR,left.location.reference,location.reference.base);
  502. end;
  503. else
  504. internalerror(2002032218);
  505. end;
  506. { check for a zero length string,
  507. we can use the ansistring routine here }
  508. if (cs_check_range in aktlocalswitches) then
  509. begin
  510. rg.saveusedintregisters(exprasmlist,pushed,all_intregisters);
  511. cg.a_param_reg(exprasmlist,OS_ADDR,location.reference.base,paramanager.getintparaloc(1));
  512. rg.saveintregvars(exprasmlist,all_intregisters);
  513. cg.a_call_name(exprasmlist,'FPC_'+Upper(tstringdef(left.resulttype.def).stringtypname)+'_CHECKZERO');
  514. rg.restoreusedintregisters(exprasmlist,pushed);
  515. end;
  516. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  517. if is_ansistring(left.resulttype.def) then
  518. dec(location.reference.offset)
  519. else
  520. dec(location.reference.offset,2);
  521. end
  522. else if is_dynamic_array(left.resulttype.def) then
  523. begin
  524. case left.location.loc of
  525. LOC_REGISTER,
  526. LOC_CREGISTER :
  527. location.reference.base:=left.location.register;
  528. LOC_REFERENCE,
  529. LOC_CREFERENCE :
  530. begin
  531. location_release(exprasmlist,left.location);
  532. location.reference.base:=rg.getaddressregister(exprasmlist);
  533. cg.a_load_ref_reg(exprasmlist,OS_ADDR,
  534. left.location.reference,location.reference.base);
  535. end;
  536. else
  537. internalerror(2002032219);
  538. end;
  539. end
  540. else
  541. location_copy(location,left.location);
  542. { offset can only differ from 0 if arraydef }
  543. if (left.resulttype.def.deftype=arraydef) and
  544. not(is_dynamic_array(left.resulttype.def)) then
  545. dec(location.reference.offset,get_mul_size*tarraydef(left.resulttype.def).lowrange);
  546. if right.nodetype=ordconstn then
  547. begin
  548. { offset can only differ from 0 if arraydef }
  549. case left.resulttype.def.deftype of
  550. arraydef :
  551. begin
  552. if not(is_open_array(left.resulttype.def)) and
  553. not(is_array_of_const(left.resulttype.def)) and
  554. not(is_dynamic_array(left.resulttype.def)) then
  555. begin
  556. if (tordconstnode(right).value>tarraydef(left.resulttype.def).highrange) or
  557. (tordconstnode(right).value<tarraydef(left.resulttype.def).lowrange) then
  558. begin
  559. { this should be caught in the resulttypepass! (JM) }
  560. if (cs_check_range in aktlocalswitches) then
  561. CGMessage(parser_e_range_check_error)
  562. else
  563. CGMessage(parser_w_range_check_error);
  564. end;
  565. end
  566. else
  567. begin
  568. { range checking for open and dynamic arrays needs
  569. runtime code }
  570. secondpass(right);
  571. if (cs_check_range in aktlocalswitches) then
  572. rangecheck_array;
  573. end;
  574. end;
  575. stringdef :
  576. begin
  577. if (cs_check_range in aktlocalswitches) then
  578. begin
  579. case tstringdef(left.resulttype.def).string_typ of
  580. { it's the same for ansi- and wide strings }
  581. st_widestring,
  582. st_ansistring:
  583. begin
  584. rg.saveusedintregisters(exprasmlist,pushed,all_intregisters);
  585. cg.a_param_const(exprasmlist,OS_INT,tordconstnode(right).value,paramanager.getintparaloc(2));
  586. href:=location.reference;
  587. dec(href.offset,7);
  588. cg.a_param_ref(exprasmlist,OS_INT,href,paramanager.getintparaloc(1));
  589. rg.saveintregvars(exprasmlist,all_intregisters);
  590. cg.a_call_name(exprasmlist,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  591. rg.restoreusedintregisters(exprasmlist,pushed);
  592. end;
  593. st_shortstring:
  594. begin
  595. {!!!!!!!!!!!!!!!!!}
  596. end;
  597. st_longstring:
  598. begin
  599. {!!!!!!!!!!!!!!!!!}
  600. end;
  601. end;
  602. end;
  603. end;
  604. end;
  605. inc(location.reference.offset,
  606. get_mul_size*tordconstnode(right).value);
  607. end
  608. else
  609. { not nodetype=ordconstn }
  610. begin
  611. if (cs_regalloc in aktglobalswitches) and
  612. { if we do range checking, we don't }
  613. { need that fancy code (it would be }
  614. { buggy) }
  615. not(cs_check_range in aktlocalswitches) and
  616. (left.resulttype.def.deftype=arraydef) then
  617. begin
  618. extraoffset:=0;
  619. if (right.nodetype=addn) then
  620. begin
  621. if taddnode(right).right.nodetype=ordconstn then
  622. begin
  623. extraoffset:=tordconstnode(taddnode(right).right).value;
  624. t:=taddnode(right).left;
  625. { First pass processed this with the assumption }
  626. { that there was an add node which may require an }
  627. { extra register. Fake it or die with IE10 (JM) }
  628. t.registers32 := taddnode(right).registers32;
  629. taddnode(right).left:=nil;
  630. right.free;
  631. right:=t;
  632. end
  633. else if taddnode(right).left.nodetype=ordconstn then
  634. begin
  635. extraoffset:=tordconstnode(taddnode(right).left).value;
  636. t:=taddnode(right).right;
  637. t.registers32 := right.registers32;
  638. taddnode(right).right:=nil;
  639. right.free;
  640. right:=t;
  641. end;
  642. end
  643. else if (right.nodetype=subn) then
  644. begin
  645. if taddnode(right).right.nodetype=ordconstn then
  646. begin
  647. extraoffset:=-tordconstnode(taddnode(right).right).value;
  648. t:=taddnode(right).left;
  649. t.registers32 := right.registers32;
  650. taddnode(right).left:=nil;
  651. right.free;
  652. right:=t;
  653. end
  654. { You also have to negate right.right in this case! I can't add an
  655. unaryminusn without causing a crash, so I've disabled it (JM)
  656. else if right.left.nodetype=ordconstn then
  657. begin
  658. extraoffset:=right.left.value;
  659. t:=right.right;
  660. t^.registers32 := right.registers32;
  661. putnode(right);
  662. putnode(right.left);
  663. right:=t;
  664. end;}
  665. end;
  666. inc(location.reference.offset,
  667. get_mul_size*extraoffset);
  668. end;
  669. { calculate from left to right }
  670. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  671. internalerror(200304237);
  672. isjump:=(right.location.loc=LOC_JUMP);
  673. if isjump then
  674. begin
  675. otl:=truelabel;
  676. objectlibrary.getlabel(truelabel);
  677. ofl:=falselabel;
  678. objectlibrary.getlabel(falselabel);
  679. end;
  680. {$ifndef newra}
  681. maybe_save(exprasmlist,right.registers32,location,pushedregs);
  682. {$endif}
  683. secondpass(right);
  684. {$ifndef newra}
  685. maybe_restore(exprasmlist,location,pushedregs);
  686. {$endif}
  687. if cs_check_range in aktlocalswitches then
  688. begin
  689. if left.resulttype.def.deftype=arraydef then
  690. rangecheck_array;
  691. end;
  692. location_force_reg(exprasmlist,right.location,OS_32,false);
  693. if isjump then
  694. begin
  695. truelabel:=otl;
  696. falselabel:=ofl;
  697. end;
  698. { produce possible range check code: }
  699. if cs_check_range in aktlocalswitches then
  700. begin
  701. if left.resulttype.def.deftype=arraydef then
  702. begin
  703. { done defore (PM) }
  704. end
  705. else if (left.resulttype.def.deftype=stringdef) then
  706. begin
  707. case tstringdef(left.resulttype.def).string_typ of
  708. { it's the same for ansi- and wide strings }
  709. st_widestring,
  710. st_ansistring:
  711. begin
  712. rg.saveusedintregisters(exprasmlist,pushed,all_intregisters);
  713. cg.a_param_reg(exprasmlist,OS_INT,right.location.register,paramanager.getintparaloc(1));
  714. href:=location.reference;
  715. dec(href.offset,7);
  716. cg.a_param_ref(exprasmlist,OS_INT,href,paramanager.getintparaloc(1));
  717. rg.saveintregvars(exprasmlist,all_intregisters);
  718. cg.a_call_name(exprasmlist,'FPC_'+upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  719. rg.restoreusedintregisters(exprasmlist,pushed);
  720. end;
  721. st_shortstring:
  722. begin
  723. {!!!!!!!!!!!!!!!!!}
  724. end;
  725. st_longstring:
  726. begin
  727. {!!!!!!!!!!!!!!!!!}
  728. end;
  729. end;
  730. end;
  731. end;
  732. { insert the register and the multiplication factor in the
  733. reference }
  734. update_reference_reg_mul(right.location.register,get_mul_size);
  735. end;
  736. location.size:=newsize;
  737. end;
  738. begin
  739. cloadvmtaddrnode:=tcgloadvmtaddrnode;
  740. caddrnode:=tcgaddrnode;
  741. cdoubleaddrnode:=tcgdoubleaddrnode;
  742. cderefnode:=tcgderefnode;
  743. csubscriptnode:=tcgsubscriptnode;
  744. cwithnode:=tcgwithnode;
  745. cvecnode:=tcgvecnode;
  746. end.
  747. {
  748. $Log$
  749. Revision 1.52 2003-05-11 14:45:12 peter
  750. * tloadnode does not support objectsymtable,withsymtable anymore
  751. * withnode cleanup
  752. * direct with rewritten to use temprefnode
  753. Revision 1.51 2003/05/09 17:47:02 peter
  754. * self moved to hidden parameter
  755. * removed hdisposen,hnewn,selfn
  756. Revision 1.50 2003/05/07 09:16:23 mazen
  757. - non used units removed from uses clause
  758. Revision 1.49 2003/04/27 11:21:33 peter
  759. * aktprocdef renamed to current_procdef
  760. * procinfo renamed to current_procinfo
  761. * procinfo will now be stored in current_module so it can be
  762. cleaned up properly
  763. * gen_main_procsym changed to create_main_proc and release_main_proc
  764. to also generate a tprocinfo structure
  765. * fixed unit implicit initfinal
  766. Revision 1.48 2003/04/22 23:50:22 peter
  767. * firstpass uses expectloc
  768. * checks if there are differences between the expectloc and
  769. location.loc from secondpass in EXTDEBUG
  770. Revision 1.47 2003/04/22 13:47:08 peter
  771. * fixed C style array of const
  772. * fixed C array passing
  773. * fixed left to right with high parameters
  774. Revision 1.46 2003/04/22 10:09:35 daniel
  775. + Implemented the actual register allocator
  776. + Scratch registers unavailable when new register allocator used
  777. + maybe_save/maybe_restore unavailable when new register allocator used
  778. Revision 1.45 2003/04/06 21:11:23 olle
  779. * changed newasmsymbol to newasmsymboldata for data symbols
  780. Revision 1.44 2003/03/28 19:16:56 peter
  781. * generic constructor working for i386
  782. * remove fixed self register
  783. * esi added as address register for i386
  784. Revision 1.43 2003/03/12 22:43:38 jonas
  785. * more powerpc and generic fixes related to the new register allocator
  786. Revision 1.42 2003/02/19 22:00:14 daniel
  787. * Code generator converted to new register notation
  788. - Horribily outdated todo.txt removed
  789. Revision 1.41 2003/01/30 21:46:57 peter
  790. * self fixes for static methods (merged)
  791. Revision 1.40 2003/01/08 18:43:56 daniel
  792. * Tregister changed into a record
  793. Revision 1.39 2002/12/20 18:13:19 peter
  794. * no rangecheck for openarrays with cdecl
  795. Revision 1.38 2002/12/17 22:19:33 peter
  796. * fixed pushing of records>8 bytes with stdcall
  797. * simplified hightree loading
  798. Revision 1.37 2002/12/08 13:39:03 carl
  799. + some documentation added
  800. Revision 1.36 2002/12/07 14:14:19 carl
  801. * bugfix on invalid typecast
  802. Revision 1.35 2002/11/25 17:43:18 peter
  803. * splitted defbase in defutil,symutil,defcmp
  804. * merged isconvertable and is_equal into compare_defs(_ext)
  805. * made operator search faster by walking the list only once
  806. Revision 1.34 2002/11/24 18:19:20 carl
  807. + checkpointer for interfaces also
  808. Revision 1.33 2002/11/23 22:50:06 carl
  809. * some small speed optimizations
  810. + added several new warnings/hints
  811. Revision 1.32 2002/11/15 01:58:51 peter
  812. * merged changes from 1.0.7 up to 04-11
  813. - -V option for generating bug report tracing
  814. - more tracing for option parsing
  815. - errors for cdecl and high()
  816. - win32 import stabs
  817. - win32 records<=8 are returned in eax:edx (turned off by default)
  818. - heaptrc update
  819. - more info for temp management in .s file with EXTDEBUG
  820. Revision 1.31 2002/10/09 20:24:47 florian
  821. + range checking for dyn. arrays
  822. Revision 1.30 2002/10/07 21:30:45 peter
  823. * rangecheck for open arrays added
  824. Revision 1.29 2002/10/05 12:43:25 carl
  825. * fixes for Delphi 6 compilation
  826. (warning : Some features do not work under Delphi)
  827. Revision 1.28 2002/09/17 18:54:02 jonas
  828. * a_load_reg_reg() now has two size parameters: source and dest. This
  829. allows some optimizations on architectures that don't encode the
  830. register size in the register name.
  831. Revision 1.27 2002/09/07 15:25:03 peter
  832. * old logs removed and tabs fixed
  833. Revision 1.26 2002/09/01 18:46:01 peter
  834. * fixed generic tcgvecnode
  835. * move code that updates a reference with index register and multiplier
  836. to separate method so it can be overriden for scaled indexing
  837. * i386 uses generic tcgvecnode
  838. Revision 1.25 2002/08/23 16:14:48 peter
  839. * tempgen cleanup
  840. * tt_noreuse temp type added that will be used in genentrycode
  841. Revision 1.24 2002/08/15 08:13:54 carl
  842. - a_load_sym_ofs_reg removed
  843. * loadvmt now calls loadaddr_ref_reg instead
  844. Revision 1.23 2002/08/11 14:32:26 peter
  845. * renamed current_library to objectlibrary
  846. Revision 1.22 2002/08/11 13:24:12 peter
  847. * saving of asmsymbols in ppu supported
  848. * asmsymbollist global is removed and moved into a new class
  849. tasmlibrarydata that will hold the info of a .a file which
  850. corresponds with a single module. Added librarydata to tmodule
  851. to keep the library info stored for the module. In the future the
  852. objectfiles will also be stored to the tasmlibrarydata class
  853. * all getlabel/newasmsymbol and friends are moved to the new class
  854. Revision 1.21 2002/08/11 11:36:57 jonas
  855. * always first try to use base and only then index
  856. Revision 1.20 2002/08/11 06:14:40 florian
  857. * fixed powerpc compilation problems
  858. Revision 1.19 2002/08/10 14:46:29 carl
  859. + moved target_cpu_string to cpuinfo
  860. * renamed asmmode enum.
  861. * assembler reader has now less ifdef's
  862. * move from nppcmem.pas -> ncgmem.pas vec. node.
  863. Revision 1.18 2002/07/28 21:34:31 florian
  864. * more powerpc fixes
  865. + dummy tcgvecnode
  866. Revision 1.17 2002/07/11 14:41:28 florian
  867. * start of the new generic parameter handling
  868. Revision 1.16 2002/07/07 09:52:32 florian
  869. * powerpc target fixed, very simple units can be compiled
  870. * some basic stuff for better callparanode handling, far from being finished
  871. Revision 1.15 2002/07/01 18:46:23 peter
  872. * internal linker
  873. * reorganized aasm layer
  874. Revision 1.14 2002/07/01 16:23:53 peter
  875. * cg64 patch
  876. * basics for currency
  877. * asnode updates for class and interface (not finished)
  878. Revision 1.13 2002/05/20 13:30:40 carl
  879. * bugfix of hdisponen (base must be set, not index)
  880. * more portability fixes
  881. Revision 1.12 2002/05/18 13:34:09 peter
  882. * readded missing revisions
  883. Revision 1.11 2002/05/16 19:46:37 carl
  884. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  885. + try to fix temp allocation (still in ifdef)
  886. + generic constructor calls
  887. + start of tassembler / tmodulebase class cleanup
  888. Revision 1.9 2002/05/12 16:53:07 peter
  889. * moved entry and exitcode to ncgutil and cgobj
  890. * foreach gets extra argument for passing local data to the
  891. iterator function
  892. * -CR checks also class typecasts at runtime by changing them
  893. into as
  894. * fixed compiler to cycle with the -CR option
  895. * fixed stabs with elf writer, finally the global variables can
  896. be watched
  897. * removed a lot of routines from cga unit and replaced them by
  898. calls to cgobj
  899. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  900. u32bit then the other is typecasted also to u32bit without giving
  901. a rangecheck warning/error.
  902. * fixed pascal calling method with reversing also the high tree in
  903. the parast, detected by tcalcst3 test
  904. Revision 1.8 2002/04/20 21:32:23 carl
  905. + generic FPC_CHECKPOINTER
  906. + first parameter offset in stack now portable
  907. * rename some constants
  908. + move some cpu stuff to other units
  909. - remove unused constents
  910. * fix stacksize for some targets
  911. * fix generic size problems which depend now on EXTEND_SIZE constant
  912. Revision 1.7 2002/04/15 18:58:47 carl
  913. + target_info.size_of_pointer -> pointer_Size
  914. Revision 1.6 2002/04/04 19:05:57 peter
  915. * removed unused units
  916. * use tlocation.size in cg.a_*loc*() routines
  917. }