cg386mem.pas 39 KB

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