cg386mem.pas 37 KB

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