cg386mem.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 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. globtype,systems,
  37. cobjects,verbose,globals,
  38. symconst,symtable,aasm,types,
  39. hcodegen,temp_gen,pass_2,pass_1,
  40. cpubase,cpuasm,
  41. cgai386,tgeni386;
  42. {*****************************************************************************
  43. SecondLoadVMT
  44. *****************************************************************************}
  45. procedure secondloadvmt(var p : ptree);
  46. begin
  47. p^.location.register:=getregister32;
  48. emit_sym_ofs_reg(A_MOV,
  49. S_L,newasmsymbol(pobjectdef(pclassrefdef(p^.resulttype)^.definition)^.vmt_mangledname),0,
  50. p^.location.register);
  51. end;
  52. {*****************************************************************************
  53. SecondHNewN
  54. *****************************************************************************}
  55. procedure secondhnewn(var p : ptree);
  56. begin
  57. end;
  58. {*****************************************************************************
  59. SecondNewN
  60. *****************************************************************************}
  61. procedure secondnewn(var p : ptree);
  62. var
  63. pushed : tpushed;
  64. r : preference;
  65. begin
  66. if assigned(p^.left) then
  67. begin
  68. secondpass(p^.left);
  69. p^.location.register:=p^.left^.location.register;
  70. end
  71. else
  72. begin
  73. pushusedregisters(pushed,$ff);
  74. gettempofsizereference(target_os.size_of_pointer,p^.location.reference);
  75. { determines the size of the mem block }
  76. push_int(ppointerdef(p^.resulttype)^.definition^.size);
  77. emit_push_lea_loc(p^.location,false);
  78. emitcall('FPC_GETMEM');
  79. if ppointerdef(p^.resulttype)^.definition^.needs_inittable then
  80. begin
  81. new(r);
  82. reset_reference(r^);
  83. r^.symbol:=ppointerdef(p^.left^.resulttype)^.definition^.get_inittable_label;
  84. emitpushreferenceaddr(r^);
  85. dispose(r);
  86. { push pointer we just allocated, we need to initialize the
  87. data located at that pointer not the pointer self (PFV) }
  88. emit_push_loc(p^.location);
  89. emitcall('FPC_INITIALIZE');
  90. end;
  91. popusedregisters(pushed);
  92. { may be load ESI }
  93. maybe_loadesi;
  94. end;
  95. if codegenerror then
  96. exit;
  97. end;
  98. {*****************************************************************************
  99. SecondDisposeN
  100. *****************************************************************************}
  101. procedure secondhdisposen(var p : ptree);
  102. begin
  103. secondpass(p^.left);
  104. if codegenerror then
  105. exit;
  106. reset_reference(p^.location.reference);
  107. case p^.left^.location.loc of
  108. LOC_REGISTER:
  109. p^.location.reference.index:=p^.left^.location.register;
  110. LOC_CREGISTER:
  111. begin
  112. p^.location.reference.index:=getregister32;
  113. emit_reg_reg(A_MOV,S_L,
  114. p^.left^.location.register,
  115. p^.location.reference.index);
  116. end;
  117. LOC_MEM,LOC_REFERENCE :
  118. begin
  119. del_reference(p^.left^.location.reference);
  120. p^.location.reference.index:=getregister32;
  121. emit_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  122. p^.location.reference.index);
  123. end;
  124. end;
  125. end;
  126. {*****************************************************************************
  127. SecondNewDispose
  128. *****************************************************************************}
  129. procedure secondsimplenewdispose(var p : ptree);
  130. var
  131. pushed : tpushed;
  132. r : preference;
  133. begin
  134. secondpass(p^.left);
  135. if codegenerror then
  136. exit;
  137. pushusedregisters(pushed,$ff);
  138. { call the mem handling procedures }
  139. case p^.treetype of
  140. simpledisposen:
  141. begin
  142. if ppointerdef(p^.left^.resulttype)^.definition^.needs_inittable then
  143. begin
  144. new(r);
  145. reset_reference(r^);
  146. r^.symbol:=ppointerdef(p^.left^.resulttype)^.definition^.get_inittable_label;
  147. emitpushreferenceaddr(r^);
  148. dispose(r);
  149. { push pointer adress }
  150. emit_push_loc(p^.left^.location);
  151. emitcall('FPC_FINALIZE');
  152. end;
  153. emit_push_lea_loc(p^.left^.location,true);
  154. emitcall('FPC_FREEMEM');
  155. end;
  156. simplenewn:
  157. begin
  158. { determines the size of the mem block }
  159. push_int(ppointerdef(p^.left^.resulttype)^.definition^.size);
  160. emit_push_lea_loc(p^.left^.location,true);
  161. emitcall('FPC_GETMEM');
  162. if ppointerdef(p^.left^.resulttype)^.definition^.needs_inittable then
  163. begin
  164. new(r);
  165. reset_reference(r^);
  166. r^.symbol:=ppointerdef(p^.left^.resulttype)^.definition^.get_inittable_label;
  167. emitpushreferenceaddr(r^);
  168. dispose(r);
  169. emit_push_loc(p^.left^.location);
  170. emitcall('FPC_INITIALIZE');
  171. end;
  172. end;
  173. end;
  174. popusedregisters(pushed);
  175. { may be load ESI }
  176. maybe_loadesi;
  177. end;
  178. {*****************************************************************************
  179. SecondAddr
  180. *****************************************************************************}
  181. procedure secondaddr(var p : ptree);
  182. begin
  183. secondpass(p^.left);
  184. { when loading procvar we do nothing with this node, so load the
  185. location of left }
  186. if p^.procvarload then
  187. begin
  188. set_location(p^.location,p^.left^.location);
  189. exit;
  190. end;
  191. p^.location.loc:=LOC_REGISTER;
  192. del_reference(p^.left^.location.reference);
  193. p^.location.register:=getregister32;
  194. {@ on a procvar means returning an address to the procedure that
  195. is stored in it.}
  196. { yes but p^.left^.symtableentry can be nil
  197. for example on @self !! }
  198. { symtableentry can be also invalid, if left is no tree node }
  199. if (m_tp_procvar in aktmodeswitches) and
  200. (p^.left^.treetype=loadn) and
  201. assigned(p^.left^.symtableentry) and
  202. (p^.left^.symtableentry^.typ=varsym) and
  203. (pvarsym(p^.left^.symtableentry)^.definition^.deftype=procvardef) then
  204. emit_ref_reg(A_MOV,S_L,
  205. newreference(p^.left^.location.reference),
  206. p^.location.register)
  207. else
  208. emit_ref_reg(A_LEA,S_L,
  209. newreference(p^.left^.location.reference),
  210. p^.location.register);
  211. { for use of other segments }
  212. if p^.left^.location.reference.segment<>R_NO then
  213. p^.location.segment:=p^.left^.location.reference.segment;
  214. end;
  215. {*****************************************************************************
  216. SecondDoubleAddr
  217. *****************************************************************************}
  218. procedure seconddoubleaddr(var p : ptree);
  219. begin
  220. secondpass(p^.left);
  221. p^.location.loc:=LOC_REGISTER;
  222. del_reference(p^.left^.location.reference);
  223. p^.location.register:=getregister32;
  224. emit_ref_reg(A_LEA,S_L,
  225. newreference(p^.left^.location.reference),
  226. p^.location.register);
  227. end;
  228. {*****************************************************************************
  229. SecondDeRef
  230. *****************************************************************************}
  231. procedure secondderef(var p : ptree);
  232. var
  233. hr : tregister;
  234. begin
  235. secondpass(p^.left);
  236. reset_reference(p^.location.reference);
  237. case p^.left^.location.loc of
  238. LOC_REGISTER:
  239. p^.location.reference.base:=p^.left^.location.register;
  240. LOC_CREGISTER:
  241. begin
  242. { ... and reserve one for the pointer }
  243. hr:=getregister32;
  244. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hr);
  245. p^.location.reference.base:=hr;
  246. end;
  247. else
  248. begin
  249. { free register }
  250. del_reference(p^.left^.location.reference);
  251. { ...and reserve one for the pointer }
  252. hr:=getregister32;
  253. emit_ref_reg(
  254. A_MOV,S_L,newreference(p^.left^.location.reference),
  255. hr);
  256. p^.location.reference.base:=hr;
  257. end;
  258. end;
  259. if ppointerdef(p^.left^.resulttype)^.is_far then
  260. p^.location.reference.segment:=R_FS;
  261. if not ppointerdef(p^.left^.resulttype)^.is_far and
  262. (cs_gdb_heaptrc in aktglobalswitches) and
  263. (cs_checkpointer in aktglobalswitches) then
  264. begin
  265. emit_reg(
  266. A_PUSH,S_L,p^.location.reference.base);
  267. emitcall('FPC_CHECKPOINTER');
  268. end;
  269. end;
  270. {*****************************************************************************
  271. SecondSubScriptN
  272. *****************************************************************************}
  273. procedure secondsubscriptn(var p : ptree);
  274. var
  275. hr : tregister;
  276. begin
  277. secondpass(p^.left);
  278. if codegenerror then
  279. exit;
  280. { classes must be dereferenced implicit }
  281. if (p^.left^.resulttype^.deftype=objectdef) and
  282. pobjectdef(p^.left^.resulttype)^.is_class then
  283. begin
  284. reset_reference(p^.location.reference);
  285. case p^.left^.location.loc of
  286. LOC_REGISTER:
  287. p^.location.reference.base:=p^.left^.location.register;
  288. LOC_CREGISTER:
  289. begin
  290. { ... and reserve one for the pointer }
  291. hr:=getregister32;
  292. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hr);
  293. p^.location.reference.base:=hr;
  294. end;
  295. else
  296. begin
  297. { free register }
  298. del_reference(p^.left^.location.reference);
  299. { ... and reserve one for the pointer }
  300. hr:=getregister32;
  301. emit_ref_reg(
  302. A_MOV,S_L,newreference(p^.left^.location.reference),
  303. hr);
  304. p^.location.reference.base:=hr;
  305. end;
  306. end;
  307. end
  308. else
  309. set_location(p^.location,p^.left^.location);
  310. inc(p^.location.reference.offset,p^.vs^.address);
  311. end;
  312. {*****************************************************************************
  313. SecondVecN
  314. *****************************************************************************}
  315. procedure secondvecn(var p : ptree);
  316. var
  317. is_pushed : boolean;
  318. ind,hr : tregister;
  319. _p : ptree;
  320. function get_mul_size:longint;
  321. begin
  322. if p^.memindex then
  323. get_mul_size:=1
  324. else
  325. get_mul_size:=p^.resulttype^.size;
  326. end;
  327. procedure calc_emit_mul;
  328. var
  329. l1,l2 : longint;
  330. begin
  331. l1:=get_mul_size;
  332. case l1 of
  333. 1,2,4,8 : p^.location.reference.scalefactor:=l1;
  334. else
  335. begin
  336. if ispowerof2(l1,l2) then
  337. emit_const_reg(A_SHL,S_L,l2,ind)
  338. else
  339. emit_const_reg(A_IMUL,S_L,l1,ind);
  340. end;
  341. end;
  342. end;
  343. var
  344. extraoffset : longint;
  345. { rl stores the resulttype of the left node, this is necessary }
  346. { to detect if it is an ansistring }
  347. { because in constant nodes which constant index }
  348. { the left tree is removed }
  349. rl : pdef;
  350. t : ptree;
  351. hp : preference;
  352. href : treference;
  353. tai : Paicpu;
  354. pushed : tpushed;
  355. hightree : ptree;
  356. begin
  357. secondpass(p^.left);
  358. rl:=p^.left^.resulttype;
  359. { we load the array reference to p^.location }
  360. { an ansistring needs to be dereferenced }
  361. if is_ansistring(p^.left^.resulttype) or
  362. is_widestring(p^.left^.resulttype) then
  363. begin
  364. reset_reference(p^.location.reference);
  365. if p^.callunique then
  366. begin
  367. if p^.left^.location.loc<>LOC_REFERENCE then
  368. begin
  369. CGMessage(cg_e_illegal_expression);
  370. exit;
  371. end;
  372. pushusedregisters(pushed,$ff);
  373. emitpushreferenceaddr(p^.left^.location.reference);
  374. if is_ansistring(p^.left^.resulttype) then
  375. emitcall('FPC_ANSISTR_UNIQUE')
  376. else
  377. emitcall('FPC_WIDESTR_UNIQUE');
  378. maybe_loadesi;
  379. popusedregisters(pushed);
  380. end;
  381. if p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  382. begin
  383. p^.location.reference.base:=p^.left^.location.register;
  384. end
  385. else
  386. begin
  387. del_reference(p^.left^.location.reference);
  388. p^.location.reference.base:=getregister32;
  389. emit_ref_reg(A_MOV,S_L,
  390. newreference(p^.left^.location.reference),
  391. p^.location.reference.base);
  392. end;
  393. { check for a zero length string,
  394. we can use the ansistring routine here }
  395. if (cs_check_range in aktlocalswitches) then
  396. begin
  397. pushusedregisters(pushed,$ff);
  398. emit_reg(A_PUSH,S_L,p^.location.reference.base);
  399. emitcall('FPC_ANSISTR_CHECKZERO');
  400. maybe_loadesi;
  401. popusedregisters(pushed);
  402. end;
  403. if is_ansistring(p^.left^.resulttype) then
  404. { in ansistrings S[1] is pchar(S)[0] !! }
  405. dec(p^.location.reference.offset)
  406. else
  407. begin
  408. { in widestrings S[1] is pwchar(S)[0] !! }
  409. dec(p^.location.reference.offset,2);
  410. emit_const_reg(A_SHL,S_L,
  411. 1,p^.location.reference.base);
  412. end;
  413. { we've also to keep left up-to-date, because it is used }
  414. { if a constant array index occurs, subject to change (FK) }
  415. set_location(p^.left^.location,p^.location);
  416. end
  417. else
  418. set_location(p^.location,p^.left^.location);
  419. { offset can only differ from 0 if arraydef }
  420. if p^.left^.resulttype^.deftype=arraydef then
  421. dec(p^.location.reference.offset,
  422. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  423. if p^.right^.treetype=ordconstn then
  424. begin
  425. { offset can only differ from 0 if arraydef }
  426. if (p^.left^.resulttype^.deftype=arraydef) then
  427. begin
  428. if not(is_open_array(p^.left^.resulttype)) and
  429. not(is_array_of_const(p^.left^.resulttype)) then
  430. begin
  431. if (p^.right^.value>parraydef(p^.left^.resulttype)^.highrange) or
  432. (p^.right^.value<parraydef(p^.left^.resulttype)^.lowrange) then
  433. begin
  434. if (cs_check_range in aktlocalswitches) then
  435. CGMessage(parser_e_range_check_error)
  436. else
  437. CGMessage(parser_w_range_check_error);
  438. end;
  439. dec(p^.left^.location.reference.offset,
  440. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  441. end
  442. else
  443. begin
  444. { range checking for open arrays !!!! }
  445. {!!!!!!!!!!!!!!!!!}
  446. end;
  447. end
  448. else if (p^.left^.resulttype^.deftype=stringdef) then
  449. begin
  450. if (p^.right^.value=0) and not(is_shortstring(p^.left^.resulttype)) then
  451. CGMessage(cg_e_can_access_element_zero);
  452. if (cs_check_range in aktlocalswitches) then
  453. case pstringdef(p^.left^.resulttype)^.string_typ of
  454. { it's the same for ansi- and wide strings }
  455. st_widestring,
  456. st_ansistring:
  457. begin
  458. pushusedregisters(pushed,$ff);
  459. push_int(p^.right^.value);
  460. hp:=newreference(p^.location.reference);
  461. dec(hp^.offset,7);
  462. emit_ref(A_PUSH,S_L,hp);
  463. emitcall('FPC_ANSISTR_RANGECHECK');
  464. popusedregisters(pushed);
  465. maybe_loadesi;
  466. end;
  467. st_shortstring:
  468. begin
  469. {!!!!!!!!!!!!!!!!!}
  470. end;
  471. st_longstring:
  472. begin
  473. {!!!!!!!!!!!!!!!!!}
  474. end;
  475. end;
  476. end;
  477. inc(p^.left^.location.reference.offset,
  478. get_mul_size*p^.right^.value);
  479. if p^.memseg then
  480. p^.left^.location.reference.segment:=R_FS;
  481. p^.left^.resulttype:=p^.resulttype;
  482. disposetree(p^.right);
  483. _p:=p^.left;
  484. putnode(p);
  485. p:=_p;
  486. end
  487. else
  488. { not treetype=ordconstn }
  489. begin
  490. { quick hack, to overcome Delphi 2 }
  491. if (cs_regalloc in aktglobalswitches) and
  492. (p^.left^.resulttype^.deftype=arraydef) then
  493. begin
  494. extraoffset:=0;
  495. if (p^.right^.treetype=addn) then
  496. begin
  497. if p^.right^.right^.treetype=ordconstn then
  498. begin
  499. extraoffset:=p^.right^.right^.value;
  500. t:=p^.right^.left;
  501. putnode(p^.right);
  502. putnode(p^.right^.right);
  503. p^.right:=t
  504. end
  505. else if p^.right^.left^.treetype=ordconstn then
  506. begin
  507. extraoffset:=p^.right^.left^.value;
  508. t:=p^.right^.right;
  509. putnode(p^.right);
  510. putnode(p^.right^.left);
  511. p^.right:=t
  512. end;
  513. end
  514. else if (p^.right^.treetype=subn) then
  515. begin
  516. if p^.right^.right^.treetype=ordconstn then
  517. begin
  518. extraoffset:=p^.right^.right^.value;
  519. t:=p^.right^.left;
  520. putnode(p^.right);
  521. putnode(p^.right^.right);
  522. p^.right:=t
  523. end
  524. else if p^.right^.left^.treetype=ordconstn then
  525. begin
  526. extraoffset:=p^.right^.left^.value;
  527. t:=p^.right^.right;
  528. putnode(p^.right);
  529. putnode(p^.right^.left);
  530. p^.right:=t
  531. end;
  532. end;
  533. inc(p^.location.reference.offset,
  534. get_mul_size*extraoffset);
  535. end;
  536. { calculate from left to right }
  537. if (p^.location.loc<>LOC_REFERENCE) and
  538. (p^.location.loc<>LOC_MEM) then
  539. CGMessage(cg_e_illegal_expression);
  540. is_pushed:=maybe_push(p^.right^.registers32,p,false);
  541. secondpass(p^.right);
  542. if is_pushed then
  543. restore(p,false);
  544. { here we change the location of p^.right
  545. and the update was forgotten so it
  546. led to wrong code in emitrangecheck later PM
  547. so make range check before }
  548. if cs_check_range in aktlocalswitches then
  549. begin
  550. if p^.left^.resulttype^.deftype=arraydef then
  551. begin
  552. if is_open_array(p^.left^.resulttype) or
  553. is_array_of_const(p^.left^.resulttype) then
  554. begin
  555. reset_reference(href);
  556. parraydef(p^.left^.resulttype)^.genrangecheck;
  557. href.symbol:=newasmsymbol(parraydef(p^.left^.resulttype)^.getrangecheckstring);
  558. href.offset:=4;
  559. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  560. hightree:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  561. firstpass(hightree);
  562. secondpass(hightree);
  563. emit_mov_loc_ref(hightree^.location,href,S_L);
  564. disposetree(hightree);
  565. end;
  566. emitrangecheck(p^.right,p^.left^.resulttype);
  567. end;
  568. end;
  569. case p^.right^.location.loc of
  570. LOC_REGISTER:
  571. begin
  572. ind:=p^.right^.location.register;
  573. case p^.right^.resulttype^.size of
  574. 1:
  575. begin
  576. hr:=reg8toreg32(ind);
  577. emit_reg_reg(A_MOVZX,S_BL,ind,hr);
  578. ind:=hr;
  579. end;
  580. 2:
  581. begin
  582. hr:=reg16toreg32(ind);
  583. emit_reg_reg(A_MOVZX,S_WL,ind,hr);
  584. ind:=hr;
  585. end;
  586. end;
  587. end;
  588. LOC_CREGISTER:
  589. begin
  590. ind:=getregister32;
  591. case p^.right^.resulttype^.size of
  592. 1:
  593. emit_reg_reg(A_MOVZX,S_BL,p^.right^.location.register,ind);
  594. 2:
  595. emit_reg_reg(A_MOVZX,S_WL,p^.right^.location.register,ind);
  596. 4:
  597. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,ind);
  598. end;
  599. end;
  600. LOC_FLAGS:
  601. begin
  602. ind:=getregister32;
  603. emit_flag2reg(p^.right^.location.resflags,reg32toreg8(ind));
  604. emit_reg_reg(A_MOVZX,S_BL,reg32toreg8(ind),ind);
  605. end
  606. else
  607. begin
  608. del_reference(p^.right^.location.reference);
  609. ind:=getregister32;
  610. { Booleans are stored in an 8 bit memory location, so
  611. the use of MOVL is not correct }
  612. case p^.right^.resulttype^.size of
  613. 1 : tai:=new(paicpu,op_ref_reg(A_MOVZX,S_BL,newreference(p^.right^.location.reference),ind));
  614. 2 : tai:=new(Paicpu,op_ref_reg(A_MOVZX,S_WL,newreference(p^.right^.location.reference),ind));
  615. 4 : tai:=new(Paicpu,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),ind));
  616. end;
  617. exprasmlist^.concat(tai);
  618. end;
  619. end;
  620. { produce possible range check code: }
  621. if cs_check_range in aktlocalswitches then
  622. begin
  623. if p^.left^.resulttype^.deftype=arraydef then
  624. begin
  625. { done defore (PM) }
  626. end
  627. else if (p^.left^.resulttype^.deftype=stringdef) then
  628. begin
  629. case pstringdef(p^.left^.resulttype)^.string_typ of
  630. { it's the same for ansi- and wide strings }
  631. st_widestring,
  632. st_ansistring:
  633. begin
  634. pushusedregisters(pushed,$ff);
  635. emit_reg(A_PUSH,S_L,ind);
  636. hp:=newreference(p^.location.reference);
  637. dec(hp^.offset,7);
  638. emit_ref(A_PUSH,S_L,hp);
  639. emitcall('FPC_ANSISTR_RANGECHECK');
  640. popusedregisters(pushed);
  641. maybe_loadesi;
  642. end;
  643. st_shortstring:
  644. begin
  645. {!!!!!!!!!!!!!!!!!}
  646. end;
  647. st_longstring:
  648. begin
  649. {!!!!!!!!!!!!!!!!!}
  650. end;
  651. end;
  652. end;
  653. end;
  654. if p^.location.reference.index=R_NO then
  655. begin
  656. p^.location.reference.index:=ind;
  657. calc_emit_mul;
  658. end
  659. else
  660. begin
  661. if p^.location.reference.base=R_NO then
  662. begin
  663. case p^.location.reference.scalefactor of
  664. 2 : emit_const_reg(A_SHL,S_L,1,p^.location.reference.index);
  665. 4 : emit_const_reg(A_SHL,S_L,2,p^.location.reference.index);
  666. 8 : emit_const_reg(A_SHL,S_L,3,p^.location.reference.index);
  667. end;
  668. calc_emit_mul;
  669. p^.location.reference.base:=p^.location.reference.index;
  670. p^.location.reference.index:=ind;
  671. end
  672. else
  673. begin
  674. emit_ref_reg(
  675. A_LEA,S_L,newreference(p^.location.reference),
  676. p^.location.reference.index);
  677. ungetregister32(p^.location.reference.base);
  678. { the symbol offset is loaded, }
  679. { so release the symbol name and set symbol }
  680. { to nil }
  681. p^.location.reference.symbol:=nil;
  682. p^.location.reference.offset:=0;
  683. calc_emit_mul;
  684. p^.location.reference.base:=p^.location.reference.index;
  685. p^.location.reference.index:=ind;
  686. end;
  687. end;
  688. if p^.memseg then
  689. p^.location.reference.segment:=R_FS;
  690. end;
  691. end;
  692. {*****************************************************************************
  693. SecondSelfN
  694. *****************************************************************************}
  695. procedure secondselfn(var p : ptree);
  696. begin
  697. reset_reference(p^.location.reference);
  698. if (p^.resulttype^.deftype=classrefdef) or
  699. ((p^.resulttype^.deftype=objectdef)
  700. and pobjectdef(p^.resulttype)^.is_class
  701. ) then
  702. p^.location.register:=R_ESI
  703. else
  704. p^.location.reference.base:=R_ESI;
  705. end;
  706. {*****************************************************************************
  707. SecondWithN
  708. *****************************************************************************}
  709. procedure secondwith(var p : ptree);
  710. var
  711. usetemp,with_expr_in_temp : boolean;
  712. begin
  713. if assigned(p^.left) then
  714. begin
  715. secondpass(p^.left);
  716. if p^.left^.location.reference.segment<>R_NO then
  717. message(parser_e_no_with_for_variable_in_other_segments);
  718. new(p^.withreference);
  719. usetemp:=false;
  720. if (p^.left^.treetype=loadn) and
  721. (p^.left^.symtable=aktprocsym^.definition^.localst) then
  722. begin
  723. { for locals use the local storage }
  724. p^.withreference^:=p^.left^.location.reference;
  725. p^.islocal:=true;
  726. end
  727. else
  728. { call can happend with a property }
  729. if { (p^.left^.treetype=calln) and Don't think that
  730. this is necessary (FK) }
  731. (p^.left^.resulttype^.deftype=objectdef) and
  732. pobjectdef(p^.left^.resulttype)^.is_class then
  733. begin
  734. emit_mov_loc_reg(p^.left^.location,R_EDI);
  735. usetemp:=true;
  736. end
  737. else
  738. begin
  739. emit_lea_loc_reg(p^.left^.location,R_EDI,false);
  740. usetemp:=true;
  741. end;
  742. release_loc(p^.left^.location);
  743. { if the with expression is stored in a temp }
  744. { area we must make it persistent and shouldn't }
  745. { release it (FK) }
  746. if (p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  747. istemp(p^.left^.location.reference) then
  748. begin
  749. normaltemptopersistant(p^.left^.location.reference.offset);
  750. with_expr_in_temp:=true;
  751. end
  752. else
  753. with_expr_in_temp:=false;
  754. { if usetemp is set the value must be in %edi }
  755. if usetemp then
  756. begin
  757. gettempofsizereference(4,p^.withreference^);
  758. normaltemptopersistant(p^.withreference^.offset);
  759. { move to temp reference }
  760. emit_reg_ref(A_MOV,S_L,
  761. R_EDI,newreference(p^.withreference^));
  762. del_reference(p^.left^.location.reference);
  763. end;
  764. { p^.right can be optimize out !!! }
  765. if assigned(p^.right) then
  766. secondpass(p^.right);
  767. if usetemp then
  768. ungetpersistanttemp(p^.withreference^.offset);
  769. if with_expr_in_temp then
  770. ungetpersistanttemp(p^.left^.location.reference.offset);
  771. dispose(p^.withreference);
  772. p^.withreference:=nil;
  773. end;
  774. end;
  775. end.
  776. {
  777. $Log$
  778. Revision 1.59 1999-10-30 17:35:26 peter
  779. * fpc_freemem fpc_getmem new callings updated
  780. Revision 1.58 1999/09/17 17:14:02 peter
  781. * @procvar fixes for tp mode
  782. * @<id>:= gives now an error
  783. Revision 1.57 1999/09/14 07:59:46 florian
  784. * finally!? fixed
  785. with <function with result in temp> do
  786. My last and also Peter's fix before were wrong :(
  787. Revision 1.56 1999/09/13 20:49:41 florian
  788. * hopefully an error in Peter's previous commit fixed
  789. Revision 1.55 1999/09/10 15:42:50 peter
  790. * fixed with <calln> do
  791. * fixed finalize/initialize call for new/dispose
  792. Revision 1.54 1999/08/25 11:59:46 jonas
  793. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  794. Revision 1.53 1999/08/23 23:49:21 pierre
  795. * hnewn location corrected
  796. Revision 1.52 1999/08/19 13:08:52 pierre
  797. * emit_??? used
  798. Revision 1.51 1999/08/16 23:20:28 peter
  799. * range check for array of const
  800. Revision 1.50 1999/08/14 00:36:05 peter
  801. * array constructor support
  802. Revision 1.49 1999/08/04 00:22:53 florian
  803. * renamed i386asm and i386base to cpuasm and cpubase
  804. Revision 1.48 1999/08/03 22:02:47 peter
  805. * moved bitmask constants to sets
  806. * some other type/const renamings
  807. Revision 1.47 1999/06/02 10:11:45 florian
  808. * make cycle fixed i.e. compilation with 0.99.10
  809. * some fixes for qword
  810. * start of register calling conventions
  811. Revision 1.46 1999/05/27 19:44:17 peter
  812. * removed oldasm
  813. * plabel -> pasmlabel
  814. * -a switches to source writing automaticly
  815. * assembler readers OOPed
  816. * asmsymbol automaticly external
  817. * jumptables and other label fixes for asm readers
  818. Revision 1.45 1999/05/23 18:42:04 florian
  819. * better error recovering in typed constants
  820. * some problems with arrays of const fixed, some problems
  821. due my previous
  822. - the location type of array constructor is now LOC_MEM
  823. - the pushing of high fixed
  824. - parameter copying fixed
  825. - zero temp. allocation removed
  826. * small problem in the assembler writers fixed:
  827. ref to nil wasn't written correctly
  828. Revision 1.44 1999/05/21 13:54:53 peter
  829. * NEWLAB for label as symbol
  830. Revision 1.43 1999/05/19 16:48:21 florian
  831. * tdef.typename: returns a now a proper type name for the most types
  832. Revision 1.42 1999/05/18 22:11:52 pierre
  833. * checkpointer code was wrong!
  834. Revision 1.41 1999/05/18 21:58:29 florian
  835. * fixed some bugs related to temp. ansistrings and functions results
  836. which return records/objects/arrays which need init/final.
  837. Revision 1.40 1999/05/18 14:15:26 peter
  838. * containsself fixes
  839. * checktypes()
  840. Revision 1.39 1999/05/17 23:51:39 peter
  841. * with temp vars now use a reference with a persistant temp instead
  842. of setting datasize
  843. Revision 1.38 1999/05/17 21:57:05 florian
  844. * new temporary ansistring handling
  845. Revision 1.37 1999/05/17 14:14:14 pierre
  846. + -gc for check pointer with heaptrc
  847. Revision 1.36 1999/05/12 00:19:44 peter
  848. * removed R_DEFAULT_SEG
  849. * uniform float names
  850. Revision 1.35 1999/05/01 13:24:13 peter
  851. * merged nasm compiler
  852. * old asm moved to oldasm/
  853. Revision 1.34 1999/04/26 18:29:54 peter
  854. * farpointerdef moved into pointerdef.is_far
  855. Revision 1.33 1999/03/26 11:43:26 pierre
  856. * bug0236 fixed
  857. Revision 1.32 1999/03/24 23:16:53 peter
  858. * fixed bugs 212,222,225,227,229,231,233
  859. Revision 1.31 1999/02/25 21:02:29 peter
  860. * ag386bin updates
  861. + coff writer
  862. Revision 1.30 1999/02/22 02:15:14 peter
  863. * updates for ag386bin
  864. Revision 1.29 1999/02/07 22:53:07 florian
  865. * potential bug in secondvecn fixed
  866. Revision 1.28 1999/02/04 17:16:51 peter
  867. * fixed crash with temp ansistring indexing
  868. Revision 1.27 1999/02/04 11:44:46 florian
  869. * fixed indexed access of ansistrings to temp. ansistring, i.e.
  870. c:=(s1+s2)[i], the temp is now correctly remove and the generated
  871. code is also fixed
  872. Revision 1.26 1999/02/04 10:49:41 florian
  873. + range checking for ansi- and widestrings
  874. * made it compilable with TP
  875. Revision 1.25 1999/01/21 16:40:52 pierre
  876. * fix for constructor inside with statements
  877. Revision 1.24 1999/01/19 12:05:27 pierre
  878. * bug with @procvar=procvar fiwed
  879. Revision 1.23 1998/12/30 22:15:45 peter
  880. + farpointer type
  881. * absolutesym now also stores if its far
  882. Revision 1.22 1998/12/11 00:02:55 peter
  883. + globtype,tokens,version unit splitted from globals
  884. Revision 1.21 1998/12/10 09:47:18 florian
  885. + basic operations with int64/qord (compiler with -dint64)
  886. + rtti of enumerations extended: names are now written
  887. Revision 1.20 1998/11/25 19:12:54 pierre
  888. * var:=new(pointer_type) support added
  889. Revision 1.19 1998/11/20 15:35:55 florian
  890. * problems with rtti fixed, hope it works
  891. Revision 1.18 1998/11/17 00:36:40 peter
  892. * more ansistring fixes
  893. Revision 1.17 1998/11/16 15:35:09 pierre
  894. * added error for with if different segment
  895. Revision 1.16 1998/10/21 11:44:42 florian
  896. + check for access to index 0 of long/wide/ansi strings added,
  897. gives now an error
  898. * problem with access to contant index of ansistrings fixed
  899. Revision 1.15 1998/10/12 09:49:53 florian
  900. + support of <procedure var type>:=<pointer> in delphi mode added
  901. Revision 1.14 1998/10/02 07:20:37 florian
  902. * range checking in units doesn't work if the units are smartlinked, fixed
  903. Revision 1.13 1998/09/27 10:16:23 florian
  904. * type casts pchar<->ansistring fixed
  905. * ansistring[..] calls does now an unique call
  906. Revision 1.12 1998/09/23 15:46:36 florian
  907. * problem with with and classes fixed
  908. Revision 1.11 1998/09/17 09:42:18 peter
  909. + pass_2 for cg386
  910. * Message() -> CGMessage() for pass_1/pass_2
  911. Revision 1.10 1998/09/14 10:43:52 peter
  912. * all internal RTL functions start with FPC_
  913. Revision 1.9 1998/09/03 16:03:15 florian
  914. + rtti generation
  915. * init table generation changed
  916. Revision 1.8 1998/08/23 21:04:34 florian
  917. + rtti generation for classes added
  918. + new/dispose do now also a call to INITIALIZE/FINALIZE, if necessaray
  919. Revision 1.7 1998/08/20 11:27:40 michael
  920. * Applied Peters Fix
  921. Revision 1.6 1998/08/10 14:49:49 peter
  922. + localswitches, moduleswitches, globalswitches splitting
  923. Revision 1.5 1998/07/26 21:58:58 florian
  924. + better support for switch $H
  925. + index access to ansi strings added
  926. + assigment of data (records/arrays) containing ansi strings
  927. Revision 1.4 1998/07/24 22:16:55 florian
  928. * internal error 10 together with array access fixed. I hope
  929. that's the final fix.
  930. Revision 1.3 1998/06/25 08:48:09 florian
  931. * first version of rtti support
  932. Revision 1.2 1998/06/08 13:13:35 pierre
  933. + temporary variables now in temp_gen.pas unit
  934. because it is processor independent
  935. * mppc68k.bat modified to undefine i386 and support_mmx
  936. (which are defaults for i386)
  937. Revision 1.1 1998/06/05 17:44:13 peter
  938. * splitted cgi386
  939. }