n386mem.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Generate i386 assembler for in memory related nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit n386mem;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. node,nmem;
  23. type
  24. ti386loadvmtnode = class(tloadvmtnode)
  25. procedure pass_2;override;
  26. end;
  27. ti386hnewnode = class(thnewnode)
  28. procedure pass_2;override;
  29. end;
  30. ti386newnode = class(tnewnode)
  31. procedure pass_2;override;
  32. end;
  33. ti386hdisposenode = class(thdisposenode)
  34. procedure pass_2;override;
  35. end;
  36. ti386simplenewdisposenode = class(tsimplenewdisposenode)
  37. procedure pass_2;override;
  38. end;
  39. ti386addrnode = class(taddrnode)
  40. procedure pass_2;override;
  41. end;
  42. ti386doubleaddrnode = class(tdoubleaddrnode)
  43. procedure pass_2;override;
  44. end;
  45. ti386derefnode = class(tderefnode)
  46. procedure pass_2;override;
  47. end;
  48. ti386subscriptnode = class(tsubscriptnode)
  49. procedure pass_2;override;
  50. end;
  51. ti386vecnode = class(tvecnode)
  52. procedure pass_2;override;
  53. end;
  54. ti386selfnode = class(tselfnode)
  55. procedure pass_2;override;
  56. end;
  57. ti386withnode = class(twithnode)
  58. procedure pass_2;override;
  59. end;
  60. implementation
  61. uses
  62. {$ifdef delphi}
  63. sysutils,
  64. {$else}
  65. strings,
  66. {$endif}
  67. {$ifdef GDB}
  68. gdb,
  69. {$endif GDB}
  70. globtype,systems,
  71. cutils,cobjects,verbose,globals,
  72. symconst,symbase,symdef,symsym,symtable,aasm,types,
  73. hcodegen,temp_gen,pass_2,
  74. pass_1,nld,ncon,nadd,
  75. cpubase,cpuasm,
  76. cgai386,tgeni386,n386util;
  77. {*****************************************************************************
  78. TI386LOADNODE
  79. *****************************************************************************}
  80. procedure ti386loadvmtnode.pass_2;
  81. begin
  82. location.register:=getregister32;
  83. emit_sym_ofs_reg(A_MOV,
  84. S_L,newasmsymbol(pobjectdef(pclassrefdef(resulttype)^.pointertype.def)^.vmt_mangledname),0,
  85. location.register);
  86. end;
  87. {*****************************************************************************
  88. TI386HNEWNODE
  89. *****************************************************************************}
  90. procedure ti386hnewnode.pass_2;
  91. begin
  92. end;
  93. {*****************************************************************************
  94. TI386NEWNODE
  95. *****************************************************************************}
  96. procedure ti386newnode.pass_2;
  97. var
  98. pushed : tpushed;
  99. r : preference;
  100. begin
  101. if assigned(left) then
  102. begin
  103. secondpass(left);
  104. location.register:=left.location.register;
  105. end
  106. else
  107. begin
  108. pushusedregisters(pushed,$ff);
  109. gettempofsizereference(target_os.size_of_pointer,location.reference);
  110. { determines the size of the mem block }
  111. push_int(ppointerdef(resulttype)^.pointertype.def^.size);
  112. emit_push_lea_loc(location,false);
  113. emitcall('FPC_GETMEM');
  114. if ppointerdef(resulttype)^.pointertype.def^.needs_inittable then
  115. begin
  116. new(r);
  117. reset_reference(r^);
  118. r^.symbol:=pstoreddef(ppointerdef(left.resulttype)^.pointertype.def)^.get_inittable_label;
  119. emitpushreferenceaddr(r^);
  120. dispose(r);
  121. { push pointer we just allocated, we need to initialize the
  122. data located at that pointer not the pointer self (PFV) }
  123. emit_push_loc(location);
  124. emitcall('FPC_INITIALIZE');
  125. end;
  126. popusedregisters(pushed);
  127. { may be load ESI }
  128. maybe_loadesi;
  129. end;
  130. if codegenerror then
  131. exit;
  132. end;
  133. {*****************************************************************************
  134. TI386HDISPOSENODE
  135. *****************************************************************************}
  136. procedure ti386hdisposenode.pass_2;
  137. begin
  138. secondpass(left);
  139. if codegenerror then
  140. exit;
  141. reset_reference(location.reference);
  142. case left.location.loc of
  143. LOC_REGISTER:
  144. location.reference.index:=left.location.register;
  145. LOC_CREGISTER:
  146. begin
  147. location.reference.index:=getregister32;
  148. emit_reg_reg(A_MOV,S_L,
  149. left.location.register,
  150. location.reference.index);
  151. end;
  152. LOC_MEM,LOC_REFERENCE :
  153. begin
  154. del_reference(left.location.reference);
  155. location.reference.index:=getregister32;
  156. emit_ref_reg(A_MOV,S_L,newreference(left.location.reference),
  157. location.reference.index);
  158. end;
  159. end;
  160. end;
  161. {*****************************************************************************
  162. TI386SIMPLENEWDISPOSENODE
  163. *****************************************************************************}
  164. procedure ti386simplenewdisposenode.pass_2;
  165. var
  166. pushed : tpushed;
  167. r : preference;
  168. begin
  169. secondpass(left);
  170. if codegenerror then
  171. exit;
  172. pushusedregisters(pushed,$ff);
  173. { call the mem handling procedures }
  174. case nodetype of
  175. simpledisposen:
  176. begin
  177. if ppointerdef(left.resulttype)^.pointertype.def^.needs_inittable then
  178. begin
  179. new(r);
  180. reset_reference(r^);
  181. r^.symbol:=pstoreddef(ppointerdef(left.resulttype)^.pointertype.def)^.get_inittable_label;
  182. emitpushreferenceaddr(r^);
  183. dispose(r);
  184. { push pointer adress }
  185. emit_push_loc(left.location);
  186. emitcall('FPC_FINALIZE');
  187. end;
  188. emit_push_lea_loc(left.location,true);
  189. emitcall('FPC_FREEMEM');
  190. end;
  191. simplenewn:
  192. begin
  193. { determines the size of the mem block }
  194. push_int(ppointerdef(left.resulttype)^.pointertype.def^.size);
  195. emit_push_lea_loc(left.location,true);
  196. emitcall('FPC_GETMEM');
  197. if ppointerdef(left.resulttype)^.pointertype.def^.needs_inittable then
  198. begin
  199. new(r);
  200. reset_reference(r^);
  201. r^.symbol:=pstoreddef(ppointerdef(left.resulttype)^.pointertype.def)^.get_inittable_label;
  202. emitpushreferenceaddr(r^);
  203. dispose(r);
  204. emit_push_loc(left.location);
  205. emitcall('FPC_INITIALIZE');
  206. end;
  207. end;
  208. end;
  209. popusedregisters(pushed);
  210. { may be load ESI }
  211. maybe_loadesi;
  212. end;
  213. {*****************************************************************************
  214. TI386ADDRNODE
  215. *****************************************************************************}
  216. procedure ti386addrnode.pass_2;
  217. begin
  218. secondpass(left);
  219. { when loading procvar we do nothing with this node, so load the
  220. location of left }
  221. if nf_procvarload in flags then
  222. begin
  223. set_location(location,left.location);
  224. exit;
  225. end;
  226. location.loc:=LOC_REGISTER;
  227. del_reference(left.location.reference);
  228. location.register:=getregister32;
  229. {@ on a procvar means returning an address to the procedure that
  230. is stored in it.}
  231. { yes but left.symtableentry can be nil
  232. for example on @self !! }
  233. { symtableentry can be also invalid, if left is no tree node }
  234. if (m_tp_procvar in aktmodeswitches) and
  235. (left.nodetype=loadn) and
  236. assigned(tloadnode(left).symtableentry) and
  237. (tloadnode(left).symtableentry^.typ=varsym) and
  238. (pvarsym(tloadnode(left).symtableentry)^.vartype.def^.deftype=procvardef) then
  239. emit_ref_reg(A_MOV,S_L,
  240. newreference(left.location.reference),
  241. location.register)
  242. else
  243. emit_ref_reg(A_LEA,S_L,
  244. newreference(left.location.reference),
  245. location.register);
  246. { for use of other segments }
  247. if left.location.reference.segment<>R_NO then
  248. location.segment:=left.location.reference.segment;
  249. end;
  250. {*****************************************************************************
  251. TI386DOUBLEADDRNODE
  252. *****************************************************************************}
  253. procedure ti386doubleaddrnode.pass_2;
  254. begin
  255. secondpass(left);
  256. location.loc:=LOC_REGISTER;
  257. del_reference(left.location.reference);
  258. location.register:=getregister32;
  259. emit_ref_reg(A_LEA,S_L,
  260. newreference(left.location.reference),
  261. location.register);
  262. end;
  263. {*****************************************************************************
  264. TI386DEREFNODE
  265. *****************************************************************************}
  266. procedure ti386derefnode.pass_2;
  267. var
  268. hr : tregister;
  269. begin
  270. secondpass(left);
  271. reset_reference(location.reference);
  272. case left.location.loc of
  273. LOC_REGISTER:
  274. location.reference.base:=left.location.register;
  275. LOC_CREGISTER:
  276. begin
  277. { ... and reserve one for the pointer }
  278. hr:=getregister32;
  279. emit_reg_reg(A_MOV,S_L,left.location.register,hr);
  280. location.reference.base:=hr;
  281. end;
  282. else
  283. begin
  284. { free register }
  285. del_reference(left.location.reference);
  286. { ...and reserve one for the pointer }
  287. hr:=getregister32;
  288. emit_ref_reg(
  289. A_MOV,S_L,newreference(left.location.reference),
  290. hr);
  291. location.reference.base:=hr;
  292. end;
  293. end;
  294. if ppointerdef(left.resulttype)^.is_far then
  295. location.reference.segment:=R_FS;
  296. if not ppointerdef(left.resulttype)^.is_far and
  297. (cs_gdb_heaptrc in aktglobalswitches) and
  298. (cs_checkpointer in aktglobalswitches) then
  299. begin
  300. emit_reg(
  301. A_PUSH,S_L,location.reference.base);
  302. emitcall('FPC_CHECKPOINTER');
  303. end;
  304. end;
  305. {*****************************************************************************
  306. TI386SUBSCRIPTNODE
  307. *****************************************************************************}
  308. procedure ti386subscriptnode.pass_2;
  309. var
  310. hr : tregister;
  311. begin
  312. secondpass(left);
  313. if codegenerror then
  314. exit;
  315. { classes and interfaces must be dereferenced implicit }
  316. if is_class_or_interface(left.resulttype) then
  317. begin
  318. reset_reference(location.reference);
  319. case left.location.loc of
  320. LOC_REGISTER:
  321. location.reference.base:=left.location.register;
  322. LOC_CREGISTER:
  323. begin
  324. { ... and reserve one for the pointer }
  325. hr:=getregister32;
  326. emit_reg_reg(A_MOV,S_L,left.location.register,hr);
  327. location.reference.base:=hr;
  328. end;
  329. else
  330. begin
  331. { free register }
  332. del_reference(left.location.reference);
  333. { ... and reserve one for the pointer }
  334. hr:=getregister32;
  335. emit_ref_reg(
  336. A_MOV,S_L,newreference(left.location.reference),
  337. hr);
  338. location.reference.base:=hr;
  339. end;
  340. end;
  341. end
  342. else if is_interfacecom(left.resulttype) then
  343. begin
  344. gettempintfcomreference(location.reference);
  345. emit_mov_loc_ref(left.location,location.reference,S_L,false);
  346. end
  347. else
  348. set_location(location,left.location);
  349. inc(location.reference.offset,vs^.address);
  350. end;
  351. {*****************************************************************************
  352. TI386VECNODE
  353. *****************************************************************************}
  354. procedure ti386vecnode.pass_2;
  355. var
  356. is_pushed : boolean;
  357. ind,hr : tregister;
  358. //_p : tnode;
  359. function get_mul_size:longint;
  360. begin
  361. if nf_memindex in flags then
  362. get_mul_size:=1
  363. else
  364. begin
  365. if (left.resulttype^.deftype=arraydef) then
  366. get_mul_size:=parraydef(left.resulttype)^.elesize
  367. else
  368. get_mul_size:=resulttype^.size;
  369. end
  370. end;
  371. procedure calc_emit_mul;
  372. var
  373. l1,l2 : longint;
  374. begin
  375. l1:=get_mul_size;
  376. case l1 of
  377. 1,2,4,8 : location.reference.scalefactor:=l1;
  378. else
  379. begin
  380. if ispowerof2(l1,l2) then
  381. emit_const_reg(A_SHL,S_L,l2,ind)
  382. else
  383. emit_const_reg(A_IMUL,S_L,l1,ind);
  384. end;
  385. end;
  386. end;
  387. var
  388. extraoffset : longint;
  389. { rl stores the resulttype of the left node, this is necessary }
  390. { to detect if it is an ansistring }
  391. { because in constant nodes which constant index }
  392. { the left tree is removed }
  393. t : tnode;
  394. hp : preference;
  395. href : treference;
  396. tai : Paicpu;
  397. pushed : tpushed;
  398. hightree : tnode;
  399. hl,otl,ofl : pasmlabel;
  400. begin
  401. secondpass(left);
  402. { we load the array reference to location }
  403. { an ansistring needs to be dereferenced }
  404. if is_ansistring(left.resulttype) or
  405. is_widestring(left.resulttype) then
  406. begin
  407. reset_reference(location.reference);
  408. if nf_callunique in flags then
  409. begin
  410. if left.location.loc<>LOC_REFERENCE then
  411. begin
  412. CGMessage(cg_e_illegal_expression);
  413. exit;
  414. end;
  415. pushusedregisters(pushed,$ff);
  416. emitpushreferenceaddr(left.location.reference);
  417. if is_ansistring(left.resulttype) then
  418. emitcall('FPC_ANSISTR_UNIQUE')
  419. else
  420. emitcall('FPC_WIDESTR_UNIQUE');
  421. maybe_loadesi;
  422. popusedregisters(pushed);
  423. end;
  424. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  425. begin
  426. location.reference.base:=left.location.register;
  427. end
  428. else
  429. begin
  430. del_reference(left.location.reference);
  431. location.reference.base:=getregister32;
  432. emit_ref_reg(A_MOV,S_L,
  433. newreference(left.location.reference),
  434. location.reference.base);
  435. end;
  436. { check for a zero length string,
  437. we can use the ansistring routine here }
  438. if (cs_check_range in aktlocalswitches) then
  439. begin
  440. pushusedregisters(pushed,$ff);
  441. emit_reg(A_PUSH,S_L,location.reference.base);
  442. emitcall('FPC_ANSISTR_CHECKZERO');
  443. maybe_loadesi;
  444. popusedregisters(pushed);
  445. end;
  446. if is_ansistring(left.resulttype) then
  447. { in ansistrings S[1] is pchar(S)[0] !! }
  448. dec(location.reference.offset)
  449. else
  450. begin
  451. { in widestrings S[1] is pwchar(S)[0] !! }
  452. dec(location.reference.offset,2);
  453. emit_const_reg(A_SHL,S_L,
  454. 1,location.reference.base);
  455. end;
  456. { we've also to keep left up-to-date, because it is used }
  457. { if a constant array index occurs, subject to change (FK) }
  458. set_location(left.location,location);
  459. end
  460. else if is_dynamic_array(left.resulttype) then
  461. { ... also a dynamic string }
  462. begin
  463. reset_reference(location.reference);
  464. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  465. begin
  466. location.reference.base:=left.location.register;
  467. end
  468. else
  469. begin
  470. del_reference(left.location.reference);
  471. location.reference.base:=getregister32;
  472. emit_ref_reg(A_MOV,S_L,
  473. newreference(left.location.reference),
  474. location.reference.base);
  475. end;
  476. {$warning FIXME}
  477. { check for a zero length string,
  478. we can use the ansistring routine here }
  479. if (cs_check_range in aktlocalswitches) then
  480. begin
  481. pushusedregisters(pushed,$ff);
  482. emit_reg(A_PUSH,S_L,location.reference.base);
  483. emitcall('FPC_ANSISTR_CHECKZERO');
  484. maybe_loadesi;
  485. popusedregisters(pushed);
  486. end;
  487. { we've also to keep left up-to-date, because it is used }
  488. { if a constant array index occurs, subject to change (FK) }
  489. set_location(left.location,location);
  490. end
  491. else
  492. set_location(location,left.location);
  493. { offset can only differ from 0 if arraydef }
  494. if (left.resulttype^.deftype=arraydef) and
  495. not(is_dynamic_array(left.resulttype)) then
  496. dec(location.reference.offset,
  497. get_mul_size*parraydef(left.resulttype)^.lowrange);
  498. if right.nodetype=ordconstn then
  499. begin
  500. { offset can only differ from 0 if arraydef }
  501. if (left.resulttype^.deftype=arraydef) then
  502. begin
  503. if not(is_open_array(left.resulttype)) and
  504. not(is_array_of_const(left.resulttype)) and
  505. not(is_dynamic_array(left.resulttype)) then
  506. begin
  507. if (tordconstnode(right).value>parraydef(left.resulttype)^.highrange) or
  508. (tordconstnode(right).value<parraydef(left.resulttype)^.lowrange) then
  509. begin
  510. if (cs_check_range in aktlocalswitches) then
  511. CGMessage(parser_e_range_check_error)
  512. else
  513. CGMessage(parser_w_range_check_error);
  514. end;
  515. dec(left.location.reference.offset,
  516. get_mul_size*parraydef(left.resulttype)^.lowrange);
  517. end
  518. else
  519. begin
  520. { range checking for open and dynamic arrays !!!! }
  521. {$warning FIXME}
  522. {!!!!!!!!!!!!!!!!!}
  523. end;
  524. end
  525. else if (left.resulttype^.deftype=stringdef) then
  526. begin
  527. if (tordconstnode(right).value=0) and not(is_shortstring(left.resulttype)) then
  528. CGMessage(cg_e_can_access_element_zero);
  529. if (cs_check_range in aktlocalswitches) then
  530. case pstringdef(left.resulttype)^.string_typ of
  531. { it's the same for ansi- and wide strings }
  532. st_widestring,
  533. st_ansistring:
  534. begin
  535. pushusedregisters(pushed,$ff);
  536. push_int(tordconstnode(right).value);
  537. hp:=newreference(location.reference);
  538. dec(hp^.offset,7);
  539. emit_ref(A_PUSH,S_L,hp);
  540. emitcall('FPC_ANSISTR_RANGECHECK');
  541. popusedregisters(pushed);
  542. maybe_loadesi;
  543. end;
  544. st_shortstring:
  545. begin
  546. {!!!!!!!!!!!!!!!!!}
  547. end;
  548. st_longstring:
  549. begin
  550. {!!!!!!!!!!!!!!!!!}
  551. end;
  552. end;
  553. end;
  554. inc(left.location.reference.offset,
  555. get_mul_size*tordconstnode(right).value);
  556. if nf_memseg in flags then
  557. left.location.reference.segment:=R_FS;
  558. {
  559. left.resulttype:=resulttype;
  560. disposetree(right);
  561. _p:=left;
  562. putnode(p);
  563. p:=_p;
  564. }
  565. set_location(location,left.location);
  566. end
  567. else
  568. { not nodetype=ordconstn }
  569. begin
  570. if (cs_regalloc in aktglobalswitches) and
  571. { if we do range checking, we don't }
  572. { need that fancy code (it would be }
  573. { buggy) }
  574. not(cs_check_range in aktlocalswitches) and
  575. (left.resulttype^.deftype=arraydef) then
  576. begin
  577. extraoffset:=0;
  578. if (right.nodetype=addn) then
  579. begin
  580. if taddnode(right).right.nodetype=ordconstn then
  581. begin
  582. extraoffset:=tordconstnode(taddnode(right).right).value;
  583. t:=taddnode(right).left;
  584. { First pass processed this with the assumption }
  585. { that there was an add node which may require an }
  586. { extra register. Fake it or die with IE10 (JM) }
  587. t.registers32 := taddnode(right).registers32;
  588. taddnode(right).left:=nil;
  589. right.free;
  590. right:=t;
  591. end
  592. else if tordconstnode(taddnode(right).left).nodetype=ordconstn then
  593. begin
  594. extraoffset:=tordconstnode(taddnode(right).left).value;
  595. t:=taddnode(right).right;
  596. t.registers32 := right.registers32;
  597. taddnode(right).right:=nil;
  598. right.free;
  599. right:=t;
  600. end;
  601. end
  602. else if (right.nodetype=subn) then
  603. begin
  604. if taddnode(right).right.nodetype=ordconstn then
  605. begin
  606. { this was "extraoffset:=right.right.value;" Looks a bit like
  607. copy-paste bug :) (JM) }
  608. extraoffset:=-tordconstnode(taddnode(right).right).value;
  609. t:=taddnode(right).left;
  610. t.registers32 := right.registers32;
  611. taddnode(right).left:=nil;
  612. right.free;
  613. right:=t;
  614. end
  615. { You also have to negate right.right in this case! I can't add an
  616. unaryminusn without causing a crash, so I've disabled it (JM)
  617. else if right.left.nodetype=ordconstn then
  618. begin
  619. extraoffset:=right.left.value;
  620. t:=right.right;
  621. t^.registers32 := right.registers32;
  622. putnode(right);
  623. putnode(right.left);
  624. right:=t;
  625. end;}
  626. end;
  627. inc(location.reference.offset,
  628. get_mul_size*extraoffset);
  629. end;
  630. { calculate from left to right }
  631. if (location.loc<>LOC_REFERENCE) and
  632. (location.loc<>LOC_MEM) then
  633. CGMessage(cg_e_illegal_expression);
  634. if (right.location.loc=LOC_JUMP) then
  635. begin
  636. otl:=truelabel;
  637. getlabel(truelabel);
  638. ofl:=falselabel;
  639. getlabel(falselabel);
  640. end;
  641. is_pushed:=maybe_push(right.registers32,self,false);
  642. secondpass(right);
  643. if is_pushed then
  644. restore(self,false);
  645. { here we change the location of right
  646. and the update was forgotten so it
  647. led to wrong code in emitrangecheck later PM
  648. so make range check before }
  649. if cs_check_range in aktlocalswitches then
  650. begin
  651. if left.resulttype^.deftype=arraydef then
  652. begin
  653. if is_open_array(left.resulttype) or
  654. is_array_of_const(left.resulttype) then
  655. begin
  656. reset_reference(href);
  657. parraydef(left.resulttype)^.genrangecheck;
  658. href.symbol:=newasmsymbol(parraydef(left.resulttype)^.getrangecheckstring);
  659. href.offset:=4;
  660. getsymonlyin(tloadnode(left).symtable,
  661. 'high'+pvarsym(tloadnode(left).symtableentry)^.name);
  662. hightree:=genloadnode(pvarsym(srsym),tloadnode(left).symtable);
  663. firstpass(hightree);
  664. secondpass(hightree);
  665. emit_mov_loc_ref(hightree.location,href,S_L,true);
  666. hightree.free;
  667. hightree:=nil;
  668. end;
  669. emitrangecheck(right,left.resulttype);
  670. end;
  671. end;
  672. case right.location.loc of
  673. LOC_REGISTER:
  674. begin
  675. ind:=right.location.register;
  676. case right.resulttype^.size of
  677. 1:
  678. begin
  679. hr:=reg8toreg32(ind);
  680. emit_reg_reg(A_MOVZX,S_BL,ind,hr);
  681. ind:=hr;
  682. end;
  683. 2:
  684. begin
  685. hr:=reg16toreg32(ind);
  686. emit_reg_reg(A_MOVZX,S_WL,ind,hr);
  687. ind:=hr;
  688. end;
  689. end;
  690. end;
  691. LOC_CREGISTER:
  692. begin
  693. ind:=getregister32;
  694. case right.resulttype^.size of
  695. 1:
  696. emit_reg_reg(A_MOVZX,S_BL,right.location.register,ind);
  697. 2:
  698. emit_reg_reg(A_MOVZX,S_WL,right.location.register,ind);
  699. 4:
  700. emit_reg_reg(A_MOV,S_L,right.location.register,ind);
  701. end;
  702. end;
  703. LOC_FLAGS:
  704. begin
  705. ind:=getregister32;
  706. emit_flag2reg(right.location.resflags,reg32toreg8(ind));
  707. emit_reg_reg(A_MOVZX,S_BL,reg32toreg8(ind),ind);
  708. end;
  709. LOC_JUMP :
  710. begin
  711. ind:=getregister32;
  712. emitlab(truelabel);
  713. truelabel:=otl;
  714. emit_const_reg(A_MOV,S_L,1,ind);
  715. getlabel(hl);
  716. emitjmp(C_None,hl);
  717. emitlab(falselabel);
  718. falselabel:=ofl;
  719. emit_reg_reg(A_XOR,S_L,ind,ind);
  720. emitlab(hl);
  721. end;
  722. LOC_REFERENCE,LOC_MEM :
  723. begin
  724. del_reference(right.location.reference);
  725. ind:=getregister32;
  726. { Booleans are stored in an 8 bit memory location, so
  727. the use of MOVL is not correct }
  728. case right.resulttype^.size of
  729. 1 : tai:=new(paicpu,op_ref_reg(A_MOVZX,S_BL,newreference(right.location.reference),ind));
  730. 2 : tai:=new(Paicpu,op_ref_reg(A_MOVZX,S_WL,newreference(right.location.reference),ind));
  731. 4 : tai:=new(Paicpu,op_ref_reg(A_MOV,S_L,newreference(right.location.reference),ind));
  732. end;
  733. exprasmlist^.concat(tai);
  734. end;
  735. else
  736. internalerror(5913428);
  737. end;
  738. { produce possible range check code: }
  739. if cs_check_range in aktlocalswitches then
  740. begin
  741. if left.resulttype^.deftype=arraydef then
  742. begin
  743. { done defore (PM) }
  744. end
  745. else if (left.resulttype^.deftype=stringdef) then
  746. begin
  747. case pstringdef(left.resulttype)^.string_typ of
  748. { it's the same for ansi- and wide strings }
  749. st_widestring,
  750. st_ansistring:
  751. begin
  752. pushusedregisters(pushed,$ff);
  753. emit_reg(A_PUSH,S_L,ind);
  754. hp:=newreference(location.reference);
  755. dec(hp^.offset,7);
  756. emit_ref(A_PUSH,S_L,hp);
  757. emitcall('FPC_ANSISTR_RANGECHECK');
  758. popusedregisters(pushed);
  759. maybe_loadesi;
  760. end;
  761. st_shortstring:
  762. begin
  763. {!!!!!!!!!!!!!!!!!}
  764. end;
  765. st_longstring:
  766. begin
  767. {!!!!!!!!!!!!!!!!!}
  768. end;
  769. end;
  770. end;
  771. end;
  772. if location.reference.index=R_NO then
  773. begin
  774. location.reference.index:=ind;
  775. calc_emit_mul;
  776. end
  777. else
  778. begin
  779. if location.reference.base=R_NO then
  780. begin
  781. case location.reference.scalefactor of
  782. 2 : emit_const_reg(A_SHL,S_L,1,location.reference.index);
  783. 4 : emit_const_reg(A_SHL,S_L,2,location.reference.index);
  784. 8 : emit_const_reg(A_SHL,S_L,3,location.reference.index);
  785. end;
  786. calc_emit_mul;
  787. location.reference.base:=location.reference.index;
  788. location.reference.index:=ind;
  789. end
  790. else
  791. begin
  792. emit_ref_reg(
  793. A_LEA,S_L,newreference(location.reference),
  794. location.reference.index);
  795. ungetregister32(location.reference.base);
  796. { the symbol offset is loaded, }
  797. { so release the symbol name and set symbol }
  798. { to nil }
  799. location.reference.symbol:=nil;
  800. location.reference.offset:=0;
  801. calc_emit_mul;
  802. location.reference.base:=location.reference.index;
  803. location.reference.index:=ind;
  804. end;
  805. end;
  806. if nf_memseg in flags then
  807. location.reference.segment:=R_FS;
  808. end;
  809. end;
  810. {*****************************************************************************
  811. TI386SELFNODE
  812. *****************************************************************************}
  813. procedure ti386selfnode.pass_2;
  814. begin
  815. reset_reference(location.reference);
  816. getexplicitregister32(R_ESI);
  817. if (resulttype^.deftype=classrefdef) or
  818. is_class(resulttype) then
  819. location.register:=R_ESI
  820. else
  821. location.reference.base:=R_ESI;
  822. end;
  823. {*****************************************************************************
  824. TI386WITHNODE
  825. *****************************************************************************}
  826. procedure ti386withnode.pass_2;
  827. var
  828. usetemp,with_expr_in_temp : boolean;
  829. {$ifdef GDB}
  830. withstartlabel,withendlabel : pasmlabel;
  831. pp : pchar;
  832. mangled_length : longint;
  833. const
  834. withlevel : longint = 0;
  835. {$endif GDB}
  836. begin
  837. if assigned(left) then
  838. begin
  839. secondpass(left);
  840. if left.location.reference.segment<>R_NO then
  841. message(parser_e_no_with_for_variable_in_other_segments);
  842. new(withreference);
  843. usetemp:=false;
  844. if (left.nodetype=loadn) and
  845. (tloadnode(left).symtable=aktprocsym^.definition^.localst) then
  846. begin
  847. { for locals use the local storage }
  848. withreference^:=left.location.reference;
  849. include(flags,nf_islocal);
  850. end
  851. else
  852. { call can have happend with a property }
  853. if is_class_or_interface(left.resulttype) then
  854. begin
  855. {$ifndef noAllocEdi}
  856. getexplicitregister32(R_EDI);
  857. {$endif noAllocEdi}
  858. emit_mov_loc_reg(left.location,R_EDI);
  859. usetemp:=true;
  860. end
  861. else
  862. begin
  863. {$ifndef noAllocEdi}
  864. getexplicitregister32(R_EDI);
  865. {$endif noAllocEdi}
  866. emit_lea_loc_reg(left.location,R_EDI,false);
  867. usetemp:=true;
  868. end;
  869. release_loc(left.location);
  870. { if the with expression is stored in a temp }
  871. { area we must make it persistent and shouldn't }
  872. { release it (FK) }
  873. if (left.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  874. istemp(left.location.reference) then
  875. begin
  876. normaltemptopersistant(left.location.reference.offset);
  877. with_expr_in_temp:=true;
  878. end
  879. else
  880. with_expr_in_temp:=false;
  881. { if usetemp is set the value must be in %edi }
  882. if usetemp then
  883. begin
  884. gettempofsizereference(4,withreference^);
  885. normaltemptopersistant(withreference^.offset);
  886. { move to temp reference }
  887. emit_reg_ref(A_MOV,S_L,R_EDI,newreference(withreference^));
  888. {$ifndef noAllocEdi}
  889. ungetregister32(R_EDI);
  890. {$endif noAllocEdi}
  891. {$ifdef GDB}
  892. if (cs_debuginfo in aktmoduleswitches) then
  893. begin
  894. inc(withlevel);
  895. getaddrlabel(withstartlabel);
  896. getaddrlabel(withendlabel);
  897. emitlab(withstartlabel);
  898. withdebuglist^.concat(new(pai_stabs,init(strpnew(
  899. '"with'+tostr(withlevel)+':'+tostr(symtablestack^.getnewtypecount)+
  900. '=*'+pstoreddef(left.resulttype)^.numberstring+'",'+
  901. tostr(N_LSYM)+',0,0,'+tostr(withreference^.offset)))));
  902. mangled_length:=length(aktprocsym^.definition^.mangledname);
  903. getmem(pp,mangled_length+50);
  904. strpcopy(pp,'192,0,0,'+withstartlabel^.name);
  905. if (target_os.use_function_relative_addresses) then
  906. begin
  907. strpcopy(strend(pp),'-');
  908. strpcopy(strend(pp),aktprocsym^.definition^.mangledname);
  909. end;
  910. withdebuglist^.concat(new(pai_stabn,init(strnew(pp))));
  911. end;
  912. {$endif GDB}
  913. end;
  914. { right can be optimize out !!! }
  915. if assigned(right) then
  916. secondpass(right);
  917. if usetemp then
  918. begin
  919. ungetpersistanttemp(withreference^.offset);
  920. {$ifdef GDB}
  921. if (cs_debuginfo in aktmoduleswitches) then
  922. begin
  923. emitlab(withendlabel);
  924. strpcopy(pp,'224,0,0,'+withendlabel^.name);
  925. if (target_os.use_function_relative_addresses) then
  926. begin
  927. strpcopy(strend(pp),'-');
  928. strpcopy(strend(pp),aktprocsym^.definition^.mangledname);
  929. end;
  930. withdebuglist^.concat(new(pai_stabn,init(strnew(pp))));
  931. freemem(pp,mangled_length+50);
  932. dec(withlevel);
  933. end;
  934. {$endif GDB}
  935. end;
  936. if with_expr_in_temp then
  937. ungetpersistanttemp(left.location.reference.offset);
  938. dispose(withreference);
  939. withreference:=nil;
  940. end;
  941. end;
  942. begin
  943. cloadvmtnode:=ti386loadvmtnode;
  944. chnewnode:=ti386hnewnode;
  945. cnewnode:=ti386newnode;
  946. chdisposenode:=ti386hdisposenode;
  947. csimplenewdisposenode:=ti386simplenewdisposenode;
  948. caddrnode:=ti386addrnode;
  949. cdoubleaddrnode:=ti386doubleaddrnode;
  950. cderefnode:=ti386derefnode;
  951. csubscriptnode:=ti386subscriptnode;
  952. cvecnode:=ti386vecnode;
  953. cselfnode:=ti386selfnode;
  954. cwithnode:=ti386withnode;
  955. end.
  956. {
  957. $Log$
  958. Revision 1.5 2000-11-04 14:25:24 florian
  959. + merged Attila's changes for interfaces, not tested yet
  960. Revision 1.4 2000/10/31 22:02:57 peter
  961. * symtable splitted, no real code changes
  962. Revision 1.3 2000/10/31 14:18:53 jonas
  963. * merged double deleting of left location when using a temp in
  964. secondwith (merged from fixes branch). This also fixes web bug1194
  965. Revision 1.2 2000/10/21 18:16:13 florian
  966. * a lot of changes:
  967. - basic dyn. array support
  968. - basic C++ support
  969. - some work for interfaces done
  970. ....
  971. Revision 1.1 2000/10/15 09:33:32 peter
  972. * moved n386*.pas to i386/ cpu_target dir
  973. Revision 1.2 2000/10/14 21:52:54 peter
  974. * fixed memory leaks
  975. Revision 1.1 2000/10/14 10:14:49 peter
  976. * moehrendorf oct 2000 rewrite
  977. }