cg386mem.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. hl,otl,ofl : pasmlabel;
  357. begin
  358. secondpass(p^.left);
  359. rl:=p^.left^.resulttype;
  360. { we load the array reference to p^.location }
  361. { an ansistring needs to be dereferenced }
  362. if is_ansistring(p^.left^.resulttype) or
  363. is_widestring(p^.left^.resulttype) then
  364. begin
  365. reset_reference(p^.location.reference);
  366. if p^.callunique then
  367. begin
  368. if p^.left^.location.loc<>LOC_REFERENCE then
  369. begin
  370. CGMessage(cg_e_illegal_expression);
  371. exit;
  372. end;
  373. pushusedregisters(pushed,$ff);
  374. emitpushreferenceaddr(p^.left^.location.reference);
  375. if is_ansistring(p^.left^.resulttype) then
  376. emitcall('FPC_ANSISTR_UNIQUE')
  377. else
  378. emitcall('FPC_WIDESTR_UNIQUE');
  379. maybe_loadesi;
  380. popusedregisters(pushed);
  381. end;
  382. if p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  383. begin
  384. p^.location.reference.base:=p^.left^.location.register;
  385. end
  386. else
  387. begin
  388. del_reference(p^.left^.location.reference);
  389. p^.location.reference.base:=getregister32;
  390. emit_ref_reg(A_MOV,S_L,
  391. newreference(p^.left^.location.reference),
  392. p^.location.reference.base);
  393. end;
  394. { check for a zero length string,
  395. we can use the ansistring routine here }
  396. if (cs_check_range in aktlocalswitches) then
  397. begin
  398. pushusedregisters(pushed,$ff);
  399. emit_reg(A_PUSH,S_L,p^.location.reference.base);
  400. emitcall('FPC_ANSISTR_CHECKZERO');
  401. maybe_loadesi;
  402. popusedregisters(pushed);
  403. end;
  404. if is_ansistring(p^.left^.resulttype) then
  405. { in ansistrings S[1] is pchar(S)[0] !! }
  406. dec(p^.location.reference.offset)
  407. else
  408. begin
  409. { in widestrings S[1] is pwchar(S)[0] !! }
  410. dec(p^.location.reference.offset,2);
  411. emit_const_reg(A_SHL,S_L,
  412. 1,p^.location.reference.base);
  413. end;
  414. { we've also to keep left up-to-date, because it is used }
  415. { if a constant array index occurs, subject to change (FK) }
  416. set_location(p^.left^.location,p^.location);
  417. end
  418. else
  419. set_location(p^.location,p^.left^.location);
  420. { offset can only differ from 0 if arraydef }
  421. if p^.left^.resulttype^.deftype=arraydef then
  422. dec(p^.location.reference.offset,
  423. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  424. if p^.right^.treetype=ordconstn then
  425. begin
  426. { offset can only differ from 0 if arraydef }
  427. if (p^.left^.resulttype^.deftype=arraydef) then
  428. begin
  429. if not(is_open_array(p^.left^.resulttype)) and
  430. not(is_array_of_const(p^.left^.resulttype)) then
  431. begin
  432. if (p^.right^.value>parraydef(p^.left^.resulttype)^.highrange) or
  433. (p^.right^.value<parraydef(p^.left^.resulttype)^.lowrange) then
  434. begin
  435. if (cs_check_range in aktlocalswitches) then
  436. CGMessage(parser_e_range_check_error)
  437. else
  438. CGMessage(parser_w_range_check_error);
  439. end;
  440. dec(p^.left^.location.reference.offset,
  441. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  442. end
  443. else
  444. begin
  445. { range checking for open arrays !!!! }
  446. {!!!!!!!!!!!!!!!!!}
  447. end;
  448. end
  449. else if (p^.left^.resulttype^.deftype=stringdef) then
  450. begin
  451. if (p^.right^.value=0) and not(is_shortstring(p^.left^.resulttype)) then
  452. CGMessage(cg_e_can_access_element_zero);
  453. if (cs_check_range in aktlocalswitches) then
  454. case pstringdef(p^.left^.resulttype)^.string_typ of
  455. { it's the same for ansi- and wide strings }
  456. st_widestring,
  457. st_ansistring:
  458. begin
  459. pushusedregisters(pushed,$ff);
  460. push_int(p^.right^.value);
  461. hp:=newreference(p^.location.reference);
  462. dec(hp^.offset,7);
  463. emit_ref(A_PUSH,S_L,hp);
  464. emitcall('FPC_ANSISTR_RANGECHECK');
  465. popusedregisters(pushed);
  466. maybe_loadesi;
  467. end;
  468. st_shortstring:
  469. begin
  470. {!!!!!!!!!!!!!!!!!}
  471. end;
  472. st_longstring:
  473. begin
  474. {!!!!!!!!!!!!!!!!!}
  475. end;
  476. end;
  477. end;
  478. inc(p^.left^.location.reference.offset,
  479. get_mul_size*p^.right^.value);
  480. if p^.memseg then
  481. p^.left^.location.reference.segment:=R_FS;
  482. p^.left^.resulttype:=p^.resulttype;
  483. disposetree(p^.right);
  484. _p:=p^.left;
  485. putnode(p);
  486. p:=_p;
  487. end
  488. else
  489. { not treetype=ordconstn }
  490. begin
  491. { quick hack, to overcome Delphi 2 }
  492. if (cs_regalloc in aktglobalswitches) and
  493. (p^.left^.resulttype^.deftype=arraydef) then
  494. begin
  495. extraoffset:=0;
  496. if (p^.right^.treetype=addn) then
  497. begin
  498. if p^.right^.right^.treetype=ordconstn then
  499. begin
  500. extraoffset:=p^.right^.right^.value;
  501. t:=p^.right^.left;
  502. putnode(p^.right);
  503. putnode(p^.right^.right);
  504. p^.right:=t
  505. end
  506. else if p^.right^.left^.treetype=ordconstn then
  507. begin
  508. extraoffset:=p^.right^.left^.value;
  509. t:=p^.right^.right;
  510. putnode(p^.right);
  511. putnode(p^.right^.left);
  512. p^.right:=t
  513. end;
  514. end
  515. else if (p^.right^.treetype=subn) then
  516. begin
  517. if p^.right^.right^.treetype=ordconstn then
  518. begin
  519. extraoffset:=p^.right^.right^.value;
  520. t:=p^.right^.left;
  521. putnode(p^.right);
  522. putnode(p^.right^.right);
  523. p^.right:=t
  524. end
  525. else if p^.right^.left^.treetype=ordconstn then
  526. begin
  527. extraoffset:=p^.right^.left^.value;
  528. t:=p^.right^.right;
  529. putnode(p^.right);
  530. putnode(p^.right^.left);
  531. p^.right:=t
  532. end;
  533. end;
  534. inc(p^.location.reference.offset,
  535. get_mul_size*extraoffset);
  536. end;
  537. { calculate from left to right }
  538. if (p^.location.loc<>LOC_REFERENCE) and
  539. (p^.location.loc<>LOC_MEM) then
  540. CGMessage(cg_e_illegal_expression);
  541. if (p^.right^.location.loc=LOC_JUMP) then
  542. begin
  543. otl:=truelabel;
  544. getlabel(truelabel);
  545. ofl:=falselabel;
  546. getlabel(falselabel);
  547. end;
  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. LOC_JUMP :
  615. begin
  616. ind:=getregister32;
  617. emitlab(truelabel);
  618. truelabel:=otl;
  619. emit_const_reg(A_MOV,S_L,1,ind);
  620. getlabel(hl);
  621. emitjmp(C_None,hl);
  622. emitlab(falselabel);
  623. falselabel:=ofl;
  624. emit_reg_reg(A_XOR,S_L,ind,ind);
  625. emitlab(hl);
  626. end;
  627. LOC_REFERENCE,LOC_MEM :
  628. begin
  629. del_reference(p^.right^.location.reference);
  630. ind:=getregister32;
  631. { Booleans are stored in an 8 bit memory location, so
  632. the use of MOVL is not correct }
  633. case p^.right^.resulttype^.size of
  634. 1 : tai:=new(paicpu,op_ref_reg(A_MOVZX,S_BL,newreference(p^.right^.location.reference),ind));
  635. 2 : tai:=new(Paicpu,op_ref_reg(A_MOVZX,S_WL,newreference(p^.right^.location.reference),ind));
  636. 4 : tai:=new(Paicpu,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),ind));
  637. end;
  638. exprasmlist^.concat(tai);
  639. end;
  640. else
  641. internalerror(5913428);
  642. end;
  643. { produce possible range check code: }
  644. if cs_check_range in aktlocalswitches then
  645. begin
  646. if p^.left^.resulttype^.deftype=arraydef then
  647. begin
  648. { done defore (PM) }
  649. end
  650. else if (p^.left^.resulttype^.deftype=stringdef) then
  651. begin
  652. case pstringdef(p^.left^.resulttype)^.string_typ of
  653. { it's the same for ansi- and wide strings }
  654. st_widestring,
  655. st_ansistring:
  656. begin
  657. pushusedregisters(pushed,$ff);
  658. emit_reg(A_PUSH,S_L,ind);
  659. hp:=newreference(p^.location.reference);
  660. dec(hp^.offset,7);
  661. emit_ref(A_PUSH,S_L,hp);
  662. emitcall('FPC_ANSISTR_RANGECHECK');
  663. popusedregisters(pushed);
  664. maybe_loadesi;
  665. end;
  666. st_shortstring:
  667. begin
  668. {!!!!!!!!!!!!!!!!!}
  669. end;
  670. st_longstring:
  671. begin
  672. {!!!!!!!!!!!!!!!!!}
  673. end;
  674. end;
  675. end;
  676. end;
  677. if p^.location.reference.index=R_NO then
  678. begin
  679. p^.location.reference.index:=ind;
  680. calc_emit_mul;
  681. end
  682. else
  683. begin
  684. if p^.location.reference.base=R_NO then
  685. begin
  686. case p^.location.reference.scalefactor of
  687. 2 : emit_const_reg(A_SHL,S_L,1,p^.location.reference.index);
  688. 4 : emit_const_reg(A_SHL,S_L,2,p^.location.reference.index);
  689. 8 : emit_const_reg(A_SHL,S_L,3,p^.location.reference.index);
  690. end;
  691. calc_emit_mul;
  692. p^.location.reference.base:=p^.location.reference.index;
  693. p^.location.reference.index:=ind;
  694. end
  695. else
  696. begin
  697. emit_ref_reg(
  698. A_LEA,S_L,newreference(p^.location.reference),
  699. p^.location.reference.index);
  700. ungetregister32(p^.location.reference.base);
  701. { the symbol offset is loaded, }
  702. { so release the symbol name and set symbol }
  703. { to nil }
  704. p^.location.reference.symbol:=nil;
  705. p^.location.reference.offset:=0;
  706. calc_emit_mul;
  707. p^.location.reference.base:=p^.location.reference.index;
  708. p^.location.reference.index:=ind;
  709. end;
  710. end;
  711. if p^.memseg then
  712. p^.location.reference.segment:=R_FS;
  713. end;
  714. end;
  715. {*****************************************************************************
  716. SecondSelfN
  717. *****************************************************************************}
  718. procedure secondselfn(var p : ptree);
  719. begin
  720. reset_reference(p^.location.reference);
  721. if (p^.resulttype^.deftype=classrefdef) or
  722. ((p^.resulttype^.deftype=objectdef)
  723. and pobjectdef(p^.resulttype)^.is_class
  724. ) then
  725. p^.location.register:=R_ESI
  726. else
  727. p^.location.reference.base:=R_ESI;
  728. end;
  729. {*****************************************************************************
  730. SecondWithN
  731. *****************************************************************************}
  732. procedure secondwith(var p : ptree);
  733. var
  734. usetemp,with_expr_in_temp : boolean;
  735. begin
  736. if assigned(p^.left) then
  737. begin
  738. secondpass(p^.left);
  739. if p^.left^.location.reference.segment<>R_NO then
  740. message(parser_e_no_with_for_variable_in_other_segments);
  741. new(p^.withreference);
  742. usetemp:=false;
  743. if (p^.left^.treetype=loadn) and
  744. (p^.left^.symtable=aktprocsym^.definition^.localst) then
  745. begin
  746. { for locals use the local storage }
  747. p^.withreference^:=p^.left^.location.reference;
  748. p^.islocal:=true;
  749. end
  750. else
  751. { call can happend with a property }
  752. if { (p^.left^.treetype=calln) and Don't think that
  753. this is necessary (FK) }
  754. (p^.left^.resulttype^.deftype=objectdef) and
  755. pobjectdef(p^.left^.resulttype)^.is_class then
  756. begin
  757. emit_mov_loc_reg(p^.left^.location,R_EDI);
  758. usetemp:=true;
  759. end
  760. else
  761. begin
  762. emit_lea_loc_reg(p^.left^.location,R_EDI,false);
  763. usetemp:=true;
  764. end;
  765. release_loc(p^.left^.location);
  766. { if the with expression is stored in a temp }
  767. { area we must make it persistent and shouldn't }
  768. { release it (FK) }
  769. if (p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  770. istemp(p^.left^.location.reference) then
  771. begin
  772. normaltemptopersistant(p^.left^.location.reference.offset);
  773. with_expr_in_temp:=true;
  774. end
  775. else
  776. with_expr_in_temp:=false;
  777. { if usetemp is set the value must be in %edi }
  778. if usetemp then
  779. begin
  780. gettempofsizereference(4,p^.withreference^);
  781. normaltemptopersistant(p^.withreference^.offset);
  782. { move to temp reference }
  783. emit_reg_ref(A_MOV,S_L,
  784. R_EDI,newreference(p^.withreference^));
  785. del_reference(p^.left^.location.reference);
  786. end;
  787. { p^.right can be optimize out !!! }
  788. if assigned(p^.right) then
  789. secondpass(p^.right);
  790. if usetemp then
  791. ungetpersistanttemp(p^.withreference^.offset);
  792. if with_expr_in_temp then
  793. ungetpersistanttemp(p^.left^.location.reference.offset);
  794. dispose(p^.withreference);
  795. p^.withreference:=nil;
  796. end;
  797. end;
  798. end.
  799. {
  800. $Log$
  801. Revision 1.61 1999-11-15 21:54:38 peter
  802. * LOC_JUMP support for vecn
  803. Revision 1.60 1999/11/06 14:34:18 peter
  804. * truncated log to 20 revs
  805. Revision 1.59 1999/10/30 17:35:26 peter
  806. * fpc_freemem fpc_getmem new callings updated
  807. Revision 1.58 1999/09/17 17:14:02 peter
  808. * @procvar fixes for tp mode
  809. * @<id>:= gives now an error
  810. Revision 1.57 1999/09/14 07:59:46 florian
  811. * finally!? fixed
  812. with <function with result in temp> do
  813. My last and also Peter's fix before were wrong :(
  814. Revision 1.56 1999/09/13 20:49:41 florian
  815. * hopefully an error in Peter's previous commit fixed
  816. Revision 1.55 1999/09/10 15:42:50 peter
  817. * fixed with <calln> do
  818. * fixed finalize/initialize call for new/dispose
  819. Revision 1.54 1999/08/25 11:59:46 jonas
  820. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  821. Revision 1.53 1999/08/23 23:49:21 pierre
  822. * hnewn location corrected
  823. Revision 1.52 1999/08/19 13:08:52 pierre
  824. * emit_??? used
  825. Revision 1.51 1999/08/16 23:20:28 peter
  826. * range check for array of const
  827. Revision 1.50 1999/08/14 00:36:05 peter
  828. * array constructor support
  829. Revision 1.49 1999/08/04 00:22:53 florian
  830. * renamed i386asm and i386base to cpuasm and cpubase
  831. Revision 1.48 1999/08/03 22:02:47 peter
  832. * moved bitmask constants to sets
  833. * some other type/const renamings
  834. Revision 1.47 1999/06/02 10:11:45 florian
  835. * make cycle fixed i.e. compilation with 0.99.10
  836. * some fixes for qword
  837. * start of register calling conventions
  838. Revision 1.46 1999/05/27 19:44:17 peter
  839. * removed oldasm
  840. * plabel -> pasmlabel
  841. * -a switches to source writing automaticly
  842. * assembler readers OOPed
  843. * asmsymbol automaticly external
  844. * jumptables and other label fixes for asm readers
  845. Revision 1.45 1999/05/23 18:42:04 florian
  846. * better error recovering in typed constants
  847. * some problems with arrays of const fixed, some problems
  848. due my previous
  849. - the location type of array constructor is now LOC_MEM
  850. - the pushing of high fixed
  851. - parameter copying fixed
  852. - zero temp. allocation removed
  853. * small problem in the assembler writers fixed:
  854. ref to nil wasn't written correctly
  855. Revision 1.44 1999/05/21 13:54:53 peter
  856. * NEWLAB for label as symbol
  857. Revision 1.43 1999/05/19 16:48:21 florian
  858. * tdef.typename: returns a now a proper type name for the most types
  859. Revision 1.42 1999/05/18 22:11:52 pierre
  860. * checkpointer code was wrong!
  861. Revision 1.41 1999/05/18 21:58:29 florian
  862. * fixed some bugs related to temp. ansistrings and functions results
  863. which return records/objects/arrays which need init/final.
  864. Revision 1.40 1999/05/18 14:15:26 peter
  865. * containsself fixes
  866. * checktypes()
  867. }