cg386mem.pas 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. begin
  329. if (p^.left^.resulttype^.deftype=arraydef) then
  330. get_mul_size:=parraydef(p^.left^.resulttype)^.elesize
  331. else
  332. get_mul_size:=p^.resulttype^.size;
  333. end
  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. t : ptree;
  358. hp : preference;
  359. href : treference;
  360. tai : Paicpu;
  361. pushed : tpushed;
  362. hightree : ptree;
  363. hl,otl,ofl : pasmlabel;
  364. begin
  365. secondpass(p^.left);
  366. { we load the array reference to p^.location }
  367. { an ansistring needs to be dereferenced }
  368. if is_ansistring(p^.left^.resulttype) or
  369. is_widestring(p^.left^.resulttype) then
  370. begin
  371. reset_reference(p^.location.reference);
  372. if p^.callunique then
  373. begin
  374. if p^.left^.location.loc<>LOC_REFERENCE then
  375. begin
  376. CGMessage(cg_e_illegal_expression);
  377. exit;
  378. end;
  379. pushusedregisters(pushed,$ff);
  380. emitpushreferenceaddr(p^.left^.location.reference);
  381. if is_ansistring(p^.left^.resulttype) then
  382. emitcall('FPC_ANSISTR_UNIQUE')
  383. else
  384. emitcall('FPC_WIDESTR_UNIQUE');
  385. maybe_loadesi;
  386. popusedregisters(pushed);
  387. end;
  388. if p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  389. begin
  390. p^.location.reference.base:=p^.left^.location.register;
  391. end
  392. else
  393. begin
  394. del_reference(p^.left^.location.reference);
  395. p^.location.reference.base:=getregister32;
  396. emit_ref_reg(A_MOV,S_L,
  397. newreference(p^.left^.location.reference),
  398. p^.location.reference.base);
  399. end;
  400. { check for a zero length string,
  401. we can use the ansistring routine here }
  402. if (cs_check_range in aktlocalswitches) then
  403. begin
  404. pushusedregisters(pushed,$ff);
  405. emit_reg(A_PUSH,S_L,p^.location.reference.base);
  406. emitcall('FPC_ANSISTR_CHECKZERO');
  407. maybe_loadesi;
  408. popusedregisters(pushed);
  409. end;
  410. if is_ansistring(p^.left^.resulttype) then
  411. { in ansistrings S[1] is pchar(S)[0] !! }
  412. dec(p^.location.reference.offset)
  413. else
  414. begin
  415. { in widestrings S[1] is pwchar(S)[0] !! }
  416. dec(p^.location.reference.offset,2);
  417. emit_const_reg(A_SHL,S_L,
  418. 1,p^.location.reference.base);
  419. end;
  420. { we've also to keep left up-to-date, because it is used }
  421. { if a constant array index occurs, subject to change (FK) }
  422. set_location(p^.left^.location,p^.location);
  423. end
  424. else
  425. set_location(p^.location,p^.left^.location);
  426. { offset can only differ from 0 if arraydef }
  427. if p^.left^.resulttype^.deftype=arraydef then
  428. dec(p^.location.reference.offset,
  429. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  430. if p^.right^.treetype=ordconstn then
  431. begin
  432. { offset can only differ from 0 if arraydef }
  433. if (p^.left^.resulttype^.deftype=arraydef) then
  434. begin
  435. if not(is_open_array(p^.left^.resulttype)) and
  436. not(is_array_of_const(p^.left^.resulttype)) then
  437. begin
  438. if (p^.right^.value>parraydef(p^.left^.resulttype)^.highrange) or
  439. (p^.right^.value<parraydef(p^.left^.resulttype)^.lowrange) then
  440. begin
  441. if (cs_check_range in aktlocalswitches) then
  442. CGMessage(parser_e_range_check_error)
  443. else
  444. CGMessage(parser_w_range_check_error);
  445. end;
  446. dec(p^.left^.location.reference.offset,
  447. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  448. end
  449. else
  450. begin
  451. { range checking for open arrays !!!! }
  452. {!!!!!!!!!!!!!!!!!}
  453. end;
  454. end
  455. else if (p^.left^.resulttype^.deftype=stringdef) then
  456. begin
  457. if (p^.right^.value=0) and not(is_shortstring(p^.left^.resulttype)) then
  458. CGMessage(cg_e_can_access_element_zero);
  459. if (cs_check_range in aktlocalswitches) then
  460. case pstringdef(p^.left^.resulttype)^.string_typ of
  461. { it's the same for ansi- and wide strings }
  462. st_widestring,
  463. st_ansistring:
  464. begin
  465. pushusedregisters(pushed,$ff);
  466. push_int(p^.right^.value);
  467. hp:=newreference(p^.location.reference);
  468. dec(hp^.offset,7);
  469. emit_ref(A_PUSH,S_L,hp);
  470. emitcall('FPC_ANSISTR_RANGECHECK');
  471. popusedregisters(pushed);
  472. maybe_loadesi;
  473. end;
  474. st_shortstring:
  475. begin
  476. {!!!!!!!!!!!!!!!!!}
  477. end;
  478. st_longstring:
  479. begin
  480. {!!!!!!!!!!!!!!!!!}
  481. end;
  482. end;
  483. end;
  484. inc(p^.left^.location.reference.offset,
  485. get_mul_size*p^.right^.value);
  486. if p^.memseg then
  487. p^.left^.location.reference.segment:=R_FS;
  488. p^.left^.resulttype:=p^.resulttype;
  489. disposetree(p^.right);
  490. _p:=p^.left;
  491. putnode(p);
  492. p:=_p;
  493. end
  494. else
  495. { not treetype=ordconstn }
  496. begin
  497. { quick hack, to overcome Delphi 2 }
  498. if (cs_regalloc in aktglobalswitches) and
  499. (p^.left^.resulttype^.deftype=arraydef) then
  500. begin
  501. extraoffset:=0;
  502. if (p^.right^.treetype=addn) then
  503. begin
  504. if p^.right^.right^.treetype=ordconstn then
  505. begin
  506. extraoffset:=p^.right^.right^.value;
  507. t:=p^.right^.left;
  508. putnode(p^.right^.right);
  509. putnode(p^.right);
  510. p^.right:=t
  511. end
  512. else if p^.right^.left^.treetype=ordconstn then
  513. begin
  514. extraoffset:=p^.right^.left^.value;
  515. t:=p^.right^.right;
  516. putnode(p^.right^.left);
  517. putnode(p^.right);
  518. p^.right:=t
  519. end;
  520. end
  521. else if (p^.right^.treetype=subn) then
  522. begin
  523. if p^.right^.right^.treetype=ordconstn then
  524. begin
  525. { this was "extraoffset:=p^.right^.right^.value;" Looks a bit like
  526. copy-paste bug :) (JM) }
  527. extraoffset:=-p^.right^.right^.value;
  528. t:=p^.right^.left;
  529. putnode(p^.right^.right);
  530. putnode(p^.right);
  531. p^.right:=t
  532. end
  533. { You also have to negate p^.right^.right in this case! I can't add an
  534. unaryminusn without causing a crash, so I've disabled it (JM)
  535. else if p^.right^.left^.treetype=ordconstn then
  536. begin
  537. extraoffset:=p^.right^.left^.value;
  538. t:=p^.right^.right;
  539. putnode(p^.right);
  540. putnode(p^.right^.left);
  541. p^.right:=t;
  542. end;}
  543. end;
  544. inc(p^.location.reference.offset,
  545. get_mul_size*extraoffset);
  546. end;
  547. { calculate from left to right }
  548. if (p^.location.loc<>LOC_REFERENCE) and
  549. (p^.location.loc<>LOC_MEM) then
  550. CGMessage(cg_e_illegal_expression);
  551. if (p^.right^.location.loc=LOC_JUMP) then
  552. begin
  553. otl:=truelabel;
  554. getlabel(truelabel);
  555. ofl:=falselabel;
  556. getlabel(falselabel);
  557. end;
  558. is_pushed:=maybe_push(p^.right^.registers32,p,false);
  559. secondpass(p^.right);
  560. if is_pushed then
  561. restore(p,false);
  562. { here we change the location of p^.right
  563. and the update was forgotten so it
  564. led to wrong code in emitrangecheck later PM
  565. so make range check before }
  566. if cs_check_range in aktlocalswitches then
  567. begin
  568. if p^.left^.resulttype^.deftype=arraydef then
  569. begin
  570. if is_open_array(p^.left^.resulttype) or
  571. is_array_of_const(p^.left^.resulttype) then
  572. begin
  573. reset_reference(href);
  574. parraydef(p^.left^.resulttype)^.genrangecheck;
  575. href.symbol:=newasmsymbol(parraydef(p^.left^.resulttype)^.getrangecheckstring);
  576. href.offset:=4;
  577. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  578. hightree:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  579. firstpass(hightree);
  580. secondpass(hightree);
  581. emit_mov_loc_ref(hightree^.location,href,S_L,true);
  582. disposetree(hightree);
  583. end;
  584. emitrangecheck(p^.right,p^.left^.resulttype);
  585. end;
  586. end;
  587. case p^.right^.location.loc of
  588. LOC_REGISTER:
  589. begin
  590. ind:=p^.right^.location.register;
  591. case p^.right^.resulttype^.size of
  592. 1:
  593. begin
  594. hr:=reg8toreg32(ind);
  595. emit_reg_reg(A_MOVZX,S_BL,ind,hr);
  596. ind:=hr;
  597. end;
  598. 2:
  599. begin
  600. hr:=reg16toreg32(ind);
  601. emit_reg_reg(A_MOVZX,S_WL,ind,hr);
  602. ind:=hr;
  603. end;
  604. end;
  605. end;
  606. LOC_CREGISTER:
  607. begin
  608. ind:=getregister32;
  609. case p^.right^.resulttype^.size of
  610. 1:
  611. emit_reg_reg(A_MOVZX,S_BL,p^.right^.location.register,ind);
  612. 2:
  613. emit_reg_reg(A_MOVZX,S_WL,p^.right^.location.register,ind);
  614. 4:
  615. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,ind);
  616. end;
  617. end;
  618. LOC_FLAGS:
  619. begin
  620. ind:=getregister32;
  621. emit_flag2reg(p^.right^.location.resflags,reg32toreg8(ind));
  622. emit_reg_reg(A_MOVZX,S_BL,reg32toreg8(ind),ind);
  623. end;
  624. LOC_JUMP :
  625. begin
  626. ind:=getregister32;
  627. emitlab(truelabel);
  628. truelabel:=otl;
  629. emit_const_reg(A_MOV,S_L,1,ind);
  630. getlabel(hl);
  631. emitjmp(C_None,hl);
  632. emitlab(falselabel);
  633. falselabel:=ofl;
  634. emit_reg_reg(A_XOR,S_L,ind,ind);
  635. emitlab(hl);
  636. end;
  637. LOC_REFERENCE,LOC_MEM :
  638. begin
  639. del_reference(p^.right^.location.reference);
  640. ind:=getregister32;
  641. { Booleans are stored in an 8 bit memory location, so
  642. the use of MOVL is not correct }
  643. case p^.right^.resulttype^.size of
  644. 1 : tai:=new(paicpu,op_ref_reg(A_MOVZX,S_BL,newreference(p^.right^.location.reference),ind));
  645. 2 : tai:=new(Paicpu,op_ref_reg(A_MOVZX,S_WL,newreference(p^.right^.location.reference),ind));
  646. 4 : tai:=new(Paicpu,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),ind));
  647. end;
  648. exprasmlist^.concat(tai);
  649. end;
  650. else
  651. internalerror(5913428);
  652. end;
  653. { produce possible range check code: }
  654. if cs_check_range in aktlocalswitches then
  655. begin
  656. if p^.left^.resulttype^.deftype=arraydef then
  657. begin
  658. { done defore (PM) }
  659. end
  660. else if (p^.left^.resulttype^.deftype=stringdef) then
  661. begin
  662. case pstringdef(p^.left^.resulttype)^.string_typ of
  663. { it's the same for ansi- and wide strings }
  664. st_widestring,
  665. st_ansistring:
  666. begin
  667. pushusedregisters(pushed,$ff);
  668. emit_reg(A_PUSH,S_L,ind);
  669. hp:=newreference(p^.location.reference);
  670. dec(hp^.offset,7);
  671. emit_ref(A_PUSH,S_L,hp);
  672. emitcall('FPC_ANSISTR_RANGECHECK');
  673. popusedregisters(pushed);
  674. maybe_loadesi;
  675. end;
  676. st_shortstring:
  677. begin
  678. {!!!!!!!!!!!!!!!!!}
  679. end;
  680. st_longstring:
  681. begin
  682. {!!!!!!!!!!!!!!!!!}
  683. end;
  684. end;
  685. end;
  686. end;
  687. if p^.location.reference.index=R_NO then
  688. begin
  689. p^.location.reference.index:=ind;
  690. calc_emit_mul;
  691. end
  692. else
  693. begin
  694. if p^.location.reference.base=R_NO then
  695. begin
  696. case p^.location.reference.scalefactor of
  697. 2 : emit_const_reg(A_SHL,S_L,1,p^.location.reference.index);
  698. 4 : emit_const_reg(A_SHL,S_L,2,p^.location.reference.index);
  699. 8 : emit_const_reg(A_SHL,S_L,3,p^.location.reference.index);
  700. end;
  701. calc_emit_mul;
  702. p^.location.reference.base:=p^.location.reference.index;
  703. p^.location.reference.index:=ind;
  704. end
  705. else
  706. begin
  707. emit_ref_reg(
  708. A_LEA,S_L,newreference(p^.location.reference),
  709. p^.location.reference.index);
  710. ungetregister32(p^.location.reference.base);
  711. { the symbol offset is loaded, }
  712. { so release the symbol name and set symbol }
  713. { to nil }
  714. p^.location.reference.symbol:=nil;
  715. p^.location.reference.offset:=0;
  716. calc_emit_mul;
  717. p^.location.reference.base:=p^.location.reference.index;
  718. p^.location.reference.index:=ind;
  719. end;
  720. end;
  721. if p^.memseg then
  722. p^.location.reference.segment:=R_FS;
  723. end;
  724. end;
  725. {*****************************************************************************
  726. SecondSelfN
  727. *****************************************************************************}
  728. procedure secondselfn(var p : ptree);
  729. begin
  730. reset_reference(p^.location.reference);
  731. getexplicitregister32(R_ESI);
  732. if (p^.resulttype^.deftype=classrefdef) or
  733. ((p^.resulttype^.deftype=objectdef)
  734. and pobjectdef(p^.resulttype)^.is_class
  735. ) then
  736. p^.location.register:=R_ESI
  737. else
  738. p^.location.reference.base:=R_ESI;
  739. end;
  740. {*****************************************************************************
  741. SecondWithN
  742. *****************************************************************************}
  743. procedure secondwith(var p : ptree);
  744. var
  745. usetemp,with_expr_in_temp : boolean;
  746. {$ifdef GDB}
  747. withstartlabel,withendlabel : pasmlabel;
  748. pp : pchar;
  749. mangled_length : longint;
  750. const
  751. withlevel : longint = 0;
  752. {$endif GDB}
  753. begin
  754. if assigned(p^.left) then
  755. begin
  756. secondpass(p^.left);
  757. if p^.left^.location.reference.segment<>R_NO then
  758. message(parser_e_no_with_for_variable_in_other_segments);
  759. new(p^.withreference);
  760. usetemp:=false;
  761. if (p^.left^.treetype=loadn) and
  762. (p^.left^.symtable=aktprocsym^.definition^.localst) then
  763. begin
  764. { for locals use the local storage }
  765. p^.withreference^:=p^.left^.location.reference;
  766. p^.islocal:=true;
  767. end
  768. else
  769. { call can have happend with a property }
  770. if (p^.left^.resulttype^.deftype=objectdef) and
  771. pobjectdef(p^.left^.resulttype)^.is_class then
  772. begin
  773. {$ifndef noAllocEdi}
  774. getexplicitregister32(R_EDI);
  775. {$endif noAllocEdi}
  776. emit_mov_loc_reg(p^.left^.location,R_EDI);
  777. usetemp:=true;
  778. end
  779. else
  780. begin
  781. {$ifndef noAllocEdi}
  782. getexplicitregister32(R_EDI);
  783. {$endif noAllocEdi}
  784. emit_lea_loc_reg(p^.left^.location,R_EDI,false);
  785. usetemp:=true;
  786. end;
  787. release_loc(p^.left^.location);
  788. { if the with expression is stored in a temp }
  789. { area we must make it persistent and shouldn't }
  790. { release it (FK) }
  791. if (p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  792. istemp(p^.left^.location.reference) then
  793. begin
  794. normaltemptopersistant(p^.left^.location.reference.offset);
  795. with_expr_in_temp:=true;
  796. end
  797. else
  798. with_expr_in_temp:=false;
  799. { if usetemp is set the value must be in %edi }
  800. if usetemp then
  801. begin
  802. gettempofsizereference(4,p^.withreference^);
  803. normaltemptopersistant(p^.withreference^.offset);
  804. { move to temp reference }
  805. emit_reg_ref(A_MOV,S_L,R_EDI,newreference(p^.withreference^));
  806. {$ifndef noAllocEdi}
  807. ungetregister32(R_EDI);
  808. {$endif noAllocEdi}
  809. {$ifdef GDB}
  810. if (cs_debuginfo in aktmoduleswitches) then
  811. begin
  812. inc(withlevel);
  813. getlabel(withstartlabel);
  814. getlabel(withendlabel);
  815. emitlab(withstartlabel);
  816. withdebuglist^.concat(new(pai_stabs,init(strpnew(
  817. '"with'+tostr(withlevel)+':'+tostr(symtablestack^.getnewtypecount)+
  818. '=*'+p^.left^.resulttype^.numberstring+'",'+
  819. tostr(N_LSYM)+',0,0,'+tostr(p^.withreference^.offset)))));
  820. mangled_length:=length(aktprocsym^.definition^.mangledname);
  821. getmem(pp,mangled_length+50);
  822. strpcopy(pp,'192,0,0,'+withstartlabel^.name);
  823. if (target_os.use_function_relative_addresses) then
  824. begin
  825. strpcopy(strend(pp),'-');
  826. strpcopy(strend(pp),aktprocsym^.definition^.mangledname);
  827. end;
  828. withdebuglist^.concat(new(pai_stabn,init(strnew(pp))));
  829. end;
  830. {$endif GDB}
  831. del_reference(p^.left^.location.reference);
  832. end;
  833. { p^.right can be optimize out !!! }
  834. if assigned(p^.right) then
  835. secondpass(p^.right);
  836. if usetemp then
  837. begin
  838. ungetpersistanttemp(p^.withreference^.offset);
  839. {$ifdef GDB}
  840. if (cs_debuginfo in aktmoduleswitches) then
  841. begin
  842. emitlab(withendlabel);
  843. strpcopy(pp,'224,0,0,'+withendlabel^.name);
  844. if (target_os.use_function_relative_addresses) then
  845. begin
  846. strpcopy(strend(pp),'-');
  847. strpcopy(strend(pp),aktprocsym^.definition^.mangledname);
  848. end;
  849. withdebuglist^.concat(new(pai_stabn,init(strnew(pp))));
  850. freemem(pp,mangled_length+50);
  851. dec(withlevel);
  852. end;
  853. {$endif GDB}
  854. end;
  855. if with_expr_in_temp then
  856. ungetpersistanttemp(p^.left^.location.reference.offset);
  857. dispose(p^.withreference);
  858. p^.withreference:=nil;
  859. end;
  860. end;
  861. end.
  862. {
  863. $Log$
  864. Revision 1.74 2000-04-01 14:18:44 peter
  865. * use arraydef.elesize instead of elementtype.def.size
  866. Revision 1.73 2000/03/19 11:55:08 peter
  867. * fixed temp ansi handling within array constructor
  868. Revision 1.72 2000/02/18 20:53:14 pierre
  869. * fixes a stabs problem for functions
  870. + includes a stabs local var for with statements
  871. the name is with in lowercase followed by an index
  872. for nested with.
  873. + Withdebuglist added because the stabs declarations of local
  874. var are postponed to end of function.
  875. Revision 1.71 2000/02/09 18:08:33 jonas
  876. * added regallocs for esi
  877. Revision 1.70 2000/02/09 13:22:47 peter
  878. * log truncated
  879. Revision 1.69 2000/01/09 15:19:23 peter
  880. * fixed misplaced getexplicitreg(r_edi) which broke make cycle
  881. Revision 1.68 2000/01/09 12:35:02 jonas
  882. * changed edi allocation to use getexplicitregister32/ungetregister
  883. (adapted tgeni386 a bit for this) and enabled it by default
  884. * fixed very big and stupid bug of mine in cg386mat that broke the
  885. include() code (and make cycle :( ) if you compiled without
  886. -dnewoptimizations
  887. Revision 1.67 2000/01/09 01:44:20 jonas
  888. + (de)allocation info for EDI to fix reported bug on mailinglist.
  889. Also some (de)allocation info for ESI added. Between -dallocEDI
  890. because at this time of the night bugs could easily slip in ;)
  891. Revision 1.66 2000/01/07 01:14:21 peter
  892. * updated copyright to 2000
  893. Revision 1.65 2000/01/04 15:15:50 florian
  894. + added compiler switch $maxfpuregisters
  895. + fixed a small problem in secondvecn
  896. Revision 1.64 2000/01/03 17:10:39 jonas
  897. * fixed "quick hack, to overcome Delphi 2" :)
  898. Revision 1.63 1999/12/01 12:42:32 peter
  899. * fixed bug 698
  900. * removed some notes about unused vars
  901. Revision 1.62 1999/11/30 10:40:43 peter
  902. + ttype, tsymlist
  903. Revision 1.61 1999/11/15 21:54:38 peter
  904. * LOC_JUMP support for vecn
  905. Revision 1.60 1999/11/06 14:34:18 peter
  906. * truncated log to 20 revs
  907. Revision 1.59 1999/10/30 17:35:26 peter
  908. * fpc_freemem fpc_getmem new callings updated
  909. Revision 1.58 1999/09/17 17:14:02 peter
  910. * @procvar fixes for tp mode
  911. * @<id>:= gives now an error
  912. Revision 1.57 1999/09/14 07:59:46 florian
  913. * finally!? fixed
  914. with <function with result in temp> do
  915. My last and also Peter's fix before were wrong :(
  916. Revision 1.56 1999/09/13 20:49:41 florian
  917. * hopefully an error in Peter's previous commit fixed
  918. Revision 1.55 1999/09/10 15:42:50 peter
  919. * fixed with <calln> do
  920. * fixed finalize/initialize call for new/dispose
  921. Revision 1.54 1999/08/25 11:59:46 jonas
  922. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  923. Revision 1.53 1999/08/23 23:49:21 pierre
  924. * hnewn location corrected
  925. Revision 1.52 1999/08/19 13:08:52 pierre
  926. * emit_??? used
  927. Revision 1.51 1999/08/16 23:20:28 peter
  928. * range check for array of const
  929. Revision 1.50 1999/08/14 00:36:05 peter
  930. * array constructor support
  931. }