cg386mem.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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.60 1999-11-06 14:34:18 peter
  779. * truncated log to 20 revs
  780. Revision 1.59 1999/10/30 17:35:26 peter
  781. * fpc_freemem fpc_getmem new callings updated
  782. Revision 1.58 1999/09/17 17:14:02 peter
  783. * @procvar fixes for tp mode
  784. * @<id>:= gives now an error
  785. Revision 1.57 1999/09/14 07:59:46 florian
  786. * finally!? fixed
  787. with <function with result in temp> do
  788. My last and also Peter's fix before were wrong :(
  789. Revision 1.56 1999/09/13 20:49:41 florian
  790. * hopefully an error in Peter's previous commit fixed
  791. Revision 1.55 1999/09/10 15:42:50 peter
  792. * fixed with <calln> do
  793. * fixed finalize/initialize call for new/dispose
  794. Revision 1.54 1999/08/25 11:59:46 jonas
  795. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  796. Revision 1.53 1999/08/23 23:49:21 pierre
  797. * hnewn location corrected
  798. Revision 1.52 1999/08/19 13:08:52 pierre
  799. * emit_??? used
  800. Revision 1.51 1999/08/16 23:20:28 peter
  801. * range check for array of const
  802. Revision 1.50 1999/08/14 00:36:05 peter
  803. * array constructor support
  804. Revision 1.49 1999/08/04 00:22:53 florian
  805. * renamed i386asm and i386base to cpuasm and cpubase
  806. Revision 1.48 1999/08/03 22:02:47 peter
  807. * moved bitmask constants to sets
  808. * some other type/const renamings
  809. Revision 1.47 1999/06/02 10:11:45 florian
  810. * make cycle fixed i.e. compilation with 0.99.10
  811. * some fixes for qword
  812. * start of register calling conventions
  813. Revision 1.46 1999/05/27 19:44:17 peter
  814. * removed oldasm
  815. * plabel -> pasmlabel
  816. * -a switches to source writing automaticly
  817. * assembler readers OOPed
  818. * asmsymbol automaticly external
  819. * jumptables and other label fixes for asm readers
  820. Revision 1.45 1999/05/23 18:42:04 florian
  821. * better error recovering in typed constants
  822. * some problems with arrays of const fixed, some problems
  823. due my previous
  824. - the location type of array constructor is now LOC_MEM
  825. - the pushing of high fixed
  826. - parameter copying fixed
  827. - zero temp. allocation removed
  828. * small problem in the assembler writers fixed:
  829. ref to nil wasn't written correctly
  830. Revision 1.44 1999/05/21 13:54:53 peter
  831. * NEWLAB for label as symbol
  832. Revision 1.43 1999/05/19 16:48:21 florian
  833. * tdef.typename: returns a now a proper type name for the most types
  834. Revision 1.42 1999/05/18 22:11:52 pierre
  835. * checkpointer code was wrong!
  836. Revision 1.41 1999/05/18 21:58:29 florian
  837. * fixed some bugs related to temp. ansistrings and functions results
  838. which return records/objects/arrays which need init/final.
  839. Revision 1.40 1999/05/18 14:15:26 peter
  840. * containsself fixes
  841. * checktypes()
  842. }