cg386mem.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Generate i386 assembler for in memory related nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cg386mem;
  19. interface
  20. uses
  21. tree;
  22. procedure secondloadvmt(var p : ptree);
  23. procedure secondhnewn(var p : ptree);
  24. procedure secondnewn(var p : ptree);
  25. procedure secondhdisposen(var p : ptree);
  26. procedure secondsimplenewdispose(var p : ptree);
  27. procedure secondaddr(var p : ptree);
  28. procedure seconddoubleaddr(var p : ptree);
  29. procedure secondderef(var p : ptree);
  30. procedure secondsubscriptn(var p : ptree);
  31. procedure secondvecn(var p : ptree);
  32. procedure secondselfn(var p : ptree);
  33. procedure secondwith(var p : ptree);
  34. implementation
  35. uses
  36. {$ifdef GDB}
  37. strings,gdb,
  38. {$endif GDB}
  39. globtype,systems,
  40. cobjects,verbose,globals,
  41. symconst,symtable,aasm,types,
  42. hcodegen,temp_gen,pass_2,pass_1,
  43. cpubase,cpuasm,
  44. cgai386,tgeni386;
  45. {*****************************************************************************
  46. SecondLoadVMT
  47. *****************************************************************************}
  48. procedure secondloadvmt(var p : ptree);
  49. begin
  50. p^.location.register:=getregister32;
  51. emit_sym_ofs_reg(A_MOV,
  52. S_L,newasmsymbol(pobjectdef(pclassrefdef(p^.resulttype)^.pointertype.def)^.vmt_mangledname),0,
  53. p^.location.register);
  54. end;
  55. {*****************************************************************************
  56. SecondHNewN
  57. *****************************************************************************}
  58. procedure secondhnewn(var p : ptree);
  59. begin
  60. end;
  61. {*****************************************************************************
  62. SecondNewN
  63. *****************************************************************************}
  64. procedure secondnewn(var p : ptree);
  65. var
  66. pushed : tpushed;
  67. r : preference;
  68. begin
  69. if assigned(p^.left) then
  70. begin
  71. secondpass(p^.left);
  72. p^.location.register:=p^.left^.location.register;
  73. end
  74. else
  75. begin
  76. pushusedregisters(pushed,$ff);
  77. gettempofsizereference(target_os.size_of_pointer,p^.location.reference);
  78. { determines the size of the mem block }
  79. push_int(ppointerdef(p^.resulttype)^.pointertype.def^.size);
  80. emit_push_lea_loc(p^.location,false);
  81. emitcall('FPC_GETMEM');
  82. if ppointerdef(p^.resulttype)^.pointertype.def^.needs_inittable then
  83. begin
  84. new(r);
  85. reset_reference(r^);
  86. r^.symbol:=ppointerdef(p^.left^.resulttype)^.pointertype.def^.get_inittable_label;
  87. emitpushreferenceaddr(r^);
  88. dispose(r);
  89. { push pointer we just allocated, we need to initialize the
  90. data located at that pointer not the pointer self (PFV) }
  91. emit_push_loc(p^.location);
  92. emitcall('FPC_INITIALIZE');
  93. end;
  94. popusedregisters(pushed);
  95. { may be load ESI }
  96. maybe_loadesi;
  97. end;
  98. if codegenerror then
  99. exit;
  100. end;
  101. {*****************************************************************************
  102. SecondDisposeN
  103. *****************************************************************************}
  104. procedure secondhdisposen(var p : ptree);
  105. begin
  106. secondpass(p^.left);
  107. if codegenerror then
  108. exit;
  109. reset_reference(p^.location.reference);
  110. case p^.left^.location.loc of
  111. LOC_REGISTER:
  112. p^.location.reference.index:=p^.left^.location.register;
  113. LOC_CREGISTER:
  114. begin
  115. p^.location.reference.index:=getregister32;
  116. emit_reg_reg(A_MOV,S_L,
  117. p^.left^.location.register,
  118. p^.location.reference.index);
  119. end;
  120. LOC_MEM,LOC_REFERENCE :
  121. begin
  122. del_reference(p^.left^.location.reference);
  123. p^.location.reference.index:=getregister32;
  124. emit_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  125. p^.location.reference.index);
  126. end;
  127. end;
  128. end;
  129. {*****************************************************************************
  130. SecondNewDispose
  131. *****************************************************************************}
  132. procedure secondsimplenewdispose(var p : ptree);
  133. var
  134. pushed : tpushed;
  135. r : preference;
  136. begin
  137. secondpass(p^.left);
  138. if codegenerror then
  139. exit;
  140. pushusedregisters(pushed,$ff);
  141. { call the mem handling procedures }
  142. case p^.treetype of
  143. simpledisposen:
  144. begin
  145. if ppointerdef(p^.left^.resulttype)^.pointertype.def^.needs_inittable then
  146. begin
  147. new(r);
  148. reset_reference(r^);
  149. r^.symbol:=ppointerdef(p^.left^.resulttype)^.pointertype.def^.get_inittable_label;
  150. emitpushreferenceaddr(r^);
  151. dispose(r);
  152. { push pointer adress }
  153. emit_push_loc(p^.left^.location);
  154. emitcall('FPC_FINALIZE');
  155. end;
  156. emit_push_lea_loc(p^.left^.location,true);
  157. emitcall('FPC_FREEMEM');
  158. end;
  159. simplenewn:
  160. begin
  161. { determines the size of the mem block }
  162. push_int(ppointerdef(p^.left^.resulttype)^.pointertype.def^.size);
  163. emit_push_lea_loc(p^.left^.location,true);
  164. emitcall('FPC_GETMEM');
  165. if ppointerdef(p^.left^.resulttype)^.pointertype.def^.needs_inittable then
  166. begin
  167. new(r);
  168. reset_reference(r^);
  169. r^.symbol:=ppointerdef(p^.left^.resulttype)^.pointertype.def^.get_inittable_label;
  170. emitpushreferenceaddr(r^);
  171. dispose(r);
  172. emit_push_loc(p^.left^.location);
  173. emitcall('FPC_INITIALIZE');
  174. end;
  175. end;
  176. end;
  177. popusedregisters(pushed);
  178. { may be load ESI }
  179. maybe_loadesi;
  180. end;
  181. {*****************************************************************************
  182. SecondAddr
  183. *****************************************************************************}
  184. procedure secondaddr(var p : ptree);
  185. begin
  186. secondpass(p^.left);
  187. { when loading procvar we do nothing with this node, so load the
  188. location of left }
  189. if p^.procvarload then
  190. begin
  191. set_location(p^.location,p^.left^.location);
  192. exit;
  193. end;
  194. p^.location.loc:=LOC_REGISTER;
  195. del_reference(p^.left^.location.reference);
  196. p^.location.register:=getregister32;
  197. {@ on a procvar means returning an address to the procedure that
  198. is stored in it.}
  199. { yes but p^.left^.symtableentry can be nil
  200. for example on @self !! }
  201. { symtableentry can be also invalid, if left is no tree node }
  202. if (m_tp_procvar in aktmodeswitches) and
  203. (p^.left^.treetype=loadn) and
  204. assigned(p^.left^.symtableentry) and
  205. (p^.left^.symtableentry^.typ=varsym) and
  206. (pvarsym(p^.left^.symtableentry)^.vartype.def^.deftype=procvardef) then
  207. emit_ref_reg(A_MOV,S_L,
  208. newreference(p^.left^.location.reference),
  209. p^.location.register)
  210. else
  211. emit_ref_reg(A_LEA,S_L,
  212. newreference(p^.left^.location.reference),
  213. p^.location.register);
  214. { for use of other segments }
  215. if p^.left^.location.reference.segment<>R_NO then
  216. p^.location.segment:=p^.left^.location.reference.segment;
  217. end;
  218. {*****************************************************************************
  219. SecondDoubleAddr
  220. *****************************************************************************}
  221. procedure seconddoubleaddr(var p : ptree);
  222. begin
  223. secondpass(p^.left);
  224. p^.location.loc:=LOC_REGISTER;
  225. del_reference(p^.left^.location.reference);
  226. p^.location.register:=getregister32;
  227. emit_ref_reg(A_LEA,S_L,
  228. newreference(p^.left^.location.reference),
  229. p^.location.register);
  230. end;
  231. {*****************************************************************************
  232. SecondDeRef
  233. *****************************************************************************}
  234. procedure secondderef(var p : ptree);
  235. var
  236. hr : tregister;
  237. begin
  238. secondpass(p^.left);
  239. reset_reference(p^.location.reference);
  240. case p^.left^.location.loc of
  241. LOC_REGISTER:
  242. p^.location.reference.base:=p^.left^.location.register;
  243. LOC_CREGISTER:
  244. begin
  245. { ... and reserve one for the pointer }
  246. hr:=getregister32;
  247. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hr);
  248. p^.location.reference.base:=hr;
  249. end;
  250. else
  251. begin
  252. { free register }
  253. del_reference(p^.left^.location.reference);
  254. { ...and reserve one for the pointer }
  255. hr:=getregister32;
  256. emit_ref_reg(
  257. A_MOV,S_L,newreference(p^.left^.location.reference),
  258. hr);
  259. p^.location.reference.base:=hr;
  260. end;
  261. end;
  262. if ppointerdef(p^.left^.resulttype)^.is_far then
  263. p^.location.reference.segment:=R_FS;
  264. if not ppointerdef(p^.left^.resulttype)^.is_far and
  265. (cs_gdb_heaptrc in aktglobalswitches) and
  266. (cs_checkpointer in aktglobalswitches) then
  267. begin
  268. emit_reg(
  269. A_PUSH,S_L,p^.location.reference.base);
  270. emitcall('FPC_CHECKPOINTER');
  271. end;
  272. end;
  273. {*****************************************************************************
  274. SecondSubScriptN
  275. *****************************************************************************}
  276. procedure secondsubscriptn(var p : ptree);
  277. var
  278. hr : tregister;
  279. begin
  280. secondpass(p^.left);
  281. if codegenerror then
  282. exit;
  283. { classes must be dereferenced implicit }
  284. if (p^.left^.resulttype^.deftype=objectdef) and
  285. pobjectdef(p^.left^.resulttype)^.is_class then
  286. begin
  287. reset_reference(p^.location.reference);
  288. case p^.left^.location.loc of
  289. LOC_REGISTER:
  290. p^.location.reference.base:=p^.left^.location.register;
  291. LOC_CREGISTER:
  292. begin
  293. { ... and reserve one for the pointer }
  294. hr:=getregister32;
  295. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hr);
  296. p^.location.reference.base:=hr;
  297. end;
  298. else
  299. begin
  300. { free register }
  301. del_reference(p^.left^.location.reference);
  302. { ... and reserve one for the pointer }
  303. hr:=getregister32;
  304. emit_ref_reg(
  305. A_MOV,S_L,newreference(p^.left^.location.reference),
  306. hr);
  307. p^.location.reference.base:=hr;
  308. end;
  309. end;
  310. end
  311. else
  312. set_location(p^.location,p^.left^.location);
  313. inc(p^.location.reference.offset,p^.vs^.address);
  314. end;
  315. {*****************************************************************************
  316. SecondVecN
  317. *****************************************************************************}
  318. procedure secondvecn(var p : ptree);
  319. var
  320. is_pushed : boolean;
  321. ind,hr : tregister;
  322. _p : ptree;
  323. function get_mul_size:longint;
  324. begin
  325. if p^.memindex then
  326. get_mul_size:=1
  327. else
  328. begin
  329. if (p^.left^.resulttype^.deftype=arraydef) then
  330. get_mul_size:=parraydef(p^.left^.resulttype)^.elesize
  331. else
  332. get_mul_size:=p^.resulttype^.size;
  333. end
  334. end;
  335. procedure calc_emit_mul;
  336. var
  337. l1,l2 : longint;
  338. begin
  339. l1:=get_mul_size;
  340. case l1 of
  341. 1,2,4,8 : p^.location.reference.scalefactor:=l1;
  342. else
  343. begin
  344. if ispowerof2(l1,l2) then
  345. emit_const_reg(A_SHL,S_L,l2,ind)
  346. else
  347. emit_const_reg(A_IMUL,S_L,l1,ind);
  348. end;
  349. end;
  350. end;
  351. var
  352. extraoffset : longint;
  353. { rl stores the resulttype of the left node, this is necessary }
  354. { to detect if it is an ansistring }
  355. { because in constant nodes which constant index }
  356. { the left tree is removed }
  357. t : ptree;
  358. hp : preference;
  359. href : treference;
  360. tai : Paicpu;
  361. pushed : tpushed;
  362. hightree : ptree;
  363. hl,otl,ofl : pasmlabel;
  364. begin
  365. secondpass(p^.left);
  366. { we load the array reference to p^.location }
  367. { an ansistring needs to be dereferenced }
  368. if is_ansistring(p^.left^.resulttype) or
  369. is_widestring(p^.left^.resulttype) then
  370. begin
  371. reset_reference(p^.location.reference);
  372. if p^.callunique then
  373. begin
  374. if p^.left^.location.loc<>LOC_REFERENCE then
  375. begin
  376. CGMessage(cg_e_illegal_expression);
  377. exit;
  378. end;
  379. pushusedregisters(pushed,$ff);
  380. emitpushreferenceaddr(p^.left^.location.reference);
  381. if is_ansistring(p^.left^.resulttype) then
  382. emitcall('FPC_ANSISTR_UNIQUE')
  383. else
  384. emitcall('FPC_WIDESTR_UNIQUE');
  385. maybe_loadesi;
  386. popusedregisters(pushed);
  387. end;
  388. if p^.left^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  389. begin
  390. p^.location.reference.base:=p^.left^.location.register;
  391. end
  392. else
  393. begin
  394. del_reference(p^.left^.location.reference);
  395. p^.location.reference.base:=getregister32;
  396. emit_ref_reg(A_MOV,S_L,
  397. newreference(p^.left^.location.reference),
  398. p^.location.reference.base);
  399. end;
  400. { check for a zero length string,
  401. we can use the ansistring routine here }
  402. if (cs_check_range in aktlocalswitches) then
  403. begin
  404. pushusedregisters(pushed,$ff);
  405. emit_reg(A_PUSH,S_L,p^.location.reference.base);
  406. emitcall('FPC_ANSISTR_CHECKZERO');
  407. maybe_loadesi;
  408. popusedregisters(pushed);
  409. end;
  410. if is_ansistring(p^.left^.resulttype) then
  411. { in ansistrings S[1] is pchar(S)[0] !! }
  412. dec(p^.location.reference.offset)
  413. else
  414. begin
  415. { in widestrings S[1] is pwchar(S)[0] !! }
  416. dec(p^.location.reference.offset,2);
  417. emit_const_reg(A_SHL,S_L,
  418. 1,p^.location.reference.base);
  419. end;
  420. { we've also to keep left up-to-date, because it is used }
  421. { if a constant array index occurs, subject to change (FK) }
  422. set_location(p^.left^.location,p^.location);
  423. end
  424. else
  425. set_location(p^.location,p^.left^.location);
  426. { offset can only differ from 0 if arraydef }
  427. if p^.left^.resulttype^.deftype=arraydef then
  428. dec(p^.location.reference.offset,
  429. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  430. if p^.right^.treetype=ordconstn then
  431. begin
  432. { offset can only differ from 0 if arraydef }
  433. if (p^.left^.resulttype^.deftype=arraydef) then
  434. begin
  435. if not(is_open_array(p^.left^.resulttype)) and
  436. not(is_array_of_const(p^.left^.resulttype)) then
  437. begin
  438. if (p^.right^.value>parraydef(p^.left^.resulttype)^.highrange) or
  439. (p^.right^.value<parraydef(p^.left^.resulttype)^.lowrange) then
  440. begin
  441. if (cs_check_range in aktlocalswitches) then
  442. CGMessage(parser_e_range_check_error)
  443. else
  444. CGMessage(parser_w_range_check_error);
  445. end;
  446. dec(p^.left^.location.reference.offset,
  447. get_mul_size*parraydef(p^.left^.resulttype)^.lowrange);
  448. end
  449. else
  450. begin
  451. { range checking for open arrays !!!! }
  452. {!!!!!!!!!!!!!!!!!}
  453. end;
  454. end
  455. else if (p^.left^.resulttype^.deftype=stringdef) then
  456. begin
  457. if (p^.right^.value=0) and not(is_shortstring(p^.left^.resulttype)) then
  458. CGMessage(cg_e_can_access_element_zero);
  459. if (cs_check_range in aktlocalswitches) then
  460. case pstringdef(p^.left^.resulttype)^.string_typ of
  461. { it's the same for ansi- and wide strings }
  462. st_widestring,
  463. st_ansistring:
  464. begin
  465. pushusedregisters(pushed,$ff);
  466. push_int(p^.right^.value);
  467. hp:=newreference(p^.location.reference);
  468. dec(hp^.offset,7);
  469. emit_ref(A_PUSH,S_L,hp);
  470. emitcall('FPC_ANSISTR_RANGECHECK');
  471. popusedregisters(pushed);
  472. maybe_loadesi;
  473. end;
  474. st_shortstring:
  475. begin
  476. {!!!!!!!!!!!!!!!!!}
  477. end;
  478. st_longstring:
  479. begin
  480. {!!!!!!!!!!!!!!!!!}
  481. end;
  482. end;
  483. end;
  484. inc(p^.left^.location.reference.offset,
  485. get_mul_size*p^.right^.value);
  486. if p^.memseg then
  487. p^.left^.location.reference.segment:=R_FS;
  488. p^.left^.resulttype:=p^.resulttype;
  489. disposetree(p^.right);
  490. _p:=p^.left;
  491. putnode(p);
  492. p:=_p;
  493. end
  494. else
  495. { not treetype=ordconstn }
  496. begin
  497. { quick hack, to overcome Delphi 2 }
  498. if (cs_regalloc in aktglobalswitches) and
  499. { if we do range checking, we don't }
  500. { need that fancy code (it would be }
  501. { buggy) }
  502. not(cs_check_range in aktlocalswitches) and
  503. (p^.left^.resulttype^.deftype=arraydef) then
  504. begin
  505. extraoffset:=0;
  506. if (p^.right^.treetype=addn) then
  507. begin
  508. if p^.right^.right^.treetype=ordconstn then
  509. begin
  510. extraoffset:=p^.right^.right^.value;
  511. t:=p^.right^.left;
  512. putnode(p^.right^.right);
  513. putnode(p^.right);
  514. p^.right:=t
  515. end
  516. else if p^.right^.left^.treetype=ordconstn then
  517. begin
  518. extraoffset:=p^.right^.left^.value;
  519. t:=p^.right^.right;
  520. putnode(p^.right^.left);
  521. putnode(p^.right);
  522. p^.right:=t
  523. end;
  524. end
  525. else if (p^.right^.treetype=subn) then
  526. begin
  527. if p^.right^.right^.treetype=ordconstn then
  528. begin
  529. { this was "extraoffset:=p^.right^.right^.value;" Looks a bit like
  530. copy-paste bug :) (JM) }
  531. extraoffset:=-p^.right^.right^.value;
  532. t:=p^.right^.left;
  533. putnode(p^.right^.right);
  534. putnode(p^.right);
  535. p^.right:=t
  536. end
  537. { You also have to negate p^.right^.right in this case! I can't add an
  538. unaryminusn without causing a crash, so I've disabled it (JM)
  539. else if p^.right^.left^.treetype=ordconstn then
  540. begin
  541. extraoffset:=p^.right^.left^.value;
  542. t:=p^.right^.right;
  543. putnode(p^.right);
  544. putnode(p^.right^.left);
  545. p^.right:=t;
  546. end;}
  547. end;
  548. inc(p^.location.reference.offset,
  549. get_mul_size*extraoffset);
  550. end;
  551. { calculate from left to right }
  552. if (p^.location.loc<>LOC_REFERENCE) and
  553. (p^.location.loc<>LOC_MEM) then
  554. CGMessage(cg_e_illegal_expression);
  555. if (p^.right^.location.loc=LOC_JUMP) then
  556. begin
  557. otl:=truelabel;
  558. getlabel(truelabel);
  559. ofl:=falselabel;
  560. getlabel(falselabel);
  561. end;
  562. is_pushed:=maybe_push(p^.right^.registers32,p,false);
  563. secondpass(p^.right);
  564. if is_pushed then
  565. restore(p,false);
  566. { here we change the location of p^.right
  567. and the update was forgotten so it
  568. led to wrong code in emitrangecheck later PM
  569. so make range check before }
  570. if cs_check_range in aktlocalswitches then
  571. begin
  572. if p^.left^.resulttype^.deftype=arraydef then
  573. begin
  574. if is_open_array(p^.left^.resulttype) or
  575. is_array_of_const(p^.left^.resulttype) then
  576. begin
  577. reset_reference(href);
  578. parraydef(p^.left^.resulttype)^.genrangecheck;
  579. href.symbol:=newasmsymbol(parraydef(p^.left^.resulttype)^.getrangecheckstring);
  580. href.offset:=4;
  581. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  582. hightree:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  583. firstpass(hightree);
  584. secondpass(hightree);
  585. emit_mov_loc_ref(hightree^.location,href,S_L,true);
  586. disposetree(hightree);
  587. end;
  588. emitrangecheck(p^.right,p^.left^.resulttype);
  589. end;
  590. end;
  591. case p^.right^.location.loc of
  592. LOC_REGISTER:
  593. begin
  594. ind:=p^.right^.location.register;
  595. case p^.right^.resulttype^.size of
  596. 1:
  597. begin
  598. hr:=reg8toreg32(ind);
  599. emit_reg_reg(A_MOVZX,S_BL,ind,hr);
  600. ind:=hr;
  601. end;
  602. 2:
  603. begin
  604. hr:=reg16toreg32(ind);
  605. emit_reg_reg(A_MOVZX,S_WL,ind,hr);
  606. ind:=hr;
  607. end;
  608. end;
  609. end;
  610. LOC_CREGISTER:
  611. begin
  612. ind:=getregister32;
  613. case p^.right^.resulttype^.size of
  614. 1:
  615. emit_reg_reg(A_MOVZX,S_BL,p^.right^.location.register,ind);
  616. 2:
  617. emit_reg_reg(A_MOVZX,S_WL,p^.right^.location.register,ind);
  618. 4:
  619. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,ind);
  620. end;
  621. end;
  622. LOC_FLAGS:
  623. begin
  624. ind:=getregister32;
  625. emit_flag2reg(p^.right^.location.resflags,reg32toreg8(ind));
  626. emit_reg_reg(A_MOVZX,S_BL,reg32toreg8(ind),ind);
  627. end;
  628. LOC_JUMP :
  629. begin
  630. ind:=getregister32;
  631. emitlab(truelabel);
  632. truelabel:=otl;
  633. emit_const_reg(A_MOV,S_L,1,ind);
  634. getlabel(hl);
  635. emitjmp(C_None,hl);
  636. emitlab(falselabel);
  637. falselabel:=ofl;
  638. emit_reg_reg(A_XOR,S_L,ind,ind);
  639. emitlab(hl);
  640. end;
  641. LOC_REFERENCE,LOC_MEM :
  642. begin
  643. del_reference(p^.right^.location.reference);
  644. ind:=getregister32;
  645. { Booleans are stored in an 8 bit memory location, so
  646. the use of MOVL is not correct }
  647. case p^.right^.resulttype^.size of
  648. 1 : tai:=new(paicpu,op_ref_reg(A_MOVZX,S_BL,newreference(p^.right^.location.reference),ind));
  649. 2 : tai:=new(Paicpu,op_ref_reg(A_MOVZX,S_WL,newreference(p^.right^.location.reference),ind));
  650. 4 : tai:=new(Paicpu,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),ind));
  651. end;
  652. exprasmlist^.concat(tai);
  653. end;
  654. else
  655. internalerror(5913428);
  656. end;
  657. { produce possible range check code: }
  658. if cs_check_range in aktlocalswitches then
  659. begin
  660. if p^.left^.resulttype^.deftype=arraydef then
  661. begin
  662. { done defore (PM) }
  663. end
  664. else if (p^.left^.resulttype^.deftype=stringdef) then
  665. begin
  666. case pstringdef(p^.left^.resulttype)^.string_typ of
  667. { it's the same for ansi- and wide strings }
  668. st_widestring,
  669. st_ansistring:
  670. begin
  671. pushusedregisters(pushed,$ff);
  672. emit_reg(A_PUSH,S_L,ind);
  673. hp:=newreference(p^.location.reference);
  674. dec(hp^.offset,7);
  675. emit_ref(A_PUSH,S_L,hp);
  676. emitcall('FPC_ANSISTR_RANGECHECK');
  677. popusedregisters(pushed);
  678. maybe_loadesi;
  679. end;
  680. st_shortstring:
  681. begin
  682. {!!!!!!!!!!!!!!!!!}
  683. end;
  684. st_longstring:
  685. begin
  686. {!!!!!!!!!!!!!!!!!}
  687. end;
  688. end;
  689. end;
  690. end;
  691. if p^.location.reference.index=R_NO then
  692. begin
  693. p^.location.reference.index:=ind;
  694. calc_emit_mul;
  695. end
  696. else
  697. begin
  698. if p^.location.reference.base=R_NO then
  699. begin
  700. case p^.location.reference.scalefactor of
  701. 2 : emit_const_reg(A_SHL,S_L,1,p^.location.reference.index);
  702. 4 : emit_const_reg(A_SHL,S_L,2,p^.location.reference.index);
  703. 8 : emit_const_reg(A_SHL,S_L,3,p^.location.reference.index);
  704. end;
  705. calc_emit_mul;
  706. p^.location.reference.base:=p^.location.reference.index;
  707. p^.location.reference.index:=ind;
  708. end
  709. else
  710. begin
  711. emit_ref_reg(
  712. A_LEA,S_L,newreference(p^.location.reference),
  713. p^.location.reference.index);
  714. ungetregister32(p^.location.reference.base);
  715. { the symbol offset is loaded, }
  716. { so release the symbol name and set symbol }
  717. { to nil }
  718. p^.location.reference.symbol:=nil;
  719. p^.location.reference.offset:=0;
  720. calc_emit_mul;
  721. p^.location.reference.base:=p^.location.reference.index;
  722. p^.location.reference.index:=ind;
  723. end;
  724. end;
  725. if p^.memseg then
  726. p^.location.reference.segment:=R_FS;
  727. end;
  728. end;
  729. {*****************************************************************************
  730. SecondSelfN
  731. *****************************************************************************}
  732. procedure secondselfn(var p : ptree);
  733. begin
  734. reset_reference(p^.location.reference);
  735. getexplicitregister32(R_ESI);
  736. if (p^.resulttype^.deftype=classrefdef) or
  737. ((p^.resulttype^.deftype=objectdef)
  738. and pobjectdef(p^.resulttype)^.is_class
  739. ) then
  740. p^.location.register:=R_ESI
  741. else
  742. p^.location.reference.base:=R_ESI;
  743. end;
  744. {*****************************************************************************
  745. SecondWithN
  746. *****************************************************************************}
  747. procedure secondwith(var p : ptree);
  748. var
  749. usetemp,with_expr_in_temp : boolean;
  750. {$ifdef GDB}
  751. withstartlabel,withendlabel : pasmlabel;
  752. pp : pchar;
  753. mangled_length : longint;
  754. const
  755. withlevel : longint = 0;
  756. {$endif GDB}
  757. begin
  758. if assigned(p^.left) then
  759. begin
  760. secondpass(p^.left);
  761. if p^.left^.location.reference.segment<>R_NO then
  762. message(parser_e_no_with_for_variable_in_other_segments);
  763. new(p^.withreference);
  764. usetemp:=false;
  765. if (p^.left^.treetype=loadn) and
  766. (p^.left^.symtable=aktprocsym^.definition^.localst) then
  767. begin
  768. { for locals use the local storage }
  769. p^.withreference^:=p^.left^.location.reference;
  770. p^.islocal:=true;
  771. end
  772. else
  773. { call can have happend with a property }
  774. if (p^.left^.resulttype^.deftype=objectdef) and
  775. pobjectdef(p^.left^.resulttype)^.is_class then
  776. begin
  777. {$ifndef noAllocEdi}
  778. getexplicitregister32(R_EDI);
  779. {$endif noAllocEdi}
  780. emit_mov_loc_reg(p^.left^.location,R_EDI);
  781. usetemp:=true;
  782. end
  783. else
  784. begin
  785. {$ifndef noAllocEdi}
  786. getexplicitregister32(R_EDI);
  787. {$endif noAllocEdi}
  788. emit_lea_loc_reg(p^.left^.location,R_EDI,false);
  789. usetemp:=true;
  790. end;
  791. release_loc(p^.left^.location);
  792. { if the with expression is stored in a temp }
  793. { area we must make it persistent and shouldn't }
  794. { release it (FK) }
  795. if (p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  796. istemp(p^.left^.location.reference) then
  797. begin
  798. normaltemptopersistant(p^.left^.location.reference.offset);
  799. with_expr_in_temp:=true;
  800. end
  801. else
  802. with_expr_in_temp:=false;
  803. { if usetemp is set the value must be in %edi }
  804. if usetemp then
  805. begin
  806. gettempofsizereference(4,p^.withreference^);
  807. normaltemptopersistant(p^.withreference^.offset);
  808. { move to temp reference }
  809. emit_reg_ref(A_MOV,S_L,R_EDI,newreference(p^.withreference^));
  810. {$ifndef noAllocEdi}
  811. ungetregister32(R_EDI);
  812. {$endif noAllocEdi}
  813. {$ifdef GDB}
  814. if (cs_debuginfo in aktmoduleswitches) then
  815. begin
  816. inc(withlevel);
  817. getlabel(withstartlabel);
  818. getlabel(withendlabel);
  819. emitlab(withstartlabel);
  820. withdebuglist^.concat(new(pai_stabs,init(strpnew(
  821. '"with'+tostr(withlevel)+':'+tostr(symtablestack^.getnewtypecount)+
  822. '=*'+p^.left^.resulttype^.numberstring+'",'+
  823. tostr(N_LSYM)+',0,0,'+tostr(p^.withreference^.offset)))));
  824. mangled_length:=length(aktprocsym^.definition^.mangledname);
  825. getmem(pp,mangled_length+50);
  826. strpcopy(pp,'192,0,0,'+withstartlabel^.name);
  827. if (target_os.use_function_relative_addresses) then
  828. begin
  829. strpcopy(strend(pp),'-');
  830. strpcopy(strend(pp),aktprocsym^.definition^.mangledname);
  831. end;
  832. withdebuglist^.concat(new(pai_stabn,init(strnew(pp))));
  833. end;
  834. {$endif GDB}
  835. del_reference(p^.left^.location.reference);
  836. end;
  837. { p^.right can be optimize out !!! }
  838. if assigned(p^.right) then
  839. secondpass(p^.right);
  840. if usetemp then
  841. begin
  842. ungetpersistanttemp(p^.withreference^.offset);
  843. {$ifdef GDB}
  844. if (cs_debuginfo in aktmoduleswitches) then
  845. begin
  846. emitlab(withendlabel);
  847. strpcopy(pp,'224,0,0,'+withendlabel^.name);
  848. if (target_os.use_function_relative_addresses) then
  849. begin
  850. strpcopy(strend(pp),'-');
  851. strpcopy(strend(pp),aktprocsym^.definition^.mangledname);
  852. end;
  853. withdebuglist^.concat(new(pai_stabn,init(strnew(pp))));
  854. freemem(pp,mangled_length+50);
  855. dec(withlevel);
  856. end;
  857. {$endif GDB}
  858. end;
  859. if with_expr_in_temp then
  860. ungetpersistanttemp(p^.left^.location.reference.offset);
  861. dispose(p^.withreference);
  862. p^.withreference:=nil;
  863. end;
  864. end;
  865. end.
  866. {
  867. $Log$
  868. Revision 1.75 2000-04-11 20:36:39 florian
  869. * sometimes wrong range checking code for arrays was generated when
  870. using register variables
  871. Revision 1.74 2000/04/01 14:18:44 peter
  872. * use arraydef.elesize instead of elementtype.def.size
  873. Revision 1.73 2000/03/19 11:55:08 peter
  874. * fixed temp ansi handling within array constructor
  875. Revision 1.72 2000/02/18 20:53:14 pierre
  876. * fixes a stabs problem for functions
  877. + includes a stabs local var for with statements
  878. the name is with in lowercase followed by an index
  879. for nested with.
  880. + Withdebuglist added because the stabs declarations of local
  881. var are postponed to end of function.
  882. Revision 1.71 2000/02/09 18:08:33 jonas
  883. * added regallocs for esi
  884. Revision 1.70 2000/02/09 13:22:47 peter
  885. * log truncated
  886. Revision 1.69 2000/01/09 15:19:23 peter
  887. * fixed misplaced getexplicitreg(r_edi) which broke make cycle
  888. Revision 1.68 2000/01/09 12:35:02 jonas
  889. * changed edi allocation to use getexplicitregister32/ungetregister
  890. (adapted tgeni386 a bit for this) and enabled it by default
  891. * fixed very big and stupid bug of mine in cg386mat that broke the
  892. include() code (and make cycle :( ) if you compiled without
  893. -dnewoptimizations
  894. Revision 1.67 2000/01/09 01:44:20 jonas
  895. + (de)allocation info for EDI to fix reported bug on mailinglist.
  896. Also some (de)allocation info for ESI added. Between -dallocEDI
  897. because at this time of the night bugs could easily slip in ;)
  898. Revision 1.66 2000/01/07 01:14:21 peter
  899. * updated copyright to 2000
  900. Revision 1.65 2000/01/04 15:15:50 florian
  901. + added compiler switch $maxfpuregisters
  902. + fixed a small problem in secondvecn
  903. Revision 1.64 2000/01/03 17:10:39 jonas
  904. * fixed "quick hack, to overcome Delphi 2" :)
  905. Revision 1.63 1999/12/01 12:42:32 peter
  906. * fixed bug 698
  907. * removed some notes about unused vars
  908. Revision 1.62 1999/11/30 10:40:43 peter
  909. + ttype, tsymlist
  910. Revision 1.61 1999/11/15 21:54:38 peter
  911. * LOC_JUMP support for vecn
  912. Revision 1.60 1999/11/06 14:34:18 peter
  913. * truncated log to 20 revs
  914. Revision 1.59 1999/10/30 17:35:26 peter
  915. * fpc_freemem fpc_getmem new callings updated
  916. Revision 1.58 1999/09/17 17:14:02 peter
  917. * @procvar fixes for tp mode
  918. * @<id>:= gives now an error
  919. Revision 1.57 1999/09/14 07:59:46 florian
  920. * finally!? fixed
  921. with <function with result in temp> do
  922. My last and also Peter's fix before were wrong :(
  923. Revision 1.56 1999/09/13 20:49:41 florian
  924. * hopefully an error in Peter's previous commit fixed
  925. Revision 1.55 1999/09/10 15:42:50 peter
  926. * fixed with <calln> do
  927. * fixed finalize/initialize call for new/dispose
  928. Revision 1.54 1999/08/25 11:59:46 jonas
  929. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  930. Revision 1.53 1999/08/23 23:49:21 pierre
  931. * hnewn location corrected
  932. Revision 1.52 1999/08/19 13:08:52 pierre
  933. * emit_??? used
  934. Revision 1.51 1999/08/16 23:20:28 peter
  935. * range check for array of const
  936. Revision 1.50 1999/08/14 00:36:05 peter
  937. * array constructor support
  938. }