cg386mem.pas 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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 cg386mem;
  19. interface
  20. uses
  21. tree;
  22. procedure secondloadvmt(var p : ptree);
  23. procedure secondhnewn(var p : ptree);
  24. procedure secondnewn(var p : ptree);
  25. procedure secondhdisposen(var p : ptree);
  26. procedure secondsimplenewdispose(var p : ptree);
  27. procedure secondaddr(var p : ptree);
  28. procedure seconddoubleaddr(var p : ptree);
  29. procedure secondderef(var p : ptree);
  30. procedure secondsubscriptn(var p : ptree);
  31. procedure secondvecn(var p : ptree);
  32. procedure secondselfn(var p : ptree);
  33. procedure secondwith(var p : ptree);
  34. implementation
  35. uses
  36. {$ifdef GDB}
  37. strings,gdb,
  38. {$endif GDB}
  39. globtype,systems,
  40. cobjects,verbose,globals,
  41. symconst,symtable,aasm,types,
  42. hcodegen,temp_gen,pass_2,pass_1,
  43. cpubase,cpuasm,
  44. cgai386,tgeni386;
  45. {*****************************************************************************
  46. SecondLoadVMT
  47. *****************************************************************************}
  48. procedure secondloadvmt(var p : ptree);
  49. begin
  50. p^.location.register:=getregister32;
  51. emit_sym_ofs_reg(A_MOV,
  52. S_L,newasmsymbol(pobjectdef(pclassrefdef(p^.resulttype)^.pointertype.def)^.vmt_mangledname),0,
  53. p^.location.register);
  54. end;
  55. {*****************************************************************************
  56. SecondHNewN
  57. *****************************************************************************}
  58. procedure secondhnewn(var p : ptree);
  59. begin
  60. end;
  61. {*****************************************************************************
  62. SecondNewN
  63. *****************************************************************************}
  64. procedure secondnewn(var p : ptree);
  65. var
  66. pushed : tpushed;
  67. r : preference;
  68. begin
  69. if assigned(p^.left) then
  70. begin
  71. secondpass(p^.left);
  72. p^.location.register:=p^.left^.location.register;
  73. end
  74. else
  75. begin
  76. pushusedregisters(pushed,$ff);
  77. gettempofsizereference(target_os.size_of_pointer,p^.location.reference);
  78. { determines the size of the mem block }
  79. push_int(ppointerdef(p^.resulttype)^.pointertype.def^.size);
  80. emit_push_lea_loc(p^.location,false);
  81. emitcall('FPC_GETMEM');
  82. if ppointerdef(p^.resulttype)^.pointertype.def^.needs_inittable then
  83. begin
  84. new(r);
  85. reset_reference(r^);
  86. r^.symbol:=ppointerdef(p^.left^.resulttype)^.pointertype.def^.get_inittable_label;
  87. emitpushreferenceaddr(r^);
  88. dispose(r);
  89. { push pointer we just allocated, we need to initialize the
  90. data located at that pointer not the pointer self (PFV) }
  91. emit_push_loc(p^.location);
  92. emitcall('FPC_INITIALIZE');
  93. end;
  94. popusedregisters(pushed);
  95. { may be load ESI }
  96. maybe_loadesi;
  97. end;
  98. if codegenerror then
  99. exit;
  100. end;
  101. {*****************************************************************************
  102. SecondDisposeN
  103. *****************************************************************************}
  104. procedure secondhdisposen(var p : ptree);
  105. begin
  106. secondpass(p^.left);
  107. if codegenerror then
  108. exit;
  109. reset_reference(p^.location.reference);
  110. case p^.left^.location.loc of
  111. LOC_REGISTER:
  112. p^.location.reference.index:=p^.left^.location.register;
  113. LOC_CREGISTER:
  114. begin
  115. p^.location.reference.index:=getregister32;
  116. emit_reg_reg(A_MOV,S_L,
  117. p^.left^.location.register,
  118. p^.location.reference.index);
  119. end;
  120. LOC_MEM,LOC_REFERENCE :
  121. begin
  122. del_reference(p^.left^.location.reference);
  123. p^.location.reference.index:=getregister32;
  124. emit_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  125. p^.location.reference.index);
  126. end;
  127. end;
  128. end;
  129. {*****************************************************************************
  130. SecondNewDispose
  131. *****************************************************************************}
  132. procedure secondsimplenewdispose(var p : ptree);
  133. var
  134. pushed : tpushed;
  135. r : preference;
  136. begin
  137. secondpass(p^.left);
  138. if codegenerror then
  139. exit;
  140. pushusedregisters(pushed,$ff);
  141. { call the mem handling procedures }
  142. case p^.treetype of
  143. simpledisposen:
  144. begin
  145. if ppointerdef(p^.left^.resulttype)^.pointertype.def^.needs_inittable then
  146. begin
  147. new(r);
  148. reset_reference(r^);
  149. r^.symbol:=ppointerdef(p^.left^.resulttype)^.pointertype.def^.get_inittable_label;
  150. emitpushreferenceaddr(r^);
  151. dispose(r);
  152. { push pointer adress }
  153. emit_push_loc(p^.left^.location);
  154. emitcall('FPC_FINALIZE');
  155. end;
  156. emit_push_lea_loc(p^.left^.location,true);
  157. emitcall('FPC_FREEMEM');
  158. end;
  159. simplenewn:
  160. begin
  161. { determines the size of the mem block }
  162. push_int(ppointerdef(p^.left^.resulttype)^.pointertype.def^.size);
  163. emit_push_lea_loc(p^.left^.location,true);
  164. emitcall('FPC_GETMEM');
  165. if ppointerdef(p^.left^.resulttype)^.pointertype.def^.needs_inittable then
  166. begin
  167. new(r);
  168. reset_reference(r^);
  169. r^.symbol:=ppointerdef(p^.left^.resulttype)^.pointertype.def^.get_inittable_label;
  170. emitpushreferenceaddr(r^);
  171. dispose(r);
  172. emit_push_loc(p^.left^.location);
  173. emitcall('FPC_INITIALIZE');
  174. end;
  175. end;
  176. end;
  177. popusedregisters(pushed);
  178. { may be load ESI }
  179. maybe_loadesi;
  180. end;
  181. {*****************************************************************************
  182. SecondAddr
  183. *****************************************************************************}
  184. procedure secondaddr(var p : ptree);
  185. begin
  186. secondpass(p^.left);
  187. { when loading procvar we do nothing with this node, so load the
  188. location of left }
  189. if p^.procvarload then
  190. begin
  191. set_location(p^.location,p^.left^.location);
  192. exit;
  193. end;
  194. p^.location.loc:=LOC_REGISTER;
  195. del_reference(p^.left^.location.reference);
  196. p^.location.register:=getregister32;
  197. {@ on a procvar means returning an address to the procedure that
  198. is stored in it.}
  199. { yes but p^.left^.symtableentry can be nil
  200. for example on @self !! }
  201. { symtableentry can be also invalid, if left is no tree node }
  202. if (m_tp_procvar in aktmodeswitches) and
  203. (p^.left^.treetype=loadn) and
  204. assigned(p^.left^.symtableentry) and
  205. (p^.left^.symtableentry^.typ=varsym) and
  206. (pvarsym(p^.left^.symtableentry)^.vartype.def^.deftype=procvardef) then
  207. emit_ref_reg(A_MOV,S_L,
  208. newreference(p^.left^.location.reference),
  209. p^.location.register)
  210. else
  211. emit_ref_reg(A_LEA,S_L,
  212. newreference(p^.left^.location.reference),
  213. p^.location.register);
  214. { for use of other segments }
  215. if p^.left^.location.reference.segment<>R_NO then
  216. p^.location.segment:=p^.left^.location.reference.segment;
  217. end;
  218. {*****************************************************************************
  219. SecondDoubleAddr
  220. *****************************************************************************}
  221. procedure seconddoubleaddr(var p : ptree);
  222. begin
  223. secondpass(p^.left);
  224. p^.location.loc:=LOC_REGISTER;
  225. del_reference(p^.left^.location.reference);
  226. p^.location.register:=getregister32;
  227. emit_ref_reg(A_LEA,S_L,
  228. newreference(p^.left^.location.reference),
  229. p^.location.register);
  230. end;
  231. {*****************************************************************************
  232. SecondDeRef
  233. *****************************************************************************}
  234. procedure secondderef(var p : ptree);
  235. var
  236. hr : tregister;
  237. begin
  238. secondpass(p^.left);
  239. reset_reference(p^.location.reference);
  240. case p^.left^.location.loc of
  241. LOC_REGISTER:
  242. p^.location.reference.base:=p^.left^.location.register;
  243. LOC_CREGISTER:
  244. begin
  245. { ... and reserve one for the pointer }
  246. hr:=getregister32;
  247. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hr);
  248. p^.location.reference.base:=hr;
  249. end;
  250. else
  251. begin
  252. { free register }
  253. del_reference(p^.left^.location.reference);
  254. { ...and reserve one for the pointer }
  255. hr:=getregister32;
  256. emit_ref_reg(
  257. A_MOV,S_L,newreference(p^.left^.location.reference),
  258. hr);
  259. p^.location.reference.base:=hr;
  260. end;
  261. end;
  262. if ppointerdef(p^.left^.resulttype)^.is_far then
  263. p^.location.reference.segment:=R_FS;
  264. if not ppointerdef(p^.left^.resulttype)^.is_far and
  265. (cs_gdb_heaptrc in aktglobalswitches) and
  266. (cs_checkpointer in aktglobalswitches) then
  267. begin
  268. emit_reg(
  269. A_PUSH,S_L,p^.location.reference.base);
  270. emitcall('FPC_CHECKPOINTER');
  271. end;
  272. end;
  273. {*****************************************************************************
  274. SecondSubScriptN
  275. *****************************************************************************}
  276. procedure secondsubscriptn(var p : ptree);
  277. var
  278. hr : tregister;
  279. begin
  280. secondpass(p^.left);
  281. if codegenerror then
  282. exit;
  283. { classes must be dereferenced implicit }
  284. if (p^.left^.resulttype^.deftype=objectdef) and
  285. pobjectdef(p^.left^.resulttype)^.is_class then
  286. begin
  287. reset_reference(p^.location.reference);
  288. case p^.left^.location.loc of
  289. LOC_REGISTER:
  290. p^.location.reference.base:=p^.left^.location.register;
  291. LOC_CREGISTER:
  292. begin
  293. { ... and reserve one for the pointer }
  294. hr:=getregister32;
  295. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hr);
  296. p^.location.reference.base:=hr;
  297. end;
  298. else
  299. begin
  300. { free register }
  301. del_reference(p^.left^.location.reference);
  302. { ... and reserve one for the pointer }
  303. hr:=getregister32;
  304. emit_ref_reg(
  305. A_MOV,S_L,newreference(p^.left^.location.reference),
  306. hr);
  307. p^.location.reference.base:=hr;
  308. end;
  309. end;
  310. end
  311. else
  312. set_location(p^.location,p^.left^.location);
  313. inc(p^.location.reference.offset,p^.vs^.address);
  314. end;
  315. {*****************************************************************************
  316. SecondVecN
  317. *****************************************************************************}
  318. procedure secondvecn(var p : ptree);
  319. var
  320. is_pushed : boolean;
  321. ind,hr : tregister;
  322. _p : ptree;
  323. function get_mul_size:longint;
  324. begin
  325. if p^.memindex then
  326. get_mul_size:=1
  327. else
  328. get_mul_size:=p^.resulttype^.size;
  329. end;
  330. procedure calc_emit_mul;
  331. var
  332. l1,l2 : longint;
  333. begin
  334. l1:=get_mul_size;
  335. case l1 of
  336. 1,2,4,8 : p^.location.reference.scalefactor:=l1;
  337. else
  338. begin
  339. if ispowerof2(l1,l2) then
  340. emit_const_reg(A_SHL,S_L,l2,ind)
  341. else
  342. emit_const_reg(A_IMUL,S_L,l1,ind);
  343. end;
  344. end;
  345. end;
  346. var
  347. extraoffset : longint;
  348. { rl stores the resulttype of the left node, this is necessary }
  349. { to detect if it is an ansistring }
  350. { because in constant nodes which constant index }
  351. { the left tree is removed }
  352. t : ptree;
  353. hp : preference;
  354. href : treference;
  355. tai : Paicpu;
  356. pushed : tpushed;
  357. hightree : ptree;
  358. hl,otl,ofl : pasmlabel;
  359. begin
  360. secondpass(p^.left);
  361. { we load the array reference to p^.location }
  362. { an ansistring needs to be dereferenced }
  363. if is_ansistring(p^.left^.resulttype) or
  364. is_widestring(p^.left^.resulttype) then
  365. begin
  366. reset_reference(p^.location.reference);
  367. if p^.callunique then
  368. begin
  369. if p^.left^.location.loc<>LOC_REFERENCE then
  370. begin
  371. CGMessage(cg_e_illegal_expression);
  372. exit;
  373. end;
  374. pushusedregisters(pushed,$ff);
  375. emitpushreferenceaddr(p^.left^.location.reference);
  376. if is_ansistring(p^.left^.resulttype) then
  377. emitcall('FPC_ANSISTR_UNIQUE')
  378. else
  379. emitcall('FPC_WIDESTR_UNIQUE');
  380. maybe_loadesi;
  381. popusedregisters(pushed);
  382. end;
  383. if p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  384. begin
  385. p^.location.reference.base:=p^.left^.location.register;
  386. end
  387. else
  388. begin
  389. del_reference(p^.left^.location.reference);
  390. p^.location.reference.base:=getregister32;
  391. emit_ref_reg(A_MOV,S_L,
  392. newreference(p^.left^.location.reference),
  393. p^.location.reference.base);
  394. end;
  395. { check for a zero length string,
  396. we can use the ansistring routine here }
  397. if (cs_check_range in aktlocalswitches) then
  398. begin
  399. pushusedregisters(pushed,$ff);
  400. emit_reg(A_PUSH,S_L,p^.location.reference.base);
  401. emitcall('FPC_ANSISTR_CHECKZERO');
  402. maybe_loadesi;
  403. popusedregisters(pushed);
  404. end;
  405. if is_ansistring(p^.left^.resulttype) then
  406. { in ansistrings S[1] is pchar(S)[0] !! }
  407. dec(p^.location.reference.offset)
  408. else
  409. begin
  410. { in widestrings S[1] is pwchar(S)[0] !! }
  411. dec(p^.location.reference.offset,2);
  412. emit_const_reg(A_SHL,S_L,
  413. 1,p^.location.reference.base);
  414. end;
  415. { we've also to keep left up-to-date, because it is used }
  416. { if a constant array index occurs, subject to change (FK) }
  417. set_location(p^.left^.location,p^.location);
  418. end
  419. else
  420. set_location(p^.location,p^.left^.location);
  421. { offset can only differ from 0 if arraydef }
  422. if p^.left^.resulttype^.deftype=arraydef then
  423. dec(p^.location.reference.offset,
  424. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  425. if p^.right^.treetype=ordconstn then
  426. begin
  427. { offset can only differ from 0 if arraydef }
  428. if (p^.left^.resulttype^.deftype=arraydef) then
  429. begin
  430. if not(is_open_array(p^.left^.resulttype)) and
  431. not(is_array_of_const(p^.left^.resulttype)) then
  432. begin
  433. if (p^.right^.value>parraydef(p^.left^.resulttype)^.highrange) or
  434. (p^.right^.value<parraydef(p^.left^.resulttype)^.lowrange) then
  435. begin
  436. if (cs_check_range in aktlocalswitches) then
  437. CGMessage(parser_e_range_check_error)
  438. else
  439. CGMessage(parser_w_range_check_error);
  440. end;
  441. dec(p^.left^.location.reference.offset,
  442. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  443. end
  444. else
  445. begin
  446. { range checking for open arrays !!!! }
  447. {!!!!!!!!!!!!!!!!!}
  448. end;
  449. end
  450. else if (p^.left^.resulttype^.deftype=stringdef) then
  451. begin
  452. if (p^.right^.value=0) and not(is_shortstring(p^.left^.resulttype)) then
  453. CGMessage(cg_e_can_access_element_zero);
  454. if (cs_check_range in aktlocalswitches) then
  455. case pstringdef(p^.left^.resulttype)^.string_typ of
  456. { it's the same for ansi- and wide strings }
  457. st_widestring,
  458. st_ansistring:
  459. begin
  460. pushusedregisters(pushed,$ff);
  461. push_int(p^.right^.value);
  462. hp:=newreference(p^.location.reference);
  463. dec(hp^.offset,7);
  464. emit_ref(A_PUSH,S_L,hp);
  465. emitcall('FPC_ANSISTR_RANGECHECK');
  466. popusedregisters(pushed);
  467. maybe_loadesi;
  468. end;
  469. st_shortstring:
  470. begin
  471. {!!!!!!!!!!!!!!!!!}
  472. end;
  473. st_longstring:
  474. begin
  475. {!!!!!!!!!!!!!!!!!}
  476. end;
  477. end;
  478. end;
  479. inc(p^.left^.location.reference.offset,
  480. get_mul_size*p^.right^.value);
  481. if p^.memseg then
  482. p^.left^.location.reference.segment:=R_FS;
  483. p^.left^.resulttype:=p^.resulttype;
  484. disposetree(p^.right);
  485. _p:=p^.left;
  486. putnode(p);
  487. p:=_p;
  488. end
  489. else
  490. { not treetype=ordconstn }
  491. begin
  492. { quick hack, to overcome Delphi 2 }
  493. if (cs_regalloc in aktglobalswitches) and
  494. (p^.left^.resulttype^.deftype=arraydef) then
  495. begin
  496. extraoffset:=0;
  497. if (p^.right^.treetype=addn) then
  498. begin
  499. if p^.right^.right^.treetype=ordconstn then
  500. begin
  501. extraoffset:=p^.right^.right^.value;
  502. t:=p^.right^.left;
  503. putnode(p^.right^.right);
  504. putnode(p^.right);
  505. p^.right:=t
  506. end
  507. else if p^.right^.left^.treetype=ordconstn then
  508. begin
  509. extraoffset:=p^.right^.left^.value;
  510. t:=p^.right^.right;
  511. putnode(p^.right^.left);
  512. putnode(p^.right);
  513. p^.right:=t
  514. end;
  515. end
  516. else if (p^.right^.treetype=subn) then
  517. begin
  518. if p^.right^.right^.treetype=ordconstn then
  519. begin
  520. { this was "extraoffset:=p^.right^.right^.value;" Looks a bit like
  521. copy-paste bug :) (JM) }
  522. extraoffset:=-p^.right^.right^.value;
  523. t:=p^.right^.left;
  524. putnode(p^.right^.right);
  525. putnode(p^.right);
  526. p^.right:=t
  527. end
  528. { You also have to negate p^.right^.right in this case! I can't add an
  529. unaryminusn without causing a crash, so I've disabled it (JM)
  530. else if p^.right^.left^.treetype=ordconstn then
  531. begin
  532. extraoffset:=p^.right^.left^.value;
  533. t:=p^.right^.right;
  534. putnode(p^.right);
  535. putnode(p^.right^.left);
  536. p^.right:=t;
  537. end;}
  538. end;
  539. inc(p^.location.reference.offset,
  540. get_mul_size*extraoffset);
  541. end;
  542. { calculate from left to right }
  543. if (p^.location.loc<>LOC_REFERENCE) and
  544. (p^.location.loc<>LOC_MEM) then
  545. CGMessage(cg_e_illegal_expression);
  546. if (p^.right^.location.loc=LOC_JUMP) then
  547. begin
  548. otl:=truelabel;
  549. getlabel(truelabel);
  550. ofl:=falselabel;
  551. getlabel(falselabel);
  552. end;
  553. is_pushed:=maybe_push(p^.right^.registers32,p,false);
  554. secondpass(p^.right);
  555. if is_pushed then
  556. restore(p,false);
  557. { here we change the location of p^.right
  558. and the update was forgotten so it
  559. led to wrong code in emitrangecheck later PM
  560. so make range check before }
  561. if cs_check_range in aktlocalswitches then
  562. begin
  563. if p^.left^.resulttype^.deftype=arraydef then
  564. begin
  565. if is_open_array(p^.left^.resulttype) or
  566. is_array_of_const(p^.left^.resulttype) then
  567. begin
  568. reset_reference(href);
  569. parraydef(p^.left^.resulttype)^.genrangecheck;
  570. href.symbol:=newasmsymbol(parraydef(p^.left^.resulttype)^.getrangecheckstring);
  571. href.offset:=4;
  572. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  573. hightree:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  574. firstpass(hightree);
  575. secondpass(hightree);
  576. emit_mov_loc_ref(hightree^.location,href,S_L);
  577. disposetree(hightree);
  578. end;
  579. emitrangecheck(p^.right,p^.left^.resulttype);
  580. end;
  581. end;
  582. case p^.right^.location.loc of
  583. LOC_REGISTER:
  584. begin
  585. ind:=p^.right^.location.register;
  586. case p^.right^.resulttype^.size of
  587. 1:
  588. begin
  589. hr:=reg8toreg32(ind);
  590. emit_reg_reg(A_MOVZX,S_BL,ind,hr);
  591. ind:=hr;
  592. end;
  593. 2:
  594. begin
  595. hr:=reg16toreg32(ind);
  596. emit_reg_reg(A_MOVZX,S_WL,ind,hr);
  597. ind:=hr;
  598. end;
  599. end;
  600. end;
  601. LOC_CREGISTER:
  602. begin
  603. ind:=getregister32;
  604. case p^.right^.resulttype^.size of
  605. 1:
  606. emit_reg_reg(A_MOVZX,S_BL,p^.right^.location.register,ind);
  607. 2:
  608. emit_reg_reg(A_MOVZX,S_WL,p^.right^.location.register,ind);
  609. 4:
  610. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,ind);
  611. end;
  612. end;
  613. LOC_FLAGS:
  614. begin
  615. ind:=getregister32;
  616. emit_flag2reg(p^.right^.location.resflags,reg32toreg8(ind));
  617. emit_reg_reg(A_MOVZX,S_BL,reg32toreg8(ind),ind);
  618. end;
  619. LOC_JUMP :
  620. begin
  621. ind:=getregister32;
  622. emitlab(truelabel);
  623. truelabel:=otl;
  624. emit_const_reg(A_MOV,S_L,1,ind);
  625. getlabel(hl);
  626. emitjmp(C_None,hl);
  627. emitlab(falselabel);
  628. falselabel:=ofl;
  629. emit_reg_reg(A_XOR,S_L,ind,ind);
  630. emitlab(hl);
  631. end;
  632. LOC_REFERENCE,LOC_MEM :
  633. begin
  634. del_reference(p^.right^.location.reference);
  635. ind:=getregister32;
  636. { Booleans are stored in an 8 bit memory location, so
  637. the use of MOVL is not correct }
  638. case p^.right^.resulttype^.size of
  639. 1 : tai:=new(paicpu,op_ref_reg(A_MOVZX,S_BL,newreference(p^.right^.location.reference),ind));
  640. 2 : tai:=new(Paicpu,op_ref_reg(A_MOVZX,S_WL,newreference(p^.right^.location.reference),ind));
  641. 4 : tai:=new(Paicpu,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),ind));
  642. end;
  643. exprasmlist^.concat(tai);
  644. end;
  645. else
  646. internalerror(5913428);
  647. end;
  648. { produce possible range check code: }
  649. if cs_check_range in aktlocalswitches then
  650. begin
  651. if p^.left^.resulttype^.deftype=arraydef then
  652. begin
  653. { done defore (PM) }
  654. end
  655. else if (p^.left^.resulttype^.deftype=stringdef) then
  656. begin
  657. case pstringdef(p^.left^.resulttype)^.string_typ of
  658. { it's the same for ansi- and wide strings }
  659. st_widestring,
  660. st_ansistring:
  661. begin
  662. pushusedregisters(pushed,$ff);
  663. emit_reg(A_PUSH,S_L,ind);
  664. hp:=newreference(p^.location.reference);
  665. dec(hp^.offset,7);
  666. emit_ref(A_PUSH,S_L,hp);
  667. emitcall('FPC_ANSISTR_RANGECHECK');
  668. popusedregisters(pushed);
  669. maybe_loadesi;
  670. end;
  671. st_shortstring:
  672. begin
  673. {!!!!!!!!!!!!!!!!!}
  674. end;
  675. st_longstring:
  676. begin
  677. {!!!!!!!!!!!!!!!!!}
  678. end;
  679. end;
  680. end;
  681. end;
  682. if p^.location.reference.index=R_NO then
  683. begin
  684. p^.location.reference.index:=ind;
  685. calc_emit_mul;
  686. end
  687. else
  688. begin
  689. if p^.location.reference.base=R_NO then
  690. begin
  691. case p^.location.reference.scalefactor of
  692. 2 : emit_const_reg(A_SHL,S_L,1,p^.location.reference.index);
  693. 4 : emit_const_reg(A_SHL,S_L,2,p^.location.reference.index);
  694. 8 : emit_const_reg(A_SHL,S_L,3,p^.location.reference.index);
  695. end;
  696. calc_emit_mul;
  697. p^.location.reference.base:=p^.location.reference.index;
  698. p^.location.reference.index:=ind;
  699. end
  700. else
  701. begin
  702. emit_ref_reg(
  703. A_LEA,S_L,newreference(p^.location.reference),
  704. p^.location.reference.index);
  705. ungetregister32(p^.location.reference.base);
  706. { the symbol offset is loaded, }
  707. { so release the symbol name and set symbol }
  708. { to nil }
  709. p^.location.reference.symbol:=nil;
  710. p^.location.reference.offset:=0;
  711. calc_emit_mul;
  712. p^.location.reference.base:=p^.location.reference.index;
  713. p^.location.reference.index:=ind;
  714. end;
  715. end;
  716. if p^.memseg then
  717. p^.location.reference.segment:=R_FS;
  718. end;
  719. end;
  720. {*****************************************************************************
  721. SecondSelfN
  722. *****************************************************************************}
  723. procedure secondselfn(var p : ptree);
  724. begin
  725. reset_reference(p^.location.reference);
  726. getexplicitregister32(R_ESI);
  727. if (p^.resulttype^.deftype=classrefdef) or
  728. ((p^.resulttype^.deftype=objectdef)
  729. and pobjectdef(p^.resulttype)^.is_class
  730. ) then
  731. p^.location.register:=R_ESI
  732. else
  733. p^.location.reference.base:=R_ESI;
  734. end;
  735. {*****************************************************************************
  736. SecondWithN
  737. *****************************************************************************}
  738. procedure secondwith(var p : ptree);
  739. var
  740. usetemp,with_expr_in_temp : boolean;
  741. {$ifdef GDB}
  742. withstartlabel,withendlabel : pasmlabel;
  743. pp : pchar;
  744. mangled_length : longint;
  745. const
  746. withlevel : longint = 0;
  747. {$endif GDB}
  748. begin
  749. if assigned(p^.left) then
  750. begin
  751. secondpass(p^.left);
  752. if p^.left^.location.reference.segment<>R_NO then
  753. message(parser_e_no_with_for_variable_in_other_segments);
  754. new(p^.withreference);
  755. usetemp:=false;
  756. if (p^.left^.treetype=loadn) and
  757. (p^.left^.symtable=aktprocsym^.definition^.localst) then
  758. begin
  759. { for locals use the local storage }
  760. p^.withreference^:=p^.left^.location.reference;
  761. p^.islocal:=true;
  762. end
  763. else
  764. { call can have happend with a property }
  765. if (p^.left^.resulttype^.deftype=objectdef) and
  766. pobjectdef(p^.left^.resulttype)^.is_class then
  767. begin
  768. {$ifndef noAllocEdi}
  769. getexplicitregister32(R_EDI);
  770. {$endif noAllocEdi}
  771. emit_mov_loc_reg(p^.left^.location,R_EDI);
  772. usetemp:=true;
  773. end
  774. else
  775. begin
  776. {$ifndef noAllocEdi}
  777. getexplicitregister32(R_EDI);
  778. {$endif noAllocEdi}
  779. emit_lea_loc_reg(p^.left^.location,R_EDI,false);
  780. usetemp:=true;
  781. end;
  782. release_loc(p^.left^.location);
  783. { if the with expression is stored in a temp }
  784. { area we must make it persistent and shouldn't }
  785. { release it (FK) }
  786. if (p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  787. istemp(p^.left^.location.reference) then
  788. begin
  789. normaltemptopersistant(p^.left^.location.reference.offset);
  790. with_expr_in_temp:=true;
  791. end
  792. else
  793. with_expr_in_temp:=false;
  794. { if usetemp is set the value must be in %edi }
  795. if usetemp then
  796. begin
  797. gettempofsizereference(4,p^.withreference^);
  798. normaltemptopersistant(p^.withreference^.offset);
  799. { move to temp reference }
  800. emit_reg_ref(A_MOV,S_L,R_EDI,newreference(p^.withreference^));
  801. {$ifndef noAllocEdi}
  802. ungetregister32(R_EDI);
  803. {$endif noAllocEdi}
  804. {$ifdef GDB}
  805. if (cs_debuginfo in aktmoduleswitches) then
  806. begin
  807. inc(withlevel);
  808. getlabel(withstartlabel);
  809. getlabel(withendlabel);
  810. emitlab(withstartlabel);
  811. withdebuglist^.concat(new(pai_stabs,init(strpnew(
  812. '"with'+tostr(withlevel)+':'+tostr(symtablestack^.getnewtypecount)+
  813. '=*'+p^.left^.resulttype^.numberstring+'",'+
  814. tostr(N_LSYM)+',0,0,'+tostr(p^.withreference^.offset)))));
  815. mangled_length:=length(aktprocsym^.definition^.mangledname);
  816. getmem(pp,mangled_length+50);
  817. strpcopy(pp,'192,0,0,'+withstartlabel^.name);
  818. if (target_os.use_function_relative_addresses) then
  819. begin
  820. strpcopy(strend(pp),'-');
  821. strpcopy(strend(pp),aktprocsym^.definition^.mangledname);
  822. end;
  823. withdebuglist^.concat(new(pai_stabn,init(strnew(pp))));
  824. end;
  825. {$endif GDB}
  826. del_reference(p^.left^.location.reference);
  827. end;
  828. { p^.right can be optimize out !!! }
  829. if assigned(p^.right) then
  830. secondpass(p^.right);
  831. if usetemp then
  832. begin
  833. ungetpersistanttemp(p^.withreference^.offset);
  834. {$ifdef GDB}
  835. if (cs_debuginfo in aktmoduleswitches) then
  836. begin
  837. emitlab(withendlabel);
  838. strpcopy(pp,'224,0,0,'+withendlabel^.name);
  839. if (target_os.use_function_relative_addresses) then
  840. begin
  841. strpcopy(strend(pp),'-');
  842. strpcopy(strend(pp),aktprocsym^.definition^.mangledname);
  843. end;
  844. withdebuglist^.concat(new(pai_stabn,init(strnew(pp))));
  845. freemem(pp,mangled_length+50);
  846. dec(withlevel);
  847. end;
  848. {$endif GDB}
  849. end;
  850. if with_expr_in_temp then
  851. ungetpersistanttemp(p^.left^.location.reference.offset);
  852. dispose(p^.withreference);
  853. p^.withreference:=nil;
  854. end;
  855. end;
  856. end.
  857. {
  858. $Log$
  859. Revision 1.72 2000-02-18 20:53:14 pierre
  860. * fixes a stabs problem for functions
  861. + includes a stabs local var for with statements
  862. the name is with in lowercase followed by an index
  863. for nested with.
  864. + Withdebuglist added because the stabs declarations of local
  865. var are postponed to end of function.
  866. Revision 1.71 2000/02/09 18:08:33 jonas
  867. * added regallocs for esi
  868. Revision 1.70 2000/02/09 13:22:47 peter
  869. * log truncated
  870. Revision 1.69 2000/01/09 15:19:23 peter
  871. * fixed misplaced getexplicitreg(r_edi) which broke make cycle
  872. Revision 1.68 2000/01/09 12:35:02 jonas
  873. * changed edi allocation to use getexplicitregister32/ungetregister
  874. (adapted tgeni386 a bit for this) and enabled it by default
  875. * fixed very big and stupid bug of mine in cg386mat that broke the
  876. include() code (and make cycle :( ) if you compiled without
  877. -dnewoptimizations
  878. Revision 1.67 2000/01/09 01:44:20 jonas
  879. + (de)allocation info for EDI to fix reported bug on mailinglist.
  880. Also some (de)allocation info for ESI added. Between -dallocEDI
  881. because at this time of the night bugs could easily slip in ;)
  882. Revision 1.66 2000/01/07 01:14:21 peter
  883. * updated copyright to 2000
  884. Revision 1.65 2000/01/04 15:15:50 florian
  885. + added compiler switch $maxfpuregisters
  886. + fixed a small problem in secondvecn
  887. Revision 1.64 2000/01/03 17:10:39 jonas
  888. * fixed "quick hack, to overcome Delphi 2" :)
  889. Revision 1.63 1999/12/01 12:42:32 peter
  890. * fixed bug 698
  891. * removed some notes about unused vars
  892. Revision 1.62 1999/11/30 10:40:43 peter
  893. + ttype, tsymlist
  894. Revision 1.61 1999/11/15 21:54:38 peter
  895. * LOC_JUMP support for vecn
  896. Revision 1.60 1999/11/06 14:34:18 peter
  897. * truncated log to 20 revs
  898. Revision 1.59 1999/10/30 17:35:26 peter
  899. * fpc_freemem fpc_getmem new callings updated
  900. Revision 1.58 1999/09/17 17:14:02 peter
  901. * @procvar fixes for tp mode
  902. * @<id>:= gives now an error
  903. Revision 1.57 1999/09/14 07:59:46 florian
  904. * finally!? fixed
  905. with <function with result in temp> do
  906. My last and also Peter's fix before were wrong :(
  907. Revision 1.56 1999/09/13 20:49:41 florian
  908. * hopefully an error in Peter's previous commit fixed
  909. Revision 1.55 1999/09/10 15:42:50 peter
  910. * fixed with <calln> do
  911. * fixed finalize/initialize call for new/dispose
  912. Revision 1.54 1999/08/25 11:59:46 jonas
  913. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  914. Revision 1.53 1999/08/23 23:49:21 pierre
  915. * hnewn location corrected
  916. Revision 1.52 1999/08/19 13:08:52 pierre
  917. * emit_??? used
  918. Revision 1.51 1999/08/16 23:20:28 peter
  919. * range check for array of const
  920. Revision 1.50 1999/08/14 00:36:05 peter
  921. * array constructor support
  922. }