ncgmem.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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. tcgloadvmtnode = class(tloadvmtnode)
  29. procedure pass_2;override;
  30. end;
  31. tcghnewnode = class(thnewnode)
  32. procedure pass_2;override;
  33. end;
  34. tcghdisposenode = class(thdisposenode)
  35. procedure pass_2;override;
  36. end;
  37. tcgaddrnode = class(taddrnode)
  38. procedure pass_2;override;
  39. end;
  40. tcgdoubleaddrnode = class(tdoubleaddrnode)
  41. procedure pass_2;override;
  42. end;
  43. tcgderefnode = class(tderefnode)
  44. procedure pass_2;override;
  45. end;
  46. tcgsubscriptnode = class(tsubscriptnode)
  47. procedure pass_2;override;
  48. end;
  49. tcgselfnode = class(tselfnode)
  50. procedure pass_2;override;
  51. end;
  52. tcgwithnode = class(twithnode)
  53. procedure pass_2;override;
  54. end;
  55. tcgvecnode = class(tvecnode)
  56. private
  57. procedure rangecheck_array;
  58. protected
  59. function get_mul_size : aword;
  60. {# This routine is used to calculate the address of the reference.
  61. On entry reg contains the index in the array,
  62. and l contains the size of each element in the array.
  63. This routine should update location.reference correctly,
  64. so it points to the correct address.
  65. }
  66. procedure update_reference_reg_mul(reg:tregister;l:aword);virtual;
  67. procedure second_wideansistring;virtual;
  68. procedure second_dynamicarray;virtual;
  69. public
  70. procedure pass_2;override;
  71. end;
  72. implementation
  73. uses
  74. {$ifdef delphi}
  75. sysutils,
  76. {$else}
  77. strings,
  78. {$endif}
  79. {$ifdef GDB}
  80. gdb,
  81. {$endif GDB}
  82. globtype,systems,
  83. cutils,verbose,globals,
  84. symconst,symtype,symdef,symsym,symtable,defutil,paramgr,
  85. aasmbase,aasmtai,aasmcpu,
  86. cginfo,cgbase,pass_2,
  87. pass_1,nld,ncon,nadd,
  88. cgobj,tgobj,rgobj,ncgutil,symbase
  89. ;
  90. {*****************************************************************************
  91. TCGLOADNODE
  92. *****************************************************************************}
  93. procedure tcgloadvmtnode.pass_2;
  94. var
  95. href : treference;
  96. begin
  97. location_reset(location,LOC_REGISTER,OS_ADDR);
  98. location.register:=rg.getaddressregister(exprasmlist);
  99. { on 80386, LEA is the same as mov imm32 }
  100. reference_reset_symbol(href,
  101. objectlibrary.newasmsymbol(tobjectdef(tclassrefdef(resulttype.def).pointertype.def).vmt_mangledname),0);
  102. cg.a_loadaddr_ref_reg(exprasmlist,href,location.register);
  103. end;
  104. {*****************************************************************************
  105. TCGHNEWNODE
  106. *****************************************************************************}
  107. procedure tcghnewnode.pass_2;
  108. begin
  109. { completely resolved in first pass now }
  110. end;
  111. {*****************************************************************************
  112. TCGHDISPOSENODE
  113. *****************************************************************************}
  114. procedure tcghdisposenode.pass_2;
  115. begin
  116. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  117. secondpass(left);
  118. if codegenerror then
  119. exit;
  120. case left.location.loc of
  121. LOC_REGISTER:
  122. begin
  123. if not rg.isaddressregister(left.location.register) then
  124. begin
  125. location_release(exprasmlist,left.location);
  126. location.reference.base := rg.getaddressregister(exprasmlist);
  127. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.register,
  128. location.reference.base);
  129. end
  130. else
  131. location.reference.base := left.location.register;
  132. end;
  133. LOC_CREGISTER,
  134. LOC_CREFERENCE,
  135. LOC_REFERENCE:
  136. begin
  137. location_release(exprasmlist,left.location);
  138. location.reference.base:=rg.getaddressregister(exprasmlist);
  139. cg.a_load_loc_reg(exprasmlist,left.location,location.reference.base);
  140. end;
  141. else
  142. internalerror(2002032217);
  143. end;
  144. end;
  145. {*****************************************************************************
  146. TCGADDRNODE
  147. *****************************************************************************}
  148. procedure tcgaddrnode.pass_2;
  149. begin
  150. secondpass(left);
  151. { when loading procvar we do nothing with this node, so load the
  152. location of left }
  153. if nf_procvarload in flags then
  154. begin
  155. location_copy(location,left.location);
  156. exit;
  157. end;
  158. location_release(exprasmlist,left.location);
  159. location_reset(location,LOC_REGISTER,OS_ADDR);
  160. location.register:=rg.getaddressregister(exprasmlist);
  161. {@ on a procvar means returning an address to the procedure that
  162. is stored in it.}
  163. { yes but left.symtableentry can be nil
  164. for example on self !! }
  165. { symtableentry can be also invalid, if left is no tree node }
  166. if (m_tp_procvar in aktmodeswitches) and
  167. (left.nodetype=loadn) and
  168. assigned(tloadnode(left).symtableentry) and
  169. (tloadnode(left).symtableentry.typ=varsym) and
  170. (tvarsym(tloadnode(left).symtableentry).vartype.def.deftype=procvardef) then
  171. cg.a_load_ref_reg(exprasmlist,OS_ADDR,left.location.reference,
  172. location.register)
  173. else
  174. begin
  175. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,
  176. location.register);
  177. end;
  178. end;
  179. {*****************************************************************************
  180. TCGDOUBLEADDRNODE
  181. *****************************************************************************}
  182. procedure tcgdoubleaddrnode.pass_2;
  183. begin
  184. secondpass(left);
  185. location_release(exprasmlist,left.location);
  186. location_reset(location,LOC_REGISTER,OS_ADDR);
  187. location.register:=rg.getaddressregister(exprasmlist);
  188. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,
  189. location.register);
  190. end;
  191. {*****************************************************************************
  192. TCGDEREFNODE
  193. *****************************************************************************}
  194. procedure tcgderefnode.pass_2;
  195. begin
  196. secondpass(left);
  197. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  198. case left.location.loc of
  199. LOC_REGISTER:
  200. begin
  201. if not rg.isaddressregister(left.location.register) then
  202. begin
  203. location_release(exprasmlist,left.location);
  204. location.reference.base := rg.getaddressregister(exprasmlist);
  205. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.register,
  206. location.reference.base);
  207. end
  208. else
  209. location.reference.base := left.location.register;
  210. end;
  211. LOC_CREGISTER,
  212. LOC_CREFERENCE,
  213. LOC_REFERENCE:
  214. begin
  215. location_release(exprasmlist,left.location);
  216. location.reference.base:=rg.getaddressregister(exprasmlist);
  217. cg.a_load_loc_reg(exprasmlist,left.location,location.reference.base);
  218. end;
  219. end;
  220. if (cs_gdb_heaptrc in aktglobalswitches) and
  221. (cs_checkpointer in aktglobalswitches) and
  222. not(cs_compilesystem in aktmoduleswitches) and
  223. (not tpointerdef(left.resulttype.def).is_far) then
  224. begin
  225. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paramanager.getintparaloc(1));
  226. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  227. end;
  228. end;
  229. {*****************************************************************************
  230. TCGSUBSCRIPTNODE
  231. *****************************************************************************}
  232. procedure tcgsubscriptnode.pass_2;
  233. begin
  234. secondpass(left);
  235. if codegenerror then
  236. exit;
  237. { classes and interfaces must be dereferenced implicit }
  238. if is_class_or_interface(left.resulttype.def) then
  239. begin
  240. location_reset(location,LOC_REFERENCE,def_cgsize(resulttype.def));
  241. case left.location.loc of
  242. LOC_REGISTER:
  243. begin
  244. if not rg.isaddressregister(left.location.register) then
  245. begin
  246. location_release(exprasmlist,left.location);
  247. location.reference.base:=rg.getaddressregister(exprasmlist);
  248. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,
  249. left.location.register,location.reference.base);
  250. end
  251. else
  252. location.reference.base := left.location.register;
  253. end;
  254. LOC_CREGISTER,
  255. LOC_CREFERENCE,
  256. LOC_REFERENCE:
  257. begin
  258. location_release(exprasmlist,left.location);
  259. location.reference.base:=rg.getaddressregister(exprasmlist);
  260. cg.a_load_loc_reg(exprasmlist,left.location,location.reference.base);
  261. end;
  262. end;
  263. { implicit deferencing }
  264. if (cs_gdb_heaptrc in aktglobalswitches) and
  265. (cs_checkpointer in aktglobalswitches) and
  266. not(cs_compilesystem in aktmoduleswitches) then
  267. begin
  268. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paramanager.getintparaloc(1));
  269. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  270. end;
  271. end
  272. else if is_interfacecom(left.resulttype.def) then
  273. begin
  274. tg.GetTemp(exprasmlist,pointer_size,tt_interfacecom,location.reference);
  275. cg.a_load_loc_ref(exprasmlist,left.location,location.reference);
  276. { implicit deferencing also for interfaces }
  277. if (cs_gdb_heaptrc in aktglobalswitches) and
  278. (cs_checkpointer in aktglobalswitches) and
  279. not(cs_compilesystem in aktmoduleswitches) then
  280. begin
  281. cg.a_param_reg(exprasmlist, OS_ADDR,location.reference.base,paramanager.getintparaloc(1));
  282. cg.a_call_name(exprasmlist,'FPC_CHECKPOINTER');
  283. end;
  284. end
  285. else
  286. location_copy(location,left.location);
  287. inc(location.reference.offset,vs.address);
  288. { also update the size of the location }
  289. location.size:=def_cgsize(resulttype.def);
  290. end;
  291. {*****************************************************************************
  292. TCGSELFNODE
  293. *****************************************************************************}
  294. procedure tcgselfnode.pass_2;
  295. begin
  296. rg.getexplicitregisterint(exprasmlist,SELF_POINTER_REG);
  297. if (resulttype.def.deftype=classrefdef) or
  298. is_class(resulttype.def) then
  299. begin
  300. location_reset(location,LOC_CREGISTER,OS_ADDR);
  301. location.register:=SELF_POINTER_REG;
  302. end
  303. else
  304. begin
  305. location_reset(location,LOC_CREFERENCE,OS_ADDR);
  306. location.reference.base:=SELF_POINTER_REG;
  307. end;
  308. end;
  309. {*****************************************************************************
  310. TCGWITHNODE
  311. *****************************************************************************}
  312. procedure tcgwithnode.pass_2;
  313. var
  314. tmpreg: tregister;
  315. usetemp,with_expr_in_temp : boolean;
  316. symtable : tsymtable;
  317. i : integer;
  318. {$ifdef GDB}
  319. withstartlabel,withendlabel : tasmlabel;
  320. pp : pchar;
  321. mangled_length : longint;
  322. const
  323. withlevel : longint = 0;
  324. {$endif GDB}
  325. begin
  326. if assigned(left) then
  327. begin
  328. secondpass(left);
  329. {$ifdef i386}
  330. if (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  331. (left.location.reference.segment<>R_NO) then
  332. message(parser_e_no_with_for_variable_in_other_segments);
  333. {$endif i386}
  334. reference_reset(withreference);
  335. usetemp:=false;
  336. if (left.nodetype=loadn) and
  337. (tloadnode(left).symtable=aktprocdef.localst) then
  338. begin
  339. { for locals use the local storage }
  340. withreference:=left.location.reference;
  341. include(flags,nf_islocal);
  342. end
  343. else
  344. { call can have happend with a property }
  345. begin
  346. usetemp:=true;
  347. if is_class_or_interface(left.resulttype.def) then
  348. begin
  349. tmpreg := cg.get_scratch_reg_int(exprasmlist);
  350. cg.a_load_loc_reg(exprasmlist,left.location,tmpreg)
  351. end
  352. else
  353. begin
  354. tmpreg := cg.get_scratch_reg_address(exprasmlist);
  355. cg.a_loadaddr_ref_reg(exprasmlist,
  356. left.location.reference,tmpreg);
  357. end;
  358. end;
  359. location_release(exprasmlist,left.location);
  360. symtable:=withsymtable;
  361. for i:=1 to tablecount do
  362. begin
  363. if (left.nodetype=loadn) and
  364. (tloadnode(left).symtable=aktprocdef.localst) then
  365. twithsymtable(symtable).direct_with:=true;
  366. twithsymtable(symtable).withnode:=self;
  367. symtable:=symtable.next;
  368. end;
  369. { if the with expression is stored in a temp }
  370. { area we must make it persistent and shouldn't }
  371. { release it (FK) }
  372. if (left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  373. tg.istemp(left.location.reference) then
  374. with_expr_in_temp:=tg.ChangeTempType(exprasmlist,left.location.reference,tt_persistant)
  375. else
  376. with_expr_in_temp:=false;
  377. { if usetemp is set the value must be in tmpreg }
  378. if usetemp then
  379. begin
  380. tg.GetTemp(exprasmlist,pointer_size,tt_persistant,withreference);
  381. { move to temp reference }
  382. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,withreference);
  383. cg.free_scratch_reg(exprasmlist,tmpreg);
  384. {$ifdef GDB}
  385. if (cs_debuginfo in aktmoduleswitches) then
  386. begin
  387. inc(withlevel);
  388. objectlibrary.getaddrlabel(withstartlabel);
  389. objectlibrary.getaddrlabel(withendlabel);
  390. cg.a_label(exprasmlist,withstartlabel);
  391. withdebugList.concat(Tai_stabs.Create(strpnew(
  392. '"with'+tostr(withlevel)+':'+tostr(symtablestack.getnewtypecount)+
  393. '=*'+tstoreddef(left.resulttype.def).numberstring+'",'+
  394. tostr(N_LSYM)+',0,0,'+tostr(withreference.offset))));
  395. mangled_length:=length(aktprocdef.mangledname);
  396. getmem(pp,mangled_length+50);
  397. strpcopy(pp,'192,0,0,'+withstartlabel.name);
  398. if (target_info.use_function_relative_addresses) then
  399. begin
  400. strpcopy(strend(pp),'-');
  401. strpcopy(strend(pp),aktprocdef.mangledname);
  402. end;
  403. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  404. end;
  405. {$endif GDB}
  406. end;
  407. { right can be optimize out !!! }
  408. if assigned(right) then
  409. secondpass(right);
  410. if usetemp then
  411. begin
  412. tg.UnGetTemp(exprasmlist,withreference);
  413. {$ifdef GDB}
  414. if (cs_debuginfo in aktmoduleswitches) then
  415. begin
  416. cg.a_label(exprasmlist,withendlabel);
  417. strpcopy(pp,'224,0,0,'+withendlabel.name);
  418. if (target_info.use_function_relative_addresses) then
  419. begin
  420. strpcopy(strend(pp),'-');
  421. strpcopy(strend(pp),aktprocdef.mangledname);
  422. end;
  423. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  424. freemem(pp,mangled_length+50);
  425. dec(withlevel);
  426. end;
  427. {$endif GDB}
  428. end;
  429. if with_expr_in_temp then
  430. tg.UnGetTemp(exprasmlist,left.location.reference);
  431. reference_reset(withreference);
  432. end;
  433. end;
  434. {*****************************************************************************
  435. TCGVECNODE
  436. *****************************************************************************}
  437. function tcgvecnode.get_mul_size : aword;
  438. begin
  439. if nf_memindex in flags then
  440. get_mul_size:=1
  441. else
  442. begin
  443. if (left.resulttype.def.deftype=arraydef) then
  444. get_mul_size:=tarraydef(left.resulttype.def).elesize
  445. else
  446. get_mul_size:=resulttype.def.size;
  447. end
  448. end;
  449. procedure tcgvecnode.update_reference_reg_mul(reg:tregister;l:aword);
  450. begin
  451. if location.reference.base=R_NO then
  452. begin
  453. cg.a_op_const_reg(exprasmlist,OP_IMUL,l,reg);
  454. location.reference.base:=reg;
  455. end
  456. else if location.reference.index=R_NO then
  457. begin
  458. cg.a_op_const_reg(exprasmlist,OP_IMUL,l,reg);
  459. location.reference.index:=reg;
  460. end
  461. else
  462. begin
  463. cg.a_loadaddr_ref_reg(exprasmlist,location.reference,location.reference.index);
  464. rg.ungetregisterint(exprasmlist,location.reference.base);
  465. reference_reset_base(location.reference,location.reference.index,0);
  466. { insert new index register }
  467. cg.a_op_const_reg(exprasmlist,OP_IMUL,l,reg);
  468. location.reference.index:=reg;
  469. end;
  470. end;
  471. procedure tcgvecnode.second_wideansistring;
  472. begin
  473. end;
  474. procedure tcgvecnode.second_dynamicarray;
  475. begin
  476. end;
  477. procedure tcgvecnode.rangecheck_array;
  478. var
  479. freereg : boolean;
  480. hightree : tnode;
  481. srsym : tsym;
  482. poslabel,
  483. neglabel : tasmlabel;
  484. hreg : tregister;
  485. href : treference;
  486. pushed : tpushedsaved;
  487. begin
  488. if is_open_array(left.resulttype.def) or
  489. is_array_of_const(left.resulttype.def) then
  490. begin
  491. { Get high value }
  492. srsym:=searchsymonlyin(tloadnode(left).symtable,
  493. 'high'+tvarsym(tloadnode(left).symtableentry).name);
  494. hightree:=cloadnode.create(tvarsym(srsym),tloadnode(left).symtable);
  495. firstpass(hightree);
  496. secondpass(hightree);
  497. { generate compares }
  498. freereg:=false;
  499. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  500. hreg:=right.location.register
  501. else
  502. begin
  503. hreg := cg.get_scratch_reg_int(exprasmlist);
  504. freereg:=true;
  505. cg.a_load_loc_reg(exprasmlist,right.location,hreg);
  506. end;
  507. objectlibrary.getlabel(neglabel);
  508. objectlibrary.getlabel(poslabel);
  509. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_LT,0,hreg,poslabel);
  510. cg.a_cmp_loc_reg_label(exprasmlist,OS_INT,OC_BE,hightree.location,hreg,neglabel);
  511. if freereg then
  512. cg.free_scratch_reg(exprasmlist,hreg);
  513. cg.a_label(exprasmlist,poslabel);
  514. cg.a_call_name(exprasmlist,'FPC_RANGEERROR');
  515. cg.a_label(exprasmlist,neglabel);
  516. { release hightree }
  517. location_release(exprasmlist,hightree.location);
  518. hightree.free;
  519. end
  520. else
  521. if is_dynamic_array(left.resulttype.def) then
  522. begin
  523. rg.saveusedregisters(exprasmlist,pushed,all_registers);
  524. cg.a_param_loc(exprasmlist,right.location,paramanager.getintparaloc(2));
  525. cg.a_param_loc(exprasmlist,left.location,paramanager.getintparaloc(1));
  526. rg.saveregvars(exprasmlist,all_registers);
  527. cg.a_call_name(exprasmlist,'FPC_DYNARRAY_RANGECHECK');
  528. rg.restoreusedregisters(exprasmlist,pushed);
  529. cg.g_maybe_loadself(exprasmlist);
  530. end
  531. else
  532. cg.g_rangecheck(exprasmlist,right,left.resulttype.def);
  533. end;
  534. procedure tcgvecnode.pass_2;
  535. var
  536. extraoffset : longint;
  537. t : tnode;
  538. href : treference;
  539. pushed : tpushedsaved;
  540. isjump : boolean;
  541. otl,ofl : tasmlabel;
  542. newsize : tcgsize;
  543. pushedregs : tmaybesave;
  544. begin
  545. newsize:=def_cgsize(resulttype.def);
  546. location_reset(location,LOC_REFERENCE,newsize);
  547. secondpass(left);
  548. { we load the array reference to location }
  549. { an ansistring needs to be dereferenced }
  550. if is_ansistring(left.resulttype.def) or
  551. is_widestring(left.resulttype.def) then
  552. begin
  553. if nf_callunique in flags then
  554. begin
  555. if left.location.loc<>LOC_REFERENCE then
  556. begin
  557. CGMessage(cg_e_illegal_expression);
  558. exit;
  559. end;
  560. rg.saveusedregisters(exprasmlist,pushed,all_registers);
  561. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paramanager.getintparaloc(1));
  562. rg.saveregvars(exprasmlist,all_registers);
  563. cg.a_call_name(exprasmlist,'FPC_'+Upper(tstringdef(left.resulttype.def).stringtypname)+'_UNIQUE');
  564. cg.g_maybe_loadself(exprasmlist);
  565. rg.restoreusedregisters(exprasmlist,pushed);
  566. end;
  567. case left.location.loc of
  568. LOC_REGISTER,
  569. LOC_CREGISTER :
  570. location.reference.base:=left.location.register;
  571. LOC_CREFERENCE,
  572. LOC_REFERENCE :
  573. begin
  574. location_release(exprasmlist,left.location);
  575. location.reference.base:=rg.getregisterint(exprasmlist);
  576. cg.a_load_ref_reg(exprasmlist,OS_ADDR,left.location.reference,location.reference.base);
  577. end;
  578. else
  579. internalerror(2002032218);
  580. end;
  581. { check for a zero length string,
  582. we can use the ansistring routine here }
  583. if (cs_check_range in aktlocalswitches) then
  584. begin
  585. rg.saveusedregisters(exprasmlist,pushed,all_registers);
  586. cg.a_param_reg(exprasmlist,OS_ADDR,location.reference.base,paramanager.getintparaloc(1));
  587. rg.saveregvars(exprasmlist,all_registers);
  588. cg.a_call_name(exprasmlist,'FPC_'+Upper(tstringdef(left.resulttype.def).stringtypname)+'_CHECKZERO');
  589. cg.g_maybe_loadself(exprasmlist);
  590. rg.restoreusedregisters(exprasmlist,pushed);
  591. end;
  592. { in ansistrings/widestrings S[1] is p<w>char(S)[0] !! }
  593. if is_ansistring(left.resulttype.def) then
  594. dec(location.reference.offset)
  595. else
  596. dec(location.reference.offset,2);
  597. end
  598. else if is_dynamic_array(left.resulttype.def) then
  599. begin
  600. case left.location.loc of
  601. LOC_REGISTER,
  602. LOC_CREGISTER :
  603. location.reference.base:=left.location.register;
  604. LOC_REFERENCE,
  605. LOC_CREFERENCE :
  606. begin
  607. location_release(exprasmlist,left.location);
  608. location.reference.base:=rg.getaddressregister(exprasmlist);
  609. cg.a_load_ref_reg(exprasmlist,OS_ADDR,
  610. left.location.reference,location.reference.base);
  611. end;
  612. else
  613. internalerror(2002032219);
  614. end;
  615. end
  616. else
  617. location_copy(location,left.location);
  618. { offset can only differ from 0 if arraydef }
  619. if (left.resulttype.def.deftype=arraydef) and
  620. not(is_dynamic_array(left.resulttype.def)) then
  621. dec(location.reference.offset,get_mul_size*tarraydef(left.resulttype.def).lowrange);
  622. if right.nodetype=ordconstn then
  623. begin
  624. { offset can only differ from 0 if arraydef }
  625. case left.resulttype.def.deftype of
  626. arraydef :
  627. begin
  628. if not(is_open_array(left.resulttype.def)) and
  629. not(is_array_of_const(left.resulttype.def)) and
  630. not(is_dynamic_array(left.resulttype.def)) then
  631. begin
  632. if (tordconstnode(right).value>tarraydef(left.resulttype.def).highrange) or
  633. (tordconstnode(right).value<tarraydef(left.resulttype.def).lowrange) then
  634. begin
  635. { this should be caught in the resulttypepass! (JM) }
  636. if (cs_check_range in aktlocalswitches) then
  637. CGMessage(parser_e_range_check_error)
  638. else
  639. CGMessage(parser_w_range_check_error);
  640. end;
  641. end
  642. else
  643. begin
  644. { range checking for open and dynamic arrays needs
  645. runtime code }
  646. secondpass(right);
  647. rangecheck_array;
  648. end;
  649. end;
  650. stringdef :
  651. begin
  652. if (cs_check_range in aktlocalswitches) then
  653. begin
  654. case tstringdef(left.resulttype.def).string_typ of
  655. { it's the same for ansi- and wide strings }
  656. st_widestring,
  657. st_ansistring:
  658. begin
  659. rg.saveusedregisters(exprasmlist,pushed,all_registers);
  660. cg.a_param_const(exprasmlist,OS_INT,tordconstnode(right).value,paramanager.getintparaloc(2));
  661. href:=location.reference;
  662. dec(href.offset,7);
  663. cg.a_param_ref(exprasmlist,OS_INT,href,paramanager.getintparaloc(1));
  664. rg.saveregvars(exprasmlist,all_registers);
  665. cg.a_call_name(exprasmlist,'FPC_'+Upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  666. rg.restoreusedregisters(exprasmlist,pushed);
  667. cg.g_maybe_loadself(exprasmlist);
  668. end;
  669. st_shortstring:
  670. begin
  671. {!!!!!!!!!!!!!!!!!}
  672. end;
  673. st_longstring:
  674. begin
  675. {!!!!!!!!!!!!!!!!!}
  676. end;
  677. end;
  678. end;
  679. end;
  680. end;
  681. inc(location.reference.offset,
  682. get_mul_size*tordconstnode(right).value);
  683. end
  684. else
  685. { not nodetype=ordconstn }
  686. begin
  687. if (cs_regalloc in aktglobalswitches) and
  688. { if we do range checking, we don't }
  689. { need that fancy code (it would be }
  690. { buggy) }
  691. not(cs_check_range in aktlocalswitches) and
  692. (left.resulttype.def.deftype=arraydef) then
  693. begin
  694. extraoffset:=0;
  695. if (right.nodetype=addn) then
  696. begin
  697. if taddnode(right).right.nodetype=ordconstn then
  698. begin
  699. extraoffset:=tordconstnode(taddnode(right).right).value;
  700. t:=taddnode(right).left;
  701. { First pass processed this with the assumption }
  702. { that there was an add node which may require an }
  703. { extra register. Fake it or die with IE10 (JM) }
  704. t.registers32 := taddnode(right).registers32;
  705. taddnode(right).left:=nil;
  706. right.free;
  707. right:=t;
  708. end
  709. else if taddnode(right).left.nodetype=ordconstn then
  710. begin
  711. extraoffset:=tordconstnode(taddnode(right).left).value;
  712. t:=taddnode(right).right;
  713. t.registers32 := right.registers32;
  714. taddnode(right).right:=nil;
  715. right.free;
  716. right:=t;
  717. end;
  718. end
  719. else if (right.nodetype=subn) then
  720. begin
  721. if taddnode(right).right.nodetype=ordconstn then
  722. begin
  723. extraoffset:=-tordconstnode(taddnode(right).right).value;
  724. t:=taddnode(right).left;
  725. t.registers32 := right.registers32;
  726. taddnode(right).left:=nil;
  727. right.free;
  728. right:=t;
  729. end
  730. { You also have to negate right.right in this case! I can't add an
  731. unaryminusn without causing a crash, so I've disabled it (JM)
  732. else if right.left.nodetype=ordconstn then
  733. begin
  734. extraoffset:=right.left.value;
  735. t:=right.right;
  736. t^.registers32 := right.registers32;
  737. putnode(right);
  738. putnode(right.left);
  739. right:=t;
  740. end;}
  741. end;
  742. inc(location.reference.offset,
  743. get_mul_size*extraoffset);
  744. end;
  745. { calculate from left to right }
  746. if not(location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  747. { should be internalerror! (JM) }
  748. CGMessage(cg_e_illegal_expression);
  749. isjump:=(right.location.loc=LOC_JUMP);
  750. if isjump then
  751. begin
  752. otl:=truelabel;
  753. objectlibrary.getlabel(truelabel);
  754. ofl:=falselabel;
  755. objectlibrary.getlabel(falselabel);
  756. end;
  757. maybe_save(exprasmlist,right.registers32,location,pushedregs);
  758. secondpass(right);
  759. maybe_restore(exprasmlist,location,pushedregs);
  760. if cs_check_range in aktlocalswitches then
  761. begin
  762. if left.resulttype.def.deftype=arraydef then
  763. rangecheck_array;
  764. end;
  765. location_force_reg(exprasmlist,right.location,OS_32,false);
  766. if isjump then
  767. begin
  768. truelabel:=otl;
  769. falselabel:=ofl;
  770. end;
  771. { produce possible range check code: }
  772. if cs_check_range in aktlocalswitches then
  773. begin
  774. if left.resulttype.def.deftype=arraydef then
  775. begin
  776. { done defore (PM) }
  777. end
  778. else if (left.resulttype.def.deftype=stringdef) then
  779. begin
  780. case tstringdef(left.resulttype.def).string_typ of
  781. { it's the same for ansi- and wide strings }
  782. st_widestring,
  783. st_ansistring:
  784. begin
  785. rg.saveusedregisters(exprasmlist,pushed,all_registers);
  786. cg.a_param_reg(exprasmlist,OS_INT,right.location.register,paramanager.getintparaloc(1));
  787. href:=location.reference;
  788. dec(href.offset,7);
  789. cg.a_param_ref(exprasmlist,OS_INT,href,paramanager.getintparaloc(1));
  790. rg.saveregvars(exprasmlist,all_registers);
  791. cg.a_call_name(exprasmlist,'FPC_'+Upper(tstringdef(left.resulttype.def).stringtypname)+'_RANGECHECK');
  792. rg.restoreusedregisters(exprasmlist,pushed);
  793. cg.g_maybe_loadself(exprasmlist);
  794. end;
  795. st_shortstring:
  796. begin
  797. {!!!!!!!!!!!!!!!!!}
  798. end;
  799. st_longstring:
  800. begin
  801. {!!!!!!!!!!!!!!!!!}
  802. end;
  803. end;
  804. end;
  805. end;
  806. { insert the register and the multiplication factor in the
  807. reference }
  808. update_reference_reg_mul(right.location.register,get_mul_size);
  809. end;
  810. location.size:=newsize;
  811. end;
  812. begin
  813. cloadvmtnode:=tcgloadvmtnode;
  814. chnewnode:=tcghnewnode;
  815. chdisposenode:=tcghdisposenode;
  816. caddrnode:=tcgaddrnode;
  817. cdoubleaddrnode:=tcgdoubleaddrnode;
  818. cderefnode:=tcgderefnode;
  819. csubscriptnode:=tcgsubscriptnode;
  820. cselfnode:=tcgselfnode;
  821. cwithnode:=tcgwithnode;
  822. cvecnode:=tcgvecnode;
  823. end.
  824. {
  825. $Log$
  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. }