cg386mem.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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. cutils,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. { First pass processed this with the assumption }
  513. { that there was an add node which may require an }
  514. { extra register. Fake it or die with IE10 (JM) }
  515. t^.registers32 := p^.right^.registers32;
  516. putnode(p^.right^.right);
  517. putnode(p^.right);
  518. p^.right:=t;
  519. end
  520. else if p^.right^.left^.treetype=ordconstn then
  521. begin
  522. extraoffset:=p^.right^.left^.value;
  523. t:=p^.right^.right;
  524. t^.registers32 := p^.right^.registers32;
  525. putnode(p^.right^.left);
  526. putnode(p^.right);
  527. p^.right:=t;
  528. end;
  529. end
  530. else if (p^.right^.treetype=subn) then
  531. begin
  532. if p^.right^.right^.treetype=ordconstn then
  533. begin
  534. { this was "extraoffset:=p^.right^.right^.value;" Looks a bit like
  535. copy-paste bug :) (JM) }
  536. extraoffset:=-p^.right^.right^.value;
  537. t:=p^.right^.left;
  538. t^.registers32 := p^.right^.registers32;
  539. putnode(p^.right^.right);
  540. putnode(p^.right);
  541. p^.right:=t;
  542. end
  543. { You also have to negate p^.right^.right in this case! I can't add an
  544. unaryminusn without causing a crash, so I've disabled it (JM)
  545. else if p^.right^.left^.treetype=ordconstn then
  546. begin
  547. extraoffset:=p^.right^.left^.value;
  548. t:=p^.right^.right;
  549. t^.registers32 := p^.right^.registers32;
  550. putnode(p^.right);
  551. putnode(p^.right^.left);
  552. p^.right:=t;
  553. end;}
  554. end;
  555. inc(p^.location.reference.offset,
  556. get_mul_size*extraoffset);
  557. end;
  558. { calculate from left to right }
  559. if (p^.location.loc<>LOC_REFERENCE) and
  560. (p^.location.loc<>LOC_MEM) then
  561. CGMessage(cg_e_illegal_expression);
  562. if (p^.right^.location.loc=LOC_JUMP) then
  563. begin
  564. otl:=truelabel;
  565. getlabel(truelabel);
  566. ofl:=falselabel;
  567. getlabel(falselabel);
  568. end;
  569. is_pushed:=maybe_push(p^.right^.registers32,p,false);
  570. secondpass(p^.right);
  571. if is_pushed then
  572. restore(p,false);
  573. { here we change the location of p^.right
  574. and the update was forgotten so it
  575. led to wrong code in emitrangecheck later PM
  576. so make range check before }
  577. if cs_check_range in aktlocalswitches then
  578. begin
  579. if p^.left^.resulttype^.deftype=arraydef then
  580. begin
  581. if is_open_array(p^.left^.resulttype) or
  582. is_array_of_const(p^.left^.resulttype) then
  583. begin
  584. reset_reference(href);
  585. parraydef(p^.left^.resulttype)^.genrangecheck;
  586. href.symbol:=newasmsymbol(parraydef(p^.left^.resulttype)^.getrangecheckstring);
  587. href.offset:=4;
  588. getsymonlyin(p^.left^.symtable,'high'+pvarsym(p^.left^.symtableentry)^.name);
  589. hightree:=genloadnode(pvarsym(srsym),p^.left^.symtable);
  590. firstpass(hightree);
  591. secondpass(hightree);
  592. emit_mov_loc_ref(hightree^.location,href,S_L,true);
  593. disposetree(hightree);
  594. end;
  595. emitrangecheck(p^.right,p^.left^.resulttype);
  596. end;
  597. end;
  598. case p^.right^.location.loc of
  599. LOC_REGISTER:
  600. begin
  601. ind:=p^.right^.location.register;
  602. case p^.right^.resulttype^.size of
  603. 1:
  604. begin
  605. hr:=reg8toreg32(ind);
  606. emit_reg_reg(A_MOVZX,S_BL,ind,hr);
  607. ind:=hr;
  608. end;
  609. 2:
  610. begin
  611. hr:=reg16toreg32(ind);
  612. emit_reg_reg(A_MOVZX,S_WL,ind,hr);
  613. ind:=hr;
  614. end;
  615. end;
  616. end;
  617. LOC_CREGISTER:
  618. begin
  619. ind:=getregister32;
  620. case p^.right^.resulttype^.size of
  621. 1:
  622. emit_reg_reg(A_MOVZX,S_BL,p^.right^.location.register,ind);
  623. 2:
  624. emit_reg_reg(A_MOVZX,S_WL,p^.right^.location.register,ind);
  625. 4:
  626. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,ind);
  627. end;
  628. end;
  629. LOC_FLAGS:
  630. begin
  631. ind:=getregister32;
  632. emit_flag2reg(p^.right^.location.resflags,reg32toreg8(ind));
  633. emit_reg_reg(A_MOVZX,S_BL,reg32toreg8(ind),ind);
  634. end;
  635. LOC_JUMP :
  636. begin
  637. ind:=getregister32;
  638. emitlab(truelabel);
  639. truelabel:=otl;
  640. emit_const_reg(A_MOV,S_L,1,ind);
  641. getlabel(hl);
  642. emitjmp(C_None,hl);
  643. emitlab(falselabel);
  644. falselabel:=ofl;
  645. emit_reg_reg(A_XOR,S_L,ind,ind);
  646. emitlab(hl);
  647. end;
  648. LOC_REFERENCE,LOC_MEM :
  649. begin
  650. del_reference(p^.right^.location.reference);
  651. ind:=getregister32;
  652. { Booleans are stored in an 8 bit memory location, so
  653. the use of MOVL is not correct }
  654. case p^.right^.resulttype^.size of
  655. 1 : tai:=new(paicpu,op_ref_reg(A_MOVZX,S_BL,newreference(p^.right^.location.reference),ind));
  656. 2 : tai:=new(Paicpu,op_ref_reg(A_MOVZX,S_WL,newreference(p^.right^.location.reference),ind));
  657. 4 : tai:=new(Paicpu,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),ind));
  658. end;
  659. exprasmlist^.concat(tai);
  660. end;
  661. else
  662. internalerror(5913428);
  663. end;
  664. { produce possible range check code: }
  665. if cs_check_range in aktlocalswitches then
  666. begin
  667. if p^.left^.resulttype^.deftype=arraydef then
  668. begin
  669. { done defore (PM) }
  670. end
  671. else if (p^.left^.resulttype^.deftype=stringdef) then
  672. begin
  673. case pstringdef(p^.left^.resulttype)^.string_typ of
  674. { it's the same for ansi- and wide strings }
  675. st_widestring,
  676. st_ansistring:
  677. begin
  678. pushusedregisters(pushed,$ff);
  679. emit_reg(A_PUSH,S_L,ind);
  680. hp:=newreference(p^.location.reference);
  681. dec(hp^.offset,7);
  682. emit_ref(A_PUSH,S_L,hp);
  683. emitcall('FPC_ANSISTR_RANGECHECK');
  684. popusedregisters(pushed);
  685. maybe_loadesi;
  686. end;
  687. st_shortstring:
  688. begin
  689. {!!!!!!!!!!!!!!!!!}
  690. end;
  691. st_longstring:
  692. begin
  693. {!!!!!!!!!!!!!!!!!}
  694. end;
  695. end;
  696. end;
  697. end;
  698. if p^.location.reference.index=R_NO then
  699. begin
  700. p^.location.reference.index:=ind;
  701. calc_emit_mul;
  702. end
  703. else
  704. begin
  705. if p^.location.reference.base=R_NO then
  706. begin
  707. case p^.location.reference.scalefactor of
  708. 2 : emit_const_reg(A_SHL,S_L,1,p^.location.reference.index);
  709. 4 : emit_const_reg(A_SHL,S_L,2,p^.location.reference.index);
  710. 8 : emit_const_reg(A_SHL,S_L,3,p^.location.reference.index);
  711. end;
  712. calc_emit_mul;
  713. p^.location.reference.base:=p^.location.reference.index;
  714. p^.location.reference.index:=ind;
  715. end
  716. else
  717. begin
  718. emit_ref_reg(
  719. A_LEA,S_L,newreference(p^.location.reference),
  720. p^.location.reference.index);
  721. ungetregister32(p^.location.reference.base);
  722. { the symbol offset is loaded, }
  723. { so release the symbol name and set symbol }
  724. { to nil }
  725. p^.location.reference.symbol:=nil;
  726. p^.location.reference.offset:=0;
  727. calc_emit_mul;
  728. p^.location.reference.base:=p^.location.reference.index;
  729. p^.location.reference.index:=ind;
  730. end;
  731. end;
  732. if p^.memseg then
  733. p^.location.reference.segment:=R_FS;
  734. end;
  735. end;
  736. {*****************************************************************************
  737. SecondSelfN
  738. *****************************************************************************}
  739. procedure secondselfn(var p : ptree);
  740. begin
  741. reset_reference(p^.location.reference);
  742. getexplicitregister32(R_ESI);
  743. if (p^.resulttype^.deftype=classrefdef) or
  744. ((p^.resulttype^.deftype=objectdef)
  745. and pobjectdef(p^.resulttype)^.is_class
  746. ) then
  747. p^.location.register:=R_ESI
  748. else
  749. p^.location.reference.base:=R_ESI;
  750. end;
  751. {*****************************************************************************
  752. SecondWithN
  753. *****************************************************************************}
  754. procedure secondwith(var p : ptree);
  755. var
  756. usetemp,with_expr_in_temp : boolean;
  757. {$ifdef GDB}
  758. withstartlabel,withendlabel : pasmlabel;
  759. pp : pchar;
  760. mangled_length : longint;
  761. const
  762. withlevel : longint = 0;
  763. {$endif GDB}
  764. begin
  765. if assigned(p^.left) then
  766. begin
  767. secondpass(p^.left);
  768. if p^.left^.location.reference.segment<>R_NO then
  769. message(parser_e_no_with_for_variable_in_other_segments);
  770. new(p^.withreference);
  771. usetemp:=false;
  772. if (p^.left^.treetype=loadn) and
  773. (p^.left^.symtable=aktprocsym^.definition^.localst) then
  774. begin
  775. { for locals use the local storage }
  776. p^.withreference^:=p^.left^.location.reference;
  777. p^.islocal:=true;
  778. end
  779. else
  780. { call can have happend with a property }
  781. if (p^.left^.resulttype^.deftype=objectdef) and
  782. pobjectdef(p^.left^.resulttype)^.is_class then
  783. begin
  784. {$ifndef noAllocEdi}
  785. getexplicitregister32(R_EDI);
  786. {$endif noAllocEdi}
  787. emit_mov_loc_reg(p^.left^.location,R_EDI);
  788. usetemp:=true;
  789. end
  790. else
  791. begin
  792. {$ifndef noAllocEdi}
  793. getexplicitregister32(R_EDI);
  794. {$endif noAllocEdi}
  795. emit_lea_loc_reg(p^.left^.location,R_EDI,false);
  796. usetemp:=true;
  797. end;
  798. release_loc(p^.left^.location);
  799. { if the with expression is stored in a temp }
  800. { area we must make it persistent and shouldn't }
  801. { release it (FK) }
  802. if (p^.left^.location.loc in [LOC_MEM,LOC_REFERENCE]) and
  803. istemp(p^.left^.location.reference) then
  804. begin
  805. normaltemptopersistant(p^.left^.location.reference.offset);
  806. with_expr_in_temp:=true;
  807. end
  808. else
  809. with_expr_in_temp:=false;
  810. { if usetemp is set the value must be in %edi }
  811. if usetemp then
  812. begin
  813. gettempofsizereference(4,p^.withreference^);
  814. normaltemptopersistant(p^.withreference^.offset);
  815. { move to temp reference }
  816. emit_reg_ref(A_MOV,S_L,R_EDI,newreference(p^.withreference^));
  817. {$ifndef noAllocEdi}
  818. ungetregister32(R_EDI);
  819. {$endif noAllocEdi}
  820. {$ifdef GDB}
  821. if (cs_debuginfo in aktmoduleswitches) then
  822. begin
  823. inc(withlevel);
  824. getaddrlabel(withstartlabel);
  825. getaddrlabel(withendlabel);
  826. emitlab(withstartlabel);
  827. withdebuglist^.concat(new(pai_stabs,init(strpnew(
  828. '"with'+tostr(withlevel)+':'+tostr(symtablestack^.getnewtypecount)+
  829. '=*'+p^.left^.resulttype^.numberstring+'",'+
  830. tostr(N_LSYM)+',0,0,'+tostr(p^.withreference^.offset)))));
  831. mangled_length:=length(aktprocsym^.definition^.mangledname);
  832. getmem(pp,mangled_length+50);
  833. strpcopy(pp,'192,0,0,'+withstartlabel^.name);
  834. if (target_os.use_function_relative_addresses) then
  835. begin
  836. strpcopy(strend(pp),'-');
  837. strpcopy(strend(pp),aktprocsym^.definition^.mangledname);
  838. end;
  839. withdebuglist^.concat(new(pai_stabn,init(strnew(pp))));
  840. end;
  841. {$endif GDB}
  842. del_reference(p^.left^.location.reference);
  843. end;
  844. { p^.right can be optimize out !!! }
  845. if assigned(p^.right) then
  846. secondpass(p^.right);
  847. if usetemp then
  848. begin
  849. ungetpersistanttemp(p^.withreference^.offset);
  850. {$ifdef GDB}
  851. if (cs_debuginfo in aktmoduleswitches) then
  852. begin
  853. emitlab(withendlabel);
  854. strpcopy(pp,'224,0,0,'+withendlabel^.name);
  855. if (target_os.use_function_relative_addresses) then
  856. begin
  857. strpcopy(strend(pp),'-');
  858. strpcopy(strend(pp),aktprocsym^.definition^.mangledname);
  859. end;
  860. withdebuglist^.concat(new(pai_stabn,init(strnew(pp))));
  861. freemem(pp,mangled_length+50);
  862. dec(withlevel);
  863. end;
  864. {$endif GDB}
  865. end;
  866. if with_expr_in_temp then
  867. ungetpersistanttemp(p^.left^.location.reference.offset);
  868. dispose(p^.withreference);
  869. p^.withreference:=nil;
  870. end;
  871. end;
  872. end.
  873. {
  874. $Log$
  875. Revision 1.6 2000-08-27 16:11:49 peter
  876. * moved some util functions from globals,cobjects to cutils
  877. * splitted files into finput,fmodule
  878. Revision 1.5 2000/07/28 07:38:13 jonas
  879. * refined previous fix (sometimes the number of necessary registers was
  880. overestimated) (merged from fixes branch)
  881. Revision 1.4 2000/07/27 12:41:54 jonas
  882. * fixed internalerror(10) when using -Or and complex arrays (merged
  883. from fixes branch)
  884. Revision 1.3 2000/07/21 15:14:02 jonas
  885. + added is_addr field for labels, if they are only used for getting the address
  886. (e.g. for io checks) and corresponding getaddrlabel() procedure
  887. Revision 1.2 2000/07/13 11:32:35 michael
  888. + removed logs
  889. }